authims_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Smile Communications, [email protected]
  5. * Copyright (C) 2012 Smile Communications, [email protected]
  6. *
  7. * The initial version of this code was written by Dragos Vingarzan
  8. * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
  9. * Fruanhofer Institute. It was and still is maintained in a separate
  10. * branch of the original SER. We are therefore migrating it to
  11. * Kamailio/SR and look forward to maintaining it from here on out.
  12. * 2011/2012 Smile Communications, Pty. Ltd.
  13. * ported/maintained/improved by
  14. * Jason Penton (jason(dot)penton(at)smilecoms.com and
  15. * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
  16. * effort to add full IMS support to Kamailio/SR using a new and
  17. * improved architecture
  18. *
  19. * NB: Alot of this code was originally part of OpenIMSCore,
  20. * FhG Fokus.
  21. * Copyright (C) 2004-2006 FhG Fokus
  22. * Thanks for great work! This is an effort to
  23. * break apart the various CSCF functions into logically separate
  24. * components. We hope this will drive wider use. We also feel
  25. * that in this way the architecture is more complete and thereby easier
  26. * to manage in the Kamailio/SR environment
  27. *
  28. * This file is part of Kamailio, a free SIP server.
  29. *
  30. * Kamailio is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version
  34. *
  35. * Kamailio is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  43. *
  44. */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include "stats.h"
  48. #include "../../sr_module.h"
  49. #include "../../lib/srdb1/db.h"
  50. #include "../../dprint.h"
  51. #include "../../error.h"
  52. #include "../../mod_fix.h"
  53. #include "../../trim.h"
  54. #include "../../mem/mem.h"
  55. #include "../../modules/sl/sl.h"
  56. #include "../cdp/cdp_load.h"
  57. #include "../tm/tm_load.h"
  58. #include "authorize.h"
  59. #include "authims_mod.h"
  60. #include "cxdx_mar.h"
  61. #include "../../lib/ims/useful_defs.h"
  62. MODULE_VERSION
  63. static void destroy(void);
  64. static int mod_init(void);
  65. static int auth_fixup(void** param, int param_no);
  66. static int auth_fixup_async(void** param, int param_no);
  67. static int challenge_fixup_async(void** param, int param_no);
  68. struct cdp_binds cdpb;
  69. /*! API structures */
  70. struct tm_binds tmb; /**< Structure with pointers to tm funcs */
  71. extern auth_hash_slot_t *auth_data; /**< authentication vectors hast table */
  72. int auth_data_hash_size = 1024; /**< the size of the hash table */
  73. int auth_vector_timeout = 60; /**< timeout for a sent auth vector to expire in sec */
  74. int auth_used_vector_timeout = 3600; /**< timeout for a used auth vector to expire in sec */
  75. int max_nonce_reuse = 0; /**< how many times a nonce can be reused (provided nc is incremented) */
  76. int auth_data_timeout = 60; /**< timeout for a hash entry to expire when empty in sec */
  77. int add_authinfo_hdr = 1; /**< should an Authentication-Info header be added on 200 OK responses? */
  78. int av_request_at_once = 1; /**< how many auth vectors to request in a MAR */
  79. int av_request_at_sync = 1; /**< how many auth vectors to request in a sync MAR */
  80. static str registration_qop = str_init("auth,auth-int"); /**< the qop options to put in the authorization challenges */
  81. str registration_qop_str = STR_NULL; /**< the qop options to put in the authorization challenges */
  82. int av_check_only_impu = 0; /**< Should we check IMPU (0) or IMPU and IMPI (1), when searching for authentication vectors? */
  83. static str s_qop_s = str_init(", qop=\"");
  84. static str s_qop_e = str_init("\"");
  85. static str registration_default_algorithm = str_init("AKAv1-MD5"); /**< default algorithm for registration (if none present)*/
  86. unsigned char registration_default_algorithm_type = 1; /**< fixed default algorithm for registration (if none present) */
  87. str cxdx_dest_realm = str_init("ims.smilecoms.com");
  88. //Only used if we want to force the Rx peer
  89. //Usually this is configured at a stack level and the first request uses realm routing
  90. str cxdx_forced_peer = str_init("");
  91. /* fixed parameter storage */
  92. str scscf_name_str = str_init("sip:scscf.ims.smilecoms.com:6060"); /**< fixed name of the S-CSCF */
  93. /* used mainly in testing - load balancing with SIPP where we don't want to worry about auth */
  94. int ignore_failed_auth = 0;
  95. /*
  96. * Exported functions
  97. */
  98. static cmd_export_t cmds[] = {
  99. {"ims_www_authenticate", (cmd_function) www_authenticate, 1, auth_fixup, 0, REQUEST_ROUTE},
  100. {"ims_www_challenge", (cmd_function) www_challenge2, 2, challenge_fixup_async, 0, REQUEST_ROUTE},
  101. {"ims_www_challenge", (cmd_function) www_challenge3, 3, challenge_fixup_async, 0, REQUEST_ROUTE},
  102. {"ims_www_resync_auth", (cmd_function) www_resync_auth, 2, challenge_fixup_async, 0, REQUEST_ROUTE},
  103. {"ims_proxy_authenticate", (cmd_function) proxy_authenticate, 1, auth_fixup, 0, REQUEST_ROUTE},
  104. {"ims_proxy_challenge", (cmd_function) proxy_challenge, 2, auth_fixup_async, 0, REQUEST_ROUTE},
  105. {"bind_ims_auth", (cmd_function) bind_ims_auth, 0, 0, 0, 0},
  106. {0, 0, 0, 0, 0, 0}
  107. };
  108. /*
  109. * Exported parameters
  110. */
  111. static param_export_t params[] = {
  112. {"name", PARAM_STR, &scscf_name_str},
  113. {"auth_data_hash_size", INT_PARAM, &auth_data_hash_size},
  114. {"auth_vector_timeout", INT_PARAM, &auth_vector_timeout},
  115. {"auth_used_vector_timeout", INT_PARAM, &auth_used_vector_timeout},
  116. {"auth_data_timeout", INT_PARAM, &auth_data_timeout},
  117. {"max_nonce_reuse", INT_PARAM, &max_nonce_reuse},
  118. {"add_authinfo_hdr", INT_PARAM, &add_authinfo_hdr},
  119. {"av_request_at_once", INT_PARAM, &av_request_at_once},
  120. {"av_request_at_sync", INT_PARAM, &av_request_at_sync},
  121. {"registration_default_algorithm", PARAM_STR, &registration_default_algorithm},
  122. {"registration_qop", PARAM_STR, &registration_qop},
  123. {"ignore_failed_auth", INT_PARAM, &ignore_failed_auth},
  124. {"av_check_only_impu", INT_PARAM, &av_check_only_impu},
  125. {"cxdx_forced_peer", PARAM_STR, &cxdx_forced_peer},
  126. {"cxdx_dest_realm", PARAM_STR, &cxdx_dest_realm},
  127. {0, 0, 0}
  128. };
  129. stat_export_t mod_stats[] = {
  130. {"mar_avg_response_time" , STAT_IS_FUNC, (stat_var**)get_avg_mar_response_time },
  131. {"mar_timeouts" , 0, (stat_var**)&stat_mar_timeouts },
  132. {0,0,0}
  133. };
  134. /*
  135. * Module interface
  136. */
  137. struct module_exports exports = {
  138. "ims_auth",
  139. DEFAULT_DLFLAGS, /* dlopen flags */
  140. cmds, /* Exported functions */
  141. params, /* Exported parameters */
  142. 0, /* exported statistics */
  143. 0, /* exported MI functions */
  144. 0, /* exported pseudo-variables */
  145. 0, /* extra processes */
  146. mod_init, /* module initialization function */
  147. 0, /* response function */
  148. destroy, /* destroy function */
  149. 0 /* child initialization function */
  150. };
  151. static int mod_init(void) {
  152. registration_default_algorithm_type = get_algorithm_type(registration_default_algorithm);
  153. #ifdef STATISTICS
  154. /* register statistics */
  155. if (register_module_stats( exports.name, mod_stats)!=0 ) {
  156. LM_ERR("failed to register core statistics\n");
  157. return -1;
  158. }
  159. if (!register_stats()){
  160. LM_ERR("Unable to register statistics\n");
  161. return -1;
  162. }
  163. #endif
  164. /* check the max_nonce_reuse param */
  165. if (auth_used_vector_timeout < 0) {
  166. LM_WARN("bad value for auth_used_vector_timeout parameter (=%d), must be positive. Fixed to 3600\n", auth_used_vector_timeout);
  167. auth_used_vector_timeout = 3600;
  168. }
  169. /* check the max_nonce_reuse param */
  170. if (max_nonce_reuse < 0) {
  171. LM_WARN("bad value for max_nonce_reuse parameter (=%d), must be positive. Fixed to 0\n", max_nonce_reuse);
  172. max_nonce_reuse = 0;
  173. }
  174. /* load the CDP API */
  175. if (load_cdp_api(&cdpb) != 0) {
  176. LM_ERR("can't load CDP API\n");
  177. return -1;
  178. }
  179. /* load the TM API */
  180. if (load_tm_api(&tmb) != 0) {
  181. LM_ERR("can't load TM API\n");
  182. return -1;
  183. }
  184. /* Init the authorization data storage */
  185. if (!auth_data_init(auth_data_hash_size)) {
  186. LM_ERR("Unable to init auth data\n");
  187. return -1;
  188. }
  189. /* set default qop */
  190. if (registration_qop.s && registration_qop.len > 0) {
  191. registration_qop_str.len = s_qop_s.len + registration_qop.len
  192. + s_qop_e.len;
  193. registration_qop_str.s = pkg_malloc(registration_qop_str.len);
  194. if (!registration_qop_str.s) {
  195. LM_ERR("Error allocating %d bytes\n", registration_qop_str.len);
  196. registration_qop_str.len = 0;
  197. return 0;
  198. }
  199. registration_qop_str.len = 0;
  200. STR_APPEND(registration_qop_str, s_qop_s);
  201. memcpy(registration_qop_str.s + registration_qop_str.len,
  202. registration_qop.s, registration_qop.len);
  203. registration_qop_str.len += registration_qop.len;
  204. STR_APPEND(registration_qop_str, s_qop_e);
  205. } else {
  206. registration_qop_str.len = 0;
  207. registration_qop_str.s = 0;
  208. }
  209. /* Register the auth vector timer */
  210. if (register_timer(reg_await_timer, auth_data, 10) < 0) {
  211. LM_ERR("Unable to register auth vector timer\n");
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static void destroy(void) {
  217. auth_data_destroy();
  218. }
  219. /*
  220. * Convert the char* parameters
  221. */
  222. static int challenge_fixup_async(void** param, int param_no) {
  223. if (strlen((char*) *param) <= 0) {
  224. LM_ERR("empty parameter %d not allowed\n", param_no);
  225. return -1;
  226. }
  227. if (param_no == 1) { //route name - static or dynamic string (config vars)
  228. if (fixup_spve_null(param, param_no) < 0)
  229. return -1;
  230. return 0;
  231. } else if (param_no == 2) {
  232. if (fixup_var_str_12(param, 1) == -1) {
  233. LM_ERR("Error doing fixup on challenge");
  234. return -1;
  235. }
  236. } else if (param_no == 3) /* algorithm */ {
  237. if (fixup_var_str_12(param, 1) == -1) {
  238. LM_ERR("Error doing fixup on challenge");
  239. return -1;
  240. }
  241. }
  242. return 0;
  243. }
  244. /*
  245. * Convert the char* parameters
  246. */
  247. static int auth_fixup(void** param, int param_no) {
  248. if (strlen((char*) *param) <= 0) {
  249. LM_ERR("empty parameter %d not allowed\n", param_no);
  250. return -1;
  251. }
  252. if (param_no == 1) {
  253. if (fixup_var_str_12(param, 1) == -1) {
  254. LM_ERR("Erroring doing fixup on auth");
  255. return -1;
  256. }
  257. }
  258. return 0;
  259. }
  260. /*
  261. * Convert the char* parameters
  262. */
  263. static int auth_fixup_async(void** param, int param_no) {
  264. if (strlen((char*) *param) <= 0) {
  265. LM_ERR("empty parameter %d not allowed\n", param_no);
  266. return -1;
  267. }
  268. if (param_no == 1) { //route name - static or dynamic string (config vars)
  269. if (fixup_spve_null(param, param_no) < 0)
  270. return -1;
  271. return 0;
  272. } else if (param_no == 2) {
  273. if (fixup_var_str_12(param, 1) == -1) {
  274. LM_ERR("Erroring doing fixup on auth");
  275. return -1;
  276. }
  277. }
  278. return 0;
  279. }