From 50b84cf73a9dcf997175f2c170727033acbb0dca Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Mon, 9 Dec 2013 11:57:15 +0100 Subject: [PATCH] Working version of the LDAP configuration panel in GTK. --- coreapi/dict.c | 2 +- coreapi/ldap/ldapprovider.c | 33 +++++---- coreapi/linphonecore.h | 2 +- gtk/linphone.h | 1 + gtk/main.c | 27 +++++--- gtk/parameters.ui | 56 +++++++++++++-- gtk/propertybox.c | 135 ++++++++++++++++++++++++++++++++++++ 7 files changed, 223 insertions(+), 33 deletions(-) diff --git a/coreapi/dict.c b/coreapi/dict.c index 69b2bb4ee..4535e1a27 100644 --- a/coreapi/dict.c +++ b/coreapi/dict.c @@ -96,7 +96,7 @@ void linphone_dictionary_clear(LinphoneDictionary* obj) belle_sip_dict_clear(obj); } -int linphone_dictionary_haskey(LinphoneDictionary* obj, const char* key) +int linphone_dictionary_haskey(const LinphoneDictionary* obj, const char* key) { return belle_sip_dict_haskey(obj, key); } diff --git a/coreapi/ldap/ldapprovider.c b/coreapi/ldap/ldapprovider.c index 3bab710b2..ec7c2f57d 100644 --- a/coreapi/ldap/ldapprovider.c +++ b/coreapi/ldap/ldapprovider.c @@ -53,16 +53,17 @@ struct _LinphoneLDAPContactProvider // config int use_tls; LDAPAuthMethod auth_method; - char* username; - char* password; - char* server; + const char* username; + const char* password; + const char* server; - char* base_object; - char** attributes; - char* sip_attr; - char* name_attr; + const char* base_object; + const char* sip_attr; + const char* name_attr; + const char* filter; + + char** attributes; - char* filter; int timeout; int deref_aliases; int max_results; @@ -150,6 +151,7 @@ BELLE_SIP_INSTANCIATE_VPTR(LinphoneLDAPContactSearch,LinphoneContactSearch, static inline LinphoneLDAPContactSearch* linphone_ldap_request_entry_search( LinphoneLDAPContactProvider* obj, int msgid ); static unsigned int linphone_ldap_contact_provider_cancel_search(LinphoneContactProvider* obj, LinphoneContactSearch *req); static void linphone_ldap_contact_provider_conf_destroy(LinphoneLDAPContactProvider* obj ); +static bool_t linphone_ldap_contact_provider_iterate(void *data); /* Authentication methods */ struct AuthMethodDescription{ @@ -182,6 +184,9 @@ static void linphone_ldap_contact_provider_destroy_request_cb(void *req) static void linphone_ldap_contact_provider_destroy( LinphoneLDAPContactProvider* obj ) { + ms_message("linphone_ldap_contact_provider_destroy"); + linphone_core_remove_iterate_hook(LINPHONE_CONTACT_PROVIDER(obj)->lc, linphone_ldap_contact_provider_iterate,obj); + // clean pending requests ms_list_for_each(obj->requests, linphone_ldap_contact_provider_destroy_request_cb); @@ -399,7 +404,7 @@ static char* required_config_keys[] = { NULL }; -static bool_t linphone_ldap_contact_provider_valid_config(LinphoneDictionary* dict) +static bool_t linphone_ldap_contact_provider_valid_config(const LinphoneDictionary* dict) { char** config_name = required_config_keys; @@ -410,6 +415,7 @@ static bool_t linphone_ldap_contact_provider_valid_config(LinphoneDictionary* di has_key = linphone_dictionary_haskey(dict, *config_name); if( !has_key ) ms_error("Missing LDAP config value for '%s'", *config_name); valid &= has_key; + config_name++; } return valid; } @@ -426,9 +432,7 @@ static void linphone_ldap_contact_provider_loadconfig(LinphoneLDAPContactProvide if( obj->config ) linphone_dictionary_unref(obj->config); // clone new config into the dictionary - obj->config = linphone_dictionary_clone(dict); - linphone_dictionary_ref(obj->config); - + obj->config = linphone_dictionary_ref(linphone_dictionary_clone(dict)); obj->use_tls = linphone_dictionary_get_int(obj->config, "use_tls", 0); obj->timeout = linphone_dictionary_get_int(obj->config, "timeout", 10); @@ -480,8 +484,7 @@ static void linphone_ldap_contact_provider_loadconfig(LinphoneLDAPContactProvide static int linphone_ldap_contact_provider_bind( LinphoneLDAPContactProvider* obj ) { - const char* password_str = linphone_dictionary_get_string(obj, "password", ""); - struct berval password = { strlen( password_str ), password_str }; + struct berval password = { strlen( obj->password ), ms_strdup(obj->password) }; int ret; int bind_msgid = 0; @@ -505,6 +508,8 @@ static int linphone_ldap_contact_provider_bind( LinphoneLDAPContactProvider* obj break; } } + if(password.bv_val) ms_free(password.bv_val); + return 0; } diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 0345344cb..479163397 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -200,7 +200,7 @@ LINPHONE_PUBLIC void linphone_dictionary_set_int64(LinphoneDictionary* obj, cons LINPHONE_PUBLIC int64_t linphone_dictionary_get_int64(LinphoneDictionary* obj, const char* key, int64_t default_value); LINPHONE_PUBLIC int linphone_dictionary_remove(LinphoneDictionary* obj, const char* key); LINPHONE_PUBLIC void linphone_dictionary_clear(LinphoneDictionary* obj); -LINPHONE_PUBLIC int linphone_dictionary_haskey(LinphoneDictionary* obj, const char* key); +LINPHONE_PUBLIC int linphone_dictionary_haskey(const LinphoneDictionary* obj, const char* key); LINPHONE_PUBLIC void linphone_dictionary_foreach( const LinphoneDictionary* obj, void (*apply_func)(const char*key, void* value, void* userdata), void* userdata); /** * Converts a config section into a dictionary. diff --git a/gtk/linphone.h b/gtk/linphone.h index da398d9df..c77a02e6d 100644 --- a/gtk/linphone.h +++ b/gtk/linphone.h @@ -74,6 +74,7 @@ void linphone_gtk_close_assistant(void); LinphoneCore *linphone_gtk_get_core(void); LinphoneLDAPContactProvider* linphone_gtk_get_ldap(void); +void linphone_gtk_set_ldap(LinphoneLDAPContactProvider* ldap); GtkWidget *linphone_gtk_get_main_window(); void linphone_gtk_display_something(GtkMessageType type,const gchar *message); void linphone_gtk_start_call(GtkWidget *button); diff --git a/gtk/main.c b/gtk/main.c index f7eec5676..5dde19199 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -232,6 +232,22 @@ static const char *linphone_gtk_get_factory_config_file(){ return _factory_config_file; } +LinphoneLDAPContactProvider* linphone_gtk_get_ldap(void){ +#ifdef BUILD_LDAP + return ldap_provider; +#else + return NULL; +#endif +} + +void linphone_gtk_set_ldap(LinphoneLDAPContactProvider* ldap) +{ + if( ldap_provider ) + belle_sip_object_unref(ldap_provider); + + ldap_provider = LINPHONE_LDAP_CONTACT_PROVIDER(belle_sip_object_ref( ldap )); +} + static void linphone_gtk_init_liblinphone(const char *config_file, const char *factory_config_file, const char *db_file) { LinphoneCoreVTable vtable={0}; @@ -263,8 +279,7 @@ static void linphone_gtk_init_liblinphone(const char *config_file, if( lp_config_has_section(linphone_core_get_config(the_core),"ldap") ){ LpConfig* cfg = linphone_core_get_config(the_core); LinphoneDictionary* ldap_cfg = lp_config_section_to_dict(cfg, "ldap"); - ldap_provider = linphone_ldap_contact_provider_create(the_core, ldap_cfg); - belle_sip_object_ref( ldap_provider ); + linphone_gtk_set_ldap( linphone_ldap_contact_provider_create(the_core, ldap_cfg) ); } #endif @@ -285,14 +300,6 @@ LinphoneCore *linphone_gtk_get_core(void){ return the_core; } -LinphoneLDAPContactProvider* linphone_gtk_get_ldap(void){ -#ifdef BUILD_LDAP - return ldap_provider; -#else - return NULL; -#endif -} - GtkWidget *linphone_gtk_get_main_window(){ return the_ui; } diff --git a/gtk/parameters.ui b/gtk/parameters.ui index 99f0cb268..9a42760b8 100644 --- a/gtk/parameters.ui +++ b/gtk/parameters.ui @@ -2520,7 +2520,7 @@ - + Use TLS Connection True True @@ -2536,7 +2536,7 @@ - + True True @@ -2585,9 +2585,6 @@ 4 - - - True @@ -2851,7 +2848,7 @@ - + True True 3 @@ -2871,7 +2868,7 @@ - + Follow Aliases True True @@ -2908,6 +2905,51 @@ 2 + + + True + False + 2 + + + + + + Reset + True + True + True + False + + + + False + False + 1 + + + + + Save + True + True + True + False + + + + False + False + 2 + + + + + False + True + 3 + + 5 diff --git a/gtk/propertybox.c b/gtk/propertybox.c index 8579fc2f4..20c0b01cd 100644 --- a/gtk/propertybox.c +++ b/gtk/propertybox.c @@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "linphone.h" #include "linphone_tunnel.h" +#include "lpconfig.h" typedef enum { CAP_IGNORE, @@ -59,6 +60,131 @@ static void linphone_gtk_fill_combo_box(GtkWidget *combo, const char **devices, gtk_combo_box_set_active(GTK_COMBO_BOX(combo),active); } +static void linphone_gtk_ldap_load_settings(GtkWidget* param) +{ + GtkWidget *mw = linphone_gtk_get_main_window(); + GtkWidget *pb = (GtkWidget *) g_object_get_data(G_OBJECT(mw), "parameters"); + LpConfig* config = linphone_core_get_config(linphone_gtk_get_core()); + LinphoneDictionary* ldap_conf = lp_config_section_to_dict(config,"ldap"); + GtkEntry* entry; + GtkToggleButton* toggle; + GtkSpinButton* spin; + + + toggle = GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(pb,"ldap_use_tls")); + gtk_toggle_button_set_active(toggle, linphone_dictionary_get_int(ldap_conf,"use_tls", 0) ); + + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_server")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"server", "ldap://example.com") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_username")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"username", "") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_password")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"password", "") ); + + // TODO + // GtkComboBox* cbox = GTK_COMBO_BOX(linphone_gtk_get_widget(pb,"ldap_auth_method")); + // gtk_combo_box_set_active(entry, linphone_dictionary_get_string(ldap_conf,"auth_method", "anonymous") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_base_object")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"base_object", "dc=example,dc=com") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_filter")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"filter", "uid=*%s*") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_name_attribute")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"name_attribute", "cn") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_sip_attribute")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"sip_attribute", "mobile") ); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_attributes")); + gtk_entry_set_text(entry, linphone_dictionary_get_string(ldap_conf,"attributes", "cn,givenName,sn,mobile,homePhone") ); + + + toggle = GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(pb,"ldap_deref_aliases")); + gtk_toggle_button_set_active(toggle, linphone_dictionary_get_int(ldap_conf,"deref_aliases", 0) ); + + spin = GTK_SPIN_BUTTON(linphone_gtk_get_widget(pb,"ldap_max_results")); + gtk_spin_button_set_value(spin, linphone_dictionary_get_int(ldap_conf,"max_results", 50) ); + + spin = GTK_SPIN_BUTTON(linphone_gtk_get_widget(pb,"ldap_timeout")); + gtk_spin_button_set_value(spin, linphone_dictionary_get_int(ldap_conf,"timeout", 10) ); + +} + +void linphone_gtk_ldap_reset(GtkWidget *tabmgr) +{ + GtkWidget *mw = linphone_gtk_get_main_window(); + GtkWidget *pb = (GtkWidget *) g_object_get_data(G_OBJECT(mw), "parameters"); + ms_message("RESET LDAP"); + linphone_gtk_ldap_load_settings(pb); +} + +void linphone_gtk_ldap_save(GtkWidget *tabmgr) +{ + LinphoneCore *lc = linphone_gtk_get_core(); + LpConfig* conf = linphone_core_get_config(lc); + LinphoneDictionary* dict = linphone_dictionary_new(); + + GtkWidget *mw = linphone_gtk_get_main_window(); + GtkWidget *pb = (GtkWidget *) g_object_get_data(G_OBJECT(mw), "parameters"); + GtkEntry* entry; + GtkToggleButton* toggle; + GtkSpinButton* spin; + + ms_message("SAVE LDAP"); + + toggle = GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(pb,"ldap_use_tls")); + linphone_dictionary_set_int(dict, "use_tls", gtk_toggle_button_get_active(toggle)); + + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_server")); + linphone_dictionary_set_string(dict, "server", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_username")); + linphone_dictionary_set_string(dict, "username", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_password")); + linphone_dictionary_set_string(dict, "password", gtk_entry_get_text(entry)); + + + GtkComboBox* cbox = GTK_COMBO_BOX(linphone_gtk_get_widget(pb,"ldap_auth_method")); + linphone_dictionary_set_string(dict, "auth_method", gtk_combo_box_get_active_text(cbox)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_base_object")); + linphone_dictionary_set_string(dict, "base_object", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_filter")); + linphone_dictionary_set_string(dict, "filter", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_name_attribute")); + linphone_dictionary_set_string(dict, "name_attribute", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_sip_attribute")); + linphone_dictionary_set_string(dict, "sip_attribute", gtk_entry_get_text(entry)); + + entry = GTK_ENTRY(linphone_gtk_get_widget(pb,"ldap_attributes")); + linphone_dictionary_set_string(dict, "attributes", gtk_entry_get_text(entry)); + + toggle = GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(pb,"ldap_deref_aliases")); + linphone_dictionary_set_int(dict, "deref_aliases", gtk_toggle_button_get_active(toggle)); + + spin = GTK_SPIN_BUTTON(linphone_gtk_get_widget(pb,"ldap_max_results")); + linphone_dictionary_set_int(dict, "max_results", gtk_spin_button_get_value(spin) ); + + spin = GTK_SPIN_BUTTON(linphone_gtk_get_widget(pb,"ldap_timeout")); + linphone_dictionary_set_int(dict, "timeout", gtk_spin_button_get_value(spin) ); + + ms_message("Create LDAP from config"); + // create new LDAP according to the validated config + linphone_gtk_set_ldap( linphone_ldap_contact_provider_create(lc, dict) ); + // save the config to linphonerc: + lp_config_load_dict_to_section(conf, "ldap", dict); +} + void linphone_gtk_fill_video_sizes(GtkWidget *combo){ const MSVideoSizeDef *def=linphone_core_get_supported_video_sizes(linphone_gtk_get_core());; int i,active=0; @@ -1301,6 +1427,15 @@ void linphone_gtk_show_parameters(void){ gtk_widget_set_visible(GTK_WIDGET(linphone_gtk_get_widget(pb,"tunnel_label")), TRUE); } + /* LDAP CONFIG */ +#ifdef BUILD_LDAP + linphone_gtk_ldap_load_settings(pb); +#else + // hide the LDAP tab + GtkNotebook* notebook = GTK_NOTEBOOK(linphone_gtk_get_widget(pb, "notebook1")); + gtk_notebook_remove_page(notebook,5); +#endif + gtk_widget_show(pb); }