blob: 270d4d90b397c6ae5098b86edda899ce7a2a8fd1 [file] [log] [blame]
Holger Freyther12aa50d2009-01-01 18:02:05 +00001/* Simple HLR/VLR database backend using dbi */
Jan Luebbefaaa49c2008-12-27 01:07:07 +00002/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00003 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltec2e302d2009-07-05 14:08:13 +02004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte7e310b12009-03-30 20:56:32 +000023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020024#include <openbsc/gsm_04_11.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000025#include <openbsc/db.h>
Harald Welte76042182009-08-08 16:03:15 +020026#include <openbsc/talloc.h>
Harald Welte (local)ee4410a2009-08-17 09:39:55 +020027#include <openbsc/debug.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000028
Holger Freytherbde36102008-12-28 22:51:39 +000029#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000030#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000031#include <stdlib.h>
32#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000033#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000034#include <dbi/dbi.h>
35
Holger Freytherbde36102008-12-28 22:51:39 +000036static char *db_basename = NULL;
37static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000038static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000039
Harald Welte7e310b12009-03-30 20:56:32 +000040static char *create_stmts[] = {
41 "CREATE TABLE IF NOT EXISTS Meta ("
42 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
43 "key TEXT UNIQUE NOT NULL, "
44 "value TEXT NOT NULL"
45 ")",
46 "INSERT OR IGNORE INTO Meta "
47 "(key, value) "
48 "VALUES "
Harald Welted0b7b772009-08-09 19:03:42 +020049 "('revision', '2')",
Harald Welte7e310b12009-03-30 20:56:32 +000050 "CREATE TABLE IF NOT EXISTS Subscriber ("
51 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
52 "created TIMESTAMP NOT NULL, "
53 "updated TIMESTAMP NOT NULL, "
54 "imsi NUMERIC UNIQUE NOT NULL, "
55 "name TEXT, "
56 "extension TEXT UNIQUE, "
57 "authorized INTEGER NOT NULL DEFAULT 0, "
58 "tmsi TEXT UNIQUE, "
59 "lac INTEGER NOT NULL DEFAULT 0"
60 ")",
Jan Luebbe31bef492009-08-12 14:31:14 +020061 "CREATE TABLE IF NOT EXISTS AuthToken ("
62 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
63 "subscriber_id INTEGER UNIQUE NOT NULL, "
64 "created TIMESTAMP NOT NULL, "
65 "token TEXT UNIQUE NOT NULL"
66 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000067 "CREATE TABLE IF NOT EXISTS Equipment ("
68 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
69 "created TIMESTAMP NOT NULL, "
70 "updated TIMESTAMP NOT NULL, "
71 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020072 "classmark1 NUMERIC, "
73 "classmark2 BLOB, "
74 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000075 "imei NUMERIC UNIQUE NOT NULL"
76 ")",
77 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
78 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
79 "created TIMESTAMP NOT NULL, "
80 "updated TIMESTAMP NOT NULL, "
81 "subscriber_id NUMERIC NOT NULL, "
82 "equipment_id NUMERIC NOT NULL, "
83 "UNIQUE (subscriber_id, equipment_id) "
84 ")",
85 "CREATE TABLE IF NOT EXISTS SMS ("
Harald Welte76042182009-08-08 16:03:15 +020086 /* metadata, not part of sms */
Harald Welte7e310b12009-03-30 20:56:32 +000087 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
88 "created TIMESTAMP NOT NULL, "
89 "sent TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020090 "sender_id INTEGER NOT NULL, "
91 "receiver_id INTEGER NOT NULL, "
Harald Welte (local)db552c52009-08-15 20:15:14 +020092 "deliver_attempts INTEGER NOT NULL DEFAULT 0, "
Harald Welte76042182009-08-08 16:03:15 +020093 /* data directly copied/derived from SMS */
Harald Weltef3efc592009-07-27 20:11:35 +020094 "valid_until TIMESTAMP, "
Harald Welte76042182009-08-08 16:03:15 +020095 "reply_path_req INTEGER NOT NULL, "
96 "status_rep_req INTEGER NOT NULL, "
97 "protocol_id INTEGER NOT NULL, "
98 "data_coding_scheme INTEGER NOT NULL, "
Harald Welted0b7b772009-08-09 19:03:42 +020099 "ud_hdr_ind INTEGER NOT NULL, "
Harald Welte76042182009-08-08 16:03:15 +0200100 "dest_addr TEXT, "
101 "user_data BLOB, " /* TP-UD */
102 /* additional data, interpreted from SMS */
103 "header BLOB, " /* UD Header */
104 "text TEXT " /* decoded UD after UDH */
Harald Welte7e310b12009-03-30 20:56:32 +0000105 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +0000106 "CREATE TABLE IF NOT EXISTS VLR ("
107 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
108 "created TIMESTAMP NOT NULL, "
109 "updated TIMESTAMP NOT NULL, "
110 "subscriber_id NUMERIC UNIQUE NOT NULL, "
111 "last_bts NUMERIC NOT NULL "
112 ")",
Harald Welte (local)026531e2009-08-16 10:40:10 +0200113 "CREATE TABLE IF NOT EXISTS ApduBlobs ("
114 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
115 "created TIMESTAMP NOT NULL, "
116 "apdu_id_flags INTEGER NOT NULL, "
117 "subscriber_id INTEGER NOT NULL, "
118 "apdu BLOB "
119 ")",
Harald Welte7e310b12009-03-30 20:56:32 +0000120};
121
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000123 const char* msg;
124 dbi_conn_error(conn, &msg);
125 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000126}
127
Harald Welted0b7b772009-08-09 19:03:42 +0200128static int check_db_revision(void)
129{
130 dbi_result result;
131 const char *rev;
132
133 result = dbi_conn_query(conn,
134 "SELECT value FROM Meta WHERE key='revision'");
135 if (!result)
136 return -EINVAL;
137
138 if (!dbi_result_next_row(result)) {
139 dbi_result_free(result);
140 return -EINVAL;
141 }
142 rev = dbi_result_get_string(result, "value");
143 if (!rev || atoi(rev) != 2) {
144 dbi_result_free(result);
145 return -EINVAL;
146 }
147
148 dbi_result_free(result);
149 return 0;
150}
151
Holger Freytherc7b86f92009-06-06 13:54:20 +0000152int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000153 dbi_initialize(NULL);
154 conn = dbi_conn_new("sqlite3");
155 if (conn==NULL) {
156 printf("DB: Failed to create connection.\n");
157 return 1;
158 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000159
Holger Freyther12aa50d2009-01-01 18:02:05 +0000160 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000161
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 /* MySQL
163 dbi_conn_set_option(conn, "host", "localhost");
164 dbi_conn_set_option(conn, "username", "your_name");
165 dbi_conn_set_option(conn, "password", "your_password");
166 dbi_conn_set_option(conn, "dbname", "your_dbname");
167 dbi_conn_set_option(conn, "encoding", "UTF-8");
168 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000169
Jan Luebbe5c15c852008-12-27 15:59:25 +0000170 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000171 db_basename = strdup(name);
172 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000173 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
174 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000175
Harald Welted0b7b772009-08-09 19:03:42 +0200176 if (dbi_conn_connect(conn) < 0)
177 goto out_err;
178
Jan Luebbe5c15c852008-12-27 15:59:25 +0000179 return 0;
Harald Welted0b7b772009-08-09 19:03:42 +0200180
181out_err:
182 free(db_dirname);
183 free(db_basename);
184 db_dirname = db_basename = NULL;
185 return -1;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000186}
187
Harald Welted0b7b772009-08-09 19:03:42 +0200188
Jan Luebbe7398eb92008-12-27 00:45:41 +0000189int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000190 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000191 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000192
Harald Welte7e310b12009-03-30 20:56:32 +0000193 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
194 result = dbi_conn_query(conn, create_stmts[i]);
195 if (result==NULL) {
196 printf("DB: Failed to create some table.\n");
197 return 1;
198 }
199 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000200 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000201
Holger Hans Peter Freyther850326e2009-08-10 08:36:04 +0200202 if (check_db_revision() < 0) {
203 fprintf(stderr, "Database schema revision invalid, "
204 "please update your database schema\n");
205 return -1;
206 }
207
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209}
210
211int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 dbi_conn_close(conn);
213 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000214
215 if (db_dirname)
216 free(db_dirname);
217 if (db_basename)
218 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000220}
221
Harald Welte9176bd42009-07-23 18:46:00 +0200222struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
223{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225 struct gsm_subscriber* subscr;
226
227 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200228 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000229 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000230 result = dbi_conn_queryf(conn,
231 "UPDATE Subscriber set updated = datetime('now') "
232 "WHERE imsi = %s " , imsi);
233 if (result==NULL) {
234 printf("DB: failed to update timestamp\n");
235 } else {
236 dbi_result_free(result);
237 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000238 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000239 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000240
241 subscr = subscr_alloc();
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200242 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 if (!subscr)
244 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000246 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000247 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000248 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000249 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000250 imsi
251 );
252 if (result==NULL) {
253 printf("DB: Failed to create Subscriber by IMSI.\n");
254 }
Harald Welte9176bd42009-07-23 18:46:00 +0200255 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 subscr->id = dbi_conn_sequence_last(conn, NULL);
257 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000258 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200260 db_subscriber_alloc_exten(subscr);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000261 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000262}
263
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200264static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
265{
266 dbi_result result;
Harald Welte55726d72009-09-26 18:54:59 +0200267 const char *string;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200268 unsigned int cm1;
269 const unsigned char *cm2, *cm3;
270 struct gsm_equipment *equip = &subscr->equipment;
271
272 result = dbi_conn_queryf(conn,
Harald Welte55726d72009-09-26 18:54:59 +0200273 "SELECT equipment.* FROM Equipment,EquipmentWatch "
274 "WHERE EquipmentWatch.equipment_id=Equipment.id "
275 "AND EquipmentWatch.subscriber_id = %llu "
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200276 "ORDER BY updated DESC", subscr->id);
277 if (!result)
278 return -EIO;
279
280 if (!dbi_result_next_row(result)) {
281 dbi_result_free(result);
282 return -ENOENT;
283 }
284
285 equip->id = dbi_result_get_ulonglong(result, "id");
286
287 string = dbi_result_get_string(result, "imei");
288 if (string)
289 strncpy(equip->imei, string, sizeof(equip->imei));
290
291 cm1 = dbi_result_get_uint(result, "classmark1") & 0xff;
292 equip->classmark1 = *((struct gsm48_classmark1 *) &cm1);
293
294 equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
295 cm2 = dbi_result_get_binary(result, "classmark2");
296 if (equip->classmark2_len > sizeof(equip->classmark2))
297 equip->classmark2_len = sizeof(equip->classmark2);
298 memcpy(equip->classmark2, cm2, equip->classmark2_len);
299
300 equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
301 cm3 = dbi_result_get_binary(result, "classmark3");
302 if (equip->classmark3_len > sizeof(equip->classmark3))
303 equip->classmark3_len = sizeof(equip->classmark3);
304 memcpy(equip->classmark3, cm3, equip->classmark3_len);
305
306 dbi_result_free(result);
307
308 return 0;
309}
310#define BASE_QUERY "SELECT * FROM Subscriber "
Harald Welte9176bd42009-07-23 18:46:00 +0200311struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
312 enum gsm_subscriber_field field,
313 const char *id)
314{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000315 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000316 const char *string;
317 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000318 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000319
Jan Luebbe5c15c852008-12-27 15:59:25 +0000320 switch (field) {
321 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000322 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000323 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200324 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000325 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000326 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000327 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000328 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000329 break;
330 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000331 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200333 BASE_QUERY
Jan Luebbe5c15c852008-12-27 15:59:25 +0000334 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000335 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000337 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000339 case GSM_SUBSCRIBER_EXTENSION:
340 dbi_conn_quote_string_copy(conn, id, &quoted);
341 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200342 BASE_QUERY
Holger Freyther9c564b82009-02-09 23:39:20 +0000343 "WHERE extension = %s ",
344 quoted
345 );
346 free(quoted);
347 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200348 case GSM_SUBSCRIBER_ID:
349 dbi_conn_quote_string_copy(conn, id, &quoted);
350 result = dbi_conn_queryf(conn,
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200351 BASE_QUERY
Harald Weltebe3e3782009-07-05 14:06:41 +0200352 "WHERE id = %s ", quoted);
353 free(quoted);
354 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000355 default:
356 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000357 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000358 }
359 if (result==NULL) {
360 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000361 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000362 }
363 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000364 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
365 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000366 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000367 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000368 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000369
370 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200371 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000372 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000373 string = dbi_result_get_string(result, "imsi");
374 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000375 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000376
377 string = dbi_result_get_string(result, "tmsi");
378 if (string)
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200379 subscr->tmsi = tmsi_from_string(string);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000380
381 string = dbi_result_get_string(result, "name");
382 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000383 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000384
385 string = dbi_result_get_string(result, "extension");
386 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000387 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000388
Holger Freyther12aa50d2009-01-01 18:02:05 +0000389 subscr->lac = dbi_result_get_uint(result, "lac");
390 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200391 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n",
Holger Freyther91754472009-06-09 08:52:41 +0000392 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000393 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000394 dbi_result_free(result);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200395
396 get_equipment_by_subscr(subscr);
397
Holger Freyther12aa50d2009-01-01 18:02:05 +0000398 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000399}
400
Holger Freyther12aa50d2009-01-01 18:02:05 +0000401int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000402 dbi_result result;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200403 char tmsi[14];
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200404 char *q_tmsi;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200405
406 if (subscriber->tmsi != GSM_RESERVED_TMSI) {
407 sprintf(tmsi, "%u", subscriber->tmsi);
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200408 dbi_conn_quote_string_copy(conn,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200409 tmsi,
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200410 &q_tmsi);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200411 } else
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200412 q_tmsi = strdup("NULL");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000413 result = dbi_conn_queryf(conn,
414 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000415 "SET updated = datetime('now'), "
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200416 "name = '%s', "
417 "extension = '%s', "
418 "authorized = %i, "
419 "tmsi = %s, "
420 "lac = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000421 "WHERE imsi = %s ",
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200422 subscriber->name,
423 subscriber->extension,
424 subscriber->authorized,
425 q_tmsi,
426 subscriber->lac,
427 subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000428 );
Jan Luebbe9eca37f2009-08-12 21:04:54 +0200429 free(q_tmsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000430 if (result==NULL) {
431 printf("DB: Failed to update Subscriber (by IMSI).\n");
432 return 1;
433 }
434 dbi_result_free(result);
435 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000436}
437
Harald Weltec2e302d2009-07-05 14:08:13 +0200438int db_sync_equipment(struct gsm_equipment *equip)
439{
440 dbi_result result;
441 unsigned char *cm2, *cm3;
442
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200443 printf("DB: Sync Equipment IMEI=%s, classmark1=%02x",
444 equip->imei, equip->classmark1);
445 if (equip->classmark2_len)
446 printf(", classmark2=%s",
447 hexdump(equip->classmark2, equip->classmark2_len));
448 if (equip->classmark3_len)
449 printf(", classmark3=%s",
450 hexdump(equip->classmark3, equip->classmark3_len));
451 printf("\n");
452
Harald Weltec2e302d2009-07-05 14:08:13 +0200453 dbi_conn_quote_binary_copy(conn, equip->classmark2,
454 equip->classmark2_len, &cm2);
455 dbi_conn_quote_binary_copy(conn, equip->classmark3,
456 equip->classmark3_len, &cm3);
457
458 result = dbi_conn_queryf(conn,
459 "UPDATE Equipment SET "
460 "updated = datetime('now'), "
461 "classmark1 = %u, "
462 "classmark2 = %s, "
463 "classmark3 = %s "
464 "WHERE imei = '%s' ",
465 equip->classmark1, cm2, cm3, equip->imei);
466
467 free(cm2);
468 free(cm3);
469
470 if (!result) {
471 printf("DB: Failed to update Equipment\n");
472 return -EIO;
473 }
474
475 dbi_result_free(result);
476 return 0;
477}
478
Jan Luebbe5c15c852008-12-27 15:59:25 +0000479int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000480 dbi_result result=NULL;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200481 char tmsi[14];
Jan Luebbe5c15c852008-12-27 15:59:25 +0000482 char* tmsi_quoted;
483 for (;;) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200484 subscriber->tmsi = rand();
485 if (subscriber->tmsi == GSM_RESERVED_TMSI)
486 continue;
487
488 sprintf(tmsi, "%u", subscriber->tmsi);
489 dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000490 result = dbi_conn_queryf(conn,
491 "SELECT * FROM Subscriber "
492 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000493 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000494 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000495 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000496 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000497 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000498 return 1;
499 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000500 if (dbi_result_get_numrows(result)){
501 dbi_result_free(result);
502 continue;
503 }
504 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000505 dbi_result_free(result);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200506 printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000507 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000508 }
509 dbi_result_free(result);
510 }
511 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000512}
513
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200514int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
515 dbi_result result=NULL;
516 u_int32_t try;
517 for (;;) {
Jan Luebbef0b4cef2009-08-12 21:27:43 +0200518 try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN);
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200519 result = dbi_conn_queryf(conn,
520 "SELECT * FROM Subscriber "
Jan Luebbe1da59ed2009-08-12 19:59:27 +0200521 "WHERE extension = %i",
Jan Luebbeebcce2a2009-08-12 19:45:37 +0200522 try
523 );
524 if (result==NULL) {
525 printf("DB: Failed to query Subscriber while allocating new extension.\n");
526 return 1;
527 }
528 if (dbi_result_get_numrows(result)){
529 dbi_result_free(result);
530 continue;
531 }
532 if (!dbi_result_next_row(result)) {
533 dbi_result_free(result);
534 break;
535 }
536 dbi_result_free(result);
537 }
538 sprintf(subscriber->extension, "%i", try);
539 printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
540 return db_sync_subscriber(subscriber);
541}
Jan Luebbe31bef492009-08-12 14:31:14 +0200542/*
543 * try to allocate a new unique token for this subscriber and return it
544 * via a parameter. if the subscriber already has a token, return
545 * an error.
546 */
547
Harald Welte (local)3feef252009-08-13 13:26:11 +0200548int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token)
549{
550 dbi_result result;
Jan Luebbe31bef492009-08-12 14:31:14 +0200551 u_int32_t try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200552
Jan Luebbe31bef492009-08-12 14:31:14 +0200553 for (;;) {
554 try = rand();
555 if (!try) /* 0 is an invalid token */
556 continue;
557 result = dbi_conn_queryf(conn,
558 "SELECT * FROM AuthToken "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200559 "WHERE subscriber_id = %llu OR token = \"%08X\" ",
560 subscriber->id, try);
561 if (!result) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200562 printf("DB: Failed to query AuthToken while allocating new token.\n");
563 return 1;
564 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200565 if (dbi_result_get_numrows(result)) {
Jan Luebbe31bef492009-08-12 14:31:14 +0200566 dbi_result_free(result);
567 continue;
568 }
569 if (!dbi_result_next_row(result)) {
570 dbi_result_free(result);
571 break;
572 }
573 dbi_result_free(result);
574 }
575 result = dbi_conn_queryf(conn,
576 "INSERT INTO AuthToken "
577 "(subscriber_id, created, token) "
578 "VALUES "
Harald Welte (local)3feef252009-08-13 13:26:11 +0200579 "(%llu, datetime('now'), \"%08X\") ",
580 subscriber->id, try);
581 if (!result) {
582 printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi);
Jan Luebbe31bef492009-08-12 14:31:14 +0200583 return 1;
584 }
Harald Welte (local)3feef252009-08-13 13:26:11 +0200585 dbi_result_free(result);
Jan Luebbe31bef492009-08-12 14:31:14 +0200586 *token = try;
Harald Welte (local)3feef252009-08-13 13:26:11 +0200587 printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
588
Jan Luebbe31bef492009-08-12 14:31:14 +0200589 return 0;
590}
591
Jan Luebbefac25fc2008-12-27 18:04:34 +0000592int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
593 u_int64_t equipment_id, watch_id;
594 dbi_result result;
595
Harald Weltec2e302d2009-07-05 14:08:13 +0200596 strncpy(subscriber->equipment.imei, imei,
597 sizeof(subscriber->equipment.imei)-1),
598
Jan Luebbefac25fc2008-12-27 18:04:34 +0000599 result = dbi_conn_queryf(conn,
600 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000601 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000602 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000603 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000604 imei
605 );
606 if (result==NULL) {
607 printf("DB: Failed to create Equipment by IMEI.\n");
608 return 1;
609 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000610 equipment_id = 0;
611 if (dbi_result_get_numrows_affected(result)) {
612 equipment_id = dbi_conn_sequence_last(conn, NULL);
613 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000614 dbi_result_free(result);
615 if (equipment_id) {
616 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
617 }
618 else {
619 result = dbi_conn_queryf(conn,
620 "SELECT id FROM Equipment "
621 "WHERE imei = %s ",
622 imei
623 );
624 if (result==NULL) {
625 printf("DB: Failed to query Equipment by IMEI.\n");
626 return 1;
627 }
628 if (!dbi_result_next_row(result)) {
629 printf("DB: Failed to find the Equipment.\n");
630 dbi_result_free(result);
631 return 1;
632 }
633 equipment_id = dbi_result_get_ulonglong(result, "id");
634 dbi_result_free(result);
635 }
636
637 result = dbi_conn_queryf(conn,
638 "INSERT OR IGNORE INTO EquipmentWatch "
639 "(subscriber_id, equipment_id, created, updated) "
640 "VALUES "
641 "(%llu, %llu, datetime('now'), datetime('now')) ",
642 subscriber->id, equipment_id
643 );
644 if (result==NULL) {
645 printf("DB: Failed to create EquipmentWatch.\n");
646 return 1;
647 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000648 watch_id = 0;
649 if (dbi_result_get_numrows_affected(result)) {
650 watch_id = dbi_conn_sequence_last(conn, NULL);
651 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000652 dbi_result_free(result);
653 if (watch_id) {
654 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
655 }
656 else {
657 result = dbi_conn_queryf(conn,
658 "UPDATE EquipmentWatch "
659 "SET updated = datetime('now') "
660 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
661 subscriber->id, equipment_id
662 );
663 if (result==NULL) {
664 printf("DB: Failed to update EquipmentWatch.\n");
665 return 1;
666 }
667 dbi_result_free(result);
668 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
669 }
670
671 return 0;
672}
673
Harald Welte7e310b12009-03-30 20:56:32 +0000674/* store an [unsent] SMS to the database */
675int db_sms_store(struct gsm_sms *sms)
676{
677 dbi_result result;
Harald Welte76042182009-08-08 16:03:15 +0200678 char *q_text, *q_daddr;
679 unsigned char *q_udata;
680 char *validity_timestamp = "2222-2-2";
681
682 /* FIXME: generate validity timestamp based on validity_minutes */
Harald Welte7e310b12009-03-30 20:56:32 +0000683
684 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welte76042182009-08-08 16:03:15 +0200685 dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr);
686 dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
687 &q_udata);
Harald Weltef3efc592009-07-27 20:11:35 +0200688 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000689 result = dbi_conn_queryf(conn,
690 "INSERT INTO SMS "
Harald Welte76042182009-08-08 16:03:15 +0200691 "(created, sender_id, receiver_id, valid_until, "
692 "reply_path_req, status_rep_req, protocol_id, "
Harald Welted0b7b772009-08-09 19:03:42 +0200693 "data_coding_scheme, ud_hdr_ind, dest_addr, "
694 "user_data, text) VALUES "
Harald Welte76042182009-08-08 16:03:15 +0200695 "(datetime('now'), %llu, %llu, %u, "
Harald Welted0b7b772009-08-09 19:03:42 +0200696 "%u, %u, %u, %u, %u, %s, %s, %s)",
Harald Welte7e310b12009-03-30 20:56:32 +0000697 sms->sender->id,
Harald Welte76042182009-08-08 16:03:15 +0200698 sms->receiver ? sms->receiver->id : 0, validity_timestamp,
699 sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
Harald Welted0b7b772009-08-09 19:03:42 +0200700 sms->data_coding_scheme, sms->ud_hdr_ind,
701 q_daddr, q_udata, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000702 free(q_text);
Harald Welte76042182009-08-08 16:03:15 +0200703 free(q_daddr);
704 free(q_udata);
Harald Welte7e310b12009-03-30 20:56:32 +0000705
706 if (!result)
707 return -EIO;
708
709 dbi_result_free(result);
710 return 0;
711}
712
Harald Welte2ebabca2009-08-09 19:05:21 +0200713static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
Harald Welte7e310b12009-03-30 20:56:32 +0000714{
Harald Welte76042182009-08-08 16:03:15 +0200715 struct gsm_sms *sms = sms_alloc();
Harald Welte2ebabca2009-08-09 19:05:21 +0200716 long long unsigned int sender_id, receiver_id;
Harald Welte76042182009-08-08 16:03:15 +0200717 const char *text, *daddr;
718 const unsigned char *user_data;
Harald Welte7e310b12009-03-30 20:56:32 +0000719
Harald Welte76042182009-08-08 16:03:15 +0200720 if (!sms)
Harald Welte7e310b12009-03-30 20:56:32 +0000721 return NULL;
Harald Welte7e310b12009-03-30 20:56:32 +0000722
Harald Weltebe3e3782009-07-05 14:06:41 +0200723 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000724
Harald Weltebe3e3782009-07-05 14:06:41 +0200725 sender_id = dbi_result_get_ulonglong(result, "sender_id");
Harald Welte76042182009-08-08 16:03:15 +0200726 sms->sender = subscr_get_by_id(net, sender_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200727
728 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
Harald Welte76042182009-08-08 16:03:15 +0200729 sms->receiver = subscr_get_by_id(net, receiver_id);
Harald Weltebe3e3782009-07-05 14:06:41 +0200730
Harald Weltef3efc592009-07-27 20:11:35 +0200731 /* FIXME: validity */
Harald Welte76042182009-08-08 16:03:15 +0200732 /* FIXME: those should all be get_uchar, but sqlite3 is braindead */
733 sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req");
734 sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req");
735 sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind");
736 sms->protocol_id = dbi_result_get_uint(result, "protocol_id");
737 sms->data_coding_scheme = dbi_result_get_uint(result,
Harald Weltef3efc592009-07-27 20:11:35 +0200738 "data_coding_scheme");
Harald Welte76042182009-08-08 16:03:15 +0200739 /* sms->msg_ref is temporary and not stored in DB */
Harald Weltef3efc592009-07-27 20:11:35 +0200740
Harald Welte76042182009-08-08 16:03:15 +0200741 daddr = dbi_result_get_string(result, "dest_addr");
742 if (daddr) {
743 strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr));
744 sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0';
745 }
746
747 sms->user_data_len = dbi_result_get_field_length(result, "user_data");
748 user_data = dbi_result_get_binary(result, "user_data");
749 if (sms->user_data_len > sizeof(sms->user_data))
Holger Hans Peter Freyther966de682009-08-10 07:56:16 +0200750 sms->user_data_len = (u_int8_t) sizeof(sms->user_data);
Harald Welte76042182009-08-08 16:03:15 +0200751 memcpy(sms->user_data, user_data, sms->user_data_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200752
753 text = dbi_result_get_string(result, "text");
Harald Welte76042182009-08-08 16:03:15 +0200754 if (text) {
Harald Weltebe3e3782009-07-05 14:06:41 +0200755 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte76042182009-08-08 16:03:15 +0200756 sms->text[sizeof(sms->text)-1] = '\0';
757 }
Harald Welte2ebabca2009-08-09 19:05:21 +0200758 return sms;
759}
760
761/* retrieve the next unsent SMS with ID >= min_id */
762struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
763{
764 dbi_result result;
765 struct gsm_sms *sms;
766
767 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200768 "SELECT * FROM SMS,Subscriber "
769 "WHERE sms.id >= %llu AND sms.sent is NULL "
770 "AND subscriber.lac > 0 "
771 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200772 min_id);
773 if (!result)
774 return NULL;
775
776 if (!dbi_result_next_row(result)) {
777 dbi_result_free(result);
778 return NULL;
779 }
780
781 sms = sms_from_result(net, result);
Harald Welte7e310b12009-03-30 20:56:32 +0000782
783 dbi_result_free(result);
Harald Welte2ebabca2009-08-09 19:05:21 +0200784
785 return sms;
786}
787
788/* retrieve the next unsent SMS with ID >= min_id */
789struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
790{
791 dbi_result result;
792 struct gsm_sms *sms;
793
794 result = dbi_conn_queryf(conn,
Harald Welte (local)db552c52009-08-15 20:15:14 +0200795 "SELECT * FROM SMS,Subscriber "
796 "WHERE sms.receiver_id = %llu AND sms.sent is NULL "
797 "AND subscriber.lac > 0 "
798 "ORDER BY id",
Harald Welte2ebabca2009-08-09 19:05:21 +0200799 subscr->id);
800 if (!result)
801 return NULL;
802
803 if (!dbi_result_next_row(result)) {
804 dbi_result_free(result);
805 return NULL;
806 }
807
808 sms = sms_from_result(subscr->net, result);
809
810 dbi_result_free(result);
811
Harald Welte7e310b12009-03-30 20:56:32 +0000812 return sms;
813}
814
815/* mark a given SMS as read */
816int db_sms_mark_sent(struct gsm_sms *sms)
817{
818 dbi_result result;
819
820 result = dbi_conn_queryf(conn,
821 "UPDATE SMS "
822 "SET sent = datetime('now') "
823 "WHERE id = %llu", sms->id);
824 if (!result) {
825 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
826 return 1;
827 }
828
829 dbi_result_free(result);
830 return 0;
831}
Harald Welte (local)db552c52009-08-15 20:15:14 +0200832
833/* increase the number of attempted deliveries */
834int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
835{
836 dbi_result result;
837
838 result = dbi_conn_queryf(conn,
839 "UPDATE SMS "
840 "SET deliver_attempts = deliver_attempts + 1 "
841 "WHERE id = %llu", sms->id);
842 if (!result) {
843 printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id);
844 return 1;
845 }
846
847 dbi_result_free(result);
848 return 0;
849}
Harald Welte (local)026531e2009-08-16 10:40:10 +0200850
851int db_apdu_blob_store(struct gsm_subscriber *subscr,
852 u_int8_t apdu_id_flags, u_int8_t len,
853 u_int8_t *apdu)
854{
855 dbi_result result;
856 char *q_apdu;
857
858 dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
859
860 result = dbi_conn_queryf(conn,
861 "INSERT INTO ApduBlobs "
862 "(created,subscriber_id,apdu_id_flags,apdu) VALUES "
863 "(datetime('now'),%llu,%u,%s)",
864 subscr->id, apdu_id_flags, q_apdu);
865
866 free(q_apdu);
867
868 if (!result)
869 return -EIO;
870
871 dbi_result_free(result);
872 return 0;
873}