r2920: Support passing a handle to a SAMR pipe to a RPC Binding or Select
[samba.git] / source / gtk / tools / gwsam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    GTK+ SAM frontend
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 "gtk/common/gtk-smb.h"
24
25 struct policy_handle sam_handle;
26 struct dcerpc_pipe *sam_pipe = NULL;
27 struct policy_handle domain_handle;
28 GtkWidget *mainwin;
29 GtkWidget *seldomain;
30 GtkWidget *mnu_disconnect;
31
32 void update_grouplist(void)
33 {
34         if(!sam_pipe) return;
35         //FIXME
36 }
37
38 void update_userlist(void)
39 {
40         NTSTATUS status;
41         struct samr_EnumDomainUsers r;
42         uint32_t resume_handle=0;
43         int i;
44         TALLOC_CTX *mem_ctx;
45
46         if(!sam_pipe) return;
47
48         mem_ctx = talloc_init("update_userlist");
49         r.in.domain_handle = &domain_handle;
50         r.in.resume_handle = &resume_handle;
51         r.in.acct_flags = 0;
52         r.in.max_size = (uint32_t)-1;
53         r.out.resume_handle = &resume_handle;
54
55         status = dcerpc_samr_EnumDomainUsers(sam_pipe, mem_ctx, &r);
56         if (!NT_STATUS_IS_OK(status)) {
57                 gtk_show_ntstatus(mainwin, status);
58                 talloc_destroy(mem_ctx);
59                 return;
60         }
61
62         if (!r.out.sam || r.out.sam->count == 0) {
63                 talloc_destroy(mem_ctx);
64                 return;
65         }
66
67         for (i=0;i<r.out.sam->count;i++) {
68                 printf("Found: %s\n", r.out.sam->entries[i].name.name);
69                 /* FIXME: Query user info */
70
71                 //              if (!test_OpenUser(sam_pipe, mem_ctx, &sam_handle, r.out.sam->entries[i].idx)) {
72                 //                      ret = False;
73                 //              }
74         }
75         talloc_destroy(mem_ctx);
76 }
77
78 void
79 on_new1_activate                       (GtkMenuItem     *menuitem,
80                                                                                 gpointer         user_data)
81 {
82
83 }
84
85 void
86 on_select_domain_activate                       (GtkMenuItem     *menuitem,
87                                                                                                  gpointer         user_data)
88 {
89         GtkSelectDomainDialog *d;
90         gint result;
91         d = GTK_SELECT_DOMAIN_DIALOG(gtk_select_domain_dialog_new(sam_pipe));
92         result = gtk_dialog_run(GTK_DIALOG(d));
93         switch(result) {
94         case GTK_RESPONSE_ACCEPT:
95                 break;
96         default:
97                 gtk_widget_destroy(GTK_WIDGET(d));
98                 return;
99         }
100         domain_handle = gtk_select_domain_dialog_get_handle(d);
101
102         update_userlist();
103         update_grouplist();
104
105         gtk_widget_destroy(GTK_WIDGET(d));
106 }
107
108 void on_connect_activate (GtkMenuItem *menuitem, gpointer user_data)
109 {
110         GtkRpcBindingDialog *d;
111         NTSTATUS status;
112         struct samr_Connect r;
113         TALLOC_CTX *mem_ctx;
114         gint result;
115
116         d = GTK_RPC_BINDING_DIALOG(gtk_rpc_binding_dialog_new(FALSE, NULL));
117         result = gtk_dialog_run(GTK_DIALOG(d));
118         switch(result) {
119         case GTK_RESPONSE_ACCEPT:
120                 break;
121         default:
122                 gtk_widget_destroy(GTK_WIDGET(d));
123                 return;
124         }
125
126         /* If connected, get list of jobs */
127         status = dcerpc_pipe_connect(&sam_pipe, gtk_rpc_binding_dialog_get_binding(d, DCERPC_SAMR_NAME), DCERPC_SAMR_UUID, DCERPC_SAMR_VERSION, lp_workgroup(), gtk_rpc_binding_dialog_get_username(d), gtk_rpc_binding_dialog_get_password(d));
128         if(!NT_STATUS_IS_OK(status)) {
129                 gtk_show_ntstatus(mainwin, status);
130                 sam_pipe = NULL;
131                 gtk_widget_destroy(GTK_WIDGET(d));
132                 return;
133         }
134
135         r.in.system_name = 0;
136         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
137         r.out.connect_handle = &sam_handle;
138
139         mem_ctx = talloc_init("connect");                                                                                                 
140         status = dcerpc_samr_Connect(sam_pipe, mem_ctx, &r);
141         if (!NT_STATUS_IS_OK(status)) {
142                 gtk_show_ntstatus(mainwin, status);
143                 sam_pipe = NULL;
144                 gtk_widget_destroy(GTK_WIDGET(d));
145                 talloc_destroy(mem_ctx);
146                 return;
147         }
148
149         gtk_widget_set_sensitive (seldomain, TRUE);
150         gtk_widget_set_sensitive (mnu_disconnect, TRUE);
151         gtk_window_set_title (GTK_WINDOW (mainwin), talloc_asprintf(mem_ctx, "User Manager - Connected to %s", gtk_rpc_binding_dialog_get_host(d)));
152         gtk_widget_destroy(GTK_WIDGET(d));
153         talloc_destroy(mem_ctx);
154 }
155
156 void on_disconnect_activate (GtkMenuItem *menuitem, gpointer user_data)
157 {
158         gtk_widget_set_sensitive (mnu_disconnect, FALSE);
159         gtk_window_set_title (GTK_WINDOW (mainwin), "User Manager");
160 }
161
162 void
163 on_quit_activate                      (GtkMenuItem     *menuitem,
164                                                                            gpointer         user_data)
165 {
166         if(sam_pipe)dcerpc_pipe_close(sam_pipe);
167         gtk_main_quit();
168 }
169
170
171 void
172 on_account_activate                    (GtkMenuItem     *menuitem,
173                                                                                 gpointer         user_data)
174 {
175         //FIXME
176 }
177
178
179 void
180 on_user_rights_activate                (GtkMenuItem     *menuitem,
181                                                                                 gpointer         user_data)
182 {
183         //FIXME
184 }
185
186
187 void
188 on_audit_activate                      (GtkMenuItem     *menuitem,
189                                                                                 gpointer         user_data)
190 {
191         //FIXME
192 }
193
194
195 void
196 on_trust_relations_activate            (GtkMenuItem     *menuitem,
197                                                                                 gpointer         user_data)
198 {
199         //FIXME
200 }
201
202
203 void
204 on_refresh_activate                    (GtkMenuItem     *menuitem,
205                                                                                 gpointer         user_data)
206 {
207         update_userlist();
208         update_grouplist();
209 }
210
211
212 void
213 on_about_activate                     (GtkMenuItem     *menuitem,
214                                                                            gpointer         user_data)
215 {
216         GtkDialog *aboutwin = GTK_DIALOG(create_gtk_samba_about_dialog("gwsam"));
217         gtk_dialog_run(aboutwin);
218         gtk_widget_destroy(GTK_WIDGET(aboutwin));
219 }
220
221 GtkWidget*
222 create_mainwindow (void)
223 {
224         GtkWidget *vbox1;
225         GtkWidget *mnu_connect;
226         GtkWidget *menubar;
227         GtkWidget *menuitem1;
228         GtkWidget *menuitem1_menu;
229         GtkWidget *new1;
230         GtkWidget *separatormenuitem1;
231         GtkWidget *quit;
232         GtkWidget *policies;
233         GtkWidget *policies_menu;
234         GtkWidget *account;
235         GtkWidget *user_rights;
236         GtkWidget *audit;
237         GtkWidget *separator1;
238         GtkWidget *trust_relations;
239         GtkWidget *menuitem3;
240         GtkWidget *menuitem3_menu;
241         GtkWidget *refresh;
242         GtkWidget *menuitem4;
243         GtkWidget *menuitem4_menu;
244         GtkWidget *about;
245         GtkWidget *vpaned;
246         GtkWidget *scrolledwindow1;
247         GtkWidget *user_list;
248         GtkWidget *scrolledwindow2;
249         GtkWidget *group_list;
250         GtkWidget *statusbar;
251         GtkAccelGroup *accel_group;
252
253         accel_group = gtk_accel_group_new ();
254
255         mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
256         gtk_window_set_default_size (GTK_WINDOW (mainwin), 642, 562);
257         gtk_window_set_title (GTK_WINDOW (mainwin), "User Manager");
258
259         vbox1 = gtk_vbox_new (FALSE, 0);
260         gtk_widget_show (vbox1);
261         gtk_container_add (GTK_CONTAINER (mainwin), vbox1);
262
263         menubar = gtk_menu_bar_new ();
264         gtk_widget_show (menubar);
265         gtk_box_pack_start (GTK_BOX (vbox1), menubar, FALSE, FALSE, 0);
266
267         menuitem1 = gtk_menu_item_new_with_mnemonic ("_File");
268         gtk_widget_show (menuitem1);
269         gtk_container_add (GTK_CONTAINER (menubar), menuitem1);
270
271         menuitem1_menu = gtk_menu_new ();
272         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);
273
274         mnu_connect = gtk_menu_item_new_with_mnemonic ("_Connect");
275         gtk_widget_show (mnu_connect);
276         gtk_container_add (GTK_CONTAINER (menuitem1_menu), mnu_connect);
277
278         mnu_disconnect = gtk_menu_item_new_with_mnemonic ("_Disconnect");
279         gtk_widget_show (mnu_disconnect);
280         gtk_widget_set_sensitive (mnu_disconnect, FALSE);
281         gtk_container_add (GTK_CONTAINER (menuitem1_menu), mnu_disconnect);
282
283         seldomain = gtk_menu_item_new_with_mnemonic("_Select Domain");
284         gtk_widget_show(seldomain);
285         gtk_widget_set_sensitive (seldomain, FALSE);
286         gtk_container_add (GTK_CONTAINER (menuitem1_menu), seldomain);
287
288         separatormenuitem1 = gtk_separator_menu_item_new ();
289         gtk_widget_show (separatormenuitem1);
290         gtk_container_add (GTK_CONTAINER (menuitem1_menu), separatormenuitem1);
291         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
292
293         quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
294         gtk_widget_show (quit);
295         gtk_container_add (GTK_CONTAINER (menuitem1_menu), quit);
296
297         menuitem1 = gtk_menu_item_new_with_mnemonic ("_User");
298         gtk_widget_show (menuitem1);
299         gtk_container_add (GTK_CONTAINER (menubar), menuitem1);
300         gtk_widget_set_sensitive (menuitem1, FALSE);
301
302         menuitem1_menu = gtk_menu_new ();
303         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);
304
305         new1 = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
306         gtk_widget_show (new1);
307         gtk_container_add (GTK_CONTAINER (menuitem1_menu), new1);
308
309
310
311         policies = gtk_menu_item_new_with_mnemonic ("_Policies");
312         gtk_widget_show (policies);
313         gtk_container_add (GTK_CONTAINER (menubar), policies);
314         gtk_widget_set_sensitive (policies, FALSE);
315
316         policies_menu = gtk_menu_new ();
317         gtk_menu_item_set_submenu (GTK_MENU_ITEM (policies), policies_menu);
318
319         account = gtk_menu_item_new_with_mnemonic ("_Account...");
320         gtk_widget_show (account);
321         gtk_container_add (GTK_CONTAINER (policies_menu), account);
322
323         user_rights = gtk_menu_item_new_with_mnemonic ("_User Rights...");
324         gtk_widget_show (user_rights);
325         gtk_container_add (GTK_CONTAINER (policies_menu), user_rights);
326
327         audit = gtk_menu_item_new_with_mnemonic ("A_udit...");
328         gtk_widget_show (audit);
329         gtk_container_add (GTK_CONTAINER (policies_menu), audit);
330
331         separator1 = gtk_separator_menu_item_new ();
332         gtk_widget_show (separator1);
333         gtk_container_add (GTK_CONTAINER (policies_menu), separator1);
334         gtk_widget_set_sensitive (separator1, FALSE);
335
336         trust_relations = gtk_menu_item_new_with_mnemonic ("_Trust relations");
337         gtk_widget_show (trust_relations);
338         gtk_container_add (GTK_CONTAINER (policies_menu), trust_relations);
339
340         menuitem3 = gtk_menu_item_new_with_mnemonic ("_View");
341         gtk_widget_show (menuitem3);
342         gtk_container_add (GTK_CONTAINER (menubar), menuitem3);
343
344         menuitem3_menu = gtk_menu_new ();
345         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem3), menuitem3_menu);
346
347         refresh = gtk_image_menu_item_new_from_stock ("gtk-refresh", accel_group);
348         gtk_widget_show (refresh);
349         gtk_container_add (GTK_CONTAINER (menuitem3_menu), refresh);
350         gtk_widget_set_sensitive (refresh, FALSE);
351
352         menuitem4 = gtk_menu_item_new_with_mnemonic ("_Help");
353         gtk_widget_show (menuitem4);
354         gtk_container_add (GTK_CONTAINER (menubar), menuitem4);
355
356         menuitem4_menu = gtk_menu_new ();
357         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);
358
359         about = gtk_menu_item_new_with_mnemonic ("_About");
360         gtk_widget_show (about);
361         gtk_container_add (GTK_CONTAINER (menuitem4_menu), about);
362
363         vpaned = gtk_vpaned_new ();
364         gtk_widget_show (vpaned);
365         gtk_box_pack_start (GTK_BOX (vbox1), vpaned, TRUE, TRUE, 0);
366
367         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
368         gtk_widget_show (scrolledwindow1);
369         gtk_paned_pack1 (GTK_PANED (vpaned), scrolledwindow1, FALSE, TRUE);
370
371         user_list = gtk_tree_view_new ();
372         gtk_widget_show (user_list);
373         gtk_container_add (GTK_CONTAINER (scrolledwindow1), user_list);
374
375         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
376         gtk_widget_show (scrolledwindow2);
377         gtk_paned_pack2 (GTK_PANED (vpaned), scrolledwindow2, TRUE, TRUE);
378
379         group_list = gtk_tree_view_new ();
380         gtk_widget_show (group_list);
381         gtk_container_add (GTK_CONTAINER (scrolledwindow2), group_list);
382
383         statusbar = gtk_statusbar_new ();
384         gtk_widget_show (statusbar);
385         gtk_box_pack_start (GTK_BOX (vbox1), statusbar, FALSE, FALSE, 0);
386
387         g_signal_connect ((gpointer) new1, "activate",
388                                           G_CALLBACK (on_new1_activate),
389                                           NULL);
390         g_signal_connect ((gpointer) seldomain, "activate",
391                                           G_CALLBACK (on_select_domain_activate),
392                                           NULL);
393         g_signal_connect ((gpointer) mnu_connect, "activate",
394                                           G_CALLBACK (on_connect_activate),
395                                           NULL);
396         g_signal_connect ((gpointer) mnu_disconnect, "activate",
397                                           G_CALLBACK (on_disconnect_activate),
398                                           NULL);
399         g_signal_connect ((gpointer) quit, "activate",
400                                           G_CALLBACK (on_quit_activate),
401                                           NULL);
402         g_signal_connect ((gpointer) account, "activate",
403                                           G_CALLBACK (on_account_activate),
404                                           NULL);
405         g_signal_connect ((gpointer) user_rights, "activate",
406                                           G_CALLBACK (on_user_rights_activate),
407                                           NULL);
408         g_signal_connect ((gpointer) audit, "activate",
409                                           G_CALLBACK (on_audit_activate),
410                                           NULL);
411         g_signal_connect ((gpointer) trust_relations, "activate",
412                                           G_CALLBACK (on_trust_relations_activate),
413                                           NULL);
414         g_signal_connect ((gpointer) refresh, "activate",
415                                           G_CALLBACK (on_refresh_activate),
416                                           NULL);
417         g_signal_connect ((gpointer) about, "activate",
418                                           G_CALLBACK (on_about_activate),
419                                           NULL);
420
421         gtk_window_add_accel_group (GTK_WINDOW (mainwin), accel_group);
422
423         return mainwin;
424 }
425
426
427
428  int main(int argc, char **argv)
429 {
430         lp_load(dyn_CONFIGFILE,True,False,False);
431         load_interfaces();
432
433         gtk_init(&argc, &argv);
434         mainwin = create_mainwindow();
435         gtk_widget_show(mainwin);
436         gtk_main();
437
438         return 0;
439 }