authorize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication - Database support
  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-28 scratchpad compatibility abandoned
  32. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  33. * 2004-06-06 updated to the new DB api, added auth_db_{init,bind,close,ver}
  34. * (andrei)
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "../../ut.h"
  40. #include "../../str.h"
  41. #include "../../lib/srdb2/db.h"
  42. #include "../../dprint.h"
  43. #include "../../parser/digest/digest.h"
  44. #include "../../parser/hf.h"
  45. #include "../../parser/parser_f.h"
  46. #include "../../usr_avp.h"
  47. #include "../../mem/mem.h"
  48. #include "../../config.h"
  49. #include "../../id.h"
  50. #include "../../sr_module.h"
  51. #include "../../modules/auth/api.h"
  52. #include "uid_auth_db_mod.h"
  53. #define IS_NULL(f) ((f).flags & DB_NULL)
  54. static inline int get_ha1(struct username* username, str* did, str* realm,
  55. authdb_table_info_t *table_info, char* ha1, db_res_t** res, db_rec_t** row)
  56. {
  57. str result;
  58. db_cmd_t *q = NULL;
  59. if (calc_ha1) {
  60. q = table_info->query_password;
  61. DBG("querying plain password\n");
  62. } else {
  63. if (username->domain.len) {
  64. q = table_info->query_pass2;
  65. DBG("querying ha1b\n");
  66. } else {
  67. q = table_info->query_pass;
  68. DBG("querying ha1\n");
  69. }
  70. }
  71. q->match[0].v.lstr = username->user;
  72. q->match[1].v.lstr = *realm;
  73. if (use_did) q->match[2].v.lstr = *did;
  74. if (db_exec(res, q) < 0 ) {
  75. ERR("Error while querying database\n");
  76. return -1;
  77. }
  78. if (*res) *row = db_first(*res);
  79. else *row = NULL;
  80. while (*row) {
  81. if (IS_NULL((*row)->fld[0]) || IS_NULL((*row)->fld[1])) {
  82. LOG(L_ERR, "auth_db:get_ha1: Credentials for '%.*s'@'%.*s' contain NULL value, skipping\n",
  83. username->user.len, ZSW(username->user.s), realm->len, ZSW(realm->s));
  84. } else {
  85. if ((*row)->fld[1].v.int4 & SRDB_DISABLED) {
  86. /* disabled rows ignored */
  87. } else {
  88. if ((*row)->fld[1].v.int4 & SRDB_LOAD_SER) {
  89. /* *row = i; */
  90. break;
  91. }
  92. }
  93. }
  94. *row = db_next(*res);
  95. }
  96. if (!*row) {
  97. DBG("auth_db:get_ha1: Credentials for '%.*s'@'%.*s' not found\n",
  98. username->user.len, ZSW(username->user.s), realm->len, ZSW(realm->s));
  99. return 1;
  100. }
  101. result.s = (*row)->fld[0].v.cstr;
  102. result.len = strlen(result.s);
  103. if (calc_ha1) {
  104. /* Only plaintext passwords are stored in database,
  105. * we have to calculate HA1 */
  106. auth_api.calc_HA1(HA_MD5, &username->whole, realm, &result, 0, 0, ha1);
  107. DBG("auth_db:get_ha1: HA1 string calculated: %s\n", ha1);
  108. } else {
  109. memcpy(ha1, result.s, result.len);
  110. ha1[result.len] = '\0';
  111. }
  112. return 0;
  113. }
  114. /*
  115. * Calculate the response and compare with the given response string
  116. * Authorization is successful if this two strings are same
  117. */
  118. static inline int check_response(dig_cred_t* cred, str* method, char* ha1)
  119. {
  120. HASHHEX resp, hent;
  121. /*
  122. * First, we have to verify that the response received has
  123. * the same length as responses created by us
  124. */
  125. if (cred->response.len != 32) {
  126. DBG("auth_db:check_response: Receive response len != 32\n");
  127. return 1;
  128. }
  129. /*
  130. * Now, calculate our response from parameters received
  131. * from the user agent
  132. */
  133. auth_api.calc_response(ha1, &(cred->nonce),
  134. &(cred->nc), &(cred->cnonce),
  135. &(cred->qop.qop_str), cred->qop.qop_parsed == QOP_AUTHINT,
  136. method, &(cred->uri), hent, resp);
  137. DBG("auth_db:check_response: Our result = \'%s\'\n", resp);
  138. /*
  139. * And simply compare the strings, the user is
  140. * authorized if they match
  141. */
  142. if (!memcmp(resp, cred->response.s, 32)) {
  143. DBG("auth_db:check_response: Authorization is OK\n");
  144. return 0;
  145. } else {
  146. DBG("auth_db:check_response: Authorization failed\n");
  147. return 2;
  148. }
  149. }
  150. /*
  151. * Generate AVPs from the database result
  152. */
  153. static int generate_avps(db_res_t* result, db_rec_t *row)
  154. {
  155. int i;
  156. int_str iname, ivalue;
  157. str value;
  158. char buf[32];
  159. for (i = 2; i < credentials_n + 2; i++) {
  160. value = row->fld[i].v.lstr;
  161. if (IS_NULL(row->fld[i]))
  162. continue;
  163. switch (row->fld[i].type) {
  164. case DB_STR:
  165. value = row->fld[i].v.lstr;
  166. break;
  167. case DB_INT:
  168. value.len = sprintf(buf, "%d", row->fld[i].v.int4);
  169. value.s = buf;
  170. break;
  171. default:
  172. abort();
  173. break;
  174. }
  175. if (value.s == NULL)
  176. continue;
  177. iname.s = credentials[i - 2];
  178. ivalue.s = value;
  179. if (add_avp(AVP_NAME_STR | AVP_VAL_STR | AVP_CLASS_USER, iname, ivalue) < 0) {
  180. LOG(L_ERR, "auth_db:generate_avps: Error while creating AVPs\n");
  181. return -1;
  182. }
  183. DBG("auth_db:generate_avps: set string AVP \'%.*s = %.*s\'\n",
  184. iname.s.len, ZSW(iname.s.s), value.len, ZSW(value.s));
  185. }
  186. return 0;
  187. }
  188. /* this is a dirty work around to check the credentials of all users,
  189. * if the database query returned more then one result
  190. *
  191. * Fills res (which must be db_free'd afterwards if the call was succesfull)
  192. * returns 0 on success, 1 on no match (?)
  193. * and -1 on error (memory, db a.s.o).
  194. * WARNING: if -1 is returned res _must_ _not_ be freed (it's empty)
  195. *
  196. */
  197. static inline int check_all_ha1(struct sip_msg* msg, struct hdr_field* hdr,
  198. dig_cred_t* dig, str* method, str* did, str* realm,
  199. authdb_table_info_t *table_info, db_res_t** res)
  200. {
  201. char ha1[256];
  202. db_rec_t *row;
  203. str result;
  204. db_cmd_t *q;
  205. if (calc_ha1) {
  206. q = table_info->query_password;
  207. DBG("querying plain password\n");
  208. }
  209. else {
  210. if (dig->username.domain.len) {
  211. q = table_info->query_pass2;
  212. DBG("querying ha1b\n");
  213. }
  214. else {
  215. q = table_info->query_pass;
  216. DBG("querying ha1\n");
  217. }
  218. }
  219. q->match[0].v.lstr = dig->username.user;
  220. if (dig->username.domain.len)
  221. q->match[1].v.lstr = dig->username.domain;
  222. else
  223. q->match[1].v.lstr = *realm;
  224. if (use_did) q->match[2].v.lstr = *did;
  225. if (db_exec(res, q) < 0 ) {
  226. ERR("Error while querying database\n");
  227. }
  228. if (*res) row = db_first(*res);
  229. else row = NULL;
  230. while (row) {
  231. if (IS_NULL(row->fld[0]) || IS_NULL(row->fld[1])) {
  232. LOG(L_ERR, "auth_db:check_all_ha1: Credentials for '%.*s'@'%.*s' contain NULL value, skipping\n",
  233. dig->username.user.len, ZSW(dig->username.user.s), realm->len, ZSW(realm->s));
  234. }
  235. else {
  236. if (row->fld[1].v.int4 & SRDB_DISABLED) {
  237. /* disabled rows ignored */
  238. }
  239. else {
  240. if (row->fld[1].v.int4 & SRDB_LOAD_SER) {
  241. result.s = row->fld[0].v.cstr;
  242. result.len = strlen(result.s);
  243. if (calc_ha1) {
  244. /* Only plaintext passwords are stored in database,
  245. * we have to calculate HA1 */
  246. auth_api.calc_HA1(HA_MD5, &(dig->username.whole), realm, &result, 0, 0, ha1);
  247. DBG("auth_db:check_all_ha1: HA1 string calculated: %s\n", ha1);
  248. } else {
  249. memcpy(ha1, result.s, result.len);
  250. ha1[result.len] = '\0';
  251. }
  252. if (!check_response(dig, method, ha1)) {
  253. if (auth_api.post_auth(msg, hdr) == AUTHENTICATED) {
  254. generate_avps(*res, row);
  255. return 0;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. row = db_next(*res);
  262. }
  263. if (!row) {
  264. DBG("auth_db:check_all_ha1: Credentials for '%.*s'@'%.*s' not found",
  265. dig->username.user.len, ZSW(dig->username.user.s), realm->len, ZSW(realm->s));
  266. }
  267. return 1;
  268. }
  269. /*
  270. * Authenticate digest credentials
  271. * Returns:
  272. * -3 -- Bad Request
  273. * -2 -- Error while checking credentials (such as malformed message or database problem)
  274. * -1 -- Authentication failed
  275. * 1 -- Authentication successful
  276. */
  277. static inline int authenticate(struct sip_msg* msg, str* realm, authdb_table_info_t *table, hdr_types_t hftype)
  278. {
  279. char ha1[256];
  280. int res, ret;
  281. db_rec_t *row;
  282. struct hdr_field* h;
  283. auth_body_t* cred;
  284. db_res_t* result;
  285. str did;
  286. cred = 0;
  287. result = 0;
  288. ret = -1;
  289. switch(auth_api.pre_auth(msg, realm, hftype, &h, NULL)) {
  290. case NONCE_REUSED:
  291. LM_DBG("nonce reused");
  292. ret = AUTH_NONCE_REUSED;
  293. goto end;
  294. case STALE_NONCE:
  295. LM_DBG("stale nonce\n");
  296. ret = AUTH_STALE_NONCE;
  297. goto end;
  298. case NO_CREDENTIALS:
  299. LM_DBG("no credentials\n");
  300. ret = AUTH_NO_CREDENTIALS;
  301. goto end;
  302. case ERROR:
  303. case BAD_CREDENTIALS:
  304. ret = -3;
  305. goto end;
  306. case CREATE_CHALLENGE:
  307. ERR("auth_db:authenticate: CREATE_CHALLENGE is not a valid state\n");
  308. ret = -2;
  309. goto end;
  310. case DO_RESYNCHRONIZATION:
  311. ERR("auth_db:authenticate: DO_RESYNCHRONIZATION is not a valid state\n");
  312. ret = -2;
  313. goto end;
  314. case NOT_AUTHENTICATED:
  315. ret = -1;
  316. goto end;
  317. case DO_AUTHENTICATION:
  318. break;
  319. case AUTHENTICATED:
  320. ret = 1;
  321. goto end;
  322. }
  323. cred = (auth_body_t*)h->parsed;
  324. if (use_did) {
  325. if (msg->REQ_METHOD == METHOD_REGISTER) {
  326. ret = get_to_did(&did, msg);
  327. } else {
  328. ret = get_from_did(&did, msg);
  329. }
  330. if (ret == 0) {
  331. did.s = DEFAULT_DID;
  332. did.len = sizeof(DEFAULT_DID) - 1;
  333. }
  334. } else {
  335. did.len = 0;
  336. did.s = 0;
  337. }
  338. if (check_all) {
  339. res = check_all_ha1(msg, h, &(cred->digest), &msg->first_line.u.request.method, &did, realm, table, &result);
  340. if (res < 0) {
  341. ret = -2;
  342. goto end;
  343. }
  344. else if (res > 0) {
  345. ret = -1;
  346. goto end;
  347. }
  348. else {
  349. ret = 1;
  350. goto end;
  351. }
  352. } else {
  353. res = get_ha1(&cred->digest.username, &did, realm, table, ha1, &result, &row);
  354. if (res < 0) {
  355. ret = -2;
  356. goto end;
  357. }
  358. if (res > 0) {
  359. /* Username not found in the database */
  360. ret = -1;
  361. goto end;
  362. }
  363. }
  364. /* Recalculate response, it must be same to authorize successfully */
  365. if (!check_response(&(cred->digest), &msg->first_line.u.request.method, ha1)) {
  366. switch(auth_api.post_auth(msg, h)) {
  367. case ERROR:
  368. case BAD_CREDENTIALS:
  369. ret = -2;
  370. break;
  371. case NOT_AUTHENTICATED:
  372. ret = -1;
  373. break;
  374. case AUTHENTICATED:
  375. generate_avps(result, row);
  376. ret = 1;
  377. break;
  378. default:
  379. ret = -1;
  380. break;
  381. }
  382. } else {
  383. ret = -1;
  384. }
  385. end:
  386. if (result) db_res_free(result);
  387. if (ret < 0) {
  388. if (auth_api.build_challenge(msg, (cred ? cred->stale : 0), realm, NULL, NULL, hftype) < 0) {
  389. ERR("Error while creating challenge\n");
  390. ret = -2;
  391. }
  392. }
  393. return ret;
  394. }
  395. /*
  396. * Authenticate using Proxy-Authorize header field
  397. */
  398. int proxy_authenticate(struct sip_msg* msg, char* p1, char* p2)
  399. {
  400. str realm;
  401. if (get_str_fparam(&realm, msg, (fparam_t*)p1) < 0) {
  402. ERR("Cannot obtain digest realm from parameter '%s'\n", ((fparam_t*)p1)->orig);
  403. return -1;
  404. }
  405. return authenticate(msg, &realm, (authdb_table_info_t*)p2, HDR_PROXYAUTH_T);
  406. }
  407. /*
  408. * Authorize using WWW-Authorize header field
  409. */
  410. int www_authenticate(struct sip_msg* msg, char* p1, char* p2)
  411. {
  412. str realm;
  413. if (get_str_fparam(&realm, msg, (fparam_t*)p1) < 0) {
  414. ERR("Cannot obtain digest realm from parameter '%s'\n", ((fparam_t*)p1)->orig);
  415. return -1;
  416. }
  417. return authenticate(msg, &realm, (authdb_table_info_t*)p2, HDR_AUTHORIZATION_T);
  418. }