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