blob: 9e3fb362f77e1ff0b9074936ac3d26750d60a9ec [file] [log] [blame]
Holger Freyther12aa50d2009-01-01 18:02:05 +00001/* Simple HLR/VLR database backend using dbi */
Jan Luebbefaaa49c2008-12-27 01:07:07 +00002/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00003 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltec2e302d2009-07-05 14:08:13 +02004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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
Jan Luebbefaaa49c2008-12-27 01:07:07 +000010 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Jan Luebbefaaa49c2008-12-27 01:07:07 +000016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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/>.
Jan Luebbefaaa49c2008-12-27 01:07:07 +000019 *
20 */
21
Harald Weltef2b4cd72010-05-13 11:45:07 +020022#include <stdint.h>
23#include <inttypes.h>
Holger Freytherbde36102008-12-28 22:51:39 +000024#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000025#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000026#include <stdlib.h>
27#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000028#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029#include <dbi/dbi.h>
30
Harald Weltef2b4cd72010-05-13 11:45:07 +020031#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther28dcbc52010-12-22 18:21:14 +010032#include <openbsc/gsm_subscriber.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020033#include <openbsc/gsm_04_11.h>
34#include <openbsc/db.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020035#include <openbsc/debug.h>
Holger Hans Peter Freytherc5faf662010-12-22 18:16:01 +010036
Harald Welted3fa84d2016-04-20 17:50:17 +020037#include <osmocom/gsm/protocol/gsm_23_003.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010038#include <osmocom/core/talloc.h>
39#include <osmocom/core/statistics.h>
40#include <osmocom/core/rate_ctr.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020041
Daniel Willmanncdeb8152015-10-08 16:10:23 +020042#include <openssl/rand.h>
43
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010044/* Semi-Private-Interface (SPI) for the subscriber code */
45void subscr_direct_free(struct gsm_subscriber *subscr);
46
Holger Freytherbde36102008-12-28 22:51:39 +000047static char *db_basename = NULL;
48static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000049static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000050
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010051#define SCHEMA_REVISION "4"
Jan Luebbebfbdeec2012-12-27 00:27:16 +010052
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010053enum {
54 SCHEMA_META,
55 INSERT_META,
56 SCHEMA_SUBSCRIBER,
57 SCHEMA_AUTH,
58 SCHEMA_EQUIPMENT,
59 SCHEMA_EQUIPMENT_WATCH,
60 SCHEMA_SMS,
61 SCHEMA_VLR,
62 SCHEMA_APDU,
63 SCHEMA_COUNTERS,
64 SCHEMA_RATE,
65 SCHEMA_AUTHKEY,
66 SCHEMA_AUTHLAST,
67};
68
69static const char *create_stmts[] = {
70 [SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
Harald Welte7e310b12009-03-30 20:56:32 +000071 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
72 "key TEXT UNIQUE NOT NULL, "
73 "value TEXT NOT NULL"
74 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010075 [INSERT_META] = "INSERT OR IGNORE INTO Meta "
Harald Welte7e310b12009-03-30 20:56:32 +000076 "(key, value) "
77 "VALUES "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010078 "('revision', " SCHEMA_REVISION ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010079 [SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
Harald Welte7e310b12009-03-30 20:56:32 +000080 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
81 "created TIMESTAMP NOT NULL, "
82 "updated TIMESTAMP NOT NULL, "
83 "imsi NUMERIC UNIQUE NOT NULL, "
84 "name TEXT, "
85 "extension TEXT UNIQUE, "
86 "authorized INTEGER NOT NULL DEFAULT 0, "
87 "tmsi TEXT UNIQUE, "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010088 "lac INTEGER NOT NULL DEFAULT 0, "
89 "expire_lu TIMESTAMP DEFAULT NULL"
Harald Welte7e310b12009-03-30 20:56:32 +000090 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010091 [SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
Jan Luebbe31bef492009-08-12 14:31:14 +020092 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
93 "subscriber_id INTEGER UNIQUE NOT NULL, "
94 "created TIMESTAMP NOT NULL, "
95 "token TEXT UNIQUE NOT NULL"
96 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010097 [SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
Harald Welte7e310b12009-03-30 20:56:32 +000098 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
99 "created TIMESTAMP NOT NULL, "
100 "updated TIMESTAMP NOT NULL, "
101 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +0200102 "classmark1 NUMERIC, "
103 "classmark2 BLOB, "
104 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +0000105 "imei NUMERIC UNIQUE NOT NULL"
106 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100107 [SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
Harald Welte7e310b12009-03-30 20:56:32 +0000108 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
109 "created TIMESTAMP NOT NULL, "
110 "updated TIMESTAMP NOT NULL, "
111 "subscriber_id NUMERIC NOT NULL, "
112 "equipment_id NUMERIC NOT NULL, "
113 "UNIQUE (subscriber_id, equipment_id) "
114 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100115 [SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +0200116 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +0000117 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
118 "created TIMESTAMP NOT NULL, "
119 "sent TIMESTAMP, "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200120 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +0200121 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +0200122 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +0200123 "reply_path_req INTEGER NOT NULL, "
124 "status_rep_req INTEGER NOT NULL, "
125 "protocol_id INTEGER NOT NULL, "
126 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200127 "ud_hdr_ind INTEGER NOT NULL, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200128 "src_addr TEXT NOT NULL, "
129 "src_ton INTEGER NOT NULL, "
130 "src_npi INTEGER NOT NULL, "
131 "dest_addr TEXT NOT NULL, "
132 "dest_ton INTEGER NOT NULL, "
133 "dest_npi INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200134 "user_data BLOB, " /* TP-UD */
135 /* additional data, interpreted from SMS */
136 "header BLOB, " /* UD Header */
137 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000138 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100139 [SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
Holger Freytherc2995ea2009-04-19 06:35:23 +0000140 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
141 "created TIMESTAMP NOT NULL, "
142 "updated TIMESTAMP NOT NULL, "
143 "subscriber_id NUMERIC UNIQUE NOT NULL, "
144 "last_bts NUMERIC NOT NULL "
145 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100146 [SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
Harald Welte (local)026531e2009-08-16 10:40:10 +0200147 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
148 "created TIMESTAMP NOT NULL, "
149 "apdu_id_flags INTEGER NOT NULL, "
150 "subscriber_id INTEGER NOT NULL, "
151 "apdu BLOB "
152 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100153 [SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
Harald Welteffa55a42009-12-22 19:07:32 +0100154 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
155 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100156 "value INTEGER NOT NULL, "
157 "name TEXT NOT NULL "
Harald Welte09f7ad02009-12-24 09:42:07 +0100158 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100159 [SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
Harald Weltec1919862010-05-13 12:55:20 +0200160 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
161 "timestamp TIMESTAMP NOT NULL, "
162 "value INTEGER NOT NULL, "
163 "name TEXT NOT NULL, "
Harald Welted94d6a02010-05-14 17:38:47 +0200164 "idx INTEGER NOT NULL "
Harald Weltec1919862010-05-13 12:55:20 +0200165 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100166 [SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200167 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut77d334a2009-12-27 19:26:12 +0100168 "algorithm_id INTEGER NOT NULL, "
Harald Welte3606cc52009-12-05 15:13:22 +0530169 "a3a8_ki BLOB "
170 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100171 [SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200172 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut70881b72009-12-27 15:41:59 +0100173 "issued TIMESTAMP NOT NULL, "
174 "use_count INTEGER NOT NULL DEFAULT 0, "
175 "key_seq INTEGER NOT NULL, "
176 "rand BLOB NOT NULL, "
177 "sres BLOB NOT NULL, "
178 "kc BLOB NOT NULL "
Harald Welteffa55a42009-12-22 19:07:32 +0100179 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000180};
181
Harald Welte0b906d02009-12-24 11:21:42 +0100182void db_error_func(dbi_conn conn, void *data)
183{
184 const char *msg;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000185 dbi_conn_error(conn, &msg);
Harald Welteae1f1592009-12-24 11:39:14 +0100186 LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
Harald Weltec7548a12014-07-10 20:18:15 +0200187 osmo_log_backtrace(DDB, LOGL_ERROR);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000188}
189
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100190static int update_db_revision_2(void)
191{
192 dbi_result result;
193
194 result = dbi_conn_query(conn,
195 "ALTER TABLE Subscriber "
196 "ADD COLUMN expire_lu "
197 "TIMESTAMP DEFAULT NULL");
198 if (!result) {
199 LOGP(DDB, LOGL_ERROR,
Alexander Chemeris7e20f642014-03-07 16:59:53 +0100200 "Failed to alter table Subscriber (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100201 return -EINVAL;
202 }
203 dbi_result_free(result);
204
205 result = dbi_conn_query(conn,
206 "UPDATE Meta "
207 "SET value = '3' "
208 "WHERE key = 'revision'");
209 if (!result) {
210 LOGP(DDB, LOGL_ERROR,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100211 "Failed to update DB schema revision (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100212 return -EINVAL;
213 }
214 dbi_result_free(result);
215
216 return 0;
217}
218
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100219/**
220 * Copied from the normal sms_from_result_v3 to avoid having
221 * to make sure that the real routine will remain backward
222 * compatible.
223 */
224static struct gsm_sms *sms_from_result_v3(dbi_result result)
225{
226 struct gsm_sms *sms = sms_alloc();
227 long long unsigned int sender_id;
228 struct gsm_subscriber *sender;
229 const char *text, *daddr;
230 const unsigned char *user_data;
231 char buf[32];
232
233 if (!sms)
234 return NULL;
235
236 sms->id = dbi_result_get_ulonglong(result, "id");
237
238 sender_id = dbi_result_get_ulonglong(result, "sender_id");
239 snprintf(buf, sizeof(buf), "%llu", sender_id);
240 sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
241 OSMO_ASSERT(sender);
242 strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
243 subscr_direct_free(sender);
244 sender = NULL;
245
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +0200246 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
247 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
248 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
249 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
250 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100251 "data_coding_scheme");
252
253 daddr = dbi_result_get_string(result, "dest_addr");
254 if (daddr) {
255 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
256 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
257 }
258
259 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
260 user_data = dbi_result_get_binary(result, "user_data");
261 if (sms->user_data_len > sizeof(sms->user_data))
262 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
263 memcpy(sms->user_data, user_data, sms->user_data_len);
264
265 text = dbi_result_get_string(result, "text");
266 if (text) {
267 strncpy(sms->text, text, sizeof(sms->text));
268 sms->text[sizeof(sms->text)-1] = '\0';
269 }
270 return sms;
271}
272
273static int update_db_revision_3(void)
274{
275 dbi_result result;
276 struct gsm_sms *sms;
277
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100278 LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
279
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100280 result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
281 if (!result) {
282 LOGP(DDB, LOGL_ERROR,
283 "Failed to begin transaction (upgrade from rev 3)\n");
284 return -EINVAL;
285 }
286 dbi_result_free(result);
287
288 /* Rename old SMS table to be able create a new one */
289 result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
290 if (!result) {
291 LOGP(DDB, LOGL_ERROR,
292 "Failed to rename the old SMS table (upgrade from rev 3).\n");
293 goto rollback;
294 }
295 dbi_result_free(result);
296
297 /* Create new SMS table with all the bells and whistles! */
298 result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
299 if (!result) {
300 LOGP(DDB, LOGL_ERROR,
301 "Failed to create a new SMS table (upgrade from rev 3).\n");
302 goto rollback;
303 }
304 dbi_result_free(result);
305
306 /* Cycle through old messages and convert them to the new format */
Max5c06e402015-07-29 20:20:28 +0200307 result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100308 if (!result) {
309 LOGP(DDB, LOGL_ERROR,
310 "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
311 goto rollback;
312 }
313 while (dbi_result_next_row(result)) {
314 sms = sms_from_result_v3(result);
315 if (db_sms_store(sms) != 0) {
316 LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
317 sms_free(sms);
318 dbi_result_free(result);
319 goto rollback;
320 }
321 sms_free(sms);
322 }
323 dbi_result_free(result);
324
325 /* Remove the temporary table */
326 result = dbi_conn_query(conn, "DROP TABLE SMS_3");
327 if (!result) {
328 LOGP(DDB, LOGL_ERROR,
329 "Failed to drop the old SMS table (upgrade from rev 3).\n");
330 goto rollback;
331 }
332 dbi_result_free(result);
333
334 /* We're done. Bump DB Meta revision to 4 */
335 result = dbi_conn_query(conn,
336 "UPDATE Meta "
337 "SET value = '4' "
338 "WHERE key = 'revision'");
339 if (!result) {
340 LOGP(DDB, LOGL_ERROR,
341 "Failed to update DB schema revision (upgrade from rev 3).\n");
342 goto rollback;
343 }
344 dbi_result_free(result);
345
346 result = dbi_conn_query(conn, "COMMIT TRANSACTION");
347 if (!result) {
348 LOGP(DDB, LOGL_ERROR,
349 "Failed to commit the transaction (upgrade from rev 3)\n");
350 return -EINVAL;
351 }
352
353 /* Shrink DB file size by actually wiping out SMS_3 table data */
354 result = dbi_conn_query(conn, "VACUUM");
355 if (!result)
356 LOGP(DDB, LOGL_ERROR,
357 "VACUUM failed. Ignoring it (upgrade from rev 3).\n");
358 else
359 dbi_result_free(result);
360
361 return 0;
362
363rollback:
364 result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
365 if (!result)
366 LOGP(DDB, LOGL_ERROR,
367 "Rollback failed (upgrade from rev 3).\n");
368 else
369 dbi_result_free(result);
370 return -EINVAL;
371}
372
Harald Welted0b7b772009-08-09 19:03:42 +0200373static int check_db_revision(void)
374{
375 dbi_result result;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100376 const char *rev_s;
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600377 int db_rev = 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200378
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600379 /* Make a query */
Harald Welted0b7b772009-08-09 19:03:42 +0200380 result = dbi_conn_query(conn,
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600381 "SELECT value FROM Meta "
382 "WHERE key = 'revision'");
383
Harald Welted0b7b772009-08-09 19:03:42 +0200384 if (!result)
385 return -EINVAL;
386
387 if (!dbi_result_next_row(result)) {
388 dbi_result_free(result);
389 return -EINVAL;
390 }
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600391
392 /* Fetch the DB schema revision */
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100393 rev_s = dbi_result_get_string(result, "value");
394 if (!rev_s) {
Harald Welted0b7b772009-08-09 19:03:42 +0200395 dbi_result_free(result);
396 return -EINVAL;
397 }
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600398
399 if (!strcmp(rev_s, SCHEMA_REVISION)) {
400 /* Everything is fine */
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100401 dbi_result_free(result);
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600402 return 0;
403 }
404
405 db_rev = atoi(rev_s);
406 dbi_result_free(result);
407
408 /* Incremental migration waterfall */
409 switch (db_rev) {
410 case 2:
411 if (update_db_revision_2())
412 goto error;
413 case 3:
414 if (update_db_revision_3())
415 goto error;
416
417 /* The end of waterfall */
418 break;
419 default:
420 LOGP(DDB, LOGL_FATAL,
421 "Invalid database schema revision '%d'.\n", db_rev);
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100422 return -EINVAL;
423 }
424
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100425 return 0;
Vadim Yanitskiya8d8e932016-05-13 15:38:09 +0600426
427error:
428 LOGP(DDB, LOGL_FATAL, "Failed to update database "
429 "from schema revision '%d'.\n", db_rev);
430 return -EINVAL;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100431}
432
433static int db_configure(void)
434{
435 dbi_result result;
436
437 result = dbi_conn_query(conn,
438 "PRAGMA synchronous = FULL");
439 if (!result)
440 return -EINVAL;
Harald Welted0b7b772009-08-09 19:03:42 +0200441
442 dbi_result_free(result);
443 return 0;
444}
445
Harald Welte0b906d02009-12-24 11:21:42 +0100446int db_init(const char *name)
447{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000448 dbi_initialize(NULL);
Harald Welte0b906d02009-12-24 11:21:42 +0100449
Jan Luebbe5c15c852008-12-27 15:59:25 +0000450 conn = dbi_conn_new("sqlite3");
Harald Welte0b906d02009-12-24 11:21:42 +0100451 if (conn == NULL) {
Harald Welteae1f1592009-12-24 11:39:14 +0100452 LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000453 return 1;
454 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000455
Holger Freyther12aa50d2009-01-01 18:02:05 +0000456 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000457
Jan Luebbe5c15c852008-12-27 15:59:25 +0000458 /* MySQL
459 dbi_conn_set_option(conn, "host", "localhost");
460 dbi_conn_set_option(conn, "username", "your_name");
461 dbi_conn_set_option(conn, "password", "your_password");
462 dbi_conn_set_option(conn, "dbname", "your_dbname");
463 dbi_conn_set_option(conn, "encoding", "UTF-8");
464 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000465
Jan Luebbe5c15c852008-12-27 15:59:25 +0000466 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000467 db_basename = strdup(name);
468 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000469 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
470 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000471
Harald Welted0b7b772009-08-09 19:03:42 +0200472 if (dbi_conn_connect(conn) < 0)
473 goto out_err;
474
Jan Luebbe5c15c852008-12-27 15:59:25 +0000475 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200476
477out_err:
478 free(db_dirname);
479 free(db_basename);
480 db_dirname = db_basename = NULL;
481 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000482}
483
Harald Welted0b7b772009-08-09 19:03:42 +0200484
Harald Welted1476bc2011-07-16 13:24:09 +0200485int db_prepare(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100486{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000487 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000488 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000489
Harald Welte7e310b12009-03-30 20:56:32 +0000490 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
491 result = dbi_conn_query(conn, create_stmts[i]);
Harald Welte0b906d02009-12-24 11:21:42 +0100492 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100493 LOGP(DDB, LOGL_ERROR,
494 "Failed to create some table.\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000495 return 1;
496 }
497 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000498 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000499
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200500 if (check_db_revision() < 0) {
Harald Welteae1f1592009-12-24 11:39:14 +0100501 LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200502 "please update your database schema\n");
503 return -1;
504 }
505
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100506 db_configure();
507
Jan Luebbe5c15c852008-12-27 15:59:25 +0000508 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000509}
510
Harald Welted1476bc2011-07-16 13:24:09 +0200511int db_fini(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100512{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000513 dbi_conn_close(conn);
514 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000515
Harald Welte2c5f4c62011-07-16 13:22:57 +0200516 free(db_dirname);
517 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000518 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000519}
520
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200521struct gsm_subscriber *db_create_subscriber(const char *imsi)
Harald Welte9176bd42009-07-23 18:46:00 +0200522{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000523 dbi_result result;
Harald Welte0b906d02009-12-24 11:21:42 +0100524 struct gsm_subscriber *subscr;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000525
526 /* Is this subscriber known in the db? */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200527 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000528 if (subscr) {
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +0200529 subscr_put(subscr);
530 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000531 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000532
533 subscr = subscr_alloc();
534 if (!subscr)
535 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200536 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000537 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000538 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000539 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000540 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000541 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000542 imsi
543 );
Holger Hans Peter Freytheradb86752016-04-01 20:31:11 +0200544 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100545 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Hans Peter Freytheradb86752016-04-01 20:31:11 +0200546 subscr_put(subscr);
547 return NULL;
548 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000549 subscr->id = dbi_conn_sequence_last(conn, NULL);
Harald Welted3fa84d2016-04-20 17:50:17 +0200550 strncpy(subscr->imsi, imsi, sizeof(subscr->imsi)-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000551 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100552 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200553 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000554 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000555}
556
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200557osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200558
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200559static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
560{
561 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200562 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100563 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200564 const unsigned char *cm2, *cm3;
565 struct gsm_equipment *equip = &subscr->equipment;
566
567 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200568 "SELECT Equipment.* "
569 "FROM Equipment JOIN EquipmentWatch ON "
570 "EquipmentWatch.equipment_id=Equipment.id "
571 "WHERE EquipmentWatch.subscriber_id = %llu "
572 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200573 if (!result)
574 return -EIO;
575
576 if (!dbi_result_next_row(result)) {
577 dbi_result_free(result);
578 return -ENOENT;
579 }
580
581 equip->id = dbi_result_get_ulonglong(result, "id");
582
583 string = dbi_result_get_string(result, "imei");
584 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200585 strncpy(equip->imei, string, sizeof(equip->imei)-1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200586
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100587 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200588 if (string) {
589 cm1 = atoi(string) & 0xff;
590 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
591 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200592
593 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
594 cm2 = dbi_result_get_binary(result, "classmark2");
595 if (equip->classmark2_len > sizeof(equip->classmark2))
596 equip->classmark2_len = sizeof(equip->classmark2);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100597 if (cm2)
598 memcpy(equip->classmark2, cm2, equip->classmark2_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200599
600 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
601 cm3 = dbi_result_get_binary(result, "classmark3");
602 if (equip->classmark3_len > sizeof(equip->classmark3))
603 equip->classmark3_len = sizeof(equip->classmark3);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100604 if (cm3)
605 memcpy(equip->classmark3, cm3, equip->classmark3_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200606
607 dbi_result_free(result);
608
609 return 0;
610}
Harald Welte3606cc52009-12-05 15:13:22 +0530611
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200612int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
613 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530614{
615 dbi_result result;
616 const unsigned char *a3a8_ki;
617
618 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200619 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530620 subscr->id);
621 if (!result)
622 return -EIO;
623
624 if (!dbi_result_next_row(result)) {
625 dbi_result_free(result);
626 return -ENOENT;
627 }
628
629 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
630 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
631 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100632 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200633 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530634 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
635
636 dbi_result_free(result);
637
638 return 0;
639}
640
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200641int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
642 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100643{
644 dbi_result result;
645 struct gsm_auth_info ainfo_old;
646 int rc, upd;
647 unsigned char *ki_str;
648
649 /* Deletion ? */
650 if (ainfo == NULL) {
651 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200652 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100653 subscr->id);
654
655 if (!result)
656 return -EIO;
657
658 dbi_result_free(result);
659
660 return 0;
661 }
662
663 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200664 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100665 if (rc && rc != -ENOENT)
666 return rc;
667 upd = rc ? 0 : 1;
668
669 /* Update / Insert */
670 dbi_conn_quote_binary_copy(conn,
671 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
672
673 if (!upd) {
674 result = dbi_conn_queryf(conn,
675 "INSERT INTO AuthKeys "
676 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200677 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100678 subscr->id, ainfo->auth_algo, ki_str);
679 } else {
680 result = dbi_conn_queryf(conn,
681 "UPDATE AuthKeys "
682 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200683 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100684 ainfo->auth_algo, ki_str, subscr->id);
685 }
686
687 free(ki_str);
688
689 if (!result)
690 return -EIO;
691
692 dbi_result_free(result);
693
694 return 0;
695}
696
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200697int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
698 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530699{
700 dbi_result result;
701 int len;
702 const unsigned char *blob;
703
704 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200705 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530706 subscr->id);
707 if (!result)
708 return -EIO;
709
710 if (!dbi_result_next_row(result)) {
711 dbi_result_free(result);
712 return -ENOENT;
713 }
714
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200715 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530716
Sylvain Munaut70881b72009-12-27 15:41:59 +0100717 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
718 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
719
Harald Welte3606cc52009-12-05 15:13:22 +0530720 len = dbi_result_get_field_length(result, "rand");
Harald Welte121e9a42016-04-20 13:13:19 +0200721 if (len != sizeof(atuple->vec.rand))
Harald Welte3606cc52009-12-05 15:13:22 +0530722 goto err_size;
723
724 blob = dbi_result_get_binary(result, "rand");
Harald Welte121e9a42016-04-20 13:13:19 +0200725 memcpy(atuple->vec.rand, blob, len);
Harald Welte3606cc52009-12-05 15:13:22 +0530726
727 len = dbi_result_get_field_length(result, "sres");
Harald Welte121e9a42016-04-20 13:13:19 +0200728 if (len != sizeof(atuple->vec.sres))
Harald Welte3606cc52009-12-05 15:13:22 +0530729 goto err_size;
730
731 blob = dbi_result_get_binary(result, "sres");
Harald Welte121e9a42016-04-20 13:13:19 +0200732 memcpy(atuple->vec.sres, blob, len);
Harald Welte3606cc52009-12-05 15:13:22 +0530733
734 len = dbi_result_get_field_length(result, "kc");
Harald Welte121e9a42016-04-20 13:13:19 +0200735 if (len != sizeof(atuple->vec.kc))
Harald Welte3606cc52009-12-05 15:13:22 +0530736 goto err_size;
737
738 blob = dbi_result_get_binary(result, "kc");
Harald Welte121e9a42016-04-20 13:13:19 +0200739 memcpy(atuple->vec.kc, blob, len);
Harald Welte3606cc52009-12-05 15:13:22 +0530740
741 dbi_result_free(result);
742
743 return 0;
744
745err_size:
746 dbi_result_free(result);
747 return -EIO;
748}
749
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200750int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
751 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100752{
753 dbi_result result;
754 int rc, upd;
755 struct gsm_auth_tuple atuple_old;
756 unsigned char *rand_str, *sres_str, *kc_str;
757
758 /* Deletion ? */
759 if (atuple == NULL) {
760 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200761 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100762 subscr->id);
763
764 if (!result)
765 return -EIO;
766
767 dbi_result_free(result);
768
769 return 0;
770 }
771
772 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200773 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100774 if (rc && rc != -ENOENT)
775 return rc;
776 upd = rc ? 0 : 1;
777
778 /* Update / Insert */
779 dbi_conn_quote_binary_copy(conn,
Harald Welte121e9a42016-04-20 13:13:19 +0200780 atuple->vec.rand, sizeof(atuple->vec.rand), &rand_str);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100781 dbi_conn_quote_binary_copy(conn,
Harald Welte121e9a42016-04-20 13:13:19 +0200782 atuple->vec.sres, sizeof(atuple->vec.sres), &sres_str);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100783 dbi_conn_quote_binary_copy(conn,
Harald Welte121e9a42016-04-20 13:13:19 +0200784 atuple->vec.kc, sizeof(atuple->vec.kc), &kc_str);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100785
786 if (!upd) {
787 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200788 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100789 "(subscriber_id, issued, use_count, "
790 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200791 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100792 "%u, %s, %s, %s ) ",
793 subscr->id, atuple->use_count, atuple->key_seq,
794 rand_str, sres_str, kc_str);
795 } else {
796 char *issued = atuple->key_seq == atuple_old.key_seq ?
797 "issued" : "datetime('now')";
798 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200799 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100800 "SET issued=%s, use_count=%u, "
801 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200802 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100803 issued, atuple->use_count, atuple->key_seq,
804 rand_str, sres_str, kc_str, subscr->id);
805 }
806
807 free(rand_str);
808 free(sres_str);
809 free(kc_str);
810
811 if (!result)
812 return -EIO;
813
814 dbi_result_free(result);
815
816 return 0;
817}
818
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100819static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
820{
821 const char *string;
822 string = dbi_result_get_string(result, "imsi");
823 if (string)
Harald Welted3fa84d2016-04-20 17:50:17 +0200824 strncpy(subscr->imsi, string, sizeof(subscr->imsi)-1);
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100825
826 string = dbi_result_get_string(result, "tmsi");
827 if (string)
828 subscr->tmsi = tmsi_from_string(string);
829
830 string = dbi_result_get_string(result, "name");
831 if (string)
832 strncpy(subscr->name, string, GSM_NAME_LENGTH);
833
834 string = dbi_result_get_string(result, "extension");
835 if (string)
836 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
837
Kevin Redonc9763a32013-11-04 22:43:15 +0100838 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100839
840 if (!dbi_result_field_is_null(result, "expire_lu"))
841 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
842 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200843 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100844
Kevin Redonc9763a32013-11-04 22:43:15 +0100845 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
846
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100847}
848
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200849#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200850struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200851 const char *id)
852{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000853 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000854 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000855 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000856
Jan Luebbe5c15c852008-12-27 15:59:25 +0000857 switch (field) {
858 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000859 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000860 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200861 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000862 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000863 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000864 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000865 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000866 break;
867 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000868 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000869 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200870 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000871 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000872 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000873 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000874 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000875 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000876 case GSM_SUBSCRIBER_EXTENSION:
877 dbi_conn_quote_string_copy(conn, id, &quoted);
878 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200879 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000880 "WHERE extension = %s ",
881 quoted
882 );
883 free(quoted);
884 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200885 case GSM_SUBSCRIBER_ID:
886 dbi_conn_quote_string_copy(conn, id, &quoted);
887 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200888 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200889 "WHERE id = %s ", quoted);
890 free(quoted);
891 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000892 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100893 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000894 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000895 }
Harald Welte0b906d02009-12-24 11:21:42 +0100896 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100897 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000898 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000899 }
900 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100901 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000902 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000903 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000904 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000905 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000906
907 subscr = subscr_alloc();
908 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000909
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100910 db_set_from_query(subscr, result);
Neels Hofmeyr307e4062016-05-09 21:28:05 +0200911 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %x, EXTEN '%s', LAC %hu, AUTH %u\n",
Harald Welte3ad03462016-03-17 14:41:26 +0100912 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
913 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000914 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200915
916 get_equipment_by_subscr(subscr);
917
Holger Freyther12aa50d2009-01-01 18:02:05 +0000918 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000919}
920
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100921int db_subscriber_update(struct gsm_subscriber *subscr)
922{
923 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100924 dbi_result result;
925
926 /* Copy the id to a string as queryf with %llu is failing */
927 sprintf(buf, "%llu", subscr->id);
928 result = dbi_conn_queryf(conn,
929 BASE_QUERY
930 "WHERE id = %s", buf);
931
932 if (!result) {
933 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
934 return -EIO;
935 }
936 if (!dbi_result_next_row(result)) {
937 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
938 subscr->id);
939 dbi_result_free(result);
940 return -EIO;
941 }
942
943 db_set_from_query(subscr, result);
944 dbi_result_free(result);
945 get_equipment_by_subscr(subscr);
946
947 return 0;
948}
949
Harald Welte0b906d02009-12-24 11:21:42 +0100950int db_sync_subscriber(struct gsm_subscriber *subscriber)
951{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000952 dbi_result result;
Harald Welte3ad03462016-03-17 14:41:26 +0100953 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100954 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200955
Harald Welte019d0162010-12-26 19:12:30 +0100956 dbi_conn_quote_string_copy(conn,
957 subscriber->name, &q_name);
958 dbi_conn_quote_string_copy(conn,
959 subscriber->extension, &q_extension);
960
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200961 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
Harald Welte3ad03462016-03-17 14:41:26 +0100962 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200963 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200964 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200965 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200966 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200967 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100968
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200969 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
970 result = dbi_conn_queryf(conn,
971 "UPDATE Subscriber "
972 "SET updated = datetime('now'), "
973 "name = %s, "
974 "extension = %s, "
975 "authorized = %i, "
976 "tmsi = %s, "
977 "lac = %i, "
978 "expire_lu = NULL "
979 "WHERE imsi = %s ",
980 q_name,
981 q_extension,
982 subscriber->authorized,
983 q_tmsi,
984 subscriber->lac,
985 subscriber->imsi);
986 } else {
987 result = dbi_conn_queryf(conn,
988 "UPDATE Subscriber "
989 "SET updated = datetime('now'), "
990 "name = %s, "
991 "extension = %s, "
992 "authorized = %i, "
993 "tmsi = %s, "
994 "lac = %i, "
995 "expire_lu = datetime(%i, 'unixepoch') "
996 "WHERE imsi = %s ",
997 q_name,
998 q_extension,
999 subscriber->authorized,
1000 q_tmsi,
1001 subscriber->lac,
1002 (int) subscriber->expire_lu,
1003 subscriber->imsi);
1004 }
Harald Welte0b906d02009-12-24 11:21:42 +01001005
Jan Luebbe9eca37f2009-08-12 21:04:54 +02001006 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +01001007 free(q_name);
1008 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +01001009
1010 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001011 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001012 return 1;
1013 }
Harald Welte0b906d02009-12-24 11:21:42 +01001014
Jan Luebbe5c15c852008-12-27 15:59:25 +00001015 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001016
Jan Luebbe5c15c852008-12-27 15:59:25 +00001017 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001018}
1019
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001020int db_subscriber_delete(struct gsm_subscriber *subscr)
1021{
1022 dbi_result result;
1023
1024 result = dbi_conn_queryf(conn,
1025 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1026 subscr->id);
1027 if (!result) {
1028 LOGP(DDB, LOGL_ERROR,
1029 "Failed to delete Authkeys for %llu\n", subscr->id);
1030 return -1;
1031 }
1032 dbi_result_free(result);
1033
1034 result = dbi_conn_queryf(conn,
1035 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1036 subscr->id);
1037 if (!result) {
1038 LOGP(DDB, LOGL_ERROR,
1039 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1040 return -1;
1041 }
1042 dbi_result_free(result);
1043
1044 result = dbi_conn_queryf(conn,
1045 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1046 subscr->id);
1047 if (!result) {
1048 LOGP(DDB, LOGL_ERROR,
1049 "Failed to delete AuthToken for %llu\n", subscr->id);
1050 return -1;
1051 }
1052 dbi_result_free(result);
1053
1054 result = dbi_conn_queryf(conn,
1055 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1056 subscr->id);
1057 if (!result) {
1058 LOGP(DDB, LOGL_ERROR,
1059 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1060 return -1;
1061 }
1062 dbi_result_free(result);
1063
1064 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001065 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1066 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001067 if (!result) {
1068 LOGP(DDB, LOGL_ERROR,
1069 "Failed to delete SMS for %llu\n", subscr->id);
1070 return -1;
1071 }
1072 dbi_result_free(result);
1073
1074 result = dbi_conn_queryf(conn,
1075 "DELETE FROM VLR WHERE subscriber_id=%llu",
1076 subscr->id);
1077 if (!result) {
1078 LOGP(DDB, LOGL_ERROR,
1079 "Failed to delete VLR for %llu\n", subscr->id);
1080 return -1;
1081 }
1082 dbi_result_free(result);
1083
1084 result = dbi_conn_queryf(conn,
1085 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1086 subscr->id);
1087 if (!result) {
1088 LOGP(DDB, LOGL_ERROR,
1089 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1090 return -1;
1091 }
1092 dbi_result_free(result);
1093
1094 result = dbi_conn_queryf(conn,
1095 "DELETE FROM Subscriber WHERE id=%llu",
1096 subscr->id);
1097 if (!result) {
1098 LOGP(DDB, LOGL_ERROR,
1099 "Failed to delete Subscriber for %llu\n", subscr->id);
1100 return -1;
1101 }
1102 dbi_result_free(result);
1103
1104 return 0;
1105}
1106
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001107/**
1108 * List all the authorized and non-expired subscribers. The callback will
1109 * be called one by one. The subscr argument is not fully initialize and
1110 * subscr_get/subscr_put must not be called. The passed in pointer will be
1111 * deleted after the callback by the database call.
1112 */
1113int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1114{
1115 dbi_result result;
1116
Max5c06e402015-07-29 20:20:28 +02001117 result = dbi_conn_query(conn,
1118 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001119 if (!result) {
1120 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1121 return -1;
1122 }
1123
1124 while (dbi_result_next_row(result)) {
1125 struct gsm_subscriber *subscr;
1126
1127 subscr = subscr_alloc();
1128 subscr->id = dbi_result_get_ulonglong(result, "id");
1129 db_set_from_query(subscr, result);
1130 cb(subscr, closure);
1131 OSMO_ASSERT(subscr->use_count == 1);
1132 llist_del(&subscr->entry);
1133 talloc_free(subscr);
1134 }
1135
1136 dbi_result_free(result);
1137 return 0;
1138}
1139
Harald Weltec2e302d2009-07-05 14:08:13 +02001140int db_sync_equipment(struct gsm_equipment *equip)
1141{
1142 dbi_result result;
1143 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001144 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001145 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001146
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001147 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001148 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001149 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001150 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001151 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001152 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001153 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001154 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001155 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001156 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001157
Harald Weltec2e302d2009-07-05 14:08:13 +02001158 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1159 equip->classmark2_len, &cm2);
1160 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1161 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001162 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001163
1164 result = dbi_conn_queryf(conn,
1165 "UPDATE Equipment SET "
1166 "updated = datetime('now'), "
1167 "classmark1 = %u, "
1168 "classmark2 = %s, "
1169 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001170 "WHERE imei = %s ",
1171 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001172
1173 free(cm2);
1174 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001175 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001176
1177 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001178 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001179 return -EIO;
1180 }
1181
1182 dbi_result_free(result);
1183 return 0;
1184}
1185
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001186int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1187{
1188 dbi_result result;
1189
1190 result = dbi_conn_query(conn,
1191 "SELECT id "
1192 "FROM Subscriber "
1193 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001194 "( expire_lu is NOT NULL "
1195 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001196 "LIMIT 1");
1197 if (!result) {
1198 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1199 return -EIO;
1200 }
1201
1202 while (dbi_result_next_row(result))
1203 callback(priv, dbi_result_get_ulonglong(result, "id"));
1204
1205 dbi_result_free(result);
1206 return 0;
1207}
1208
Harald Welte0b906d02009-12-24 11:21:42 +01001209int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1210{
1211 dbi_result result = NULL;
Harald Welte3ad03462016-03-17 14:41:26 +01001212 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001213 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001214
Jan Luebbe5c15c852008-12-27 15:59:25 +00001215 for (;;) {
Daniel Willmanncdeb8152015-10-08 16:10:23 +02001216 if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
1217 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1218 return 1;
1219 }
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001220 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1221 continue;
1222
Harald Welte3ad03462016-03-17 14:41:26 +01001223 sprintf(tmsi, "%u", subscriber->tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001224 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001225 result = dbi_conn_queryf(conn,
1226 "SELECT * FROM Subscriber "
1227 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001228 tmsi_quoted);
1229
Holger Freyther12aa50d2009-01-01 18:02:05 +00001230 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001231
1232 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001233 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1234 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001235 return 1;
1236 }
Harald Welte0b906d02009-12-24 11:21:42 +01001237 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001238 dbi_result_free(result);
1239 continue;
1240 }
1241 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001242 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001243 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1244 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001245 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001246 }
1247 dbi_result_free(result);
1248 }
1249 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001250}
1251
Harald Welte0b906d02009-12-24 11:21:42 +01001252int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1253{
1254 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001255 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001256
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001257 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001258 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001259 result = dbi_conn_queryf(conn,
1260 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001261 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001262 try
1263 );
Harald Welte0b906d02009-12-24 11:21:42 +01001264 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001265 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1266 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001267 return 1;
1268 }
1269 if (dbi_result_get_numrows(result)){
1270 dbi_result_free(result);
1271 continue;
1272 }
1273 if (!dbi_result_next_row(result)) {
1274 dbi_result_free(result);
1275 break;
1276 }
1277 dbi_result_free(result);
1278 }
1279 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001280 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001281 return db_sync_subscriber(subscriber);
1282}
Jan Luebbe31bef492009-08-12 14:31:14 +02001283/*
1284 * try to allocate a new unique token for this subscriber and return it
1285 * via a parameter. if the subscriber already has a token, return
1286 * an error.
1287 */
1288
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001289int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001290{
1291 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001292 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001293
Jan Luebbe31bef492009-08-12 14:31:14 +02001294 for (;;) {
Daniel Willmann2aedfbd2015-10-08 16:10:26 +02001295 if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
1296 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1297 return 1;
1298 }
Jan Luebbe31bef492009-08-12 14:31:14 +02001299 if (!try) /* 0 is an invalid token */
1300 continue;
1301 result = dbi_conn_queryf(conn,
1302 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001303 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1304 subscriber->id, try);
1305 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001306 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1307 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001308 return 1;
1309 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001310 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001311 dbi_result_free(result);
1312 continue;
1313 }
1314 if (!dbi_result_next_row(result)) {
1315 dbi_result_free(result);
1316 break;
1317 }
1318 dbi_result_free(result);
1319 }
1320 result = dbi_conn_queryf(conn,
1321 "INSERT INTO AuthToken "
1322 "(subscriber_id, created, token) "
1323 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001324 "(%llu, datetime('now'), \"%08X\") ",
1325 subscriber->id, try);
1326 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001327 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1328 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001329 return 1;
1330 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001331 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001332 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001333 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001334
Jan Luebbe31bef492009-08-12 14:31:14 +02001335 return 0;
1336}
1337
Harald Welted3fa84d2016-04-20 17:50:17 +02001338int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM23003_IMEISV_NUM_DIGITS])
Harald Welte0b906d02009-12-24 11:21:42 +01001339{
Harald Welted409be72009-11-07 00:06:19 +09001340 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001341 dbi_result result;
1342
Harald Weltec2e302d2009-07-05 14:08:13 +02001343 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001344 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001345
Jan Luebbefac25fc2008-12-27 18:04:34 +00001346 result = dbi_conn_queryf(conn,
1347 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001348 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001349 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001350 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001351 imei);
1352 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001353 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001354 return 1;
1355 }
Harald Welte0b906d02009-12-24 11:21:42 +01001356
Jan Luebbe391d86e2008-12-27 22:33:34 +00001357 equipment_id = 0;
1358 if (dbi_result_get_numrows_affected(result)) {
1359 equipment_id = dbi_conn_sequence_last(conn, NULL);
1360 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001361 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001362
1363 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001364 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001365 else {
1366 result = dbi_conn_queryf(conn,
1367 "SELECT id FROM Equipment "
1368 "WHERE imei = %s ",
1369 imei
1370 );
Harald Welte0b906d02009-12-24 11:21:42 +01001371 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001372 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001373 return 1;
1374 }
1375 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001376 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001377 dbi_result_free(result);
1378 return 1;
1379 }
1380 equipment_id = dbi_result_get_ulonglong(result, "id");
1381 dbi_result_free(result);
1382 }
1383
1384 result = dbi_conn_queryf(conn,
1385 "INSERT OR IGNORE INTO EquipmentWatch "
1386 "(subscriber_id, equipment_id, created, updated) "
1387 "VALUES "
1388 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001389 subscriber->id, equipment_id);
1390 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001391 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001392 return 1;
1393 }
Harald Welte0b906d02009-12-24 11:21:42 +01001394
Jan Luebbe391d86e2008-12-27 22:33:34 +00001395 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001396 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001397 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001398
Jan Luebbefac25fc2008-12-27 18:04:34 +00001399 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001400 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001401 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1402 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001403 else {
1404 result = dbi_conn_queryf(conn,
1405 "UPDATE EquipmentWatch "
1406 "SET updated = datetime('now') "
1407 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001408 subscriber->id, equipment_id);
1409 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001410 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001411 return 1;
1412 }
1413 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001414 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1415 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001416 }
1417
1418 return 0;
1419}
1420
Harald Welte7e310b12009-03-30 20:56:32 +00001421/* store an [unsent] SMS to the database */
1422int db_sms_store(struct gsm_sms *sms)
1423{
1424 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001425 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001426 unsigned char *q_udata;
1427 char *validity_timestamp = "2222-2-2";
1428
1429 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001430
1431 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001432 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001433 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001434 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1435 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001436
Harald Weltef3efc592009-07-27 20:11:35 +02001437 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001438 result = dbi_conn_queryf(conn,
1439 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001440 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001441 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001442 "data_coding_scheme, ud_hdr_ind, "
1443 "user_data, text, "
1444 "dest_addr, dest_ton, dest_npi, "
1445 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001446 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001447 "%u, %u, %u, "
1448 "%u, %u, "
1449 "%s, %s, "
1450 "%s, %u, %u, "
1451 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001452 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001453 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001454 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001455 q_udata, q_text,
1456 q_daddr, sms->dst.ton, sms->dst.npi,
1457 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001458 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001459 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001460 free(q_daddr);
1461 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001462
1463 if (!result)
1464 return -EIO;
1465
1466 dbi_result_free(result);
1467 return 0;
1468}
1469
Harald Welte2ebabca2009-08-09 19:05:21 +02001470static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001471{
Harald Welte76042182009-08-08 16:03:15 +02001472 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001473 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001474 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001475
Harald Welte76042182009-08-08 16:03:15 +02001476 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001477 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001478
Harald Weltebe3e3782009-07-05 14:06:41 +02001479 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001480
Harald Weltef3efc592009-07-27 20:11:35 +02001481 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001482 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001483 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1484 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1485 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1486 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1487 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001488 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001489 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001490
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001491 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1492 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001493 daddr = dbi_result_get_string(result, "dest_addr");
1494 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001495 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1496 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001497 }
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001498 sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001499
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001500 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1501 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001502 saddr = dbi_result_get_string(result, "src_addr");
1503 if (saddr) {
1504 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1505 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1506 }
1507
Harald Welte76042182009-08-08 16:03:15 +02001508 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1509 user_data = dbi_result_get_binary(result, "user_data");
1510 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001511 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001512 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001513
1514 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001515 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001516 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001517 sms->text[sizeof(sms->text)-1] = '\0';
1518 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001519 return sms;
1520}
1521
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001522struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1523{
1524 dbi_result result;
1525 struct gsm_sms *sms;
1526
1527 result = dbi_conn_queryf(conn,
1528 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1529 if (!result)
1530 return NULL;
1531
1532 if (!dbi_result_next_row(result)) {
1533 dbi_result_free(result);
1534 return NULL;
1535 }
1536
1537 sms = sms_from_result(net, result);
1538
1539 dbi_result_free(result);
1540
1541 return sms;
1542}
1543
Harald Welte2ebabca2009-08-09 19:05:21 +02001544/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001545struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001546{
1547 dbi_result result;
1548 struct gsm_sms *sms;
1549
1550 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001551 "SELECT SMS.* "
1552 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001553 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001554 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1555 "AND Subscriber.lac > 0 "
1556 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001557 min_id);
1558 if (!result)
1559 return NULL;
1560
1561 if (!dbi_result_next_row(result)) {
1562 dbi_result_free(result);
1563 return NULL;
1564 }
1565
1566 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001567
1568 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001569
1570 return sms;
1571}
1572
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001573struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1574 unsigned long long min_subscr_id,
1575 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001576{
1577 dbi_result result;
1578 struct gsm_sms *sms;
1579
1580 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001581 "SELECT SMS.* "
1582 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001583 "SMS.dest_addr = Subscriber.extension "
1584 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001585 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001586 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001587 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001588 if (!result)
1589 return NULL;
1590
1591 if (!dbi_result_next_row(result)) {
1592 dbi_result_free(result);
1593 return NULL;
1594 }
1595
1596 sms = sms_from_result(net, result);
1597
1598 dbi_result_free(result);
1599
1600 return sms;
1601}
1602
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001603/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001604struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1605{
1606 dbi_result result;
1607 struct gsm_sms *sms;
1608
1609 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001610 "SELECT SMS.* "
1611 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001612 "SMS.dest_addr = Subscriber.extension "
1613 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001614 "AND Subscriber.lac > 0 "
1615 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001616 subscr->id);
1617 if (!result)
1618 return NULL;
1619
1620 if (!dbi_result_next_row(result)) {
1621 dbi_result_free(result);
1622 return NULL;
1623 }
1624
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001625 sms = sms_from_result(subscr->group->net, result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001626
1627 dbi_result_free(result);
1628
Harald Welte7e310b12009-03-30 20:56:32 +00001629 return sms;
1630}
1631
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001632/* mark a given SMS as delivered */
1633int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001634{
1635 dbi_result result;
1636
1637 result = dbi_conn_queryf(conn,
1638 "UPDATE SMS "
1639 "SET sent = datetime('now') "
1640 "WHERE id = %llu", sms->id);
1641 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001642 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001643 return 1;
1644 }
1645
1646 dbi_result_free(result);
1647 return 0;
1648}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001649
1650/* increase the number of attempted deliveries */
1651int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1652{
1653 dbi_result result;
1654
1655 result = dbi_conn_queryf(conn,
1656 "UPDATE SMS "
1657 "SET deliver_attempts = deliver_attempts + 1 "
1658 "WHERE id = %llu", sms->id);
1659 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001660 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1661 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001662 return 1;
1663 }
1664
1665 dbi_result_free(result);
1666 return 0;
1667}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001668
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001669int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001670 uint8_t apdu_id_flags, uint8_t len,
1671 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001672{
1673 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001674 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001675
1676 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1677
1678 result = dbi_conn_queryf(conn,
1679 "INSERT INTO ApduBlobs "
1680 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1681 "(datetime('now'),%llu,%u,%s)",
1682 subscr->id, apdu_id_flags, q_apdu);
1683
1684 free(q_apdu);
1685
1686 if (!result)
1687 return -EIO;
1688
1689 dbi_result_free(result);
1690 return 0;
1691}
Harald Welteffa55a42009-12-22 19:07:32 +01001692
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001693int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001694{
1695 dbi_result result;
1696 char *q_name;
1697
1698 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1699
1700 result = dbi_conn_queryf(conn,
1701 "INSERT INTO Counters "
1702 "(timestamp,name,value) VALUES "
1703 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1704
1705 free(q_name);
1706
1707 if (!result)
1708 return -EIO;
1709
1710 dbi_result_free(result);
1711 return 0;
1712}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001713
1714static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1715 char *q_prefix)
1716{
1717 dbi_result result;
1718 char *q_name;
1719
1720 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1721 &q_name);
1722
1723 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001724 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001725 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001726 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1727 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001728
1729 free(q_name);
1730
1731 if (!result)
1732 return -EIO;
1733
1734 dbi_result_free(result);
1735 return 0;
1736}
1737
1738int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1739{
1740 unsigned int i;
1741 char *q_prefix;
1742
Harald Weltec1919862010-05-13 12:55:20 +02001743 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001744
1745 for (i = 0; i < ctrg->desc->num_ctr; i++)
1746 db_store_rate_ctr(ctrg, i, q_prefix);
1747
1748 free(q_prefix);
1749
1750 return 0;
1751}