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