blob: 938c5c440ae02dd27b224549d0cc96fd52376fdf [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, "
91 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020092 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020093 "reply_path_req INTEGER NOT NULL, "
94 "status_rep_req INTEGER NOT NULL, "
95 "protocol_id INTEGER NOT NULL, "
96 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020097 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +020098 "dest_addr TEXT, "
99 "user_data BLOB, " /* TP-UD */
100 /* additional data, interpreted from SMS */
101 "header BLOB, " /* UD Header */
102 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000103 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000104 "CREATE TABLE IF NOT EXISTS VLR ("
105 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
106 "created TIMESTAMP NOT NULL, "
107 "updated TIMESTAMP NOT NULL, "
108 "subscriber_id NUMERIC UNIQUE NOT NULL, "
109 "last_bts NUMERIC NOT NULL "
110 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000111};
112
Holger Freyther12aa50d2009-01-01 18:02:05 +0000113void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000114 const char* msg;
115 dbi_conn_error(conn, &msg);
116 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000117}
118
Harald Welted0b7b772009-08-09 19:03:42 +0200119static int check_db_revision(void)
120{
121 dbi_result result;
122 const char *rev;
123
124 result = dbi_conn_query(conn,
125 "SELECT value FROM Meta WHERE key='revision'");
126 if (!result)
127 return -EINVAL;
128
129 if (!dbi_result_next_row(result)) {
130 dbi_result_free(result);
131 return -EINVAL;
132 }
133 rev = dbi_result_get_string(result, "value");
134 if (!rev || atoi(rev) != 2) {
135 dbi_result_free(result);
136 return -EINVAL;
137 }
138
139 dbi_result_free(result);
140 return 0;
141}
142
Holger Freytherc7b86f92009-06-06 13:54:20 +0000143int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000144 dbi_initialize(NULL);
145 conn = dbi_conn_new("sqlite3");
146 if (conn==NULL) {
147 printf("DB: Failed to create connection.\n");
148 return 1;
149 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000150
Holger Freyther12aa50d2009-01-01 18:02:05 +0000151 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000152
Jan Luebbe5c15c852008-12-27 15:59:25 +0000153 /* MySQL
154 dbi_conn_set_option(conn, "host", "localhost");
155 dbi_conn_set_option(conn, "username", "your_name");
156 dbi_conn_set_option(conn, "password", "your_password");
157 dbi_conn_set_option(conn, "dbname", "your_dbname");
158 dbi_conn_set_option(conn, "encoding", "UTF-8");
159 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000160
Jan Luebbe5c15c852008-12-27 15:59:25 +0000161 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000162 db_basename = strdup(name);
163 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000164 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
165 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000166
Harald Welted0b7b772009-08-09 19:03:42 +0200167 if (dbi_conn_connect(conn) < 0)
168 goto out_err;
169
Jan Luebbe5c15c852008-12-27 15:59:25 +0000170 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200171
172out_err:
173 free(db_dirname);
174 free(db_basename);
175 db_dirname = db_basename = NULL;
176 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000177}
178
Harald Welted0b7b772009-08-09 19:03:42 +0200179
Jan Luebbe7398eb92008-12-27 00:45:41 +0000180int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000181 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000182 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000183
Harald Welte7e310b12009-03-30 20:56:32 +0000184 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
185 result = dbi_conn_query(conn, create_stmts[i]);
186 if (result==NULL) {
187 printf("DB: Failed to create some table.\n");
188 return 1;
189 }
190 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000191 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000192
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200193 if (check_db_revision() < 0) {
194 fprintf(stderr, "Database schema revision invalid, "
195 "please update your database schema\n");
196 return -1;
197 }
198
Jan Luebbe5c15c852008-12-27 15:59:25 +0000199 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000200}
201
202int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000203 dbi_conn_close(conn);
204 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000205
206 if (db_dirname)
207 free(db_dirname);
208 if (db_basename)
209 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000211}
212
Harald Welte9176bd42009-07-23 18:46:00 +0200213struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
214{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000215 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000216 struct gsm_subscriber* subscr;
217
218 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200219 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000220 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000221 result = dbi_conn_queryf(conn,
222 "UPDATE Subscriber set updated = datetime('now') "
223 "WHERE imsi = %s " , imsi);
224 if (result==NULL) {
225 printf("DB: failed to update timestamp\n");
226 } else {
227 dbi_result_free(result);
228 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000229 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000230 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000231
232 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200233 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000234 if (!subscr)
235 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000236 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000237 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000238 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000239 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000240 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000241 imsi
242 );
243 if (result==NULL) {
244 printf("DB: Failed to create Subscriber by IMSI.\n");
245 }
Harald Welte9176bd42009-07-23 18:46:00 +0200246 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000247 subscr->id = dbi_conn_sequence_last(conn, NULL);
248 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000249 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000250 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200251 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000252 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000253}
254
Harald Welte9176bd42009-07-23 18:46:00 +0200255struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
256 enum gsm_subscriber_field field,
257 const char *id)
258{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000259 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000260 const char *string;
261 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000263
Jan Luebbe5c15c852008-12-27 15:59:25 +0000264 switch (field) {
265 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000267 result = dbi_conn_queryf(conn,
268 "SELECT * FROM Subscriber "
269 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000270 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000271 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000272 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000273 break;
274 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000276 result = dbi_conn_queryf(conn,
277 "SELECT * FROM Subscriber "
278 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000279 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000280 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000281 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000282 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000283 case GSM_SUBSCRIBER_EXTENSION:
284 dbi_conn_quote_string_copy(conn, id, &quoted);
285 result = dbi_conn_queryf(conn,
286 "SELECT * FROM Subscriber "
287 "WHERE extension = %s ",
288 quoted
289 );
290 free(quoted);
291 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200292 case GSM_SUBSCRIBER_ID:
293 dbi_conn_quote_string_copy(conn, id, &quoted);
294 result = dbi_conn_queryf(conn,
295 "SELECT * FROM Subscriber "
296 "WHERE id = %s ", quoted);
297 free(quoted);
298 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000299 default:
300 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000301 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000302 }
303 if (result==NULL) {
304 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000305 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000306 }
307 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000308 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
309 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000310 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000311 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000312 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000313
314 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200315 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000316 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000317 string = dbi_result_get_string(result, "imsi");
318 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000319 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000320
321 string = dbi_result_get_string(result, "tmsi");
322 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000323 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000324
325 string = dbi_result_get_string(result, "name");
326 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000327 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000328
329 string = dbi_result_get_string(result, "extension");
330 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000331 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000332
Holger Freyther12aa50d2009-01-01 18:02:05 +0000333 subscr->lac = dbi_result_get_uint(result, "lac");
334 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000335 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
336 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000337 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000339 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000340}
341
Holger Freyther12aa50d2009-01-01 18:02:05 +0000342int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000343 dbi_result result;
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200344 char *q_tmsi;
345 if (subscriber->tmsi[0])
346 dbi_conn_quote_string_copy(conn,
347 subscriber->tmsi,
348 &q_tmsi);
349 else
350 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000351 result = dbi_conn_queryf(conn,
352 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000353 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200354 "name = '%s', "
355 "extension = '%s', "
356 "authorized = %i, "
357 "tmsi = %s, "
358 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000359 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200360 subscriber->name,
361 subscriber->extension,
362 subscriber->authorized,
363 q_tmsi,
364 subscriber->lac,
365 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000366 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200367 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000368 if (result==NULL) {
369 printf("DB: Failed to update Subscriber (by IMSI).\n");
370 return 1;
371 }
372 dbi_result_free(result);
373 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000374}
375
Harald Weltec2e302d2009-07-05 14:08:13 +0200376int db_sync_equipment(struct gsm_equipment *equip)
377{
378 dbi_result result;
379 unsigned char *cm2, *cm3;
380
381 dbi_conn_quote_binary_copy(conn, equip->classmark2,
382 equip->classmark2_len, &cm2);
383 dbi_conn_quote_binary_copy(conn, equip->classmark3,
384 equip->classmark3_len, &cm3);
385
386 result = dbi_conn_queryf(conn,
387 "UPDATE Equipment SET "
388 "updated = datetime('now'), "
389 "classmark1 = %u, "
390 "classmark2 = %s, "
391 "classmark3 = %s "
392 "WHERE imei = '%s' ",
393 equip->classmark1, cm2, cm3, equip->imei);
394
395 free(cm2);
396 free(cm3);
397
398 if (!result) {
399 printf("DB: Failed to update Equipment\n");
400 return -EIO;
401 }
402
403 dbi_result_free(result);
404 return 0;
405}
406
Jan Luebbe5c15c852008-12-27 15:59:25 +0000407int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000408 dbi_result result=NULL;
409 char* tmsi_quoted;
410 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000411 sprintf(subscriber->tmsi, "%i", rand());
412 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000413 result = dbi_conn_queryf(conn,
414 "SELECT * FROM Subscriber "
415 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000416 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000417 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000418 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000419 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000420 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000421 return 1;
422 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000423 if (dbi_result_get_numrows(result)){
424 dbi_result_free(result);
425 continue;
426 }
427 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 dbi_result_free(result);
429 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000430 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000431 }
432 dbi_result_free(result);
433 }
434 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000435}
436
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200437int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
438 dbi_result result=NULL;
439 u_int32_t try;
440 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200441 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200442 result = dbi_conn_queryf(conn,
443 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200444 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200445 try
446 );
447 if (result==NULL) {
448 printf("DB: Failed to query Subscriber while allocating new extension.\n");
449 return 1;
450 }
451 if (dbi_result_get_numrows(result)){
452 dbi_result_free(result);
453 continue;
454 }
455 if (!dbi_result_next_row(result)) {
456 dbi_result_free(result);
457 break;
458 }
459 dbi_result_free(result);
460 }
461 sprintf(subscriber->extension, "%i", try);
462 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
463 return db_sync_subscriber(subscriber);
464}
Jan Luebbe31bef492009-08-12 14:31:14 +0200465/*
466 * try to allocate a new unique token for this subscriber and return it
467 * via a parameter. if the subscriber already has a token, return
468 * an error.
469 */
470
471int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token) {
472 dbi_result result=NULL;
473 u_int32_t try;
474 for (;;) {
475 try = rand();
476 if (!try) /* 0 is an invalid token */
477 continue;
478 result = dbi_conn_queryf(conn,
479 "SELECT * FROM AuthToken "
480 "WHERE subscriber_id = %llu OR token = %08x ",
481 subscriber->id, try
482 );
483 if (result==NULL) {
484 printf("DB: Failed to query AuthToken while allocating new token.\n");
485 return 1;
486 }
487 if (dbi_result_get_numrows(result)){
488 dbi_result_free(result);
489 continue;
490 }
491 if (!dbi_result_next_row(result)) {
492 dbi_result_free(result);
493 break;
494 }
495 dbi_result_free(result);
496 }
497 result = dbi_conn_queryf(conn,
498 "INSERT INTO AuthToken "
499 "(subscriber_id, created, token) "
500 "VALUES "
501 "(%llu, datetime('now'), %08x)) ",
502 subscriber->id, try
503 );
504 if (result==NULL) {
505 printf("DB: Failed to create token %08x for IMSI %s.\n", try, subscriber->imsi);
506 return 1;
507 }
508 *token = try;
509 printf("DB: Allocated token %08x for IMSI %s.\n", try, subscriber->imsi);
510 return 0;
511}
512
Jan Luebbefac25fc2008-12-27 18:04:34 +0000513int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
514 u_int64_t equipment_id, watch_id;
515 dbi_result result;
516
Harald Weltec2e302d2009-07-05 14:08:13 +0200517 strncpy(subscriber->equipment.imei, imei,
518 sizeof(subscriber->equipment.imei)-1),
519
Jan Luebbefac25fc2008-12-27 18:04:34 +0000520 result = dbi_conn_queryf(conn,
521 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000522 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000523 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000524 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000525 imei
526 );
527 if (result==NULL) {
528 printf("DB: Failed to create Equipment by IMEI.\n");
529 return 1;
530 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000531 equipment_id = 0;
532 if (dbi_result_get_numrows_affected(result)) {
533 equipment_id = dbi_conn_sequence_last(conn, NULL);
534 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000535 dbi_result_free(result);
536 if (equipment_id) {
537 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
538 }
539 else {
540 result = dbi_conn_queryf(conn,
541 "SELECT id FROM Equipment "
542 "WHERE imei = %s ",
543 imei
544 );
545 if (result==NULL) {
546 printf("DB: Failed to query Equipment by IMEI.\n");
547 return 1;
548 }
549 if (!dbi_result_next_row(result)) {
550 printf("DB: Failed to find the Equipment.\n");
551 dbi_result_free(result);
552 return 1;
553 }
554 equipment_id = dbi_result_get_ulonglong(result, "id");
555 dbi_result_free(result);
556 }
557
558 result = dbi_conn_queryf(conn,
559 "INSERT OR IGNORE INTO EquipmentWatch "
560 "(subscriber_id, equipment_id, created, updated) "
561 "VALUES "
562 "(%llu, %llu, datetime('now'), datetime('now')) ",
563 subscriber->id, equipment_id
564 );
565 if (result==NULL) {
566 printf("DB: Failed to create EquipmentWatch.\n");
567 return 1;
568 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000569 watch_id = 0;
570 if (dbi_result_get_numrows_affected(result)) {
571 watch_id = dbi_conn_sequence_last(conn, NULL);
572 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000573 dbi_result_free(result);
574 if (watch_id) {
575 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
576 }
577 else {
578 result = dbi_conn_queryf(conn,
579 "UPDATE EquipmentWatch "
580 "SET updated = datetime('now') "
581 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
582 subscriber->id, equipment_id
583 );
584 if (result==NULL) {
585 printf("DB: Failed to update EquipmentWatch.\n");
586 return 1;
587 }
588 dbi_result_free(result);
589 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
590 }
591
592 return 0;
593}
594
Harald Welte7e310b12009-03-30 20:56:32 +0000595/* store an [unsent] SMS to the database */
596int db_sms_store(struct gsm_sms *sms)
597{
598 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200599 char *q_text, *q_daddr;
600 unsigned char *q_udata;
601 char *validity_timestamp = "2222-2-2";
602
603 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000604
605 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200606 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
607 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
608 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200609 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000610 result = dbi_conn_queryf(conn,
611 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200612 "(created, sender_id, receiver_id, valid_until, "
613 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200614 "data_coding_scheme, ud_hdr_ind, dest_addr, "
615 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200616 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200617 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000618 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200619 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
620 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200621 sms->data_coding_scheme, sms->ud_hdr_ind,
622 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000623 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200624 free(q_daddr);
625 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000626
627 if (!result)
628 return -EIO;
629
630 dbi_result_free(result);
631 return 0;
632}
633
Harald Welte2ebabca2009-08-09 19:05:21 +0200634static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000635{
Harald Welte76042182009-08-08 16:03:15 +0200636 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200637 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200638 const char *text, *daddr;
639 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000640
Harald Welte76042182009-08-08 16:03:15 +0200641 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000642 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000643
Harald Weltebe3e3782009-07-05 14:06:41 +0200644 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000645
Harald Weltebe3e3782009-07-05 14:06:41 +0200646 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200647 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200648
649 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200650 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200651
Harald Weltef3efc592009-07-27 20:11:35 +0200652 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200653 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
654 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
655 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
656 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
657 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
658 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200659 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200660 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200661
Harald Welte76042182009-08-08 16:03:15 +0200662 daddr = dbi_result_get_string(result, "dest_addr");
663 if (daddr) {
664 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
665 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
666 }
667
668 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
669 user_data = dbi_result_get_binary(result, "user_data");
670 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200671 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200672 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200673
674 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200675 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200676 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200677 sms->text[sizeof(sms->text)-1] = '\0';
678 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200679 return sms;
680}
681
682/* retrieve the next unsent SMS with ID >= min_id */
683struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
684{
685 dbi_result result;
686 struct gsm_sms *sms;
687
688 result = dbi_conn_queryf(conn,
689 "SELECT * FROM SMS "
690 "WHERE id >= %llu AND sent is NULL ORDER BY id",
691 min_id);
692 if (!result)
693 return NULL;
694
695 if (!dbi_result_next_row(result)) {
696 dbi_result_free(result);
697 return NULL;
698 }
699
700 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000701
702 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200703
704 return sms;
705}
706
707/* retrieve the next unsent SMS with ID >= min_id */
708struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
709{
710 dbi_result result;
711 struct gsm_sms *sms;
712
713 result = dbi_conn_queryf(conn,
714 "SELECT * FROM SMS "
715 "WHERE receiver_id = %llu AND sent is NULL ORDER BY id",
716 subscr->id);
717 if (!result)
718 return NULL;
719
720 if (!dbi_result_next_row(result)) {
721 dbi_result_free(result);
722 return NULL;
723 }
724
725 sms = sms_from_result(subscr->net, result);
726
727 dbi_result_free(result);
728
Harald Welte7e310b12009-03-30 20:56:32 +0000729 return sms;
730}
731
732/* mark a given SMS as read */
733int db_sms_mark_sent(struct gsm_sms *sms)
734{
735 dbi_result result;
736
737 result = dbi_conn_queryf(conn,
738 "UPDATE SMS "
739 "SET sent = datetime('now') "
740 "WHERE id = %llu", sms->id);
741 if (!result) {
742 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
743 return 1;
744 }
745
746 dbi_result_free(result);
747 return 0;
748}