blob: 11fb4dd4e334ad44611c80ad99d58824674aef28 [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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte7e310b12009-03-30 20:56:32 +000023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020024#include <openbsc/gsm_04_11.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000025#include <openbsc/db.h>
Harald Welte76042182009-08-08 16:03:15 +020026#include <openbsc/talloc.h>
Harald Welte (local)ee4410a2009-08-17 09:39:55 +020027#include <openbsc/debug.h>
Harald Welteffa55a42009-12-22 19:07:32 +010028#include <openbsc/statistics.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029
Holger Freytherbde36102008-12-28 22:51:39 +000030#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000031#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000032#include <stdlib.h>
33#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000034#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000035#include <dbi/dbi.h>
36
Holger Freytherbde36102008-12-28 22:51:39 +000037static char *db_basename = NULL;
38static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000039static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000040
Harald Welte7e310b12009-03-30 20:56:32 +000041static char *create_stmts[] = {
42 "CREATE TABLE IF NOT EXISTS Meta ("
43 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
44 "key TEXT UNIQUE NOT NULL, "
45 "value TEXT NOT NULL"
46 ")",
47 "INSERT OR IGNORE INTO Meta "
48 "(key, value) "
49 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020050 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000051 "CREATE TABLE IF NOT EXISTS Subscriber ("
52 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
53 "created TIMESTAMP NOT NULL, "
54 "updated TIMESTAMP NOT NULL, "
55 "imsi NUMERIC UNIQUE NOT NULL, "
56 "name TEXT, "
57 "extension TEXT UNIQUE, "
58 "authorized INTEGER NOT NULL DEFAULT 0, "
59 "tmsi TEXT UNIQUE, "
60 "lac INTEGER NOT NULL DEFAULT 0"
61 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020062 "CREATE TABLE IF NOT EXISTS AuthToken ("
63 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
64 "subscriber_id INTEGER UNIQUE NOT NULL, "
65 "created TIMESTAMP NOT NULL, "
66 "token TEXT UNIQUE NOT NULL"
67 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000068 "CREATE TABLE IF NOT EXISTS Equipment ("
69 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
70 "created TIMESTAMP NOT NULL, "
71 "updated TIMESTAMP NOT NULL, "
72 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020073 "classmark1 NUMERIC, "
74 "classmark2 BLOB, "
75 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000076 "imei NUMERIC UNIQUE NOT NULL"
77 ")",
78 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
79 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
80 "created TIMESTAMP NOT NULL, "
81 "updated TIMESTAMP NOT NULL, "
82 "subscriber_id NUMERIC NOT NULL, "
83 "equipment_id NUMERIC NOT NULL, "
84 "UNIQUE (subscriber_id, equipment_id) "
85 ")",
86 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020087 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000088 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
89 "created TIMESTAMP NOT NULL, "
90 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020091 "sender_id INTEGER NOT NULL, "
92 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020093 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020094 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020095 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020096 "reply_path_req INTEGER NOT NULL, "
97 "status_rep_req INTEGER NOT NULL, "
98 "protocol_id INTEGER NOT NULL, "
99 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +0200100 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200101 "dest_addr TEXT, "
102 "user_data BLOB, " /* TP-UD */
103 /* additional data, interpreted from SMS */
104 "header BLOB, " /* UD Header */
105 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000106 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000107 "CREATE TABLE IF NOT EXISTS VLR ("
108 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
109 "created TIMESTAMP NOT NULL, "
110 "updated TIMESTAMP NOT NULL, "
111 "subscriber_id NUMERIC UNIQUE NOT NULL, "
112 "last_bts NUMERIC NOT NULL "
113 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200114 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
115 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
116 "created TIMESTAMP NOT NULL, "
117 "apdu_id_flags INTEGER NOT NULL, "
118 "subscriber_id INTEGER NOT NULL, "
119 "apdu BLOB "
120 ")",
Harald Welteffa55a42009-12-22 19:07:32 +0100121 "CREATE TABLE IF NOT EXISTS Counters ("
122 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
123 "timestamp TIMESTAMP NOT NULL, "
Harald Weltef9a43c42009-12-22 21:40:42 +0100124 "value INTEGER NOT NULL, "
125 "name TEXT NOT NULL "
Harald Welte09f7ad02009-12-24 09:42:07 +0100126 ")",
Harald Welte3606cc52009-12-05 15:13:22 +0530127 "CREATE TABLE IF NOT EXISTS AuthKeys ("
128 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
129 "subscriber_id NUMERIC UNIQUE NOT NULL, "
130 "algorithm_id NUMERIC NOT NULL, "
131 "a3a8_ki BLOB "
132 ")",
133 "CREATE TABLE IF NOT EXISTS AuthTuples ("
134 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
135 "subscriber_id NUMERIC UNIQUE NOT NULL, "
136 "rand BLOB"
137 "sres BLOB"
138 "kc BLOB"
Harald Welteffa55a42009-12-22 19:07:32 +0100139 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000140};
141
Holger Freyther12aa50d2009-01-01 18:02:05 +0000142void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000143 const char* msg;
144 dbi_conn_error(conn, &msg);
145 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000146}
147
Harald Welted0b7b772009-08-09 19:03:42 +0200148static int check_db_revision(void)
149{
150 dbi_result result;
151 const char *rev;
152
153 result = dbi_conn_query(conn,
154 "SELECT value FROM Meta WHERE key='revision'");
155 if (!result)
156 return -EINVAL;
157
158 if (!dbi_result_next_row(result)) {
159 dbi_result_free(result);
160 return -EINVAL;
161 }
162 rev = dbi_result_get_string(result, "value");
163 if (!rev || atoi(rev) != 2) {
164 dbi_result_free(result);
165 return -EINVAL;
166 }
167
168 dbi_result_free(result);
169 return 0;
170}
171
Holger Freytherc7b86f92009-06-06 13:54:20 +0000172int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000173 dbi_initialize(NULL);
174 conn = dbi_conn_new("sqlite3");
175 if (conn==NULL) {
176 printf("DB: Failed to create connection.\n");
177 return 1;
178 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000179
Holger Freyther12aa50d2009-01-01 18:02:05 +0000180 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000181
Jan Luebbe5c15c852008-12-27 15:59:25 +0000182 /* MySQL
183 dbi_conn_set_option(conn, "host", "localhost");
184 dbi_conn_set_option(conn, "username", "your_name");
185 dbi_conn_set_option(conn, "password", "your_password");
186 dbi_conn_set_option(conn, "dbname", "your_dbname");
187 dbi_conn_set_option(conn, "encoding", "UTF-8");
188 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000189
Jan Luebbe5c15c852008-12-27 15:59:25 +0000190 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000191 db_basename = strdup(name);
192 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000193 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
194 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000195
Harald Welted0b7b772009-08-09 19:03:42 +0200196 if (dbi_conn_connect(conn) < 0)
197 goto out_err;
198
Jan Luebbe5c15c852008-12-27 15:59:25 +0000199 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200200
201out_err:
202 free(db_dirname);
203 free(db_basename);
204 db_dirname = db_basename = NULL;
205 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000206}
207
Harald Welted0b7b772009-08-09 19:03:42 +0200208
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000211 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000212
Harald Welte7e310b12009-03-30 20:56:32 +0000213 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
214 result = dbi_conn_query(conn, create_stmts[i]);
215 if (result==NULL) {
216 printf("DB: Failed to create some table.\n");
217 return 1;
218 }
219 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000220 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000221
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200222 if (check_db_revision() < 0) {
223 fprintf(stderr, "Database schema revision invalid, "
224 "please update your database schema\n");
225 return -1;
226 }
227
Jan Luebbe5c15c852008-12-27 15:59:25 +0000228 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000229}
230
231int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000232 dbi_conn_close(conn);
233 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000234
235 if (db_dirname)
236 free(db_dirname);
237 if (db_basename)
238 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000239 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000240}
241
Harald Welte9176bd42009-07-23 18:46:00 +0200242struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
243{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000244 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245 struct gsm_subscriber* subscr;
246
247 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200248 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000249 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000250 result = dbi_conn_queryf(conn,
251 "UPDATE Subscriber set updated = datetime('now') "
252 "WHERE imsi = %s " , imsi);
253 if (result==NULL) {
254 printf("DB: failed to update timestamp\n");
255 } else {
256 dbi_result_free(result);
257 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000259 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000260
261 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200262 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000263 if (!subscr)
264 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000265 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000266 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000267 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000268 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000269 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000270 imsi
271 );
272 if (result==NULL) {
273 printf("DB: Failed to create Subscriber by IMSI.\n");
274 }
Harald Welte9176bd42009-07-23 18:46:00 +0200275 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000276 subscr->id = dbi_conn_sequence_last(conn, NULL);
277 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000278 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000279 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200280 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000281 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000282}
283
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200284static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
285{
286 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200287 const char *string;
Harald Welte4669f3d2009-12-09 19:19:45 +0100288 unsigned char cm1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200289 const unsigned char *cm2, *cm3;
290 struct gsm_equipment *equip = &subscr->equipment;
291
292 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200293 "SELECT equipment.* FROM Equipment,EquipmentWatch "
294 "WHERE EquipmentWatch.equipment_id=Equipment.id "
295 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200296 "ORDER BY updated DESC", subscr->id);
297 if (!result)
298 return -EIO;
299
300 if (!dbi_result_next_row(result)) {
301 dbi_result_free(result);
302 return -ENOENT;
303 }
304
305 equip->id = dbi_result_get_ulonglong(result, "id");
306
307 string = dbi_result_get_string(result, "imei");
308 if (string)
309 strncpy(equip->imei, string, sizeof(equip->imei));
310
311 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
312 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
313
314 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
315 cm2 = dbi_result_get_binary(result, "classmark2");
316 if (equip->classmark2_len > sizeof(equip->classmark2))
317 equip->classmark2_len = sizeof(equip->classmark2);
318 memcpy(equip->classmark2, cm2, equip->classmark2_len);
319
320 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
321 cm3 = dbi_result_get_binary(result, "classmark3");
322 if (equip->classmark3_len > sizeof(equip->classmark3))
323 equip->classmark3_len = sizeof(equip->classmark3);
324 memcpy(equip->classmark3, cm3, equip->classmark3_len);
325
326 dbi_result_free(result);
327
328 return 0;
329}
Harald Welte3606cc52009-12-05 15:13:22 +0530330
331int get_authinfo_by_subscr(struct gsm_auth_info *ainfo,
332 struct gsm_subscriber *subscr)
333{
334 dbi_result result;
335 const unsigned char *a3a8_ki;
336
337 result = dbi_conn_queryf(conn,
338 "SELECT * FROM AuthKeys WHERE subscriber_id=%u",
339 subscr->id);
340 if (!result)
341 return -EIO;
342
343 if (!dbi_result_next_row(result)) {
344 dbi_result_free(result);
345 return -ENOENT;
346 }
347
348 ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
349 ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
350 a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
351 if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki_len))
352 ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki_len);
353 memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
354
355 dbi_result_free(result);
356
357 return 0;
358}
359
360int get_authtuple_by_subscr(struct gsm_auth_tuple *atuple,
361 struct gsm_subscriber *subscr)
362{
363 dbi_result result;
364 int len;
365 const unsigned char *blob;
366
367 result = dbi_conn_queryf(conn,
368 "SELECT * FROM AuthTuples WHERE subscriber_id=%u",
369 subscr->id);
370 if (!result)
371 return -EIO;
372
373 if (!dbi_result_next_row(result)) {
374 dbi_result_free(result);
375 return -ENOENT;
376 }
377
378 memset(atuple, 0, sizeof(atuple));
379
380 len = dbi_result_get_field_length(result, "rand");
381 if (len != sizeof(atuple->rand))
382 goto err_size;
383
384 blob = dbi_result_get_binary(result, "rand");
385 memcpy(atuple->rand, blob, len);
386
387 len = dbi_result_get_field_length(result, "sres");
388 if (len != sizeof(atuple->sres))
389 goto err_size;
390
391 blob = dbi_result_get_binary(result, "sres");
392 memcpy(atuple->sres, blob, len);
393
394 len = dbi_result_get_field_length(result, "kc");
395 if (len != sizeof(atuple->kc))
396 goto err_size;
397
398 blob = dbi_result_get_binary(result, "kc");
399 memcpy(atuple->kc, blob, len);
400
401 dbi_result_free(result);
402
403 return 0;
404
405err_size:
406 dbi_result_free(result);
407 return -EIO;
408}
409
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200410#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200411struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
412 enum gsm_subscriber_field field,
413 const char *id)
414{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000415 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000416 const char *string;
417 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000418 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000419
Jan Luebbe5c15c852008-12-27 15:59:25 +0000420 switch (field) {
421 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000422 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000423 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200424 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000425 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000426 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000427 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000428 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000429 break;
430 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000431 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000432 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200433 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000434 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000435 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000436 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000437 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000438 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000439 case GSM_SUBSCRIBER_EXTENSION:
440 dbi_conn_quote_string_copy(conn, id, &quoted);
441 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200442 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000443 "WHERE extension = %s ",
444 quoted
445 );
446 free(quoted);
447 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200448 case GSM_SUBSCRIBER_ID:
449 dbi_conn_quote_string_copy(conn, id, &quoted);
450 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200451 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200452 "WHERE id = %s ", quoted);
453 free(quoted);
454 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000455 default:
456 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000457 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000458 }
459 if (result==NULL) {
460 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000461 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000462 }
463 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000464 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
465 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000466 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000467 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000468 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000469
470 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200471 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000472 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000473 string = dbi_result_get_string(result, "imsi");
474 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000475 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000476
477 string = dbi_result_get_string(result, "tmsi");
478 if (string)
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200479 subscr->tmsi = tmsi_from_string(string);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000480
481 string = dbi_result_get_string(result, "name");
482 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000483 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000484
485 string = dbi_result_get_string(result, "extension");
486 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000487 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000488
Holger Freyther12aa50d2009-01-01 18:02:05 +0000489 subscr->lac = dbi_result_get_uint(result, "lac");
490 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200491 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
Holger Freyther91754472009-06-09 08:52:41 +0000492 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000493 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000494 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200495
496 get_equipment_by_subscr(subscr);
497
Holger Freyther12aa50d2009-01-01 18:02:05 +0000498 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000499}
500
Holger Freyther12aa50d2009-01-01 18:02:05 +0000501int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000502 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200503 char tmsi[14];
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200504 char *q_tmsi;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200505
506 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
507 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200508 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200509 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200510 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200511 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200512 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000513 result = dbi_conn_queryf(conn,
514 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000515 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200516 "name = '%s', "
517 "extension = '%s', "
518 "authorized = %i, "
519 "tmsi = %s, "
520 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000521 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200522 subscriber->name,
523 subscriber->extension,
524 subscriber->authorized,
525 q_tmsi,
526 subscriber->lac,
527 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000528 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200529 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000530 if (result==NULL) {
531 printf("DB: Failed to update Subscriber (by IMSI).\n");
532 return 1;
533 }
534 dbi_result_free(result);
535 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000536}
537
Harald Weltec2e302d2009-07-05 14:08:13 +0200538int db_sync_equipment(struct gsm_equipment *equip)
539{
540 dbi_result result;
541 unsigned char *cm2, *cm3;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200542 u_int8_t classmark1;
Harald Weltec2e302d2009-07-05 14:08:13 +0200543
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200544 memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200545 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200546 equip->imei, classmark1);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200547 if (equip->classmark2_len)
548 printf(", classmark2=%s",
549 hexdump(equip->classmark2, equip->classmark2_len));
550 if (equip->classmark3_len)
551 printf(", classmark3=%s",
552 hexdump(equip->classmark3, equip->classmark3_len));
553 printf("\n");
554
Harald Weltec2e302d2009-07-05 14:08:13 +0200555 dbi_conn_quote_binary_copy(conn, equip->classmark2,
556 equip->classmark2_len, &cm2);
557 dbi_conn_quote_binary_copy(conn, equip->classmark3,
558 equip->classmark3_len, &cm3);
559
560 result = dbi_conn_queryf(conn,
561 "UPDATE Equipment SET "
562 "updated = datetime('now'), "
563 "classmark1 = %u, "
564 "classmark2 = %s, "
565 "classmark3 = %s "
566 "WHERE imei = '%s' ",
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200567 classmark1, cm2, cm3, equip->imei);
Harald Weltec2e302d2009-07-05 14:08:13 +0200568
569 free(cm2);
570 free(cm3);
571
572 if (!result) {
573 printf("DB: Failed to update Equipment\n");
574 return -EIO;
575 }
576
577 dbi_result_free(result);
578 return 0;
579}
580
Jan Luebbe5c15c852008-12-27 15:59:25 +0000581int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000582 dbi_result result=NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200583 char tmsi[14];
Jan Luebbe5c15c852008-12-27 15:59:25 +0000584 char* tmsi_quoted;
585 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200586 subscriber->tmsi = rand();
587 if (subscriber->tmsi == GSM_RESERVED_TMSI)
588 continue;
589
590 sprintf(tmsi, "%u", subscriber->tmsi);
591 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000592 result = dbi_conn_queryf(conn,
593 "SELECT * FROM Subscriber "
594 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000595 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000596 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000597 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000598 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000599 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000600 return 1;
601 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000602 if (dbi_result_get_numrows(result)){
603 dbi_result_free(result);
604 continue;
605 }
606 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000607 dbi_result_free(result);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200608 printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000609 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000610 }
611 dbi_result_free(result);
612 }
613 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000614}
615
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200616int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
617 dbi_result result=NULL;
618 u_int32_t try;
619 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200620 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200621 result = dbi_conn_queryf(conn,
622 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200623 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200624 try
625 );
626 if (result==NULL) {
627 printf("DB: Failed to query Subscriber while allocating new extension.\n");
628 return 1;
629 }
630 if (dbi_result_get_numrows(result)){
631 dbi_result_free(result);
632 continue;
633 }
634 if (!dbi_result_next_row(result)) {
635 dbi_result_free(result);
636 break;
637 }
638 dbi_result_free(result);
639 }
640 sprintf(subscriber->extension, "%i", try);
641 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
642 return db_sync_subscriber(subscriber);
643}
Jan Luebbe31bef492009-08-12 14:31:14 +0200644/*
645 * try to allocate a new unique token for this subscriber and return it
646 * via a parameter. if the subscriber already has a token, return
647 * an error.
648 */
649
Harald Welte (local)3feef252009-08-13 13:26:11 +0200650int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
651{
652 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200653 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200654
Jan Luebbe31bef492009-08-12 14:31:14 +0200655 for (;;) {
656 try = rand();
657 if (!try) /* 0 is an invalid token */
658 continue;
659 result = dbi_conn_queryf(conn,
660 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200661 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
662 subscriber->id, try);
663 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200664 printf("DB: Failed to query AuthToken while allocating new token.\n");
665 return 1;
666 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200667 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200668 dbi_result_free(result);
669 continue;
670 }
671 if (!dbi_result_next_row(result)) {
672 dbi_result_free(result);
673 break;
674 }
675 dbi_result_free(result);
676 }
677 result = dbi_conn_queryf(conn,
678 "INSERT INTO AuthToken "
679 "(subscriber_id, created, token) "
680 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200681 "(%llu, datetime('now'), \"%08X\") ",
682 subscriber->id, try);
683 if (!result) {
684 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200685 return 1;
686 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200687 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200688 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200689 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
690
Jan Luebbe31bef492009-08-12 14:31:14 +0200691 return 0;
692}
693
Jan Luebbefac25fc2008-12-27 18:04:34 +0000694int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
Harald Welted409be72009-11-07 00:06:19 +0900695 unsigned long long equipment_id, watch_id;
Jan Luebbefac25fc2008-12-27 18:04:34 +0000696 dbi_result result;
697
Harald Weltec2e302d2009-07-05 14:08:13 +0200698 strncpy(subscriber->equipment.imei, imei,
699 sizeof(subscriber->equipment.imei)-1),
700
Jan Luebbefac25fc2008-12-27 18:04:34 +0000701 result = dbi_conn_queryf(conn,
702 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000703 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000704 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000705 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000706 imei
707 );
708 if (result==NULL) {
709 printf("DB: Failed to create Equipment by IMEI.\n");
710 return 1;
711 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000712 equipment_id = 0;
713 if (dbi_result_get_numrows_affected(result)) {
714 equipment_id = dbi_conn_sequence_last(conn, NULL);
715 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000716 dbi_result_free(result);
717 if (equipment_id) {
718 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
719 }
720 else {
721 result = dbi_conn_queryf(conn,
722 "SELECT id FROM Equipment "
723 "WHERE imei = %s ",
724 imei
725 );
726 if (result==NULL) {
727 printf("DB: Failed to query Equipment by IMEI.\n");
728 return 1;
729 }
730 if (!dbi_result_next_row(result)) {
731 printf("DB: Failed to find the Equipment.\n");
732 dbi_result_free(result);
733 return 1;
734 }
735 equipment_id = dbi_result_get_ulonglong(result, "id");
736 dbi_result_free(result);
737 }
738
739 result = dbi_conn_queryf(conn,
740 "INSERT OR IGNORE INTO EquipmentWatch "
741 "(subscriber_id, equipment_id, created, updated) "
742 "VALUES "
743 "(%llu, %llu, datetime('now'), datetime('now')) ",
744 subscriber->id, equipment_id
745 );
746 if (result==NULL) {
747 printf("DB: Failed to create EquipmentWatch.\n");
748 return 1;
749 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000750 watch_id = 0;
751 if (dbi_result_get_numrows_affected(result)) {
752 watch_id = dbi_conn_sequence_last(conn, NULL);
753 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000754 dbi_result_free(result);
755 if (watch_id) {
756 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
757 }
758 else {
759 result = dbi_conn_queryf(conn,
760 "UPDATE EquipmentWatch "
761 "SET updated = datetime('now') "
762 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
763 subscriber->id, equipment_id
764 );
765 if (result==NULL) {
766 printf("DB: Failed to update EquipmentWatch.\n");
767 return 1;
768 }
769 dbi_result_free(result);
770 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
771 }
772
773 return 0;
774}
775
Harald Welte7e310b12009-03-30 20:56:32 +0000776/* store an [unsent] SMS to the database */
777int db_sms_store(struct gsm_sms *sms)
778{
779 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200780 char *q_text, *q_daddr;
781 unsigned char *q_udata;
782 char *validity_timestamp = "2222-2-2";
783
784 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000785
786 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200787 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
788 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
789 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200790 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000791 result = dbi_conn_queryf(conn,
792 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200793 "(created, sender_id, receiver_id, valid_until, "
794 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200795 "data_coding_scheme, ud_hdr_ind, dest_addr, "
796 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200797 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200798 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000799 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200800 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
801 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200802 sms->data_coding_scheme, sms->ud_hdr_ind,
803 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000804 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200805 free(q_daddr);
806 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000807
808 if (!result)
809 return -EIO;
810
811 dbi_result_free(result);
812 return 0;
813}
814
Harald Welte2ebabca2009-08-09 19:05:21 +0200815static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000816{
Harald Welte76042182009-08-08 16:03:15 +0200817 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200818 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200819 const char *text, *daddr;
820 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000821
Harald Welte76042182009-08-08 16:03:15 +0200822 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000823 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000824
Harald Weltebe3e3782009-07-05 14:06:41 +0200825 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000826
Harald Weltebe3e3782009-07-05 14:06:41 +0200827 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200828 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200829
830 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200831 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200832
Harald Weltef3efc592009-07-27 20:11:35 +0200833 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200834 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
835 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
836 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
837 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
838 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
839 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200840 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200841 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200842
Harald Welte76042182009-08-08 16:03:15 +0200843 daddr = dbi_result_get_string(result, "dest_addr");
844 if (daddr) {
845 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
846 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
847 }
848
849 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
850 user_data = dbi_result_get_binary(result, "user_data");
851 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200852 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200853 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200854
855 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200856 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200857 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200858 sms->text[sizeof(sms->text)-1] = '\0';
859 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200860 return sms;
861}
862
863/* retrieve the next unsent SMS with ID >= min_id */
864struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
865{
866 dbi_result result;
867 struct gsm_sms *sms;
868
869 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200870 "SELECT * FROM SMS,Subscriber "
871 "WHERE sms.id >= %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100872 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200873 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100874 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200875 min_id);
876 if (!result)
877 return NULL;
878
879 if (!dbi_result_next_row(result)) {
880 dbi_result_free(result);
881 return NULL;
882 }
883
884 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000885
886 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200887
888 return sms;
889}
890
Sylvain Munautff1f19e2009-12-22 13:22:29 +0100891struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, int min_subscr_id)
892{
893 dbi_result result;
894 struct gsm_sms *sms;
895
896 result = dbi_conn_queryf(conn,
897 "SELECT * FROM SMS,Subscriber "
898 "WHERE sms.receiver_id >= %llu AND sms.sent is NULL "
899 "AND sms.receiver_id = subscriber.id "
900 "AND subscriber.lac > 0 "
901 "ORDER BY sms.receiver_id, id LIMIT 1",
902 min_subscr_id);
903 if (!result)
904 return NULL;
905
906 if (!dbi_result_next_row(result)) {
907 dbi_result_free(result);
908 return NULL;
909 }
910
911 sms = sms_from_result(net, result);
912
913 dbi_result_free(result);
914
915 return sms;
916}
917
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100918/* retrieve the next unsent SMS for a given subscriber */
Harald Welte2ebabca2009-08-09 19:05:21 +0200919struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
920{
921 dbi_result result;
922 struct gsm_sms *sms;
923
924 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200925 "SELECT * FROM SMS,Subscriber "
926 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100927 "AND sms.receiver_id = subscriber.id "
Harald Welte (local)db552c52009-08-15 20:15:14 +0200928 "AND subscriber.lac > 0 "
Sylvain Munautd5778fc2009-12-21 01:09:57 +0100929 "ORDER BY sms.id LIMIT 1",
Harald Welte2ebabca2009-08-09 19:05:21 +0200930 subscr->id);
931 if (!result)
932 return NULL;
933
934 if (!dbi_result_next_row(result)) {
935 dbi_result_free(result);
936 return NULL;
937 }
938
939 sms = sms_from_result(subscr->net, result);
940
941 dbi_result_free(result);
942
Harald Welte7e310b12009-03-30 20:56:32 +0000943 return sms;
944}
945
946/* mark a given SMS as read */
947int db_sms_mark_sent(struct gsm_sms *sms)
948{
949 dbi_result result;
950
951 result = dbi_conn_queryf(conn,
952 "UPDATE SMS "
953 "SET sent = datetime('now') "
954 "WHERE id = %llu", sms->id);
955 if (!result) {
956 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
957 return 1;
958 }
959
960 dbi_result_free(result);
961 return 0;
962}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200963
964/* increase the number of attempted deliveries */
965int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
966{
967 dbi_result result;
968
969 result = dbi_conn_queryf(conn,
970 "UPDATE SMS "
971 "SET deliver_attempts = deliver_attempts + 1 "
972 "WHERE id = %llu", sms->id);
973 if (!result) {
974 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
975 return 1;
976 }
977
978 dbi_result_free(result);
979 return 0;
980}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200981
982int db_apdu_blob_store(struct gsm_subscriber *subscr,
983 u_int8_t apdu_id_flags, u_int8_t len,
984 u_int8_t *apdu)
985{
986 dbi_result result;
Holger Hans Peter Freyther2657abf2009-10-22 15:34:37 +0200987 unsigned char *q_apdu;
Harald Welte (local)026531e2009-08-16 10:40:10 +0200988
989 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
990
991 result = dbi_conn_queryf(conn,
992 "INSERT INTO ApduBlobs "
993 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
994 "(datetime('now'),%llu,%u,%s)",
995 subscr->id, apdu_id_flags, q_apdu);
996
997 free(q_apdu);
998
999 if (!result)
1000 return -EIO;
1001
1002 dbi_result_free(result);
1003 return 0;
1004}
Harald Welteffa55a42009-12-22 19:07:32 +01001005
1006int db_store_counter(struct counter *ctr)
1007{
1008 dbi_result result;
1009 char *q_name;
1010
1011 dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
1012
1013 result = dbi_conn_queryf(conn,
1014 "INSERT INTO Counters "
1015 "(timestamp,name,value) VALUES "
1016 "(datetime('now'),%s,%lu)", q_name, ctr->value);
1017
1018 free(q_name);
1019
1020 if (!result)
1021 return -EIO;
1022
1023 dbi_result_free(result);
1024 return 0;
1025}