authorize.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * $Id$
  3. *
  4. * Digest Authentication - Radius 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-03-09: Based on authorize.c from radius_auth (janakj)
  32. */
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include "../../mem/mem.h"
  36. #include "../../str.h"
  37. #include "../../sr_module.h"
  38. #include "../../parser/hf.h"
  39. #include "../../parser/digest/digest.h"
  40. #include "../../parser/parse_uri.h"
  41. #include "../../parser/parse_from.h"
  42. #include "../../parser/parse_to.h"
  43. #include "../../dprint.h"
  44. #include "../../id.h"
  45. #include "../../ut.h"
  46. #include "../../modules/auth/api.h"
  47. #include "authorize.h"
  48. #include "sterman.h"
  49. #include "authrad_mod.h"
  50. static void attr_name_value(str* name, str* value, VALUE_PAIR* vp)
  51. {
  52. int i;
  53. for (i = 0; i < vp->lvalue; i++) {
  54. if (vp->strvalue[i] == ':' || vp->strvalue[i] == '=') {
  55. name->s = vp->strvalue;
  56. name->len = i;
  57. if (i == (vp->lvalue - 1)) {
  58. value->s = (char*)0;
  59. value->len = 0;
  60. } else {
  61. value->s = vp->strvalue + i + 1;
  62. value->len = vp->lvalue - i - 1;
  63. }
  64. return;
  65. }
  66. }
  67. name->len = value->len = 0;
  68. name->s = value->s = (char*)0;
  69. }
  70. /*
  71. * Generate AVPs from the database result
  72. */
  73. static int generate_avps(VALUE_PAIR* received)
  74. {
  75. int_str name, val;
  76. VALUE_PAIR *vp;
  77. vp = rc_avpair_get(received, ATTRID(attrs[A_SER_UID].v), VENDOR(attrs[A_SER_UID].v));
  78. if (vp == NULL) {
  79. WARN("RADIUS server did not send SER-UID attribute in digest authentication reply\n");
  80. return -1;
  81. }
  82. val.s.len = vp->lvalue;
  83. val.s.s = vp->strvalue;
  84. name.s.s = "uid";
  85. name.s.len = 3;
  86. if (add_avp(AVP_TRACK_FROM | AVP_CLASS_USER | AVP_NAME_STR | AVP_VAL_STR, name, val) < 0) {
  87. ERR("Unable to create UID attribute\n");
  88. return -1;
  89. }
  90. vp = received;
  91. while ((vp = rc_avpair_get(vp, ATTRID(attrs[A_SER_ATTR].v), VENDOR(attrs[A_SER_ATTR].v)))) {
  92. attr_name_value(&name.s, &val.s, vp);
  93. if (name.s.len == 0) {
  94. ERR("Missing attribute name\n");
  95. return -1;
  96. }
  97. if (add_avp(AVP_TRACK_FROM | AVP_CLASS_USER | AVP_NAME_STR | AVP_VAL_STR, name, val) < 0) {
  98. LOG(L_ERR, "generate_avps: Unable to create a new AVP\n");
  99. return -1;
  100. } else {
  101. DBG("generate_avps: AVP '%.*s'='%.*s' has been added\n",
  102. name.s.len, ZSW(name.s.s),
  103. val.s.len, ZSW(val.s.s));
  104. }
  105. vp = vp->next;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Extract URI depending on the request from To or From header
  111. */
  112. static inline int get_uri(struct sip_msg* _m, str** _uri)
  113. {
  114. if ((REQ_LINE(_m).method.len == 8) && (memcmp(REQ_LINE(_m).method.s, "REGISTER", 8) == 0)) {
  115. if (!_m->to && ((parse_headers(_m, HDR_TO_F, 0) == -1) || !_m->to)) {
  116. LOG(L_ERR, "get_uri(): To header field not found or malformed\n");
  117. return -1;
  118. }
  119. *_uri = &(get_to(_m)->uri);
  120. } else {
  121. if (parse_from_header(_m) == -1) {
  122. LOG(L_ERR, "get_uri(): Error while parsing headers\n");
  123. return -2;
  124. }
  125. *_uri = &(get_from(_m)->uri);
  126. }
  127. return 0;
  128. }
  129. /*
  130. * Authorize digest credentials
  131. */
  132. static inline int authenticate(struct sip_msg* msg, str* realm,
  133. hdr_types_t hftype)
  134. {
  135. int res;
  136. auth_result_t ret;
  137. struct hdr_field* h;
  138. auth_body_t* cred;
  139. str* uri;
  140. struct sip_uri puri;
  141. str user, did;
  142. VALUE_PAIR* received;
  143. cred = 0;
  144. ret = -1;
  145. user.s = 0;
  146. received = NULL;
  147. switch(auth_api.pre_auth(msg, realm, hftype, &h, NULL)) {
  148. default:
  149. BUG("unexpected reply '%d'.\n", auth_api.pre_auth(msg, realm, hftype,
  150. &h, NULL));
  151. #ifdef EXTRA_DEBUG
  152. abort();
  153. #endif
  154. case NONCE_REUSED:
  155. LM_DBG("nonce reused");
  156. ret = AUTH_NONCE_REUSED;
  157. goto end;
  158. case STALE_NONCE:
  159. LM_DBG("stale nonce\n");
  160. ret = AUTH_STALE_NONCE;
  161. goto end;
  162. case NO_CREDENTIALS:
  163. LM_DBG("no credentials\n");
  164. ret = AUTH_NO_CREDENTIALS;
  165. case ERROR:
  166. case BAD_CREDENTIALS:
  167. ret = -3;
  168. goto end;
  169. case NOT_AUTHENTICATED:
  170. ret = -1;
  171. goto end;
  172. case DO_AUTHENTICATION:
  173. break;
  174. case AUTHENTICATED:
  175. ret = 1;
  176. goto end;
  177. }
  178. cred = (auth_body_t*)h->parsed;
  179. if (use_did) {
  180. if (msg->REQ_METHOD == METHOD_REGISTER) {
  181. ret = get_to_did(&did, msg);
  182. } else {
  183. ret = get_from_did(&did, msg);
  184. }
  185. if (ret == 0) {
  186. did.s = DEFAULT_DID;
  187. did.len = sizeof(DEFAULT_DID) - 1;
  188. }
  189. } else {
  190. did.len = 0;
  191. did.s = 0;
  192. }
  193. if (get_uri(msg, &uri) < 0) {
  194. LOG(L_ERR, "authorize(): From/To URI not found\n");
  195. ret = -1;
  196. goto end;
  197. }
  198. if (parse_uri(uri->s, uri->len, &puri) < 0) {
  199. LOG(L_ERR, "authorize(): Error while parsing From/To URI\n");
  200. ret = -1;
  201. goto end;
  202. }
  203. user.s = (char *)pkg_malloc(puri.user.len);
  204. if (user.s == NULL) {
  205. LOG(L_ERR, "authorize: No memory left\n");
  206. ret = -1;
  207. goto end;
  208. }
  209. un_escape(&(puri.user), &user);
  210. res = radius_authorize_sterman(&received, msg, &cred->digest, &msg->first_line.u.request.method, &user);
  211. if (res == 1) {
  212. switch(auth_api.post_auth(msg, h)) {
  213. case ERROR:
  214. case BAD_CREDENTIALS:
  215. ret = -2;
  216. break;
  217. case NOT_AUTHENTICATED:
  218. ret = -1;
  219. break;
  220. case AUTHENTICATED:
  221. if (generate_avps(received) < 0) {
  222. ret = -1;
  223. break;
  224. }
  225. ret = 1;
  226. break;
  227. default:
  228. ret = -1;
  229. break;
  230. }
  231. } else {
  232. ret = -1;
  233. }
  234. end:
  235. if (received) rc_avpair_free(received);
  236. if (user.s) pkg_free(user.s);
  237. if (ret < 0) {
  238. if (auth_api.build_challenge(msg, (cred ? cred->stale : 0), realm, NULL, NULL, hftype) < 0) {
  239. ERR("Error while creating challenge\n");
  240. ret = -2;
  241. }
  242. }
  243. return ret;
  244. }
  245. /*
  246. * Authorize using Proxy-Authorize header field
  247. */
  248. int radius_proxy_authorize(struct sip_msg* _msg, char* p1, char* p2)
  249. {
  250. str realm;
  251. if (get_str_fparam(&realm, _msg, (fparam_t*)p1) < 0) {
  252. ERR("Cannot obtain digest realm from parameter '%s'\n", ((fparam_t*)p1)->orig);
  253. return -1;
  254. }
  255. /* realm parameter is converted to str* in str_fixup */
  256. return authenticate(_msg, &realm, HDR_PROXYAUTH_T);
  257. }
  258. /*
  259. * Authorize using WWW-Authorize header field
  260. */
  261. int radius_www_authorize(struct sip_msg* _msg, char* p1, char* p2)
  262. {
  263. str realm;
  264. if (get_str_fparam(&realm, _msg, (fparam_t*)p1) < 0) {
  265. ERR("Cannot obtain digest realm from parameter '%s'\n", ((fparam_t*)p1)->orig);
  266. return -1;
  267. }
  268. return authenticate(_msg, &realm, HDR_AUTHORIZATION_T);
  269. }