blob: 656c661b7f9654afa213419e3432e6a37c5b75a5 [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);
Harald Weltec7548a12014-07-10 20:18:15 +0200184 osmo_log_backtrace(DDB, LOGL_ERROR);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000185}
186
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100187static int update_db_revision_2(void)
188{
189 dbi_result result;
190
191 result = dbi_conn_query(conn,
192 "ALTER TABLE Subscriber "
193 "ADD COLUMN expire_lu "
194 "TIMESTAMP DEFAULT NULL");
195 if (!result) {
196 LOGP(DDB, LOGL_ERROR,
Alexander Chemeris7e20f642014-03-07 16:59:53 +0100197 "Failed to alter table Subscriber (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100198 return -EINVAL;
199 }
200 dbi_result_free(result);
201
202 result = dbi_conn_query(conn,
203 "UPDATE Meta "
204 "SET value = '3' "
205 "WHERE key = 'revision'");
206 if (!result) {
207 LOGP(DDB, LOGL_ERROR,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100208 "Failed to update DB schema revision (upgrade from rev 2).\n");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100209 return -EINVAL;
210 }
211 dbi_result_free(result);
212
213 return 0;
214}
215
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100216/**
217 * Copied from the normal sms_from_result_v3 to avoid having
218 * to make sure that the real routine will remain backward
219 * compatible.
220 */
221static struct gsm_sms *sms_from_result_v3(dbi_result result)
222{
223 struct gsm_sms *sms = sms_alloc();
224 long long unsigned int sender_id;
225 struct gsm_subscriber *sender;
226 const char *text, *daddr;
227 const unsigned char *user_data;
228 char buf[32];
229
230 if (!sms)
231 return NULL;
232
233 sms->id = dbi_result_get_ulonglong(result, "id");
234
235 sender_id = dbi_result_get_ulonglong(result, "sender_id");
236 snprintf(buf, sizeof(buf), "%llu", sender_id);
237 sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
238 OSMO_ASSERT(sender);
239 strncpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)-1);
240 subscr_direct_free(sender);
241 sender = NULL;
242
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +0200243 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
244 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
245 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
246 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
247 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100248 "data_coding_scheme");
249
250 daddr = dbi_result_get_string(result, "dest_addr");
251 if (daddr) {
252 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
253 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
254 }
255
256 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
257 user_data = dbi_result_get_binary(result, "user_data");
258 if (sms->user_data_len > sizeof(sms->user_data))
259 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
260 memcpy(sms->user_data, user_data, sms->user_data_len);
261
262 text = dbi_result_get_string(result, "text");
263 if (text) {
264 strncpy(sms->text, text, sizeof(sms->text));
265 sms->text[sizeof(sms->text)-1] = '\0';
266 }
267 return sms;
268}
269
270static int update_db_revision_3(void)
271{
272 dbi_result result;
273 struct gsm_sms *sms;
274
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100275 LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
276
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100277 result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
278 if (!result) {
279 LOGP(DDB, LOGL_ERROR,
280 "Failed to begin transaction (upgrade from rev 3)\n");
281 return -EINVAL;
282 }
283 dbi_result_free(result);
284
285 /* Rename old SMS table to be able create a new one */
286 result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
287 if (!result) {
288 LOGP(DDB, LOGL_ERROR,
289 "Failed to rename the old SMS table (upgrade from rev 3).\n");
290 goto rollback;
291 }
292 dbi_result_free(result);
293
294 /* Create new SMS table with all the bells and whistles! */
295 result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
296 if (!result) {
297 LOGP(DDB, LOGL_ERROR,
298 "Failed to create a new SMS table (upgrade from rev 3).\n");
299 goto rollback;
300 }
301 dbi_result_free(result);
302
303 /* Cycle through old messages and convert them to the new format */
304 result = dbi_conn_queryf(conn, "SELECT * FROM SMS_3");
305 if (!result) {
306 LOGP(DDB, LOGL_ERROR,
307 "Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
308 goto rollback;
309 }
310 while (dbi_result_next_row(result)) {
311 sms = sms_from_result_v3(result);
312 if (db_sms_store(sms) != 0) {
313 LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
314 sms_free(sms);
315 dbi_result_free(result);
316 goto rollback;
317 }
318 sms_free(sms);
319 }
320 dbi_result_free(result);
321
322 /* Remove the temporary table */
323 result = dbi_conn_query(conn, "DROP TABLE SMS_3");
324 if (!result) {
325 LOGP(DDB, LOGL_ERROR,
326 "Failed to drop the old SMS table (upgrade from rev 3).\n");
327 goto rollback;
328 }
329 dbi_result_free(result);
330
331 /* We're done. Bump DB Meta revision to 4 */
332 result = dbi_conn_query(conn,
333 "UPDATE Meta "
334 "SET value = '4' "
335 "WHERE key = 'revision'");
336 if (!result) {
337 LOGP(DDB, LOGL_ERROR,
338 "Failed to update DB schema revision (upgrade from rev 3).\n");
339 goto rollback;
340 }
341 dbi_result_free(result);
342
343 result = dbi_conn_query(conn, "COMMIT TRANSACTION");
344 if (!result) {
345 LOGP(DDB, LOGL_ERROR,
346 "Failed to commit the transaction (upgrade from rev 3)\n");
347 return -EINVAL;
348 }
349
350 /* Shrink DB file size by actually wiping out SMS_3 table data */
351 result = dbi_conn_query(conn, "VACUUM");
352 if (!result)
353 LOGP(DDB, LOGL_ERROR,
354 "VACUUM failed. Ignoring it (upgrade from rev 3).\n");
355 else
356 dbi_result_free(result);
357
358 return 0;
359
360rollback:
361 result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
362 if (!result)
363 LOGP(DDB, LOGL_ERROR,
364 "Rollback failed (upgrade from rev 3).\n");
365 else
366 dbi_result_free(result);
367 return -EINVAL;
368}
369
Harald Welted0b7b772009-08-09 19:03:42 +0200370static int check_db_revision(void)
371{
372 dbi_result result;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100373 const char *rev_s;
Harald Welted0b7b772009-08-09 19:03:42 +0200374
375 result = dbi_conn_query(conn,
376 "SELECT value FROM Meta WHERE key='revision'");
377 if (!result)
378 return -EINVAL;
379
380 if (!dbi_result_next_row(result)) {
381 dbi_result_free(result);
382 return -EINVAL;
383 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100384 rev_s = dbi_result_get_string(result, "value");
385 if (!rev_s) {
Harald Welted0b7b772009-08-09 19:03:42 +0200386 dbi_result_free(result);
387 return -EINVAL;
388 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100389 if (!strcmp(rev_s, "2")) {
390 if (update_db_revision_2()) {
391 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
392 dbi_result_free(result);
393 return -EINVAL;
394 }
Holger Hans Peter Freythere7cc9aa2014-03-07 18:17:22 +0100395 } else if (!strcmp(rev_s, "3")) {
396 if (update_db_revision_3()) {
397 LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s);
398 dbi_result_free(result);
399 return -EINVAL;
400 }
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100401 } else if (!strcmp(rev_s, SCHEMA_REVISION)) {
402 /* everything is fine */
403 } else {
404 LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s);
405 dbi_result_free(result);
406 return -EINVAL;
407 }
408
409 dbi_result_free(result);
410 return 0;
411}
412
413static int db_configure(void)
414{
415 dbi_result result;
416
417 result = dbi_conn_query(conn,
418 "PRAGMA synchronous = FULL");
419 if (!result)
420 return -EINVAL;
Harald Welted0b7b772009-08-09 19:03:42 +0200421
422 dbi_result_free(result);
423 return 0;
424}
425
Harald Welte0b906d02009-12-24 11:21:42 +0100426int db_init(const char *name)
427{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 dbi_initialize(NULL);
Harald Welte0b906d02009-12-24 11:21:42 +0100429
Jan Luebbe5c15c852008-12-27 15:59:25 +0000430 conn = dbi_conn_new("sqlite3");
Harald Welte0b906d02009-12-24 11:21:42 +0100431 if (conn == NULL) {
Harald Welteae1f1592009-12-24 11:39:14 +0100432 LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000433 return 1;
434 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000435
Holger Freyther12aa50d2009-01-01 18:02:05 +0000436 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000437
Jan Luebbe5c15c852008-12-27 15:59:25 +0000438 /* MySQL
439 dbi_conn_set_option(conn, "host", "localhost");
440 dbi_conn_set_option(conn, "username", "your_name");
441 dbi_conn_set_option(conn, "password", "your_password");
442 dbi_conn_set_option(conn, "dbname", "your_dbname");
443 dbi_conn_set_option(conn, "encoding", "UTF-8");
444 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000445
Jan Luebbe5c15c852008-12-27 15:59:25 +0000446 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000447 db_basename = strdup(name);
448 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000449 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
450 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000451
Harald Welted0b7b772009-08-09 19:03:42 +0200452 if (dbi_conn_connect(conn) < 0)
453 goto out_err;
454
Jan Luebbe5c15c852008-12-27 15:59:25 +0000455 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200456
457out_err:
458 free(db_dirname);
459 free(db_basename);
460 db_dirname = db_basename = NULL;
461 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000462}
463
Harald Welted0b7b772009-08-09 19:03:42 +0200464
Harald Welted1476bc2011-07-16 13:24:09 +0200465int db_prepare(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100466{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000467 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000468 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000469
Harald Welte7e310b12009-03-30 20:56:32 +0000470 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
471 result = dbi_conn_query(conn, create_stmts[i]);
Harald Welte0b906d02009-12-24 11:21:42 +0100472 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100473 LOGP(DDB, LOGL_ERROR,
474 "Failed to create some table.\n");
Harald Welte7e310b12009-03-30 20:56:32 +0000475 return 1;
476 }
477 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000478 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000479
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200480 if (check_db_revision() < 0) {
Harald Welteae1f1592009-12-24 11:39:14 +0100481 LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200482 "please update your database schema\n");
483 return -1;
484 }
485
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100486 db_configure();
487
Jan Luebbe5c15c852008-12-27 15:59:25 +0000488 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000489}
490
Harald Welted1476bc2011-07-16 13:24:09 +0200491int db_fini(void)
Harald Welte0b906d02009-12-24 11:21:42 +0100492{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000493 dbi_conn_close(conn);
494 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000495
Harald Welte2c5f4c62011-07-16 13:22:57 +0200496 free(db_dirname);
497 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000498 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000499}
500
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200501struct gsm_subscriber *db_create_subscriber(const char *imsi)
Harald Welte9176bd42009-07-23 18:46:00 +0200502{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000503 dbi_result result;
Harald Welte0b906d02009-12-24 11:21:42 +0100504 struct gsm_subscriber *subscr;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000505
506 /* Is this subscriber known in the db? */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200507 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000508 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000509 result = dbi_conn_queryf(conn,
510 "UPDATE Subscriber set updated = datetime('now') "
511 "WHERE imsi = %s " , imsi);
Harald Welte0b906d02009-12-24 11:21:42 +0100512 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100513 LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n");
Harald Welte0b906d02009-12-24 11:21:42 +0100514 else
Harald Welte523200b2008-12-30 14:58:44 +0000515 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000516 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000517 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000518
519 subscr = subscr_alloc();
520 if (!subscr)
521 return NULL;
Harald Welte2c5f4c62011-07-16 13:22:57 +0200522 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000523 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000524 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000525 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000526 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000527 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000528 imsi
529 );
Harald Welte0b906d02009-12-24 11:21:42 +0100530 if (!result)
Harald Welteae1f1592009-12-24 11:39:14 +0100531 LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000532 subscr->id = dbi_conn_sequence_last(conn, NULL);
533 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000534 dbi_result_free(result);
Harald Welte (local)441e4832009-12-26 18:57:32 +0100535 LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200536 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000537 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000538}
539
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200540osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200541
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200542static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
543{
544 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200545 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100546 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200547 const unsigned char *cm2, *cm3;
548 struct gsm_equipment *equip = &subscr->equipment;
549
550 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +0200551 "SELECT Equipment.* "
552 "FROM Equipment JOIN EquipmentWatch ON "
553 "EquipmentWatch.equipment_id=Equipment.id "
554 "WHERE EquipmentWatch.subscriber_id = %llu "
555 "ORDER BY EquipmentWatch.updated DESC", subscr->id);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200556 if (!result)
557 return -EIO;
558
559 if (!dbi_result_next_row(result)) {
560 dbi_result_free(result);
561 return -ENOENT;
562 }
563
564 equip->id = dbi_result_get_ulonglong(result, "id");
565
566 string = dbi_result_get_string(result, "imei");
567 if (string)
568 strncpy(equip->imei, string, sizeof(equip->imei));
569
Harald Welte (local)1f3ecd42009-12-26 18:56:00 +0100570 string = dbi_result_get_string(result, "classmark1");
Holger Hans Peter Freytherceb072d2010-03-30 15:28:36 +0200571 if (string) {
572 cm1 = atoi(string) & 0xff;
573 memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
574 }
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200575
576 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
577 cm2 = dbi_result_get_binary(result, "classmark2");
578 if (equip->classmark2_len > sizeof(equip->classmark2))
579 equip->classmark2_len = sizeof(equip->classmark2);
580 memcpy(equip->classmark2, cm2, equip->classmark2_len);
581
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);
586 memcpy(equip->classmark3, cm3, equip->classmark3_len);
587
588 dbi_result_free(result);
589
590 return 0;
591}
Harald Welte3606cc52009-12-05 15:13:22 +0530592
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200593int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
594 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530595{
596 dbi_result result;
597 const unsigned char *a3a8_ki;
598
599 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200600 "SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530601 subscr->id);
602 if (!result)
603 return -EIO;
604
605 if (!dbi_result_next_row(result)) {
606 dbi_result_free(result);
607 return -ENOENT;
608 }
609
610 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
611 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
612 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
Sylvain Munaute1cb4de2009-12-27 19:24:05 +0100613 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
Sylvain Munaut5e80cc42012-05-07 22:09:15 +0200614 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
Harald Welte3606cc52009-12-05 15:13:22 +0530615 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
616
617 dbi_result_free(result);
618
619 return 0;
620}
621
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200622int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
623 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100624{
625 dbi_result result;
626 struct gsm_auth_info ainfo_old;
627 int rc, upd;
628 unsigned char *ki_str;
629
630 /* Deletion ? */
631 if (ainfo == NULL) {
632 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200633 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100634 subscr->id);
635
636 if (!result)
637 return -EIO;
638
639 dbi_result_free(result);
640
641 return 0;
642 }
643
644 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200645 rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100646 if (rc && rc != -ENOENT)
647 return rc;
648 upd = rc ? 0 : 1;
649
650 /* Update / Insert */
651 dbi_conn_quote_binary_copy(conn,
652 ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
653
654 if (!upd) {
655 result = dbi_conn_queryf(conn,
656 "INSERT INTO AuthKeys "
657 "(subscriber_id, algorithm_id, a3a8_ki) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200658 "VALUES (%llu, %u, %s)",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100659 subscr->id, ainfo->auth_algo, ki_str);
660 } else {
661 result = dbi_conn_queryf(conn,
662 "UPDATE AuthKeys "
663 "SET algorithm_id=%u, a3a8_ki=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200664 "WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100665 ainfo->auth_algo, ki_str, subscr->id);
666 }
667
668 free(ki_str);
669
670 if (!result)
671 return -EIO;
672
673 dbi_result_free(result);
674
675 return 0;
676}
677
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200678int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
679 struct gsm_subscriber *subscr)
Harald Welte3606cc52009-12-05 15:13:22 +0530680{
681 dbi_result result;
682 int len;
683 const unsigned char *blob;
684
685 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200686 "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
Harald Welte3606cc52009-12-05 15:13:22 +0530687 subscr->id);
688 if (!result)
689 return -EIO;
690
691 if (!dbi_result_next_row(result)) {
692 dbi_result_free(result);
693 return -ENOENT;
694 }
695
Holger Hans Peter Freythere8859512013-07-04 20:24:02 +0200696 memset(atuple, 0, sizeof(*atuple));
Harald Welte3606cc52009-12-05 15:13:22 +0530697
Sylvain Munaut70881b72009-12-27 15:41:59 +0100698 atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
699 atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
700
Harald Welte3606cc52009-12-05 15:13:22 +0530701 len = dbi_result_get_field_length(result, "rand");
702 if (len != sizeof(atuple->rand))
703 goto err_size;
704
705 blob = dbi_result_get_binary(result, "rand");
706 memcpy(atuple->rand, blob, len);
707
708 len = dbi_result_get_field_length(result, "sres");
709 if (len != sizeof(atuple->sres))
710 goto err_size;
711
712 blob = dbi_result_get_binary(result, "sres");
713 memcpy(atuple->sres, blob, len);
714
715 len = dbi_result_get_field_length(result, "kc");
716 if (len != sizeof(atuple->kc))
717 goto err_size;
718
719 blob = dbi_result_get_binary(result, "kc");
720 memcpy(atuple->kc, blob, len);
721
722 dbi_result_free(result);
723
724 return 0;
725
726err_size:
727 dbi_result_free(result);
728 return -EIO;
729}
730
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200731int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
732 struct gsm_subscriber *subscr)
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100733{
734 dbi_result result;
735 int rc, upd;
736 struct gsm_auth_tuple atuple_old;
737 unsigned char *rand_str, *sres_str, *kc_str;
738
739 /* Deletion ? */
740 if (atuple == NULL) {
741 result = dbi_conn_queryf(conn,
Sylvain Munautadea4f12010-07-03 15:38:35 +0200742 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100743 subscr->id);
744
745 if (!result)
746 return -EIO;
747
748 dbi_result_free(result);
749
750 return 0;
751 }
752
753 /* Check if already existing */
Sylvain Munaut92b2ff52010-06-09 11:32:51 +0200754 rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100755 if (rc && rc != -ENOENT)
756 return rc;
757 upd = rc ? 0 : 1;
758
759 /* Update / Insert */
760 dbi_conn_quote_binary_copy(conn,
761 atuple->rand, sizeof(atuple->rand), &rand_str);
762 dbi_conn_quote_binary_copy(conn,
763 atuple->sres, sizeof(atuple->sres), &sres_str);
764 dbi_conn_quote_binary_copy(conn,
765 atuple->kc, sizeof(atuple->kc), &kc_str);
766
767 if (!upd) {
768 result = dbi_conn_queryf(conn,
Sylvain Munautc614a6a2010-06-09 13:03:39 +0200769 "INSERT INTO AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100770 "(subscriber_id, issued, use_count, "
771 "key_seq, rand, sres, kc) "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200772 "VALUES (%llu, datetime('now'), %u, "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100773 "%u, %s, %s, %s ) ",
774 subscr->id, atuple->use_count, atuple->key_seq,
775 rand_str, sres_str, kc_str);
776 } else {
777 char *issued = atuple->key_seq == atuple_old.key_seq ?
778 "issued" : "datetime('now')";
779 result = dbi_conn_queryf(conn,
Sylvain Munaut31ac3072010-06-10 22:26:21 +0200780 "UPDATE AuthLastTuples "
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100781 "SET issued=%s, use_count=%u, "
782 "key_seq=%u, rand=%s, sres=%s, kc=%s "
Sylvain Munautadea4f12010-07-03 15:38:35 +0200783 "WHERE subscriber_id = %llu",
Sylvain Munaut062d5ef2009-12-27 19:27:53 +0100784 issued, atuple->use_count, atuple->key_seq,
785 rand_str, sres_str, kc_str, subscr->id);
786 }
787
788 free(rand_str);
789 free(sres_str);
790 free(kc_str);
791
792 if (!result)
793 return -EIO;
794
795 dbi_result_free(result);
796
797 return 0;
798}
799
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100800static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
801{
802 const char *string;
803 string = dbi_result_get_string(result, "imsi");
804 if (string)
805 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
806
807 string = dbi_result_get_string(result, "tmsi");
808 if (string)
809 subscr->tmsi = tmsi_from_string(string);
810
811 string = dbi_result_get_string(result, "name");
812 if (string)
813 strncpy(subscr->name, string, GSM_NAME_LENGTH);
814
815 string = dbi_result_get_string(result, "extension");
816 if (string)
817 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
818
Kevin Redonc9763a32013-11-04 22:43:15 +0100819 subscr->lac = dbi_result_get_ulonglong(result, "lac");
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100820
821 if (!dbi_result_field_is_null(result, "expire_lu"))
822 subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
823 else
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200824 subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100825
Kevin Redonc9763a32013-11-04 22:43:15 +0100826 subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
827
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100828}
829
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200830#define BASE_QUERY "SELECT * FROM Subscriber "
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200831struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
Harald Welte9176bd42009-07-23 18:46:00 +0200832 const char *id)
833{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000834 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000835 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000836 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000837
Jan Luebbe5c15c852008-12-27 15:59:25 +0000838 switch (field) {
839 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000840 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000841 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200842 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000843 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000844 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000845 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000846 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000847 break;
848 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000849 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000850 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200851 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000852 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000853 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000854 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000855 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000856 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000857 case GSM_SUBSCRIBER_EXTENSION:
858 dbi_conn_quote_string_copy(conn, id, &quoted);
859 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200860 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000861 "WHERE extension = %s ",
862 quoted
863 );
864 free(quoted);
865 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200866 case GSM_SUBSCRIBER_ID:
867 dbi_conn_quote_string_copy(conn, id, &quoted);
868 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200869 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200870 "WHERE id = %s ", quoted);
871 free(quoted);
872 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000873 default:
Harald Welteae1f1592009-12-24 11:39:14 +0100874 LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000875 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000876 }
Harald Welte0b906d02009-12-24 11:21:42 +0100877 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100878 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000879 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000880 }
881 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +0100882 DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
Holger Freyther1ef983b2009-02-22 20:33:09 +0000883 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000884 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000885 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000886 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000887
888 subscr = subscr_alloc();
889 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000890
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100891 db_set_from_query(subscr, result);
Harald Welteae1f1592009-12-24 11:39:14 +0100892 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 +0000893 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000894 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000895 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200896
897 get_equipment_by_subscr(subscr);
898
Holger Freyther12aa50d2009-01-01 18:02:05 +0000899 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000900}
901
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100902int db_subscriber_update(struct gsm_subscriber *subscr)
903{
904 char buf[32];
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100905 dbi_result result;
906
907 /* Copy the id to a string as queryf with %llu is failing */
908 sprintf(buf, "%llu", subscr->id);
909 result = dbi_conn_queryf(conn,
910 BASE_QUERY
911 "WHERE id = %s", buf);
912
913 if (!result) {
914 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
915 return -EIO;
916 }
917 if (!dbi_result_next_row(result)) {
918 DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
919 subscr->id);
920 dbi_result_free(result);
921 return -EIO;
922 }
923
924 db_set_from_query(subscr, result);
925 dbi_result_free(result);
926 get_equipment_by_subscr(subscr);
927
928 return 0;
929}
930
Harald Welte0b906d02009-12-24 11:21:42 +0100931int db_sync_subscriber(struct gsm_subscriber *subscriber)
932{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000933 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200934 char tmsi[14];
Harald Welte019d0162010-12-26 19:12:30 +0100935 char *q_tmsi, *q_name, *q_extension;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200936
Harald Welte019d0162010-12-26 19:12:30 +0100937 dbi_conn_quote_string_copy(conn,
938 subscriber->name, &q_name);
939 dbi_conn_quote_string_copy(conn,
940 subscriber->extension, &q_extension);
941
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200942 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
943 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200944 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200945 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200946 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200947 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200948 q_tmsi = strdup("NULL");
Harald Welte0b906d02009-12-24 11:21:42 +0100949
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200950 if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
951 result = dbi_conn_queryf(conn,
952 "UPDATE Subscriber "
953 "SET updated = datetime('now'), "
954 "name = %s, "
955 "extension = %s, "
956 "authorized = %i, "
957 "tmsi = %s, "
958 "lac = %i, "
959 "expire_lu = NULL "
960 "WHERE imsi = %s ",
961 q_name,
962 q_extension,
963 subscriber->authorized,
964 q_tmsi,
965 subscriber->lac,
966 subscriber->imsi);
967 } else {
968 result = dbi_conn_queryf(conn,
969 "UPDATE Subscriber "
970 "SET updated = datetime('now'), "
971 "name = %s, "
972 "extension = %s, "
973 "authorized = %i, "
974 "tmsi = %s, "
975 "lac = %i, "
976 "expire_lu = datetime(%i, 'unixepoch') "
977 "WHERE imsi = %s ",
978 q_name,
979 q_extension,
980 subscriber->authorized,
981 q_tmsi,
982 subscriber->lac,
983 (int) subscriber->expire_lu,
984 subscriber->imsi);
985 }
Harald Welte0b906d02009-12-24 11:21:42 +0100986
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200987 free(q_tmsi);
Harald Welte019d0162010-12-26 19:12:30 +0100988 free(q_name);
989 free(q_extension);
Harald Welte0b906d02009-12-24 11:21:42 +0100990
991 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +0100992 LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000993 return 1;
994 }
Harald Welte0b906d02009-12-24 11:21:42 +0100995
Jan Luebbe5c15c852008-12-27 15:59:25 +0000996 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +0100997
Jan Luebbe5c15c852008-12-27 15:59:25 +0000998 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000999}
1000
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001001int db_subscriber_delete(struct gsm_subscriber *subscr)
1002{
1003 dbi_result result;
1004
1005 result = dbi_conn_queryf(conn,
1006 "DELETE FROM AuthKeys WHERE subscriber_id=%llu",
1007 subscr->id);
1008 if (!result) {
1009 LOGP(DDB, LOGL_ERROR,
1010 "Failed to delete Authkeys for %llu\n", subscr->id);
1011 return -1;
1012 }
1013 dbi_result_free(result);
1014
1015 result = dbi_conn_queryf(conn,
1016 "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
1017 subscr->id);
1018 if (!result) {
1019 LOGP(DDB, LOGL_ERROR,
1020 "Failed to delete AuthLastTuples for %llu\n", subscr->id);
1021 return -1;
1022 }
1023 dbi_result_free(result);
1024
1025 result = dbi_conn_queryf(conn,
1026 "DELETE FROM AuthToken WHERE subscriber_id=%llu",
1027 subscr->id);
1028 if (!result) {
1029 LOGP(DDB, LOGL_ERROR,
1030 "Failed to delete AuthToken for %llu\n", subscr->id);
1031 return -1;
1032 }
1033 dbi_result_free(result);
1034
1035 result = dbi_conn_queryf(conn,
1036 "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
1037 subscr->id);
1038 if (!result) {
1039 LOGP(DDB, LOGL_ERROR,
1040 "Failed to delete EquipmentWatch for %llu\n", subscr->id);
1041 return -1;
1042 }
1043 dbi_result_free(result);
1044
1045 result = dbi_conn_queryf(conn,
Holger Hans Peter Freytherf242e7a2014-04-30 18:59:11 +02001046 "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
1047 subscr->extension, subscr->extension);
Holger Hans Peter Freyther2d99eeb2014-03-23 14:01:08 +01001048 if (!result) {
1049 LOGP(DDB, LOGL_ERROR,
1050 "Failed to delete SMS for %llu\n", subscr->id);
1051 return -1;
1052 }
1053 dbi_result_free(result);
1054
1055 result = dbi_conn_queryf(conn,
1056 "DELETE FROM VLR WHERE subscriber_id=%llu",
1057 subscr->id);
1058 if (!result) {
1059 LOGP(DDB, LOGL_ERROR,
1060 "Failed to delete VLR for %llu\n", subscr->id);
1061 return -1;
1062 }
1063 dbi_result_free(result);
1064
1065 result = dbi_conn_queryf(conn,
1066 "DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
1067 subscr->id);
1068 if (!result) {
1069 LOGP(DDB, LOGL_ERROR,
1070 "Failed to delete ApduBlobs for %llu\n", subscr->id);
1071 return -1;
1072 }
1073 dbi_result_free(result);
1074
1075 result = dbi_conn_queryf(conn,
1076 "DELETE FROM Subscriber WHERE id=%llu",
1077 subscr->id);
1078 if (!result) {
1079 LOGP(DDB, LOGL_ERROR,
1080 "Failed to delete Subscriber for %llu\n", subscr->id);
1081 return -1;
1082 }
1083 dbi_result_free(result);
1084
1085 return 0;
1086}
1087
Holger Hans Peter Freytherd883db02014-03-23 16:22:55 +01001088/**
1089 * List all the authorized and non-expired subscribers. The callback will
1090 * be called one by one. The subscr argument is not fully initialize and
1091 * subscr_get/subscr_put must not be called. The passed in pointer will be
1092 * deleted after the callback by the database call.
1093 */
1094int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
1095{
1096 dbi_result result;
1097
1098 result = dbi_conn_queryf(conn,
1099 "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
1100 if (!result) {
1101 LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
1102 return -1;
1103 }
1104
1105 while (dbi_result_next_row(result)) {
1106 struct gsm_subscriber *subscr;
1107
1108 subscr = subscr_alloc();
1109 subscr->id = dbi_result_get_ulonglong(result, "id");
1110 db_set_from_query(subscr, result);
1111 cb(subscr, closure);
1112 OSMO_ASSERT(subscr->use_count == 1);
1113 llist_del(&subscr->entry);
1114 talloc_free(subscr);
1115 }
1116
1117 dbi_result_free(result);
1118 return 0;
1119}
1120
Harald Weltec2e302d2009-07-05 14:08:13 +02001121int db_sync_equipment(struct gsm_equipment *equip)
1122{
1123 dbi_result result;
1124 unsigned char *cm2, *cm3;
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001125 char *q_imei;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001126 uint8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +02001127
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001128 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welteae1f1592009-12-24 11:39:14 +01001129 DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001130 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001131 if (equip->classmark2_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001132 DEBUGPC(DDB, ", classmark2=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001133 osmo_hexdump(equip->classmark2, equip->classmark2_len));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001134 if (equip->classmark3_len)
Harald Welteae1f1592009-12-24 11:39:14 +01001135 DEBUGPC(DDB, ", classmark3=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001136 osmo_hexdump(equip->classmark3, equip->classmark3_len));
Harald Welteae1f1592009-12-24 11:39:14 +01001137 DEBUGPC(DDB, "\n");
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001138
Harald Weltec2e302d2009-07-05 14:08:13 +02001139 dbi_conn_quote_binary_copy(conn, equip->classmark2,
1140 equip->classmark2_len, &cm2);
1141 dbi_conn_quote_binary_copy(conn, equip->classmark3,
1142 equip->classmark3_len, &cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001143 dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001144
1145 result = dbi_conn_queryf(conn,
1146 "UPDATE Equipment SET "
1147 "updated = datetime('now'), "
1148 "classmark1 = %u, "
1149 "classmark2 = %s, "
1150 "classmark3 = %s "
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001151 "WHERE imei = %s ",
1152 classmark1, cm2, cm3, q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001153
1154 free(cm2);
1155 free(cm3);
Holger Hans Peter Freytherf64a20f2010-12-26 20:04:49 +01001156 free(q_imei);
Harald Weltec2e302d2009-07-05 14:08:13 +02001157
1158 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001159 LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
Harald Weltec2e302d2009-07-05 14:08:13 +02001160 return -EIO;
1161 }
1162
1163 dbi_result_free(result);
1164 return 0;
1165}
1166
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001167int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
1168{
1169 dbi_result result;
1170
1171 result = dbi_conn_query(conn,
1172 "SELECT id "
1173 "FROM Subscriber "
1174 "WHERE lac != 0 AND "
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +02001175 "( expire_lu is NOT NULL "
1176 "AND expire_lu < datetime('now') ) "
Jan Luebbebfbdeec2012-12-27 00:27:16 +01001177 "LIMIT 1");
1178 if (!result) {
1179 LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
1180 return -EIO;
1181 }
1182
1183 while (dbi_result_next_row(result))
1184 callback(priv, dbi_result_get_ulonglong(result, "id"));
1185
1186 dbi_result_free(result);
1187 return 0;
1188}
1189
Harald Welte0b906d02009-12-24 11:21:42 +01001190int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
1191{
1192 dbi_result result = NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001193 char tmsi[14];
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +08001194 char *tmsi_quoted;
Harald Welte0b906d02009-12-24 11:21:42 +01001195
Jan Luebbe5c15c852008-12-27 15:59:25 +00001196 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001197 subscriber->tmsi = rand();
1198 if (subscriber->tmsi == GSM_RESERVED_TMSI)
1199 continue;
1200
1201 sprintf(tmsi, "%u", subscriber->tmsi);
1202 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001203 result = dbi_conn_queryf(conn,
1204 "SELECT * FROM Subscriber "
1205 "WHERE tmsi = %s ",
Harald Welte0b906d02009-12-24 11:21:42 +01001206 tmsi_quoted);
1207
Holger Freyther12aa50d2009-01-01 18:02:05 +00001208 free(tmsi_quoted);
Harald Welte0b906d02009-12-24 11:21:42 +01001209
1210 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001211 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1212 "while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +00001213 return 1;
1214 }
Harald Welte0b906d02009-12-24 11:21:42 +01001215 if (dbi_result_get_numrows(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001216 dbi_result_free(result);
1217 continue;
1218 }
1219 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +00001220 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001221 DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
1222 subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +00001223 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +00001224 }
1225 dbi_result_free(result);
1226 }
1227 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +00001228}
1229
Harald Welte0b906d02009-12-24 11:21:42 +01001230int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber)
1231{
1232 dbi_result result = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001233 uint32_t try;
Harald Welte0b906d02009-12-24 11:21:42 +01001234
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001235 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +02001236 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001237 result = dbi_conn_queryf(conn,
1238 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +02001239 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001240 try
1241 );
Harald Welte0b906d02009-12-24 11:21:42 +01001242 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001243 LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
1244 "while allocating new extension.\n");
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001245 return 1;
1246 }
1247 if (dbi_result_get_numrows(result)){
1248 dbi_result_free(result);
1249 continue;
1250 }
1251 if (!dbi_result_next_row(result)) {
1252 dbi_result_free(result);
1253 break;
1254 }
1255 dbi_result_free(result);
1256 }
1257 sprintf(subscriber->extension, "%i", try);
Harald Welteae1f1592009-12-24 11:39:14 +01001258 DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +02001259 return db_sync_subscriber(subscriber);
1260}
Jan Luebbe31bef492009-08-12 14:31:14 +02001261/*
1262 * try to allocate a new unique token for this subscriber and return it
1263 * via a parameter. if the subscriber already has a token, return
1264 * an error.
1265 */
1266
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001267int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
Harald Welte (local)3feef252009-08-13 13:26:11 +02001268{
1269 dbi_result result;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001270 uint32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +02001271
Jan Luebbe31bef492009-08-12 14:31:14 +02001272 for (;;) {
1273 try = rand();
1274 if (!try) /* 0 is an invalid token */
1275 continue;
1276 result = dbi_conn_queryf(conn,
1277 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001278 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
1279 subscriber->id, try);
1280 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001281 LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
1282 "while allocating new token.\n");
Jan Luebbe31bef492009-08-12 14:31:14 +02001283 return 1;
1284 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001285 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +02001286 dbi_result_free(result);
1287 continue;
1288 }
1289 if (!dbi_result_next_row(result)) {
1290 dbi_result_free(result);
1291 break;
1292 }
1293 dbi_result_free(result);
1294 }
1295 result = dbi_conn_queryf(conn,
1296 "INSERT INTO AuthToken "
1297 "(subscriber_id, created, token) "
1298 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +02001299 "(%llu, datetime('now'), \"%08X\") ",
1300 subscriber->id, try);
1301 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001302 LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
1303 "IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +02001304 return 1;
1305 }
Harald Welte (local)3feef252009-08-13 13:26:11 +02001306 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +02001307 *token = try;
Harald Welteae1f1592009-12-24 11:39:14 +01001308 DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
Harald Welte (local)3feef252009-08-13 13:26:11 +02001309
Jan Luebbe31bef492009-08-12 14:31:14 +02001310 return 0;
1311}
1312
Harald Welte0b906d02009-12-24 11:21:42 +01001313int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH])
1314{
Harald Welted409be72009-11-07 00:06:19 +09001315 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +00001316 dbi_result result;
1317
Harald Weltec2e302d2009-07-05 14:08:13 +02001318 strncpy(subscriber->equipment.imei, imei,
Alexander Chemeris8c169282013-10-04 02:42:25 +02001319 sizeof(subscriber->equipment.imei)-1);
Harald Weltec2e302d2009-07-05 14:08:13 +02001320
Jan Luebbefac25fc2008-12-27 18:04:34 +00001321 result = dbi_conn_queryf(conn,
1322 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001323 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +00001324 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +00001325 "(%s, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001326 imei);
1327 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001328 LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001329 return 1;
1330 }
Harald Welte0b906d02009-12-24 11:21:42 +01001331
Jan Luebbe391d86e2008-12-27 22:33:34 +00001332 equipment_id = 0;
1333 if (dbi_result_get_numrows_affected(result)) {
1334 equipment_id = dbi_conn_sequence_last(conn, NULL);
1335 }
Jan Luebbefac25fc2008-12-27 18:04:34 +00001336 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001337
1338 if (equipment_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001339 DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001340 else {
1341 result = dbi_conn_queryf(conn,
1342 "SELECT id FROM Equipment "
1343 "WHERE imei = %s ",
1344 imei
1345 );
Harald Welte0b906d02009-12-24 11:21:42 +01001346 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001347 LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001348 return 1;
1349 }
1350 if (!dbi_result_next_row(result)) {
Harald Welteae1f1592009-12-24 11:39:14 +01001351 LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001352 dbi_result_free(result);
1353 return 1;
1354 }
1355 equipment_id = dbi_result_get_ulonglong(result, "id");
1356 dbi_result_free(result);
1357 }
1358
1359 result = dbi_conn_queryf(conn,
1360 "INSERT OR IGNORE INTO EquipmentWatch "
1361 "(subscriber_id, equipment_id, created, updated) "
1362 "VALUES "
1363 "(%llu, %llu, datetime('now'), datetime('now')) ",
Harald Welte0b906d02009-12-24 11:21:42 +01001364 subscriber->id, equipment_id);
1365 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001366 LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001367 return 1;
1368 }
Harald Welte0b906d02009-12-24 11:21:42 +01001369
Jan Luebbe391d86e2008-12-27 22:33:34 +00001370 watch_id = 0;
Harald Welte0b906d02009-12-24 11:21:42 +01001371 if (dbi_result_get_numrows_affected(result))
Jan Luebbe391d86e2008-12-27 22:33:34 +00001372 watch_id = dbi_conn_sequence_last(conn, NULL);
Harald Welte0b906d02009-12-24 11:21:42 +01001373
Jan Luebbefac25fc2008-12-27 18:04:34 +00001374 dbi_result_free(result);
Harald Welte0b906d02009-12-24 11:21:42 +01001375 if (watch_id)
Harald Welteae1f1592009-12-24 11:39:14 +01001376 DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1377 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001378 else {
1379 result = dbi_conn_queryf(conn,
1380 "UPDATE EquipmentWatch "
1381 "SET updated = datetime('now') "
1382 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
Harald Welte0b906d02009-12-24 11:21:42 +01001383 subscriber->id, equipment_id);
1384 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001385 LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
Jan Luebbefac25fc2008-12-27 18:04:34 +00001386 return 1;
1387 }
1388 dbi_result_free(result);
Harald Welteae1f1592009-12-24 11:39:14 +01001389 DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
1390 equipment_id, subscriber->imsi, imei);
Jan Luebbefac25fc2008-12-27 18:04:34 +00001391 }
1392
1393 return 0;
1394}
1395
Harald Welte7e310b12009-03-30 20:56:32 +00001396/* store an [unsent] SMS to the database */
1397int db_sms_store(struct gsm_sms *sms)
1398{
1399 dbi_result result;
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001400 char *q_text, *q_daddr, *q_saddr;
Harald Welte76042182009-08-08 16:03:15 +02001401 unsigned char *q_udata;
1402 char *validity_timestamp = "2222-2-2";
1403
1404 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +00001405
1406 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Weltec0de14d2012-11-23 23:35:01 +01001407 dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001408 dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
Harald Welte76042182009-08-08 16:03:15 +02001409 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
1410 &q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001411
Harald Weltef3efc592009-07-27 20:11:35 +02001412 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +00001413 result = dbi_conn_queryf(conn,
1414 "INSERT INTO SMS "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001415 "(created, valid_until, "
Harald Welte76042182009-08-08 16:03:15 +02001416 "reply_path_req, status_rep_req, protocol_id, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001417 "data_coding_scheme, ud_hdr_ind, "
1418 "user_data, text, "
1419 "dest_addr, dest_ton, dest_npi, "
1420 "src_addr, src_ton, src_npi) VALUES "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001421 "(datetime('now'), %u, "
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001422 "%u, %u, %u, "
1423 "%u, %u, "
1424 "%s, %s, "
1425 "%s, %u, %u, "
1426 "%s, %u, %u)",
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001427 validity_timestamp,
Harald Welte76042182009-08-08 16:03:15 +02001428 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +02001429 sms->data_coding_scheme, sms->ud_hdr_ind,
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001430 q_udata, q_text,
1431 q_daddr, sms->dst.ton, sms->dst.npi,
1432 q_saddr, sms->src.ton, sms->src.npi);
Harald Welte7e310b12009-03-30 20:56:32 +00001433 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +02001434 free(q_udata);
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001435 free(q_daddr);
1436 free(q_saddr);
Harald Welte7e310b12009-03-30 20:56:32 +00001437
1438 if (!result)
1439 return -EIO;
1440
1441 dbi_result_free(result);
1442 return 0;
1443}
1444
Harald Welte2ebabca2009-08-09 19:05:21 +02001445static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +00001446{
Harald Welte76042182009-08-08 16:03:15 +02001447 struct gsm_sms *sms = sms_alloc();
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001448 const char *text, *daddr, *saddr;
Harald Welte76042182009-08-08 16:03:15 +02001449 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +00001450
Harald Welte76042182009-08-08 16:03:15 +02001451 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001452 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +00001453
Harald Weltebe3e3782009-07-05 14:06:41 +02001454 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +00001455
Harald Weltef3efc592009-07-27 20:11:35 +02001456 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +02001457 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001458 sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
1459 sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
1460 sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
1461 sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
1462 sms->data_coding_scheme = dbi_result_get_ulonglong(result,
Harald Weltef3efc592009-07-27 20:11:35 +02001463 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +02001464 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +02001465
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001466 sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
1467 sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
Harald Welte76042182009-08-08 16:03:15 +02001468 daddr = dbi_result_get_string(result, "dest_addr");
1469 if (daddr) {
Harald Weltec0de14d2012-11-23 23:35:01 +01001470 strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
1471 sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0';
Harald Welte76042182009-08-08 16:03:15 +02001472 }
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001473 sms->receiver = subscr_get_by_extension(net, sms->dst.addr);
Harald Welte76042182009-08-08 16:03:15 +02001474
Holger Hans Peter Freytherb115cb62014-07-03 14:00:30 +02001475 sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
1476 sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
Holger Hans Peter Freytherca3c2562013-10-08 03:17:30 +02001477 saddr = dbi_result_get_string(result, "src_addr");
1478 if (saddr) {
1479 strncpy(sms->src.addr, saddr, sizeof(sms->src.addr));
1480 sms->src.addr[sizeof(sms->src.addr)-1] = '\0';
1481 }
1482
Harald Welte76042182009-08-08 16:03:15 +02001483 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
1484 user_data = dbi_result_get_binary(result, "user_data");
1485 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001486 sms->user_data_len = (uint8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +02001487 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +02001488
1489 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +02001490 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +02001491 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +02001492 sms->text[sizeof(sms->text)-1] = '\0';
1493 }
Harald Welte2ebabca2009-08-09 19:05:21 +02001494 return sms;
1495}
1496
Holger Hans Peter Freyther812dad02010-12-24 23:18:31 +01001497struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
1498{
1499 dbi_result result;
1500 struct gsm_sms *sms;
1501
1502 result = dbi_conn_queryf(conn,
1503 "SELECT * FROM SMS WHERE SMS.id = %llu", id);
1504 if (!result)
1505 return NULL;
1506
1507 if (!dbi_result_next_row(result)) {
1508 dbi_result_free(result);
1509 return NULL;
1510 }
1511
1512 sms = sms_from_result(net, result);
1513
1514 dbi_result_free(result);
1515
1516 return sms;
1517}
1518
Harald Welte2ebabca2009-08-09 19:05:21 +02001519/* retrieve the next unsent SMS with ID >= min_id */
Holger Hans Peter Freytherb464fb42010-03-25 09:59:30 +01001520struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
Harald Welte2ebabca2009-08-09 19:05:21 +02001521{
1522 dbi_result result;
1523 struct gsm_sms *sms;
1524
1525 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001526 "SELECT SMS.* "
1527 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001528 "SMS.dest_addr = Subscriber.extension "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001529 "WHERE SMS.id >= %llu AND SMS.sent IS NULL "
1530 "AND Subscriber.lac > 0 "
1531 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001532 min_id);
1533 if (!result)
1534 return NULL;
1535
1536 if (!dbi_result_next_row(result)) {
1537 dbi_result_free(result);
1538 return NULL;
1539 }
1540
1541 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +00001542
1543 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +02001544
1545 return sms;
1546}
1547
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001548struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
1549 unsigned long long min_subscr_id,
1550 unsigned int failed)
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001551{
1552 dbi_result result;
1553 struct gsm_sms *sms;
1554
1555 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001556 "SELECT SMS.* "
1557 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001558 "SMS.dest_addr = Subscriber.extension "
1559 "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001560 "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001561 "ORDER BY Subscriber.id, SMS.id LIMIT 1",
Holger Hans Peter Freyther73b878a2010-12-25 00:33:40 +01001562 min_subscr_id, failed);
Sylvain Munautff1f19e2009-12-22 13:22:29 +01001563 if (!result)
1564 return NULL;
1565
1566 if (!dbi_result_next_row(result)) {
1567 dbi_result_free(result);
1568 return NULL;
1569 }
1570
1571 sms = sms_from_result(net, result);
1572
1573 dbi_result_free(result);
1574
1575 return sms;
1576}
1577
Sylvain Munautd5778fc2009-12-21 01:09:57 +01001578/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +02001579struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
1580{
1581 dbi_result result;
1582 struct gsm_sms *sms;
1583
1584 result = dbi_conn_queryf(conn,
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001585 "SELECT SMS.* "
1586 "FROM SMS JOIN Subscriber ON "
Alexander Chemerisca7ed2d2013-10-08 03:17:32 +02001587 "SMS.dest_addr = Subscriber.extension "
1588 "WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
Sylvain Munaut7a7d3642010-07-03 22:00:45 +02001589 "AND Subscriber.lac > 0 "
1590 "ORDER BY SMS.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +02001591 subscr->id);
1592 if (!result)
1593 return NULL;
1594
1595 if (!dbi_result_next_row(result)) {
1596 dbi_result_free(result);
1597 return NULL;
1598 }
1599
1600 sms = sms_from_result(subscr->net, result);
1601
1602 dbi_result_free(result);
1603
Harald Welte7e310b12009-03-30 20:56:32 +00001604 return sms;
1605}
1606
Alexander Chemeris1e77e3d2014-03-08 21:27:37 +01001607/* mark a given SMS as delivered */
1608int db_sms_mark_delivered(struct gsm_sms *sms)
Harald Welte7e310b12009-03-30 20:56:32 +00001609{
1610 dbi_result result;
1611
1612 result = dbi_conn_queryf(conn,
1613 "UPDATE SMS "
1614 "SET sent = datetime('now') "
1615 "WHERE id = %llu", sms->id);
1616 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001617 LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
Harald Welte7e310b12009-03-30 20:56:32 +00001618 return 1;
1619 }
1620
1621 dbi_result_free(result);
1622 return 0;
1623}
Harald Welte (local)db552c52009-08-15 20:15:14 +02001624
1625/* increase the number of attempted deliveries */
1626int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
1627{
1628 dbi_result result;
1629
1630 result = dbi_conn_queryf(conn,
1631 "UPDATE SMS "
1632 "SET deliver_attempts = deliver_attempts + 1 "
1633 "WHERE id = %llu", sms->id);
1634 if (!result) {
Harald Welteae1f1592009-12-24 11:39:14 +01001635 LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
1636 "SMS %llu.\n", sms->id);
Harald Welte (local)db552c52009-08-15 20:15:14 +02001637 return 1;
1638 }
1639
1640 dbi_result_free(result);
1641 return 0;
1642}
Harald Welte (local)026531e2009-08-16 10:40:10 +02001643
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001644int db_apdu_blob_store(struct gsm_subscriber *subscr,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001645 uint8_t apdu_id_flags, uint8_t len,
1646 uint8_t *apdu)
Harald Welte (local)026531e2009-08-16 10:40:10 +02001647{
1648 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +02001649 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +02001650
1651 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
1652
1653 result = dbi_conn_queryf(conn,
1654 "INSERT INTO ApduBlobs "
1655 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
1656 "(datetime('now'),%llu,%u,%s)",
1657 subscr->id, apdu_id_flags, q_apdu);
1658
1659 free(q_apdu);
1660
1661 if (!result)
1662 return -EIO;
1663
1664 dbi_result_free(result);
1665 return 0;
1666}
Harald Welteffa55a42009-12-22 19:07:32 +01001667
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001668int db_store_counter(struct osmo_counter *ctr)
Harald Welteffa55a42009-12-22 19:07:32 +01001669{
1670 dbi_result result;
1671 char *q_name;
1672
1673 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1674
1675 result = dbi_conn_queryf(conn,
1676 "INSERT INTO Counters "
1677 "(timestamp,name,value) VALUES "
1678 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1679
1680 free(q_name);
1681
1682 if (!result)
1683 return -EIO;
1684
1685 dbi_result_free(result);
1686 return 0;
1687}
Harald Weltef2b4cd72010-05-13 11:45:07 +02001688
1689static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
1690 char *q_prefix)
1691{
1692 dbi_result result;
1693 char *q_name;
1694
1695 dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
1696 &q_name);
1697
1698 result = dbi_conn_queryf(conn,
Harald Weltec1919862010-05-13 12:55:20 +02001699 "Insert INTO RateCounters "
Harald Welted94d6a02010-05-14 17:38:47 +02001700 "(timestamp,name,idx,value) VALUES "
Harald Weltec1919862010-05-13 12:55:20 +02001701 "(datetime('now'),%s.%s,%u,%"PRIu64")",
1702 q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001703
1704 free(q_name);
1705
1706 if (!result)
1707 return -EIO;
1708
1709 dbi_result_free(result);
1710 return 0;
1711}
1712
1713int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
1714{
1715 unsigned int i;
1716 char *q_prefix;
1717
Harald Weltec1919862010-05-13 12:55:20 +02001718 dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
Harald Weltef2b4cd72010-05-13 11:45:07 +02001719
1720 for (i = 0; i < ctrg->desc->num_ctr; i++)
1721 db_store_rate_ctr(ctrg, i, q_prefix);
1722
1723 free(q_prefix);
1724
1725 return 0;
1726}