uid_auth_db_mod.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication Module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History:
  30. * --------
  31. * 2003-02-26: checks and group moved to separate modules (janakj)
  32. * 2003-03-11: New module interface (janakj)
  33. * 2003-03-16: flags export parameter added (janakj)
  34. * 2003-03-19 all mallocs/frees replaced w/ pkg_malloc/pkg_free (andrei)
  35. * 2003-04-05: default_uri #define used (jiri)
  36. * 2004-06-06 cleanup: static & auth_db_{init,bind,close.ver} used (andrei)
  37. */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "../../sr_module.h"
  41. #include "../../lib/srdb2/db.h"
  42. #include "../../dprint.h"
  43. #include "../../error.h"
  44. #include "../../mem/mem.h"
  45. #include "../../modules/auth/api.h"
  46. #include "authorize.h"
  47. #include "aaa_avps.h"
  48. #include "uid_auth_db_mod.h"
  49. MODULE_VERSION
  50. #define TABLE_VERSION 7
  51. /*
  52. * Module destroy function prototype
  53. */
  54. static void destroy(void);
  55. /*
  56. * Module child-init function prototype
  57. */
  58. static int child_init(int rank);
  59. /*
  60. * Module initialization function prototype
  61. */
  62. static int mod_init(void);
  63. static int authdb_fixup(void** param, int param_no);
  64. #define USERNAME_COL "auth_username"
  65. #define DID_COL "did"
  66. #define REALM_COL "realm"
  67. #define PASS_COL "ha1"
  68. #define PASS_COL_2 "ha1b"
  69. #define PLAIN_PASS_COL "password"
  70. #define DEFAULT_CRED_LIST "uid"
  71. #define FLAGS_COL "flags"
  72. /*
  73. * Module parameter variables
  74. */
  75. static char* db_url = DEFAULT_RODB_URL;
  76. str username_column = STR_STATIC_INIT(USERNAME_COL);
  77. str did_column = STR_STATIC_INIT(DID_COL);
  78. str realm_column = STR_STATIC_INIT(REALM_COL);
  79. str pass_column = STR_STATIC_INIT(PASS_COL);
  80. str pass_column_2 = STR_STATIC_INIT(PASS_COL_2);
  81. str flags_column = STR_STATIC_INIT(FLAGS_COL);
  82. str plain_password_column = STR_STATIC_INIT(PLAIN_PASS_COL);
  83. int calc_ha1 = 0;
  84. int use_did = 0;
  85. int check_all = 0;
  86. db_ctx_t* auth_db_handle = 0; /* database connection handle */
  87. auth_api_s_t auth_api;
  88. str credentials_list = STR_STATIC_INIT(DEFAULT_CRED_LIST);
  89. str* credentials; /* Parsed list of credentials to load */
  90. int credentials_n; /* Number of credentials in the list */
  91. /*
  92. * Exported functions
  93. */
  94. static cmd_export_t cmds[] = {
  95. {"www_authenticate", www_authenticate, 2, authdb_fixup, REQUEST_ROUTE},
  96. {"www_authorize", www_authenticate, 2, authdb_fixup, REQUEST_ROUTE},
  97. {"proxy_authenticate", proxy_authenticate, 2, authdb_fixup, REQUEST_ROUTE},
  98. {"proxy_authorize", proxy_authenticate, 2, authdb_fixup, REQUEST_ROUTE},
  99. {0, 0, 0, 0, 0}
  100. };
  101. /*
  102. * Exported parameters
  103. */
  104. static param_export_t params[] = {
  105. {"db_url", PARAM_STRING, &db_url },
  106. {"username_column", PARAM_STR, &username_column },
  107. {"did_column", PARAM_STR, &did_column },
  108. {"realm_column", PARAM_STR, &realm_column },
  109. {"password_column", PARAM_STR, &pass_column },
  110. {"password_column_2", PARAM_STR, &pass_column_2 },
  111. {"plain_password_column", PARAM_STR, &plain_password_column },
  112. {"flags_column", PARAM_STR, &flags_column },
  113. {"calculate_ha1", PARAM_INT, &calc_ha1 },
  114. {"load_credentials", PARAM_STR, &credentials_list},
  115. {"use_did", PARAM_INT, &use_did },
  116. {"check_all_ha1", PARAM_INT, &check_all },
  117. {0, 0, 0}
  118. };
  119. /*
  120. * Module interface
  121. */
  122. struct module_exports exports = {
  123. "uid_auth_db",
  124. cmds, /* Exported functions */
  125. 0, /* RPC methods */
  126. params, /* Exported parameters */
  127. mod_init, /* module initialization function */
  128. 0, /* response function */
  129. destroy, /* destroy function */
  130. 0, /* oncancel function */
  131. child_init /* child initialization function */
  132. };
  133. static authdb_table_info_t *registered_tables = NULL;
  134. static int generate_queries(authdb_table_info_t *info)
  135. {
  136. db_fld_t match_with_did[] = {
  137. { .name = username_column.s, .type = DB_STR },
  138. { .name = realm_column.s, .type = DB_STR },
  139. { .name = did_column.s, .type = DB_STR },
  140. { .name = NULL }
  141. };
  142. db_fld_t match_without_did[] = {
  143. { .name = username_column.s, .type = DB_STR },
  144. { .name = realm_column.s, .type = DB_STR },
  145. { .name = NULL }
  146. };
  147. db_fld_t *result_cols = NULL;
  148. int len, i;
  149. len = sizeof(*result_cols) * (credentials_n + 3);
  150. result_cols = pkg_malloc(len);
  151. if (!result_cols) {
  152. ERR("can't allocate pkg mem\n");
  153. return -1;
  154. }
  155. memset(result_cols, 0, len);
  156. result_cols[0].name = pass_column.s;
  157. result_cols[0].type = DB_CSTR;
  158. result_cols[1].name = flags_column.s;
  159. result_cols[1].type = DB_INT;
  160. for (i = 0; i < credentials_n; i++) {
  161. result_cols[2 + i].name = credentials[i].s;
  162. result_cols[2 + i].type = DB_STR;
  163. }
  164. result_cols[2 + i].name = NULL;
  165. if (use_did) {
  166. info->query_pass = db_cmd(DB_GET, auth_db_handle, info->table.s,
  167. result_cols, match_with_did, NULL);
  168. result_cols[0].name = pass_column_2.s;
  169. info->query_pass2 = db_cmd(DB_GET, auth_db_handle, info->table.s,
  170. result_cols, match_with_did, NULL);
  171. result_cols[0].name = plain_password_column.s;
  172. info->query_password = db_cmd(DB_GET, auth_db_handle, info->table.s,
  173. result_cols, match_with_did, NULL);
  174. }
  175. else {
  176. info->query_pass = db_cmd(DB_GET, auth_db_handle, info->table.s,
  177. result_cols, match_without_did, NULL);
  178. result_cols[0].name = pass_column_2.s;
  179. info->query_pass2 = db_cmd(DB_GET, auth_db_handle, info->table.s,
  180. result_cols, match_without_did, NULL);
  181. result_cols[0].name = plain_password_column.s;
  182. info->query_password = db_cmd(DB_GET, auth_db_handle, info->table.s,
  183. result_cols, match_without_did, NULL);
  184. }
  185. pkg_free(result_cols);
  186. if (info->query_pass && info->query_pass2 && info->query_password) return 0;
  187. else return -1;
  188. }
  189. static int child_init(int rank)
  190. {
  191. authdb_table_info_t *i;
  192. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  193. return 0; /* do nothing for the main process */
  194. auth_db_handle = db_ctx("auth_db");
  195. if (!auth_db_handle) goto err;
  196. if (db_add_db(auth_db_handle, db_url) < 0) goto err;
  197. if (db_connect(auth_db_handle) < 0) goto err;
  198. /* initializing queries */
  199. i = registered_tables;
  200. while (i) {
  201. if (generate_queries(i) < 0) {
  202. ERR("can't prepare queries\n");
  203. return -1;
  204. }
  205. i = i->next;
  206. }
  207. return 0;
  208. err:
  209. if (auth_db_handle) {
  210. auth_db_handle = NULL;
  211. db_ctx_free(auth_db_handle);
  212. }
  213. ERR("Error while initializing database layer\n");
  214. return -1;
  215. }
  216. static int mod_init(void)
  217. {
  218. bind_auth_s_t bind_auth;
  219. DBG("auth_db module - initializing\n");
  220. bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
  221. if (!bind_auth) {
  222. LOG(L_ERR, "auth_db:mod_init: Unable to find bind_auth function\n");
  223. return -1;
  224. }
  225. if (bind_auth(&auth_api) < 0) {
  226. LOG(L_ERR, "auth_db:child_init: Unable to bind auth module\n");
  227. return -3;
  228. }
  229. if (aaa_avps_init(&credentials_list, &credentials, &credentials_n)) {
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. static void destroy(void)
  235. {
  236. if (auth_db_handle) {
  237. db_ctx_free(auth_db_handle);
  238. auth_db_handle = NULL;
  239. }
  240. }
  241. static int str_case_equals(const str *a, const str *b)
  242. {
  243. /* ugly hack: taken from libcds */
  244. int i;
  245. if (!a) {
  246. if (!b) return 0;
  247. else return (b->len == 0) ? 0 : 1;
  248. }
  249. if (!b) return (a->len == 0) ? 0 : 1;
  250. if (a->len != b->len) return 1;
  251. for (i = 0; i < a->len; i++)
  252. if (a->s[i] != b->s[i]) return 1;
  253. return 0;
  254. }
  255. static authdb_table_info_t *find_table_info(str *table)
  256. {
  257. authdb_table_info_t *i = registered_tables;
  258. /* sequential search is OK because it is called only in child init */
  259. while (i) {
  260. if (str_case_equals(&i->table, table) == 0) return i;
  261. i = i->next;
  262. }
  263. return NULL;
  264. }
  265. static authdb_table_info_t *register_table(str *table)
  266. {
  267. authdb_table_info_t *info;
  268. info = find_table_info(table);
  269. if (info) return info; /* queries for this table already exist */
  270. info = (authdb_table_info_t*)pkg_malloc(sizeof(authdb_table_info_t) + table->len + 1);
  271. if (!info) {
  272. ERR("can't allocate pkg mem\n");
  273. return NULL;
  274. }
  275. info->table.s = info->buf;
  276. info->table.len = table->len;
  277. memcpy(info->table.s, table->s, table->len);
  278. info->table.s[table->len] = 0;
  279. /* append to the begining (we don't care about order) */
  280. info->next = registered_tables;
  281. registered_tables = info;
  282. return info;
  283. }
  284. /*
  285. * Convert char* parameter to str* parameter
  286. */
  287. static int authdb_fixup(void** param, int param_no)
  288. {
  289. fparam_t* p;
  290. if (param_no == 1) {
  291. return fixup_var_str_12(param, param_no);
  292. } else if (param_no == 2) {
  293. if (fixup_var_str_12(param, param_no) < 0) return -1;
  294. p = (fparam_t*)(*param);
  295. if (p->type == FPARAM_STR) {
  296. *param = register_table(&p->v.str);
  297. if (!*param) {
  298. ERR("can't register table %.*s\n", p->v.str.len, p->v.str.s);
  299. return -1;
  300. }
  301. } else {
  302. ERR("Non-string value of table with credentials is not allowed.\n");
  303. /* TODO: allow this too */
  304. return -1;
  305. }
  306. }
  307. return 0;
  308. }