blob: 267b5ef412514c1abf6275d8eb7221e66327c6c3 [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 );
Holger Hans Peter Freytheradb86752016-04-01 20:31:11 +0200526 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100527 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Hans Peter Freytheradb86752016-04-01 20:31:11 +0200528 subscr_put(subscr);
529 return NULL;
530 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000531 subscr->id = dbi_conn_sequence_last(conn, NULL);
532 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000533 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100534 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200535 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000536 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000537}
538
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200539osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200540
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200541static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
542{
543 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200544 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100545 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200546 const unsigned char *cm2, *cm3;
547 struct gsm_equipment *equip = &subscr->equipment;
548
549 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200550 "SELECT Equipment.* "
551 "FROM Equipment JOIN EquipmentWatch ON "
552 "EquipmentWatch.equipment_id=Equipment.id "
553 "WHERE EquipmentWatch.subscriber_id = %llu "
554 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200555 if (!result)
556 return -EIO;
557
558 if (!dbi_result_next_row(result)) {
559 dbi_result_free(result);
560 return -ENOENT;
561 }
562
563 equip->id = dbi_result_get_ulonglong(result, "id");
564
565 string = dbi_result_get_string(result, "imei");
566 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200567 strncpy(equip->imei, string, sizeof(equip->imei)-1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200568
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100569 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200570 if (string) {
571 cm1 = atoi(string) & 0xff;
572 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
573 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200574
575 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
576 cm2 = dbi_result_get_binary(result, "classmark2");
577 if (equip->classmark2_len > sizeof(equip->classmark2))
578 equip->classmark2_len = sizeof(equip->classmark2);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100579 if (cm2)
580 memcpy(equip->classmark2, cm2, equip->classmark2_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200581
582 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
583 cm3 = dbi_result_get_binary(result, "classmark3");
584 if (equip->classmark3_len > sizeof(equip->classmark3))
585 equip->classmark3_len = sizeof(equip->classmark3);
Holger Hans Peter Freytherf9f44902016-01-23 09:21:04 +0100586 if (cm3)
587 memcpy(equip->classmark3, cm3, equip->classmark3_len);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200588
589 dbi_result_free(result);
590
591 return 0;
592}
Harald Welte3606cc52009-12-05 15:13:22 +0530593
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200594int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
595 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530596{
597 dbi_result result;
598 const unsigned char *a3a8_ki;
599
600 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200601 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530602 subscr->id);
603 if (!result)
604 return -EIO;
605
606 if (!dbi_result_next_row(result)) {
607 dbi_result_free(result);
608 return -ENOENT;
609 }
610
611 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
612 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
613 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100614 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200615 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530616 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
617
618 dbi_result_free(result);
619
620 return 0;
621}
622
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200623int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
624 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100625{
626 dbi_result result;
627 struct gsm_auth_info ainfo_old;
628 int rc, upd;
629 unsigned char *ki_str;
630
631 /* Deletion ? */
632 if (ainfo == NULL) {
633 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200634 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100635 subscr->id);
636
637 if (!result)
638 return -EIO;
639
640 dbi_result_free(result);
641
642 return 0;
643 }
644
645 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200646 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100647 if (rc && rc != -ENOENT)
648 return rc;
649 upd = rc ? 0 : 1;
650
651 /* Update / Insert */
652 dbi_conn_quote_binary_copy(conn,
653 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
654
655 if (!upd) {
656 result = dbi_conn_queryf(conn,
657 "INSERT INTO AuthKeys "
658 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200659 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100660 subscr->id, ainfo->auth_algo, ki_str);
661 } else {
662 result = dbi_conn_queryf(conn,
663 "UPDATE AuthKeys "
664 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200665 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100666 ainfo->auth_algo, ki_str, subscr->id);
667 }
668
669 free(ki_str);
670
671 if (!result)
672 return -EIO;
673
674 dbi_result_free(result);
675
676 return 0;
677}
678
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200679int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
680 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530681{
682 dbi_result result;
683 int len;
684 const unsigned char *blob;
685
686 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200687 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530688 subscr->id);
689 if (!result)
690 return -EIO;
691
692 if (!dbi_result_next_row(result)) {
693 dbi_result_free(result);
694 return -ENOENT;
695 }
696
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200697 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530698
Sylvain Munaut70881b72009-12-27 15:41:59 +0100699 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
700 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
701
Harald Welte3606cc52009-12-05 15:13:22 +0530702 len = dbi_result_get_field_length(result, "rand");
703 if (len != sizeof(atuple->rand))
704 goto err_size;
705
706 blob = dbi_result_get_binary(result, "rand");
707 memcpy(atuple->rand, blob, len);
708
709 len = dbi_result_get_field_length(result, "sres");
710 if (len != sizeof(atuple->sres))
711 goto err_size;
712
713 blob = dbi_result_get_binary(result, "sres");
714 memcpy(atuple->sres, blob, len);
715
716 len = dbi_result_get_field_length(result, "kc");
717 if (len != sizeof(atuple->kc))
718 goto err_size;
719
720 blob = dbi_result_get_binary(result, "kc");
721 memcpy(atuple->kc, blob, len);
722
723 dbi_result_free(result);
724
725 return 0;
726
727err_size:
728 dbi_result_free(result);
729 return -EIO;
730}
731
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200732int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
733 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100734{
735 dbi_result result;
736 int rc, upd;
737 struct gsm_auth_tuple atuple_old;
738 unsigned char *rand_str, *sres_str, *kc_str;
739
740 /* Deletion ? */
741 if (atuple == NULL) {
742 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200743 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100744 subscr->id);
745
746 if (!result)
747 return -EIO;
748
749 dbi_result_free(result);
750
751 return 0;
752 }
753
754 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200755 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100756 if (rc && rc != -ENOENT)
757 return rc;
758 upd = rc ? 0 : 1;
759
760 /* Update / Insert */
761 dbi_conn_quote_binary_copy(conn,
762 atuple->rand, sizeof(atuple->rand), &rand_str);
763 dbi_conn_quote_binary_copy(conn,
764 atuple->sres, sizeof(atuple->sres), &sres_str);
765 dbi_conn_quote_binary_copy(conn,
766 atuple->kc, sizeof(atuple->kc), &kc_str);
767
768 if (!upd) {
769 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200770 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100771 "(subscriber_id, issued, use_count, "
772 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200773 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100774 "%u, %s, %s, %s ) ",
775 subscr->id, atuple->use_count, atuple->key_seq,
776 rand_str, sres_str, kc_str);
777 } else {
778 char *issued = atuple->key_seq == atuple_old.key_seq ?
779 "issued" : "datetime('now')";
780 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200781 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100782 "SET issued=%s, use_count=%u, "
783 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200784 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100785 issued, atuple->use_count, atuple->key_seq,
786 rand_str, sres_str, kc_str, subscr->id);
787 }
788
789 free(rand_str);
790 free(sres_str);
791 free(kc_str);
792
793 if (!result)
794 return -EIO;
795
796 dbi_result_free(result);
797
798 return 0;
799}
800
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100801static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
802{
803 const char *string;
804 string = dbi_result_get_string(result, "imsi");
805 if (string)
Jacob Erlbeck7ffa7b02015-04-09 14:47:18 +0200806 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH-1);
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100807
808 string = dbi_result_get_string(result, "tmsi");
809 if (string)
810 subscr->tmsi = tmsi_from_string(string);
811
812 string = dbi_result_get_string(result, "name");
813 if (string)
814 strncpy(subscr->name, string, GSM_NAME_LENGTH);
815
816 string = dbi_result_get_string(result, "extension");
817 if (string)
818 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
819
Kevin Redonc9763a32013-11-04 22:43:15 +0100820 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100821
822 if (!dbi_result_field_is_null(result, "expire_lu"))
823 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
824 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200825 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100826
Kevin Redonc9763a32013-11-04 22:43:15 +0100827 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
828
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100829}
830
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200831#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200832struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200833 const char *id)
834{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000835 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000836 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000837 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000838
Jan Luebbe5c15c852008-12-27 15:59:25 +0000839 switch (field) {
840 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000841 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000842 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200843 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000844 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000845 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000846 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000847 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000848 break;
849 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000850 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000851 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200852 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000853 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000854 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000855 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000856 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000857 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000858 case GSM_SUBSCRIBER_EXTENSION:
859 dbi_conn_quote_string_copy(conn, id, &quoted);
860 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200861 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000862 "WHERE extension = %s ",
863 quoted
864 );
865 free(quoted);
866 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200867 case GSM_SUBSCRIBER_ID:
868 dbi_conn_quote_string_copy(conn, id, &quoted);
869 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200870 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200871 "WHERE id = %s ", quoted);
872 free(quoted);
873 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000874 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100875 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000876 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000877 }
Harald Welte0b906d02009-12-24 11:21:42 +0100878 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100879 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000880 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000881 }
882 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100883 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000884 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000885 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000886 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000887 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000888
889 subscr = subscr_alloc();
890 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000891
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100892 db_set_from_query(subscr, result);
Harald Welte3ad03462016-03-17 14:41:26 +0100893 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
894 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
895 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000896 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200897
898 get_equipment_by_subscr(subscr);
899
Holger Freyther12aa50d2009-01-01 18:02:05 +0000900 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000901}
902
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100903int db_subscriber_update(struct gsm_subscriber *subscr)
904{
905 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100906 dbi_result result;
907
908 /* Copy the id to a string as queryf with %llu is failing */
909 sprintf(buf, "%llu", subscr->id);
910 result = dbi_conn_queryf(conn,
911 BASE_QUERY
912 "WHERE id = %s", buf);
913
914 if (!result) {
915 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
916 return -EIO;
917 }
918 if (!dbi_result_next_row(result)) {
919 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
920 subscr->id);
921 dbi_result_free(result);
922 return -EIO;
923 }
924
925 db_set_from_query(subscr, result);
926 dbi_result_free(result);
927 get_equipment_by_subscr(subscr);
928
929 return 0;
930}
931
Harald Welte0b906d02009-12-24 11:21:42 +0100932int db_sync_subscriber(struct gsm_subscriber *subscriber)
933{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000934 dbi_result result;
Harald Welte3ad03462016-03-17 14:41:26 +0100935 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100936 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200937
Harald Welte019d0162010-12-26 19:12:30 +0100938 dbi_conn_quote_string_copy(conn,
939 subscriber->name, &q_name);
940 dbi_conn_quote_string_copy(conn,
941 subscriber->extension, &q_extension);
942
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200943 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
Harald Welte3ad03462016-03-17 14:41:26 +0100944 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200945 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200946 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200947 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200948 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200949 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100950
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200951 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
952 result = dbi_conn_queryf(conn,
953 "UPDATE Subscriber "
954 "SET updated = datetime('now'), "
955 "name = %s, "
956 "extension = %s, "
957 "authorized = %i, "
958 "tmsi = %s, "
959 "lac = %i, "
960 "expire_lu = NULL "
961 "WHERE imsi = %s ",
962 q_name,
963 q_extension,
964 subscriber->authorized,
965 q_tmsi,
966 subscriber->lac,
967 subscriber->imsi);
968 } else {
969 result = dbi_conn_queryf(conn,
970 "UPDATE Subscriber "
971 "SET updated = datetime('now'), "
972 "name = %s, "
973 "extension = %s, "
974 "authorized = %i, "
975 "tmsi = %s, "
976 "lac = %i, "
977 "expire_lu = datetime(%i, 'unixepoch') "
978 "WHERE imsi = %s ",
979 q_name,
980 q_extension,
981 subscriber->authorized,
982 q_tmsi,
983 subscriber->lac,
984 (int) subscriber->expire_lu,
985 subscriber->imsi);
986 }
Harald Welte0b906d02009-12-24 11:21:42 +0100987
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200988 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100989 free(q_name);
990 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100991
992 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100993 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000994 return 1;
995 }
Harald Welte0b906d02009-12-24 11:21:42 +0100996
Jan Luebbe5c15c852008-12-27 15:59:25 +0000997 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100998
Jan Luebbe5c15c852008-12-27 15:59:25 +0000999 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001000}
1001
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001002int db_subscriber_delete(struct gsm_subscriber *subscr)
1003{
1004 dbi_result result;
1005
1006 result = dbi_conn_queryf(conn,
1007 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1008 subscr->id);
1009 if (!result) {
1010 LOGP(DDB, LOGL_ERROR,
1011 "Failed to delete Authkeys for %llu\n", subscr->id);
1012 return -1;
1013 }
1014 dbi_result_free(result);
1015
1016 result = dbi_conn_queryf(conn,
1017 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1018 subscr->id);
1019 if (!result) {
1020 LOGP(DDB, LOGL_ERROR,
1021 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1022 return -1;
1023 }
1024 dbi_result_free(result);
1025
1026 result = dbi_conn_queryf(conn,
1027 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1028 subscr->id);
1029 if (!result) {
1030 LOGP(DDB, LOGL_ERROR,
1031 "Failed to delete AuthToken for %llu\n", subscr->id);
1032 return -1;
1033 }
1034 dbi_result_free(result);
1035
1036 result = dbi_conn_queryf(conn,
1037 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1038 subscr->id);
1039 if (!result) {
1040 LOGP(DDB, LOGL_ERROR,
1041 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1042 return -1;
1043 }
1044 dbi_result_free(result);
1045
1046 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001047 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1048 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001049 if (!result) {
1050 LOGP(DDB, LOGL_ERROR,
1051 "Failed to delete SMS for %llu\n", subscr->id);
1052 return -1;
1053 }
1054 dbi_result_free(result);
1055
1056 result = dbi_conn_queryf(conn,
1057 "DELETE FROM VLR WHERE subscriber_id=%llu",
1058 subscr->id);
1059 if (!result) {
1060 LOGP(DDB, LOGL_ERROR,
1061 "Failed to delete VLR for %llu\n", subscr->id);
1062 return -1;
1063 }
1064 dbi_result_free(result);
1065
1066 result = dbi_conn_queryf(conn,
1067 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1068 subscr->id);
1069 if (!result) {
1070 LOGP(DDB, LOGL_ERROR,
1071 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1072 return -1;
1073 }
1074 dbi_result_free(result);
1075
1076 result = dbi_conn_queryf(conn,
1077 "DELETE FROM Subscriber WHERE id=%llu",
1078 subscr->id);
1079 if (!result) {
1080 LOGP(DDB, LOGL_ERROR,
1081 "Failed to delete Subscriber for %llu\n", subscr->id);
1082 return -1;
1083 }
1084 dbi_result_free(result);
1085
1086 return 0;
1087}
1088
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001089/**
1090 * List all the authorized and non-expired subscribers. The callback will
1091 * be called one by one. The subscr argument is not fully initialize and
1092 * subscr_get/subscr_put must not be called. The passed in pointer will be
1093 * deleted after the callback by the database call.
1094 */
1095int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1096{
1097 dbi_result result;
1098
Max5c06e402015-07-29 20:20:28 +02001099 result = dbi_conn_query(conn,
1100 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001101 if (!result) {
1102 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1103 return -1;
1104 }
1105
1106 while (dbi_result_next_row(result)) {
1107 struct gsm_subscriber *subscr;
1108
1109 subscr = subscr_alloc();
1110 subscr->id = dbi_result_get_ulonglong(result, "id");
1111 db_set_from_query(subscr, result);
1112 cb(subscr, closure);
1113 OSMO_ASSERT(subscr->use_count == 1);
1114 llist_del(&subscr->entry);
1115 talloc_free(subscr);
1116 }
1117
1118 dbi_result_free(result);
1119 return 0;
1120}
1121
Harald Weltec2e302d2009-07-05 14:08:13 +02001122int db_sync_equipment(struct gsm_equipment *equip)
1123{
1124 dbi_result result;
1125 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001126 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001127 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001128
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001129 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001130 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001131 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001132 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001133 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001134 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001135 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001136 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001137 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001138 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001139
Harald Weltec2e302d2009-07-05 14:08:13 +02001140 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1141 equip->classmark2_len, &cm2);
1142 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1143 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001144 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001145
1146 result = dbi_conn_queryf(conn,
1147 "UPDATE Equipment SET "
1148 "updated = datetime('now'), "
1149 "classmark1 = %u, "
1150 "classmark2 = %s, "
1151 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001152 "WHERE imei = %s ",
1153 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001154
1155 free(cm2);
1156 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001157 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001158
1159 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001160 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001161 return -EIO;
1162 }
1163
1164 dbi_result_free(result);
1165 return 0;
1166}
1167
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001168int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1169{
1170 dbi_result result;
1171
1172 result = dbi_conn_query(conn,
1173 "SELECT id "
1174 "FROM Subscriber "
1175 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001176 "( expire_lu is NOT NULL "
1177 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001178 "LIMIT 1");
1179 if (!result) {
1180 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1181 return -EIO;
1182 }
1183
1184 while (dbi_result_next_row(result))
1185 callback(priv, dbi_result_get_ulonglong(result, "id"));
1186
1187 dbi_result_free(result);
1188 return 0;
1189}
1190
Harald Welte0b906d02009-12-24 11:21:42 +01001191int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1192{
1193 dbi_result result = NULL;
Harald Welte3ad03462016-03-17 14:41:26 +01001194 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001195 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001196
Jan Luebbe5c15c852008-12-27 15:59:25 +00001197 for (;;) {
Daniel Willmanncdeb8152015-10-08 16:10:23 +02001198 if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
1199 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1200 return 1;
1201 }
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001202 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1203 continue;
1204
Harald Welte3ad03462016-03-17 14:41:26 +01001205 sprintf(tmsi, "%u", subscriber->tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001206 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001207 result = dbi_conn_queryf(conn,
1208 "SELECT * FROM Subscriber "
1209 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001210 tmsi_quoted);
1211
Holger Freyther12aa50d2009-01-01 18:02:05 +00001212 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001213
1214 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001215 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1216 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001217 return 1;
1218 }
Harald Welte0b906d02009-12-24 11:21:42 +01001219 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001220 dbi_result_free(result);
1221 continue;
1222 }
1223 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001224 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001225 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1226 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001227 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001228 }
1229 dbi_result_free(result);
1230 }
1231 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001232}
1233
Harald Welte0b906d02009-12-24 11:21:42 +01001234int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1235{
1236 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001237 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001238
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001239 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001240 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001241 result = dbi_conn_queryf(conn,
1242 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001243 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001244 try
1245 );
Harald Welte0b906d02009-12-24 11:21:42 +01001246 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001247 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1248 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001249 return 1;
1250 }
1251 if (dbi_result_get_numrows(result)){
1252 dbi_result_free(result);
1253 continue;
1254 }
1255 if (!dbi_result_next_row(result)) {
1256 dbi_result_free(result);
1257 break;
1258 }
1259 dbi_result_free(result);
1260 }
1261 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001262 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001263 return db_sync_subscriber(subscriber);
1264}
Jan Luebbe31bef492009-08-12 14:31:14 +02001265/*
1266 * try to allocate a new unique token for this subscriber and return it
1267 * via a parameter. if the subscriber already has a token, return
1268 * an error.
1269 */
1270
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001271int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001272{
1273 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001274 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001275
Jan Luebbe31bef492009-08-12 14:31:14 +02001276 for (;;) {
Daniel Willmann2aedfbd2015-10-08 16:10:26 +02001277 if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
1278 LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
1279 return 1;
1280 }
Jan Luebbe31bef492009-08-12 14:31:14 +02001281 if (!try) /* 0 is an invalid token */
1282 continue;
1283 result = dbi_conn_queryf(conn,
1284 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001285 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1286 subscriber->id, try);
1287 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001288 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1289 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001290 return 1;
1291 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001292 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001293 dbi_result_free(result);
1294 continue;
1295 }
1296 if (!dbi_result_next_row(result)) {
1297 dbi_result_free(result);
1298 break;
1299 }
1300 dbi_result_free(result);
1301 }
1302 result = dbi_conn_queryf(conn,
1303 "INSERT INTO AuthToken "
1304 "(subscriber_id, created, token) "
1305 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001306 "(%llu, datetime('now'), \"%08X\") ",
1307 subscriber->id, try);
1308 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001309 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1310 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001311 return 1;
1312 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001313 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001314 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001315 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001316
Jan Luebbe31bef492009-08-12 14:31:14 +02001317 return 0;
1318}
1319
Harald Welte0b906d02009-12-24 11:21:42 +01001320int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1321{
Harald Welted409be72009-11-07 00:06:19 +09001322 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001323 dbi_result result;
1324
Harald Weltec2e302d2009-07-05 14:08:13 +02001325 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001326 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001327
Jan Luebbefac25fc2008-12-27 18:04:34 +00001328 result = dbi_conn_queryf(conn,
1329 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001330 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001331 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001332 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001333 imei);
1334 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001335 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001336 return 1;
1337 }
Harald Welte0b906d02009-12-24 11:21:42 +01001338
Jan Luebbe391d86e2008-12-27 22:33:34 +00001339 equipment_id = 0;
1340 if (dbi_result_get_numrows_affected(result)) {
1341 equipment_id = dbi_conn_sequence_last(conn, NULL);
1342 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001343 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001344
1345 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001346 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001347 else {
1348 result = dbi_conn_queryf(conn,
1349 "SELECT id FROM Equipment "
1350 "WHERE imei = %s ",
1351 imei
1352 );
Harald Welte0b906d02009-12-24 11:21:42 +01001353 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001354 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001355 return 1;
1356 }
1357 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001358 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001359 dbi_result_free(result);
1360 return 1;
1361 }
1362 equipment_id = dbi_result_get_ulonglong(result, "id");
1363 dbi_result_free(result);
1364 }
1365
1366 result = dbi_conn_queryf(conn,
1367 "INSERT OR IGNORE INTO EquipmentWatch "
1368 "(subscriber_id, equipment_id, created, updated) "
1369 "VALUES "
1370 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001371 subscriber->id, equipment_id);
1372 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001373 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001374 return 1;
1375 }
Harald Welte0b906d02009-12-24 11:21:42 +01001376
Jan Luebbe391d86e2008-12-27 22:33:34 +00001377 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001378 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001379 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001380
Jan Luebbefac25fc2008-12-27 18:04:34 +00001381 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001382 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001383 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1384 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001385 else {
1386 result = dbi_conn_queryf(conn,
1387 "UPDATE EquipmentWatch "
1388 "SET updated = datetime('now') "
1389 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001390 subscriber->id, equipment_id);
1391 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001392 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001393 return 1;
1394 }
1395 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001396 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1397 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001398 }
1399
1400 return 0;
1401}
1402
Harald Welte7e310b12009-03-30 20:56:32 +00001403/* store an [unsent] SMS to the database */
1404int db_sms_store(struct gsm_sms *sms)
1405{
1406 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001407 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001408 unsigned char *q_udata;
1409 char *validity_timestamp = "2222-2-2";
1410
1411 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001412
1413 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001414 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001415 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001416 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1417 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001418
Harald Weltef3efc592009-07-27 20:11:35 +02001419 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001420 result = dbi_conn_queryf(conn,
1421 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001422 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001423 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001424 "data_coding_scheme, ud_hdr_ind, "
1425 "user_data, text, "
1426 "dest_addr, dest_ton, dest_npi, "
1427 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001428 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001429 "%u, %u, %u, "
1430 "%u, %u, "
1431 "%s, %s, "
1432 "%s, %u, %u, "
1433 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001434 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001435 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001436 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001437 q_udata, q_text,
1438 q_daddr, sms->dst.ton, sms->dst.npi,
1439 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001440 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001441 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001442 free(q_daddr);
1443 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001444
1445 if (!result)
1446 return -EIO;
1447
1448 dbi_result_free(result);
1449 return 0;
1450}
1451
Harald Welte2ebabca2009-08-09 19:05:21 +02001452static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001453{
Harald Welte76042182009-08-08 16:03:15 +02001454 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001455 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001456 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001457
Harald Welte76042182009-08-08 16:03:15 +02001458 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001459 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001460
Harald Weltebe3e3782009-07-05 14:06:41 +02001461 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001462
Harald Weltef3efc592009-07-27 20:11:35 +02001463 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001464 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001465 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1466 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1467 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1468 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1469 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001470 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001471 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001472
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001473 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1474 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001475 daddr = dbi_result_get_string(result, "dest_addr");
1476 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001477 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1478 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001479 }
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001480 sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001481
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001482 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1483 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001484 saddr = dbi_result_get_string(result, "src_addr");
1485 if (saddr) {
1486 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1487 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1488 }
1489
Harald Welte76042182009-08-08 16:03:15 +02001490 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1491 user_data = dbi_result_get_binary(result, "user_data");
1492 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001493 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001494 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001495
1496 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001497 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001498 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001499 sms->text[sizeof(sms->text)-1] = '\0';
1500 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001501 return sms;
1502}
1503
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001504struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1505{
1506 dbi_result result;
1507 struct gsm_sms *sms;
1508
1509 result = dbi_conn_queryf(conn,
1510 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1511 if (!result)
1512 return NULL;
1513
1514 if (!dbi_result_next_row(result)) {
1515 dbi_result_free(result);
1516 return NULL;
1517 }
1518
1519 sms = sms_from_result(net, result);
1520
1521 dbi_result_free(result);
1522
1523 return sms;
1524}
1525
Harald Welte2ebabca2009-08-09 19:05:21 +02001526/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001527struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001528{
1529 dbi_result result;
1530 struct gsm_sms *sms;
1531
1532 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001533 "SELECT SMS.* "
1534 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001535 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001536 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1537 "AND Subscriber.lac > 0 "
1538 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001539 min_id);
1540 if (!result)
1541 return NULL;
1542
1543 if (!dbi_result_next_row(result)) {
1544 dbi_result_free(result);
1545 return NULL;
1546 }
1547
1548 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001549
1550 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001551
1552 return sms;
1553}
1554
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001555struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1556 unsigned long long min_subscr_id,
1557 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001558{
1559 dbi_result result;
1560 struct gsm_sms *sms;
1561
1562 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001563 "SELECT SMS.* "
1564 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001565 "SMS.dest_addr = Subscriber.extension "
1566 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001567 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001568 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001569 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001570 if (!result)
1571 return NULL;
1572
1573 if (!dbi_result_next_row(result)) {
1574 dbi_result_free(result);
1575 return NULL;
1576 }
1577
1578 sms = sms_from_result(net, result);
1579
1580 dbi_result_free(result);
1581
1582 return sms;
1583}
1584
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001585/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001586struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1587{
1588 dbi_result result;
1589 struct gsm_sms *sms;
1590
1591 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001592 "SELECT SMS.* "
1593 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001594 "SMS.dest_addr = Subscriber.extension "
1595 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001596 "AND Subscriber.lac > 0 "
1597 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001598 subscr->id);
1599 if (!result)
1600 return NULL;
1601
1602 if (!dbi_result_next_row(result)) {
1603 dbi_result_free(result);
1604 return NULL;
1605 }
1606
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001607 sms = sms_from_result(subscr->group->net, result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001608
1609 dbi_result_free(result);
1610
Harald Welte7e310b12009-03-30 20:56:32 +00001611 return sms;
1612}
1613
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001614/* mark a given SMS as delivered */
1615int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001616{
1617 dbi_result result;
1618
1619 result = dbi_conn_queryf(conn,
1620 "UPDATE SMS "
1621 "SET sent = datetime('now') "
1622 "WHERE id = %llu", sms->id);
1623 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001624 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001625 return 1;
1626 }
1627
1628 dbi_result_free(result);
1629 return 0;
1630}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001631
1632/* increase the number of attempted deliveries */
1633int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1634{
1635 dbi_result result;
1636
1637 result = dbi_conn_queryf(conn,
1638 "UPDATE SMS "
1639 "SET deliver_attempts = deliver_attempts + 1 "
1640 "WHERE id = %llu", sms->id);
1641 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001642 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1643 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001644 return 1;
1645 }
1646
1647 dbi_result_free(result);
1648 return 0;
1649}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001650
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001651int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001652 uint8_t apdu_id_flags, uint8_t len,
1653 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001654{
1655 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001656 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001657
1658 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1659
1660 result = dbi_conn_queryf(conn,
1661 "INSERT INTO ApduBlobs "
1662 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1663 "(datetime('now'),%llu,%u,%s)",
1664 subscr->id, apdu_id_flags, q_apdu);
1665
1666 free(q_apdu);
1667
1668 if (!result)
1669 return -EIO;
1670
1671 dbi_result_free(result);
1672 return 0;
1673}
Harald Welteffa55a42009-12-22 19:07:32 +01001674
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001675int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001676{
1677 dbi_result result;
1678 char *q_name;
1679
1680 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1681
1682 result = dbi_conn_queryf(conn,
1683 "INSERT INTO Counters "
1684 "(timestamp,name,value) VALUES "
1685 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1686
1687 free(q_name);
1688
1689 if (!result)
1690 return -EIO;
1691
1692 dbi_result_free(result);
1693 return 0;
1694}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001695
1696static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1697 char *q_prefix)
1698{
1699 dbi_result result;
1700 char *q_name;
1701
1702 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1703 &q_name);
1704
1705 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001706 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001707 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001708 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1709 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001710
1711 free(q_name);
1712
1713 if (!result)
1714 return -EIO;
1715
1716 dbi_result_free(result);
1717 return 0;
1718}
1719
1720int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1721{
1722 unsigned int i;
1723 char *q_prefix;
1724
Harald Weltec1919862010-05-13 12:55:20 +02001725 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001726
1727 for (i = 0; i < ctrg->desc->num_ctr; i++)
1728 db_store_rate_ctr(ctrg, i, q_prefix);
1729
1730 free(q_prefix);
1731
1732 return 0;
1733}