r6600: Rework of the GTK credentials system; the credentials information is
[samba.git] / source / gtk / tools / gepdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    GTK+ Endpoint Mapper 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 "dynconfig.h"
24 #include "librpc/gen_ndr/ndr_epmapper.h"
25 #include "librpc/gen_ndr/ndr_mgmt.h"
26 #include "gtk/common/select.h"
27 #include "gtk/common/gtk-smb.h"
28 #include "lib/cmdline/popt_common.h"
29
30 /* 
31  * Show: 
32  *  - RPC statistics
33  *  - Available interfaces
34  *   - Per interface: available endpoints
35  *   - Per interface auth details
36  */
37
38 static GtkWidget *mainwin;
39 static GtkTreeStore *store_eps;
40 static GtkWidget *table_statistics;
41 static GtkWidget *lbl_calls_in, *lbl_calls_out, *lbl_pkts_in, *lbl_pkts_out;
42 static GtkWidget *lbl_iface_version, *lbl_iface_uuid, *lbl_iface_name;
43 static GtkListStore *store_princ_names;
44 static GtkWidget *mnu_refresh;
45 TALLOC_CTX *eps_ctx = NULL;
46 TALLOC_CTX *conn_ctx = NULL;
47
48 struct dcerpc_pipe *epmapper_pipe;
49 struct dcerpc_pipe *mgmt_pipe;
50
51 static void on_quit1_activate (GtkMenuItem *menuitem, gpointer user_data)
52 {
53         gtk_main_quit();
54 }
55
56
57 static void on_about1_activate (GtkMenuItem *menuitem, gpointer user_data)
58 {
59         GtkDialog *aboutwin = GTK_DIALOG(create_gtk_samba_about_dialog("gepdump"));
60         gtk_dialog_run(aboutwin);
61         gtk_widget_destroy(GTK_WIDGET(aboutwin));
62 }
63
64 static const char *get_protocol_name(enum epm_protocol protocol)
65 {
66         switch (protocol) {
67         case EPM_PROTOCOL_UUID: return "UUID";
68         case EPM_PROTOCOL_NCACN: return "NCACN";
69         case EPM_PROTOCOL_NCALRPC: return "NCALRPC";
70         case EPM_PROTOCOL_NCADG: return "NCADG";
71         case EPM_PROTOCOL_IP: return "IP";
72         case EPM_PROTOCOL_TCP: return "TCP";
73         case EPM_PROTOCOL_NETBIOS: return "NetBIOS";
74         case EPM_PROTOCOL_SMB: return "SMB";
75         case EPM_PROTOCOL_PIPE: return "PIPE";
76         case EPM_PROTOCOL_UNIX_DS: return "Unix";
77         default: return "Unknown";
78         }
79 }
80
81 static void add_epm_entry(TALLOC_CTX *mem_ctx, const char *annotation, struct epm_tower *t)
82 {
83         struct dcerpc_binding *bd;
84         int i;
85         NTSTATUS status;
86         GtkTreeIter toweriter;
87
88         status = dcerpc_binding_from_tower(mem_ctx, t, &bd);
89         if (!NT_STATUS_IS_OK(status)) {
90                 gtk_show_ntstatus(mainwin, "Error creating binding from tower", status);
91                 return;
92         }
93         
94         /* Don't show UUID's */
95         ZERO_STRUCT(bd->object);
96
97         gtk_tree_store_append(store_eps, &toweriter, NULL);
98         gtk_tree_store_set(store_eps, &toweriter, 0, strdup(annotation), 1, strdup(dcerpc_binding_string(mem_ctx, bd)), 2, t, -1);
99
100         for (i = 0; i < t->num_floors; i++) {
101                 const char *data;
102                 struct GUID if_uuid;
103                 uint16_t if_version;
104                 GtkTreeIter iter;
105                 gtk_tree_store_append(store_eps, &iter, &toweriter);
106
107                 dcerpc_floor_get_lhs_data(&t->floors[i], &if_uuid, &if_version);
108                 if (t->floors[i].lhs.protocol == EPM_PROTOCOL_UUID) {
109                         data = GUID_string(mem_ctx, &if_uuid);
110                 } else {
111                         data = dcerpc_floor_get_rhs_data(mem_ctx, &t->floors[i]);
112                 }
113                 
114                 gtk_tree_store_set(store_eps, &iter, 0, get_protocol_name(t->floors[i].lhs.protocol), 1, data, -1);
115         }
116 }
117
118 static void refresh_eps(void)
119 {
120         NTSTATUS status;
121         struct epm_Lookup r;
122         struct GUID uuid;
123         struct rpc_if_id_t iface;
124         struct policy_handle handle;
125         TALLOC_CTX *mem_ctx = talloc_init("dump");
126
127         talloc_free(eps_ctx);
128
129         ZERO_STRUCT(handle);
130
131         r.in.inquiry_type = 0;
132         r.in.object = &uuid;
133         r.in.interface_id = &iface;
134         r.in.vers_option = 0;
135         r.in.entry_handle = r.out.entry_handle = &handle;
136         r.in.max_ents = 10;
137
138         gtk_tree_store_clear(store_eps);
139
140         eps_ctx = talloc_init("current endpoint list data");
141
142         do {
143                 int i;
144                 ZERO_STRUCT(uuid);
145                 ZERO_STRUCT(iface);
146
147                 status = dcerpc_epm_Lookup(epmapper_pipe, eps_ctx, &r);
148                 if (!NT_STATUS_IS_OK(status) || r.out.result != 0) {
149                         break;
150                 }
151                 for (i=0;i<r.out.num_ents;i++) {
152                         add_epm_entry(mem_ctx, r.out.entries[i].annotation, &r.out.entries[i].tower->tower);
153                 }
154
155         } while (NT_STATUS_IS_OK(status) && 
156                  r.out.result == 0 && 
157                  r.out.num_ents == r.in.max_ents);
158
159         if (!NT_STATUS_IS_OK(status)) {
160                 gtk_show_ntstatus(mainwin, "Error adding endpoint mapper entry", status);
161                 talloc_free(mem_ctx);
162                 return;
163         }
164         talloc_free(mem_ctx);
165 }
166
167 static void on_refresh_clicked (GtkButton *btn, gpointer user_data)
168 {
169         refresh_eps();
170 }
171
172 static void on_connect_clicked(GtkButton *btn, gpointer         user_data)
173 {
174         GtkRpcBindingDialog *d;
175         const char *bs;
176         TALLOC_CTX *mem_ctx;
177         NTSTATUS status;
178         gint result;
179         struct cli_credentials *credentials;
180
181         d = GTK_RPC_BINDING_DIALOG(gtk_rpc_binding_dialog_new(NULL));
182         result = gtk_dialog_run(GTK_DIALOG(d));
183         switch(result) {
184         case GTK_RESPONSE_ACCEPT:
185                 break;
186         default:
187                 gtk_widget_destroy(GTK_WIDGET(d));
188                 return;
189         }
190
191         mem_ctx = talloc_init("connect");
192         bs = gtk_rpc_binding_dialog_get_binding_string (d, mem_ctx);
193
194         credentials = cli_credentials_init(mem_ctx);
195         cli_credentials_guess(credentials);
196         cli_credentials_set_gtk_callbacks(credentials);
197
198         status = dcerpc_pipe_connect(talloc_autofree_context(), &epmapper_pipe, bs, 
199                                      DCERPC_EPMAPPER_UUID, DCERPC_EPMAPPER_VERSION, 
200                                      credentials);
201
202         if (NT_STATUS_IS_ERR(status)) {
203                 gtk_show_ntstatus(mainwin, "Error connecting to endpoint mapper", status);
204                 goto fail;
205         }
206         
207         gtk_widget_set_sensitive( mnu_refresh, True );
208
209         refresh_eps();
210
211         status = dcerpc_secondary_context(epmapper_pipe, &mgmt_pipe, DCERPC_MGMT_UUID, DCERPC_MGMT_VERSION);
212
213         if (NT_STATUS_IS_ERR(status)) {
214                 mgmt_pipe = NULL;
215                 gtk_show_ntstatus(NULL, "Error connecting to mgmt interface over secondary connection", status);
216                 goto fail;
217         }
218
219 fail:
220         gtk_widget_destroy(GTK_WIDGET(d));
221 }
222
223 static gboolean on_eps_select(GtkTreeSelection *selection,
224     GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer data)
225 {
226         NTSTATUS status;
227         TALLOC_CTX *mem_ctx;
228         
229         if (mgmt_pipe == NULL) 
230                 return FALSE;
231         
232         mem_ctx = talloc_init("eps");
233
234         {
235                 /* Do an InqStats call */
236                 struct mgmt_inq_stats r;
237
238                 r.in.max_count = MGMT_STATS_ARRAY_MAX_SIZE;
239                 r.in.unknown = 0;
240
241                 status = dcerpc_mgmt_inq_stats(mgmt_pipe, mem_ctx, &r);
242                 if (NT_STATUS_IS_ERR(status)) {
243                         gtk_show_ntstatus(NULL, "Error inquiring statistics", status);
244                         return TRUE;
245                 }
246
247                 if (r.out.statistics.count != MGMT_STATS_ARRAY_MAX_SIZE) {
248                         printf("Unexpected array size %d\n", r.out.statistics.count);
249                         return False;
250                 }
251
252                 gtk_label_set_text(GTK_LABEL(lbl_calls_in), talloc_asprintf(mem_ctx, "%6d", r.out.statistics.statistics[MGMT_STATS_CALLS_IN]));
253                 gtk_label_set_text(GTK_LABEL(lbl_calls_out), talloc_asprintf(mem_ctx, "%6d", r.out.statistics.statistics[MGMT_STATS_CALLS_OUT]));
254                 gtk_label_set_text(GTK_LABEL(lbl_pkts_in), talloc_asprintf(mem_ctx, "%6d", r.out.statistics.statistics[MGMT_STATS_PKTS_IN]));
255                 gtk_label_set_text(GTK_LABEL(lbl_pkts_out), talloc_asprintf(mem_ctx, "%6d", r.out.statistics.statistics[MGMT_STATS_PKTS_OUT]));
256         }
257
258         {
259                 struct mgmt_inq_princ_name r;
260                 int i;
261
262                 gtk_list_store_clear(store_princ_names);
263
264                 for (i=0;i<100;i++) {
265                         r.in.authn_proto = i;  /* DCERPC_AUTH_TYPE_* */
266                         r.in.princ_name_size = 100;
267
268                         status = dcerpc_mgmt_inq_princ_name(mgmt_pipe, mem_ctx, &r);
269                         if (!NT_STATUS_IS_OK(status)) {
270                                 continue;
271                         }
272                         if (W_ERROR_IS_OK(r.out.result)) {
273                                 GtkTreeIter iter;
274                                 const char *name = gensec_get_name_by_authtype(i);
275                                 char *protocol;
276                                 if (name) {
277                                         protocol = talloc_asprintf(mem_ctx, "%u (%s)", i, name);
278                                 } else {
279                                         protocol = talloc_asprintf(mem_ctx, "%u", i);
280                                 }
281                                 gtk_list_store_append(store_princ_names, &iter);
282                                 gtk_list_store_set(store_princ_names, &iter, 
283                                                                    0, protocol,
284                                                                    1, r.out.princ_name,
285                                                                    -1);
286
287                         }
288                 }
289         }
290
291         return TRUE;
292 }
293
294
295 static GtkWidget* create_mainwindow (void)
296 {
297         GtkWidget *mainwindow;
298         GtkWidget *vbox1, *vbox2, *vbox3;
299         GtkWidget *menubar1;
300         GtkWidget *menuitem1;
301         GtkWidget *menuitem1_menu;
302         GtkWidget *quit1;
303         GtkWidget *menuitem4;
304         GtkWidget *menuitem4_menu;
305         GtkWidget *mnu_connect;
306         GtkWidget *treeview_princ_names;
307         GtkWidget *about1;
308         GtkWidget *hbox2;
309         GtkWidget *scrolledwindow1;
310         GtkWidget *frame1;
311         GtkWidget *tree_eps;
312         GtkTreeViewColumn *curcol;
313         GtkCellRenderer *renderer;
314         GtkWidget *statusbar;
315         GtkAccelGroup *accel_group;
316
317         accel_group = gtk_accel_group_new ();
318
319         mainwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
320         gtk_window_set_title (GTK_WINDOW (mainwindow), "Gtk+ Endpoint Mapper Viewer");
321
322         vbox1 = gtk_vbox_new (FALSE, 0);
323         gtk_widget_show (vbox1);
324         gtk_container_add (GTK_CONTAINER (mainwindow), vbox1);
325
326         menubar1 = gtk_menu_bar_new ();
327         gtk_widget_show (menubar1);
328         gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);
329
330         menuitem1 = gtk_menu_item_new_with_mnemonic ("_File");
331         gtk_widget_show (menuitem1);
332         gtk_container_add (GTK_CONTAINER (menubar1), menuitem1);
333
334         menuitem1_menu = gtk_menu_new ();
335         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);
336
337         mnu_connect = gtk_menu_item_new_with_mnemonic ("_Connect");
338         gtk_container_add(GTK_CONTAINER(menuitem1_menu), mnu_connect);
339
340         mnu_refresh = gtk_menu_item_new_with_mnemonic ("_Refresh");
341         gtk_container_add(GTK_CONTAINER(menuitem1_menu), mnu_refresh);
342         gtk_widget_set_sensitive( mnu_refresh, False );
343
344         quit1 = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
345         gtk_container_add (GTK_CONTAINER (menuitem1_menu), quit1);
346
347         menuitem4 = gtk_menu_item_new_with_mnemonic ("_Help");
348         gtk_container_add (GTK_CONTAINER (menubar1), menuitem4);
349
350         menuitem4_menu = gtk_menu_new ();
351         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);
352
353         about1 = gtk_menu_item_new_with_mnemonic ("_About");
354         gtk_container_add (GTK_CONTAINER (menuitem4_menu), about1);
355
356         hbox2 = gtk_hbox_new (FALSE, 0);
357         gtk_container_add (GTK_CONTAINER (vbox1), hbox2);
358
359         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
360         gtk_box_pack_start (GTK_BOX(hbox2), scrolledwindow1, TRUE, TRUE, 0);
361
362         tree_eps = gtk_tree_view_new ();
363
364         curcol = gtk_tree_view_column_new ();
365         gtk_tree_view_column_set_title(curcol, "Name");
366         renderer = gtk_cell_renderer_text_new();
367         gtk_tree_view_column_pack_start(curcol, renderer, True);
368
369         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_eps), curcol);
370         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
371
372         curcol = gtk_tree_view_column_new ();
373         gtk_tree_view_column_set_title(curcol, "Binding String");
374         renderer = gtk_cell_renderer_text_new();
375         gtk_tree_view_column_pack_start(curcol, renderer, True);
376         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 1);
377
378
379         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_eps), curcol);
380
381         store_eps = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
382         gtk_tree_view_set_model(GTK_TREE_VIEW(tree_eps), GTK_TREE_MODEL(store_eps));
383         g_object_unref(store_eps);
384
385         gtk_container_add (GTK_CONTAINER (scrolledwindow1), tree_eps);
386
387         gtk_tree_selection_set_select_function (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_eps)), on_eps_select, NULL, NULL);
388
389         vbox2 = gtk_vbox_new (FALSE, 0);
390         gtk_container_add (GTK_CONTAINER (hbox2), vbox2);
391
392         frame1 = gtk_frame_new("Interface");
393         gtk_container_add (GTK_CONTAINER(vbox2), frame1);
394
395         vbox3 = gtk_vbox_new (FALSE, 0);
396         gtk_container_add (GTK_CONTAINER (frame1), vbox3);
397         gtk_container_add (GTK_CONTAINER (vbox3), lbl_iface_uuid = gtk_label_new(""));
398         gtk_container_add (GTK_CONTAINER (vbox3), lbl_iface_version = gtk_label_new(""));
399         gtk_container_add (GTK_CONTAINER (vbox3), lbl_iface_name = gtk_label_new(""));
400
401         frame1 = gtk_frame_new("Statistics");
402         gtk_container_add (GTK_CONTAINER(vbox2), frame1);
403
404         table_statistics = gtk_table_new(4, 2, TRUE);
405         gtk_container_add (GTK_CONTAINER(frame1), table_statistics);
406
407         gtk_table_attach_defaults (GTK_TABLE(table_statistics), gtk_label_new("Calls In: "), 0, 1, 0, 1);
408         gtk_table_attach_defaults (GTK_TABLE(table_statistics), lbl_calls_in = gtk_label_new(""), 1, 2, 0, 1);
409         gtk_table_attach_defaults (GTK_TABLE(table_statistics), gtk_label_new("Calls Out: "), 0, 1, 1, 2);
410         gtk_table_attach_defaults (GTK_TABLE(table_statistics), lbl_calls_out = gtk_label_new(""), 1, 2, 1, 2);
411         gtk_table_attach_defaults (GTK_TABLE(table_statistics), gtk_label_new("Packets In: "), 0, 1, 2, 3);
412         gtk_table_attach_defaults (GTK_TABLE(table_statistics), lbl_pkts_in = gtk_label_new(""), 1, 2, 2, 3);
413         gtk_table_attach_defaults (GTK_TABLE(table_statistics), gtk_label_new("Packets Out: "), 0, 1, 3, 4);
414         gtk_table_attach_defaults (GTK_TABLE(table_statistics), lbl_pkts_out = gtk_label_new(""), 1, 2, 3, 4);
415         
416         frame1 = gtk_frame_new("Authentication");
417         gtk_container_add (GTK_CONTAINER(vbox2), frame1);
418
419         treeview_princ_names = gtk_tree_view_new();
420
421         curcol = gtk_tree_view_column_new ();
422         gtk_tree_view_column_set_title(curcol, "Protocol");
423     renderer = gtk_cell_renderer_text_new();
424     gtk_tree_view_column_pack_start(curcol, renderer, True);
425     gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_princ_names), curcol);
426     gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
427
428         curcol = gtk_tree_view_column_new ();
429         gtk_tree_view_column_set_title(curcol, "Principal Name");
430     renderer = gtk_cell_renderer_text_new();
431     gtk_tree_view_column_pack_start(curcol, renderer, True);
432     gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_princ_names), curcol);
433     gtk_tree_view_column_add_attribute(curcol, renderer, "text", 1);
434
435         gtk_container_add (GTK_CONTAINER(frame1), treeview_princ_names);
436
437     store_princ_names = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
438         gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_princ_names), GTK_TREE_MODEL(store_princ_names));
439     g_object_unref(store_princ_names);
440
441         statusbar = gtk_statusbar_new ();
442         gtk_box_pack_start (GTK_BOX (vbox1), statusbar, FALSE, FALSE, 0);
443
444         g_signal_connect ((gpointer) quit1, "activate",
445                                           G_CALLBACK (on_quit1_activate),
446                                           NULL);
447         g_signal_connect ((gpointer) about1, "activate",
448                                           G_CALLBACK (on_about1_activate),
449                                           NULL);
450         g_signal_connect ((gpointer) mnu_connect, "activate",
451                                           G_CALLBACK (on_connect_clicked),
452                                           NULL);
453         g_signal_connect ((gpointer) mnu_refresh, "activate",
454                                           G_CALLBACK (on_refresh_clicked),
455                                           NULL);
456
457         gtk_window_add_accel_group (GTK_WINDOW (mainwindow), accel_group);
458
459         return mainwindow;
460 }
461
462
463  int main(int argc, char **argv)
464 {
465         gepdump_init_subsystems;
466         lp_load(dyn_CONFIGFILE,True,False,False);
467         load_interfaces();
468         setup_logging(argv[0], DEBUG_STDERR);
469
470         gtk_init(&argc, &argv);
471         mainwin = create_mainwindow();
472         gtk_widget_show_all(mainwin);
473
474         return gtk_event_loop();
475 }