blob: 001605a463589799bbda055247d1a111462dfe3e [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;
344 result = dbi_conn_queryf(conn,
345 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000346 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000347 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000348 "lac = %i, "
349 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000350 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000351 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000352 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000353
Jan Luebbe5c15c852008-12-27 15:59:25 +0000354 if (result==NULL) {
355 printf("DB: Failed to update Subscriber (by IMSI).\n");
356 return 1;
357 }
358 dbi_result_free(result);
359 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000360}
361
Harald Weltec2e302d2009-07-05 14:08:13 +0200362int db_sync_equipment(struct gsm_equipment *equip)
363{
364 dbi_result result;
365 unsigned char *cm2, *cm3;
366
367 dbi_conn_quote_binary_copy(conn, equip->classmark2,
368 equip->classmark2_len, &cm2);
369 dbi_conn_quote_binary_copy(conn, equip->classmark3,
370 equip->classmark3_len, &cm3);
371
372 result = dbi_conn_queryf(conn,
373 "UPDATE Equipment SET "
374 "updated = datetime('now'), "
375 "classmark1 = %u, "
376 "classmark2 = %s, "
377 "classmark3 = %s "
378 "WHERE imei = '%s' ",
379 equip->classmark1, cm2, cm3, equip->imei);
380
381 free(cm2);
382 free(cm3);
383
384 if (!result) {
385 printf("DB: Failed to update Equipment\n");
386 return -EIO;
387 }
388
389 dbi_result_free(result);
390 return 0;
391}
392
Jan Luebbe5c15c852008-12-27 15:59:25 +0000393int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000394 dbi_result result=NULL;
395 char* tmsi_quoted;
396 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000397 sprintf(subscriber->tmsi, "%i", rand());
398 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000399 result = dbi_conn_queryf(conn,
400 "SELECT * FROM Subscriber "
401 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000402 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000403 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000404 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000405 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000406 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000407 return 1;
408 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000409 if (dbi_result_get_numrows(result)){
410 dbi_result_free(result);
411 continue;
412 }
413 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000414 dbi_result_free(result);
415 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000416 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000417 }
418 dbi_result_free(result);
419 }
420 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000421}
422
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200423int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
424 dbi_result result=NULL;
425 u_int32_t try;
426 for (;;) {
427 try = (rand()%(GSM_MAX_EXTEN+1)+GSM_MIN_EXTEN)%(GSM_MAX_EXTEN+1);
428 result = dbi_conn_queryf(conn,
429 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200430 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200431 try
432 );
433 if (result==NULL) {
434 printf("DB: Failed to query Subscriber while allocating new extension.\n");
435 return 1;
436 }
437 if (dbi_result_get_numrows(result)){
438 dbi_result_free(result);
439 continue;
440 }
441 if (!dbi_result_next_row(result)) {
442 dbi_result_free(result);
443 break;
444 }
445 dbi_result_free(result);
446 }
447 sprintf(subscriber->extension, "%i", try);
448 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
449 return db_sync_subscriber(subscriber);
450}
Jan Luebbe31bef492009-08-12 14:31:14 +0200451/*
452 * try to allocate a new unique token for this subscriber and return it
453 * via a parameter. if the subscriber already has a token, return
454 * an error.
455 */
456
457int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token) {
458 dbi_result result=NULL;
459 u_int32_t try;
460 for (;;) {
461 try = rand();
462 if (!try) /* 0 is an invalid token */
463 continue;
464 result = dbi_conn_queryf(conn,
465 "SELECT * FROM AuthToken "
466 "WHERE subscriber_id = %llu OR token = %08x ",
467 subscriber->id, try
468 );
469 if (result==NULL) {
470 printf("DB: Failed to query AuthToken while allocating new token.\n");
471 return 1;
472 }
473 if (dbi_result_get_numrows(result)){
474 dbi_result_free(result);
475 continue;
476 }
477 if (!dbi_result_next_row(result)) {
478 dbi_result_free(result);
479 break;
480 }
481 dbi_result_free(result);
482 }
483 result = dbi_conn_queryf(conn,
484 "INSERT INTO AuthToken "
485 "(subscriber_id, created, token) "
486 "VALUES "
487 "(%llu, datetime('now'), %08x)) ",
488 subscriber->id, try
489 );
490 if (result==NULL) {
491 printf("DB: Failed to create token %08x for IMSI %s.\n", try, subscriber->imsi);
492 return 1;
493 }
494 *token = try;
495 printf("DB: Allocated token %08x for IMSI %s.\n", try, subscriber->imsi);
496 return 0;
497}
498
Jan Luebbefac25fc2008-12-27 18:04:34 +0000499int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
500 u_int64_t equipment_id, watch_id;
501 dbi_result result;
502
Harald Weltec2e302d2009-07-05 14:08:13 +0200503 strncpy(subscriber->equipment.imei, imei,
504 sizeof(subscriber->equipment.imei)-1),
505
Jan Luebbefac25fc2008-12-27 18:04:34 +0000506 result = dbi_conn_queryf(conn,
507 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000508 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000509 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000510 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000511 imei
512 );
513 if (result==NULL) {
514 printf("DB: Failed to create Equipment by IMEI.\n");
515 return 1;
516 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000517 equipment_id = 0;
518 if (dbi_result_get_numrows_affected(result)) {
519 equipment_id = dbi_conn_sequence_last(conn, NULL);
520 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000521 dbi_result_free(result);
522 if (equipment_id) {
523 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
524 }
525 else {
526 result = dbi_conn_queryf(conn,
527 "SELECT id FROM Equipment "
528 "WHERE imei = %s ",
529 imei
530 );
531 if (result==NULL) {
532 printf("DB: Failed to query Equipment by IMEI.\n");
533 return 1;
534 }
535 if (!dbi_result_next_row(result)) {
536 printf("DB: Failed to find the Equipment.\n");
537 dbi_result_free(result);
538 return 1;
539 }
540 equipment_id = dbi_result_get_ulonglong(result, "id");
541 dbi_result_free(result);
542 }
543
544 result = dbi_conn_queryf(conn,
545 "INSERT OR IGNORE INTO EquipmentWatch "
546 "(subscriber_id, equipment_id, created, updated) "
547 "VALUES "
548 "(%llu, %llu, datetime('now'), datetime('now')) ",
549 subscriber->id, equipment_id
550 );
551 if (result==NULL) {
552 printf("DB: Failed to create EquipmentWatch.\n");
553 return 1;
554 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000555 watch_id = 0;
556 if (dbi_result_get_numrows_affected(result)) {
557 watch_id = dbi_conn_sequence_last(conn, NULL);
558 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000559 dbi_result_free(result);
560 if (watch_id) {
561 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
562 }
563 else {
564 result = dbi_conn_queryf(conn,
565 "UPDATE EquipmentWatch "
566 "SET updated = datetime('now') "
567 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
568 subscriber->id, equipment_id
569 );
570 if (result==NULL) {
571 printf("DB: Failed to update EquipmentWatch.\n");
572 return 1;
573 }
574 dbi_result_free(result);
575 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
576 }
577
578 return 0;
579}
580
Harald Welte7e310b12009-03-30 20:56:32 +0000581/* store an [unsent] SMS to the database */
582int db_sms_store(struct gsm_sms *sms)
583{
584 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200585 char *q_text, *q_daddr;
586 unsigned char *q_udata;
587 char *validity_timestamp = "2222-2-2";
588
589 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000590
591 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200592 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
593 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
594 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200595 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000596 result = dbi_conn_queryf(conn,
597 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200598 "(created, sender_id, receiver_id, valid_until, "
599 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200600 "data_coding_scheme, ud_hdr_ind, dest_addr, "
601 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200602 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200603 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000604 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200605 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
606 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200607 sms->data_coding_scheme, sms->ud_hdr_ind,
608 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000609 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200610 free(q_daddr);
611 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000612
613 if (!result)
614 return -EIO;
615
616 dbi_result_free(result);
617 return 0;
618}
619
Harald Welte2ebabca2009-08-09 19:05:21 +0200620static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000621{
Harald Welte76042182009-08-08 16:03:15 +0200622 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200623 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200624 const char *text, *daddr;
625 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000626
Harald Welte76042182009-08-08 16:03:15 +0200627 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000628 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000629
Harald Weltebe3e3782009-07-05 14:06:41 +0200630 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000631
Harald Weltebe3e3782009-07-05 14:06:41 +0200632 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200633 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200634
635 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200636 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200637
Harald Weltef3efc592009-07-27 20:11:35 +0200638 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200639 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
640 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
641 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
642 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
643 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
644 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200645 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200646 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200647
Harald Welte76042182009-08-08 16:03:15 +0200648 daddr = dbi_result_get_string(result, "dest_addr");
649 if (daddr) {
650 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
651 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
652 }
653
654 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
655 user_data = dbi_result_get_binary(result, "user_data");
656 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200657 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200658 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200659
660 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200661 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200662 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200663 sms->text[sizeof(sms->text)-1] = '\0';
664 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200665 return sms;
666}
667
668/* retrieve the next unsent SMS with ID >= min_id */
669struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
670{
671 dbi_result result;
672 struct gsm_sms *sms;
673
674 result = dbi_conn_queryf(conn,
675 "SELECT * FROM SMS "
676 "WHERE id >= %llu AND sent is NULL ORDER BY id",
677 min_id);
678 if (!result)
679 return NULL;
680
681 if (!dbi_result_next_row(result)) {
682 dbi_result_free(result);
683 return NULL;
684 }
685
686 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000687
688 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200689
690 return sms;
691}
692
693/* retrieve the next unsent SMS with ID >= min_id */
694struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
695{
696 dbi_result result;
697 struct gsm_sms *sms;
698
699 result = dbi_conn_queryf(conn,
700 "SELECT * FROM SMS "
701 "WHERE receiver_id = %llu AND sent is NULL ORDER BY id",
702 subscr->id);
703 if (!result)
704 return NULL;
705
706 if (!dbi_result_next_row(result)) {
707 dbi_result_free(result);
708 return NULL;
709 }
710
711 sms = sms_from_result(subscr->net, result);
712
713 dbi_result_free(result);
714
Harald Welte7e310b12009-03-30 20:56:32 +0000715 return sms;
716}
717
718/* mark a given SMS as read */
719int db_sms_mark_sent(struct gsm_sms *sms)
720{
721 dbi_result result;
722
723 result = dbi_conn_queryf(conn,
724 "UPDATE SMS "
725 "SET sent = datetime('now') "
726 "WHERE id = %llu", sms->id);
727 if (!result) {
728 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
729 return 1;
730 }
731
732 dbi_result_free(result);
733 return 0;
734}