blob: 7e2309d742f3a70be31301a1191e77eaab488959 [file] [log] [blame]
Holger Freyther12aa50d2009-01-01 18:02:05 +00001/* Simple HLR/VLR database backend using dbi */
Jan Luebbefaaa49c2008-12-27 01:07:07 +00002/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00003 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltec2e302d2009-07-05 14:08:13 +02004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Jan Luebbefaaa49c2008-12-27 01:07:07 +000010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Jan Luebbefaaa49c2008-12-27 01:07:07 +000016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Jan Luebbefaaa49c2008-12-27 01:07:07 +000019 *
20 */
21
Harald Weltef2b4cd72010-05-13 11:45:07 +020022#include <stdint.h>
23#include <inttypes.h>
Holger Freytherbde36102008-12-28 22:51:39 +000024#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000025#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000026#include <stdlib.h>
27#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000028#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029#include <dbi/dbi.h>
30
Harald Weltef2b4cd72010-05-13 11:45:07 +020031#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther28dcbc52010-12-22 18:21:14 +010032#include <openbsc/gsm_subscriber.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020033#include <openbsc/gsm_04_11.h>
34#include <openbsc/db.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020035#include <openbsc/debug.h>
Holger Hans Peter Freytherc5faf662010-12-22 18:16:01 +010036
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/talloc.h>
38#include <osmocom/core/statistics.h>
39#include <osmocom/core/rate_ctr.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020040
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010041/* Semi-Private-Interface (SPI) for the subscriber code */
42void subscr_direct_free(struct gsm_subscriber *subscr);
43
Holger Freytherbde36102008-12-28 22:51:39 +000044static char *db_basename = NULL;
45static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000046static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000047
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010048#define SCHEMA_REVISION "4"
Jan Luebbebfbdeec2012-12-27 00:27:16 +010049
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010050enum {
51 SCHEMA_META,
52 INSERT_META,
53 SCHEMA_SUBSCRIBER,
54 SCHEMA_AUTH,
55 SCHEMA_EQUIPMENT,
56 SCHEMA_EQUIPMENT_WATCH,
57 SCHEMA_SMS,
58 SCHEMA_VLR,
59 SCHEMA_APDU,
60 SCHEMA_COUNTERS,
61 SCHEMA_RATE,
62 SCHEMA_AUTHKEY,
63 SCHEMA_AUTHLAST,
64};
65
66static const char *create_stmts[] = {
67 [SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
Harald Welte7e310b12009-03-30 20:56:32 +000068 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
69 "key TEXT UNIQUE NOT NULL, "
70 "value TEXT NOT NULL"
71 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010072 [INSERT_META] = "INSERT OR IGNORE INTO Meta "
Harald Welte7e310b12009-03-30 20:56:32 +000073 "(key, value) "
74 "VALUES "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010075 "('revision', " SCHEMA_REVISION ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010076 [SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
Harald Welte7e310b12009-03-30 20:56:32 +000077 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
78 "created TIMESTAMP NOT NULL, "
79 "updated TIMESTAMP NOT NULL, "
80 "imsi NUMERIC UNIQUE NOT NULL, "
81 "name TEXT, "
82 "extension TEXT UNIQUE, "
83 "authorized INTEGER NOT NULL DEFAULT 0, "
84 "tmsi TEXT UNIQUE, "
Jan Luebbebfbdeec2012-12-27 00:27:16 +010085 "lac INTEGER NOT NULL DEFAULT 0, "
86 "expire_lu TIMESTAMP DEFAULT NULL"
Harald Welte7e310b12009-03-30 20:56:32 +000087 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010088 [SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
Jan Luebbe31bef492009-08-12 14:31:14 +020089 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
90 "subscriber_id INTEGER UNIQUE NOT NULL, "
91 "created TIMESTAMP NOT NULL, "
92 "token TEXT UNIQUE NOT NULL"
93 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +010094 [SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
Harald Welte7e310b12009-03-30 20:56:32 +000095 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
96 "created TIMESTAMP NOT NULL, "
97 "updated TIMESTAMP NOT NULL, "
98 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020099 "classmark1 NUMERIC, "
100 "classmark2 BLOB, "
101 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +0000102 "imei NUMERIC UNIQUE NOT NULL"
103 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100104 [SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
Harald Welte7e310b12009-03-30 20:56:32 +0000105 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
106 "created TIMESTAMP NOT NULL, "
107 "updated TIMESTAMP NOT NULL, "
108 "subscriber_id NUMERIC NOT NULL, "
109 "equipment_id NUMERIC NOT NULL, "
110 "UNIQUE (subscriber_id, equipment_id) "
111 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100112 [SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +0200113 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +0000114 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
115 "created TIMESTAMP NOT NULL, "
116 "sent TIMESTAMP, "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200117 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +0200118 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +0200119 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +0200120 "reply_path_req INTEGER NOT NULL, "
121 "status_rep_req INTEGER NOT NULL, "
122 "protocol_id INTEGER NOT NULL, "
123 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200124 "ud_hdr_ind INTEGER NOT NULL, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +0200125 "src_addr TEXT NOT NULL, "
126 "src_ton INTEGER NOT NULL, "
127 "src_npi INTEGER NOT NULL, "
128 "dest_addr TEXT NOT NULL, "
129 "dest_ton INTEGER NOT NULL, "
130 "dest_npi INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200131 "user_data BLOB, " /* TP-UD */
132 /* additional data, interpreted from SMS */
133 "header BLOB, " /* UD Header */
134 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000135 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100136 [SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
Holger Freytherc2995ea2009-04-19 06:35:23 +0000137 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
138 "created TIMESTAMP NOT NULL, "
139 "updated TIMESTAMP NOT NULL, "
140 "subscriber_id NUMERIC UNIQUE NOT NULL, "
141 "last_bts NUMERIC NOT NULL "
142 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100143 [SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
Harald Welte (local)026531e2009-08-16 10:40:10 +0200144 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
145 "created TIMESTAMP NOT NULL, "
146 "apdu_id_flags INTEGER NOT NULL, "
147 "subscriber_id INTEGER NOT NULL, "
148 "apdu BLOB "
149 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100150 [SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
Harald Welteffa55a42009-12-22 19:07:32 +0100151 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
152 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100153 "value INTEGER NOT NULL, "
154 "name TEXT NOT NULL "
Harald Welte09f7ad02009-12-24 09:42:07 +0100155 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100156 [SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
Harald Weltec1919862010-05-13 12:55:20 +0200157 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
158 "timestamp TIMESTAMP NOT NULL, "
159 "value INTEGER NOT NULL, "
160 "name TEXT NOT NULL, "
Harald Welted94d6a02010-05-14 17:38:47 +0200161 "idx INTEGER NOT NULL "
Harald Weltec1919862010-05-13 12:55:20 +0200162 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100163 [SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200164 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut77d334a2009-12-27 19:26:12 +0100165 "algorithm_id INTEGER NOT NULL, "
Harald Welte3606cc52009-12-05 15:13:22 +0530166 "a3a8_ki BLOB "
167 ")",
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100168 [SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
Sylvain Munaut10bf8122010-06-09 11:31:32 +0200169 "subscriber_id INTEGER PRIMARY KEY, "
Sylvain Munaut70881b72009-12-27 15:41:59 +0100170 "issued TIMESTAMP NOT NULL, "
171 "use_count INTEGER NOT NULL DEFAULT 0, "
172 "key_seq INTEGER NOT NULL, "
173 "rand BLOB NOT NULL, "
174 "sres BLOB NOT NULL, "
175 "kc BLOB NOT NULL "
Harald Welteffa55a42009-12-22 19:07:32 +0100176 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000177};
178
Harald Welte0b906d02009-12-24 11:21:42 +0100179void db_error_func(dbi_conn conn, void *data)
180{
181 const char *msg;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000182 dbi_conn_error(conn, &msg);
Harald Welteae1f1592009-12-24 11:39:14 +0100183 LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000184}
185
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100186static int update_db_revision_2(void)
187{
188 dbi_result result;
189
190 result = dbi_conn_query(conn,
191 "ALTER TABLE Subscriber "
192 "ADD COLUMN expire_lu "
193 "TIMESTAMP DEFAULT NULL");
194 if (!result) {
195 LOGP(DDB, LOGL_ERROR,
Alexander Chemeris7e20f642014-03-07 16:59:53 +0100196 "Failed to alter table Subscriber (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100197 return -EINVAL;
198 }
199 dbi_result_free(result);
200
201 result = dbi_conn_query(conn,
202 "UPDATE Meta "
203 "SET value = '3' "
204 "WHERE key = 'revision'");
205 if (!result) {
206 LOGP(DDB, LOGL_ERROR,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100207 "Failed to update DB schema revision (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100208 return -EINVAL;
209 }
210 dbi_result_free(result);
211
212 return 0;
213}
214
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100215/**
216 * Copied from the normal sms_from_result_v3 to avoid having
217 * to make sure that the real routine will remain backward
218 * compatible.
219 */
220static struct gsm_sms *sms_from_result_v3(dbi_result result)
221{
222 struct gsm_sms *sms = sms_alloc();
223 long long unsigned int sender_id;
224 struct gsm_subscriber *sender;
225 const char *text, *daddr;
226 const unsigned char *user_data;
227 char buf[32];
228
229 if (!sms)
230 return NULL;
231
232 sms->id = dbi_result_get_ulonglong(result, "id");
233
234 sender_id = dbi_result_get_ulonglong(result, "sender_id");
235 snprintf(buf, sizeof(buf), "%llu", sender_id);
236 sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
237 OSMO_ASSERT(sender);
238 strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
239 subscr_direct_free(sender);
240 sender = NULL;
241
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +0200242 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
243 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
244 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
245 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
246 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100247 "data_coding_scheme");
248
249 daddr = dbi_result_get_string(result, "dest_addr");
250 if (daddr) {
251 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
252 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
253 }
254
255 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
256 user_data = dbi_result_get_binary(result, "user_data");
257 if (sms->user_data_len > sizeof(sms->user_data))
258 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
259 memcpy(sms->user_data, user_data, sms->user_data_len);
260
261 text = dbi_result_get_string(result, "text");
262 if (text) {
263 strncpy(sms->text, text, sizeof(sms->text));
264 sms->text[sizeof(sms->text)-1] = '\0';
265 }
266 return sms;
267}
268
269static int update_db_revision_3(void)
270{
271 dbi_result result;
272 struct gsm_sms *sms;
273
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100274 LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
275
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100276 result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
277 if (!result) {
278 LOGP(DDB, LOGL_ERROR,
279 "Failed to begin transaction (upgrade from rev 3)\n");
280 return -EINVAL;
281 }
282 dbi_result_free(result);
283
284 /* Rename old SMS table to be able create a new one */
285 result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
286 if (!result) {
287 LOGP(DDB, LOGL_ERROR,
288 "Failed to rename the old SMS table (upgrade from rev 3).\n");
289 goto rollback;
290 }
291 dbi_result_free(result);
292
293 /* Create new SMS table with all the bells and whistles! */
294 result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
295 if (!result) {
296 LOGP(DDB, LOGL_ERROR,
297 "Failed to create a new SMS table (upgrade from rev 3).\n");
298 goto rollback;
299 }
300 dbi_result_free(result);
301
302 /* Cycle through old messages and convert them to the new format */
303 result = dbi_conn_queryf(conn, "SELECT * FROM SMS_3");
304 if (!result) {
305 LOGP(DDB, LOGL_ERROR,
306 "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
307 goto rollback;
308 }
309 while (dbi_result_next_row(result)) {
310 sms = sms_from_result_v3(result);
311 if (db_sms_store(sms) != 0) {
312 LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
313 sms_free(sms);
314 dbi_result_free(result);
315 goto rollback;
316 }
317 sms_free(sms);
318 }
319 dbi_result_free(result);
320
321 /* Remove the temporary table */
322 result = dbi_conn_query(conn, "DROP TABLE SMS_3");
323 if (!result) {
324 LOGP(DDB, LOGL_ERROR,
325 "Failed to drop the old SMS table (upgrade from rev 3).\n");
326 goto rollback;
327 }
328 dbi_result_free(result);
329
330 /* We're done. Bump DB Meta revision to 4 */
331 result = dbi_conn_query(conn,
332 "UPDATE Meta "
333 "SET value = '4' "
334 "WHERE key = 'revision'");
335 if (!result) {
336 LOGP(DDB, LOGL_ERROR,
337 "Failed to update DB schema revision (upgrade from rev 3).\n");
338 goto rollback;
339 }
340 dbi_result_free(result);
341
342 result = dbi_conn_query(conn, "COMMIT TRANSACTION");
343 if (!result) {
344 LOGP(DDB, LOGL_ERROR,
345 "Failed to commit the transaction (upgrade from rev 3)\n");
346 return -EINVAL;
347 }
348
349 /* Shrink DB file size by actually wiping out SMS_3 table data */
350 result = dbi_conn_query(conn, "VACUUM");
351 if (!result)
352 LOGP(DDB, LOGL_ERROR,
353 "VACUUM failed. Ignoring it (upgrade from rev 3).\n");
354 else
355 dbi_result_free(result);
356
357 return 0;
358
359rollback:
360 result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
361 if (!result)
362 LOGP(DDB, LOGL_ERROR,
363 "Rollback failed (upgrade from rev 3).\n");
364 else
365 dbi_result_free(result);
366 return -EINVAL;
367}
368
Harald Welted0b7b772009-08-09 19:03:42 +0200369static int check_db_revision(void)
370{
371 dbi_result result;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100372 const char *rev_s;
Harald Welted0b7b772009-08-09 19:03:42 +0200373
374 result = dbi_conn_query(conn,
375 "SELECT value FROM Meta WHERE key='revision'");
376 if (!result)
377 return -EINVAL;
378
379 if (!dbi_result_next_row(result)) {
380 dbi_result_free(result);
381 return -EINVAL;
382 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100383 rev_s = dbi_result_get_string(result, "value");
384 if (!rev_s) {
Harald Welted0b7b772009-08-09 19:03:42 +0200385 dbi_result_free(result);
386 return -EINVAL;
387 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100388 if (!strcmp(rev_s, "2")) {
389 if (update_db_revision_2()) {
390 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
391 dbi_result_free(result);
392 return -EINVAL;
393 }
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100394 } else if (!strcmp(rev_s, "3")) {
395 if (update_db_revision_3()) {
396 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
397 dbi_result_free(result);
398 return -EINVAL;
399 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100400 } else if (!strcmp(rev_s, SCHEMA_REVISION)) {
401 /* everything is fine */
402 } else {
403 LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s);
404 dbi_result_free(result);
405 return -EINVAL;
406 }
407
408 dbi_result_free(result);
409 return 0;
410}
411
412static int db_configure(void)
413{
414 dbi_result result;
415
416 result = dbi_conn_query(conn,
417 "PRAGMA synchronous = FULL");
418 if (!result)
419 return -EINVAL;
Harald Welted0b7b772009-08-09 19:03:42 +0200420
421 dbi_result_free(result);
422 return 0;
423}
424
Harald Welte0b906d02009-12-24 11:21:42 +0100425int db_init(const char *name)
426{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000427 dbi_initialize(NULL);
Harald Welte0b906d02009-12-24 11:21:42 +0100428
Jan Luebbe5c15c852008-12-27 15:59:25 +0000429 conn = dbi_conn_new("sqlite3");
Harald Welte0b906d02009-12-24 11:21:42 +0100430 if (conn == NULL) {
Harald Welteae1f1592009-12-24 11:39:14 +0100431 LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000432 return 1;
433 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000434
Holger Freyther12aa50d2009-01-01 18:02:05 +0000435 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000436
Jan Luebbe5c15c852008-12-27 15:59:25 +0000437 /* MySQL
438 dbi_conn_set_option(conn, "host", "localhost");
439 dbi_conn_set_option(conn, "username", "your_name");
440 dbi_conn_set_option(conn, "password", "your_password");
441 dbi_conn_set_option(conn, "dbname", "your_dbname");
442 dbi_conn_set_option(conn, "encoding", "UTF-8");
443 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000444
Jan Luebbe5c15c852008-12-27 15:59:25 +0000445 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000446 db_basename = strdup(name);
447 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000448 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
449 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000450
Harald Welted0b7b772009-08-09 19:03:42 +0200451 if (dbi_conn_connect(conn) < 0)
452 goto out_err;
453
Jan Luebbe5c15c852008-12-27 15:59:25 +0000454 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200455
456out_err:
457 free(db_dirname);
458 free(db_basename);
459 db_dirname = db_basename = NULL;
460 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000461}
462
Harald Welted0b7b772009-08-09 19:03:42 +0200463
Harald Welted1476bc2011-07-16 13:24:09 +0200464int db_prepare(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100465{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000466 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000467 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000468
Harald Welte7e310b12009-03-30 20:56:32 +0000469 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
470 result = dbi_conn_query(conn, create_stmts[i]);
Harald Welte0b906d02009-12-24 11:21:42 +0100471 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100472 LOGP(DDB, LOGL_ERROR,
473 "Failed to create some table.\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000474 return 1;
475 }
476 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000477 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000478
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200479 if (check_db_revision() < 0) {
Harald Welteae1f1592009-12-24 11:39:14 +0100480 LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200481 "please update your database schema\n");
482 return -1;
483 }
484
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100485 db_configure();
486
Jan Luebbe5c15c852008-12-27 15:59:25 +0000487 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000488}
489
Harald Welted1476bc2011-07-16 13:24:09 +0200490int db_fini(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100491{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000492 dbi_conn_close(conn);
493 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000494
Harald Welte2c5f4c62011-07-16 13:22:57 +0200495 free(db_dirname);
496 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000497 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000498}
499
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200500struct gsm_subscriber *db_create_subscriber(const char *imsi)
Harald Welte9176bd42009-07-23 18:46:00 +0200501{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000502 dbi_result result;
Harald Welte0b906d02009-12-24 11:21:42 +0100503 struct gsm_subscriber *subscr;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000504
505 /* Is this subscriber known in the db? */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200506 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000507 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000508 result = dbi_conn_queryf(conn,
509 "UPDATE Subscriber set updated = datetime('now') "
510 "WHERE imsi = %s " , imsi);
Harald Welte0b906d02009-12-24 11:21:42 +0100511 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100512 LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n");
Harald Welte0b906d02009-12-24 11:21:42 +0100513 else
Harald Welte523200b2008-12-30 14:58:44 +0000514 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000515 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000516 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000517
518 subscr = subscr_alloc();
519 if (!subscr)
520 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200521 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000522 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000523 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000524 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000525 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000526 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000527 imsi
528 );
Harald Welte0b906d02009-12-24 11:21:42 +0100529 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100530 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
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)
567 strncpy(equip->imei, string, sizeof(equip->imei));
568
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);
579 memcpy(equip->classmark2, cm2, equip->classmark2_len);
580
581 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
582 cm3 = dbi_result_get_binary(result, "classmark3");
583 if (equip->classmark3_len > sizeof(equip->classmark3))
584 equip->classmark3_len = sizeof(equip->classmark3);
585 memcpy(equip->classmark3, cm3, equip->classmark3_len);
586
587 dbi_result_free(result);
588
589 return 0;
590}
Harald Welte3606cc52009-12-05 15:13:22 +0530591
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200592int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
593 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530594{
595 dbi_result result;
596 const unsigned char *a3a8_ki;
597
598 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200599 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530600 subscr->id);
601 if (!result)
602 return -EIO;
603
604 if (!dbi_result_next_row(result)) {
605 dbi_result_free(result);
606 return -ENOENT;
607 }
608
609 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
610 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
611 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100612 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200613 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530614 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
615
616 dbi_result_free(result);
617
618 return 0;
619}
620
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200621int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
622 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100623{
624 dbi_result result;
625 struct gsm_auth_info ainfo_old;
626 int rc, upd;
627 unsigned char *ki_str;
628
629 /* Deletion ? */
630 if (ainfo == NULL) {
631 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200632 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100633 subscr->id);
634
635 if (!result)
636 return -EIO;
637
638 dbi_result_free(result);
639
640 return 0;
641 }
642
643 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200644 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100645 if (rc && rc != -ENOENT)
646 return rc;
647 upd = rc ? 0 : 1;
648
649 /* Update / Insert */
650 dbi_conn_quote_binary_copy(conn,
651 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
652
653 if (!upd) {
654 result = dbi_conn_queryf(conn,
655 "INSERT INTO AuthKeys "
656 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200657 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100658 subscr->id, ainfo->auth_algo, ki_str);
659 } else {
660 result = dbi_conn_queryf(conn,
661 "UPDATE AuthKeys "
662 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200663 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100664 ainfo->auth_algo, ki_str, subscr->id);
665 }
666
667 free(ki_str);
668
669 if (!result)
670 return -EIO;
671
672 dbi_result_free(result);
673
674 return 0;
675}
676
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200677int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
678 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530679{
680 dbi_result result;
681 int len;
682 const unsigned char *blob;
683
684 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200685 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530686 subscr->id);
687 if (!result)
688 return -EIO;
689
690 if (!dbi_result_next_row(result)) {
691 dbi_result_free(result);
692 return -ENOENT;
693 }
694
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200695 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530696
Sylvain Munaut70881b72009-12-27 15:41:59 +0100697 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
698 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
699
Harald Welte3606cc52009-12-05 15:13:22 +0530700 len = dbi_result_get_field_length(result, "rand");
701 if (len != sizeof(atuple->rand))
702 goto err_size;
703
704 blob = dbi_result_get_binary(result, "rand");
705 memcpy(atuple->rand, blob, len);
706
707 len = dbi_result_get_field_length(result, "sres");
708 if (len != sizeof(atuple->sres))
709 goto err_size;
710
711 blob = dbi_result_get_binary(result, "sres");
712 memcpy(atuple->sres, blob, len);
713
714 len = dbi_result_get_field_length(result, "kc");
715 if (len != sizeof(atuple->kc))
716 goto err_size;
717
718 blob = dbi_result_get_binary(result, "kc");
719 memcpy(atuple->kc, blob, len);
720
721 dbi_result_free(result);
722
723 return 0;
724
725err_size:
726 dbi_result_free(result);
727 return -EIO;
728}
729
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200730int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
731 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100732{
733 dbi_result result;
734 int rc, upd;
735 struct gsm_auth_tuple atuple_old;
736 unsigned char *rand_str, *sres_str, *kc_str;
737
738 /* Deletion ? */
739 if (atuple == NULL) {
740 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200741 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100742 subscr->id);
743
744 if (!result)
745 return -EIO;
746
747 dbi_result_free(result);
748
749 return 0;
750 }
751
752 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200753 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100754 if (rc && rc != -ENOENT)
755 return rc;
756 upd = rc ? 0 : 1;
757
758 /* Update / Insert */
759 dbi_conn_quote_binary_copy(conn,
760 atuple->rand, sizeof(atuple->rand), &rand_str);
761 dbi_conn_quote_binary_copy(conn,
762 atuple->sres, sizeof(atuple->sres), &sres_str);
763 dbi_conn_quote_binary_copy(conn,
764 atuple->kc, sizeof(atuple->kc), &kc_str);
765
766 if (!upd) {
767 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200768 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100769 "(subscriber_id, issued, use_count, "
770 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200771 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100772 "%u, %s, %s, %s ) ",
773 subscr->id, atuple->use_count, atuple->key_seq,
774 rand_str, sres_str, kc_str);
775 } else {
776 char *issued = atuple->key_seq == atuple_old.key_seq ?
777 "issued" : "datetime('now')";
778 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200779 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100780 "SET issued=%s, use_count=%u, "
781 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200782 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100783 issued, atuple->use_count, atuple->key_seq,
784 rand_str, sres_str, kc_str, subscr->id);
785 }
786
787 free(rand_str);
788 free(sres_str);
789 free(kc_str);
790
791 if (!result)
792 return -EIO;
793
794 dbi_result_free(result);
795
796 return 0;
797}
798
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100799static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
800{
801 const char *string;
802 string = dbi_result_get_string(result, "imsi");
803 if (string)
804 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
805
806 string = dbi_result_get_string(result, "tmsi");
807 if (string)
808 subscr->tmsi = tmsi_from_string(string);
809
810 string = dbi_result_get_string(result, "name");
811 if (string)
812 strncpy(subscr->name, string, GSM_NAME_LENGTH);
813
814 string = dbi_result_get_string(result, "extension");
815 if (string)
816 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
817
Kevin Redonc9763a32013-11-04 22:43:15 +0100818 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100819
820 if (!dbi_result_field_is_null(result, "expire_lu"))
821 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
822 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200823 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100824
Kevin Redonc9763a32013-11-04 22:43:15 +0100825 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
826
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100827}
828
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200829#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200830struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200831 const char *id)
832{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000833 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000834 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000835 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000836
Jan Luebbe5c15c852008-12-27 15:59:25 +0000837 switch (field) {
838 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000839 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000840 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200841 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000842 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000843 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000844 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000845 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000846 break;
847 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000848 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000849 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200850 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000851 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000852 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000853 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000854 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000855 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000856 case GSM_SUBSCRIBER_EXTENSION:
857 dbi_conn_quote_string_copy(conn, id, &quoted);
858 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200859 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000860 "WHERE extension = %s ",
861 quoted
862 );
863 free(quoted);
864 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200865 case GSM_SUBSCRIBER_ID:
866 dbi_conn_quote_string_copy(conn, id, &quoted);
867 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200868 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200869 "WHERE id = %s ", quoted);
870 free(quoted);
871 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000872 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100873 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000874 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000875 }
Harald Welte0b906d02009-12-24 11:21:42 +0100876 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100877 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000878 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000879 }
880 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100881 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000882 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000883 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000884 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000885 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000886
887 subscr = subscr_alloc();
888 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000889
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100890 db_set_from_query(subscr, result);
Harald Welteae1f1592009-12-24 11:39:14 +0100891 DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
Holger Freyther91754472009-06-09 08:52:41 +0000892 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000893 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000894 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200895
896 get_equipment_by_subscr(subscr);
897
Holger Freyther12aa50d2009-01-01 18:02:05 +0000898 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000899}
900
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100901int db_subscriber_update(struct gsm_subscriber *subscr)
902{
903 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100904 dbi_result result;
905
906 /* Copy the id to a string as queryf with %llu is failing */
907 sprintf(buf, "%llu", subscr->id);
908 result = dbi_conn_queryf(conn,
909 BASE_QUERY
910 "WHERE id = %s", buf);
911
912 if (!result) {
913 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
914 return -EIO;
915 }
916 if (!dbi_result_next_row(result)) {
917 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
918 subscr->id);
919 dbi_result_free(result);
920 return -EIO;
921 }
922
923 db_set_from_query(subscr, result);
924 dbi_result_free(result);
925 get_equipment_by_subscr(subscr);
926
927 return 0;
928}
929
Harald Welte0b906d02009-12-24 11:21:42 +0100930int db_sync_subscriber(struct gsm_subscriber *subscriber)
931{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000932 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200933 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100934 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200935
Harald Welte019d0162010-12-26 19:12:30 +0100936 dbi_conn_quote_string_copy(conn,
937 subscriber->name, &q_name);
938 dbi_conn_quote_string_copy(conn,
939 subscriber->extension, &q_extension);
940
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200941 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
942 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200943 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200944 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200945 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200946 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200947 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100948
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200949 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
950 result = dbi_conn_queryf(conn,
951 "UPDATE Subscriber "
952 "SET updated = datetime('now'), "
953 "name = %s, "
954 "extension = %s, "
955 "authorized = %i, "
956 "tmsi = %s, "
957 "lac = %i, "
958 "expire_lu = NULL "
959 "WHERE imsi = %s ",
960 q_name,
961 q_extension,
962 subscriber->authorized,
963 q_tmsi,
964 subscriber->lac,
965 subscriber->imsi);
966 } else {
967 result = dbi_conn_queryf(conn,
968 "UPDATE Subscriber "
969 "SET updated = datetime('now'), "
970 "name = %s, "
971 "extension = %s, "
972 "authorized = %i, "
973 "tmsi = %s, "
974 "lac = %i, "
975 "expire_lu = datetime(%i, 'unixepoch') "
976 "WHERE imsi = %s ",
977 q_name,
978 q_extension,
979 subscriber->authorized,
980 q_tmsi,
981 subscriber->lac,
982 (int) subscriber->expire_lu,
983 subscriber->imsi);
984 }
Harald Welte0b906d02009-12-24 11:21:42 +0100985
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200986 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100987 free(q_name);
988 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100989
990 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100991 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000992 return 1;
993 }
Harald Welte0b906d02009-12-24 11:21:42 +0100994
Jan Luebbe5c15c852008-12-27 15:59:25 +0000995 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100996
Jan Luebbe5c15c852008-12-27 15:59:25 +0000997 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000998}
999
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001000int db_subscriber_delete(struct gsm_subscriber *subscr)
1001{
1002 dbi_result result;
1003
1004 result = dbi_conn_queryf(conn,
1005 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1006 subscr->id);
1007 if (!result) {
1008 LOGP(DDB, LOGL_ERROR,
1009 "Failed to delete Authkeys for %llu\n", subscr->id);
1010 return -1;
1011 }
1012 dbi_result_free(result);
1013
1014 result = dbi_conn_queryf(conn,
1015 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1016 subscr->id);
1017 if (!result) {
1018 LOGP(DDB, LOGL_ERROR,
1019 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1020 return -1;
1021 }
1022 dbi_result_free(result);
1023
1024 result = dbi_conn_queryf(conn,
1025 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1026 subscr->id);
1027 if (!result) {
1028 LOGP(DDB, LOGL_ERROR,
1029 "Failed to delete AuthToken for %llu\n", subscr->id);
1030 return -1;
1031 }
1032 dbi_result_free(result);
1033
1034 result = dbi_conn_queryf(conn,
1035 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1036 subscr->id);
1037 if (!result) {
1038 LOGP(DDB, LOGL_ERROR,
1039 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1040 return -1;
1041 }
1042 dbi_result_free(result);
1043
1044 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001045 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1046 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001047 if (!result) {
1048 LOGP(DDB, LOGL_ERROR,
1049 "Failed to delete SMS for %llu\n", subscr->id);
1050 return -1;
1051 }
1052 dbi_result_free(result);
1053
1054 result = dbi_conn_queryf(conn,
1055 "DELETE FROM VLR WHERE subscriber_id=%llu",
1056 subscr->id);
1057 if (!result) {
1058 LOGP(DDB, LOGL_ERROR,
1059 "Failed to delete VLR for %llu\n", subscr->id);
1060 return -1;
1061 }
1062 dbi_result_free(result);
1063
1064 result = dbi_conn_queryf(conn,
1065 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1066 subscr->id);
1067 if (!result) {
1068 LOGP(DDB, LOGL_ERROR,
1069 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1070 return -1;
1071 }
1072 dbi_result_free(result);
1073
1074 result = dbi_conn_queryf(conn,
1075 "DELETE FROM Subscriber WHERE id=%llu",
1076 subscr->id);
1077 if (!result) {
1078 LOGP(DDB, LOGL_ERROR,
1079 "Failed to delete Subscriber for %llu\n", subscr->id);
1080 return -1;
1081 }
1082 dbi_result_free(result);
1083
1084 return 0;
1085}
1086
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001087/**
1088 * List all the authorized and non-expired subscribers. The callback will
1089 * be called one by one. The subscr argument is not fully initialize and
1090 * subscr_get/subscr_put must not be called. The passed in pointer will be
1091 * deleted after the callback by the database call.
1092 */
1093int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1094{
1095 dbi_result result;
1096
1097 result = dbi_conn_queryf(conn,
1098 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
1099 if (!result) {
1100 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1101 return -1;
1102 }
1103
1104 while (dbi_result_next_row(result)) {
1105 struct gsm_subscriber *subscr;
1106
1107 subscr = subscr_alloc();
1108 subscr->id = dbi_result_get_ulonglong(result, "id");
1109 db_set_from_query(subscr, result);
1110 cb(subscr, closure);
1111 OSMO_ASSERT(subscr->use_count == 1);
1112 llist_del(&subscr->entry);
1113 talloc_free(subscr);
1114 }
1115
1116 dbi_result_free(result);
1117 return 0;
1118}
1119
Harald Weltec2e302d2009-07-05 14:08:13 +02001120int db_sync_equipment(struct gsm_equipment *equip)
1121{
1122 dbi_result result;
1123 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001124 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001125 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001126
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001127 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001128 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001129 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001130 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001131 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001132 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001133 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001134 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001135 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001136 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001137
Harald Weltec2e302d2009-07-05 14:08:13 +02001138 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1139 equip->classmark2_len, &cm2);
1140 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1141 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001142 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001143
1144 result = dbi_conn_queryf(conn,
1145 "UPDATE Equipment SET "
1146 "updated = datetime('now'), "
1147 "classmark1 = %u, "
1148 "classmark2 = %s, "
1149 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001150 "WHERE imei = %s ",
1151 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001152
1153 free(cm2);
1154 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001155 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001156
1157 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001158 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001159 return -EIO;
1160 }
1161
1162 dbi_result_free(result);
1163 return 0;
1164}
1165
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001166int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1167{
1168 dbi_result result;
1169
1170 result = dbi_conn_query(conn,
1171 "SELECT id "
1172 "FROM Subscriber "
1173 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001174 "( expire_lu is NOT NULL "
1175 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001176 "LIMIT 1");
1177 if (!result) {
1178 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1179 return -EIO;
1180 }
1181
1182 while (dbi_result_next_row(result))
1183 callback(priv, dbi_result_get_ulonglong(result, "id"));
1184
1185 dbi_result_free(result);
1186 return 0;
1187}
1188
Harald Welte0b906d02009-12-24 11:21:42 +01001189int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1190{
1191 dbi_result result = NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001192 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001193 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001194
Jan Luebbe5c15c852008-12-27 15:59:25 +00001195 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001196 subscriber->tmsi = rand();
1197 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1198 continue;
1199
1200 sprintf(tmsi, "%u", subscriber->tmsi);
1201 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001202 result = dbi_conn_queryf(conn,
1203 "SELECT * FROM Subscriber "
1204 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001205 tmsi_quoted);
1206
Holger Freyther12aa50d2009-01-01 18:02:05 +00001207 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001208
1209 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001210 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1211 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001212 return 1;
1213 }
Harald Welte0b906d02009-12-24 11:21:42 +01001214 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001215 dbi_result_free(result);
1216 continue;
1217 }
1218 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001219 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001220 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1221 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001222 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001223 }
1224 dbi_result_free(result);
1225 }
1226 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001227}
1228
Harald Welte0b906d02009-12-24 11:21:42 +01001229int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1230{
1231 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001232 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001233
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001234 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001235 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001236 result = dbi_conn_queryf(conn,
1237 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001238 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001239 try
1240 );
Harald Welte0b906d02009-12-24 11:21:42 +01001241 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001242 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1243 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001244 return 1;
1245 }
1246 if (dbi_result_get_numrows(result)){
1247 dbi_result_free(result);
1248 continue;
1249 }
1250 if (!dbi_result_next_row(result)) {
1251 dbi_result_free(result);
1252 break;
1253 }
1254 dbi_result_free(result);
1255 }
1256 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001257 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001258 return db_sync_subscriber(subscriber);
1259}
Jan Luebbe31bef492009-08-12 14:31:14 +02001260/*
1261 * try to allocate a new unique token for this subscriber and return it
1262 * via a parameter. if the subscriber already has a token, return
1263 * an error.
1264 */
1265
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001266int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001267{
1268 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001269 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001270
Jan Luebbe31bef492009-08-12 14:31:14 +02001271 for (;;) {
1272 try = rand();
1273 if (!try) /* 0 is an invalid token */
1274 continue;
1275 result = dbi_conn_queryf(conn,
1276 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001277 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1278 subscriber->id, try);
1279 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001280 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1281 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001282 return 1;
1283 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001284 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001285 dbi_result_free(result);
1286 continue;
1287 }
1288 if (!dbi_result_next_row(result)) {
1289 dbi_result_free(result);
1290 break;
1291 }
1292 dbi_result_free(result);
1293 }
1294 result = dbi_conn_queryf(conn,
1295 "INSERT INTO AuthToken "
1296 "(subscriber_id, created, token) "
1297 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001298 "(%llu, datetime('now'), \"%08X\") ",
1299 subscriber->id, try);
1300 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001301 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1302 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001303 return 1;
1304 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001305 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001306 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001307 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001308
Jan Luebbe31bef492009-08-12 14:31:14 +02001309 return 0;
1310}
1311
Harald Welte0b906d02009-12-24 11:21:42 +01001312int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1313{
Harald Welted409be72009-11-07 00:06:19 +09001314 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001315 dbi_result result;
1316
Harald Weltec2e302d2009-07-05 14:08:13 +02001317 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001318 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001319
Jan Luebbefac25fc2008-12-27 18:04:34 +00001320 result = dbi_conn_queryf(conn,
1321 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001322 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001323 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001324 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001325 imei);
1326 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001327 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001328 return 1;
1329 }
Harald Welte0b906d02009-12-24 11:21:42 +01001330
Jan Luebbe391d86e2008-12-27 22:33:34 +00001331 equipment_id = 0;
1332 if (dbi_result_get_numrows_affected(result)) {
1333 equipment_id = dbi_conn_sequence_last(conn, NULL);
1334 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001335 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001336
1337 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001338 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001339 else {
1340 result = dbi_conn_queryf(conn,
1341 "SELECT id FROM Equipment "
1342 "WHERE imei = %s ",
1343 imei
1344 );
Harald Welte0b906d02009-12-24 11:21:42 +01001345 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001346 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001347 return 1;
1348 }
1349 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001350 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001351 dbi_result_free(result);
1352 return 1;
1353 }
1354 equipment_id = dbi_result_get_ulonglong(result, "id");
1355 dbi_result_free(result);
1356 }
1357
1358 result = dbi_conn_queryf(conn,
1359 "INSERT OR IGNORE INTO EquipmentWatch "
1360 "(subscriber_id, equipment_id, created, updated) "
1361 "VALUES "
1362 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001363 subscriber->id, equipment_id);
1364 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001365 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001366 return 1;
1367 }
Harald Welte0b906d02009-12-24 11:21:42 +01001368
Jan Luebbe391d86e2008-12-27 22:33:34 +00001369 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001370 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001371 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001372
Jan Luebbefac25fc2008-12-27 18:04:34 +00001373 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001374 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001375 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1376 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001377 else {
1378 result = dbi_conn_queryf(conn,
1379 "UPDATE EquipmentWatch "
1380 "SET updated = datetime('now') "
1381 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001382 subscriber->id, equipment_id);
1383 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001384 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001385 return 1;
1386 }
1387 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001388 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1389 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001390 }
1391
1392 return 0;
1393}
1394
Harald Welte7e310b12009-03-30 20:56:32 +00001395/* store an [unsent] SMS to the database */
1396int db_sms_store(struct gsm_sms *sms)
1397{
1398 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001399 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001400 unsigned char *q_udata;
1401 char *validity_timestamp = "2222-2-2";
1402
1403 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001404
1405 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001406 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001407 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001408 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1409 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001410
Harald Weltef3efc592009-07-27 20:11:35 +02001411 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001412 result = dbi_conn_queryf(conn,
1413 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001414 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001415 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001416 "data_coding_scheme, ud_hdr_ind, "
1417 "user_data, text, "
1418 "dest_addr, dest_ton, dest_npi, "
1419 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001420 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001421 "%u, %u, %u, "
1422 "%u, %u, "
1423 "%s, %s, "
1424 "%s, %u, %u, "
1425 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001426 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001427 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001428 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001429 q_udata, q_text,
1430 q_daddr, sms->dst.ton, sms->dst.npi,
1431 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001432 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001433 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001434 free(q_daddr);
1435 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001436
1437 if (!result)
1438 return -EIO;
1439
1440 dbi_result_free(result);
1441 return 0;
1442}
1443
Harald Welte2ebabca2009-08-09 19:05:21 +02001444static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001445{
Harald Welte76042182009-08-08 16:03:15 +02001446 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001447 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001448 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001449
Harald Welte76042182009-08-08 16:03:15 +02001450 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001451 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001452
Harald Weltebe3e3782009-07-05 14:06:41 +02001453 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001454
Harald Weltef3efc592009-07-27 20:11:35 +02001455 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001456 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001457 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1458 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1459 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1460 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1461 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001462 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001463 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001464
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001465 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1466 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001467 daddr = dbi_result_get_string(result, "dest_addr");
1468 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001469 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1470 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001471 }
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001472 sms->receiver = subscr_get_by_extension(net, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001473
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001474 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1475 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001476 saddr = dbi_result_get_string(result, "src_addr");
1477 if (saddr) {
1478 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1479 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1480 }
1481
Harald Welte76042182009-08-08 16:03:15 +02001482 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1483 user_data = dbi_result_get_binary(result, "user_data");
1484 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001485 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001486 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001487
1488 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001489 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001490 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001491 sms->text[sizeof(sms->text)-1] = '\0';
1492 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001493 return sms;
1494}
1495
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001496struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1497{
1498 dbi_result result;
1499 struct gsm_sms *sms;
1500
1501 result = dbi_conn_queryf(conn,
1502 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1503 if (!result)
1504 return NULL;
1505
1506 if (!dbi_result_next_row(result)) {
1507 dbi_result_free(result);
1508 return NULL;
1509 }
1510
1511 sms = sms_from_result(net, result);
1512
1513 dbi_result_free(result);
1514
1515 return sms;
1516}
1517
Harald Welte2ebabca2009-08-09 19:05:21 +02001518/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001519struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001520{
1521 dbi_result result;
1522 struct gsm_sms *sms;
1523
1524 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001525 "SELECT SMS.* "
1526 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001527 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001528 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1529 "AND Subscriber.lac > 0 "
1530 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001531 min_id);
1532 if (!result)
1533 return NULL;
1534
1535 if (!dbi_result_next_row(result)) {
1536 dbi_result_free(result);
1537 return NULL;
1538 }
1539
1540 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001541
1542 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001543
1544 return sms;
1545}
1546
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001547struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1548 unsigned long long min_subscr_id,
1549 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001550{
1551 dbi_result result;
1552 struct gsm_sms *sms;
1553
1554 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001555 "SELECT SMS.* "
1556 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001557 "SMS.dest_addr = Subscriber.extension "
1558 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001559 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001560 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001561 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001562 if (!result)
1563 return NULL;
1564
1565 if (!dbi_result_next_row(result)) {
1566 dbi_result_free(result);
1567 return NULL;
1568 }
1569
1570 sms = sms_from_result(net, result);
1571
1572 dbi_result_free(result);
1573
1574 return sms;
1575}
1576
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001577/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001578struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1579{
1580 dbi_result result;
1581 struct gsm_sms *sms;
1582
1583 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001584 "SELECT SMS.* "
1585 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001586 "SMS.dest_addr = Subscriber.extension "
1587 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001588 "AND Subscriber.lac > 0 "
1589 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001590 subscr->id);
1591 if (!result)
1592 return NULL;
1593
1594 if (!dbi_result_next_row(result)) {
1595 dbi_result_free(result);
1596 return NULL;
1597 }
1598
1599 sms = sms_from_result(subscr->net, result);
1600
1601 dbi_result_free(result);
1602
Harald Welte7e310b12009-03-30 20:56:32 +00001603 return sms;
1604}
1605
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001606/* mark a given SMS as delivered */
1607int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001608{
1609 dbi_result result;
1610
1611 result = dbi_conn_queryf(conn,
1612 "UPDATE SMS "
1613 "SET sent = datetime('now') "
1614 "WHERE id = %llu", sms->id);
1615 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001616 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001617 return 1;
1618 }
1619
1620 dbi_result_free(result);
1621 return 0;
1622}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001623
1624/* increase the number of attempted deliveries */
1625int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1626{
1627 dbi_result result;
1628
1629 result = dbi_conn_queryf(conn,
1630 "UPDATE SMS "
1631 "SET deliver_attempts = deliver_attempts + 1 "
1632 "WHERE id = %llu", sms->id);
1633 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001634 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1635 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001636 return 1;
1637 }
1638
1639 dbi_result_free(result);
1640 return 0;
1641}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001642
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001643int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001644 uint8_t apdu_id_flags, uint8_t len,
1645 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001646{
1647 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001648 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001649
1650 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1651
1652 result = dbi_conn_queryf(conn,
1653 "INSERT INTO ApduBlobs "
1654 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1655 "(datetime('now'),%llu,%u,%s)",
1656 subscr->id, apdu_id_flags, q_apdu);
1657
1658 free(q_apdu);
1659
1660 if (!result)
1661 return -EIO;
1662
1663 dbi_result_free(result);
1664 return 0;
1665}
Harald Welteffa55a42009-12-22 19:07:32 +01001666
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001667int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001668{
1669 dbi_result result;
1670 char *q_name;
1671
1672 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1673
1674 result = dbi_conn_queryf(conn,
1675 "INSERT INTO Counters "
1676 "(timestamp,name,value) VALUES "
1677 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1678
1679 free(q_name);
1680
1681 if (!result)
1682 return -EIO;
1683
1684 dbi_result_free(result);
1685 return 0;
1686}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001687
1688static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1689 char *q_prefix)
1690{
1691 dbi_result result;
1692 char *q_name;
1693
1694 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1695 &q_name);
1696
1697 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001698 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001699 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001700 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1701 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001702
1703 free(q_name);
1704
1705 if (!result)
1706 return -EIO;
1707
1708 dbi_result_free(result);
1709 return 0;
1710}
1711
1712int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1713{
1714 unsigned int i;
1715 char *q_prefix;
1716
Harald Weltec1919862010-05-13 12:55:20 +02001717 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001718
1719 for (i = 0; i < ctrg->desc->num_ctr; i++)
1720 db_store_rate_ctr(ctrg, i, q_prefix);
1721
1722 free(q_prefix);
1723
1724 return 0;
1725}