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