aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/backend-support.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2019-08-06 14:07:33 +0000
committerWerner Koch <[email protected]>2019-08-06 14:07:33 +0000
commit5ea6250cc5761612d17ca4fb34eed096f26e2826 (patch)
tree062877d21a2f53ff01918a2a73ed02fcf80fded1 /kbx/backend-support.c
parentkbx: Allow writing using a estream. (diff)
downloadgnupg-5ea6250cc5761612d17ca4fb34eed096f26e2826.tar.gz
gnupg-5ea6250cc5761612d17ca4fb34eed096f26e2826.zip
kbx: Add framework for the SEARCH command
* kbx/backend-kbx.c: New. * kbx/backend-support.c: New. * kbx/backend.h: New. * kbx/frontend.c: New. * kbx/frontend.h: New. * kbx/kbxserver.c: Implement SEARCH and NEXT command. * kbx/keybox-search-desc.h (enum pubkey_types): New. * kbx/keybox-search.c (keybox_get_data): New. * kbx/keyboxd.c (main): Add a standard resource. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'kbx/backend-support.c')
-rw-r--r--kbx/backend-support.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/kbx/backend-support.c b/kbx/backend-support.c
new file mode 100644
index 000000000..28b51875c
--- /dev/null
+++ b/kbx/backend-support.c
@@ -0,0 +1,128 @@
+/* backend-support.c - Supporting functions for the backend.
+ * Copyright (C) 2019 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ * SPDX-License-Identifier: GPL-3.0+
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "keyboxd.h"
+#include "../common/i18n.h"
+#include "../common/asshelp.h"
+#include "backend.h"
+
+
+/* Common definition part of all backend handle. */
+struct backend_handle_s
+{
+ enum database_types db_type;
+};
+
+
+
+/* Return a string with the name of the database type T. */
+const char *
+strdbtype (enum database_types t)
+{
+ switch (t)
+ {
+ case DB_TYPE_NONE: return "none";
+ case DB_TYPE_KBX: return "keybox";
+ }
+ return "?";
+}
+
+
+/* Return a new backend ID. Backend IDs are used to identify backends
+ * without using the actual object. The number of backend resources
+ * is limited because they are specified in the config file. Thus an
+ * overflow check is not required. */
+unsigned int
+be_new_backend_id (void)
+{
+ static unsigned int last;
+
+ return ++last;
+}
+
+
+/* Release the backend described by HD. This is a generic function
+ * which dispatches to the the actual backend. */
+void
+be_generic_release_backend (ctrl_t ctrl, backend_handle_t hd)
+{
+ if (!hd)
+ return;
+ switch (hd->db_type)
+ {
+ case DB_TYPE_NONE:
+ xfree (hd);
+ break;
+ case DB_TYPE_KBX:
+ be_kbx_release_resource (ctrl, hd);
+ break;
+ default:
+ log_error ("%s: faulty backend handle of type %d given\n",
+ __func__, hd->db_type);
+ }
+}
+
+
+/* Release the request object REQ. */
+void
+be_release_request (db_request_t req)
+{
+ db_request_part_t part, partn;
+
+ if (!req)
+ return;
+
+ for (part = req->part; part; part = partn)
+ {
+ partn = part->next;
+ be_kbx_release_kbx_hd (part->kbx_hd);
+ xfree (part);
+ }
+}
+
+
+/* Return the public key (BUFFER,BUFLEN) which has the type
+ * PUBVKEY_TYPE to the caller. Owenership of BUFFER is taken by thgis
+ * function even in the error case. */
+gpg_error_t
+be_return_pubkey (ctrl_t ctrl, void *buffer, size_t buflen,
+ enum pubkey_types pubkey_type)
+{
+ gpg_error_t err;
+
+ err = status_printf (ctrl, "PUBKEY_TYPE", "%d", pubkey_type);
+ if (err)
+ goto leave;
+
+ if (ctrl->no_data_return)
+ err = 0;
+ else
+ err = kbxd_write_data_line(ctrl, buffer, buflen);
+
+ leave:
+ xfree (buffer);
+ return err;
+}