Move gepdump to bin/
[samba-gtk.git] / 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 3 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 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <gen_ndr/ndr_samr_c.h>
30 #include "common/select.h"
31 #include "common/gtk-smb.h"
32 #include <credentials.h>
33
34 /* GtkSelectDomainDialog */
35
36 const char *gtk_select_domain_dialog_get_domain(GtkSelectDomainDialog *d)
37 {
38         return gtk_entry_get_text(GTK_ENTRY(d->entry_domain));
39 }
40
41 static void gtk_select_domain_dialog_init (GtkSelectDomainDialog *select_domain_dialog)
42 {
43         GtkWidget *dialog_vbox1;
44         GtkWidget *hbox1;
45         GtkWidget *label1;
46         GtkWidget *scrolledwindow1;
47         GtkWidget *dialog_action_area1;
48         GtkWidget *cancelbutton1;
49         GtkWidget *okbutton1;
50         GtkCellRenderer *renderer;
51         GtkTreeViewColumn *curcol;
52
53         gtk_window_set_title (GTK_WINDOW (select_domain_dialog), "Select Domain");
54
55         dialog_vbox1 = GTK_DIALOG (select_domain_dialog)->vbox;
56
57         hbox1 = gtk_hbox_new (FALSE, 0);
58         gtk_box_pack_start (GTK_BOX (dialog_vbox1), hbox1, TRUE, TRUE, 0);
59
60         label1 = gtk_label_new ("Domain:");
61         gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
62
63         select_domain_dialog->entry_domain = gtk_entry_new ();
64         gtk_box_pack_start (GTK_BOX (hbox1), select_domain_dialog->entry_domain, TRUE, TRUE, 0);
65
66         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
67         gtk_box_pack_start (GTK_BOX (dialog_vbox1), scrolledwindow1, TRUE, TRUE, 0);
68
69         select_domain_dialog->list_domains = gtk_tree_view_new ();
70         gtk_container_add (GTK_CONTAINER (scrolledwindow1), select_domain_dialog->list_domains);
71
72         curcol = gtk_tree_view_column_new ();
73         gtk_tree_view_column_set_title(curcol, "Name");
74         renderer = gtk_cell_renderer_text_new();
75         gtk_tree_view_column_pack_start(curcol, renderer, TRUE);
76         gtk_tree_view_append_column(GTK_TREE_VIEW(select_domain_dialog->list_domains), curcol);
77         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
78
79         select_domain_dialog->store_domains = gtk_list_store_new(1, G_TYPE_STRING);
80         gtk_tree_view_set_model(GTK_TREE_VIEW(select_domain_dialog->list_domains), GTK_TREE_MODEL(select_domain_dialog->store_domains));
81         g_object_unref(select_domain_dialog->store_domains);
82
83         dialog_action_area1 = GTK_DIALOG (select_domain_dialog)->action_area;
84         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
85
86         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
87         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
88         gtk_widget_set_can_default(cancelbutton1, TRUE);
89
90         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
91         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), okbutton1, GTK_RESPONSE_OK);
92         gtk_widget_show_all(dialog_vbox1);
93         gtk_widget_set_can_default(okbutton1, TRUE);
94 }
95
96 struct policy_handle gtk_select_domain_dialog_get_handle(GtkSelectDomainDialog *d)
97 {
98         struct policy_handle h;
99         
100         
101         /* FIXME */
102         return h;
103 }
104
105 GType gtk_select_domain_dialog_get_type (void)
106 {
107         static GType mytype = 0;
108
109         if (!mytype)
110         {
111                 static const GTypeInfo myinfo =
112                 {
113                         sizeof (GtkSelectDomainDialogClass),
114                         NULL,
115                         NULL,
116                         NULL,
117                         NULL,
118                         NULL,
119                         sizeof(GtkSelectDomainDialog),
120                         0,
121                         (GInstanceInitFunc) gtk_select_domain_dialog_init,
122                 };
123
124                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
125                                                                                  "GtkSelectDomainDialog", &myinfo, 0);
126         }
127
128         return mytype;
129 }
130
131 GtkWidget *gtk_select_domain_dialog_new (struct dcerpc_binding_handle *sam_pipe)
132 {
133         GtkSelectDomainDialog *d = g_object_new(gtk_select_domain_dialog_get_type (), NULL);
134         NTSTATUS status, result;
135         struct samr_SamArray *sam;
136         struct policy_handle handle;
137         uint32_t resume_handle = 0;
138         uint32_t num_entries;
139         int i;
140         TALLOC_CTX *mem_ctx = talloc_init("gtk_select_domain_dialog_new");
141
142         d->sam_pipe = sam_pipe;
143
144         status = dcerpc_samr_Connect(sam_pipe, mem_ctx, 0,
145                 SEC_FLAG_MAXIMUM_ALLOWED, &handle, &result);
146         if (!NT_STATUS_IS_OK(status)) {
147                 gtk_show_ntstatus(NULL, "Running Connect on SAMR", status);
148                 talloc_free(mem_ctx);
149                 return GTK_WIDGET(d);
150         }
151
152         status = dcerpc_samr_EnumDomains(sam_pipe, mem_ctx, &handle, &resume_handle, &sam, -1, &num_entries, &result);
153         if (!NT_STATUS_IS_OK(status)) {
154                 gtk_show_ntstatus(NULL, "Enumerating domains", status);
155         } else if (sam != NULL) {
156                 for (i=0;i<sam->count;i++) {
157                         GtkTreeIter iter;
158                         gtk_list_store_append(d->store_domains, &iter);
159                         gtk_list_store_set (d->store_domains, &iter, 0, sam->entries[i].name.string, -1);
160                 }
161         }
162
163         status = dcerpc_samr_Close(sam_pipe, mem_ctx, &handle, &result);
164         if (!NT_STATUS_IS_OK(status)) {
165                 gtk_show_ntstatus(NULL, "Closing SAMR connection", status);
166                 talloc_free(mem_ctx);
167                 return GTK_WIDGET ( d );
168         }
169
170         talloc_free(mem_ctx);
171
172         return GTK_WIDGET ( d );
173 }
174
175
176 /* GtkSelectHostDialog */
177 const char *gtk_select_host_dialog_get_host (GtkSelectHostDialog *d)
178 {
179         return gtk_entry_get_text(GTK_ENTRY(d->entry_host));
180 }
181
182 static void gtk_select_host_dialog_init (GtkSelectHostDialog *select_host_dialog)
183 {
184         GtkWidget *dialog_vbox2;
185         GtkWidget *hbox2;
186         GtkWidget *label2;
187         GtkWidget *scrolledwindow2;
188         GtkWidget *dialog_action_area2;
189         GtkWidget *cancelbutton2;
190         GtkWidget *okbutton2;
191
192         gtk_window_set_title (GTK_WINDOW (select_host_dialog), "Select Host");
193
194         dialog_vbox2 = GTK_DIALOG (select_host_dialog)->vbox;
195
196         hbox2 = gtk_hbox_new (FALSE, 0);
197         gtk_box_pack_start (GTK_BOX (dialog_vbox2), hbox2, TRUE, TRUE, 0);
198
199         label2 = gtk_label_new ("Host");
200         gtk_box_pack_start (GTK_BOX (hbox2), label2, FALSE, FALSE, 0);
201
202         select_host_dialog->entry_host = gtk_entry_new ();
203         gtk_box_pack_start (GTK_BOX (hbox2), select_host_dialog->entry_host, TRUE, TRUE, 0);
204
205         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
206         gtk_box_pack_start (GTK_BOX (dialog_vbox2), scrolledwindow2, TRUE, TRUE, 0);
207
208         select_host_dialog->tree_host = gtk_tree_view_new ();
209         gtk_container_add (GTK_CONTAINER (scrolledwindow2), select_host_dialog->tree_host);
210
211         select_host_dialog->store_host = gtk_tree_store_new(1, G_TYPE_STRING);
212         gtk_tree_view_set_model(GTK_TREE_VIEW(select_host_dialog->tree_host), GTK_TREE_MODEL(select_host_dialog->store_host));
213         g_object_unref(select_host_dialog->store_host); 
214
215         dialog_action_area2 = GTK_DIALOG (select_host_dialog)->action_area;
216         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area2), GTK_BUTTONBOX_END);
217
218         cancelbutton2 = gtk_button_new_from_stock ("gtk-cancel");
219         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), cancelbutton2, GTK_RESPONSE_CANCEL);
220         gtk_widget_set_can_default(cancelbutton2, TRUE);
221
222         okbutton2 = gtk_button_new_from_stock ("gtk-ok");
223         gtk_widget_show_all (dialog_vbox2);
224         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), okbutton2, GTK_RESPONSE_OK);
225         gtk_widget_set_can_default(okbutton2, TRUE);
226 }
227
228 GType gtk_select_host_dialog_get_type (void)
229 {
230         static GType mytype = 0;
231
232         if (!mytype)
233         {
234                 static const GTypeInfo myinfo =
235                 {
236                         sizeof (GtkSelectHostDialogClass),
237                         NULL,
238                         NULL,
239                         NULL,
240                         NULL,
241                         NULL,
242                         sizeof(GtkSelectHostDialog),
243                         0,
244                         (GInstanceInitFunc) gtk_select_host_dialog_init,
245                 };
246
247                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
248                                                                                  "GtkSelectHostDialog", &myinfo, 0);
249         }
250
251         return mytype;
252 }
253
254 GtkWidget *gtk_select_host_dialog_new (struct dcerpc_binding_handle *sam_pipe)
255 {
256         return GTK_WIDGET ( g_object_new (gtk_select_host_dialog_get_type (), NULL ));
257 }
258
259 /**
260  * Connect to a specific interface, but ask the user 
261  * for information not specified
262  */
263 struct dcerpc_binding_handle *gtk_connect_rpc_interface(TALLOC_CTX *mem_ctx, 
264                                                   struct tevent_context *ev_ctx,
265                                               struct loadparm_context *lp_ctx,
266                                               const struct ndr_interface_table *table)
267 {
268         GtkRpcBindingDialog *d;
269         NTSTATUS status;
270         struct dcerpc_binding_handle *pipe;
271         struct cli_credentials *cred;
272         gint result;
273
274         d = SAMBAGTK_RPC_BINDING_DIALOG(gtk_rpc_binding_dialog_new(NULL));
275         result = gtk_dialog_run(GTK_DIALOG(d));
276
277         if (result != GTK_RESPONSE_ACCEPT) {
278                 gtk_widget_destroy(GTK_WIDGET(d));
279                 return NULL;
280         }
281
282         cred = cli_credentials_init(mem_ctx);
283         cli_credentials_guess(cred, lp_ctx);
284         cli_credentials_set_gtk_callbacks(cred);
285
286         status = dcerpc_pipe_connect_b(mem_ctx, &pipe,
287                                        gtk_rpc_binding_dialog_get_binding(d, mem_ctx),
288                                        table, cred, ev_ctx, lp_ctx);
289
290         if(!NT_STATUS_IS_OK(status)) {
291                 gtk_show_ntstatus(NULL, "While connecting to interface", status);
292                 gtk_widget_destroy(GTK_WIDGET(d));
293                 talloc_free(cred);
294                 return NULL;
295         }
296
297         gtk_widget_destroy(GTK_WIDGET(d));
298         
299         talloc_free(cred);
300
301         return pipe;
302 }