1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 /*
20  * LDAP Base DN selection dialog.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #include "claws-features.h"
26 #endif
27 
28 #ifdef USE_LDAP
29 
30 #include "defs.h"
31 
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
36 
37 #include "prefs_common.h"
38 #include "ldaputil.h"
39 #include "mgutils.h"
40 #include "gtkutils.h"
41 #include "manage_window.h"
42 
43 static struct _LDAPEdit_basedn {
44 	GtkWidget *window;
45 	GtkWidget *host_label;
46 	GtkWidget *port_label;
47 	GtkWidget *basedn_entry;
48 	GtkWidget *basedn_list;
49 	GtkWidget *ok_btn;
50 	GtkWidget *cancel_btn;
51 	GtkWidget *statusbar;
52 	gint status_cid;
53 } ldapedit_basedn;
54 
55 static gboolean ldapedit_basedn_cancelled;
56 static gboolean ldapedit_basedn_bad_server;
57 
58 /*
59 * Edit functions.
60 */
edit_ldap_bdn_status_show(gchar * msg)61 static void edit_ldap_bdn_status_show( gchar *msg ) {
62 	if( ldapedit_basedn.statusbar != NULL ) {
63 		gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
64 		if( msg ) {
65 			gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
66 		}
67 	}
68 }
69 
edit_ldap_bdn_delete_event(GtkWidget * widget,GdkEventAny * event,gboolean * cancelled)70 static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
71 	ldapedit_basedn_cancelled = TRUE;
72 	gtk_main_quit();
73 	return TRUE;
74 }
75 
edit_ldap_bdn_key_pressed(GtkWidget * widget,GdkEventKey * event,gboolean * cancelled)76 static gboolean edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
77 	if (event && event->keyval == GDK_KEY_Escape) {
78 		ldapedit_basedn_cancelled = TRUE;
79 		gtk_main_quit();
80 	}
81 	return FALSE;
82 }
83 
edit_ldap_bdn_ok(GtkWidget * widget,gboolean * cancelled)84 static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
85 	ldapedit_basedn_cancelled = FALSE;
86 	gtk_main_quit();
87 }
88 
edit_ldap_bdn_cancel(GtkWidget * widget,gboolean * cancelled)89 static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
90 	ldapedit_basedn_cancelled = TRUE;
91 	gtk_main_quit();
92 }
93 
set_selected()94 static void set_selected()
95 {
96 	GtkWidget *entry = ldapedit_basedn.basedn_entry;
97 	GtkWidget *view = ldapedit_basedn.basedn_list;
98 	gchar *text;
99 
100 	if (entry == NULL || view == NULL)
101 		return;
102 
103 	text = gtkut_tree_view_get_selected_pointer(
104 			GTK_TREE_VIEW(view), 0, NULL, NULL, NULL);
105 
106 	if (text == NULL)
107 		return;
108 
109 	gtk_entry_set_text(GTK_ENTRY(entry), text);
110 	g_free(text);
111 }
112 
basedn_list_cursor_changed(GtkTreeView * view,gpointer user_data)113 static void basedn_list_cursor_changed(GtkTreeView *view,
114 		gpointer user_data)
115 {
116 	set_selected();
117 }
118 
basedn_list_row_activated(GtkTreeView * view,GtkTreePath * path,GtkTreeViewColumn * column,gpointer user_data)119 static void basedn_list_row_activated(GtkTreeView *view,
120 		GtkTreePath *path,
121 		GtkTreeViewColumn *column,
122 		gpointer user_data)
123 {
124 	set_selected();
125 	edit_ldap_bdn_ok(NULL, NULL);
126 }
127 
edit_ldap_bdn_create(void)128 static void edit_ldap_bdn_create(void) {
129 	GtkWidget *window;
130 	GtkWidget *vbox;
131 	GtkWidget *table;
132 	GtkWidget *label;
133 	GtkWidget *host_label;
134 	GtkWidget *port_label;
135 	GtkWidget *basedn_list;
136 	GtkWidget *vlbox;
137 	GtkWidget *lwindow;
138 	GtkWidget *basedn_entry;
139 	GtkWidget *hbbox;
140 	GtkWidget *hsep;
141 	GtkWidget *ok_btn;
142 	GtkWidget *cancel_btn;
143 	GtkWidget *hsbox;
144 	GtkWidget *statusbar;
145 	gint top;
146 	GtkListStore *store;
147 	GtkTreeSelection *sel;
148 	GtkTreeViewColumn *col;
149 	GtkCellRenderer *rdr;
150 
151 	window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editldap_basedn");
152 	gtk_container_set_border_width(GTK_CONTAINER(window), 0);
153 	gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
154 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
155 	gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
156 	g_signal_connect(G_OBJECT(window), "delete_event",
157 			 G_CALLBACK(edit_ldap_bdn_delete_event), NULL );
158 	g_signal_connect(G_OBJECT(window), "key_press_event",
159 			 G_CALLBACK(edit_ldap_bdn_key_pressed), NULL );
160 
161 	vbox = gtk_vbox_new(FALSE, 8);
162 	gtk_container_add(GTK_CONTAINER(window), vbox);
163 	gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
164 
165 	table = gtk_table_new(3, 2, FALSE);
166 	gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
167 	gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
168 	gtk_table_set_row_spacings(GTK_TABLE(table), 8);
169 	gtk_table_set_col_spacings(GTK_TABLE(table), 8);
170 
171 	/* First row */
172 	top = 0;
173 	label = gtk_label_new(_("Hostname"));
174 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
175 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
176 
177 	host_label = gtk_label_new("");
178 	gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
179 	gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
180 
181 	/* Second row */
182 	top = 1;
183 	label = gtk_label_new(_("Port"));
184 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
186 
187 	port_label = gtk_label_new("");
188 	gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
189 	gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
190 
191 	/* Third row */
192 	top = 2;
193 	label = gtk_label_new(_("Search Base"));
194 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
195 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
196 
197 	basedn_entry = gtk_entry_new();
198 	gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
199 
200 	/* Basedn list */
201 	vlbox = gtk_vbox_new(FALSE, 8);
202 	gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
203 	gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
204 
205 	lwindow = gtk_scrolled_window_new(NULL, NULL);
206 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
207 				       GTK_POLICY_AUTOMATIC,
208 				       GTK_POLICY_AUTOMATIC);
209 	gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
210 
211 	store = gtk_list_store_new(1, G_TYPE_STRING, -1);
212 
213 	basedn_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
214 	g_object_unref(store);
215 	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(basedn_list), TRUE);
216 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(basedn_list));
217 	gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
218 
219 	rdr = gtk_cell_renderer_text_new();
220 	col = gtk_tree_view_column_new_with_attributes(
221 			_("Available Search Base(s)"), rdr,
222 			"markup", 0, NULL);
223 	gtk_tree_view_append_column(GTK_TREE_VIEW(basedn_list), col);
224 
225 	gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
226 
227 	g_signal_connect(G_OBJECT(basedn_list), "cursor-changed",
228 			G_CALLBACK(basedn_list_cursor_changed), NULL);
229 	g_signal_connect(G_OBJECT(basedn_list), "row-activated",
230 			G_CALLBACK(basedn_list_row_activated), NULL);
231 
232 
233 	/* Status line */
234 	hsbox = gtk_hbox_new(FALSE, 0);
235 	gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
236 	statusbar = gtk_statusbar_new();
237 	gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
238 
239 	/* Button panel */
240 	gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
241 				      &cancel_btn, GTK_STOCK_CANCEL,
242 				      NULL, NULL);
243 	gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
244 	gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
245 	gtk_widget_grab_default(ok_btn);
246 
247 	hsep = gtk_hseparator_new();
248 	gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
249 
250 	g_signal_connect(G_OBJECT(ok_btn), "clicked",
251 			 G_CALLBACK(edit_ldap_bdn_ok), NULL);
252 	g_signal_connect(G_OBJECT(cancel_btn), "clicked",
253 			 G_CALLBACK(edit_ldap_bdn_cancel), NULL);
254 
255 	gtk_widget_show_all(vbox);
256 
257 	ldapedit_basedn.window     = window;
258 	ldapedit_basedn.host_label = host_label;
259 	ldapedit_basedn.port_label = port_label;
260 	ldapedit_basedn.basedn_entry = basedn_entry;
261 	ldapedit_basedn.basedn_list  = basedn_list;
262 	ldapedit_basedn.ok_btn     = ok_btn;
263 	ldapedit_basedn.cancel_btn = cancel_btn;
264 	ldapedit_basedn.statusbar  = statusbar;
265 	ldapedit_basedn.status_cid =
266 		gtk_statusbar_get_context_id(
267 			GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
268 }
269 
edit_ldap_bdn_load_data(const gchar * hostName,const gint iPort,const gint tov,const gchar * bindDN,const gchar * bindPW,int ssl,int tls)270 static void edit_ldap_bdn_load_data(
271 	const gchar *hostName, const gint iPort, const gint tov,
272 	const gchar* bindDN, const gchar *bindPW, int ssl, int tls )
273 {
274 	gchar *sHost;
275 	gchar *sMsg = NULL;
276 	gchar sPort[20];
277 	gboolean flgConn;
278 	gboolean flgDN;
279 	GList *baseDN = NULL;
280 	GtkWidget *view = ldapedit_basedn.basedn_list;
281 	GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
282 	GtkTreeIter iter;
283 	GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
284 
285 	edit_ldap_bdn_status_show( "" );
286 
287 	gtk_list_store_clear(GTK_LIST_STORE(model));
288 
289 	ldapedit_basedn_bad_server = TRUE;
290 	flgConn = flgDN = FALSE;
291 	sHost = g_strdup( hostName );
292 	sprintf( sPort, "%d", iPort );
293 	gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
294 	gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
295 	if( *sHost != '\0' ) {
296 		/* Test connection to server */
297 		if( ldaputil_test_connect( sHost, iPort, ssl, tls, tov ) ) {
298 			/* Attempt to read base DN */
299 			baseDN = ldaputil_read_basedn( sHost, iPort, bindDN, bindPW, tov, ssl, tls );
300 			if( baseDN ) {
301 				GList *node = baseDN;
302 
303 				while( node ) {
304 					gtk_list_store_append(GTK_LIST_STORE(model), &iter);
305 					gtk_list_store_set(GTK_LIST_STORE(model), &iter,
306 							0, (gchar *)node->data, -1);
307 					node = g_list_next( node );
308 					flgDN = TRUE;
309 				}
310 
311 				/* Select the first line */
312 				if (gtk_tree_model_get_iter_first(model, &iter))
313 					gtk_tree_selection_select_iter(sel, &iter);
314 
315 				g_list_free_full( baseDN, g_free );
316 				baseDN = node = NULL;
317 			}
318 			ldapedit_basedn_bad_server = FALSE;
319 			flgConn = TRUE;
320 		}
321 	}
322 	g_free( sHost );
323 
324 	/* Display appropriate message */
325 	if( flgConn ) {
326 		if( ! flgDN ) {
327 			sMsg = _( "Could not read Search Base(s) from server - please set manually" );
328 		}
329 	}
330 	else {
331 		sMsg = _( "Could not connect to server" );
332 	}
333 	edit_ldap_bdn_status_show( sMsg );
334 }
335 
edit_ldap_basedn_selection(const gchar * hostName,const gint port,gchar * baseDN,const gint tov,const gchar * bindDN,const gchar * bindPW,int ssl,int tls)336 gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
337 	       const gchar* bindDN, const gchar *bindPW, int ssl, int tls ) {
338 	gchar *retVal = NULL;
339 
340 	ldapedit_basedn_cancelled = FALSE;
341 	if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
342 	gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
343 	gtk_widget_show(ldapedit_basedn.window);
344 	manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
345 	gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), TRUE);
346 	edit_ldap_bdn_status_show( "" );
347 	edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW, ssl, tls );
348 	gtk_widget_show(ldapedit_basedn.window);
349 
350 	gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
351 
352 	gtk_main();
353 	gtk_widget_hide(ldapedit_basedn.window);
354 	gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), FALSE);
355 	if( ldapedit_basedn_cancelled ) return NULL;
356 	if( ldapedit_basedn_bad_server ) return NULL;
357 
358 	retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
359 	g_strstrip( retVal );
360 	if( *retVal == '\0' ) {
361 		g_free( retVal );
362 		retVal = NULL;
363 	}
364 	return retVal;
365 }
366 
367 #endif /* USE_LDAP */
368 
369 /*
370 * End of Source.
371 */
372 
373