Fix error creation and warning
[claws.git] / src / editldap.c
blobc8ed4b438d0747cc16bdf62999c37d37220505e9
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2022 the Claws Mail team and Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Edit LDAP address book data.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #ifdef USE_LDAP
30 #include "defs.h"
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
37 #include "addressbook.h"
38 #include "prefs_common.h"
39 #include "addressitem.h"
40 #include "mgutils.h"
41 #include "ldapserver.h"
42 #include "ldapctrl.h"
43 #include "ldaputil.h"
44 #include "editldap_basedn.h"
45 #include "manage_window.h"
46 #include "gtkutils.h"
47 #include "prefs_gtk.h"
48 #include "passwordstore.h"
50 #define PAGE_BASIC 0
51 #define PAGE_SEARCH 1
52 #define PAGE_EXTENDED 2
54 #define ADDRESSBOOK_GUESS_LDAP_NAME "MyServer"
55 #define ADDRESSBOOK_GUESS_LDAP_SERVER "localhost"
57 #define LDAPEDIT_TABLE_ROWS 6
58 #define LDAPEDIT_TABLE_COLS 3
60 static struct _LDAPEdit {
61 GtkWidget *window;
62 GtkWidget *notebook;
63 GtkWidget *ok_btn;
64 GtkWidget *cancel_btn;
65 GtkWidget *statusbar;
66 gint status_cid;
67 GtkWidget *entry_name;
68 GtkWidget *entry_server;
69 GtkWidget *spinbtn_port;
70 GtkWidget *entry_baseDN;
71 GtkWidget *spinbtn_timeout;
72 GtkWidget *entry_bindDN;
73 GtkWidget *entry_bindPW;
74 GtkWidget *spinbtn_maxentry;
75 GtkWidget *entry_criteria;
76 GtkWidget *spinbtn_queryage;
77 GtkWidget *check_dynsearch;
78 GtkWidget *check_matchoption;
79 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
80 GtkWidget *enable_ssl;
81 GtkWidget *enable_tls;
82 #endif
83 } ldapedit;
85 /**
86 * Parse out individual attribute names from criteria string.
87 * \param criteria Criteria string.
88 * \ctl Control object.
90 static gboolean editldap_validate_criteria( gchar *criteria ) {
91 gchar *ptr;
92 gchar **splitStr;
93 gint i;
94 gboolean errorFlag;
96 cm_return_val_if_fail(criteria != NULL, TRUE);
98 errorFlag = TRUE;
100 /* Replace delimiters with spaces */
101 ptr = criteria;
102 while( *ptr ) {
103 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
104 *ptr = ' ';
105 ptr++;
107 debug_print("cleaned criteria list: %s\n", criteria);
109 /* Parse string */
110 splitStr = g_strsplit( criteria, " ", 0 );
111 i = 0;
112 while( TRUE ) {
113 if( splitStr[i] ) {
114 if( *splitStr[i] ) {
115 errorFlag = FALSE;
116 break;
119 else {
120 break;
122 i++;
124 g_strfreev( splitStr );
125 return errorFlag;
129 * Edit functions.
131 static void edit_ldap_status_show( gchar *msg ) {
132 if( ldapedit.statusbar != NULL ) {
133 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit.statusbar), ldapedit.status_cid );
134 if( msg ) {
135 gtk_statusbar_push( GTK_STATUSBAR(ldapedit.statusbar),
136 ldapedit.status_cid, msg );
141 static gboolean edit_ldap_validate( void ) {
142 gchar *str;
143 gboolean errorFlag;
144 gint page = 0;
146 errorFlag = FALSE;
147 str = gtk_editable_get_chars(
148 GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
149 if( *str == '\0' ) {
150 page = PAGE_BASIC;
151 gtk_widget_grab_focus( ldapedit.entry_name );
152 edit_ldap_status_show( _( "A Name must be supplied." ) );
153 errorFlag = TRUE;
155 g_free( str );
157 if( ! errorFlag ) {
158 str = gtk_editable_get_chars(
159 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
160 if( *str == '\0' ) {
161 page = PAGE_BASIC;
162 gtk_widget_grab_focus( ldapedit.entry_server );
163 edit_ldap_status_show(
164 _( "A Hostname must be supplied for the server." ) );
165 errorFlag = TRUE;
167 g_free( str );
170 if( ! errorFlag ) {
171 str = gtk_editable_get_chars(
172 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
173 if( editldap_validate_criteria( str ) ) {
174 page = PAGE_SEARCH;
175 gtk_widget_grab_focus( ldapedit.entry_criteria );
176 edit_ldap_status_show(
177 _( "At least one LDAP search attribute should be supplied." ) );
178 errorFlag = TRUE;
180 g_free( str );
183 /* Switch to page with error */
184 if( errorFlag ) {
185 gtk_notebook_set_current_page( GTK_NOTEBOOK(ldapedit.notebook), page );
188 return errorFlag;
191 static void edit_ldap_ok( GtkWidget *widget, gboolean *cancelled ) {
192 if( ! edit_ldap_validate() ) {
193 *cancelled = FALSE;
194 gtk_main_quit();
198 static void edit_ldap_cancel( GtkWidget *widget, gboolean *cancelled ) {
199 *cancelled = TRUE;
200 gtk_main_quit();
203 static gint edit_ldap_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
204 *cancelled = TRUE;
205 gtk_main_quit();
206 return TRUE;
209 static gboolean edit_ldap_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
210 if (event && event->keyval == GDK_KEY_Escape) {
211 *cancelled = TRUE;
212 gtk_main_quit();
214 return FALSE;
217 static void edit_ldap_server_check( void ) {
218 gchar *sHost, *sBind, *sPass;
219 gint iPort, iTime;
220 gchar *sMsg;
221 gchar *sBaseDN = NULL;
222 gint iBaseDN = 0;
223 gboolean flg;
224 gboolean tls = FALSE, ssl = FALSE;
225 GList *baseDN = NULL;
227 edit_ldap_status_show( "" );
228 flg = FALSE;
229 sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
230 sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
231 sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
232 iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
233 iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
234 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
235 tls = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldapedit.enable_tls));
236 ssl = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldapedit.enable_ssl));
237 #endif
239 g_strstrip( sHost );
240 g_strstrip( sBind );
241 g_strstrip( sPass );
242 if( *sHost != '\0' ) {
243 /* Test connection to server */
244 debug_print("ldap server: %s\nport: %d\nssl: %d\ntls: %d\nbindDN: %s\n", sHost, iPort, ssl, tls, sBind);
245 if( ldaputil_test_connect( sHost, iPort, ssl, tls, iTime ) ) {
246 /* Attempt to read base DN */
247 baseDN = ldaputil_read_basedn(sHost, iPort, sBind, sPass, iTime, ssl, tls);
248 if( baseDN ) {
249 GList *node = baseDN;
250 while( node ) {
251 ++iBaseDN;
252 if( ! sBaseDN ) {
253 sBaseDN = g_strdup( node->data );
255 node = g_list_next( node );
257 g_list_free_full( baseDN, g_free );
258 baseDN = node = NULL;
259 flg = TRUE;
260 } else {
261 flg = FALSE;
265 g_free( sHost );
266 g_free( sBind );
267 g_free( sPass );
269 if( sBaseDN ) {
270 /* Load search DN */
271 debug_print("baseDN: %s\n", sBaseDN);
272 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), sBaseDN);
273 g_free( sBaseDN );
276 /* Display appropriate message */
277 if( flg ) {
278 sMsg = _( "Connected successfully to server" );
280 else {
281 sMsg = _( "Could not connect to server" );
283 edit_ldap_status_show( sMsg );
286 static void edit_ldap_basedn_select( void ) {
287 gchar *sHost, *sBind, *sPass, *sBase;
288 gint iPort, iTime, tls = 0, ssl = 0;
289 gchar *selectDN;
291 sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
292 sBase = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
293 sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
294 sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
295 iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
296 iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
297 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
298 tls = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldapedit.enable_tls));
299 ssl = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldapedit.enable_ssl));
300 #endif
302 g_strstrip( sHost );
303 g_strstrip( sBind );
304 g_strstrip( sPass );
305 debug_print("ldap server: %s\nport: %d\nssl: %d\ntls: %d\nbindDN: %s\n", sHost, iPort, ssl, tls, sBind);
306 selectDN = edit_ldap_basedn_selection( sHost, iPort, sBase, iTime, sBind, sPass, ssl, tls );
307 if( selectDN ) {
308 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), selectDN);
309 g_free( selectDN );
310 selectDN = NULL;
312 g_free( sHost );
313 g_free( sBase );
314 g_free( sBind );
315 g_free( sPass );
318 static void edit_ldap_search_reset(void) {
319 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST);
322 static void addressbook_edit_ldap_dialog_create( gboolean *cancelled ) {
323 GtkWidget *window;
324 GtkWidget *vbox;
325 GtkWidget *notebook;
326 GtkWidget *hbbox;
327 GtkWidget *ok_btn;
328 GtkWidget *cancel_btn;
329 GtkWidget *hsbox;
330 GtkWidget *statusbar;
332 debug_print("creating edit_ldap_dialog\n");
333 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editldap");
334 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
335 gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP Server"));
336 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
337 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
338 g_signal_connect(G_OBJECT(window), "delete_event",
339 G_CALLBACK(edit_ldap_delete_event),
340 cancelled);
341 g_signal_connect(G_OBJECT(window), "key_press_event",
342 G_CALLBACK(edit_ldap_key_pressed),
343 cancelled);
345 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6 );
346 gtk_widget_show( vbox );
347 gtk_container_add( GTK_CONTAINER( window ), vbox );
349 /* Notebook */
350 notebook = gtk_notebook_new();
351 gtk_widget_show( notebook );
352 gtk_box_pack_start( GTK_BOX( vbox ), notebook, TRUE, TRUE, 0 );
353 gtk_container_set_border_width( GTK_CONTAINER( notebook ), 6 );
355 /* Status line */
356 hsbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
357 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
358 statusbar = gtk_statusbar_new();
359 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
361 /* Button panel */
362 gtkut_stock_button_set_create(&hbbox, &cancel_btn, NULL, _("_Cancel"),
363 &ok_btn, NULL, _("_OK"),
364 NULL, NULL, NULL);
365 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
366 gtk_widget_grab_default(ok_btn);
368 g_signal_connect(G_OBJECT(ok_btn), "clicked",
369 G_CALLBACK(edit_ldap_ok), cancelled);
370 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
371 G_CALLBACK(edit_ldap_cancel), cancelled);
373 gtk_widget_show_all(vbox);
375 ldapedit.window = window;
376 ldapedit.notebook = notebook;
377 ldapedit.ok_btn = ok_btn;
378 ldapedit.cancel_btn = cancel_btn;
379 ldapedit.statusbar = statusbar;
380 ldapedit.status_cid =
381 gtk_statusbar_get_context_id(
382 GTK_STATUSBAR(statusbar), "Edit LDAP Server Dialog" );
385 static void editldap_update_port (GtkToggleButton *ssl_btn, gpointer data) {
386 gboolean val = gtk_toggle_button_get_active(ssl_btn);
387 gtk_spin_button_set_value(
388 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ),
389 val ? LDAPCTL_DFL_SSL_PORT:LDAPCTL_DFL_PORT );
390 debug_print("Setting port: %d\n", val ? LDAPCTL_DFL_SSL_PORT:LDAPCTL_DFL_PORT);
393 static void addressbook_edit_ldap_page_basic( gint pageNum, gchar *pageLbl ) {
394 GtkWidget *vbox;
395 GtkWidget *table;
396 GtkWidget *label;
397 GtkWidget *entry_name;
398 GtkWidget *entry_server;
399 GtkWidget *hbox_spin;
400 GtkAdjustment *spinbtn_port_adj;
401 GtkWidget *spinbtn_port;
402 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
403 GtkWidget *enable_ssl_checkbtn, *enable_tls_checkbtn;
404 #endif
405 GtkWidget *entry_baseDN;
406 GtkWidget *check_btn;
407 GtkWidget *lookdn_btn;
409 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8 );
410 gtk_widget_show( vbox );
411 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
413 label = gtk_label_new( pageLbl );
414 gtk_widget_show( label );
415 gtk_notebook_set_tab_label(
416 GTK_NOTEBOOK( ldapedit.notebook ),
417 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
419 table = gtk_grid_new();
420 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
421 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
422 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
423 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
425 /* First row */
426 label = gtk_label_new(_("Name"));
427 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
428 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
430 entry_name = gtk_entry_new();
431 gtk_grid_attach(GTK_GRID(table), entry_name, 1, 0, 1, 1);
432 gtk_widget_set_hexpand(entry_name, TRUE);
433 gtk_widget_set_halign(entry_name, GTK_ALIGN_FILL);
435 CLAWS_SET_TIP(entry_name, _(
436 "A name that you wish to call the server." ));
438 /* Next row */
439 label = gtk_label_new(_("Hostname"));
440 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
441 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
443 entry_server = gtk_entry_new();
444 gtk_grid_attach(GTK_GRID(table), entry_server, 1, 1, 1, 1);
445 gtk_widget_set_hexpand(entry_server, TRUE);
446 gtk_widget_set_halign(entry_server, GTK_ALIGN_FILL);
448 CLAWS_SET_TIP(entry_server, _(
449 "This is the hostname of the server. For example, " \
450 "\"ldap.example.org\" may be appropriate for the " \
451 "\"example.org\" organization. An IP address may also be " \
452 "used. You may specify \"localhost\" if running an LDAP " \
453 "server on the same computer as Claws Mail." ));
455 /* Next row */
456 label = gtk_label_new(_("Port"));
457 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
458 gtk_grid_attach(GTK_GRID(table), entry_server, 0, 2, 1, 1);
460 hbox_spin = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
461 spinbtn_port_adj = GTK_ADJUSTMENT(gtk_adjustment_new (389, 1, 65535, 1, 1000, 0));
462 spinbtn_port = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_port_adj), 1, 0);
463 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_port, TRUE, FALSE, 0);
464 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_port), TRUE);
466 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
467 enable_tls_checkbtn = gtk_check_button_new_with_label(_("STARTTLS"));
468 enable_ssl_checkbtn = gtk_check_button_new_with_label(_("TLS"));
469 SET_TOGGLE_SENSITIVITY_REVERSE(enable_tls_checkbtn, enable_ssl_checkbtn);
470 SET_TOGGLE_SENSITIVITY_REVERSE(enable_ssl_checkbtn, enable_tls_checkbtn);
471 CLAWS_SET_TIP(enable_tls_checkbtn, _(
472 "Enable secure connection to the LDAP server via STARTTLS. "
473 "Connection starts unencrypted and is secured by STARTTLS command. "
474 "If connection fails, be sure to check the correct "
475 "configuration in ldap.conf (TLS_CACERTDIR and TLS_REQCERT fields)." ));
476 CLAWS_SET_TIP(enable_ssl_checkbtn, _(
477 "Enable secure connection to the LDAP server via TLS. "
478 "If connection fails, be sure to check the correct "
479 "configuration in ldap.conf (TLS_CACERTDIR and TLS_REQCERT fields)." ));
481 gtk_box_pack_start (GTK_BOX (hbox_spin), enable_tls_checkbtn, TRUE, FALSE, 0);
482 gtk_box_pack_start (GTK_BOX (hbox_spin), enable_ssl_checkbtn, TRUE, FALSE, 0);
483 #endif
484 gtk_grid_attach(GTK_GRID(table), hbox_spin, 1, 2, 1, 1);
486 CLAWS_SET_TIP(spinbtn_port, _(
487 "The port number that the server listens on. Port 389 is " \
488 "the default." ));
490 check_btn = gtk_button_new_with_label( _(" Check Server "));
491 gtk_grid_attach(GTK_GRID(table), hbox_spin, 2, 2, 1, 1);
493 CLAWS_SET_TIP(check_btn, _(
494 "Press this button to test the connection to the server." ));
496 /* Next row */
497 label = gtk_label_new(_("Search Base"));
498 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
499 gtk_grid_attach(GTK_GRID(table), label, 0, 3, 1, 1);
501 entry_baseDN = gtk_entry_new();
502 gtk_grid_attach(GTK_GRID(table), entry_baseDN, 1, 3, 1, 1);
503 gtk_widget_set_hexpand(entry_baseDN, TRUE);
504 gtk_widget_set_halign(entry_baseDN, GTK_ALIGN_FILL);
506 CLAWS_SET_TIP(entry_baseDN, _(
507 "This specifies the name of the directory to be searched " \
508 "on the server. Examples include:\n" \
509 " dc=claws-mail,dc=org\n" \
510 " ou=people,dc=domainname,dc=com\n" \
511 " o=Organization Name,c=Country\n"
514 lookdn_btn = gtkut_get_browse_file_btn(_("_Browse"));
515 gtk_grid_attach(GTK_GRID(table), lookdn_btn, 2, 3, 1, 1);
517 CLAWS_SET_TIP(lookdn_btn, _(
518 "Press this button to lookup the name of available " \
519 "directory names on the server." ));
521 /* Signal handlers */
522 g_signal_connect(G_OBJECT(check_btn), "clicked",
523 G_CALLBACK(edit_ldap_server_check), NULL);
524 g_signal_connect(G_OBJECT(lookdn_btn), "clicked",
525 G_CALLBACK(edit_ldap_basedn_select), NULL);
527 /* Done */
528 gtk_widget_show_all(vbox);
530 ldapedit.entry_name = entry_name;
531 ldapedit.entry_server = entry_server;
532 ldapedit.spinbtn_port = spinbtn_port;
533 ldapedit.entry_baseDN = entry_baseDN;
534 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
535 ldapedit.enable_ssl = enable_ssl_checkbtn;
536 ldapedit.enable_tls = enable_tls_checkbtn;
538 g_signal_connect(G_OBJECT(enable_ssl_checkbtn), "toggled", \
539 G_CALLBACK(editldap_update_port), NULL);
540 #endif
543 static void addressbook_edit_ldap_page_search( gint pageNum, gchar *pageLbl ) {
544 GtkWidget *vbox;
545 GtkWidget *table;
546 GtkWidget *label;
547 GtkWidget *entry_criteria;
548 GtkWidget *hbox_spin;
549 GtkAdjustment *spinbtn_queryage_adj;
550 GtkWidget *spinbtn_queryage;
551 GtkWidget *check_dynsearch;
552 GtkWidget *check_matchoption;
553 GtkWidget *reset_btn;
555 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8 );
556 gtk_widget_show( vbox );
557 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
559 label = gtk_label_new( pageLbl );
560 gtk_widget_show( label );
561 gtk_notebook_set_tab_label(
562 GTK_NOTEBOOK( ldapedit.notebook ),
563 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
565 table = gtk_grid_new();
566 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
567 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
568 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
569 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
571 /* First row */
572 label = gtk_label_new(_("Search Attributes"));
573 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
574 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
576 entry_criteria = gtk_entry_new();
577 gtk_grid_attach(GTK_GRID(table), entry_criteria, 1, 0, 1, 1);
578 gtk_widget_set_hexpand(entry_criteria, TRUE);
579 gtk_widget_set_halign(entry_criteria, GTK_ALIGN_FILL);
581 CLAWS_SET_TIP(entry_criteria, _(
582 "A list of LDAP attribute names that should be searched " \
583 "when attempting to find a name or address." ));
585 reset_btn = gtk_button_new_with_label( _(" Defaults "));
586 gtk_grid_attach(GTK_GRID(table), reset_btn, 2, 0, 1, 1);
588 CLAWS_SET_TIP(reset_btn, _(
589 "This resets the attribute names to a default value " \
590 "that should find most names and addresses during a " \
591 "name or address search process." ));
593 /* Next row */
594 label = gtk_label_new(_("Max Query Age (secs)"));
595 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
596 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
598 hbox_spin = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
599 spinbtn_queryage_adj = GTK_ADJUSTMENT(gtk_adjustment_new(
600 LDAPCTL_DFL_QUERY_AGE, 1, LDAPCTL_MAX_QUERY_AGE, 10, 1000, 0 ));
601 spinbtn_queryage = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_queryage_adj), 1, 0);
602 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_queryage, FALSE, FALSE, 0);
603 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_queryage), TRUE);
604 gtk_grid_attach(GTK_GRID(table), hbox_spin, 1, 1, 1, 1);
606 CLAWS_SET_TIP(spinbtn_queryage, _(
607 "This defines the maximum period of time (in seconds) that " \
608 "an address search result is valid for address completion " \
609 "purposes. Search results are stored in a cache until this " \
610 "period of time has passed and then retired. This will " \
611 "improve the response time when attempting to search for " \
612 "the same name or address on subsequent address completion " \
613 "requests. The cache will be searched in preference to " \
614 "performing a new server search request. The default value " \
615 "of 600 seconds (10 minutes), should be sufficient for most " \
616 "servers. A larger value will reduce the search time for " \
617 "subsequent searches. This is useful for servers that have " \
618 "slow response times at the expense of more memory to cache " \
619 "results." ));
621 /* Next row */
622 check_dynsearch = gtk_check_button_new_with_label(
623 _("Include server in dynamic search") );
624 gtk_grid_attach(GTK_GRID(table), check_dynsearch, 1, 2, 1, 1);
626 CLAWS_SET_TIP(check_dynsearch, _(
627 "Check this option to include this server for dynamic " \
628 "searches when using address completion." ));
630 /* Next row */
631 check_matchoption = gtk_check_button_new_with_label(
632 _("Match names 'containing' search term") );
633 gtk_grid_attach(GTK_GRID(table), check_matchoption, 1, 3, 1, 1);
635 CLAWS_SET_TIP(check_matchoption, _(
636 "Searches for names and addresses can be performed either " \
637 "using \"begins-with\" or \"contains\" search term. Check " \
638 "this option to perform a \"contains\" search; this type of " \
639 "search usually takes longer to complete. Note that for " \
640 "performance reasons, address completion uses " \
641 "\"begins-with\" for all searches against other address " \
642 "interfaces." \
645 /* Signal handlers */
646 g_signal_connect(G_OBJECT(reset_btn), "clicked",
647 G_CALLBACK(edit_ldap_search_reset), NULL);
649 /* Done */
650 gtk_widget_show_all(vbox);
652 ldapedit.entry_criteria = entry_criteria;
653 ldapedit.spinbtn_queryage = spinbtn_queryage;
654 ldapedit.check_dynsearch = check_dynsearch;
655 ldapedit.check_matchoption = check_matchoption;
658 static void showpwd_checkbtn_toggled(GtkToggleButton *button,
659 gpointer user_data)
661 gboolean active = gtk_toggle_button_get_active(button);
662 GtkWidget *entry = GTK_WIDGET(user_data);
664 gtk_entry_set_visibility(GTK_ENTRY(entry), active);
667 static void addressbook_edit_ldap_page_extended( gint pageNum, gchar *pageLbl ) {
668 GtkWidget *vbox;
669 GtkWidget *table;
670 GtkWidget *label;
671 GtkWidget *entry_bindDN;
672 GtkWidget *entry_bindPW;
673 GtkWidget *showpwd_checkbtn;
674 GtkWidget *hbox_spin;
675 GtkAdjustment *spinbtn_timeout_adj;
676 GtkWidget *spinbtn_timeout;
677 GtkAdjustment *spinbtn_maxentry_adj;
678 GtkWidget *spinbtn_maxentry;
680 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8 );
681 gtk_widget_show( vbox );
682 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
684 label = gtk_label_new( pageLbl );
685 gtk_widget_show( label );
686 gtk_notebook_set_tab_label(
687 GTK_NOTEBOOK( ldapedit.notebook ),
688 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
690 table = gtk_grid_new();
691 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
692 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
693 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
694 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
696 /* Next row */
697 label = gtk_label_new(_("Bind DN"));
698 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
699 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
701 entry_bindDN = gtk_entry_new();
702 gtk_grid_attach(GTK_GRID(table), entry_bindDN, 1, 0, 1, 1);
703 gtk_widget_set_hexpand(entry_bindDN, TRUE);
704 gtk_widget_set_halign(entry_bindDN, GTK_ALIGN_FILL);
706 CLAWS_SET_TIP(entry_bindDN, _(
707 "The LDAP user account name to be used to connect to the server. " \
708 "This is usually only used for protected servers. This name " \
709 "is typically formatted as: \"cn=user,dc=claws-mail,dc=org\". " \
710 "This is usually left empty when performing a search." ));
712 /* Next row */
713 label = gtk_label_new(_("Bind Password"));
714 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
715 gtk_grid_attach(GTK_GRID(table), entry_bindDN, 0, 1, 1, 1);
717 entry_bindPW = gtk_entry_new();
718 gtk_entry_set_visibility(GTK_ENTRY(entry_bindPW), FALSE);
719 gtk_grid_attach(GTK_GRID(table), entry_bindPW, 1, 1, 1, 1);
720 gtk_widget_set_hexpand(entry_bindPW, TRUE);
721 gtk_widget_set_halign(entry_bindPW, GTK_ALIGN_FILL);
723 CLAWS_SET_TIP(entry_bindPW, _(
724 "The password to be used when connecting as the \"Bind DN\" " \
725 "user." ));
727 showpwd_checkbtn = gtk_check_button_new_with_label (_("Show password"));
728 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(showpwd_checkbtn), FALSE);
729 g_signal_connect(G_OBJECT(showpwd_checkbtn), "toggled",
730 G_CALLBACK(showpwd_checkbtn_toggled), entry_bindPW);
731 gtk_grid_attach(GTK_GRID(table), showpwd_checkbtn, 2, 1, 1, 1);
733 /* Next row */
734 label = gtk_label_new(_("Timeout (secs)"));
735 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
736 gtk_grid_attach(GTK_GRID(table), label, 0, 2, 1, 1);
738 hbox_spin = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
739 spinbtn_timeout_adj = GTK_ADJUSTMENT(gtk_adjustment_new (0, 0, 300, 1, 10, 0));
740 spinbtn_timeout = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_timeout_adj), 1, 0);
741 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_timeout, FALSE, FALSE, 0);
742 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_timeout), TRUE);
743 gtk_grid_attach(GTK_GRID(table), hbox_spin, 1, 2, 1, 1);
745 CLAWS_SET_TIP(spinbtn_timeout, _(
746 "The timeout period in seconds." ));
748 /* Next row */
749 label = gtk_label_new(_("Maximum Entries"));
750 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
751 gtk_grid_attach(GTK_GRID(table), label, 0, 3, 1, 1);
753 hbox_spin = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
754 spinbtn_maxentry_adj = GTK_ADJUSTMENT(gtk_adjustment_new (0, 0, 500, 1, 10, 0));
755 spinbtn_maxentry = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_maxentry_adj), 1, 0);
756 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_maxentry, FALSE, FALSE, 0);
757 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_maxentry), TRUE);
758 gtk_grid_attach(GTK_GRID(table), hbox_spin, 1, 3, 1, 1);
760 CLAWS_SET_TIP(spinbtn_maxentry, _(
761 "The maximum number of entries that should be returned " \
762 "in the search result." ));
764 /* Done */
765 gtk_widget_show_all(vbox);
767 ldapedit.entry_bindDN = entry_bindDN;
768 ldapedit.entry_bindPW = entry_bindPW;
769 ldapedit.spinbtn_timeout = spinbtn_timeout;
770 ldapedit.spinbtn_maxentry = spinbtn_maxentry;
773 static void addressbook_edit_ldap_create( gboolean *cancelled ) {
774 gint page = 0;
775 addressbook_edit_ldap_dialog_create( cancelled );
776 addressbook_edit_ldap_page_basic( page++, _( "Basic" ) );
777 addressbook_edit_ldap_page_search( page++, _( "Search" ) );
778 addressbook_edit_ldap_page_extended( page++, _( "Extended" ) );
779 gtk_widget_show_all( ldapedit.window );
783 * Format criteria list for display.
784 * \param ctl Control object.
785 * \return Formatted string, or <i>NULL</i> if no attributes found.
787 static gchar *editldap_build_criteria_list( const LdapControl *ctl ) {
788 gchar *str = NULL;
789 gchar *tmp = NULL;
790 GList *node;
792 node = ldapctl_get_criteria_list( ctl );
793 while( node ) {
794 gchar *attr = node->data;
795 if( str ) {
796 tmp = g_strdup_printf( "%s, %s", str, attr );
797 g_free( str );
798 str = tmp;
799 tmp = NULL;
801 else {
802 str = g_strdup( attr );
804 node = g_list_next( node );
807 return str;
811 * Parse out individual attribute names from criteria string.
812 * \param criteria Criteria string.
813 * \ctl Control object.
815 static void editldap_parse_criteria( gchar *criteria, LdapControl *ctl ) {
816 gchar *ptr;
817 gchar **splitStr;
818 gint i;
820 /* Replace delimiters with spaces */
821 ptr = criteria;
822 while( *ptr ) {
823 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
824 *ptr = ' ';
825 ptr++;
828 /* Parse string */
829 ldapctl_criteria_list_clear( ctl );
830 splitStr = g_strsplit( criteria, " ", 0 );
831 i = 0;
832 while( TRUE ) {
833 if( splitStr[i] ) {
834 if( *splitStr[i] ) {
835 ldapctl_criteria_list_add( ctl, splitStr[i] );
838 else {
839 break;
841 i++;
843 g_strfreev( splitStr );
847 * Clear entry fields to reasonable defaults (for a new server entry).
849 static void edit_ldap_clear_fields(void) {
850 gtk_entry_set_text(
851 GTK_ENTRY(ldapedit.entry_name), ADDRESSBOOK_GUESS_LDAP_NAME );
852 gtk_entry_set_text(
853 GTK_ENTRY(ldapedit.entry_server), ADDRESSBOOK_GUESS_LDAP_SERVER );
854 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), "");
855 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindDN), "");
856 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindPW), "");
857 gtk_spin_button_set_value(
858 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ), LDAPCTL_DFL_PORT );
859 gtk_spin_button_set_value(
860 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ), LDAPCTL_DFL_TIMEOUT );
861 gtk_spin_button_set_value(
862 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ), LDAPCTL_DFL_TIMEOUT );
863 gtk_entry_set_text(
864 GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST);
865 gtk_spin_button_set_value(
866 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), LDAPCTL_DFL_QUERY_AGE );
867 gtk_toggle_button_set_active(
868 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), TRUE );
869 gtk_toggle_button_set_active(
870 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption), FALSE );
871 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
872 gtk_toggle_button_set_active(
873 GTK_TOGGLE_BUTTON( ldapedit.enable_ssl), FALSE );
874 gtk_toggle_button_set_active(
875 GTK_TOGGLE_BUTTON( ldapedit.enable_tls), FALSE );
876 #endif
880 * Load entry fields from server control data.
881 * \param server Server object.
883 static void edit_ldap_set_fields( LdapServer *server ) {
884 LdapControl *ctl;
885 gchar *crit, *pwd;
887 if( ldapsvr_get_name( server ) )
888 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_name),
889 ldapsvr_get_name( server ) );
891 ctl = server->control;
892 if( ctl->hostName )
893 gtk_entry_set_text(
894 GTK_ENTRY(ldapedit.entry_server), ctl->hostName);
895 if( ctl->baseDN )
896 gtk_entry_set_text(
897 GTK_ENTRY(ldapedit.entry_baseDN), ctl->baseDN );
898 if( ctl->bindDN )
899 gtk_entry_set_text(
900 GTK_ENTRY(ldapedit.entry_bindDN), ctl->bindDN );
902 pwd = passwd_store_get(PWS_CORE, "LDAP", ctl->hostName);
903 gtk_entry_set_text( GTK_ENTRY(ldapedit.entry_bindPW),
904 (pwd ? pwd : ""));
905 if (pwd != NULL) {
906 memset(pwd, 0, strlen(pwd));
907 g_free(pwd);
910 gtk_spin_button_set_value(
911 GTK_SPIN_BUTTON(ldapedit.spinbtn_timeout), ctl->timeOut );
912 gtk_spin_button_set_value(
913 GTK_SPIN_BUTTON(ldapedit.spinbtn_maxentry), ctl->maxEntries );
914 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
915 gtk_toggle_button_set_active(
916 GTK_TOGGLE_BUTTON(ldapedit.enable_tls), ctl->enableTLS );
917 gtk_toggle_button_set_active(
918 GTK_TOGGLE_BUTTON(ldapedit.enable_ssl), ctl->enableSSL );
919 #endif
920 gtk_spin_button_set_value(
921 GTK_SPIN_BUTTON(ldapedit.spinbtn_port), ctl->port );
922 /* Format criteria */
923 crit = editldap_build_criteria_list( ctl );
924 if( crit ) {
925 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), crit );
926 g_free( crit );
928 else {
929 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), "" );
931 gtk_spin_button_set_value(
932 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), ctl->maxQueryAge );
933 gtk_toggle_button_set_active(
934 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), server->searchFlag );
935 gtk_toggle_button_set_active(
936 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption),
937 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) );
941 * Edit LDAP server datasource that appears addressbook.
942 * \param addrIndex Address index object.
943 * \param ads Data source adapter.
944 * \return Update data source adapter, or <code>NULL</code> if user cancelled
945 * edit with dialog.
947 AdapterDSource *addressbook_edit_ldap(
948 AddressIndex *addrIndex, AdapterDSource *ads )
950 static gboolean cancelled;
951 gchar *sName, *sHost, *sBase, *sBind, *sPass, *sCrit;
952 gint iPort, iMaxE, iTime, iAge;
953 gboolean bSrch, bMatch;
954 AddressDataSource *ds = NULL;
955 LdapServer *server = NULL;
956 LdapControl *ctl = NULL;
957 gboolean fin, ssl = FALSE, tls = FALSE;
959 if (!ldapedit.window)
960 addressbook_edit_ldap_create(&cancelled);
961 gtk_notebook_set_current_page( GTK_NOTEBOOK(ldapedit.notebook), PAGE_BASIC );
962 gtk_widget_grab_focus(ldapedit.ok_btn);
963 gtk_widget_grab_focus(ldapedit.entry_name);
964 gtk_widget_show(ldapedit.window);
965 manage_window_set_transient(GTK_WINDOW(ldapedit.window));
966 gtk_window_set_modal(GTK_WINDOW(ldapedit.window), TRUE);
968 edit_ldap_status_show( "" );
969 if( ads ) {
970 ds = ads->dataSource;
971 server = ds->rawDataSource;
972 edit_ldap_set_fields( server );
973 gtk_window_set_title(
974 GTK_WINDOW(ldapedit.window), _("Edit LDAP Server"));
976 else {
977 edit_ldap_clear_fields();
978 gtk_window_set_title(
979 GTK_WINDOW(ldapedit.window), _("Add New LDAP Server"));
982 gtk_main();
983 gtk_widget_hide(ldapedit.window);
984 gtk_window_set_modal(GTK_WINDOW(ldapedit.window), FALSE);
985 if (cancelled == TRUE) return NULL;
987 sName = gtk_editable_get_chars(
988 GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
989 sHost = gtk_editable_get_chars(
990 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
991 sBase = gtk_editable_get_chars(
992 GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
993 sCrit = gtk_editable_get_chars(
994 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
995 sBind = gtk_editable_get_chars(
996 GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
997 sPass = gtk_editable_get_chars(
998 GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
999 iPort = gtk_spin_button_get_value_as_int(
1000 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
1001 iTime = gtk_spin_button_get_value_as_int(
1002 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
1003 iMaxE = gtk_spin_button_get_value_as_int(
1004 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ) );
1005 iAge = gtk_spin_button_get_value_as_int(
1006 GTK_SPIN_BUTTON( ldapedit.spinbtn_queryage ) );
1007 bSrch = gtk_toggle_button_get_active(
1008 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch ) );
1009 bMatch = gtk_toggle_button_get_active(
1010 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption ) );
1011 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
1012 ssl = gtk_toggle_button_get_active(
1013 GTK_TOGGLE_BUTTON( ldapedit.enable_ssl ) );
1014 tls = gtk_toggle_button_get_active(
1015 GTK_TOGGLE_BUTTON( ldapedit.enable_tls ) );
1016 #endif
1017 debug_print("saving server config:\nname: %s\nhost: %s\nbase: %s\ncriteria: %s\nbind: %s\nport: %d\ntime: %d\nmax_entries: %d\ntimeout: %d\ndynamic: %d\ncheck_match: %d\n",
1018 sName, sHost, sBase, sCrit, sBind, iPort, iTime, iMaxE, iAge, bSrch, bMatch);
1019 fin = FALSE;
1020 if( *sName == '\0' ) fin = TRUE;
1021 if( *sHost == '\0' ) fin = TRUE;
1023 if( ! fin ) {
1024 /* Save changes */
1025 if( ! ads ) {
1026 /* New server */
1027 server = ldapsvr_create();
1028 ds = addrindex_index_add_datasource(
1029 addrIndex, ADDR_IF_LDAP, server );
1030 ads = addressbook_create_ds_adapter(
1031 ds, ADDR_LDAP, NULL );
1033 ctl = server->control;
1034 addressbook_ads_set_name( ads, sName );
1035 ldapsvr_set_name( server, sName );
1036 ldapsvr_set_search_flag( server, bSrch );
1037 ldapctl_set_host( ctl, sHost );
1038 ldapctl_set_base_dn( ctl, sBase );
1039 ldapctl_set_bind_dn( ctl, sBind );
1040 ldapctl_set_port( ctl, iPort );
1041 ldapctl_set_max_entries( ctl, iMaxE );
1042 ldapctl_set_timeout( ctl, iTime );
1043 ldapctl_set_max_query_age( ctl, iAge );
1044 ldapctl_set_tls( ctl, tls );
1045 ldapctl_set_ssl( ctl, ssl );
1046 ldapctl_set_matching_option(
1047 ctl, bMatch ?
1048 LDAPCTL_MATCH_CONTAINS : LDAPCTL_MATCH_BEGINWITH );
1050 addrindex_save_data(addrIndex);
1052 passwd_store_set(PWS_CORE, "LDAP", sHost, sPass, FALSE);
1053 passwd_store_write_config();
1055 /* Save attributes */
1056 editldap_parse_criteria( sCrit, ctl );
1059 g_free( sName );
1060 g_free( sHost );
1061 g_free( sBase );
1062 g_free( sBind );
1063 g_free( sCrit );
1065 if (sPass != NULL && strlen(sPass) > 0)
1066 memset(sPass, 0, strlen(sPass));
1067 g_free( sPass );
1069 return ads;
1072 #endif /* USE_LDAP */
1075 * End of Source.