2009-01-01 18:02:05 +00:00
|
|
|
/* Simple HLR/VLR database backend using dbi */
|
2008-12-27 01:07:07 +00:00
|
|
|
/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
|
2009-01-01 18:02:05 +00:00
|
|
|
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2009-07-05 12:08:13 +00:00
|
|
|
* (C) 2009 by Harald Welte <laforge@gnumonks.org>
|
2008-12-27 01:07:07 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2011-01-01 14:25:50 +00:00
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2008-12-27 01:07:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-01-01 14:25:50 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
2008-12-27 01:07:07 +00:00
|
|
|
*
|
2011-01-01 14:25:50 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-27 01:07:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-05-13 09:45:07 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
2008-12-28 22:51:39 +00:00
|
|
|
#include <libgen.h>
|
2008-12-27 00:45:41 +00:00
|
|
|
#include <stdio.h>
|
2016-06-30 08:25:49 +00:00
|
|
|
#include <stdbool.h>
|
2008-12-27 15:59:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-30 20:56:32 +00:00
|
|
|
#include <errno.h>
|
2008-12-27 00:45:41 +00:00
|
|
|
#include <dbi/dbi.h>
|
|
|
|
|
2010-05-13 09:45:07 +00:00
|
|
|
#include <openbsc/gsm_data.h>
|
2010-12-22 17:21:14 +00:00
|
|
|
#include <openbsc/gsm_subscriber.h>
|
2010-05-13 09:45:07 +00:00
|
|
|
#include <openbsc/gsm_04_11.h>
|
|
|
|
#include <openbsc/db.h>
|
|
|
|
#include <openbsc/debug.h>
|
2010-12-22 17:16:01 +00:00
|
|
|
|
2016-04-20 15:50:17 +00:00
|
|
|
#include <osmocom/gsm/protocol/gsm_23_003.h>
|
2011-03-22 15:47:59 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/statistics.h>
|
|
|
|
#include <osmocom/core/rate_ctr.h>
|
2017-01-13 02:12:08 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2010-05-13 09:45:07 +00:00
|
|
|
|
2015-10-08 14:10:23 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2014-03-07 17:17:22 +00:00
|
|
|
/* Semi-Private-Interface (SPI) for the subscriber code */
|
|
|
|
void subscr_direct_free(struct gsm_subscriber *subscr);
|
|
|
|
|
2008-12-28 22:51:39 +00:00
|
|
|
static char *db_basename = NULL;
|
|
|
|
static char *db_dirname = NULL;
|
2009-04-19 06:35:20 +00:00
|
|
|
static dbi_conn conn;
|
2008-12-27 00:45:41 +00:00
|
|
|
|
2014-03-07 17:17:22 +00:00
|
|
|
#define SCHEMA_REVISION "4"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SCHEMA_META,
|
|
|
|
INSERT_META,
|
|
|
|
SCHEMA_SUBSCRIBER,
|
|
|
|
SCHEMA_AUTH,
|
|
|
|
SCHEMA_EQUIPMENT,
|
|
|
|
SCHEMA_EQUIPMENT_WATCH,
|
|
|
|
SCHEMA_SMS,
|
|
|
|
SCHEMA_VLR,
|
|
|
|
SCHEMA_APDU,
|
|
|
|
SCHEMA_COUNTERS,
|
|
|
|
SCHEMA_RATE,
|
|
|
|
SCHEMA_AUTHKEY,
|
|
|
|
SCHEMA_AUTHLAST,
|
|
|
|
};
|
2012-12-26 23:27:16 +00:00
|
|
|
|
2014-03-07 17:17:22 +00:00
|
|
|
static const char *create_stmts[] = {
|
|
|
|
[SCHEMA_META] = "CREATE TABLE IF NOT EXISTS Meta ("
|
2009-03-30 20:56:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"key TEXT UNIQUE NOT NULL, "
|
|
|
|
"value TEXT NOT NULL"
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[INSERT_META] = "INSERT OR IGNORE INTO Meta "
|
2009-03-30 20:56:32 +00:00
|
|
|
"(key, value) "
|
|
|
|
"VALUES "
|
2012-12-26 23:27:16 +00:00
|
|
|
"('revision', " SCHEMA_REVISION ")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_SUBSCRIBER] = "CREATE TABLE IF NOT EXISTS Subscriber ("
|
2009-03-30 20:56:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"updated TIMESTAMP NOT NULL, "
|
|
|
|
"imsi NUMERIC UNIQUE NOT NULL, "
|
|
|
|
"name TEXT, "
|
|
|
|
"extension TEXT UNIQUE, "
|
|
|
|
"authorized INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"tmsi TEXT UNIQUE, "
|
2012-12-26 23:27:16 +00:00
|
|
|
"lac INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"expire_lu TIMESTAMP DEFAULT NULL"
|
2009-03-30 20:56:32 +00:00
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_AUTH] = "CREATE TABLE IF NOT EXISTS AuthToken ("
|
2009-08-12 12:31:14 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"subscriber_id INTEGER UNIQUE NOT NULL, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"token TEXT UNIQUE NOT NULL"
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_EQUIPMENT] = "CREATE TABLE IF NOT EXISTS Equipment ("
|
2009-03-30 20:56:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"updated TIMESTAMP NOT NULL, "
|
|
|
|
"name TEXT, "
|
2009-07-05 12:08:13 +00:00
|
|
|
"classmark1 NUMERIC, "
|
|
|
|
"classmark2 BLOB, "
|
|
|
|
"classmark3 BLOB, "
|
2009-03-30 20:56:32 +00:00
|
|
|
"imei NUMERIC UNIQUE NOT NULL"
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_EQUIPMENT_WATCH] = "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
|
2009-03-30 20:56:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"updated TIMESTAMP NOT NULL, "
|
|
|
|
"subscriber_id NUMERIC NOT NULL, "
|
|
|
|
"equipment_id NUMERIC NOT NULL, "
|
|
|
|
"UNIQUE (subscriber_id, equipment_id) "
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_SMS] = "CREATE TABLE IF NOT EXISTS SMS ("
|
2009-08-08 14:03:15 +00:00
|
|
|
/* metadata, not part of sms */
|
2009-03-30 20:56:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"sent TIMESTAMP, "
|
2009-08-15 18:15:14 +00:00
|
|
|
"deliver_attempts INTEGER NOT NULL DEFAULT 0, "
|
2009-08-08 14:03:15 +00:00
|
|
|
/* data directly copied/derived from SMS */
|
2009-07-27 18:11:35 +00:00
|
|
|
"valid_until TIMESTAMP, "
|
2009-08-08 14:03:15 +00:00
|
|
|
"reply_path_req INTEGER NOT NULL, "
|
|
|
|
"status_rep_req INTEGER NOT NULL, "
|
|
|
|
"protocol_id INTEGER NOT NULL, "
|
|
|
|
"data_coding_scheme INTEGER NOT NULL, "
|
2009-08-09 17:03:42 +00:00
|
|
|
"ud_hdr_ind INTEGER NOT NULL, "
|
2013-10-08 01:17:30 +00:00
|
|
|
"src_addr TEXT NOT NULL, "
|
|
|
|
"src_ton INTEGER NOT NULL, "
|
|
|
|
"src_npi INTEGER NOT NULL, "
|
|
|
|
"dest_addr TEXT NOT NULL, "
|
|
|
|
"dest_ton INTEGER NOT NULL, "
|
|
|
|
"dest_npi INTEGER NOT NULL, "
|
2009-08-08 14:03:15 +00:00
|
|
|
"user_data BLOB, " /* TP-UD */
|
|
|
|
/* additional data, interpreted from SMS */
|
|
|
|
"header BLOB, " /* UD Header */
|
|
|
|
"text TEXT " /* decoded UD after UDH */
|
2009-03-30 20:56:32 +00:00
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_VLR] = "CREATE TABLE IF NOT EXISTS VLR ("
|
2009-04-19 06:35:23 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"updated TIMESTAMP NOT NULL, "
|
|
|
|
"subscriber_id NUMERIC UNIQUE NOT NULL, "
|
|
|
|
"last_bts NUMERIC NOT NULL "
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_APDU] = "CREATE TABLE IF NOT EXISTS ApduBlobs ("
|
2009-08-16 08:40:10 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"created TIMESTAMP NOT NULL, "
|
|
|
|
"apdu_id_flags INTEGER NOT NULL, "
|
|
|
|
"subscriber_id INTEGER NOT NULL, "
|
|
|
|
"apdu BLOB "
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_COUNTERS] = "CREATE TABLE IF NOT EXISTS Counters ("
|
2009-12-22 18:07:32 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"timestamp TIMESTAMP NOT NULL, "
|
2009-12-22 20:40:42 +00:00
|
|
|
"value INTEGER NOT NULL, "
|
|
|
|
"name TEXT NOT NULL "
|
2009-12-24 08:42:07 +00:00
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_RATE] = "CREATE TABLE IF NOT EXISTS RateCounters ("
|
2010-05-13 10:55:20 +00:00
|
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
"timestamp TIMESTAMP NOT NULL, "
|
|
|
|
"value INTEGER NOT NULL, "
|
|
|
|
"name TEXT NOT NULL, "
|
2010-05-14 15:38:47 +00:00
|
|
|
"idx INTEGER NOT NULL "
|
2010-05-13 10:55:20 +00:00
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_AUTHKEY] = "CREATE TABLE IF NOT EXISTS AuthKeys ("
|
2010-06-09 09:31:32 +00:00
|
|
|
"subscriber_id INTEGER PRIMARY KEY, "
|
2009-12-27 18:26:12 +00:00
|
|
|
"algorithm_id INTEGER NOT NULL, "
|
2009-12-05 09:43:22 +00:00
|
|
|
"a3a8_ki BLOB "
|
|
|
|
")",
|
2014-03-07 17:17:22 +00:00
|
|
|
[SCHEMA_AUTHLAST] = "CREATE TABLE IF NOT EXISTS AuthLastTuples ("
|
2010-06-09 09:31:32 +00:00
|
|
|
"subscriber_id INTEGER PRIMARY KEY, "
|
2009-12-27 14:41:59 +00:00
|
|
|
"issued TIMESTAMP NOT NULL, "
|
|
|
|
"use_count INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"key_seq INTEGER NOT NULL, "
|
|
|
|
"rand BLOB NOT NULL, "
|
|
|
|
"sres BLOB NOT NULL, "
|
|
|
|
"kc BLOB NOT NULL "
|
2009-12-22 18:07:32 +00:00
|
|
|
")",
|
2009-03-30 20:56:32 +00:00
|
|
|
};
|
|
|
|
|
2009-12-24 10:21:42 +00:00
|
|
|
void db_error_func(dbi_conn conn, void *data)
|
|
|
|
{
|
|
|
|
const char *msg;
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_conn_error(conn, &msg);
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "DBI: %s\n", msg);
|
2014-07-10 18:18:15 +00:00
|
|
|
osmo_log_backtrace(DDB, LOGL_ERROR);
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 23:27:16 +00:00
|
|
|
static int update_db_revision_2(void)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"ALTER TABLE Subscriber "
|
|
|
|
"ADD COLUMN expire_lu "
|
|
|
|
"TIMESTAMP DEFAULT NULL");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
2014-03-07 15:59:53 +00:00
|
|
|
"Failed to alter table Subscriber (upgrade from rev 2).\n");
|
2012-12-26 23:27:16 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"UPDATE Meta "
|
|
|
|
"SET value = '3' "
|
|
|
|
"WHERE key = 'revision'");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
2014-03-07 17:17:22 +00:00
|
|
|
"Failed to update DB schema revision (upgrade from rev 2).\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copied from the normal sms_from_result_v3 to avoid having
|
|
|
|
* to make sure that the real routine will remain backward
|
|
|
|
* compatible.
|
|
|
|
*/
|
|
|
|
static struct gsm_sms *sms_from_result_v3(dbi_result result)
|
|
|
|
{
|
|
|
|
struct gsm_sms *sms = sms_alloc();
|
|
|
|
long long unsigned int sender_id;
|
|
|
|
struct gsm_subscriber *sender;
|
|
|
|
const char *text, *daddr;
|
|
|
|
const unsigned char *user_data;
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
if (!sms)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sms->id = dbi_result_get_ulonglong(result, "id");
|
|
|
|
|
|
|
|
sender_id = dbi_result_get_ulonglong(result, "sender_id");
|
|
|
|
snprintf(buf, sizeof(buf), "%llu", sender_id);
|
|
|
|
sender = db_get_subscriber(GSM_SUBSCRIBER_ID, buf);
|
|
|
|
OSMO_ASSERT(sender);
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(sms->src.addr, sender->extension, sizeof(sms->src.addr));
|
2014-03-07 17:17:22 +00:00
|
|
|
subscr_direct_free(sender);
|
|
|
|
sender = NULL;
|
|
|
|
|
2014-07-03 12:00:30 +00:00
|
|
|
sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
|
|
|
|
sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
|
|
|
|
sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
|
|
|
|
sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
|
|
|
|
sms->data_coding_scheme = dbi_result_get_ulonglong(result,
|
2014-03-07 17:17:22 +00:00
|
|
|
"data_coding_scheme");
|
|
|
|
|
|
|
|
daddr = dbi_result_get_string(result, "dest_addr");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (daddr)
|
|
|
|
osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
|
2014-03-07 17:17:22 +00:00
|
|
|
|
|
|
|
sms->user_data_len = dbi_result_get_field_length(result, "user_data");
|
|
|
|
user_data = dbi_result_get_binary(result, "user_data");
|
|
|
|
if (sms->user_data_len > sizeof(sms->user_data))
|
|
|
|
sms->user_data_len = (uint8_t) sizeof(sms->user_data);
|
|
|
|
memcpy(sms->user_data, user_data, sms->user_data_len);
|
|
|
|
|
|
|
|
text = dbi_result_get_string(result, "text");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (text)
|
|
|
|
osmo_strlcpy(sms->text, text, sizeof(sms->text));
|
2014-03-07 17:17:22 +00:00
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_db_revision_3(void)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_sms *sms;
|
|
|
|
|
2014-03-08 15:41:37 +00:00
|
|
|
LOGP(DDB, LOGL_NOTICE, "Going to migrate from revision 3\n");
|
|
|
|
|
2014-03-07 17:17:22 +00:00
|
|
|
result = dbi_conn_query(conn, "BEGIN EXCLUSIVE TRANSACTION");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to begin transaction (upgrade from rev 3)\n");
|
2012-12-26 23:27:16 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
2014-03-07 17:17:22 +00:00
|
|
|
/* Rename old SMS table to be able create a new one */
|
|
|
|
result = dbi_conn_query(conn, "ALTER TABLE SMS RENAME TO SMS_3");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to rename the old SMS table (upgrade from rev 3).\n");
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
/* Create new SMS table with all the bells and whistles! */
|
|
|
|
result = dbi_conn_query(conn, create_stmts[SCHEMA_SMS]);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to create a new SMS table (upgrade from rev 3).\n");
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
/* Cycle through old messages and convert them to the new format */
|
2015-07-29 18:20:28 +00:00
|
|
|
result = dbi_conn_query(conn, "SELECT * FROM SMS_3");
|
2014-03-07 17:17:22 +00:00
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed fetch messages from the old SMS table (upgrade from rev 3).\n");
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
while (dbi_result_next_row(result)) {
|
|
|
|
sms = sms_from_result_v3(result);
|
|
|
|
if (db_sms_store(sms) != 0) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to store message to the new SMS table(upgrade from rev 3).\n");
|
|
|
|
sms_free(sms);
|
|
|
|
dbi_result_free(result);
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
sms_free(sms);
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
/* Remove the temporary table */
|
|
|
|
result = dbi_conn_query(conn, "DROP TABLE SMS_3");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to drop the old SMS table (upgrade from rev 3).\n");
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
/* We're done. Bump DB Meta revision to 4 */
|
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"UPDATE Meta "
|
|
|
|
"SET value = '4' "
|
|
|
|
"WHERE key = 'revision'");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to update DB schema revision (upgrade from rev 3).\n");
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_query(conn, "COMMIT TRANSACTION");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to commit the transaction (upgrade from rev 3)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shrink DB file size by actually wiping out SMS_3 table data */
|
|
|
|
result = dbi_conn_query(conn, "VACUUM");
|
|
|
|
if (!result)
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"VACUUM failed. Ignoring it (upgrade from rev 3).\n");
|
|
|
|
else
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
2012-12-26 23:27:16 +00:00
|
|
|
return 0;
|
2014-03-07 17:17:22 +00:00
|
|
|
|
|
|
|
rollback:
|
|
|
|
result = dbi_conn_query(conn, "ROLLBACK TRANSACTION");
|
|
|
|
if (!result)
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Rollback failed (upgrade from rev 3).\n");
|
|
|
|
else
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -EINVAL;
|
2012-12-26 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 17:03:42 +00:00
|
|
|
static int check_db_revision(void)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
2012-12-26 23:27:16 +00:00
|
|
|
const char *rev_s;
|
2016-05-13 09:38:09 +00:00
|
|
|
int db_rev = 0;
|
2009-08-09 17:03:42 +00:00
|
|
|
|
2016-05-13 09:38:09 +00:00
|
|
|
/* Make a query */
|
2009-08-09 17:03:42 +00:00
|
|
|
result = dbi_conn_query(conn,
|
2016-05-13 09:38:09 +00:00
|
|
|
"SELECT value FROM Meta "
|
|
|
|
"WHERE key = 'revision'");
|
|
|
|
|
2009-08-09 17:03:42 +00:00
|
|
|
if (!result)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-05-13 09:38:09 +00:00
|
|
|
|
|
|
|
/* Fetch the DB schema revision */
|
2012-12-26 23:27:16 +00:00
|
|
|
rev_s = dbi_result_get_string(result, "value");
|
|
|
|
if (!rev_s) {
|
2009-08-09 17:03:42 +00:00
|
|
|
dbi_result_free(result);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-05-13 09:38:09 +00:00
|
|
|
|
|
|
|
if (!strcmp(rev_s, SCHEMA_REVISION)) {
|
|
|
|
/* Everything is fine */
|
2012-12-26 23:27:16 +00:00
|
|
|
dbi_result_free(result);
|
2016-05-13 09:38:09 +00:00
|
|
|
return 0;
|
2012-12-26 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 09:38:09 +00:00
|
|
|
db_rev = atoi(rev_s);
|
2012-12-26 23:27:16 +00:00
|
|
|
dbi_result_free(result);
|
2016-05-13 09:38:09 +00:00
|
|
|
|
|
|
|
/* Incremental migration waterfall */
|
|
|
|
switch (db_rev) {
|
|
|
|
case 2:
|
|
|
|
if (update_db_revision_2())
|
|
|
|
goto error;
|
|
|
|
case 3:
|
|
|
|
if (update_db_revision_3())
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* The end of waterfall */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGP(DDB, LOGL_FATAL,
|
|
|
|
"Invalid database schema revision '%d'.\n", db_rev);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-12-26 23:27:16 +00:00
|
|
|
return 0;
|
2016-05-13 09:38:09 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
LOGP(DDB, LOGL_FATAL, "Failed to update database "
|
|
|
|
"from schema revision '%d'.\n", db_rev);
|
|
|
|
return -EINVAL;
|
2012-12-26 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int db_configure(void)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"PRAGMA synchronous = FULL");
|
|
|
|
if (!result)
|
|
|
|
return -EINVAL;
|
2009-08-09 17:03:42 +00:00
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-24 10:21:42 +00:00
|
|
|
int db_init(const char *name)
|
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_initialize(NULL);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
conn = dbi_conn_new("sqlite3");
|
2009-12-24 10:21:42 +00:00
|
|
|
if (conn == NULL) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_FATAL, "Failed to create connection.\n");
|
2008-12-27 15:59:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-01 18:02:05 +00:00
|
|
|
dbi_conn_error_handler( conn, db_error_func, NULL );
|
2008-12-27 15:59:25 +00:00
|
|
|
|
|
|
|
/* MySQL
|
|
|
|
dbi_conn_set_option(conn, "host", "localhost");
|
|
|
|
dbi_conn_set_option(conn, "username", "your_name");
|
|
|
|
dbi_conn_set_option(conn, "password", "your_password");
|
|
|
|
dbi_conn_set_option(conn, "dbname", "your_dbname");
|
|
|
|
dbi_conn_set_option(conn, "encoding", "UTF-8");
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* SqLite 3 */
|
2009-01-01 18:02:05 +00:00
|
|
|
db_basename = strdup(name);
|
|
|
|
db_dirname = strdup(name);
|
2008-12-28 22:51:39 +00:00
|
|
|
dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
|
|
|
|
dbi_conn_set_option(conn, "dbname", basename(db_basename));
|
2008-12-27 15:59:25 +00:00
|
|
|
|
2009-08-09 17:03:42 +00:00
|
|
|
if (dbi_conn_connect(conn) < 0)
|
|
|
|
goto out_err;
|
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
return 0;
|
2009-08-09 17:03:42 +00:00
|
|
|
|
|
|
|
out_err:
|
|
|
|
free(db_dirname);
|
|
|
|
free(db_basename);
|
|
|
|
db_dirname = db_basename = NULL;
|
|
|
|
return -1;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 17:03:42 +00:00
|
|
|
|
2011-07-16 11:24:09 +00:00
|
|
|
int db_prepare(void)
|
2009-12-24 10:21:42 +00:00
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result result;
|
2009-03-30 20:56:32 +00:00
|
|
|
int i;
|
2009-02-23 00:50:31 +00:00
|
|
|
|
2009-03-30 20:56:32 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
|
|
|
|
result = dbi_conn_query(conn, create_stmts[i]);
|
2009-12-24 10:21:42 +00:00
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to create some table.\n");
|
2009-03-30 20:56:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
2009-02-23 00:50:31 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 06:36:04 +00:00
|
|
|
if (check_db_revision() < 0) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_FATAL, "Database schema revision invalid, "
|
2009-08-10 06:36:04 +00:00
|
|
|
"please update your database schema\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-26 23:27:16 +00:00
|
|
|
db_configure();
|
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
return 0;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2011-07-16 11:24:09 +00:00
|
|
|
int db_fini(void)
|
2009-12-24 10:21:42 +00:00
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_conn_close(conn);
|
|
|
|
dbi_shutdown();
|
2008-12-28 22:51:39 +00:00
|
|
|
|
2011-07-16 11:22:57 +00:00
|
|
|
free(db_dirname);
|
|
|
|
free(db_basename);
|
2008-12-27 15:59:25 +00:00
|
|
|
return 0;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 13:32:16 +00:00
|
|
|
struct gsm_subscriber *db_create_subscriber(const char *imsi, uint64_t smin,
|
2016-06-30 08:25:49 +00:00
|
|
|
uint64_t smax, bool alloc_exten)
|
2009-07-23 16:46:00 +00:00
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result result;
|
2009-12-24 10:21:42 +00:00
|
|
|
struct gsm_subscriber *subscr;
|
2009-01-01 18:02:05 +00:00
|
|
|
|
|
|
|
/* Is this subscriber known in the db? */
|
2013-10-04 06:35:11 +00:00
|
|
|
subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
|
2009-01-01 18:02:05 +00:00
|
|
|
if (subscr) {
|
2016-04-01 18:21:03 +00:00
|
|
|
subscr_put(subscr);
|
|
|
|
return NULL;
|
2008-12-27 15:59:25 +00:00
|
|
|
}
|
2009-01-01 18:02:05 +00:00
|
|
|
|
|
|
|
subscr = subscr_alloc();
|
|
|
|
if (!subscr)
|
|
|
|
return NULL;
|
2011-07-16 11:22:57 +00:00
|
|
|
subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
|
2008-12-27 15:59:25 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
2008-12-27 22:33:34 +00:00
|
|
|
"INSERT INTO Subscriber "
|
2008-12-27 18:08:13 +00:00
|
|
|
"(imsi, created, updated) "
|
2008-12-27 15:59:25 +00:00
|
|
|
"VALUES "
|
2008-12-27 18:08:13 +00:00
|
|
|
"(%s, datetime('now'), datetime('now')) ",
|
2008-12-27 15:59:25 +00:00
|
|
|
imsi
|
|
|
|
);
|
2016-04-01 18:31:11 +00:00
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to create Subscriber by IMSI.\n");
|
2016-04-01 18:31:11 +00:00
|
|
|
subscr_put(subscr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-01-01 18:02:05 +00:00
|
|
|
subscr->id = dbi_conn_sequence_last(conn, NULL);
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(subscr->imsi, imsi, sizeof(subscr->imsi));
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result_free(result);
|
2009-12-26 17:57:32 +00:00
|
|
|
LOGP(DDB, LOGL_INFO, "New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
|
2016-06-30 08:25:49 +00:00
|
|
|
if (alloc_exten)
|
|
|
|
db_subscriber_alloc_exten(subscr, smin, smax);
|
2009-01-01 18:02:05 +00:00
|
|
|
return subscr;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-07 10:12:48 +00:00
|
|
|
osmo_static_assert(sizeof(unsigned char) == sizeof(struct gsm48_classmark1), classmark1_size);
|
2010-03-30 13:28:36 +00:00
|
|
|
|
2009-08-17 07:39:55 +00:00
|
|
|
static int get_equipment_by_subscr(struct gsm_subscriber *subscr)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
2009-09-26 16:54:59 +00:00
|
|
|
const char *string;
|
2009-12-09 18:19:45 +00:00
|
|
|
unsigned char cm1;
|
2009-08-17 07:39:55 +00:00
|
|
|
const unsigned char *cm2, *cm3;
|
|
|
|
struct gsm_equipment *equip = &subscr->equipment;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 20:00:45 +00:00
|
|
|
"SELECT Equipment.* "
|
|
|
|
"FROM Equipment JOIN EquipmentWatch ON "
|
|
|
|
"EquipmentWatch.equipment_id=Equipment.id "
|
|
|
|
"WHERE EquipmentWatch.subscriber_id = %llu "
|
|
|
|
"ORDER BY EquipmentWatch.updated DESC", subscr->id);
|
2009-08-17 07:39:55 +00:00
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
equip->id = dbi_result_get_ulonglong(result, "id");
|
|
|
|
|
|
|
|
string = dbi_result_get_string(result, "imei");
|
|
|
|
if (string)
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(equip->imei, string, sizeof(equip->imei));
|
2009-08-17 07:39:55 +00:00
|
|
|
|
2009-12-26 17:56:00 +00:00
|
|
|
string = dbi_result_get_string(result, "classmark1");
|
2010-03-30 13:28:36 +00:00
|
|
|
if (string) {
|
|
|
|
cm1 = atoi(string) & 0xff;
|
|
|
|
memcpy(&equip->classmark1, &cm1, sizeof(equip->classmark1));
|
|
|
|
}
|
2009-08-17 07:39:55 +00:00
|
|
|
|
|
|
|
equip->classmark2_len = dbi_result_get_field_length(result, "classmark2");
|
|
|
|
cm2 = dbi_result_get_binary(result, "classmark2");
|
|
|
|
if (equip->classmark2_len > sizeof(equip->classmark2))
|
|
|
|
equip->classmark2_len = sizeof(equip->classmark2);
|
2016-01-23 08:21:04 +00:00
|
|
|
if (cm2)
|
|
|
|
memcpy(equip->classmark2, cm2, equip->classmark2_len);
|
2009-08-17 07:39:55 +00:00
|
|
|
|
|
|
|
equip->classmark3_len = dbi_result_get_field_length(result, "classmark3");
|
|
|
|
cm3 = dbi_result_get_binary(result, "classmark3");
|
|
|
|
if (equip->classmark3_len > sizeof(equip->classmark3))
|
|
|
|
equip->classmark3_len = sizeof(equip->classmark3);
|
2016-01-23 08:21:04 +00:00
|
|
|
if (cm3)
|
|
|
|
memcpy(equip->classmark3, cm3, equip->classmark3_len);
|
2009-08-17 07:39:55 +00:00
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-05 09:43:22 +00:00
|
|
|
|
2010-06-09 09:32:51 +00:00
|
|
|
int db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo,
|
|
|
|
struct gsm_subscriber *subscr)
|
2009-12-05 09:43:22 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
const unsigned char *a3a8_ki;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 13:38:35 +00:00
|
|
|
"SELECT * FROM AuthKeys WHERE subscriber_id=%llu",
|
2009-12-05 09:43:22 +00:00
|
|
|
subscr->id);
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ainfo->auth_algo = dbi_result_get_ulonglong(result, "algorithm_id");
|
|
|
|
ainfo->a3a8_ki_len = dbi_result_get_field_length(result, "a3a8_ki");
|
|
|
|
a3a8_ki = dbi_result_get_binary(result, "a3a8_ki");
|
2009-12-27 18:24:05 +00:00
|
|
|
if (ainfo->a3a8_ki_len > sizeof(ainfo->a3a8_ki))
|
2012-05-07 20:09:15 +00:00
|
|
|
ainfo->a3a8_ki_len = sizeof(ainfo->a3a8_ki);
|
2009-12-05 09:43:22 +00:00
|
|
|
memcpy(ainfo->a3a8_ki, a3a8_ki, ainfo->a3a8_ki_len);
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-09 09:32:51 +00:00
|
|
|
int db_sync_authinfo_for_subscr(struct gsm_auth_info *ainfo,
|
|
|
|
struct gsm_subscriber *subscr)
|
2009-12-27 18:27:53 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_auth_info ainfo_old;
|
|
|
|
int rc, upd;
|
|
|
|
unsigned char *ki_str;
|
|
|
|
|
|
|
|
/* Deletion ? */
|
|
|
|
if (ainfo == NULL) {
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 13:38:35 +00:00
|
|
|
"DELETE FROM AuthKeys WHERE subscriber_id=%llu",
|
2009-12-27 18:27:53 +00:00
|
|
|
subscr->id);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if already existing */
|
2010-06-09 09:32:51 +00:00
|
|
|
rc = db_get_authinfo_for_subscr(&ainfo_old, subscr);
|
2009-12-27 18:27:53 +00:00
|
|
|
if (rc && rc != -ENOENT)
|
|
|
|
return rc;
|
|
|
|
upd = rc ? 0 : 1;
|
|
|
|
|
|
|
|
/* Update / Insert */
|
|
|
|
dbi_conn_quote_binary_copy(conn,
|
|
|
|
ainfo->a3a8_ki, ainfo->a3a8_ki_len, &ki_str);
|
|
|
|
|
|
|
|
if (!upd) {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT INTO AuthKeys "
|
|
|
|
"(subscriber_id, algorithm_id, a3a8_ki) "
|
2010-07-03 13:38:35 +00:00
|
|
|
"VALUES (%llu, %u, %s)",
|
2009-12-27 18:27:53 +00:00
|
|
|
subscr->id, ainfo->auth_algo, ki_str);
|
|
|
|
} else {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE AuthKeys "
|
|
|
|
"SET algorithm_id=%u, a3a8_ki=%s "
|
2010-07-03 13:38:35 +00:00
|
|
|
"WHERE subscriber_id=%llu",
|
2009-12-27 18:27:53 +00:00
|
|
|
ainfo->auth_algo, ki_str, subscr->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ki_str);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-09 09:32:51 +00:00
|
|
|
int db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
|
|
|
|
struct gsm_subscriber *subscr)
|
2009-12-05 09:43:22 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
int len;
|
|
|
|
const unsigned char *blob;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 13:38:35 +00:00
|
|
|
"SELECT * FROM AuthLastTuples WHERE subscriber_id=%llu",
|
2009-12-05 09:43:22 +00:00
|
|
|
subscr->id);
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:24:02 +00:00
|
|
|
memset(atuple, 0, sizeof(*atuple));
|
2009-12-05 09:43:22 +00:00
|
|
|
|
2009-12-27 14:41:59 +00:00
|
|
|
atuple->use_count = dbi_result_get_ulonglong(result, "use_count");
|
|
|
|
atuple->key_seq = dbi_result_get_ulonglong(result, "key_seq");
|
|
|
|
|
2009-12-05 09:43:22 +00:00
|
|
|
len = dbi_result_get_field_length(result, "rand");
|
2016-04-20 11:13:19 +00:00
|
|
|
if (len != sizeof(atuple->vec.rand))
|
2009-12-05 09:43:22 +00:00
|
|
|
goto err_size;
|
|
|
|
|
|
|
|
blob = dbi_result_get_binary(result, "rand");
|
2016-04-20 11:13:19 +00:00
|
|
|
memcpy(atuple->vec.rand, blob, len);
|
2009-12-05 09:43:22 +00:00
|
|
|
|
|
|
|
len = dbi_result_get_field_length(result, "sres");
|
2016-04-20 11:13:19 +00:00
|
|
|
if (len != sizeof(atuple->vec.sres))
|
2009-12-05 09:43:22 +00:00
|
|
|
goto err_size;
|
|
|
|
|
|
|
|
blob = dbi_result_get_binary(result, "sres");
|
2016-04-20 11:13:19 +00:00
|
|
|
memcpy(atuple->vec.sres, blob, len);
|
2009-12-05 09:43:22 +00:00
|
|
|
|
|
|
|
len = dbi_result_get_field_length(result, "kc");
|
2016-04-20 11:13:19 +00:00
|
|
|
if (len != sizeof(atuple->vec.kc))
|
2009-12-05 09:43:22 +00:00
|
|
|
goto err_size;
|
|
|
|
|
|
|
|
blob = dbi_result_get_binary(result, "kc");
|
2016-04-20 11:13:19 +00:00
|
|
|
memcpy(atuple->vec.kc, blob, len);
|
2009-12-05 09:43:22 +00:00
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_size:
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2010-06-09 09:32:51 +00:00
|
|
|
int db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple,
|
|
|
|
struct gsm_subscriber *subscr)
|
2009-12-27 18:27:53 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
int rc, upd;
|
|
|
|
struct gsm_auth_tuple atuple_old;
|
|
|
|
unsigned char *rand_str, *sres_str, *kc_str;
|
|
|
|
|
|
|
|
/* Deletion ? */
|
|
|
|
if (atuple == NULL) {
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 13:38:35 +00:00
|
|
|
"DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
|
2009-12-27 18:27:53 +00:00
|
|
|
subscr->id);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if already existing */
|
2010-06-09 09:32:51 +00:00
|
|
|
rc = db_get_lastauthtuple_for_subscr(&atuple_old, subscr);
|
2009-12-27 18:27:53 +00:00
|
|
|
if (rc && rc != -ENOENT)
|
|
|
|
return rc;
|
|
|
|
upd = rc ? 0 : 1;
|
|
|
|
|
|
|
|
/* Update / Insert */
|
|
|
|
dbi_conn_quote_binary_copy(conn,
|
2016-04-20 11:13:19 +00:00
|
|
|
atuple->vec.rand, sizeof(atuple->vec.rand), &rand_str);
|
2009-12-27 18:27:53 +00:00
|
|
|
dbi_conn_quote_binary_copy(conn,
|
2016-04-20 11:13:19 +00:00
|
|
|
atuple->vec.sres, sizeof(atuple->vec.sres), &sres_str);
|
2009-12-27 18:27:53 +00:00
|
|
|
dbi_conn_quote_binary_copy(conn,
|
2016-04-20 11:13:19 +00:00
|
|
|
atuple->vec.kc, sizeof(atuple->vec.kc), &kc_str);
|
2009-12-27 18:27:53 +00:00
|
|
|
|
|
|
|
if (!upd) {
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-06-09 11:03:39 +00:00
|
|
|
"INSERT INTO AuthLastTuples "
|
2009-12-27 18:27:53 +00:00
|
|
|
"(subscriber_id, issued, use_count, "
|
|
|
|
"key_seq, rand, sres, kc) "
|
2010-07-03 13:38:35 +00:00
|
|
|
"VALUES (%llu, datetime('now'), %u, "
|
2009-12-27 18:27:53 +00:00
|
|
|
"%u, %s, %s, %s ) ",
|
|
|
|
subscr->id, atuple->use_count, atuple->key_seq,
|
|
|
|
rand_str, sres_str, kc_str);
|
|
|
|
} else {
|
|
|
|
char *issued = atuple->key_seq == atuple_old.key_seq ?
|
|
|
|
"issued" : "datetime('now')";
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-06-10 20:26:21 +00:00
|
|
|
"UPDATE AuthLastTuples "
|
2009-12-27 18:27:53 +00:00
|
|
|
"SET issued=%s, use_count=%u, "
|
|
|
|
"key_seq=%u, rand=%s, sres=%s, kc=%s "
|
2010-07-03 13:38:35 +00:00
|
|
|
"WHERE subscriber_id = %llu",
|
2009-12-27 18:27:53 +00:00
|
|
|
issued, atuple->use_count, atuple->key_seq,
|
|
|
|
rand_str, sres_str, kc_str, subscr->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(rand_str);
|
|
|
|
free(sres_str);
|
|
|
|
free(kc_str);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-22 17:12:11 +00:00
|
|
|
static void db_set_from_query(struct gsm_subscriber *subscr, dbi_conn result)
|
|
|
|
{
|
|
|
|
const char *string;
|
|
|
|
string = dbi_result_get_string(result, "imsi");
|
|
|
|
if (string)
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(subscr->imsi, string, sizeof(subscr->imsi));
|
2010-12-22 17:12:11 +00:00
|
|
|
|
|
|
|
string = dbi_result_get_string(result, "tmsi");
|
|
|
|
if (string)
|
|
|
|
subscr->tmsi = tmsi_from_string(string);
|
|
|
|
|
|
|
|
string = dbi_result_get_string(result, "name");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (string)
|
|
|
|
osmo_strlcpy(subscr->name, string, sizeof(subscr->name));
|
2010-12-22 17:12:11 +00:00
|
|
|
|
|
|
|
string = dbi_result_get_string(result, "extension");
|
|
|
|
if (string)
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(subscr->extension, string, sizeof(subscr->extension));
|
2010-12-22 17:12:11 +00:00
|
|
|
|
2013-11-04 21:43:15 +00:00
|
|
|
subscr->lac = dbi_result_get_ulonglong(result, "lac");
|
2012-12-26 23:27:16 +00:00
|
|
|
|
|
|
|
if (!dbi_result_field_is_null(result, "expire_lu"))
|
|
|
|
subscr->expire_lu = dbi_result_get_datetime(result, "expire_lu");
|
|
|
|
else
|
2013-07-27 19:07:57 +00:00
|
|
|
subscr->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
|
2012-12-26 23:27:16 +00:00
|
|
|
|
2013-11-04 21:43:15 +00:00
|
|
|
subscr->authorized = dbi_result_get_ulonglong(result, "authorized");
|
|
|
|
|
2010-12-22 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
2009-08-17 07:39:55 +00:00
|
|
|
#define BASE_QUERY "SELECT * FROM Subscriber "
|
2013-10-04 06:35:11 +00:00
|
|
|
struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field,
|
2009-07-23 16:46:00 +00:00
|
|
|
const char *id)
|
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result result;
|
2008-12-27 22:33:34 +00:00
|
|
|
char *quoted;
|
2009-01-01 18:02:05 +00:00
|
|
|
struct gsm_subscriber *subscr;
|
2008-12-27 21:34:06 +00:00
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
switch (field) {
|
|
|
|
case GSM_SUBSCRIBER_IMSI:
|
2009-01-01 18:02:05 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, id, "ed);
|
2008-12-27 15:59:25 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
2009-08-17 07:39:55 +00:00
|
|
|
BASE_QUERY
|
2008-12-27 15:59:25 +00:00
|
|
|
"WHERE imsi = %s ",
|
2008-12-27 22:33:34 +00:00
|
|
|
quoted
|
2008-12-27 15:59:25 +00:00
|
|
|
);
|
2009-01-01 18:02:05 +00:00
|
|
|
free(quoted);
|
2008-12-27 15:59:25 +00:00
|
|
|
break;
|
|
|
|
case GSM_SUBSCRIBER_TMSI:
|
2009-01-01 18:02:05 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, id, "ed);
|
2008-12-27 15:59:25 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
2009-08-17 07:39:55 +00:00
|
|
|
BASE_QUERY
|
2008-12-27 15:59:25 +00:00
|
|
|
"WHERE tmsi = %s ",
|
2008-12-27 22:33:34 +00:00
|
|
|
quoted
|
2008-12-27 15:59:25 +00:00
|
|
|
);
|
2009-01-01 18:02:05 +00:00
|
|
|
free(quoted);
|
2008-12-27 15:59:25 +00:00
|
|
|
break;
|
2009-02-09 23:39:20 +00:00
|
|
|
case GSM_SUBSCRIBER_EXTENSION:
|
|
|
|
dbi_conn_quote_string_copy(conn, id, "ed);
|
|
|
|
result = dbi_conn_queryf(conn,
|
2009-08-17 07:39:55 +00:00
|
|
|
BASE_QUERY
|
2009-02-09 23:39:20 +00:00
|
|
|
"WHERE extension = %s ",
|
|
|
|
quoted
|
|
|
|
);
|
|
|
|
free(quoted);
|
|
|
|
break;
|
2009-07-05 12:06:41 +00:00
|
|
|
case GSM_SUBSCRIBER_ID:
|
|
|
|
dbi_conn_quote_string_copy(conn, id, "ed);
|
|
|
|
result = dbi_conn_queryf(conn,
|
2009-08-17 07:39:55 +00:00
|
|
|
BASE_QUERY
|
2009-07-05 12:06:41 +00:00
|
|
|
"WHERE id = %s ", quoted);
|
|
|
|
free(quoted);
|
|
|
|
break;
|
2008-12-27 15:59:25 +00:00
|
|
|
default:
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_NOTICE, "Unknown query selector for Subscriber.\n");
|
2009-01-01 18:02:05 +00:00
|
|
|
return NULL;
|
2008-12-27 15:59:25 +00:00
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber.\n");
|
2009-01-01 18:02:05 +00:00
|
|
|
return NULL;
|
2008-12-27 15:59:25 +00:00
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Failed to find the Subscriber. '%u' '%s'\n",
|
2009-02-22 20:33:09 +00:00
|
|
|
field, id);
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result_free(result);
|
2009-01-01 18:02:05 +00:00
|
|
|
return NULL;
|
2008-12-27 15:59:25 +00:00
|
|
|
}
|
2009-01-01 18:02:05 +00:00
|
|
|
|
|
|
|
subscr = subscr_alloc();
|
|
|
|
subscr->id = dbi_result_get_ulonglong(result, "id");
|
2008-12-27 22:33:34 +00:00
|
|
|
|
2010-12-22 17:12:11 +00:00
|
|
|
db_set_from_query(subscr, result);
|
2016-05-09 19:28:05 +00:00
|
|
|
DEBUGP(DDB, "Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %x, EXTEN '%s', LAC %hu, AUTH %u\n",
|
2016-03-17 13:41:26 +00:00
|
|
|
subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
|
|
|
|
subscr->lac, subscr->authorized);
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result_free(result);
|
2009-08-17 07:39:55 +00:00
|
|
|
|
|
|
|
get_equipment_by_subscr(subscr);
|
|
|
|
|
2009-01-01 18:02:05 +00:00
|
|
|
return subscr;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 17:12:11 +00:00
|
|
|
int db_subscriber_update(struct gsm_subscriber *subscr)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
/* Copy the id to a string as queryf with %llu is failing */
|
|
|
|
sprintf(buf, "%llu", subscr->id);
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
BASE_QUERY
|
|
|
|
"WHERE id = %s", buf);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber: %llu\n", subscr->id);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
DEBUGP(DDB, "Failed to find the Subscriber. %llu\n",
|
|
|
|
subscr->id);
|
|
|
|
dbi_result_free(result);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
db_set_from_query(subscr, result);
|
|
|
|
dbi_result_free(result);
|
|
|
|
get_equipment_by_subscr(subscr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-24 10:21:42 +00:00
|
|
|
int db_sync_subscriber(struct gsm_subscriber *subscriber)
|
|
|
|
{
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result result;
|
2016-03-17 13:41:26 +00:00
|
|
|
char tmsi[14];
|
2010-12-26 18:12:30 +00:00
|
|
|
char *q_tmsi, *q_name, *q_extension;
|
2009-08-19 10:53:57 +00:00
|
|
|
|
2010-12-26 18:12:30 +00:00
|
|
|
dbi_conn_quote_string_copy(conn,
|
|
|
|
subscriber->name, &q_name);
|
2016-06-30 08:25:49 +00:00
|
|
|
if (subscriber->extension[0] != '\0')
|
|
|
|
dbi_conn_quote_string_copy(conn,
|
|
|
|
subscriber->extension, &q_extension);
|
|
|
|
else
|
|
|
|
q_extension = strdup("NULL");
|
2010-12-26 18:12:30 +00:00
|
|
|
|
2009-08-19 10:53:57 +00:00
|
|
|
if (subscriber->tmsi != GSM_RESERVED_TMSI) {
|
2016-03-17 13:41:26 +00:00
|
|
|
sprintf(tmsi, "%u", subscriber->tmsi);
|
2009-08-12 19:04:54 +00:00
|
|
|
dbi_conn_quote_string_copy(conn,
|
2009-08-19 10:53:57 +00:00
|
|
|
tmsi,
|
2009-08-12 19:04:54 +00:00
|
|
|
&q_tmsi);
|
2009-08-19 10:53:57 +00:00
|
|
|
} else
|
2009-08-12 19:04:54 +00:00
|
|
|
q_tmsi = strdup("NULL");
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2013-07-27 19:07:57 +00:00
|
|
|
if (subscriber->expire_lu == GSM_SUBSCRIBER_NO_EXPIRATION) {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE Subscriber "
|
|
|
|
"SET updated = datetime('now'), "
|
|
|
|
"name = %s, "
|
|
|
|
"extension = %s, "
|
|
|
|
"authorized = %i, "
|
|
|
|
"tmsi = %s, "
|
|
|
|
"lac = %i, "
|
|
|
|
"expire_lu = NULL "
|
|
|
|
"WHERE imsi = %s ",
|
|
|
|
q_name,
|
|
|
|
q_extension,
|
|
|
|
subscriber->authorized,
|
|
|
|
q_tmsi,
|
|
|
|
subscriber->lac,
|
|
|
|
subscriber->imsi);
|
|
|
|
} else {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE Subscriber "
|
|
|
|
"SET updated = datetime('now'), "
|
|
|
|
"name = %s, "
|
|
|
|
"extension = %s, "
|
|
|
|
"authorized = %i, "
|
|
|
|
"tmsi = %s, "
|
|
|
|
"lac = %i, "
|
|
|
|
"expire_lu = datetime(%i, 'unixepoch') "
|
|
|
|
"WHERE imsi = %s ",
|
|
|
|
q_name,
|
|
|
|
q_extension,
|
|
|
|
subscriber->authorized,
|
|
|
|
q_tmsi,
|
|
|
|
subscriber->lac,
|
|
|
|
(int) subscriber->expire_lu,
|
|
|
|
subscriber->imsi);
|
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2009-08-12 19:04:54 +00:00
|
|
|
free(q_tmsi);
|
2010-12-26 18:12:30 +00:00
|
|
|
free(q_name);
|
|
|
|
free(q_extension);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to update Subscriber (by IMSI).\n");
|
2008-12-27 15:59:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result_free(result);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
return 0;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 13:01:08 +00:00
|
|
|
int db_subscriber_delete(struct gsm_subscriber *subscr)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM AuthKeys WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete Authkeys for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM AuthLastTuples WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete AuthLastTuples for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM AuthToken WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete AuthToken for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM EquipmentWatch WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete EquipmentWatch for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
2016-06-30 08:25:49 +00:00
|
|
|
if (subscr->extension[0] != '\0') {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM SMS WHERE src_addr=%s OR dest_addr=%s",
|
|
|
|
subscr->extension, subscr->extension);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete SMS for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
2014-03-23 13:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM VLR WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete VLR for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM ApduBlobs WHERE subscriber_id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete ApduBlobs for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"DELETE FROM Subscriber WHERE id=%llu",
|
|
|
|
subscr->id);
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR,
|
|
|
|
"Failed to delete Subscriber for %llu\n", subscr->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-23 15:22:55 +00:00
|
|
|
/**
|
|
|
|
* List all the authorized and non-expired subscribers. The callback will
|
|
|
|
* be called one by one. The subscr argument is not fully initialize and
|
|
|
|
* subscr_get/subscr_put must not be called. The passed in pointer will be
|
|
|
|
* deleted after the callback by the database call.
|
|
|
|
*/
|
|
|
|
int db_subscriber_list_active(void (*cb)(struct gsm_subscriber*,void*), void *closure)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
2015-07-29 18:20:28 +00:00
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"SELECT * from Subscriber WHERE LAC != 0 AND authorized = 1");
|
2014-03-23 15:22:55 +00:00
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to list active subscribers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (dbi_result_next_row(result)) {
|
|
|
|
struct gsm_subscriber *subscr;
|
|
|
|
|
|
|
|
subscr = subscr_alloc();
|
|
|
|
subscr->id = dbi_result_get_ulonglong(result, "id");
|
|
|
|
db_set_from_query(subscr, result);
|
|
|
|
cb(subscr, closure);
|
|
|
|
OSMO_ASSERT(subscr->use_count == 1);
|
|
|
|
llist_del(&subscr->entry);
|
|
|
|
talloc_free(subscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-05 12:08:13 +00:00
|
|
|
int db_sync_equipment(struct gsm_equipment *equip)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
unsigned char *cm2, *cm3;
|
2010-12-26 19:04:49 +00:00
|
|
|
char *q_imei;
|
2011-04-18 15:04:00 +00:00
|
|
|
uint8_t classmark1;
|
2009-07-05 12:08:13 +00:00
|
|
|
|
2009-10-22 13:34:37 +00:00
|
|
|
memcpy(&classmark1, &equip->classmark1, sizeof(classmark1));
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Sync Equipment IMEI=%s, classmark1=%02x",
|
2009-10-22 13:34:37 +00:00
|
|
|
equip->imei, classmark1);
|
2009-08-17 07:39:55 +00:00
|
|
|
if (equip->classmark2_len)
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGPC(DDB, ", classmark2=%s",
|
2011-05-07 10:12:48 +00:00
|
|
|
osmo_hexdump(equip->classmark2, equip->classmark2_len));
|
2009-08-17 07:39:55 +00:00
|
|
|
if (equip->classmark3_len)
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGPC(DDB, ", classmark3=%s",
|
2011-05-07 10:12:48 +00:00
|
|
|
osmo_hexdump(equip->classmark3, equip->classmark3_len));
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGPC(DDB, "\n");
|
2009-08-17 07:39:55 +00:00
|
|
|
|
2009-07-05 12:08:13 +00:00
|
|
|
dbi_conn_quote_binary_copy(conn, equip->classmark2,
|
|
|
|
equip->classmark2_len, &cm2);
|
|
|
|
dbi_conn_quote_binary_copy(conn, equip->classmark3,
|
|
|
|
equip->classmark3_len, &cm3);
|
2010-12-26 19:04:49 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, equip->imei, &q_imei);
|
2009-07-05 12:08:13 +00:00
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE Equipment SET "
|
|
|
|
"updated = datetime('now'), "
|
|
|
|
"classmark1 = %u, "
|
|
|
|
"classmark2 = %s, "
|
|
|
|
"classmark3 = %s "
|
2010-12-26 19:04:49 +00:00
|
|
|
"WHERE imei = %s ",
|
|
|
|
classmark1, cm2, cm3, q_imei);
|
2009-07-05 12:08:13 +00:00
|
|
|
|
|
|
|
free(cm2);
|
|
|
|
free(cm3);
|
2010-12-26 19:04:49 +00:00
|
|
|
free(q_imei);
|
2009-07-05 12:08:13 +00:00
|
|
|
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to update Equipment\n");
|
2009-07-05 12:08:13 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-26 23:27:16 +00:00
|
|
|
int db_subscriber_expire(void *priv, void (*callback)(void *priv, long long unsigned int id))
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_query(conn,
|
|
|
|
"SELECT id "
|
|
|
|
"FROM Subscriber "
|
|
|
|
"WHERE lac != 0 AND "
|
2013-07-27 19:07:57 +00:00
|
|
|
"( expire_lu is NOT NULL "
|
|
|
|
"AND expire_lu < datetime('now') ) "
|
2012-12-26 23:27:16 +00:00
|
|
|
"LIMIT 1");
|
|
|
|
if (!result) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to get expired subscribers\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (dbi_result_next_row(result))
|
|
|
|
callback(priv, dbi_result_get_ulonglong(result, "id"));
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-24 10:21:42 +00:00
|
|
|
int db_subscriber_alloc_tmsi(struct gsm_subscriber *subscriber)
|
|
|
|
{
|
|
|
|
dbi_result result = NULL;
|
2016-03-17 13:41:26 +00:00
|
|
|
char tmsi[14];
|
2010-09-17 22:44:24 +00:00
|
|
|
char *tmsi_quoted;
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 15:59:25 +00:00
|
|
|
for (;;) {
|
2015-10-08 14:10:23 +00:00
|
|
|
if (RAND_bytes((uint8_t *) &subscriber->tmsi, sizeof(subscriber->tmsi)) != 1) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-08-19 10:53:57 +00:00
|
|
|
if (subscriber->tmsi == GSM_RESERVED_TMSI)
|
|
|
|
continue;
|
|
|
|
|
2016-03-17 13:41:26 +00:00
|
|
|
sprintf(tmsi, "%u", subscriber->tmsi);
|
2009-08-19 10:53:57 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, tmsi, &tmsi_quoted);
|
2008-12-27 15:59:25 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"SELECT * FROM Subscriber "
|
|
|
|
"WHERE tmsi = %s ",
|
2009-12-24 10:21:42 +00:00
|
|
|
tmsi_quoted);
|
|
|
|
|
2009-01-01 18:02:05 +00:00
|
|
|
free(tmsi_quoted);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
|
|
|
|
"while allocating new TMSI.\n");
|
2008-12-27 15:59:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
if (dbi_result_get_numrows(result)) {
|
2008-12-27 15:59:25 +00:00
|
|
|
dbi_result_free(result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Allocated TMSI %u for IMSI %s.\n",
|
|
|
|
subscriber->tmsi, subscriber->imsi);
|
2009-01-01 18:02:05 +00:00
|
|
|
return db_sync_subscriber(subscriber);
|
2008-12-27 15:59:25 +00:00
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
}
|
|
|
|
return 0;
|
2008-12-27 00:45:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 13:32:16 +00:00
|
|
|
int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber, uint64_t smin,
|
|
|
|
uint64_t smax)
|
2009-12-24 10:21:42 +00:00
|
|
|
{
|
|
|
|
dbi_result result = NULL;
|
2011-04-18 15:04:00 +00:00
|
|
|
uint32_t try;
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2009-08-12 17:45:37 +00:00
|
|
|
for (;;) {
|
2016-06-07 13:32:16 +00:00
|
|
|
try = (rand() % (smax - smin + 1) + smin);
|
2009-08-12 17:45:37 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"SELECT * FROM Subscriber "
|
2009-08-12 17:59:27 +00:00
|
|
|
"WHERE extension = %i",
|
2009-08-12 17:45:37 +00:00
|
|
|
try
|
|
|
|
);
|
2009-12-24 10:21:42 +00:00
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
|
|
|
|
"while allocating new extension.\n");
|
2009-08-12 17:45:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (dbi_result_get_numrows(result)){
|
|
|
|
dbi_result_free(result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
}
|
|
|
|
sprintf(subscriber->extension, "%i", try);
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
|
2009-08-12 17:45:37 +00:00
|
|
|
return db_sync_subscriber(subscriber);
|
|
|
|
}
|
2009-08-12 12:31:14 +00:00
|
|
|
/*
|
|
|
|
* try to allocate a new unique token for this subscriber and return it
|
|
|
|
* via a parameter. if the subscriber already has a token, return
|
|
|
|
* an error.
|
|
|
|
*/
|
|
|
|
|
2011-04-18 15:04:00 +00:00
|
|
|
int db_subscriber_alloc_token(struct gsm_subscriber *subscriber, uint32_t *token)
|
2009-08-13 11:26:11 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
2011-04-18 15:04:00 +00:00
|
|
|
uint32_t try;
|
2009-08-13 11:26:11 +00:00
|
|
|
|
2009-08-12 12:31:14 +00:00
|
|
|
for (;;) {
|
2015-10-08 14:10:26 +00:00
|
|
|
if (RAND_bytes((uint8_t *) &try, sizeof(try)) != 1) {
|
|
|
|
LOGP(DDB, LOGL_ERROR, "RAND_bytes failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-08-12 12:31:14 +00:00
|
|
|
if (!try) /* 0 is an invalid token */
|
|
|
|
continue;
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"SELECT * FROM AuthToken "
|
2009-08-13 11:26:11 +00:00
|
|
|
"WHERE subscriber_id = %llu OR token = \"%08X\" ",
|
|
|
|
subscriber->id, try);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query AuthToken "
|
|
|
|
"while allocating new token.\n");
|
2009-08-12 12:31:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-08-13 11:26:11 +00:00
|
|
|
if (dbi_result_get_numrows(result)) {
|
2009-08-12 12:31:14 +00:00
|
|
|
dbi_result_free(result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
|
|
|
}
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT INTO AuthToken "
|
|
|
|
"(subscriber_id, created, token) "
|
|
|
|
"VALUES "
|
2009-08-13 11:26:11 +00:00
|
|
|
"(%llu, datetime('now'), \"%08X\") ",
|
|
|
|
subscriber->id, try);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to create token %08X for "
|
|
|
|
"IMSI %s.\n", try, subscriber->imsi);
|
2009-08-12 12:31:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-08-13 11:26:11 +00:00
|
|
|
dbi_result_free(result);
|
2009-08-12 12:31:14 +00:00
|
|
|
*token = try;
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Allocated token %08X for IMSI %s.\n", try, subscriber->imsi);
|
2009-08-13 11:26:11 +00:00
|
|
|
|
2009-08-12 12:31:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 15:50:17 +00:00
|
|
|
int db_subscriber_assoc_imei(struct gsm_subscriber *subscriber, char imei[GSM23003_IMEISV_NUM_DIGITS])
|
2009-12-24 10:21:42 +00:00
|
|
|
{
|
2009-11-06 15:06:19 +00:00
|
|
|
unsigned long long equipment_id, watch_id;
|
2008-12-27 18:04:34 +00:00
|
|
|
dbi_result result;
|
|
|
|
|
2017-01-13 02:12:08 +00:00
|
|
|
osmo_strlcpy(subscriber->equipment.imei, imei, sizeof(subscriber->equipment.imei));
|
2009-07-05 12:08:13 +00:00
|
|
|
|
2008-12-27 18:04:34 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT OR IGNORE INTO Equipment "
|
2008-12-27 18:08:13 +00:00
|
|
|
"(imei, created, updated) "
|
2008-12-27 18:04:34 +00:00
|
|
|
"VALUES "
|
2008-12-27 18:08:13 +00:00
|
|
|
"(%s, datetime('now'), datetime('now')) ",
|
2009-12-24 10:21:42 +00:00
|
|
|
imei);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to create Equipment by IMEI.\n");
|
2008-12-27 18:04:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 22:33:34 +00:00
|
|
|
equipment_id = 0;
|
|
|
|
if (dbi_result_get_numrows_affected(result)) {
|
|
|
|
equipment_id = dbi_conn_sequence_last(conn, NULL);
|
|
|
|
}
|
2008-12-27 18:04:34 +00:00
|
|
|
dbi_result_free(result);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
|
|
|
if (equipment_id)
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
|
2008-12-27 18:04:34 +00:00
|
|
|
else {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"SELECT id FROM Equipment "
|
|
|
|
"WHERE imei = %s ",
|
|
|
|
imei
|
|
|
|
);
|
2009-12-24 10:21:42 +00:00
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to query Equipment by IMEI.\n");
|
2008-12-27 18:04:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!dbi_result_next_row(result)) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to find the Equipment.\n");
|
2008-12-27 18:04:34 +00:00
|
|
|
dbi_result_free(result);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
equipment_id = dbi_result_get_ulonglong(result, "id");
|
|
|
|
dbi_result_free(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT OR IGNORE INTO EquipmentWatch "
|
|
|
|
"(subscriber_id, equipment_id, created, updated) "
|
|
|
|
"VALUES "
|
|
|
|
"(%llu, %llu, datetime('now'), datetime('now')) ",
|
2009-12-24 10:21:42 +00:00
|
|
|
subscriber->id, equipment_id);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to create EquipmentWatch.\n");
|
2008-12-27 18:04:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 22:33:34 +00:00
|
|
|
watch_id = 0;
|
2009-12-24 10:21:42 +00:00
|
|
|
if (dbi_result_get_numrows_affected(result))
|
2008-12-27 22:33:34 +00:00
|
|
|
watch_id = dbi_conn_sequence_last(conn, NULL);
|
2009-12-24 10:21:42 +00:00
|
|
|
|
2008-12-27 18:04:34 +00:00
|
|
|
dbi_result_free(result);
|
2009-12-24 10:21:42 +00:00
|
|
|
if (watch_id)
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
|
|
|
|
equipment_id, subscriber->imsi, imei);
|
2008-12-27 18:04:34 +00:00
|
|
|
else {
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE EquipmentWatch "
|
|
|
|
"SET updated = datetime('now') "
|
|
|
|
"WHERE subscriber_id = %llu AND equipment_id = %llu ",
|
2009-12-24 10:21:42 +00:00
|
|
|
subscriber->id, equipment_id);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to update EquipmentWatch.\n");
|
2008-12-27 18:04:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dbi_result_free(result);
|
2009-12-24 10:39:14 +00:00
|
|
|
DEBUGP(DDB, "Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n",
|
|
|
|
equipment_id, subscriber->imsi, imei);
|
2008-12-27 18:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-30 20:56:32 +00:00
|
|
|
/* store an [unsent] SMS to the database */
|
|
|
|
int db_sms_store(struct gsm_sms *sms)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
2013-10-08 01:17:30 +00:00
|
|
|
char *q_text, *q_daddr, *q_saddr;
|
2009-08-08 14:03:15 +00:00
|
|
|
unsigned char *q_udata;
|
|
|
|
char *validity_timestamp = "2222-2-2";
|
|
|
|
|
|
|
|
/* FIXME: generate validity timestamp based on validity_minutes */
|
2009-03-30 20:56:32 +00:00
|
|
|
|
|
|
|
dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
|
2012-11-23 22:35:01 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr);
|
2013-10-08 01:17:30 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr);
|
2009-08-08 14:03:15 +00:00
|
|
|
dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len,
|
|
|
|
&q_udata);
|
2013-10-08 01:17:30 +00:00
|
|
|
|
2009-07-27 18:11:35 +00:00
|
|
|
/* FIXME: correct validity period */
|
2009-03-30 20:56:32 +00:00
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT INTO SMS "
|
2013-10-08 01:17:32 +00:00
|
|
|
"(created, valid_until, "
|
2009-08-08 14:03:15 +00:00
|
|
|
"reply_path_req, status_rep_req, protocol_id, "
|
2013-10-08 01:17:30 +00:00
|
|
|
"data_coding_scheme, ud_hdr_ind, "
|
|
|
|
"user_data, text, "
|
|
|
|
"dest_addr, dest_ton, dest_npi, "
|
|
|
|
"src_addr, src_ton, src_npi) VALUES "
|
2013-10-08 01:17:32 +00:00
|
|
|
"(datetime('now'), %u, "
|
2013-10-08 01:17:30 +00:00
|
|
|
"%u, %u, %u, "
|
|
|
|
"%u, %u, "
|
|
|
|
"%s, %s, "
|
|
|
|
"%s, %u, %u, "
|
|
|
|
"%s, %u, %u)",
|
2013-10-08 01:17:32 +00:00
|
|
|
validity_timestamp,
|
2009-08-08 14:03:15 +00:00
|
|
|
sms->reply_path_req, sms->status_rep_req, sms->protocol_id,
|
2009-08-09 17:03:42 +00:00
|
|
|
sms->data_coding_scheme, sms->ud_hdr_ind,
|
2013-10-08 01:17:30 +00:00
|
|
|
q_udata, q_text,
|
|
|
|
q_daddr, sms->dst.ton, sms->dst.npi,
|
|
|
|
q_saddr, sms->src.ton, sms->src.npi);
|
2009-03-30 20:56:32 +00:00
|
|
|
free(q_text);
|
2009-08-08 14:03:15 +00:00
|
|
|
free(q_udata);
|
2013-10-08 01:17:30 +00:00
|
|
|
free(q_daddr);
|
|
|
|
free(q_saddr);
|
2009-03-30 20:56:32 +00:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-09 17:05:21 +00:00
|
|
|
static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result)
|
2009-03-30 20:56:32 +00:00
|
|
|
{
|
2009-08-08 14:03:15 +00:00
|
|
|
struct gsm_sms *sms = sms_alloc();
|
2013-10-08 01:17:30 +00:00
|
|
|
const char *text, *daddr, *saddr;
|
2009-08-08 14:03:15 +00:00
|
|
|
const unsigned char *user_data;
|
2009-03-30 20:56:32 +00:00
|
|
|
|
2009-08-08 14:03:15 +00:00
|
|
|
if (!sms)
|
2009-03-30 20:56:32 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-07-05 12:06:41 +00:00
|
|
|
sms->id = dbi_result_get_ulonglong(result, "id");
|
|
|
|
|
2009-07-27 18:11:35 +00:00
|
|
|
/* FIXME: validity */
|
2009-08-08 14:03:15 +00:00
|
|
|
/* FIXME: those should all be get_uchar, but sqlite3 is braindead */
|
2014-07-03 12:00:30 +00:00
|
|
|
sms->reply_path_req = dbi_result_get_ulonglong(result, "reply_path_req");
|
|
|
|
sms->status_rep_req = dbi_result_get_ulonglong(result, "status_rep_req");
|
|
|
|
sms->ud_hdr_ind = dbi_result_get_ulonglong(result, "ud_hdr_ind");
|
|
|
|
sms->protocol_id = dbi_result_get_ulonglong(result, "protocol_id");
|
|
|
|
sms->data_coding_scheme = dbi_result_get_ulonglong(result,
|
2009-07-27 18:11:35 +00:00
|
|
|
"data_coding_scheme");
|
2009-08-08 14:03:15 +00:00
|
|
|
/* sms->msg_ref is temporary and not stored in DB */
|
|
|
|
|
2014-07-03 12:00:30 +00:00
|
|
|
sms->dst.npi = dbi_result_get_ulonglong(result, "dest_npi");
|
|
|
|
sms->dst.ton = dbi_result_get_ulonglong(result, "dest_ton");
|
2009-08-08 14:03:15 +00:00
|
|
|
daddr = dbi_result_get_string(result, "dest_addr");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (daddr)
|
|
|
|
osmo_strlcpy(sms->dst.addr, daddr, sizeof(sms->dst.addr));
|
2014-12-03 08:28:24 +00:00
|
|
|
sms->receiver = subscr_get_by_extension(net->subscr_group, sms->dst.addr);
|
2009-07-27 18:11:35 +00:00
|
|
|
|
2014-07-03 12:00:30 +00:00
|
|
|
sms->src.npi = dbi_result_get_ulonglong(result, "src_npi");
|
|
|
|
sms->src.ton = dbi_result_get_ulonglong(result, "src_ton");
|
2013-10-08 01:17:30 +00:00
|
|
|
saddr = dbi_result_get_string(result, "src_addr");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (saddr)
|
|
|
|
osmo_strlcpy(sms->src.addr, saddr, sizeof(sms->src.addr));
|
2013-10-08 01:17:30 +00:00
|
|
|
|
2009-08-08 14:03:15 +00:00
|
|
|
sms->user_data_len = dbi_result_get_field_length(result, "user_data");
|
|
|
|
user_data = dbi_result_get_binary(result, "user_data");
|
|
|
|
if (sms->user_data_len > sizeof(sms->user_data))
|
2011-04-18 15:04:00 +00:00
|
|
|
sms->user_data_len = (uint8_t) sizeof(sms->user_data);
|
2009-08-08 14:03:15 +00:00
|
|
|
memcpy(sms->user_data, user_data, sms->user_data_len);
|
2009-03-30 20:56:32 +00:00
|
|
|
|
2009-07-05 12:06:41 +00:00
|
|
|
text = dbi_result_get_string(result, "text");
|
2017-01-13 02:12:08 +00:00
|
|
|
if (text)
|
|
|
|
osmo_strlcpy(sms->text, text, sizeof(sms->text));
|
2009-08-09 17:05:21 +00:00
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
2010-12-24 22:18:31 +00:00
|
|
|
struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_sms *sms;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"SELECT * FROM SMS WHERE SMS.id = %llu", id);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sms = sms_from_result(net, result);
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
2009-08-09 17:05:21 +00:00
|
|
|
/* retrieve the next unsent SMS with ID >= min_id */
|
2010-03-25 08:59:30 +00:00
|
|
|
struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id)
|
2009-08-09 17:05:21 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_sms *sms;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 20:00:45 +00:00
|
|
|
"SELECT SMS.* "
|
|
|
|
"FROM SMS JOIN Subscriber ON "
|
2013-10-08 01:17:32 +00:00
|
|
|
"SMS.dest_addr = Subscriber.extension "
|
2010-07-03 20:00:45 +00:00
|
|
|
"WHERE SMS.id >= %llu AND SMS.sent IS NULL "
|
|
|
|
"AND Subscriber.lac > 0 "
|
|
|
|
"ORDER BY SMS.id LIMIT 1",
|
2009-08-09 17:05:21 +00:00
|
|
|
min_id);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sms = sms_from_result(net, result);
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
2010-12-24 23:33:40 +00:00
|
|
|
struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net,
|
|
|
|
unsigned long long min_subscr_id,
|
|
|
|
unsigned int failed)
|
2009-12-22 12:22:29 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_sms *sms;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 20:00:45 +00:00
|
|
|
"SELECT SMS.* "
|
|
|
|
"FROM SMS JOIN Subscriber ON "
|
2013-10-08 01:17:32 +00:00
|
|
|
"SMS.dest_addr = Subscriber.extension "
|
|
|
|
"WHERE Subscriber.id >= %llu AND SMS.sent IS NULL "
|
2010-12-24 23:33:40 +00:00
|
|
|
"AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u "
|
2013-10-08 01:17:32 +00:00
|
|
|
"ORDER BY Subscriber.id, SMS.id LIMIT 1",
|
2010-12-24 23:33:40 +00:00
|
|
|
min_subscr_id, failed);
|
2009-12-22 12:22:29 +00:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sms = sms_from_result(net, result);
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
|
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
2009-12-21 00:09:57 +00:00
|
|
|
/* retrieve the next unsent SMS for a given subscriber */
|
2009-08-09 17:05:21 +00:00
|
|
|
struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
struct gsm_sms *sms;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-07-03 20:00:45 +00:00
|
|
|
"SELECT SMS.* "
|
|
|
|
"FROM SMS JOIN Subscriber ON "
|
2013-10-08 01:17:32 +00:00
|
|
|
"SMS.dest_addr = Subscriber.extension "
|
|
|
|
"WHERE Subscriber.id = %llu AND SMS.sent IS NULL "
|
2010-07-03 20:00:45 +00:00
|
|
|
"AND Subscriber.lac > 0 "
|
|
|
|
"ORDER BY SMS.id LIMIT 1",
|
2009-08-09 17:05:21 +00:00
|
|
|
subscr->id);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!dbi_result_next_row(result)) {
|
|
|
|
dbi_result_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-03 08:28:24 +00:00
|
|
|
sms = sms_from_result(subscr->group->net, result);
|
2009-03-30 20:56:32 +00:00
|
|
|
|
|
|
|
dbi_result_free(result);
|
2009-08-09 17:05:21 +00:00
|
|
|
|
2009-03-30 20:56:32 +00:00
|
|
|
return sms;
|
|
|
|
}
|
|
|
|
|
2014-03-08 20:27:37 +00:00
|
|
|
/* mark a given SMS as delivered */
|
|
|
|
int db_sms_mark_delivered(struct gsm_sms *sms)
|
2009-03-30 20:56:32 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE SMS "
|
|
|
|
"SET sent = datetime('now') "
|
|
|
|
"WHERE id = %llu", sms->id);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to mark SMS %llu as sent.\n", sms->id);
|
2009-03-30 20:56:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-15 18:15:14 +00:00
|
|
|
|
|
|
|
/* increase the number of attempted deliveries */
|
|
|
|
int db_sms_inc_deliver_attempts(struct gsm_sms *sms)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"UPDATE SMS "
|
|
|
|
"SET deliver_attempts = deliver_attempts + 1 "
|
|
|
|
"WHERE id = %llu", sms->id);
|
|
|
|
if (!result) {
|
2009-12-24 10:39:14 +00:00
|
|
|
LOGP(DDB, LOGL_ERROR, "Failed to inc deliver attempts for "
|
|
|
|
"SMS %llu.\n", sms->id);
|
2009-08-15 18:15:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-16 08:40:10 +00:00
|
|
|
|
2010-03-29 06:47:44 +00:00
|
|
|
int db_apdu_blob_store(struct gsm_subscriber *subscr,
|
2011-04-18 15:04:00 +00:00
|
|
|
uint8_t apdu_id_flags, uint8_t len,
|
|
|
|
uint8_t *apdu)
|
2009-08-16 08:40:10 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
2009-10-22 13:34:37 +00:00
|
|
|
unsigned char *q_apdu;
|
2009-08-16 08:40:10 +00:00
|
|
|
|
|
|
|
dbi_conn_quote_binary_copy(conn, apdu, len, &q_apdu);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT INTO ApduBlobs "
|
|
|
|
"(created,subscriber_id,apdu_id_flags,apdu) VALUES "
|
|
|
|
"(datetime('now'),%llu,%u,%s)",
|
|
|
|
subscr->id, apdu_id_flags, q_apdu);
|
|
|
|
|
|
|
|
free(q_apdu);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-22 18:07:32 +00:00
|
|
|
|
2011-05-06 10:13:10 +00:00
|
|
|
int db_store_counter(struct osmo_counter *ctr)
|
2009-12-22 18:07:32 +00:00
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
char *q_name;
|
|
|
|
|
|
|
|
dbi_conn_quote_string_copy(conn, ctr->name, &q_name);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
|
|
|
"INSERT INTO Counters "
|
|
|
|
"(timestamp,name,value) VALUES "
|
|
|
|
"(datetime('now'),%s,%lu)", q_name, ctr->value);
|
|
|
|
|
|
|
|
free(q_name);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-13 09:45:07 +00:00
|
|
|
|
|
|
|
static int db_store_rate_ctr(struct rate_ctr_group *ctrg, unsigned int num,
|
|
|
|
char *q_prefix)
|
|
|
|
{
|
|
|
|
dbi_result result;
|
|
|
|
char *q_name;
|
|
|
|
|
|
|
|
dbi_conn_quote_string_copy(conn, ctrg->desc->ctr_desc[num].name,
|
|
|
|
&q_name);
|
|
|
|
|
|
|
|
result = dbi_conn_queryf(conn,
|
2010-05-13 10:55:20 +00:00
|
|
|
"Insert INTO RateCounters "
|
2010-05-14 15:38:47 +00:00
|
|
|
"(timestamp,name,idx,value) VALUES "
|
2010-05-13 10:55:20 +00:00
|
|
|
"(datetime('now'),%s.%s,%u,%"PRIu64")",
|
|
|
|
q_prefix, q_name, ctrg->idx, ctrg->ctr[num].current);
|
2010-05-13 09:45:07 +00:00
|
|
|
|
|
|
|
free(q_name);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
dbi_result_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_store_rate_ctr_group(struct rate_ctr_group *ctrg)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
char *q_prefix;
|
|
|
|
|
2010-05-13 10:55:20 +00:00
|
|
|
dbi_conn_quote_string_copy(conn, ctrg->desc->group_name_prefix, &q_prefix);
|
2010-05-13 09:45:07 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ctrg->desc->num_ctr; i++)
|
|
|
|
db_store_rate_ctr(ctrg, i, q_prefix);
|
|
|
|
|
|
|
|
free(q_prefix);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|