2 * Asterisk -- A telephony toolkit for Linux.
4 * Copyright (C) 2005, Oxymium sarl
5 * Manuel Guesdon <mguesdon@oxymium.net> - LDAP RealTime Driver Author/Adaptor
7 * Copyright (C) 2007, Digium, Inc.
8 * Russell Bryant <russell@digium.com>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
24 * \brief ldap plugin for portable configuration engine (ARA)
26 * \author Mark Spencer <markster@digium.com>
27 * \author Manuel Guesdon
28 * \author Carl-Einar Thorner <cthorner@voicerd.com>
29 * \author Russell Bryant <russell@digium.com>
31 * \extref OpenLDAP http://www.openldap.org
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48 #include "asterisk/channel.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/config.h"
51 #include "asterisk/module.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/options.h"
54 #include "asterisk/cli.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/strings.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/linkedlists.h"
60 #define RES_CONFIG_LDAP_CONF "res_ldap.conf"
61 #define RES_CONFIG_LDAP_DEFAULT_BASEDN "asterisk"
63 AST_MUTEX_DEFINE_STATIC(ldap_lock);
65 static LDAP *ldapConn;
67 static char user[512];
68 static char pass[512];
69 static char base_distinguished_name[512];
71 static time_t connect_time;
73 static int parse_config(void);
74 static int ldap_reconnect(void);
75 static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
77 struct category_and_metric {
80 const char *variable_name;
81 const char *variable_value;
82 int var_metric; /*!< For organizing variables (particularly includes and switch statments) within a context */
85 /*! \brief Table configuration */
86 struct ldap_table_config {
87 char *table_name; /*!< table name */
88 char *additional_filter; /*!< additional filter */
89 struct ast_variable *attributes; /*!< attribute names conversion */
90 struct ast_variable *delimiters; /*!< the current delimiter is semicolon, so we are not using this variable */
91 AST_LIST_ENTRY(ldap_table_config) entry;
92 /* TODO: Make proxies work */
95 /*! \brief Should be locked before using it */
96 static AST_LIST_HEAD_NOLOCK_STATIC(table_configs, ldap_table_config);
97 static struct ldap_table_config *base_table_config;
98 static struct ldap_table_config *static_table_config;
100 static struct ast_cli_entry ldap_cli[] = {
101 AST_CLI_DEFINE(realtime_ldap_status, "Shows connection information for the LDAP RealTime driver"),
104 /*! \brief Create a new table_config */
105 static struct ldap_table_config *table_config_new(const char *table_name)
107 struct ldap_table_config *p;
109 if (!(p = ast_calloc(1, sizeof(*p))))
113 if (!(p->table_name = ast_strdup(table_name))) {
122 /*! \brief Find a table_config - Should be locked before using it
123 * \note This function assumes ldap_lock to be locked. */
124 static struct ldap_table_config *table_config_for_table_name(const char *table_name)
126 struct ldap_table_config *c = NULL;
128 AST_LIST_TRAVERSE(&table_configs, c, entry) {
129 if (!strcmp(c->table_name, table_name))
136 /*! \brief Find variable by name */
137 static struct ast_variable *variable_named(struct ast_variable *var, const char *name)
139 for (; var; var = var->next) {
140 if (!strcasecmp(name, var->name))
147 /*! \brief for the semicolon delimiter
148 \param somestr - pointer to a string
150 \return number of occurances of the delimiter(semicolon)
152 static int semicolon_count_str(const char *somestr)
156 for (; *somestr; somestr++) {
164 /* takes a linked list of \a ast_variable variables, finds the one with the name variable_value
165 * and returns the number of semicolons in the value for that \a ast_variable
167 static int semicolon_count_var(struct ast_variable *var)
169 struct ast_variable *var_value = variable_named(var, "variable_value");
175 ast_debug(2, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
177 return semicolon_count_str(var_value->value);
180 /*! \brief add attribute to table config - Should be locked before using it */
181 static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
182 const char *attribute_name, const char *attribute_value)
184 struct ast_variable *var;
186 if (ast_strlen_zero(attribute_name) || ast_strlen_zero(attribute_value)) {
190 if (!(var = ast_variable_new(attribute_name, attribute_value, table_config->table_name))) {
194 if (table_config->attributes) {
195 var->next = table_config->attributes;
197 table_config->attributes = var;
200 /*! \brief Free table_config
201 * \note assumes ldap_lock to be locked */
202 static void table_configs_free(void)
204 struct ldap_table_config *c;
206 while ((c = AST_LIST_REMOVE_HEAD(&table_configs, entry))) {
208 ast_free(c->table_name);
210 if (c->additional_filter) {
211 ast_free(c->additional_filter);
214 ast_variables_destroy(c->attributes);
219 base_table_config = NULL;
220 static_table_config = NULL;
223 /*! \brief Convert variable name to ldap attribute name - Should be locked before using it */
224 static const char *convert_attribute_name_to_ldap(struct ldap_table_config *table_config,
225 const char *attribute_name)
228 struct ldap_table_config *configs[] = { table_config, base_table_config };
230 for (i = 0; i < ARRAY_LEN(configs); i++) {
231 struct ast_variable *attribute;
237 attribute = configs[i]->attributes;
238 for (; attribute; attribute = attribute->next) {
239 if (!strcasecmp(attribute_name, attribute->name)) {
240 return attribute->value;
245 return attribute_name;
248 /*! \brief Convert ldap attribute name to variable name
249 \note Should be locked before using it */
250 static const char *convert_attribute_name_from_ldap(struct ldap_table_config *table_config,
251 const char *attribute_name)
254 struct ldap_table_config *configs[] = { table_config, base_table_config };
256 for (i = 0; i < ARRAY_LEN(configs); i++) {
257 struct ast_variable *attribute;
263 attribute = configs[i]->attributes;
264 for (; attribute; attribute = attribute->next) {
265 if (strcasecmp(attribute_name, attribute->value) == 0) {
266 return attribute->name;
271 return attribute_name;
274 /*! \brief Get variables from ldap entry attributes
275 \note Should be locked before using it
276 \return a linked list of ast_variable variables.
278 static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config *table_config,
279 LDAPMessage *ldap_entry)
281 BerElement *ber = NULL;
282 struct ast_variable *var = NULL;
283 struct ast_variable *prev = NULL;
284 int is_delimited = 0;
286 char *ldap_attribute_name;
287 struct berval *value;
290 ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
292 while (ldap_attribute_name) {
293 struct berval **values = NULL;
294 const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
295 int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
297 values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name); /* these are freed at the end */
302 for (v = values; *v; v++) {
304 valptr = value->bv_val;
305 ast_debug(2, "LINE(%d) attribute_name: %s LDAP value: %s\n", __LINE__, attribute_name, valptr);
306 if (is_realmed_password_attribute) {
307 if (!strncasecmp(valptr, "{md5}", 5)) {
310 ast_debug(2, "md5: %s\n", valptr);
313 /* ok, so looping through all delimited values except the last one (not, last character is not delimited...) */
317 while (!ast_strlen_zero(valptr + i)) {
318 if (valptr[i] == ';') {
321 prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
326 prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
333 /* for the last delimited value or if the value is not delimited: */
335 prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
340 prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
344 ldap_value_free_len(values);
346 ldap_memfree(ldap_attribute_name);
347 ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
354 /*! \brief Get variables from ldap entry attributes - Should be locked before using it
356 * The results are freed outside this function so is the \a vars array.
358 * \return \a vars - an array of ast_variable variables terminated with a null.
360 static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_config *table_config,
361 LDAPMessage *ldap_result_msg, unsigned int *entries_count_ptr)
363 struct ast_variable **vars;
367 LDAPMessage *ldap_entry = NULL;
368 BerElement *ber = NULL;
369 struct ast_variable *var = NULL;
370 struct ast_variable *prev = NULL;
371 int is_delimited = 0;
372 char *delim_value = NULL;
373 int delim_tot_count = 0;
376 /* First find the total count */
377 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
379 for (tot_count = 0; ldap_entry; tot_count++) {
380 struct ast_variable *tmp = realtime_ldap_entry_to_var(table_config, ldap_entry);
381 tot_count += semicolon_count_var(tmp);
382 ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
383 ast_variables_destroy(tmp);
386 if (entries_count_ptr) {
387 *entries_count_ptr = tot_count;
390 /* Now that we have the total count we allocate space and create the variables
391 * Remember that each element in vars is a linked list that points to realtime variable.
392 * If the we are dealing with a static realtime variable we create a new element in the \a vars array for each delimited
393 * value in \a variable_value; otherwise, we keep \a vars static and increase the length of the linked list of variables in the array element.
394 * This memory must be freed outside of this function. */
395 vars = ast_calloc(sizeof(struct ast_variable *), tot_count + 1);
397 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
401 /* For each static realtime variable we may create several entries in the \a vars array if it's delimited */
402 for (entry_index = 0; ldap_entry; ) {
408 do { /* while delim_count */
410 /* Starting new static var */
411 char *ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
412 struct berval *value;
413 while (ldap_attribute_name) {
415 const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
416 int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
417 struct berval **values = NULL;
419 values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);
424 for (v = values; *v; v++) {
426 valptr = value->bv_val;
427 if (is_realmed_password_attribute) {
428 if (strncasecmp(valptr, "{md5}", 5) == 0) {
431 ast_debug(2, "md5: %s\n", valptr);
434 if (delim_value == NULL && !is_realmed_password_attribute
435 && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0)) {
437 delim_value = ast_strdup(valptr);
439 if ((delim_tot_count = semicolon_count_str(delim_value)) > 0) {
440 ast_debug(4, "LINE(%d) is delimited %d times: %s\n", __LINE__, delim_tot_count, delim_value);
445 if (is_delimited != 0 && !is_realmed_password_attribute
446 && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0) ) {
447 /* for non-Static RealTime, first */
449 for (i = pos; !ast_strlen_zero(valptr + i); i++) {
450 ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
451 if (delim_value[i] == ';') {
452 delim_value[i] = '\0';
454 ast_debug(2, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
457 prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
462 prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
466 if (static_table_config == table_config) {
471 if (ast_strlen_zero(valptr + i)) {
472 ast_debug(4, "LINE(%d) DELIM pos: %d i: %d delim_count: %d\n", __LINE__, pos, i, delim_count);
473 /* Last delimited value */
474 ast_debug(4, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
476 prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
481 prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
483 /* Remembering to free memory */
490 ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
497 ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
500 prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
505 prev = var = ast_variable_new(attribute_name, valptr, table_config->table_name);
509 } /*!< for (v = values; *v; v++) */
510 ldap_value_free_len(values);
512 ldap_memfree(ldap_attribute_name);
513 ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
514 } /*!< while (ldap_attribute_name) */
516 if (static_table_config == table_config) {
517 if (option_debug > 2) {
518 const struct ast_variable *tmpdebug = variable_named(var, "variable_name");
519 const struct ast_variable *tmpdebug2 = variable_named(var, "variable_value");
520 if (tmpdebug && tmpdebug2) {
521 ast_debug(3, "LINE(%d) Added to vars - %s = %s\n", __LINE__, tmpdebug->value, tmpdebug2->value);
524 vars[entry_index++] = var;
529 } while (delim_count <= delim_tot_count && static_table_config == table_config);
531 if (static_table_config != table_config) {
532 ast_debug(3, "LINE(%d) Added to vars - non static\n", __LINE__);
534 vars[entry_index++] = var;
537 ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
538 } /*!< end for loop over ldap_entry */
544 /*! \brief Check if we have a connection error */
545 static int is_ldap_connect_error(int err)
547 return (err == LDAP_SERVER_DOWN || err == LDAP_TIMEOUT || err == LDAP_CONNECT_ERROR);
550 /*! \brief Get LDAP entry by dn and return attributes as variables - Should be locked before using it
551 This is used for setting the default values of an object(i.e., with accountBaseDN)
553 static struct ast_variable *ldap_loadentry(struct ldap_table_config *table_config,
557 ast_log(LOG_ERROR, "No table config\n");
560 struct ast_variable **vars = NULL;
561 struct ast_variable *var = NULL;
563 LDAPMessage *ldap_result_msg = NULL;
566 ast_debug(2, "ldap_loadentry dn=%s\n", dn);
569 result = ldap_search_ext_s(ldapConn, dn, LDAP_SCOPE_BASE,
570 "(objectclass=*)", NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &ldap_result_msg);
571 if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
572 ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
575 usleep(500000L * tries);
577 ldap_unbind_ext_s(ldapConn, NULL, NULL);
580 if (!ldap_reconnect()) {
585 } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
587 if (result != LDAP_SUCCESS) {
588 ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
589 ast_debug(2, "dn=%s\n", dn);
590 ast_mutex_unlock(&ldap_lock);
594 unsigned int *entries_count_ptr = NULL; /*!< not using this */
596 if ((num_entry = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
597 ast_debug(3, "num_entry: %d\n", num_entry);
599 vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
601 ast_log(LOG_NOTICE, "More than one entry for dn=%s. Take only 1st one\n", dn);
604 ast_debug(2, "Could not find any entry dn=%s.\n", dn);
607 ldap_msgfree(ldap_result_msg);
609 /* Chopping \a vars down to one variable */
611 struct ast_variable **p = vars;
615 ast_variables_destroy(var);
618 vars = ast_realloc(vars, sizeof(struct ast_variable *));
627 /*! \note caller should free returned pointer */
628 static char *substituted(struct ast_channel *channel, const char *string)
630 #define MAXRESULT 2048
631 char *ret_string = NULL;
633 if (!ast_strlen_zero(string)) {
634 ret_string = ast_calloc(1, MAXRESULT);
635 pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1);
637 ast_debug(2, "substituted: string: '%s' => '%s' \n", string, ret_string);
641 /*! \note caller should free returned pointer */
642 static char *cleaned_basedn(struct ast_channel *channel, const char *basedn)
644 char *cbasedn = NULL;
647 cbasedn = substituted(channel, basedn);
648 if (*cbasedn == '"') {
650 if (!ast_strlen_zero(cbasedn)) {
651 int len = strlen(cbasedn);
652 if (cbasedn[len - 1] == '"')
653 cbasedn[len - 1] = '\0';
664 ast_debug(2, "basedn: '%s' => '%s' \n", basedn, cbasedn);
668 /*! \brief Replace \<search\> by \<by\> in string.
669 \note No check is done on string allocated size ! */
670 static int replace_string_in_string(char *string, const char *search, const char *by)
672 int search_len = strlen(search);
673 int by_len = strlen(by);
675 char *p = strstr(string, search);
680 if (by_len == search_len) {
681 memcpy(p, by, by_len);
683 memmove(p + by_len, p + search_len, strlen(p + search_len) + 1);
684 memcpy(p, by, by_len);
686 p = strstr(p + by_len, search);
692 /*! \brief Append a name=value filter string. The filter string can grow. */
693 static void append_var_and_value_to_filter(struct ast_str **filter,
694 struct ldap_table_config *table_config,
695 const char *name, const char *value)
697 char *new_name = NULL;
698 char *new_value = NULL;
699 char *like_pos = strstr(name, " LIKE");
701 ast_debug(2, "name='%s' value='%s'\n", name, value);
704 int len = like_pos - name;
706 name = new_name = ast_strdupa(name);
707 new_name[len] = '\0';
708 value = new_value = ast_strdupa(value);
709 replace_string_in_string(new_value, "\\_", "_");
710 replace_string_in_string(new_value, "%", "*");
713 name = convert_attribute_name_to_ldap(table_config, name);
715 ast_str_append(filter, 0, "(%s=%s)", name, value);
718 /*! \brief LDAP base function
719 * \return a null terminated array of ast_variable (one per entry) or NULL if no entry is found or if an error occured
720 * caller should free the returned array and ast_variables
721 * \param entries_count_ptr is a pointer to found entries count (can be NULL)
722 * \param basedn is the base DN
723 * \param table_name is the table_name (used dor attribute convertion and additional filter)
724 * \param ap contains null terminated list of pairs name/value
726 static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_ptr,
727 const char *basedn, const char *table_name, va_list ap)
729 struct ast_variable **vars = NULL;
730 const char *newparam = NULL;
731 const char *newval = NULL;
732 struct ldap_table_config *table_config = NULL;
733 char *clean_basedn = cleaned_basedn(NULL, basedn);
734 struct ast_str *filter = NULL;
737 LDAPMessage *ldap_result_msg = NULL;
740 ast_log(LOG_ERROR, "No table_name specified.\n");
741 ast_free(clean_basedn);
745 if (!(filter = ast_str_create(80))) {
746 ast_log(LOG_ERROR, "Can't initialize data structures.n");
747 ast_free(clean_basedn);
751 /* Get the first parameter and first value in our list of passed paramater/value pairs */
752 newparam = va_arg(ap, const char *);
753 newval = va_arg(ap, const char *);
755 if (!newparam || !newval) {
756 ast_log(LOG_ERROR, "Realtime retrieval requires at least 1 parameter"
757 " and 1 value to search on.\n");
759 ast_free(clean_basedn);
763 ast_mutex_lock(&ldap_lock);
765 /* We now have our complete statement; Lets connect to the server and execute it. */
766 if (!ldap_reconnect()) {
767 ast_mutex_unlock(&ldap_lock);
769 ast_free(clean_basedn);
773 table_config = table_config_for_table_name(table_name);
775 ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
776 ast_mutex_unlock(&ldap_lock);
778 ast_free(clean_basedn);
782 ast_str_append(&filter, 0, "(&");
784 if (table_config && table_config->additional_filter) {
785 ast_str_append(&filter, 0, "%s", table_config->additional_filter);
787 if (table_config != base_table_config && base_table_config &&
788 base_table_config->additional_filter) {
789 ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
792 /* Create the first part of the query using the first parameter/value pairs we just extracted */
793 /* If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
795 append_var_and_value_to_filter(&filter, table_config, newparam, newval);
796 while ((newparam = va_arg(ap, const char *))) {
797 newval = va_arg(ap, const char *);
798 append_var_and_value_to_filter(&filter, table_config, newparam, newval);
800 ast_str_append(&filter, 0, ")");
803 /* freeing ldap_result further down */
804 result = ldap_search_ext_s(ldapConn, clean_basedn,
805 LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
807 if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
808 ast_log(LOG_DEBUG, "Failed to query directory. Try %d/10\n", tries + 1);
812 ldap_unbind_ext_s(ldapConn, NULL, NULL);
815 if (!ldap_reconnect()) {
820 } while (result != LDAP_SUCCESS && tries < 10 && is_ldap_connect_error(result));
822 if (result != LDAP_SUCCESS) {
823 ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
824 ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
826 /* this is where we create the variables from the search result
827 * freeing this \a vars outside this function */
828 if (ldap_count_entries(ldapConn, ldap_result_msg) > 0) {
829 /* is this a static var or some other? they are handled different for delimited values */
830 vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
832 ast_debug(1, "Could not find any entry matching %s in base dn %s.\n", ast_str_buffer(filter), clean_basedn);
835 ldap_msgfree(ldap_result_msg);
837 /* TODO: get the default variables from the accountBaseDN, not implemented with delimited values */
839 struct ast_variable **p = vars;
841 struct ast_variable *append_var = NULL;
842 struct ast_variable *tmp = *p;
844 if (strcasecmp(tmp->name, "accountBaseDN") == 0) {
845 /* Get the variable to compare with for the defaults */
846 struct ast_variable *base_var = ldap_loadentry(table_config, tmp->value);
849 struct ast_variable *next = base_var->next;
850 struct ast_variable *test_var = *p;
851 int base_var_found = 0;
853 /* run throught the default values and fill it inn if it is missing */
855 if (strcasecmp(test_var->name, base_var->name) == 0) {
859 test_var = test_var->next;
862 if (base_var_found) {
863 base_var->next = NULL;
864 ast_variables_destroy(base_var);
868 base_var->next = append_var;
870 base_var->next = NULL;
872 append_var = base_var;
877 if (!tmp->next && append_var) {
878 tmp->next = append_var;
894 ast_free(clean_basedn);
897 ast_mutex_unlock(&ldap_lock);
902 /*! \brief same as realtime_ldap_base_ap but take variable arguments count list */
903 static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
904 const char *basedn, const char *table_name, ...)
906 struct ast_variable **vars = NULL;
909 va_start(ap, table_name);
910 vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, ap);
916 /*! \brief See Asterisk doc
918 * For Realtime Dynamic(i.e., switch, queues, and directory) -- I think
920 static struct ast_variable *realtime_ldap(const char *basedn,
921 const char *table_name, va_list ap)
923 struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, ap);
924 struct ast_variable *var = NULL;
927 struct ast_variable *last_var = NULL;
928 struct ast_variable **p = vars;
931 while (last_var->next) {
932 last_var = last_var->next;
946 /*! \brief See Asterisk doc
948 * this function will be called for the switch statment if no match is found with the realtime_ldap function(i.e. it is a failover);
949 * however, the ast_load_realtime wil match on wildcharacters also depending on what the mode is set to
950 * this is an area of asterisk that could do with a lot of modification
951 * I think this function returns Realtime dynamic objects
953 static struct ast_config *realtime_multi_ldap(const char *basedn,
954 const char *table_name, va_list ap)
957 const char *initfield = NULL;
958 const char *newparam, *newval;
959 struct ast_variable **vars =
960 realtime_ldap_base_ap(NULL, basedn, table_name, ap);
961 struct ast_config *cfg = NULL;
963 newparam = va_arg(ap, const char *);
964 newval = va_arg(ap, const char *);
965 if (!newparam || !newval) {
966 ast_log(LOG_WARNING, "realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
969 initfield = ast_strdupa(newparam);
970 if ((op = strchr(initfield, ' '))) {
975 cfg = ast_config_new();
977 ast_log(LOG_ERROR, "Unable to create a config!\n");
979 struct ast_variable **p = vars;
982 struct ast_category *cat = NULL;
983 cat = ast_category_new("", table_name, -1);
985 ast_log(LOG_ERROR, "Unable to create a new category!\n");
988 struct ast_variable *var = *p;
990 struct ast_variable *next = var->next;
991 if (initfield && !strcmp(initfield, var->name)) {
992 ast_category_rename(cat, var->value);
995 ast_variable_append(cat, var);
999 ast_category_append(cfg, cat);
1010 * \brief Sorting alogrithm for qsort to find the order of the variables \a a and \a b
1011 * \param a pointer to category_and_metric struct
1012 * \param b pointer to category_and_metric struct
1014 * \retval -1 for if b is greater
1015 * \retval 0 zero for equal
1016 * \retval 1 if a is greater
1018 static int compare_categories(const void *a, const void *b)
1020 const struct category_and_metric *as = a;
1021 const struct category_and_metric *bs = b;
1023 if (as->metric < bs->metric) {
1025 } else if (as->metric > bs->metric) {
1027 } else if (as->metric == bs->metric && strcmp(as->name, bs->name) != 0) {
1028 return strcmp(as->name, bs->name);
1030 /* if the metric and the category name is the same, we check the variable metric */
1031 if (as->var_metric < bs->var_metric) {
1033 } else if (as->var_metric > bs->var_metric) {
1040 /*! \brief See Asterisk doc
1042 * This is for Static Realtime (again: I think...)
1044 * load the configuration stuff for the .conf files
1045 * called on a reload
1047 static struct ast_config *config_ldap(const char *basedn, const char *table_name,
1048 const char *file, struct ast_config *cfg, struct ast_flags config_flags, const char *sugg_incl, const char *who_asked)
1050 unsigned int vars_count = 0;
1051 struct ast_variable **vars;
1053 struct ast_variable *new_v = NULL;
1054 struct ast_category *cur_cat = NULL;
1055 const char *last_category = NULL;
1056 int last_category_metric = 0;
1057 struct category_and_metric *categories;
1058 struct ast_variable **p;
1060 if (ast_strlen_zero(file) || !strcasecmp(file, RES_CONFIG_LDAP_CONF)) {
1061 ast_log(LOG_ERROR, "Missing configuration file: %s. Can't configure myself.\n", RES_CONFIG_LDAP_CONF);
1065 vars = realtime_ldap_base(&vars_count, basedn, table_name, "filename", file, "commented", "FALSE", NULL);
1068 ast_log(LOG_WARNING, "Could not find config '%s' in directory.\n", file);
1072 /*!\note Since the items come back in random order, they need to be sorted
1073 * first, and since the data could easily exceed stack size, this is
1074 * allocated from the heap.
1076 if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
1080 for (vars_count = 0, p = vars; *p; p++) {
1081 struct ast_variable *category = variable_named(*p, "category");
1082 struct ast_variable *cat_metric = variable_named(*p, "cat_metric");
1083 struct ast_variable *var_name = variable_named(*p, "variable_name");
1084 struct ast_variable *var_val = variable_named(*p, "variable_value");
1085 struct ast_variable *var_metric = variable_named(*p, "var_metric");
1086 struct ast_variable *dn = variable_named(*p, "dn");
1088 ast_debug(3, "category: %s\n", category->value);
1089 ast_debug(3, "var_name: %s\n", var_name->value);
1090 ast_debug(3, "var_val: %s\n", var_val->value);
1091 ast_debug(3, "cat_metric: %s\n", cat_metric->value);
1094 ast_log(LOG_ERROR, "No category name in entry '%s' for file '%s'.\n",
1095 (dn ? dn->value : "?"), file);
1096 } else if (!cat_metric) {
1097 ast_log(LOG_ERROR, "No category metric in entry '%s'(category: %s) for file '%s'.\n",
1098 (dn ? dn->value : "?"), category->value, file);
1099 } else if (!var_metric) {
1100 ast_log(LOG_ERROR, "No variable metric in entry '%s'(category: %s) for file '%s'.\n",
1101 (dn ? dn->value : "?"), category->value, file);
1102 } else if (!var_name) {
1103 ast_log(LOG_ERROR, "No variable name in entry '%s' (category: %s metric: %s) for file '%s'.\n",
1104 (dn ? dn->value : "?"), category->value,
1105 cat_metric->value, file);
1106 } else if (!var_val) {
1107 ast_log(LOG_ERROR, "No variable value in entry '%s' (category: %s metric: %s variable: %s) for file '%s'.\n",
1108 (dn ? dn->value : "?"), category->value,
1109 cat_metric->value, var_name->value, file);
1111 categories[vars_count].name = category->value;
1112 categories[vars_count].metric = atoi(cat_metric->value);
1113 categories[vars_count].variable_name = var_name->value;
1114 categories[vars_count].variable_value = var_val->value;
1115 categories[vars_count].var_metric = atoi(var_metric->value);
1120 qsort(categories, vars_count, sizeof(*categories), compare_categories);
1122 for (i = 0; i < vars_count; i++) {
1123 if (!strcmp(categories[i].variable_name, "#include")) {
1124 struct ast_flags flags = { 0 };
1125 if (!ast_config_internal_load(categories[i].variable_value, cfg, flags, "", who_asked)) {
1131 if (!last_category || strcmp(last_category, categories[i].name) ||
1132 last_category_metric != categories[i].metric) {
1134 cur_cat = ast_category_new(categories[i].name, table_name, -1);
1138 last_category = categories[i].name;
1139 last_category_metric = categories[i].metric;
1140 ast_category_append(cfg, cur_cat);
1143 if (!(new_v = ast_variable_new(categories[i].variable_name, categories[i].variable_value, table_name))) {
1147 ast_variable_append(cur_cat, new_v);
1151 ast_free(categories);
1156 /* \brief Function to update a set of values in ldap static mode
1158 static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
1159 const char *lookup, va_list ap)
1162 LDAPMessage *ldap_entry = NULL;
1163 LDAPMod **ldap_mods;
1164 const char *newparam = NULL;
1165 const char *newval = NULL;
1167 int num_entries = 0;
1171 struct ldap_table_config *table_config = NULL;
1172 char *clean_basedn = NULL;
1173 struct ast_str *filter = NULL;
1176 LDAPMessage *ldap_result_msg = NULL;
1179 ast_log(LOG_ERROR, "No table_name specified.\n");
1183 if (!(filter = ast_str_create(80))) {
1187 if (!attribute || !lookup) {
1188 ast_log(LOG_WARNING, "LINE(%d): search parameters are empty.\n", __LINE__);
1191 ast_mutex_lock(&ldap_lock);
1193 /* We now have our complete statement; Lets connect to the server and execute it. */
1194 if (!ldap_reconnect()) {
1195 ast_mutex_unlock(&ldap_lock);
1199 table_config = table_config_for_table_name(table_name);
1200 if (!table_config) {
1201 ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
1202 ast_mutex_unlock(&ldap_lock);
1206 clean_basedn = cleaned_basedn(NULL, basedn);
1208 /* Create the filter with the table additional filter and the parameter/value pairs we were given */
1209 ast_str_append(&filter, 0, "(&");
1210 if (table_config && table_config->additional_filter) {
1211 ast_str_append(&filter, 0, "%s", table_config->additional_filter);
1213 if (table_config != base_table_config && base_table_config && base_table_config->additional_filter) {
1214 ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
1216 append_var_and_value_to_filter(&filter, table_config, attribute, lookup);
1217 ast_str_append(&filter, 0, ")");
1219 /* Create the modification array with the parameter/value pairs we were given,
1220 * if there are several parameters with the same name, we collect them into
1221 * one parameter/value pair and delimit them with a semicolon */
1222 newparam = va_arg(ap, const char *);
1223 newparam = convert_attribute_name_to_ldap(table_config, newparam);
1224 newval = va_arg(ap, const char *);
1225 if (!newparam || !newval) {
1226 ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
1230 mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
1231 ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
1232 ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
1234 ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
1235 ldap_mods[0]->mod_type = ast_strdup(newparam);
1237 ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
1238 ldap_mods[0]->mod_values[0] = ast_strdup(newval);
1240 while ((newparam = va_arg(ap, const char *))) {
1241 newparam = convert_attribute_name_to_ldap(table_config, newparam);
1242 newval = va_arg(ap, const char *);
1245 for (i = 0; i < mods_size - 1; i++) {
1246 if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
1247 /* We have the parameter allready, adding the value as a semicolon delimited value */
1248 ldap_mods[i]->mod_values[0] = ast_realloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(newval) + 2));
1249 strcat(ldap_mods[i]->mod_values[0], ";");
1250 strcat(ldap_mods[i]->mod_values[0], newval);
1256 /* create new mod */
1259 ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
1260 ldap_mods[mods_size - 1] = NULL;
1262 ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
1264 ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
1265 strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
1267 if (strlen(newval) == 0) {
1268 ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
1270 ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
1272 ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
1273 ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(newval) + 1);
1274 strcpy(ldap_mods[mods_size - 2]->mod_values[0], newval);
1278 /* freeing ldap_mods further down */
1281 /* freeing ldap_result further down */
1282 result = ldap_search_ext_s(ldapConn, clean_basedn,
1283 LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
1285 if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
1286 ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
1289 usleep(500000L * tries);
1291 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1294 if (!ldap_reconnect())
1298 } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
1300 if (result != LDAP_SUCCESS) {
1301 ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
1302 ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
1304 ast_mutex_unlock(&ldap_lock);
1307 ldap_msgfree(ldap_result_msg);
1308 ldap_mods_free(ldap_mods, 0);
1311 /* Ready to update */
1312 if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
1313 ast_debug(3, "LINE(%d) Modifying %s=%s hits: %d\n", __LINE__, attribute, lookup, num_entries);
1314 for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
1315 if (ldap_mods[i]->mod_op != LDAP_MOD_DELETE) {
1316 ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
1318 ast_debug(3, "LINE(%d) deleting %s \n", __LINE__, ldap_mods[i]->mod_type);
1321 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
1323 for (i = 0; ldap_entry; i++) {
1324 dn = ldap_get_dn(ldapConn, ldap_entry);
1325 if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
1326 ast_log(LOG_ERROR, "Couldn't modify '%s'='%s', dn:%s because %s\n",
1327 attribute, lookup, dn, ldap_err2string(error));
1330 ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
1334 ast_mutex_unlock(&ldap_lock);
1336 ast_free(clean_basedn);
1337 ldap_msgfree(ldap_result_msg);
1338 ldap_mods_free(ldap_mods, 0);
1342 static int update2_ldap(const char *basedn, const char *table_name, va_list ap)
1345 LDAPMessage *ldap_entry = NULL;
1346 LDAPMod **ldap_mods;
1347 const char *newparam = NULL;
1348 const char *newval = NULL;
1350 int num_entries = 0;
1354 struct ldap_table_config *table_config = NULL;
1355 char *clean_basedn = NULL;
1356 struct ast_str *filter = NULL;
1359 LDAPMessage *ldap_result_msg = NULL;
1362 ast_log(LOG_ERROR, "No table_name specified.\n");
1366 if (!(filter = ast_str_create(80))) {
1370 ast_mutex_lock(&ldap_lock);
1372 /* We now have our complete statement; Lets connect to the server and execute it. */
1373 if (!ldap_reconnect()) {
1374 ast_mutex_unlock(&ldap_lock);
1379 table_config = table_config_for_table_name(table_name);
1380 if (!table_config) {
1381 ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
1382 ast_mutex_unlock(&ldap_lock);
1387 clean_basedn = cleaned_basedn(NULL, basedn);
1389 /* Create the filter with the table additional filter and the parameter/value pairs we were given */
1390 ast_str_append(&filter, 0, "(&");
1391 if (table_config && table_config->additional_filter) {
1392 ast_str_append(&filter, 0, "%s", table_config->additional_filter);
1394 if (table_config != base_table_config && base_table_config
1395 && base_table_config->additional_filter) {
1396 ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
1399 /* Get multiple lookup keyfields and values */
1400 while ((newparam = va_arg(ap, const char *))) {
1401 newval = va_arg(ap, const char *);
1402 append_var_and_value_to_filter(&filter, table_config, newparam, newval);
1404 ast_str_append(&filter, 0, ")");
1406 /* Create the modification array with the parameter/value pairs we were given,
1407 * if there are several parameters with the same name, we collect them into
1408 * one parameter/value pair and delimit them with a semicolon */
1409 newparam = va_arg(ap, const char *);
1410 newparam = convert_attribute_name_to_ldap(table_config, newparam);
1411 newval = va_arg(ap, const char *);
1412 if (!newparam || !newval) {
1413 ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
1415 ast_free(clean_basedn);
1419 mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
1420 ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
1421 ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
1423 ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
1424 ldap_mods[0]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
1425 strcpy(ldap_mods[0]->mod_type, newparam);
1427 ldap_mods[0]->mod_values = ast_calloc(sizeof(char), 2);
1428 ldap_mods[0]->mod_values[0] = ast_calloc(sizeof(char), strlen(newval) + 1);
1429 strcpy(ldap_mods[0]->mod_values[0], newval);
1431 while ((newparam = va_arg(ap, const char *))) {
1432 newparam = convert_attribute_name_to_ldap(table_config, newparam);
1433 newval = va_arg(ap, const char *);
1436 for (i = 0; i < mods_size - 1; i++) {
1437 if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
1438 /* We have the parameter allready, adding the value as a semicolon delimited value */
1439 ldap_mods[i]->mod_values[0] = ast_realloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(newval) + 2));
1440 strcat(ldap_mods[i]->mod_values[0], ";");
1441 strcat(ldap_mods[i]->mod_values[0], newval);
1447 /* create new mod */
1450 ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
1451 ldap_mods[mods_size - 1] = NULL;
1452 ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
1454 ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
1456 ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
1457 strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
1459 ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
1460 ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(newval) + 1);
1461 strcpy(ldap_mods[mods_size - 2]->mod_values[0], newval);
1464 /* freeing ldap_mods further down */
1467 /* freeing ldap_result further down */
1468 result = ldap_search_ext_s(ldapConn, clean_basedn,
1469 LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
1471 if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
1472 ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
1475 usleep(500000L * tries);
1477 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1480 if (!ldap_reconnect()) {
1485 } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
1487 if (result != LDAP_SUCCESS) {
1488 ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
1489 ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
1491 ast_mutex_unlock(&ldap_lock);
1493 ast_free(clean_basedn);
1494 ldap_msgfree(ldap_result_msg);
1495 ldap_mods_free(ldap_mods, 0);
1498 /* Ready to update */
1499 if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
1500 for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
1501 ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
1504 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
1506 for (i = 0; ldap_entry; i++) {
1507 dn = ldap_get_dn(ldapConn, ldap_entry);
1508 if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
1509 ast_log(LOG_ERROR, "Couldn't modify dn:%s because %s", dn, ldap_err2string(error));
1512 ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
1516 ast_mutex_unlock(&ldap_lock);
1521 ast_free(clean_basedn);
1523 ldap_msgfree(ldap_result_msg);
1524 ldap_mods_free(ldap_mods, 0);
1528 static struct ast_config_engine ldap_engine = {
1530 .load_func = config_ldap,
1531 .realtime_func = realtime_ldap,
1532 .realtime_multi_func = realtime_multi_ldap,
1533 .update_func = update_ldap,
1534 .update2_func = update2_ldap,
1537 static int load_module(void)
1539 if (parse_config() < 0) {
1540 ast_log(LOG_ERROR, "Cannot load LDAP RealTime driver.\n");
1544 ast_mutex_lock(&ldap_lock);
1546 if (!ldap_reconnect()) {
1547 ast_log(LOG_WARNING, "Couldn't establish connection to LDAP directory. Check debug.\n");
1550 ast_config_engine_register(&ldap_engine);
1551 ast_verb(1, "LDAP RealTime driver loaded.\n");
1552 ast_cli_register_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
1554 ast_mutex_unlock(&ldap_lock);
1559 static int unload_module(void)
1561 /* Aquire control before doing anything to the module itself. */
1562 ast_mutex_lock(&ldap_lock);
1564 table_configs_free();
1567 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1570 ast_cli_unregister_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
1571 ast_config_engine_deregister(&ldap_engine);
1572 ast_verb(1, "LDAP RealTime driver unloaded.\n");
1574 /* Unlock so something else can destroy the lock. */
1575 ast_mutex_unlock(&ldap_lock);
1580 static int reload(void)
1582 /* Aquire control before doing anything to the module itself. */
1583 ast_mutex_lock(&ldap_lock);
1586 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1590 if (parse_config() < 0) {
1591 ast_log(LOG_NOTICE, "Cannot reload LDAP RealTime driver.\n");
1592 ast_mutex_unlock(&ldap_lock);
1596 if (!ldap_reconnect()) {
1597 ast_log(LOG_WARNING, "Couldn't establish connection to your directory server. Check debug.\n");
1600 ast_verb(2, "LDAP RealTime driver reloaded.\n");
1602 /* Done reloading. Release lock so others can now use driver. */
1603 ast_mutex_unlock(&ldap_lock);
1608 /*! \brief parse the configuration file */
1609 static int parse_config(void)
1611 struct ast_config *config;
1612 struct ast_flags config_flags = {0};
1613 const char *s, *host;
1615 char *category_name = NULL;
1617 /* Make sure that global variables are reset */
1621 base_distinguished_name[0] = '\0';
1624 config = ast_config_load(RES_CONFIG_LDAP_CONF, config_flags);
1625 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1626 ast_log(LOG_ERROR, "Cannot load configuration file: %s\n", RES_CONFIG_LDAP_CONF);
1630 if (!(s = ast_variable_retrieve(config, "_general", "user"))) {
1631 ast_log(LOG_NOTICE, "No directory user found, anonymous binding as default.\n");
1634 ast_copy_string(user, s, sizeof(user));
1637 if (!ast_strlen_zero(user)) {
1638 if (!(s = ast_variable_retrieve(config, "_general", "pass"))) {
1639 ast_log(LOG_WARNING, "No directory password found, using 'asterisk' as default.\n");
1640 ast_copy_string(pass, "asterisk", sizeof(pass));
1642 ast_copy_string(pass, s, sizeof(pass));
1646 /* URL is preferred, use host and port if not found */
1647 if ((s = ast_variable_retrieve(config, "_general", "url"))) {
1648 ast_copy_string(url, s, sizeof(url));
1649 } else if ((host = ast_variable_retrieve(config, "_general", "host"))) {
1650 if (!(s = ast_variable_retrieve(config, "_general", "port")) || sscanf(s, "%5d", &port) != 1 || port > 65535) {
1651 ast_log(LOG_NOTICE, "No directory port found, using 389 as default.\n");
1655 snprintf(url, sizeof(url), "ldap://%s:%d", host, port);
1657 ast_log(LOG_ERROR, "No directory URL or host found.\n");
1658 ast_config_destroy(config);
1662 if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) {
1663 ast_log(LOG_ERROR, "No LDAP base dn found, using '%s' as default.\n", RES_CONFIG_LDAP_DEFAULT_BASEDN);
1664 ast_copy_string(base_distinguished_name, RES_CONFIG_LDAP_DEFAULT_BASEDN, sizeof(base_distinguished_name));
1666 ast_copy_string(base_distinguished_name, s, sizeof(base_distinguished_name));
1668 if (!(s = ast_variable_retrieve(config, "_general", "version")) && !(s = ast_variable_retrieve(config, "_general", "protocol"))) {
1669 ast_log(LOG_NOTICE, "No explicit LDAP version found, using 3 as default.\n");
1670 } else if (sscanf(s, "%30d", &version) != 1 || version < 1 || version > 6) {
1671 ast_log(LOG_WARNING, "Invalid LDAP version '%s', using 3 as default.\n", s);
1675 table_configs_free();
1677 while ((category_name = ast_category_browse(config, category_name))) {
1678 int is_general = (strcasecmp(category_name, "_general") == 0);
1679 int is_config = (strcasecmp(category_name, "config") == 0); /*!< using the [config] context for Static RealTime */
1680 struct ast_variable *var = ast_variable_browse(config, category_name);
1683 struct ldap_table_config *table_config =
1684 table_config_for_table_name(category_name);
1685 if (!table_config) {
1686 table_config = table_config_new(category_name);
1687 AST_LIST_INSERT_HEAD(&table_configs, table_config, entry);
1689 base_table_config = table_config;
1691 static_table_config = table_config;
1693 for (; var; var = var->next) {
1694 if (!strcasecmp(var->name, "additionalFilter")) {
1695 table_config->additional_filter = ast_strdup(var->value);
1697 ldap_table_config_add_attribute(table_config, var->name, var->value);
1703 ast_config_destroy(config);
1708 /*! \note ldap_lock should have been locked before calling this function. */
1709 static int ldap_reconnect(void)
1711 int bind_result = 0;
1715 ast_debug(2, "Everything seems fine.\n");
1719 if (ast_strlen_zero(url)) {
1720 ast_log(LOG_ERROR, "Not enough parameters to connect to ldap directory\n");
1724 if (LDAP_SUCCESS != ldap_initialize(&ldapConn, url)) {
1725 ast_log(LOG_ERROR, "Failed to init ldap connection to '%s'. Check debug for more info.\n", url);
1729 if (LDAP_OPT_SUCCESS != ldap_set_option(ldapConn, LDAP_OPT_PROTOCOL_VERSION, &version)) {
1730 ast_log(LOG_WARNING, "Unable to set LDAP protocol version to %d, falling back to default.\n", version);
1733 if (!ast_strlen_zero(user)) {
1734 ast_debug(2, "bind to '%s' as user '%s'\n", url, user);
1735 cred.bv_val = (char *) pass;
1736 cred.bv_len = strlen(pass);
1737 bind_result = ldap_sasl_bind_s(ldapConn, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
1739 ast_debug(2, "bind %s anonymously\n", url);
1742 bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
1744 if (bind_result == LDAP_SUCCESS) {
1745 ast_debug(2, "Successfully connected to directory.\n");
1746 connect_time = time(NULL);
1749 ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result));
1750 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1756 static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1758 char status[256], credentials[100] = "";
1759 int ctimesec = time(NULL) - connect_time;
1763 e->command = "realtime show ldap status";
1765 "Usage: realtime show ldap status\n"
1766 " Shows connection information for the LDAP RealTime driver\n";
1775 if (!ast_strlen_zero(url))
1776 snprintf(status, sizeof(status), "Connected to '%s', baseDN %s", url, base_distinguished_name);
1778 if (!ast_strlen_zero(user))
1779 snprintf(credentials, sizeof(credentials), " with username %s", user);
1781 if (ctimesec > 31536000) {
1782 ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
1783 status, credentials, ctimesec / 31536000,
1784 (ctimesec % 31536000) / 86400, (ctimesec % 86400) / 3600,
1785 (ctimesec % 3600) / 60, ctimesec % 60);
1786 } else if (ctimesec > 86400) {
1787 ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n",
1788 status, credentials, ctimesec / 86400, (ctimesec % 86400) / 3600,
1789 (ctimesec % 3600) / 60, ctimesec % 60);
1790 } else if (ctimesec > 3600) {
1791 ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n",
1792 status, credentials, ctimesec / 3600, (ctimesec % 3600) / 60,
1794 } else if (ctimesec > 60) {
1795 ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials,
1796 ctimesec / 60, ctimesec % 60);
1798 ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
1804 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "LDAP realtime interface",
1805 .load = load_module,
1806 .unload = unload_module,
1808 .load_pri = AST_MODPRI_REALTIME_DRIVER,