aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc
diff options
context:
space:
mode:
authorJan Luebbe <jluebbe@debian.org>2009-08-12 14:31:14 +0200
committerHarald Welte <laforge@gnumonks.org>2009-08-12 22:21:56 +0200
commit31bef49be2dffdeb28d0b4d0ee5f6df3c06ff960 (patch)
tree51f76f79a2adc1b7693e4133128ac689df9d8af0 /openbsc
parentc70979a337edfae1782507ddca717e099571b686 (diff)
token support in the DB
Diffstat (limited to 'openbsc')
-rw-r--r--openbsc/include/openbsc/db.h1
-rw-r--r--openbsc/src/db.c54
2 files changed, 55 insertions, 0 deletions
diff --git a/openbsc/include/openbsc/db.h b/openbsc/include/openbsc/db.h
index d7547d601..1f52d4e62 100644
--- a/openbsc/include/openbsc/db.h
+++ b/openbsc/include/openbsc/db.h
@@ -38,6 +38,7 @@ struct gsm_subscriber* db_get_subscriber(struct gsm_network *net,
const char *subscr);
int db_sync_subscriber(struct gsm_subscriber* subscriber);
int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber);
+int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token);
int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char *imei);
int db_sync_equipment(struct gsm_equipment *equip);
diff --git a/openbsc/src/db.c b/openbsc/src/db.c
index e24b619c6..601726096 100644
--- a/openbsc/src/db.c
+++ b/openbsc/src/db.c
@@ -57,6 +57,12 @@ static char *create_stmts[] = {
"tmsi TEXT UNIQUE, "
"lac INTEGER NOT NULL DEFAULT 0"
")",
+ "CREATE TABLE IF NOT EXISTS AuthToken ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "subscriber_id INTEGER UNIQUE NOT NULL, "
+ "created TIMESTAMP NOT NULL, "
+ "token TEXT UNIQUE NOT NULL"
+ ")",
"CREATE TABLE IF NOT EXISTS Equipment ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"created TIMESTAMP NOT NULL, "
@@ -413,6 +419,54 @@ int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
return 0;
}
+/*
+ * 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.
+ */
+
+int db_subscriber_alloc_token(struct gsm_subscriber* subscriber, u_int32_t* token) {
+ dbi_result result=NULL;
+ u_int32_t try;
+ for (;;) {
+ try = rand();
+ if (!try) /* 0 is an invalid token */
+ continue;
+ result = dbi_conn_queryf(conn,
+ "SELECT * FROM AuthToken "
+ "WHERE subscriber_id = %llu OR token = %08x ",
+ subscriber->id, try
+ );
+ if (result==NULL) {
+ printf("DB: Failed to query AuthToken while allocating new token.\n");
+ 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);
+ }
+ result = dbi_conn_queryf(conn,
+ "INSERT INTO AuthToken "
+ "(subscriber_id, created, token) "
+ "VALUES "
+ "(%llu, datetime('now'), %08x)) ",
+ subscriber->id, try
+ );
+ if (result==NULL) {
+ printf("DB: Failed to create token %08x for IMSI %s.\n", try, subscriber->imsi);
+ return 1;
+ }
+ *token = try;
+ printf("DB: Allocated token %08x for IMSI %s.\n", try, subscriber->imsi);
+ return 0;
+}
+
int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
u_int64_t equipment_id, watch_id;
dbi_result result;