authdb_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication Module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-02-26: checks and group moved to separate modules (janakj)
  27. * 2003-03-11: New module interface (janakj)
  28. * 2003-03-16: flags export parameter added (janakj)
  29. * 2003-03-19 all mallocs/frees replaced w/ pkg_malloc/pkg_free (andrei)
  30. * 2003-04-05: default_uri #define used (jiri)
  31. * 2004-06-06 cleanup: static & auth_db_{init,bind,close.ver} used (andrei)
  32. * 2005-05-31 general definition of AVPs in credentials now accepted - ID AVP,
  33. * STRING AVP, AVP aliases (bogdan)
  34. * 2006-03-01 pseudo variables support for domain name (bogdan)
  35. */
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "../../sr_module.h"
  39. #include "../../lib/srdb1/db.h"
  40. #include "../../dprint.h"
  41. #include "../../error.h"
  42. #include "../../mod_fix.h"
  43. #include "../../trim.h"
  44. #include "../../mem/mem.h"
  45. #include "../../parser/parse_uri.h"
  46. #include "../../modules/auth/api.h"
  47. #include "authorize.h"
  48. MODULE_VERSION
  49. #define TABLE_VERSION 6
  50. /*
  51. * Module destroy function prototype
  52. */
  53. static void destroy(void);
  54. /*
  55. * Module child-init function prototype
  56. */
  57. static int child_init(int rank);
  58. /*
  59. * Module initialization function prototype
  60. */
  61. static int mod_init(void);
  62. static int w_is_subscriber(sip_msg_t *msg, char *_uri, char* _table,
  63. char *_flags);
  64. static int auth_fixup(void** param, int param_no);
  65. static int auth_check_fixup(void** param, int param_no);
  66. int parse_aaa_pvs(char *definition, pv_elem_t **pv_def, int *cnt);
  67. #define USER_COL "username"
  68. #define DOMAIN_COL "domain"
  69. #define PASS_COL "ha1"
  70. #define PASS_COL_2 "ha1b"
  71. /*
  72. * Module parameter variables
  73. */
  74. static str db_url = str_init(DEFAULT_RODB_URL);
  75. str user_column = str_init(USER_COL);
  76. str domain_column = str_init(DOMAIN_COL);
  77. str pass_column = str_init(PASS_COL);
  78. str pass_column_2 = str_init(PASS_COL_2);
  79. static int version_table_check = 1;
  80. int calc_ha1 = 0;
  81. int use_domain = 0; /* Use also domain when looking up in table */
  82. db1_con_t* auth_db_handle = 0; /* database connection handle */
  83. db_func_t auth_dbf;
  84. auth_api_s_t auth_api;
  85. char *credentials_list = 0;
  86. pv_elem_t *credentials = 0; /* Parsed list of credentials to load */
  87. int credentials_n = 0; /* Number of credentials in the list */
  88. /*
  89. * Exported functions
  90. */
  91. static cmd_export_t cmds[] = {
  92. {"www_authorize", (cmd_function)www_authenticate, 2, auth_fixup, 0,
  93. REQUEST_ROUTE},
  94. {"www_authenticate", (cmd_function)www_authenticate, 2, auth_fixup, 0,
  95. REQUEST_ROUTE},
  96. {"www_authenticate", (cmd_function)www_authenticate2, 3, auth_fixup, 0,
  97. REQUEST_ROUTE},
  98. {"proxy_authorize", (cmd_function)proxy_authenticate, 2, auth_fixup, 0,
  99. REQUEST_ROUTE},
  100. {"proxy_authenticate", (cmd_function)proxy_authenticate, 2, auth_fixup, 0,
  101. REQUEST_ROUTE},
  102. {"auth_check", (cmd_function)auth_check, 3, auth_check_fixup, 0,
  103. REQUEST_ROUTE},
  104. {"is_subscriber", (cmd_function)w_is_subscriber, 3, auth_check_fixup, 0,
  105. ANY_ROUTE},
  106. {"bind_auth_db", (cmd_function)bind_auth_db, 0, 0, 0,
  107. 0},
  108. {0, 0, 0, 0, 0, 0}
  109. };
  110. /*
  111. * Exported parameters
  112. */
  113. static param_export_t params[] = {
  114. {"db_url", PARAM_STR, &db_url },
  115. {"user_column", PARAM_STR, &user_column },
  116. {"domain_column", PARAM_STR, &domain_column },
  117. {"password_column", PARAM_STR, &pass_column },
  118. {"password_column_2", PARAM_STR, &pass_column_2 },
  119. {"calculate_ha1", INT_PARAM, &calc_ha1 },
  120. {"use_domain", INT_PARAM, &use_domain },
  121. {"load_credentials", PARAM_STRING, &credentials_list },
  122. {"version_table", INT_PARAM, &version_table_check },
  123. {0, 0, 0}
  124. };
  125. /*
  126. * Module interface
  127. */
  128. struct module_exports exports = {
  129. "auth_db",
  130. DEFAULT_DLFLAGS, /* dlopen flags */
  131. cmds, /* Exported functions */
  132. params, /* Exported parameters */
  133. 0, /* exported statistics */
  134. 0, /* exported MI functions */
  135. 0, /* exported pseudo-variables */
  136. 0, /* extra processes */
  137. mod_init, /* module initialization function */
  138. 0, /* response function */
  139. destroy, /* destroy function */
  140. child_init /* child initialization function */
  141. };
  142. static int child_init(int rank)
  143. {
  144. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  145. return 0; /* do nothing for the main process */
  146. auth_db_handle = auth_dbf.init(&db_url);
  147. if (auth_db_handle == 0){
  148. LM_ERR("unable to connect to the database\n");
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static int mod_init(void)
  154. {
  155. bind_auth_s_t bind_auth;
  156. /* Find a database module */
  157. if (db_bind_mod(&db_url, &auth_dbf) < 0){
  158. LM_ERR("unable to bind to a database driver\n");
  159. return -1;
  160. }
  161. /* bind to auth module and import the API */
  162. bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
  163. if (!bind_auth) {
  164. LM_ERR("unable to find bind_auth function. Check if you load"
  165. " the auth module.\n");
  166. return -2;
  167. }
  168. if (bind_auth(&auth_api) < 0) {
  169. LM_ERR("unable to bind auth module\n");
  170. return -3;
  171. }
  172. /* process additional list of credentials */
  173. if (parse_aaa_pvs(credentials_list, &credentials, &credentials_n) != 0) {
  174. LM_ERR("failed to parse credentials\n");
  175. return -5;
  176. }
  177. return 0;
  178. }
  179. static void destroy(void)
  180. {
  181. if (auth_db_handle) {
  182. auth_dbf.close(auth_db_handle);
  183. auth_db_handle = 0;
  184. }
  185. if (credentials) {
  186. pv_elem_free_all(credentials);
  187. credentials = 0;
  188. credentials_n = 0;
  189. }
  190. }
  191. /**
  192. * check if the subscriber identified by _uri has a valid record in
  193. * database table _table
  194. */
  195. static int w_is_subscriber(sip_msg_t *msg, char *_uri, char* _table,
  196. char *_flags)
  197. {
  198. str suri;
  199. str stable;
  200. int iflags;
  201. int ret;
  202. sip_uri_t puri;
  203. if(msg==NULL || _uri==NULL || _table==NULL || _flags==NULL) {
  204. LM_ERR("invalid parameters\n");
  205. return AUTH_ERROR;
  206. }
  207. if (get_str_fparam(&suri, msg, (fparam_t*)_uri) < 0) {
  208. LM_ERR("failed to get uri value\n");
  209. return -1;
  210. }
  211. if (suri.len==0) {
  212. LM_ERR("invalid uri parameter - empty value\n");
  213. return -1;
  214. }
  215. if(parse_uri(suri.s, suri.len, &puri)<0){
  216. LM_ERR("invalid uri parameter format\n");
  217. return -1;
  218. }
  219. if (get_str_fparam(&stable, msg, (fparam_t*)_table) < 0) {
  220. LM_ERR("failed to get table value\n");
  221. return -1;
  222. }
  223. if (stable.len==0) {
  224. LM_ERR("invalid table parameter - empty value\n");
  225. return -1;
  226. }
  227. if(fixup_get_ivalue(msg, (gparam_p)_flags, &iflags)!=0)
  228. {
  229. LM_ERR("invalid flags parameter\n");
  230. return -1;
  231. }
  232. LM_DBG("uri [%.*s] table [%.*s] flags [%d]\n", suri.len, suri.s,
  233. stable.len, stable.s, iflags);
  234. ret = fetch_credentials(msg, &puri.user, (iflags==1)?&puri.host:NULL,
  235. &stable);
  236. if(ret>=0)
  237. return 1;
  238. return ret;
  239. }
  240. /*
  241. * Convert the char* parameters
  242. */
  243. static int auth_fixup(void** param, int param_no)
  244. {
  245. db1_con_t* dbh = NULL;
  246. str name;
  247. if(strlen((char*)*param)<=0) {
  248. LM_ERR("empty parameter %d not allowed\n", param_no);
  249. return -1;
  250. }
  251. if (param_no == 1 || param_no == 3) {
  252. return fixup_var_str_12(param, 1);
  253. } else if (param_no == 2) {
  254. name.s = (char*)*param;
  255. name.len = strlen(name.s);
  256. dbh = auth_dbf.init(&db_url);
  257. if (!dbh) {
  258. LM_ERR("unable to open database connection\n");
  259. return -1;
  260. }
  261. if(version_table_check!=0
  262. && db_check_table_version(&auth_dbf, dbh, &name,
  263. TABLE_VERSION) < 0) {
  264. LM_ERR("error during table version check.\n");
  265. auth_dbf.close(dbh);
  266. return -1;
  267. }
  268. auth_dbf.close(dbh);
  269. }
  270. return 0;
  271. }
  272. /*
  273. * Convert cfg parameters to run-time structures
  274. */
  275. static int auth_check_fixup(void** param, int param_no)
  276. {
  277. if(strlen((char*)*param)<=0) {
  278. LM_ERR("empty parameter %d not allowed\n", param_no);
  279. return -1;
  280. }
  281. if (param_no == 1) {
  282. return fixup_var_str_12(param, 1);
  283. }
  284. if (param_no == 2) {
  285. return fixup_var_str_12(param, 2);
  286. }
  287. if (param_no == 3) {
  288. return fixup_igp_null(param, 1);
  289. }
  290. return 0;
  291. }
  292. /*
  293. * Parse extra credentials list
  294. */
  295. int parse_aaa_pvs(char *definition, pv_elem_t **pv_def, int *cnt)
  296. {
  297. pv_elem_t *pve;
  298. str pv;
  299. char *p;
  300. char *end;
  301. char *sep;
  302. p = definition;
  303. if (p==0 || *p==0)
  304. return 0;
  305. *pv_def = 0;
  306. *cnt = 0;
  307. /* get element by element */
  308. while ( (end=strchr(p,';'))!=0 || (end=p+strlen(p))!=p ) {
  309. /* new pv_elem_t */
  310. if ( (pve=(pv_elem_t*)pkg_malloc(sizeof(pv_elem_t)))==0 ) {
  311. LM_ERR("no more pkg mem\n");
  312. goto error;
  313. }
  314. memset( pve, 0, sizeof(pv_elem_t));
  315. /* definition is between p and e */
  316. /* search backwards because PV definition may contain '=' characters */
  317. for (sep = end; sep >= p && *sep != '='; sep--);
  318. if (sep > p) {
  319. /* pv=column style */
  320. /* set column name */
  321. pve->text.s = sep + 1;
  322. pve->text.len = end - pve->text.s;
  323. trim(&pve->text);
  324. if (pve->text.len == 0)
  325. goto parse_error;
  326. /* set pv spec */
  327. pv.s = p;
  328. pv.len = sep - p;
  329. trim(&pv);
  330. if (pv.len == 0)
  331. goto parse_error;
  332. } else {
  333. /* no pv, only column name */
  334. pve->text.s = p;
  335. pve->text.len = end - pve->text.s;
  336. trim(&pve->text);
  337. if (pve->text.len == 0)
  338. goto parse_error;
  339. /* create an avp definition for the spec parser */
  340. pv.s = (char*)pkg_malloc(pve->text.len + 7);
  341. if (pv.s == NULL) {
  342. LM_ERR("no more pkg mem\n");
  343. goto parse_error;
  344. }
  345. pv.len = snprintf(pv.s, pve->text.len + 7, "$avp(%.*s)",
  346. pve->text.len, pve->text.s);
  347. }
  348. /* create a pv spec */
  349. LM_DBG("column: %.*s pv: %.*s\n", pve->text.len, pve->text.s, pv.len, pv.s);
  350. pve->spec = pv_spec_lookup(&pv, NULL);
  351. if(pve->spec==NULL || pve->spec->setf == NULL) {
  352. LM_ERR("PV is not writeable: %.*s\n", pv.len, pv.s);
  353. goto parse_error;
  354. }
  355. /* link the element */
  356. pve->next = *pv_def;
  357. *pv_def = pve;
  358. (*cnt)++;
  359. pve = 0;
  360. /* go to the end */
  361. p = end;
  362. if (*p==';')
  363. p++;
  364. if (*p==0)
  365. break;
  366. }
  367. return 0;
  368. parse_error:
  369. LM_ERR("parse failed in \"%s\" at pos %d(%s)\n",
  370. definition, (int)(long)(p-definition),p);
  371. error:
  372. pkg_free( pve );
  373. pv_elem_free_all( *pv_def );
  374. *pv_def = 0;
  375. *cnt = 0;
  376. return -1;
  377. }