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