]> the.earth.li Git - onak.git/blobdiff - keydb_keyd.c
Set Rules-Requires-Root to no
[onak.git] / keydb_keyd.c
index d05c7f8998e2008455ce20fa6d27eb717936a075..e8f9961b79a6d2f9364ec53e6dd42abe70953ec5 100644 (file)
  * more details.
  *
  * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdbool.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -34,6 +35,7 @@
 #include "keystructs.h"
 #include "log.h"
 #include "mem.h"
+#include "onak.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 
@@ -59,6 +61,28 @@ static void keyd_endtrans(struct onak_dbctx *dbctx)
        return;
 }
 
+static bool keyd_send_cmd(int fd, enum keyd_ops _cmd)
+{
+       uint32_t cmd = _cmd;
+       ssize_t bytes;
+
+       bytes = write(fd, &cmd, sizeof(cmd));
+       if (bytes != sizeof(cmd)) {
+               return false;
+       }
+
+       bytes = read(fd, &cmd, sizeof(cmd));
+       if (bytes != sizeof(cmd)) {
+               return false;
+       }
+
+       if (cmd != KEYD_REPLY_OK) {
+               return false;
+       }
+
+       return true;
+}
+
 /**
  *     fetch_key - Given a keyid fetch the key from storage.
  *     @keyid: The keyid to fetch.
@@ -78,13 +102,10 @@ static int keyd_fetch_key_id(struct onak_dbctx *dbctx,
        int keyd_fd = (intptr_t) dbctx->priv;
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GET_ID;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_ID)) {
                write(keyd_fd, &keyid, sizeof(keyid));
                keybuf.offset = 0;
                read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
@@ -123,7 +144,6 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
        int keyd_fd = (intptr_t) dbctx->priv;
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GET_FP;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
        uint8_t                     size;
@@ -132,9 +152,7 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
                return 0;
        }
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_FP)) {
                size = fingerprint->length;
                write(keyd_fd, &size, sizeof(size));
                write(keyd_fd, fingerprint->fp, size);
@@ -169,22 +187,19 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
 
 /**
 *      delete_key - Given a keyid delete the key from storage.
-*      @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
 *      @intrans: If we're already in a transaction.
 *
 *      This function deletes a public key from whatever storage mechanism we
 *      are using. Returns 0 if the key existed.
 */
 static int keyd_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        int keyd_fd = (intptr_t) dbctx->priv;
-       uint32_t cmd = KEYD_CMD_DELETE;
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
-               write(keyd_fd, &keyid, sizeof(keyid));
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_DELETE)) {
+               write(keyd_fd, fp, sizeof(*fp));
        }
 
        return 0;
@@ -213,8 +228,8 @@ static int keyd_store_key(struct onak_dbctx *dbctx,
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct openpgp_publickey   *next = NULL;
-       uint32_t                    cmd = KEYD_CMD_STORE;
        uint64_t                    keyid;
+       enum keyd_ops               cmd = KEYD_CMD_STORE;
 
        if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
                logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
@@ -222,12 +237,10 @@ static int keyd_store_key(struct onak_dbctx *dbctx,
        }
 
        if (update) {
-               keyd_delete_key(dbctx, keyid, false);
+               cmd = KEYD_CMD_UPDATE;
        }
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, cmd)) {
                keybuf.offset = 0;
                keybuf.size = 8192;
                keybuf.buffer = malloc(keybuf.size);
@@ -269,13 +282,10 @@ static int keyd_fetch_key_text(struct onak_dbctx *dbctx,
        int keyd_fd = (intptr_t) dbctx->priv;
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GET_TEXT;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_TEXT)) {
                bytes = strlen(search);
                write(keyd_fd, &bytes, sizeof(bytes));
                write(keyd_fd, search, bytes);
@@ -317,13 +327,10 @@ static int keyd_fetch_key_skshash(struct onak_dbctx *dbctx,
        int keyd_fd = (intptr_t) dbctx->priv;
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       uint32_t                    cmd = KEYD_CMD_GET_SKSHASH;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_SKSHASH)) {
                write(keyd_fd, hash->hash, sizeof(hash->hash));
                keybuf.offset = 0;
                read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
@@ -354,33 +361,6 @@ static int keyd_fetch_key_skshash(struct onak_dbctx *dbctx,
        return (count > 0) ? 1 : 0;
 }
 
-
-/**
- *     getfullkeyid - Maps a 32bit key id to a 64bit one.
- *     @keyid: The 32bit keyid.
- *
- *     This function maps a 32bit key id to the full 64bit one. It returns the
- *     full keyid. If the key isn't found a keyid of 0 is returned.
- */
-static uint64_t keyd_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
-{
-       int keyd_fd = (intptr_t) dbctx->priv;
-       uint32_t cmd = KEYD_CMD_GETFULLKEYID;
-
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
-               write(keyd_fd, &keyid, sizeof(keyid));
-               read(keyd_fd, &cmd, sizeof(cmd));
-               if (cmd != sizeof(keyid)) {
-                       return 0;
-               }
-               read(keyd_fd, &keyid, sizeof(keyid));
-       }
-
-       return keyid;
-}
-
 /**
  *     iterate_keys - call a function once for each key in the db.
  *     @iterfunc: The function to call.
@@ -400,14 +380,11 @@ static int keyd_iterate_keys(struct onak_dbctx *dbctx,
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_publickey   *key = NULL;
-       uint32_t                    cmd = KEYD_CMD_KEYITER;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
        int                         numkeys = 0;
 
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
+       if (keyd_send_cmd(keyd_fd, KEYD_CMD_KEYITER)) {
                keybuf.offset = 0;
                read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
                while (keybuf.size > 0) {
@@ -503,7 +480,7 @@ static void keyd_cleanupdb(struct onak_dbctx *dbctx)
  *     this file are called in order to allow the DB to be initialized ready
  *     for access.
  */
-struct onak_dbctx *keydb_keyd_init(bool readonly)
+struct onak_dbctx *keydb_keyd_init(struct onak_db_config *dbcfg, bool readonly)
 {
        struct sockaddr_un sock;
        uint32_t           cmd = KEYD_CMD_UNKNOWN;
@@ -516,6 +493,7 @@ struct onak_dbctx *keydb_keyd_init(bool readonly)
        if (dbctx == NULL) {
                return NULL;
        }
+       dbctx->config = dbcfg;
 
        keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (keyd_fd < 0) {
@@ -528,7 +506,7 @@ struct onak_dbctx *keydb_keyd_init(bool readonly)
 
        sock.sun_family = AF_UNIX;
        snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
-                       config.db_dir,
+                       config.sock_dir,
                        KEYD_SOCKET);
        if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
                logthing(LOGTHING_CRITICAL,
@@ -591,7 +569,6 @@ struct onak_dbctx *keydb_keyd_init(bool readonly)
        dbctx->getkeysigs               = generic_getkeysigs;
        dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
        dbctx->keyid2uid                = generic_keyid2uid;
-       dbctx->getfullkeyid             = keyd_getfullkeyid;
        dbctx->iterate_keys             = keyd_iterate_keys;
 
        return dbctx;