-
Notifications
You must be signed in to change notification settings - Fork 3
/
sipauthserve.cpp
333 lines (292 loc) · 9.24 KB
/
sipauthserve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2011 Kestrel Signal Processing, Inc.
* Copyright 2011 Range Networks, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <cstdlib>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <netinet/in.h>
#include <osip2/osip.h>
#include <osipparser2/osip_message.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <Logger.h>
#include <Configuration.h>
#include "servershare.h"
#include "SubscriberRegistry.h"
using namespace std;
ConfigurationTable gConfig("/etc/OpenBTS/sipauthserve.db", "sipauthserve", getConfigurationKeys());
Log dummy("sipauthserve", gConfig.getStr("Log.Level").c_str(), LOG_LOCAL7);
int my_udp_port;
// just using this for the database access
SubscriberRegistry gSubscriberRegistry;
void prettyPrint(const char *label, osip_message_t *sip)
{
char *dest=NULL;
size_t length=0;
int i = osip_message_to_str(sip, &dest, &length);
if (i!=0) {
LOG(ERR) << "cannot get printable message";
} else {
LOG(INFO) << label << ":\n" << dest;
osip_free(dest);
}
}
string imsiFromSip(osip_message_t *sip)
{
int i;
char *dest;
osip_uri_t *fromUri = osip_from_get_url(sip->from);
if (!fromUri) {
LOG(ERR) << "osip_from_get_url problem";
return "";
}
i = osip_uri_to_str(fromUri, &dest);
string imsi = dest;
osip_free(dest);
return imsi;
}
string imsiToSip(osip_message_t *sip)
{
int i;
char *dest;
osip_uri_t *toUri = osip_to_get_url(sip->to);
if (!toUri) {
LOG(ERR) << "osip_to_get_url problem";
return "";
}
i = osip_uri_to_str(toUri, &dest);
string imsi = dest;
osip_free(dest);
return imsi;
}
// is imsi in the database?
bool imsiFound(string imsi)
{
string x = imsiGet(imsi, "id");
return x.length() != 0;
}
string imsiClean(string imsi)
{
// remove leading sip:
if (0 == strncasecmp(imsi.c_str(), "sip:", 4)) {
imsi = imsi.substr(4);
}
// remove trailing @...
size_t p = imsi.find("@");
if (p != string::npos) {
imsi = imsi.substr(0, p);
}
// remove leading IMSI
if (0 == strncasecmp(imsi.c_str(), "imsi", 4)) {
imsi = imsi.substr(4);
}
return imsi;
}
char *processBuffer(char *buffer)
{
int i;
// parse sip message
osip_message_t *sip;
i=osip_message_init(&sip);
if (i!=0) {
LOG(ERR) << "cannot allocate";
osip_message_free(sip);
return NULL;
}
i=osip_message_parse(sip, buffer, strlen(buffer));
if (i!=0) {
LOG(ERR) << "cannot parse sip message";
osip_message_free(sip);
return NULL;
}
prettyPrint("request", sip);
// response starts as clone of message
osip_message_t *response;
osip_message_clone(sip, &response);
osip_from_t * contact_header = (osip_from_t*)osip_list_get(&sip->contacts,0);
osip_uri_t* contact_url = contact_header->url;
char *remote_host = contact_url->host;
char *remote_port = contact_url->port;
// return via
ostringstream newvia;
// newvia << "SIP/2.0/UDP localhost:5063;branch=1;[email protected]";
const char *my_ipaddress = "localhost";
newvia << "SIP/2.0/UDP " << my_ipaddress << ":" << my_udp_port << ";branch=1;received="
<< "[email protected]"; // << my_network.string_addr((struct sockaddr *)netaddr, netaddrlen, false);
osip_message_append_via(response, newvia.str().c_str());
// no method
osip_message_set_method(response, NULL);
string imsi = imsiClean(imsiFromSip(sip));
string imsiTo = imsiClean(imsiToSip(sip));
if ((imsi == "EXIT") && (imsiTo == "EXIT")) exit(0); // for testing only
if (!imsiFound(imsi)) {
LOG(NOTICE) << "imsi unknown";
// imsi problem => 404 IMSI Not Found
osip_message_set_status_code (response, 404);
osip_message_set_reason_phrase (response, osip_strdup("IMSI Not Found"));
} else if (gConfig.defines("SubscriberRegistry.IgnoreAuthentication")) {
osip_message_set_status_code (response, 200);
osip_message_set_reason_phrase (response, osip_strdup("OK"));
LOG(INFO) << "success, registering for IP address " << remote_host;
imsiSet(imsi,"ipaddr",remote_host);
imsiSet(imsi,"port",remote_port);
} else {
// look for rand and sres in Authorization header (assume imsi same as in from)
string randx;
string sres;
// sip parser is not working reliably for Authorization, so we'll do the parsing
char *RAND = strcasestr(buffer, "nonce=");
char *SRES = strcasestr(buffer, "response=");
if (RAND && SRES) {
// find RAND digits
RAND += 6;
while (!isalnum(*RAND)) { RAND++; }
RAND[32] = 0;
int j=0;
// FIXME -- These loops should use strspn instead.
while(isalnum(RAND[j])) { j++; }
RAND[j] = '\0';
// find SRES digits
SRES += 9;
while (!isalnum(*SRES)) { SRES++; }
int i=0;
// FIXME -- These loops should use strspn instead.
while(isalnum(SRES[i])) { i++; }
SRES[i] = '\0';
LOG(INFO) << "rand = /" << RAND << "/";
LOG(INFO) << "sres = /" << SRES << "/";
}
if (!RAND || !SRES) {
LOG(NOTICE) << "imsi known, 1st register";
// no rand and sres => 401 Unauthorized
osip_message_set_status_code (response, 401);
osip_message_set_reason_phrase (response, osip_strdup("Unauthorized"));
// but include rand in www_authenticate
osip_www_authenticate_t *auth;
osip_www_authenticate_init(&auth);
// auth type is required by osip_www_authenticate_to_str (and therefore osip_message_to_str)
string auth_type = "Digest";
osip_www_authenticate_set_auth_type(auth, osip_strdup(auth_type.c_str()));
// returning RAND in www_authenticate header
string randz = generateRand(imsi);
osip_www_authenticate_set_nonce(auth, osip_strdup(randz.c_str()));
i = osip_list_add (&response->www_authenticates, auth, -1);
if (i < 0) LOG(ERR) << "problem adding www_authenticate";
} else {
string kc;
bool sres_good = authenticate(imsi, RAND, SRES, &kc);
LOG(INFO) << "imsi known, 2nd register, good = " << sres_good;
if (sres_good) {
// sres matches rand => 200 OK
osip_message_set_status_code (response, 200);
osip_message_set_reason_phrase (response, osip_strdup("OK"));
if (kc.size() != 0) {
osip_authentication_info *auth;
osip_authentication_info_init(&auth);
osip_authentication_info_set_cnonce(auth, osip_strdup(kc.c_str()));
i = osip_list_add (&response->authentication_infos, auth, -1);
if (i < 0) LOG(ERR) << "problem adding authentication_infos";
}
// And register it.
LOG(INFO) << "success, registering for IP address " << remote_host;
imsiSet(imsi,"ipaddr",remote_host);
imsiSet(imsi,"port",remote_port);
} else {
// sres does not match rand => 401 Unauthorized
osip_message_set_status_code (response, 401);
osip_message_set_reason_phrase (response, osip_strdup("Unauthorized"));
}
}
}
prettyPrint("response", response);
size_t length = 0;
char *dest;
int ii = osip_message_to_str(response, &dest, &length);
if (ii != 0) {
LOG(ERR) << "cannot get printable message";
}
osip_message_free(sip);
osip_message_free(response);
return dest;
}
#define BUFLEN 5000
int
main(int argc, char **argv)
{
sockaddr_in si_me;
sockaddr_in si_other;
int aSocket;
char buf[BUFLEN];
LOG(ALERT) << argv[0] << " (re)starting";
srand ( time(NULL) + (int)getpid() );
my_udp_port = gConfig.getNum("SubscriberRegistry.Port");
gSubscriberRegistry.init();
// init osip lib
osip_t *osip;
int i=osip_init(&osip);
if (i!=0) {
LOG(ALERT) << "cannot init sip lib";
return NULL;
}
if ((aSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
LOG(ALERT) << "can't initialize socket";
exit(1);
}
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(my_udp_port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(aSocket, (sockaddr*)&si_me, sizeof(si_me)) == -1) {
LOG(ALERT) << "can't bind socket on port " << my_udp_port;
exit(1);
}
LOG(NOTICE) << "binding on port " << my_udp_port;
while (true) {
gConfig.purge();
socklen_t slen = sizeof(si_other);
memset(buf, 0, BUFLEN);
if (recvfrom(aSocket, buf, BUFLEN, 0, (sockaddr*)&si_other, &slen) == -1) {
LOG(ERR) << "recvfrom problem";
continue;
}
LOG(INFO) << " receiving " << buf;
char *dest = processBuffer(buf);
if (dest == NULL) {
continue;
}
if (sendto(aSocket, dest, strlen(dest), 0, (sockaddr*)&si_other, sizeof(si_other)) == -1) {
LOG(ERR) << "sendto problem";
continue;
}
osip_free(dest);
}
close(aSocket);
return 0;
}