19075833bfcacd64097936c15ac507b09922a1b5
[asterisk/asterisk.git] / res / res_config_ldap.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Copyright (C) 2005, Oxymium sarl
5  * Manuel Guesdon <mguesdon@oxymium.net> - LDAP RealTime Driver Author/Adaptor
6  *
7  * Copyright (C) 2007, Digium, Inc.
8  * Russell Bryant <russell@digium.com>
9  *
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.
15  *
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.
19  *
20  */
21
22 /*! \file
23  *
24  * \brief ldap plugin for portable configuration engine (ARA)
25  *
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>
30  *
31  * \extref OpenLDAP http://www.openldap.org
32  */
33
34 /*** MODULEINFO
35         <depend>ldap</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <ldap.h>
45
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47
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"
59
60 #define RES_CONFIG_LDAP_CONF "res_ldap.conf"
61 #define RES_CONFIG_LDAP_DEFAULT_BASEDN "asterisk"
62
63 AST_MUTEX_DEFINE_STATIC(ldap_lock);
64
65 static LDAP *ldapConn;
66 static char url[512];
67 static char user[512];
68 static char pass[512];
69 static char base_distinguished_name[512];
70 static int version;
71 static time_t connect_time;
72
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);
76
77 struct category_and_metric {
78         const char *name;
79         int 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 */
83 };
84
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 */
93 };
94
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;
99
100 static struct ast_cli_entry ldap_cli[] = {
101         AST_CLI_DEFINE(realtime_ldap_status, "Shows connection information for the LDAP RealTime driver"),
102 };
103
104 /*! \brief Create a new table_config */
105 static struct ldap_table_config *table_config_new(const char *table_name)
106 {
107         struct ldap_table_config *p;
108
109         if (!(p = ast_calloc(1, sizeof(*p))))
110                 return NULL;
111
112         if (table_name) {
113                 if (!(p->table_name = ast_strdup(table_name))) {
114                         free(p);
115                         return NULL;
116                 }
117         }
118
119         return p;
120 }
121
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)
125 {
126         struct ldap_table_config *c = NULL;
127
128         AST_LIST_TRAVERSE(&table_configs, c, entry) {
129                 if (!strcmp(c->table_name, table_name))
130                         break;
131         }
132
133         return c;
134 }
135
136 /*! \brief Find variable by name */
137 static struct ast_variable *variable_named(struct ast_variable *var, const char *name)
138 {
139         for (; var; var = var->next) {
140                 if (!strcasecmp(name, var->name))
141                         break;
142         }
143
144         return var;
145 }
146
147 /*! \brief for the semicolon delimiter
148         \param somestr - pointer to a string
149
150         \return number of occurances of the delimiter(semicolon)
151  */
152 static int semicolon_count_str(const char *somestr)
153 {
154         int count = 0;
155
156         for (; *somestr; somestr++) {
157                 if (*somestr == ';')
158                         count++;
159         }
160
161         return count;
162
163
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
166  */
167 static int semicolon_count_var(struct ast_variable *var)
168 {
169         struct ast_variable *var_value = variable_named(var, "variable_value");
170
171         if (!var_value) {
172                 return 0;
173         }
174
175         ast_debug(2, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
176
177         return semicolon_count_str(var_value->value);
178 }
179
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)
183 {
184         struct ast_variable *var;
185
186         if (ast_strlen_zero(attribute_name) || ast_strlen_zero(attribute_value)) {
187                 return;
188         }
189
190         if (!(var = ast_variable_new(attribute_name, attribute_value, table_config->table_name))) {
191                 return;
192         }
193
194         if (table_config->attributes) {
195                 var->next = table_config->attributes;
196         }
197         table_config->attributes = var;
198 }
199
200 /*! \brief Free table_config 
201  *  \note assumes ldap_lock to be locked */
202 static void table_configs_free(void)
203 {
204         struct ldap_table_config *c;
205
206         while ((c = AST_LIST_REMOVE_HEAD(&table_configs, entry))) {
207                 if (c->table_name) {
208                         ast_free(c->table_name);
209                 }
210                 if (c->additional_filter) {
211                         ast_free(c->additional_filter);
212                 }
213                 if (c->attributes) {
214                         ast_variables_destroy(c->attributes);
215                 }
216                 free(c);
217         }
218
219         base_table_config = NULL;
220         static_table_config = NULL;
221 }
222
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)
226 {
227         int i = 0;
228         struct ldap_table_config *configs[] = { table_config, base_table_config };
229
230         for (i = 0; i < ARRAY_LEN(configs); i++) {
231                 struct ast_variable *attribute;
232
233                 if (!configs[i]) {
234                         continue;
235                 }
236
237                 attribute = configs[i]->attributes;
238                 for (; attribute; attribute = attribute->next) {
239                         if (!strcasecmp(attribute_name, attribute->name)) {
240                                 return attribute->value;
241                         }
242                 }
243         }
244
245         return attribute_name;
246 }
247
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)
252 {
253         int i = 0;
254         struct ldap_table_config *configs[] = { table_config, base_table_config };
255
256         for (i = 0; i < ARRAY_LEN(configs); i++) {
257                 struct ast_variable *attribute;
258
259                 if (!configs[i]) {
260                         continue;
261                 }
262
263                 attribute = configs[i]->attributes;
264                 for (; attribute; attribute = attribute->next) {
265                         if (strcasecmp(attribute_name, attribute->value) == 0) {
266                                 return attribute->name;
267                         }
268                 }
269         }
270
271         return attribute_name;
272 }
273
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.
277  */
278 static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config *table_config,
279         LDAPMessage *ldap_entry)
280 {
281         BerElement *ber = NULL;
282         struct ast_variable *var = NULL;
283         struct ast_variable *prev = NULL;
284         int is_delimited = 0;
285         int i = 0;
286         char *ldap_attribute_name;
287         struct berval *value;
288         int pos = 0;
289
290         ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
291
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;
296
297                 values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name); /* these are freed at the end */
298                 if (values) {
299                         struct berval **v;
300                         char *valptr;
301
302                         for (v = values; *v; v++) {
303                                 value = *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)) {
308                                                 valptr += 5;
309                                         }
310                                         ast_debug(2, "md5: %s\n", valptr);
311                                 }
312                                 if (valptr) {
313                                         /* ok, so looping through all delimited values except the last one (not, last character is not delimited...) */
314                                         if (is_delimited) {
315                                                 i = 0;
316                                                 pos = 0;
317                                                 while (!ast_strlen_zero(valptr + i)) {
318                                                         if (valptr[i] == ';') {
319                                                                 valptr[i] = '\0';
320                                                                 if (prev) {
321                                                                         prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
322                                                                         if (prev->next) {
323                                                                                 prev = prev->next;
324                                                                         }
325                                                                 } else {
326                                                                         prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
327                                                                 }
328                                                                 pos = i + 1;
329                                                         }
330                                                         i++;
331                                                 }
332                                         }
333                                         /* for the last delimited value or if the value is not delimited: */
334                                         if (prev) {
335                                                 prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
336                                                 if (prev->next) {
337                                                         prev = prev->next;
338                                                 }
339                                         } else {
340                                                 prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
341                                         }
342                                 }
343                         }
344                         ldap_value_free_len(values);
345                 }
346                 ldap_memfree(ldap_attribute_name);
347                 ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
348         }
349         ber_free(ber, 0);
350
351         return var;
352 }
353
354 /*! \brief Get variables from ldap entry attributes - Should be locked before using it
355  *
356  * The results are freed outside this function so is the \a vars array.
357  *      
358  * \return \a vars - an array of ast_variable variables terminated with a null.
359  **/
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)
362 {
363         struct ast_variable **vars;
364         int i = 0;
365         int tot_count = 0;
366         int entry_index = 0;
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;
374         int delim_count = 0;
375
376         /* First find the total count */
377         ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
378
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);
384         }
385
386         if (entries_count_ptr) {
387                 *entries_count_ptr = tot_count;
388         }
389
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);
396
397         ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
398
399         i = 0;
400
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; ) { 
403                 int pos = 0;
404                 delim_value = NULL;
405                 delim_tot_count = 0;
406                 delim_count = 0;
407                 
408                 do { /* while delim_count */
409
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) {
414                         
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;
418
419                                 values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);
420                                 if (values) {
421                                         struct berval **v;
422                                         char *valptr;
423
424                                         for (v = values; *v; v++) {
425                                                 value = *v;
426                                                 valptr = value->bv_val;
427                                                 if (is_realmed_password_attribute) {
428                                                         if (strncasecmp(valptr, "{md5}", 5) == 0) {
429                                                                 valptr += 5;
430                                                         }
431                                                         ast_debug(2, "md5: %s\n", valptr);
432                                                 }
433                                                 if (valptr) {
434                                                         if (delim_value == NULL && !is_realmed_password_attribute 
435                                                                 && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0)) {
436
437                                                                 delim_value = ast_strdup(valptr);
438
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);
441                                                                         is_delimited = 1;
442                                                                 }
443                                                         }
444
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 */
448
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';
453
454                                                                                 ast_debug(2, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
455                                                         
456                                                                                 if (prev) {
457                                                                                         prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
458                                                                                         if (prev->next) {
459                                                                                                 prev = prev->next;
460                                                                                         }
461                                                                                 } else {
462                                                                                         prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
463                                                                                 }
464                                                                                 pos = i + 1;
465
466                                                                                 if (static_table_config == table_config) {
467                                                                                         break;
468                                                                                 }
469                                                                         }
470                                                                 }
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);
475                                                                         if (prev) {
476                                                                                 prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
477                                                                                 if (prev->next) {
478                                                                                         prev = prev->next;
479                                                                                 }
480                                                                         } else {
481                                                                                 prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
482                                                                         }
483                                                                         /* Remembering to free memory */
484                                                                         is_delimited = 0;
485                                                                         pos = 0;
486                                                                 }
487                                                                 free(delim_value);
488                                                                 delim_value = NULL;
489                                                                 
490                                                                 ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
491                                                         } else {
492                                                                 /* not delimited */
493                                                                 if (delim_value) {
494                                                                         free(delim_value);
495                                                                         delim_value = NULL;
496                                                                 }
497                                                                 ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
498
499                                                                 if (prev) {
500                                                                         prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
501                                                                         if (prev->next) {
502                                                                                 prev = prev->next;
503                                                                         }
504                                                                 } else {
505                                                                         prev = var = ast_variable_new(attribute_name, valptr, table_config->table_name);
506                                                                 }
507                                                         }
508                                                 }
509                                         } /*!< for (v = values; *v; v++) */
510                                         ldap_value_free_len(values);
511                                 }/*!< if (values) */
512                                 ldap_memfree(ldap_attribute_name);
513                                 ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
514                         } /*!< while (ldap_attribute_name) */
515                         ber_free(ber, 0);
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);
522                                         }
523                                 }
524                                 vars[entry_index++] = var;
525                                 prev = NULL;
526                         }
527
528                         delim_count++;
529                 } while (delim_count <= delim_tot_count && static_table_config == table_config);
530
531                 if (static_table_config != table_config) {
532                         ast_debug(3, "LINE(%d) Added to vars - non static\n", __LINE__);
533                                 
534                         vars[entry_index++] = var;
535                         prev = NULL;
536                 }
537                 ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
538         } /*!< end for loop over ldap_entry */
539
540         return vars;
541 }
542
543
544 /*! \brief Check if we have a connection error */
545 static int is_ldap_connect_error(int err)
546 {
547         return (err == LDAP_SERVER_DOWN || err == LDAP_TIMEOUT || err == LDAP_CONNECT_ERROR);
548 }
549
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)
552 */
553 static struct ast_variable *ldap_loadentry(struct ldap_table_config *table_config,
554                                            const char *dn)
555 {
556         if (!table_config) {
557                 ast_log(LOG_ERROR, "No table config\n");
558                 return NULL;
559         } else {
560                 struct ast_variable **vars = NULL;
561                 struct ast_variable *var = NULL;
562                 int result = -1;
563                 LDAPMessage *ldap_result_msg = NULL;
564                 int tries = 0;
565
566                 ast_debug(2, "ldap_loadentry dn=%s\n", dn);
567
568                 do {
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);
573                                 tries++;
574                                 if (tries < 3) {
575                                         usleep(500000L * tries);
576                                         if (ldapConn) {
577                                                 ldap_unbind_ext_s(ldapConn, NULL, NULL);
578                                                 ldapConn = NULL;
579                                         }
580                                         if (!ldap_reconnect()) {
581                                                 break;
582                                         }
583                                 }
584                         }
585                 } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
586
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);
591                         return NULL;
592                 } else {
593                         int num_entry = 0;
594                         unsigned int *entries_count_ptr = NULL; /*!< not using this */
595
596                         if ((num_entry = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
597                                 ast_debug(3, "num_entry: %d\n", num_entry);
598
599                                 vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
600                                 if (num_entry > 1) {
601                                         ast_log(LOG_NOTICE, "More than one entry for dn=%s. Take only 1st one\n", dn);
602                                 }
603                         } else {
604                                 ast_debug(2, "Could not find any entry dn=%s.\n", dn);
605                         }
606                 }
607                 ldap_msgfree(ldap_result_msg);
608
609                 /* Chopping \a vars down to one variable */
610                 if (vars != NULL) {
611                         struct ast_variable **p = vars;
612                         p++;
613                         var = *p;
614                         while (var) {
615                                 ast_variables_destroy(var);
616                                 p++;
617                         }
618                         vars = ast_realloc(vars, sizeof(struct ast_variable *));
619                 }
620
621                 var = *vars;
622
623                 return var;
624         }
625 }
626
627 /*! \note caller should free returned pointer */
628 static char *substituted(struct ast_channel *channel, const char *string)
629 {
630 #define MAXRESULT       2048
631         char *ret_string = NULL;
632
633         if (!ast_strlen_zero(string)) {
634                 ret_string = ast_calloc(1, MAXRESULT);
635                 pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1);
636         }
637         ast_debug(2, "substituted: string: '%s' => '%s' \n", string, ret_string);
638         return ret_string;
639 }
640
641 /*! \note caller should free returned pointer */
642 static char *cleaned_basedn(struct ast_channel *channel, const char *basedn)
643 {
644         char *cbasedn = NULL;
645         if (basedn) {
646                 char *p = NULL;
647                 cbasedn = substituted(channel, basedn);
648                 if (*cbasedn == '"') {
649                         cbasedn++;
650                         if (!ast_strlen_zero(cbasedn)) {
651                                 int len = strlen(cbasedn);
652                                 if (cbasedn[len - 1] == '"')
653                                         cbasedn[len - 1] = '\0';
654
655                         }
656                 }
657                 p = cbasedn;
658                 while (*p) {
659                         if (*p == '|')
660                                 *p = ',';
661                         p++;
662                 }
663         }
664         ast_debug(2, "basedn: '%s' => '%s' \n", basedn, cbasedn);
665         return cbasedn;
666 }
667
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)
671 {
672         int search_len = strlen(search);
673         int by_len = strlen(by);
674         int replaced = 0;
675         char *p = strstr(string, search);
676
677         if (p) {
678                 replaced = 1;
679                 while (p) {
680                         if (by_len == search_len) {
681                                 memcpy(p, by, by_len);
682                         } else {
683                                 memmove(p + by_len, p + search_len, strlen(p + search_len) + 1);
684                                 memcpy(p, by, by_len);
685                         }
686                         p = strstr(p + by_len, search);
687                 }
688         }
689         return replaced;
690 }
691
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)
696 {
697         char *new_name = NULL;
698         char *new_value = NULL;
699         char *like_pos = strstr(name, " LIKE");
700
701         ast_debug(2, "name='%s' value='%s'\n", name, value);
702
703         if (like_pos) {
704                 int len = like_pos - name;
705
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, "%", "*");
711         }
712
713         name = convert_attribute_name_to_ldap(table_config, name);
714
715         ast_str_append(filter, 0, "(%s=%s)", name, value);
716 }
717
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
725 */
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)
728 {
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;
735         int tries = 0;
736         int result = 0;
737         LDAPMessage *ldap_result_msg = NULL;
738
739         if (!table_name) {
740                 ast_log(LOG_ERROR, "No table_name specified.\n");
741                 ast_free(clean_basedn);
742                 return NULL;
743         } 
744
745         if (!(filter = ast_str_create(80))) {
746                 ast_log(LOG_ERROR, "Can't initialize data structures.n");
747                 ast_free(clean_basedn);
748                 return NULL;
749         }
750
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 *);
754
755         if (!newparam || !newval) {
756                 ast_log(LOG_ERROR, "Realtime retrieval requires at least 1 parameter"
757                         " and 1 value to search on.\n");
758                 ast_free(filter);
759                 ast_free(clean_basedn);
760                 return NULL;
761         }
762
763         ast_mutex_lock(&ldap_lock);
764
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);
768                 ast_free(filter);
769                 ast_free(clean_basedn);
770                 return NULL;
771         }
772
773         table_config = table_config_for_table_name(table_name);
774         if (!table_config) {
775                 ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
776                 ast_mutex_unlock(&ldap_lock);
777                 ast_free(filter);
778                 ast_free(clean_basedn);
779                 return NULL;
780         }
781
782         ast_str_append(&filter, 0, "(&");
783
784         if (table_config && table_config->additional_filter) {
785                 ast_str_append(&filter, 0, "%s", table_config->additional_filter);
786         }
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);
790         }
791
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 */
794
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);
799         }
800         ast_str_append(&filter, 0, ")");
801
802         do {
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,
806                                   &ldap_result_msg);
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);
809                         if (++tries < 10) {
810                                 usleep(1);
811                                 if (ldapConn) {
812                                         ldap_unbind_ext_s(ldapConn, NULL, NULL);
813                                         ldapConn = NULL;
814                                 }
815                                 if (!ldap_reconnect()) {
816                                         break;
817                                 }
818                         }
819                 }
820         } while (result != LDAP_SUCCESS && tries < 10 && is_ldap_connect_error(result));
821
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));
825         } else {
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);
831                 } else {
832                         ast_debug(1, "Could not find any entry matching %s in base dn %s.\n", ast_str_buffer(filter), clean_basedn);
833                 }
834
835                 ldap_msgfree(ldap_result_msg);
836
837                 /* TODO: get the default variables from the accountBaseDN, not implemented with delimited values */
838                 if (vars) {
839                         struct ast_variable **p = vars;
840                         while (*p) {
841                                 struct ast_variable *append_var = NULL;
842                                 struct ast_variable *tmp = *p;
843                                 while (tmp) {
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);
847                                                 
848                                                 while (base_var) {
849                                                         struct ast_variable *next = base_var->next;
850                                                         struct ast_variable *test_var = *p;
851                                                         int base_var_found = 0;
852
853                                                         /* run throught the default values and fill it inn if it is missing */
854                                                         while (test_var) {
855                                                                 if (strcasecmp(test_var->name, base_var->name) == 0) {
856                                                                         base_var_found = 1;
857                                                                         break;
858                                                                 } else {
859                                                                         test_var = test_var->next;
860                                                                 }
861                                                         }
862                                                         if (base_var_found) {
863                                                                 base_var->next = NULL;
864                                                                 ast_variables_destroy(base_var);
865                                                                 base_var = next;
866                                                         } else {
867                                                                 if (append_var) {
868                                                                         base_var->next = append_var;
869                                                                 } else {
870                                                                         base_var->next = NULL;
871                                                                 }
872                                                                 append_var = base_var;
873                                                                 base_var = next;
874                                                         }
875                                                 }
876                                         }
877                                         if (!tmp->next && append_var) {
878                                                 tmp->next = append_var;
879                                                 tmp = NULL;
880                                         } else {
881                                                 tmp = tmp->next;
882                                         }
883                                 }
884                                 p++;
885                         }
886                 }
887         }
888
889         if (filter) {
890                 ast_free(filter);
891         }
892
893         if (clean_basedn) {
894                 ast_free(clean_basedn);
895         }
896
897         ast_mutex_unlock(&ldap_lock);
898
899         return vars;
900 }
901
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, ...)
905 {
906         struct ast_variable **vars = NULL;
907         va_list ap;
908
909         va_start(ap, table_name);
910         vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, ap);
911         va_end(ap);
912
913         return vars;
914 }
915
916 /*! \brief See Asterisk doc
917 *
918 * For Realtime Dynamic(i.e., switch, queues, and directory) -- I think
919 */
920 static struct ast_variable *realtime_ldap(const char *basedn,
921                                           const char *table_name, va_list ap)
922 {
923         struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, ap);
924         struct ast_variable *var = NULL;
925
926         if (vars) {
927                 struct ast_variable *last_var = NULL;
928                 struct ast_variable **p = vars;
929                 while (*p) {
930                         if (last_var) {
931                                 while (last_var->next) {
932                                         last_var = last_var->next;
933                                 }
934                                 last_var->next = *p;
935                         } else {
936                                 var = *p;
937                                 last_var = var;
938                         }
939                         p++;
940                 }
941                 free(vars);
942         }
943         return var;
944 }
945
946 /*! \brief See Asterisk doc
947 *
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
952 */
953 static struct ast_config *realtime_multi_ldap(const char *basedn,
954       const char *table_name, va_list ap)
955 {
956         char *op;
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;
962
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");
967             return NULL;
968         }
969         initfield = ast_strdupa(newparam);
970         if ((op = strchr(initfield, ' '))) {
971                 *op = '\0';
972         }
973
974         if (vars) {
975                 cfg = ast_config_new();
976                 if (!cfg) {
977                         ast_log(LOG_ERROR, "Unable to create a config!\n");
978                 } else {
979                         struct ast_variable **p = vars;
980
981                         while (*p) {
982                                 struct ast_category *cat = NULL;
983                                 cat = ast_category_new("", table_name, -1);
984                                 if (!cat) {
985                                         ast_log(LOG_ERROR, "Unable to create a new category!\n");
986                                         break;
987                                 } else {
988                                         struct ast_variable *var = *p;
989                                         while (var) {
990                                                 struct ast_variable *next = var->next;
991                                                 if (initfield && !strcmp(initfield, var->name)) {
992                                                         ast_category_rename(cat, var->value);
993                                                 }
994                                                 var->next = NULL;
995                                                 ast_variable_append(cat, var);
996                                                 var = next;
997                                         }
998                                 }
999                                 ast_category_append(cfg, cat);
1000                                 p++;
1001                         }
1002                 }
1003                 free(vars);
1004         }
1005         return cfg;
1006
1007 }
1008
1009 /*! 
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
1013  *
1014  * \retval -1 for if b is greater
1015  * \retval 0 zero for equal
1016  * \retval 1 if a is greater
1017  */
1018 static int compare_categories(const void *a, const void *b)
1019 {
1020         const struct category_and_metric *as = a;
1021         const struct category_and_metric *bs = b;
1022
1023         if (as->metric < bs->metric) {
1024                 return -1;
1025         } else if (as->metric > bs->metric) {
1026                 return 1;
1027         } else if (as->metric == bs->metric && strcmp(as->name, bs->name) != 0) {
1028                 return strcmp(as->name, bs->name);
1029         } 
1030         /* if the metric and the category name is the same, we check the variable metric */
1031         if (as->var_metric < bs->var_metric) {
1032                 return -1;
1033         } else if (as->var_metric > bs->var_metric) {
1034                 return 1;
1035         }
1036
1037         return 0;
1038 }
1039
1040 /*! \brief See Asterisk doc
1041  *
1042 *       This is for Static Realtime (again: I think...)
1043 *       
1044 *       load the configuration stuff for the .conf files
1045 *       called on a reload
1046 */
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)
1049 {
1050         unsigned int vars_count = 0;
1051         struct ast_variable **vars;
1052         int i = 0;
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;
1059
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);
1062                 return NULL;
1063         }
1064
1065         vars = realtime_ldap_base(&vars_count, basedn, table_name, "filename", file, "commented", "FALSE", NULL);
1066
1067         if (!vars) {
1068                 ast_log(LOG_WARNING, "Could not find config '%s' in directory.\n", file);
1069                 return NULL;
1070         }
1071
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.
1075          */
1076         if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
1077                 return NULL;
1078         }
1079
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");
1087                         
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);
1092
1093                 if (!category) {
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);
1110                 } else {
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);
1116                         vars_count++;
1117                 }
1118         }
1119
1120         qsort(categories, vars_count, sizeof(*categories), compare_categories);
1121
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)) {
1126                                 break;
1127                         }
1128                         continue;
1129                 }
1130
1131                 if (!last_category || strcmp(last_category, categories[i].name) ||
1132                         last_category_metric != categories[i].metric) {
1133
1134                         cur_cat = ast_category_new(categories[i].name, table_name, -1);
1135                         if (!cur_cat) {
1136                                 break;
1137                         }
1138                         last_category = categories[i].name;
1139                         last_category_metric = categories[i].metric;
1140                         ast_category_append(cfg, cur_cat);
1141                 }
1142
1143                 if (!(new_v = ast_variable_new(categories[i].variable_name, categories[i].variable_value, table_name))) {
1144                         break;
1145                 }
1146
1147                 ast_variable_append(cur_cat, new_v);
1148         }
1149
1150         ast_free(vars);
1151         ast_free(categories);
1152
1153         return cfg;
1154 }
1155
1156 /* \brief Function to update a set of values in ldap static mode
1157 */
1158 static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
1159         const char *lookup, va_list ap)
1160 {
1161         int error = 0;
1162         LDAPMessage *ldap_entry = NULL;
1163         LDAPMod **ldap_mods;
1164         const char *newparam = NULL;
1165         const char *newval = NULL;
1166         char *dn;
1167         int num_entries = 0;
1168         int i = 0;
1169         int mods_size = 0;
1170         int mod_exists = 0;
1171         struct ldap_table_config *table_config = NULL;
1172         char *clean_basedn = NULL;
1173         struct ast_str *filter = NULL;
1174         int tries = 0;
1175         int result = 0;
1176         LDAPMessage *ldap_result_msg = NULL;
1177
1178         if (!table_name) {
1179                 ast_log(LOG_ERROR, "No table_name specified.\n");
1180                 return -1;
1181         } 
1182
1183         if (!(filter = ast_str_create(80))) {
1184                 return -1;
1185         }
1186
1187         if (!attribute || !lookup) {
1188                 ast_log(LOG_WARNING, "LINE(%d): search parameters are empty.\n", __LINE__);
1189                 return -1;
1190         }
1191         ast_mutex_lock(&ldap_lock);
1192
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);
1196                 return -1;
1197         }
1198
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);
1203                 return -1;
1204         }
1205
1206         clean_basedn = cleaned_basedn(NULL, basedn);
1207
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);
1212         }
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);
1215         }
1216         append_var_and_value_to_filter(&filter, table_config, attribute, lookup);
1217         ast_str_append(&filter, 0, ")");
1218
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__);
1227                 return -1;
1228         }
1229
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));
1233
1234         ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
1235         ldap_mods[0]->mod_type = ast_strdup(newparam);
1236
1237         ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
1238         ldap_mods[0]->mod_values[0] = ast_strdup(newval);
1239
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 *);
1243                 mod_exists = 0;
1244
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);
1251                                 mod_exists = 1; 
1252                                 break;
1253                         }
1254                 }
1255
1256                 /* create new mod */
1257                 if (!mod_exists) {
1258                         mods_size++;
1259                         ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
1260                         ldap_mods[mods_size - 1] = NULL;
1261                         
1262                         ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
1263
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);
1266
1267                         if (strlen(newval) == 0) {
1268                                 ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
1269                         } else {
1270                                 ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
1271
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);
1275                         }
1276                 }
1277         }
1278         /* freeing ldap_mods further down */
1279
1280         do {
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,
1284                                   &ldap_result_msg);
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);
1287                         tries++;
1288                         if (tries < 3) {
1289                                 usleep(500000L * tries);
1290                                 if (ldapConn) {
1291                                         ldap_unbind_ext_s(ldapConn, NULL, NULL);
1292                                         ldapConn = NULL;
1293                                 }
1294                                 if (!ldap_reconnect())
1295                                         break;
1296                         }
1297                 }
1298         } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
1299
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));
1303
1304                 ast_mutex_unlock(&ldap_lock);
1305                 free(filter);
1306                 free(clean_basedn);
1307                 ldap_msgfree(ldap_result_msg);
1308                 ldap_mods_free(ldap_mods, 0);
1309                 return -1;
1310         }
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]);
1317                         } else {
1318                                 ast_debug(3, "LINE(%d) deleting %s \n", __LINE__, ldap_mods[i]->mod_type);
1319                         }
1320                 }
1321                 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
1322
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));
1328                         }
1329                         ldap_memfree(dn);
1330                         ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
1331                 }
1332         }
1333
1334         ast_mutex_unlock(&ldap_lock);
1335         ast_free(filter);
1336         ast_free(clean_basedn);
1337         ldap_msgfree(ldap_result_msg);
1338         ldap_mods_free(ldap_mods, 0);
1339         return num_entries;
1340 }
1341
1342 static int update2_ldap(const char *basedn, const char *table_name, va_list ap)
1343 {
1344         int error = 0;
1345         LDAPMessage *ldap_entry = NULL;
1346         LDAPMod **ldap_mods;
1347         const char *newparam = NULL;
1348         const char *newval = NULL;
1349         char *dn;
1350         int num_entries = 0;
1351         int i = 0;
1352         int mods_size = 0;
1353         int mod_exists = 0;
1354         struct ldap_table_config *table_config = NULL;
1355         char *clean_basedn = NULL;
1356         struct ast_str *filter = NULL;
1357         int tries = 0;
1358         int result = 0;
1359         LDAPMessage *ldap_result_msg = NULL;
1360
1361         if (!table_name) {
1362                 ast_log(LOG_ERROR, "No table_name specified.\n");
1363                 return -1;
1364         } 
1365
1366         if (!(filter = ast_str_create(80))) {
1367                 return -1;
1368         }
1369
1370         ast_mutex_lock(&ldap_lock);
1371
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);
1375                 ast_free(filter);
1376                 return -1;
1377         }
1378
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);
1383                 ast_free(filter);
1384                 return -1;
1385         }
1386
1387         clean_basedn = cleaned_basedn(NULL, basedn);
1388
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);
1393         }
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);
1397         }
1398
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);
1403         }
1404         ast_str_append(&filter, 0, ")");
1405
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__);
1414                 ast_free(filter);
1415                 ast_free(clean_basedn);
1416                 return -1;
1417         }
1418
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));
1422
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);
1426
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);
1430
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 *);
1434                 mod_exists = 0;
1435
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);
1442                                 mod_exists = 1; 
1443                                 break;
1444                         }
1445                 }
1446
1447                 /* create new mod */
1448                 if (!mod_exists) {
1449                         mods_size++;
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));
1453
1454                         ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
1455
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);
1458
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);
1462                 }
1463         }
1464         /* freeing ldap_mods further down */
1465
1466         do {
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,
1470                                   &ldap_result_msg);
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);
1473                         tries++;
1474                         if (tries < 3) {
1475                                 usleep(500000L * tries);
1476                                 if (ldapConn) {
1477                                         ldap_unbind_ext_s(ldapConn, NULL, NULL);
1478                                         ldapConn = NULL;
1479                                 }
1480                                 if (!ldap_reconnect()) {
1481                                         break;
1482                                 }
1483                         }
1484                 }
1485         } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
1486
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));
1490
1491                 ast_mutex_unlock(&ldap_lock);
1492                 ast_free(filter);
1493                 ast_free(clean_basedn);
1494                 ldap_msgfree(ldap_result_msg);
1495                 ldap_mods_free(ldap_mods, 0);
1496                 return -1;
1497         }
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]);
1502                 }
1503
1504                 ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
1505
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));
1510                         }
1511                         ldap_memfree(dn);
1512                         ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
1513                 }
1514         }
1515
1516         ast_mutex_unlock(&ldap_lock);
1517         if (filter) {
1518                 ast_free(filter);
1519         }
1520         if (clean_basedn) {
1521                 ast_free(clean_basedn);
1522         }
1523         ldap_msgfree(ldap_result_msg);
1524         ldap_mods_free(ldap_mods, 0);
1525         return num_entries;
1526 }
1527
1528 static struct ast_config_engine ldap_engine = {
1529         .name = "ldap",
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,
1535 };
1536
1537 static int load_module(void)
1538 {
1539         if (parse_config() < 0) {
1540                 ast_log(LOG_ERROR, "Cannot load LDAP RealTime driver.\n");
1541                 return 0;
1542         }
1543
1544         ast_mutex_lock(&ldap_lock);
1545
1546         if (!ldap_reconnect())  {
1547                 ast_log(LOG_WARNING, "Couldn't establish connection to LDAP directory. Check debug.\n");
1548         }
1549
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));
1553
1554         ast_mutex_unlock(&ldap_lock);
1555
1556         return 0;
1557 }
1558
1559 static int unload_module(void)
1560 {
1561         /* Aquire control before doing anything to the module itself. */
1562         ast_mutex_lock(&ldap_lock);
1563
1564         table_configs_free();
1565
1566         if (ldapConn) {
1567                 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1568                 ldapConn = NULL;
1569         }
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");
1573
1574         /* Unlock so something else can destroy the lock. */
1575         ast_mutex_unlock(&ldap_lock);
1576
1577         return 0;
1578 }
1579
1580 static int reload(void)
1581 {
1582         /* Aquire control before doing anything to the module itself. */
1583         ast_mutex_lock(&ldap_lock);
1584
1585         if (ldapConn) {
1586                 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1587                 ldapConn = NULL;
1588         }
1589
1590         if (parse_config() < 0) {
1591                 ast_log(LOG_NOTICE, "Cannot reload LDAP RealTime driver.\n");
1592                 ast_mutex_unlock(&ldap_lock);
1593                 return 0;
1594         }               
1595
1596         if (!ldap_reconnect())  {
1597                 ast_log(LOG_WARNING, "Couldn't establish connection to your directory server. Check debug.\n");
1598         }
1599
1600         ast_verb(2, "LDAP RealTime driver reloaded.\n");
1601
1602         /* Done reloading. Release lock so others can now use driver. */
1603         ast_mutex_unlock(&ldap_lock);
1604
1605         return 0;
1606 }
1607
1608 /*! \brief parse the configuration file */
1609 static int parse_config(void)
1610 {
1611         struct ast_config *config;
1612         struct ast_flags config_flags = {0};
1613         const char *s, *host;
1614         int port;
1615         char *category_name = NULL;
1616
1617         /* Make sure that global variables are reset */
1618         url[0] = '\0';
1619         user[0] = '\0';
1620         pass[0] = '\0';
1621         base_distinguished_name[0] = '\0';
1622         version = 3;
1623
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);
1627                 return -1;
1628         }
1629
1630         if (!(s = ast_variable_retrieve(config, "_general", "user"))) {
1631                 ast_log(LOG_NOTICE, "No directory user found, anonymous binding as default.\n");
1632                 user[0] = '\0';
1633         } else {
1634                 ast_copy_string(user, s, sizeof(user));
1635         }
1636
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));
1641                 } else {
1642                         ast_copy_string(pass, s, sizeof(pass));
1643                 }
1644         }
1645
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");
1652                         port = 389;
1653                 }
1654
1655                 snprintf(url, sizeof(url), "ldap://%s:%d", host, port);
1656         } else {
1657                 ast_log(LOG_ERROR, "No directory URL or host found.\n");
1658                 ast_config_destroy(config);
1659                 return -1;
1660         }
1661
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));
1665         } else 
1666                 ast_copy_string(base_distinguished_name, s, sizeof(base_distinguished_name));
1667
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);
1672                 version = 3;
1673         }
1674
1675         table_configs_free();
1676
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);
1681                 
1682                 if (var) {
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);
1688                                 if (is_general)
1689                                         base_table_config = table_config;
1690                                 if (is_config)
1691                                         static_table_config = table_config;
1692                         }
1693                         for (; var; var = var->next) {
1694                                 if (!strcasecmp(var->name, "additionalFilter")) {
1695                                         table_config->additional_filter = ast_strdup(var->value);
1696                                 } else {
1697                                         ldap_table_config_add_attribute(table_config, var->name, var->value);
1698                                 }
1699                         }
1700                 }
1701         }
1702
1703         ast_config_destroy(config);
1704
1705         return 1;
1706 }
1707
1708 /*! \note ldap_lock should have been locked before calling this function. */
1709 static int ldap_reconnect(void)
1710 {
1711         int bind_result = 0;
1712         struct berval cred;
1713
1714         if (ldapConn) {
1715                 ast_debug(2, "Everything seems fine.\n");
1716                 return 1;
1717         }
1718
1719         if (ast_strlen_zero(url)) {
1720                 ast_log(LOG_ERROR, "Not enough parameters to connect to ldap directory\n");
1721                 return 0;
1722         }
1723
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);
1726                 return 0;
1727         }
1728
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);
1731         }
1732
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);
1738         } else {
1739                 ast_debug(2, "bind %s anonymously\n", url);
1740                 cred.bv_val = NULL;
1741                 cred.bv_len = 0;
1742                 bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
1743         }
1744         if (bind_result == LDAP_SUCCESS) {
1745                 ast_debug(2, "Successfully connected to directory.\n");
1746                 connect_time = time(NULL);
1747                 return 1;
1748         } else {
1749                 ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result));
1750                 ldap_unbind_ext_s(ldapConn, NULL, NULL);
1751                 ldapConn = NULL;
1752                 return 0;
1753         }
1754 }
1755
1756 static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1757 {
1758         char status[256], credentials[100] = "";
1759         int ctimesec = time(NULL) - connect_time;
1760
1761         switch (cmd) {
1762         case CLI_INIT:
1763                 e->command = "realtime show ldap status";
1764                 e->usage =
1765                         "Usage: realtime show ldap status\n"
1766                         "              Shows connection information for the LDAP RealTime driver\n";
1767                 return NULL;
1768         case CLI_GENERATE:
1769                 return NULL;
1770         }
1771
1772         if (!ldapConn)
1773                 return CLI_FAILURE;
1774
1775         if (!ast_strlen_zero(url)) 
1776                 snprintf(status, sizeof(status), "Connected to '%s', baseDN %s", url, base_distinguished_name);
1777
1778         if (!ast_strlen_zero(user))
1779                 snprintf(credentials, sizeof(credentials), " with username %s", user);
1780
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,
1793                                 ctimesec % 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);
1797         } else {
1798                 ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
1799         }
1800
1801         return CLI_SUCCESS;
1802 }
1803
1804 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "LDAP realtime interface",
1805         .load = load_module,
1806         .unload = unload_module,
1807         .reload = reload,
1808         .load_pri = AST_MODPRI_REALTIME_DRIVER,
1809 );