*
* Jonathan McDowell <noodles@earth.li>
*
- * Copyright 2002 Project Purple
- *
- * $Id: keydb_pg.c,v 1.12 2003/06/08 21:11:01 noodles Exp $
+ * Copyright 2002-2004 Project Purple
*/
#include <postgresql/libpq-fe.h>
#include <postgresql/libpq/libpq-fs.h>
-//#include <libpq-fe.h>
-//#include <libpq/libpq-fs.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <errno.h>
*/
static int keydb_fetchchar(void *fd, size_t count, unsigned char *c)
{
- return (!lo_read(dbconn, *(int *) fd, c, count));
+ return (!lo_read(dbconn, *(int *) fd, (char *) c, count));
}
/**
*/
static int keydb_putchar(void *fd, size_t count, unsigned char *c)
{
- return !(lo_write(dbconn, *(int *) fd, c, count));
+ return !(lo_write(dbconn, *(int *) fd, (char *) c, count));
}
/**
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-void initdb(void)
+void initdb(bool readonly)
{
dbconn = PQsetdbLogin(config.pg_dbhost, // host
NULL, // port
"Can't open large object.");
} else {
read_openpgp_stream(keydb_fetchchar, &fd,
- &packets);
+ &packets, 0);
parse_keys(packets, publickey);
lo_close(dbconn, fd);
free_packet_list(packets);
"Can't open large object.");
} else {
read_openpgp_stream(keydb_fetchchar, &fd,
- &packets);
+ &packets,
+ 0);
parse_keys(packets, publickey);
lo_close(dbconn, fd);
free_packet_list(packets);
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)
+{
+ struct openpgp_packet_list *packets = NULL;
+ struct openpgp_publickey *key = NULL;
+ PGresult *result = NULL;
+ char *oids = NULL;
+ char statement[1024];
+ int fd = -1;
+ int i = 0;
+ int numkeys = 0;
+ Oid key_oid;
+
+ result = PQexec(dbconn, "SELECT keydata FROM onak_keys;");
+
+ if (PQresultStatus(result) == PGRES_TUPLES_OK) {
+ numkeys = PQntuples(result);
+ for (i = 0; i < numkeys; i++) {
+ oids = PQgetvalue(result, i, 0);
+ key_oid = (Oid) atoi(oids);
+
+ fd = lo_open(dbconn, key_oid, INV_READ);
+ if (fd < 0) {
+ logthing(LOGTHING_ERROR,
+ "Can't open large object.");
+ } else {
+ read_openpgp_stream(keydb_fetchchar, &fd,
+ &packets, 0);
+ parse_keys(packets, key);
+ lo_close(dbconn, fd);
+
+ iterfunc(ctx, key);
+
+ free_publickey(key);
+ key = NULL;
+ free_packet_list(packets);
+ packets = NULL;
+ }
+ }
+ } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
+ logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
+ }
+
+ PQclear(result);
+
+ return (numkeys);
+}
+
/*
* Include the basic keydb routines.
*/
#define NEED_GETFULLKEYID 1
+#define NEED_UPDATEKEYS 1
#include "keydb.c"