* 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>
#include "keystructs.h"
#include "log.h"
#include "mem.h"
+#include "onak.h"
#include "onak-conf.h"
#include "parsekey.h"
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.
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));
}
static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
- uint8_t *fp, size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey,
bool intrans)
{
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;
- if (fpsize > MAX_FINGERPRINT_LEN) {
+ if (fingerprint->length > MAX_FINGERPRINT_LEN) {
return 0;
}
- write(keyd_fd, &cmd, sizeof(cmd));
- read(keyd_fd, &cmd, sizeof(cmd));
- if (cmd == KEYD_REPLY_OK) {
- size = fpsize;
+ if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_FP)) {
+ size = fingerprint->length;
write(keyd_fd, &size, sizeof(size));
- write(keyd_fd, fp, size);
+ write(keyd_fd, fingerprint->fp, size);
keybuf.offset = 0;
read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
if (keybuf.size > 0) {
uint64_t keyid, 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) {
+ if (keyd_send_cmd(keyd_fd, KEYD_CMD_DELETE)) {
write(keyd_fd, &keyid, sizeof(keyid));
}
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.");
}
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);
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);
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));
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) {
+ if (keyd_send_cmd(keyd_fd, KEYD_CMD_GETFULLKEYID)) {
write(keyd_fd, &keyid, sizeof(keyid));
read(keyd_fd, &cmd, sizeof(cmd));
if (cmd != sizeof(keyid)) {
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) {
logthing(LOGTHING_NOTICE, "Error closing down socket: %d",
errno);
}
- keyd_fd = -1;
free(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;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (keyd_fd < 0) {
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,
}
count = read(keyd_fd, &reply, sizeof(reply));
+ if (count != sizeof(reply)) {
+ logthing(LOGTHING_CRITICAL,
+ "Error! Unexpected keyd version "
+ "length: %d != %d",
+ count, sizeof(reply));
+ exit(EXIT_FAILURE);
+ }
logthing(LOGTHING_DEBUG,
"keyd protocol version %d",
reply);