commit a7c454679a92a4bc6fbc9d4442cf7c9cb173435b
parent f26fe4151792acf8e50364a0cfc249d357784513
Author: lash <dev@holbrook.no>
Date: Thu, 13 Jun 2024 14:47:29 +0100
Move settings init, unlock into cli lib
Diffstat:
4 files changed, 78 insertions(+), 64 deletions(-)
diff --git a/src/cmd/Makefile b/src/cmd/Makefile
@@ -1,9 +1,12 @@
-OBJS := $(patsubst %.c,%.o,$(wildcard *.c))
+OBJS := $(patsubst %.c,%.o,$(filter-out cli.c,$(wildcard *.c)))
LINKOBJS := $(wildcard ../*.o)
INCLUDES := -I.. -I../aux/include
CFLAGS += `pkg-config --cflags zbar` $(INCLUDES) -Wall -DRERR -DRERR_N_PREFIX=2
LIBS := `pkg-config --libs zlib lmdb libgcrypt libxdg-basedir libqrencode zbar` -lb64 -llash -ltasn1 -lcmime -lldap -L../aux/lib
LDFLAGS += $(LIBS)
-all: $(OBJS)
- gcc $(CFLAGS) $(LIBS) $(LINKOBJS) -o kee-$@ $<
+all: $(OBJS) cliobj
+ gcc $(CFLAGS) $(LIBS) $(LINKOBJS) cli.o -o kee-$@ $<
+
+cliobj:
+ gcc $(CFLAGS) -c cli.c -o cli.o
diff --git a/src/cmd/cli.c b/src/cmd/cli.c
@@ -0,0 +1,57 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include "gpg.h"
+#include "err.h"
+#include "debug.h"
+
+#include "cli.h"
+
+
+static char* unlock(struct gpg_store *keystore, struct kee_settings *settings, char *passphrase) {
+ int r;
+
+ if (passphrase == NULL) {
+ passphrase = getenv("KEE_PASSPHRASE");
+ }
+ if (passphrase == NULL || strlen(passphrase) == 0) {
+ return NULL;
+ }
+ gpg_store_init(keystore, (const char*)settings->key);
+ r = gpg_store_check(keystore, passphrase);
+ if (r) {
+ return NULL;
+ }
+
+ return passphrase;
+}
+
+static void cli_set_passphrase(struct kee_cli_t *cli, const char *passphrase) {
+ cli->passphrase = malloc(cli->gpg.passphrase_digest_len);
+ gpg_store_digest(&cli->gpg, cli->passphrase, passphrase);
+}
+
+static void cli_free(struct kee_cli_t *cli) {
+ if (cli->passphrase) {
+ free(cli->passphrase);
+ }
+}
+
+int cli_init(struct kee_cli_t *cli, const char *passphrase) {
+ memset(cli, 0, sizeof(struct kee_cli_t));
+ err_init();
+ settings_new_from_xdg(&cli->settings);
+ settings_init(&cli->settings);
+ passphrase = unlock(&cli->gpg, &cli->settings, NULL);
+ if (passphrase == NULL) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "keyunlock fail");
+ return ERR_FAIL;
+ }
+ cli_set_passphrase(cli, passphrase);
+ return ERR_OK;
+}
+
+int cli_exit(struct kee_cli_t *cli, int err) {
+ cli_free(cli);
+ return err;
+}
diff --git a/src/cmd/cli.h b/src/cmd/cli.h
@@ -3,12 +3,16 @@
#define KEE_CLI_BUFMAX 4096
+#include "settings.h"
+#include "gpg.h"
+
struct kee_cli_t {
+ struct kee_settings settings;
+ struct gpg_store gpg;
char *passphrase;
};
-void cli_init(struct kee_cli_t *cli);
-void cli_set_passphrase(struct kee_cli_t *cli, struct gpg_store *keystore, const char *passphrase);
+int cli_init(struct kee_cli_t *cli, const char *passphrase);
int cli_exit(struct kee_cli_t *cli, int err);
#endif // KEE_CLI_H_
diff --git a/src/cmd/sign.c b/src/cmd/sign.c
@@ -1,15 +1,11 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include "transport.h"
-#include "settings.h"
#include "ledger.h"
#include "debug.h"
-#include "err.h"
-#include "llog.h"
+#include "transport.h"
+//#include "llog.h"
#include "cli.h"
@@ -21,52 +17,12 @@ void debug_log(int lvl, const char *s) {
fprintf(stderr, "%s\n", s);
}
-void cli_init(struct kee_cli_t *cli) {
- memset(cli, 0, sizeof(struct kee_cli_t));
- err_init();
-}
-
-void cli_set_passphrase(struct kee_cli_t *cli, struct gpg_store *keystore, const char *passphrase) {
- cli->passphrase = malloc(keystore->passphrase_digest_len);
- gpg_store_digest(keystore, cli->passphrase, passphrase);
-}
-
-static void cli_free(struct kee_cli_t *cli) {
- if (cli->passphrase) {
- free(cli->passphrase);
- }
-}
-
-int cli_exit(struct kee_cli_t *cli, int err) {
- cli_free(cli);
- return err;
-}
-
-char* unlock(struct gpg_store *keystore, struct kee_settings *settings, char *passphrase) {
- int r;
-
- if (passphrase == NULL) {
- passphrase = getenv("KEE_PASSPHRASE");
- }
- if (passphrase == NULL || strlen(passphrase) == 0) {
- return NULL;
- }
- gpg_store_init(keystore, (const char*)settings->key);
- r = gpg_store_check(keystore, passphrase);
- if (r) {
- return NULL;
- }
- return passphrase;
-}
int main(int argc, char **argv) {
- struct kee_settings settings;
- struct gpg_store keystore;
- struct kee_ledger_t ledger;
struct kee_transport_t trans;
+ struct kee_ledger_t ledger;
struct kee_cli_t cli;
- char *passphrase;
char dbg[4096];
char b[KEE_CLI_BUFMAX];
char *p;
@@ -75,16 +31,10 @@ int main(int argc, char **argv) {
long unsigned int c;
int l;
- cli_init(&cli);
-
- settings_new_from_xdg(&settings);
- settings_init(&settings);
- passphrase = unlock(&keystore, &settings, NULL);
- if (passphrase == NULL) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "keyunlock fail");
- return ERR_FAIL;
- }
- cli_set_passphrase(&cli, &keystore, passphrase);
+ r = cli_init(&cli, NULL);
+ if (r) {
+ return cli_exit(&cli, ERR_FAIL);
+ }
if (argc < 2) {
debug_logerr(LLOG_CRITICAL, ERR_FAIL, "usage: kee-sign <file>");
@@ -136,7 +86,7 @@ int main(int argc, char **argv) {
return cli_exit(&cli, ERR_FAIL);
}
- r = kee_ledger_parse_open(&ledger, &keystore, b, c);
+ r = kee_ledger_parse_open(&ledger, &cli.gpg, b, c);
if (r) {
debug_logerr(LLOG_CRITICAL, ERR_FAIL, "not valid ledger data");
return cli_exit(&cli, ERR_FAIL);
@@ -145,7 +95,7 @@ int main(int argc, char **argv) {
sprintf(dbg, "parsed ledger: %s", ledger.content.subject);
debug_log(DEBUG_INFO, dbg);
- r = kee_ledger_sign(&ledger, ledger.last_item, &keystore, cli.passphrase);
+ r = kee_ledger_sign(&ledger, ledger.last_item, &cli.gpg, cli.passphrase);
if (r) {
debug_logerr(LLOG_CRITICAL, r, "ledger sign fail");
return cli_exit(&cli, ERR_FAIL);