/* Allocate space for the keys we've got stored */
retval = malloc(sizeof(struct afsconf_typedKeyList));
retval->nkeys = opr_queue_Count(&kvnoEntry->subTypeList);
- retval->keys = calloc(retval->nkeys, sizeof(struct afsconf_typedKey *));
- i = 0;
- for(opr_queue_Scan(&kvnoEntry->subTypeList, cursor)) {
- struct subTypeList *entry;
+ if (retval->nkeys > 0) {
+ retval->keys = calloc(retval->nkeys, sizeof(struct afsconf_typedKey *));
- entry = opr_queue_Entry(cursor, struct subTypeList, link);
- retval->keys[i] = afsconf_typedKey_get(entry->key);
- i++;
+ i = 0;
+ for(opr_queue_Scan(&kvnoEntry->subTypeList, cursor)) {
+ struct subTypeList *entry;
+
+ entry = opr_queue_Entry(cursor, struct subTypeList, link);
+ retval->keys[i] = afsconf_typedKey_get(entry->key);
+ i++;
+ }
+ } else {
+ retval->keys = NULL;
}
*keys = retval;
* in the original KeyFile, so that we can continue to be compatible with
* utilities that directly modify that file.
*
- * All other keys are stored in the file KeyFileEx, which has the following
+ * All other keys are stored in the file KeyFileExt, which has the following
* format:
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- * | version number |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | number of keys |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Key data ...
* +-+-+-+-+-+-+-+
*
- * The version number is 1 at present. Version numbers higher than 1
- * indicate a keyfile that is not backwards compatible with this
- * specification.
+ * If the format ever needs to chanage incompatibly, a new file name
+ * will be used.
*
* Key data is a sequence of the following records (note that these are
* not word aligned - the next record begins where the previous one ends)
for(i=0; i<nkeys; i++) {
key = afsconf_typedKey_blank();
+ if (key == NULL)
+ goto fail;
key->type = afsconf_rxkad;
key->subType = 0;
return AFSCONF_FAILURE;
typeEntry = findByType(dir, afsconf_rxkad);
- if (typeEntry == NULL)
- goto out;
-
- nkeys = opr_queue_Count(&typeEntry->kvnoList);
+ if (typeEntry)
+ nkeys = opr_queue_Count(&typeEntry->kvnoList);
+ else
+ nkeys = 0;
if (writeWord(fd, nkeys))
goto fail;
+ if (typeEntry == NULL)
+ goto out;
+
for (opr_queue_Scan(&typeEntry->kvnoList, cursor)) {
struct kvnoList *kvnoEntry;
struct subTypeList *subEntry;
{
int fd, i, code;
afs_int32 nkeys;
- struct afsconf_typedKey *key;
+ struct afsconf_typedKey *key = NULL;
fd = open(fileName, O_RDONLY);
if (fd < 0)
afs_int32 reclen;
key = afsconf_typedKey_blank();
+ if (key == NULL)
+ goto fail;
/* The only data version we currently parse has a reclen of 16.
* Anything smaller indicates a corrupt key file. Anything more,
code = read(fd, key->key.val, reclen);
if (code != reclen) {
rx_opaque_freeContents(&key->key);
- free(key);
goto fail;
}
code = addMemoryKey(dir, key, 1);
return 0;
fail:
+ if (key)
+ afsconf_typedKey_put(&key);
+
close(fd);
return EIO;
}
/* XXX - Should check that the key is of the correct length */
/* Copy out the relevant details */
- *kvno = typedKey->kvno;
- memcpy(key, typedKey->key.val, 8);
+ if (kvno != NULL)
+ *kvno = typedKey->kvno;
+
+ if (key != NULL)
+ memcpy(key, typedKey->key.val, 8);
afsconf_typedKey_put(&typedKey);
}
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))
+ count++;
+ }
+ }
+
+ /* Allocate space for all of these */
+ retval = malloc(sizeof(struct afsconf_typedKeyList));
+ retval->nkeys = count;
+
+ if (count > 0) {
+ 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++;
+ }
+ }
+ }
+ } else {
+ retval->keys = NULL;
+ }
+
+ *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)
{
/* Except, if we're in the rxkad list, we might have a bcrypt entry that
* has a kvno of 999. So we need to skip that one
*/
- while (type == afsconf_rxgk && kvnoEntry->kvno == 999) {
+ while (type == afsconf_rxkad && kvnoEntry->kvno == 999) {
kvnoEntry = opr_queue_Prev(&typeEntry->kvnoList, struct kvnoList,
link);
if (opr_queue_IsEnd(&typeEntry->kvnoList, &kvnoEntry->link))
for (i=0;i<(*keys)->nkeys;i++)
afsconf_typedKey_put(&((*keys)->keys[i]));
- free((*keys)->keys);
+
+ if ((*keys)->keys != NULL)
+ free((*keys)->keys);
+
free(*keys);
*keys = NULL;
}
{
struct afsconf_typedKey *key;
- key = malloc(sizeof(struct afsconf_typedKey));
+ key = calloc(1, sizeof(struct afsconf_typedKey));
if (key == NULL)
return NULL;
- memset(key, 0, sizeof(struct afsconf_typedKey));
rx_atomic_set(&key->refcnt, 1);
return key;