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