res_config_ldap.c 54 KB

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