ldap_mod.c 12 KB

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