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> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 22 | #include <openbsc/gsm_data.h> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 23 | #include <openbsc/db.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 24 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 25 | #include <libgen.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 30 | #include <dbi/dbi.h> |
| 31 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 32 | static char *db_basename = NULL; |
| 33 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 34 | static dbi_conn conn; |
Holger Freyther | 36650b8 | 2009-04-19 06:35:16 +0000 | [diff] [blame] | 35 | static struct gsm_network *current_network = NULL; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 36 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 37 | static char *create_stmts[] = { |
| 38 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 39 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 40 | "key TEXT UNIQUE NOT NULL, " |
| 41 | "value TEXT NOT NULL" |
| 42 | ")", |
| 43 | "INSERT OR IGNORE INTO Meta " |
| 44 | "(key, value) " |
| 45 | "VALUES " |
| 46 | "('revision', '1')", |
| 47 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 48 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 49 | "created TIMESTAMP NOT NULL, " |
| 50 | "updated TIMESTAMP NOT NULL, " |
| 51 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 52 | "name TEXT, " |
| 53 | "extension TEXT UNIQUE, " |
| 54 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 55 | "tmsi TEXT UNIQUE, " |
| 56 | "lac INTEGER NOT NULL DEFAULT 0" |
| 57 | ")", |
| 58 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 59 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 60 | "created TIMESTAMP NOT NULL, " |
| 61 | "updated TIMESTAMP NOT NULL, " |
| 62 | "name TEXT, " |
| 63 | "imei NUMERIC UNIQUE NOT NULL" |
| 64 | ")", |
| 65 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 66 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 67 | "created TIMESTAMP NOT NULL, " |
| 68 | "updated TIMESTAMP NOT NULL, " |
| 69 | "subscriber_id NUMERIC NOT NULL, " |
| 70 | "equipment_id NUMERIC NOT NULL, " |
| 71 | "UNIQUE (subscriber_id, equipment_id) " |
| 72 | ")", |
| 73 | "CREATE TABLE IF NOT EXISTS SMS (" |
| 74 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 75 | "created TIMESTAMP NOT NULL, " |
| 76 | "sent TIMESTAMP, " |
| 77 | "sender_id NUMERIC NOT NULL, " |
| 78 | "receiver_id NUMERIC NOT NULL, " |
| 79 | "header NUMERIC, " |
| 80 | "text TEXT NOT NULL " |
| 81 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 82 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 83 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 84 | "created TIMESTAMP NOT NULL, " |
| 85 | "updated TIMESTAMP NOT NULL, " |
| 86 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 87 | "last_bts NUMERIC NOT NULL " |
| 88 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 91 | void db_error_func(dbi_conn conn, void* data) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 92 | const char* msg; |
| 93 | dbi_conn_error(conn, &msg); |
| 94 | printf("DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Holger Freyther | 36650b8 | 2009-04-19 06:35:16 +0000 | [diff] [blame] | 97 | int db_init(const char *name, struct gsm_network *network) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 98 | dbi_initialize(NULL); |
| 99 | conn = dbi_conn_new("sqlite3"); |
| 100 | if (conn==NULL) { |
| 101 | printf("DB: Failed to create connection.\n"); |
| 102 | return 1; |
| 103 | } |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 104 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 105 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 106 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 107 | /* MySQL |
| 108 | dbi_conn_set_option(conn, "host", "localhost"); |
| 109 | dbi_conn_set_option(conn, "username", "your_name"); |
| 110 | dbi_conn_set_option(conn, "password", "your_password"); |
| 111 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 112 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 113 | */ |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 114 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 115 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 116 | db_basename = strdup(name); |
| 117 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 118 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 119 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 120 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 121 | if (dbi_conn_connect(conn) < 0) { |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 122 | free(db_dirname); |
| 123 | free(db_basename); |
| 124 | db_dirname = db_basename = NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Holger Freyther | 36650b8 | 2009-04-19 06:35:16 +0000 | [diff] [blame] | 128 | current_network = network; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 129 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int db_prepare() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 133 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 134 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 135 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 136 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 137 | result = dbi_conn_query(conn, create_stmts[i]); |
| 138 | if (result==NULL) { |
| 139 | printf("DB: Failed to create some table.\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | dbi_result_free(result); |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 143 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 144 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 145 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int db_fini() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 149 | dbi_conn_close(conn); |
| 150 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 151 | |
| 152 | if (db_dirname) |
| 153 | free(db_dirname); |
| 154 | if (db_basename) |
| 155 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 156 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 159 | struct gsm_subscriber* db_create_subscriber(char *imsi) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 160 | dbi_result result; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 161 | struct gsm_subscriber* subscr; |
| 162 | |
| 163 | /* Is this subscriber known in the db? */ |
| 164 | subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
| 165 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 166 | result = dbi_conn_queryf(conn, |
| 167 | "UPDATE Subscriber set updated = datetime('now') " |
| 168 | "WHERE imsi = %s " , imsi); |
| 169 | if (result==NULL) { |
| 170 | printf("DB: failed to update timestamp\n"); |
| 171 | } else { |
| 172 | dbi_result_free(result); |
| 173 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 174 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 175 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 176 | |
| 177 | subscr = subscr_alloc(); |
| 178 | if (!subscr) |
| 179 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 180 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 181 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 182 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 183 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 184 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 185 | imsi |
| 186 | ); |
| 187 | if (result==NULL) { |
| 188 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 189 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 190 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 191 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 192 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 193 | printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
| 194 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 197 | struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 198 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 199 | const char *string; |
| 200 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 201 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 202 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 203 | switch (field) { |
| 204 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 205 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 206 | result = dbi_conn_queryf(conn, |
| 207 | "SELECT * FROM Subscriber " |
| 208 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 209 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 210 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 211 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 212 | break; |
| 213 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 214 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 215 | result = dbi_conn_queryf(conn, |
| 216 | "SELECT * FROM Subscriber " |
| 217 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 218 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 219 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 220 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 221 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 222 | case GSM_SUBSCRIBER_EXTENSION: |
| 223 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 224 | result = dbi_conn_queryf(conn, |
| 225 | "SELECT * FROM Subscriber " |
| 226 | "WHERE extension = %s ", |
| 227 | quoted |
| 228 | ); |
| 229 | free(quoted); |
| 230 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 231 | default: |
| 232 | printf("DB: Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 233 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 234 | } |
| 235 | if (result==NULL) { |
| 236 | printf("DB: Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 237 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 238 | } |
| 239 | if (!dbi_result_next_row(result)) { |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 240 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 241 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 242 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 243 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 244 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 245 | |
| 246 | subscr = subscr_alloc(); |
| 247 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 248 | string = dbi_result_get_string(result, "imsi"); |
| 249 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 250 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 251 | |
| 252 | string = dbi_result_get_string(result, "tmsi"); |
| 253 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 254 | strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 255 | |
| 256 | string = dbi_result_get_string(result, "name"); |
| 257 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 258 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 259 | |
| 260 | string = dbi_result_get_string(result, "extension"); |
| 261 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 262 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 263 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 264 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 265 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Jan Luebbe | 3444ddb | 2008-12-28 12:37:15 +0000 | [diff] [blame] | 266 | printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, LAC %hu, AUTH %u\n", |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 267 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, |
| 268 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 269 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 270 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 273 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 274 | dbi_result result; |
| 275 | result = dbi_conn_queryf(conn, |
| 276 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 277 | "SET updated = datetime('now'), " |
| 278 | "tmsi = %s, " |
| 279 | "lac = %i, " |
| 280 | "authorized = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 281 | "WHERE imsi = %s ", |
Jan Luebbe | 6e2e545 | 2008-12-27 16:47:55 +0000 | [diff] [blame] | 282 | subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 283 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 284 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 285 | if (result==NULL) { |
| 286 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 287 | return 1; |
| 288 | } |
| 289 | dbi_result_free(result); |
| 290 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 293 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 294 | dbi_result result=NULL; |
| 295 | char* tmsi_quoted; |
| 296 | for (;;) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 297 | sprintf(subscriber->tmsi, "%i", rand()); |
| 298 | dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 299 | result = dbi_conn_queryf(conn, |
| 300 | "SELECT * FROM Subscriber " |
| 301 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 302 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 303 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 304 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 305 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 306 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 307 | return 1; |
| 308 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 309 | if (dbi_result_get_numrows(result)){ |
| 310 | dbi_result_free(result); |
| 311 | continue; |
| 312 | } |
| 313 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 314 | dbi_result_free(result); |
| 315 | printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 316 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 317 | } |
| 318 | dbi_result_free(result); |
| 319 | } |
| 320 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 323 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
| 324 | u_int64_t equipment_id, watch_id; |
| 325 | dbi_result result; |
| 326 | |
| 327 | result = dbi_conn_queryf(conn, |
| 328 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 329 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 330 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 331 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 332 | imei |
| 333 | ); |
| 334 | if (result==NULL) { |
| 335 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 336 | return 1; |
| 337 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 338 | equipment_id = 0; |
| 339 | if (dbi_result_get_numrows_affected(result)) { |
| 340 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 341 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 342 | dbi_result_free(result); |
| 343 | if (equipment_id) { |
| 344 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 345 | } |
| 346 | else { |
| 347 | result = dbi_conn_queryf(conn, |
| 348 | "SELECT id FROM Equipment " |
| 349 | "WHERE imei = %s ", |
| 350 | imei |
| 351 | ); |
| 352 | if (result==NULL) { |
| 353 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 354 | return 1; |
| 355 | } |
| 356 | if (!dbi_result_next_row(result)) { |
| 357 | printf("DB: Failed to find the Equipment.\n"); |
| 358 | dbi_result_free(result); |
| 359 | return 1; |
| 360 | } |
| 361 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 362 | dbi_result_free(result); |
| 363 | } |
| 364 | |
| 365 | result = dbi_conn_queryf(conn, |
| 366 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 367 | "(subscriber_id, equipment_id, created, updated) " |
| 368 | "VALUES " |
| 369 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 370 | subscriber->id, equipment_id |
| 371 | ); |
| 372 | if (result==NULL) { |
| 373 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 374 | return 1; |
| 375 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 376 | watch_id = 0; |
| 377 | if (dbi_result_get_numrows_affected(result)) { |
| 378 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 379 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 380 | dbi_result_free(result); |
| 381 | if (watch_id) { |
| 382 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 383 | } |
| 384 | else { |
| 385 | result = dbi_conn_queryf(conn, |
| 386 | "UPDATE EquipmentWatch " |
| 387 | "SET updated = datetime('now') " |
| 388 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 389 | subscriber->id, equipment_id |
| 390 | ); |
| 391 | if (result==NULL) { |
| 392 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 393 | return 1; |
| 394 | } |
| 395 | dbi_result_free(result); |
| 396 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 402 | /* store an [unsent] SMS to the database */ |
| 403 | int db_sms_store(struct gsm_sms *sms) |
| 404 | { |
| 405 | dbi_result result; |
| 406 | char *q_text; |
| 407 | |
| 408 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
| 409 | result = dbi_conn_queryf(conn, |
| 410 | "INSERT INTO SMS " |
| 411 | "(created,sender_id,receiver_id,header,text) VALUES " |
| 412 | "(datetime('now'),%llu,%llu,%s,%s)", |
| 413 | sms->sender->id, |
| 414 | sms->receiver ? sms->receiver->id : 0, |
| 415 | NULL, q_text); |
| 416 | free(q_text); |
| 417 | |
| 418 | if (!result) |
| 419 | return -EIO; |
| 420 | |
| 421 | dbi_result_free(result); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 426 | struct gsm_sms *db_sms_get_unsent(int min_id) |
| 427 | { |
| 428 | dbi_result result; |
| 429 | struct gsm_sms *sms = malloc(sizeof(*sms)); |
| 430 | |
| 431 | if (!sms) { |
| 432 | free(sms); |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | result = dbi_conn_queryf(conn, |
| 437 | "SELECT * FROM SMS " |
| 438 | "WHERE id >= %llu ORDER BY id", min_id); |
| 439 | if (!result) { |
| 440 | free(sms); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | /* FIXME: fill gsm_sms from database */ |
| 445 | |
| 446 | dbi_result_free(result); |
| 447 | return sms; |
| 448 | } |
| 449 | |
| 450 | /* mark a given SMS as read */ |
| 451 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 452 | { |
| 453 | dbi_result result; |
| 454 | |
| 455 | result = dbi_conn_queryf(conn, |
| 456 | "UPDATE SMS " |
| 457 | "SET sent = datetime('now') " |
| 458 | "WHERE id = %llu", sms->id); |
| 459 | if (!result) { |
| 460 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | dbi_result_free(result); |
| 465 | return 0; |
| 466 | } |