blob: d5e924db76470f736ee838b7a054ea071419de1f [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 Welte7e310b12009-03-30 20:56:32 +0000112};
113
Holger Freyther12aa50d2009-01-01 18:02:05 +0000114void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000115 const char* msg;
116 dbi_conn_error(conn, &msg);
117 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000118}
119
Harald Welted0b7b772009-08-09 19:03:42 +0200120static int check_db_revision(void)
121{
122 dbi_result result;
123 const char *rev;
124
125 result = dbi_conn_query(conn,
126 "SELECT value FROM Meta WHERE key='revision'");
127 if (!result)
128 return -EINVAL;
129
130 if (!dbi_result_next_row(result)) {
131 dbi_result_free(result);
132 return -EINVAL;
133 }
134 rev = dbi_result_get_string(result, "value");
135 if (!rev || atoi(rev) != 2) {
136 dbi_result_free(result);
137 return -EINVAL;
138 }
139
140 dbi_result_free(result);
141 return 0;
142}
143
Holger Freytherc7b86f92009-06-06 13:54:20 +0000144int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000145 dbi_initialize(NULL);
146 conn = dbi_conn_new("sqlite3");
147 if (conn==NULL) {
148 printf("DB: Failed to create connection.\n");
149 return 1;
150 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000151
Holger Freyther12aa50d2009-01-01 18:02:05 +0000152 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000153
Jan Luebbe5c15c852008-12-27 15:59:25 +0000154 /* MySQL
155 dbi_conn_set_option(conn, "host", "localhost");
156 dbi_conn_set_option(conn, "username", "your_name");
157 dbi_conn_set_option(conn, "password", "your_password");
158 dbi_conn_set_option(conn, "dbname", "your_dbname");
159 dbi_conn_set_option(conn, "encoding", "UTF-8");
160 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000161
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000163 db_basename = strdup(name);
164 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000165 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
166 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000167
Harald Welted0b7b772009-08-09 19:03:42 +0200168 if (dbi_conn_connect(conn) < 0)
169 goto out_err;
170
Jan Luebbe5c15c852008-12-27 15:59:25 +0000171 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200172
173out_err:
174 free(db_dirname);
175 free(db_basename);
176 db_dirname = db_basename = NULL;
177 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000178}
179
Harald Welted0b7b772009-08-09 19:03:42 +0200180
Jan Luebbe7398eb92008-12-27 00:45:41 +0000181int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000182 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000183 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000184
Harald Welte7e310b12009-03-30 20:56:32 +0000185 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
186 result = dbi_conn_query(conn, create_stmts[i]);
187 if (result==NULL) {
188 printf("DB: Failed to create some table.\n");
189 return 1;
190 }
191 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000192 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000193
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200194 if (check_db_revision() < 0) {
195 fprintf(stderr, "Database schema revision invalid, "
196 "please update your database schema\n");
197 return -1;
198 }
199
Jan Luebbe5c15c852008-12-27 15:59:25 +0000200 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000201}
202
203int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000204 dbi_conn_close(conn);
205 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000206
207 if (db_dirname)
208 free(db_dirname);
209 if (db_basename)
210 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000211 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000212}
213
Harald Welte9176bd42009-07-23 18:46:00 +0200214struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
215{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000216 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000217 struct gsm_subscriber* subscr;
218
219 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200220 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000221 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000222 result = dbi_conn_queryf(conn,
223 "UPDATE Subscriber set updated = datetime('now') "
224 "WHERE imsi = %s " , imsi);
225 if (result==NULL) {
226 printf("DB: failed to update timestamp\n");
227 } else {
228 dbi_result_free(result);
229 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000230 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000232
233 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200234 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000235 if (!subscr)
236 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000237 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000238 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000239 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000241 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000242 imsi
243 );
244 if (result==NULL) {
245 printf("DB: Failed to create Subscriber by IMSI.\n");
246 }
Harald Welte9176bd42009-07-23 18:46:00 +0200247 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000248 subscr->id = dbi_conn_sequence_last(conn, NULL);
249 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000250 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000251 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200252 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000253 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000254}
255
Harald Welte9176bd42009-07-23 18:46:00 +0200256struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
257 enum gsm_subscriber_field field,
258 const char *id)
259{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000260 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000261 const char *string;
262 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000263 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000264
Jan Luebbe5c15c852008-12-27 15:59:25 +0000265 switch (field) {
266 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000267 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000268 result = dbi_conn_queryf(conn,
269 "SELECT * FROM Subscriber "
270 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000271 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000272 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000273 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000274 break;
275 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000276 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000277 result = dbi_conn_queryf(conn,
278 "SELECT * FROM Subscriber "
279 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000280 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000281 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000282 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000283 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000284 case GSM_SUBSCRIBER_EXTENSION:
285 dbi_conn_quote_string_copy(conn, id, &quoted);
286 result = dbi_conn_queryf(conn,
287 "SELECT * FROM Subscriber "
288 "WHERE extension = %s ",
289 quoted
290 );
291 free(quoted);
292 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200293 case GSM_SUBSCRIBER_ID:
294 dbi_conn_quote_string_copy(conn, id, &quoted);
295 result = dbi_conn_queryf(conn,
296 "SELECT * FROM Subscriber "
297 "WHERE id = %s ", quoted);
298 free(quoted);
299 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000300 default:
301 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000302 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000303 }
304 if (result==NULL) {
305 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000306 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000307 }
308 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000309 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
310 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000311 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000312 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000313 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000314
315 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200316 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000317 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000318 string = dbi_result_get_string(result, "imsi");
319 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000320 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000321
322 string = dbi_result_get_string(result, "tmsi");
323 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000324 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000325
326 string = dbi_result_get_string(result, "name");
327 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000328 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000329
330 string = dbi_result_get_string(result, "extension");
331 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000332 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000333
Holger Freyther12aa50d2009-01-01 18:02:05 +0000334 subscr->lac = dbi_result_get_uint(result, "lac");
335 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000336 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
337 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000338 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000339 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000340 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000341}
342
Holger Freyther12aa50d2009-01-01 18:02:05 +0000343int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000344 dbi_result result;
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200345 char *q_tmsi;
346 if (subscriber->tmsi[0])
347 dbi_conn_quote_string_copy(conn,
348 subscriber->tmsi,
349 &q_tmsi);
350 else
351 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000352 result = dbi_conn_queryf(conn,
353 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000354 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200355 "name = '%s', "
356 "extension = '%s', "
357 "authorized = %i, "
358 "tmsi = %s, "
359 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000360 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200361 subscriber->name,
362 subscriber->extension,
363 subscriber->authorized,
364 q_tmsi,
365 subscriber->lac,
366 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000367 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200368 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000369 if (result==NULL) {
370 printf("DB: Failed to update Subscriber (by IMSI).\n");
371 return 1;
372 }
373 dbi_result_free(result);
374 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000375}
376
Harald Weltec2e302d2009-07-05 14:08:13 +0200377int db_sync_equipment(struct gsm_equipment *equip)
378{
379 dbi_result result;
380 unsigned char *cm2, *cm3;
381
382 dbi_conn_quote_binary_copy(conn, equip->classmark2,
383 equip->classmark2_len, &cm2);
384 dbi_conn_quote_binary_copy(conn, equip->classmark3,
385 equip->classmark3_len, &cm3);
386
387 result = dbi_conn_queryf(conn,
388 "UPDATE Equipment SET "
389 "updated = datetime('now'), "
390 "classmark1 = %u, "
391 "classmark2 = %s, "
392 "classmark3 = %s "
393 "WHERE imei = '%s' ",
394 equip->classmark1, cm2, cm3, equip->imei);
395
396 free(cm2);
397 free(cm3);
398
399 if (!result) {
400 printf("DB: Failed to update Equipment\n");
401 return -EIO;
402 }
403
404 dbi_result_free(result);
405 return 0;
406}
407
Jan Luebbe5c15c852008-12-27 15:59:25 +0000408int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000409 dbi_result result=NULL;
410 char* tmsi_quoted;
411 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000412 sprintf(subscriber->tmsi, "%i", rand());
413 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000414 result = dbi_conn_queryf(conn,
415 "SELECT * FROM Subscriber "
416 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000417 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000418 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000419 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000420 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000421 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000422 return 1;
423 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000424 if (dbi_result_get_numrows(result)){
425 dbi_result_free(result);
426 continue;
427 }
428 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000429 dbi_result_free(result);
430 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000431 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000432 }
433 dbi_result_free(result);
434 }
435 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000436}
437
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200438int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
439 dbi_result result=NULL;
440 u_int32_t try;
441 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200442 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200443 result = dbi_conn_queryf(conn,
444 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200445 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200446 try
447 );
448 if (result==NULL) {
449 printf("DB: Failed to query Subscriber while allocating new extension.\n");
450 return 1;
451 }
452 if (dbi_result_get_numrows(result)){
453 dbi_result_free(result);
454 continue;
455 }
456 if (!dbi_result_next_row(result)) {
457 dbi_result_free(result);
458 break;
459 }
460 dbi_result_free(result);
461 }
462 sprintf(subscriber->extension, "%i", try);
463 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
464 return db_sync_subscriber(subscriber);
465}
Jan Luebbe31bef492009-08-12 14:31:14 +0200466/*
467 * try to allocate a new unique token for this subscriber and return it
468 * via a parameter. if the subscriber already has a token, return
469 * an error.
470 */
471
Harald Welte (local)3feef252009-08-13 13:26:11 +0200472int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
473{
474 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200475 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200476
Jan Luebbe31bef492009-08-12 14:31:14 +0200477 for (;;) {
478 try = rand();
479 if (!try) /* 0 is an invalid token */
480 continue;
481 result = dbi_conn_queryf(conn,
482 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200483 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
484 subscriber->id, try);
485 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200486 printf("DB: Failed to query AuthToken while allocating new token.\n");
487 return 1;
488 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200489 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200490 dbi_result_free(result);
491 continue;
492 }
493 if (!dbi_result_next_row(result)) {
494 dbi_result_free(result);
495 break;
496 }
497 dbi_result_free(result);
498 }
499 result = dbi_conn_queryf(conn,
500 "INSERT INTO AuthToken "
501 "(subscriber_id, created, token) "
502 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200503 "(%llu, datetime('now'), \"%08X\") ",
504 subscriber->id, try);
505 if (!result) {
506 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200507 return 1;
508 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200509 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200510 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200511 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
512
Jan Luebbe31bef492009-08-12 14:31:14 +0200513 return 0;
514}
515
Jan Luebbefac25fc2008-12-27 18:04:34 +0000516int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
517 u_int64_t equipment_id, watch_id;
518 dbi_result result;
519
Harald Weltec2e302d2009-07-05 14:08:13 +0200520 strncpy(subscriber->equipment.imei, imei,
521 sizeof(subscriber->equipment.imei)-1),
522
Jan Luebbefac25fc2008-12-27 18:04:34 +0000523 result = dbi_conn_queryf(conn,
524 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000525 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000526 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000527 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000528 imei
529 );
530 if (result==NULL) {
531 printf("DB: Failed to create Equipment by IMEI.\n");
532 return 1;
533 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000534 equipment_id = 0;
535 if (dbi_result_get_numrows_affected(result)) {
536 equipment_id = dbi_conn_sequence_last(conn, NULL);
537 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000538 dbi_result_free(result);
539 if (equipment_id) {
540 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
541 }
542 else {
543 result = dbi_conn_queryf(conn,
544 "SELECT id FROM Equipment "
545 "WHERE imei = %s ",
546 imei
547 );
548 if (result==NULL) {
549 printf("DB: Failed to query Equipment by IMEI.\n");
550 return 1;
551 }
552 if (!dbi_result_next_row(result)) {
553 printf("DB: Failed to find the Equipment.\n");
554 dbi_result_free(result);
555 return 1;
556 }
557 equipment_id = dbi_result_get_ulonglong(result, "id");
558 dbi_result_free(result);
559 }
560
561 result = dbi_conn_queryf(conn,
562 "INSERT OR IGNORE INTO EquipmentWatch "
563 "(subscriber_id, equipment_id, created, updated) "
564 "VALUES "
565 "(%llu, %llu, datetime('now'), datetime('now')) ",
566 subscriber->id, equipment_id
567 );
568 if (result==NULL) {
569 printf("DB: Failed to create EquipmentWatch.\n");
570 return 1;
571 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000572 watch_id = 0;
573 if (dbi_result_get_numrows_affected(result)) {
574 watch_id = dbi_conn_sequence_last(conn, NULL);
575 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000576 dbi_result_free(result);
577 if (watch_id) {
578 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
579 }
580 else {
581 result = dbi_conn_queryf(conn,
582 "UPDATE EquipmentWatch "
583 "SET updated = datetime('now') "
584 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
585 subscriber->id, equipment_id
586 );
587 if (result==NULL) {
588 printf("DB: Failed to update EquipmentWatch.\n");
589 return 1;
590 }
591 dbi_result_free(result);
592 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
593 }
594
595 return 0;
596}
597
Harald Welte7e310b12009-03-30 20:56:32 +0000598/* store an [unsent] SMS to the database */
599int db_sms_store(struct gsm_sms *sms)
600{
601 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200602 char *q_text, *q_daddr;
603 unsigned char *q_udata;
604 char *validity_timestamp = "2222-2-2";
605
606 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000607
608 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200609 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
610 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
611 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200612 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000613 result = dbi_conn_queryf(conn,
614 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200615 "(created, sender_id, receiver_id, valid_until, "
616 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200617 "data_coding_scheme, ud_hdr_ind, dest_addr, "
618 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200619 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200620 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000621 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200622 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
623 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200624 sms->data_coding_scheme, sms->ud_hdr_ind,
625 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000626 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200627 free(q_daddr);
628 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000629
630 if (!result)
631 return -EIO;
632
633 dbi_result_free(result);
634 return 0;
635}
636
Harald Welte2ebabca2009-08-09 19:05:21 +0200637static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000638{
Harald Welte76042182009-08-08 16:03:15 +0200639 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200640 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200641 const char *text, *daddr;
642 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000643
Harald Welte76042182009-08-08 16:03:15 +0200644 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000645 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000646
Harald Weltebe3e3782009-07-05 14:06:41 +0200647 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000648
Harald Weltebe3e3782009-07-05 14:06:41 +0200649 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200650 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200651
652 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200653 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200654
Harald Weltef3efc592009-07-27 20:11:35 +0200655 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200656 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
657 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
658 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
659 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
660 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
661 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200662 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200663 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200664
Harald Welte76042182009-08-08 16:03:15 +0200665 daddr = dbi_result_get_string(result, "dest_addr");
666 if (daddr) {
667 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
668 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
669 }
670
671 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
672 user_data = dbi_result_get_binary(result, "user_data");
673 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200674 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200675 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200676
677 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200678 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200679 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200680 sms->text[sizeof(sms->text)-1] = '\0';
681 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200682 return sms;
683}
684
685/* retrieve the next unsent SMS with ID >= min_id */
686struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
687{
688 dbi_result result;
689 struct gsm_sms *sms;
690
691 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200692 "SELECT * FROM SMS,Subscriber "
693 "WHERE sms.id >= %llu AND sms.sent is NULL "
694 "AND subscriber.lac > 0 "
695 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200696 min_id);
697 if (!result)
698 return NULL;
699
700 if (!dbi_result_next_row(result)) {
701 dbi_result_free(result);
702 return NULL;
703 }
704
705 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000706
707 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200708
709 return sms;
710}
711
712/* retrieve the next unsent SMS with ID >= min_id */
713struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
714{
715 dbi_result result;
716 struct gsm_sms *sms;
717
718 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200719 "SELECT * FROM SMS,Subscriber "
720 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
721 "AND subscriber.lac > 0 "
722 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200723 subscr->id);
724 if (!result)
725 return NULL;
726
727 if (!dbi_result_next_row(result)) {
728 dbi_result_free(result);
729 return NULL;
730 }
731
732 sms = sms_from_result(subscr->net, result);
733
734 dbi_result_free(result);
735
Harald Welte7e310b12009-03-30 20:56:32 +0000736 return sms;
737}
738
739/* mark a given SMS as read */
740int db_sms_mark_sent(struct gsm_sms *sms)
741{
742 dbi_result result;
743
744 result = dbi_conn_queryf(conn,
745 "UPDATE SMS "
746 "SET sent = datetime('now') "
747 "WHERE id = %llu", sms->id);
748 if (!result) {
749 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
750 return 1;
751 }
752
753 dbi_result_free(result);
754 return 0;
755}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200756
757/* increase the number of attempted deliveries */
758int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
759{
760 dbi_result result;
761
762 result = dbi_conn_queryf(conn,
763 "UPDATE SMS "
764 "SET deliver_attempts = deliver_attempts + 1 "
765 "WHERE id = %llu", sms->id);
766 if (!result) {
767 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
768 return 1;
769 }
770
771 dbi_result_free(result);
772 return 0;
773}