blob: 0935fc54d5a0069f45db665c317872f4b45834d5 [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
Daniel Willmanncdeb8152015-10-08 16:10:23 +020041#include <openssl/rand.h>
42
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010043/* Semi-Private-Interface (SPI) for the subscriber code */
44void subscr_direct_free(struct gsm_subscriber *subscr);
45
Holger Freytherbde36102008-12-28 22:51:39 +000046static char *db_basename = NULL;
47static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000048static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000049
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010050#define SCHEMA_REVISION "4"
Jan Luebbebfbdeec2012-12-27 00:27:16 +010051
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010052enum {
53 SCHEMA_META,
54 INSERT_META,
55 SCHEMA_SUBSCRIBER,
56 SCHEMA_AUTH,
57 SCHEMA_EQUIPMENT,
58 SCHEMA_EQUIPMENT_WATCH,
59 SCHEMA_SMS,
60 SCHEMA_VLR,
61 SCHEMA_APDU,
62 SCHEMA_COUNTERS,
63 SCHEMA_RATE,
64 SCHEMA_AUTHKEY,
65 SCHEMA_AUTHLAST,
66};
67
68static const char *create_stmts[] = {
69 [SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
Harald Welte7e310b12009-03-30 20:56:32 +000070 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
71 "key TEXT UNIQUE NOT NULL, "
72 "value TEXT NOT NULL"
73 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010074 [INSERT_META] = "INSERT OR IGNORE INTO Meta "
Harald Welte7e310b12009-03-30 20:56:32 +000075 "(key, value) "
76 "VALUES "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010077 "('revision', " SCHEMA_REVISION ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010078 [SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
Harald Welte7e310b12009-03-30 20:56:32 +000079 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
80 "created TIMESTAMP NOT NULL, "
81 "updated TIMESTAMP NOT NULL, "
82 "imsi NUMERIC UNIQUE NOT NULL, "
83 "name TEXT, "
84 "extension TEXT UNIQUE, "
85 "authorized INTEGER NOT NULL DEFAULT 0, "
86 "tmsi TEXT UNIQUE, "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010087 "lac INTEGER NOT NULL DEFAULT 0, "
88 "expire_lu TIMESTAMP DEFAULT NULL"
Harald Welte7e310b12009-03-30 20:56:32 +000089 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010090 [SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
Jan Luebbe31bef492009-08-12 14:31:14 +020091 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
92 "subscriber_id INTEGER UNIQUE NOT NULL, "
93 "created TIMESTAMP NOT NULL, "
94 "token TEXT UNIQUE NOT NULL"
95 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010096 [SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
Harald Welte7e310b12009-03-30 20:56:32 +000097 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
98 "created TIMESTAMP NOT NULL, "
99 "updated TIMESTAMP NOT NULL, "
100 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +0200101 "classmark1 NUMERIC, "
102 "classmark2 BLOB, "
103 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +0000104 "imei NUMERIC UNIQUE NOT NULL"
105 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100106 [SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
Harald Welte7e310b12009-03-30 20:56:32 +0000107 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
108 "created TIMESTAMP NOT NULL, "
109 "updated TIMESTAMP NOT NULL, "
110 "subscriber_id NUMERIC NOT NULL, "
111 "equipment_id NUMERIC NOT NULL, "
112 "UNIQUE (subscriber_id, equipment_id) "
113 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100114 [SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +0200115 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +0000116 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
117 "created TIMESTAMP NOT NULL, "
118 "sent TIMESTAMP, "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200119 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +0200120 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +0200121 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +0200122 "reply_path_req INTEGER NOT NULL, "
123 "status_rep_req INTEGER NOT NULL, "
124 "protocol_id INTEGER NOT NULL, "
125 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200126 "ud_hdr_ind INTEGER NOT NULL, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200127 "src_addr TEXT NOT NULL, "
128 "src_ton INTEGER NOT NULL, "
129 "src_npi INTEGER NOT NULL, "
130 "dest_addr TEXT NOT NULL, "
131 "dest_ton INTEGER NOT NULL, "
132 "dest_npi INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200133 "user_data BLOB, " /* TP-UD */
134 /* additional data, interpreted from SMS */
135 "header BLOB, " /* UD Header */
136 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000137 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100138 [SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
Holger Freytherc2995ea2009-04-19 06:35:23 +0000139 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
140 "created TIMESTAMP NOT NULL, "
141 "updated TIMESTAMP NOT NULL, "
142 "subscriber_id NUMERIC UNIQUE NOT NULL, "
143 "last_bts NUMERIC NOT NULL "
144 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100145 [SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
Harald Welte (local)026531e2009-08-16 10:40:10 +0200146 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
147 "created TIMESTAMP NOT NULL, "
148 "apdu_id_flags INTEGER NOT NULL, "
149 "subscriber_id INTEGER NOT NULL, "
150 "apdu BLOB "
151 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100152 [SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
Harald Welteffa55a42009-12-22 19:07:32 +0100153 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
154 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100155 "value INTEGER NOT NULL, "
156 "name TEXT NOT NULL "
Harald Welte09f7ad02009-12-24 09:42:07 +0100157 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100158 [SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
Harald Weltec1919862010-05-13 12:55:20 +0200159 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
160 "timestamp TIMESTAMP NOT NULL, "
161 "value INTEGER NOT NULL, "
162 "name TEXT NOT NULL, "
Harald Welted94d6a02010-05-14 17:38:47 +0200163 "idx INTEGER NOT NULL "
Harald Weltec1919862010-05-13 12:55:20 +0200164 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100165 [SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200166 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut77d334a2009-12-27 19:26:12 +0100167 "algorithm_id INTEGER NOT NULL, "
Harald Welte3606cc52009-12-05 15:13:22 +0530168 "a3a8_ki BLOB "
169 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100170 [SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200171 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut70881b72009-12-27 15:41:59 +0100172 "issued TIMESTAMP NOT NULL, "
173 "use_count INTEGER NOT NULL DEFAULT 0, "
174 "key_seq INTEGER NOT NULL, "
175 "rand BLOB NOT NULL, "
176 "sres BLOB NOT NULL, "
177 "kc BLOB NOT NULL "
Harald Welteffa55a42009-12-22 19:07:32 +0100178 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000179};
180
Harald Welte0b906d02009-12-24 11:21:42 +0100181void db_error_func(dbi_conn conn, void *data)
182{
183 const char *msg;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000184 dbi_conn_error(conn, &msg);
Harald Welteae1f1592009-12-24 11:39:14 +0100185 LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
Harald Weltec7548a12014-07-10 20:18:15 +0200186 osmo_log_backtrace(DDB, LOGL_ERROR);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000187}
188
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100189static int update_db_revision_2(void)
190{
191 dbi_result result;
192
193 result = dbi_conn_query(conn,
194 "ALTER TABLE Subscriber "
195 "ADD COLUMN expire_lu "
196 "TIMESTAMP DEFAULT NULL");
197 if (!result) {
198 LOGP(DDB, LOGL_ERROR,
Alexander Chemeris7e20f642014-03-07 16:59:53 +0100199 "Failed to alter table Subscriber (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100200 return -EINVAL;
201 }
202 dbi_result_free(result);
203
204 result = dbi_conn_query(conn,
205 "UPDATE Meta "
206 "SET value = '3' "
207 "WHERE key = 'revision'");
208 if (!result) {
209 LOGP(DDB, LOGL_ERROR,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100210 "Failed to update DB schema revision (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100211 return -EINVAL;
212 }
213 dbi_result_free(result);
214
215 return 0;
216}
217
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100218/**
219 * Copied from the normal sms_from_result_v3 to avoid having
220 * to make sure that the real routine will remain backward
221 * compatible.
222 */
223static struct gsm_sms *sms_from_result_v3(dbi_result result)
224{
225 struct gsm_sms *sms = sms_alloc();
226 long long unsigned int sender_id;
227 struct gsm_subscriber *sender;
228 const char *text, *daddr;
229 const unsigned char *user_data;
230 char buf[32];
231
232 if (!sms)
233 return NULL;
234
235 sms->id = dbi_result_get_ulonglong(result, "id");
236
237 sender_id = dbi_result_get_ulonglong(result, "sender_id");
238 snprintf(buf, sizeof(buf), "%llu", sender_id);
239 sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
240 OSMO_ASSERT(sender);
241 strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
242 subscr_direct_free(sender);
243 sender = NULL;
244
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +0200245 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
246 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
247 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
248 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
249 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100250 "data_coding_scheme");
251
252 daddr = dbi_result_get_string(result, "dest_addr");
253 if (daddr) {
254 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
255 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
256 }
257
258 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
259 user_data = dbi_result_get_binary(result, "user_data");
260 if (sms->user_data_len > sizeof(sms->user_data))
261 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
262 memcpy(sms->user_data, user_data, sms->user_data_len);
263
264 text = dbi_result_get_string(result, "text");
265 if (text) {
266 strncpy(sms->text, text, sizeof(sms->text));
267 sms->text[sizeof(sms->text)-1] = '\0';
268 }
269 return sms;
270}
271
272static int update_db_revision_3(void)
273{
274 dbi_result result;
275 struct gsm_sms *sms;
276
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100277 LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
278
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100279 result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
280 if (!result) {
281 LOGP(DDB, LOGL_ERROR,
282 "Failed to begin transaction (upgrade from rev 3)\n");
283 return -EINVAL;
284 }
285 dbi_result_free(result);
286
287 /* Rename old SMS table to be able create a new one */
288 result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
289 if (!result) {
290 LOGP(DDB, LOGL_ERROR,
291 "Failed to rename the old SMS table (upgrade from rev 3).\n");
292 goto rollback;
293 }
294 dbi_result_free(result);
295
296 /* Create new SMS table with all the bells and whistles! */
297 result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
298 if (!result) {
299 LOGP(DDB, LOGL_ERROR,
300 "Failed to create a new SMS table (upgrade from rev 3).\n");
301 goto rollback;
302 }
303 dbi_result_free(result);
304
305 /* Cycle through old messages and convert them to the new format */
Max5c06e402015-07-29 20:20:28 +0200306 result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100307 if (!result) {
308 LOGP(DDB, LOGL_ERROR,
309 "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
310 goto rollback;
311 }
312 while (dbi_result_next_row(result)) {
313 sms = sms_from_result_v3(result);
314 if (db_sms_store(sms) != 0) {
315 LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
316 sms_free(sms);
317 dbi_result_free(result);
318 goto rollback;
319 }
320 sms_free(sms);
321 }
322 dbi_result_free(result);
323
324 /* Remove the temporary table */
325 result = dbi_conn_query(conn, "DROP TABLE SMS_3");
326 if (!result) {
327 LOGP(DDB, LOGL_ERROR,
328 "Failed to drop the old SMS table (upgrade from rev 3).\n");
329 goto rollback;
330 }
331 dbi_result_free(result);
332
333 /* We're done. Bump DB Meta revision to 4 */
334 result = dbi_conn_query(conn,
335 "UPDATE Meta "
336 "SET value = '4' "
337 "WHERE key = 'revision'");
338 if (!result) {
339 LOGP(DDB, LOGL_ERROR,
340 "Failed to update DB schema revision (upgrade from rev 3).\n");
341 goto rollback;
342 }
343 dbi_result_free(result);
344
345 result = dbi_conn_query(conn, "COMMIT TRANSACTION");
346 if (!result) {
347 LOGP(DDB, LOGL_ERROR,
348 "Failed to commit the transaction (upgrade from rev 3)\n");
349 return -EINVAL;
350 }
351
352 /* Shrink DB file size by actually wiping out SMS_3 table data */
353 result = dbi_conn_query(conn, "VACUUM");
354 if (!result)
355 LOGP(DDB, LOGL_ERROR,
356 "VACUUM failed. Ignoring it (upgrade from rev 3).\n");
357 else
358 dbi_result_free(result);
359
360 return 0;
361
362rollback:
363 result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
364 if (!result)
365 LOGP(DDB, LOGL_ERROR,
366 "Rollback failed (upgrade from rev 3).\n");
367 else
368 dbi_result_free(result);
369 return -EINVAL;
370}
371
Harald Welted0b7b772009-08-09 19:03:42 +0200372static int check_db_revision(void)
373{
374 dbi_result result;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100375 const char *rev_s;
Harald Welted0b7b772009-08-09 19:03:42 +0200376
377 result = dbi_conn_query(conn,
378 "SELECT value FROM Meta WHERE key='revision'");
379 if (!result)
380 return -EINVAL;
381
382 if (!dbi_result_next_row(result)) {
383 dbi_result_free(result);
384 return -EINVAL;
385 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100386 rev_s = dbi_result_get_string(result, "value");
387 if (!rev_s) {
Harald Welted0b7b772009-08-09 19:03:42 +0200388 dbi_result_free(result);
389 return -EINVAL;
390 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100391 if (!strcmp(rev_s, "2")) {
392 if (update_db_revision_2()) {
393 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
394 dbi_result_free(result);
395 return -EINVAL;
396 }
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100397 } else if (!strcmp(rev_s, "3")) {
398 if (update_db_revision_3()) {
399 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
400 dbi_result_free(result);
401 return -EINVAL;
402 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100403 } else if (!strcmp(rev_s, SCHEMA_REVISION)) {
404 /* everything is fine */
405 } else {
406 LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s);
407 dbi_result_free(result);
408 return -EINVAL;
409 }
410
411 dbi_result_free(result);
412 return 0;
413}
414
415static int db_configure(void)
416{
417 dbi_result result;
418
419 result = dbi_conn_query(conn,
420 "PRAGMA synchronous = FULL");
421 if (!result)
422 return -EINVAL;
Harald Welted0b7b772009-08-09 19:03:42 +0200423
424 dbi_result_free(result);
425 return 0;
426}
427
Harald Welte0b906d02009-12-24 11:21:42 +0100428int db_init(const char *name)
429{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000430 dbi_initialize(NULL);
Harald Welte0b906d02009-12-24 11:21:42 +0100431
Jan Luebbe5c15c852008-12-27 15:59:25 +0000432 conn = dbi_conn_new("sqlite3");
Harald Welte0b906d02009-12-24 11:21:42 +0100433 if (conn == NULL) {
Harald Welteae1f1592009-12-24 11:39:14 +0100434 LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000435 return 1;
436 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000437
Holger Freyther12aa50d2009-01-01 18:02:05 +0000438 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000439
Jan Luebbe5c15c852008-12-27 15:59:25 +0000440 /* MySQL
441 dbi_conn_set_option(conn, "host", "localhost");
442 dbi_conn_set_option(conn, "username", "your_name");
443 dbi_conn_set_option(conn, "password", "your_password");
444 dbi_conn_set_option(conn, "dbname", "your_dbname");
445 dbi_conn_set_option(conn, "encoding", "UTF-8");
446 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000447
Jan Luebbe5c15c852008-12-27 15:59:25 +0000448 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000449 db_basename = strdup(name);
450 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000451 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
452 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000453
Harald Welted0b7b772009-08-09 19:03:42 +0200454 if (dbi_conn_connect(conn) < 0)
455 goto out_err;
456
Jan Luebbe5c15c852008-12-27 15:59:25 +0000457 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200458
459out_err:
460 free(db_dirname);
461 free(db_basename);
462 db_dirname = db_basename = NULL;
463 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000464}
465
Harald Welted0b7b772009-08-09 19:03:42 +0200466
Harald Welted1476bc2011-07-16 13:24:09 +0200467int db_prepare(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100468{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000469 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000470 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000471
Harald Welte7e310b12009-03-30 20:56:32 +0000472 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
473 result = dbi_conn_query(conn, create_stmts[i]);
Harald Welte0b906d02009-12-24 11:21:42 +0100474 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100475 LOGP(DDB, LOGL_ERROR,
476 "Failed to create some table.\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000477 return 1;
478 }
479 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000480 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000481
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200482 if (check_db_revision() < 0) {
Harald Welteae1f1592009-12-24 11:39:14 +0100483 LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200484 "please update your database schema\n");
485 return -1;
486 }
487
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100488 db_configure();
489
Jan Luebbe5c15c852008-12-27 15:59:25 +0000490 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000491}
492
Harald Welted1476bc2011-07-16 13:24:09 +0200493int db_fini(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100494{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000495 dbi_conn_close(conn);
496 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000497
Harald Welte2c5f4c62011-07-16 13:22:57 +0200498 free(db_dirname);
499 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000500 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000501}
502
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200503struct gsm_subscriber *db_create_subscriber(const char *imsi)
Harald Welte9176bd42009-07-23 18:46:00 +0200504{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000505 dbi_result result;
Harald Welte0b906d02009-12-24 11:21:42 +0100506 struct gsm_subscriber *subscr;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000507
508 /* Is this subscriber known in the db? */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200509 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000510 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000511 result = dbi_conn_queryf(conn,
512 "UPDATE Subscriber set updated = datetime('now') "
513 "WHERE imsi = %s " , imsi);
Harald Welte0b906d02009-12-24 11:21:42 +0100514 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100515 LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n");
Harald Welte0b906d02009-12-24 11:21:42 +0100516 else
Harald Welte523200b2008-12-30 14:58:44 +0000517 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000518 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000519 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000520
521 subscr = subscr_alloc();
522 if (!subscr)
523 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200524 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000525 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000526 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000527 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000528 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000529 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000530 imsi
531 );
Harald Welte0b906d02009-12-24 11:21:42 +0100532 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100533 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000534 subscr->id = dbi_conn_sequence_last(conn, NULL);
535 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000536 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100537 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200538 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000539 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000540}
541
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200542osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200543
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200544static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
545{
546 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200547 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100548 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200549 const unsigned char *cm2, *cm3;
550 struct gsm_equipment *equip = &subscr->equipment;
551
552 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200553 "SELECT Equipment.* "
554 "FROM Equipment JOIN EquipmentWatch ON "
555 "EquipmentWatch.equipment_id=Equipment.id "
556 "WHERE EquipmentWatch.subscriber_id = %llu "
557 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200558 if (!result)
559 return -EIO;
560
561 if (!dbi_result_next_row(result)) {
562 dbi_result_free(result);
563 return -ENOENT;
564 }
565
566 equip->id = dbi_result_get_ulonglong(result, "id");
567
568 string = dbi_result_get_string(result, "imei");
569 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200570 strncpy(equip->imei, string, sizeof(equip->imei)-1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200571
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100572 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200573 if (string) {
574 cm1 = atoi(string) & 0xff;
575 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
576 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200577
578 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
579 cm2 = dbi_result_get_binary(result, "classmark2");
580 if (equip->classmark2_len > sizeof(equip->classmark2))
581 equip->classmark2_len = sizeof(equip->classmark2);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100582 if (cm2)
583 memcpy(equip->classmark2, cm2, equip->classmark2_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200584
585 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
586 cm3 = dbi_result_get_binary(result, "classmark3");
587 if (equip->classmark3_len > sizeof(equip->classmark3))
588 equip->classmark3_len = sizeof(equip->classmark3);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100589 if (cm3)
590 memcpy(equip->classmark3, cm3, equip->classmark3_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200591
592 dbi_result_free(result);
593
594 return 0;
595}
Harald Welte3606cc52009-12-05 15:13:22 +0530596
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200597int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
598 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530599{
600 dbi_result result;
601 const unsigned char *a3a8_ki;
602
603 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200604 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530605 subscr->id);
606 if (!result)
607 return -EIO;
608
609 if (!dbi_result_next_row(result)) {
610 dbi_result_free(result);
611 return -ENOENT;
612 }
613
614 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
615 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
616 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100617 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200618 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530619 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
620
621 dbi_result_free(result);
622
623 return 0;
624}
625
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200626int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
627 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100628{
629 dbi_result result;
630 struct gsm_auth_info ainfo_old;
631 int rc, upd;
632 unsigned char *ki_str;
633
634 /* Deletion ? */
635 if (ainfo == NULL) {
636 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200637 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100638 subscr->id);
639
640 if (!result)
641 return -EIO;
642
643 dbi_result_free(result);
644
645 return 0;
646 }
647
648 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200649 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100650 if (rc && rc != -ENOENT)
651 return rc;
652 upd = rc ? 0 : 1;
653
654 /* Update / Insert */
655 dbi_conn_quote_binary_copy(conn,
656 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
657
658 if (!upd) {
659 result = dbi_conn_queryf(conn,
660 "INSERT INTO AuthKeys "
661 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200662 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100663 subscr->id, ainfo->auth_algo, ki_str);
664 } else {
665 result = dbi_conn_queryf(conn,
666 "UPDATE AuthKeys "
667 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200668 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100669 ainfo->auth_algo, ki_str, subscr->id);
670 }
671
672 free(ki_str);
673
674 if (!result)
675 return -EIO;
676
677 dbi_result_free(result);
678
679 return 0;
680}
681
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200682int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
683 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530684{
685 dbi_result result;
686 int len;
687 const unsigned char *blob;
688
689 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200690 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530691 subscr->id);
692 if (!result)
693 return -EIO;
694
695 if (!dbi_result_next_row(result)) {
696 dbi_result_free(result);
697 return -ENOENT;
698 }
699
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200700 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530701
Sylvain Munaut70881b72009-12-27 15:41:59 +0100702 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
703 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
704
Harald Welte3606cc52009-12-05 15:13:22 +0530705 len = dbi_result_get_field_length(result, "rand");
706 if (len != sizeof(atuple->rand))
707 goto err_size;
708
709 blob = dbi_result_get_binary(result, "rand");
710 memcpy(atuple->rand, blob, len);
711
712 len = dbi_result_get_field_length(result, "sres");
713 if (len != sizeof(atuple->sres))
714 goto err_size;
715
716 blob = dbi_result_get_binary(result, "sres");
717 memcpy(atuple->sres, blob, len);
718
719 len = dbi_result_get_field_length(result, "kc");
720 if (len != sizeof(atuple->kc))
721 goto err_size;
722
723 blob = dbi_result_get_binary(result, "kc");
724 memcpy(atuple->kc, blob, len);
725
726 dbi_result_free(result);
727
728 return 0;
729
730err_size:
731 dbi_result_free(result);
732 return -EIO;
733}
734
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200735int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
736 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100737{
738 dbi_result result;
739 int rc, upd;
740 struct gsm_auth_tuple atuple_old;
741 unsigned char *rand_str, *sres_str, *kc_str;
742
743 /* Deletion ? */
744 if (atuple == NULL) {
745 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200746 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100747 subscr->id);
748
749 if (!result)
750 return -EIO;
751
752 dbi_result_free(result);
753
754 return 0;
755 }
756
757 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200758 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100759 if (rc && rc != -ENOENT)
760 return rc;
761 upd = rc ? 0 : 1;
762
763 /* Update / Insert */
764 dbi_conn_quote_binary_copy(conn,
765 atuple->rand, sizeof(atuple->rand), &rand_str);
766 dbi_conn_quote_binary_copy(conn,
767 atuple->sres, sizeof(atuple->sres), &sres_str);
768 dbi_conn_quote_binary_copy(conn,
769 atuple->kc, sizeof(atuple->kc), &kc_str);
770
771 if (!upd) {
772 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200773 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100774 "(subscriber_id, issued, use_count, "
775 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200776 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100777 "%u, %s, %s, %s ) ",
778 subscr->id, atuple->use_count, atuple->key_seq,
779 rand_str, sres_str, kc_str);
780 } else {
781 char *issued = atuple->key_seq == atuple_old.key_seq ?
782 "issued" : "datetime('now')";
783 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200784 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100785 "SET issued=%s, use_count=%u, "
786 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200787 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100788 issued, atuple->use_count, atuple->key_seq,
789 rand_str, sres_str, kc_str, subscr->id);
790 }
791
792 free(rand_str);
793 free(sres_str);
794 free(kc_str);
795
796 if (!result)
797 return -EIO;
798
799 dbi_result_free(result);
800
801 return 0;
802}
803
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100804static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
805{
806 const char *string;
807 string = dbi_result_get_string(result, "imsi");
808 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200809 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH-1);
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100810
811 string = dbi_result_get_string(result, "tmsi");
812 if (string)
813 subscr->tmsi = tmsi_from_string(string);
814
815 string = dbi_result_get_string(result, "name");
816 if (string)
817 strncpy(subscr->name, string, GSM_NAME_LENGTH);
818
819 string = dbi_result_get_string(result, "extension");
820 if (string)
821 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
822
Kevin Redonc9763a32013-11-04 22:43:15 +0100823 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100824
825 if (!dbi_result_field_is_null(result, "expire_lu"))
826 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
827 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200828 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100829
Kevin Redonc9763a32013-11-04 22:43:15 +0100830 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
831
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100832}
833
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200834#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200835struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200836 const char *id)
837{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000838 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000839 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000840 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000841
Jan Luebbe5c15c852008-12-27 15:59:25 +0000842 switch (field) {
843 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000844 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000845 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200846 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000847 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000848 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000849 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000850 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000851 break;
852 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000853 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000854 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200855 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000856 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000857 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000858 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000859 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000860 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000861 case GSM_SUBSCRIBER_EXTENSION:
862 dbi_conn_quote_string_copy(conn, id, &quoted);
863 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200864 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000865 "WHERE extension = %s ",
866 quoted
867 );
868 free(quoted);
869 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200870 case GSM_SUBSCRIBER_ID:
871 dbi_conn_quote_string_copy(conn, id, &quoted);
872 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200873 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200874 "WHERE id = %s ", quoted);
875 free(quoted);
876 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000877 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100878 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000879 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000880 }
Harald Welte0b906d02009-12-24 11:21:42 +0100881 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100882 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000883 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000884 }
885 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100886 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000887 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000888 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000889 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000890 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000891
892 subscr = subscr_alloc();
893 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000894
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100895 db_set_from_query(subscr, result);
Harald Welte3ad03462016-03-17 14:41:26 +0100896 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
897 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
898 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000899 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200900
901 get_equipment_by_subscr(subscr);
902
Holger Freyther12aa50d2009-01-01 18:02:05 +0000903 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000904}
905
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100906int db_subscriber_update(struct gsm_subscriber *subscr)
907{
908 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100909 dbi_result result;
910
911 /* Copy the id to a string as queryf with %llu is failing */
912 sprintf(buf, "%llu", subscr->id);
913 result = dbi_conn_queryf(conn,
914 BASE_QUERY
915 "WHERE id = %s", buf);
916
917 if (!result) {
918 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
919 return -EIO;
920 }
921 if (!dbi_result_next_row(result)) {
922 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
923 subscr->id);
924 dbi_result_free(result);
925 return -EIO;
926 }
927
928 db_set_from_query(subscr, result);
929 dbi_result_free(result);
930 get_equipment_by_subscr(subscr);
931
932 return 0;
933}
934
Harald Welte0b906d02009-12-24 11:21:42 +0100935int db_sync_subscriber(struct gsm_subscriber *subscriber)
936{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000937 dbi_result result;
Harald Welte3ad03462016-03-17 14:41:26 +0100938 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100939 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200940
Harald Welte019d0162010-12-26 19:12:30 +0100941 dbi_conn_quote_string_copy(conn,
942 subscriber->name, &q_name);
943 dbi_conn_quote_string_copy(conn,
944 subscriber->extension, &q_extension);
945
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200946 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
Harald Welte3ad03462016-03-17 14:41:26 +0100947 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200948 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200949 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200950 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200951 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200952 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100953
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200954 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
955 result = dbi_conn_queryf(conn,
956 "UPDATE Subscriber "
957 "SET updated = datetime('now'), "
958 "name = %s, "
959 "extension = %s, "
960 "authorized = %i, "
961 "tmsi = %s, "
962 "lac = %i, "
963 "expire_lu = NULL "
964 "WHERE imsi = %s ",
965 q_name,
966 q_extension,
967 subscriber->authorized,
968 q_tmsi,
969 subscriber->lac,
970 subscriber->imsi);
971 } else {
972 result = dbi_conn_queryf(conn,
973 "UPDATE Subscriber "
974 "SET updated = datetime('now'), "
975 "name = %s, "
976 "extension = %s, "
977 "authorized = %i, "
978 "tmsi = %s, "
979 "lac = %i, "
980 "expire_lu = datetime(%i, 'unixepoch') "
981 "WHERE imsi = %s ",
982 q_name,
983 q_extension,
984 subscriber->authorized,
985 q_tmsi,
986 subscriber->lac,
987 (int) subscriber->expire_lu,
988 subscriber->imsi);
989 }
Harald Welte0b906d02009-12-24 11:21:42 +0100990
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200991 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100992 free(q_name);
993 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100994
995 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100996 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000997 return 1;
998 }
Harald Welte0b906d02009-12-24 11:21:42 +0100999
Jan Luebbe5c15c852008-12-27 15:59:25 +00001000 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001001
Jan Luebbe5c15c852008-12-27 15:59:25 +00001002 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001003}
1004
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001005int db_subscriber_delete(struct gsm_subscriber *subscr)
1006{
1007 dbi_result result;
1008
1009 result = dbi_conn_queryf(conn,
1010 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1011 subscr->id);
1012 if (!result) {
1013 LOGP(DDB, LOGL_ERROR,
1014 "Failed to delete Authkeys for %llu\n", subscr->id);
1015 return -1;
1016 }
1017 dbi_result_free(result);
1018
1019 result = dbi_conn_queryf(conn,
1020 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1021 subscr->id);
1022 if (!result) {
1023 LOGP(DDB, LOGL_ERROR,
1024 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1025 return -1;
1026 }
1027 dbi_result_free(result);
1028
1029 result = dbi_conn_queryf(conn,
1030 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1031 subscr->id);
1032 if (!result) {
1033 LOGP(DDB, LOGL_ERROR,
1034 "Failed to delete AuthToken for %llu\n", subscr->id);
1035 return -1;
1036 }
1037 dbi_result_free(result);
1038
1039 result = dbi_conn_queryf(conn,
1040 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1041 subscr->id);
1042 if (!result) {
1043 LOGP(DDB, LOGL_ERROR,
1044 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1045 return -1;
1046 }
1047 dbi_result_free(result);
1048
1049 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001050 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1051 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001052 if (!result) {
1053 LOGP(DDB, LOGL_ERROR,
1054 "Failed to delete SMS for %llu\n", subscr->id);
1055 return -1;
1056 }
1057 dbi_result_free(result);
1058
1059 result = dbi_conn_queryf(conn,
1060 "DELETE FROM VLR WHERE subscriber_id=%llu",
1061 subscr->id);
1062 if (!result) {
1063 LOGP(DDB, LOGL_ERROR,
1064 "Failed to delete VLR for %llu\n", subscr->id);
1065 return -1;
1066 }
1067 dbi_result_free(result);
1068
1069 result = dbi_conn_queryf(conn,
1070 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1071 subscr->id);
1072 if (!result) {
1073 LOGP(DDB, LOGL_ERROR,
1074 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1075 return -1;
1076 }
1077 dbi_result_free(result);
1078
1079 result = dbi_conn_queryf(conn,
1080 "DELETE FROM Subscriber WHERE id=%llu",
1081 subscr->id);
1082 if (!result) {
1083 LOGP(DDB, LOGL_ERROR,
1084 "Failed to delete Subscriber for %llu\n", subscr->id);
1085 return -1;
1086 }
1087 dbi_result_free(result);
1088
1089 return 0;
1090}
1091
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001092/**
1093 * List all the authorized and non-expired subscribers. The callback will
1094 * be called one by one. The subscr argument is not fully initialize and
1095 * subscr_get/subscr_put must not be called. The passed in pointer will be
1096 * deleted after the callback by the database call.
1097 */
1098int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1099{
1100 dbi_result result;
1101
Max5c06e402015-07-29 20:20:28 +02001102 result = dbi_conn_query(conn,
1103 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001104 if (!result) {
1105 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1106 return -1;
1107 }
1108
1109 while (dbi_result_next_row(result)) {
1110 struct gsm_subscriber *subscr;
1111
1112 subscr = subscr_alloc();
1113 subscr->id = dbi_result_get_ulonglong(result, "id");
1114 db_set_from_query(subscr, result);
1115 cb(subscr, closure);
1116 OSMO_ASSERT(subscr->use_count == 1);
1117 llist_del(&subscr->entry);
1118 talloc_free(subscr);
1119 }
1120
1121 dbi_result_free(result);
1122 return 0;
1123}
1124
Harald Weltec2e302d2009-07-05 14:08:13 +02001125int db_sync_equipment(struct gsm_equipment *equip)
1126{
1127 dbi_result result;
1128 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001129 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001130 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001131
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001132 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001133 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001134 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001135 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001136 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001137 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001138 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001139 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001140 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001141 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001142
Harald Weltec2e302d2009-07-05 14:08:13 +02001143 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1144 equip->classmark2_len, &cm2);
1145 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1146 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001147 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001148
1149 result = dbi_conn_queryf(conn,
1150 "UPDATE Equipment SET "
1151 "updated = datetime('now'), "
1152 "classmark1 = %u, "
1153 "classmark2 = %s, "
1154 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001155 "WHERE imei = %s ",
1156 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001157
1158 free(cm2);
1159 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001160 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001161
1162 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001163 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001164 return -EIO;
1165 }
1166
1167 dbi_result_free(result);
1168 return 0;
1169}
1170
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001171int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1172{
1173 dbi_result result;
1174
1175 result = dbi_conn_query(conn,
1176 "SELECT id "
1177 "FROM Subscriber "
1178 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001179 "( expire_lu is NOT NULL "
1180 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001181 "LIMIT 1");
1182 if (!result) {
1183 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1184 return -EIO;
1185 }
1186
1187 while (dbi_result_next_row(result))
1188 callback(priv, dbi_result_get_ulonglong(result, "id"));
1189
1190 dbi_result_free(result);
1191 return 0;
1192}
1193
Harald Welte0b906d02009-12-24 11:21:42 +01001194int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1195{
1196 dbi_result result = NULL;
Harald Welte3ad03462016-03-17 14:41:26 +01001197 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001198 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001199
Jan Luebbe5c15c852008-12-27 15:59:25 +00001200 for (;;) {
Daniel Willmanncdeb8152015-10-08 16:10:23 +02001201 if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
1202 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1203 return 1;
1204 }
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001205 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1206 continue;
1207
Harald Welte3ad03462016-03-17 14:41:26 +01001208 sprintf(tmsi, "%u", subscriber->tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001209 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001210 result = dbi_conn_queryf(conn,
1211 "SELECT * FROM Subscriber "
1212 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001213 tmsi_quoted);
1214
Holger Freyther12aa50d2009-01-01 18:02:05 +00001215 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001216
1217 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001218 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1219 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001220 return 1;
1221 }
Harald Welte0b906d02009-12-24 11:21:42 +01001222 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001223 dbi_result_free(result);
1224 continue;
1225 }
1226 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001227 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001228 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1229 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001230 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001231 }
1232 dbi_result_free(result);
1233 }
1234 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001235}
1236
Harald Welte0b906d02009-12-24 11:21:42 +01001237int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1238{
1239 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001240 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001241
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001242 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001243 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001244 result = dbi_conn_queryf(conn,
1245 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001246 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001247 try
1248 );
Harald Welte0b906d02009-12-24 11:21:42 +01001249 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001250 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1251 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001252 return 1;
1253 }
1254 if (dbi_result_get_numrows(result)){
1255 dbi_result_free(result);
1256 continue;
1257 }
1258 if (!dbi_result_next_row(result)) {
1259 dbi_result_free(result);
1260 break;
1261 }
1262 dbi_result_free(result);
1263 }
1264 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001265 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001266 return db_sync_subscriber(subscriber);
1267}
Jan Luebbe31bef492009-08-12 14:31:14 +02001268/*
1269 * try to allocate a new unique token for this subscriber and return it
1270 * via a parameter. if the subscriber already has a token, return
1271 * an error.
1272 */
1273
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001274int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001275{
1276 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001277 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001278
Jan Luebbe31bef492009-08-12 14:31:14 +02001279 for (;;) {
Daniel Willmann2aedfbd2015-10-08 16:10:26 +02001280 if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
1281 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1282 return 1;
1283 }
Jan Luebbe31bef492009-08-12 14:31:14 +02001284 if (!try) /* 0 is an invalid token */
1285 continue;
1286 result = dbi_conn_queryf(conn,
1287 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001288 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1289 subscriber->id, try);
1290 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001291 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1292 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001293 return 1;
1294 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001295 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001296 dbi_result_free(result);
1297 continue;
1298 }
1299 if (!dbi_result_next_row(result)) {
1300 dbi_result_free(result);
1301 break;
1302 }
1303 dbi_result_free(result);
1304 }
1305 result = dbi_conn_queryf(conn,
1306 "INSERT INTO AuthToken "
1307 "(subscriber_id, created, token) "
1308 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001309 "(%llu, datetime('now'), \"%08X\") ",
1310 subscriber->id, try);
1311 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001312 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1313 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001314 return 1;
1315 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001316 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001317 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001318 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001319
Jan Luebbe31bef492009-08-12 14:31:14 +02001320 return 0;
1321}
1322
Harald Welte0b906d02009-12-24 11:21:42 +01001323int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1324{
Harald Welted409be72009-11-07 00:06:19 +09001325 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001326 dbi_result result;
1327
Harald Weltec2e302d2009-07-05 14:08:13 +02001328 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001329 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001330
Jan Luebbefac25fc2008-12-27 18:04:34 +00001331 result = dbi_conn_queryf(conn,
1332 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001333 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001334 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001335 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001336 imei);
1337 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001338 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001339 return 1;
1340 }
Harald Welte0b906d02009-12-24 11:21:42 +01001341
Jan Luebbe391d86e2008-12-27 22:33:34 +00001342 equipment_id = 0;
1343 if (dbi_result_get_numrows_affected(result)) {
1344 equipment_id = dbi_conn_sequence_last(conn, NULL);
1345 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001346 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001347
1348 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001349 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001350 else {
1351 result = dbi_conn_queryf(conn,
1352 "SELECT id FROM Equipment "
1353 "WHERE imei = %s ",
1354 imei
1355 );
Harald Welte0b906d02009-12-24 11:21:42 +01001356 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001357 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001358 return 1;
1359 }
1360 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001361 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001362 dbi_result_free(result);
1363 return 1;
1364 }
1365 equipment_id = dbi_result_get_ulonglong(result, "id");
1366 dbi_result_free(result);
1367 }
1368
1369 result = dbi_conn_queryf(conn,
1370 "INSERT OR IGNORE INTO EquipmentWatch "
1371 "(subscriber_id, equipment_id, created, updated) "
1372 "VALUES "
1373 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001374 subscriber->id, equipment_id);
1375 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001376 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001377 return 1;
1378 }
Harald Welte0b906d02009-12-24 11:21:42 +01001379
Jan Luebbe391d86e2008-12-27 22:33:34 +00001380 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001381 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001382 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001383
Jan Luebbefac25fc2008-12-27 18:04:34 +00001384 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001385 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001386 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1387 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001388 else {
1389 result = dbi_conn_queryf(conn,
1390 "UPDATE EquipmentWatch "
1391 "SET updated = datetime('now') "
1392 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001393 subscriber->id, equipment_id);
1394 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001395 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001396 return 1;
1397 }
1398 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001399 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1400 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001401 }
1402
1403 return 0;
1404}
1405
Harald Welte7e310b12009-03-30 20:56:32 +00001406/* store an [unsent] SMS to the database */
1407int db_sms_store(struct gsm_sms *sms)
1408{
1409 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001410 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001411 unsigned char *q_udata;
1412 char *validity_timestamp = "2222-2-2";
1413
1414 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001415
1416 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001417 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001418 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001419 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1420 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001421
Harald Weltef3efc592009-07-27 20:11:35 +02001422 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001423 result = dbi_conn_queryf(conn,
1424 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001425 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001426 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001427 "data_coding_scheme, ud_hdr_ind, "
1428 "user_data, text, "
1429 "dest_addr, dest_ton, dest_npi, "
1430 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001431 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001432 "%u, %u, %u, "
1433 "%u, %u, "
1434 "%s, %s, "
1435 "%s, %u, %u, "
1436 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001437 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001438 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001439 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001440 q_udata, q_text,
1441 q_daddr, sms->dst.ton, sms->dst.npi,
1442 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001443 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001444 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001445 free(q_daddr);
1446 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001447
1448 if (!result)
1449 return -EIO;
1450
1451 dbi_result_free(result);
1452 return 0;
1453}
1454
Harald Welte2ebabca2009-08-09 19:05:21 +02001455static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001456{
Harald Welte76042182009-08-08 16:03:15 +02001457 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001458 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001459 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001460
Harald Welte76042182009-08-08 16:03:15 +02001461 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001462 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001463
Harald Weltebe3e3782009-07-05 14:06:41 +02001464 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001465
Harald Weltef3efc592009-07-27 20:11:35 +02001466 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001467 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001468 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1469 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1470 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1471 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1472 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001473 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001474 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001475
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001476 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1477 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001478 daddr = dbi_result_get_string(result, "dest_addr");
1479 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001480 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1481 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001482 }
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001483 sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001484
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001485 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1486 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001487 saddr = dbi_result_get_string(result, "src_addr");
1488 if (saddr) {
1489 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1490 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1491 }
1492
Harald Welte76042182009-08-08 16:03:15 +02001493 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1494 user_data = dbi_result_get_binary(result, "user_data");
1495 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001496 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001497 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001498
1499 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001500 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001501 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001502 sms->text[sizeof(sms->text)-1] = '\0';
1503 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001504 return sms;
1505}
1506
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001507struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1508{
1509 dbi_result result;
1510 struct gsm_sms *sms;
1511
1512 result = dbi_conn_queryf(conn,
1513 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1514 if (!result)
1515 return NULL;
1516
1517 if (!dbi_result_next_row(result)) {
1518 dbi_result_free(result);
1519 return NULL;
1520 }
1521
1522 sms = sms_from_result(net, result);
1523
1524 dbi_result_free(result);
1525
1526 return sms;
1527}
1528
Harald Welte2ebabca2009-08-09 19:05:21 +02001529/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001530struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001531{
1532 dbi_result result;
1533 struct gsm_sms *sms;
1534
1535 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001536 "SELECT SMS.* "
1537 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001538 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001539 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1540 "AND Subscriber.lac > 0 "
1541 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001542 min_id);
1543 if (!result)
1544 return NULL;
1545
1546 if (!dbi_result_next_row(result)) {
1547 dbi_result_free(result);
1548 return NULL;
1549 }
1550
1551 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001552
1553 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001554
1555 return sms;
1556}
1557
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001558struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1559 unsigned long long min_subscr_id,
1560 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001561{
1562 dbi_result result;
1563 struct gsm_sms *sms;
1564
1565 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001566 "SELECT SMS.* "
1567 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001568 "SMS.dest_addr = Subscriber.extension "
1569 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001570 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001571 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001572 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001573 if (!result)
1574 return NULL;
1575
1576 if (!dbi_result_next_row(result)) {
1577 dbi_result_free(result);
1578 return NULL;
1579 }
1580
1581 sms = sms_from_result(net, result);
1582
1583 dbi_result_free(result);
1584
1585 return sms;
1586}
1587
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001588/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001589struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1590{
1591 dbi_result result;
1592 struct gsm_sms *sms;
1593
1594 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001595 "SELECT SMS.* "
1596 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001597 "SMS.dest_addr = Subscriber.extension "
1598 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001599 "AND Subscriber.lac > 0 "
1600 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001601 subscr->id);
1602 if (!result)
1603 return NULL;
1604
1605 if (!dbi_result_next_row(result)) {
1606 dbi_result_free(result);
1607 return NULL;
1608 }
1609
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001610 sms = sms_from_result(subscr->group->net, result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001611
1612 dbi_result_free(result);
1613
Harald Welte7e310b12009-03-30 20:56:32 +00001614 return sms;
1615}
1616
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001617/* mark a given SMS as delivered */
1618int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001619{
1620 dbi_result result;
1621
1622 result = dbi_conn_queryf(conn,
1623 "UPDATE SMS "
1624 "SET sent = datetime('now') "
1625 "WHERE id = %llu", sms->id);
1626 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001627 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001628 return 1;
1629 }
1630
1631 dbi_result_free(result);
1632 return 0;
1633}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001634
1635/* increase the number of attempted deliveries */
1636int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1637{
1638 dbi_result result;
1639
1640 result = dbi_conn_queryf(conn,
1641 "UPDATE SMS "
1642 "SET deliver_attempts = deliver_attempts + 1 "
1643 "WHERE id = %llu", sms->id);
1644 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001645 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1646 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001647 return 1;
1648 }
1649
1650 dbi_result_free(result);
1651 return 0;
1652}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001653
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001654int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001655 uint8_t apdu_id_flags, uint8_t len,
1656 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001657{
1658 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001659 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001660
1661 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1662
1663 result = dbi_conn_queryf(conn,
1664 "INSERT INTO ApduBlobs "
1665 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1666 "(datetime('now'),%llu,%u,%s)",
1667 subscr->id, apdu_id_flags, q_apdu);
1668
1669 free(q_apdu);
1670
1671 if (!result)
1672 return -EIO;
1673
1674 dbi_result_free(result);
1675 return 0;
1676}
Harald Welteffa55a42009-12-22 19:07:32 +01001677
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001678int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001679{
1680 dbi_result result;
1681 char *q_name;
1682
1683 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1684
1685 result = dbi_conn_queryf(conn,
1686 "INSERT INTO Counters "
1687 "(timestamp,name,value) VALUES "
1688 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1689
1690 free(q_name);
1691
1692 if (!result)
1693 return -EIO;
1694
1695 dbi_result_free(result);
1696 return 0;
1697}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001698
1699static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1700 char *q_prefix)
1701{
1702 dbi_result result;
1703 char *q_name;
1704
1705 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1706 &q_name);
1707
1708 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001709 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001710 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001711 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1712 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001713
1714 free(q_name);
1715
1716 if (!result)
1717 return -EIO;
1718
1719 dbi_result_free(result);
1720 return 0;
1721}
1722
1723int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1724{
1725 unsigned int i;
1726 char *q_prefix;
1727
Harald Weltec1919862010-05-13 12:55:20 +02001728 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001729
1730 for (i = 0; i < ctrg->desc->num_ctr; i++)
1731 db_store_rate_ctr(ctrg, i, q_prefix);
1732
1733 free(q_prefix);
1734
1735 return 0;
1736}