blob: 13281f2bcb66f0a3418ac228fee29e7262415ff3 [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 Hofmeyr73d14af2017-10-24 23:26:53 +020069 printf(" import-nitb-db db Add OsmoNITB db's subscribers to OsmoHLR db.\n");
70 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++];
146 printf("command '%s', %d extra arguments\n", cmd, argc - optind);
147
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +0100148 if (!strcmp(cmd, "create")) {
149 /* Nothing to do, just run the main program to open the database without running any
150 * action, which will bootstrap all tables. */
151 } else if (!strcmp(cmd, "import-nitb-db")) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200152 if (argc - optind < 1) {
153 fprintf(stderr, "You must specify an input db file\n");
154 print_help();
155 exit(EXIT_FAILURE);
156 }
157 cmdline_opts.import_nitb_db = argv[optind++];
158 } else {
159 fprintf(stderr, "Error: Unknown command `%s'\n", cmd);
160 print_help();
161 exit(EXIT_FAILURE);
162 }
163}
164
165static void signal_hdlr(int signal)
166{
167 switch (signal) {
168 case SIGINT:
169 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
170 db_close(g_hlr_db_tool_ctx->dbc);
171 log_fini();
172 talloc_report_full(g_hlr_db_tool_ctx, stderr);
173 exit(EXIT_SUCCESS);
174 break;
175 case SIGUSR1:
176 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
177 talloc_report_full(g_hlr_db_tool_ctx, stderr);
178 break;
179 }
180}
181
182sqlite3 *open_nitb_db(const char *filename)
183{
184 int rc;
185 sqlite3 *nitb_db = NULL;
186
187 rc = sqlite3_open(filename, &nitb_db);
188 if (rc != SQLITE_OK) {
189 LOGP(DDB, LOGL_ERROR, "Unable to open OsmoNITB DB %s; rc = %d\n", filename, rc);
190 return NULL;
191 }
192
193 return nitb_db;
194}
195
196enum nitb_stmt {
197 NITB_SELECT_SUBSCR,
198 NITB_SELECT_AUTH_KEYS,
199};
200
201static const char *nitb_stmt_sql[] = {
202 [NITB_SELECT_SUBSCR] =
203 "SELECT imsi, id, extension, authorized"
204 " FROM Subscriber"
205 " ORDER BY id",
206 [NITB_SELECT_AUTH_KEYS] =
207 "SELECT algorithm_id, a3a8_ki from authkeys"
208 " WHERE subscriber_id = $subscr_id",
209};
210
211sqlite3_stmt *nitb_stmt[ARRAY_SIZE(nitb_stmt_sql)] = {};
212
213size_t _dbd_decode_binary(const unsigned char *in, unsigned char *out);
214
215void import_nitb_subscr_aud(sqlite3 *nitb_db, const char *imsi, int64_t nitb_id, int64_t hlr_id)
216{
217 int rc;
218 struct db_context *dbc = g_hlr_db_tool_ctx->dbc;
219 sqlite3_stmt *stmt;
220
221 int count = 0;
222
223 stmt = nitb_stmt[NITB_SELECT_AUTH_KEYS];
224 if (!db_bind_int(stmt, NULL, nitb_id))
225 return;
226
227 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
228 const void *blob;
229 unsigned int blob_size;
230 static unsigned char buf[4096];
231 static char ki[128];
232 int decoded_size;
233 struct sub_auth_data_str aud2g = {
234 .type = OSMO_AUTH_TYPE_GSM,
235 .algo = OSMO_AUTH_ALG_NONE,
236 .u.gsm.ki = ki,
237 };
238
239 aud2g.algo = sqlite3_column_int(stmt, 0);
240
241 if (count) {
242 LOGP(DDB, LOGL_ERROR,
243 "Warning: subscriber has more than one auth key,"
244 " importing only the first key, for IMSI=%s\n",
245 imsi);
246 break;
247 }
248
249 blob = sqlite3_column_blob(stmt, 1);
250 blob_size = sqlite3_column_bytes(stmt, 1);
251
252 if (blob_size > sizeof(buf)) {
253 LOGP(DDB, LOGL_ERROR,
254 "OsmoNITB import to %s: Cannot import auth data for IMSI %s:"
255 " too large blob: %u\n",
256 dbc->fname, imsi, blob_size);
257 db_remove_reset(stmt);
258 continue;
259 }
260
261 decoded_size = _dbd_decode_binary(blob, buf);
262 osmo_strlcpy(ki, osmo_hexdump_nospc(buf, decoded_size), sizeof(ki));
263
264 db_subscr_update_aud_by_id(dbc, hlr_id, &aud2g);
265 count ++;
266 }
267
268 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
269 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: SQL error: (%d) %s,"
270 " during stmt '%s'",
271 rc, sqlite3_errmsg(nitb_db),
272 nitb_stmt_sql[NITB_SELECT_AUTH_KEYS]);
273 }
274
275 db_remove_reset(stmt);
276}
277
278void import_nitb_subscr(sqlite3 *nitb_db, sqlite3_stmt *stmt)
279{
280 struct db_context *dbc = g_hlr_db_tool_ctx->dbc;
281 int rc;
282 struct hlr_subscriber subscr;
283
284 int64_t nitb_id;
285 int64_t imsi;
286 char imsi_str[32];
287 bool authorized;
288
289 imsi = sqlite3_column_int64(stmt, 0);
290
291 snprintf(imsi_str, sizeof(imsi_str), "%"PRId64, imsi);
292
293 rc = db_subscr_create(dbc, imsi_str);
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100294 if (rc < 0) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200295 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB import to %s: failed to create IMSI %s: %d: %s\n",
296 dbc->fname,
297 imsi_str,
298 rc,
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100299 strerror(-rc));
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200300 /* on error, still attempt to continue */
301 }
302
303 nitb_id = sqlite3_column_int64(stmt, 1);
304 copy_sqlite3_text_to_buf(subscr.msisdn, stmt, 2);
305 authorized = sqlite3_column_int(stmt, 3) ? true : false;
306
307 db_subscr_update_msisdn_by_imsi(dbc, imsi_str, subscr.msisdn);
308 db_subscr_nam(dbc, imsi_str, authorized, true);
309 db_subscr_nam(dbc, imsi_str, authorized, false);
310
311 /* find the just created id */
312 rc = db_subscr_get_by_imsi(dbc, imsi_str, &subscr);
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100313 if (rc < 0) {
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200314 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB import to %s: created IMSI %s,"
315 " but failed to get new subscriber id: %d: %s\n",
316 dbc->fname,
317 imsi_str,
318 rc,
Neels Hofmeyr87a04b62017-11-07 13:20:44 +0100319 strerror(-rc));
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200320 return;
321 }
322
323 OSMO_ASSERT(!strcmp(imsi_str, subscr.imsi));
324
325 import_nitb_subscr_aud(nitb_db, imsi_str, nitb_id, subscr.id);
326}
327
328int import_nitb_db(void)
329{
330 int i;
331 int ret;
332 int rc;
333 const char *sql;
334 sqlite3_stmt *stmt;
335
336 sqlite3 *nitb_db = open_nitb_db(cmdline_opts.import_nitb_db);
337
338 if (!nitb_db)
339 return -1;
340 ret = 0;
341
342 for (i = 0; i < ARRAY_SIZE(nitb_stmt_sql); i++) {
343 sql = nitb_stmt_sql[i];
344 rc = sqlite3_prepare_v2(nitb_db, sql, -1, &nitb_stmt[i], NULL);
345 if (rc != SQLITE_OK) {
346 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: Unable to prepare SQL statement '%s'\n", sql);
347 ret = -1;
348 goto out_free;
349 }
350 }
351
352 stmt = nitb_stmt[NITB_SELECT_SUBSCR];
353
354 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
355 import_nitb_subscr(nitb_db, stmt);
356 /* On failure, carry on with the rest. */
357 }
358 if (rc != SQLITE_DONE) {
359 LOGP(DDB, LOGL_ERROR, "OsmoNITB DB: SQL error: (%d) %s,"
360 " during stmt '%s'",
361 rc, sqlite3_errmsg(nitb_db),
362 nitb_stmt_sql[NITB_SELECT_SUBSCR]);
363 goto out_free;
364 }
365
366 db_remove_reset(stmt);
367 sqlite3_finalize(stmt);
368
369out_free:
370 sqlite3_close(nitb_db);
371 return ret;
372}
373
374int main(int argc, char **argv)
375{
376 int rc;
377 int (*main_action)(void);
378 main_action = NULL;
379
380 g_hlr_db_tool_ctx = talloc_zero(NULL, struct hlr_db_tool_ctx);
381 OSMO_ASSERT(g_hlr_db_tool_ctx);
382 talloc_set_name_const(g_hlr_db_tool_ctx, "OsmoHLR-db-tool");
383
384 rc = osmo_init_logging(&hlr_log_info);
385 if (rc < 0) {
386 fprintf(stderr, "Error initializing logging\n");
387 exit(EXIT_FAILURE);
388 }
389
390 handle_options(argc, argv);
391
392 if (cmdline_opts.import_nitb_db) {
393 if (main_action)
394 goto too_many_actions;
395 main_action = import_nitb_db;
396 }
Neels Hofmeyr0959e8b2017-11-10 16:58:00 +0100397 /* Future: add more main_actions, besides import-nitb-db, here.
398 * For command 'create', no action is required. */
Neels Hofmeyr73d14af2017-10-24 23:26:53 +0200399
400 /* Just in case any db actions need randomness */
401 rc = rand_init();
402 if (rc < 0) {
403 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
404 exit(EXIT_FAILURE);
405 }
406
407 g_hlr_db_tool_ctx->dbc = db_open(g_hlr_db_tool_ctx, cmdline_opts.db_file);
408 if (!g_hlr_db_tool_ctx->dbc) {
409 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
410 exit(EXIT_FAILURE);
411 }
412
413 osmo_init_ignore_signals();
414 signal(SIGINT, &signal_hdlr);
415 signal(SIGUSR1, &signal_hdlr);
416
417 rc = 0;
418 if (main_action)
419 rc = (*main_action)();
420
421 db_close(g_hlr_db_tool_ctx->dbc);
422 log_fini();
423 exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
424
425too_many_actions:
426 fprintf(stderr, "Too many actions requested.\n");
427 log_fini();
428 exit(EXIT_FAILURE);
429}
430
431/* stubs */
432void lu_op_alloc_conn(void) { OSMO_ASSERT(0); }
433void lu_op_tx_del_subscr_data(void) { OSMO_ASSERT(0); }
434void lu_op_free(void) { OSMO_ASSERT(0); }