-
Notifications
You must be signed in to change notification settings - Fork 3
/
servershare.cpp
executable file
·402 lines (358 loc) · 10.7 KB
/
servershare.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* 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 <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <Configuration.h>
#include <string.h>
#include "servershare.h"
#include "sqlite3.h"
#include "Logger.h"
#include "SubscriberRegistry.h"
using namespace std;
extern ConfigurationTable gConfig;
// just using this for the database access
extern SubscriberRegistry gSubscriberRegistry;
ConfigurationKeyMap getConfigurationKeys()
{
ConfigurationKeyMap map;
ConfigurationKey *tmp;
tmp = new ConfigurationKey("SIP.Proxy.Registration","127.0.0.1:5064",
"",
ConfigurationKey::CUSTOMERWARN,
ConfigurationKey::IPANDPORT,
"",
false,
"The IP host and port of the proxy to be used for registration and authentication. "
"This should normally be the subscriber registry SIP interface, not Asterisk."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.A3A8","/OpenBTS/comp128",
"",
ConfigurationKey::CUSTOMERWARN,
ConfigurationKey::FILEPATH,
"",
false,
"Path to the program that implements the A3/A8 algorithm."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.db","/var/lib/asterisk/sqlite3dir/sqlite3.db",
"",
ConfigurationKey::CUSTOMERWARN,
ConfigurationKey::FILEPATH,
"",
false,
"The location of the sqlite3 database holding the subscriber registry."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.Manager.Title","Subscriber Registry",
"",
ConfigurationKey::CUSTOMER,
ConfigurationKey::STRING,
"^[[:print:]]+$",
false,
"Title text to be displayed on the subscriber registry manager."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.Manager.VisibleColumns","name username type context host",
"",
ConfigurationKey::CUSTOMERTUNE,
ConfigurationKey::STRING,
"^(name){0,1} (username){0,1} (type){0,1} (context){0,1} (host){0,1}$",
false,
"A space separated list of columns to display in the subscriber registry manager."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.Port","5064",
"",
ConfigurationKey::CUSTOMERWARN,
ConfigurationKey::PORT,
"",
false,
"Port used by the SIP Authentication Server. NOTE: In some older releases (pre-2.8.1) this is called SIP.myPort."
);
map[tmp->getName()] = *tmp;
delete tmp;
tmp = new ConfigurationKey("SubscriberRegistry.UpstreamServer","",
"",
ConfigurationKey::CUSTOMERWARN,
ConfigurationKey::STRING_OPT,// audited
"",
false,
"URL of the subscriber registry HTTP interface on the upstream server. "
"By default, this feature is disabled. "
"To enable, specify a server URL eg: http://localhost/cgi/subreg.cgi. "
"To disable again, execute \"unconfig SubscriberRegistry.UpstreamServer\"."
);
map[tmp->getName()] = *tmp;
delete tmp;
return map;
}
string imsiGet(string imsi, string key)
{
string name = imsi.substr(0,4) == "IMSI" ? imsi : "IMSI" + imsi;
char *value;
if (!sqlite3_single_lookup(gSubscriberRegistry.db(), "sip_buddies", "username", name.c_str(), key.c_str(), value)) {
return "";
}
if (!value) { return ""; }
string retValue = value;
free(value);
return retValue;
}
void imsiSet(string imsi, string key, string value)
{
string name = imsi.substr(0,4) == "IMSI" ? imsi : "IMSI" + imsi;
ostringstream os2;
os2 << "update sip_buddies set " << key << " = \"" << value << "\" where username = \"" << name << "\"";
if (!sqlite3_command(gSubscriberRegistry.db(), os2.str().c_str())) {
LOG(ERR) << "sqlite3_command problem";
return;
}
}
string soGenerateIt()
{
ostringstream os;
for (int i = 0; i < 32; i++) {
// if rand() is too slow you can call it fewer times
os << hex << (rand() & 0xf);
}
return os.str();
}
// generate a 128' random number
string generateRand(string imsi)
{
string ki = imsiGet(imsi, "ki");
string ret;
if (ki.length() != 0) {
LOG(INFO) << "ki is known";
// generate and return rand (clear any cached rand or sres)
imsiSet(imsi, "rand", "");
imsiSet(imsi, "sres", "");
ret = soGenerateIt();
} else {
string wRand = imsiGet(imsi, "rand");
if (wRand.length() != 0) {
LOG(INFO) << "ki is unknown, rand is cached";
// return cached rand
ret = wRand;
} else {
LOG(INFO) << "ki is unknown, rand is not cached";
// generate rand, cache rand, clear sres, and return rand
wRand = soGenerateIt();
imsiSet(imsi, "rand", wRand);
imsiSet(imsi, "sres", "");
ret = wRand;
}
}
return ret;
}
bool strEqual(string a, string b)
{
return 0 == strcasecmp(a.c_str(), b.c_str());
}
bool sresEqual(string a, string b)
{
stringstream ss1;
stringstream ss2;
uint32_t sres1 = 0xffffffff;
uint32_t sres2 = 0xffffffff;
if (a.empty() || b.empty())
return false;
ss1 << hex << a;
ss2 << hex << b;
ss1 >> sres1;
ss2 >> sres2;
LOG(DEBUG) << "sres1 = " << sres1;
LOG(DEBUG) << "sres2 = " << sres2;
return (sres1 == sres2);
}
bool randEqual(string a, string b)
{
uint64_t rand1h = 0;
uint64_t rand1l = 0;
uint64_t rand2h = 0;
uint64_t rand2l = 0;
if (a.empty() || b.empty())
return false;
gSubscriberRegistry.stringToUint(a, &rand1h, &rand1l);
gSubscriberRegistry.stringToUint(b, &rand2h, &rand2l);
LOG(DEBUG) << "rand1h = " << rand1h << ", rand1l = " << rand1l;
LOG(DEBUG) << "rand2h = " << rand2h << ", rand2l = " << rand2l;
return (rand1h == rand2h) && (rand1l == rand2l);
}
// verify sres given rand and imsi's ki
// may set kc
// may cache sres and rand
bool authenticate(string imsi, string randx, string sres, string *kc)
{
string ki = imsiGet(imsi, "ki");
bool ret;
if (ki.length() == 0) {
// Ki is unknown
string upstream_server = gConfig.getStr("SubscriberRegistry.UpstreamServer");
if (upstream_server.length()) {
LOG(INFO) << "ki unknown, upstream server";
// there's an upstream server for authentication.
// TODO - call the upstream server
ret = false;
} else {
// there's no upstream server for authentication. fake it.
string sres2 = imsiGet(imsi, "sres");
if (sres2.length() == 0) {
LOG(INFO) << "ki unknown, no upstream server, sres not cached";
// first time - cache sres and rand so next time
// correct cell phone will calc same sres from same rand
imsiSet(imsi, "sres", sres);
imsiSet(imsi, "rand", randx);
ret = true;
} else {
LOG(INFO) << "ki unknown, no upstream server, sres cached";
// check against cached values of rand and sres
string rand2 = imsiGet(imsi, "rand");
// TODO - on success, compute and return kc
LOG(DEBUG) << "comparing " << sres << " to " << sres2 << " and " << randx << " to " << rand2;
ret = sresEqual(sres, sres2) && randEqual(randx, rand2);
}
}
} else {
LOG(INFO) << "ki known";
// Ki is known, so do normal authentication
ostringstream os;
// per user value from subscriber registry
string a3a8 = imsiGet(imsi, "a3_a8");
if (a3a8.length() == 0) {
// config value is default
a3a8 = gConfig.getStr("SubscriberRegistry.A3A8");
}
os << a3a8 << " 0x" << ki << " 0x" << randx;
// must not put ki into the log
// LOG(INFO) << "running " << os.str();
FILE *f = popen(os.str().c_str(), "r");
if (f == NULL) {
LOG(CRIT) << "error: popen failed";
return false;
}
char sres2[26];
char *str = fgets(sres2, 26, f);
if (str != NULL && strlen(str) == 25) str[24] = 0;
if (str == NULL || strlen(str) != 24) {
LOG(CRIT) << "error: popen result failed";
return false;
}
int st = pclose(f);
if (st == -1) {
LOG(CRIT) << "error: pclose failed";
return false;
}
// first 8 chars are SRES; rest are Kc
*kc = sres2+8;
sres2[8] = 0;
LOG(INFO) << "result = " << sres2;
ret = sresEqual(sres, sres2);
}
LOG(INFO) << "returning = " << ret;
return ret;
}
void decodeQuery(map<string,string> &args)
{
string query;
// this works for GET or POST.
// get the request method
char *g = getenv("REQUEST_METHOD");
string method = g ? g : "";
LOG(INFO) << "REQUEST_METHOD = " << g;
// if POST, then read from stdin the number of bytes specified in CONTENT_LENGTH, and that's the query
if (method == "POST") {
int lth = atoi(getenv("CONTENT_LENGTH"));
LOG(INFO) << "CONTENT_LENGTH = " << lth;
char *buf = new char[lth+1];
cin.get(buf, lth+1);
int nread = cin.gcount();
if (nread != lth) {
LOG(ERR) << "content length changed to " << nread;
lth = nread;
}
query = string(buf, lth);
LOG(INFO) << "QUERY = " << query;
delete[] buf;
// if GET, then the query is in the environment variable QUERY_STRING
} else if (method == "GET") {
char *q = getenv("QUERY_STRING");
query = q ? q : "";
LOG(INFO) << "QUERY_STRING = " << q;
}
if (query.length() != 0) {
// fields of http request are separated with "&"
vector<string> fields;
split('&', query, &fields);
vector<string>::iterator it;
for (it = fields.begin(); it != fields.end(); it++) {
string field = *it;
size_t p = field.find('=');
string key = field.substr(0, p);
string value = field.substr(p+1);
p = 0;
while (1) {
size_t q = value.find('%', p);
if (q == string::npos) break;
string hex = value.substr(q+1, 2);
char s[2];
strcpy(s, "x");
int i;
sscanf(hex.c_str(), "%x", &i);
s[0] = i;
string hexx = s;
value.replace(q, 3, hexx);
}
args[key] = value;
}
}
}
string join(string separator, vector<string> &strings)
{
string result("");
vector<string>::iterator it;
for (it = strings.begin(); it != strings.end(); it++) {
if (it != strings.begin()) result.append(separator);
result.append(*it);
}
return result;
}
void split(char separator, string tosplit, vector<string> *fields)
{
int p = 0;
while (1) {
size_t q = tosplit.find(separator, p);
if (q == string::npos) {
fields->push_back(tosplit.substr(p));
break;
}
fields->push_back(tosplit.substr(p, q-p));
p = q+1;
}
}