-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubscriberserver.cpp
More file actions
executable file
·193 lines (171 loc) · 4.84 KB
/
subscriberserver.cpp
File metadata and controls
executable file
·193 lines (171 loc) · 4.84 KB
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
/*
* 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 <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <sqlite3.h>
#include <time.h>
#include "Configuration.h"
#include "Logger.h"
#include <string.h>
#include "servershare.h"
#include "SubscriberRegistry.h"
using namespace std;
ConfigurationTable gConfig("/etc/OpenBTS/OpenBTS.db", "subscriberserver", getConfigurationKeys());
Log dummy("subscriberserver",gConfig.getStr("Log.Level").c_str(),LOG_LOCAL7);
// just using this for the database access
SubscriberRegistry gSubscriberRegistry;
// map of http query parameters and values
map<string,string> gArgs;
// lines of http response
vector<string> gResponse;
// retrieve a query field from args
string getArg(string label)
{
if (gArgs.find(label) == gArgs.end()) {
LOG(ERR) << "error: " << label << " missing";
return "";
}
return gArgs[label];
}
// run an sql statement through sqlite3 to get a response
void generateSqlResponse()
{
string stmts = getArg("stmts");
vector<string> vstmts;
split(';', stmts, &vstmts);
vector<string>::iterator it;
for (it = vstmts.begin(); it != vstmts.end(); it++) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_statement(gSubscriberRegistry.db(), &stmt, it->c_str())) {
LOG(ERR) << "sqlite3_prepare_statement problem - statement: " << it->c_str();
return;
}
int src = sqlite3_run_query(gSubscriberRegistry.db(), stmt);
while (src == SQLITE_ROW) {
string resp = "res=";
int cols = sqlite3_column_count(stmt);
for (int i = 0; i < cols; i++) {
resp.append((const char*)sqlite3_column_text(stmt, i));
}
gResponse.push_back(resp);
src = sqlite3_run_query(gSubscriberRegistry.db(), stmt);
}
}
}
void sresCheck(bool b)
{
if (b) {
// SRESs match. return success (or Kc (TODO))
gResponse.push_back("status=SUCCESS");
} else {
// SRESs don't match. return failure.
gResponse.push_back("status=FAILURE");
}
}
// authenticate
void generateAuthResponse()
{
string imsi = getArg("imsi");
string randx = getArg("rand");
string sres = getArg("sres");
string kc;
bool st = authenticate(imsi, randx, sres, &kc);
sresCheck(st);
}
// generate a 128' random number
void generateRandResponse()
{
string imsi = getArg("imsi");
string randx = generateRand(imsi);
gResponse.push_back("rand=" + randx);
gResponse.push_back("imsi=" + imsi);
}
// generate our http response, putting each line of it into vector response
void generateResponse()
{
if (gArgs.find("req") == gArgs.end()) {
LOG(ERR) << "req not specified";
return;
}
// check for request types
string req = gArgs["req"];
if (req == "sql") {
generateSqlResponse();
return;
}
if (req == "rand") {
generateRandResponse();
return;
}
if (req == "auth") {
generateAuthResponse();
return;
}
// if none of the above, then it's an error
LOG(ERR) << "unrecognized request";
}
// write the query to the log file
void logQuery()
{
// list query key->value pairs
map<string,string>::iterator it;
LOG(INFO) << "query";
for (it = gArgs.begin(); it != gArgs.end(); it++) {
LOG(INFO) << " " << it->first << " -> " << it->second;
}
}
// write the http response from the global array @response to the log file
void logResponse()
{
LOG(INFO) << "response";
vector<string>::iterator it;
for (it = gResponse.begin(); it != gResponse.end(); it++) {
LOG(INFO) << " " << *it;
}
}
// print the http response to stdout
void respond()
{
vector<string>::iterator it;
for (it = gResponse.begin(); it != gResponse.end(); it++) {
cout << *it << endl;
}
}
int main()
{
cout << "Content-Type: text\n\n";
srand ( time(NULL) + (int)getpid() );
// decode the http query
decodeQuery(gArgs);
// write the http query into the log file
logQuery();
// put the http response into the global array @response
generateResponse();
// write the http response to the log file
logResponse();
// print out the http response to stdout
respond();
}