res_config_ldap.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  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. /*! \file
  22. *
  23. * \brief LDAP plugin for portable configuration engine (ARA)
  24. *
  25. * \author Mark Spencer <markster@digium.com>
  26. * \author Manuel Guesdon
  27. * \author Carl-Einar Thorner <cthorner@voicerd.com>
  28. * \author Russell Bryant <russell@digium.com>
  29. *
  30. * OpenLDAP http://www.openldap.org
  31. */
  32. /*! \li \ref res_config_ldap.c uses the configuration file \ref res_ldap.conf
  33. * \addtogroup configuration_file Configuration Files
  34. */
  35. /*!
  36. * \page res_ldap.conf res_ldap.conf
  37. * \verbinclude res_ldap.conf.sample
  38. */
  39. /*** MODULEINFO
  40. <depend>ldap</depend>
  41. <support_level>extended</support_level>
  42. ***/
  43. #include "asterisk.h"
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <stdio.h>
  48. #include <ldap.h>
  49. #include "asterisk/channel.h"
  50. #include "asterisk/logger.h"
  51. #include "asterisk/config.h"
  52. #include "asterisk/module.h"
  53. #include "asterisk/lock.h"
  54. #include "asterisk/options.h"
  55. #include "asterisk/cli.h"
  56. #include "asterisk/utils.h"
  57. #include "asterisk/strings.h"
  58. #include "asterisk/pbx.h"
  59. #include "asterisk/linkedlists.h"
  60. #define RES_CONFIG_LDAP_CONF "res_ldap.conf"
  61. #define RES_CONFIG_LDAP_DEFAULT_BASEDN "asterisk"
  62. AST_MUTEX_DEFINE_STATIC(ldap_lock);
  63. static LDAP *ldapConn;
  64. static char url[512];
  65. static char user[512];
  66. static char pass[512];
  67. static char base_distinguished_name[512];
  68. static int version;
  69. static time_t connect_time;
  70. static int parse_config(void);
  71. static int ldap_reconnect(void);
  72. static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  73. struct category_and_metric {
  74. const char *name;
  75. int metric;
  76. const char *variable_name;
  77. const char *variable_value;
  78. int var_metric; /*!< For organizing variables (particularly includes and switch statements) within a context */
  79. };
  80. /*! \brief Table configuration
  81. */
  82. struct ldap_table_config {
  83. char *table_name; /*!< table name */
  84. char *additional_filter; /*!< additional filter */
  85. struct ast_variable *attributes; /*!< attribute names conversion */
  86. struct ast_variable *delimiters; /*!< the current delimiter is semicolon, so we are not using this variable */
  87. AST_LIST_ENTRY(ldap_table_config) entry;
  88. /*! \todo: Make proxies work */
  89. };
  90. /*! \brief Should be locked before using it
  91. */
  92. static AST_LIST_HEAD_NOLOCK_STATIC(table_configs, ldap_table_config);
  93. static struct ldap_table_config *base_table_config;
  94. static struct ldap_table_config *static_table_config;
  95. static struct ast_cli_entry ldap_cli[] = {
  96. AST_CLI_DEFINE(realtime_ldap_status, "Shows connection information for the LDAP RealTime driver"),
  97. };
  98. /*! \brief Create a new table_config
  99. */
  100. static struct ldap_table_config *table_config_new(const char *table_name)
  101. {
  102. struct ldap_table_config *p;
  103. if (!(p = ast_calloc(1, sizeof(*p))))
  104. return NULL;
  105. if (table_name) {
  106. if (!(p->table_name = ast_strdup(table_name))) {
  107. ast_free(p);
  108. return NULL;
  109. }
  110. }
  111. return p;
  112. }
  113. /*! \brief Find a table_config
  114. *
  115. * Should be locked before using it
  116. *
  117. * \note This function assumes ldap_lock to be locked.
  118. */
  119. static struct ldap_table_config *table_config_for_table_name(const char *table_name)
  120. {
  121. struct ldap_table_config *c = NULL;
  122. AST_LIST_TRAVERSE(&table_configs, c, entry) {
  123. if (!strcmp(c->table_name, table_name))
  124. break;
  125. }
  126. return c;
  127. }
  128. /*! \brief Find variable by name
  129. */
  130. static struct ast_variable *variable_named(struct ast_variable *var, const char *name)
  131. {
  132. for (; var; var = var->next) {
  133. if (!strcasecmp(name, var->name))
  134. break;
  135. }
  136. return var;
  137. }
  138. /*!
  139. * \brief Count semicolons in string
  140. * \param somestr - pointer to a string
  141. *
  142. * \return number of occurances of the delimiter(semicolon)
  143. */
  144. static int semicolon_count_str(const char *somestr)
  145. {
  146. int count = 0;
  147. for (; *somestr; somestr++) {
  148. if (*somestr == ';')
  149. count++;
  150. }
  151. return count;
  152. }
  153. /*!
  154. * \brief Count semicolons in variables
  155. *
  156. * takes a linked list of \a ast_variable variables, finds the one with the name variable_value
  157. * and returns the number of semicolons in the value for that \a ast_variable
  158. */
  159. static int semicolon_count_var(struct ast_variable *var)
  160. {
  161. struct ast_variable *var_value = variable_named(var, "variable_value");
  162. if (!var_value) {
  163. return 0;
  164. }
  165. ast_debug(2, "semicolon_count_var: %s\n", var_value->value);
  166. return semicolon_count_str(var_value->value);
  167. }
  168. /*! \brief add attribute to table config
  169. *
  170. * Should be locked before using it
  171. */
  172. static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
  173. const char *attribute_name, const char *attribute_value)
  174. {
  175. struct ast_variable *var;
  176. if (ast_strlen_zero(attribute_name) || ast_strlen_zero(attribute_value)) {
  177. return;
  178. }
  179. if (!(var = ast_variable_new(attribute_name, attribute_value, table_config->table_name))) {
  180. return;
  181. }
  182. if (table_config->attributes) {
  183. var->next = table_config->attributes;
  184. }
  185. table_config->attributes = var;
  186. }
  187. /*! \brief Free table_config
  188. *
  189. * \note assumes ldap_lock to be locked
  190. */
  191. static void table_configs_free(void)
  192. {
  193. struct ldap_table_config *c;
  194. while ((c = AST_LIST_REMOVE_HEAD(&table_configs, entry))) {
  195. if (c->table_name) {
  196. ast_free(c->table_name);
  197. }
  198. if (c->additional_filter) {
  199. ast_free(c->additional_filter);
  200. }
  201. if (c->attributes) {
  202. ast_variables_destroy(c->attributes);
  203. }
  204. ast_free(c);
  205. }
  206. base_table_config = NULL;
  207. static_table_config = NULL;
  208. }
  209. /*! \brief Convert variable name to ldap attribute name
  210. *
  211. * \note Should be locked before using it
  212. */
  213. static const char *convert_attribute_name_to_ldap(struct ldap_table_config *table_config,
  214. const char *attribute_name)
  215. {
  216. int i = 0;
  217. struct ldap_table_config *configs[] = { table_config, base_table_config };
  218. for (i = 0; i < ARRAY_LEN(configs); i++) {
  219. struct ast_variable *attribute;
  220. if (!configs[i]) {
  221. continue;
  222. }
  223. attribute = configs[i]->attributes;
  224. for (; attribute; attribute = attribute->next) {
  225. if (!strcasecmp(attribute_name, attribute->name)) {
  226. return attribute->value;
  227. }
  228. }
  229. }
  230. return attribute_name;
  231. }
  232. /*! \brief Convert ldap attribute name to variable name
  233. *
  234. * \note Should be locked before using it
  235. */
  236. static const char *convert_attribute_name_from_ldap(struct ldap_table_config *table_config,
  237. const char *attribute_name)
  238. {
  239. int i = 0;
  240. struct ldap_table_config *configs[] = { table_config, base_table_config };
  241. for (i = 0; i < ARRAY_LEN(configs); i++) {
  242. struct ast_variable *attribute;
  243. if (!configs[i]) {
  244. continue;
  245. }
  246. attribute = configs[i]->attributes;
  247. for (; attribute; attribute = attribute->next) {
  248. if (strcasecmp(attribute_name, attribute->value) == 0) {
  249. return attribute->name;
  250. }
  251. }
  252. }
  253. return attribute_name;
  254. }
  255. /*! \brief Get variables from ldap entry attributes
  256. * \note Should be locked before using it
  257. * \return a linked list of ast_variable variables.
  258. */
  259. static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config *table_config,
  260. LDAPMessage *ldap_entry)
  261. {
  262. BerElement *ber = NULL;
  263. struct ast_variable *var = NULL;
  264. struct ast_variable *prev = NULL;
  265. #if 0
  266. int is_delimited = 0;
  267. int i = 0;
  268. #endif
  269. char *ldap_attribute_name;
  270. struct berval *value;
  271. int pos = 0;
  272. ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
  273. while (ldap_attribute_name) {
  274. struct berval **values = NULL;
  275. const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
  276. int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
  277. values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name); /* these are freed at the end */
  278. if (values) {
  279. struct berval **v;
  280. char *valptr;
  281. for (v = values; *v; v++) {
  282. value = *v;
  283. valptr = value->bv_val;
  284. ast_debug(2, "attribute_name: %s LDAP value: %s\n", attribute_name, valptr);
  285. if (is_realmed_password_attribute) {
  286. if (!strncasecmp(valptr, "{md5}", 5)) {
  287. valptr += 5;
  288. }
  289. ast_debug(2, "md5: %s\n", valptr);
  290. }
  291. if (valptr) {
  292. #if 0
  293. /* ok, so looping through all delimited values except the last one (not, last character is not delimited...) */
  294. if (is_delimited) {
  295. i = 0;
  296. pos = 0;
  297. while (!ast_strlen_zero(valptr + i)) {
  298. if (valptr[i] == ';') {
  299. valptr[i] = '\0';
  300. if (prev) {
  301. prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
  302. if (prev->next) {
  303. prev = prev->next;
  304. }
  305. } else {
  306. prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
  307. }
  308. pos = i + 1;
  309. }
  310. i++;
  311. }
  312. }
  313. #endif
  314. /* for the last delimited value or if the value is not delimited: */
  315. if (prev) {
  316. prev->next = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
  317. if (prev->next) {
  318. prev = prev->next;
  319. }
  320. } else {
  321. prev = var = ast_variable_new(attribute_name, &valptr[pos], table_config->table_name);
  322. }
  323. }
  324. }
  325. ldap_value_free_len(values);
  326. }
  327. ldap_memfree(ldap_attribute_name);
  328. ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
  329. }
  330. ber_free(ber, 0);
  331. return var;
  332. }
  333. /*! \brief Get variables from ldap entry attributes - Should be locked before using it
  334. *
  335. * The results are freed outside this function so is the \a vars array.
  336. *
  337. * \return \a vars - an array of ast_variable variables terminated with a null.
  338. */
  339. static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_config *table_config,
  340. LDAPMessage *ldap_result_msg, unsigned int *entries_count_ptr)
  341. {
  342. struct ast_variable **vars;
  343. int i = 0;
  344. int tot_count = 0;
  345. int entry_index = 0;
  346. LDAPMessage *ldap_entry = NULL;
  347. BerElement *ber = NULL;
  348. struct ast_variable *var = NULL;
  349. struct ast_variable *prev = NULL;
  350. int is_delimited = 0;
  351. char *delim_value = NULL;
  352. int delim_tot_count = 0;
  353. int delim_count = 0;
  354. /*! \brief First find the total count
  355. */
  356. ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
  357. for (tot_count = 0; ldap_entry; tot_count++) {
  358. struct ast_variable *tmp = realtime_ldap_entry_to_var(table_config, ldap_entry);
  359. tot_count += semicolon_count_var(tmp);
  360. ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
  361. ast_variables_destroy(tmp);
  362. }
  363. if (entries_count_ptr) {
  364. *entries_count_ptr = tot_count;
  365. }
  366. /*! \note Now that we have the total count we allocate space and create the variables
  367. * Remember that each element in vars is a linked list that points to realtime variable.
  368. * If the we are dealing with a static realtime variable we create a new element in the \a vars array for each delimited
  369. * 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.
  370. * This memory must be freed outside of this function.
  371. */
  372. vars = ast_calloc(tot_count + 1, sizeof(struct ast_variable *));
  373. ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
  374. i = 0;
  375. /*! \brief For each static realtime variable we may create several entries in the \a vars array if it's delimited
  376. */
  377. for (entry_index = 0; ldap_entry; ) {
  378. int pos = 0;
  379. delim_value = NULL;
  380. delim_tot_count = 0;
  381. delim_count = 0;
  382. do { /* while delim_count */
  383. /* Starting new static var */
  384. char *ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
  385. struct berval *value;
  386. while (ldap_attribute_name) {
  387. const char *attribute_name = convert_attribute_name_from_ldap(table_config, ldap_attribute_name);
  388. int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
  389. struct berval **values = NULL;
  390. values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);
  391. if (values) {
  392. struct berval **v;
  393. char *valptr;
  394. for (v = values; *v; v++) {
  395. value = *v;
  396. valptr = value->bv_val;
  397. if (is_realmed_password_attribute) {
  398. if (strncasecmp(valptr, "{md5}", 5) == 0) {
  399. valptr += 5;
  400. }
  401. ast_debug(2, "md5: %s\n", valptr);
  402. }
  403. if (valptr) {
  404. if (delim_value == NULL && !is_realmed_password_attribute
  405. && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0)) {
  406. delim_value = ast_strdup(valptr);
  407. if ((delim_tot_count = semicolon_count_str(delim_value)) > 0) {
  408. ast_debug(4, "is delimited %d times: %s\n", delim_tot_count, delim_value);
  409. is_delimited = 1;
  410. }
  411. }
  412. if (is_delimited != 0 && !is_realmed_password_attribute
  413. && (static_table_config != table_config || strcmp(attribute_name, "variable_value") == 0) ) {
  414. /* for non-Static RealTime, first */
  415. for (i = pos; !ast_strlen_zero(valptr + i); i++) {
  416. ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
  417. if (delim_value[i] == ';') {
  418. delim_value[i] = '\0';
  419. ast_debug(2, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
  420. if (prev) {
  421. prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
  422. if (prev->next) {
  423. prev = prev->next;
  424. }
  425. } else {
  426. prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
  427. }
  428. pos = i + 1;
  429. if (static_table_config == table_config) {
  430. break;
  431. }
  432. }
  433. }
  434. if (ast_strlen_zero(valptr + i)) {
  435. ast_debug(4, "DELIM pos: %d i: %d delim_count: %d\n", pos, i, delim_count);
  436. /* Last delimited value */
  437. ast_debug(4, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
  438. if (prev) {
  439. prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
  440. if (prev->next) {
  441. prev = prev->next;
  442. }
  443. } else {
  444. prev = var = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
  445. }
  446. /* Remembering to free memory */
  447. is_delimited = 0;
  448. pos = 0;
  449. }
  450. ast_free(delim_value);
  451. delim_value = NULL;
  452. ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
  453. } else {
  454. /* not delimited */
  455. if (delim_value) {
  456. ast_free(delim_value);
  457. delim_value = NULL;
  458. }
  459. ast_debug(2, "attribute_name: %s value: %s\n", attribute_name, valptr);
  460. if (prev) {
  461. prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
  462. if (prev->next) {
  463. prev = prev->next;
  464. }
  465. } else {
  466. prev = var = ast_variable_new(attribute_name, valptr, table_config->table_name);
  467. }
  468. }
  469. }
  470. } /*!< for (v = values; *v; v++) */
  471. ldap_value_free_len(values);
  472. }/*!< if (values) */
  473. ldap_memfree(ldap_attribute_name);
  474. ldap_attribute_name = ldap_next_attribute(ldapConn, ldap_entry, ber);
  475. } /*!< while (ldap_attribute_name) */
  476. ber_free(ber, 0);
  477. if (static_table_config == table_config) {
  478. if (DEBUG_ATLEAST(3)) {
  479. const struct ast_variable *tmpdebug = variable_named(var, "variable_name");
  480. const struct ast_variable *tmpdebug2 = variable_named(var, "variable_value");
  481. if (tmpdebug && tmpdebug2) {
  482. ast_log(LOG_DEBUG, "Added to vars - %s = %s\n", tmpdebug->value, tmpdebug2->value);
  483. }
  484. }
  485. vars[entry_index++] = var;
  486. prev = NULL;
  487. }
  488. delim_count++;
  489. } while (delim_count <= delim_tot_count && static_table_config == table_config);
  490. if (static_table_config != table_config) {
  491. ast_debug(3, "Added to vars - non static\n");
  492. vars[entry_index++] = var;
  493. prev = NULL;
  494. }
  495. ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
  496. } /*!< end for loop over ldap_entry */
  497. return vars;
  498. }
  499. /*! \brief Check if we have a connection error
  500. */
  501. static int is_ldap_connect_error(int err)
  502. {
  503. return (err == LDAP_SERVER_DOWN || err == LDAP_TIMEOUT || err == LDAP_CONNECT_ERROR);
  504. }
  505. /*! \brief Get LDAP entry by dn and return attributes as variables
  506. *
  507. * Should be locked before using it
  508. *
  509. * This is used for setting the default values of an object
  510. * i.e., with accountBaseDN
  511. */
  512. static struct ast_variable *ldap_loadentry(struct ldap_table_config *table_config,
  513. const char *dn)
  514. {
  515. if (!table_config) {
  516. ast_log(LOG_ERROR, "No table config\n");
  517. return NULL;
  518. } else {
  519. struct ast_variable **vars = NULL;
  520. struct ast_variable *var = NULL;
  521. int result = -1;
  522. LDAPMessage *ldap_result_msg = NULL;
  523. int tries = 0;
  524. ast_debug(2, "ldap_loadentry dn=%s\n", dn);
  525. do {
  526. result = ldap_search_ext_s(ldapConn, dn, LDAP_SCOPE_BASE,
  527. "(objectclass=*)", NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &ldap_result_msg);
  528. if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
  529. ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
  530. tries++;
  531. if (tries < 3) {
  532. usleep(500000L * tries);
  533. if (ldapConn) {
  534. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  535. ldapConn = NULL;
  536. }
  537. if (!ldap_reconnect()) {
  538. break;
  539. }
  540. }
  541. }
  542. } while (result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(result));
  543. if (result != LDAP_SUCCESS) {
  544. ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
  545. ast_debug(2, "dn=%s\n", dn);
  546. ast_mutex_unlock(&ldap_lock);
  547. return NULL;
  548. } else {
  549. int num_entry = 0;
  550. unsigned int *entries_count_ptr = NULL; /*!< not using this */
  551. if ((num_entry = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
  552. ast_debug(3, "num_entry: %d\n", num_entry);
  553. vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
  554. if (num_entry > 1) {
  555. ast_log(LOG_NOTICE, "More than one entry for dn=%s. Take only 1st one\n", dn);
  556. }
  557. } else {
  558. ast_debug(2, "Could not find any entry dn=%s.\n", dn);
  559. }
  560. }
  561. ldap_msgfree(ldap_result_msg);
  562. /* Chopping \a vars down to one variable */
  563. if (vars != NULL) {
  564. struct ast_variable **p = vars;
  565. /* Only take the first one. */
  566. var = *vars;
  567. /* Destroy the rest. */
  568. while (*++p) {
  569. ast_variables_destroy(*p);
  570. }
  571. ast_free(vars);
  572. }
  573. return var;
  574. }
  575. }
  576. /*! \note caller should free returned pointer
  577. */
  578. static char *substituted(struct ast_channel *channel, const char *string)
  579. {
  580. #define MAXRESULT 2048
  581. char *ret_string = NULL;
  582. if (!ast_strlen_zero(string)) {
  583. ret_string = ast_calloc(1, MAXRESULT);
  584. pbx_substitute_variables_helper(channel, string, ret_string, MAXRESULT - 1);
  585. }
  586. ast_debug(2, "substituted: string: '%s' => '%s' \n", string, ret_string);
  587. return ret_string;
  588. }
  589. /*! \note caller should free returned pointer
  590. */
  591. static char *cleaned_basedn(struct ast_channel *channel, const char *basedn)
  592. {
  593. char *cbasedn = NULL;
  594. if (basedn) {
  595. char *p = NULL;
  596. cbasedn = substituted(channel, basedn);
  597. if (*cbasedn == '"') {
  598. cbasedn++;
  599. if (!ast_strlen_zero(cbasedn)) {
  600. int len = strlen(cbasedn);
  601. if (cbasedn[len - 1] == '"')
  602. cbasedn[len - 1] = '\0';
  603. }
  604. }
  605. p = cbasedn;
  606. while (*p) {
  607. if (*p == '|')
  608. *p = ',';
  609. p++;
  610. }
  611. }
  612. ast_debug(2, "basedn: '%s' => '%s' \n", basedn, cbasedn);
  613. return cbasedn;
  614. }
  615. /*! \brief Replace \<search\> by \<by\> in string.
  616. * \note No check is done on string allocated size !
  617. */
  618. static int replace_string_in_string(char *string, const char *search, const char *by)
  619. {
  620. int search_len = strlen(search);
  621. int by_len = strlen(by);
  622. int replaced = 0;
  623. char *p = strstr(string, search);
  624. if (p) {
  625. replaced = 1;
  626. while (p) {
  627. if (by_len == search_len) {
  628. memcpy(p, by, by_len);
  629. } else {
  630. memmove(p + by_len, p + search_len, strlen(p + search_len) + 1);
  631. memcpy(p, by, by_len);
  632. }
  633. p = strstr(p + by_len, search);
  634. }
  635. }
  636. return replaced;
  637. }
  638. /*! \brief Append a name=value filter string. The filter string can grow.
  639. */
  640. static void append_var_and_value_to_filter(struct ast_str **filter,
  641. struct ldap_table_config *table_config,
  642. const char *name, const char *value)
  643. {
  644. char *new_name = NULL;
  645. char *new_value = NULL;
  646. char *like_pos = strstr(name, " LIKE");
  647. ast_debug(2, "name='%s' value='%s'\n", name, value);
  648. if (like_pos) {
  649. int len = like_pos - name;
  650. name = new_name = ast_strdupa(name);
  651. new_name[len] = '\0';
  652. value = new_value = ast_strdupa(value);
  653. replace_string_in_string(new_value, "\\_", "_");
  654. replace_string_in_string(new_value, "%", "*");
  655. }
  656. name = convert_attribute_name_to_ldap(table_config, name);
  657. ast_str_append(filter, 0, "(%s=%s)", name, value);
  658. }
  659. /*!
  660. * \internal
  661. * \brief Create an LDAP filter using search fields
  662. *
  663. * \param config the \c ldap_table_config for this search
  664. * \param fields the \c ast_variable criteria to include
  665. *
  666. * \returns an \c ast_str pointer on success, NULL otherwise.
  667. */
  668. static struct ast_str *create_lookup_filter(struct ldap_table_config *config, const struct ast_variable *fields)
  669. {
  670. struct ast_str *filter;
  671. const struct ast_variable *field;
  672. filter = ast_str_create(80);
  673. if (!filter) {
  674. return NULL;
  675. }
  676. /*
  677. * Create the filter with the table additional filter and the
  678. * parameter/value pairs we were given
  679. */
  680. ast_str_append(&filter, 0, "(&");
  681. if (config && config->additional_filter) {
  682. ast_str_append(&filter, 0, "%s", config->additional_filter);
  683. }
  684. if (config != base_table_config
  685. && base_table_config
  686. && base_table_config->additional_filter) {
  687. ast_str_append(&filter, 0, "%s", base_table_config->additional_filter);
  688. }
  689. /* Append the lookup fields */
  690. for (field = fields; field; field = field->next) {
  691. append_var_and_value_to_filter(&filter, config, field->name, field->value);
  692. }
  693. ast_str_append(&filter, 0, ")");
  694. return filter;
  695. }
  696. /*! \brief LDAP base function
  697. * \return a null terminated array of ast_variable (one per entry) or NULL if no entry is found or if an error occured
  698. * caller should free the returned array and ast_variables
  699. * \param entries_count_ptr is a pointer to found entries count (can be NULL)
  700. * \param basedn is the base DN
  701. * \param table_name is the table_name (used dor attribute convertion and additional filter)
  702. * \param fields contains list of pairs name/value
  703. */
  704. static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_ptr,
  705. const char *basedn, const char *table_name, const struct ast_variable *fields)
  706. {
  707. struct ast_variable **vars = NULL;
  708. const struct ast_variable *field = fields;
  709. struct ldap_table_config *table_config = NULL;
  710. char *clean_basedn = cleaned_basedn(NULL, basedn);
  711. struct ast_str *filter = NULL;
  712. int tries = 0;
  713. int result = 0;
  714. LDAPMessage *ldap_result_msg = NULL;
  715. if (!table_name) {
  716. ast_log(LOG_ERROR, "No table_name specified.\n");
  717. ast_free(clean_basedn);
  718. return NULL;
  719. }
  720. if (!field) {
  721. ast_log(LOG_ERROR, "Realtime retrieval requires at least 1 parameter"
  722. " and 1 value to search on.\n");
  723. ast_free(clean_basedn);
  724. return NULL;
  725. }
  726. ast_mutex_lock(&ldap_lock);
  727. /* We now have our complete statement; Lets connect to the server and execute it. */
  728. if (!ldap_reconnect()) {
  729. ast_mutex_unlock(&ldap_lock);
  730. ast_free(clean_basedn);
  731. return NULL;
  732. }
  733. table_config = table_config_for_table_name(table_name);
  734. if (!table_config) {
  735. ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
  736. ast_mutex_unlock(&ldap_lock);
  737. ast_free(clean_basedn);
  738. return NULL;
  739. }
  740. filter = create_lookup_filter(table_config, fields);
  741. if (!filter) {
  742. ast_mutex_unlock(&ldap_lock);
  743. ast_free(clean_basedn);
  744. return NULL;
  745. }
  746. do {
  747. /* freeing ldap_result further down */
  748. result = ldap_search_ext_s(ldapConn, clean_basedn,
  749. LDAP_SCOPE_SUBTREE, ast_str_buffer(filter), NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
  750. &ldap_result_msg);
  751. if (result != LDAP_SUCCESS && is_ldap_connect_error(result)) {
  752. ast_debug(1, "Failed to query directory. Try %d/10\n", tries + 1);
  753. if (++tries < 10) {
  754. usleep(1);
  755. if (ldapConn) {
  756. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  757. ldapConn = NULL;
  758. }
  759. if (!ldap_reconnect()) {
  760. break;
  761. }
  762. }
  763. }
  764. } while (result != LDAP_SUCCESS && tries < 10 && is_ldap_connect_error(result));
  765. if (result != LDAP_SUCCESS) {
  766. ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(result));
  767. ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
  768. } else {
  769. /* this is where we create the variables from the search result
  770. * freeing this \a vars outside this function */
  771. if (ldap_count_entries(ldapConn, ldap_result_msg) > 0) {
  772. /* is this a static var or some other? they are handled different for delimited values */
  773. vars = realtime_ldap_result_to_vars(table_config, ldap_result_msg, entries_count_ptr);
  774. } else {
  775. ast_debug(1, "Could not find any entry matching %s in base dn %s.\n", ast_str_buffer(filter), clean_basedn);
  776. }
  777. ldap_msgfree(ldap_result_msg);
  778. /*! \todo get the default variables from the accountBaseDN, not implemented with delimited values
  779. */
  780. if (vars) {
  781. struct ast_variable **p = vars;
  782. while (*p) {
  783. struct ast_variable *append_var = NULL;
  784. struct ast_variable *tmp = *p;
  785. while (tmp) {
  786. if (strcasecmp(tmp->name, "accountBaseDN") == 0) {
  787. /* Get the variable to compare with for the defaults */
  788. struct ast_variable *base_var = ldap_loadentry(table_config, tmp->value);
  789. while (base_var) {
  790. struct ast_variable *next = base_var->next;
  791. struct ast_variable *test_var = *p;
  792. int base_var_found = 0;
  793. /* run throught the default values and fill it inn if it is missing */
  794. while (test_var) {
  795. if (strcasecmp(test_var->name, base_var->name) == 0) {
  796. base_var_found = 1;
  797. break;
  798. } else {
  799. test_var = test_var->next;
  800. }
  801. }
  802. if (base_var_found) {
  803. base_var->next = NULL;
  804. ast_variables_destroy(base_var);
  805. base_var = next;
  806. } else {
  807. /*!
  808. * \todo XXX The interactions with base_var and append_var may
  809. * cause a memory leak of base_var nodes. Also the append_var
  810. * list and base_var list may get cross linked.
  811. */
  812. if (append_var) {
  813. base_var->next = append_var;
  814. } else {
  815. base_var->next = NULL;
  816. }
  817. append_var = base_var;
  818. base_var = next;
  819. }
  820. }
  821. }
  822. if (!tmp->next && append_var) {
  823. tmp->next = append_var;
  824. tmp = NULL;
  825. } else {
  826. tmp = tmp->next;
  827. }
  828. }
  829. p++;
  830. }
  831. }
  832. }
  833. ast_free(filter);
  834. ast_free(clean_basedn);
  835. ast_mutex_unlock(&ldap_lock);
  836. return vars;
  837. }
  838. static struct ast_variable *realtime_arguments_to_fields(va_list ap)
  839. {
  840. struct ast_variable *fields = NULL;
  841. const char *newparam, *newval;
  842. while ((newparam = va_arg(ap, const char *))) {
  843. struct ast_variable *field;
  844. newval = va_arg(ap, const char *);
  845. if (!(field = ast_variable_new(newparam, newval, ""))) {
  846. ast_variables_destroy(fields);
  847. return NULL;
  848. }
  849. field->next = fields;
  850. fields = field;
  851. }
  852. return fields;
  853. }
  854. /*! \brief same as realtime_ldap_base_ap but take variable arguments count list
  855. */
  856. static struct ast_variable **realtime_ldap_base(unsigned int *entries_count_ptr,
  857. const char *basedn, const char *table_name, ...)
  858. {
  859. RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
  860. struct ast_variable **vars = NULL;
  861. va_list ap;
  862. va_start(ap, table_name);
  863. fields = realtime_arguments_to_fields(ap);
  864. va_end(ap);
  865. vars = realtime_ldap_base_ap(entries_count_ptr, basedn, table_name, fields);
  866. return vars;
  867. }
  868. /*! \brief See Asterisk doc
  869. *
  870. * For Realtime Dynamic(i.e., switch, queues, and directory)
  871. */
  872. static struct ast_variable *realtime_ldap(const char *basedn,
  873. const char *table_name, const struct ast_variable *fields)
  874. {
  875. struct ast_variable **vars = realtime_ldap_base_ap(NULL, basedn, table_name, fields);
  876. struct ast_variable *var = NULL;
  877. if (vars) {
  878. struct ast_variable *last_var = NULL;
  879. struct ast_variable **p = vars;
  880. /* Chain the vars array of lists into one list to return. */
  881. while (*p) {
  882. if (last_var) {
  883. while (last_var->next) {
  884. last_var = last_var->next;
  885. }
  886. last_var->next = *p;
  887. } else {
  888. var = *p;
  889. last_var = var;
  890. }
  891. p++;
  892. }
  893. ast_free(vars);
  894. }
  895. return var;
  896. }
  897. /*! \brief See Asterisk doc
  898. *
  899. * this function will be called for the switch statement if no match is found with the realtime_ldap function(i.e. it is a failover);
  900. * however, the ast_load_realtime wil match on wildcharacters also depending on what the mode is set to
  901. * this is an area of asterisk that could do with a lot of modification
  902. * I think this function returns Realtime dynamic objects
  903. */
  904. static struct ast_config *realtime_multi_ldap(const char *basedn,
  905. const char *table_name, const struct ast_variable *fields)
  906. {
  907. char *op;
  908. const char *initfield = NULL;
  909. struct ast_variable **vars =
  910. realtime_ldap_base_ap(NULL, basedn, table_name, fields);
  911. struct ast_config *cfg = NULL;
  912. if (!fields) {
  913. ast_log(LOG_WARNING, "realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
  914. return NULL;
  915. }
  916. initfield = ast_strdupa(fields->name);
  917. if ((op = strchr(initfield, ' '))) {
  918. *op = '\0';
  919. }
  920. if (vars) {
  921. cfg = ast_config_new();
  922. if (!cfg) {
  923. ast_log(LOG_ERROR, "Unable to create a config!\n");
  924. } else {
  925. struct ast_variable **p = vars;
  926. while (*p) {
  927. struct ast_category *cat = ast_category_new_anonymous();
  928. if (!cat) {
  929. break;
  930. } else {
  931. struct ast_variable *var = *p;
  932. while (var) {
  933. struct ast_variable *next = var->next;
  934. if (initfield && !strcmp(initfield, var->name)) {
  935. ast_category_rename(cat, var->value);
  936. }
  937. var->next = NULL;
  938. ast_variable_append(cat, var);
  939. var = next;
  940. }
  941. }
  942. ast_category_append(cfg, cat);
  943. p++;
  944. }
  945. }
  946. ast_free(vars);
  947. }
  948. return cfg;
  949. }
  950. /*! \brief Sorting alogrithm for qsort to find the order of the variables \a a and \a b
  951. * \param a pointer to category_and_metric struct
  952. * \param b pointer to category_and_metric struct
  953. *
  954. * \retval -1 for if b is greater
  955. * \retval 0 zero for equal
  956. * \retval 1 if a is greater
  957. */
  958. static int compare_categories(const void *a, const void *b)
  959. {
  960. const struct category_and_metric *as = a;
  961. const struct category_and_metric *bs = b;
  962. if (as->metric < bs->metric) {
  963. return -1;
  964. } else if (as->metric > bs->metric) {
  965. return 1;
  966. } else if (as->metric == bs->metric && strcmp(as->name, bs->name) != 0) {
  967. return strcmp(as->name, bs->name);
  968. }
  969. /* if the metric and the category name is the same, we check the variable metric */
  970. if (as->var_metric < bs->var_metric) {
  971. return -1;
  972. } else if (as->var_metric > bs->var_metric) {
  973. return 1;
  974. }
  975. return 0;
  976. }
  977. /*! \brief See Asterisk Realtime Documentation
  978. *
  979. * This is for Static Realtime
  980. *
  981. * load the configuration stuff for the .conf files
  982. * called on a reload
  983. */
  984. static struct ast_config *config_ldap(const char *basedn, const char *table_name,
  985. const char *file, struct ast_config *cfg, struct ast_flags config_flags, const char *sugg_incl, const char *who_asked)
  986. {
  987. unsigned int vars_count = 0;
  988. struct ast_variable **vars;
  989. int i = 0;
  990. struct ast_variable *new_v = NULL;
  991. struct ast_category *cur_cat = NULL;
  992. const char *last_category = NULL;
  993. int last_category_metric = 0;
  994. struct category_and_metric *categories;
  995. struct ast_variable **p;
  996. if (ast_strlen_zero(file) || !strcasecmp(file, RES_CONFIG_LDAP_CONF)) {
  997. ast_log(LOG_ERROR, "Missing configuration file: %s. Can't configure myself.\n", RES_CONFIG_LDAP_CONF);
  998. return NULL;
  999. }
  1000. vars = realtime_ldap_base(&vars_count, basedn, table_name, "filename", file, "commented", "FALSE", NULL);
  1001. if (!vars) {
  1002. ast_log(LOG_WARNING, "Could not find config '%s' in directory.\n", file);
  1003. return NULL;
  1004. }
  1005. /*! \note Since the items come back in random order, they need to be sorted
  1006. * first, and since the data could easily exceed stack size, this is
  1007. * allocated from the heap.
  1008. */
  1009. if (!(categories = ast_calloc(vars_count, sizeof(*categories)))) {
  1010. return NULL;
  1011. }
  1012. for (vars_count = 0, p = vars; *p; p++) {
  1013. struct ast_variable *category = variable_named(*p, "category");
  1014. struct ast_variable *cat_metric = variable_named(*p, "cat_metric");
  1015. struct ast_variable *var_name = variable_named(*p, "variable_name");
  1016. struct ast_variable *var_val = variable_named(*p, "variable_value");
  1017. struct ast_variable *var_metric = variable_named(*p, "var_metric");
  1018. struct ast_variable *dn = variable_named(*p, "dn");
  1019. if (!category) {
  1020. ast_log(LOG_ERROR, "No category name in entry '%s' for file '%s'.\n",
  1021. (dn ? dn->value : "?"), file);
  1022. } else if (!cat_metric) {
  1023. ast_log(LOG_ERROR, "No category metric in entry '%s'(category: %s) for file '%s'.\n",
  1024. (dn ? dn->value : "?"), category->value, file);
  1025. } else if (!var_metric) {
  1026. ast_log(LOG_ERROR, "No variable metric in entry '%s'(category: %s) for file '%s'.\n",
  1027. (dn ? dn->value : "?"), category->value, file);
  1028. } else if (!var_name) {
  1029. ast_log(LOG_ERROR, "No variable name in entry '%s' (category: %s metric: %s) for file '%s'.\n",
  1030. (dn ? dn->value : "?"), category->value,
  1031. cat_metric->value, file);
  1032. } else if (!var_val) {
  1033. ast_log(LOG_ERROR, "No variable value in entry '%s' (category: %s metric: %s variable: %s) for file '%s'.\n",
  1034. (dn ? dn->value : "?"), category->value,
  1035. cat_metric->value, var_name->value, file);
  1036. } else {
  1037. categories[vars_count].name = category->value;
  1038. categories[vars_count].metric = atoi(cat_metric->value);
  1039. categories[vars_count].variable_name = var_name->value;
  1040. categories[vars_count].variable_value = var_val->value;
  1041. categories[vars_count].var_metric = atoi(var_metric->value);
  1042. vars_count++;
  1043. }
  1044. ast_debug(3, "category: %s\n", category->value);
  1045. ast_debug(3, "var_name: %s\n", var_name->value);
  1046. ast_debug(3, "var_val: %s\n", var_val->value);
  1047. ast_debug(3, "cat_metric: %s\n", cat_metric->value);
  1048. }
  1049. qsort(categories, vars_count, sizeof(*categories), compare_categories);
  1050. for (i = 0; i < vars_count; i++) {
  1051. if (!strcmp(categories[i].variable_name, "#include")) {
  1052. struct ast_flags flags = { 0 };
  1053. if (!ast_config_internal_load(categories[i].variable_value, cfg, flags, "", who_asked)) {
  1054. break;
  1055. }
  1056. continue;
  1057. }
  1058. if (!last_category || strcmp(last_category, categories[i].name) ||
  1059. last_category_metric != categories[i].metric) {
  1060. cur_cat = ast_category_new_dynamic(categories[i].name);
  1061. if (!cur_cat) {
  1062. break;
  1063. }
  1064. last_category = categories[i].name;
  1065. last_category_metric = categories[i].metric;
  1066. ast_category_append(cfg, cur_cat);
  1067. }
  1068. if (!(new_v = ast_variable_new(categories[i].variable_name, categories[i].variable_value, table_name))) {
  1069. break;
  1070. }
  1071. ast_variable_append(cur_cat, new_v);
  1072. }
  1073. ast_free(vars);
  1074. ast_free(categories);
  1075. return cfg;
  1076. }
  1077. /*!
  1078. * \internal
  1079. * \brief Create an LDAP modification structure (LDAPMod)
  1080. *
  1081. * \param attribute the name of the LDAP attribute to modify
  1082. * \param new_value the new value of the LDAP attribute
  1083. *
  1084. * \returns an LDAPMod * if successful, NULL otherwise.
  1085. */
  1086. static LDAPMod *ldap_mod_create(const char *attribute, const char *new_value)
  1087. {
  1088. LDAPMod *mod;
  1089. char *type;
  1090. mod = ldap_memcalloc(1, sizeof(LDAPMod));
  1091. type = ldap_strdup(attribute);
  1092. if (!(mod && type)) {
  1093. ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
  1094. ldap_memfree(type);
  1095. ldap_memfree(mod);
  1096. return NULL;
  1097. }
  1098. mod->mod_type = type;
  1099. if (strlen(new_value)) {
  1100. char **values, *value;
  1101. values = ldap_memcalloc(2, sizeof(char *));
  1102. value = ldap_strdup(new_value);
  1103. if (!(values && value)) {
  1104. ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
  1105. ldap_memfree(value);
  1106. ldap_memfree(values);
  1107. ldap_memfree(type);
  1108. ldap_memfree(mod);
  1109. return NULL;
  1110. }
  1111. mod->mod_op = LDAP_MOD_REPLACE;
  1112. mod->mod_values = values;
  1113. mod->mod_values[0] = value;
  1114. } else {
  1115. mod->mod_op = LDAP_MOD_DELETE;
  1116. }
  1117. return mod;
  1118. }
  1119. /*!
  1120. * \internal
  1121. * \brief Append a value to an existing LDAP modification structure
  1122. *
  1123. * \param src the LDAPMod to update
  1124. * \param new_value the new value to append to the LDAPMod
  1125. *
  1126. * \returns the \c src original passed in if successful, NULL otherwise.
  1127. */
  1128. static LDAPMod *ldap_mod_append(LDAPMod *src, const char *new_value)
  1129. {
  1130. char *new_buffer;
  1131. if (src->mod_op != LDAP_MOD_REPLACE) {
  1132. return src;
  1133. }
  1134. new_buffer = ldap_memrealloc(
  1135. src->mod_values[0],
  1136. strlen(src->mod_values[0]) + strlen(new_value) + sizeof(";"));
  1137. if (!new_buffer) {
  1138. ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
  1139. return NULL;
  1140. }
  1141. strcat(new_buffer, ";");
  1142. strcat(new_buffer, new_value);
  1143. src->mod_values[0] = new_buffer;
  1144. return src;
  1145. }
  1146. /*!
  1147. * \internal
  1148. * \brief Duplicates an LDAP modification structure
  1149. *
  1150. * \param src the LDAPMod to duplicate
  1151. *
  1152. * \returns a deep copy of \c src if successful, NULL otherwise.
  1153. */
  1154. static LDAPMod *ldap_mod_duplicate(const LDAPMod *src)
  1155. {
  1156. LDAPMod *mod;
  1157. char *type, **values = NULL;
  1158. mod = ldap_memcalloc(1, sizeof(LDAPMod));
  1159. type = ldap_strdup(src->mod_type);
  1160. if (!(mod && type)) {
  1161. ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
  1162. ldap_memfree(type);
  1163. ldap_memfree(mod);
  1164. return NULL;
  1165. }
  1166. if (src->mod_op == LDAP_MOD_REPLACE) {
  1167. char *value;
  1168. values = ldap_memcalloc(2, sizeof(char *));
  1169. value = ldap_strdup(src->mod_values[0]);
  1170. if (!(values && value)) {
  1171. ast_log(LOG_ERROR, "Memory allocation failure creating LDAP modification\n");
  1172. ldap_memfree(value);
  1173. ldap_memfree(values);
  1174. ldap_memfree(type);
  1175. ldap_memfree(mod);
  1176. return NULL;
  1177. }
  1178. values[0] = value;
  1179. }
  1180. mod->mod_op = src->mod_op;
  1181. mod->mod_type = type;
  1182. mod->mod_values = values;
  1183. return mod;
  1184. }
  1185. /*!
  1186. * \internal
  1187. * \brief Search for an existing LDAP modification structure
  1188. *
  1189. * \param modifications a NULL terminated array of LDAP modification structures
  1190. * \param lookup the attribute name to search for
  1191. *
  1192. * \returns an LDAPMod * if successful, NULL otherwise.
  1193. */
  1194. static LDAPMod *ldap_mod_find(LDAPMod **modifications, const char *lookup)
  1195. {
  1196. size_t i;
  1197. for (i = 0; modifications[i]; i++) {
  1198. if (modifications[i]->mod_op == LDAP_MOD_REPLACE &&
  1199. !strcasecmp(modifications[i]->mod_type, lookup)) {
  1200. return modifications[i];
  1201. }
  1202. }
  1203. return NULL;
  1204. }
  1205. /*!
  1206. * \internal
  1207. * \brief Determine if an LDAP entry has the specified attribute
  1208. *
  1209. * \param entry the LDAP entry to examine
  1210. * \param lookup the attribute name to search for
  1211. *
  1212. * \returns 1 if the attribute was found, 0 otherwise.
  1213. */
  1214. static int ldap_entry_has_attribute(LDAPMessage *entry, const char *lookup)
  1215. {
  1216. BerElement *ber = NULL;
  1217. char *attribute;
  1218. attribute = ldap_first_attribute(ldapConn, entry, &ber);
  1219. while (attribute) {
  1220. if (!strcasecmp(attribute, lookup)) {
  1221. ldap_memfree(attribute);
  1222. ber_free(ber, 0);
  1223. return 1;
  1224. }
  1225. ldap_memfree(attribute);
  1226. attribute = ldap_next_attribute(ldapConn, entry, ber);
  1227. }
  1228. ber_free(ber, 0);
  1229. return 0;
  1230. }
  1231. /*!
  1232. * \internal
  1233. * \brief Remove LDAP_MOD_DELETE modifications that will not succeed
  1234. *
  1235. * \details
  1236. * A LDAP_MOD_DELETE operation will fail if the LDAP entry does not already have
  1237. * the corresponding attribute. Because we may be updating multiple LDAP entries
  1238. * in a single call to update_ldap(), we may need our own copy of the
  1239. * modifications array for each one.
  1240. *
  1241. * \note
  1242. * This function dynamically allocates memory. If it returns a non-NULL pointer,
  1243. * it is up to the caller to free it with ldap_mods_free()
  1244. *
  1245. * \returns an LDAPMod * if modifications needed to be removed, NULL otherwise.
  1246. */
  1247. static LDAPMod **massage_mods_for_entry(LDAPMessage *entry, LDAPMod **mods)
  1248. {
  1249. size_t k, i, remove_count;
  1250. LDAPMod **copies;
  1251. for (i = remove_count = 0; mods[i]; i++) {
  1252. if (mods[i]->mod_op == LDAP_MOD_DELETE
  1253. && !ldap_entry_has_attribute(entry, mods[i]->mod_type)) {
  1254. remove_count++;
  1255. }
  1256. }
  1257. if (!remove_count) {
  1258. return NULL;
  1259. }
  1260. copies = ldap_memcalloc(i - remove_count + 1, sizeof(LDAPMod *));
  1261. if (!copies) {
  1262. ast_log(LOG_ERROR, "Memory allocation failure massaging LDAP modification\n");
  1263. return NULL;
  1264. }
  1265. for (i = k = 0; mods[i]; i++) {
  1266. if (mods[i]->mod_op != LDAP_MOD_DELETE
  1267. || ldap_entry_has_attribute(entry, mods[i]->mod_type)) {
  1268. copies[k] = ldap_mod_duplicate(mods[i]);
  1269. if (!copies[k]) {
  1270. ast_log(LOG_ERROR, "Memory allocation failure massaging LDAP modification\n");
  1271. ldap_mods_free(copies, 1);
  1272. return NULL;
  1273. }
  1274. k++;
  1275. } else {
  1276. ast_debug(3, "Skipping %s deletion because it doesn't exist\n",
  1277. mods[i]->mod_type);
  1278. }
  1279. }
  1280. return copies;
  1281. }
  1282. /*!
  1283. * \internal
  1284. * \brief Count the number of variables in an ast_variables list
  1285. *
  1286. * \param vars the list of variables to count
  1287. *
  1288. * \returns the number of variables in the specified list
  1289. */
  1290. static size_t variables_count(const struct ast_variable *vars)
  1291. {
  1292. const struct ast_variable *var;
  1293. size_t count = 0;
  1294. for (var = vars; var; var = var->next) {
  1295. count++;
  1296. }
  1297. return count;
  1298. }
  1299. static int update2_ldap(const char *basedn, const char *table_name, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
  1300. {
  1301. const struct ast_variable *field;
  1302. struct ldap_table_config *table_config = NULL;
  1303. char *clean_basedn = NULL;
  1304. struct ast_str *filter = NULL;
  1305. int search_result = 0;
  1306. int res = -1;
  1307. int tries = 0;
  1308. size_t update_count, update_index, entry_count;
  1309. LDAPMessage *ldap_entry = NULL;
  1310. LDAPMod **modifications;
  1311. LDAPMessage *ldap_result_msg = NULL;
  1312. if (!table_name) {
  1313. ast_log(LOG_ERROR, "No table_name specified.\n");
  1314. return res;
  1315. }
  1316. update_count = variables_count(update_fields);
  1317. if (!update_count) {
  1318. ast_log(LOG_WARNING, "Need at least one parameter to modify.\n");
  1319. return res;
  1320. }
  1321. ast_mutex_lock(&ldap_lock);
  1322. /* We now have our complete statement; Lets connect to the server and execute it. */
  1323. if (!ldap_reconnect()) {
  1324. ast_mutex_unlock(&ldap_lock);
  1325. return res;
  1326. }
  1327. table_config = table_config_for_table_name(table_name);
  1328. if (!table_config) {
  1329. ast_log(LOG_ERROR, "No table named '%s'.\n", table_name);
  1330. ast_mutex_unlock(&ldap_lock);
  1331. return res;
  1332. }
  1333. clean_basedn = cleaned_basedn(NULL, basedn);
  1334. filter = create_lookup_filter(table_config, lookup_fields);
  1335. if (!filter) {
  1336. ast_mutex_unlock(&ldap_lock);
  1337. ast_free(clean_basedn);
  1338. return res;
  1339. }
  1340. /*
  1341. * Find LDAP records that match our lookup filter. If there are none, then
  1342. * we don't go through the hassle of building our modifications list.
  1343. */
  1344. do {
  1345. search_result = ldap_search_ext_s(
  1346. ldapConn,
  1347. clean_basedn,
  1348. LDAP_SCOPE_SUBTREE,
  1349. ast_str_buffer(filter),
  1350. NULL, 0, NULL, NULL, NULL,
  1351. LDAP_NO_LIMIT,
  1352. &ldap_result_msg);
  1353. if (search_result != LDAP_SUCCESS && is_ldap_connect_error(search_result)) {
  1354. ast_log(LOG_WARNING, "Failed to query directory. Try %d/3\n", tries + 1);
  1355. tries++;
  1356. if (tries < 3) {
  1357. usleep(500000L * tries);
  1358. if (ldapConn) {
  1359. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  1360. ldapConn = NULL;
  1361. }
  1362. if (!ldap_reconnect()) {
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. } while (search_result != LDAP_SUCCESS && tries < 3 && is_ldap_connect_error(search_result));
  1368. if (search_result != LDAP_SUCCESS) {
  1369. ast_log(LOG_WARNING, "Failed to query directory. Error: %s.\n", ldap_err2string(search_result));
  1370. ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
  1371. goto early_bailout;
  1372. }
  1373. entry_count = ldap_count_entries(ldapConn, ldap_result_msg);
  1374. if (!entry_count) {
  1375. /* Nothing found, nothing to update */
  1376. res = 0;
  1377. goto early_bailout;
  1378. }
  1379. /* We need to NULL terminate, so we allocate one more than we need */
  1380. modifications = ldap_memcalloc(update_count + 1, sizeof(LDAPMod *));
  1381. if (!modifications) {
  1382. ast_log(LOG_ERROR, "Memory allocation failure\n");
  1383. goto early_bailout;
  1384. }
  1385. /*
  1386. * Create the modification array with the parameter/value pairs we were given,
  1387. * if there are several parameters with the same name, we collect them into
  1388. * one parameter/value pair and delimit them with a semicolon
  1389. */
  1390. for (field = update_fields, update_index = 0; field; field = field->next) {
  1391. LDAPMod *mod;
  1392. const char *ldap_attribute_name = convert_attribute_name_to_ldap(
  1393. table_config,
  1394. field->name);
  1395. /* See if we already have it */
  1396. mod = ldap_mod_find(modifications, ldap_attribute_name);
  1397. if (mod) {
  1398. mod = ldap_mod_append(mod, field->value);
  1399. if (!mod) {
  1400. goto late_bailout;
  1401. }
  1402. } else {
  1403. mod = ldap_mod_create(ldap_attribute_name, field->value);
  1404. if (!mod) {
  1405. goto late_bailout;
  1406. }
  1407. modifications[update_index++] = mod;
  1408. }
  1409. }
  1410. /* Ready to update */
  1411. ast_debug(3, "Modifying %zu matched entries\n", entry_count);
  1412. if (DEBUG_ATLEAST(3)) {
  1413. size_t i;
  1414. for (i = 0; modifications[i]; i++) {
  1415. if (modifications[i]->mod_op != LDAP_MOD_DELETE) {
  1416. ast_log(LOG_DEBUG, "%s => %s\n", modifications[i]->mod_type,
  1417. modifications[i]->mod_values[0]);
  1418. } else {
  1419. ast_log(LOG_DEBUG, "deleting %s\n", modifications[i]->mod_type);
  1420. }
  1421. }
  1422. }
  1423. for (ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
  1424. ldap_entry;
  1425. ldap_entry = ldap_next_entry(ldapConn, ldap_entry)) {
  1426. int error;
  1427. LDAPMod **massaged, **working;
  1428. char *dn = ldap_get_dn(ldapConn, ldap_entry);
  1429. if (!dn) {
  1430. ast_log(LOG_ERROR, "Memory allocation failure\n");
  1431. goto late_bailout;
  1432. }
  1433. working = modifications;
  1434. massaged = massage_mods_for_entry(ldap_entry, modifications);
  1435. if (massaged) {
  1436. /* Did we massage everything out of the list? */
  1437. if (!massaged[0]) {
  1438. ast_debug(3, "Nothing left to modify - skipping\n");
  1439. ldap_mods_free(massaged, 1);
  1440. ldap_memfree(dn);
  1441. continue;
  1442. }
  1443. working = massaged;
  1444. }
  1445. if ((error = ldap_modify_ext_s(ldapConn, dn, working, NULL, NULL)) != LDAP_SUCCESS) {
  1446. ast_log(LOG_ERROR, "Couldn't modify dn:%s because %s", dn, ldap_err2string(error));
  1447. }
  1448. if (massaged) {
  1449. ldap_mods_free(massaged, 1);
  1450. }
  1451. ldap_memfree(dn);
  1452. }
  1453. res = entry_count;
  1454. late_bailout:
  1455. ldap_mods_free(modifications, 1);
  1456. early_bailout:
  1457. ldap_msgfree(ldap_result_msg);
  1458. ast_free(filter);
  1459. ast_free(clean_basedn);
  1460. ast_mutex_unlock(&ldap_lock);
  1461. return res;
  1462. }
  1463. static int update_ldap(const char *basedn, const char *table_name, const char *attribute, const char *lookup, const struct ast_variable *fields)
  1464. {
  1465. int res;
  1466. struct ast_variable *lookup_fields = ast_variable_new(attribute, lookup, "");
  1467. res = update2_ldap(basedn, table_name, lookup_fields, fields);
  1468. ast_variables_destroy(lookup_fields);
  1469. return res;
  1470. }
  1471. static struct ast_config_engine ldap_engine = {
  1472. .name = "ldap",
  1473. .load_func = config_ldap,
  1474. .realtime_func = realtime_ldap,
  1475. .realtime_multi_func = realtime_multi_ldap,
  1476. .update_func = update_ldap,
  1477. .update2_func = update2_ldap,
  1478. };
  1479. /*!
  1480. * \brief Load the module
  1481. *
  1482. * Module loading including tests for configuration or dependencies.
  1483. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1484. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1485. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1486. * configuration file or other non-critical problem return
  1487. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1488. *
  1489. * \todo Don't error or warn on a default install. If the config is
  1490. * default we should not attempt to connect to a server. -lathama
  1491. */
  1492. static int load_module(void)
  1493. {
  1494. if (parse_config() < 0) {
  1495. ast_log(LOG_ERROR, "Cannot load LDAP RealTime driver.\n");
  1496. return 0;
  1497. }
  1498. ast_mutex_lock(&ldap_lock);
  1499. if (!ldap_reconnect()) {
  1500. ast_log(LOG_WARNING, "Couldn't establish connection to LDAP directory. Check debug.\n");
  1501. }
  1502. ast_config_engine_register(&ldap_engine);
  1503. ast_verb(1, "LDAP RealTime driver loaded.\n");
  1504. ast_cli_register_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
  1505. ast_mutex_unlock(&ldap_lock);
  1506. return 0;
  1507. }
  1508. /*! \brief Unload Module
  1509. *
  1510. */
  1511. static int unload_module(void)
  1512. {
  1513. /* Aquire control before doing anything to the module itself. */
  1514. ast_mutex_lock(&ldap_lock);
  1515. table_configs_free();
  1516. if (ldapConn) {
  1517. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  1518. ldapConn = NULL;
  1519. }
  1520. ast_cli_unregister_multiple(ldap_cli, ARRAY_LEN(ldap_cli));
  1521. ast_config_engine_deregister(&ldap_engine);
  1522. ast_verb(1, "LDAP RealTime driver unloaded.\n");
  1523. /* Unlock so something else can destroy the lock. */
  1524. ast_mutex_unlock(&ldap_lock);
  1525. return 0;
  1526. }
  1527. /*! \brief Reload Module
  1528. */
  1529. static int reload(void)
  1530. {
  1531. /* Aquire control before doing anything to the module itself. */
  1532. ast_mutex_lock(&ldap_lock);
  1533. if (ldapConn) {
  1534. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  1535. ldapConn = NULL;
  1536. }
  1537. if (parse_config() < 0) {
  1538. ast_log(LOG_NOTICE, "Cannot reload LDAP RealTime driver.\n");
  1539. ast_mutex_unlock(&ldap_lock);
  1540. return 0;
  1541. }
  1542. if (!ldap_reconnect()) {
  1543. ast_log(LOG_WARNING, "Couldn't establish connection to your directory server. Check debug.\n");
  1544. }
  1545. ast_verb(2, "LDAP RealTime driver reloaded.\n");
  1546. /* Done reloading. Release lock so others can now use driver. */
  1547. ast_mutex_unlock(&ldap_lock);
  1548. return 0;
  1549. }
  1550. static int config_can_be_inherited(const char *key)
  1551. {
  1552. int i;
  1553. static const char * const config[] = {
  1554. "basedn", "host", "pass", "port", "protocol", "url", "user", "version", NULL
  1555. };
  1556. for (i = 0; config[i]; i++) {
  1557. if (!strcasecmp(key, config[i])) {
  1558. return 0;
  1559. }
  1560. }
  1561. return 1;
  1562. }
  1563. /*! \brief parse the configuration file
  1564. */
  1565. static int parse_config(void)
  1566. {
  1567. struct ast_config *config;
  1568. struct ast_flags config_flags = {0};
  1569. const char *s, *host;
  1570. int port;
  1571. char *category_name = NULL;
  1572. /* Make sure that global variables are reset */
  1573. url[0] = '\0';
  1574. user[0] = '\0';
  1575. pass[0] = '\0';
  1576. base_distinguished_name[0] = '\0';
  1577. version = 3;
  1578. config = ast_config_load(RES_CONFIG_LDAP_CONF, config_flags);
  1579. if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
  1580. ast_log(LOG_ERROR, "Cannot load configuration file: %s\n", RES_CONFIG_LDAP_CONF);
  1581. return -1;
  1582. }
  1583. if (!(s = ast_variable_retrieve(config, "_general", "user"))) {
  1584. ast_log(LOG_NOTICE, "No directory user found, anonymous binding as default.\n");
  1585. user[0] = '\0';
  1586. } else {
  1587. ast_copy_string(user, s, sizeof(user));
  1588. }
  1589. if (!ast_strlen_zero(user)) {
  1590. if (!(s = ast_variable_retrieve(config, "_general", "pass"))) {
  1591. ast_log(LOG_WARNING, "No directory password found, using 'asterisk' as default.\n");
  1592. ast_copy_string(pass, "asterisk", sizeof(pass));
  1593. } else {
  1594. ast_copy_string(pass, s, sizeof(pass));
  1595. }
  1596. }
  1597. /* URL is preferred, use host and port if not found */
  1598. if ((s = ast_variable_retrieve(config, "_general", "url"))) {
  1599. ast_copy_string(url, s, sizeof(url));
  1600. } else if ((host = ast_variable_retrieve(config, "_general", "host"))) {
  1601. if (!(s = ast_variable_retrieve(config, "_general", "port")) || sscanf(s, "%5d", &port) != 1 || port > 65535) {
  1602. ast_log(LOG_NOTICE, "No directory port found, using 389 as default.\n");
  1603. port = 389;
  1604. }
  1605. snprintf(url, sizeof(url), "ldap://%s:%d", host, port);
  1606. } else {
  1607. ast_log(LOG_ERROR, "No directory URL or host found.\n");
  1608. ast_config_destroy(config);
  1609. return -1;
  1610. }
  1611. if (!(s = ast_variable_retrieve(config, "_general", "basedn"))) {
  1612. ast_log(LOG_ERROR, "No LDAP base dn found, using '%s' as default.\n", RES_CONFIG_LDAP_DEFAULT_BASEDN);
  1613. ast_copy_string(base_distinguished_name, RES_CONFIG_LDAP_DEFAULT_BASEDN, sizeof(base_distinguished_name));
  1614. } else
  1615. ast_copy_string(base_distinguished_name, s, sizeof(base_distinguished_name));
  1616. if (!(s = ast_variable_retrieve(config, "_general", "version")) && !(s = ast_variable_retrieve(config, "_general", "protocol"))) {
  1617. ast_log(LOG_NOTICE, "No explicit LDAP version found, using 3 as default.\n");
  1618. } else if (sscanf(s, "%30d", &version) != 1 || version < 1 || version > 6) {
  1619. ast_log(LOG_WARNING, "Invalid LDAP version '%s', using 3 as default.\n", s);
  1620. version = 3;
  1621. }
  1622. table_configs_free();
  1623. while ((category_name = ast_category_browse(config, category_name))) {
  1624. int is_general = (strcasecmp(category_name, "_general") == 0);
  1625. int is_config = (strcasecmp(category_name, "config") == 0); /*!< using the [config] context for Static RealTime */
  1626. struct ast_variable *var = ast_variable_browse(config, category_name);
  1627. if (var) {
  1628. struct ldap_table_config *table_config =
  1629. table_config_for_table_name(category_name);
  1630. if (!table_config) {
  1631. table_config = table_config_new(category_name);
  1632. AST_LIST_INSERT_HEAD(&table_configs, table_config, entry);
  1633. if (is_general)
  1634. base_table_config = table_config;
  1635. if (is_config)
  1636. static_table_config = table_config;
  1637. }
  1638. for (; var; var = var->next) {
  1639. if (!strcasecmp(var->name, "additionalFilter")) {
  1640. table_config->additional_filter = ast_strdup(var->value);
  1641. } else {
  1642. if (!is_general || config_can_be_inherited(var->name)) {
  1643. ldap_table_config_add_attribute(table_config, var->name, var->value);
  1644. }
  1645. }
  1646. }
  1647. }
  1648. }
  1649. ast_config_destroy(config);
  1650. return 1;
  1651. }
  1652. /*! \note ldap_lock should have been locked before calling this function. */
  1653. static int ldap_reconnect(void)
  1654. {
  1655. int bind_result = 0;
  1656. struct berval cred;
  1657. if (ldapConn) {
  1658. ast_debug(2, "Everything seems fine.\n");
  1659. return 1;
  1660. }
  1661. if (ast_strlen_zero(url)) {
  1662. ast_log(LOG_ERROR, "Not enough parameters to connect to ldap directory\n");
  1663. return 0;
  1664. }
  1665. if (LDAP_SUCCESS != ldap_initialize(&ldapConn, url)) {
  1666. ast_log(LOG_ERROR, "Failed to init ldap connection to '%s'. Check debug for more info.\n", url);
  1667. return 0;
  1668. }
  1669. if (LDAP_OPT_SUCCESS != ldap_set_option(ldapConn, LDAP_OPT_PROTOCOL_VERSION, &version)) {
  1670. ast_log(LOG_WARNING, "Unable to set LDAP protocol version to %d, falling back to default.\n", version);
  1671. }
  1672. if (!ast_strlen_zero(user)) {
  1673. ast_debug(2, "bind to '%s' as user '%s'\n", url, user);
  1674. cred.bv_val = (char *) pass;
  1675. cred.bv_len = strlen(pass);
  1676. bind_result = ldap_sasl_bind_s(ldapConn, user, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
  1677. } else {
  1678. ast_debug(2, "bind %s anonymously\n", url);
  1679. cred.bv_val = NULL;
  1680. cred.bv_len = 0;
  1681. bind_result = ldap_sasl_bind_s(ldapConn, NULL, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
  1682. }
  1683. if (bind_result == LDAP_SUCCESS) {
  1684. ast_debug(2, "Successfully connected to directory.\n");
  1685. connect_time = time(NULL);
  1686. return 1;
  1687. } else {
  1688. ast_log(LOG_WARNING, "bind failed: %s\n", ldap_err2string(bind_result));
  1689. ldap_unbind_ext_s(ldapConn, NULL, NULL);
  1690. ldapConn = NULL;
  1691. return 0;
  1692. }
  1693. }
  1694. /*! \brief Realtime Status
  1695. *
  1696. */
  1697. static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1698. {
  1699. struct ast_str *buf;
  1700. int ctimesec = time(NULL) - connect_time;
  1701. switch (cmd) {
  1702. case CLI_INIT:
  1703. e->command = "realtime show ldap status";
  1704. e->usage =
  1705. "Usage: realtime show ldap status\n"
  1706. " Shows connection information for the LDAP RealTime driver\n";
  1707. return NULL;
  1708. case CLI_GENERATE:
  1709. return NULL;
  1710. }
  1711. if (!ldapConn)
  1712. return CLI_FAILURE;
  1713. buf = ast_str_create(512);
  1714. if (!ast_strlen_zero(url)) {
  1715. ast_str_append(&buf, 0, "Connected to '%s', baseDN %s", url, base_distinguished_name);
  1716. }
  1717. if (!ast_strlen_zero(user)) {
  1718. ast_str_append(&buf, 0, " with username %s", user);
  1719. }
  1720. ast_str_append(&buf, 0, " for ");
  1721. ast_cli_print_timestr_fromseconds(a->fd, ctimesec, ast_str_buffer(buf));
  1722. ast_free(buf);
  1723. return CLI_SUCCESS;
  1724. }
  1725. /*! \brief Module Information
  1726. *
  1727. */
  1728. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "LDAP realtime interface",
  1729. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1730. .load = load_module,
  1731. .unload = unload_module,
  1732. .reload = reload,
  1733. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  1734. .requires = "extconfig",
  1735. );