123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2005, Oxymium sarl
- * Manuel Guesdon <mguesdon@oxymium.net> - LDAP RealTime Driver Author/Adaptor
- *
- * Copyright (C) 2007, Digium, Inc.
- * Russell Bryant <russell@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- *
- */
- /*! \file
- *
- * \brief LDAP plugin for portable configuration engine (ARA)
- *
- * \author Mark Spencer <markster@digium.com>
- * \author Manuel Guesdon
- * \author Carl-Einar Thorner <cthorner@voicerd.com>
- * \author Russell Bryant <russell@digium.com>
- *
- * OpenLDAP http://www.openldap.org
- */
- /*! \li \ref res_config_ldap.c uses the configuration file \ref res_ldap.conf
- * \addtogroup configuration_file Configuration Files
- */
- /*!
- * \page res_ldap.conf res_ldap.conf
- * \verbinclude res_ldap.conf.sample
- */
- /*** MODULEINFO
- <depend>ldap</depend>
- <support_level>extended</support_level>
- ***/
- #include "asterisk.h"
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <ldap.h>
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/channel.h"
- #include "asterisk/logger.h"
- #include "asterisk/config.h"
- #include "asterisk/module.h"
- #include "asterisk/lock.h"
- #include "asterisk/options.h"
- #include "asterisk/cli.h"
- #include "asterisk/utils.h"
- #include "asterisk/strings.h"
- #include "asterisk/pbx.h"
- #include "asterisk/linkedlists.h"
- #define RES_CONFIG_LDAP_CONF "res_ldap.conf"
- #define RES_CONFIG_LDAP_DEFAULT_BASEDN "asterisk"
- AST_MUTEX_DEFINE_STATIC(ldap_lock);
- static LDAP *ldapConn;
- static char url[512];
- static char user[512];
- static char pass[512];
- static char base_distinguished_name[512];
- static int version;
- static time_t connect_time;
- static int parse_config(void);
- static int ldap_reconnect(void);
- static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
- struct category_and_metric {
- const char *name;
- int metric;
- const char *variable_name;
- const char *variable_value;
- int var_metric; /*!< For organizing variables (particularly includes and switch statments) within a context */
- };
- /*! \brief Table configuration
- */
- struct ldap_table_config {
- char *table_name; /*!< table name */
- char *additional_filter; /*!< additional filter */
- struct ast_variable *attributes; /*!< attribute names conversion */
- struct ast_variable *delimiters; /*!< the current delimiter is semicolon, so we are not using this variable */
- AST_LIST_ENTRY(ldap_table_config) entry;
- /* TODO: Make proxies work */
- };
- /*! \brief Should be locked before using it
- */
- static AST_LIST_HEAD_NOLOCK_STATIC(table_configs, ldap_table_config);
- static struct ldap_table_config *base_table_config;
- static struct ldap_table_config *static_table_config;
- static struct ast_cli_entry ldap_cli[] = {
- AST_CLI_DEFINE(realtime_ldap_status, "Shows connection information for the LDAP RealTime driver"),
- };
- /*! \brief Create a new table_config
- */
- static struct ldap_table_config *table_config_new(const char *table_name)
- {
- struct ldap_table_config *p;
- if (!(p = ast_calloc(1, sizeof(*p))))
- return NULL;
- if (table_name) {
- if (!(p->table_name = ast_strdup(table_name))) {
- free(p);
- return NULL;
- }
- }
- return p;
- }
- /*! \brief Find a table_config
- *
- * Should be locked before using it
- *
- * \note This function assumes ldap_lock to be locked.
- */
- static struct ldap_table_config *table_config_for_table_name(const char *table_name)
- {
- struct ldap_table_config *c = NULL;
- AST_LIST_TRAVERSE(&table_configs, c, entry) {
- if (!strcmp(c->table_name, table_name))
- break;
- }
- return c;
- }
- /*! \brief Find variable by name
- */
- static struct ast_variable *variable_named(struct ast_variable *var, const char *name)
- {
- for (; var; var = var->next) {
- if (!strcasecmp(name, var->name))
- break;
- }
- return var;
- }
- /*! \brief Count semicolons in string
- * \param somestr - pointer to a string
- *
- * \return number of occurances of the delimiter(semicolon)
- */
- static int semicolon_count_str(const char *somestr)
- {
- int count = 0;
- for (; *somestr; somestr++) {
- if (*somestr == ';')
- count++;
- }
- return count;
- }
- /* \brief Count semicolons in variables
- *
- * takes a linked list of \a ast_variable variables, finds the one with the name variable_value
- * and returns the number of semicolons in the value for that \a ast_variable
- */
- static int semicolon_count_var(struct ast_variable *var)
- {
- struct ast_variable *var_value = variable_named(var, "variable_value");
- if (!var_value) {
- return 0;
- }
- ast_debug(2, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
- return semicolon_count_str(var_value->value);
- }
- /*! \brief add attribute to table config
- *
- * Should be locked before using it
- */
- static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
- const char *attribute_name, const char *attribute_value)
- {
- struct ast_variable *var;
- if (ast_strlen_zero(attribute_name) || ast_strlen_zero(attribute_value)) {
- return;
- }
- if (!(var = ast_variable_new(attribute_name, attribute_value, table_config->table_name))) {
- return;
- }
- if (table_config->attributes) {
- var->next = table_config->attributes;
- }
- table_config->attributes = var;
- }
- /*! \brief Free table_config
- *
- * \note assumes ldap_lock to be locked
- */
- static void table_configs_free(void)
- {
- struct ldap_table_config *c;
- while ((c = AST_LIST_REMOVE_HEAD(&table_configs, entry))) {
- if (c->table_name) {
- ast_free(c->table_name);
- }
- if (c->additional_filter) {
- ast_free(c->additional_filter);
- }
- if (c->attributes) {
- ast_variables_destroy(c->attributes);
- }
- free(c);
- }
- base_table_config = NULL;
- static_table_config = NULL;
- }
- /*! \brief Convert variable name to ldap attribute name
- *
- * \note Should be locked before using it
- */
- static const char *convert_attribute_name_to_ldap(struct ldap_table_config *table_config,
- const char *attribute_name)
- {
- int i = 0;
- struct ldap_table_config *configs[] = { table_config, base_table_config };
- for (i = 0; i < ARRAY_LEN(configs); i++) {
- struct ast_variable *attribute;
- if (!configs[i]) {
- continue;
- }
- attribute = configs[i]->attributes;
- for (; attribute; attribute = attribute->next) {
- if (!strcasecmp(attribute_name, attribute->name)) {
- return attribute->value;
- }
- }
- }
- return attribute_name;
- }
- /*! \brief Convert ldap attribute name to variable name
- *
- * \note Should be locked before using it
- */
- static const char *convert_attribute_name_from_ldap(struct ldap_table_config *table_config,
- const char *attribute_name)
- {
- int i = 0;
- struct ldap_table_config *configs[] = { table_config, base_table_config };
- for (i = 0; i < ARRAY_LEN(configs); i++) {
- struct ast_variable *attribute;
- if (!configs[i]) {
- continue;
- }
- attribute = configs[i]->attributes;
- for (; attribute; attribute = attribute->next) {
- if (strcasecmp(attribute_name, attribute->value) == 0) {
- return attribute->name;
- }
- }
- }
- return attribute_name;
- }
- /*! \brief Get variables from ldap entry attributes
- * \note Should be locked before using it
- * \return a linked list of ast_variable variables.
- */
- static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config *table_config,
- LDAPMessage *ldap_entry)
- {
- BerElement *ber = NULL;
- struct ast_variable *var = NULL;
- struct ast_variable *prev = NULL;
- int is_delimited = 0;
- int i = 0;
- char *ldap_attribute_name;
- struct berval *value;
- int pos = 0;
- ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
- while (ldap_attribute_name) {
- struct berval **values = NULL;
- const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
- int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
- values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name); /* these are freed at the end */
- if (values) {
- struct berval **v;
- char *valptr;
- for (v = values; *v; v++) {
- value = *v;
- valptr = value->bv_val;
- ast_debug(2, "LINE(%d) attribute_name: %s LDAP value: %s\n", __LINE__, attribute_name, valptr);
- if (is_realmed_password_attribute) {
- if (!strncasecmp(valptr, "{md5}", 5)) {
- valptr += 5;
- }
- ast_debug(2, "md5: %s\n", valptr);
- }
- if (valptr) {
- /* ok, so looping through all delimited values except the last one (not, last character is not delimited...) */
- if (is_delimited) {
- i = 0;
- pos = 0;
- while (!ast_strlen_zero(valptr + i)) {
- if (valptr[i] == ';') {
- valptr[i] = '\0';
- if (prev) {
- prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
- }
- pos = i + 1;
- }
- i++;
- }
- }
- /* for the last delimited value or if the value is not delimited: */
- if (prev) {
- prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
- }
- }
- }
- ldap_value_free_len(values);
- }
- ldap_memfree(ldap_attribute_name);
- ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
- }
- ber_free(ber, 0);
- return var;
- }
- /*! \brief Get variables from ldap entry attributes - Should be locked before using it
- *
- * The results are freed outside this function so is the \a vars array.
- *
- * \return \a vars - an array of ast_variable variables terminated with a null.
- */
- static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_config *table_config,
- LDAPMessage *ldap_result_msg, unsigned int *entries_count_ptr)
- {
- struct ast_variable **vars;
- int i = 0;
- int tot_count = 0;
- int entry_index = 0;
- LDAPMessage *ldap_entry = NULL;
- BerElement *ber = NULL;
- struct ast_variable *var = NULL;
- struct ast_variable *prev = NULL;
- int is_delimited = 0;
- char *delim_value = NULL;
- int delim_tot_count = 0;
- int delim_count = 0;
- /* \breif First find the total count
- */
- ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
- for (tot_count = 0; ldap_entry; tot_count++) {
- struct ast_variable *tmp = realtime_ldap_entry_to_var(table_config, ldap_entry);
- tot_count += semicolon_count_var(tmp);
- ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
- ast_variables_destroy(tmp);
- }
- if (entries_count_ptr) {
- *entries_count_ptr = tot_count;
- }
- /*! \note Now that we have the total count we allocate space and create the variables
- * Remember that each element in vars is a linked list that points to realtime variable.
- * If the we are dealing with a static realtime variable we create a new element in the \a vars array for each delimited
- * 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.
- * This memory must be freed outside of this function.
- */
- vars = ast_calloc(sizeof(struct ast_variable *), tot_count + 1);
- ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
- i = 0;
- /* \brief For each static realtime variable we may create several entries in the \a vars array if it's delimited
- */
- for (entry_index = 0; ldap_entry; ) {
- int pos = 0;
- delim_value = NULL;
- delim_tot_count = 0;
- delim_count = 0;
- do { /* while delim_count */
- /* Starting new static var */
- char *ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
- struct berval *value;
- while (ldap_attribute_name) {
- const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
- int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
- struct berval **values = NULL;
- values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);
- if (values) {
- struct berval **v;
- char *valptr;
- for (v = values; *v; v++) {
- value = *v;
- valptr = value->bv_val;
- if (is_realmed_password_attribute) {
- if (strncasecmp(valptr, "{md5}", 5) == 0) {
- valptr += 5;
- }
- ast_debug(2, "md5: %s\n", valptr);
- }
- if (valptr) {
- if (delim_value == NULL && !is_realmed_password_attribute
- && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0)) {
- delim_value = ast_strdup(valptr);
- if ((delim_tot_count = semicolon_count_str(delim_value)) > 0) {
- ast_debug(4, "LINE(%d) is delimited %d times: %s\n", __LINE__, delim_tot_count, delim_value);
- is_delimited = 1;
- }
- }
- if (is_delimited != 0 && !is_realmed_password_attribute
- && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0) ) {
- /* for non-Static RealTime, first */
- for (i = pos; !ast_strlen_zero(valptr + i); i++) {
- ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
- if (delim_value[i] == ';') {
- delim_value[i] = '\0';
- ast_debug(2, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
- if (prev) {
- prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
- }
- pos = i + 1;
- if (static_table_config == table_config) {
- break;
- }
- }
- }
- if (ast_strlen_zero(valptr + i)) {
- ast_debug(4, "LINE(%d) DELIM pos: %d i: %d delim_count: %d\n", __LINE__, pos, i, delim_count);
- /* Last delimited value */
- ast_debug(4, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
- if (prev) {
- prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
- }
- /* Remembering to free memory */
- is_delimited = 0;
- pos = 0;
- }
- free(delim_value);
- delim_value = NULL;
- ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
- } else {
- /* not delimited */
- if (delim_value) {
- free(delim_value);
- delim_value = NULL;
- }
- ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
- if (prev) {
- prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(attribute_name, valptr, table_config->table_name);
- }
- }
- }
- } /*!< for (v = values; *v; v++) */
- ldap_value_free_len(values);
- }/*!< if (values) */
- ldap_memfree(ldap_attribute_name);
- ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
- } /*!< while (ldap_attribute_name) */
- ber_free(ber, 0);
- if (static_table_config == table_config) {
- if (option_debug > 2) {
- const struct ast_variable *tmpdebug = variable_named(var, "variable_name");
- const struct ast_variable *tmpdebug2 = variable_named(var, "variable_value");
- if (tmpdebug && tmpdebug2) {
- ast_debug(3, "LINE(%d) Added to vars - %s = %s\n", __LINE__, tmpdebug->value, tmpdebug2->value);
- }
- }
- vars[entry_index++] = var;
- prev = NULL;
- }
- delim_count++;
- } while (delim_count <= delim_tot_count && static_table_config == table_config);
- if (static_table_config != table_config) {
- ast_debug(3, "LINE(%d) Added to vars - non static\n", __LINE__);
- vars[entry_index++] = var;
- prev = NULL;
- }
- ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
- } /*!< end for loop over ldap_entry */
- return vars;
- }
- /*! \brief Check if we have a connection error
- */
- static int is_ldap_connect_error(int err)
- {
- return (err == LDAP_SERVER_DOWN || err == LDAP_TIMEOUT || err == LDAP_CONNECT_ERROR);
- }
- /*! \brief Get LDAP entry by dn and return attributes as variables
- *
- * Should be locked before using it
- *
- * This is used for setting the default values of an object
- * i.e., with accountBaseDN
- */
- static struct ast_variable *ldap_loadentry(struct ldap_table_config *table_config,
- const char *dn)
- {
- if (!table_config) {
- ast_log(LOG_ERROR, "No table config\n");
- return NULL;
- } else {
- struct ast_variable **vars = NULL;
- struct ast_variable *var = NULL;
- int result = -1;
- LDAPMessage *ldap_result_msg = NULL;
- int tries = 0;
- ast_debug(2, "ldap_loadentry dn=%s\n", dn);
- do {
- result = ldap_search_ext_s(ldapConn, dn, LDAP_SCOPE_BASE,
- "(objectclass=*)", NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &ldap_result_msg);
- if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
- ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
- tries++;
- if (tries < 3) {
- usleep(500000L * tries);
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- if (!ldap_reconnect()) {
- break;
- }
- }
- }
- } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
- if (result != LDAP_SUCCESS) {
- ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
- ast_debug(2, "dn=%s\n", dn);
- ast_mutex_unlock(&ldap_lock);
- return NULL;
- } else {
- int num_entry = 0;
- unsigned int *entries_count_ptr = NULL; /*!< not using this */
- if ((num_entry = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
- ast_debug(3, "num_entry: %d\n", num_entry);
- vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
- if (num_entry > 1) {
- ast_log(LOG_NOTICE, "More than one entry for dn=%s. Take only 1st one\n", dn);
- }
- } else {
- ast_debug(2, "Could not find any entry dn=%s.\n", dn);
- }
- }
- ldap_msgfree(ldap_result_msg);
- /* Chopping \a vars down to one variable */
- if (vars != NULL) {
- struct ast_variable **p = vars;
- /* Only take the first one. */
- var = *vars;
- /* Destroy the rest. */
- while (*++p) {
- ast_variables_destroy(*p);
- }
- ast_free(vars);
- }
- return var;
- }
- }
- /*! \note caller should free returned pointer
- */
- static char *substituted(struct ast_channel *channel, const char *string)
- {
- #define MAXRESULT 2048
- char *ret_string = NULL;
- if (!ast_strlen_zero(string)) {
- ret_string = ast_calloc(1, MAXRESULT);
- pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1);
- }
- ast_debug(2, "substituted: string: '%s' => '%s' \n", string, ret_string);
- return ret_string;
- }
- /*! \note caller should free returned pointer
- */
- static char *cleaned_basedn(struct ast_channel *channel, const char *basedn)
- {
- char *cbasedn = NULL;
- if (basedn) {
- char *p = NULL;
- cbasedn = substituted(channel, basedn);
- if (*cbasedn == '"') {
- cbasedn++;
- if (!ast_strlen_zero(cbasedn)) {
- int len = strlen(cbasedn);
- if (cbasedn[len - 1] == '"')
- cbasedn[len - 1] = '\0';
- }
- }
- p = cbasedn;
- while (*p) {
- if (*p == '|')
- *p = ',';
- p++;
- }
- }
- ast_debug(2, "basedn: '%s' => '%s' \n", basedn, cbasedn);
- return cbasedn;
- }
- /*! \brief Replace \<search\> by \<by\> in string.
- * \note No check is done on string allocated size !
- */
- static int replace_string_in_string(char *string, const char *search, const char *by)
- {
- int search_len = strlen(search);
- int by_len = strlen(by);
- int replaced = 0;
- char *p = strstr(string, search);
- if (p) {
- replaced = 1;
- while (p) {
- if (by_len == search_len) {
- memcpy(p, by, by_len);
- } else {
- memmove(p + by_len, p + search_len, strlen(p + search_len) + 1);
- memcpy(p, by, by_len);
- }
- p = strstr(p + by_len, search);
- }
- }
- return replaced;
- }
- /*! \brief Append a name=value filter string. The filter string can grow.
- */
- static void append_var_and_value_to_filter(struct ast_str **filter,
- struct ldap_table_config *table_config,
- const char *name, const char *value)
- {
- char *new_name = NULL;
- char *new_value = NULL;
- char *like_pos = strstr(name, " LIKE");
- ast_debug(2, "name='%s' value='%s'\n", name, value);
- if (like_pos) {
- int len = like_pos - name;
- name = new_name = ast_strdupa(name);
- new_name[len] = '\0';
- value = new_value = ast_strdupa(value);
- replace_string_in_string(new_value, "\\_", "_");
- replace_string_in_string(new_value, "%", "*");
- }
- name = convert_attribute_name_to_ldap(table_config, name);
- ast_str_append(filter, 0, "(%s=%s)", name, value);
- }
- /*! \brief LDAP base function
- * \return a null terminated array of ast_variable (one per entry) or NULL if no entry is found or if an error occured
- * caller should free the returned array and ast_variables
- * \param entries_count_ptr is a pointer to found entries count (can be NULL)
- * \param basedn is the base DN
- * \param table_name is the table_name (used dor attribute convertion and additional filter)
- * \param fields contains list of pairs name/value
- */
- static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_ptr,
- const char *basedn, const char *table_name, const struct ast_variable *fields)
- {
- struct ast_variable **vars = NULL;
- const struct ast_variable *field = fields;
- struct ldap_table_config *table_config = NULL;
- char *clean_basedn = cleaned_basedn(NULL, basedn);
- struct ast_str *filter = NULL;
- int tries = 0;
- int result = 0;
- LDAPMessage *ldap_result_msg = NULL;
- if (!table_name) {
- ast_log(LOG_ERROR, "No table_name specified.\n");
- ast_free(clean_basedn);
- return NULL;
- }
- if (!(filter = ast_str_create(80))) {
- ast_log(LOG_ERROR, "Can't initialize data structures.n");
- ast_free(clean_basedn);
- return NULL;
- }
- if (!field) {
- ast_log(LOG_ERROR, "Realtime retrieval requires at least 1 parameter"
- " and 1 value to search on.\n");
- ast_free(filter);
- ast_free(clean_basedn);
- return NULL;
- }
- ast_mutex_lock(&ldap_lock);
- /* We now have our complete statement; Lets connect to the server and execute it. */
- if (!ldap_reconnect()) {
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- ast_free(clean_basedn);
- return NULL;
- }
- table_config = table_config_for_table_name(table_name);
- if (!table_config) {
- ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- ast_free(clean_basedn);
- return NULL;
- }
- ast_str_append(&filter, 0, "(&");
- if (table_config && table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", table_config->additional_filter);
- }
- if (table_config != base_table_config && base_table_config &&
- base_table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
- }
- /* Create the first part of the query using the first parameter/value pairs we just extracted.
- * If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat
- */
- append_var_and_value_to_filter(&filter, table_config, field->name, field->value);
- while ((field = field->next)) {
- append_var_and_value_to_filter(&filter, table_config, field->name, field->value);
- }
- ast_str_append(&filter, 0, ")");
- do {
- /* freeing ldap_result further down */
- result = ldap_search_ext_s(ldapConn, clean_basedn,
- LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
- &ldap_result_msg);
- if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
- ast_debug(1, "Failed to query directory. Try %d/10\n", tries + 1);
- if (++tries < 10) {
- usleep(1);
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- if (!ldap_reconnect()) {
- break;
- }
- }
- }
- } while (result != LDAP_SUCCESS && tries < 10 && is_ldap_connect_error(result));
- if (result != LDAP_SUCCESS) {
- ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
- ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
- } else {
- /* this is where we create the variables from the search result
- * freeing this \a vars outside this function */
- if (ldap_count_entries(ldapConn, ldap_result_msg) > 0) {
- /* is this a static var or some other? they are handled different for delimited values */
- vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
- } else {
- ast_debug(1, "Could not find any entry matching %s in base dn %s.\n", ast_str_buffer(filter), clean_basedn);
- }
- ldap_msgfree(ldap_result_msg);
- /*! \TODO get the default variables from the accountBaseDN, not implemented with delimited values
- */
- if (vars) {
- struct ast_variable **p = vars;
- while (*p) {
- struct ast_variable *append_var = NULL;
- struct ast_variable *tmp = *p;
- while (tmp) {
- if (strcasecmp(tmp->name, "accountBaseDN") == 0) {
- /* Get the variable to compare with for the defaults */
- struct ast_variable *base_var = ldap_loadentry(table_config, tmp->value);
-
- while (base_var) {
- struct ast_variable *next = base_var->next;
- struct ast_variable *test_var = *p;
- int base_var_found = 0;
- /* run throught the default values and fill it inn if it is missing */
- while (test_var) {
- if (strcasecmp(test_var->name, base_var->name) == 0) {
- base_var_found = 1;
- break;
- } else {
- test_var = test_var->next;
- }
- }
- if (base_var_found) {
- base_var->next = NULL;
- ast_variables_destroy(base_var);
- base_var = next;
- } else {
- /*!
- * \todo XXX The interactions with base_var and append_var may
- * cause a memory leak of base_var nodes. Also the append_var
- * list and base_var list may get cross linked.
- */
- if (append_var) {
- base_var->next = append_var;
- } else {
- base_var->next = NULL;
- }
- append_var = base_var;
- base_var = next;
- }
- }
- }
- if (!tmp->next && append_var) {
- tmp->next = append_var;
- tmp = NULL;
- } else {
- tmp = tmp->next;
- }
- }
- p++;
- }
- }
- }
- if (filter) {
- ast_free(filter);
- }
- if (clean_basedn) {
- ast_free(clean_basedn);
- }
- ast_mutex_unlock(&ldap_lock);
- return vars;
- }
- static struct ast_variable *realtime_arguments_to_fields(va_list ap)
- {
- struct ast_variable *fields = NULL;
- const char *newparam, *newval;
- while ((newparam = va_arg(ap, const char *))) {
- struct ast_variable *field;
- newval = va_arg(ap, const char *);
- if (!(field = ast_variable_new(newparam, newval, ""))) {
- ast_variables_destroy(fields);
- return NULL;
- }
- field->next = fields;
- fields = field;
- }
- return fields;
- }
- /*! \brief same as realtime_ldap_base_ap but take variable arguments count list
- */
- static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
- const char *basedn, const char *table_name, ...)
- {
- RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
- struct ast_variable **vars = NULL;
- va_list ap;
- va_start(ap, table_name);
- fields = realtime_arguments_to_fields(ap);
- va_end(ap);
- vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, fields);
- return vars;
- }
- /*! \brief See Asterisk doc
- *
- * For Realtime Dynamic(i.e., switch, queues, and directory)
- */
- static struct ast_variable *realtime_ldap(const char *basedn,
- const char *table_name, const struct ast_variable *fields)
- {
- struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, fields);
- struct ast_variable *var = NULL;
- if (vars) {
- struct ast_variable *last_var = NULL;
- struct ast_variable **p = vars;
- /* Chain the vars array of lists into one list to return. */
- while (*p) {
- if (last_var) {
- while (last_var->next) {
- last_var = last_var->next;
- }
- last_var->next = *p;
- } else {
- var = *p;
- last_var = var;
- }
- p++;
- }
- free(vars);
- }
- return var;
- }
- /*! \brief See Asterisk doc
- *
- * 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);
- * however, the ast_load_realtime wil match on wildcharacters also depending on what the mode is set to
- * this is an area of asterisk that could do with a lot of modification
- * I think this function returns Realtime dynamic objects
- */
- static struct ast_config *realtime_multi_ldap(const char *basedn,
- const char *table_name, const struct ast_variable *fields)
- {
- char *op;
- const char *initfield = NULL;
- struct ast_variable **vars =
- realtime_ldap_base_ap(NULL, basedn, table_name, fields);
- struct ast_config *cfg = NULL;
- if (!fields) {
- ast_log(LOG_WARNING, "realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
- return NULL;
- }
- initfield = ast_strdupa(fields->name);
- if ((op = strchr(initfield, ' '))) {
- *op = '\0';
- }
- if (vars) {
- cfg = ast_config_new();
- if (!cfg) {
- ast_log(LOG_ERROR, "Unable to create a config!\n");
- } else {
- struct ast_variable **p = vars;
- while (*p) {
- struct ast_category *cat = NULL;
- cat = ast_category_new("", table_name, -1);
- if (!cat) {
- ast_log(LOG_ERROR, "Unable to create a new category!\n");
- break;
- } else {
- struct ast_variable *var = *p;
- while (var) {
- struct ast_variable *next = var->next;
- if (initfield && !strcmp(initfield, var->name)) {
- ast_category_rename(cat, var->value);
- }
- var->next = NULL;
- ast_variable_append(cat, var);
- var = next;
- }
- }
- ast_category_append(cfg, cat);
- p++;
- }
- }
- free(vars);
- }
- return cfg;
- }
- /*! \brief Sorting alogrithm for qsort to find the order of the variables \a a and \a b
- * \param a pointer to category_and_metric struct
- * \param b pointer to category_and_metric struct
- *
- * \retval -1 for if b is greater
- * \retval 0 zero for equal
- * \retval 1 if a is greater
- */
- static int compare_categories(const void *a, const void *b)
- {
- const struct category_and_metric *as = a;
- const struct category_and_metric *bs = b;
- if (as->metric < bs->metric) {
- return -1;
- } else if (as->metric > bs->metric) {
- return 1;
- } else if (as->metric == bs->metric && strcmp(as->name, bs->name) != 0) {
- return strcmp(as->name, bs->name);
- }
- /* if the metric and the category name is the same, we check the variable metric */
- if (as->var_metric < bs->var_metric) {
- return -1;
- } else if (as->var_metric > bs->var_metric) {
- return 1;
- }
- return 0;
- }
- /*! \brief See Asterisk Realtime Documentation
- *
- * This is for Static Realtime
- *
- * load the configuration stuff for the .conf files
- * called on a reload
- */
- static struct ast_config *config_ldap(const char *basedn, const char *table_name,
- const char *file, struct ast_config *cfg, struct ast_flags config_flags, const char *sugg_incl, const char *who_asked)
- {
- unsigned int vars_count = 0;
- struct ast_variable **vars;
- int i = 0;
- struct ast_variable *new_v = NULL;
- struct ast_category *cur_cat = NULL;
- const char *last_category = NULL;
- int last_category_metric = 0;
- struct category_and_metric *categories;
- struct ast_variable **p;
- if (ast_strlen_zero(file) || !strcasecmp(file, RES_CONFIG_LDAP_CONF)) {
- ast_log(LOG_ERROR, "Missing configuration file: %s. Can't configure myself.\n", RES_CONFIG_LDAP_CONF);
- return NULL;
- }
- vars = realtime_ldap_base(&vars_count, basedn, table_name, "filename", file, "commented", "FALSE", NULL);
- if (!vars) {
- ast_log(LOG_WARNING, "Could not find config '%s' in directory.\n", file);
- return NULL;
- }
- /*! \note Since the items come back in random order, they need to be sorted
- * first, and since the data could easily exceed stack size, this is
- * allocated from the heap.
- */
- if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
- return NULL;
- }
- for (vars_count = 0, p = vars; *p; p++) {
- struct ast_variable *category = variable_named(*p, "category");
- struct ast_variable *cat_metric = variable_named(*p, "cat_metric");
- struct ast_variable *var_name = variable_named(*p, "variable_name");
- struct ast_variable *var_val = variable_named(*p, "variable_value");
- struct ast_variable *var_metric = variable_named(*p, "var_metric");
- struct ast_variable *dn = variable_named(*p, "dn");
- if (!category) {
- ast_log(LOG_ERROR, "No category name in entry '%s' for file '%s'.\n",
- (dn ? dn->value : "?"), file);
- } else if (!cat_metric) {
- ast_log(LOG_ERROR, "No category metric in entry '%s'(category: %s) for file '%s'.\n",
- (dn ? dn->value : "?"), category->value, file);
- } else if (!var_metric) {
- ast_log(LOG_ERROR, "No variable metric in entry '%s'(category: %s) for file '%s'.\n",
- (dn ? dn->value : "?"), category->value, file);
- } else if (!var_name) {
- ast_log(LOG_ERROR, "No variable name in entry '%s' (category: %s metric: %s) for file '%s'.\n",
- (dn ? dn->value : "?"), category->value,
- cat_metric->value, file);
- } else if (!var_val) {
- ast_log(LOG_ERROR, "No variable value in entry '%s' (category: %s metric: %s variable: %s) for file '%s'.\n",
- (dn ? dn->value : "?"), category->value,
- cat_metric->value, var_name->value, file);
- } else {
- categories[vars_count].name = category->value;
- categories[vars_count].metric = atoi(cat_metric->value);
- categories[vars_count].variable_name = var_name->value;
- categories[vars_count].variable_value = var_val->value;
- categories[vars_count].var_metric = atoi(var_metric->value);
- vars_count++;
- }
- ast_debug(3, "category: %s\n", category->value);
- ast_debug(3, "var_name: %s\n", var_name->value);
- ast_debug(3, "var_val: %s\n", var_val->value);
- ast_debug(3, "cat_metric: %s\n", cat_metric->value);
- }
- qsort(categories, vars_count, sizeof(*categories), compare_categories);
- for (i = 0; i < vars_count; i++) {
- if (!strcmp(categories[i].variable_name, "#include")) {
- struct ast_flags flags = { 0 };
- if (!ast_config_internal_load(categories[i].variable_value, cfg, flags, "", who_asked)) {
- break;
- }
- continue;
- }
- if (!last_category || strcmp(last_category, categories[i].name) ||
- last_category_metric != categories[i].metric) {
- cur_cat = ast_category_new(categories[i].name, table_name, -1);
- if (!cur_cat) {
- break;
- }
- last_category = categories[i].name;
- last_category_metric = categories[i].metric;
- ast_category_append(cfg, cur_cat);
- }
- if (!(new_v = ast_variable_new(categories[i].variable_name, categories[i].variable_value, table_name))) {
- break;
- }
- ast_variable_append(cur_cat, new_v);
- }
- ast_free(vars);
- ast_free(categories);
- return cfg;
- }
- /* \brief Function to update a set of values in ldap static mode
- */
- static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
- const char *lookup, const struct ast_variable *fields)
- {
- int error = 0;
- LDAPMessage *ldap_entry = NULL;
- LDAPMod **ldap_mods;
- const char *newparam;
- const struct ast_variable *field = fields;
- char *dn;
- int num_entries = 0;
- int i = 0;
- int mods_size = 0;
- int mod_exists = 0;
- struct ldap_table_config *table_config = NULL;
- char *clean_basedn = NULL;
- struct ast_str *filter = NULL;
- int tries = 0;
- int result = 0;
- LDAPMessage *ldap_result_msg = NULL;
- if (!table_name) {
- ast_log(LOG_ERROR, "No table_name specified.\n");
- return -1;
- }
- if (!(filter = ast_str_create(80))) {
- return -1;
- }
- if (!attribute || !lookup) {
- ast_log(LOG_WARNING, "LINE(%d): search parameters are empty.\n", __LINE__);
- return -1;
- }
- ast_mutex_lock(&ldap_lock);
- /* We now have our complete statement; Lets connect to the server and execute it. */
- if (!ldap_reconnect()) {
- ast_mutex_unlock(&ldap_lock);
- return -1;
- }
- table_config = table_config_for_table_name(table_name);
- if (!table_config) {
- ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
- ast_mutex_unlock(&ldap_lock);
- return -1;
- }
- clean_basedn = cleaned_basedn(NULL, basedn);
- /* Create the filter with the table additional filter and the parameter/value pairs we were given */
- ast_str_append(&filter, 0, "(&");
- if (table_config && table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", table_config->additional_filter);
- }
- if (table_config != base_table_config && base_table_config && base_table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
- }
- append_var_and_value_to_filter(&filter, table_config, attribute, lookup);
- ast_str_append(&filter, 0, ")");
- /* Create the modification array with the parameter/value pairs we were given,
- * if there are several parameters with the same name, we collect them into
- * one parameter/value pair and delimit them with a semicolon */
- newparam = convert_attribute_name_to_ldap(table_config, field->name);
- if (!newparam) {
- ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
- return -1;
- }
- mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
- ldap_mods = ldap_memcalloc(sizeof(LDAPMod *), mods_size);
- ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
- ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
- ldap_mods[0]->mod_type = ldap_strdup(newparam);
- ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
- ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
- while ((field = field->next)) {
- newparam = convert_attribute_name_to_ldap(table_config, field->name);
- mod_exists = 0;
- for (i = 0; i < mods_size - 1; i++) {
- if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
- /* We have the parameter allready, adding the value as a semicolon delimited value */
- ldap_mods[i]->mod_values[0] = ldap_memrealloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(field->value) + 2));
- strcat(ldap_mods[i]->mod_values[0], ";");
- strcat(ldap_mods[i]->mod_values[0], field->value);
- mod_exists = 1;
- break;
- }
- }
- /* create new mod */
- if (!mod_exists) {
- mods_size++;
- ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
- ldap_mods[mods_size - 1] = NULL;
-
- ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
- ldap_mods[mods_size - 2]->mod_type = ldap_memcalloc(sizeof(char), strlen(newparam) + 1);
- strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
- if (strlen(field->value) == 0) {
- ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
- } else {
- ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
- ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(sizeof(char *), 2);
- ldap_mods[mods_size - 2]->mod_values[0] = ldap_memcalloc(sizeof(char), strlen(field->value) + 1);
- strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
- }
- }
- }
- /* freeing ldap_mods further down */
- do {
- /* freeing ldap_result further down */
- result = ldap_search_ext_s(ldapConn, clean_basedn,
- LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
- &ldap_result_msg);
- if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
- ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
- tries++;
- if (tries < 3) {
- usleep(500000L * tries);
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- if (!ldap_reconnect())
- break;
- }
- }
- } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
- if (result != LDAP_SUCCESS) {
- ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
- ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
- ast_mutex_unlock(&ldap_lock);
- free(filter);
- free(clean_basedn);
- ldap_msgfree(ldap_result_msg);
- ldap_mods_free(ldap_mods, 0);
- return -1;
- }
- /* Ready to update */
- if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
- ast_debug(3, "LINE(%d) Modifying %s=%s hits: %d\n", __LINE__, attribute, lookup, num_entries);
- for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
- if (ldap_mods[i]->mod_op != LDAP_MOD_DELETE) {
- ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
- } else {
- ast_debug(3, "LINE(%d) deleting %s \n", __LINE__, ldap_mods[i]->mod_type);
- }
- }
- ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
- for (i = 0; ldap_entry; i++) {
- dn = ldap_get_dn(ldapConn, ldap_entry);
- if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
- ast_log(LOG_ERROR, "Couldn't modify '%s'='%s', dn:%s because %s\n",
- attribute, lookup, dn, ldap_err2string(error));
- }
- ldap_memfree(dn);
- ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
- }
- }
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- ast_free(clean_basedn);
- ldap_msgfree(ldap_result_msg);
- ldap_mods_free(ldap_mods, 0);
- return num_entries;
- }
- static int update2_ldap(const char *basedn, const char *table_name, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
- {
- int error = 0;
- LDAPMessage *ldap_entry = NULL;
- LDAPMod **ldap_mods;
- const char *newparam;
- const struct ast_variable *field;
- char *dn;
- int num_entries = 0;
- int i = 0;
- int mods_size = 0;
- int mod_exists = 0;
- struct ldap_table_config *table_config = NULL;
- char *clean_basedn = NULL;
- struct ast_str *filter = NULL;
- int tries = 0;
- int result = 0;
- LDAPMessage *ldap_result_msg = NULL;
- if (!table_name) {
- ast_log(LOG_ERROR, "No table_name specified.\n");
- return -1;
- }
- if (!(filter = ast_str_create(80))) {
- return -1;
- }
- ast_mutex_lock(&ldap_lock);
- /* We now have our complete statement; Lets connect to the server and execute it. */
- if (!ldap_reconnect()) {
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- return -1;
- }
- table_config = table_config_for_table_name(table_name);
- if (!table_config) {
- ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- return -1;
- }
- clean_basedn = cleaned_basedn(NULL, basedn);
- /* Create the filter with the table additional filter and the parameter/value pairs we were given */
- ast_str_append(&filter, 0, "(&");
- if (table_config && table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", table_config->additional_filter);
- }
- if (table_config != base_table_config && base_table_config
- && base_table_config->additional_filter) {
- ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
- }
- /* Get multiple lookup keyfields and values */
- for (field = lookup_fields; field; field = field->next) {
- append_var_and_value_to_filter(&filter, table_config, field->name, field->value);
- }
- ast_str_append(&filter, 0, ")");
- /* Create the modification array with the parameter/value pairs we were given,
- * if there are several parameters with the same name, we collect them into
- * one parameter/value pair and delimit them with a semicolon */
- field = update_fields;
- newparam = convert_attribute_name_to_ldap(table_config, field->name);
- if (!newparam) {
- ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
- ast_free(filter);
- ast_free(clean_basedn);
- return -1;
- }
- mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
- ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
- ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
- ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
- ldap_mods[0]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
- strcpy(ldap_mods[0]->mod_type, newparam);
- ldap_mods[0]->mod_values = ast_calloc(sizeof(char), 2);
- ldap_mods[0]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
- strcpy(ldap_mods[0]->mod_values[0], field->value);
- while ((field = field->next)) {
- newparam = convert_attribute_name_to_ldap(table_config, field->name);
- mod_exists = 0;
- for (i = 0; i < mods_size - 1; i++) {
- if (ldap_mods[i]&& !strcmp(ldap_mods[i]->mod_type, newparam)) {
- /* We have the parameter allready, adding the value as a semicolon delimited value */
- ldap_mods[i]->mod_values[0] = ast_realloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(field->value) + 2));
- strcat(ldap_mods[i]->mod_values[0], ";");
- strcat(ldap_mods[i]->mod_values[0], field->value);
- mod_exists = 1;
- break;
- }
- }
- /* create new mod */
- if (!mod_exists) {
- mods_size++;
- ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
- ldap_mods[mods_size - 1] = NULL;
- ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
- ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
- ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
- strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
- ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
- ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
- strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
- }
- }
- /* freeing ldap_mods further down */
- do {
- /* freeing ldap_result further down */
- result = ldap_search_ext_s(ldapConn, clean_basedn,
- LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
- &ldap_result_msg);
- if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
- ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
- tries++;
- if (tries < 3) {
- usleep(500000L * tries);
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- if (!ldap_reconnect()) {
- break;
- }
- }
- }
- } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
- if (result != LDAP_SUCCESS) {
- ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
- ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
- ast_mutex_unlock(&ldap_lock);
- ast_free(filter);
- ast_free(clean_basedn);
- ldap_msgfree(ldap_result_msg);
- ldap_mods_free(ldap_mods, 0);
- return -1;
- }
- /* Ready to update */
- if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
- for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
- ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
- }
- ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
- for (i = 0; ldap_entry; i++) {
- dn = ldap_get_dn(ldapConn, ldap_entry);
- if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
- ast_log(LOG_ERROR, "Couldn't modify dn:%s because %s", dn, ldap_err2string(error));
- }
- ldap_memfree(dn);
- ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
- }
- }
- ast_mutex_unlock(&ldap_lock);
- if (filter) {
- ast_free(filter);
- }
- if (clean_basedn) {
- ast_free(clean_basedn);
- }
- ldap_msgfree(ldap_result_msg);
- ldap_mods_free(ldap_mods, 0);
- return num_entries;
- }
- static struct ast_config_engine ldap_engine = {
- .name = "ldap",
- .load_func = config_ldap,
- .realtime_func = realtime_ldap,
- .realtime_multi_func = realtime_multi_ldap,
- .update_func = update_ldap,
- .update2_func = update2_ldap,
- };
- /*!
- * \brief Load the module
- *
- * Module loading including tests for configuration or dependencies.
- * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
- * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
- * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
- * configuration file or other non-critical problem return
- * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
- *
- * \todo Don't error or warn on a default install. If the config is
- * default we should not attempt to connect to a server. -lathama
- */
- static int load_module(void)
- {
- if (parse_config() < 0) {
- ast_log(LOG_ERROR, "Cannot load LDAP RealTime driver.\n");
- return 0;
- }
- ast_mutex_lock(&ldap_lock);
- if (!ldap_reconnect()) {
- ast_log(LOG_WARNING, "Couldn't establish connection to LDAP directory. Check debug.\n");
- }
- ast_config_engine_register(&ldap_engine);
- ast_verb(1, "LDAP RealTime driver loaded.\n");
- ast_cli_register_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
- ast_mutex_unlock(&ldap_lock);
- return 0;
- }
- /*! \brief Unload Module
- *
- */
- static int unload_module(void)
- {
- /* Aquire control before doing anything to the module itself. */
- ast_mutex_lock(&ldap_lock);
- table_configs_free();
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- ast_cli_unregister_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
- ast_config_engine_deregister(&ldap_engine);
- ast_verb(1, "LDAP RealTime driver unloaded.\n");
- /* Unlock so something else can destroy the lock. */
- ast_mutex_unlock(&ldap_lock);
- return 0;
- }
- /*! \breif Relod Module
- */
- static int reload(void)
- {
- /* Aquire control before doing anything to the module itself. */
- ast_mutex_lock(&ldap_lock);
- if (ldapConn) {
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- }
- if (parse_config() < 0) {
- ast_log(LOG_NOTICE, "Cannot reload LDAP RealTime driver.\n");
- ast_mutex_unlock(&ldap_lock);
- return 0;
- }
- if (!ldap_reconnect()) {
- ast_log(LOG_WARNING, "Couldn't establish connection to your directory server. Check debug.\n");
- }
- ast_verb(2, "LDAP RealTime driver reloaded.\n");
- /* Done reloading. Release lock so others can now use driver. */
- ast_mutex_unlock(&ldap_lock);
- return 0;
- }
- /*! \brief parse the configuration file
- */
- static int parse_config(void)
- {
- struct ast_config *config;
- struct ast_flags config_flags = {0};
- const char *s, *host;
- int port;
- char *category_name = NULL;
- /* Make sure that global variables are reset */
- url[0] = '\0';
- user[0] = '\0';
- pass[0] = '\0';
- base_distinguished_name[0] = '\0';
- version = 3;
- config = ast_config_load(RES_CONFIG_LDAP_CONF, config_flags);
- if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_ERROR, "Cannot load configuration file: %s\n", RES_CONFIG_LDAP_CONF);
- return -1;
- }
- if (!(s = ast_variable_retrieve(config, "_general", "user"))) {
- ast_log(LOG_NOTICE, "No directory user found, anonymous binding as default.\n");
- user[0] = '\0';
- } else {
- ast_copy_string(user, s, sizeof(user));
- }
- if (!ast_strlen_zero(user)) {
- if (!(s = ast_variable_retrieve(config, "_general", "pass"))) {
- ast_log(LOG_WARNING, "No directory password found, using 'asterisk' as default.\n");
- ast_copy_string(pass, "asterisk", sizeof(pass));
- } else {
- ast_copy_string(pass, s, sizeof(pass));
- }
- }
- /* URL is preferred, use host and port if not found */
- if ((s = ast_variable_retrieve(config, "_general", "url"))) {
- ast_copy_string(url, s, sizeof(url));
- } else if ((host = ast_variable_retrieve(config, "_general", "host"))) {
- if (!(s = ast_variable_retrieve(config, "_general", "port")) || sscanf(s, "%5d", &port) != 1 || port > 65535) {
- ast_log(LOG_NOTICE, "No directory port found, using 389 as default.\n");
- port = 389;
- }
- snprintf(url, sizeof(url), "ldap://%s:%d", host, port);
- } else {
- ast_log(LOG_ERROR, "No directory URL or host found.\n");
- ast_config_destroy(config);
- return -1;
- }
- if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) {
- ast_log(LOG_ERROR, "No LDAP base dn found, using '%s' as default.\n", RES_CONFIG_LDAP_DEFAULT_BASEDN);
- ast_copy_string(base_distinguished_name, RES_CONFIG_LDAP_DEFAULT_BASEDN, sizeof(base_distinguished_name));
- } else
- ast_copy_string(base_distinguished_name, s, sizeof(base_distinguished_name));
- if (!(s = ast_variable_retrieve(config, "_general", "version")) && !(s = ast_variable_retrieve(config, "_general", "protocol"))) {
- ast_log(LOG_NOTICE, "No explicit LDAP version found, using 3 as default.\n");
- } else if (sscanf(s, "%30d", &version) != 1 || version < 1 || version > 6) {
- ast_log(LOG_WARNING, "Invalid LDAP version '%s', using 3 as default.\n", s);
- version = 3;
- }
- table_configs_free();
- while ((category_name = ast_category_browse(config, category_name))) {
- int is_general = (strcasecmp(category_name, "_general") == 0);
- int is_config = (strcasecmp(category_name, "config") == 0); /*!< using the [config] context for Static RealTime */
- struct ast_variable *var = ast_variable_browse(config, category_name);
-
- if (var) {
- struct ldap_table_config *table_config =
- table_config_for_table_name(category_name);
- if (!table_config) {
- table_config = table_config_new(category_name);
- AST_LIST_INSERT_HEAD(&table_configs, table_config, entry);
- if (is_general)
- base_table_config = table_config;
- if (is_config)
- static_table_config = table_config;
- }
- for (; var; var = var->next) {
- if (!strcasecmp(var->name, "additionalFilter")) {
- table_config->additional_filter = ast_strdup(var->value);
- } else {
- ldap_table_config_add_attribute(table_config, var->name, var->value);
- }
- }
- }
- }
- ast_config_destroy(config);
- return 1;
- }
- /*! \note ldap_lock should have been locked before calling this function. */
- static int ldap_reconnect(void)
- {
- int bind_result = 0;
- struct berval cred;
- if (ldapConn) {
- ast_debug(2, "Everything seems fine.\n");
- return 1;
- }
- if (ast_strlen_zero(url)) {
- ast_log(LOG_ERROR, "Not enough parameters to connect to ldap directory\n");
- return 0;
- }
- if (LDAP_SUCCESS != ldap_initialize(&ldapConn, url)) {
- ast_log(LOG_ERROR, "Failed to init ldap connection to '%s'. Check debug for more info.\n", url);
- return 0;
- }
- if (LDAP_OPT_SUCCESS != ldap_set_option(ldapConn, LDAP_OPT_PROTOCOL_VERSION, &version)) {
- ast_log(LOG_WARNING, "Unable to set LDAP protocol version to %d, falling back to default.\n", version);
- }
- if (!ast_strlen_zero(user)) {
- ast_debug(2, "bind to '%s' as user '%s'\n", url, user);
- cred.bv_val = (char *) pass;
- cred.bv_len = strlen(pass);
- bind_result = ldap_sasl_bind_s(ldapConn, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
- } else {
- ast_debug(2, "bind %s anonymously\n", url);
- cred.bv_val = NULL;
- cred.bv_len = 0;
- bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
- }
- if (bind_result == LDAP_SUCCESS) {
- ast_debug(2, "Successfully connected to directory.\n");
- connect_time = time(NULL);
- return 1;
- } else {
- ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result));
- ldap_unbind_ext_s(ldapConn, NULL, NULL);
- ldapConn = NULL;
- return 0;
- }
- }
- /*! \brief Realtime Status
- *
- */
- static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- char status[256], credentials[100] = "";
- int ctimesec = time(NULL) - connect_time;
- switch (cmd) {
- case CLI_INIT:
- e->command = "realtime show ldap status";
- e->usage =
- "Usage: realtime show ldap status\n"
- " Shows connection information for the LDAP RealTime driver\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (!ldapConn)
- return CLI_FAILURE;
- if (!ast_strlen_zero(url))
- snprintf(status, sizeof(status), "Connected to '%s', baseDN %s", url, base_distinguished_name);
- if (!ast_strlen_zero(user))
- snprintf(credentials, sizeof(credentials), " with username %s", user);
- if (ctimesec > 31536000) {
- ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
- status, credentials, ctimesec / 31536000,
- (ctimesec % 31536000) / 86400, (ctimesec % 86400) / 3600,
- (ctimesec % 3600) / 60, ctimesec % 60);
- } else if (ctimesec > 86400) {
- ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n",
- status, credentials, ctimesec / 86400, (ctimesec % 86400) / 3600,
- (ctimesec % 3600) / 60, ctimesec % 60);
- } else if (ctimesec > 3600) {
- ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n",
- status, credentials, ctimesec / 3600, (ctimesec % 3600) / 60,
- ctimesec % 60);
- } else if (ctimesec > 60) {
- ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials,
- ctimesec / 60, ctimesec % 60);
- } else {
- ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
- }
- return CLI_SUCCESS;
- }
- /*! \brief Module Information
- *
- */
- AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "LDAP realtime interface",
- .support_level = AST_MODULE_SUPPORT_EXTENDED,
- .load = load_module,
- .unload = unload_module,
- .reload = reload,
- .load_pri = AST_MODPRI_REALTIME_DRIVER,
- );
|