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