5a0480ab28cd778a5dea10e038f529a704b6e130
[ddiss/samba.git] / source4 / gtk / common / select.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB-related GTK+ functions
4    
5    Copyright (C) Jelmer Vernooij 2004
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_samr_c.h"
24 #include "gtk/common/select.h"
25 #include "gtk/common/gtk-smb.h"
26 #include "auth/credentials/credentials.h"
27
28 /* GtkSelectDomainDialog */
29
30 const char *gtk_select_domain_dialog_get_domain(GtkSelectDomainDialog *d)
31 {
32         return gtk_entry_get_text(GTK_ENTRY(d->entry_domain));
33 }
34
35 static void gtk_select_domain_dialog_init (GtkSelectDomainDialog *select_domain_dialog)
36 {
37         GtkWidget *dialog_vbox1;
38         GtkWidget *hbox1;
39         GtkWidget *label1;
40         GtkWidget *scrolledwindow1;
41         GtkWidget *dialog_action_area1;
42         GtkWidget *cancelbutton1;
43         GtkWidget *okbutton1;
44         GtkCellRenderer *renderer;
45         GtkTreeViewColumn *curcol;
46
47         gtk_window_set_title (GTK_WINDOW (select_domain_dialog), "Select Domain");
48
49         dialog_vbox1 = GTK_DIALOG (select_domain_dialog)->vbox;
50
51         hbox1 = gtk_hbox_new (FALSE, 0);
52         gtk_box_pack_start (GTK_BOX (dialog_vbox1), hbox1, TRUE, TRUE, 0);
53
54         label1 = gtk_label_new ("Domain:");
55         gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
56
57         select_domain_dialog->entry_domain = gtk_entry_new ();
58         gtk_box_pack_start (GTK_BOX (hbox1), select_domain_dialog->entry_domain, TRUE, TRUE, 0);
59
60         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
61         gtk_box_pack_start (GTK_BOX (dialog_vbox1), scrolledwindow1, TRUE, TRUE, 0);
62
63         select_domain_dialog->list_domains = gtk_tree_view_new ();
64         gtk_container_add (GTK_CONTAINER (scrolledwindow1), select_domain_dialog->list_domains);
65
66         curcol = gtk_tree_view_column_new ();
67         gtk_tree_view_column_set_title(curcol, "Name");
68         renderer = gtk_cell_renderer_text_new();
69         gtk_tree_view_column_pack_start(curcol, renderer, True);
70         gtk_tree_view_append_column(GTK_TREE_VIEW(select_domain_dialog->list_domains), curcol);
71         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
72
73         select_domain_dialog->store_domains = gtk_list_store_new(1, G_TYPE_STRING);
74         gtk_tree_view_set_model(GTK_TREE_VIEW(select_domain_dialog->list_domains), GTK_TREE_MODEL(select_domain_dialog->store_domains));
75         g_object_unref(select_domain_dialog->store_domains);
76
77         dialog_action_area1 = GTK_DIALOG (select_domain_dialog)->action_area;
78         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
79
80         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
81         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
82         GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
83
84         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
85         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), okbutton1, GTK_RESPONSE_OK);
86         gtk_widget_show_all(dialog_vbox1);
87         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
88 }
89
90 struct policy_handle gtk_select_domain_dialog_get_handle(GtkSelectDomainDialog *d)
91 {
92         struct policy_handle h;
93         
94         
95         /* FIXME */
96         return h;
97 }
98
99 GType gtk_select_domain_dialog_get_type (void)
100 {
101         static GType mytype = 0;
102
103         if (!mytype)
104         {
105                 static const GTypeInfo myinfo =
106                 {
107                         sizeof (GtkSelectDomainDialogClass),
108                         NULL,
109                         NULL,
110                         NULL,
111                         NULL,
112                         NULL,
113                         sizeof(GtkSelectDomainDialog),
114                         0,
115                         (GInstanceInitFunc) gtk_select_domain_dialog_init,
116                 };
117
118                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
119                                                                                  "GtkSelectDomainDialog", &myinfo, 0);
120         }
121
122         return mytype;
123 }
124                                                                                                                              
125 GtkWidget *gtk_select_domain_dialog_new (struct dcerpc_pipe *sam_pipe)
126 {
127         GtkSelectDomainDialog *d = g_object_new(gtk_select_domain_dialog_get_type (), NULL);
128         NTSTATUS status;
129         struct samr_EnumDomains r;
130         struct samr_Connect cr;
131         struct samr_Close dr;
132         struct policy_handle handle;
133         uint32_t resume_handle = 0;
134         int i;
135         TALLOC_CTX *mem_ctx = talloc_init("gtk_select_domain_dialog_new");
136
137         d->sam_pipe = sam_pipe;
138
139         cr.in.system_name = 0;
140         cr.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
141         cr.out.connect_handle = &handle;
142
143         status = dcerpc_samr_Connect(sam_pipe, mem_ctx, &cr);
144         if (!NT_STATUS_IS_OK(status)) {
145                 gtk_show_ntstatus(NULL, "Running Connect on SAMR", status);
146                 talloc_free(mem_ctx);
147                 return GTK_WIDGET(d);
148         }
149
150         r.in.connect_handle = &handle;
151         r.in.resume_handle = &resume_handle;
152         r.in.buf_size = (uint32_t)-1;
153         r.out.resume_handle = &resume_handle;
154
155         status = dcerpc_samr_EnumDomains(sam_pipe, mem_ctx, &r);
156         if (!NT_STATUS_IS_OK(status)) {
157                 gtk_show_ntstatus(NULL, "Enumerating domains", status);
158         } else if (r.out.sam) {
159                 for (i=0;i<r.out.sam->count;i++) {
160                         GtkTreeIter iter;
161                         gtk_list_store_append(d->store_domains, &iter);
162                         gtk_list_store_set (d->store_domains, &iter, 0, r.out.sam->entries[i].name.string, -1);
163                 }
164         }
165
166         dr.in.handle = &handle;
167         dr.out.handle = &handle;
168
169         status = dcerpc_samr_Close(sam_pipe, mem_ctx, &dr);
170         if (!NT_STATUS_IS_OK(status)) {
171                 gtk_show_ntstatus(NULL, "Closing SAMR connection", status);
172                 talloc_free(mem_ctx);
173                 return GTK_WIDGET ( d );
174         }
175
176         talloc_free(mem_ctx);
177
178         return GTK_WIDGET ( d );
179 }
180
181
182 /* GtkSelectHostDialog */
183 const char *gtk_select_host_dialog_get_host (GtkSelectHostDialog *d)
184 {
185         return gtk_entry_get_text(GTK_ENTRY(d->entry_host));
186 }
187
188 static void gtk_select_host_dialog_init (GtkSelectHostDialog *select_host_dialog)
189 {
190         GtkWidget *dialog_vbox2;
191         GtkWidget *hbox2;
192         GtkWidget *label2;
193         GtkWidget *scrolledwindow2;
194         GtkWidget *dialog_action_area2;
195         GtkWidget *cancelbutton2;
196         GtkWidget *okbutton2;
197
198         gtk_window_set_title (GTK_WINDOW (select_host_dialog), "Select Host");
199
200         dialog_vbox2 = GTK_DIALOG (select_host_dialog)->vbox;
201
202         hbox2 = gtk_hbox_new (FALSE, 0);
203         gtk_box_pack_start (GTK_BOX (dialog_vbox2), hbox2, TRUE, TRUE, 0);
204
205         label2 = gtk_label_new ("Host");
206         gtk_box_pack_start (GTK_BOX (hbox2), label2, FALSE, FALSE, 0);
207
208         select_host_dialog->entry_host = gtk_entry_new ();
209         gtk_box_pack_start (GTK_BOX (hbox2), select_host_dialog->entry_host, TRUE, TRUE, 0);
210
211         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
212         gtk_box_pack_start (GTK_BOX (dialog_vbox2), scrolledwindow2, TRUE, TRUE, 0);
213
214         select_host_dialog->tree_host = gtk_tree_view_new ();
215         gtk_container_add (GTK_CONTAINER (scrolledwindow2), select_host_dialog->tree_host);
216
217         select_host_dialog->store_host = gtk_tree_store_new(1, G_TYPE_STRING);
218         gtk_tree_view_set_model(GTK_TREE_VIEW(select_host_dialog->tree_host), GTK_TREE_MODEL(select_host_dialog->store_host));
219         g_object_unref(select_host_dialog->store_host); 
220
221         dialog_action_area2 = GTK_DIALOG (select_host_dialog)->action_area;
222         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area2), GTK_BUTTONBOX_END);
223
224         cancelbutton2 = gtk_button_new_from_stock ("gtk-cancel");
225         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), cancelbutton2, GTK_RESPONSE_CANCEL);
226         GTK_WIDGET_SET_FLAGS (cancelbutton2, GTK_CAN_DEFAULT);
227
228         okbutton2 = gtk_button_new_from_stock ("gtk-ok");
229         gtk_widget_show_all (dialog_vbox2);
230         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), okbutton2, GTK_RESPONSE_OK);
231         GTK_WIDGET_SET_FLAGS (okbutton2, GTK_CAN_DEFAULT);
232 }
233
234 GType gtk_select_host_dialog_get_type (void)
235 {
236         static GType mytype = 0;
237
238         if (!mytype)
239         {
240                 static const GTypeInfo myinfo =
241                 {
242                         sizeof (GtkSelectHostDialogClass),
243                         NULL,
244                         NULL,
245                         NULL,
246                         NULL,
247                         NULL,
248                         sizeof(GtkSelectHostDialog),
249                         0,
250                         (GInstanceInitFunc) gtk_select_host_dialog_init,
251                 };
252
253                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
254                                                                                  "GtkSelectHostDialog", &myinfo, 0);
255         }
256
257         return mytype;
258 }
259                                                                                                                              
260 GtkWidget *gtk_select_host_dialog_new (struct dcerpc_pipe *sam_pipe)
261 {
262         return GTK_WIDGET ( g_object_new (gtk_select_host_dialog_get_type (), NULL ));
263 }
264
265 /**
266  * Connect to a specific interface, but ask the user 
267  * for information not specified
268  */
269 struct dcerpc_pipe *gtk_connect_rpc_interface(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *table)
270 {
271         GtkRpcBindingDialog *d;
272         NTSTATUS status;
273         struct dcerpc_pipe *pipe;
274         struct cli_credentials *cred;
275         gint result;
276
277         d = GTK_RPC_BINDING_DIALOG(gtk_rpc_binding_dialog_new(NULL));
278         result = gtk_dialog_run(GTK_DIALOG(d));
279
280         if (result != GTK_RESPONSE_ACCEPT) {
281                 gtk_widget_destroy(GTK_WIDGET(d));
282                 return NULL;
283         }
284
285         cred = cli_credentials_init(mem_ctx);
286         cli_credentials_guess(cred);
287         cli_credentials_set_gtk_callbacks(cred);
288
289         status = dcerpc_pipe_connect_b(mem_ctx, &pipe,
290                                        gtk_rpc_binding_dialog_get_binding(d, mem_ctx),
291                                        table, cred, NULL);
292
293         if(!NT_STATUS_IS_OK(status)) {
294                 gtk_show_ntstatus(NULL, "While connecting to interface", status);
295                 gtk_widget_destroy(GTK_WIDGET(d));
296                 talloc_free(cred);
297                 return NULL;
298         }
299
300         gtk_widget_destroy(GTK_WIDGET(d));
301         
302         talloc_free(cred);
303
304         return pipe;
305 }