Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Simple HLR/VLR database backend using dbi */ |
| 2 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
| 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #include <libgen.h> |
| 25 | #include <stdio.h> |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 26 | #include <stdbool.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <dbi/dbi.h> |
| 31 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 32 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | ef3b59d | 2010-12-22 18:21:14 +0100 | [diff] [blame] | 33 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 34 | #include <openbsc/gsm_04_11.h> |
| 35 | #include <openbsc/db.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | b74772b | 2010-12-22 18:16:01 +0100 | [diff] [blame] | 37 | |
Harald Welte | 02884f2 | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 38 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/talloc.h> |
| 40 | #include <osmocom/core/statistics.h> |
| 41 | #include <osmocom/core/rate_ctr.h> |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 42 | #include <osmocom/core/utils.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 43 | |
Daniel Willmann | 5926596 | 2015-10-08 16:10:23 +0200 | [diff] [blame] | 44 | #include <openssl/rand.h> |
| 45 | |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 46 | /* Semi-Private-Interface (SPI) for the subscriber code */ |
| 47 | void subscr_direct_free(struct gsm_subscriber *subscr); |
| 48 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 49 | static char *db_basename = NULL; |
| 50 | static char *db_dirname = NULL; |
| 51 | static dbi_conn conn; |
| 52 | |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 53 | #define SCHEMA_REVISION "4" |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 54 | |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 55 | enum { |
| 56 | SCHEMA_META, |
| 57 | INSERT_META, |
| 58 | SCHEMA_SUBSCRIBER, |
| 59 | SCHEMA_AUTH, |
| 60 | SCHEMA_EQUIPMENT, |
| 61 | SCHEMA_EQUIPMENT_WATCH, |
| 62 | SCHEMA_SMS, |
| 63 | SCHEMA_VLR, |
| 64 | SCHEMA_APDU, |
| 65 | SCHEMA_COUNTERS, |
| 66 | SCHEMA_RATE, |
| 67 | SCHEMA_AUTHKEY, |
| 68 | SCHEMA_AUTHLAST, |
| 69 | }; |
| 70 | |
| 71 | static const char *create_stmts[] = { |
| 72 | [SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta (" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 73 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 74 | "key TEXT UNIQUE NOT NULL, " |
| 75 | "value TEXT NOT NULL" |
| 76 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 77 | [INSERT_META] = "INSERT OR IGNORE INTO Meta " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 78 | "(key, value) " |
| 79 | "VALUES " |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 80 | "('revision', " SCHEMA_REVISION ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 81 | [SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber (" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 82 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 83 | "created TIMESTAMP NOT NULL, " |
| 84 | "updated TIMESTAMP NOT NULL, " |
| 85 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 86 | "name TEXT, " |
| 87 | "extension TEXT UNIQUE, " |
| 88 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 89 | "tmsi TEXT UNIQUE, " |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 90 | "lac INTEGER NOT NULL DEFAULT 0, " |
| 91 | "expire_lu TIMESTAMP DEFAULT NULL" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 92 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 93 | [SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken (" |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 94 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 95 | "subscriber_id INTEGER UNIQUE NOT NULL, " |
| 96 | "created TIMESTAMP NOT NULL, " |
| 97 | "token TEXT UNIQUE NOT NULL" |
| 98 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 99 | [SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment (" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 100 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 101 | "created TIMESTAMP NOT NULL, " |
| 102 | "updated TIMESTAMP NOT NULL, " |
| 103 | "name TEXT, " |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 104 | "classmark1 NUMERIC, " |
| 105 | "classmark2 BLOB, " |
| 106 | "classmark3 BLOB, " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 107 | "imei NUMERIC UNIQUE NOT NULL" |
| 108 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 109 | [SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 110 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 111 | "created TIMESTAMP NOT NULL, " |
| 112 | "updated TIMESTAMP NOT NULL, " |
| 113 | "subscriber_id NUMERIC NOT NULL, " |
| 114 | "equipment_id NUMERIC NOT NULL, " |
| 115 | "UNIQUE (subscriber_id, equipment_id) " |
| 116 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 117 | [SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 118 | /* metadata, not part of sms */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 119 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 120 | "created TIMESTAMP NOT NULL, " |
| 121 | "sent TIMESTAMP, " |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 122 | "deliver_attempts INTEGER NOT NULL DEFAULT 0, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 123 | /* data directly copied/derived from SMS */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 124 | "valid_until TIMESTAMP, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 125 | "reply_path_req INTEGER NOT NULL, " |
| 126 | "status_rep_req INTEGER NOT NULL, " |
| 127 | "protocol_id INTEGER NOT NULL, " |
| 128 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 129 | "ud_hdr_ind INTEGER NOT NULL, " |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 130 | "src_addr TEXT NOT NULL, " |
| 131 | "src_ton INTEGER NOT NULL, " |
| 132 | "src_npi INTEGER NOT NULL, " |
| 133 | "dest_addr TEXT NOT NULL, " |
| 134 | "dest_ton INTEGER NOT NULL, " |
| 135 | "dest_npi INTEGER NOT NULL, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 136 | "user_data BLOB, " /* TP-UD */ |
| 137 | /* additional data, interpreted from SMS */ |
| 138 | "header BLOB, " /* UD Header */ |
| 139 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 140 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 141 | [SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR (" |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 142 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 143 | "created TIMESTAMP NOT NULL, " |
| 144 | "updated TIMESTAMP NOT NULL, " |
| 145 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 146 | "last_bts NUMERIC NOT NULL " |
| 147 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 148 | [SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs (" |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 149 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 150 | "created TIMESTAMP NOT NULL, " |
| 151 | "apdu_id_flags INTEGER NOT NULL, " |
| 152 | "subscriber_id INTEGER NOT NULL, " |
| 153 | "apdu BLOB " |
| 154 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 155 | [SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters (" |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 156 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 157 | "timestamp TIMESTAMP NOT NULL, " |
Harald Welte | 4051831 | 2009-12-22 21:40:42 +0100 | [diff] [blame] | 158 | "value INTEGER NOT NULL, " |
| 159 | "name TEXT NOT NULL " |
Harald Welte | d574fac | 2009-12-24 09:42:07 +0100 | [diff] [blame] | 160 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 161 | [SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters (" |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 162 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 163 | "timestamp TIMESTAMP NOT NULL, " |
| 164 | "value INTEGER NOT NULL, " |
| 165 | "name TEXT NOT NULL, " |
Harald Welte | 805eb48 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 166 | "idx INTEGER NOT NULL " |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 167 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 168 | [SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys (" |
Sylvain Munaut | 6025f80 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 169 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | 47a79bc | 2009-12-27 19:26:12 +0100 | [diff] [blame] | 170 | "algorithm_id INTEGER NOT NULL, " |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 171 | "a3a8_ki BLOB " |
| 172 | ")", |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 173 | [SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples (" |
Sylvain Munaut | 6025f80 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 174 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | da87636 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 175 | "issued TIMESTAMP NOT NULL, " |
| 176 | "use_count INTEGER NOT NULL DEFAULT 0, " |
| 177 | "key_seq INTEGER NOT NULL, " |
| 178 | "rand BLOB NOT NULL, " |
| 179 | "sres BLOB NOT NULL, " |
| 180 | "kc BLOB NOT NULL " |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 181 | ")", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 182 | }; |
| 183 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 184 | void db_error_func(dbi_conn conn, void *data) |
| 185 | { |
| 186 | const char *msg; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 187 | dbi_conn_error(conn, &msg); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 188 | LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg); |
Harald Welte | 68a82f7 | 2014-07-10 20:18:15 +0200 | [diff] [blame] | 189 | osmo_log_backtrace(DDB, LOGL_ERROR); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 192 | static int update_db_revision_2(void) |
| 193 | { |
| 194 | dbi_result result; |
| 195 | |
| 196 | result = dbi_conn_query(conn, |
| 197 | "ALTER TABLE Subscriber " |
| 198 | "ADD COLUMN expire_lu " |
| 199 | "TIMESTAMP DEFAULT NULL"); |
| 200 | if (!result) { |
| 201 | LOGP(DDB, LOGL_ERROR, |
Alexander Chemeris | 1271c77 | 2014-03-07 16:59:53 +0100 | [diff] [blame] | 202 | "Failed to alter table Subscriber (upgrade from rev 2).\n"); |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | } |
| 205 | dbi_result_free(result); |
| 206 | |
| 207 | result = dbi_conn_query(conn, |
| 208 | "UPDATE Meta " |
| 209 | "SET value = '3' " |
| 210 | "WHERE key = 'revision'"); |
| 211 | if (!result) { |
| 212 | LOGP(DDB, LOGL_ERROR, |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 213 | "Failed to update DB schema revision (upgrade from rev 2).\n"); |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | dbi_result_free(result); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 221 | /** |
| 222 | * Copied from the normal sms_from_result_v3 to avoid having |
| 223 | * to make sure that the real routine will remain backward |
| 224 | * compatible. |
| 225 | */ |
| 226 | static struct gsm_sms *sms_from_result_v3(dbi_result result) |
| 227 | { |
| 228 | struct gsm_sms *sms = sms_alloc(); |
| 229 | long long unsigned int sender_id; |
| 230 | struct gsm_subscriber *sender; |
| 231 | const char *text, *daddr; |
| 232 | const unsigned char *user_data; |
| 233 | char buf[32]; |
| 234 | |
| 235 | if (!sms) |
| 236 | return NULL; |
| 237 | |
| 238 | sms->id = dbi_result_get_ulonglong(result, "id"); |
| 239 | |
| 240 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
| 241 | snprintf(buf, sizeof(buf), "%llu", sender_id); |
| 242 | sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf); |
| 243 | OSMO_ASSERT(sender); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 244 | osmo_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr)); |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 245 | subscr_direct_free(sender); |
| 246 | sender = NULL; |
| 247 | |
Holger Hans Peter Freyther | 77c9f13 | 2014-07-03 14:00:30 +0200 | [diff] [blame] | 248 | sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req"); |
| 249 | sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req"); |
| 250 | sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind"); |
| 251 | sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id"); |
| 252 | sms->data_coding_scheme = dbi_result_get_ulonglong(result, |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 253 | "data_coding_scheme"); |
| 254 | |
| 255 | daddr = dbi_result_get_string(result, "dest_addr"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 256 | if (daddr) |
| 257 | osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 258 | |
| 259 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 260 | user_data = dbi_result_get_binary(result, "user_data"); |
| 261 | if (sms->user_data_len > sizeof(sms->user_data)) |
| 262 | sms->user_data_len = (uint8_t) sizeof(sms->user_data); |
| 263 | memcpy(sms->user_data, user_data, sms->user_data_len); |
| 264 | |
| 265 | text = dbi_result_get_string(result, "text"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 266 | if (text) |
| 267 | osmo_strlcpy(sms->text, text, sizeof(sms->text)); |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 268 | return sms; |
| 269 | } |
| 270 | |
| 271 | static int update_db_revision_3(void) |
| 272 | { |
| 273 | dbi_result result; |
| 274 | struct gsm_sms *sms; |
| 275 | |
Holger Hans Peter Freyther | daf0eea | 2014-03-08 16:41:37 +0100 | [diff] [blame] | 276 | LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n"); |
| 277 | |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 278 | result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION"); |
| 279 | if (!result) { |
| 280 | LOGP(DDB, LOGL_ERROR, |
| 281 | "Failed to begin transaction (upgrade from rev 3)\n"); |
| 282 | return -EINVAL; |
| 283 | } |
| 284 | dbi_result_free(result); |
| 285 | |
| 286 | /* Rename old SMS table to be able create a new one */ |
| 287 | result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3"); |
| 288 | if (!result) { |
| 289 | LOGP(DDB, LOGL_ERROR, |
| 290 | "Failed to rename the old SMS table (upgrade from rev 3).\n"); |
| 291 | goto rollback; |
| 292 | } |
| 293 | dbi_result_free(result); |
| 294 | |
| 295 | /* Create new SMS table with all the bells and whistles! */ |
| 296 | result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]); |
| 297 | if (!result) { |
| 298 | LOGP(DDB, LOGL_ERROR, |
| 299 | "Failed to create a new SMS table (upgrade from rev 3).\n"); |
| 300 | goto rollback; |
| 301 | } |
| 302 | dbi_result_free(result); |
| 303 | |
| 304 | /* Cycle through old messages and convert them to the new format */ |
Max | 2787c75 | 2015-07-29 20:20:28 +0200 | [diff] [blame] | 305 | result = dbi_conn_query(conn, "SELECT * FROM SMS_3"); |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 306 | if (!result) { |
| 307 | LOGP(DDB, LOGL_ERROR, |
| 308 | "Failed fetch messages from the old SMS table (upgrade from rev 3).\n"); |
| 309 | goto rollback; |
| 310 | } |
| 311 | while (dbi_result_next_row(result)) { |
| 312 | sms = sms_from_result_v3(result); |
| 313 | if (db_sms_store(sms) != 0) { |
| 314 | LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n"); |
| 315 | sms_free(sms); |
| 316 | dbi_result_free(result); |
| 317 | goto rollback; |
| 318 | } |
| 319 | sms_free(sms); |
| 320 | } |
| 321 | dbi_result_free(result); |
| 322 | |
| 323 | /* Remove the temporary table */ |
| 324 | result = dbi_conn_query(conn, "DROP TABLE SMS_3"); |
| 325 | if (!result) { |
| 326 | LOGP(DDB, LOGL_ERROR, |
| 327 | "Failed to drop the old SMS table (upgrade from rev 3).\n"); |
| 328 | goto rollback; |
| 329 | } |
| 330 | dbi_result_free(result); |
| 331 | |
| 332 | /* We're done. Bump DB Meta revision to 4 */ |
| 333 | result = dbi_conn_query(conn, |
| 334 | "UPDATE Meta " |
| 335 | "SET value = '4' " |
| 336 | "WHERE key = 'revision'"); |
| 337 | if (!result) { |
| 338 | LOGP(DDB, LOGL_ERROR, |
| 339 | "Failed to update DB schema revision (upgrade from rev 3).\n"); |
| 340 | goto rollback; |
| 341 | } |
| 342 | dbi_result_free(result); |
| 343 | |
| 344 | result = dbi_conn_query(conn, "COMMIT TRANSACTION"); |
| 345 | if (!result) { |
| 346 | LOGP(DDB, LOGL_ERROR, |
| 347 | "Failed to commit the transaction (upgrade from rev 3)\n"); |
| 348 | return -EINVAL; |
Alexander Couzens | 022721f | 2017-02-04 00:20:17 +0100 | [diff] [blame^] | 349 | } else { |
| 350 | dbi_result_free(result); |
Holger Hans Peter Freyther | 1071e0a | 2014-03-07 18:17:22 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* Shrink DB file size by actually wiping out SMS_3 table data */ |
| 354 | result = dbi_conn_query(conn, "VACUUM"); |
| 355 | if (!result) |
| 356 | LOGP(DDB, LOGL_ERROR, |
| 357 | "VACUUM failed. Ignoring it (upgrade from rev 3).\n"); |
| 358 | else |
| 359 | dbi_result_free(result); |
| 360 | |
| 361 | return 0; |
| 362 | |
| 363 | rollback: |
| 364 | result = dbi_conn_query(conn, "ROLLBACK TRANSACTION"); |
| 365 | if (!result) |
| 366 | LOGP(DDB, LOGL_ERROR, |
| 367 | "Rollback failed (upgrade from rev 3).\n"); |
| 368 | else |
| 369 | dbi_result_free(result); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 373 | static int check_db_revision(void) |
| 374 | { |
| 375 | dbi_result result; |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 376 | const char *rev_s; |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 377 | int db_rev = 0; |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 378 | |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 379 | /* Make a query */ |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 380 | result = dbi_conn_query(conn, |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 381 | "SELECT value FROM Meta " |
| 382 | "WHERE key = 'revision'"); |
| 383 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 384 | if (!result) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | if (!dbi_result_next_row(result)) { |
| 388 | dbi_result_free(result); |
| 389 | return -EINVAL; |
| 390 | } |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 391 | |
| 392 | /* Fetch the DB schema revision */ |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 393 | rev_s = dbi_result_get_string(result, "value"); |
| 394 | if (!rev_s) { |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 395 | dbi_result_free(result); |
| 396 | return -EINVAL; |
| 397 | } |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 398 | |
| 399 | if (!strcmp(rev_s, SCHEMA_REVISION)) { |
| 400 | /* Everything is fine */ |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 401 | dbi_result_free(result); |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | db_rev = atoi(rev_s); |
| 406 | dbi_result_free(result); |
| 407 | |
| 408 | /* Incremental migration waterfall */ |
| 409 | switch (db_rev) { |
| 410 | case 2: |
| 411 | if (update_db_revision_2()) |
| 412 | goto error; |
| 413 | case 3: |
| 414 | if (update_db_revision_3()) |
| 415 | goto error; |
| 416 | |
| 417 | /* The end of waterfall */ |
| 418 | break; |
| 419 | default: |
| 420 | LOGP(DDB, LOGL_FATAL, |
| 421 | "Invalid database schema revision '%d'.\n", db_rev); |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 422 | return -EINVAL; |
| 423 | } |
| 424 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 425 | return 0; |
Vadim Yanitskiy | f77ac9a | 2016-05-13 15:38:09 +0600 | [diff] [blame] | 426 | |
| 427 | error: |
| 428 | LOGP(DDB, LOGL_FATAL, "Failed to update database " |
| 429 | "from schema revision '%d'.\n", db_rev); |
| 430 | return -EINVAL; |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int db_configure(void) |
| 434 | { |
| 435 | dbi_result result; |
| 436 | |
| 437 | result = dbi_conn_query(conn, |
| 438 | "PRAGMA synchronous = FULL"); |
| 439 | if (!result) |
| 440 | return -EINVAL; |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 441 | |
| 442 | dbi_result_free(result); |
| 443 | return 0; |
| 444 | } |
| 445 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 446 | int db_init(const char *name) |
| 447 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 448 | dbi_initialize(NULL); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 449 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 450 | conn = dbi_conn_new("sqlite3"); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 451 | if (conn == NULL) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 452 | LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
| 457 | |
| 458 | /* MySQL |
| 459 | dbi_conn_set_option(conn, "host", "localhost"); |
| 460 | dbi_conn_set_option(conn, "username", "your_name"); |
| 461 | dbi_conn_set_option(conn, "password", "your_password"); |
| 462 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 463 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 464 | */ |
| 465 | |
| 466 | /* SqLite 3 */ |
| 467 | db_basename = strdup(name); |
| 468 | db_dirname = strdup(name); |
| 469 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 470 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
| 471 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 472 | if (dbi_conn_connect(conn) < 0) |
| 473 | goto out_err; |
| 474 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 475 | return 0; |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 476 | |
| 477 | out_err: |
| 478 | free(db_dirname); |
| 479 | free(db_basename); |
| 480 | db_dirname = db_basename = NULL; |
| 481 | return -1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 482 | } |
| 483 | |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 484 | |
Harald Welte | a26aeba | 2011-07-16 13:24:09 +0200 | [diff] [blame] | 485 | int db_prepare(void) |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 486 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 487 | dbi_result result; |
| 488 | int i; |
| 489 | |
| 490 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 491 | result = dbi_conn_query(conn, create_stmts[i]); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 492 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 493 | LOGP(DDB, LOGL_ERROR, |
| 494 | "Failed to create some table.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 495 | return 1; |
| 496 | } |
| 497 | dbi_result_free(result); |
| 498 | } |
| 499 | |
Holger Hans Peter Freyther | d8d0035 | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 500 | if (check_db_revision() < 0) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 501 | LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, " |
Holger Hans Peter Freyther | d8d0035 | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 502 | "please update your database schema\n"); |
| 503 | return -1; |
| 504 | } |
| 505 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 506 | db_configure(); |
| 507 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
Harald Welte | a26aeba | 2011-07-16 13:24:09 +0200 | [diff] [blame] | 511 | int db_fini(void) |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 512 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 513 | dbi_conn_close(conn); |
| 514 | dbi_shutdown(); |
| 515 | |
Harald Welte | b4660bd | 2011-07-16 13:22:57 +0200 | [diff] [blame] | 516 | free(db_dirname); |
| 517 | free(db_basename); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Max | 3911533 | 2016-06-07 15:32:16 +0200 | [diff] [blame] | 521 | struct gsm_subscriber *db_create_subscriber(const char *imsi, uint64_t smin, |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 522 | uint64_t smax, bool alloc_exten) |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 523 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 524 | dbi_result result; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 525 | struct gsm_subscriber *subscr; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 526 | |
| 527 | /* Is this subscriber known in the db? */ |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 528 | subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 529 | if (subscr) { |
Holger Hans Peter Freyther | 19206b8 | 2016-04-01 20:21:03 +0200 | [diff] [blame] | 530 | subscr_put(subscr); |
| 531 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | subscr = subscr_alloc(); |
| 535 | if (!subscr) |
| 536 | return NULL; |
Harald Welte | b4660bd | 2011-07-16 13:22:57 +0200 | [diff] [blame] | 537 | subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 538 | result = dbi_conn_queryf(conn, |
| 539 | "INSERT INTO Subscriber " |
| 540 | "(imsi, created, updated) " |
| 541 | "VALUES " |
| 542 | "(%s, datetime('now'), datetime('now')) ", |
| 543 | imsi |
| 544 | ); |
Holger Hans Peter Freyther | 9a2fa40 | 2016-04-01 20:31:11 +0200 | [diff] [blame] | 545 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 546 | LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n"); |
Holger Hans Peter Freyther | 9a2fa40 | 2016-04-01 20:31:11 +0200 | [diff] [blame] | 547 | subscr_put(subscr); |
| 548 | return NULL; |
| 549 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 550 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 551 | osmo_strlcpy(subscr->imsi, imsi, sizeof(subscr->imsi)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 552 | dbi_result_free(result); |
Harald Welte (local) | c9ba256 | 2009-12-26 18:57:32 +0100 | [diff] [blame] | 553 | LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 554 | if (alloc_exten) |
| 555 | db_subscriber_alloc_exten(subscr, smin, smax); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 556 | return subscr; |
| 557 | } |
| 558 | |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 559 | osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size); |
Holger Hans Peter Freyther | 5a5ee60 | 2010-03-30 15:28:36 +0200 | [diff] [blame] | 560 | |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 561 | static int get_equipment_by_subscr(struct gsm_subscriber *subscr) |
| 562 | { |
| 563 | dbi_result result; |
Harald Welte | 044008d | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 564 | const char *string; |
Harald Welte | f330bf4 | 2009-12-09 19:19:45 +0100 | [diff] [blame] | 565 | unsigned char cm1; |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 566 | const unsigned char *cm2, *cm3; |
| 567 | struct gsm_equipment *equip = &subscr->equipment; |
| 568 | |
| 569 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 570 | "SELECT Equipment.* " |
| 571 | "FROM Equipment JOIN EquipmentWatch ON " |
| 572 | "EquipmentWatch.equipment_id=Equipment.id " |
| 573 | "WHERE EquipmentWatch.subscriber_id = %llu " |
| 574 | "ORDER BY EquipmentWatch.updated DESC", subscr->id); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 575 | if (!result) |
| 576 | return -EIO; |
| 577 | |
| 578 | if (!dbi_result_next_row(result)) { |
| 579 | dbi_result_free(result); |
| 580 | return -ENOENT; |
| 581 | } |
| 582 | |
| 583 | equip->id = dbi_result_get_ulonglong(result, "id"); |
| 584 | |
| 585 | string = dbi_result_get_string(result, "imei"); |
| 586 | if (string) |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 587 | osmo_strlcpy(equip->imei, string, sizeof(equip->imei)); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 588 | |
Harald Welte (local) | 990ca03 | 2009-12-26 18:56:00 +0100 | [diff] [blame] | 589 | string = dbi_result_get_string(result, "classmark1"); |
Holger Hans Peter Freyther | 5a5ee60 | 2010-03-30 15:28:36 +0200 | [diff] [blame] | 590 | if (string) { |
| 591 | cm1 = atoi(string) & 0xff; |
| 592 | memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1)); |
| 593 | } |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 594 | |
| 595 | equip->classmark2_len = dbi_result_get_field_length(result, "classmark2"); |
| 596 | cm2 = dbi_result_get_binary(result, "classmark2"); |
| 597 | if (equip->classmark2_len > sizeof(equip->classmark2)) |
| 598 | equip->classmark2_len = sizeof(equip->classmark2); |
Holger Hans Peter Freyther | 2c38f71 | 2016-01-23 09:21:04 +0100 | [diff] [blame] | 599 | if (cm2) |
| 600 | memcpy(equip->classmark2, cm2, equip->classmark2_len); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 601 | |
| 602 | equip->classmark3_len = dbi_result_get_field_length(result, "classmark3"); |
| 603 | cm3 = dbi_result_get_binary(result, "classmark3"); |
| 604 | if (equip->classmark3_len > sizeof(equip->classmark3)) |
| 605 | equip->classmark3_len = sizeof(equip->classmark3); |
Holger Hans Peter Freyther | 2c38f71 | 2016-01-23 09:21:04 +0100 | [diff] [blame] | 606 | if (cm3) |
| 607 | memcpy(equip->classmark3, cm3, equip->classmark3_len); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 608 | |
| 609 | dbi_result_free(result); |
| 610 | |
| 611 | return 0; |
| 612 | } |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 613 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 614 | int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 615 | struct gsm_subscriber *subscr) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 616 | { |
| 617 | dbi_result result; |
| 618 | const unsigned char *a3a8_ki; |
| 619 | |
| 620 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 621 | "SELECT * FROM AuthKeys WHERE subscriber_id=%llu", |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 622 | subscr->id); |
| 623 | if (!result) |
| 624 | return -EIO; |
| 625 | |
| 626 | if (!dbi_result_next_row(result)) { |
| 627 | dbi_result_free(result); |
| 628 | return -ENOENT; |
| 629 | } |
| 630 | |
| 631 | ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id"); |
| 632 | ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki"); |
| 633 | a3a8_ki = dbi_result_get_binary(result, "a3a8_ki"); |
Sylvain Munaut | 00b043a | 2009-12-27 19:24:05 +0100 | [diff] [blame] | 634 | if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki)) |
Sylvain Munaut | 40b7dfd | 2012-05-07 22:09:15 +0200 | [diff] [blame] | 635 | ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki); |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 636 | memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len); |
| 637 | |
| 638 | dbi_result_free(result); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 643 | int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 644 | struct gsm_subscriber *subscr) |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 645 | { |
| 646 | dbi_result result; |
| 647 | struct gsm_auth_info ainfo_old; |
| 648 | int rc, upd; |
| 649 | unsigned char *ki_str; |
| 650 | |
| 651 | /* Deletion ? */ |
| 652 | if (ainfo == NULL) { |
| 653 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 654 | "DELETE FROM AuthKeys WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 655 | subscr->id); |
| 656 | |
| 657 | if (!result) |
| 658 | return -EIO; |
| 659 | |
| 660 | dbi_result_free(result); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Check if already existing */ |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 666 | rc = db_get_authinfo_for_subscr(&ainfo_old, subscr); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 667 | if (rc && rc != -ENOENT) |
| 668 | return rc; |
| 669 | upd = rc ? 0 : 1; |
| 670 | |
| 671 | /* Update / Insert */ |
| 672 | dbi_conn_quote_binary_copy(conn, |
| 673 | ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str); |
| 674 | |
| 675 | if (!upd) { |
| 676 | result = dbi_conn_queryf(conn, |
| 677 | "INSERT INTO AuthKeys " |
| 678 | "(subscriber_id, algorithm_id, a3a8_ki) " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 679 | "VALUES (%llu, %u, %s)", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 680 | subscr->id, ainfo->auth_algo, ki_str); |
| 681 | } else { |
| 682 | result = dbi_conn_queryf(conn, |
| 683 | "UPDATE AuthKeys " |
| 684 | "SET algorithm_id=%u, a3a8_ki=%s " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 685 | "WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 686 | ainfo->auth_algo, ki_str, subscr->id); |
| 687 | } |
| 688 | |
| 689 | free(ki_str); |
| 690 | |
| 691 | if (!result) |
| 692 | return -EIO; |
| 693 | |
| 694 | dbi_result_free(result); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 699 | int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 700 | struct gsm_subscriber *subscr) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 701 | { |
| 702 | dbi_result result; |
| 703 | int len; |
| 704 | const unsigned char *blob; |
| 705 | |
| 706 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 707 | "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu", |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 708 | subscr->id); |
| 709 | if (!result) |
| 710 | return -EIO; |
| 711 | |
| 712 | if (!dbi_result_next_row(result)) { |
| 713 | dbi_result_free(result); |
| 714 | return -ENOENT; |
| 715 | } |
| 716 | |
Holger Hans Peter Freyther | 461a0d8 | 2013-07-04 20:24:02 +0200 | [diff] [blame] | 717 | memset(atuple, 0, sizeof(*atuple)); |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 718 | |
Sylvain Munaut | da87636 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 719 | atuple->use_count = dbi_result_get_ulonglong(result, "use_count"); |
| 720 | atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq"); |
| 721 | |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 722 | len = dbi_result_get_field_length(result, "rand"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 723 | if (len != sizeof(atuple->vec.rand)) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 724 | goto err_size; |
| 725 | |
| 726 | blob = dbi_result_get_binary(result, "rand"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 727 | memcpy(atuple->vec.rand, blob, len); |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 728 | |
| 729 | len = dbi_result_get_field_length(result, "sres"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 730 | if (len != sizeof(atuple->vec.sres)) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 731 | goto err_size; |
| 732 | |
| 733 | blob = dbi_result_get_binary(result, "sres"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 734 | memcpy(atuple->vec.sres, blob, len); |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 735 | |
| 736 | len = dbi_result_get_field_length(result, "kc"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 737 | if (len != sizeof(atuple->vec.kc)) |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 738 | goto err_size; |
| 739 | |
| 740 | blob = dbi_result_get_binary(result, "kc"); |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 741 | memcpy(atuple->vec.kc, blob, len); |
Harald Welte | 28f7c57 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 742 | |
| 743 | dbi_result_free(result); |
| 744 | |
| 745 | return 0; |
| 746 | |
| 747 | err_size: |
| 748 | dbi_result_free(result); |
| 749 | return -EIO; |
| 750 | } |
| 751 | |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 752 | int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 753 | struct gsm_subscriber *subscr) |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 754 | { |
| 755 | dbi_result result; |
| 756 | int rc, upd; |
| 757 | struct gsm_auth_tuple atuple_old; |
| 758 | unsigned char *rand_str, *sres_str, *kc_str; |
| 759 | |
| 760 | /* Deletion ? */ |
| 761 | if (atuple == NULL) { |
| 762 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 763 | "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 764 | subscr->id); |
| 765 | |
| 766 | if (!result) |
| 767 | return -EIO; |
| 768 | |
| 769 | dbi_result_free(result); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* Check if already existing */ |
Sylvain Munaut | 48858d2 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 775 | rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 776 | if (rc && rc != -ENOENT) |
| 777 | return rc; |
| 778 | upd = rc ? 0 : 1; |
| 779 | |
| 780 | /* Update / Insert */ |
| 781 | dbi_conn_quote_binary_copy(conn, |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 782 | atuple->vec.rand, sizeof(atuple->vec.rand), &rand_str); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 783 | dbi_conn_quote_binary_copy(conn, |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 784 | atuple->vec.sres, sizeof(atuple->vec.sres), &sres_str); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 785 | dbi_conn_quote_binary_copy(conn, |
Harald Welte | 34ef4c5 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 786 | atuple->vec.kc, sizeof(atuple->vec.kc), &kc_str); |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 787 | |
| 788 | if (!upd) { |
| 789 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | f190bdf | 2010-06-09 13:03:39 +0200 | [diff] [blame] | 790 | "INSERT INTO AuthLastTuples " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 791 | "(subscriber_id, issued, use_count, " |
| 792 | "key_seq, rand, sres, kc) " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 793 | "VALUES (%llu, datetime('now'), %u, " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 794 | "%u, %s, %s, %s ) ", |
| 795 | subscr->id, atuple->use_count, atuple->key_seq, |
| 796 | rand_str, sres_str, kc_str); |
| 797 | } else { |
| 798 | char *issued = atuple->key_seq == atuple_old.key_seq ? |
| 799 | "issued" : "datetime('now')"; |
| 800 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | ef710ae | 2010-06-10 22:26:21 +0200 | [diff] [blame] | 801 | "UPDATE AuthLastTuples " |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 802 | "SET issued=%s, use_count=%u, " |
| 803 | "key_seq=%u, rand=%s, sres=%s, kc=%s " |
Sylvain Munaut | e795f67 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 804 | "WHERE subscriber_id = %llu", |
Sylvain Munaut | deab241 | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 805 | issued, atuple->use_count, atuple->key_seq, |
| 806 | rand_str, sres_str, kc_str, subscr->id); |
| 807 | } |
| 808 | |
| 809 | free(rand_str); |
| 810 | free(sres_str); |
| 811 | free(kc_str); |
| 812 | |
| 813 | if (!result) |
| 814 | return -EIO; |
| 815 | |
| 816 | dbi_result_free(result); |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 821 | static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result) |
| 822 | { |
| 823 | const char *string; |
| 824 | string = dbi_result_get_string(result, "imsi"); |
| 825 | if (string) |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 826 | osmo_strlcpy(subscr->imsi, string, sizeof(subscr->imsi)); |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 827 | |
| 828 | string = dbi_result_get_string(result, "tmsi"); |
| 829 | if (string) |
| 830 | subscr->tmsi = tmsi_from_string(string); |
| 831 | |
| 832 | string = dbi_result_get_string(result, "name"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 833 | if (string) |
| 834 | osmo_strlcpy(subscr->name, string, sizeof(subscr->name)); |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 835 | |
| 836 | string = dbi_result_get_string(result, "extension"); |
| 837 | if (string) |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 838 | osmo_strlcpy(subscr->extension, string, sizeof(subscr->extension)); |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 839 | |
Kevin Redon | 3dc0ab1 | 2013-11-04 22:43:15 +0100 | [diff] [blame] | 840 | subscr->lac = dbi_result_get_ulonglong(result, "lac"); |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 841 | |
| 842 | if (!dbi_result_field_is_null(result, "expire_lu")) |
| 843 | subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu"); |
| 844 | else |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 845 | subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION; |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 846 | |
Kevin Redon | 3dc0ab1 | 2013-11-04 22:43:15 +0100 | [diff] [blame] | 847 | subscr->authorized = dbi_result_get_ulonglong(result, "authorized"); |
| 848 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 851 | #define BASE_QUERY "SELECT * FROM Subscriber " |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 852 | struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 853 | const char *id) |
| 854 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 855 | dbi_result result; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 856 | char *quoted; |
| 857 | struct gsm_subscriber *subscr; |
| 858 | |
| 859 | switch (field) { |
| 860 | case GSM_SUBSCRIBER_IMSI: |
| 861 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 862 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 863 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 864 | "WHERE imsi = %s ", |
| 865 | quoted |
| 866 | ); |
| 867 | free(quoted); |
| 868 | break; |
| 869 | case GSM_SUBSCRIBER_TMSI: |
| 870 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 871 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 872 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 873 | "WHERE tmsi = %s ", |
| 874 | quoted |
| 875 | ); |
| 876 | free(quoted); |
| 877 | break; |
| 878 | case GSM_SUBSCRIBER_EXTENSION: |
| 879 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 880 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 881 | BASE_QUERY |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 882 | "WHERE extension = %s ", |
| 883 | quoted |
| 884 | ); |
| 885 | free(quoted); |
| 886 | break; |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 887 | case GSM_SUBSCRIBER_ID: |
| 888 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 889 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 890 | BASE_QUERY |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 891 | "WHERE id = %s ", quoted); |
| 892 | free(quoted); |
| 893 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 894 | default: |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 895 | LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 896 | return NULL; |
| 897 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 898 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 899 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 900 | return NULL; |
| 901 | } |
| 902 | if (!dbi_result_next_row(result)) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 903 | DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 904 | field, id); |
| 905 | dbi_result_free(result); |
| 906 | return NULL; |
| 907 | } |
| 908 | |
| 909 | subscr = subscr_alloc(); |
| 910 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 911 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 912 | db_set_from_query(subscr, result); |
Neels Hofmeyr | cac441c | 2016-05-09 21:28:05 +0200 | [diff] [blame] | 913 | DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %x, EXTEN '%s', LAC %hu, AUTH %u\n", |
Harald Welte | 5c33631 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 914 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
| 915 | subscr->lac, subscr->authorized); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 916 | dbi_result_free(result); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 917 | |
| 918 | get_equipment_by_subscr(subscr); |
| 919 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 920 | return subscr; |
| 921 | } |
| 922 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 923 | int db_subscriber_update(struct gsm_subscriber *subscr) |
| 924 | { |
| 925 | char buf[32]; |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 926 | dbi_result result; |
| 927 | |
| 928 | /* Copy the id to a string as queryf with %llu is failing */ |
| 929 | sprintf(buf, "%llu", subscr->id); |
| 930 | result = dbi_conn_queryf(conn, |
| 931 | BASE_QUERY |
| 932 | "WHERE id = %s", buf); |
| 933 | |
| 934 | if (!result) { |
| 935 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id); |
| 936 | return -EIO; |
| 937 | } |
| 938 | if (!dbi_result_next_row(result)) { |
| 939 | DEBUGP(DDB, "Failed to find the Subscriber. %llu\n", |
| 940 | subscr->id); |
| 941 | dbi_result_free(result); |
| 942 | return -EIO; |
| 943 | } |
| 944 | |
| 945 | db_set_from_query(subscr, result); |
| 946 | dbi_result_free(result); |
| 947 | get_equipment_by_subscr(subscr); |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 952 | int db_sync_subscriber(struct gsm_subscriber *subscriber) |
| 953 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 954 | dbi_result result; |
Harald Welte | 5c33631 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 955 | char tmsi[14]; |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 956 | char *q_tmsi, *q_name, *q_extension; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 957 | |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 958 | dbi_conn_quote_string_copy(conn, |
| 959 | subscriber->name, &q_name); |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 960 | if (subscriber->extension[0] != '\0') |
| 961 | dbi_conn_quote_string_copy(conn, |
| 962 | subscriber->extension, &q_extension); |
| 963 | else |
| 964 | q_extension = strdup("NULL"); |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 965 | |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 966 | if (subscriber->tmsi != GSM_RESERVED_TMSI) { |
Harald Welte | 5c33631 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 967 | sprintf(tmsi, "%u", subscriber->tmsi); |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 968 | dbi_conn_quote_string_copy(conn, |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 969 | tmsi, |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 970 | &q_tmsi); |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 971 | } else |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 972 | q_tmsi = strdup("NULL"); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 973 | |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 974 | if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) { |
| 975 | result = dbi_conn_queryf(conn, |
| 976 | "UPDATE Subscriber " |
| 977 | "SET updated = datetime('now'), " |
| 978 | "name = %s, " |
| 979 | "extension = %s, " |
| 980 | "authorized = %i, " |
| 981 | "tmsi = %s, " |
| 982 | "lac = %i, " |
| 983 | "expire_lu = NULL " |
| 984 | "WHERE imsi = %s ", |
| 985 | q_name, |
| 986 | q_extension, |
| 987 | subscriber->authorized, |
| 988 | q_tmsi, |
| 989 | subscriber->lac, |
| 990 | subscriber->imsi); |
| 991 | } else { |
| 992 | result = dbi_conn_queryf(conn, |
| 993 | "UPDATE Subscriber " |
| 994 | "SET updated = datetime('now'), " |
| 995 | "name = %s, " |
| 996 | "extension = %s, " |
| 997 | "authorized = %i, " |
| 998 | "tmsi = %s, " |
| 999 | "lac = %i, " |
| 1000 | "expire_lu = datetime(%i, 'unixepoch') " |
| 1001 | "WHERE imsi = %s ", |
| 1002 | q_name, |
| 1003 | q_extension, |
| 1004 | subscriber->authorized, |
| 1005 | q_tmsi, |
| 1006 | subscriber->lac, |
| 1007 | (int) subscriber->expire_lu, |
| 1008 | subscriber->imsi); |
| 1009 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1010 | |
Jan Luebbe | f4ac9a7 | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 1011 | free(q_tmsi); |
Harald Welte | 447bc5a | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 1012 | free(q_name); |
| 1013 | free(q_extension); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1014 | |
| 1015 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1016 | LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1017 | return 1; |
| 1018 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1019 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1020 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1021 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 1025 | int db_subscriber_delete(struct gsm_subscriber *subscr) |
| 1026 | { |
| 1027 | dbi_result result; |
| 1028 | |
| 1029 | result = dbi_conn_queryf(conn, |
| 1030 | "DELETE FROM AuthKeys WHERE subscriber_id=%llu", |
| 1031 | subscr->id); |
| 1032 | if (!result) { |
| 1033 | LOGP(DDB, LOGL_ERROR, |
| 1034 | "Failed to delete Authkeys for %llu\n", subscr->id); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | dbi_result_free(result); |
| 1038 | |
| 1039 | result = dbi_conn_queryf(conn, |
| 1040 | "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu", |
| 1041 | subscr->id); |
| 1042 | if (!result) { |
| 1043 | LOGP(DDB, LOGL_ERROR, |
| 1044 | "Failed to delete AuthLastTuples for %llu\n", subscr->id); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | dbi_result_free(result); |
| 1048 | |
| 1049 | result = dbi_conn_queryf(conn, |
| 1050 | "DELETE FROM AuthToken WHERE subscriber_id=%llu", |
| 1051 | subscr->id); |
| 1052 | if (!result) { |
| 1053 | LOGP(DDB, LOGL_ERROR, |
| 1054 | "Failed to delete AuthToken for %llu\n", subscr->id); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | dbi_result_free(result); |
| 1058 | |
| 1059 | result = dbi_conn_queryf(conn, |
| 1060 | "DELETE FROM EquipmentWatch WHERE subscriber_id=%llu", |
| 1061 | subscr->id); |
| 1062 | if (!result) { |
| 1063 | LOGP(DDB, LOGL_ERROR, |
| 1064 | "Failed to delete EquipmentWatch for %llu\n", subscr->id); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | dbi_result_free(result); |
| 1068 | |
Max | 5872641 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 1069 | if (subscr->extension[0] != '\0') { |
| 1070 | result = dbi_conn_queryf(conn, |
| 1071 | "DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s", |
| 1072 | subscr->extension, subscr->extension); |
| 1073 | if (!result) { |
| 1074 | LOGP(DDB, LOGL_ERROR, |
| 1075 | "Failed to delete SMS for %llu\n", subscr->id); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | dbi_result_free(result); |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 1079 | } |
Holger Hans Peter Freyther | c652a5d | 2014-03-23 14:01:08 +0100 | [diff] [blame] | 1080 | |
| 1081 | result = dbi_conn_queryf(conn, |
| 1082 | "DELETE FROM VLR WHERE subscriber_id=%llu", |
| 1083 | subscr->id); |
| 1084 | if (!result) { |
| 1085 | LOGP(DDB, LOGL_ERROR, |
| 1086 | "Failed to delete VLR for %llu\n", subscr->id); |
| 1087 | return -1; |
| 1088 | } |
| 1089 | dbi_result_free(result); |
| 1090 | |
| 1091 | result = dbi_conn_queryf(conn, |
| 1092 | "DELETE FROM ApduBlobs WHERE subscriber_id=%llu", |
| 1093 | subscr->id); |
| 1094 | if (!result) { |
| 1095 | LOGP(DDB, LOGL_ERROR, |
| 1096 | "Failed to delete ApduBlobs for %llu\n", subscr->id); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | dbi_result_free(result); |
| 1100 | |
| 1101 | result = dbi_conn_queryf(conn, |
| 1102 | "DELETE FROM Subscriber WHERE id=%llu", |
| 1103 | subscr->id); |
| 1104 | if (!result) { |
| 1105 | LOGP(DDB, LOGL_ERROR, |
| 1106 | "Failed to delete Subscriber for %llu\n", subscr->id); |
| 1107 | return -1; |
| 1108 | } |
| 1109 | dbi_result_free(result); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Holger Hans Peter Freyther | d41b7b7 | 2014-03-23 16:22:55 +0100 | [diff] [blame] | 1114 | /** |
| 1115 | * List all the authorized and non-expired subscribers. The callback will |
| 1116 | * be called one by one. The subscr argument is not fully initialize and |
| 1117 | * subscr_get/subscr_put must not be called. The passed in pointer will be |
| 1118 | * deleted after the callback by the database call. |
| 1119 | */ |
| 1120 | int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure) |
| 1121 | { |
| 1122 | dbi_result result; |
| 1123 | |
Max | 2787c75 | 2015-07-29 20:20:28 +0200 | [diff] [blame] | 1124 | result = dbi_conn_query(conn, |
| 1125 | "SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1"); |
Holger Hans Peter Freyther | d41b7b7 | 2014-03-23 16:22:55 +0100 | [diff] [blame] | 1126 | if (!result) { |
| 1127 | LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n"); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | while (dbi_result_next_row(result)) { |
| 1132 | struct gsm_subscriber *subscr; |
| 1133 | |
| 1134 | subscr = subscr_alloc(); |
| 1135 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
| 1136 | db_set_from_query(subscr, result); |
| 1137 | cb(subscr, closure); |
| 1138 | OSMO_ASSERT(subscr->use_count == 1); |
| 1139 | llist_del(&subscr->entry); |
| 1140 | talloc_free(subscr); |
| 1141 | } |
| 1142 | |
| 1143 | dbi_result_free(result); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1147 | int db_sync_equipment(struct gsm_equipment *equip) |
| 1148 | { |
| 1149 | dbi_result result; |
| 1150 | unsigned char *cm2, *cm3; |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 1151 | char *q_imei; |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1152 | uint8_t classmark1; |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1153 | |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 1154 | memcpy(&classmark1, &equip->classmark1, sizeof(classmark1)); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1155 | DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x", |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 1156 | equip->imei, classmark1); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 1157 | if (equip->classmark2_len) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1158 | DEBUGPC(DDB, ", classmark2=%s", |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 1159 | osmo_hexdump(equip->classmark2, equip->classmark2_len)); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 1160 | if (equip->classmark3_len) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1161 | DEBUGPC(DDB, ", classmark3=%s", |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 1162 | osmo_hexdump(equip->classmark3, equip->classmark3_len)); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1163 | DEBUGPC(DDB, "\n"); |
Harald Welte (local) | ced09ed | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 1164 | |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1165 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 1166 | equip->classmark2_len, &cm2); |
| 1167 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 1168 | equip->classmark3_len, &cm3); |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 1169 | dbi_conn_quote_string_copy(conn, equip->imei, &q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1170 | |
| 1171 | result = dbi_conn_queryf(conn, |
| 1172 | "UPDATE Equipment SET " |
| 1173 | "updated = datetime('now'), " |
| 1174 | "classmark1 = %u, " |
| 1175 | "classmark2 = %s, " |
| 1176 | "classmark3 = %s " |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 1177 | "WHERE imei = %s ", |
| 1178 | classmark1, cm2, cm3, q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1179 | |
| 1180 | free(cm2); |
| 1181 | free(cm3); |
Holger Hans Peter Freyther | f29c9bf | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 1182 | free(q_imei); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1183 | |
| 1184 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1185 | LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n"); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1186 | return -EIO; |
| 1187 | } |
| 1188 | |
| 1189 | dbi_result_free(result); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 1193 | int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id)) |
| 1194 | { |
| 1195 | dbi_result result; |
| 1196 | |
| 1197 | result = dbi_conn_query(conn, |
| 1198 | "SELECT id " |
| 1199 | "FROM Subscriber " |
| 1200 | "WHERE lac != 0 AND " |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 1201 | "( expire_lu is NOT NULL " |
| 1202 | "AND expire_lu < datetime('now') ) " |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 1203 | "LIMIT 1"); |
| 1204 | if (!result) { |
| 1205 | LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n"); |
| 1206 | return -EIO; |
| 1207 | } |
| 1208 | |
| 1209 | while (dbi_result_next_row(result)) |
| 1210 | callback(priv, dbi_result_get_ulonglong(result, "id")); |
| 1211 | |
| 1212 | dbi_result_free(result); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1216 | int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber) |
| 1217 | { |
| 1218 | dbi_result result = NULL; |
Harald Welte | 5c33631 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 1219 | char tmsi[14]; |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 1220 | char *tmsi_quoted; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1221 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1222 | for (;;) { |
Daniel Willmann | 5926596 | 2015-10-08 16:10:23 +0200 | [diff] [blame] | 1223 | if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) { |
| 1224 | LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n"); |
| 1225 | return 1; |
| 1226 | } |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 1227 | if (subscriber->tmsi == GSM_RESERVED_TMSI) |
| 1228 | continue; |
| 1229 | |
Harald Welte | 5c33631 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 1230 | sprintf(tmsi, "%u", subscriber->tmsi); |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 1231 | dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1232 | result = dbi_conn_queryf(conn, |
| 1233 | "SELECT * FROM Subscriber " |
| 1234 | "WHERE tmsi = %s ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1235 | tmsi_quoted); |
| 1236 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1237 | free(tmsi_quoted); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1238 | |
| 1239 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1240 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 1241 | "while allocating new TMSI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1242 | return 1; |
| 1243 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1244 | if (dbi_result_get_numrows(result)) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1245 | dbi_result_free(result); |
| 1246 | continue; |
| 1247 | } |
| 1248 | if (!dbi_result_next_row(result)) { |
| 1249 | dbi_result_free(result); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1250 | DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n", |
| 1251 | subscriber->tmsi, subscriber->imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1252 | return db_sync_subscriber(subscriber); |
| 1253 | } |
| 1254 | dbi_result_free(result); |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Max | 3911533 | 2016-06-07 15:32:16 +0200 | [diff] [blame] | 1259 | int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber, uint64_t smin, |
| 1260 | uint64_t smax) |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1261 | { |
| 1262 | dbi_result result = NULL; |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1263 | uint32_t try; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1264 | |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 1265 | for (;;) { |
Max | 3911533 | 2016-06-07 15:32:16 +0200 | [diff] [blame] | 1266 | try = (rand() % (smax - smin + 1) + smin); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 1267 | result = dbi_conn_queryf(conn, |
| 1268 | "SELECT * FROM Subscriber " |
Jan Luebbe | 70a9a0f | 2009-08-12 19:59:27 +0200 | [diff] [blame] | 1269 | "WHERE extension = %i", |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 1270 | try |
| 1271 | ); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1272 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1273 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 1274 | "while allocating new extension.\n"); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 1275 | return 1; |
| 1276 | } |
| 1277 | if (dbi_result_get_numrows(result)){ |
| 1278 | dbi_result_free(result); |
| 1279 | continue; |
| 1280 | } |
| 1281 | if (!dbi_result_next_row(result)) { |
| 1282 | dbi_result_free(result); |
| 1283 | break; |
| 1284 | } |
| 1285 | dbi_result_free(result); |
| 1286 | } |
| 1287 | sprintf(subscriber->extension, "%i", try); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1288 | DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | 62cc86d | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 1289 | return db_sync_subscriber(subscriber); |
| 1290 | } |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1291 | /* |
| 1292 | * try to allocate a new unique token for this subscriber and return it |
| 1293 | * via a parameter. if the subscriber already has a token, return |
| 1294 | * an error. |
| 1295 | */ |
| 1296 | |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1297 | int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token) |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1298 | { |
| 1299 | dbi_result result; |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1300 | uint32_t try; |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1301 | |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1302 | for (;;) { |
Daniel Willmann | 14368df | 2015-10-08 16:10:26 +0200 | [diff] [blame] | 1303 | if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) { |
| 1304 | LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n"); |
| 1305 | return 1; |
| 1306 | } |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1307 | if (!try) /* 0 is an invalid token */ |
| 1308 | continue; |
| 1309 | result = dbi_conn_queryf(conn, |
| 1310 | "SELECT * FROM AuthToken " |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1311 | "WHERE subscriber_id = %llu OR token = \"%08X\" ", |
| 1312 | subscriber->id, try); |
| 1313 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1314 | LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken " |
| 1315 | "while allocating new token.\n"); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1316 | return 1; |
| 1317 | } |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1318 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1319 | dbi_result_free(result); |
| 1320 | continue; |
| 1321 | } |
| 1322 | if (!dbi_result_next_row(result)) { |
| 1323 | dbi_result_free(result); |
| 1324 | break; |
| 1325 | } |
| 1326 | dbi_result_free(result); |
| 1327 | } |
| 1328 | result = dbi_conn_queryf(conn, |
| 1329 | "INSERT INTO AuthToken " |
| 1330 | "(subscriber_id, created, token) " |
| 1331 | "VALUES " |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1332 | "(%llu, datetime('now'), \"%08X\") ", |
| 1333 | subscriber->id, try); |
| 1334 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1335 | LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for " |
| 1336 | "IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1337 | return 1; |
| 1338 | } |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1339 | dbi_result_free(result); |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1340 | *token = try; |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1341 | DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi); |
Harald Welte (local) | 1b47047 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 1342 | |
Jan Luebbe | bb7501f | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
Harald Welte | 02884f2 | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 1346 | int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM23003_IMEISV_NUM_DIGITS]) |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1347 | { |
Harald Welte | 26abffa | 2009-11-07 00:06:19 +0900 | [diff] [blame] | 1348 | unsigned long long equipment_id, watch_id; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1349 | dbi_result result; |
| 1350 | |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1351 | osmo_strlcpy(subscriber->equipment.imei, imei, sizeof(subscriber->equipment.imei)); |
Harald Welte | f6845a7 | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 1352 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1353 | result = dbi_conn_queryf(conn, |
| 1354 | "INSERT OR IGNORE INTO Equipment " |
| 1355 | "(imei, created, updated) " |
| 1356 | "VALUES " |
| 1357 | "(%s, datetime('now'), datetime('now')) ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1358 | imei); |
| 1359 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1360 | LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1361 | return 1; |
| 1362 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1363 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1364 | equipment_id = 0; |
| 1365 | if (dbi_result_get_numrows_affected(result)) { |
| 1366 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 1367 | } |
| 1368 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1369 | |
| 1370 | if (equipment_id) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1371 | DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1372 | else { |
| 1373 | result = dbi_conn_queryf(conn, |
| 1374 | "SELECT id FROM Equipment " |
| 1375 | "WHERE imei = %s ", |
| 1376 | imei |
| 1377 | ); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1378 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1379 | LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1380 | return 1; |
| 1381 | } |
| 1382 | if (!dbi_result_next_row(result)) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1383 | LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1384 | dbi_result_free(result); |
| 1385 | return 1; |
| 1386 | } |
| 1387 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 1388 | dbi_result_free(result); |
| 1389 | } |
| 1390 | |
| 1391 | result = dbi_conn_queryf(conn, |
| 1392 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 1393 | "(subscriber_id, equipment_id, created, updated) " |
| 1394 | "VALUES " |
| 1395 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1396 | subscriber->id, equipment_id); |
| 1397 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1398 | LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1399 | return 1; |
| 1400 | } |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1401 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1402 | watch_id = 0; |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1403 | if (dbi_result_get_numrows_affected(result)) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1404 | watch_id = dbi_conn_sequence_last(conn, NULL); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1405 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1406 | dbi_result_free(result); |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1407 | if (watch_id) |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1408 | DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 1409 | equipment_id, subscriber->imsi, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1410 | else { |
| 1411 | result = dbi_conn_queryf(conn, |
| 1412 | "UPDATE EquipmentWatch " |
| 1413 | "SET updated = datetime('now') " |
| 1414 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
Harald Welte | eff28ac | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1415 | subscriber->id, equipment_id); |
| 1416 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1417 | LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1418 | return 1; |
| 1419 | } |
| 1420 | dbi_result_free(result); |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1421 | DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 1422 | equipment_id, subscriber->imsi, imei); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | /* store an [unsent] SMS to the database */ |
| 1429 | int db_sms_store(struct gsm_sms *sms) |
| 1430 | { |
| 1431 | dbi_result result; |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1432 | char *q_text, *q_daddr, *q_saddr; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1433 | unsigned char *q_udata; |
| 1434 | char *validity_timestamp = "2222-2-2"; |
| 1435 | |
| 1436 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1437 | |
| 1438 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | fde4916 | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 1439 | dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1440 | dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1441 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 1442 | &q_udata); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1443 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1444 | /* FIXME: correct validity period */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1445 | result = dbi_conn_queryf(conn, |
| 1446 | "INSERT INTO SMS " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1447 | "(created, valid_until, " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1448 | "reply_path_req, status_rep_req, protocol_id, " |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1449 | "data_coding_scheme, ud_hdr_ind, " |
| 1450 | "user_data, text, " |
| 1451 | "dest_addr, dest_ton, dest_npi, " |
| 1452 | "src_addr, src_ton, src_npi) VALUES " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1453 | "(datetime('now'), %u, " |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1454 | "%u, %u, %u, " |
| 1455 | "%u, %u, " |
| 1456 | "%s, %s, " |
| 1457 | "%s, %u, %u, " |
| 1458 | "%s, %u, %u)", |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1459 | validity_timestamp, |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1460 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | ccf68fd | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1461 | sms->data_coding_scheme, sms->ud_hdr_ind, |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1462 | q_udata, q_text, |
| 1463 | q_daddr, sms->dst.ton, sms->dst.npi, |
| 1464 | q_saddr, sms->src.ton, sms->src.npi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1465 | free(q_text); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1466 | free(q_udata); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1467 | free(q_daddr); |
| 1468 | free(q_saddr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1469 | |
| 1470 | if (!result) |
| 1471 | return -EIO; |
| 1472 | |
| 1473 | dbi_result_free(result); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1477 | static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1478 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1479 | struct gsm_sms *sms = sms_alloc(); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1480 | const char *text, *daddr, *saddr; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1481 | const unsigned char *user_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1482 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1483 | if (!sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1484 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1485 | |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1486 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1487 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1488 | /* FIXME: validity */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1489 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
Holger Hans Peter Freyther | 77c9f13 | 2014-07-03 14:00:30 +0200 | [diff] [blame] | 1490 | sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req"); |
| 1491 | sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req"); |
| 1492 | sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind"); |
| 1493 | sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id"); |
| 1494 | sms->data_coding_scheme = dbi_result_get_ulonglong(result, |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1495 | "data_coding_scheme"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1496 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1497 | |
Holger Hans Peter Freyther | 77c9f13 | 2014-07-03 14:00:30 +0200 | [diff] [blame] | 1498 | sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi"); |
| 1499 | sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton"); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1500 | daddr = dbi_result_get_string(result, "dest_addr"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1501 | if (daddr) |
| 1502 | osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 1503 | sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1504 | |
Holger Hans Peter Freyther | 77c9f13 | 2014-07-03 14:00:30 +0200 | [diff] [blame] | 1505 | sms->src.npi = dbi_result_get_ulonglong(result, "src_npi"); |
| 1506 | sms->src.ton = dbi_result_get_ulonglong(result, "src_ton"); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1507 | saddr = dbi_result_get_string(result, "src_addr"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1508 | if (saddr) |
| 1509 | osmo_strlcpy(sms->src.addr, saddr, sizeof(sms->src.addr)); |
Holger Hans Peter Freyther | d262daa | 2013-10-08 03:17:30 +0200 | [diff] [blame] | 1510 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1511 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 1512 | user_data = dbi_result_get_binary(result, "user_data"); |
| 1513 | if (sms->user_data_len > sizeof(sms->user_data)) |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1514 | sms->user_data_len = (uint8_t) sizeof(sms->user_data); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1515 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | 4531c6b | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1516 | |
| 1517 | text = dbi_result_get_string(result, "text"); |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1518 | if (text) |
| 1519 | osmo_strlcpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1520 | return sms; |
| 1521 | } |
| 1522 | |
Holger Hans Peter Freyther | d0095e4 | 2010-12-24 23:18:31 +0100 | [diff] [blame] | 1523 | struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id) |
| 1524 | { |
| 1525 | dbi_result result; |
| 1526 | struct gsm_sms *sms; |
| 1527 | |
| 1528 | result = dbi_conn_queryf(conn, |
| 1529 | "SELECT * FROM SMS WHERE SMS.id = %llu", id); |
| 1530 | if (!result) |
| 1531 | return NULL; |
| 1532 | |
| 1533 | if (!dbi_result_next_row(result)) { |
| 1534 | dbi_result_free(result); |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | |
| 1538 | sms = sms_from_result(net, result); |
| 1539 | |
| 1540 | dbi_result_free(result); |
| 1541 | |
| 1542 | return sms; |
| 1543 | } |
| 1544 | |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1545 | /* retrieve the next unsent SMS with ID >= min_id */ |
Holger Hans Peter Freyther | 082ec46 | 2010-03-25 09:59:30 +0100 | [diff] [blame] | 1546 | struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id) |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1547 | { |
| 1548 | dbi_result result; |
| 1549 | struct gsm_sms *sms; |
| 1550 | |
| 1551 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1552 | "SELECT SMS.* " |
| 1553 | "FROM SMS JOIN Subscriber ON " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1554 | "SMS.dest_addr = Subscriber.extension " |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1555 | "WHERE SMS.id >= %llu AND SMS.sent IS NULL " |
| 1556 | "AND Subscriber.lac > 0 " |
| 1557 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1558 | min_id); |
| 1559 | if (!result) |
| 1560 | return NULL; |
| 1561 | |
| 1562 | if (!dbi_result_next_row(result)) { |
| 1563 | dbi_result_free(result); |
| 1564 | return NULL; |
| 1565 | } |
| 1566 | |
| 1567 | sms = sms_from_result(net, result); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1568 | |
| 1569 | dbi_result_free(result); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1570 | |
| 1571 | return sms; |
| 1572 | } |
| 1573 | |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1574 | struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, |
| 1575 | unsigned long long min_subscr_id, |
| 1576 | unsigned int failed) |
Sylvain Munaut | c2e0a44 | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1577 | { |
| 1578 | dbi_result result; |
| 1579 | struct gsm_sms *sms; |
| 1580 | |
| 1581 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1582 | "SELECT SMS.* " |
| 1583 | "FROM SMS JOIN Subscriber ON " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1584 | "SMS.dest_addr = Subscriber.extension " |
| 1585 | "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL " |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1586 | "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1587 | "ORDER BY Subscriber.id, SMS.id LIMIT 1", |
Holger Hans Peter Freyther | ae9c9d9 | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1588 | min_subscr_id, failed); |
Sylvain Munaut | c2e0a44 | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1589 | if (!result) |
| 1590 | return NULL; |
| 1591 | |
| 1592 | if (!dbi_result_next_row(result)) { |
| 1593 | dbi_result_free(result); |
| 1594 | return NULL; |
| 1595 | } |
| 1596 | |
| 1597 | sms = sms_from_result(net, result); |
| 1598 | |
| 1599 | dbi_result_free(result); |
| 1600 | |
| 1601 | return sms; |
| 1602 | } |
| 1603 | |
Sylvain Munaut | 889b01c | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 1604 | /* retrieve the next unsent SMS for a given subscriber */ |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1605 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 1606 | { |
| 1607 | dbi_result result; |
| 1608 | struct gsm_sms *sms; |
| 1609 | |
| 1610 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1611 | "SELECT SMS.* " |
| 1612 | "FROM SMS JOIN Subscriber ON " |
Alexander Chemeris | c5104f2 | 2013-10-08 03:17:32 +0200 | [diff] [blame] | 1613 | "SMS.dest_addr = Subscriber.extension " |
| 1614 | "WHERE Subscriber.id = %llu AND SMS.sent IS NULL " |
Sylvain Munaut | 8de47dc | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1615 | "AND Subscriber.lac > 0 " |
| 1616 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1617 | subscr->id); |
| 1618 | if (!result) |
| 1619 | return NULL; |
| 1620 | |
| 1621 | if (!dbi_result_next_row(result)) { |
| 1622 | dbi_result_free(result); |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 1626 | sms = sms_from_result(subscr->group->net, result); |
Harald Welte | 43762f7 | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1627 | |
| 1628 | dbi_result_free(result); |
| 1629 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1630 | return sms; |
| 1631 | } |
| 1632 | |
Alexander Chemeris | 5c3e05f | 2014-03-08 21:27:37 +0100 | [diff] [blame] | 1633 | /* mark a given SMS as delivered */ |
| 1634 | int db_sms_mark_delivered(struct gsm_sms *sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1635 | { |
| 1636 | dbi_result result; |
| 1637 | |
| 1638 | result = dbi_conn_queryf(conn, |
| 1639 | "UPDATE SMS " |
| 1640 | "SET sent = datetime('now') " |
| 1641 | "WHERE id = %llu", sms->id); |
| 1642 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1643 | LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1644 | return 1; |
| 1645 | } |
| 1646 | |
| 1647 | dbi_result_free(result); |
| 1648 | return 0; |
| 1649 | } |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* increase the number of attempted deliveries */ |
| 1652 | int db_sms_inc_deliver_attempts(struct gsm_sms *sms) |
| 1653 | { |
| 1654 | dbi_result result; |
| 1655 | |
| 1656 | result = dbi_conn_queryf(conn, |
| 1657 | "UPDATE SMS " |
| 1658 | "SET deliver_attempts = deliver_attempts + 1 " |
| 1659 | "WHERE id = %llu", sms->id); |
| 1660 | if (!result) { |
Harald Welte | 273fdfd | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1661 | LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for " |
| 1662 | "SMS %llu.\n", sms->id); |
Harald Welte (local) | 7e3c85b | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1663 | return 1; |
| 1664 | } |
| 1665 | |
| 1666 | dbi_result_free(result); |
| 1667 | return 0; |
| 1668 | } |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1669 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 1670 | int db_apdu_blob_store(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1671 | uint8_t apdu_id_flags, uint8_t len, |
| 1672 | uint8_t *apdu) |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1673 | { |
| 1674 | dbi_result result; |
Holger Hans Peter Freyther | eb12526 | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 1675 | unsigned char *q_apdu; |
Harald Welte (local) | bd72aa2 | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1676 | |
| 1677 | dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu); |
| 1678 | |
| 1679 | result = dbi_conn_queryf(conn, |
| 1680 | "INSERT INTO ApduBlobs " |
| 1681 | "(created,subscriber_id,apdu_id_flags,apdu) VALUES " |
| 1682 | "(datetime('now'),%llu,%u,%s)", |
| 1683 | subscr->id, apdu_id_flags, q_apdu); |
| 1684 | |
| 1685 | free(q_apdu); |
| 1686 | |
| 1687 | if (!result) |
| 1688 | return -EIO; |
| 1689 | |
| 1690 | dbi_result_free(result); |
| 1691 | return 0; |
| 1692 | } |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1693 | |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 1694 | int db_store_counter(struct osmo_counter *ctr) |
Harald Welte | bdbb744 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1695 | { |
| 1696 | dbi_result result; |
| 1697 | char *q_name; |
| 1698 | |
| 1699 | dbi_conn_quote_string_copy(conn, ctr->name, &q_name); |
| 1700 | |
| 1701 | result = dbi_conn_queryf(conn, |
| 1702 | "INSERT INTO Counters " |
| 1703 | "(timestamp,name,value) VALUES " |
| 1704 | "(datetime('now'),%s,%lu)", q_name, ctr->value); |
| 1705 | |
| 1706 | free(q_name); |
| 1707 | |
| 1708 | if (!result) |
| 1709 | return -EIO; |
| 1710 | |
| 1711 | dbi_result_free(result); |
| 1712 | return 0; |
| 1713 | } |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1714 | |
| 1715 | static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num, |
| 1716 | char *q_prefix) |
| 1717 | { |
| 1718 | dbi_result result; |
| 1719 | char *q_name; |
| 1720 | |
| 1721 | dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name, |
| 1722 | &q_name); |
| 1723 | |
| 1724 | result = dbi_conn_queryf(conn, |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1725 | "Insert INTO RateCounters " |
Harald Welte | 805eb48 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 1726 | "(timestamp,name,idx,value) VALUES " |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1727 | "(datetime('now'),%s.%s,%u,%"PRIu64")", |
| 1728 | q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current); |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1729 | |
| 1730 | free(q_name); |
| 1731 | |
| 1732 | if (!result) |
| 1733 | return -EIO; |
| 1734 | |
| 1735 | dbi_result_free(result); |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | int db_store_rate_ctr_group(struct rate_ctr_group *ctrg) |
| 1740 | { |
| 1741 | unsigned int i; |
| 1742 | char *q_prefix; |
| 1743 | |
Harald Welte | 7e85eaf | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1744 | dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix); |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1745 | |
| 1746 | for (i = 0; i < ctrg->desc->num_ctr; i++) |
| 1747 | db_store_rate_ctr(ctrg, i, q_prefix); |
| 1748 | |
| 1749 | free(q_prefix); |
| 1750 | |
| 1751 | return 0; |
| 1752 | } |