id.c 5.0 KB

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