ldap_mod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * $Id$
  3. *
  4. * Kamailio LDAP Module
  5. *
  6. * Copyright (C) 2007 University of North Carolina
  7. *
  8. * Original author: Christian Schlatter, [email protected]
  9. *
  10. *
  11. * This file is part of Kamailio, a free SIP server.
  12. *
  13. * Kamailio is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version
  17. *
  18. * Kamailio is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2007-02-18: Initial version
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/time.h>
  34. #include "../../ut.h"
  35. #include "../../parser/hf.h"
  36. #include "../../sr_module.h"
  37. #include "../../pvar.h"
  38. #include "../../mem/mem.h"
  39. #include "ld_session.h"
  40. #include "ldap_exp_fn.h"
  41. #include "api.h"
  42. #include "ldap_connect.h"
  43. #include "ldap_api_fn.h"
  44. #include "iniparser.h"
  45. MODULE_VERSION
  46. /*
  47. * Module management function prototypes
  48. */
  49. static int mod_init(void);
  50. static void destroy(void);
  51. static int child_init(int rank);
  52. /*
  53. * fixup functions
  54. */
  55. static int ldap_search_fixup(void** param, int param_no);
  56. static int ldap_result_fixup(void** param, int param_no);
  57. static int ldap_filter_url_encode_fixup(void** param, int param_no);
  58. static int ldap_result_check_fixup(void** param, int param_no);
  59. /*
  60. * exported functions
  61. */
  62. static int w_ldap_search(struct sip_msg* msg, char* ldap_url, char* param);
  63. static int w_ldap_result1(struct sip_msg* msg, char* src, char* param);
  64. static int w_ldap_result2(struct sip_msg* msg, char* src, char* subst);
  65. static int w_ldap_result_next(struct sip_msg* msg, char* foo, char *bar);
  66. static int w_ldap_filter_url_encode(struct sip_msg* msg,
  67. char* filter_component, char* dst_avp_name);
  68. static int w_ldap_result_check_1(struct sip_msg* msg,
  69. char* attr_name_check_str, char* param);
  70. static int w_ldap_result_check_2(struct sip_msg* msg,
  71. char* attr_name_check_str, char* attr_val_re);
  72. /*
  73. * Default module parameter values
  74. */
  75. #define DEF_LDAP_CONFIG "/usr/local/etc/kamailio/ldap.cfg"
  76. /*
  77. * Module parameter variables
  78. */
  79. str ldap_config = str_init(DEF_LDAP_CONFIG);
  80. static dictionary* config_vals = NULL;
  81. /*
  82. * Exported functions
  83. */
  84. static cmd_export_t cmds[] = {
  85. {"ldap_search", (cmd_function)w_ldap_search, 1,
  86. ldap_search_fixup, 0,
  87. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  88. {"ldap_result", (cmd_function)w_ldap_result1, 1,
  89. ldap_result_fixup, 0,
  90. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  91. {"ldap_result", (cmd_function)w_ldap_result2, 2,
  92. ldap_result_fixup, 0,
  93. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  94. {"ldap_result_next", (cmd_function)w_ldap_result_next, 0,
  95. 0, 0,
  96. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  97. {"ldap_result_check", (cmd_function)w_ldap_result_check_1, 1,
  98. ldap_result_check_fixup, 0,
  99. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  100. {"ldap_result_check", (cmd_function)w_ldap_result_check_2, 2,
  101. ldap_result_check_fixup, 0,
  102. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  103. {"ldap_filter_url_encode", (cmd_function)w_ldap_filter_url_encode, 2,
  104. ldap_filter_url_encode_fixup, 0,
  105. REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
  106. {"load_ldap", (cmd_function)load_ldap, 0,
  107. 0, 0,
  108. 0},
  109. {0, 0, 0, 0, 0, 0}
  110. };
  111. /*
  112. * Exported parameters
  113. */
  114. static param_export_t params[] = {
  115. {"config_file", PARAM_STR, &ldap_config},
  116. {0, 0, 0}
  117. };
  118. /*
  119. * Module interface
  120. */
  121. struct module_exports exports = {
  122. "ldap",
  123. DEFAULT_DLFLAGS, /* dlopen flags */
  124. cmds, /* Exported functions */
  125. params, /* Exported parameters */
  126. 0, /* exported statistics */
  127. 0, /* exported MI functions */
  128. 0, /* exported pseudo-variables */
  129. 0, /* extra processes */
  130. mod_init, /* module initialization function */
  131. 0, /* response function */
  132. destroy, /* destroy function */
  133. child_init /* child initialization function */
  134. };
  135. static int child_init(int rank)
  136. {
  137. int i = 0, ld_count = 0;
  138. char* ld_name;
  139. /* don't do anything for non-worker processes */
  140. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  141. return 0;
  142. /*
  143. * build ld_sessions and connect all sessions
  144. */
  145. ld_count = iniparser_getnsec(config_vals);
  146. for (i = 0; i < ld_count; i++)
  147. {
  148. ld_name = iniparser_getsecname(config_vals, i);
  149. if (add_ld_session(ld_name,
  150. NULL,
  151. config_vals)
  152. != 0)
  153. {
  154. LM_ERR("[%s]: add_ld_session failed\n", ld_name);
  155. return -1;
  156. }
  157. if (ldap_connect(ld_name) != 0)
  158. {
  159. LM_ERR("[%s]: failed to connect to LDAP host(s)\n", ld_name);
  160. ldap_disconnect(ld_name);
  161. return -1;
  162. }
  163. }
  164. return 0;
  165. }
  166. static int mod_init(void)
  167. {
  168. int ld_count = 0, i = 0;
  169. char* section_name;
  170. char* ldap_version;
  171. /*
  172. * read config file
  173. */
  174. if (ldap_config.len <= 0)
  175. {
  176. LM_ERR("config_file is empty - this module param is mandatory\n");
  177. return -2;
  178. }
  179. if ((config_vals = iniparser_new(ldap_config.s)) == NULL)
  180. {
  181. LM_ERR("failed to read config_file [%s]\n", ldap_config.s);
  182. return -2;
  183. }
  184. if ((ld_count = iniparser_getnsec(config_vals)) < 1)
  185. {
  186. LM_ERR("no section found in config_file [%s]\n", ldap_config.s);
  187. return -2;
  188. }
  189. /* check if mandatory settings are present */
  190. for (i = 0; i < ld_count; i++)
  191. {
  192. section_name = iniparser_getsecname(config_vals, i);
  193. if (strlen(section_name) > 255)
  194. {
  195. LM_ERR( "config_file section name [%s]"
  196. " longer than allowed 255 characters",
  197. section_name);
  198. return -2;
  199. }
  200. if (!iniparser_find_entry(config_vals,
  201. get_ini_key_name(section_name, CFG_N_LDAP_HOST)))
  202. {
  203. LM_ERR( "mandatory %s not defined in [%s]\n",
  204. CFG_N_LDAP_HOST,
  205. section_name);
  206. return -2;
  207. }
  208. }
  209. /*
  210. * print ldap version string
  211. */
  212. if (ldap_get_vendor_version(&ldap_version) != 0)
  213. {
  214. LM_ERR("ldap_get_vendor_version failed\n");
  215. return -2;
  216. }
  217. LM_INFO("%s\n", ldap_version);
  218. return 0;
  219. }
  220. static void destroy(void)
  221. {
  222. /* ldap_unbind */
  223. free_ld_sessions();
  224. /* free config file memory */
  225. iniparser_free(config_vals);
  226. }
  227. /*
  228. * EXPORTED functions
  229. */
  230. static int w_ldap_search(struct sip_msg* msg, char* ldap_url, char* param)
  231. {
  232. return ldap_search_impl(msg, (pv_elem_t*)ldap_url);
  233. }
  234. static int w_ldap_result1(struct sip_msg* msg, char* src, char* param)
  235. {
  236. return ldap_write_result(msg, (struct ldap_result_params*)src, NULL);
  237. }
  238. static int w_ldap_result2(struct sip_msg* msg, char* src, char* subst)
  239. {
  240. return ldap_write_result(msg, (struct ldap_result_params*)src,
  241. (struct subst_expr*)subst);
  242. }
  243. static int w_ldap_result_next(struct sip_msg* msg, char* foo, char *bar)
  244. {
  245. return ldap_result_next();
  246. }
  247. static int w_ldap_filter_url_encode(struct sip_msg* msg,
  248. char* filter_component, char* dst_avp_name)
  249. {
  250. return ldap_filter_url_encode(msg, (pv_elem_t*)filter_component,
  251. (pv_spec_t*)dst_avp_name);
  252. }
  253. static int w_ldap_result_check_1(struct sip_msg* msg,
  254. char* attr_name_check_str, char* param)
  255. {
  256. return ldap_result_check(msg,
  257. (struct ldap_result_check_params*)attr_name_check_str, NULL);
  258. }
  259. static int w_ldap_result_check_2(struct sip_msg* msg,
  260. char* attr_name_check_str, char* attr_val_re)
  261. {
  262. return ldap_result_check( msg,
  263. (struct ldap_result_check_params*)attr_name_check_str,
  264. (struct subst_expr*)attr_val_re);
  265. }
  266. /*
  267. * FIXUP functions
  268. */
  269. static int ldap_search_fixup(void** param, int param_no)
  270. {
  271. pv_elem_t *model;
  272. str s;
  273. if (param_no == 1) {
  274. s.s = (char*)*param;
  275. s.len = strlen(s.s);
  276. if (s.len==0) {
  277. LM_ERR("ldap url is empty string!\n");
  278. return E_CFG;
  279. }
  280. if ( pv_parse_format(&s,&model) || model==NULL) {
  281. LM_ERR("wrong format [%s] for ldap url!\n", s.s);
  282. return E_CFG;
  283. }
  284. *param = (void*)model;
  285. }
  286. return 0;
  287. }
  288. static int ldap_result_fixup(void** param, int param_no)
  289. {
  290. struct ldap_result_params* lp;
  291. struct subst_expr* se;
  292. str subst;
  293. char *arg_str, *dst_avp_str, *dst_avp_val_type_str;
  294. char *p;
  295. str s;
  296. int dst_avp_val_type = 0;
  297. if (param_no == 1) {
  298. arg_str = (char*)*param;
  299. if ((dst_avp_str = strchr(arg_str, '/')) == 0)
  300. {
  301. /* no / found in arg_str */
  302. LM_ERR("invalid first argument [%s]\n", arg_str);
  303. return E_UNSPEC;
  304. }
  305. *(dst_avp_str++) = 0;
  306. if ((dst_avp_val_type_str = strchr(dst_avp_str, '/')))
  307. {
  308. *(dst_avp_val_type_str++) = 0;
  309. if (!strcmp(dst_avp_val_type_str, "int"))
  310. {
  311. dst_avp_val_type = 1;
  312. }
  313. else if (strcmp(dst_avp_val_type_str, "str"))
  314. {
  315. LM_ERR( "invalid avp_type [%s]\n",
  316. dst_avp_val_type_str);
  317. return E_UNSPEC;
  318. }
  319. }
  320. lp = (struct ldap_result_params*)pkg_malloc(sizeof(struct ldap_result_params));
  321. if (lp == NULL) {
  322. LM_ERR("no memory\n");
  323. return E_OUT_OF_MEM;
  324. }
  325. memset(lp, 0, sizeof(struct ldap_result_params));
  326. lp->ldap_attr_name.s = arg_str;
  327. lp->ldap_attr_name.len = strlen(arg_str);
  328. lp->dst_avp_val_type = dst_avp_val_type;
  329. s.s = dst_avp_str; s.len = strlen(s.s);
  330. p = pv_parse_spec(&s, &lp->dst_avp_spec);
  331. if (p == 0) {
  332. pkg_free(lp);
  333. LM_ERR("parse error for [%s]\n",
  334. dst_avp_str);
  335. return E_UNSPEC;
  336. }
  337. if (lp->dst_avp_spec.type != PVT_AVP) {
  338. pkg_free(lp);
  339. LM_ERR( "bad attribute name [%s]\n",
  340. dst_avp_str);
  341. return E_UNSPEC;
  342. }
  343. *param = (void*)lp;
  344. } else if (param_no == 2) {
  345. subst.s = *param;
  346. subst.len = strlen(*param);
  347. se = subst_parser(&subst);
  348. if (se == 0) {
  349. LM_ERR("bad subst re [%s]\n",
  350. (char*)*param);
  351. return E_BAD_RE;
  352. }
  353. *param = (void*)se;
  354. }
  355. return 0;
  356. }
  357. static int ldap_result_check_fixup(void** param, int param_no)
  358. {
  359. struct ldap_result_check_params *lp;
  360. struct subst_expr *se;
  361. str subst;
  362. str s;
  363. char *arg_str, *check_str;
  364. int arg_str_len;
  365. if (param_no == 1)
  366. {
  367. arg_str = (char*)*param;
  368. arg_str_len = strlen(arg_str);
  369. if ((check_str = strchr(arg_str, '/')) == 0)
  370. {
  371. /* no / found in arg_str */
  372. LM_ERR( "invalid first argument [%s] (no '/' found)\n",
  373. arg_str);
  374. return E_UNSPEC;
  375. }
  376. *(check_str++) = 0;
  377. lp = (struct ldap_result_check_params*)pkg_malloc(sizeof(struct ldap_result_check_params));
  378. if (lp == NULL) {
  379. LM_ERR("no memory\n");
  380. return E_OUT_OF_MEM;
  381. }
  382. memset(lp, 0, sizeof(struct ldap_result_check_params));
  383. lp->ldap_attr_name.s = arg_str;
  384. lp->ldap_attr_name.len = strlen(arg_str);
  385. if (lp->ldap_attr_name.len + 1 == arg_str_len)
  386. {
  387. /* empty check_str */
  388. lp->check_str_elem_p = 0;
  389. }
  390. else
  391. {
  392. s.s = check_str; s.len = strlen(s.s);
  393. if (pv_parse_format(&s, &(lp->check_str_elem_p)) < 0)
  394. {
  395. LM_ERR("pv_parse_format failed\n");
  396. return E_OUT_OF_MEM;
  397. }
  398. }
  399. *param = (void*)lp;
  400. }
  401. else if (param_no == 2)
  402. {
  403. subst.s = *param;
  404. subst.len = strlen(*param);
  405. se = subst_parser(&subst);
  406. if (se == 0) {
  407. LM_ERR( "bad subst re [%s]\n",
  408. (char*)*param);
  409. return E_BAD_RE;
  410. }
  411. *param = (void*)se;
  412. }
  413. return 0;
  414. }
  415. static int ldap_filter_url_encode_fixup(void** param, int param_no)
  416. {
  417. pv_elem_t *elem_p;
  418. pv_spec_t *spec_p;
  419. str s;
  420. if (param_no == 1) {
  421. s.s = (char*)*param;
  422. if (s.s==0 || s.s[0]==0) {
  423. elem_p = 0;
  424. } else {
  425. s.len = strlen(s.s);
  426. if (pv_parse_format(&s, &elem_p) < 0) {
  427. LM_ERR("pv_parse_format failed\n");
  428. return E_OUT_OF_MEM;
  429. }
  430. }
  431. *param = (void*)elem_p;
  432. }
  433. else if (param_no == 2)
  434. {
  435. spec_p = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
  436. if (spec_p == NULL) {
  437. LM_ERR("no memory\n");
  438. return E_OUT_OF_MEM;
  439. }
  440. s.s = (char*)*param; s.len = strlen(s.s);
  441. if (pv_parse_spec(&s, spec_p)
  442. == 0)
  443. {
  444. pkg_free(spec_p);
  445. LM_ERR("parse error for [%s]\n",
  446. (char*)*param);
  447. return E_UNSPEC;
  448. }
  449. if (spec_p->type != PVT_AVP) {
  450. pkg_free(spec_p);
  451. LM_ERR("bad attribute name"
  452. " [%s]\n", (char*)*param);
  453. return E_UNSPEC;
  454. }
  455. *param = (void*)spec_p;
  456. }
  457. return 0;
  458. }