+int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, bool intrans)
+{
+ struct openpgp_packet_list *packets = NULL;
+ PGresult *result = NULL;
+ char *oids = NULL;
+ char statement[1024];
+ int fd = -1;
+ int i = 0;
+ int numkeys = 0;
+ Oid key_oid;
+
+ if (!intrans) {
+ result = PQexec(dbconn, "BEGIN");
+ PQclear(result);
+ }
+
+ if (keyid > 0xFFFFFFFF) {
+ snprintf(statement, 1023,
+ "SELECT keydata FROM onak_keys WHERE keyid = '%llX'",
+ keyid);
+ } else {
+ snprintf(statement, 1023,
+ "SELECT keydata FROM onak_keys WHERE keyid "
+ "LIKE '%%%llX'",
+ keyid);
+ }
+ result = PQexec(dbconn, statement);
+
+ if (PQresultStatus(result) == PGRES_TUPLES_OK) {
+ numkeys = PQntuples(result);
+ for (i = 0; i < numkeys && numkeys <= config.maxkeys; i++) {
+ oids = PQgetvalue(result, i, 0);
+ key_oid = (Oid) atoi(oids);
+
+ fd = lo_open(dbconn, key_oid, INV_READ);
+ if (fd < 0) {
+ fprintf(stderr, "Can't open large object.\n");
+ } else {
+ read_openpgp_stream(keydb_fetchchar, &fd,
+ &packets);
+ parse_keys(packets, publickey);
+ lo_close(dbconn, fd);
+ }
+ }
+ } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
+ fprintf(stderr, "Problem retrieving key from DB.\n");
+ }
+
+ PQclear(result);
+
+ if (!intrans) {
+ result = PQexec(dbconn, "COMMIT");
+ PQclear(result);
+ }
+ return (numkeys);
+}
+
+/**
+ * fetch_key_text - Trys to find the keys that contain the supplied text.
+ * @search: The text to search for.
+ * @publickey: A pointer to a structure to return the key in.
+ *
+ * This function searches for the supplied text and returns the keys that
+ * contain it.
+ */
+int fetch_key_text(const char *search, struct openpgp_publickey **publickey)