Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 1 | /* Simple HLR/VLR database backend using dbi */ |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 2 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 5 | * 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 23 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 24 | #include <openbsc/gsm_04_11.h> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 25 | #include <openbsc/db.h> |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 26 | #include <openbsc/talloc.h> |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 27 | #include <openbsc/debug.h> |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 28 | #include <openbsc/statistics.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 29 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 30 | #include <libgen.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 34 | #include <errno.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 35 | #include <dbi/dbi.h> |
| 36 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 37 | static char *db_basename = NULL; |
| 38 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 39 | static dbi_conn conn; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 40 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 41 | static char *create_stmts[] = { |
| 42 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 43 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 44 | "key TEXT UNIQUE NOT NULL, " |
| 45 | "value TEXT NOT NULL" |
| 46 | ")", |
| 47 | "INSERT OR IGNORE INTO Meta " |
| 48 | "(key, value) " |
| 49 | "VALUES " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 50 | "('revision', '2')", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 51 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 52 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 53 | "created TIMESTAMP NOT NULL, " |
| 54 | "updated TIMESTAMP NOT NULL, " |
| 55 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 56 | "name TEXT, " |
| 57 | "extension TEXT UNIQUE, " |
| 58 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 59 | "tmsi TEXT UNIQUE, " |
| 60 | "lac INTEGER NOT NULL DEFAULT 0" |
| 61 | ")", |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 62 | "CREATE TABLE IF NOT EXISTS AuthToken (" |
| 63 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 64 | "subscriber_id INTEGER UNIQUE NOT NULL, " |
| 65 | "created TIMESTAMP NOT NULL, " |
| 66 | "token TEXT UNIQUE NOT NULL" |
| 67 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 68 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 69 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 70 | "created TIMESTAMP NOT NULL, " |
| 71 | "updated TIMESTAMP NOT NULL, " |
| 72 | "name TEXT, " |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 73 | "classmark1 NUMERIC, " |
| 74 | "classmark2 BLOB, " |
| 75 | "classmark3 BLOB, " |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 76 | "imei NUMERIC UNIQUE NOT NULL" |
| 77 | ")", |
| 78 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 79 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 80 | "created TIMESTAMP NOT NULL, " |
| 81 | "updated TIMESTAMP NOT NULL, " |
| 82 | "subscriber_id NUMERIC NOT NULL, " |
| 83 | "equipment_id NUMERIC NOT NULL, " |
| 84 | "UNIQUE (subscriber_id, equipment_id) " |
| 85 | ")", |
| 86 | "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 87 | /* metadata, not part of sms */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 88 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 89 | "created TIMESTAMP NOT NULL, " |
| 90 | "sent TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 91 | "sender_id INTEGER NOT NULL, " |
| 92 | "receiver_id INTEGER NOT NULL, " |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 93 | "deliver_attempts INTEGER NOT NULL DEFAULT 0, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 94 | /* data directly copied/derived from SMS */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 95 | "valid_until TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 96 | "reply_path_req INTEGER NOT NULL, " |
| 97 | "status_rep_req INTEGER NOT NULL, " |
| 98 | "protocol_id INTEGER NOT NULL, " |
| 99 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 100 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 101 | "dest_addr TEXT, " |
| 102 | "user_data BLOB, " /* TP-UD */ |
| 103 | /* additional data, interpreted from SMS */ |
| 104 | "header BLOB, " /* UD Header */ |
| 105 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 106 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 107 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 108 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 109 | "created TIMESTAMP NOT NULL, " |
| 110 | "updated TIMESTAMP NOT NULL, " |
| 111 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 112 | "last_bts NUMERIC NOT NULL " |
| 113 | ")", |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 114 | "CREATE TABLE IF NOT EXISTS ApduBlobs (" |
| 115 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 116 | "created TIMESTAMP NOT NULL, " |
| 117 | "apdu_id_flags INTEGER NOT NULL, " |
| 118 | "subscriber_id INTEGER NOT NULL, " |
| 119 | "apdu BLOB " |
| 120 | ")", |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 121 | "CREATE TABLE IF NOT EXISTS Counters (" |
| 122 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 123 | "timestamp TIMESTAMP NOT NULL, " |
Harald Welte | f9a43c4 | 2009-12-22 21:40:42 +0100 | [diff] [blame] | 124 | "value INTEGER NOT NULL, " |
| 125 | "name TEXT NOT NULL " |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame^] | 126 | "CREATE TABLE IF NOT EXISTS AuthKeys (" |
| 127 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 128 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 129 | "algorithm_id NUMERIC NOT NULL, " |
| 130 | "a3a8_ki BLOB " |
| 131 | ")", |
| 132 | "CREATE TABLE IF NOT EXISTS AuthTuples (" |
| 133 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 134 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 135 | "rand BLOB" |
| 136 | "sres BLOB" |
| 137 | "kc BLOB" |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 138 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 141 | void db_error_func(dbi_conn conn, void* data) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 142 | const char* msg; |
| 143 | dbi_conn_error(conn, &msg); |
| 144 | printf("DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 147 | static int check_db_revision(void) |
| 148 | { |
| 149 | dbi_result result; |
| 150 | const char *rev; |
| 151 | |
| 152 | result = dbi_conn_query(conn, |
| 153 | "SELECT value FROM Meta WHERE key='revision'"); |
| 154 | if (!result) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | if (!dbi_result_next_row(result)) { |
| 158 | dbi_result_free(result); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | rev = dbi_result_get_string(result, "value"); |
| 162 | if (!rev || atoi(rev) != 2) { |
| 163 | dbi_result_free(result); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
| 167 | dbi_result_free(result); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Holger Freyther | c7b86f9 | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 171 | int db_init(const char *name) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 172 | dbi_initialize(NULL); |
| 173 | conn = dbi_conn_new("sqlite3"); |
| 174 | if (conn==NULL) { |
| 175 | printf("DB: Failed to create connection.\n"); |
| 176 | return 1; |
| 177 | } |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 178 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 179 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 180 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 181 | /* MySQL |
| 182 | dbi_conn_set_option(conn, "host", "localhost"); |
| 183 | dbi_conn_set_option(conn, "username", "your_name"); |
| 184 | dbi_conn_set_option(conn, "password", "your_password"); |
| 185 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 186 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 187 | */ |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 188 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 189 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 190 | db_basename = strdup(name); |
| 191 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 192 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 193 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 194 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 195 | if (dbi_conn_connect(conn) < 0) |
| 196 | goto out_err; |
| 197 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 198 | return 0; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 199 | |
| 200 | out_err: |
| 201 | free(db_dirname); |
| 202 | free(db_basename); |
| 203 | db_dirname = db_basename = NULL; |
| 204 | return -1; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 207 | |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 208 | int db_prepare() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 209 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 210 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 211 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 212 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 213 | result = dbi_conn_query(conn, create_stmts[i]); |
| 214 | if (result==NULL) { |
| 215 | printf("DB: Failed to create some table.\n"); |
| 216 | return 1; |
| 217 | } |
| 218 | dbi_result_free(result); |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 219 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 220 | |
Holger Hans Peter Freyther | 850326e | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 221 | if (check_db_revision() < 0) { |
| 222 | fprintf(stderr, "Database schema revision invalid, " |
| 223 | "please update your database schema\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 227 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int db_fini() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 231 | dbi_conn_close(conn); |
| 232 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 233 | |
| 234 | if (db_dirname) |
| 235 | free(db_dirname); |
| 236 | if (db_basename) |
| 237 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 238 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 241 | struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi) |
| 242 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 243 | dbi_result result; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 244 | struct gsm_subscriber* subscr; |
| 245 | |
| 246 | /* Is this subscriber known in the db? */ |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 247 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 248 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 249 | result = dbi_conn_queryf(conn, |
| 250 | "UPDATE Subscriber set updated = datetime('now') " |
| 251 | "WHERE imsi = %s " , imsi); |
| 252 | if (result==NULL) { |
| 253 | printf("DB: failed to update timestamp\n"); |
| 254 | } else { |
| 255 | dbi_result_free(result); |
| 256 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 257 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 258 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 259 | |
| 260 | subscr = subscr_alloc(); |
Jan Luebbe | b0dfc31 | 2009-08-12 10:12:52 +0200 | [diff] [blame] | 261 | subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 262 | if (!subscr) |
| 263 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 264 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 265 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 266 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 267 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 268 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 269 | imsi |
| 270 | ); |
| 271 | if (result==NULL) { |
| 272 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 273 | } |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 274 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 275 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 276 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 277 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 278 | printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 279 | db_subscriber_alloc_exten(subscr); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 280 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 283 | static int get_equipment_by_subscr(struct gsm_subscriber *subscr) |
| 284 | { |
| 285 | dbi_result result; |
Harald Welte | 55726d7 | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 286 | const char *string; |
Harald Welte | 4669f3d | 2009-12-09 19:19:45 +0100 | [diff] [blame] | 287 | unsigned char cm1; |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 288 | const unsigned char *cm2, *cm3; |
| 289 | struct gsm_equipment *equip = &subscr->equipment; |
| 290 | |
| 291 | result = dbi_conn_queryf(conn, |
Harald Welte | 55726d7 | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 292 | "SELECT equipment.* FROM Equipment,EquipmentWatch " |
| 293 | "WHERE EquipmentWatch.equipment_id=Equipment.id " |
| 294 | "AND EquipmentWatch.subscriber_id = %llu " |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 295 | "ORDER BY updated DESC", subscr->id); |
| 296 | if (!result) |
| 297 | return -EIO; |
| 298 | |
| 299 | if (!dbi_result_next_row(result)) { |
| 300 | dbi_result_free(result); |
| 301 | return -ENOENT; |
| 302 | } |
| 303 | |
| 304 | equip->id = dbi_result_get_ulonglong(result, "id"); |
| 305 | |
| 306 | string = dbi_result_get_string(result, "imei"); |
| 307 | if (string) |
| 308 | strncpy(equip->imei, string, sizeof(equip->imei)); |
| 309 | |
| 310 | cm1 = dbi_result_get_uint(result, "classmark1") & 0xff; |
| 311 | equip->classmark1 = *((struct gsm48_classmark1 *) &cm1); |
| 312 | |
| 313 | equip->classmark2_len = dbi_result_get_field_length(result, "classmark2"); |
| 314 | cm2 = dbi_result_get_binary(result, "classmark2"); |
| 315 | if (equip->classmark2_len > sizeof(equip->classmark2)) |
| 316 | equip->classmark2_len = sizeof(equip->classmark2); |
| 317 | memcpy(equip->classmark2, cm2, equip->classmark2_len); |
| 318 | |
| 319 | equip->classmark3_len = dbi_result_get_field_length(result, "classmark3"); |
| 320 | cm3 = dbi_result_get_binary(result, "classmark3"); |
| 321 | if (equip->classmark3_len > sizeof(equip->classmark3)) |
| 322 | equip->classmark3_len = sizeof(equip->classmark3); |
| 323 | memcpy(equip->classmark3, cm3, equip->classmark3_len); |
| 324 | |
| 325 | dbi_result_free(result); |
| 326 | |
| 327 | return 0; |
| 328 | } |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame^] | 329 | |
| 330 | int get_authinfo_by_subscr(struct gsm_auth_info *ainfo, |
| 331 | struct gsm_subscriber *subscr) |
| 332 | { |
| 333 | dbi_result result; |
| 334 | const unsigned char *a3a8_ki; |
| 335 | |
| 336 | result = dbi_conn_queryf(conn, |
| 337 | "SELECT * FROM AuthKeys WHERE subscriber_id=%u", |
| 338 | subscr->id); |
| 339 | if (!result) |
| 340 | return -EIO; |
| 341 | |
| 342 | if (!dbi_result_next_row(result)) { |
| 343 | dbi_result_free(result); |
| 344 | return -ENOENT; |
| 345 | } |
| 346 | |
| 347 | ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id"); |
| 348 | ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki"); |
| 349 | a3a8_ki = dbi_result_get_binary(result, "a3a8_ki"); |
| 350 | if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki_len)) |
| 351 | ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki_len); |
| 352 | memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len); |
| 353 | |
| 354 | dbi_result_free(result); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int get_authtuple_by_subscr(struct gsm_auth_tuple *atuple, |
| 360 | struct gsm_subscriber *subscr) |
| 361 | { |
| 362 | dbi_result result; |
| 363 | int len; |
| 364 | const unsigned char *blob; |
| 365 | |
| 366 | result = dbi_conn_queryf(conn, |
| 367 | "SELECT * FROM AuthTuples WHERE subscriber_id=%u", |
| 368 | subscr->id); |
| 369 | if (!result) |
| 370 | return -EIO; |
| 371 | |
| 372 | if (!dbi_result_next_row(result)) { |
| 373 | dbi_result_free(result); |
| 374 | return -ENOENT; |
| 375 | } |
| 376 | |
| 377 | memset(atuple, 0, sizeof(atuple)); |
| 378 | |
| 379 | len = dbi_result_get_field_length(result, "rand"); |
| 380 | if (len != sizeof(atuple->rand)) |
| 381 | goto err_size; |
| 382 | |
| 383 | blob = dbi_result_get_binary(result, "rand"); |
| 384 | memcpy(atuple->rand, blob, len); |
| 385 | |
| 386 | len = dbi_result_get_field_length(result, "sres"); |
| 387 | if (len != sizeof(atuple->sres)) |
| 388 | goto err_size; |
| 389 | |
| 390 | blob = dbi_result_get_binary(result, "sres"); |
| 391 | memcpy(atuple->sres, blob, len); |
| 392 | |
| 393 | len = dbi_result_get_field_length(result, "kc"); |
| 394 | if (len != sizeof(atuple->kc)) |
| 395 | goto err_size; |
| 396 | |
| 397 | blob = dbi_result_get_binary(result, "kc"); |
| 398 | memcpy(atuple->kc, blob, len); |
| 399 | |
| 400 | dbi_result_free(result); |
| 401 | |
| 402 | return 0; |
| 403 | |
| 404 | err_size: |
| 405 | dbi_result_free(result); |
| 406 | return -EIO; |
| 407 | } |
| 408 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 409 | #define BASE_QUERY "SELECT * FROM Subscriber " |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 410 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 411 | enum gsm_subscriber_field field, |
| 412 | const char *id) |
| 413 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 414 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 415 | const char *string; |
| 416 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 417 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 418 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 419 | switch (field) { |
| 420 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 421 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 422 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 423 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 424 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 425 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 426 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 427 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 428 | break; |
| 429 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 430 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 431 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 432 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 433 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 434 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 435 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 436 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 437 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 438 | case GSM_SUBSCRIBER_EXTENSION: |
| 439 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 440 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 441 | BASE_QUERY |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 442 | "WHERE extension = %s ", |
| 443 | quoted |
| 444 | ); |
| 445 | free(quoted); |
| 446 | break; |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 447 | case GSM_SUBSCRIBER_ID: |
| 448 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 449 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 450 | BASE_QUERY |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 451 | "WHERE id = %s ", quoted); |
| 452 | free(quoted); |
| 453 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 454 | default: |
| 455 | printf("DB: Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 456 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 457 | } |
| 458 | if (result==NULL) { |
| 459 | printf("DB: Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 460 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 461 | } |
| 462 | if (!dbi_result_next_row(result)) { |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 463 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 464 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 465 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 466 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 467 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 468 | |
| 469 | subscr = subscr_alloc(); |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 470 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 471 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 472 | string = dbi_result_get_string(result, "imsi"); |
| 473 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 474 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 475 | |
| 476 | string = dbi_result_get_string(result, "tmsi"); |
| 477 | if (string) |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 478 | subscr->tmsi = tmsi_from_string(string); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 479 | |
| 480 | string = dbi_result_get_string(result, "name"); |
| 481 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 482 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 483 | |
| 484 | string = dbi_result_get_string(result, "extension"); |
| 485 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 486 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 487 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 488 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 489 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 490 | printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %u, EXTEN '%s', LAC %hu, AUTH %u\n", |
Holger Freyther | 9175447 | 2009-06-09 08:52:41 +0000 | [diff] [blame] | 491 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 492 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 493 | dbi_result_free(result); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 494 | |
| 495 | get_equipment_by_subscr(subscr); |
| 496 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 497 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 500 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 501 | dbi_result result; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 502 | char tmsi[14]; |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 503 | char *q_tmsi; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 504 | |
| 505 | if (subscriber->tmsi != GSM_RESERVED_TMSI) { |
| 506 | sprintf(tmsi, "%u", subscriber->tmsi); |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 507 | dbi_conn_quote_string_copy(conn, |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 508 | tmsi, |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 509 | &q_tmsi); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 510 | } else |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 511 | q_tmsi = strdup("NULL"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 512 | result = dbi_conn_queryf(conn, |
| 513 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 514 | "SET updated = datetime('now'), " |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 515 | "name = '%s', " |
| 516 | "extension = '%s', " |
| 517 | "authorized = %i, " |
| 518 | "tmsi = %s, " |
| 519 | "lac = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 520 | "WHERE imsi = %s ", |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 521 | subscriber->name, |
| 522 | subscriber->extension, |
| 523 | subscriber->authorized, |
| 524 | q_tmsi, |
| 525 | subscriber->lac, |
| 526 | subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 527 | ); |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 528 | free(q_tmsi); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 529 | if (result==NULL) { |
| 530 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 531 | return 1; |
| 532 | } |
| 533 | dbi_result_free(result); |
| 534 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 537 | int db_sync_equipment(struct gsm_equipment *equip) |
| 538 | { |
| 539 | dbi_result result; |
| 540 | unsigned char *cm2, *cm3; |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 541 | u_int8_t classmark1; |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 542 | |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 543 | memcpy(&classmark1, &equip->classmark1, sizeof(classmark1)); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 544 | printf("DB: Sync Equipment IMEI=%s, classmark1=%02x", |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 545 | equip->imei, classmark1); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 546 | if (equip->classmark2_len) |
| 547 | printf(", classmark2=%s", |
| 548 | hexdump(equip->classmark2, equip->classmark2_len)); |
| 549 | if (equip->classmark3_len) |
| 550 | printf(", classmark3=%s", |
| 551 | hexdump(equip->classmark3, equip->classmark3_len)); |
| 552 | printf("\n"); |
| 553 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 554 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 555 | equip->classmark2_len, &cm2); |
| 556 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 557 | equip->classmark3_len, &cm3); |
| 558 | |
| 559 | result = dbi_conn_queryf(conn, |
| 560 | "UPDATE Equipment SET " |
| 561 | "updated = datetime('now'), " |
| 562 | "classmark1 = %u, " |
| 563 | "classmark2 = %s, " |
| 564 | "classmark3 = %s " |
| 565 | "WHERE imei = '%s' ", |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 566 | classmark1, cm2, cm3, equip->imei); |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 567 | |
| 568 | free(cm2); |
| 569 | free(cm3); |
| 570 | |
| 571 | if (!result) { |
| 572 | printf("DB: Failed to update Equipment\n"); |
| 573 | return -EIO; |
| 574 | } |
| 575 | |
| 576 | dbi_result_free(result); |
| 577 | return 0; |
| 578 | } |
| 579 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 580 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 581 | dbi_result result=NULL; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 582 | char tmsi[14]; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 583 | char* tmsi_quoted; |
| 584 | for (;;) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 585 | subscriber->tmsi = rand(); |
| 586 | if (subscriber->tmsi == GSM_RESERVED_TMSI) |
| 587 | continue; |
| 588 | |
| 589 | sprintf(tmsi, "%u", subscriber->tmsi); |
| 590 | dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 591 | result = dbi_conn_queryf(conn, |
| 592 | "SELECT * FROM Subscriber " |
| 593 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 594 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 595 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 596 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 597 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 598 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 599 | return 1; |
| 600 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 601 | if (dbi_result_get_numrows(result)){ |
| 602 | dbi_result_free(result); |
| 603 | continue; |
| 604 | } |
| 605 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 606 | dbi_result_free(result); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 607 | printf("DB: Allocated TMSI %u for IMSI %s.\n", subscriber->tmsi, subscriber->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 608 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 609 | } |
| 610 | dbi_result_free(result); |
| 611 | } |
| 612 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 615 | int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) { |
| 616 | dbi_result result=NULL; |
| 617 | u_int32_t try; |
| 618 | for (;;) { |
Jan Luebbe | f0b4cef | 2009-08-12 21:27:43 +0200 | [diff] [blame] | 619 | try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 620 | result = dbi_conn_queryf(conn, |
| 621 | "SELECT * FROM Subscriber " |
Jan Luebbe | 1da59ed | 2009-08-12 19:59:27 +0200 | [diff] [blame] | 622 | "WHERE extension = %i", |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 623 | try |
| 624 | ); |
| 625 | if (result==NULL) { |
| 626 | printf("DB: Failed to query Subscriber while allocating new extension.\n"); |
| 627 | return 1; |
| 628 | } |
| 629 | if (dbi_result_get_numrows(result)){ |
| 630 | dbi_result_free(result); |
| 631 | continue; |
| 632 | } |
| 633 | if (!dbi_result_next_row(result)) { |
| 634 | dbi_result_free(result); |
| 635 | break; |
| 636 | } |
| 637 | dbi_result_free(result); |
| 638 | } |
| 639 | sprintf(subscriber->extension, "%i", try); |
| 640 | printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi); |
| 641 | return db_sync_subscriber(subscriber); |
| 642 | } |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 643 | /* |
| 644 | * try to allocate a new unique token for this subscriber and return it |
| 645 | * via a parameter. if the subscriber already has a token, return |
| 646 | * an error. |
| 647 | */ |
| 648 | |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 649 | int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token) |
| 650 | { |
| 651 | dbi_result result; |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 652 | u_int32_t try; |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 653 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 654 | for (;;) { |
| 655 | try = rand(); |
| 656 | if (!try) /* 0 is an invalid token */ |
| 657 | continue; |
| 658 | result = dbi_conn_queryf(conn, |
| 659 | "SELECT * FROM AuthToken " |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 660 | "WHERE subscriber_id = %llu OR token = \"%08X\" ", |
| 661 | subscriber->id, try); |
| 662 | if (!result) { |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 663 | printf("DB: Failed to query AuthToken while allocating new token.\n"); |
| 664 | return 1; |
| 665 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 666 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 667 | dbi_result_free(result); |
| 668 | continue; |
| 669 | } |
| 670 | if (!dbi_result_next_row(result)) { |
| 671 | dbi_result_free(result); |
| 672 | break; |
| 673 | } |
| 674 | dbi_result_free(result); |
| 675 | } |
| 676 | result = dbi_conn_queryf(conn, |
| 677 | "INSERT INTO AuthToken " |
| 678 | "(subscriber_id, created, token) " |
| 679 | "VALUES " |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 680 | "(%llu, datetime('now'), \"%08X\") ", |
| 681 | subscriber->id, try); |
| 682 | if (!result) { |
| 683 | printf("DB: Failed to create token %08X for IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 684 | return 1; |
| 685 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 686 | dbi_result_free(result); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 687 | *token = try; |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 688 | printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi); |
| 689 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 690 | return 0; |
| 691 | } |
| 692 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 693 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
Harald Welte | d409be7 | 2009-11-07 00:06:19 +0900 | [diff] [blame] | 694 | unsigned long long equipment_id, watch_id; |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 695 | dbi_result result; |
| 696 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 697 | strncpy(subscriber->equipment.imei, imei, |
| 698 | sizeof(subscriber->equipment.imei)-1), |
| 699 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 700 | result = dbi_conn_queryf(conn, |
| 701 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 702 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 703 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 704 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 705 | imei |
| 706 | ); |
| 707 | if (result==NULL) { |
| 708 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 709 | return 1; |
| 710 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 711 | equipment_id = 0; |
| 712 | if (dbi_result_get_numrows_affected(result)) { |
| 713 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 714 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 715 | dbi_result_free(result); |
| 716 | if (equipment_id) { |
| 717 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 718 | } |
| 719 | else { |
| 720 | result = dbi_conn_queryf(conn, |
| 721 | "SELECT id FROM Equipment " |
| 722 | "WHERE imei = %s ", |
| 723 | imei |
| 724 | ); |
| 725 | if (result==NULL) { |
| 726 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 727 | return 1; |
| 728 | } |
| 729 | if (!dbi_result_next_row(result)) { |
| 730 | printf("DB: Failed to find the Equipment.\n"); |
| 731 | dbi_result_free(result); |
| 732 | return 1; |
| 733 | } |
| 734 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 735 | dbi_result_free(result); |
| 736 | } |
| 737 | |
| 738 | result = dbi_conn_queryf(conn, |
| 739 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 740 | "(subscriber_id, equipment_id, created, updated) " |
| 741 | "VALUES " |
| 742 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 743 | subscriber->id, equipment_id |
| 744 | ); |
| 745 | if (result==NULL) { |
| 746 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 747 | return 1; |
| 748 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 749 | watch_id = 0; |
| 750 | if (dbi_result_get_numrows_affected(result)) { |
| 751 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 752 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 753 | dbi_result_free(result); |
| 754 | if (watch_id) { |
| 755 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 756 | } |
| 757 | else { |
| 758 | result = dbi_conn_queryf(conn, |
| 759 | "UPDATE EquipmentWatch " |
| 760 | "SET updated = datetime('now') " |
| 761 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 762 | subscriber->id, equipment_id |
| 763 | ); |
| 764 | if (result==NULL) { |
| 765 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 766 | return 1; |
| 767 | } |
| 768 | dbi_result_free(result); |
| 769 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 775 | /* store an [unsent] SMS to the database */ |
| 776 | int db_sms_store(struct gsm_sms *sms) |
| 777 | { |
| 778 | dbi_result result; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 779 | char *q_text, *q_daddr; |
| 780 | unsigned char *q_udata; |
| 781 | char *validity_timestamp = "2222-2-2"; |
| 782 | |
| 783 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 784 | |
| 785 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 786 | dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr); |
| 787 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 788 | &q_udata); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 789 | /* FIXME: correct validity period */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 790 | result = dbi_conn_queryf(conn, |
| 791 | "INSERT INTO SMS " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 792 | "(created, sender_id, receiver_id, valid_until, " |
| 793 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 794 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 795 | "user_data, text) VALUES " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 796 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 797 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 798 | sms->sender->id, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 799 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 800 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 801 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 802 | q_daddr, q_udata, q_text); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 803 | free(q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 804 | free(q_daddr); |
| 805 | free(q_udata); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 806 | |
| 807 | if (!result) |
| 808 | return -EIO; |
| 809 | |
| 810 | dbi_result_free(result); |
| 811 | return 0; |
| 812 | } |
| 813 | |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 814 | static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 815 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 816 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 817 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 818 | const char *text, *daddr; |
| 819 | const unsigned char *user_data; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 820 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 821 | if (!sms) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 822 | return NULL; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 823 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 824 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 825 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 826 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 827 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 828 | |
| 829 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 830 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 831 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 832 | /* FIXME: validity */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 833 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
| 834 | sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); |
| 835 | sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); |
| 836 | sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind"); |
| 837 | sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); |
| 838 | sms->data_coding_scheme = dbi_result_get_uint(result, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 839 | "data_coding_scheme"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 840 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 841 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 842 | daddr = dbi_result_get_string(result, "dest_addr"); |
| 843 | if (daddr) { |
| 844 | strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr)); |
| 845 | sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0'; |
| 846 | } |
| 847 | |
| 848 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 849 | user_data = dbi_result_get_binary(result, "user_data"); |
| 850 | if (sms->user_data_len > sizeof(sms->user_data)) |
Holger Hans Peter Freyther | 966de68 | 2009-08-10 07:56:16 +0200 | [diff] [blame] | 851 | sms->user_data_len = (u_int8_t) sizeof(sms->user_data); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 852 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 853 | |
| 854 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 855 | if (text) { |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 856 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 857 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 858 | } |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 859 | return sms; |
| 860 | } |
| 861 | |
| 862 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 863 | struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id) |
| 864 | { |
| 865 | dbi_result result; |
| 866 | struct gsm_sms *sms; |
| 867 | |
| 868 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 869 | "SELECT * FROM SMS,Subscriber " |
| 870 | "WHERE sms.id >= %llu AND sms.sent is NULL " |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 871 | "AND sms.receiver_id = subscriber.id " |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 872 | "AND subscriber.lac > 0 " |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 873 | "ORDER BY sms.id LIMIT 1", |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 874 | min_id); |
| 875 | if (!result) |
| 876 | return NULL; |
| 877 | |
| 878 | if (!dbi_result_next_row(result)) { |
| 879 | dbi_result_free(result); |
| 880 | return NULL; |
| 881 | } |
| 882 | |
| 883 | sms = sms_from_result(net, result); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 884 | |
| 885 | dbi_result_free(result); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 886 | |
| 887 | return sms; |
| 888 | } |
| 889 | |
Sylvain Munaut | ff1f19e | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 890 | struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, int min_subscr_id) |
| 891 | { |
| 892 | dbi_result result; |
| 893 | struct gsm_sms *sms; |
| 894 | |
| 895 | result = dbi_conn_queryf(conn, |
| 896 | "SELECT * FROM SMS,Subscriber " |
| 897 | "WHERE sms.receiver_id >= %llu AND sms.sent is NULL " |
| 898 | "AND sms.receiver_id = subscriber.id " |
| 899 | "AND subscriber.lac > 0 " |
| 900 | "ORDER BY sms.receiver_id, id LIMIT 1", |
| 901 | min_subscr_id); |
| 902 | if (!result) |
| 903 | return NULL; |
| 904 | |
| 905 | if (!dbi_result_next_row(result)) { |
| 906 | dbi_result_free(result); |
| 907 | return NULL; |
| 908 | } |
| 909 | |
| 910 | sms = sms_from_result(net, result); |
| 911 | |
| 912 | dbi_result_free(result); |
| 913 | |
| 914 | return sms; |
| 915 | } |
| 916 | |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 917 | /* retrieve the next unsent SMS for a given subscriber */ |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 918 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 919 | { |
| 920 | dbi_result result; |
| 921 | struct gsm_sms *sms; |
| 922 | |
| 923 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 924 | "SELECT * FROM SMS,Subscriber " |
| 925 | "WHERE sms.receiver_id = %llu AND sms.sent is NULL " |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 926 | "AND sms.receiver_id = subscriber.id " |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 927 | "AND subscriber.lac > 0 " |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 928 | "ORDER BY sms.id LIMIT 1", |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 929 | subscr->id); |
| 930 | if (!result) |
| 931 | return NULL; |
| 932 | |
| 933 | if (!dbi_result_next_row(result)) { |
| 934 | dbi_result_free(result); |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | sms = sms_from_result(subscr->net, result); |
| 939 | |
| 940 | dbi_result_free(result); |
| 941 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 942 | return sms; |
| 943 | } |
| 944 | |
| 945 | /* mark a given SMS as read */ |
| 946 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 947 | { |
| 948 | dbi_result result; |
| 949 | |
| 950 | result = dbi_conn_queryf(conn, |
| 951 | "UPDATE SMS " |
| 952 | "SET sent = datetime('now') " |
| 953 | "WHERE id = %llu", sms->id); |
| 954 | if (!result) { |
| 955 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 956 | return 1; |
| 957 | } |
| 958 | |
| 959 | dbi_result_free(result); |
| 960 | return 0; |
| 961 | } |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 962 | |
| 963 | /* increase the number of attempted deliveries */ |
| 964 | int db_sms_inc_deliver_attempts(struct gsm_sms *sms) |
| 965 | { |
| 966 | dbi_result result; |
| 967 | |
| 968 | result = dbi_conn_queryf(conn, |
| 969 | "UPDATE SMS " |
| 970 | "SET deliver_attempts = deliver_attempts + 1 " |
| 971 | "WHERE id = %llu", sms->id); |
| 972 | if (!result) { |
| 973 | printf("DB: Failed to inc deliver attempts for SMS %llu.\n", sms->id); |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | dbi_result_free(result); |
| 978 | return 0; |
| 979 | } |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 980 | |
| 981 | int db_apdu_blob_store(struct gsm_subscriber *subscr, |
| 982 | u_int8_t apdu_id_flags, u_int8_t len, |
| 983 | u_int8_t *apdu) |
| 984 | { |
| 985 | dbi_result result; |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 986 | unsigned char *q_apdu; |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 987 | |
| 988 | dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu); |
| 989 | |
| 990 | result = dbi_conn_queryf(conn, |
| 991 | "INSERT INTO ApduBlobs " |
| 992 | "(created,subscriber_id,apdu_id_flags,apdu) VALUES " |
| 993 | "(datetime('now'),%llu,%u,%s)", |
| 994 | subscr->id, apdu_id_flags, q_apdu); |
| 995 | |
| 996 | free(q_apdu); |
| 997 | |
| 998 | if (!result) |
| 999 | return -EIO; |
| 1000 | |
| 1001 | dbi_result_free(result); |
| 1002 | return 0; |
| 1003 | } |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1004 | |
| 1005 | int db_store_counter(struct counter *ctr) |
| 1006 | { |
| 1007 | dbi_result result; |
| 1008 | char *q_name; |
| 1009 | |
| 1010 | dbi_conn_quote_string_copy(conn, ctr->name, &q_name); |
| 1011 | |
| 1012 | result = dbi_conn_queryf(conn, |
| 1013 | "INSERT INTO Counters " |
| 1014 | "(timestamp,name,value) VALUES " |
| 1015 | "(datetime('now'),%s,%lu)", q_name, ctr->value); |
| 1016 | |
| 1017 | free(q_name); |
| 1018 | |
| 1019 | if (!result) |
| 1020 | return -EIO; |
| 1021 | |
| 1022 | dbi_result_free(result); |
| 1023 | return 0; |
| 1024 | } |