id.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*!
  28. * \file
  29. * \brief SIP-router core ::
  30. * \ingroup core
  31. * Module: \ref core
  32. */
  33. #include "id.h"
  34. #include "parser/parse_from.h"
  35. #include "parser/parse_uri.h"
  36. #include "parser/digest/digest.h"
  37. #include "ut.h"
  38. static str uid_name = STR_STATIC_INIT(AVP_UID);
  39. static str did_name = STR_STATIC_INIT(AVP_DID);
  40. /*
  41. * Set From UID
  42. */
  43. int set_from_uid(str* uid)
  44. {
  45. struct search_state s;
  46. int_str val, name;
  47. avp_t* a;
  48. name.s = uid_name;
  49. a = search_first_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR, name, 0, &s);
  50. while(a) {
  51. destroy_avp(a);
  52. a = search_next_avp(&s, 0);
  53. }
  54. val.s = *uid;
  55. return add_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR | AVP_VAL_STR, name, val);
  56. }
  57. /* Extract username attribute from authorized credentials */
  58. static inline str* cred_user(struct sip_msg* msg)
  59. {
  60. struct hdr_field* h;
  61. auth_body_t* cred;
  62. get_authorized_cred(msg->proxy_auth, &h);
  63. if (!h) get_authorized_cred(msg->authorization, &h);
  64. if (!h) return 0;
  65. cred = (auth_body_t*)(h->parsed);
  66. if (!cred || !cred->digest.username.user.len) return 0;
  67. return &cred->digest.username.user;
  68. }
  69. /*
  70. * Set From UID
  71. */
  72. int get_from_uid(str* uid, struct sip_msg* msg)
  73. {
  74. static char buf[MAX_URI_SIZE];
  75. struct to_body* from;
  76. struct sip_uri puri;
  77. str* du;
  78. int_str val, name;
  79. name.s = uid_name;
  80. if (search_first_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR, name, &val, 0)) {
  81. *uid = val.s;
  82. return 1;
  83. } else {
  84. du = cred_user(msg);
  85. if (du) {
  86. /* Try digest username first */
  87. *uid = *du;
  88. } else {
  89. /* Get From URI username */
  90. if (parse_from_header(msg) < 0) {
  91. LOG(L_ERR, "get_from_uid: Error while parsing From header\n");
  92. return -1;
  93. }
  94. from = get_from(msg);
  95. if (parse_uri(from->uri.s, from->uri.len, &puri) == -1) {
  96. LOG(L_ERR, "get_from_uid: Error while parsing From URI\n");
  97. return -1;
  98. }
  99. if (puri.user.len > MAX_URI_SIZE) {
  100. LOG(L_ERR, "get_from_uid: Username too long\n");
  101. return -1;
  102. }
  103. memcpy(buf, puri.user.s, puri.user.len);
  104. uid->s = buf;
  105. uid->len = puri.user.len;
  106. strlower(uid);
  107. }
  108. val.s = *uid;
  109. add_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR | AVP_VAL_STR, name, val);
  110. return 0;
  111. }
  112. }
  113. int get_to_uid(str* uid, struct sip_msg* msg)
  114. {
  115. static char buf[MAX_URI_SIZE];
  116. struct to_body* to;
  117. struct sip_uri puri;
  118. char* p;
  119. int_str val, name;
  120. name.s = uid_name;
  121. if (search_first_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR, name, &val, 0)) {
  122. *uid = val.s;
  123. return 1;
  124. } else {
  125. if (msg->REQ_METHOD == METHOD_REGISTER) {
  126. if ((msg->to==0) &&
  127. (parse_headers(msg, HDR_TO_F, 0) < 0 || msg->to == 0)) {
  128. DBG("get_to_uid: Error while parsing To URI: "
  129. " to header bad or missing\n");
  130. return -1;
  131. }
  132. to = get_to(msg);
  133. if (parse_uri(to->uri.s, to->uri.len, &puri) == -1) {
  134. DBG("get_to_uid: Error while parsing To URI\n");
  135. return -1;
  136. }
  137. p = puri.user.s;
  138. uid->len = puri.user.len;
  139. } else {
  140. if (!msg->parsed_uri_ok && (parse_sip_msg_uri(msg) < 0)) {
  141. DBG("Error while parsing the Request-URI\n");
  142. return -1;
  143. }
  144. p = msg->parsed_uri.user.s;
  145. uid->len = msg->parsed_uri.user.len;
  146. }
  147. if (uid->len > MAX_URI_SIZE) {
  148. DBG("get_to_uid: Username too long\n");
  149. return -1;
  150. }
  151. if (p == NULL || uid->len == 0) {
  152. DBG("get_to_uid: Username is empty\n");
  153. return -1;
  154. }
  155. memcpy(buf, p, uid->len);
  156. uid->s = buf;
  157. strlower(uid);
  158. val.s = *uid;
  159. add_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR | AVP_VAL_STR, name, val);
  160. return 0;
  161. }
  162. }
  163. /*
  164. * Set To UID
  165. */
  166. int set_to_uid(str* uid)
  167. {
  168. struct search_state s;
  169. int_str val, name;
  170. avp_t* a;
  171. name.s = uid_name;
  172. a = search_first_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR, name, 0, &s);
  173. while(a) {
  174. destroy_avp(a);
  175. a = search_next_avp(&s, 0);
  176. }
  177. val.s = *uid;
  178. return add_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR | AVP_VAL_STR, name, val);
  179. }
  180. /*
  181. * Return current To domain id
  182. */
  183. int get_to_did(str* did, struct sip_msg* msg)
  184. {
  185. int_str val, name;
  186. name.s = did_name;
  187. if (search_first_avp(AVP_TRACK_TO | AVP_NAME_STR, name, &val, 0)) {
  188. *did = val.s;
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. /*
  194. * Return current To domain id
  195. */
  196. int get_from_did(str* did, struct sip_msg* msg)
  197. {
  198. int_str val, name;
  199. name.s = did_name;
  200. if (search_first_avp(AVP_TRACK_FROM | AVP_NAME_STR, name, &val, 0)) {
  201. *did = val.s;
  202. return 1;
  203. }
  204. return 0;
  205. }