auth_mod.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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-10 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-28 rpid contributed by Juha Heinanen added (janakj)
  36. * 2007-10-19 auth extra checks: longer nonces that include selected message
  37. * parts to protect against various reply attacks without keeping
  38. * state (andrei)
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <time.h>
  43. #include "../../sr_module.h"
  44. #include "../../dprint.h"
  45. #include "../../mem/mem.h"
  46. #include "../../parser/digest/digest.h"
  47. #include "../../parser/parse_from.h"
  48. #include "../../parser/parse_to.h"
  49. #include "../../parser/parse_uri.h"
  50. #include "../../data_lump.h"
  51. #include "../../data_lump_rpl.h"
  52. #include "../../error.h"
  53. #include "../../ut.h"
  54. #include "../../pvapi.h"
  55. #include "../../lvalue.h"
  56. #include "../../mod_fix.h"
  57. #include "../../modules/sl/sl.h"
  58. #include "auth_mod.h"
  59. #include "challenge.h"
  60. #include "api.h"
  61. #include "nid.h"
  62. #include "nc.h"
  63. #include "ot_nonce.h"
  64. #include "rfc2617.h"
  65. MODULE_VERSION
  66. #define RAND_SECRET_LEN 32
  67. /*
  68. * Module destroy function prototype
  69. */
  70. static void destroy(void);
  71. /*
  72. * Module initialization function prototype
  73. */
  74. static int mod_init(void);
  75. /*
  76. * Remove used credentials from a SIP message header
  77. */
  78. int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2);
  79. /*
  80. * Check for credentials with given realm
  81. */
  82. int w_has_credentials(struct sip_msg* msg, char* s1, char* s2);
  83. static int pv_proxy_authenticate(struct sip_msg* msg, char* realm,
  84. char *passwd, char *flags);
  85. static int pv_www_authenticate(struct sip_msg* msg, char* realm,
  86. char *passwd, char *flags);
  87. static int pv_www_authenticate2(struct sip_msg* msg, char* realm,
  88. char *passwd, char *flags, char *method);
  89. static int fixup_pv_auth(void **param, int param_no);
  90. static int pv_auth_check(sip_msg_t *msg, char *realm,
  91. char *passwd, char *flags, char *checks);
  92. static int fixup_pv_auth_check(void **param, int param_no);
  93. static int proxy_challenge(struct sip_msg *msg, char* realm, char *flags);
  94. static int www_challenge(struct sip_msg *msg, char* realm, char *flags);
  95. static int w_auth_challenge(struct sip_msg *msg, char* realm, char *flags);
  96. static int fixup_auth_challenge(void **param, int param_no);
  97. static int w_auth_get_www_authenticate(sip_msg_t* msg, char* realm,
  98. char *flags, char *dst);
  99. static int fixup_auth_get_www_authenticate(void **param, int param_no);
  100. /*
  101. * Module parameter variables
  102. */
  103. char* sec_param = 0; /* If the parameter was not used, the secret phrase will be auto-generated */
  104. int nonce_expire = 300; /* Nonce lifetime */
  105. /*int auth_extra_checks = 0; -- in nonce.c */
  106. int protect_contacts = 0; /* Do not include contacts in nonce by default */
  107. int force_stateless_reply = 0; /* Always send reply statelessly */
  108. /*! Prefix to strip from realm */
  109. str auth_realm_prefix = {"", 0};
  110. static int auth_use_domain = 0;
  111. str secret1;
  112. str secret2;
  113. char* sec_rand1 = 0;
  114. char* sec_rand2 = 0;
  115. str challenge_attr = STR_STATIC_INIT("$digest_challenge");
  116. avp_ident_t challenge_avpid;
  117. str proxy_challenge_header = STR_STATIC_INIT("Proxy-Authenticate");
  118. str www_challenge_header = STR_STATIC_INIT("WWW-Authenticate");
  119. struct qp auth_qop = {
  120. STR_STATIC_INIT("auth"),
  121. QOP_AUTH
  122. };
  123. static struct qp auth_qauth = {
  124. STR_STATIC_INIT("auth"),
  125. QOP_AUTH
  126. };
  127. static struct qp auth_qauthint = {
  128. STR_STATIC_INIT("auth-int"),
  129. QOP_AUTHINT
  130. };
  131. /*! SL API structure */
  132. sl_api_t slb;
  133. /*
  134. * Exported functions
  135. */
  136. static cmd_export_t cmds[] = {
  137. {"consume_credentials", w_consume_credentials, 0,
  138. 0, REQUEST_ROUTE},
  139. {"www_challenge", (cmd_function)www_challenge, 2,
  140. fixup_auth_challenge, REQUEST_ROUTE},
  141. {"proxy_challenge", (cmd_function)proxy_challenge, 2,
  142. fixup_auth_challenge, REQUEST_ROUTE},
  143. {"auth_challenge", (cmd_function)w_auth_challenge, 2,
  144. fixup_auth_challenge, REQUEST_ROUTE},
  145. {"pv_www_authorize", (cmd_function)pv_www_authenticate, 3,
  146. fixup_pv_auth, REQUEST_ROUTE},
  147. {"pv_www_authenticate", (cmd_function)pv_www_authenticate, 3,
  148. fixup_pv_auth, REQUEST_ROUTE},
  149. {"pv_www_authenticate", (cmd_function)pv_www_authenticate2, 4,
  150. fixup_pv_auth, REQUEST_ROUTE},
  151. {"pv_proxy_authorize", (cmd_function)pv_proxy_authenticate, 3,
  152. fixup_pv_auth, REQUEST_ROUTE},
  153. {"pv_proxy_authenticate", (cmd_function)pv_proxy_authenticate, 3,
  154. fixup_pv_auth, REQUEST_ROUTE},
  155. {"auth_get_www_authenticate", (cmd_function)w_auth_get_www_authenticate, 3,
  156. fixup_auth_get_www_authenticate, REQUEST_ROUTE},
  157. {"has_credentials", w_has_credentials, 1,
  158. fixup_spve_null, REQUEST_ROUTE},
  159. {"pv_auth_check", (cmd_function)pv_auth_check, 4,
  160. fixup_pv_auth_check, REQUEST_ROUTE},
  161. {"bind_auth_s", (cmd_function)bind_auth_s, 0, 0, 0 },
  162. {0, 0, 0, 0, 0}
  163. };
  164. /*
  165. * Exported parameters
  166. */
  167. static param_export_t params[] = {
  168. {"secret", PARAM_STRING, &sec_param },
  169. {"nonce_expire", PARAM_INT, &nonce_expire },
  170. {"nonce_auth_max_drift", PARAM_INT, &nonce_auth_max_drift },
  171. {"protect_contacts", PARAM_INT, &protect_contacts },
  172. {"challenge_attr", PARAM_STR, &challenge_attr },
  173. {"proxy_challenge_header", PARAM_STR, &proxy_challenge_header},
  174. {"www_challenge_header", PARAM_STR, &www_challenge_header },
  175. {"qop", PARAM_STR, &auth_qop.qop_str },
  176. {"auth_checks_register", PARAM_INT, &auth_checks_reg },
  177. {"auth_checks_no_dlg", PARAM_INT, &auth_checks_ood },
  178. {"auth_checks_in_dlg", PARAM_INT, &auth_checks_ind },
  179. {"nonce_count" , PARAM_INT, &nc_enabled },
  180. {"nc_array_size", PARAM_INT, &nc_array_size },
  181. {"nc_array_order", PARAM_INT, &nc_array_k },
  182. {"one_time_nonce" , PARAM_INT, &otn_enabled },
  183. {"otn_in_flight_no", PARAM_INT, &otn_in_flight_no },
  184. {"otn_in_flight_order", PARAM_INT, &otn_in_flight_k },
  185. {"nid_pool_no", PARAM_INT, &nid_pool_no },
  186. {"force_stateless_reply", PARAM_INT, &force_stateless_reply },
  187. {"realm_prefix", PARAM_STRING, &auth_realm_prefix.s },
  188. {"use_domain", PARAM_INT, &auth_use_domain },
  189. {0, 0, 0}
  190. };
  191. /*
  192. * Module interface
  193. */
  194. struct module_exports exports = {
  195. "auth",
  196. cmds,
  197. 0, /* RPC methods */
  198. params,
  199. mod_init, /* module initialization function */
  200. 0, /* response function */
  201. destroy, /* destroy function */
  202. 0, /* oncancel function */
  203. 0 /* child initialization function */
  204. };
  205. /*
  206. * Secret parameter was not used so we generate
  207. * a random value here
  208. */
  209. static inline int generate_random_secret(void)
  210. {
  211. int i;
  212. sec_rand1 = (char*)pkg_malloc(RAND_SECRET_LEN);
  213. sec_rand2 = (char*)pkg_malloc(RAND_SECRET_LEN);
  214. if (!sec_rand1 || !sec_rand2) {
  215. LOG(L_ERR, "auth:generate_random_secret: No memory left\n");
  216. if (sec_rand1){
  217. pkg_free(sec_rand1);
  218. sec_rand1=0;
  219. }
  220. return -1;
  221. }
  222. /* srandom(time(0)); -- seeded by core */
  223. for(i = 0; i < RAND_SECRET_LEN; i++) {
  224. sec_rand1[i] = 32 + (int)(95.0 * rand() / (RAND_MAX + 1.0));
  225. }
  226. secret1.s = sec_rand1;
  227. secret1.len = RAND_SECRET_LEN;
  228. for(i = 0; i < RAND_SECRET_LEN; i++) {
  229. sec_rand2[i] = 32 + (int)(95.0 * rand() / (RAND_MAX + 1.0));
  230. }
  231. secret2.s = sec_rand2;
  232. secret2.len = RAND_SECRET_LEN;
  233. /* DBG("Generated secret: '%.*s'\n", secret.len, secret.s); */
  234. return 0;
  235. }
  236. static int mod_init(void)
  237. {
  238. str attr;
  239. DBG("auth module - initializing\n");
  240. auth_realm_prefix.len = strlen(auth_realm_prefix.s);
  241. /* bind the SL API */
  242. if (sl_load_api(&slb)!=0) {
  243. LM_ERR("cannot bind to SL API\n");
  244. return -1;
  245. }
  246. /* If the parameter was not used */
  247. if (sec_param == 0) {
  248. /* Generate secret using random generator */
  249. if (generate_random_secret() < 0) {
  250. LOG(L_ERR, "auth:mod_init: Error while generating random secret\n");
  251. return -3;
  252. }
  253. } else {
  254. /* Otherwise use the parameter's value */
  255. secret1.s = sec_param;
  256. secret1.len = strlen(secret1.s);
  257. if (auth_checks_reg || auth_checks_ind || auth_checks_ood) {
  258. /* divide the secret in half: one half for secret1 and one half for
  259. * secret2 */
  260. secret2.len = secret1.len/2;
  261. secret1.len -= secret2.len;
  262. secret2.s = secret1.s + secret1.len;
  263. if (secret2.len < 16) {
  264. WARN("auth: consider a longer secret when extra auth checks are"
  265. " enabled (the config secret is divided in 2!)\n");
  266. }
  267. }
  268. }
  269. if ((!challenge_attr.s || challenge_attr.len == 0) ||
  270. challenge_attr.s[0] != '$') {
  271. ERR("auth: Invalid value of challenge_attr module parameter\n");
  272. return -1;
  273. }
  274. attr.s = challenge_attr.s + 1;
  275. attr.len = challenge_attr.len - 1;
  276. if (parse_avp_ident(&attr, &challenge_avpid) < 0) {
  277. ERR("auth: Error while parsing value of challenge_attr module"
  278. " parameter\n");
  279. return -1;
  280. }
  281. parse_qop(&auth_qop);
  282. switch(auth_qop.qop_parsed){
  283. case QOP_OTHER:
  284. ERR("auth: Unsupported qop parameter value\n");
  285. return -1;
  286. case QOP_AUTH:
  287. case QOP_AUTHINT:
  288. if (nc_enabled){
  289. #ifndef USE_NC
  290. WARN("auth: nounce count support enabled from config, but"
  291. " disabled at compile time (recompile with -DUSE_NC)\n");
  292. nc_enabled=0;
  293. #else
  294. if (nid_crt==0)
  295. init_nonce_id();
  296. if (init_nonce_count()!=0)
  297. return -1;
  298. #endif
  299. }
  300. #ifdef USE_NC
  301. else{
  302. INFO("auth: qop set, but nonce-count (nc_enabled) support"
  303. " disabled\n");
  304. }
  305. #endif
  306. break;
  307. default:
  308. if (nc_enabled){
  309. WARN("auth: nonce-count support enabled, but qop not set\n");
  310. nc_enabled=0;
  311. }
  312. break;
  313. }
  314. if (otn_enabled){
  315. #ifdef USE_OT_NONCE
  316. if (nid_crt==0) init_nonce_id();
  317. if (init_ot_nonce()!=0)
  318. return -1;
  319. #else
  320. WARN("auth: one-time-nonce support enabled from config, but "
  321. "disabled at compile time (recompile with -DUSE_OT_NONCE)\n");
  322. otn_enabled=0;
  323. #endif /* USE_OT_NONCE */
  324. }
  325. return 0;
  326. }
  327. static void destroy(void)
  328. {
  329. if (sec_rand1) pkg_free(sec_rand1);
  330. if (sec_rand2) pkg_free(sec_rand2);
  331. #ifdef USE_NC
  332. destroy_nonce_count();
  333. #endif
  334. #ifdef USE_OT_NONCE
  335. destroy_ot_nonce();
  336. #endif
  337. #if defined USE_NC || defined USE_OT_NONCE
  338. destroy_nonce_id();
  339. #endif
  340. }
  341. /*
  342. * Remove used credentials from a SIP message header
  343. */
  344. int consume_credentials(struct sip_msg* msg)
  345. {
  346. struct hdr_field* h;
  347. int len;
  348. /* skip requests that can't be authenticated */
  349. if (msg->REQ_METHOD & (METHOD_ACK|METHOD_CANCEL|METHOD_PRACK))
  350. return -1;
  351. get_authorized_cred(msg->authorization, &h);
  352. if (!h) {
  353. get_authorized_cred(msg->proxy_auth, &h);
  354. if (!h) {
  355. LOG(L_ERR, "auth:consume_credentials: No authorized "
  356. "credentials found (error in scripts)\n");
  357. return -1;
  358. }
  359. }
  360. len = h->len;
  361. if (del_lump(msg, h->name.s - msg->buf, len, 0) == 0) {
  362. LOG(L_ERR, "auth:consume_credentials: Can't remove credentials\n");
  363. return -1;
  364. }
  365. return 1;
  366. }
  367. /**
  368. *
  369. */
  370. int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2)
  371. {
  372. return consume_credentials(msg);
  373. }
  374. /**
  375. *
  376. */
  377. int w_has_credentials(sip_msg_t *msg, char* realm, char* s2)
  378. {
  379. str srealm = {0, 0};
  380. hdr_field_t *hdr = NULL;
  381. int ret;
  382. if (fixup_get_svalue(msg, (gparam_t*)realm, &srealm) < 0) {
  383. LM_ERR("failed to get realm value\n");
  384. return -1;
  385. }
  386. ret = find_credentials(msg, &srealm, HDR_PROXYAUTH_T, &hdr);
  387. if(ret==0) {
  388. LM_DBG("found www credentials with realm [%.*s]\n", srealm.len, srealm.s);
  389. return 1;
  390. }
  391. ret = find_credentials(msg, &srealm, HDR_AUTHORIZATION_T, &hdr);
  392. if(ret==0) {
  393. LM_DBG("found proxy credentials with realm [%.*s]\n", srealm.len, srealm.s);
  394. return 1;
  395. }
  396. LM_DBG("no credentials with realm [%.*s]\n", srealm.len, srealm.s);
  397. return -1;
  398. }
  399. /**
  400. * @brief do WWW-Digest authentication with password taken from cfg var
  401. */
  402. int pv_authenticate(struct sip_msg *msg, str *realm, str *passwd,
  403. int flags, int hftype, str *method)
  404. {
  405. struct hdr_field* h;
  406. auth_body_t* cred;
  407. int ret;
  408. str hf = {0, 0};
  409. avp_value_t val;
  410. static char ha1[256];
  411. struct qp *qop = NULL;
  412. cred = 0;
  413. ret = AUTH_ERROR;
  414. switch(pre_auth(msg, realm, hftype, &h, NULL)) {
  415. case NONCE_REUSED:
  416. LM_DBG("nonce reused");
  417. ret = AUTH_NONCE_REUSED;
  418. goto end;
  419. case STALE_NONCE:
  420. LM_DBG("stale nonce\n");
  421. ret = AUTH_STALE_NONCE;
  422. goto end;
  423. case NO_CREDENTIALS:
  424. LM_DBG("no credentials\n");
  425. ret = AUTH_NO_CREDENTIALS;
  426. goto end;
  427. case ERROR:
  428. case BAD_CREDENTIALS:
  429. LM_DBG("error or bad credentials\n");
  430. ret = AUTH_ERROR;
  431. goto end;
  432. case CREATE_CHALLENGE:
  433. LM_ERR("CREATE_CHALLENGE is not a valid state\n");
  434. ret = AUTH_ERROR;
  435. goto end;
  436. case DO_RESYNCHRONIZATION:
  437. LM_ERR("DO_RESYNCHRONIZATION is not a valid state\n");
  438. ret = AUTH_ERROR;
  439. goto end;
  440. case NOT_AUTHENTICATED:
  441. LM_DBG("not authenticated\n");
  442. ret = AUTH_ERROR;
  443. goto end;
  444. case DO_AUTHENTICATION:
  445. break;
  446. case AUTHENTICATED:
  447. ret = AUTH_OK;
  448. goto end;
  449. }
  450. cred = (auth_body_t*)h->parsed;
  451. /* compute HA1 if needed */
  452. if ((flags&1)==0) {
  453. /* Plaintext password is stored in PV, calculate HA1 */
  454. calc_HA1(HA_MD5, &cred->digest.username.whole, realm,
  455. passwd, 0, 0, ha1);
  456. LM_DBG("HA1 string calculated: %s\n", ha1);
  457. } else {
  458. memcpy(ha1, passwd->s, passwd->len);
  459. ha1[passwd->len] = '\0';
  460. }
  461. /* Recalculate response, it must be same to authorize successfully */
  462. ret = auth_check_response(&(cred->digest), method, ha1);
  463. if(ret==AUTHENTICATED) {
  464. ret = AUTH_OK;
  465. switch(post_auth(msg, h)) {
  466. case AUTHENTICATED:
  467. break;
  468. default:
  469. ret = AUTH_ERROR;
  470. break;
  471. }
  472. } else {
  473. if(ret==NOT_AUTHENTICATED)
  474. ret = AUTH_INVALID_PASSWORD;
  475. else
  476. ret = AUTH_ERROR;
  477. }
  478. end:
  479. if (ret < 0) {
  480. /* check if required to add challenge header as avp */
  481. if(!(flags&14))
  482. return ret;
  483. if(flags&8) {
  484. qop = &auth_qauthint;
  485. } else if(flags&4) {
  486. qop = &auth_qauth;
  487. }
  488. if (get_challenge_hf(msg, (cred ? cred->stale : 0),
  489. realm, NULL, NULL, qop, hftype, &hf) < 0) {
  490. ERR("Error while creating challenge\n");
  491. ret = AUTH_ERROR;
  492. } else {
  493. val.s = hf;
  494. if(add_avp(challenge_avpid.flags | AVP_VAL_STR,
  495. challenge_avpid.name, val) < 0) {
  496. LM_ERR("Error while creating attribute with challenge\n");
  497. ret = AUTH_ERROR;
  498. }
  499. pkg_free(hf.s);
  500. }
  501. }
  502. return ret;
  503. }
  504. /**
  505. *
  506. */
  507. static int pv_proxy_authenticate(struct sip_msg *msg, char* realm,
  508. char *passwd, char *flags)
  509. {
  510. int vflags = 0;
  511. str srealm = {0, 0};
  512. str spasswd = {0, 0};
  513. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  514. LM_ERR("failed to get realm value\n");
  515. goto error;
  516. }
  517. if(srealm.len==0) {
  518. LM_ERR("invalid realm value - empty content\n");
  519. goto error;
  520. }
  521. if (get_str_fparam(&spasswd, msg, (fparam_t*)passwd) < 0) {
  522. LM_ERR("failed to get passwd value\n");
  523. goto error;
  524. }
  525. if(spasswd.len==0) {
  526. LM_ERR("invalid password value - empty content\n");
  527. goto error;
  528. }
  529. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  530. LM_ERR("invalid flags value\n");
  531. goto error;
  532. }
  533. return pv_authenticate(msg, &srealm, &spasswd, vflags, HDR_PROXYAUTH_T,
  534. &msg->first_line.u.request.method);
  535. error:
  536. return AUTH_ERROR;
  537. }
  538. /**
  539. *
  540. */
  541. static int pv_www_authenticate(struct sip_msg *msg, char* realm,
  542. char *passwd, char *flags)
  543. {
  544. int vflags = 0;
  545. str srealm = {0, 0};
  546. str spasswd = {0, 0};
  547. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  548. LM_ERR("failed to get realm value\n");
  549. goto error;
  550. }
  551. if(srealm.len==0) {
  552. LM_ERR("invalid realm value - empty content\n");
  553. goto error;
  554. }
  555. if (get_str_fparam(&spasswd, msg, (fparam_t*)passwd) < 0) {
  556. LM_ERR("failed to get passwd value\n");
  557. goto error;
  558. }
  559. if(spasswd.len==0) {
  560. LM_ERR("invalid password value - empty content\n");
  561. goto error;
  562. }
  563. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  564. LM_ERR("invalid flags value\n");
  565. goto error;
  566. }
  567. return pv_authenticate(msg, &srealm, &spasswd, vflags, HDR_AUTHORIZATION_T,
  568. &msg->first_line.u.request.method);
  569. error:
  570. return AUTH_ERROR;
  571. }
  572. static int pv_www_authenticate2(struct sip_msg *msg, char* realm,
  573. char *passwd, char *flags, char *method)
  574. {
  575. int vflags = 0;
  576. str srealm = {0, 0};
  577. str spasswd = {0, 0};
  578. str smethod = {0, 0};
  579. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  580. LM_ERR("failed to get realm value\n");
  581. goto error;
  582. }
  583. if(srealm.len==0) {
  584. LM_ERR("invalid realm value - empty content\n");
  585. goto error;
  586. }
  587. if (get_str_fparam(&spasswd, msg, (fparam_t*)passwd) < 0) {
  588. LM_ERR("failed to get passwd value\n");
  589. goto error;
  590. }
  591. if(spasswd.len==0) {
  592. LM_ERR("invalid password value - empty content\n");
  593. goto error;
  594. }
  595. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  596. LM_ERR("invalid flags value\n");
  597. goto error;
  598. }
  599. if (get_str_fparam(&smethod, msg, (fparam_t*)method) < 0) {
  600. LM_ERR("failed to get method value from msg %p var %p\n", msg, method);
  601. goto error;
  602. }
  603. if(smethod.len==0) {
  604. LM_ERR("invalid method value - empty content\n");
  605. goto error;
  606. }
  607. return pv_authenticate(msg, &srealm, &spasswd, vflags, HDR_AUTHORIZATION_T,
  608. &smethod);
  609. error:
  610. return AUTH_ERROR;
  611. }
  612. /**
  613. *
  614. */
  615. static int pv_auth_check(sip_msg_t *msg, char *realm,
  616. char *passwd, char *flags, char *checks)
  617. {
  618. int vflags = 0;
  619. int vchecks = 0;
  620. str srealm = {0, 0};
  621. str spasswd = {0, 0};
  622. int ret;
  623. hdr_field_t *hdr;
  624. sip_uri_t *uri = NULL;
  625. sip_uri_t *turi = NULL;
  626. sip_uri_t *furi = NULL;
  627. if(msg==NULL) {
  628. LM_ERR("invalid msg parameter\n");
  629. return AUTH_ERROR;
  630. }
  631. if ((msg->REQ_METHOD == METHOD_ACK) || (msg->REQ_METHOD == METHOD_CANCEL)) {
  632. return AUTH_OK;
  633. }
  634. if(realm==NULL || passwd==NULL || flags==NULL || checks==NULL) {
  635. LM_ERR("invalid parameters\n");
  636. return AUTH_ERROR;
  637. }
  638. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  639. LM_ERR("failed to get realm value\n");
  640. return AUTH_ERROR;
  641. }
  642. if(srealm.len==0) {
  643. LM_ERR("invalid realm value - empty content\n");
  644. return AUTH_ERROR;
  645. }
  646. if (get_str_fparam(&spasswd, msg, (fparam_t*)passwd) < 0) {
  647. LM_ERR("failed to get passwd value\n");
  648. return AUTH_ERROR;
  649. }
  650. if(spasswd.len==0) {
  651. LM_ERR("invalid password value - empty content\n");
  652. return AUTH_ERROR;
  653. }
  654. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  655. LM_ERR("invalid flags value\n");
  656. return AUTH_ERROR;
  657. }
  658. if (get_int_fparam(&vchecks, msg, (fparam_t*)checks) < 0) {
  659. LM_ERR("invalid checks value\n");
  660. return AUTH_ERROR;
  661. }
  662. LM_DBG("realm [%.*s] flags [%d] checks [%d]\n", srealm.len, srealm.s,
  663. vflags, vchecks);
  664. if(msg->REQ_METHOD==METHOD_REGISTER)
  665. ret = pv_authenticate(msg, &srealm, &spasswd, vflags, HDR_AUTHORIZATION_T,
  666. &msg->first_line.u.request.method);
  667. else
  668. ret = pv_authenticate(msg, &srealm, &spasswd, vflags, HDR_PROXYAUTH_T,
  669. &msg->first_line.u.request.method);
  670. if(ret==AUTH_OK && (vflags&AUTH_CHECK_ID_F)) {
  671. hdr = (msg->proxy_auth==0)?msg->authorization:msg->proxy_auth;
  672. srealm = ((auth_body_t*)(hdr->parsed))->digest.username.user;
  673. if((furi=parse_from_uri(msg))==NULL)
  674. return AUTH_ERROR;
  675. if(msg->REQ_METHOD==METHOD_REGISTER || msg->REQ_METHOD==METHOD_PUBLISH) {
  676. if((turi=parse_to_uri(msg))==NULL)
  677. return AUTH_ERROR;
  678. uri = turi;
  679. } else {
  680. uri = furi;
  681. }
  682. if(srealm.len!=uri->user.len
  683. || strncmp(srealm.s, uri->user.s, srealm.len)!=0)
  684. return AUTH_USER_MISMATCH;
  685. if(msg->REQ_METHOD==METHOD_REGISTER || msg->REQ_METHOD==METHOD_PUBLISH) {
  686. /* check from==to */
  687. if(furi->user.len!=turi->user.len
  688. || strncmp(furi->user.s, turi->user.s, furi->user.len)!=0)
  689. return AUTH_USER_MISMATCH;
  690. if(auth_use_domain!=0 && (furi->host.len!=turi->host.len
  691. || strncmp(furi->host.s, turi->host.s, furi->host.len)!=0))
  692. return AUTH_USER_MISMATCH;
  693. /* check r-uri==from for publish */
  694. if(msg->REQ_METHOD==METHOD_PUBLISH) {
  695. if(parse_sip_msg_uri(msg)<0)
  696. return AUTH_ERROR;
  697. uri = &msg->parsed_uri;
  698. if(furi->user.len!=uri->user.len
  699. || strncmp(furi->user.s, uri->user.s, furi->user.len)!=0)
  700. return AUTH_USER_MISMATCH;
  701. if(auth_use_domain!=0 && (furi->host.len!=uri->host.len
  702. || strncmp(furi->host.s, uri->host.s, furi->host.len)!=0))
  703. return AUTH_USER_MISMATCH;
  704. }
  705. }
  706. return AUTH_OK;
  707. }
  708. return ret;
  709. }
  710. /**
  711. * @brief fixup function for pv_{www,proxy}_authenticate
  712. */
  713. static int fixup_pv_auth(void **param, int param_no)
  714. {
  715. if(strlen((char*)*param)<=0) {
  716. LM_ERR("empty parameter %d not allowed\n", param_no);
  717. return -1;
  718. }
  719. switch(param_no) {
  720. case 1:
  721. case 2:
  722. case 4:
  723. return fixup_var_pve_str_12(param, 1);
  724. case 3:
  725. return fixup_var_int_12(param, 1);
  726. }
  727. return 0;
  728. }
  729. /**
  730. * @brief fixup function for pv_{www,proxy}_authenticate
  731. */
  732. static int fixup_pv_auth_check(void **param, int param_no)
  733. {
  734. if(strlen((char*)*param)<=0) {
  735. LM_ERR("empty parameter %d not allowed\n", param_no);
  736. return -1;
  737. }
  738. switch(param_no) {
  739. case 1:
  740. case 2:
  741. return fixup_var_pve_str_12(param, 1);
  742. case 3:
  743. case 4:
  744. return fixup_var_int_12(param, 1);
  745. }
  746. return 0;
  747. }
  748. /**
  749. *
  750. */
  751. static int auth_send_reply(struct sip_msg *msg, int code, char *reason,
  752. char *hdr, int hdr_len)
  753. {
  754. str reason_str;
  755. /* Add new headers if there are any */
  756. if ((hdr!=NULL) && (hdr_len>0)) {
  757. if (add_lump_rpl(msg, hdr, hdr_len, LUMP_RPL_HDR)==0) {
  758. LM_ERR("failed to append hdr to reply\n");
  759. return -1;
  760. }
  761. }
  762. reason_str.s = reason;
  763. reason_str.len = strlen(reason);
  764. return force_stateless_reply ?
  765. slb.sreply(msg, code, &reason_str) :
  766. slb.freply(msg, code, &reason_str);
  767. }
  768. /**
  769. *
  770. */
  771. int auth_challenge_helper(struct sip_msg *msg, str *realm, int flags, int hftype,
  772. str *res)
  773. {
  774. int ret, stale;
  775. str hf = {0, 0};
  776. struct qp *qop = NULL;
  777. ret = -1;
  778. if(flags&2) {
  779. qop = &auth_qauthint;
  780. } else if(flags&1) {
  781. qop = &auth_qauth;
  782. }
  783. if (flags & 16) {
  784. stale = 1;
  785. } else {
  786. stale = 0;
  787. }
  788. if (get_challenge_hf(msg, stale, realm, NULL, NULL, qop, hftype, &hf)
  789. < 0) {
  790. ERR("Error while creating challenge\n");
  791. ret = -2;
  792. goto error;
  793. }
  794. ret = 1;
  795. if(res!=NULL)
  796. {
  797. *res = hf;
  798. return ret;
  799. }
  800. switch(hftype) {
  801. case HDR_AUTHORIZATION_T:
  802. if(auth_send_reply(msg, 401, "Unauthorized",
  803. hf.s, hf.len) <0 )
  804. ret = -3;
  805. break;
  806. case HDR_PROXYAUTH_T:
  807. if(auth_send_reply(msg, 407, "Proxy Authentication Required",
  808. hf.s, hf.len) <0 )
  809. ret = -3;
  810. break;
  811. }
  812. if(hf.s) pkg_free(hf.s);
  813. return ret;
  814. error:
  815. if(hf.s) pkg_free(hf.s);
  816. if(!(flags&4)) {
  817. if(auth_send_reply(msg, 500, "Internal Server Error", 0, 0) <0 )
  818. ret = -4;
  819. }
  820. return ret;
  821. }
  822. /**
  823. *
  824. */
  825. int auth_challenge(struct sip_msg *msg, str *realm, int flags, int hftype)
  826. {
  827. return auth_challenge_helper(msg, realm, flags, hftype, NULL);
  828. }
  829. /**
  830. *
  831. */
  832. static int proxy_challenge(struct sip_msg *msg, char* realm, char *flags)
  833. {
  834. int vflags = 0;
  835. str srealm = {0, 0};
  836. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  837. LM_ERR("failed to get realm value\n");
  838. goto error;
  839. }
  840. if(srealm.len==0) {
  841. LM_ERR("invalid realm value - empty content\n");
  842. goto error;
  843. }
  844. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  845. LM_ERR("invalid flags value\n");
  846. goto error;
  847. }
  848. return auth_challenge(msg, &srealm, vflags, HDR_PROXYAUTH_T);
  849. error:
  850. if(!(vflags&4)) {
  851. if(auth_send_reply(msg, 500, "Internal Server Error", 0, 0) <0 )
  852. return -4;
  853. }
  854. return -1;
  855. }
  856. /**
  857. *
  858. */
  859. static int www_challenge(struct sip_msg *msg, char* realm, char *flags)
  860. {
  861. int vflags = 0;
  862. str srealm = {0, 0};
  863. if (get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  864. LM_ERR("failed to get realm value\n");
  865. goto error;
  866. }
  867. if(srealm.len==0) {
  868. LM_ERR("invalid realm value - empty content\n");
  869. goto error;
  870. }
  871. if (get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  872. LM_ERR("invalid flags value\n");
  873. goto error;
  874. }
  875. return auth_challenge(msg, &srealm, vflags, HDR_AUTHORIZATION_T);
  876. error:
  877. if(!(vflags&4)) {
  878. if(auth_send_reply(msg, 500, "Internal Server Error", 0, 0) <0 )
  879. return -4;
  880. }
  881. return -1;
  882. }
  883. /**
  884. *
  885. */
  886. static int w_auth_challenge(struct sip_msg *msg, char* realm, char *flags)
  887. {
  888. int vflags = 0;
  889. str srealm = {0, 0};
  890. if((msg->REQ_METHOD == METHOD_ACK) || (msg->REQ_METHOD == METHOD_CANCEL)) {
  891. return 1;
  892. }
  893. if(get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  894. LM_ERR("failed to get realm value\n");
  895. goto error;
  896. }
  897. if(srealm.len==0) {
  898. LM_ERR("invalid realm value - empty content\n");
  899. goto error;
  900. }
  901. if(get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  902. LM_ERR("invalid flags value\n");
  903. goto error;
  904. }
  905. if(msg->REQ_METHOD==METHOD_REGISTER)
  906. return auth_challenge(msg, &srealm, vflags, HDR_AUTHORIZATION_T);
  907. else
  908. return auth_challenge(msg, &srealm, vflags, HDR_PROXYAUTH_T);
  909. error:
  910. if(!(vflags&4)) {
  911. if(auth_send_reply(msg, 500, "Internal Server Error", 0, 0) <0 )
  912. return -4;
  913. }
  914. return -1;
  915. }
  916. /**
  917. * @brief fixup function for {www,proxy}_challenge
  918. */
  919. static int fixup_auth_challenge(void **param, int param_no)
  920. {
  921. if(strlen((char*)*param)<=0) {
  922. LM_ERR("empty parameter %d not allowed\n", param_no);
  923. return -1;
  924. }
  925. switch(param_no) {
  926. case 1:
  927. return fixup_var_str_12(param, 1);
  928. case 2:
  929. return fixup_var_int_12(param, 1);
  930. }
  931. return 0;
  932. }
  933. /**
  934. *
  935. */
  936. static int w_auth_get_www_authenticate(sip_msg_t* msg, char* realm,
  937. char *flags, char *dst)
  938. {
  939. int vflags = 0;
  940. str srealm = {0};
  941. str hf = {0};
  942. pv_spec_t *pv;
  943. pv_value_t val;
  944. int ret;
  945. if(get_str_fparam(&srealm, msg, (fparam_t*)realm) < 0) {
  946. LM_ERR("failed to get realm value\n");
  947. goto error;
  948. }
  949. if(srealm.len==0) {
  950. LM_ERR("invalid realm value - empty content\n");
  951. goto error;
  952. }
  953. if(get_int_fparam(&vflags, msg, (fparam_t*)flags) < 0) {
  954. LM_ERR("invalid flags value\n");
  955. goto error;
  956. }
  957. pv = (pv_spec_t *)dst;
  958. ret = auth_challenge_helper(NULL, &srealm, vflags,
  959. HDR_AUTHORIZATION_T, &hf);
  960. if(ret<0)
  961. return ret;
  962. val.rs.s = pv_get_buffer();
  963. val.rs.len = 0;
  964. if(hf.s!=NULL)
  965. {
  966. memcpy(val.rs.s, hf.s, hf.len);
  967. val.rs.len = hf.len;
  968. val.rs.s[val.rs.len] = '\0';
  969. pkg_free(hf.s);
  970. }
  971. val.flags = PV_VAL_STR;
  972. pv->setf(msg, &pv->pvp, (int)EQ_T, &val);
  973. return ret;
  974. error:
  975. return -1;
  976. }
  977. static int fixup_auth_get_www_authenticate(void **param, int param_no)
  978. {
  979. if(strlen((char*)*param)<=0) {
  980. LM_ERR("empty parameter %d not allowed\n", param_no);
  981. return -1;
  982. }
  983. switch(param_no) {
  984. case 1:
  985. return fixup_var_str_12(param, 1);
  986. case 2:
  987. return fixup_var_int_12(param, 1);
  988. case 3:
  989. if (fixup_pvar_null(param, 1) != 0) {
  990. LM_ERR("failed to fixup result pvar\n");
  991. return -1;
  992. }
  993. if (((pv_spec_t *)(*param))->setf == NULL) {
  994. LM_ERR("result pvar is not writeble\n");
  995. return -1;
  996. }
  997. return 0;
  998. }
  999. return 0;
  1000. }