blob: 45c55aff9fdba77606c6db69c476b219ea660de6 [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>
Jan Luebbe7398eb92008-12-27 00:45:41 +000028
Holger Freytherbde36102008-12-28 22:51:39 +000029#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000030#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000031#include <stdlib.h>
32#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000033#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000034#include <dbi/dbi.h>
35
Holger Freytherbde36102008-12-28 22:51:39 +000036static char *db_basename = NULL;
37static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000038static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000039
Harald Welte7e310b12009-03-30 20:56:32 +000040static char *create_stmts[] = {
41 "CREATE TABLE IF NOT EXISTS Meta ("
42 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
43 "key TEXT UNIQUE NOT NULL, "
44 "value TEXT NOT NULL"
45 ")",
46 "INSERT OR IGNORE INTO Meta "
47 "(key, value) "
48 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020049 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000050 "CREATE TABLE IF NOT EXISTS Subscriber ("
51 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
52 "created TIMESTAMP NOT NULL, "
53 "updated TIMESTAMP NOT NULL, "
54 "imsi NUMERIC UNIQUE NOT NULL, "
55 "name TEXT, "
56 "extension TEXT UNIQUE, "
57 "authorized INTEGER NOT NULL DEFAULT 0, "
58 "tmsi TEXT UNIQUE, "
59 "lac INTEGER NOT NULL DEFAULT 0"
60 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020061 "CREATE TABLE IF NOT EXISTS AuthToken ("
62 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
63 "subscriber_id INTEGER UNIQUE NOT NULL, "
64 "created TIMESTAMP NOT NULL, "
65 "token TEXT UNIQUE NOT NULL"
66 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000067 "CREATE TABLE IF NOT EXISTS Equipment ("
68 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
69 "created TIMESTAMP NOT NULL, "
70 "updated TIMESTAMP NOT NULL, "
71 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020072 "classmark1 NUMERIC, "
73 "classmark2 BLOB, "
74 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000075 "imei NUMERIC UNIQUE NOT NULL"
76 ")",
77 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
78 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
79 "created TIMESTAMP NOT NULL, "
80 "updated TIMESTAMP NOT NULL, "
81 "subscriber_id NUMERIC NOT NULL, "
82 "equipment_id NUMERIC NOT NULL, "
83 "UNIQUE (subscriber_id, equipment_id) "
84 ")",
85 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020086 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000087 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
88 "created TIMESTAMP NOT NULL, "
89 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020090 "sender_id INTEGER NOT NULL, "
91 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020092 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020093 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020094 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020095 "reply_path_req INTEGER NOT NULL, "
96 "status_rep_req INTEGER NOT NULL, "
97 "protocol_id INTEGER NOT NULL, "
98 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020099 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200100 "dest_addr TEXT, "
101 "user_data BLOB, " /* TP-UD */
102 /* additional data, interpreted from SMS */
103 "header BLOB, " /* UD Header */
104 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000105 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000106 "CREATE TABLE IF NOT EXISTS VLR ("
107 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
108 "created TIMESTAMP NOT NULL, "
109 "updated TIMESTAMP NOT NULL, "
110 "subscriber_id NUMERIC UNIQUE NOT NULL, "
111 "last_bts NUMERIC NOT NULL "
112 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200113 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
114 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
115 "created TIMESTAMP NOT NULL, "
116 "apdu_id_flags INTEGER NOT NULL, "
117 "subscriber_id INTEGER NOT NULL, "
118 "apdu BLOB "
119 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000120};
121
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000123 const char* msg;
124 dbi_conn_error(conn, &msg);
125 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000126}
127
Harald Welted0b7b772009-08-09 19:03:42 +0200128static int check_db_revision(void)
129{
130 dbi_result result;
131 const char *rev;
132
133 result = dbi_conn_query(conn,
134 "SELECT value FROM Meta WHERE key='revision'");
135 if (!result)
136 return -EINVAL;
137
138 if (!dbi_result_next_row(result)) {
139 dbi_result_free(result);
140 return -EINVAL;
141 }
142 rev = dbi_result_get_string(result, "value");
143 if (!rev || atoi(rev) != 2) {
144 dbi_result_free(result);
145 return -EINVAL;
146 }
147
148 dbi_result_free(result);
149 return 0;
150}
151
Holger Freytherc7b86f92009-06-06 13:54:20 +0000152int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000153 dbi_initialize(NULL);
154 conn = dbi_conn_new("sqlite3");
155 if (conn==NULL) {
156 printf("DB: Failed to create connection.\n");
157 return 1;
158 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000159
Holger Freyther12aa50d2009-01-01 18:02:05 +0000160 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000161
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 /* MySQL
163 dbi_conn_set_option(conn, "host", "localhost");
164 dbi_conn_set_option(conn, "username", "your_name");
165 dbi_conn_set_option(conn, "password", "your_password");
166 dbi_conn_set_option(conn, "dbname", "your_dbname");
167 dbi_conn_set_option(conn, "encoding", "UTF-8");
168 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000169
Jan Luebbe5c15c852008-12-27 15:59:25 +0000170 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000171 db_basename = strdup(name);
172 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000173 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
174 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000175
Harald Welted0b7b772009-08-09 19:03:42 +0200176 if (dbi_conn_connect(conn) < 0)
177 goto out_err;
178
Jan Luebbe5c15c852008-12-27 15:59:25 +0000179 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200180
181out_err:
182 free(db_dirname);
183 free(db_basename);
184 db_dirname = db_basename = NULL;
185 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000186}
187
Harald Welted0b7b772009-08-09 19:03:42 +0200188
Jan Luebbe7398eb92008-12-27 00:45:41 +0000189int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000190 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000191 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000192
Harald Welte7e310b12009-03-30 20:56:32 +0000193 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
194 result = dbi_conn_query(conn, create_stmts[i]);
195 if (result==NULL) {
196 printf("DB: Failed to create some table.\n");
197 return 1;
198 }
199 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000200 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000201
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200202 if (check_db_revision() < 0) {
203 fprintf(stderr, "Database schema revision invalid, "
204 "please update your database schema\n");
205 return -1;
206 }
207
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209}
210
211int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 dbi_conn_close(conn);
213 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000214
215 if (db_dirname)
216 free(db_dirname);
217 if (db_basename)
218 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000220}
221
Harald Welte9176bd42009-07-23 18:46:00 +0200222struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
223{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225 struct gsm_subscriber* subscr;
226
227 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200228 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000229 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000230 result = dbi_conn_queryf(conn,
231 "UPDATE Subscriber set updated = datetime('now') "
232 "WHERE imsi = %s " , imsi);
233 if (result==NULL) {
234 printf("DB: failed to update timestamp\n");
235 } else {
236 dbi_result_free(result);
237 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000238 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000239 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000240
241 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200242 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 if (!subscr)
244 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000246 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000247 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000248 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000249 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000250 imsi
251 );
252 if (result==NULL) {
253 printf("DB: Failed to create Subscriber by IMSI.\n");
254 }
Harald Welte9176bd42009-07-23 18:46:00 +0200255 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 subscr->id = dbi_conn_sequence_last(conn, NULL);
257 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000258 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200260 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000261 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000262}
263
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200264static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
265{
266 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200267 const char *string;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200268 unsigned int cm1;
269 const unsigned char *cm2, *cm3;
270 struct gsm_equipment *equip = &subscr->equipment;
271
272 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200273 "SELECT equipment.* FROM Equipment,EquipmentWatch "
274 "WHERE EquipmentWatch.equipment_id=Equipment.id "
275 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200276 "ORDER BY updated DESC", subscr->id);
277 if (!result)
278 return -EIO;
279
280 if (!dbi_result_next_row(result)) {
281 dbi_result_free(result);
282 return -ENOENT;
283 }
284
285 equip->id = dbi_result_get_ulonglong(result, "id");
286
287 string = dbi_result_get_string(result, "imei");
288 if (string)
289 strncpy(equip->imei, string, sizeof(equip->imei));
290
291 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
292 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
293
294 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
295 cm2 = dbi_result_get_binary(result, "classmark2");
296 if (equip->classmark2_len > sizeof(equip->classmark2))
297 equip->classmark2_len = sizeof(equip->classmark2);
298 memcpy(equip->classmark2, cm2, equip->classmark2_len);
299
300 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
301 cm3 = dbi_result_get_binary(result, "classmark3");
302 if (equip->classmark3_len > sizeof(equip->classmark3))
303 equip->classmark3_len = sizeof(equip->classmark3);
304 memcpy(equip->classmark3, cm3, equip->classmark3_len);
305
306 dbi_result_free(result);
307
308 return 0;
309}
310#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200311struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
312 enum gsm_subscriber_field field,
313 const char *id)
314{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000315 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000316 const char *string;
317 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000318 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000319
Jan Luebbe5c15c852008-12-27 15:59:25 +0000320 switch (field) {
321 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000322 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000323 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200324 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000325 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000326 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000327 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000328 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000329 break;
330 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000331 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200333 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000334 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000335 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000337 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000339 case GSM_SUBSCRIBER_EXTENSION:
340 dbi_conn_quote_string_copy(conn, id, &quoted);
341 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200342 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000343 "WHERE extension = %s ",
344 quoted
345 );
346 free(quoted);
347 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200348 case GSM_SUBSCRIBER_ID:
349 dbi_conn_quote_string_copy(conn, id, &quoted);
350 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200351 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200352 "WHERE id = %s ", quoted);
353 free(quoted);
354 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000355 default:
356 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000357 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000358 }
359 if (result==NULL) {
360 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000361 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000362 }
363 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000364 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
365 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000366 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000367 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000368 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000369
370 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200371 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000372 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000373 string = dbi_result_get_string(result, "imsi");
374 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000375 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000376
377 string = dbi_result_get_string(result, "tmsi");
378 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000379 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000380
381 string = dbi_result_get_string(result, "name");
382 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000383 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000384
385 string = dbi_result_get_string(result, "extension");
386 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000387 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000388
Holger Freyther12aa50d2009-01-01 18:02:05 +0000389 subscr->lac = dbi_result_get_uint(result, "lac");
390 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000391 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
392 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000393 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000394 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200395
396 get_equipment_by_subscr(subscr);
397
Holger Freyther12aa50d2009-01-01 18:02:05 +0000398 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000399}
400
Holger Freyther12aa50d2009-01-01 18:02:05 +0000401int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000402 dbi_result result;
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200403 char *q_tmsi;
404 if (subscriber->tmsi[0])
405 dbi_conn_quote_string_copy(conn,
406 subscriber->tmsi,
407 &q_tmsi);
408 else
409 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000410 result = dbi_conn_queryf(conn,
411 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000412 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200413 "name = '%s', "
414 "extension = '%s', "
415 "authorized = %i, "
416 "tmsi = %s, "
417 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000418 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200419 subscriber->name,
420 subscriber->extension,
421 subscriber->authorized,
422 q_tmsi,
423 subscriber->lac,
424 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000425 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200426 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000427 if (result==NULL) {
428 printf("DB: Failed to update Subscriber (by IMSI).\n");
429 return 1;
430 }
431 dbi_result_free(result);
432 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000433}
434
Harald Weltec2e302d2009-07-05 14:08:13 +0200435int db_sync_equipment(struct gsm_equipment *equip)
436{
437 dbi_result result;
438 unsigned char *cm2, *cm3;
439
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200440 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
441 equip->imei, equip->classmark1);
442 if (equip->classmark2_len)
443 printf(", classmark2=%s",
444 hexdump(equip->classmark2, equip->classmark2_len));
445 if (equip->classmark3_len)
446 printf(", classmark3=%s",
447 hexdump(equip->classmark3, equip->classmark3_len));
448 printf("\n");
449
Harald Weltec2e302d2009-07-05 14:08:13 +0200450 dbi_conn_quote_binary_copy(conn, equip->classmark2,
451 equip->classmark2_len, &cm2);
452 dbi_conn_quote_binary_copy(conn, equip->classmark3,
453 equip->classmark3_len, &cm3);
454
455 result = dbi_conn_queryf(conn,
456 "UPDATE Equipment SET "
457 "updated = datetime('now'), "
458 "classmark1 = %u, "
459 "classmark2 = %s, "
460 "classmark3 = %s "
461 "WHERE imei = '%s' ",
462 equip->classmark1, cm2, cm3, equip->imei);
463
464 free(cm2);
465 free(cm3);
466
467 if (!result) {
468 printf("DB: Failed to update Equipment\n");
469 return -EIO;
470 }
471
472 dbi_result_free(result);
473 return 0;
474}
475
Jan Luebbe5c15c852008-12-27 15:59:25 +0000476int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000477 dbi_result result=NULL;
478 char* tmsi_quoted;
479 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000480 sprintf(subscriber->tmsi, "%i", rand());
481 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000482 result = dbi_conn_queryf(conn,
483 "SELECT * FROM Subscriber "
484 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000485 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000486 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000487 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000488 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000489 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000490 return 1;
491 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000492 if (dbi_result_get_numrows(result)){
493 dbi_result_free(result);
494 continue;
495 }
496 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000497 dbi_result_free(result);
498 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000499 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000500 }
501 dbi_result_free(result);
502 }
503 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000504}
505
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200506int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
507 dbi_result result=NULL;
508 u_int32_t try;
509 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200510 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200511 result = dbi_conn_queryf(conn,
512 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200513 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200514 try
515 );
516 if (result==NULL) {
517 printf("DB: Failed to query Subscriber while allocating new extension.\n");
518 return 1;
519 }
520 if (dbi_result_get_numrows(result)){
521 dbi_result_free(result);
522 continue;
523 }
524 if (!dbi_result_next_row(result)) {
525 dbi_result_free(result);
526 break;
527 }
528 dbi_result_free(result);
529 }
530 sprintf(subscriber->extension, "%i", try);
531 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
532 return db_sync_subscriber(subscriber);
533}
Jan Luebbe31bef492009-08-12 14:31:14 +0200534/*
535 * try to allocate a new unique token for this subscriber and return it
536 * via a parameter. if the subscriber already has a token, return
537 * an error.
538 */
539
Harald Welte (local)3feef252009-08-13 13:26:11 +0200540int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
541{
542 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200543 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200544
Jan Luebbe31bef492009-08-12 14:31:14 +0200545 for (;;) {
546 try = rand();
547 if (!try) /* 0 is an invalid token */
548 continue;
549 result = dbi_conn_queryf(conn,
550 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200551 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
552 subscriber->id, try);
553 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200554 printf("DB: Failed to query AuthToken while allocating new token.\n");
555 return 1;
556 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200557 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200558 dbi_result_free(result);
559 continue;
560 }
561 if (!dbi_result_next_row(result)) {
562 dbi_result_free(result);
563 break;
564 }
565 dbi_result_free(result);
566 }
567 result = dbi_conn_queryf(conn,
568 "INSERT INTO AuthToken "
569 "(subscriber_id, created, token) "
570 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200571 "(%llu, datetime('now'), \"%08X\") ",
572 subscriber->id, try);
573 if (!result) {
574 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200575 return 1;
576 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200577 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200578 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200579 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
580
Jan Luebbe31bef492009-08-12 14:31:14 +0200581 return 0;
582}
583
Jan Luebbefac25fc2008-12-27 18:04:34 +0000584int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
585 u_int64_t equipment_id, watch_id;
586 dbi_result result;
587
Harald Weltec2e302d2009-07-05 14:08:13 +0200588 strncpy(subscriber->equipment.imei, imei,
589 sizeof(subscriber->equipment.imei)-1),
590
Jan Luebbefac25fc2008-12-27 18:04:34 +0000591 result = dbi_conn_queryf(conn,
592 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000593 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000594 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000595 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000596 imei
597 );
598 if (result==NULL) {
599 printf("DB: Failed to create Equipment by IMEI.\n");
600 return 1;
601 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000602 equipment_id = 0;
603 if (dbi_result_get_numrows_affected(result)) {
604 equipment_id = dbi_conn_sequence_last(conn, NULL);
605 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000606 dbi_result_free(result);
607 if (equipment_id) {
608 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
609 }
610 else {
611 result = dbi_conn_queryf(conn,
612 "SELECT id FROM Equipment "
613 "WHERE imei = %s ",
614 imei
615 );
616 if (result==NULL) {
617 printf("DB: Failed to query Equipment by IMEI.\n");
618 return 1;
619 }
620 if (!dbi_result_next_row(result)) {
621 printf("DB: Failed to find the Equipment.\n");
622 dbi_result_free(result);
623 return 1;
624 }
625 equipment_id = dbi_result_get_ulonglong(result, "id");
626 dbi_result_free(result);
627 }
628
629 result = dbi_conn_queryf(conn,
630 "INSERT OR IGNORE INTO EquipmentWatch "
631 "(subscriber_id, equipment_id, created, updated) "
632 "VALUES "
633 "(%llu, %llu, datetime('now'), datetime('now')) ",
634 subscriber->id, equipment_id
635 );
636 if (result==NULL) {
637 printf("DB: Failed to create EquipmentWatch.\n");
638 return 1;
639 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000640 watch_id = 0;
641 if (dbi_result_get_numrows_affected(result)) {
642 watch_id = dbi_conn_sequence_last(conn, NULL);
643 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000644 dbi_result_free(result);
645 if (watch_id) {
646 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
647 }
648 else {
649 result = dbi_conn_queryf(conn,
650 "UPDATE EquipmentWatch "
651 "SET updated = datetime('now') "
652 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
653 subscriber->id, equipment_id
654 );
655 if (result==NULL) {
656 printf("DB: Failed to update EquipmentWatch.\n");
657 return 1;
658 }
659 dbi_result_free(result);
660 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
661 }
662
663 return 0;
664}
665
Harald Welte7e310b12009-03-30 20:56:32 +0000666/* store an [unsent] SMS to the database */
667int db_sms_store(struct gsm_sms *sms)
668{
669 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200670 char *q_text, *q_daddr;
671 unsigned char *q_udata;
672 char *validity_timestamp = "2222-2-2";
673
674 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000675
676 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200677 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
678 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
679 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200680 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000681 result = dbi_conn_queryf(conn,
682 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200683 "(created, sender_id, receiver_id, valid_until, "
684 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200685 "data_coding_scheme, ud_hdr_ind, dest_addr, "
686 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200687 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200688 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000689 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200690 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
691 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200692 sms->data_coding_scheme, sms->ud_hdr_ind,
693 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000694 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200695 free(q_daddr);
696 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000697
698 if (!result)
699 return -EIO;
700
701 dbi_result_free(result);
702 return 0;
703}
704
Harald Welte2ebabca2009-08-09 19:05:21 +0200705static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000706{
Harald Welte76042182009-08-08 16:03:15 +0200707 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200708 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200709 const char *text, *daddr;
710 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000711
Harald Welte76042182009-08-08 16:03:15 +0200712 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000713 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000714
Harald Weltebe3e3782009-07-05 14:06:41 +0200715 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000716
Harald Weltebe3e3782009-07-05 14:06:41 +0200717 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200718 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200719
720 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200721 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200722
Harald Weltef3efc592009-07-27 20:11:35 +0200723 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200724 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
725 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
726 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
727 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
728 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
729 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200730 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200731 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200732
Harald Welte76042182009-08-08 16:03:15 +0200733 daddr = dbi_result_get_string(result, "dest_addr");
734 if (daddr) {
735 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
736 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
737 }
738
739 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
740 user_data = dbi_result_get_binary(result, "user_data");
741 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200742 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200743 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200744
745 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200746 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200747 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200748 sms->text[sizeof(sms->text)-1] = '\0';
749 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200750 return sms;
751}
752
753/* retrieve the next unsent SMS with ID >= min_id */
754struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
755{
756 dbi_result result;
757 struct gsm_sms *sms;
758
759 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200760 "SELECT * FROM SMS,Subscriber "
761 "WHERE sms.id >= %llu AND sms.sent is NULL "
762 "AND subscriber.lac > 0 "
763 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200764 min_id);
765 if (!result)
766 return NULL;
767
768 if (!dbi_result_next_row(result)) {
769 dbi_result_free(result);
770 return NULL;
771 }
772
773 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000774
775 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200776
777 return sms;
778}
779
780/* retrieve the next unsent SMS with ID >= min_id */
781struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
782{
783 dbi_result result;
784 struct gsm_sms *sms;
785
786 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200787 "SELECT * FROM SMS,Subscriber "
788 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
789 "AND subscriber.lac > 0 "
790 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200791 subscr->id);
792 if (!result)
793 return NULL;
794
795 if (!dbi_result_next_row(result)) {
796 dbi_result_free(result);
797 return NULL;
798 }
799
800 sms = sms_from_result(subscr->net, result);
801
802 dbi_result_free(result);
803
Harald Welte7e310b12009-03-30 20:56:32 +0000804 return sms;
805}
806
807/* mark a given SMS as read */
808int db_sms_mark_sent(struct gsm_sms *sms)
809{
810 dbi_result result;
811
812 result = dbi_conn_queryf(conn,
813 "UPDATE SMS "
814 "SET sent = datetime('now') "
815 "WHERE id = %llu", sms->id);
816 if (!result) {
817 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
818 return 1;
819 }
820
821 dbi_result_free(result);
822 return 0;
823}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200824
825/* increase the number of attempted deliveries */
826int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
827{
828 dbi_result result;
829
830 result = dbi_conn_queryf(conn,
831 "UPDATE SMS "
832 "SET deliver_attempts = deliver_attempts + 1 "
833 "WHERE id = %llu", sms->id);
834 if (!result) {
835 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
836 return 1;
837 }
838
839 dbi_result_free(result);
840 return 0;
841}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200842
843int db_apdu_blob_store(struct gsm_subscriber *subscr,
844 u_int8_t apdu_id_flags, u_int8_t len,
845 u_int8_t *apdu)
846{
847 dbi_result result;
848 char *q_apdu;
849
850 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
851
852 result = dbi_conn_queryf(conn,
853 "INSERT INTO ApduBlobs "
854 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
855 "(datetime('now'),%llu,%u,%s)",
856 subscr->id, apdu_id_flags, q_apdu);
857
858 free(q_apdu);
859
860 if (!result)
861 return -EIO;
862
863 dbi_result_free(result);
864 return 0;
865}