blob: 9f265901c84a6ee72464ee05ce8463ee77937d34 [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);
582 memcpy(equip->classmark2, cm2, equip->classmark2_len);
583
584 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
585 cm3 = dbi_result_get_binary(result, "classmark3");
586 if (equip->classmark3_len > sizeof(equip->classmark3))
587 equip->classmark3_len = sizeof(equip->classmark3);
588 memcpy(equip->classmark3, cm3, equip->classmark3_len);
589
590 dbi_result_free(result);
591
592 return 0;
593}
Harald Welte3606cc52009-12-05 15:13:22 +0530594
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200595int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
596 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530597{
598 dbi_result result;
599 const unsigned char *a3a8_ki;
600
601 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200602 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530603 subscr->id);
604 if (!result)
605 return -EIO;
606
607 if (!dbi_result_next_row(result)) {
608 dbi_result_free(result);
609 return -ENOENT;
610 }
611
612 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
613 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
614 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100615 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200616 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530617 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
618
619 dbi_result_free(result);
620
621 return 0;
622}
623
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200624int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
625 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100626{
627 dbi_result result;
628 struct gsm_auth_info ainfo_old;
629 int rc, upd;
630 unsigned char *ki_str;
631
632 /* Deletion ? */
633 if (ainfo == NULL) {
634 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200635 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100636 subscr->id);
637
638 if (!result)
639 return -EIO;
640
641 dbi_result_free(result);
642
643 return 0;
644 }
645
646 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200647 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100648 if (rc && rc != -ENOENT)
649 return rc;
650 upd = rc ? 0 : 1;
651
652 /* Update / Insert */
653 dbi_conn_quote_binary_copy(conn,
654 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
655
656 if (!upd) {
657 result = dbi_conn_queryf(conn,
658 "INSERT INTO AuthKeys "
659 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200660 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100661 subscr->id, ainfo->auth_algo, ki_str);
662 } else {
663 result = dbi_conn_queryf(conn,
664 "UPDATE AuthKeys "
665 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200666 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100667 ainfo->auth_algo, ki_str, subscr->id);
668 }
669
670 free(ki_str);
671
672 if (!result)
673 return -EIO;
674
675 dbi_result_free(result);
676
677 return 0;
678}
679
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200680int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
681 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530682{
683 dbi_result result;
684 int len;
685 const unsigned char *blob;
686
687 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200688 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530689 subscr->id);
690 if (!result)
691 return -EIO;
692
693 if (!dbi_result_next_row(result)) {
694 dbi_result_free(result);
695 return -ENOENT;
696 }
697
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200698 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530699
Sylvain Munaut70881b72009-12-27 15:41:59 +0100700 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
701 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
702
Harald Welte3606cc52009-12-05 15:13:22 +0530703 len = dbi_result_get_field_length(result, "rand");
704 if (len != sizeof(atuple->rand))
705 goto err_size;
706
707 blob = dbi_result_get_binary(result, "rand");
708 memcpy(atuple->rand, blob, len);
709
710 len = dbi_result_get_field_length(result, "sres");
711 if (len != sizeof(atuple->sres))
712 goto err_size;
713
714 blob = dbi_result_get_binary(result, "sres");
715 memcpy(atuple->sres, blob, len);
716
717 len = dbi_result_get_field_length(result, "kc");
718 if (len != sizeof(atuple->kc))
719 goto err_size;
720
721 blob = dbi_result_get_binary(result, "kc");
722 memcpy(atuple->kc, blob, len);
723
724 dbi_result_free(result);
725
726 return 0;
727
728err_size:
729 dbi_result_free(result);
730 return -EIO;
731}
732
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200733int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
734 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100735{
736 dbi_result result;
737 int rc, upd;
738 struct gsm_auth_tuple atuple_old;
739 unsigned char *rand_str, *sres_str, *kc_str;
740
741 /* Deletion ? */
742 if (atuple == NULL) {
743 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200744 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100745 subscr->id);
746
747 if (!result)
748 return -EIO;
749
750 dbi_result_free(result);
751
752 return 0;
753 }
754
755 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200756 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100757 if (rc && rc != -ENOENT)
758 return rc;
759 upd = rc ? 0 : 1;
760
761 /* Update / Insert */
762 dbi_conn_quote_binary_copy(conn,
763 atuple->rand, sizeof(atuple->rand), &rand_str);
764 dbi_conn_quote_binary_copy(conn,
765 atuple->sres, sizeof(atuple->sres), &sres_str);
766 dbi_conn_quote_binary_copy(conn,
767 atuple->kc, sizeof(atuple->kc), &kc_str);
768
769 if (!upd) {
770 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200771 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100772 "(subscriber_id, issued, use_count, "
773 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200774 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100775 "%u, %s, %s, %s ) ",
776 subscr->id, atuple->use_count, atuple->key_seq,
777 rand_str, sres_str, kc_str);
778 } else {
779 char *issued = atuple->key_seq == atuple_old.key_seq ?
780 "issued" : "datetime('now')";
781 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200782 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100783 "SET issued=%s, use_count=%u, "
784 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200785 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100786 issued, atuple->use_count, atuple->key_seq,
787 rand_str, sres_str, kc_str, subscr->id);
788 }
789
790 free(rand_str);
791 free(sres_str);
792 free(kc_str);
793
794 if (!result)
795 return -EIO;
796
797 dbi_result_free(result);
798
799 return 0;
800}
801
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100802static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
803{
804 const char *string;
805 string = dbi_result_get_string(result, "imsi");
806 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200807 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH-1);
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100808
809 string = dbi_result_get_string(result, "tmsi");
810 if (string)
811 subscr->tmsi = tmsi_from_string(string);
812
813 string = dbi_result_get_string(result, "name");
814 if (string)
815 strncpy(subscr->name, string, GSM_NAME_LENGTH);
816
817 string = dbi_result_get_string(result, "extension");
818 if (string)
819 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
820
Kevin Redonc9763a32013-11-04 22:43:15 +0100821 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100822
823 if (!dbi_result_field_is_null(result, "expire_lu"))
824 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
825 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200826 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100827
Kevin Redonc9763a32013-11-04 22:43:15 +0100828 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
829
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100830}
831
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200832#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200833struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200834 const char *id)
835{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000836 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000837 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000838 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000839
Jan Luebbe5c15c852008-12-27 15:59:25 +0000840 switch (field) {
841 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000842 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000843 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200844 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000845 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000846 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000847 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000848 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000849 break;
850 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000851 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000852 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200853 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000854 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000855 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000856 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000857 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000858 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000859 case GSM_SUBSCRIBER_EXTENSION:
860 dbi_conn_quote_string_copy(conn, id, &quoted);
861 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200862 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000863 "WHERE extension = %s ",
864 quoted
865 );
866 free(quoted);
867 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200868 case GSM_SUBSCRIBER_ID:
869 dbi_conn_quote_string_copy(conn, id, &quoted);
870 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200871 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200872 "WHERE id = %s ", quoted);
873 free(quoted);
874 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000875 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100876 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000877 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000878 }
Harald Welte0b906d02009-12-24 11:21:42 +0100879 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100880 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000881 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000882 }
883 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100884 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000885 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000886 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000887 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000888 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000889
890 subscr = subscr_alloc();
891 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000892
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100893 db_set_from_query(subscr, result);
Harald Welteae1f1592009-12-24 11:39:14 +0100894 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 +0000895 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000896 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000897 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200898
899 get_equipment_by_subscr(subscr);
900
Holger Freyther12aa50d2009-01-01 18:02:05 +0000901 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000902}
903
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100904int db_subscriber_update(struct gsm_subscriber *subscr)
905{
906 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100907 dbi_result result;
908
909 /* Copy the id to a string as queryf with %llu is failing */
910 sprintf(buf, "%llu", subscr->id);
911 result = dbi_conn_queryf(conn,
912 BASE_QUERY
913 "WHERE id = %s", buf);
914
915 if (!result) {
916 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
917 return -EIO;
918 }
919 if (!dbi_result_next_row(result)) {
920 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
921 subscr->id);
922 dbi_result_free(result);
923 return -EIO;
924 }
925
926 db_set_from_query(subscr, result);
927 dbi_result_free(result);
928 get_equipment_by_subscr(subscr);
929
930 return 0;
931}
932
Harald Welte0b906d02009-12-24 11:21:42 +0100933int db_sync_subscriber(struct gsm_subscriber *subscriber)
934{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000935 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200936 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100937 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200938
Harald Welte019d0162010-12-26 19:12:30 +0100939 dbi_conn_quote_string_copy(conn,
940 subscriber->name, &q_name);
941 dbi_conn_quote_string_copy(conn,
942 subscriber->extension, &q_extension);
943
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200944 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
945 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200946 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200947 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200948 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200949 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200950 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100951
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200952 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
953 result = dbi_conn_queryf(conn,
954 "UPDATE Subscriber "
955 "SET updated = datetime('now'), "
956 "name = %s, "
957 "extension = %s, "
958 "authorized = %i, "
959 "tmsi = %s, "
960 "lac = %i, "
961 "expire_lu = NULL "
962 "WHERE imsi = %s ",
963 q_name,
964 q_extension,
965 subscriber->authorized,
966 q_tmsi,
967 subscriber->lac,
968 subscriber->imsi);
969 } else {
970 result = dbi_conn_queryf(conn,
971 "UPDATE Subscriber "
972 "SET updated = datetime('now'), "
973 "name = %s, "
974 "extension = %s, "
975 "authorized = %i, "
976 "tmsi = %s, "
977 "lac = %i, "
978 "expire_lu = datetime(%i, 'unixepoch') "
979 "WHERE imsi = %s ",
980 q_name,
981 q_extension,
982 subscriber->authorized,
983 q_tmsi,
984 subscriber->lac,
985 (int) subscriber->expire_lu,
986 subscriber->imsi);
987 }
Harald Welte0b906d02009-12-24 11:21:42 +0100988
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200989 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100990 free(q_name);
991 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100992
993 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100994 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000995 return 1;
996 }
Harald Welte0b906d02009-12-24 11:21:42 +0100997
Jan Luebbe5c15c852008-12-27 15:59:25 +0000998 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100999
Jan Luebbe5c15c852008-12-27 15:59:25 +00001000 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001001}
1002
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001003int db_subscriber_delete(struct gsm_subscriber *subscr)
1004{
1005 dbi_result result;
1006
1007 result = dbi_conn_queryf(conn,
1008 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1009 subscr->id);
1010 if (!result) {
1011 LOGP(DDB, LOGL_ERROR,
1012 "Failed to delete Authkeys for %llu\n", subscr->id);
1013 return -1;
1014 }
1015 dbi_result_free(result);
1016
1017 result = dbi_conn_queryf(conn,
1018 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1019 subscr->id);
1020 if (!result) {
1021 LOGP(DDB, LOGL_ERROR,
1022 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1023 return -1;
1024 }
1025 dbi_result_free(result);
1026
1027 result = dbi_conn_queryf(conn,
1028 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1029 subscr->id);
1030 if (!result) {
1031 LOGP(DDB, LOGL_ERROR,
1032 "Failed to delete AuthToken for %llu\n", subscr->id);
1033 return -1;
1034 }
1035 dbi_result_free(result);
1036
1037 result = dbi_conn_queryf(conn,
1038 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1039 subscr->id);
1040 if (!result) {
1041 LOGP(DDB, LOGL_ERROR,
1042 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1043 return -1;
1044 }
1045 dbi_result_free(result);
1046
1047 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001048 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1049 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001050 if (!result) {
1051 LOGP(DDB, LOGL_ERROR,
1052 "Failed to delete SMS for %llu\n", subscr->id);
1053 return -1;
1054 }
1055 dbi_result_free(result);
1056
1057 result = dbi_conn_queryf(conn,
1058 "DELETE FROM VLR WHERE subscriber_id=%llu",
1059 subscr->id);
1060 if (!result) {
1061 LOGP(DDB, LOGL_ERROR,
1062 "Failed to delete VLR for %llu\n", subscr->id);
1063 return -1;
1064 }
1065 dbi_result_free(result);
1066
1067 result = dbi_conn_queryf(conn,
1068 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1069 subscr->id);
1070 if (!result) {
1071 LOGP(DDB, LOGL_ERROR,
1072 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1073 return -1;
1074 }
1075 dbi_result_free(result);
1076
1077 result = dbi_conn_queryf(conn,
1078 "DELETE FROM Subscriber WHERE id=%llu",
1079 subscr->id);
1080 if (!result) {
1081 LOGP(DDB, LOGL_ERROR,
1082 "Failed to delete Subscriber for %llu\n", subscr->id);
1083 return -1;
1084 }
1085 dbi_result_free(result);
1086
1087 return 0;
1088}
1089
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001090/**
1091 * List all the authorized and non-expired subscribers. The callback will
1092 * be called one by one. The subscr argument is not fully initialize and
1093 * subscr_get/subscr_put must not be called. The passed in pointer will be
1094 * deleted after the callback by the database call.
1095 */
1096int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1097{
1098 dbi_result result;
1099
Max5c06e402015-07-29 20:20:28 +02001100 result = dbi_conn_query(conn,
1101 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001102 if (!result) {
1103 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1104 return -1;
1105 }
1106
1107 while (dbi_result_next_row(result)) {
1108 struct gsm_subscriber *subscr;
1109
1110 subscr = subscr_alloc();
1111 subscr->id = dbi_result_get_ulonglong(result, "id");
1112 db_set_from_query(subscr, result);
1113 cb(subscr, closure);
1114 OSMO_ASSERT(subscr->use_count == 1);
1115 llist_del(&subscr->entry);
1116 talloc_free(subscr);
1117 }
1118
1119 dbi_result_free(result);
1120 return 0;
1121}
1122
Harald Weltec2e302d2009-07-05 14:08:13 +02001123int db_sync_equipment(struct gsm_equipment *equip)
1124{
1125 dbi_result result;
1126 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001127 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001128 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001129
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001130 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001131 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001132 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001133 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001134 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001135 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001136 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001137 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001138 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001139 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001140
Harald Weltec2e302d2009-07-05 14:08:13 +02001141 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1142 equip->classmark2_len, &cm2);
1143 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1144 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001145 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001146
1147 result = dbi_conn_queryf(conn,
1148 "UPDATE Equipment SET "
1149 "updated = datetime('now'), "
1150 "classmark1 = %u, "
1151 "classmark2 = %s, "
1152 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001153 "WHERE imei = %s ",
1154 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001155
1156 free(cm2);
1157 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001158 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001159
1160 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001161 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001162 return -EIO;
1163 }
1164
1165 dbi_result_free(result);
1166 return 0;
1167}
1168
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001169int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1170{
1171 dbi_result result;
1172
1173 result = dbi_conn_query(conn,
1174 "SELECT id "
1175 "FROM Subscriber "
1176 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001177 "( expire_lu is NOT NULL "
1178 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001179 "LIMIT 1");
1180 if (!result) {
1181 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1182 return -EIO;
1183 }
1184
1185 while (dbi_result_next_row(result))
1186 callback(priv, dbi_result_get_ulonglong(result, "id"));
1187
1188 dbi_result_free(result);
1189 return 0;
1190}
1191
Harald Welte0b906d02009-12-24 11:21:42 +01001192int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1193{
1194 dbi_result result = NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001195 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001196 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001197
Jan Luebbe5c15c852008-12-27 15:59:25 +00001198 for (;;) {
Daniel Willmanncdeb8152015-10-08 16:10:23 +02001199 if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
1200 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1201 return 1;
1202 }
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001203 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1204 continue;
1205
1206 sprintf(tmsi, "%u", subscriber->tmsi);
1207 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001208 result = dbi_conn_queryf(conn,
1209 "SELECT * FROM Subscriber "
1210 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001211 tmsi_quoted);
1212
Holger Freyther12aa50d2009-01-01 18:02:05 +00001213 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001214
1215 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001216 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1217 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001218 return 1;
1219 }
Harald Welte0b906d02009-12-24 11:21:42 +01001220 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001221 dbi_result_free(result);
1222 continue;
1223 }
1224 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001225 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001226 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1227 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001228 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001229 }
1230 dbi_result_free(result);
1231 }
1232 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001233}
1234
Harald Welte0b906d02009-12-24 11:21:42 +01001235int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1236{
1237 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001238 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001239
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001240 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001241 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001242 result = dbi_conn_queryf(conn,
1243 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001244 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001245 try
1246 );
Harald Welte0b906d02009-12-24 11:21:42 +01001247 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001248 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1249 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001250 return 1;
1251 }
1252 if (dbi_result_get_numrows(result)){
1253 dbi_result_free(result);
1254 continue;
1255 }
1256 if (!dbi_result_next_row(result)) {
1257 dbi_result_free(result);
1258 break;
1259 }
1260 dbi_result_free(result);
1261 }
1262 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001263 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001264 return db_sync_subscriber(subscriber);
1265}
Jan Luebbe31bef492009-08-12 14:31:14 +02001266/*
1267 * try to allocate a new unique token for this subscriber and return it
1268 * via a parameter. if the subscriber already has a token, return
1269 * an error.
1270 */
1271
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001272int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001273{
1274 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001275 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001276
Jan Luebbe31bef492009-08-12 14:31:14 +02001277 for (;;) {
Daniel Willmann2aedfbd2015-10-08 16:10:26 +02001278 if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
1279 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1280 return 1;
1281 }
Jan Luebbe31bef492009-08-12 14:31:14 +02001282 if (!try) /* 0 is an invalid token */
1283 continue;
1284 result = dbi_conn_queryf(conn,
1285 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001286 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1287 subscriber->id, try);
1288 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001289 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1290 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001291 return 1;
1292 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001293 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001294 dbi_result_free(result);
1295 continue;
1296 }
1297 if (!dbi_result_next_row(result)) {
1298 dbi_result_free(result);
1299 break;
1300 }
1301 dbi_result_free(result);
1302 }
1303 result = dbi_conn_queryf(conn,
1304 "INSERT INTO AuthToken "
1305 "(subscriber_id, created, token) "
1306 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001307 "(%llu, datetime('now'), \"%08X\") ",
1308 subscriber->id, try);
1309 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001310 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1311 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001312 return 1;
1313 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001314 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001315 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001316 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001317
Jan Luebbe31bef492009-08-12 14:31:14 +02001318 return 0;
1319}
1320
Harald Welte0b906d02009-12-24 11:21:42 +01001321int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1322{
Harald Welted409be72009-11-07 00:06:19 +09001323 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001324 dbi_result result;
1325
Harald Weltec2e302d2009-07-05 14:08:13 +02001326 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001327 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001328
Jan Luebbefac25fc2008-12-27 18:04:34 +00001329 result = dbi_conn_queryf(conn,
1330 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001331 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001332 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001333 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001334 imei);
1335 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001336 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001337 return 1;
1338 }
Harald Welte0b906d02009-12-24 11:21:42 +01001339
Jan Luebbe391d86e2008-12-27 22:33:34 +00001340 equipment_id = 0;
1341 if (dbi_result_get_numrows_affected(result)) {
1342 equipment_id = dbi_conn_sequence_last(conn, NULL);
1343 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001344 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001345
1346 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001347 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001348 else {
1349 result = dbi_conn_queryf(conn,
1350 "SELECT id FROM Equipment "
1351 "WHERE imei = %s ",
1352 imei
1353 );
Harald Welte0b906d02009-12-24 11:21:42 +01001354 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001355 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001356 return 1;
1357 }
1358 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001359 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001360 dbi_result_free(result);
1361 return 1;
1362 }
1363 equipment_id = dbi_result_get_ulonglong(result, "id");
1364 dbi_result_free(result);
1365 }
1366
1367 result = dbi_conn_queryf(conn,
1368 "INSERT OR IGNORE INTO EquipmentWatch "
1369 "(subscriber_id, equipment_id, created, updated) "
1370 "VALUES "
1371 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001372 subscriber->id, equipment_id);
1373 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001374 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001375 return 1;
1376 }
Harald Welte0b906d02009-12-24 11:21:42 +01001377
Jan Luebbe391d86e2008-12-27 22:33:34 +00001378 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001379 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001380 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001381
Jan Luebbefac25fc2008-12-27 18:04:34 +00001382 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001383 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001384 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1385 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001386 else {
1387 result = dbi_conn_queryf(conn,
1388 "UPDATE EquipmentWatch "
1389 "SET updated = datetime('now') "
1390 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001391 subscriber->id, equipment_id);
1392 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001393 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001394 return 1;
1395 }
1396 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001397 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1398 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001399 }
1400
1401 return 0;
1402}
1403
Harald Welte7e310b12009-03-30 20:56:32 +00001404/* store an [unsent] SMS to the database */
1405int db_sms_store(struct gsm_sms *sms)
1406{
1407 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001408 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001409 unsigned char *q_udata;
1410 char *validity_timestamp = "2222-2-2";
1411
1412 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001413
1414 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001415 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001416 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001417 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1418 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001419
Harald Weltef3efc592009-07-27 20:11:35 +02001420 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001421 result = dbi_conn_queryf(conn,
1422 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001423 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001424 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001425 "data_coding_scheme, ud_hdr_ind, "
1426 "user_data, text, "
1427 "dest_addr, dest_ton, dest_npi, "
1428 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001429 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001430 "%u, %u, %u, "
1431 "%u, %u, "
1432 "%s, %s, "
1433 "%s, %u, %u, "
1434 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001435 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001436 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001437 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001438 q_udata, q_text,
1439 q_daddr, sms->dst.ton, sms->dst.npi,
1440 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001441 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001442 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001443 free(q_daddr);
1444 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001445
1446 if (!result)
1447 return -EIO;
1448
1449 dbi_result_free(result);
1450 return 0;
1451}
1452
Harald Welte2ebabca2009-08-09 19:05:21 +02001453static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001454{
Harald Welte76042182009-08-08 16:03:15 +02001455 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001456 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001457 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001458
Harald Welte76042182009-08-08 16:03:15 +02001459 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001460 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001461
Harald Weltebe3e3782009-07-05 14:06:41 +02001462 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001463
Harald Weltef3efc592009-07-27 20:11:35 +02001464 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001465 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001466 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1467 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1468 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1469 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1470 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001471 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001472 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001473
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001474 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1475 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001476 daddr = dbi_result_get_string(result, "dest_addr");
1477 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001478 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1479 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001480 }
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001481 sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001482
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001483 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1484 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001485 saddr = dbi_result_get_string(result, "src_addr");
1486 if (saddr) {
1487 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1488 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1489 }
1490
Harald Welte76042182009-08-08 16:03:15 +02001491 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1492 user_data = dbi_result_get_binary(result, "user_data");
1493 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001494 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001495 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001496
1497 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001498 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001499 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001500 sms->text[sizeof(sms->text)-1] = '\0';
1501 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001502 return sms;
1503}
1504
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001505struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1506{
1507 dbi_result result;
1508 struct gsm_sms *sms;
1509
1510 result = dbi_conn_queryf(conn,
1511 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1512 if (!result)
1513 return NULL;
1514
1515 if (!dbi_result_next_row(result)) {
1516 dbi_result_free(result);
1517 return NULL;
1518 }
1519
1520 sms = sms_from_result(net, result);
1521
1522 dbi_result_free(result);
1523
1524 return sms;
1525}
1526
Harald Welte2ebabca2009-08-09 19:05:21 +02001527/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001528struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001529{
1530 dbi_result result;
1531 struct gsm_sms *sms;
1532
1533 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001534 "SELECT SMS.* "
1535 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001536 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001537 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1538 "AND Subscriber.lac > 0 "
1539 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001540 min_id);
1541 if (!result)
1542 return NULL;
1543
1544 if (!dbi_result_next_row(result)) {
1545 dbi_result_free(result);
1546 return NULL;
1547 }
1548
1549 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001550
1551 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001552
1553 return sms;
1554}
1555
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001556struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1557 unsigned long long min_subscr_id,
1558 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001559{
1560 dbi_result result;
1561 struct gsm_sms *sms;
1562
1563 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001564 "SELECT SMS.* "
1565 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001566 "SMS.dest_addr = Subscriber.extension "
1567 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001568 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001569 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001570 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001571 if (!result)
1572 return NULL;
1573
1574 if (!dbi_result_next_row(result)) {
1575 dbi_result_free(result);
1576 return NULL;
1577 }
1578
1579 sms = sms_from_result(net, result);
1580
1581 dbi_result_free(result);
1582
1583 return sms;
1584}
1585
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001586/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001587struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1588{
1589 dbi_result result;
1590 struct gsm_sms *sms;
1591
1592 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001593 "SELECT SMS.* "
1594 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001595 "SMS.dest_addr = Subscriber.extension "
1596 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001597 "AND Subscriber.lac > 0 "
1598 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001599 subscr->id);
1600 if (!result)
1601 return NULL;
1602
1603 if (!dbi_result_next_row(result)) {
1604 dbi_result_free(result);
1605 return NULL;
1606 }
1607
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001608 sms = sms_from_result(subscr->group->net, result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001609
1610 dbi_result_free(result);
1611
Harald Welte7e310b12009-03-30 20:56:32 +00001612 return sms;
1613}
1614
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001615/* mark a given SMS as delivered */
1616int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001617{
1618 dbi_result result;
1619
1620 result = dbi_conn_queryf(conn,
1621 "UPDATE SMS "
1622 "SET sent = datetime('now') "
1623 "WHERE id = %llu", sms->id);
1624 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001625 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001626 return 1;
1627 }
1628
1629 dbi_result_free(result);
1630 return 0;
1631}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001632
1633/* increase the number of attempted deliveries */
1634int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1635{
1636 dbi_result result;
1637
1638 result = dbi_conn_queryf(conn,
1639 "UPDATE SMS "
1640 "SET deliver_attempts = deliver_attempts + 1 "
1641 "WHERE id = %llu", sms->id);
1642 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001643 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1644 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001645 return 1;
1646 }
1647
1648 dbi_result_free(result);
1649 return 0;
1650}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001651
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001652int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001653 uint8_t apdu_id_flags, uint8_t len,
1654 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001655{
1656 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001657 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001658
1659 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1660
1661 result = dbi_conn_queryf(conn,
1662 "INSERT INTO ApduBlobs "
1663 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1664 "(datetime('now'),%llu,%u,%s)",
1665 subscr->id, apdu_id_flags, q_apdu);
1666
1667 free(q_apdu);
1668
1669 if (!result)
1670 return -EIO;
1671
1672 dbi_result_free(result);
1673 return 0;
1674}
Harald Welteffa55a42009-12-22 19:07:32 +01001675
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001676int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001677{
1678 dbi_result result;
1679 char *q_name;
1680
1681 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1682
1683 result = dbi_conn_queryf(conn,
1684 "INSERT INTO Counters "
1685 "(timestamp,name,value) VALUES "
1686 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1687
1688 free(q_name);
1689
1690 if (!result)
1691 return -EIO;
1692
1693 dbi_result_free(result);
1694 return 0;
1695}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001696
1697static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1698 char *q_prefix)
1699{
1700 dbi_result result;
1701 char *q_name;
1702
1703 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1704 &q_name);
1705
1706 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001707 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001708 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001709 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1710 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001711
1712 free(q_name);
1713
1714 if (!result)
1715 return -EIO;
1716
1717 dbi_result_free(result);
1718 return 0;
1719}
1720
1721int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1722{
1723 unsigned int i;
1724 char *q_prefix;
1725
Harald Weltec1919862010-05-13 12:55:20 +02001726 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001727
1728 for (i = 0; i < ctrg->desc->num_ctr; i++)
1729 db_store_rate_ctr(ctrg, i, q_prefix);
1730
1731 free(q_prefix);
1732
1733 return 0;
1734}