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