blob: 17bea2470e592e9ad5eabd0fbd8e75d0a7363226 [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) {
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +0200511 subscr_put(subscr);
512 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000513 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000514
515 subscr = subscr_alloc();
516 if (!subscr)
517 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200518 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000519 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000520 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000521 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000522 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000523 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000524 imsi
525 );
Harald Welte0b906d02009-12-24 11:21:42 +0100526 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100527 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000528 subscr->id = dbi_conn_sequence_last(conn, NULL);
529 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000530 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100531 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200532 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000533 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000534}
535
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200536osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200537
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200538static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
539{
540 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200541 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100542 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200543 const unsigned char *cm2, *cm3;
544 struct gsm_equipment *equip = &subscr->equipment;
545
546 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200547 "SELECT Equipment.* "
548 "FROM Equipment JOIN EquipmentWatch ON "
549 "EquipmentWatch.equipment_id=Equipment.id "
550 "WHERE EquipmentWatch.subscriber_id = %llu "
551 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200552 if (!result)
553 return -EIO;
554
555 if (!dbi_result_next_row(result)) {
556 dbi_result_free(result);
557 return -ENOENT;
558 }
559
560 equip->id = dbi_result_get_ulonglong(result, "id");
561
562 string = dbi_result_get_string(result, "imei");
563 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200564 strncpy(equip->imei, string, sizeof(equip->imei)-1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200565
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100566 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200567 if (string) {
568 cm1 = atoi(string) & 0xff;
569 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
570 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200571
572 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
573 cm2 = dbi_result_get_binary(result, "classmark2");
574 if (equip->classmark2_len > sizeof(equip->classmark2))
575 equip->classmark2_len = sizeof(equip->classmark2);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100576 if (cm2)
577 memcpy(equip->classmark2, cm2, equip->classmark2_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200578
579 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
580 cm3 = dbi_result_get_binary(result, "classmark3");
581 if (equip->classmark3_len > sizeof(equip->classmark3))
582 equip->classmark3_len = sizeof(equip->classmark3);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100583 if (cm3)
584 memcpy(equip->classmark3, cm3, equip->classmark3_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200585
586 dbi_result_free(result);
587
588 return 0;
589}
Harald Welte3606cc52009-12-05 15:13:22 +0530590
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200591int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
592 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530593{
594 dbi_result result;
595 const unsigned char *a3a8_ki;
596
597 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200598 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530599 subscr->id);
600 if (!result)
601 return -EIO;
602
603 if (!dbi_result_next_row(result)) {
604 dbi_result_free(result);
605 return -ENOENT;
606 }
607
608 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
609 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
610 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100611 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200612 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530613 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
614
615 dbi_result_free(result);
616
617 return 0;
618}
619
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200620int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
621 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100622{
623 dbi_result result;
624 struct gsm_auth_info ainfo_old;
625 int rc, upd;
626 unsigned char *ki_str;
627
628 /* Deletion ? */
629 if (ainfo == NULL) {
630 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200631 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100632 subscr->id);
633
634 if (!result)
635 return -EIO;
636
637 dbi_result_free(result);
638
639 return 0;
640 }
641
642 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200643 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100644 if (rc && rc != -ENOENT)
645 return rc;
646 upd = rc ? 0 : 1;
647
648 /* Update / Insert */
649 dbi_conn_quote_binary_copy(conn,
650 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
651
652 if (!upd) {
653 result = dbi_conn_queryf(conn,
654 "INSERT INTO AuthKeys "
655 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200656 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100657 subscr->id, ainfo->auth_algo, ki_str);
658 } else {
659 result = dbi_conn_queryf(conn,
660 "UPDATE AuthKeys "
661 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200662 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100663 ainfo->auth_algo, ki_str, subscr->id);
664 }
665
666 free(ki_str);
667
668 if (!result)
669 return -EIO;
670
671 dbi_result_free(result);
672
673 return 0;
674}
675
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200676int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
677 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530678{
679 dbi_result result;
680 int len;
681 const unsigned char *blob;
682
683 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200684 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530685 subscr->id);
686 if (!result)
687 return -EIO;
688
689 if (!dbi_result_next_row(result)) {
690 dbi_result_free(result);
691 return -ENOENT;
692 }
693
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200694 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530695
Sylvain Munaut70881b72009-12-27 15:41:59 +0100696 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
697 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
698
Harald Welte3606cc52009-12-05 15:13:22 +0530699 len = dbi_result_get_field_length(result, "rand");
700 if (len != sizeof(atuple->rand))
701 goto err_size;
702
703 blob = dbi_result_get_binary(result, "rand");
704 memcpy(atuple->rand, blob, len);
705
706 len = dbi_result_get_field_length(result, "sres");
707 if (len != sizeof(atuple->sres))
708 goto err_size;
709
710 blob = dbi_result_get_binary(result, "sres");
711 memcpy(atuple->sres, blob, len);
712
713 len = dbi_result_get_field_length(result, "kc");
714 if (len != sizeof(atuple->kc))
715 goto err_size;
716
717 blob = dbi_result_get_binary(result, "kc");
718 memcpy(atuple->kc, blob, len);
719
720 dbi_result_free(result);
721
722 return 0;
723
724err_size:
725 dbi_result_free(result);
726 return -EIO;
727}
728
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200729int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
730 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100731{
732 dbi_result result;
733 int rc, upd;
734 struct gsm_auth_tuple atuple_old;
735 unsigned char *rand_str, *sres_str, *kc_str;
736
737 /* Deletion ? */
738 if (atuple == NULL) {
739 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200740 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100741 subscr->id);
742
743 if (!result)
744 return -EIO;
745
746 dbi_result_free(result);
747
748 return 0;
749 }
750
751 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200752 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100753 if (rc && rc != -ENOENT)
754 return rc;
755 upd = rc ? 0 : 1;
756
757 /* Update / Insert */
758 dbi_conn_quote_binary_copy(conn,
759 atuple->rand, sizeof(atuple->rand), &rand_str);
760 dbi_conn_quote_binary_copy(conn,
761 atuple->sres, sizeof(atuple->sres), &sres_str);
762 dbi_conn_quote_binary_copy(conn,
763 atuple->kc, sizeof(atuple->kc), &kc_str);
764
765 if (!upd) {
766 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200767 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100768 "(subscriber_id, issued, use_count, "
769 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200770 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100771 "%u, %s, %s, %s ) ",
772 subscr->id, atuple->use_count, atuple->key_seq,
773 rand_str, sres_str, kc_str);
774 } else {
775 char *issued = atuple->key_seq == atuple_old.key_seq ?
776 "issued" : "datetime('now')";
777 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200778 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100779 "SET issued=%s, use_count=%u, "
780 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200781 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100782 issued, atuple->use_count, atuple->key_seq,
783 rand_str, sres_str, kc_str, subscr->id);
784 }
785
786 free(rand_str);
787 free(sres_str);
788 free(kc_str);
789
790 if (!result)
791 return -EIO;
792
793 dbi_result_free(result);
794
795 return 0;
796}
797
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100798static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
799{
800 const char *string;
801 string = dbi_result_get_string(result, "imsi");
802 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200803 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH-1);
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100804
805 string = dbi_result_get_string(result, "tmsi");
806 if (string)
807 subscr->tmsi = tmsi_from_string(string);
808
809 string = dbi_result_get_string(result, "name");
810 if (string)
811 strncpy(subscr->name, string, GSM_NAME_LENGTH);
812
813 string = dbi_result_get_string(result, "extension");
814 if (string)
815 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
816
Kevin Redonc9763a32013-11-04 22:43:15 +0100817 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100818
819 if (!dbi_result_field_is_null(result, "expire_lu"))
820 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
821 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200822 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100823
Kevin Redonc9763a32013-11-04 22:43:15 +0100824 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
825
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100826}
827
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200828#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200829struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200830 const char *id)
831{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000832 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000833 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000834 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000835
Jan Luebbe5c15c852008-12-27 15:59:25 +0000836 switch (field) {
837 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000838 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000839 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200840 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000841 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000842 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000843 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000844 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000845 break;
846 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000847 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000848 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200849 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000850 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000851 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000852 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000853 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000854 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000855 case GSM_SUBSCRIBER_EXTENSION:
856 dbi_conn_quote_string_copy(conn, id, &quoted);
857 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200858 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000859 "WHERE extension = %s ",
860 quoted
861 );
862 free(quoted);
863 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200864 case GSM_SUBSCRIBER_ID:
865 dbi_conn_quote_string_copy(conn, id, &quoted);
866 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200867 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200868 "WHERE id = %s ", quoted);
869 free(quoted);
870 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000871 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100872 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000873 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000874 }
Harald Welte0b906d02009-12-24 11:21:42 +0100875 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100876 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000877 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000878 }
879 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100880 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000881 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000882 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000883 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000884 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000885
886 subscr = subscr_alloc();
887 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000888
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100889 db_set_from_query(subscr, result);
Harald Welte3ad03462016-03-17 14:41:26 +0100890 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
891 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
892 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000893 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200894
895 get_equipment_by_subscr(subscr);
896
Holger Freyther12aa50d2009-01-01 18:02:05 +0000897 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000898}
899
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100900int db_subscriber_update(struct gsm_subscriber *subscr)
901{
902 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100903 dbi_result result;
904
905 /* Copy the id to a string as queryf with %llu is failing */
906 sprintf(buf, "%llu", subscr->id);
907 result = dbi_conn_queryf(conn,
908 BASE_QUERY
909 "WHERE id = %s", buf);
910
911 if (!result) {
912 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
913 return -EIO;
914 }
915 if (!dbi_result_next_row(result)) {
916 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
917 subscr->id);
918 dbi_result_free(result);
919 return -EIO;
920 }
921
922 db_set_from_query(subscr, result);
923 dbi_result_free(result);
924 get_equipment_by_subscr(subscr);
925
926 return 0;
927}
928
Harald Welte0b906d02009-12-24 11:21:42 +0100929int db_sync_subscriber(struct gsm_subscriber *subscriber)
930{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000931 dbi_result result;
Harald Welte3ad03462016-03-17 14:41:26 +0100932 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100933 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200934
Harald Welte019d0162010-12-26 19:12:30 +0100935 dbi_conn_quote_string_copy(conn,
936 subscriber->name, &q_name);
937 dbi_conn_quote_string_copy(conn,
938 subscriber->extension, &q_extension);
939
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200940 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
Harald Welte3ad03462016-03-17 14:41:26 +0100941 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200942 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200943 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200944 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200945 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200946 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100947
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200948 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
949 result = dbi_conn_queryf(conn,
950 "UPDATE Subscriber "
951 "SET updated = datetime('now'), "
952 "name = %s, "
953 "extension = %s, "
954 "authorized = %i, "
955 "tmsi = %s, "
956 "lac = %i, "
957 "expire_lu = NULL "
958 "WHERE imsi = %s ",
959 q_name,
960 q_extension,
961 subscriber->authorized,
962 q_tmsi,
963 subscriber->lac,
964 subscriber->imsi);
965 } else {
966 result = dbi_conn_queryf(conn,
967 "UPDATE Subscriber "
968 "SET updated = datetime('now'), "
969 "name = %s, "
970 "extension = %s, "
971 "authorized = %i, "
972 "tmsi = %s, "
973 "lac = %i, "
974 "expire_lu = datetime(%i, 'unixepoch') "
975 "WHERE imsi = %s ",
976 q_name,
977 q_extension,
978 subscriber->authorized,
979 q_tmsi,
980 subscriber->lac,
981 (int) subscriber->expire_lu,
982 subscriber->imsi);
983 }
Harald Welte0b906d02009-12-24 11:21:42 +0100984
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200985 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100986 free(q_name);
987 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100988
989 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100990 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000991 return 1;
992 }
Harald Welte0b906d02009-12-24 11:21:42 +0100993
Jan Luebbe5c15c852008-12-27 15:59:25 +0000994 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100995
Jan Luebbe5c15c852008-12-27 15:59:25 +0000996 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000997}
998
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +0100999int db_subscriber_delete(struct gsm_subscriber *subscr)
1000{
1001 dbi_result result;
1002
1003 result = dbi_conn_queryf(conn,
1004 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1005 subscr->id);
1006 if (!result) {
1007 LOGP(DDB, LOGL_ERROR,
1008 "Failed to delete Authkeys for %llu\n", subscr->id);
1009 return -1;
1010 }
1011 dbi_result_free(result);
1012
1013 result = dbi_conn_queryf(conn,
1014 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1015 subscr->id);
1016 if (!result) {
1017 LOGP(DDB, LOGL_ERROR,
1018 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1019 return -1;
1020 }
1021 dbi_result_free(result);
1022
1023 result = dbi_conn_queryf(conn,
1024 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1025 subscr->id);
1026 if (!result) {
1027 LOGP(DDB, LOGL_ERROR,
1028 "Failed to delete AuthToken for %llu\n", subscr->id);
1029 return -1;
1030 }
1031 dbi_result_free(result);
1032
1033 result = dbi_conn_queryf(conn,
1034 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1035 subscr->id);
1036 if (!result) {
1037 LOGP(DDB, LOGL_ERROR,
1038 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1039 return -1;
1040 }
1041 dbi_result_free(result);
1042
1043 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001044 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1045 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001046 if (!result) {
1047 LOGP(DDB, LOGL_ERROR,
1048 "Failed to delete SMS for %llu\n", subscr->id);
1049 return -1;
1050 }
1051 dbi_result_free(result);
1052
1053 result = dbi_conn_queryf(conn,
1054 "DELETE FROM VLR WHERE subscriber_id=%llu",
1055 subscr->id);
1056 if (!result) {
1057 LOGP(DDB, LOGL_ERROR,
1058 "Failed to delete VLR for %llu\n", subscr->id);
1059 return -1;
1060 }
1061 dbi_result_free(result);
1062
1063 result = dbi_conn_queryf(conn,
1064 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1065 subscr->id);
1066 if (!result) {
1067 LOGP(DDB, LOGL_ERROR,
1068 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1069 return -1;
1070 }
1071 dbi_result_free(result);
1072
1073 result = dbi_conn_queryf(conn,
1074 "DELETE FROM Subscriber WHERE id=%llu",
1075 subscr->id);
1076 if (!result) {
1077 LOGP(DDB, LOGL_ERROR,
1078 "Failed to delete Subscriber for %llu\n", subscr->id);
1079 return -1;
1080 }
1081 dbi_result_free(result);
1082
1083 return 0;
1084}
1085
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001086/**
1087 * List all the authorized and non-expired subscribers. The callback will
1088 * be called one by one. The subscr argument is not fully initialize and
1089 * subscr_get/subscr_put must not be called. The passed in pointer will be
1090 * deleted after the callback by the database call.
1091 */
1092int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1093{
1094 dbi_result result;
1095
Max5c06e402015-07-29 20:20:28 +02001096 result = dbi_conn_query(conn,
1097 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001098 if (!result) {
1099 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1100 return -1;
1101 }
1102
1103 while (dbi_result_next_row(result)) {
1104 struct gsm_subscriber *subscr;
1105
1106 subscr = subscr_alloc();
1107 subscr->id = dbi_result_get_ulonglong(result, "id");
1108 db_set_from_query(subscr, result);
1109 cb(subscr, closure);
1110 OSMO_ASSERT(subscr->use_count == 1);
1111 llist_del(&subscr->entry);
1112 talloc_free(subscr);
1113 }
1114
1115 dbi_result_free(result);
1116 return 0;
1117}
1118
Harald Weltec2e302d2009-07-05 14:08:13 +02001119int db_sync_equipment(struct gsm_equipment *equip)
1120{
1121 dbi_result result;
1122 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001123 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001124 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001125
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001126 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001127 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001128 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001129 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001130 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001131 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001132 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001133 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001134 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001135 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001136
Harald Weltec2e302d2009-07-05 14:08:13 +02001137 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1138 equip->classmark2_len, &cm2);
1139 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1140 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001141 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001142
1143 result = dbi_conn_queryf(conn,
1144 "UPDATE Equipment SET "
1145 "updated = datetime('now'), "
1146 "classmark1 = %u, "
1147 "classmark2 = %s, "
1148 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001149 "WHERE imei = %s ",
1150 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001151
1152 free(cm2);
1153 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001154 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001155
1156 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001157 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001158 return -EIO;
1159 }
1160
1161 dbi_result_free(result);
1162 return 0;
1163}
1164
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001165int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1166{
1167 dbi_result result;
1168
1169 result = dbi_conn_query(conn,
1170 "SELECT id "
1171 "FROM Subscriber "
1172 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001173 "( expire_lu is NOT NULL "
1174 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001175 "LIMIT 1");
1176 if (!result) {
1177 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1178 return -EIO;
1179 }
1180
1181 while (dbi_result_next_row(result))
1182 callback(priv, dbi_result_get_ulonglong(result, "id"));
1183
1184 dbi_result_free(result);
1185 return 0;
1186}
1187
Harald Welte0b906d02009-12-24 11:21:42 +01001188int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1189{
1190 dbi_result result = NULL;
Harald Welte3ad03462016-03-17 14:41:26 +01001191 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001192 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001193
Jan Luebbe5c15c852008-12-27 15:59:25 +00001194 for (;;) {
Daniel Willmanncdeb8152015-10-08 16:10:23 +02001195 if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
1196 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1197 return 1;
1198 }
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001199 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1200 continue;
1201
Harald Welte3ad03462016-03-17 14:41:26 +01001202 sprintf(tmsi, "%u", subscriber->tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001203 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001204 result = dbi_conn_queryf(conn,
1205 "SELECT * FROM Subscriber "
1206 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001207 tmsi_quoted);
1208
Holger Freyther12aa50d2009-01-01 18:02:05 +00001209 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001210
1211 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001212 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1213 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001214 return 1;
1215 }
Harald Welte0b906d02009-12-24 11:21:42 +01001216 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001217 dbi_result_free(result);
1218 continue;
1219 }
1220 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001221 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001222 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1223 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001224 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001225 }
1226 dbi_result_free(result);
1227 }
1228 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001229}
1230
Harald Welte0b906d02009-12-24 11:21:42 +01001231int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1232{
1233 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001234 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001235
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001236 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001237 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001238 result = dbi_conn_queryf(conn,
1239 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001240 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001241 try
1242 );
Harald Welte0b906d02009-12-24 11:21:42 +01001243 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001244 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1245 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001246 return 1;
1247 }
1248 if (dbi_result_get_numrows(result)){
1249 dbi_result_free(result);
1250 continue;
1251 }
1252 if (!dbi_result_next_row(result)) {
1253 dbi_result_free(result);
1254 break;
1255 }
1256 dbi_result_free(result);
1257 }
1258 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001259 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001260 return db_sync_subscriber(subscriber);
1261}
Jan Luebbe31bef492009-08-12 14:31:14 +02001262/*
1263 * try to allocate a new unique token for this subscriber and return it
1264 * via a parameter. if the subscriber already has a token, return
1265 * an error.
1266 */
1267
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001268int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001269{
1270 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001271 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001272
Jan Luebbe31bef492009-08-12 14:31:14 +02001273 for (;;) {
Daniel Willmann2aedfbd2015-10-08 16:10:26 +02001274 if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
1275 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1276 return 1;
1277 }
Jan Luebbe31bef492009-08-12 14:31:14 +02001278 if (!try) /* 0 is an invalid token */
1279 continue;
1280 result = dbi_conn_queryf(conn,
1281 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001282 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1283 subscriber->id, try);
1284 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001285 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1286 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001287 return 1;
1288 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001289 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001290 dbi_result_free(result);
1291 continue;
1292 }
1293 if (!dbi_result_next_row(result)) {
1294 dbi_result_free(result);
1295 break;
1296 }
1297 dbi_result_free(result);
1298 }
1299 result = dbi_conn_queryf(conn,
1300 "INSERT INTO AuthToken "
1301 "(subscriber_id, created, token) "
1302 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001303 "(%llu, datetime('now'), \"%08X\") ",
1304 subscriber->id, try);
1305 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001306 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1307 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001308 return 1;
1309 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001310 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001311 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001312 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001313
Jan Luebbe31bef492009-08-12 14:31:14 +02001314 return 0;
1315}
1316
Harald Welte0b906d02009-12-24 11:21:42 +01001317int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1318{
Harald Welted409be72009-11-07 00:06:19 +09001319 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001320 dbi_result result;
1321
Harald Weltec2e302d2009-07-05 14:08:13 +02001322 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001323 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001324
Jan Luebbefac25fc2008-12-27 18:04:34 +00001325 result = dbi_conn_queryf(conn,
1326 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001327 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001328 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001329 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001330 imei);
1331 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001332 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001333 return 1;
1334 }
Harald Welte0b906d02009-12-24 11:21:42 +01001335
Jan Luebbe391d86e2008-12-27 22:33:34 +00001336 equipment_id = 0;
1337 if (dbi_result_get_numrows_affected(result)) {
1338 equipment_id = dbi_conn_sequence_last(conn, NULL);
1339 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001340 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001341
1342 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001343 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001344 else {
1345 result = dbi_conn_queryf(conn,
1346 "SELECT id FROM Equipment "
1347 "WHERE imei = %s ",
1348 imei
1349 );
Harald Welte0b906d02009-12-24 11:21:42 +01001350 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001351 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001352 return 1;
1353 }
1354 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001355 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001356 dbi_result_free(result);
1357 return 1;
1358 }
1359 equipment_id = dbi_result_get_ulonglong(result, "id");
1360 dbi_result_free(result);
1361 }
1362
1363 result = dbi_conn_queryf(conn,
1364 "INSERT OR IGNORE INTO EquipmentWatch "
1365 "(subscriber_id, equipment_id, created, updated) "
1366 "VALUES "
1367 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001368 subscriber->id, equipment_id);
1369 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001370 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001371 return 1;
1372 }
Harald Welte0b906d02009-12-24 11:21:42 +01001373
Jan Luebbe391d86e2008-12-27 22:33:34 +00001374 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001375 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001376 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001377
Jan Luebbefac25fc2008-12-27 18:04:34 +00001378 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001379 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001380 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1381 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001382 else {
1383 result = dbi_conn_queryf(conn,
1384 "UPDATE EquipmentWatch "
1385 "SET updated = datetime('now') "
1386 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001387 subscriber->id, equipment_id);
1388 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001389 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001390 return 1;
1391 }
1392 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001393 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1394 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001395 }
1396
1397 return 0;
1398}
1399
Harald Welte7e310b12009-03-30 20:56:32 +00001400/* store an [unsent] SMS to the database */
1401int db_sms_store(struct gsm_sms *sms)
1402{
1403 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001404 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001405 unsigned char *q_udata;
1406 char *validity_timestamp = "2222-2-2";
1407
1408 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001409
1410 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001411 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001412 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001413 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1414 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001415
Harald Weltef3efc592009-07-27 20:11:35 +02001416 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001417 result = dbi_conn_queryf(conn,
1418 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001419 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001420 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001421 "data_coding_scheme, ud_hdr_ind, "
1422 "user_data, text, "
1423 "dest_addr, dest_ton, dest_npi, "
1424 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001425 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001426 "%u, %u, %u, "
1427 "%u, %u, "
1428 "%s, %s, "
1429 "%s, %u, %u, "
1430 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001431 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001432 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001433 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001434 q_udata, q_text,
1435 q_daddr, sms->dst.ton, sms->dst.npi,
1436 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001437 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001438 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001439 free(q_daddr);
1440 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001441
1442 if (!result)
1443 return -EIO;
1444
1445 dbi_result_free(result);
1446 return 0;
1447}
1448
Harald Welte2ebabca2009-08-09 19:05:21 +02001449static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001450{
Harald Welte76042182009-08-08 16:03:15 +02001451 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001452 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001453 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001454
Harald Welte76042182009-08-08 16:03:15 +02001455 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001456 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001457
Harald Weltebe3e3782009-07-05 14:06:41 +02001458 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001459
Harald Weltef3efc592009-07-27 20:11:35 +02001460 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001461 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001462 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1463 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1464 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1465 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1466 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001467 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001468 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001469
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001470 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1471 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001472 daddr = dbi_result_get_string(result, "dest_addr");
1473 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001474 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1475 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001476 }
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001477 sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001478
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001479 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1480 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001481 saddr = dbi_result_get_string(result, "src_addr");
1482 if (saddr) {
1483 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1484 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1485 }
1486
Harald Welte76042182009-08-08 16:03:15 +02001487 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1488 user_data = dbi_result_get_binary(result, "user_data");
1489 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001490 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001491 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001492
1493 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001494 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001495 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001496 sms->text[sizeof(sms->text)-1] = '\0';
1497 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001498 return sms;
1499}
1500
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001501struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1502{
1503 dbi_result result;
1504 struct gsm_sms *sms;
1505
1506 result = dbi_conn_queryf(conn,
1507 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1508 if (!result)
1509 return NULL;
1510
1511 if (!dbi_result_next_row(result)) {
1512 dbi_result_free(result);
1513 return NULL;
1514 }
1515
1516 sms = sms_from_result(net, result);
1517
1518 dbi_result_free(result);
1519
1520 return sms;
1521}
1522
Harald Welte2ebabca2009-08-09 19:05:21 +02001523/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001524struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001525{
1526 dbi_result result;
1527 struct gsm_sms *sms;
1528
1529 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001530 "SELECT SMS.* "
1531 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001532 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001533 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1534 "AND Subscriber.lac > 0 "
1535 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001536 min_id);
1537 if (!result)
1538 return NULL;
1539
1540 if (!dbi_result_next_row(result)) {
1541 dbi_result_free(result);
1542 return NULL;
1543 }
1544
1545 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001546
1547 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001548
1549 return sms;
1550}
1551
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001552struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1553 unsigned long long min_subscr_id,
1554 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001555{
1556 dbi_result result;
1557 struct gsm_sms *sms;
1558
1559 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001560 "SELECT SMS.* "
1561 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001562 "SMS.dest_addr = Subscriber.extension "
1563 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001564 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001565 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001566 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001567 if (!result)
1568 return NULL;
1569
1570 if (!dbi_result_next_row(result)) {
1571 dbi_result_free(result);
1572 return NULL;
1573 }
1574
1575 sms = sms_from_result(net, result);
1576
1577 dbi_result_free(result);
1578
1579 return sms;
1580}
1581
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001582/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001583struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1584{
1585 dbi_result result;
1586 struct gsm_sms *sms;
1587
1588 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001589 "SELECT SMS.* "
1590 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001591 "SMS.dest_addr = Subscriber.extension "
1592 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001593 "AND Subscriber.lac > 0 "
1594 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001595 subscr->id);
1596 if (!result)
1597 return NULL;
1598
1599 if (!dbi_result_next_row(result)) {
1600 dbi_result_free(result);
1601 return NULL;
1602 }
1603
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001604 sms = sms_from_result(subscr->group->net, result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001605
1606 dbi_result_free(result);
1607
Harald Welte7e310b12009-03-30 20:56:32 +00001608 return sms;
1609}
1610
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001611/* mark a given SMS as delivered */
1612int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001613{
1614 dbi_result result;
1615
1616 result = dbi_conn_queryf(conn,
1617 "UPDATE SMS "
1618 "SET sent = datetime('now') "
1619 "WHERE id = %llu", sms->id);
1620 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001621 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001622 return 1;
1623 }
1624
1625 dbi_result_free(result);
1626 return 0;
1627}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001628
1629/* increase the number of attempted deliveries */
1630int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1631{
1632 dbi_result result;
1633
1634 result = dbi_conn_queryf(conn,
1635 "UPDATE SMS "
1636 "SET deliver_attempts = deliver_attempts + 1 "
1637 "WHERE id = %llu", sms->id);
1638 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001639 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1640 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001641 return 1;
1642 }
1643
1644 dbi_result_free(result);
1645 return 0;
1646}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001647
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001648int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001649 uint8_t apdu_id_flags, uint8_t len,
1650 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001651{
1652 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001653 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001654
1655 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1656
1657 result = dbi_conn_queryf(conn,
1658 "INSERT INTO ApduBlobs "
1659 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1660 "(datetime('now'),%llu,%u,%s)",
1661 subscr->id, apdu_id_flags, q_apdu);
1662
1663 free(q_apdu);
1664
1665 if (!result)
1666 return -EIO;
1667
1668 dbi_result_free(result);
1669 return 0;
1670}
Harald Welteffa55a42009-12-22 19:07:32 +01001671
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001672int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001673{
1674 dbi_result result;
1675 char *q_name;
1676
1677 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1678
1679 result = dbi_conn_queryf(conn,
1680 "INSERT INTO Counters "
1681 "(timestamp,name,value) VALUES "
1682 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1683
1684 free(q_name);
1685
1686 if (!result)
1687 return -EIO;
1688
1689 dbi_result_free(result);
1690 return 0;
1691}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001692
1693static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1694 char *q_prefix)
1695{
1696 dbi_result result;
1697 char *q_name;
1698
1699 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1700 &q_name);
1701
1702 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001703 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001704 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001705 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1706 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001707
1708 free(q_name);
1709
1710 if (!result)
1711 return -EIO;
1712
1713 dbi_result_free(result);
1714 return 0;
1715}
1716
1717int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1718{
1719 unsigned int i;
1720 char *q_prefix;
1721
Harald Weltec1919862010-05-13 12:55:20 +02001722 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001723
1724 for (i = 0; i < ctrg->desc->num_ctr; i++)
1725 db_store_rate_ctr(ctrg, i, q_prefix);
1726
1727 free(q_prefix);
1728
1729 return 0;
1730}