blob: fb10f3e540edfa88bb586debe0e97e6ed6d1dc91 [file] [log] [blame]
Neels Hofmeyr73d14af2017-10-24 23:26:53 +02001/* (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * All Rights Reserved
4 *
5 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <stdlib.h>
23#include <signal.h>
24#include <stdio.h>
25#include <getopt.h>
26#include <inttypes.h>
27#include <string.h>
28
29#include <osmocom/core/logging.h>
30#include <osmocom/core/application.h>
31
32#include "logging.h"
33#include "db.h"
34#include "rand.h"
35
36struct hlr_db_tool_ctx {
37 /* DB context */
38 struct db_context *dbc;
39};
40
41struct hlr_db_tool_ctx *g_hlr_db_tool_ctx;
42
43static struct {
44 const char *db_file;
45 bool bootstrap;
46 const char *import_nitb_db;
47} cmdline_opts = {
48 .db_file = "hlr.db",
49};
50
51static void print_help()
52{
53 printf("\n");
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +010054 printf("Usage: osmo-hlr-db-tool [-l <hlr.db>] [create|import-nitb-db <nitb.db>]\n");
Neels Hofmeyr73d14af2017-10-24 23:26:53 +020055 printf(" -l --database db-name The OsmoHLR database to use, default '%s'.\n",
56 cmdline_opts.db_file);
57 printf(" -h --help This text.\n");
58 printf(" -d option --debug=DMAIN:DDB:DAUC Enable debugging.\n");
59 printf(" -s --disable-color Do not print ANSI colors in the log\n");
60 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
61 printf(" -e --log-level number Set a global loglevel.\n");
62 printf(" -V --version Print the version of OsmoHLR-db-tool.\n");
63 printf("\n");
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +010064 printf("Commands:\n");
65 printf("\n");
66 printf(" create Create an empty OsmoHLR database.\n");
67 printf(" (All commands imply this if none exists yet.)\n");
68 printf("\n");
Neels Hofmeyrc82e6ad2017-11-10 16:58:42 +010069 printf(" import-nitb-db <nitb.db> Add OsmoNITB db's subscribers to OsmoHLR db.\n");
Neels Hofmeyr73d14af2017-10-24 23:26:53 +020070 printf(" Be aware that the import is lossy, only the\n");
71 printf(" IMSI, MSISDN, nam_cs/ps and 2G auth data are set.\n");
72}
73
74static void print_version(int print_copyright)
75{
76 printf("OsmoHLR-db-tool version %s\n", PACKAGE_VERSION);
77 if (print_copyright)
78 printf("\n"
79 "Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH\n"
80 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\n"
81 "This is free software: you are free to change and redistribute it.\n"
82 "There is NO WARRANTY, to the extent permitted by law.\n"
83 "\n");
84}
85
86static void handle_options(int argc, char **argv)
87{
88 const char *cmd;
89
90 while (1) {
91 int option_index = 0, c;
92 static struct option long_options[] = {
93 {"help", 0, 0, 'h'},
94 {"database", 1, 0, 'l'},
95 {"debug", 1, 0, 'd'},
96 {"disable-color", 0, 0, 's'},
97 {"timestamp", 0, 0, 'T'},
98 {"log-level", 1, 0, 'e'},
99 {"version", 0, 0, 'V' },
100 {0, 0, 0, 0}
101 };
102
103 c = getopt_long(argc, argv, "hl:d:sTe:V",
104 long_options, &option_index);
105 if (c == -1)
106 break;
107
108 switch (c) {
109 case 'h':
110 print_help();
111 exit(EXIT_SUCCESS);
112 case 'l':
113 cmdline_opts.db_file = optarg;
114 break;
115 case 'd':
116 log_parse_category_mask(osmo_stderr_target, optarg);
117 break;
118 case 's':
119 log_set_use_color(osmo_stderr_target, 0);
120 break;
121 case 'T':
122 log_set_print_timestamp(osmo_stderr_target, 1);
123 break;
124 case 'e':
125 log_set_log_level(osmo_stderr_target, atoi(optarg));
126 break;
127 case 'V':
128 print_version(1);
129 exit(EXIT_SUCCESS);
130 break;
131 default:
132 /* catch unknown options *as well as* missing arguments. */
133 fprintf(stderr, "Error in command line options. Exiting.\n");
134 exit(EXIT_FAILURE);
135 break;
136 }
137 }
138
139 if (argc - optind <= 0) {
140 fprintf(stderr, "Error: You must specify a command.\n");
141 print_help();
142 exit(EXIT_FAILURE);
143 }
144
145 cmd = argv[optind++];
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200146
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +0100147 if (!strcmp(cmd, "create")) {
148 /* Nothing to do, just run the main program to open the database without running any
149 * action, which will bootstrap all tables. */
150 } else if (!strcmp(cmd, "import-nitb-db")) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200151 if (argc - optind < 1) {
152 fprintf(stderr, "You must specify an input db file\n");
153 print_help();
154 exit(EXIT_FAILURE);
155 }
156 cmdline_opts.import_nitb_db = argv[optind++];
157 } else {
158 fprintf(stderr, "Error: Unknown command `%s'\n", cmd);
159 print_help();
160 exit(EXIT_FAILURE);
161 }
162}
163
164static void signal_hdlr(int signal)
165{
166 switch (signal) {
167 case SIGINT:
168 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
169 db_close(g_hlr_db_tool_ctx->dbc);
170 log_fini();
171 talloc_report_full(g_hlr_db_tool_ctx, stderr);
172 exit(EXIT_SUCCESS);
173 break;
174 case SIGUSR1:
175 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
176 talloc_report_full(g_hlr_db_tool_ctx, stderr);
177 break;
178 }
179}
180
181sqlite3 *open_nitb_db(const char *filename)
182{
183 int rc;
184 sqlite3 *nitb_db = NULL;
185
186 rc = sqlite3_open(filename, &nitb_db);
187 if (rc != SQLITE_OK) {
188 LOGP(DDB, LOGL_ERROR, "Unable to open OsmoNITB DB %s; rc = %d\n", filename, rc);
189 return NULL;
190 }
191
192 return nitb_db;
193}
194
195enum nitb_stmt {
196 NITB_SELECT_SUBSCR,
197 NITB_SELECT_AUTH_KEYS,
198};
199
200static const char *nitb_stmt_sql[] = {
201 [NITB_SELECT_SUBSCR] =
202 "SELECT imsi, id, extension, authorized"
203 " FROM Subscriber"
204 " ORDER BY id",
205 [NITB_SELECT_AUTH_KEYS] =
206 "SELECT algorithm_id, a3a8_ki from authkeys"
207 " WHERE subscriber_id = $subscr_id",
208};
209
210sqlite3_stmt *nitb_stmt[ARRAY_SIZE(nitb_stmt_sql)] = {};
211
212size_t _dbd_decode_binary(const unsigned char *in, unsigned char *out);
213
214void import_nitb_subscr_aud(sqlite3 *nitb_db, const char *imsi, int64_t nitb_id, int64_t hlr_id)
215{
216 int rc;
217 struct db_context *dbc = g_hlr_db_tool_ctx->dbc;
218 sqlite3_stmt *stmt;
219
220 int count = 0;
221
222 stmt = nitb_stmt[NITB_SELECT_AUTH_KEYS];
223 if (!db_bind_int(stmt, NULL, nitb_id))
224 return;
225
226 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
227 const void *blob;
228 unsigned int blob_size;
229 static unsigned char buf[4096];
230 static char ki[128];
231 int decoded_size;
232 struct sub_auth_data_str aud2g = {
233 .type = OSMO_AUTH_TYPE_GSM,
234 .algo = OSMO_AUTH_ALG_NONE,
235 .u.gsm.ki = ki,
236 };
237
238 aud2g.algo = sqlite3_column_int(stmt, 0);
239
240 if (count) {
241 LOGP(DDB, LOGL_ERROR,
242 "Warning: subscriber has more than one auth key,"
243 " importing only the first key, for IMSI=%s\n",
244 imsi);
245 break;
246 }
247
248 blob = sqlite3_column_blob(stmt, 1);
249 blob_size = sqlite3_column_bytes(stmt, 1);
250
251 if (blob_size > sizeof(buf)) {
252 LOGP(DDB, LOGL_ERROR,
253 "OsmoNITB import to %s: Cannot import auth data for IMSI %s:"
254 " too large blob: %u\n",
255 dbc->fname, imsi, blob_size);
256 db_remove_reset(stmt);
257 continue;
258 }
259
260 decoded_size = _dbd_decode_binary(blob, buf);
261 osmo_strlcpy(ki, osmo_hexdump_nospc(buf, decoded_size), sizeof(ki));
262
263 db_subscr_update_aud_by_id(dbc, hlr_id, &aud2g);
264 count ++;
265 }
266
267 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
268 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: SQL error: (%d) %s,"
269 " during stmt '%s'",
270 rc, sqlite3_errmsg(nitb_db),
271 nitb_stmt_sql[NITB_SELECT_AUTH_KEYS]);
272 }
273
274 db_remove_reset(stmt);
275}
276
277void import_nitb_subscr(sqlite3 *nitb_db, sqlite3_stmt *stmt)
278{
279 struct db_context *dbc = g_hlr_db_tool_ctx->dbc;
280 int rc;
281 struct hlr_subscriber subscr;
282
283 int64_t nitb_id;
284 int64_t imsi;
285 char imsi_str[32];
286 bool authorized;
287
288 imsi = sqlite3_column_int64(stmt, 0);
289
290 snprintf(imsi_str, sizeof(imsi_str), "%"PRId64, imsi);
291
292 rc = db_subscr_create(dbc, imsi_str);
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100293 if (rc < 0) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200294 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB import to %s: failed to create IMSI %s: %d: %s\n",
295 dbc->fname,
296 imsi_str,
297 rc,
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100298 strerror(-rc));
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200299 /* on error, still attempt to continue */
300 }
301
302 nitb_id = sqlite3_column_int64(stmt, 1);
303 copy_sqlite3_text_to_buf(subscr.msisdn, stmt, 2);
304 authorized = sqlite3_column_int(stmt, 3) ? true : false;
305
306 db_subscr_update_msisdn_by_imsi(dbc, imsi_str, subscr.msisdn);
307 db_subscr_nam(dbc, imsi_str, authorized, true);
308 db_subscr_nam(dbc, imsi_str, authorized, false);
309
310 /* find the just created id */
311 rc = db_subscr_get_by_imsi(dbc, imsi_str, &subscr);
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100312 if (rc < 0) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200313 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB import to %s: created IMSI %s,"
314 " but failed to get new subscriber id: %d: %s\n",
315 dbc->fname,
316 imsi_str,
317 rc,
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100318 strerror(-rc));
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200319 return;
320 }
321
322 OSMO_ASSERT(!strcmp(imsi_str, subscr.imsi));
323
324 import_nitb_subscr_aud(nitb_db, imsi_str, nitb_id, subscr.id);
325}
326
327int import_nitb_db(void)
328{
329 int i;
330 int ret;
331 int rc;
332 const char *sql;
333 sqlite3_stmt *stmt;
334
335 sqlite3 *nitb_db = open_nitb_db(cmdline_opts.import_nitb_db);
336
337 if (!nitb_db)
338 return -1;
339 ret = 0;
340
341 for (i = 0; i < ARRAY_SIZE(nitb_stmt_sql); i++) {
342 sql = nitb_stmt_sql[i];
343 rc = sqlite3_prepare_v2(nitb_db, sql, -1, &nitb_stmt[i], NULL);
344 if (rc != SQLITE_OK) {
345 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: Unable to prepare SQL statement '%s'\n", sql);
346 ret = -1;
347 goto out_free;
348 }
349 }
350
351 stmt = nitb_stmt[NITB_SELECT_SUBSCR];
352
353 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
354 import_nitb_subscr(nitb_db, stmt);
355 /* On failure, carry on with the rest. */
356 }
357 if (rc != SQLITE_DONE) {
358 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: SQL error: (%d) %s,"
359 " during stmt '%s'",
360 rc, sqlite3_errmsg(nitb_db),
361 nitb_stmt_sql[NITB_SELECT_SUBSCR]);
362 goto out_free;
363 }
364
365 db_remove_reset(stmt);
366 sqlite3_finalize(stmt);
367
368out_free:
369 sqlite3_close(nitb_db);
370 return ret;
371}
372
373int main(int argc, char **argv)
374{
375 int rc;
376 int (*main_action)(void);
377 main_action = NULL;
378
379 g_hlr_db_tool_ctx = talloc_zero(NULL, struct hlr_db_tool_ctx);
380 OSMO_ASSERT(g_hlr_db_tool_ctx);
381 talloc_set_name_const(g_hlr_db_tool_ctx, "OsmoHLR-db-tool");
382
383 rc = osmo_init_logging(&hlr_log_info);
384 if (rc < 0) {
385 fprintf(stderr, "Error initializing logging\n");
386 exit(EXIT_FAILURE);
387 }
388
389 handle_options(argc, argv);
390
391 if (cmdline_opts.import_nitb_db) {
392 if (main_action)
393 goto too_many_actions;
394 main_action = import_nitb_db;
395 }
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +0100396 /* Future: add more main_actions, besides import-nitb-db, here.
397 * For command 'create', no action is required. */
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200398
399 /* Just in case any db actions need randomness */
400 rc = rand_init();
401 if (rc < 0) {
402 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
403 exit(EXIT_FAILURE);
404 }
405
406 g_hlr_db_tool_ctx->dbc = db_open(g_hlr_db_tool_ctx, cmdline_opts.db_file);
407 if (!g_hlr_db_tool_ctx->dbc) {
408 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
409 exit(EXIT_FAILURE);
410 }
411
412 osmo_init_ignore_signals();
413 signal(SIGINT, &signal_hdlr);
414 signal(SIGUSR1, &signal_hdlr);
415
416 rc = 0;
417 if (main_action)
418 rc = (*main_action)();
419
420 db_close(g_hlr_db_tool_ctx->dbc);
421 log_fini();
422 exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
423
424too_many_actions:
425 fprintf(stderr, "Too many actions requested.\n");
426 log_fini();
427 exit(EXIT_FAILURE);
428}
429
430/* stubs */
431void lu_op_alloc_conn(void) { OSMO_ASSERT(0); }
432void lu_op_tx_del_subscr_data(void) { OSMO_ASSERT(0); }
433void lu_op_free(void) { OSMO_ASSERT(0); }