diff options
author | Jan Luebbe <jluebbe@debian.org> | 2009-08-12 14:31:14 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2009-08-12 22:21:56 +0200 |
commit | 31bef49be2dffdeb28d0b4d0ee5f6df3c06ff960 (patch) | |
tree | 51f76f79a2adc1b7693e4133128ac689df9d8af0 /openbsc/src | |
parent | c70979a337edfae1782507ddca717e099571b686 (diff) |
token support in the DB
Diffstat (limited to 'openbsc/src')
-rw-r--r-- | openbsc/src/db.c | 54 |
1 files changed, 54 insertions, 0 deletions
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; |