*
* Jonathan McDowell <noodles@earth.li>
*
- * Copyright 2002 Project Purple
+ * Copyright 2002-2004 Project Purple
*/
#include <sys/types.h>
#include <sys/uio.h>
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include "keyid.h"
#include "keystructs.h"
#include "ll.h"
+#include "log.h"
#include "mem.h"
#include "onak-conf.h"
#include "parsekey.h"
*
* This is just a no-op for flat file access.
*/
-void initdb(void)
+void initdb(bool readonly)
{
}
char keyfile[1024];
int fd = -1;
- snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
+ snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
keyid & 0xFFFFFFFF);
fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
if (fd > -1) {
- read_openpgp_stream(file_fetchchar, &fd, &packets);
+ read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
parse_keys(packets, publickey);
+ free_packet_list(packets);
+ packets = NULL;
close(fd);
}
char keyfile[1024];
int fd = -1;
- snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
+ snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
get_keyid(publickey) & 0xFFFFFFFF);
fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
write_openpgp_stream(file_putchar, &fd, packets);
close(fd);
+ free_packet_list(packets);
+ packets = NULL;
}
return (fd > -1);
{
char keyfile[1024];
- snprintf(keyfile, 1023, "%s/0x%llX", config.file_dbpath,
+ snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
keyid & 0xFFFFFFFF);
return unlink(keyfile);
return 0;
}
+/**
+ * iterate_keys - call a function once for each key in the db.
+ * @iterfunc: The function to call.
+ * @ctx: A context pointer
+ *
+ * Calls iterfunc once for each key in the database. ctx is passed
+ * unaltered to iterfunc. This function is intended to aid database dumps
+ * and statistic calculations.
+ *
+ * Returns the number of keys we iterated over.
+ */
+int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
+ void *ctx)
+{
+ int numkeys = 0;
+ struct openpgp_packet_list *packets = NULL;
+ struct openpgp_publickey *key = NULL;
+ DIR *dir;
+ char keyfile[1024];
+ int fd = -1;
+ struct dirent *curfile = NULL;
+
+ dir = opendir(config.db_dir);
+
+ if (dir != NULL) {
+ while ((curfile = readdir(dir)) != NULL) {
+ if (curfile->d_name[0] == '0' &&
+ curfile->d_name[1] == 'x') {
+ snprintf(keyfile, 1023, "%s/%s",
+ config.db_dir,
+ curfile->d_name);
+ fd = open(keyfile, O_RDONLY);
+
+ if (fd > -1) {
+ read_openpgp_stream(file_fetchchar,
+ &fd,
+ &packets,
+ 0);
+ parse_keys(packets, &key);
+
+ iterfunc(ctx, key);
+
+ free_publickey(key);
+ key = NULL;
+ free_packet_list(packets);
+ packets = NULL;
+ close(fd);
+ }
+ numkeys++;
+ }
+ }
+
+ closedir(dir);
+ dir = NULL;
+ }
+
+ return numkeys;
+}
+
/*
* Include the basic keydb routines.
*/
#define NEED_KEYID2UID 1
#define NEED_GETKEYSIGS 1
#define NEED_GETFULLKEYID 1
+#define NEED_UPDATEKEYS 1
#include "keydb.c"