blob: 7746e4471583fe9a2c6f2b309cd7874a87b1785d [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
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/talloc.h>
38#include <osmocom/core/statistics.h>
39#include <osmocom/core/rate_ctr.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020040
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010041/* Semi-Private-Interface (SPI) for the subscriber code */
42void subscr_direct_free(struct gsm_subscriber *subscr);
43
Holger Freytherbde36102008-12-28 22:51:39 +000044static char *db_basename = NULL;
45static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000046static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000047
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010048#define SCHEMA_REVISION "4"
Jan Luebbebfbdeec2012-12-27 00:27:16 +010049
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010050enum {
51 SCHEMA_META,
52 INSERT_META,
53 SCHEMA_SUBSCRIBER,
54 SCHEMA_AUTH,
55 SCHEMA_EQUIPMENT,
56 SCHEMA_EQUIPMENT_WATCH,
57 SCHEMA_SMS,
58 SCHEMA_VLR,
59 SCHEMA_APDU,
60 SCHEMA_COUNTERS,
61 SCHEMA_RATE,
62 SCHEMA_AUTHKEY,
63 SCHEMA_AUTHLAST,
64};
65
66static const char *create_stmts[] = {
67 [SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
Harald Welte7e310b12009-03-30 20:56:32 +000068 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
69 "key TEXT UNIQUE NOT NULL, "
70 "value TEXT NOT NULL"
71 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010072 [INSERT_META] = "INSERT OR IGNORE INTO Meta "
Harald Welte7e310b12009-03-30 20:56:32 +000073 "(key, value) "
74 "VALUES "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010075 "('revision', " SCHEMA_REVISION ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010076 [SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
Harald Welte7e310b12009-03-30 20:56:32 +000077 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
78 "created TIMESTAMP NOT NULL, "
79 "updated TIMESTAMP NOT NULL, "
80 "imsi NUMERIC UNIQUE NOT NULL, "
81 "name TEXT, "
82 "extension TEXT UNIQUE, "
83 "authorized INTEGER NOT NULL DEFAULT 0, "
84 "tmsi TEXT UNIQUE, "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010085 "lac INTEGER NOT NULL DEFAULT 0, "
86 "expire_lu TIMESTAMP DEFAULT NULL"
Harald Welte7e310b12009-03-30 20:56:32 +000087 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010088 [SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
Jan Luebbe31bef492009-08-12 14:31:14 +020089 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
90 "subscriber_id INTEGER UNIQUE NOT NULL, "
91 "created TIMESTAMP NOT NULL, "
92 "token TEXT UNIQUE NOT NULL"
93 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010094 [SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
Harald Welte7e310b12009-03-30 20:56:32 +000095 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
96 "created TIMESTAMP NOT NULL, "
97 "updated TIMESTAMP NOT NULL, "
98 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020099 "classmark1 NUMERIC, "
100 "classmark2 BLOB, "
101 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +0000102 "imei NUMERIC UNIQUE NOT NULL"
103 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100104 [SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
Harald Welte7e310b12009-03-30 20:56:32 +0000105 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
106 "created TIMESTAMP NOT NULL, "
107 "updated TIMESTAMP NOT NULL, "
108 "subscriber_id NUMERIC NOT NULL, "
109 "equipment_id NUMERIC NOT NULL, "
110 "UNIQUE (subscriber_id, equipment_id) "
111 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100112 [SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +0200113 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +0000114 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
115 "created TIMESTAMP NOT NULL, "
116 "sent TIMESTAMP, "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200117 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +0200118 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +0200119 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +0200120 "reply_path_req INTEGER NOT NULL, "
121 "status_rep_req INTEGER NOT NULL, "
122 "protocol_id INTEGER NOT NULL, "
123 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200124 "ud_hdr_ind INTEGER NOT NULL, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200125 "src_addr TEXT NOT NULL, "
126 "src_ton INTEGER NOT NULL, "
127 "src_npi INTEGER NOT NULL, "
128 "dest_addr TEXT NOT NULL, "
129 "dest_ton INTEGER NOT NULL, "
130 "dest_npi INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200131 "user_data BLOB, " /* TP-UD */
132 /* additional data, interpreted from SMS */
133 "header BLOB, " /* UD Header */
134 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000135 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100136 [SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
Holger Freytherc2995ea2009-04-19 06:35:23 +0000137 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
138 "created TIMESTAMP NOT NULL, "
139 "updated TIMESTAMP NOT NULL, "
140 "subscriber_id NUMERIC UNIQUE NOT NULL, "
141 "last_bts NUMERIC NOT NULL "
142 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100143 [SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
Harald Welte (local)026531e2009-08-16 10:40:10 +0200144 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
145 "created TIMESTAMP NOT NULL, "
146 "apdu_id_flags INTEGER NOT NULL, "
147 "subscriber_id INTEGER NOT NULL, "
148 "apdu BLOB "
149 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100150 [SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
Harald Welteffa55a42009-12-22 19:07:32 +0100151 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
152 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100153 "value INTEGER NOT NULL, "
154 "name TEXT NOT NULL "
Harald Welte09f7ad02009-12-24 09:42:07 +0100155 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100156 [SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
Harald Weltec1919862010-05-13 12:55:20 +0200157 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
158 "timestamp TIMESTAMP NOT NULL, "
159 "value INTEGER NOT NULL, "
160 "name TEXT NOT NULL, "
Harald Welted94d6a02010-05-14 17:38:47 +0200161 "idx INTEGER NOT NULL "
Harald Weltec1919862010-05-13 12:55:20 +0200162 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100163 [SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200164 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut77d334a2009-12-27 19:26:12 +0100165 "algorithm_id INTEGER NOT NULL, "
Harald Welte3606cc52009-12-05 15:13:22 +0530166 "a3a8_ki BLOB "
167 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100168 [SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200169 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut70881b72009-12-27 15:41:59 +0100170 "issued TIMESTAMP NOT NULL, "
171 "use_count INTEGER NOT NULL DEFAULT 0, "
172 "key_seq INTEGER NOT NULL, "
173 "rand BLOB NOT NULL, "
174 "sres BLOB NOT NULL, "
175 "kc BLOB NOT NULL "
Harald Welteffa55a42009-12-22 19:07:32 +0100176 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000177};
178
Harald Welte0b906d02009-12-24 11:21:42 +0100179void db_error_func(dbi_conn conn, void *data)
180{
181 const char *msg;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000182 dbi_conn_error(conn, &msg);
Harald Welteae1f1592009-12-24 11:39:14 +0100183 LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000184}
185
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100186static int update_db_revision_2(void)
187{
188 dbi_result result;
189
190 result = dbi_conn_query(conn,
191 "ALTER TABLE Subscriber "
192 "ADD COLUMN expire_lu "
193 "TIMESTAMP DEFAULT NULL");
194 if (!result) {
195 LOGP(DDB, LOGL_ERROR,
Alexander Chemeris7e20f642014-03-07 16:59:53 +0100196 "Failed to alter table Subscriber (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100197 return -EINVAL;
198 }
199 dbi_result_free(result);
200
201 result = dbi_conn_query(conn,
202 "UPDATE Meta "
203 "SET value = '3' "
204 "WHERE key = 'revision'");
205 if (!result) {
206 LOGP(DDB, LOGL_ERROR,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100207 "Failed to update DB schema revision (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100208 return -EINVAL;
209 }
210 dbi_result_free(result);
211
212 return 0;
213}
214
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100215/**
216 * Copied from the normal sms_from_result_v3 to avoid having
217 * to make sure that the real routine will remain backward
218 * compatible.
219 */
220static struct gsm_sms *sms_from_result_v3(dbi_result result)
221{
222 struct gsm_sms *sms = sms_alloc();
223 long long unsigned int sender_id;
224 struct gsm_subscriber *sender;
225 const char *text, *daddr;
226 const unsigned char *user_data;
227 char buf[32];
228
229 if (!sms)
230 return NULL;
231
232 sms->id = dbi_result_get_ulonglong(result, "id");
233
234 sender_id = dbi_result_get_ulonglong(result, "sender_id");
235 snprintf(buf, sizeof(buf), "%llu", sender_id);
236 sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
237 OSMO_ASSERT(sender);
238 strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
239 subscr_direct_free(sender);
240 sender = NULL;
241
242 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
243 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
244 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
245 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
246 sms->data_coding_scheme = dbi_result_get_uint(result,
247 "data_coding_scheme");
248
249 daddr = dbi_result_get_string(result, "dest_addr");
250 if (daddr) {
251 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
252 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
253 }
254
255 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
256 user_data = dbi_result_get_binary(result, "user_data");
257 if (sms->user_data_len > sizeof(sms->user_data))
258 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
259 memcpy(sms->user_data, user_data, sms->user_data_len);
260
261 text = dbi_result_get_string(result, "text");
262 if (text) {
263 strncpy(sms->text, text, sizeof(sms->text));
264 sms->text[sizeof(sms->text)-1] = '\0';
265 }
266 return sms;
267}
268
269static int update_db_revision_3(void)
270{
271 dbi_result result;
272 struct gsm_sms *sms;
273
274 result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
275 if (!result) {
276 LOGP(DDB, LOGL_ERROR,
277 "Failed to begin transaction (upgrade from rev 3)\n");
278 return -EINVAL;
279 }
280 dbi_result_free(result);
281
282 /* Rename old SMS table to be able create a new one */
283 result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
284 if (!result) {
285 LOGP(DDB, LOGL_ERROR,
286 "Failed to rename the old SMS table (upgrade from rev 3).\n");
287 goto rollback;
288 }
289 dbi_result_free(result);
290
291 /* Create new SMS table with all the bells and whistles! */
292 result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
293 if (!result) {
294 LOGP(DDB, LOGL_ERROR,
295 "Failed to create a new SMS table (upgrade from rev 3).\n");
296 goto rollback;
297 }
298 dbi_result_free(result);
299
300 /* Cycle through old messages and convert them to the new format */
301 result = dbi_conn_queryf(conn, "SELECT * FROM SMS_3");
302 if (!result) {
303 LOGP(DDB, LOGL_ERROR,
304 "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
305 goto rollback;
306 }
307 while (dbi_result_next_row(result)) {
308 sms = sms_from_result_v3(result);
309 if (db_sms_store(sms) != 0) {
310 LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
311 sms_free(sms);
312 dbi_result_free(result);
313 goto rollback;
314 }
315 sms_free(sms);
316 }
317 dbi_result_free(result);
318
319 /* Remove the temporary table */
320 result = dbi_conn_query(conn, "DROP TABLE SMS_3");
321 if (!result) {
322 LOGP(DDB, LOGL_ERROR,
323 "Failed to drop the old SMS table (upgrade from rev 3).\n");
324 goto rollback;
325 }
326 dbi_result_free(result);
327
328 /* We're done. Bump DB Meta revision to 4 */
329 result = dbi_conn_query(conn,
330 "UPDATE Meta "
331 "SET value = '4' "
332 "WHERE key = 'revision'");
333 if (!result) {
334 LOGP(DDB, LOGL_ERROR,
335 "Failed to update DB schema revision (upgrade from rev 3).\n");
336 goto rollback;
337 }
338 dbi_result_free(result);
339
340 result = dbi_conn_query(conn, "COMMIT TRANSACTION");
341 if (!result) {
342 LOGP(DDB, LOGL_ERROR,
343 "Failed to commit the transaction (upgrade from rev 3)\n");
344 return -EINVAL;
345 }
346
347 /* Shrink DB file size by actually wiping out SMS_3 table data */
348 result = dbi_conn_query(conn, "VACUUM");
349 if (!result)
350 LOGP(DDB, LOGL_ERROR,
351 "VACUUM failed. Ignoring it (upgrade from rev 3).\n");
352 else
353 dbi_result_free(result);
354
355 return 0;
356
357rollback:
358 result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
359 if (!result)
360 LOGP(DDB, LOGL_ERROR,
361 "Rollback failed (upgrade from rev 3).\n");
362 else
363 dbi_result_free(result);
364 return -EINVAL;
365}
366
Harald Welted0b7b772009-08-09 19:03:42 +0200367static int check_db_revision(void)
368{
369 dbi_result result;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100370 const char *rev_s;
Harald Welted0b7b772009-08-09 19:03:42 +0200371
372 result = dbi_conn_query(conn,
373 "SELECT value FROM Meta WHERE key='revision'");
374 if (!result)
375 return -EINVAL;
376
377 if (!dbi_result_next_row(result)) {
378 dbi_result_free(result);
379 return -EINVAL;
380 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100381 rev_s = dbi_result_get_string(result, "value");
382 if (!rev_s) {
Harald Welted0b7b772009-08-09 19:03:42 +0200383 dbi_result_free(result);
384 return -EINVAL;
385 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100386 if (!strcmp(rev_s, "2")) {
387 if (update_db_revision_2()) {
388 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
389 dbi_result_free(result);
390 return -EINVAL;
391 }
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100392 } else if (!strcmp(rev_s, "3")) {
393 if (update_db_revision_3()) {
394 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
395 dbi_result_free(result);
396 return -EINVAL;
397 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100398 } else if (!strcmp(rev_s, SCHEMA_REVISION)) {
399 /* everything is fine */
400 } else {
401 LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s);
402 dbi_result_free(result);
403 return -EINVAL;
404 }
405
406 dbi_result_free(result);
407 return 0;
408}
409
410static int db_configure(void)
411{
412 dbi_result result;
413
414 result = dbi_conn_query(conn,
415 "PRAGMA synchronous = FULL");
416 if (!result)
417 return -EINVAL;
Harald Welted0b7b772009-08-09 19:03:42 +0200418
419 dbi_result_free(result);
420 return 0;
421}
422
Harald Welte0b906d02009-12-24 11:21:42 +0100423int db_init(const char *name)
424{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000425 dbi_initialize(NULL);
Harald Welte0b906d02009-12-24 11:21:42 +0100426
Jan Luebbe5c15c852008-12-27 15:59:25 +0000427 conn = dbi_conn_new("sqlite3");
Harald Welte0b906d02009-12-24 11:21:42 +0100428 if (conn == NULL) {
Harald Welteae1f1592009-12-24 11:39:14 +0100429 LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000430 return 1;
431 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000432
Holger Freyther12aa50d2009-01-01 18:02:05 +0000433 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000434
Jan Luebbe5c15c852008-12-27 15:59:25 +0000435 /* MySQL
436 dbi_conn_set_option(conn, "host", "localhost");
437 dbi_conn_set_option(conn, "username", "your_name");
438 dbi_conn_set_option(conn, "password", "your_password");
439 dbi_conn_set_option(conn, "dbname", "your_dbname");
440 dbi_conn_set_option(conn, "encoding", "UTF-8");
441 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000442
Jan Luebbe5c15c852008-12-27 15:59:25 +0000443 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000444 db_basename = strdup(name);
445 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000446 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
447 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000448
Harald Welted0b7b772009-08-09 19:03:42 +0200449 if (dbi_conn_connect(conn) < 0)
450 goto out_err;
451
Jan Luebbe5c15c852008-12-27 15:59:25 +0000452 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200453
454out_err:
455 free(db_dirname);
456 free(db_basename);
457 db_dirname = db_basename = NULL;
458 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000459}
460
Harald Welted0b7b772009-08-09 19:03:42 +0200461
Harald Welted1476bc2011-07-16 13:24:09 +0200462int db_prepare(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100463{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000464 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000465 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000466
Harald Welte7e310b12009-03-30 20:56:32 +0000467 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
468 result = dbi_conn_query(conn, create_stmts[i]);
Harald Welte0b906d02009-12-24 11:21:42 +0100469 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100470 LOGP(DDB, LOGL_ERROR,
471 "Failed to create some table.\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000472 return 1;
473 }
474 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000475 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000476
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200477 if (check_db_revision() < 0) {
Harald Welteae1f1592009-12-24 11:39:14 +0100478 LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200479 "please update your database schema\n");
480 return -1;
481 }
482
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100483 db_configure();
484
Jan Luebbe5c15c852008-12-27 15:59:25 +0000485 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000486}
487
Harald Welted1476bc2011-07-16 13:24:09 +0200488int db_fini(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100489{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000490 dbi_conn_close(conn);
491 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000492
Harald Welte2c5f4c62011-07-16 13:22:57 +0200493 free(db_dirname);
494 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000495 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000496}
497
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200498struct gsm_subscriber *db_create_subscriber(const char *imsi)
Harald Welte9176bd42009-07-23 18:46:00 +0200499{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000500 dbi_result result;
Harald Welte0b906d02009-12-24 11:21:42 +0100501 struct gsm_subscriber *subscr;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000502
503 /* Is this subscriber known in the db? */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200504 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000505 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000506 result = dbi_conn_queryf(conn,
507 "UPDATE Subscriber set updated = datetime('now') "
508 "WHERE imsi = %s " , imsi);
Harald Welte0b906d02009-12-24 11:21:42 +0100509 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100510 LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n");
Harald Welte0b906d02009-12-24 11:21:42 +0100511 else
Harald Welte523200b2008-12-30 14:58:44 +0000512 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000513 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000514 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000515
516 subscr = subscr_alloc();
517 if (!subscr)
518 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200519 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000520 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000521 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000522 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000523 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000524 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000525 imsi
526 );
Harald Welte0b906d02009-12-24 11:21:42 +0100527 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100528 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000529 subscr->id = dbi_conn_sequence_last(conn, NULL);
530 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000531 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100532 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200533 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000534 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000535}
536
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200537osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200538
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200539static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
540{
541 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200542 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100543 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200544 const unsigned char *cm2, *cm3;
545 struct gsm_equipment *equip = &subscr->equipment;
546
547 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200548 "SELECT Equipment.* "
549 "FROM Equipment JOIN EquipmentWatch ON "
550 "EquipmentWatch.equipment_id=Equipment.id "
551 "WHERE EquipmentWatch.subscriber_id = %llu "
552 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200553 if (!result)
554 return -EIO;
555
556 if (!dbi_result_next_row(result)) {
557 dbi_result_free(result);
558 return -ENOENT;
559 }
560
561 equip->id = dbi_result_get_ulonglong(result, "id");
562
563 string = dbi_result_get_string(result, "imei");
564 if (string)
565 strncpy(equip->imei, string, sizeof(equip->imei));
566
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100567 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200568 if (string) {
569 cm1 = atoi(string) & 0xff;
570 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
571 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200572
573 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
574 cm2 = dbi_result_get_binary(result, "classmark2");
575 if (equip->classmark2_len > sizeof(equip->classmark2))
576 equip->classmark2_len = sizeof(equip->classmark2);
577 memcpy(equip->classmark2, cm2, equip->classmark2_len);
578
579 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
580 cm3 = dbi_result_get_binary(result, "classmark3");
581 if (equip->classmark3_len > sizeof(equip->classmark3))
582 equip->classmark3_len = sizeof(equip->classmark3);
583 memcpy(equip->classmark3, cm3, equip->classmark3_len);
584
585 dbi_result_free(result);
586
587 return 0;
588}
Harald Welte3606cc52009-12-05 15:13:22 +0530589
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200590int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
591 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530592{
593 dbi_result result;
594 const unsigned char *a3a8_ki;
595
596 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200597 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530598 subscr->id);
599 if (!result)
600 return -EIO;
601
602 if (!dbi_result_next_row(result)) {
603 dbi_result_free(result);
604 return -ENOENT;
605 }
606
607 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
608 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
609 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100610 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200611 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530612 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
613
614 dbi_result_free(result);
615
616 return 0;
617}
618
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200619int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
620 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100621{
622 dbi_result result;
623 struct gsm_auth_info ainfo_old;
624 int rc, upd;
625 unsigned char *ki_str;
626
627 /* Deletion ? */
628 if (ainfo == NULL) {
629 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200630 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100631 subscr->id);
632
633 if (!result)
634 return -EIO;
635
636 dbi_result_free(result);
637
638 return 0;
639 }
640
641 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200642 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100643 if (rc && rc != -ENOENT)
644 return rc;
645 upd = rc ? 0 : 1;
646
647 /* Update / Insert */
648 dbi_conn_quote_binary_copy(conn,
649 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
650
651 if (!upd) {
652 result = dbi_conn_queryf(conn,
653 "INSERT INTO AuthKeys "
654 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200655 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100656 subscr->id, ainfo->auth_algo, ki_str);
657 } else {
658 result = dbi_conn_queryf(conn,
659 "UPDATE AuthKeys "
660 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200661 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100662 ainfo->auth_algo, ki_str, subscr->id);
663 }
664
665 free(ki_str);
666
667 if (!result)
668 return -EIO;
669
670 dbi_result_free(result);
671
672 return 0;
673}
674
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200675int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
676 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530677{
678 dbi_result result;
679 int len;
680 const unsigned char *blob;
681
682 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200683 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530684 subscr->id);
685 if (!result)
686 return -EIO;
687
688 if (!dbi_result_next_row(result)) {
689 dbi_result_free(result);
690 return -ENOENT;
691 }
692
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200693 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530694
Sylvain Munaut70881b72009-12-27 15:41:59 +0100695 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
696 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
697
Harald Welte3606cc52009-12-05 15:13:22 +0530698 len = dbi_result_get_field_length(result, "rand");
699 if (len != sizeof(atuple->rand))
700 goto err_size;
701
702 blob = dbi_result_get_binary(result, "rand");
703 memcpy(atuple->rand, blob, len);
704
705 len = dbi_result_get_field_length(result, "sres");
706 if (len != sizeof(atuple->sres))
707 goto err_size;
708
709 blob = dbi_result_get_binary(result, "sres");
710 memcpy(atuple->sres, blob, len);
711
712 len = dbi_result_get_field_length(result, "kc");
713 if (len != sizeof(atuple->kc))
714 goto err_size;
715
716 blob = dbi_result_get_binary(result, "kc");
717 memcpy(atuple->kc, blob, len);
718
719 dbi_result_free(result);
720
721 return 0;
722
723err_size:
724 dbi_result_free(result);
725 return -EIO;
726}
727
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200728int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
729 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100730{
731 dbi_result result;
732 int rc, upd;
733 struct gsm_auth_tuple atuple_old;
734 unsigned char *rand_str, *sres_str, *kc_str;
735
736 /* Deletion ? */
737 if (atuple == NULL) {
738 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200739 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100740 subscr->id);
741
742 if (!result)
743 return -EIO;
744
745 dbi_result_free(result);
746
747 return 0;
748 }
749
750 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200751 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100752 if (rc && rc != -ENOENT)
753 return rc;
754 upd = rc ? 0 : 1;
755
756 /* Update / Insert */
757 dbi_conn_quote_binary_copy(conn,
758 atuple->rand, sizeof(atuple->rand), &rand_str);
759 dbi_conn_quote_binary_copy(conn,
760 atuple->sres, sizeof(atuple->sres), &sres_str);
761 dbi_conn_quote_binary_copy(conn,
762 atuple->kc, sizeof(atuple->kc), &kc_str);
763
764 if (!upd) {
765 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200766 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100767 "(subscriber_id, issued, use_count, "
768 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200769 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100770 "%u, %s, %s, %s ) ",
771 subscr->id, atuple->use_count, atuple->key_seq,
772 rand_str, sres_str, kc_str);
773 } else {
774 char *issued = atuple->key_seq == atuple_old.key_seq ?
775 "issued" : "datetime('now')";
776 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200777 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100778 "SET issued=%s, use_count=%u, "
779 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200780 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100781 issued, atuple->use_count, atuple->key_seq,
782 rand_str, sres_str, kc_str, subscr->id);
783 }
784
785 free(rand_str);
786 free(sres_str);
787 free(kc_str);
788
789 if (!result)
790 return -EIO;
791
792 dbi_result_free(result);
793
794 return 0;
795}
796
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100797static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
798{
799 const char *string;
800 string = dbi_result_get_string(result, "imsi");
801 if (string)
802 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
803
804 string = dbi_result_get_string(result, "tmsi");
805 if (string)
806 subscr->tmsi = tmsi_from_string(string);
807
808 string = dbi_result_get_string(result, "name");
809 if (string)
810 strncpy(subscr->name, string, GSM_NAME_LENGTH);
811
812 string = dbi_result_get_string(result, "extension");
813 if (string)
814 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
815
Kevin Redonc9763a32013-11-04 22:43:15 +0100816 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100817
818 if (!dbi_result_field_is_null(result, "expire_lu"))
819 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
820 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200821 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100822
Kevin Redonc9763a32013-11-04 22:43:15 +0100823 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
824
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100825}
826
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200827#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200828struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200829 const char *id)
830{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000831 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000832 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000833 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000834
Jan Luebbe5c15c852008-12-27 15:59:25 +0000835 switch (field) {
836 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000837 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000838 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200839 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000840 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000841 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000842 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000843 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000844 break;
845 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000846 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000847 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200848 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000849 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000850 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000851 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000852 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000853 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000854 case GSM_SUBSCRIBER_EXTENSION:
855 dbi_conn_quote_string_copy(conn, id, &quoted);
856 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200857 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000858 "WHERE extension = %s ",
859 quoted
860 );
861 free(quoted);
862 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200863 case GSM_SUBSCRIBER_ID:
864 dbi_conn_quote_string_copy(conn, id, &quoted);
865 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200866 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200867 "WHERE id = %s ", quoted);
868 free(quoted);
869 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000870 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100871 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000872 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000873 }
Harald Welte0b906d02009-12-24 11:21:42 +0100874 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100875 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000876 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000877 }
878 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100879 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000880 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000881 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000882 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000883 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000884
885 subscr = subscr_alloc();
886 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000887
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100888 db_set_from_query(subscr, result);
Harald Welteae1f1592009-12-24 11:39:14 +0100889 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
Holger Freyther91754472009-06-09 08:52:41 +0000890 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000891 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000892 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200893
894 get_equipment_by_subscr(subscr);
895
Holger Freyther12aa50d2009-01-01 18:02:05 +0000896 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000897}
898
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100899int db_subscriber_update(struct gsm_subscriber *subscr)
900{
901 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100902 dbi_result result;
903
904 /* Copy the id to a string as queryf with %llu is failing */
905 sprintf(buf, "%llu", subscr->id);
906 result = dbi_conn_queryf(conn,
907 BASE_QUERY
908 "WHERE id = %s", buf);
909
910 if (!result) {
911 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
912 return -EIO;
913 }
914 if (!dbi_result_next_row(result)) {
915 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
916 subscr->id);
917 dbi_result_free(result);
918 return -EIO;
919 }
920
921 db_set_from_query(subscr, result);
922 dbi_result_free(result);
923 get_equipment_by_subscr(subscr);
924
925 return 0;
926}
927
Harald Welte0b906d02009-12-24 11:21:42 +0100928int db_sync_subscriber(struct gsm_subscriber *subscriber)
929{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000930 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200931 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100932 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200933
Harald Welte019d0162010-12-26 19:12:30 +0100934 dbi_conn_quote_string_copy(conn,
935 subscriber->name, &q_name);
936 dbi_conn_quote_string_copy(conn,
937 subscriber->extension, &q_extension);
938
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200939 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
940 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200941 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200942 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200943 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200944 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200945 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100946
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200947 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
948 result = dbi_conn_queryf(conn,
949 "UPDATE Subscriber "
950 "SET updated = datetime('now'), "
951 "name = %s, "
952 "extension = %s, "
953 "authorized = %i, "
954 "tmsi = %s, "
955 "lac = %i, "
956 "expire_lu = NULL "
957 "WHERE imsi = %s ",
958 q_name,
959 q_extension,
960 subscriber->authorized,
961 q_tmsi,
962 subscriber->lac,
963 subscriber->imsi);
964 } else {
965 result = dbi_conn_queryf(conn,
966 "UPDATE Subscriber "
967 "SET updated = datetime('now'), "
968 "name = %s, "
969 "extension = %s, "
970 "authorized = %i, "
971 "tmsi = %s, "
972 "lac = %i, "
973 "expire_lu = datetime(%i, 'unixepoch') "
974 "WHERE imsi = %s ",
975 q_name,
976 q_extension,
977 subscriber->authorized,
978 q_tmsi,
979 subscriber->lac,
980 (int) subscriber->expire_lu,
981 subscriber->imsi);
982 }
Harald Welte0b906d02009-12-24 11:21:42 +0100983
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200984 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100985 free(q_name);
986 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100987
988 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100989 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000990 return 1;
991 }
Harald Welte0b906d02009-12-24 11:21:42 +0100992
Jan Luebbe5c15c852008-12-27 15:59:25 +0000993 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100994
Jan Luebbe5c15c852008-12-27 15:59:25 +0000995 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000996}
997
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100998int db_subscriber_delete(struct gsm_subscriber *subscr)
999{
1000 dbi_result result;
1001
1002 result = dbi_conn_queryf(conn,
1003 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1004 subscr->id);
1005 if (!result) {
1006 LOGP(DDB, LOGL_ERROR,
1007 "Failed to delete Authkeys for %llu\n", subscr->id);
1008 return -1;
1009 }
1010 dbi_result_free(result);
1011
1012 result = dbi_conn_queryf(conn,
1013 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1014 subscr->id);
1015 if (!result) {
1016 LOGP(DDB, LOGL_ERROR,
1017 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1018 return -1;
1019 }
1020 dbi_result_free(result);
1021
1022 result = dbi_conn_queryf(conn,
1023 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1024 subscr->id);
1025 if (!result) {
1026 LOGP(DDB, LOGL_ERROR,
1027 "Failed to delete AuthToken for %llu\n", subscr->id);
1028 return -1;
1029 }
1030 dbi_result_free(result);
1031
1032 result = dbi_conn_queryf(conn,
1033 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1034 subscr->id);
1035 if (!result) {
1036 LOGP(DDB, LOGL_ERROR,
1037 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1038 return -1;
1039 }
1040 dbi_result_free(result);
1041
1042 result = dbi_conn_queryf(conn,
1043 "DELETE FROM SMS WHERE sender_id=%llu OR receiver_id=%llu",
1044 subscr->id, subscr->id);
1045 if (!result) {
1046 LOGP(DDB, LOGL_ERROR,
1047 "Failed to delete SMS for %llu\n", subscr->id);
1048 return -1;
1049 }
1050 dbi_result_free(result);
1051
1052 result = dbi_conn_queryf(conn,
1053 "DELETE FROM VLR WHERE subscriber_id=%llu",
1054 subscr->id);
1055 if (!result) {
1056 LOGP(DDB, LOGL_ERROR,
1057 "Failed to delete VLR for %llu\n", subscr->id);
1058 return -1;
1059 }
1060 dbi_result_free(result);
1061
1062 result = dbi_conn_queryf(conn,
1063 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1064 subscr->id);
1065 if (!result) {
1066 LOGP(DDB, LOGL_ERROR,
1067 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1068 return -1;
1069 }
1070 dbi_result_free(result);
1071
1072 result = dbi_conn_queryf(conn,
1073 "DELETE FROM Subscriber WHERE id=%llu",
1074 subscr->id);
1075 if (!result) {
1076 LOGP(DDB, LOGL_ERROR,
1077 "Failed to delete Subscriber for %llu\n", subscr->id);
1078 return -1;
1079 }
1080 dbi_result_free(result);
1081
1082 return 0;
1083}
1084
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001085/**
1086 * List all the authorized and non-expired subscribers. The callback will
1087 * be called one by one. The subscr argument is not fully initialize and
1088 * subscr_get/subscr_put must not be called. The passed in pointer will be
1089 * deleted after the callback by the database call.
1090 */
1091int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1092{
1093 dbi_result result;
1094
1095 result = dbi_conn_queryf(conn,
1096 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
1097 if (!result) {
1098 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1099 return -1;
1100 }
1101
1102 while (dbi_result_next_row(result)) {
1103 struct gsm_subscriber *subscr;
1104
1105 subscr = subscr_alloc();
1106 subscr->id = dbi_result_get_ulonglong(result, "id");
1107 db_set_from_query(subscr, result);
1108 cb(subscr, closure);
1109 OSMO_ASSERT(subscr->use_count == 1);
1110 llist_del(&subscr->entry);
1111 talloc_free(subscr);
1112 }
1113
1114 dbi_result_free(result);
1115 return 0;
1116}
1117
Harald Weltec2e302d2009-07-05 14:08:13 +02001118int db_sync_equipment(struct gsm_equipment *equip)
1119{
1120 dbi_result result;
1121 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001122 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001123 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001124
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001125 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001126 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001127 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001128 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001129 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001130 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001131 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001132 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001133 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001134 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001135
Harald Weltec2e302d2009-07-05 14:08:13 +02001136 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1137 equip->classmark2_len, &cm2);
1138 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1139 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001140 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001141
1142 result = dbi_conn_queryf(conn,
1143 "UPDATE Equipment SET "
1144 "updated = datetime('now'), "
1145 "classmark1 = %u, "
1146 "classmark2 = %s, "
1147 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001148 "WHERE imei = %s ",
1149 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001150
1151 free(cm2);
1152 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001153 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001154
1155 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001156 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001157 return -EIO;
1158 }
1159
1160 dbi_result_free(result);
1161 return 0;
1162}
1163
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001164int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1165{
1166 dbi_result result;
1167
1168 result = dbi_conn_query(conn,
1169 "SELECT id "
1170 "FROM Subscriber "
1171 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001172 "( expire_lu is NOT NULL "
1173 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001174 "LIMIT 1");
1175 if (!result) {
1176 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1177 return -EIO;
1178 }
1179
1180 while (dbi_result_next_row(result))
1181 callback(priv, dbi_result_get_ulonglong(result, "id"));
1182
1183 dbi_result_free(result);
1184 return 0;
1185}
1186
Harald Welte0b906d02009-12-24 11:21:42 +01001187int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1188{
1189 dbi_result result = NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001190 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001191 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001192
Jan Luebbe5c15c852008-12-27 15:59:25 +00001193 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001194 subscriber->tmsi = rand();
1195 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1196 continue;
1197
1198 sprintf(tmsi, "%u", subscriber->tmsi);
1199 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001200 result = dbi_conn_queryf(conn,
1201 "SELECT * FROM Subscriber "
1202 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001203 tmsi_quoted);
1204
Holger Freyther12aa50d2009-01-01 18:02:05 +00001205 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001206
1207 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001208 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1209 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001210 return 1;
1211 }
Harald Welte0b906d02009-12-24 11:21:42 +01001212 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001213 dbi_result_free(result);
1214 continue;
1215 }
1216 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001217 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001218 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1219 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001220 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001221 }
1222 dbi_result_free(result);
1223 }
1224 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001225}
1226
Harald Welte0b906d02009-12-24 11:21:42 +01001227int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1228{
1229 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001230 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001231
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001232 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001233 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001234 result = dbi_conn_queryf(conn,
1235 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001236 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001237 try
1238 );
Harald Welte0b906d02009-12-24 11:21:42 +01001239 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001240 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1241 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001242 return 1;
1243 }
1244 if (dbi_result_get_numrows(result)){
1245 dbi_result_free(result);
1246 continue;
1247 }
1248 if (!dbi_result_next_row(result)) {
1249 dbi_result_free(result);
1250 break;
1251 }
1252 dbi_result_free(result);
1253 }
1254 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001255 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001256 return db_sync_subscriber(subscriber);
1257}
Jan Luebbe31bef492009-08-12 14:31:14 +02001258/*
1259 * try to allocate a new unique token for this subscriber and return it
1260 * via a parameter. if the subscriber already has a token, return
1261 * an error.
1262 */
1263
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001264int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001265{
1266 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001267 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001268
Jan Luebbe31bef492009-08-12 14:31:14 +02001269 for (;;) {
1270 try = rand();
1271 if (!try) /* 0 is an invalid token */
1272 continue;
1273 result = dbi_conn_queryf(conn,
1274 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001275 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1276 subscriber->id, try);
1277 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001278 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1279 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001280 return 1;
1281 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001282 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001283 dbi_result_free(result);
1284 continue;
1285 }
1286 if (!dbi_result_next_row(result)) {
1287 dbi_result_free(result);
1288 break;
1289 }
1290 dbi_result_free(result);
1291 }
1292 result = dbi_conn_queryf(conn,
1293 "INSERT INTO AuthToken "
1294 "(subscriber_id, created, token) "
1295 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001296 "(%llu, datetime('now'), \"%08X\") ",
1297 subscriber->id, try);
1298 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001299 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1300 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001301 return 1;
1302 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001303 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001304 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001305 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001306
Jan Luebbe31bef492009-08-12 14:31:14 +02001307 return 0;
1308}
1309
Harald Welte0b906d02009-12-24 11:21:42 +01001310int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1311{
Harald Welted409be72009-11-07 00:06:19 +09001312 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001313 dbi_result result;
1314
Harald Weltec2e302d2009-07-05 14:08:13 +02001315 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001316 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001317
Jan Luebbefac25fc2008-12-27 18:04:34 +00001318 result = dbi_conn_queryf(conn,
1319 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001320 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001321 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001322 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001323 imei);
1324 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001325 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001326 return 1;
1327 }
Harald Welte0b906d02009-12-24 11:21:42 +01001328
Jan Luebbe391d86e2008-12-27 22:33:34 +00001329 equipment_id = 0;
1330 if (dbi_result_get_numrows_affected(result)) {
1331 equipment_id = dbi_conn_sequence_last(conn, NULL);
1332 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001333 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001334
1335 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001336 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001337 else {
1338 result = dbi_conn_queryf(conn,
1339 "SELECT id FROM Equipment "
1340 "WHERE imei = %s ",
1341 imei
1342 );
Harald Welte0b906d02009-12-24 11:21:42 +01001343 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001344 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001345 return 1;
1346 }
1347 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001348 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001349 dbi_result_free(result);
1350 return 1;
1351 }
1352 equipment_id = dbi_result_get_ulonglong(result, "id");
1353 dbi_result_free(result);
1354 }
1355
1356 result = dbi_conn_queryf(conn,
1357 "INSERT OR IGNORE INTO EquipmentWatch "
1358 "(subscriber_id, equipment_id, created, updated) "
1359 "VALUES "
1360 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001361 subscriber->id, equipment_id);
1362 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001363 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001364 return 1;
1365 }
Harald Welte0b906d02009-12-24 11:21:42 +01001366
Jan Luebbe391d86e2008-12-27 22:33:34 +00001367 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001368 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001369 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001370
Jan Luebbefac25fc2008-12-27 18:04:34 +00001371 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001372 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001373 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1374 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001375 else {
1376 result = dbi_conn_queryf(conn,
1377 "UPDATE EquipmentWatch "
1378 "SET updated = datetime('now') "
1379 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001380 subscriber->id, equipment_id);
1381 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001382 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001383 return 1;
1384 }
1385 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001386 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1387 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001388 }
1389
1390 return 0;
1391}
1392
Harald Welte7e310b12009-03-30 20:56:32 +00001393/* store an [unsent] SMS to the database */
1394int db_sms_store(struct gsm_sms *sms)
1395{
1396 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001397 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001398 unsigned char *q_udata;
1399 char *validity_timestamp = "2222-2-2";
1400
1401 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001402
1403 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001404 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001405 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001406 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1407 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001408
Harald Weltef3efc592009-07-27 20:11:35 +02001409 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001410 result = dbi_conn_queryf(conn,
1411 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001412 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001413 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001414 "data_coding_scheme, ud_hdr_ind, "
1415 "user_data, text, "
1416 "dest_addr, dest_ton, dest_npi, "
1417 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001418 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001419 "%u, %u, %u, "
1420 "%u, %u, "
1421 "%s, %s, "
1422 "%s, %u, %u, "
1423 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001424 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001425 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001426 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001427 q_udata, q_text,
1428 q_daddr, sms->dst.ton, sms->dst.npi,
1429 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001430 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001431 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001432 free(q_daddr);
1433 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001434
1435 if (!result)
1436 return -EIO;
1437
1438 dbi_result_free(result);
1439 return 0;
1440}
1441
Harald Welte2ebabca2009-08-09 19:05:21 +02001442static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001443{
Harald Welte76042182009-08-08 16:03:15 +02001444 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001445 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001446 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001447
Harald Welte76042182009-08-08 16:03:15 +02001448 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001449 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001450
Harald Weltebe3e3782009-07-05 14:06:41 +02001451 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001452
Harald Weltef3efc592009-07-27 20:11:35 +02001453 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001454 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
1455 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
1456 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
1457 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
1458 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
1459 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001460 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001461 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001462
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001463 sms->dst.npi = dbi_result_get_uint(result, "dest_npi");
1464 sms->dst.ton = dbi_result_get_uint(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001465 daddr = dbi_result_get_string(result, "dest_addr");
1466 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001467 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1468 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001469 }
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001470 sms->receiver = subscr_get_by_extension(net, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001471
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001472 sms->src.npi = dbi_result_get_uint(result, "src_npi");
1473 sms->src.ton = dbi_result_get_uint(result, "src_ton");
1474 saddr = dbi_result_get_string(result, "src_addr");
1475 if (saddr) {
1476 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1477 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1478 }
1479
Harald Welte76042182009-08-08 16:03:15 +02001480 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1481 user_data = dbi_result_get_binary(result, "user_data");
1482 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001483 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001484 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001485
1486 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001487 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001488 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001489 sms->text[sizeof(sms->text)-1] = '\0';
1490 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001491 return sms;
1492}
1493
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001494struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1495{
1496 dbi_result result;
1497 struct gsm_sms *sms;
1498
1499 result = dbi_conn_queryf(conn,
1500 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1501 if (!result)
1502 return NULL;
1503
1504 if (!dbi_result_next_row(result)) {
1505 dbi_result_free(result);
1506 return NULL;
1507 }
1508
1509 sms = sms_from_result(net, result);
1510
1511 dbi_result_free(result);
1512
1513 return sms;
1514}
1515
Harald Welte2ebabca2009-08-09 19:05:21 +02001516/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001517struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001518{
1519 dbi_result result;
1520 struct gsm_sms *sms;
1521
1522 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001523 "SELECT SMS.* "
1524 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001525 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001526 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1527 "AND Subscriber.lac > 0 "
1528 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001529 min_id);
1530 if (!result)
1531 return NULL;
1532
1533 if (!dbi_result_next_row(result)) {
1534 dbi_result_free(result);
1535 return NULL;
1536 }
1537
1538 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001539
1540 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001541
1542 return sms;
1543}
1544
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001545struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1546 unsigned long long min_subscr_id,
1547 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001548{
1549 dbi_result result;
1550 struct gsm_sms *sms;
1551
1552 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001553 "SELECT SMS.* "
1554 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001555 "SMS.dest_addr = Subscriber.extension "
1556 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001557 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001558 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001559 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001560 if (!result)
1561 return NULL;
1562
1563 if (!dbi_result_next_row(result)) {
1564 dbi_result_free(result);
1565 return NULL;
1566 }
1567
1568 sms = sms_from_result(net, result);
1569
1570 dbi_result_free(result);
1571
1572 return sms;
1573}
1574
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001575/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001576struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1577{
1578 dbi_result result;
1579 struct gsm_sms *sms;
1580
1581 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001582 "SELECT SMS.* "
1583 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001584 "SMS.dest_addr = Subscriber.extension "
1585 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001586 "AND Subscriber.lac > 0 "
1587 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001588 subscr->id);
1589 if (!result)
1590 return NULL;
1591
1592 if (!dbi_result_next_row(result)) {
1593 dbi_result_free(result);
1594 return NULL;
1595 }
1596
1597 sms = sms_from_result(subscr->net, result);
1598
1599 dbi_result_free(result);
1600
Harald Welte7e310b12009-03-30 20:56:32 +00001601 return sms;
1602}
1603
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001604/* mark a given SMS as delivered */
1605int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001606{
1607 dbi_result result;
1608
1609 result = dbi_conn_queryf(conn,
1610 "UPDATE SMS "
1611 "SET sent = datetime('now') "
1612 "WHERE id = %llu", sms->id);
1613 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001614 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001615 return 1;
1616 }
1617
1618 dbi_result_free(result);
1619 return 0;
1620}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001621
1622/* increase the number of attempted deliveries */
1623int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1624{
1625 dbi_result result;
1626
1627 result = dbi_conn_queryf(conn,
1628 "UPDATE SMS "
1629 "SET deliver_attempts = deliver_attempts + 1 "
1630 "WHERE id = %llu", sms->id);
1631 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001632 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1633 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001634 return 1;
1635 }
1636
1637 dbi_result_free(result);
1638 return 0;
1639}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001640
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001641int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001642 uint8_t apdu_id_flags, uint8_t len,
1643 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001644{
1645 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001646 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001647
1648 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1649
1650 result = dbi_conn_queryf(conn,
1651 "INSERT INTO ApduBlobs "
1652 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1653 "(datetime('now'),%llu,%u,%s)",
1654 subscr->id, apdu_id_flags, q_apdu);
1655
1656 free(q_apdu);
1657
1658 if (!result)
1659 return -EIO;
1660
1661 dbi_result_free(result);
1662 return 0;
1663}
Harald Welteffa55a42009-12-22 19:07:32 +01001664
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001665int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001666{
1667 dbi_result result;
1668 char *q_name;
1669
1670 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1671
1672 result = dbi_conn_queryf(conn,
1673 "INSERT INTO Counters "
1674 "(timestamp,name,value) VALUES "
1675 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1676
1677 free(q_name);
1678
1679 if (!result)
1680 return -EIO;
1681
1682 dbi_result_free(result);
1683 return 0;
1684}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001685
1686static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1687 char *q_prefix)
1688{
1689 dbi_result result;
1690 char *q_name;
1691
1692 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1693 &q_name);
1694
1695 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001696 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001697 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001698 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1699 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001700
1701 free(q_name);
1702
1703 if (!result)
1704 return -EIO;
1705
1706 dbi_result_free(result);
1707 return 0;
1708}
1709
1710int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1711{
1712 unsigned int i;
1713 char *q_prefix;
1714
Harald Weltec1919862010-05-13 12:55:20 +02001715 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001716
1717 for (i = 0; i < ctrg->desc->num_ctr; i++)
1718 db_store_rate_ctr(ctrg, i, q_prefix);
1719
1720 free(q_prefix);
1721
1722 return 0;
1723}