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> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 28 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 29 | #include <libgen.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 34 | #include <dbi/dbi.h> |
| 35 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 36 | static char *db_basename = NULL; |
| 37 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 38 | static dbi_conn conn; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 39 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 40 | static 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 Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 49 | "('revision', '2')", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 50 | "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 Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 61 | "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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 67 | "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 Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 72 | "classmark1 NUMERIC, " |
| 73 | "classmark2 BLOB, " |
| 74 | "classmark3 BLOB, " |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 75 | "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 Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 86 | /* metadata, not part of sms */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 87 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 88 | "created TIMESTAMP NOT NULL, " |
| 89 | "sent TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 90 | "sender_id INTEGER NOT NULL, " |
| 91 | "receiver_id INTEGER NOT NULL, " |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 92 | "deliver_attempts INTEGER NOT NULL DEFAULT 0, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 93 | /* data directly copied/derived from SMS */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 94 | "valid_until TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 95 | "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 Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 99 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 100 | "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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 105 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 106 | "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) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 113 | "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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 122 | void db_error_func(dbi_conn conn, void* data) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 123 | const char* msg; |
| 124 | dbi_conn_error(conn, &msg); |
| 125 | printf("DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 128 | static 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 Freyther | c7b86f9 | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 152 | int db_init(const char *name) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 153 | 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 Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 159 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 160 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 161 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 162 | /* 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 Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 169 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 170 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 171 | db_basename = strdup(name); |
| 172 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 173 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 174 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 175 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 176 | if (dbi_conn_connect(conn) < 0) |
| 177 | goto out_err; |
| 178 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 179 | return 0; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 180 | |
| 181 | out_err: |
| 182 | free(db_dirname); |
| 183 | free(db_basename); |
| 184 | db_dirname = db_basename = NULL; |
| 185 | return -1; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 188 | |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 189 | int db_prepare() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 190 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 191 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 192 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 193 | 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 Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 200 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 201 | |
Holger Hans Peter Freyther | 850326e | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 202 | 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 Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 208 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int db_fini() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 212 | dbi_conn_close(conn); |
| 213 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 214 | |
| 215 | if (db_dirname) |
| 216 | free(db_dirname); |
| 217 | if (db_basename) |
| 218 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 219 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 222 | struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi) |
| 223 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 224 | dbi_result result; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 225 | struct gsm_subscriber* subscr; |
| 226 | |
| 227 | /* Is this subscriber known in the db? */ |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 228 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 229 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 230 | 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 Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 238 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 239 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 240 | |
| 241 | subscr = subscr_alloc(); |
Jan Luebbe | b0dfc31 | 2009-08-12 10:12:52 +0200 | [diff] [blame] | 242 | subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 243 | if (!subscr) |
| 244 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 245 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 246 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 247 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 248 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 249 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 250 | imsi |
| 251 | ); |
| 252 | if (result==NULL) { |
| 253 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 254 | } |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 255 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 256 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 257 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 258 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 259 | 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] | 260 | db_subscriber_alloc_exten(subscr); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 261 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 264 | static int get_equipment_by_subscr(struct gsm_subscriber *subscr) |
| 265 | { |
| 266 | dbi_result result; |
Harald Welte | 55726d7 | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 267 | const char *string; |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 268 | unsigned int cm1; |
| 269 | const unsigned char *cm2, *cm3; |
| 270 | struct gsm_equipment *equip = &subscr->equipment; |
| 271 | |
| 272 | result = dbi_conn_queryf(conn, |
Harald Welte | 55726d7 | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 273 | "SELECT equipment.* FROM Equipment,EquipmentWatch " |
| 274 | "WHERE EquipmentWatch.equipment_id=Equipment.id " |
| 275 | "AND EquipmentWatch.subscriber_id = %llu " |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 276 | "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 Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 311 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 312 | enum gsm_subscriber_field field, |
| 313 | const char *id) |
| 314 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 315 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 316 | const char *string; |
| 317 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 318 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 319 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 320 | switch (field) { |
| 321 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 322 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 323 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 324 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 325 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 326 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 327 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 328 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 329 | break; |
| 330 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 331 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 332 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 333 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 334 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 335 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 336 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 337 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 338 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 339 | case GSM_SUBSCRIBER_EXTENSION: |
| 340 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 341 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 342 | BASE_QUERY |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 343 | "WHERE extension = %s ", |
| 344 | quoted |
| 345 | ); |
| 346 | free(quoted); |
| 347 | break; |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 348 | case GSM_SUBSCRIBER_ID: |
| 349 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 350 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 351 | BASE_QUERY |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 352 | "WHERE id = %s ", quoted); |
| 353 | free(quoted); |
| 354 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 355 | default: |
| 356 | printf("DB: Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 357 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 358 | } |
| 359 | if (result==NULL) { |
| 360 | printf("DB: Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 361 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 362 | } |
| 363 | if (!dbi_result_next_row(result)) { |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 364 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 365 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 366 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 367 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 368 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 369 | |
| 370 | subscr = subscr_alloc(); |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 371 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 372 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 373 | string = dbi_result_get_string(result, "imsi"); |
| 374 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 375 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 376 | |
| 377 | string = dbi_result_get_string(result, "tmsi"); |
| 378 | if (string) |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 379 | subscr->tmsi = tmsi_from_string(string); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 380 | |
| 381 | string = dbi_result_get_string(result, "name"); |
| 382 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 383 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 384 | |
| 385 | string = dbi_result_get_string(result, "extension"); |
| 386 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 387 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 388 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 389 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 390 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 391 | 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] | 392 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 393 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 394 | dbi_result_free(result); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 395 | |
| 396 | get_equipment_by_subscr(subscr); |
| 397 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 398 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 401 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 402 | dbi_result result; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 403 | char tmsi[14]; |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 404 | char *q_tmsi; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 405 | |
| 406 | if (subscriber->tmsi != GSM_RESERVED_TMSI) { |
| 407 | sprintf(tmsi, "%u", subscriber->tmsi); |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 408 | dbi_conn_quote_string_copy(conn, |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 409 | tmsi, |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 410 | &q_tmsi); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 411 | } else |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 412 | q_tmsi = strdup("NULL"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 413 | result = dbi_conn_queryf(conn, |
| 414 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 415 | "SET updated = datetime('now'), " |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 416 | "name = '%s', " |
| 417 | "extension = '%s', " |
| 418 | "authorized = %i, " |
| 419 | "tmsi = %s, " |
| 420 | "lac = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 421 | "WHERE imsi = %s ", |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 422 | subscriber->name, |
| 423 | subscriber->extension, |
| 424 | subscriber->authorized, |
| 425 | q_tmsi, |
| 426 | subscriber->lac, |
| 427 | subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 428 | ); |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 429 | free(q_tmsi); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 430 | 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 Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 438 | int db_sync_equipment(struct gsm_equipment *equip) |
| 439 | { |
| 440 | dbi_result result; |
| 441 | unsigned char *cm2, *cm3; |
| 442 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 443 | 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 Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 453 | 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 Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 479 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 480 | dbi_result result=NULL; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 481 | char tmsi[14]; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 482 | char* tmsi_quoted; |
| 483 | for (;;) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 484 | 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 Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 490 | result = dbi_conn_queryf(conn, |
| 491 | "SELECT * FROM Subscriber " |
| 492 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 493 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 494 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 495 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 496 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 497 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 498 | return 1; |
| 499 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 500 | if (dbi_result_get_numrows(result)){ |
| 501 | dbi_result_free(result); |
| 502 | continue; |
| 503 | } |
| 504 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 505 | dbi_result_free(result); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 506 | 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] | 507 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 508 | } |
| 509 | dbi_result_free(result); |
| 510 | } |
| 511 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 514 | int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) { |
| 515 | dbi_result result=NULL; |
| 516 | u_int32_t try; |
| 517 | for (;;) { |
Jan Luebbe | f0b4cef | 2009-08-12 21:27:43 +0200 | [diff] [blame] | 518 | try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 519 | result = dbi_conn_queryf(conn, |
| 520 | "SELECT * FROM Subscriber " |
Jan Luebbe | 1da59ed | 2009-08-12 19:59:27 +0200 | [diff] [blame] | 521 | "WHERE extension = %i", |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 522 | 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 Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 542 | /* |
| 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) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 548 | int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token) |
| 549 | { |
| 550 | dbi_result result; |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 551 | u_int32_t try; |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 552 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 553 | 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) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 559 | "WHERE subscriber_id = %llu OR token = \"%08X\" ", |
| 560 | subscriber->id, try); |
| 561 | if (!result) { |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 562 | printf("DB: Failed to query AuthToken while allocating new token.\n"); |
| 563 | return 1; |
| 564 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 565 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 566 | 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) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 579 | "(%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 Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 583 | return 1; |
| 584 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 585 | dbi_result_free(result); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 586 | *token = try; |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 587 | printf("DB: Allocated token %08X for IMSI %s.\n", try, subscriber->imsi); |
| 588 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 592 | int 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 Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 596 | strncpy(subscriber->equipment.imei, imei, |
| 597 | sizeof(subscriber->equipment.imei)-1), |
| 598 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 599 | result = dbi_conn_queryf(conn, |
| 600 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 601 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 602 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 603 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 604 | imei |
| 605 | ); |
| 606 | if (result==NULL) { |
| 607 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 608 | return 1; |
| 609 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 610 | equipment_id = 0; |
| 611 | if (dbi_result_get_numrows_affected(result)) { |
| 612 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 613 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 614 | 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 Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 648 | watch_id = 0; |
| 649 | if (dbi_result_get_numrows_affected(result)) { |
| 650 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 651 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 652 | 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 674 | /* store an [unsent] SMS to the database */ |
| 675 | int db_sms_store(struct gsm_sms *sms) |
| 676 | { |
| 677 | dbi_result result; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 678 | 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 683 | |
| 684 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 685 | 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 Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 688 | /* FIXME: correct validity period */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 689 | result = dbi_conn_queryf(conn, |
| 690 | "INSERT INTO SMS " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 691 | "(created, sender_id, receiver_id, valid_until, " |
| 692 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 693 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 694 | "user_data, text) VALUES " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 695 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 696 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 697 | sms->sender->id, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 698 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 699 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 700 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 701 | q_daddr, q_udata, q_text); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 702 | free(q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 703 | free(q_daddr); |
| 704 | free(q_udata); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 705 | |
| 706 | if (!result) |
| 707 | return -EIO; |
| 708 | |
| 709 | dbi_result_free(result); |
| 710 | return 0; |
| 711 | } |
| 712 | |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 713 | 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] | 714 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 715 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 716 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 717 | const char *text, *daddr; |
| 718 | const unsigned char *user_data; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 719 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 720 | if (!sms) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 721 | return NULL; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 722 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 723 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 724 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 725 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 726 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 727 | |
| 728 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 729 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 730 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 731 | /* FIXME: validity */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 732 | /* 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 Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 738 | "data_coding_scheme"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 739 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 740 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 741 | 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 Freyther | 966de68 | 2009-08-10 07:56:16 +0200 | [diff] [blame] | 750 | sms->user_data_len = (u_int8_t) sizeof(sms->user_data); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 751 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 752 | |
| 753 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 754 | if (text) { |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 755 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 756 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 757 | } |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 758 | return sms; |
| 759 | } |
| 760 | |
| 761 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 762 | struct 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) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 768 | "SELECT * FROM SMS,Subscriber " |
| 769 | "WHERE sms.id >= %llu AND sms.sent is NULL " |
| 770 | "AND subscriber.lac > 0 " |
| 771 | "ORDER BY id", |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 772 | 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 782 | |
| 783 | dbi_result_free(result); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 784 | |
| 785 | return sms; |
| 786 | } |
| 787 | |
| 788 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 789 | struct 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) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 795 | "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 Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 799 | 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 Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 812 | return sms; |
| 813 | } |
| 814 | |
| 815 | /* mark a given SMS as read */ |
| 816 | int 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) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 832 | |
| 833 | /* increase the number of attempted deliveries */ |
| 834 | int 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) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 850 | |
| 851 | int 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 | } |