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 Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 41 | static char *db_basename = NULL; |
| 42 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 43 | static dbi_conn conn; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 44 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 45 | #define SCHEMA_REVISION "3" |
| 46 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 47 | static char *create_stmts[] = { |
| 48 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 49 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 50 | "key TEXT UNIQUE NOT NULL, " |
| 51 | "value TEXT NOT NULL" |
| 52 | ")", |
| 53 | "INSERT OR IGNORE INTO Meta " |
| 54 | "(key, value) " |
| 55 | "VALUES " |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 56 | "('revision', " SCHEMA_REVISION ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 57 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 58 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 59 | "created TIMESTAMP NOT NULL, " |
| 60 | "updated TIMESTAMP NOT NULL, " |
| 61 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 62 | "name TEXT, " |
| 63 | "extension TEXT UNIQUE, " |
| 64 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 65 | "tmsi TEXT UNIQUE, " |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 66 | "lac INTEGER NOT NULL DEFAULT 0, " |
| 67 | "expire_lu TIMESTAMP DEFAULT NULL" |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 68 | ")", |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 69 | "CREATE TABLE IF NOT EXISTS AuthToken (" |
| 70 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 71 | "subscriber_id INTEGER UNIQUE NOT NULL, " |
| 72 | "created TIMESTAMP NOT NULL, " |
| 73 | "token TEXT UNIQUE NOT NULL" |
| 74 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 75 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 76 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 77 | "created TIMESTAMP NOT NULL, " |
| 78 | "updated TIMESTAMP NOT NULL, " |
| 79 | "name TEXT, " |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 80 | "classmark1 NUMERIC, " |
| 81 | "classmark2 BLOB, " |
| 82 | "classmark3 BLOB, " |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 83 | "imei NUMERIC UNIQUE NOT NULL" |
| 84 | ")", |
| 85 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 86 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 87 | "created TIMESTAMP NOT NULL, " |
| 88 | "updated TIMESTAMP NOT NULL, " |
| 89 | "subscriber_id NUMERIC NOT NULL, " |
| 90 | "equipment_id NUMERIC NOT NULL, " |
| 91 | "UNIQUE (subscriber_id, equipment_id) " |
| 92 | ")", |
| 93 | "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 94 | /* metadata, not part of sms */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 95 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 96 | "created TIMESTAMP NOT NULL, " |
| 97 | "sent TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 98 | "sender_id INTEGER NOT NULL, " |
| 99 | "receiver_id INTEGER NOT NULL, " |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 100 | "deliver_attempts INTEGER NOT NULL DEFAULT 0, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 101 | /* data directly copied/derived from SMS */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 102 | "valid_until TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 103 | "reply_path_req INTEGER NOT NULL, " |
| 104 | "status_rep_req INTEGER NOT NULL, " |
| 105 | "protocol_id INTEGER NOT NULL, " |
| 106 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 107 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 108 | "dest_addr TEXT, " |
| 109 | "user_data BLOB, " /* TP-UD */ |
| 110 | /* additional data, interpreted from SMS */ |
| 111 | "header BLOB, " /* UD Header */ |
| 112 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 113 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 114 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 115 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 116 | "created TIMESTAMP NOT NULL, " |
| 117 | "updated TIMESTAMP NOT NULL, " |
| 118 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 119 | "last_bts NUMERIC NOT NULL " |
| 120 | ")", |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 121 | "CREATE TABLE IF NOT EXISTS ApduBlobs (" |
| 122 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 123 | "created TIMESTAMP NOT NULL, " |
| 124 | "apdu_id_flags INTEGER NOT NULL, " |
| 125 | "subscriber_id INTEGER NOT NULL, " |
| 126 | "apdu BLOB " |
| 127 | ")", |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 128 | "CREATE TABLE IF NOT EXISTS Counters (" |
| 129 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 130 | "timestamp TIMESTAMP NOT NULL, " |
Harald Welte | f9a43c4 | 2009-12-22 21:40:42 +0100 | [diff] [blame] | 131 | "value INTEGER NOT NULL, " |
| 132 | "name TEXT NOT NULL " |
Harald Welte | 09f7ad0 | 2009-12-24 09:42:07 +0100 | [diff] [blame] | 133 | ")", |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 134 | "CREATE TABLE IF NOT EXISTS RateCounters (" |
| 135 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 136 | "timestamp TIMESTAMP NOT NULL, " |
| 137 | "value INTEGER NOT NULL, " |
| 138 | "name TEXT NOT NULL, " |
Harald Welte | d94d6a0 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 139 | "idx INTEGER NOT NULL " |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 140 | ")", |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 141 | "CREATE TABLE IF NOT EXISTS AuthKeys (" |
Sylvain Munaut | 10bf812 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 142 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | 77d334a | 2009-12-27 19:26:12 +0100 | [diff] [blame] | 143 | "algorithm_id INTEGER NOT NULL, " |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 144 | "a3a8_ki BLOB " |
| 145 | ")", |
Sylvain Munaut | c614a6a | 2010-06-09 13:03:39 +0200 | [diff] [blame] | 146 | "CREATE TABLE IF NOT EXISTS AuthLastTuples (" |
Sylvain Munaut | 10bf812 | 2010-06-09 11:31:32 +0200 | [diff] [blame] | 147 | "subscriber_id INTEGER PRIMARY KEY, " |
Sylvain Munaut | 70881b7 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 148 | "issued TIMESTAMP NOT NULL, " |
| 149 | "use_count INTEGER NOT NULL DEFAULT 0, " |
| 150 | "key_seq INTEGER NOT NULL, " |
| 151 | "rand BLOB NOT NULL, " |
| 152 | "sres BLOB NOT NULL, " |
| 153 | "kc BLOB NOT NULL " |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 154 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 157 | void db_error_func(dbi_conn conn, void *data) |
| 158 | { |
| 159 | const char *msg; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 160 | dbi_conn_error(conn, &msg); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 161 | LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 164 | static int update_db_revision_2(void) |
| 165 | { |
| 166 | dbi_result result; |
| 167 | |
| 168 | result = dbi_conn_query(conn, |
| 169 | "ALTER TABLE Subscriber " |
| 170 | "ADD COLUMN expire_lu " |
| 171 | "TIMESTAMP DEFAULT NULL"); |
| 172 | if (!result) { |
| 173 | LOGP(DDB, LOGL_ERROR, |
| 174 | "Failed to alter table Subscriber (upgrade vom rev 2).\n"); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | dbi_result_free(result); |
| 178 | |
| 179 | result = dbi_conn_query(conn, |
| 180 | "UPDATE Meta " |
| 181 | "SET value = '3' " |
| 182 | "WHERE key = 'revision'"); |
| 183 | if (!result) { |
| 184 | LOGP(DDB, LOGL_ERROR, |
| 185 | "Failed set new revision (upgrade vom rev 2).\n"); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | dbi_result_free(result); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 193 | static int check_db_revision(void) |
| 194 | { |
| 195 | dbi_result result; |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 196 | const char *rev_s; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 197 | |
| 198 | result = dbi_conn_query(conn, |
| 199 | "SELECT value FROM Meta WHERE key='revision'"); |
| 200 | if (!result) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | if (!dbi_result_next_row(result)) { |
| 204 | dbi_result_free(result); |
| 205 | return -EINVAL; |
| 206 | } |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 207 | rev_s = dbi_result_get_string(result, "value"); |
| 208 | if (!rev_s) { |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 209 | dbi_result_free(result); |
| 210 | return -EINVAL; |
| 211 | } |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 212 | if (!strcmp(rev_s, "2")) { |
| 213 | if (update_db_revision_2()) { |
| 214 | LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s); |
| 215 | dbi_result_free(result); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | } else if (!strcmp(rev_s, SCHEMA_REVISION)) { |
| 219 | /* everything is fine */ |
| 220 | } else { |
| 221 | LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s); |
| 222 | dbi_result_free(result); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | dbi_result_free(result); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int db_configure(void) |
| 231 | { |
| 232 | dbi_result result; |
| 233 | |
| 234 | result = dbi_conn_query(conn, |
| 235 | "PRAGMA synchronous = FULL"); |
| 236 | if (!result) |
| 237 | return -EINVAL; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 238 | |
| 239 | dbi_result_free(result); |
| 240 | return 0; |
| 241 | } |
| 242 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 243 | int db_init(const char *name) |
| 244 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 245 | dbi_initialize(NULL); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 246 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 247 | conn = dbi_conn_new("sqlite3"); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 248 | if (conn == NULL) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 249 | LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 250 | return 1; |
| 251 | } |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 252 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 253 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 254 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 255 | /* MySQL |
| 256 | dbi_conn_set_option(conn, "host", "localhost"); |
| 257 | dbi_conn_set_option(conn, "username", "your_name"); |
| 258 | dbi_conn_set_option(conn, "password", "your_password"); |
| 259 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 260 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 261 | */ |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 262 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 263 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 264 | db_basename = strdup(name); |
| 265 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 266 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 267 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 268 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 269 | if (dbi_conn_connect(conn) < 0) |
| 270 | goto out_err; |
| 271 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 272 | return 0; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 273 | |
| 274 | out_err: |
| 275 | free(db_dirname); |
| 276 | free(db_basename); |
| 277 | db_dirname = db_basename = NULL; |
| 278 | return -1; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 281 | |
Harald Welte | d1476bc | 2011-07-16 13:24:09 +0200 | [diff] [blame] | 282 | int db_prepare(void) |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 283 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 284 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 285 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 286 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 287 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 288 | result = dbi_conn_query(conn, create_stmts[i]); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 289 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 290 | LOGP(DDB, LOGL_ERROR, |
| 291 | "Failed to create some table.\n"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | dbi_result_free(result); |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 295 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 296 | |
Holger Hans Peter Freyther | 850326e | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 297 | if (check_db_revision() < 0) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 298 | LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, " |
Holger Hans Peter Freyther | 850326e | 2009-08-10 08:36:04 +0200 | [diff] [blame] | 299 | "please update your database schema\n"); |
| 300 | return -1; |
| 301 | } |
| 302 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 303 | db_configure(); |
| 304 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 305 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Harald Welte | d1476bc | 2011-07-16 13:24:09 +0200 | [diff] [blame] | 308 | int db_fini(void) |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 309 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 310 | dbi_conn_close(conn); |
| 311 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 312 | |
Harald Welte | 2c5f4c6 | 2011-07-16 13:22:57 +0200 | [diff] [blame] | 313 | free(db_dirname); |
| 314 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 315 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 318 | struct gsm_subscriber *db_create_subscriber(struct gsm_network *net, char *imsi) |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 319 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 320 | dbi_result result; |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 321 | struct gsm_subscriber *subscr; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 322 | |
| 323 | /* Is this subscriber known in the db? */ |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 324 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 325 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 326 | result = dbi_conn_queryf(conn, |
| 327 | "UPDATE Subscriber set updated = datetime('now') " |
| 328 | "WHERE imsi = %s " , imsi); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 329 | if (!result) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 330 | LOGP(DDB, LOGL_ERROR, "failed to update timestamp\n"); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 331 | else |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 332 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 333 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 334 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 335 | |
| 336 | subscr = subscr_alloc(); |
| 337 | if (!subscr) |
| 338 | return NULL; |
Harald Welte | 2c5f4c6 | 2011-07-16 13:22:57 +0200 | [diff] [blame] | 339 | subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 340 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 341 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 342 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 343 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 344 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 345 | imsi |
| 346 | ); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 347 | if (!result) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 348 | LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n"); |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 349 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 350 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 351 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 352 | dbi_result_free(result); |
Harald Welte (local) | 441e483 | 2009-12-26 18:57:32 +0100 | [diff] [blame] | 353 | 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] | 354 | db_subscriber_alloc_exten(subscr); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 355 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 358 | 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] | 359 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 360 | static int get_equipment_by_subscr(struct gsm_subscriber *subscr) |
| 361 | { |
| 362 | dbi_result result; |
Harald Welte | 55726d7 | 2009-09-26 18:54:59 +0200 | [diff] [blame] | 363 | const char *string; |
Harald Welte | 4669f3d | 2009-12-09 19:19:45 +0100 | [diff] [blame] | 364 | unsigned char cm1; |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 365 | const unsigned char *cm2, *cm3; |
| 366 | struct gsm_equipment *equip = &subscr->equipment; |
| 367 | |
| 368 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 7a7d364 | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 369 | "SELECT Equipment.* " |
| 370 | "FROM Equipment JOIN EquipmentWatch ON " |
| 371 | "EquipmentWatch.equipment_id=Equipment.id " |
| 372 | "WHERE EquipmentWatch.subscriber_id = %llu " |
| 373 | "ORDER BY EquipmentWatch.updated DESC", subscr->id); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 374 | if (!result) |
| 375 | return -EIO; |
| 376 | |
| 377 | if (!dbi_result_next_row(result)) { |
| 378 | dbi_result_free(result); |
| 379 | return -ENOENT; |
| 380 | } |
| 381 | |
| 382 | equip->id = dbi_result_get_ulonglong(result, "id"); |
| 383 | |
| 384 | string = dbi_result_get_string(result, "imei"); |
| 385 | if (string) |
| 386 | strncpy(equip->imei, string, sizeof(equip->imei)); |
| 387 | |
Harald Welte (local) | 1f3ecd4 | 2009-12-26 18:56:00 +0100 | [diff] [blame] | 388 | string = dbi_result_get_string(result, "classmark1"); |
Holger Hans Peter Freyther | ceb072d | 2010-03-30 15:28:36 +0200 | [diff] [blame] | 389 | if (string) { |
| 390 | cm1 = atoi(string) & 0xff; |
| 391 | memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1)); |
| 392 | } |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 393 | |
| 394 | equip->classmark2_len = dbi_result_get_field_length(result, "classmark2"); |
| 395 | cm2 = dbi_result_get_binary(result, "classmark2"); |
| 396 | if (equip->classmark2_len > sizeof(equip->classmark2)) |
| 397 | equip->classmark2_len = sizeof(equip->classmark2); |
| 398 | memcpy(equip->classmark2, cm2, equip->classmark2_len); |
| 399 | |
| 400 | equip->classmark3_len = dbi_result_get_field_length(result, "classmark3"); |
| 401 | cm3 = dbi_result_get_binary(result, "classmark3"); |
| 402 | if (equip->classmark3_len > sizeof(equip->classmark3)) |
| 403 | equip->classmark3_len = sizeof(equip->classmark3); |
| 404 | memcpy(equip->classmark3, cm3, equip->classmark3_len); |
| 405 | |
| 406 | dbi_result_free(result); |
| 407 | |
| 408 | return 0; |
| 409 | } |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 410 | |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 411 | int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 412 | struct gsm_subscriber *subscr) |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 413 | { |
| 414 | dbi_result result; |
| 415 | const unsigned char *a3a8_ki; |
| 416 | |
| 417 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 418 | "SELECT * FROM AuthKeys WHERE subscriber_id=%llu", |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 419 | subscr->id); |
| 420 | if (!result) |
| 421 | return -EIO; |
| 422 | |
| 423 | if (!dbi_result_next_row(result)) { |
| 424 | dbi_result_free(result); |
| 425 | return -ENOENT; |
| 426 | } |
| 427 | |
| 428 | ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id"); |
| 429 | ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki"); |
| 430 | a3a8_ki = dbi_result_get_binary(result, "a3a8_ki"); |
Sylvain Munaut | e1cb4de | 2009-12-27 19:24:05 +0100 | [diff] [blame] | 431 | if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki)) |
Sylvain Munaut | 5e80cc4 | 2012-05-07 22:09:15 +0200 | [diff] [blame] | 432 | ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki); |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 433 | memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len); |
| 434 | |
| 435 | dbi_result_free(result); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 440 | int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 441 | struct gsm_subscriber *subscr) |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 442 | { |
| 443 | dbi_result result; |
| 444 | struct gsm_auth_info ainfo_old; |
| 445 | int rc, upd; |
| 446 | unsigned char *ki_str; |
| 447 | |
| 448 | /* Deletion ? */ |
| 449 | if (ainfo == NULL) { |
| 450 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 451 | "DELETE FROM AuthKeys WHERE subscriber_id=%llu", |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 452 | subscr->id); |
| 453 | |
| 454 | if (!result) |
| 455 | return -EIO; |
| 456 | |
| 457 | dbi_result_free(result); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* Check if already existing */ |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 463 | rc = db_get_authinfo_for_subscr(&ainfo_old, subscr); |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 464 | if (rc && rc != -ENOENT) |
| 465 | return rc; |
| 466 | upd = rc ? 0 : 1; |
| 467 | |
| 468 | /* Update / Insert */ |
| 469 | dbi_conn_quote_binary_copy(conn, |
| 470 | ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str); |
| 471 | |
| 472 | if (!upd) { |
| 473 | result = dbi_conn_queryf(conn, |
| 474 | "INSERT INTO AuthKeys " |
| 475 | "(subscriber_id, algorithm_id, a3a8_ki) " |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 476 | "VALUES (%llu, %u, %s)", |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 477 | subscr->id, ainfo->auth_algo, ki_str); |
| 478 | } else { |
| 479 | result = dbi_conn_queryf(conn, |
| 480 | "UPDATE AuthKeys " |
| 481 | "SET algorithm_id=%u, a3a8_ki=%s " |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 482 | "WHERE subscriber_id=%llu", |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 483 | ainfo->auth_algo, ki_str, subscr->id); |
| 484 | } |
| 485 | |
| 486 | free(ki_str); |
| 487 | |
| 488 | if (!result) |
| 489 | return -EIO; |
| 490 | |
| 491 | dbi_result_free(result); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 496 | int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 497 | struct gsm_subscriber *subscr) |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 498 | { |
| 499 | dbi_result result; |
| 500 | int len; |
| 501 | const unsigned char *blob; |
| 502 | |
| 503 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 504 | "SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu", |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 505 | subscr->id); |
| 506 | if (!result) |
| 507 | return -EIO; |
| 508 | |
| 509 | if (!dbi_result_next_row(result)) { |
| 510 | dbi_result_free(result); |
| 511 | return -ENOENT; |
| 512 | } |
| 513 | |
Holger Hans Peter Freyther | e885951 | 2013-07-04 20:24:02 +0200 | [diff] [blame] | 514 | memset(atuple, 0, sizeof(*atuple)); |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 515 | |
Sylvain Munaut | 70881b7 | 2009-12-27 15:41:59 +0100 | [diff] [blame] | 516 | atuple->use_count = dbi_result_get_ulonglong(result, "use_count"); |
| 517 | atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq"); |
| 518 | |
Harald Welte | 3606cc5 | 2009-12-05 15:13:22 +0530 | [diff] [blame] | 519 | len = dbi_result_get_field_length(result, "rand"); |
| 520 | if (len != sizeof(atuple->rand)) |
| 521 | goto err_size; |
| 522 | |
| 523 | blob = dbi_result_get_binary(result, "rand"); |
| 524 | memcpy(atuple->rand, blob, len); |
| 525 | |
| 526 | len = dbi_result_get_field_length(result, "sres"); |
| 527 | if (len != sizeof(atuple->sres)) |
| 528 | goto err_size; |
| 529 | |
| 530 | blob = dbi_result_get_binary(result, "sres"); |
| 531 | memcpy(atuple->sres, blob, len); |
| 532 | |
| 533 | len = dbi_result_get_field_length(result, "kc"); |
| 534 | if (len != sizeof(atuple->kc)) |
| 535 | goto err_size; |
| 536 | |
| 537 | blob = dbi_result_get_binary(result, "kc"); |
| 538 | memcpy(atuple->kc, blob, len); |
| 539 | |
| 540 | dbi_result_free(result); |
| 541 | |
| 542 | return 0; |
| 543 | |
| 544 | err_size: |
| 545 | dbi_result_free(result); |
| 546 | return -EIO; |
| 547 | } |
| 548 | |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 549 | int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 550 | struct gsm_subscriber *subscr) |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 551 | { |
| 552 | dbi_result result; |
| 553 | int rc, upd; |
| 554 | struct gsm_auth_tuple atuple_old; |
| 555 | unsigned char *rand_str, *sres_str, *kc_str; |
| 556 | |
| 557 | /* Deletion ? */ |
| 558 | if (atuple == NULL) { |
| 559 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 560 | "DELETE FROM AuthLastTuples WHERE subscriber_id=%llu", |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 561 | subscr->id); |
| 562 | |
| 563 | if (!result) |
| 564 | return -EIO; |
| 565 | |
| 566 | dbi_result_free(result); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* Check if already existing */ |
Sylvain Munaut | 92b2ff5 | 2010-06-09 11:32:51 +0200 | [diff] [blame] | 572 | rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr); |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 573 | if (rc && rc != -ENOENT) |
| 574 | return rc; |
| 575 | upd = rc ? 0 : 1; |
| 576 | |
| 577 | /* Update / Insert */ |
| 578 | dbi_conn_quote_binary_copy(conn, |
| 579 | atuple->rand, sizeof(atuple->rand), &rand_str); |
| 580 | dbi_conn_quote_binary_copy(conn, |
| 581 | atuple->sres, sizeof(atuple->sres), &sres_str); |
| 582 | dbi_conn_quote_binary_copy(conn, |
| 583 | atuple->kc, sizeof(atuple->kc), &kc_str); |
| 584 | |
| 585 | if (!upd) { |
| 586 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | c614a6a | 2010-06-09 13:03:39 +0200 | [diff] [blame] | 587 | "INSERT INTO AuthLastTuples " |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 588 | "(subscriber_id, issued, use_count, " |
| 589 | "key_seq, rand, sres, kc) " |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 590 | "VALUES (%llu, datetime('now'), %u, " |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 591 | "%u, %s, %s, %s ) ", |
| 592 | subscr->id, atuple->use_count, atuple->key_seq, |
| 593 | rand_str, sres_str, kc_str); |
| 594 | } else { |
| 595 | char *issued = atuple->key_seq == atuple_old.key_seq ? |
| 596 | "issued" : "datetime('now')"; |
| 597 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 31ac307 | 2010-06-10 22:26:21 +0200 | [diff] [blame] | 598 | "UPDATE AuthLastTuples " |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 599 | "SET issued=%s, use_count=%u, " |
| 600 | "key_seq=%u, rand=%s, sres=%s, kc=%s " |
Sylvain Munaut | adea4f1 | 2010-07-03 15:38:35 +0200 | [diff] [blame] | 601 | "WHERE subscriber_id = %llu", |
Sylvain Munaut | 062d5ef | 2009-12-27 19:27:53 +0100 | [diff] [blame] | 602 | issued, atuple->use_count, atuple->key_seq, |
| 603 | rand_str, sres_str, kc_str, subscr->id); |
| 604 | } |
| 605 | |
| 606 | free(rand_str); |
| 607 | free(sres_str); |
| 608 | free(kc_str); |
| 609 | |
| 610 | if (!result) |
| 611 | return -EIO; |
| 612 | |
| 613 | dbi_result_free(result); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 618 | static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result) |
| 619 | { |
| 620 | const char *string; |
| 621 | string = dbi_result_get_string(result, "imsi"); |
| 622 | if (string) |
| 623 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
| 624 | |
| 625 | string = dbi_result_get_string(result, "tmsi"); |
| 626 | if (string) |
| 627 | subscr->tmsi = tmsi_from_string(string); |
| 628 | |
| 629 | string = dbi_result_get_string(result, "name"); |
| 630 | if (string) |
| 631 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
| 632 | |
| 633 | string = dbi_result_get_string(result, "extension"); |
| 634 | if (string) |
| 635 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
| 636 | |
| 637 | subscr->lac = dbi_result_get_uint(result, "lac"); |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 638 | |
| 639 | if (!dbi_result_field_is_null(result, "expire_lu")) |
| 640 | subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu"); |
| 641 | else |
| 642 | subscr->expire_lu = 0; |
| 643 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 644 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
| 645 | } |
| 646 | |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 647 | #define BASE_QUERY "SELECT * FROM Subscriber " |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 648 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 649 | enum gsm_subscriber_field field, |
| 650 | const char *id) |
| 651 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 652 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 653 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 654 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 655 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 656 | switch (field) { |
| 657 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 658 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 659 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 660 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 661 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 662 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 663 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 664 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 665 | break; |
| 666 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 667 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 668 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 669 | BASE_QUERY |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 670 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 671 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 672 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 673 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 674 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 675 | case GSM_SUBSCRIBER_EXTENSION: |
| 676 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 677 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 678 | BASE_QUERY |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 679 | "WHERE extension = %s ", |
| 680 | quoted |
| 681 | ); |
| 682 | free(quoted); |
| 683 | break; |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 684 | case GSM_SUBSCRIBER_ID: |
| 685 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 686 | result = dbi_conn_queryf(conn, |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 687 | BASE_QUERY |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 688 | "WHERE id = %s ", quoted); |
| 689 | free(quoted); |
| 690 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 691 | default: |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 692 | LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 693 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 694 | } |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 695 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 696 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 697 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 698 | } |
| 699 | if (!dbi_result_next_row(result)) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 700 | DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n", |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 701 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 702 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 703 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 704 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 705 | |
| 706 | subscr = subscr_alloc(); |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 707 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 708 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 709 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 710 | db_set_from_query(subscr, result); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 711 | 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] | 712 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 713 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 714 | dbi_result_free(result); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 715 | |
| 716 | get_equipment_by_subscr(subscr); |
| 717 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 718 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 721 | int db_subscriber_update(struct gsm_subscriber *subscr) |
| 722 | { |
| 723 | char buf[32]; |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 724 | dbi_result result; |
| 725 | |
| 726 | /* Copy the id to a string as queryf with %llu is failing */ |
| 727 | sprintf(buf, "%llu", subscr->id); |
| 728 | result = dbi_conn_queryf(conn, |
| 729 | BASE_QUERY |
| 730 | "WHERE id = %s", buf); |
| 731 | |
| 732 | if (!result) { |
| 733 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id); |
| 734 | return -EIO; |
| 735 | } |
| 736 | if (!dbi_result_next_row(result)) { |
| 737 | DEBUGP(DDB, "Failed to find the Subscriber. %llu\n", |
| 738 | subscr->id); |
| 739 | dbi_result_free(result); |
| 740 | return -EIO; |
| 741 | } |
| 742 | |
| 743 | db_set_from_query(subscr, result); |
| 744 | dbi_result_free(result); |
| 745 | get_equipment_by_subscr(subscr); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 750 | int db_sync_subscriber(struct gsm_subscriber *subscriber) |
| 751 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 752 | dbi_result result; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 753 | char tmsi[14]; |
Harald Welte | 019d016 | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 754 | char *q_tmsi, *q_name, *q_extension; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 755 | |
Harald Welte | 019d016 | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 756 | dbi_conn_quote_string_copy(conn, |
| 757 | subscriber->name, &q_name); |
| 758 | dbi_conn_quote_string_copy(conn, |
| 759 | subscriber->extension, &q_extension); |
| 760 | |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 761 | if (subscriber->tmsi != GSM_RESERVED_TMSI) { |
| 762 | sprintf(tmsi, "%u", subscriber->tmsi); |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 763 | dbi_conn_quote_string_copy(conn, |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 764 | tmsi, |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 765 | &q_tmsi); |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 766 | } else |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 767 | q_tmsi = strdup("NULL"); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 768 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 769 | result = dbi_conn_queryf(conn, |
| 770 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 771 | "SET updated = datetime('now'), " |
Harald Welte | 624e89d | 2010-12-26 21:23:51 +0100 | [diff] [blame] | 772 | "name = %s, " |
| 773 | "extension = %s, " |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 774 | "authorized = %i, " |
| 775 | "tmsi = %s, " |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 776 | "lac = %i, " |
| 777 | "expire_lu = datetime(%i, 'unixepoch') " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 778 | "WHERE imsi = %s ", |
Harald Welte | 019d016 | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 779 | q_name, |
| 780 | q_extension, |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 781 | subscriber->authorized, |
| 782 | q_tmsi, |
| 783 | subscriber->lac, |
Holger Hans Peter Freyther | 5ecbc93 | 2013-07-27 18:39:30 +0200 | [diff] [blame] | 784 | (int) subscriber->expire_lu, |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 785 | subscriber->imsi); |
| 786 | |
Jan Luebbe | 9eca37f | 2009-08-12 21:04:54 +0200 | [diff] [blame] | 787 | free(q_tmsi); |
Harald Welte | 019d016 | 2010-12-26 19:12:30 +0100 | [diff] [blame] | 788 | free(q_name); |
| 789 | free(q_extension); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 790 | |
| 791 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 792 | LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 793 | return 1; |
| 794 | } |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 795 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 796 | dbi_result_free(result); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 797 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 798 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 801 | int db_sync_equipment(struct gsm_equipment *equip) |
| 802 | { |
| 803 | dbi_result result; |
| 804 | unsigned char *cm2, *cm3; |
Holger Hans Peter Freyther | f64a20f | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 805 | char *q_imei; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 806 | uint8_t classmark1; |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 807 | |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 808 | memcpy(&classmark1, &equip->classmark1, sizeof(classmark1)); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 809 | DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x", |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 810 | equip->imei, classmark1); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 811 | if (equip->classmark2_len) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 812 | DEBUGPC(DDB, ", classmark2=%s", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 813 | osmo_hexdump(equip->classmark2, equip->classmark2_len)); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 814 | if (equip->classmark3_len) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 815 | DEBUGPC(DDB, ", classmark3=%s", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 816 | osmo_hexdump(equip->classmark3, equip->classmark3_len)); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 817 | DEBUGPC(DDB, "\n"); |
Harald Welte (local) | ee4410a | 2009-08-17 09:39:55 +0200 | [diff] [blame] | 818 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 819 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 820 | equip->classmark2_len, &cm2); |
| 821 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 822 | equip->classmark3_len, &cm3); |
Holger Hans Peter Freyther | f64a20f | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 823 | dbi_conn_quote_string_copy(conn, equip->imei, &q_imei); |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 824 | |
| 825 | result = dbi_conn_queryf(conn, |
| 826 | "UPDATE Equipment SET " |
| 827 | "updated = datetime('now'), " |
| 828 | "classmark1 = %u, " |
| 829 | "classmark2 = %s, " |
| 830 | "classmark3 = %s " |
Holger Hans Peter Freyther | f64a20f | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 831 | "WHERE imei = %s ", |
| 832 | classmark1, cm2, cm3, q_imei); |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 833 | |
| 834 | free(cm2); |
| 835 | free(cm3); |
Holger Hans Peter Freyther | f64a20f | 2010-12-26 20:04:49 +0100 | [diff] [blame] | 836 | free(q_imei); |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 837 | |
| 838 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 839 | LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n"); |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 840 | return -EIO; |
| 841 | } |
| 842 | |
| 843 | dbi_result_free(result); |
| 844 | return 0; |
| 845 | } |
| 846 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 847 | int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id)) |
| 848 | { |
| 849 | dbi_result result; |
| 850 | |
| 851 | result = dbi_conn_query(conn, |
| 852 | "SELECT id " |
| 853 | "FROM Subscriber " |
| 854 | "WHERE lac != 0 AND " |
| 855 | "( expire_lu is NULL " |
| 856 | "OR expire_lu < datetime('now') ) " |
| 857 | "LIMIT 1"); |
| 858 | if (!result) { |
| 859 | LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n"); |
| 860 | return -EIO; |
| 861 | } |
| 862 | |
| 863 | while (dbi_result_next_row(result)) |
| 864 | callback(priv, dbi_result_get_ulonglong(result, "id")); |
| 865 | |
| 866 | dbi_result_free(result); |
| 867 | return 0; |
| 868 | } |
| 869 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 870 | int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber) |
| 871 | { |
| 872 | dbi_result result = NULL; |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 873 | char tmsi[14]; |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 874 | char *tmsi_quoted; |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 875 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 876 | for (;;) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 877 | subscriber->tmsi = rand(); |
| 878 | if (subscriber->tmsi == GSM_RESERVED_TMSI) |
| 879 | continue; |
| 880 | |
| 881 | sprintf(tmsi, "%u", subscriber->tmsi); |
| 882 | dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 883 | result = dbi_conn_queryf(conn, |
| 884 | "SELECT * FROM Subscriber " |
| 885 | "WHERE tmsi = %s ", |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 886 | tmsi_quoted); |
| 887 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 888 | free(tmsi_quoted); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 889 | |
| 890 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 891 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 892 | "while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 893 | return 1; |
| 894 | } |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 895 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 896 | dbi_result_free(result); |
| 897 | continue; |
| 898 | } |
| 899 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 900 | dbi_result_free(result); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 901 | DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n", |
| 902 | subscriber->tmsi, subscriber->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 903 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 904 | } |
| 905 | dbi_result_free(result); |
| 906 | } |
| 907 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 910 | int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber) |
| 911 | { |
| 912 | dbi_result result = NULL; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 913 | uint32_t try; |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 914 | |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 915 | for (;;) { |
Jan Luebbe | f0b4cef | 2009-08-12 21:27:43 +0200 | [diff] [blame] | 916 | try = (rand()%(GSM_MAX_EXTEN-GSM_MIN_EXTEN+1)+GSM_MIN_EXTEN); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 917 | result = dbi_conn_queryf(conn, |
| 918 | "SELECT * FROM Subscriber " |
Jan Luebbe | 1da59ed | 2009-08-12 19:59:27 +0200 | [diff] [blame] | 919 | "WHERE extension = %i", |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 920 | try |
| 921 | ); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 922 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 923 | LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber " |
| 924 | "while allocating new extension.\n"); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 925 | return 1; |
| 926 | } |
| 927 | if (dbi_result_get_numrows(result)){ |
| 928 | dbi_result_free(result); |
| 929 | continue; |
| 930 | } |
| 931 | if (!dbi_result_next_row(result)) { |
| 932 | dbi_result_free(result); |
| 933 | break; |
| 934 | } |
| 935 | dbi_result_free(result); |
| 936 | } |
| 937 | sprintf(subscriber->extension, "%i", try); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 938 | DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | ebcce2a | 2009-08-12 19:45:37 +0200 | [diff] [blame] | 939 | return db_sync_subscriber(subscriber); |
| 940 | } |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 941 | /* |
| 942 | * try to allocate a new unique token for this subscriber and return it |
| 943 | * via a parameter. if the subscriber already has a token, return |
| 944 | * an error. |
| 945 | */ |
| 946 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 947 | 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] | 948 | { |
| 949 | dbi_result result; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 950 | uint32_t try; |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 951 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 952 | for (;;) { |
| 953 | try = rand(); |
| 954 | if (!try) /* 0 is an invalid token */ |
| 955 | continue; |
| 956 | result = dbi_conn_queryf(conn, |
| 957 | "SELECT * FROM AuthToken " |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 958 | "WHERE subscriber_id = %llu OR token = \"%08X\" ", |
| 959 | subscriber->id, try); |
| 960 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 961 | LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken " |
| 962 | "while allocating new token.\n"); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 963 | return 1; |
| 964 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 965 | if (dbi_result_get_numrows(result)) { |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 966 | dbi_result_free(result); |
| 967 | continue; |
| 968 | } |
| 969 | if (!dbi_result_next_row(result)) { |
| 970 | dbi_result_free(result); |
| 971 | break; |
| 972 | } |
| 973 | dbi_result_free(result); |
| 974 | } |
| 975 | result = dbi_conn_queryf(conn, |
| 976 | "INSERT INTO AuthToken " |
| 977 | "(subscriber_id, created, token) " |
| 978 | "VALUES " |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 979 | "(%llu, datetime('now'), \"%08X\") ", |
| 980 | subscriber->id, try); |
| 981 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 982 | LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for " |
| 983 | "IMSI %s.\n", try, subscriber->imsi); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 984 | return 1; |
| 985 | } |
Harald Welte (local) | 3feef25 | 2009-08-13 13:26:11 +0200 | [diff] [blame] | 986 | dbi_result_free(result); |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 987 | *token = try; |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 988 | 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] | 989 | |
Jan Luebbe | 31bef49 | 2009-08-12 14:31:14 +0200 | [diff] [blame] | 990 | return 0; |
| 991 | } |
| 992 | |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 993 | int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM_IMEI_LENGTH]) |
| 994 | { |
Harald Welte | d409be7 | 2009-11-07 00:06:19 +0900 | [diff] [blame] | 995 | unsigned long long equipment_id, watch_id; |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 996 | dbi_result result; |
| 997 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 998 | strncpy(subscriber->equipment.imei, imei, |
| 999 | sizeof(subscriber->equipment.imei)-1), |
| 1000 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1001 | result = dbi_conn_queryf(conn, |
| 1002 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 1003 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1004 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 1005 | "(%s, datetime('now'), datetime('now')) ", |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1006 | imei); |
| 1007 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1008 | LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n"); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1009 | return 1; |
| 1010 | } |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1011 | |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 1012 | equipment_id = 0; |
| 1013 | if (dbi_result_get_numrows_affected(result)) { |
| 1014 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 1015 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1016 | dbi_result_free(result); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1017 | |
| 1018 | if (equipment_id) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1019 | DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1020 | else { |
| 1021 | result = dbi_conn_queryf(conn, |
| 1022 | "SELECT id FROM Equipment " |
| 1023 | "WHERE imei = %s ", |
| 1024 | imei |
| 1025 | ); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1026 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1027 | LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n"); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1028 | return 1; |
| 1029 | } |
| 1030 | if (!dbi_result_next_row(result)) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1031 | LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n"); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1032 | dbi_result_free(result); |
| 1033 | return 1; |
| 1034 | } |
| 1035 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 1036 | dbi_result_free(result); |
| 1037 | } |
| 1038 | |
| 1039 | result = dbi_conn_queryf(conn, |
| 1040 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 1041 | "(subscriber_id, equipment_id, created, updated) " |
| 1042 | "VALUES " |
| 1043 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1044 | subscriber->id, equipment_id); |
| 1045 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1046 | LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n"); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1047 | return 1; |
| 1048 | } |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1049 | |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 1050 | watch_id = 0; |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1051 | if (dbi_result_get_numrows_affected(result)) |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 1052 | watch_id = dbi_conn_sequence_last(conn, NULL); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1053 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1054 | dbi_result_free(result); |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1055 | if (watch_id) |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1056 | DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 1057 | equipment_id, subscriber->imsi, imei); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1058 | else { |
| 1059 | result = dbi_conn_queryf(conn, |
| 1060 | "UPDATE EquipmentWatch " |
| 1061 | "SET updated = datetime('now') " |
| 1062 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
Harald Welte | 0b906d0 | 2009-12-24 11:21:42 +0100 | [diff] [blame] | 1063 | subscriber->id, equipment_id); |
| 1064 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1065 | LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n"); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1066 | return 1; |
| 1067 | } |
| 1068 | dbi_result_free(result); |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1069 | DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", |
| 1070 | equipment_id, subscriber->imsi, imei); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1076 | /* store an [unsent] SMS to the database */ |
| 1077 | int db_sms_store(struct gsm_sms *sms) |
| 1078 | { |
| 1079 | dbi_result result; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1080 | char *q_text, *q_daddr; |
| 1081 | unsigned char *q_udata; |
| 1082 | char *validity_timestamp = "2222-2-2"; |
| 1083 | |
| 1084 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1085 | |
| 1086 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 1087 | dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1088 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 1089 | &q_udata); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1090 | /* FIXME: correct validity period */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1091 | result = dbi_conn_queryf(conn, |
| 1092 | "INSERT INTO SMS " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1093 | "(created, sender_id, receiver_id, valid_until, " |
| 1094 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1095 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 1096 | "user_data, text) VALUES " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1097 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1098 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1099 | sms->sender->id, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1100 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 1101 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 1102 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 1103 | q_daddr, q_udata, q_text); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1104 | free(q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1105 | free(q_daddr); |
| 1106 | free(q_udata); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (!result) |
| 1109 | return -EIO; |
| 1110 | |
| 1111 | dbi_result_free(result); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1115 | 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] | 1116 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1117 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1118 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1119 | const char *text, *daddr; |
| 1120 | const unsigned char *user_data; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1121 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1122 | if (!sms) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1123 | return NULL; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1124 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1125 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1126 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1127 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1128 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 1129 | strncpy(sms->src.addr, sms->sender->extension, sizeof(sms->src.addr)-1); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1130 | |
| 1131 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1132 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1133 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1134 | /* FIXME: validity */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1135 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
| 1136 | sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); |
| 1137 | sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); |
| 1138 | sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind"); |
| 1139 | sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); |
| 1140 | sms->data_coding_scheme = dbi_result_get_uint(result, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1141 | "data_coding_scheme"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1142 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 1143 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1144 | daddr = dbi_result_get_string(result, "dest_addr"); |
| 1145 | if (daddr) { |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 1146 | strncpy(sms->dst.addr, daddr, sizeof(sms->dst.addr)); |
| 1147 | sms->dst.addr[sizeof(sms->dst.addr)-1] = '\0'; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 1151 | user_data = dbi_result_get_binary(result, "user_data"); |
| 1152 | if (sms->user_data_len > sizeof(sms->user_data)) |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1153 | sms->user_data_len = (uint8_t) sizeof(sms->user_data); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1154 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1155 | |
| 1156 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1157 | if (text) { |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 1158 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 1159 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 1160 | } |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1161 | return sms; |
| 1162 | } |
| 1163 | |
Holger Hans Peter Freyther | 812dad0 | 2010-12-24 23:18:31 +0100 | [diff] [blame] | 1164 | struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id) |
| 1165 | { |
| 1166 | dbi_result result; |
| 1167 | struct gsm_sms *sms; |
| 1168 | |
| 1169 | result = dbi_conn_queryf(conn, |
| 1170 | "SELECT * FROM SMS WHERE SMS.id = %llu", id); |
| 1171 | if (!result) |
| 1172 | return NULL; |
| 1173 | |
| 1174 | if (!dbi_result_next_row(result)) { |
| 1175 | dbi_result_free(result); |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | |
| 1179 | sms = sms_from_result(net, result); |
| 1180 | |
| 1181 | dbi_result_free(result); |
| 1182 | |
| 1183 | return sms; |
| 1184 | } |
| 1185 | |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1186 | /* retrieve the next unsent SMS with ID >= min_id */ |
Holger Hans Peter Freyther | b464fb4 | 2010-03-25 09:59:30 +0100 | [diff] [blame] | 1187 | 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] | 1188 | { |
| 1189 | dbi_result result; |
| 1190 | struct gsm_sms *sms; |
| 1191 | |
| 1192 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 7a7d364 | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1193 | "SELECT SMS.* " |
| 1194 | "FROM SMS JOIN Subscriber ON " |
| 1195 | "SMS.receiver_id = Subscriber.id " |
| 1196 | "WHERE SMS.id >= %llu AND SMS.sent IS NULL " |
| 1197 | "AND Subscriber.lac > 0 " |
| 1198 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1199 | min_id); |
| 1200 | if (!result) |
| 1201 | return NULL; |
| 1202 | |
| 1203 | if (!dbi_result_next_row(result)) { |
| 1204 | dbi_result_free(result); |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
| 1208 | sms = sms_from_result(net, result); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1209 | |
| 1210 | dbi_result_free(result); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1211 | |
| 1212 | return sms; |
| 1213 | } |
| 1214 | |
Holger Hans Peter Freyther | 73b878a | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1215 | struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, |
| 1216 | unsigned long long min_subscr_id, |
| 1217 | unsigned int failed) |
Sylvain Munaut | ff1f19e | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1218 | { |
| 1219 | dbi_result result; |
| 1220 | struct gsm_sms *sms; |
| 1221 | |
| 1222 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 7a7d364 | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1223 | "SELECT SMS.* " |
| 1224 | "FROM SMS JOIN Subscriber ON " |
| 1225 | "SMS.receiver_id = Subscriber.id " |
| 1226 | "WHERE SMS.receiver_id >= %llu AND SMS.sent IS NULL " |
Holger Hans Peter Freyther | 73b878a | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1227 | "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u " |
Sylvain Munaut | 7a7d364 | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1228 | "ORDER BY SMS.receiver_id, SMS.id LIMIT 1", |
Holger Hans Peter Freyther | 73b878a | 2010-12-25 00:33:40 +0100 | [diff] [blame] | 1229 | min_subscr_id, failed); |
Sylvain Munaut | ff1f19e | 2009-12-22 13:22:29 +0100 | [diff] [blame] | 1230 | if (!result) |
| 1231 | return NULL; |
| 1232 | |
| 1233 | if (!dbi_result_next_row(result)) { |
| 1234 | dbi_result_free(result); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
| 1238 | sms = sms_from_result(net, result); |
| 1239 | |
| 1240 | dbi_result_free(result); |
| 1241 | |
| 1242 | return sms; |
| 1243 | } |
| 1244 | |
Sylvain Munaut | d5778fc | 2009-12-21 01:09:57 +0100 | [diff] [blame] | 1245 | /* retrieve the next unsent SMS for a given subscriber */ |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1246 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 1247 | { |
| 1248 | dbi_result result; |
| 1249 | struct gsm_sms *sms; |
| 1250 | |
| 1251 | result = dbi_conn_queryf(conn, |
Sylvain Munaut | 7a7d364 | 2010-07-03 22:00:45 +0200 | [diff] [blame] | 1252 | "SELECT SMS.* " |
| 1253 | "FROM SMS JOIN Subscriber ON " |
| 1254 | "SMS.receiver_id = Subscriber.id " |
| 1255 | "WHERE SMS.receiver_id = %llu AND SMS.sent IS NULL " |
| 1256 | "AND Subscriber.lac > 0 " |
| 1257 | "ORDER BY SMS.id LIMIT 1", |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 1258 | subscr->id); |
| 1259 | if (!result) |
| 1260 | return NULL; |
| 1261 | |
| 1262 | if (!dbi_result_next_row(result)) { |
| 1263 | dbi_result_free(result); |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | |
| 1267 | sms = sms_from_result(subscr->net, result); |
| 1268 | |
| 1269 | dbi_result_free(result); |
| 1270 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 1271 | return sms; |
| 1272 | } |
| 1273 | |
| 1274 | /* mark a given SMS as read */ |
| 1275 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 1276 | { |
| 1277 | dbi_result result; |
| 1278 | |
| 1279 | result = dbi_conn_queryf(conn, |
| 1280 | "UPDATE SMS " |
| 1281 | "SET sent = datetime('now') " |
| 1282 | "WHERE id = %llu", sms->id); |
| 1283 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1284 | 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] | 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | dbi_result_free(result); |
| 1289 | return 0; |
| 1290 | } |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1291 | |
| 1292 | /* increase the number of attempted deliveries */ |
| 1293 | int db_sms_inc_deliver_attempts(struct gsm_sms *sms) |
| 1294 | { |
| 1295 | dbi_result result; |
| 1296 | |
| 1297 | result = dbi_conn_queryf(conn, |
| 1298 | "UPDATE SMS " |
| 1299 | "SET deliver_attempts = deliver_attempts + 1 " |
| 1300 | "WHERE id = %llu", sms->id); |
| 1301 | if (!result) { |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 1302 | LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for " |
| 1303 | "SMS %llu.\n", sms->id); |
Harald Welte (local) | db552c5 | 2009-08-15 20:15:14 +0200 | [diff] [blame] | 1304 | return 1; |
| 1305 | } |
| 1306 | |
| 1307 | dbi_result_free(result); |
| 1308 | return 0; |
| 1309 | } |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1310 | |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 1311 | int db_apdu_blob_store(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 1312 | uint8_t apdu_id_flags, uint8_t len, |
| 1313 | uint8_t *apdu) |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1314 | { |
| 1315 | dbi_result result; |
Holger Hans Peter Freyther | 2657abf | 2009-10-22 15:34:37 +0200 | [diff] [blame] | 1316 | unsigned char *q_apdu; |
Harald Welte (local) | 026531e | 2009-08-16 10:40:10 +0200 | [diff] [blame] | 1317 | |
| 1318 | dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu); |
| 1319 | |
| 1320 | result = dbi_conn_queryf(conn, |
| 1321 | "INSERT INTO ApduBlobs " |
| 1322 | "(created,subscriber_id,apdu_id_flags,apdu) VALUES " |
| 1323 | "(datetime('now'),%llu,%u,%s)", |
| 1324 | subscr->id, apdu_id_flags, q_apdu); |
| 1325 | |
| 1326 | free(q_apdu); |
| 1327 | |
| 1328 | if (!result) |
| 1329 | return -EIO; |
| 1330 | |
| 1331 | dbi_result_free(result); |
| 1332 | return 0; |
| 1333 | } |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1334 | |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 1335 | int db_store_counter(struct osmo_counter *ctr) |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1336 | { |
| 1337 | dbi_result result; |
| 1338 | char *q_name; |
| 1339 | |
| 1340 | dbi_conn_quote_string_copy(conn, ctr->name, &q_name); |
| 1341 | |
| 1342 | result = dbi_conn_queryf(conn, |
| 1343 | "INSERT INTO Counters " |
| 1344 | "(timestamp,name,value) VALUES " |
| 1345 | "(datetime('now'),%s,%lu)", q_name, ctr->value); |
| 1346 | |
| 1347 | free(q_name); |
| 1348 | |
| 1349 | if (!result) |
| 1350 | return -EIO; |
| 1351 | |
| 1352 | dbi_result_free(result); |
| 1353 | return 0; |
| 1354 | } |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1355 | |
| 1356 | static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num, |
| 1357 | char *q_prefix) |
| 1358 | { |
| 1359 | dbi_result result; |
| 1360 | char *q_name; |
| 1361 | |
| 1362 | dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name, |
| 1363 | &q_name); |
| 1364 | |
| 1365 | result = dbi_conn_queryf(conn, |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1366 | "Insert INTO RateCounters " |
Harald Welte | d94d6a0 | 2010-05-14 17:38:47 +0200 | [diff] [blame] | 1367 | "(timestamp,name,idx,value) VALUES " |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1368 | "(datetime('now'),%s.%s,%u,%"PRIu64")", |
| 1369 | q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current); |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1370 | |
| 1371 | free(q_name); |
| 1372 | |
| 1373 | if (!result) |
| 1374 | return -EIO; |
| 1375 | |
| 1376 | dbi_result_free(result); |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | int db_store_rate_ctr_group(struct rate_ctr_group *ctrg) |
| 1381 | { |
| 1382 | unsigned int i; |
| 1383 | char *q_prefix; |
| 1384 | |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1385 | 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] | 1386 | |
| 1387 | for (i = 0; i < ctrg->desc->num_ctr; i++) |
| 1388 | db_store_rate_ctr(ctrg, i, q_prefix); |
| 1389 | |
| 1390 | free(q_prefix); |
| 1391 | |
| 1392 | return 0; |
| 1393 | } |