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