blob: 16a7f6ad7b9bcf25a4f4469411f57627748a8fd8 [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>
Jan Luebbe7398eb92008-12-27 00:45:41 +000027
Holger Freytherbde36102008-12-28 22:51:39 +000028#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000029#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000030#include <stdlib.h>
31#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000032#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000033#include <dbi/dbi.h>
34
Holger Freytherbde36102008-12-28 22:51:39 +000035static char *db_basename = NULL;
36static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000037static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000038
Harald Welte7e310b12009-03-30 20:56:32 +000039static char *create_stmts[] = {
40 "CREATE TABLE IF NOT EXISTS Meta ("
41 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
42 "key TEXT UNIQUE NOT NULL, "
43 "value TEXT NOT NULL"
44 ")",
45 "INSERT OR IGNORE INTO Meta "
46 "(key, value) "
47 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020048 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000049 "CREATE TABLE IF NOT EXISTS Subscriber ("
50 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
51 "created TIMESTAMP NOT NULL, "
52 "updated TIMESTAMP NOT NULL, "
53 "imsi NUMERIC UNIQUE NOT NULL, "
54 "name TEXT, "
55 "extension TEXT UNIQUE, "
56 "authorized INTEGER NOT NULL DEFAULT 0, "
57 "tmsi TEXT UNIQUE, "
58 "lac INTEGER NOT NULL DEFAULT 0"
59 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020060 "CREATE TABLE IF NOT EXISTS AuthToken ("
61 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
62 "subscriber_id INTEGER UNIQUE NOT NULL, "
63 "created TIMESTAMP NOT NULL, "
64 "token TEXT UNIQUE NOT NULL"
65 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000066 "CREATE TABLE IF NOT EXISTS Equipment ("
67 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
68 "created TIMESTAMP NOT NULL, "
69 "updated TIMESTAMP NOT NULL, "
70 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020071 "classmark1 NUMERIC, "
72 "classmark2 BLOB, "
73 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000074 "imei NUMERIC UNIQUE NOT NULL"
75 ")",
76 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
77 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
78 "created TIMESTAMP NOT NULL, "
79 "updated TIMESTAMP NOT NULL, "
80 "subscriber_id NUMERIC NOT NULL, "
81 "equipment_id NUMERIC NOT NULL, "
82 "UNIQUE (subscriber_id, equipment_id) "
83 ")",
84 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020085 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000086 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
87 "created TIMESTAMP NOT NULL, "
88 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020089 "sender_id INTEGER NOT NULL, "
90 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020091 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020092 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020093 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020094 "reply_path_req INTEGER NOT NULL, "
95 "status_rep_req INTEGER NOT NULL, "
96 "protocol_id INTEGER NOT NULL, "
97 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020098 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +020099 "dest_addr TEXT, "
100 "user_data BLOB, " /* TP-UD */
101 /* additional data, interpreted from SMS */
102 "header BLOB, " /* UD Header */
103 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000104 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000105 "CREATE TABLE IF NOT EXISTS VLR ("
106 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
107 "created TIMESTAMP NOT NULL, "
108 "updated TIMESTAMP NOT NULL, "
109 "subscriber_id NUMERIC UNIQUE NOT NULL, "
110 "last_bts NUMERIC NOT NULL "
111 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200112 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
113 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
114 "created TIMESTAMP NOT NULL, "
115 "apdu_id_flags INTEGER NOT NULL, "
116 "subscriber_id INTEGER NOT NULL, "
117 "apdu BLOB "
118 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000119};
120
Holger Freyther12aa50d2009-01-01 18:02:05 +0000121void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000122 const char* msg;
123 dbi_conn_error(conn, &msg);
124 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000125}
126
Harald Welted0b7b772009-08-09 19:03:42 +0200127static int check_db_revision(void)
128{
129 dbi_result result;
130 const char *rev;
131
132 result = dbi_conn_query(conn,
133 "SELECT value FROM Meta WHERE key='revision'");
134 if (!result)
135 return -EINVAL;
136
137 if (!dbi_result_next_row(result)) {
138 dbi_result_free(result);
139 return -EINVAL;
140 }
141 rev = dbi_result_get_string(result, "value");
142 if (!rev || atoi(rev) != 2) {
143 dbi_result_free(result);
144 return -EINVAL;
145 }
146
147 dbi_result_free(result);
148 return 0;
149}
150
Holger Freytherc7b86f92009-06-06 13:54:20 +0000151int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000152 dbi_initialize(NULL);
153 conn = dbi_conn_new("sqlite3");
154 if (conn==NULL) {
155 printf("DB: Failed to create connection.\n");
156 return 1;
157 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000158
Holger Freyther12aa50d2009-01-01 18:02:05 +0000159 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000160
Jan Luebbe5c15c852008-12-27 15:59:25 +0000161 /* MySQL
162 dbi_conn_set_option(conn, "host", "localhost");
163 dbi_conn_set_option(conn, "username", "your_name");
164 dbi_conn_set_option(conn, "password", "your_password");
165 dbi_conn_set_option(conn, "dbname", "your_dbname");
166 dbi_conn_set_option(conn, "encoding", "UTF-8");
167 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000168
Jan Luebbe5c15c852008-12-27 15:59:25 +0000169 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000170 db_basename = strdup(name);
171 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000172 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
173 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000174
Harald Welted0b7b772009-08-09 19:03:42 +0200175 if (dbi_conn_connect(conn) < 0)
176 goto out_err;
177
Jan Luebbe5c15c852008-12-27 15:59:25 +0000178 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200179
180out_err:
181 free(db_dirname);
182 free(db_basename);
183 db_dirname = db_basename = NULL;
184 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000185}
186
Harald Welted0b7b772009-08-09 19:03:42 +0200187
Jan Luebbe7398eb92008-12-27 00:45:41 +0000188int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000189 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000190 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000191
Harald Welte7e310b12009-03-30 20:56:32 +0000192 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
193 result = dbi_conn_query(conn, create_stmts[i]);
194 if (result==NULL) {
195 printf("DB: Failed to create some table.\n");
196 return 1;
197 }
198 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000199 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000200
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200201 if (check_db_revision() < 0) {
202 fprintf(stderr, "Database schema revision invalid, "
203 "please update your database schema\n");
204 return -1;
205 }
206
Jan Luebbe5c15c852008-12-27 15:59:25 +0000207 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000208}
209
210int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000211 dbi_conn_close(conn);
212 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000213
214 if (db_dirname)
215 free(db_dirname);
216 if (db_basename)
217 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000218 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000219}
220
Harald Welte9176bd42009-07-23 18:46:00 +0200221struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
222{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000223 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000224 struct gsm_subscriber* subscr;
225
226 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200227 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000228 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000229 result = dbi_conn_queryf(conn,
230 "UPDATE Subscriber set updated = datetime('now') "
231 "WHERE imsi = %s " , imsi);
232 if (result==NULL) {
233 printf("DB: failed to update timestamp\n");
234 } else {
235 dbi_result_free(result);
236 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000237 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000238 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000239
240 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200241 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000242 if (!subscr)
243 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000244 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000245 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000246 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000247 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000248 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000249 imsi
250 );
251 if (result==NULL) {
252 printf("DB: Failed to create Subscriber by IMSI.\n");
253 }
Harald Welte9176bd42009-07-23 18:46:00 +0200254 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 subscr->id = dbi_conn_sequence_last(conn, NULL);
256 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000257 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200259 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000260 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000261}
262
Harald Welte9176bd42009-07-23 18:46:00 +0200263struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
264 enum gsm_subscriber_field field,
265 const char *id)
266{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000267 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000268 const char *string;
269 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000271
Jan Luebbe5c15c852008-12-27 15:59:25 +0000272 switch (field) {
273 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000274 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000275 result = dbi_conn_queryf(conn,
276 "SELECT * FROM Subscriber "
277 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000278 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000279 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000280 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000281 break;
282 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000283 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000284 result = dbi_conn_queryf(conn,
285 "SELECT * FROM Subscriber "
286 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000287 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000288 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000289 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000290 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000291 case GSM_SUBSCRIBER_EXTENSION:
292 dbi_conn_quote_string_copy(conn, id, &quoted);
293 result = dbi_conn_queryf(conn,
294 "SELECT * FROM Subscriber "
295 "WHERE extension = %s ",
296 quoted
297 );
298 free(quoted);
299 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200300 case GSM_SUBSCRIBER_ID:
301 dbi_conn_quote_string_copy(conn, id, &quoted);
302 result = dbi_conn_queryf(conn,
303 "SELECT * FROM Subscriber "
304 "WHERE id = %s ", quoted);
305 free(quoted);
306 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000307 default:
308 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000309 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000310 }
311 if (result==NULL) {
312 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000313 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000314 }
315 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000316 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
317 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000318 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000319 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000320 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000321
322 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200323 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000324 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000325 string = dbi_result_get_string(result, "imsi");
326 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000327 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000328
329 string = dbi_result_get_string(result, "tmsi");
330 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000331 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000332
333 string = dbi_result_get_string(result, "name");
334 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000335 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000336
337 string = dbi_result_get_string(result, "extension");
338 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000339 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000340
Holger Freyther12aa50d2009-01-01 18:02:05 +0000341 subscr->lac = dbi_result_get_uint(result, "lac");
342 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000343 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
344 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000345 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000346 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000347 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000348}
349
Holger Freyther12aa50d2009-01-01 18:02:05 +0000350int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000351 dbi_result result;
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200352 char *q_tmsi;
353 if (subscriber->tmsi[0])
354 dbi_conn_quote_string_copy(conn,
355 subscriber->tmsi,
356 &q_tmsi);
357 else
358 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000359 result = dbi_conn_queryf(conn,
360 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000361 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200362 "name = '%s', "
363 "extension = '%s', "
364 "authorized = %i, "
365 "tmsi = %s, "
366 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000367 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200368 subscriber->name,
369 subscriber->extension,
370 subscriber->authorized,
371 q_tmsi,
372 subscriber->lac,
373 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000374 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200375 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000376 if (result==NULL) {
377 printf("DB: Failed to update Subscriber (by IMSI).\n");
378 return 1;
379 }
380 dbi_result_free(result);
381 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000382}
383
Harald Weltec2e302d2009-07-05 14:08:13 +0200384int db_sync_equipment(struct gsm_equipment *equip)
385{
386 dbi_result result;
387 unsigned char *cm2, *cm3;
388
389 dbi_conn_quote_binary_copy(conn, equip->classmark2,
390 equip->classmark2_len, &cm2);
391 dbi_conn_quote_binary_copy(conn, equip->classmark3,
392 equip->classmark3_len, &cm3);
393
394 result = dbi_conn_queryf(conn,
395 "UPDATE Equipment SET "
396 "updated = datetime('now'), "
397 "classmark1 = %u, "
398 "classmark2 = %s, "
399 "classmark3 = %s "
400 "WHERE imei = '%s' ",
401 equip->classmark1, cm2, cm3, equip->imei);
402
403 free(cm2);
404 free(cm3);
405
406 if (!result) {
407 printf("DB: Failed to update Equipment\n");
408 return -EIO;
409 }
410
411 dbi_result_free(result);
412 return 0;
413}
414
Jan Luebbe5c15c852008-12-27 15:59:25 +0000415int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000416 dbi_result result=NULL;
417 char* tmsi_quoted;
418 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000419 sprintf(subscriber->tmsi, "%i", rand());
420 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000421 result = dbi_conn_queryf(conn,
422 "SELECT * FROM Subscriber "
423 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000424 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000425 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000426 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000427 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000428 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000429 return 1;
430 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000431 if (dbi_result_get_numrows(result)){
432 dbi_result_free(result);
433 continue;
434 }
435 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000436 dbi_result_free(result);
437 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000438 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000439 }
440 dbi_result_free(result);
441 }
442 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000443}
444
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200445int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
446 dbi_result result=NULL;
447 u_int32_t try;
448 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200449 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200450 result = dbi_conn_queryf(conn,
451 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200452 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200453 try
454 );
455 if (result==NULL) {
456 printf("DB: Failed to query Subscriber while allocating new extension.\n");
457 return 1;
458 }
459 if (dbi_result_get_numrows(result)){
460 dbi_result_free(result);
461 continue;
462 }
463 if (!dbi_result_next_row(result)) {
464 dbi_result_free(result);
465 break;
466 }
467 dbi_result_free(result);
468 }
469 sprintf(subscriber->extension, "%i", try);
470 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
471 return db_sync_subscriber(subscriber);
472}
Jan Luebbe31bef492009-08-12 14:31:14 +0200473/*
474 * try to allocate a new unique token for this subscriber and return it
475 * via a parameter. if the subscriber already has a token, return
476 * an error.
477 */
478
Harald Welte (local)3feef252009-08-13 13:26:11 +0200479int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
480{
481 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200482 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200483
Jan Luebbe31bef492009-08-12 14:31:14 +0200484 for (;;) {
485 try = rand();
486 if (!try) /* 0 is an invalid token */
487 continue;
488 result = dbi_conn_queryf(conn,
489 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200490 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
491 subscriber->id, try);
492 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200493 printf("DB: Failed to query AuthToken while allocating new token.\n");
494 return 1;
495 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200496 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200497 dbi_result_free(result);
498 continue;
499 }
500 if (!dbi_result_next_row(result)) {
501 dbi_result_free(result);
502 break;
503 }
504 dbi_result_free(result);
505 }
506 result = dbi_conn_queryf(conn,
507 "INSERT INTO AuthToken "
508 "(subscriber_id, created, token) "
509 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200510 "(%llu, datetime('now'), \"%08X\") ",
511 subscriber->id, try);
512 if (!result) {
513 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200514 return 1;
515 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200516 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200517 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200518 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
519
Jan Luebbe31bef492009-08-12 14:31:14 +0200520 return 0;
521}
522
Jan Luebbefac25fc2008-12-27 18:04:34 +0000523int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
524 u_int64_t equipment_id, watch_id;
525 dbi_result result;
526
Harald Weltec2e302d2009-07-05 14:08:13 +0200527 strncpy(subscriber->equipment.imei, imei,
528 sizeof(subscriber->equipment.imei)-1),
529
Jan Luebbefac25fc2008-12-27 18:04:34 +0000530 result = dbi_conn_queryf(conn,
531 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000532 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000533 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000534 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000535 imei
536 );
537 if (result==NULL) {
538 printf("DB: Failed to create Equipment by IMEI.\n");
539 return 1;
540 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000541 equipment_id = 0;
542 if (dbi_result_get_numrows_affected(result)) {
543 equipment_id = dbi_conn_sequence_last(conn, NULL);
544 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000545 dbi_result_free(result);
546 if (equipment_id) {
547 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
548 }
549 else {
550 result = dbi_conn_queryf(conn,
551 "SELECT id FROM Equipment "
552 "WHERE imei = %s ",
553 imei
554 );
555 if (result==NULL) {
556 printf("DB: Failed to query Equipment by IMEI.\n");
557 return 1;
558 }
559 if (!dbi_result_next_row(result)) {
560 printf("DB: Failed to find the Equipment.\n");
561 dbi_result_free(result);
562 return 1;
563 }
564 equipment_id = dbi_result_get_ulonglong(result, "id");
565 dbi_result_free(result);
566 }
567
568 result = dbi_conn_queryf(conn,
569 "INSERT OR IGNORE INTO EquipmentWatch "
570 "(subscriber_id, equipment_id, created, updated) "
571 "VALUES "
572 "(%llu, %llu, datetime('now'), datetime('now')) ",
573 subscriber->id, equipment_id
574 );
575 if (result==NULL) {
576 printf("DB: Failed to create EquipmentWatch.\n");
577 return 1;
578 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000579 watch_id = 0;
580 if (dbi_result_get_numrows_affected(result)) {
581 watch_id = dbi_conn_sequence_last(conn, NULL);
582 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000583 dbi_result_free(result);
584 if (watch_id) {
585 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
586 }
587 else {
588 result = dbi_conn_queryf(conn,
589 "UPDATE EquipmentWatch "
590 "SET updated = datetime('now') "
591 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
592 subscriber->id, equipment_id
593 );
594 if (result==NULL) {
595 printf("DB: Failed to update EquipmentWatch.\n");
596 return 1;
597 }
598 dbi_result_free(result);
599 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
600 }
601
602 return 0;
603}
604
Harald Welte7e310b12009-03-30 20:56:32 +0000605/* store an [unsent] SMS to the database */
606int db_sms_store(struct gsm_sms *sms)
607{
608 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200609 char *q_text, *q_daddr;
610 unsigned char *q_udata;
611 char *validity_timestamp = "2222-2-2";
612
613 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000614
615 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200616 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
617 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
618 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200619 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000620 result = dbi_conn_queryf(conn,
621 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200622 "(created, sender_id, receiver_id, valid_until, "
623 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200624 "data_coding_scheme, ud_hdr_ind, dest_addr, "
625 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200626 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200627 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000628 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200629 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
630 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200631 sms->data_coding_scheme, sms->ud_hdr_ind,
632 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000633 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200634 free(q_daddr);
635 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000636
637 if (!result)
638 return -EIO;
639
640 dbi_result_free(result);
641 return 0;
642}
643
Harald Welte2ebabca2009-08-09 19:05:21 +0200644static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000645{
Harald Welte76042182009-08-08 16:03:15 +0200646 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200647 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200648 const char *text, *daddr;
649 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000650
Harald Welte76042182009-08-08 16:03:15 +0200651 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000652 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000653
Harald Weltebe3e3782009-07-05 14:06:41 +0200654 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000655
Harald Weltebe3e3782009-07-05 14:06:41 +0200656 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200657 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200658
659 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200660 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200661
Harald Weltef3efc592009-07-27 20:11:35 +0200662 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200663 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
664 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
665 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
666 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
667 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
668 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200669 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200670 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200671
Harald Welte76042182009-08-08 16:03:15 +0200672 daddr = dbi_result_get_string(result, "dest_addr");
673 if (daddr) {
674 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
675 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
676 }
677
678 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
679 user_data = dbi_result_get_binary(result, "user_data");
680 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200681 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200682 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200683
684 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200685 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200686 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200687 sms->text[sizeof(sms->text)-1] = '\0';
688 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200689 return sms;
690}
691
692/* retrieve the next unsent SMS with ID >= min_id */
693struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
694{
695 dbi_result result;
696 struct gsm_sms *sms;
697
698 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200699 "SELECT * FROM SMS,Subscriber "
700 "WHERE sms.id >= %llu AND sms.sent is NULL "
701 "AND subscriber.lac > 0 "
702 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200703 min_id);
704 if (!result)
705 return NULL;
706
707 if (!dbi_result_next_row(result)) {
708 dbi_result_free(result);
709 return NULL;
710 }
711
712 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000713
714 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200715
716 return sms;
717}
718
719/* retrieve the next unsent SMS with ID >= min_id */
720struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
721{
722 dbi_result result;
723 struct gsm_sms *sms;
724
725 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200726 "SELECT * FROM SMS,Subscriber "
727 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
728 "AND subscriber.lac > 0 "
729 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200730 subscr->id);
731 if (!result)
732 return NULL;
733
734 if (!dbi_result_next_row(result)) {
735 dbi_result_free(result);
736 return NULL;
737 }
738
739 sms = sms_from_result(subscr->net, result);
740
741 dbi_result_free(result);
742
Harald Welte7e310b12009-03-30 20:56:32 +0000743 return sms;
744}
745
746/* mark a given SMS as read */
747int db_sms_mark_sent(struct gsm_sms *sms)
748{
749 dbi_result result;
750
751 result = dbi_conn_queryf(conn,
752 "UPDATE SMS "
753 "SET sent = datetime('now') "
754 "WHERE id = %llu", sms->id);
755 if (!result) {
756 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
757 return 1;
758 }
759
760 dbi_result_free(result);
761 return 0;
762}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200763
764/* increase the number of attempted deliveries */
765int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
766{
767 dbi_result result;
768
769 result = dbi_conn_queryf(conn,
770 "UPDATE SMS "
771 "SET deliver_attempts = deliver_attempts + 1 "
772 "WHERE id = %llu", sms->id);
773 if (!result) {
774 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
775 return 1;
776 }
777
778 dbi_result_free(result);
779 return 0;
780}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200781
782int db_apdu_blob_store(struct gsm_subscriber *subscr,
783 u_int8_t apdu_id_flags, u_int8_t len,
784 u_int8_t *apdu)
785{
786 dbi_result result;
787 char *q_apdu;
788
789 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
790
791 result = dbi_conn_queryf(conn,
792 "INSERT INTO ApduBlobs "
793 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
794 "(datetime('now'),%llu,%u,%s)",
795 subscr->id, apdu_id_flags, q_apdu);
796
797 free(q_apdu);
798
799 if (!result)
800 return -EIO;
801
802 dbi_result_free(result);
803 return 0;
804}