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 /*!
22 * \file
23 * \brief Kamailio core :: UID handling
24 * \ingroup core
25 * Module: \ref core
26 */
27
28 #include "id.h"
29 #include "parser/parse_from.h"
30 #include "parser/parse_uri.h"
31 #include "parser/digest/digest.h"
32 #include "ut.h"
33
34 static str uid_name = STR_STATIC_INIT(AVP_UID);
35 static str did_name = STR_STATIC_INIT(AVP_DID);
36
37
38 /**
39 * Set From UID
40 */
set_from_uid(str * uid)41 int set_from_uid(str* uid)
42 {
43 struct search_state s;
44 int_str val, name;
45 avp_t* a;
46
47 name.s = uid_name;
48 a = search_first_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR, name, 0, &s);
49 while(a) {
50 destroy_avp(a);
51 a = search_next_avp(&s, 0);
52 }
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
58
59 /** Extract username attribute from authorized credentials */
cred_user(struct sip_msg * msg)60 static inline str* cred_user(struct sip_msg* msg)
61 {
62 struct hdr_field* h;
63 auth_body_t* cred;
64
65 get_authorized_cred(msg->proxy_auth, &h);
66 if (!h) get_authorized_cred(msg->authorization, &h);
67 if (!h) return 0;
68 cred = (auth_body_t*)(h->parsed);
69 if (!cred || !cred->digest.username.user.len) return 0;
70 return &cred->digest.username.user;
71 }
72
73 /**
74 * Set From UID
75 */
get_from_uid(str * uid,struct sip_msg * msg)76 int get_from_uid(str* uid, struct sip_msg* msg)
77 {
78 static char buf[MAX_URI_SIZE];
79 struct to_body* from;
80 struct sip_uri puri;
81 str* du;
82 int_str val, name;
83
84 name.s = uid_name;
85 if (search_first_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR, name, &val, 0)) {
86 *uid = val.s;
87 return 1;
88 } else {
89 du = cred_user(msg);
90 if (du) {
91 /* Try digest username first */
92 *uid = *du;
93 } else {
94 /* Get From URI username */
95 if (parse_from_header(msg) < 0) {
96 LM_ERR("unable to parse From header\n");
97 return -1;
98 }
99 from = get_from(msg);
100 if (parse_uri(from->uri.s, from->uri.len, &puri) == -1) {
101 LM_ERR("unable to parsie From URI\n");
102 return -1;
103 }
104
105 if (puri.user.len > MAX_URI_SIZE) {
106 LM_ERR("username too long\n");
107 return -1;
108 }
109 memcpy(buf, puri.user.s, puri.user.len);
110 uid->s = buf;
111 uid->len = puri.user.len;
112 strlower(uid);
113 }
114
115 val.s = *uid;
116 add_avp(AVP_CLASS_USER | AVP_TRACK_FROM | AVP_NAME_STR | AVP_VAL_STR, name, val);
117 return 0;
118 }
119 }
120
121 /** Get to UID
122 */
get_to_uid(str * uid,struct sip_msg * msg)123 int get_to_uid(str* uid, struct sip_msg* msg)
124 {
125 static char buf[MAX_URI_SIZE];
126 struct to_body* to;
127 struct sip_uri puri;
128 char* p;
129 int_str val, name;
130
131 name.s = uid_name;
132 if (search_first_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR, name, &val, 0)) {
133 *uid = val.s;
134 return 1;
135 } else {
136 if (msg->REQ_METHOD == METHOD_REGISTER) {
137 if ((msg->to==0) &&
138 (parse_headers(msg, HDR_TO_F, 0) < 0 || msg->to == 0)) {
139 LM_DBG("Error while parsing To URI: to header bad or missing\n");
140 return -1;
141 }
142 to = get_to(msg);
143 if (parse_uri(to->uri.s, to->uri.len, &puri) == -1) {
144 LM_DBG("Error while parsing To URI\n");
145 return -1;
146 }
147 p = puri.user.s;
148 uid->len = puri.user.len;
149 } else {
150 if (!msg->parsed_uri_ok && (parse_sip_msg_uri(msg) < 0)) {
151 LM_DBG("Error while parsing the Request-URI\n");
152 return -1;
153 }
154 p = msg->parsed_uri.user.s;
155 uid->len = msg->parsed_uri.user.len;
156 }
157
158 if (uid->len > MAX_URI_SIZE) {
159 LM_DBG("Username too long\n");
160 return -1;
161 }
162 if (p == NULL || uid->len == 0) {
163 LM_DBG("Username is empty\n");
164 return -1;
165 }
166 memcpy(buf, p, uid->len);
167 uid->s = buf;
168 strlower(uid);
169
170 val.s = *uid;
171 add_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR | AVP_VAL_STR, name, val);
172 return 0;
173 }
174 }
175
176
177 /**
178 * Set To UID
179 */
set_to_uid(str * uid)180 int set_to_uid(str* uid)
181 {
182 struct search_state s;
183 int_str val, name;
184 avp_t* a;
185
186 name.s = uid_name;
187 a = search_first_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR, name, 0, &s);
188 while(a) {
189 destroy_avp(a);
190 a = search_next_avp(&s, 0);
191 }
192
193 val.s = *uid;
194 return add_avp(AVP_CLASS_USER | AVP_TRACK_TO | AVP_NAME_STR | AVP_VAL_STR, name, val);
195 }
196
197
198 /**
199 * Return current To domain id
200 */
get_to_did(str * did,struct sip_msg * msg)201 int get_to_did(str* did, struct sip_msg* msg)
202 {
203 int_str val, name;
204
205 name.s = did_name;
206 if (search_first_avp(AVP_TRACK_TO | AVP_NAME_STR, name, &val, 0)) {
207 *did = val.s;
208 return 1;
209 }
210 return 0;
211 }
212
213
214 /**
215 * Return current To domain id
216 */
get_from_did(str * did,struct sip_msg * msg)217 int get_from_did(str* did, struct sip_msg* msg)
218 {
219 int_str val, name;
220
221 name.s = did_name;
222 if (search_first_avp(AVP_TRACK_FROM | AVP_NAME_STR, name, &val, 0)) {
223 *did = val.s;
224 return 1;
225 }
226 return 0;
227 }
228
229