From d0d69ccb643104d06a3036da4b70bd878062f743 Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Tue, 1 Mar 2011 14:23:47 +0000 Subject: [PATCH] auth: Add GetAllKeys function Add support for a GetAllKeys function that can be used to list all of the keys in a configuration directory. Change-Id: I0711fde6afc2941a5f03f2e26ea89ae73750c1a9 Reviewed-on: http://gerrit.openafs.org/4103 Reviewed-by: Derrick Brashear Tested-by: Derrick Brashear --- src/auth/cellconfig.p.h | 2 ++ src/auth/keys.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/auth/keys-t.c | 19 +++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/auth/cellconfig.p.h b/src/auth/cellconfig.p.h index cf137f9..4c2d79b 100644 --- a/src/auth/cellconfig.p.h +++ b/src/auth/cellconfig.p.h @@ -154,6 +154,8 @@ extern void afsconf_typedKey_values(struct afsconf_typedKey *key, int *minorType, struct rx_opaque **keyMaterial); +extern int afsconf_GetAllKeys(struct afsconf_dir *, + struct afsconf_typedKeyList **); extern int afsconf_GetKeysByType(struct afsconf_dir *dir, afsconf_keyType type, int kvno, struct afsconf_typedKeyList **); diff --git a/src/auth/keys.c b/src/auth/keys.c index 0a3723f..c02fec6 100644 --- a/src/auth/keys.c +++ b/src/auth/keys.c @@ -814,6 +814,64 @@ out: } int +afsconf_GetAllKeys(struct afsconf_dir *dir, struct afsconf_typedKeyList **keys) +{ + int code; + struct afsconf_typedKeyList *retval; + struct opr_queue *typeCursor; + struct keyTypeList *typeEntry; + struct opr_queue *kvnoCursor; + struct kvnoList *kvnoEntry; + struct opr_queue *subCursor; + struct subTypeList *subEntry; + int count; + + LOCK_GLOBAL_MUTEX; + + code = _afsconf_Check(dir); + if (code) + goto out; + + count = 0; + /* First, work out how many keys we have in total */ + for (opr_queue_Scan(&dir->keyList, typeCursor)) { + typeEntry = opr_queue_Entry(typeCursor, struct keyTypeList, link); + for (opr_queue_Scan(&typeEntry->kvnoList, kvnoCursor)) { + kvnoEntry = opr_queue_Entry(kvnoCursor, struct kvnoList, link); + for (opr_queue_Scan(&kvnoEntry->subTypeList, subCursor)) { + subEntry = opr_queue_Entry(subCursor, struct subTypeList, link); + count++; + } + } + } + + /* Allocate space for all of these */ + retval = malloc(sizeof(struct afsconf_typedKeyList)); + retval->nkeys = count; + retval->keys = calloc(retval->nkeys, sizeof(struct afsconf_typedKey *)); + + /* Populate the key list */ + count = 0; + for (opr_queue_Scan(&dir->keyList, typeCursor)) { + typeEntry = opr_queue_Entry(typeCursor, struct keyTypeList, link); + for (opr_queue_Scan(&typeEntry->kvnoList, kvnoCursor)) { + kvnoEntry = opr_queue_Entry(kvnoCursor, struct kvnoList, link); + for (opr_queue_Scan(&kvnoEntry->subTypeList, subCursor)) { + subEntry = opr_queue_Entry(subCursor, struct subTypeList, link); + retval->keys[count] = afsconf_typedKey_get(subEntry->key); + count++; + } + } + } + + *keys = retval; + +out: + UNLOCK_GLOBAL_MUTEX; + return code; +} + +int afsconf_GetKeyByTypes(struct afsconf_dir *dir, afsconf_keyType type, int kvno, int subType, struct afsconf_typedKey **key) { diff --git a/tests/auth/keys-t.c b/tests/auth/keys-t.c index 2e3dc6c..9e53958 100644 --- a/tests/auth/keys-t.c +++ b/tests/auth/keys-t.c @@ -105,7 +105,7 @@ int main(int argc, char **argv) int code; int i; - plan(127); + plan(134); /* Create a temporary afs configuration directory */ @@ -527,6 +527,23 @@ int main(int argc, char **argv) " ... with the right key in slot 1"); afsconf_PutTypedKeyList(&typedKeyList); + /* Check that GetAllKeys works as expected */ + code = afsconf_GetAllKeys(dir, &typedKeyList); + is_int(0, code, "afsconf_GetAllKeys returns success"); + is_int(5, typedKeyList->nkeys, " ... with the correct number of keys"); + ok(keyMatches(typedKeyList->keys[0], afsconf_rxkad, 1, 0, + "\x10\x10\x10\x10\x10\x10\x10\x10", 8), + " ... with right key in slot 0"); + ok(keyMatches(typedKeyList->keys[1], afsconf_rxkad, 2, 0, + "\x30\x30\x30\x30\x30\x30\x30\x30", 8), + " ... with right key in slot 1"); + ok(keyMatches(typedKeyList->keys[2], 1, 1, 0, "\x03", 1), + " ... with right key in slot 2"); + ok(keyMatches(typedKeyList->keys[3], 1, 2, 0, "\x01", 1), + " ... with right key in slot 3"); + ok(keyMatches(typedKeyList->keys[4], 1, 2, 1, "\x02\03", 2), + " ... with right key in slot 4"); + afsconf_Close(dir); unlinkTestConfig(dirname); -- 1.9.4