+ fprintf(stderr, "Problem retrieving key from DB.\n");
+ }
+
+ PQclear(result);
+
+ 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)
+{
+ 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;
+ char *dodgychar = NULL;
+
+ result = PQexec(dbconn, "BEGIN");
+ PQclear(result);
+
+ /*
+ * TODO: We really want to use PQescapeString, but this isn't supported
+ * by the version of Postgresql in Debian Stable. Roll on Woody and for
+ * now kludge it.
+ */
+ dodgychar = strchr(search, '\'');
+ while (dodgychar != NULL) {
+ *dodgychar = ' ';
+ dodgychar = strchr(search, '\'');
+ }
+ dodgychar = strchr(search, '\\');
+ while (dodgychar != NULL) {
+ *dodgychar = ' ';
+ dodgychar = strchr(search, '\\');
+ }
+
+
+ snprintf(statement, 1023,
+ "SELECT DISTINCT onak_keys.keydata FROM onak_keys, "
+ "onak_uids WHERE onak_keys.keyid = onak_uids.keyid "
+ "AND onak_uids.uid LIKE '%%%s%%'",
+ search);
+ 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");