summaryrefslogtreecommitdiff
path: root/module/storage.c
diff options
context:
space:
mode:
Diffstat (limited to 'module/storage.c')
-rw-r--r--module/storage.c156
1 files changed, 81 insertions, 75 deletions
diff --git a/module/storage.c b/module/storage.c
index 3cb9cdd..cb1085d 100644
--- a/module/storage.c
+++ b/module/storage.c
@@ -1,8 +1,15 @@
+#include "mod_auth_singleid.h"
+
+#include <assert.h>
+#include <string.h>
+
+#include <apr_sha1.h>
+
/* Yes, this looks backwards. */
-typedef char nonce_t[40];
+typedef char sid_nonce_t[40];
-struct singleid_board {
+struct sid_storage {
/* The association with our server */
char server[256];
@@ -13,7 +20,7 @@ struct singleid_board {
time_t expires;
/* Book keeping for the records */
- nonce_t *records;
+ sid_nonce_t *records;
size_t first;
size_t total;
size_t count;
@@ -21,78 +28,78 @@ struct singleid_board {
};
int
-singleid_board_store_assoc (singleid_board_t *board, const singleid_assoc_t *assoc)
+sid_storage_store_assoc (sid_storage_t *store, const sid_assoc_t *assoc)
{
- assert (board);
+ assert (store);
assert (assoc);
/* Check that we have enough space to store this information */
- if ((assoc->server && strlen (assoc->server) > sizeof (board->server)) ||
- (assoc->handle && strlen (assoc->handle) > sizeof (board->handle)) ||
- (assoc->n_secret && assoc->n_secret > sizeof (board->secret)) ||
- (assoc->type && strlen (assoc->type) > sizeof (board->type)))
+ if ((assoc->server && strlen (assoc->server) > sizeof (store->server)) ||
+ (assoc->handle && strlen (assoc->handle) > sizeof (store->handle)) ||
+ (assoc->n_secret && assoc->n_secret > sizeof (store->secret)) ||
+ (assoc->type && strlen (assoc->type) > sizeof (store->type)))
return 0;
- strcpy (board->server, assoc->server ? assoc->server : "");
- strcpy (board->handle, assoc->handle ? assoc->handle : "");
- memcpy (board->secret, assoc->secret, assoc->n_secret);
- strcpy (board->type, assoc->type ? assoc->type : "");
- board->expires = assoc->expires;
+ strcpy (store->server, assoc->server ? assoc->server : "");
+ strcpy (store->handle, assoc->handle ? assoc->handle : "");
+ memcpy (store->secret, assoc->secret, assoc->n_secret);
+ strcpy (store->type, assoc->type ? assoc->type : "");
+ store->expires = assoc->expires;
return 1;
}
int
-singleid_board_find_assoc (singleid_board_t *board, const char *server,
- const char *handle, singleid_assoc_t *assoc)
+sid_storage_find_assoc (sid_storage_t *store, const char *server,
+ const char *handle, sid_assoc_t *assoc)
{
- assert (board);
+ assert (store);
assert (assoc);
- if (server && (strlen (server) > sizeof (board->server) ||
- strcmp (server, board->server) != 0))
+ if (server && (strlen (server) > sizeof (store->server) ||
+ strcmp (server, store->server) != 0))
return 0;
- if (handle && (strlen (handle) > sizeof (board->handle) ||
- strcmp (handle, board->handle) != 0))
+ if (handle && (strlen (handle) > sizeof (store->handle) ||
+ strcmp (handle, store->handle) != 0))
return 0;
- assoc->server = board->server;
- assoc->handle = board->handle;
- assoc->type = board->type;
- assoc->secret = board->secret;
- assoc->n_secret = board->n_secret;
- assoc->expires = board->expires;
+ assoc->server = store->server;
+ assoc->handle = store->handle;
+ assoc->type = store->type;
+ assoc->secret = store->secret;
+ assoc->n_secret = store->n_secret;
+ assoc->expires = store->expires;
return 1;
}
void
-singleid_board_invalidate_assoc (singleid_board_t *board, const char *server,
+sid_storage_invalidate_assoc (sid_storage_t *store, const char *server,
const char *handle)
{
- singleid_assoc_t dummy;
- assert (board);
-
- if (singleid_board_find_assoc (board, server, handle, &dummy)) {
- board->server[0] = 0;
- board->handle[0] = 0;
- board->type[0] = 0;
- memset (board->secret, 0, sizeof (board->secret));
- board->n_secret = 0;
- board->expires = 0;
- board->secret[0] = 0;
+ sid_assoc_t dummy;
+ assert (store);
+
+ if (sid_storage_find_assoc (store, server, handle, &dummy)) {
+ store->server[0] = 0;
+ store->handle[0] = 0;
+ store->type[0] = 0;
+ memset (store->secret, 0, sizeof (store->secret));
+ store->n_secret = 0;
+ store->expires = 0;
+ store->secret[0] = 0;
}
}
#define nonce_compare(a, b) \
- memcmp (a, b, sizeof (nonce_t))
+ memcmp (a, b, sizeof (sid_nonce_t))
static void
-nonce_put (nonce_t rec, const char *nonce)
+nonce_put (sid_nonce_t rec, const char *nonce)
{
size_t len = strlen (nonce);
char *dst = (char*)rec;
- /* If it's short enough, then just board. Fast */
+ /* If it's short enough, then just store. Fast */
if (len < sizeof (rec)) {
memcpy (dst, nonce, len);
memset (dst + len, 0, sizeof (rec) - len);
@@ -101,7 +108,7 @@ nonce_put (nonce_t rec, const char *nonce)
} else {
apr_sha1_ctx_t ctx;
- assert (sizeof (nonce_t) == APR_SHA1_DIGESTSIZE + 20);
+ assert (sizeof (sid_nonce_t) == APR_SHA1_DIGESTSIZE + 20);
assert (len > 20);
/* The date prefix we just copy in */
@@ -110,65 +117,64 @@ nonce_put (nonce_t rec, const char *nonce)
/* Hash the rest into the buffer */
apr_sha1_init (&ctx);
apr_sha1_update (&ctx, nonce + 20, len - 20);
- apr_sha1_final (dst + 20, &ctx);
+ apr_sha1_final ((unsigned char*)dst + 20, &ctx);
}
}
static void
-insert_nonce_record (singleid_board_t *board, nonce_t rec, size_t at)
+insert_nonce_record (sid_storage_t *store, sid_nonce_t rec, size_t at)
{
- nonce_t *records = board->records;
- size_t from, to, num;
+ sid_nonce_t *records = store->records;
- assert (board->total > 2);
- assert (at < board->total);
- assert (at != board->first);
+ assert (store->total > 2);
+ assert (at < store->total);
+ assert (at != store->first);
/* Insertion right after latest, either ancient, more likely top */
- if (at == board->first + 1 % board->total) {
+ if (at == store->first + 1 % store->total) {
/* We can just copy in at this point */
/* Our ring has empty space in it, so always push forwards, but only until first */
- } else if (!board->wrapped) {
- memmove (records + at + 1, records + at, sizeof (rec) * (board->first - at));
- board->first += 1;
+ } else if (!store->wrapped) {
+ memmove (records + at + 1, records + at, sizeof (rec) * (store->first - at));
+ store->first += 1;
/* Move data backwards to make space */
- } else if (board->first < at) {
- memmove (records + board->first + 2, records + board->first + 1,
- sizeof (rec) * (at - board->first));
+ } else if (store->first < at) {
+ memmove (records + store->first + 2, records + store->first + 1,
+ sizeof (rec) * (at - store->first));
/* Move data forwards to make space simply */
} else {
- memmove (records + at + 1, records + at, sizeof (rec) * (board->first - at - 1));
+ memmove (records + at + 1, records + at, sizeof (rec) * (store->first - at - 1));
}
memcpy (records[at], rec, sizeof (rec));
- ++board->count;
+ ++store->count;
/* Track whether we have a full ring or not. */
- if (!board->wrapped && board->count > board->total)
- board->wrapped = 1;
+ if (!store->wrapped && store->count > store->total)
+ store->wrapped = 1;
}
int
-singleid_board_check_nonce (singleid_board_t *board, const char *nonce)
+sid_storage_check_nonce (sid_storage_t *store, const char *server, const char *nonce)
{
- nonce_t *records;
- nonce_t rec;
+ sid_nonce_t *records;
+ sid_nonce_t rec;
size_t at, lower, upper, mid;
int res;
- assert (board);
+ assert (store);
assert (nonce);
- assert (board->records);
- assert (board->first < board->total);
+ assert (store->records);
+ assert (store->first < store->total);
nonce_put (rec, nonce);
- records = board->records;
+ records = store->records;
/* Best case scenario, new nonce is higher than our latest one */
- res = nonce_compare (rec, records[top]);
+ res = nonce_compare (rec, records[store->first]);
/* Was the last nonce */
if (res == 0) {
@@ -176,16 +182,16 @@ singleid_board_check_nonce (singleid_board_t *board, const char *nonce)
/* Newer than anything, push on top */
} else if (res < 0) {
- at = (board->first + 1) % board->total;
- insert_nonce_record (board, rec, at);
- board->first = at;
+ at = (store->first + 1) % store->total;
+ insert_nonce_record (store, rec, at);
+ store->first = at;
return 0;
}
/* Do a binary search for the item */
- for (lower = 0, upper = board->total; lower < upper; ) {
+ for (lower = 0, upper = store->total; lower < upper; ) {
mid = lower + ((upper - lower) / 2); // Note: not (low + high) / 2 !!
- at = at + board->first % board->total;
+ at = at + store->first % store->total;
res = nonce_compare (rec, records[at]);
if (res == 0)
return 1; /* Have the nonce */
@@ -195,6 +201,6 @@ singleid_board_check_nonce (singleid_board_t *board, const char *nonce)
upper = mid;
}
- insert_nonce_record (board, rec, at);
+ insert_nonce_record (store, rec, at);
return 0; /* Didn't find nonce */
}