res_config_ldap.c 56 KB

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