639aa68bac12d1e5003c48ae314ae9f2ea596c5c
[samba.git] / source4 / gtk / common / gtk-smb.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 "gtk/common/select.h"
24 #include "gtk/common/gtk-smb.h"
25
26 void gtk_show_werror(GtkWidget *win, WERROR err) 
27 {
28         GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(win), 
29                                                     GTK_DIALOG_DESTROY_WITH_PARENT,
30                                                     GTK_MESSAGE_ERROR,
31                                                     GTK_BUTTONS_CLOSE,
32                                                     "Windows error: %s\n",
33                                                     win_errstr(err));
34         gtk_dialog_run (GTK_DIALOG (dialog));
35         gtk_widget_destroy (dialog);
36 }
37                    
38 void gtk_show_ntstatus(GtkWidget *win, NTSTATUS status) 
39 {
40         GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(win), 
41                                                     GTK_DIALOG_DESTROY_WITH_PARENT,
42                                                     GTK_MESSAGE_ERROR,
43                                                     GTK_BUTTONS_CLOSE,
44                                                     "Windows error: %s\n",
45                                                     nt_errstr(status));
46         gtk_dialog_run (GTK_DIALOG (dialog));
47         gtk_widget_destroy (dialog);
48 }
49
50 static void on_browse_activate  (GtkButton     *button,  gpointer         user_data)
51 {
52         GtkRpcBindingDialog *rbd = user_data;
53         GtkWidget *shd = gtk_select_host_dialog_new(rbd->sam_pipe, TRUE);
54         if(gtk_dialog_run(GTK_DIALOG(shd)) == GTK_RESPONSE_ACCEPT) {
55                 gtk_entry_set_text(GTK_ENTRY(rbd->entry_host), gtk_select_host_dialog_get_host(GTK_SELECT_HOST_DIALOG(shd)));
56         }
57         
58         gtk_widget_destroy(GTK_WIDGET(shd));
59 }
60
61 static void on_krb5_toggled(GtkToggleButton *togglebutton, GtkRpcBindingDialog *d)
62 {
63         gtk_widget_set_sensitive(d->entry_password, !gtk_toggle_button_get_active(togglebutton));
64 }
65
66 static void on_ncalrpc_toggled(GtkToggleButton *tb, GtkRpcBindingDialog *d)
67 {
68         gtk_widget_set_sensitive(d->frame_host, !gtk_toggle_button_get_active(tb));
69 }
70
71 static void gtk_rpc_binding_dialog_init (GtkRpcBindingDialog *gtk_rpc_binding_dialog)
72 {
73         GtkWidget *dialog_vbox1;
74         GtkWidget *vbox1;
75         GtkWidget *vbox6;
76         GtkWidget *frame_transport;
77         GtkWidget *label1;
78         GtkWidget *hbox1;
79         GtkWidget *lbl_name;
80         GtkWidget *label2;
81         GtkWidget *frame_security;
82         GtkWidget *vbox2;
83         GtkWidget *label3;
84         GtkWidget *table1;
85         GtkWidget *lbl_username;
86         GtkWidget *lbl_userdomain;
87         GtkWidget *lbl_password;
88         GtkWidget *btn_browse;
89         GtkWidget *label9;
90         GtkWidget *lbl_credentials;
91         GtkWidget *dialog_action_area1;
92         GtkWidget *btn_cancel;
93         GtkWidget *btn_connect;
94         GSList *transport_smb_group = NULL;
95
96         gtk_rpc_binding_dialog->mem_ctx = talloc_init("gtk_rcp_binding_dialog");
97         
98         gtk_window_set_title (GTK_WINDOW (gtk_rpc_binding_dialog), "Connect");
99
100         dialog_vbox1 = GTK_DIALOG (gtk_rpc_binding_dialog)->vbox;
101
102         vbox1 = gtk_vbox_new (FALSE, 0);
103         gtk_box_pack_start (GTK_BOX (dialog_vbox1), vbox1, TRUE, TRUE, 0);
104
105         frame_transport = gtk_frame_new (NULL);
106         gtk_box_pack_start (GTK_BOX (vbox1), frame_transport, TRUE, TRUE, 0);
107
108         vbox6 = gtk_vbox_new (FALSE, 0);
109         gtk_container_add (GTK_CONTAINER (frame_transport), vbox6);
110
111         gtk_rpc_binding_dialog->transport_ncalrpc = gtk_radio_button_new_with_mnemonic (NULL, "Local Host");
112         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_ncalrpc, FALSE, FALSE, 0);
113         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_ncalrpc), transport_smb_group);
114         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_ncalrpc));
115
116
117         gtk_rpc_binding_dialog->transport_smb = gtk_radio_button_new_with_mnemonic (NULL, "RPC over SMB over TCP/IP");
118         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_smb, FALSE, FALSE, 0);
119         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_smb), transport_smb_group);
120         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_smb));
121         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_rpc_binding_dialog->transport_smb), TRUE);
122
123         gtk_rpc_binding_dialog->transport_tcp_ip = gtk_radio_button_new_with_mnemonic (NULL, "RPC over TCP/IP");
124         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_tcp_ip, FALSE, FALSE, 0);
125         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_tcp_ip), transport_smb_group);
126         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_tcp_ip));
127
128
129
130         label1 = gtk_label_new ("Transport");
131         gtk_frame_set_label_widget (GTK_FRAME (frame_transport), label1);
132
133         gtk_rpc_binding_dialog->frame_host = gtk_frame_new (NULL);
134         gtk_box_pack_start (GTK_BOX (vbox1), gtk_rpc_binding_dialog->frame_host, TRUE, TRUE, 0);
135
136         hbox1 = gtk_hbox_new (FALSE, 0);
137         gtk_container_add (GTK_CONTAINER (gtk_rpc_binding_dialog->frame_host), hbox1);
138
139         lbl_name = gtk_label_new ("Name");
140         gtk_box_pack_start (GTK_BOX (hbox1), lbl_name, TRUE, TRUE, 0);
141
142         gtk_rpc_binding_dialog->entry_host = gtk_entry_new ();
143         gtk_box_pack_start (GTK_BOX (hbox1), gtk_rpc_binding_dialog->entry_host, TRUE, TRUE, 0);
144
145         if(gtk_rpc_binding_dialog->sam_pipe)
146         {
147                 btn_browse = gtk_button_new_with_label ("Browse");
148                 gtk_box_pack_start (GTK_BOX (hbox1), btn_browse, TRUE, TRUE, 0);
149
150                 g_signal_connect ((gpointer) btn_browse, "pressed",
151                                                   G_CALLBACK (on_browse_activate),
152                                                   gtk_rpc_binding_dialog);
153         }
154
155         label2 = gtk_label_new ("Host");
156         gtk_frame_set_label_widget (GTK_FRAME (gtk_rpc_binding_dialog->frame_host), label2);
157
158         frame_security = gtk_frame_new (NULL);
159         gtk_box_pack_start (GTK_BOX (vbox1), frame_security, TRUE, TRUE, 0);
160
161         vbox2 = gtk_vbox_new (FALSE, 0);
162         gtk_container_add (GTK_CONTAINER (frame_security), vbox2);
163
164         gtk_rpc_binding_dialog->chk_sign = gtk_check_button_new_with_mnemonic ("S_ign");
165         gtk_box_pack_start (GTK_BOX (vbox2), gtk_rpc_binding_dialog->chk_sign, FALSE, FALSE, 0);
166
167         gtk_rpc_binding_dialog->chk_seal = gtk_check_button_new_with_mnemonic ("_Seal");
168         gtk_box_pack_start (GTK_BOX (vbox2), gtk_rpc_binding_dialog->chk_seal, FALSE, FALSE, 0);
169
170         label3 = gtk_label_new ("Security");
171         gtk_frame_set_label_widget (GTK_FRAME (frame_security), label3);
172
173         gtk_rpc_binding_dialog->frame_credentials = gtk_frame_new (NULL);
174         gtk_box_pack_start (GTK_BOX (dialog_vbox1), gtk_rpc_binding_dialog->frame_credentials, TRUE, TRUE, 0);
175
176         table1 = gtk_table_new (4, 2, FALSE);
177         gtk_container_add (GTK_CONTAINER (gtk_rpc_binding_dialog->frame_credentials), table1);
178
179         lbl_username = gtk_label_new ("Username:");
180         gtk_table_attach (GTK_TABLE (table1), lbl_username, 0,1, 0,1,
181                                           (GtkAttachOptions) (GTK_FILL),
182                                           (GtkAttachOptions) (0), 0, 0);
183         gtk_misc_set_alignment (GTK_MISC (lbl_username), 0, 0.5);
184
185         lbl_userdomain= gtk_label_new ("Domain:");
186         gtk_table_attach (GTK_TABLE (table1), lbl_userdomain, 0,1, 1,2,
187                                           (GtkAttachOptions) (GTK_FILL),
188                                           (GtkAttachOptions) (0), 0, 0);
189         gtk_misc_set_alignment (GTK_MISC (lbl_userdomain), 0, 0.5);
190
191         lbl_password = gtk_label_new ("Password:");
192         gtk_table_attach (GTK_TABLE (table1), lbl_password, 0,1, 2,3,
193                                           (GtkAttachOptions) (GTK_FILL),
194                                           (GtkAttachOptions) (0), 0, 0);
195         gtk_misc_set_alignment (GTK_MISC (lbl_password), 0, 0.5);
196
197         label9 = gtk_label_new ("");
198         gtk_table_attach (GTK_TABLE (table1), label9, 0,1, 3,4,
199                                           (GtkAttachOptions) (GTK_FILL),
200                                           (GtkAttachOptions) (0), 0, 0);
201         gtk_misc_set_alignment (GTK_MISC (label9), 0, 0.5);
202
203         gtk_rpc_binding_dialog->entry_username = gtk_entry_new ();
204         gtk_table_attach (GTK_TABLE (table1), gtk_rpc_binding_dialog->entry_username, 1,2, 0,1,
205                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
206                                           (GtkAttachOptions) (0), 0, 0);
207
208         gtk_entry_set_text(GTK_ENTRY(gtk_rpc_binding_dialog->entry_username), getenv("LOGNAME"));
209
210         gtk_rpc_binding_dialog->entry_userdomain = gtk_entry_new ();
211         gtk_table_attach (GTK_TABLE (table1), gtk_rpc_binding_dialog->entry_userdomain, 1,2, 1,2,
212                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
213                                           (GtkAttachOptions) (0), 0, 0);
214
215         gtk_entry_set_text(GTK_ENTRY(gtk_rpc_binding_dialog->entry_userdomain), lp_workgroup());
216
217         gtk_rpc_binding_dialog->entry_password = gtk_entry_new ();
218         gtk_entry_set_visibility (GTK_ENTRY (gtk_rpc_binding_dialog->entry_password), FALSE);
219         gtk_table_attach (GTK_TABLE (table1), gtk_rpc_binding_dialog->entry_password, 1,2, 2,3,
220                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
221                                           (GtkAttachOptions) (0), 0, 0);
222
223         gtk_rpc_binding_dialog->krb5_chk_button = gtk_check_button_new_with_mnemonic ("_Use kerberos");
224         gtk_table_attach (GTK_TABLE (table1), gtk_rpc_binding_dialog->krb5_chk_button, 1,2, 3,4,
225                                           (GtkAttachOptions) (GTK_FILL),
226                                           (GtkAttachOptions) (0), 0, 0);
227
228         g_signal_connect ((gpointer) gtk_rpc_binding_dialog->krb5_chk_button, "toggled",
229                                           G_CALLBACK (on_krb5_toggled),
230                                           gtk_rpc_binding_dialog);
231
232         /* Poor man's autodetection */
233         if(getenv("KRB5CCNAME")) {
234                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gtk_rpc_binding_dialog->krb5_chk_button), TRUE);
235         }
236
237         lbl_credentials = gtk_label_new ("Credentials");
238         gtk_frame_set_label_widget (GTK_FRAME (gtk_rpc_binding_dialog->frame_credentials), lbl_credentials);
239
240         dialog_action_area1 = GTK_DIALOG (gtk_rpc_binding_dialog)->action_area;
241         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
242
243         btn_cancel = gtk_button_new_from_stock ("gtk-cancel");
244         gtk_dialog_add_action_widget (GTK_DIALOG (gtk_rpc_binding_dialog), btn_cancel, GTK_RESPONSE_CANCEL);
245         GTK_WIDGET_SET_FLAGS (btn_cancel, GTK_CAN_DEFAULT);
246
247         btn_connect = gtk_button_new_with_mnemonic ("_Connect");
248         gtk_dialog_add_action_widget (GTK_DIALOG (gtk_rpc_binding_dialog), btn_connect, GTK_RESPONSE_ACCEPT);
249         gtk_container_set_border_width (GTK_CONTAINER (btn_connect), 1);
250         GTK_WIDGET_SET_FLAGS (btn_connect, GTK_CAN_DEFAULT);
251
252         g_signal_connect ((gpointer) gtk_rpc_binding_dialog->transport_ncalrpc, "toggled",
253                                                   G_CALLBACK (on_ncalrpc_toggled),
254                                                   gtk_rpc_binding_dialog);
255
256
257         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gtk_rpc_binding_dialog->transport_ncalrpc), TRUE);
258         gtk_widget_show_all(dialog_vbox1);
259
260         gtk_widget_grab_focus (btn_connect);
261         gtk_widget_grab_default (btn_connect);
262 }
263
264 GType gtk_rpc_binding_dialog_get_type (void)
265 {
266   static GType mytype = 0;
267
268   if (!mytype)
269     {
270       static const GTypeInfo myinfo =
271       {
272         sizeof (GtkRpcBindingDialogClass),
273         NULL,
274         NULL,
275         NULL,
276         NULL,
277         NULL,
278         sizeof(GtkRpcBindingDialog),
279         0,
280         (GInstanceInitFunc) gtk_rpc_binding_dialog_init,
281       };
282
283       mytype = g_type_register_static (GTK_TYPE_DIALOG, 
284                 "GtkRpcBindingDialog", &myinfo, 0);
285     }
286
287   return mytype;
288 }
289
290 GtkWidget *gtk_rpc_binding_dialog_new (BOOL nocredentials, struct sam_pipe *sam_pipe)
291 {
292         GtkRpcBindingDialog *d = GTK_RPC_BINDING_DIALOG ( g_object_new (gtk_rpc_binding_dialog_get_type (), NULL));
293         if (nocredentials) {
294                 gtk_widget_hide_all(d->frame_credentials);
295         }
296         d->sam_pipe = sam_pipe;
297         return GTK_WIDGET(d);
298 }
299
300 const char *gtk_rpc_binding_dialog_get_username(GtkRpcBindingDialog *d)
301 {
302         return gtk_entry_get_text(GTK_ENTRY(d->entry_username));
303 }
304
305 const char *gtk_rpc_binding_dialog_get_userdomain(GtkRpcBindingDialog *d)
306 {
307         return gtk_entry_get_text(GTK_ENTRY(d->entry_userdomain));
308 }
309
310 const char *gtk_rpc_binding_dialog_get_password(GtkRpcBindingDialog *d)
311 {
312         return gtk_entry_get_text(GTK_ENTRY(d->entry_password));
313 }
314
315 const char *gtk_rpc_binding_dialog_get_host(GtkRpcBindingDialog *d)
316 {
317         return gtk_entry_get_text(GTK_ENTRY(d->entry_host));
318 }
319
320 struct dcerpc_binding *gtk_rpc_binding_dialog_get_binding(GtkRpcBindingDialog *d, TALLOC_CTX *mem_ctx)
321 {
322         struct dcerpc_binding *binding = talloc_p(mem_ctx, struct dcerpc_binding);
323
324         ZERO_STRUCT(binding->object);
325
326         /* Format: TRANSPORT:host[\pipe\foo,foo,foo] */
327
328         binding->host = talloc_strdup(mem_ctx, gtk_entry_get_text(GTK_ENTRY(d->entry_host)));
329         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->transport_tcp_ip))) {
330                 binding->transport = NCACN_IP_TCP;
331         } else if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->transport_ncalrpc))) {
332                 binding->transport = NCALRPC;
333                 binding->host = NULL;
334         } else {
335                 binding->transport = NCACN_NP;
336         }
337
338         binding->options = NULL;
339         binding->flags = 0;
340         binding->endpoint = NULL;
341
342         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->chk_seal))) {
343                 binding->flags |= DCERPC_SEAL;
344         }
345
346         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->chk_sign))) {
347                 binding->flags |= DCERPC_SIGN;
348         }
349
350         return binding;
351 }
352
353 const char *gtk_rpc_binding_dialog_get_binding_string(GtkRpcBindingDialog *d, TALLOC_CTX *mem_ctx)
354 {
355         return dcerpc_binding_string(mem_ctx, gtk_rpc_binding_dialog_get_binding(d, mem_ctx));
356 }
357
358 GtkWidget *create_gtk_samba_about_dialog (const char *appname)
359 {
360         GtkWidget *samba_about_dialog;
361         GtkWidget *dialog_vbox1;
362         GtkWidget *label1;
363         GtkWidget *label2;
364         GtkWidget *label3;
365         GtkWidget *label4;
366         GtkWidget *dialog_action_area1;
367         GtkWidget *okbutton1;
368
369         samba_about_dialog = gtk_dialog_new ();
370         gtk_window_set_title (GTK_WINDOW (samba_about_dialog), "About");
371
372         dialog_vbox1 = GTK_DIALOG (samba_about_dialog)->vbox;
373
374         /* FIXME image1 = create_pixmap (samba_about_dialog, "slmed.png");
375            gtk_box_pack_start (GTK_BOX (dialog_vbox1), image1, TRUE, TRUE, 0);*/
376
377         label1 = gtk_label_new (appname);
378         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label1, FALSE, FALSE, 0);
379
380         label2 = gtk_label_new (samba_version_string());
381         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label2, FALSE, FALSE, 0);
382
383         label3 = gtk_label_new_with_mnemonic ("Part of Samba <http://www.samba.org/>");
384         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label3, FALSE, FALSE, 0);
385
386         label4 = gtk_label_new ("\302\251 1992-2004 The Samba Team");
387         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label4, FALSE, FALSE, 0);
388
389         dialog_action_area1 = GTK_DIALOG (samba_about_dialog)->action_area;
390         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
391
392         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
393         gtk_dialog_add_action_widget (GTK_DIALOG (samba_about_dialog), okbutton1, GTK_RESPONSE_OK);
394         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
395         gtk_widget_show_all(dialog_vbox1);
396
397         return samba_about_dialog;
398 }