commit 5a0db17b237a65fb8f376bddc130634df9ec529c
parent f61610b133207fabc4dee4336caa5723fb56f74f
Author: lash <dev@holbrook.no>
Date: Wed, 12 Jun 2024 19:26:23 +0100
Start cli signer for ledger
Diffstat:
11 files changed, 176 insertions(+), 30 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -14,4 +14,5 @@ src/asn1/schema_entry_asn1_tab.c
src/asn1/generate_asn1
src/aux/include
src/aux/lib
+src/cmd/kee-
__pycache__
diff --git a/Makefile b/Makefile
@@ -52,3 +52,6 @@ testdata_gtk: gtk testdata
doc:
pandoc -fgfm -tplain README.md > README
+
+cmd: src
+ make -C src/cmd
diff --git a/src/aux/liblash/src/rerr/rerr.c b/src/aux/liblash/src/rerr/rerr.c
@@ -2,7 +2,7 @@
#ifdef RERR
static char** rerr[RERR_N_PFX + 1];
-static char* rerr_pfx[RERR_N_PFX + 1];
+static const char* rerr_pfx[RERR_N_PFX + 1];
char *rerr_base[3] = {
"OK",
"FAIL",
@@ -10,7 +10,7 @@ char *rerr_base[3] = {
};
#endif
-void rerr_init() {
+void rerr_init(const char *coreprefix) {
#ifdef RERR
int i;
@@ -19,7 +19,7 @@ void rerr_init() {
rerr_pfx[i] = 0x0;
}
rerr[0] = rerr_base;
- rerr_pfx[0] = 0x0;
+ rerr_pfx[0] = coreprefix;
#endif
}
@@ -45,7 +45,7 @@ static char *strv(short k, char v) {
}
#endif
-char *rerrpfx(int code) {
+const char *rerrpfx(int code) {
#ifdef RERR
short k;
char v;
diff --git a/src/aux/liblash/src/rerr/rerr.h b/src/aux/liblash/src/rerr/rerr.h
@@ -9,10 +9,10 @@
#define RERR_N_PFX 0
#endif
-void rerr_init();
+void rerr_init(const char *coreprefix);
void rerr_register(int pfx, char *label, void *start);
char* rerrstr(int code, char *buf);
char* rerrstrv(int code);
-char* rerrpfx(int code);
+const char* rerrpfx(int code);
#endif // RERR_H
diff --git a/src/cmd/Makefile b/src/cmd/Makefile
@@ -0,0 +1,9 @@
+OBJS := $(patsubst %.c,%.o,$(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-$@ $<
diff --git a/src/cmd/cmd.h b/src/cmd/cmd.h
@@ -0,0 +1 @@
+#define KEE_CLI_BUFMAX 4096
diff --git a/src/cmd/sign.c b/src/cmd/sign.c
@@ -0,0 +1,125 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "transport.h"
+#include "settings.h"
+#include "ledger.h"
+#include "debug.h"
+#include "err.h"
+#include "llog.h"
+
+#include "cmd.h"
+
+
+void debug_log(int lvl, const char *s) {
+ char *e;
+
+ //e = llog_new(lvl, (char*)s);
+ fprintf(stderr, "%s\n", s);
+}
+
+int 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 ERR_FAIL;
+ }
+ gpg_store_init(keystore, (const char*)settings->key);
+ r = gpg_store_check(keystore, passphrase);
+ if (r) {
+ return ERR_FAIL;
+ }
+
+ return ERR_OK;
+}
+
+int main(int argc, char **argv) {
+ struct kee_settings settings;
+ struct gpg_store keystore;
+ struct kee_ledger_t ledger;
+ struct kee_transport_t trans;
+ char dbg[4096];
+ char b[KEE_CLI_BUFMAX];
+ char *p;
+ int r;
+ int f;
+ long unsigned int c;
+ int l;
+
+ err_init();
+
+ settings_new_from_xdg(&settings);
+ settings_init(&settings);
+ r = unlock(&keystore, &settings, NULL);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "keyunlock fail");
+ return 1;
+ }
+
+ if (argc < 2) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "usage: kee-sign <file>");
+ return 1;
+ }
+
+ f = open(*(argv+1), O_RDONLY);
+ if (f < 0) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "argument is not a file that can be opened");
+ return 1;
+ }
+
+ l = KEE_CLI_BUFMAX;
+ p = b;
+ while (1) {
+ c = read(f, b, l);
+ if (c == 0) {
+ break;
+ }
+ p += c;
+ l -= c;
+ if (l == 0) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "read buffer overrun");
+ return 1;
+ }
+ }
+ close(f);
+
+ c = KEE_CLI_BUFMAX - l;
+ sprintf(dbg, "Read %lu bytes from %s", c, *(argv+1));
+ debug_log(DEBUG_INFO, dbg);
+
+ r = kee_transport_single(&trans, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport init fail");
+ return ERR_FAIL;
+ }
+
+ r = kee_transport_write(&trans, b, c);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "parse transport fail");
+ return ERR_FAIL;
+ }
+
+ c = KEE_CLI_BUFMAX;
+ r = kee_transport_read(&trans, b, &c);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "unwrap transport fail");
+ return ERR_FAIL;
+ }
+
+ r = kee_ledger_parse_open(&ledger, &keystore, b, c);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "not valid ledger data");
+ return 1;
+ }
+
+ sprintf(dbg, "parsed ledger: %s", ledger.content.subject);
+ debug_log(DEBUG_INFO, dbg);
+
+ return 0;
+
+}
diff --git a/src/debug.h b/src/debug.h
@@ -8,20 +8,18 @@
/**
* \brief Debug levels for simple log output.
*/
-enum debugLevel {
- /// Critical error, should terminate application.
- DEBUG_CRITICAL,
- /// Error, anomalous condition that should not occur.
- DEBUG_ERROR,
- /// Warning, condition that may contribute to affecting the state of the program adversely.
- DEBUG_WARNING,
- /// Info, events that an end-user may be interested in during normal operation.
- DEBUG_INFO,
- /// Debug, events that a developer may be intereted in during normal operation.
- DEBUG_DEBUG,
- /// Trace, mostly temporary loglines used for debugging concrete issues during development.
- DEBUG_TRACE,
-};
+/// Critical error, should terminate application.
+#define DEBUG_CRITICAL LLOG_CRITICAL
+/// Error, anomalous condition that should not occur.
+#define DEBUG_ERROR LLOG_ERROR
+/// Warning, condition that may contribute to affecting the state of the program adversely.
+#define DEBUG_WARNING LLOG_WARNING
+/// Info, events that an end-user may be interested in during normal operation.
+#define DEBUG_INFO LLOG_INFO
+/// Debug, events that a developer may be intereted in during normal operation.
+#define DEBUG_DEBUG LLOG_DEBUG
+/// Trace, mostly temporary loglines used for debugging concrete issues during development.
+#define DEBUG_TRACE LLOG_GURU
/**
*
@@ -31,7 +29,7 @@ enum debugLevel {
* \param s String to log
*/
//void debug_log(enum debugLevel level, const char *s);
-void debug_log(enum debugLevel level, const char *s);
+void debug_log(int level, const char *s);
int debug_logerr(enum lloglvl_e, int err, char *s);
#endif
diff --git a/src/err.c b/src/err.c
@@ -0,0 +1,6 @@
+#include <rerr.h>
+#include "err.h"
+
+void err_init() {
+ rerr_init("keecore");
+}
diff --git a/src/err.h b/src/err.h
@@ -1,5 +1,5 @@
-//#ifndef KEE_ERR_H_
-//#define KEE_ERR_H_
+#ifndef KEE_ERR_H_
+#define KEE_ERR_H_
//
///**
// *
@@ -7,9 +7,9 @@
// *
// */
//
-//#define ERR_OK 0x0
-//#define ERR_FAIL 0x1
-//#define ERR_UNSUPPORTED 0x2
+//#define ERR_OK 0x0000
+//#define ERR_FAIL 0x0001
+//#define ERR_UNSUPPORTED 0x0002
//
//#ifndef RERR_N_PFX
//#define RERR_N_PFX 0
@@ -18,4 +18,7 @@
//void rerr_register(int pfx, char *label, void *start);
//char* rerrstr(int code, char *buf);
//
-//#endif // _KEE_ERR_H
+
+void err_init();
+
+#endif // _KEE_ERR_H
diff --git a/src/tests/debugdebug.c b/src/tests/debugdebug.c
@@ -1,14 +1,14 @@
#include "debug.h"
-char *_rerr[2] = {
+char *_rerr_test[2] = {
"Epic Foo Fail",
"Epic Bar Fail",
};
int main() {
- rerr_init();
- rerr_register(0x100, "debugtest", _rerr);
+ rerr_init("testcore");
+ rerr_register(0x100, "debugtest", _rerr_test);
debug_logerr(LLOG_INFO, 0x101, "foo");
return ERR_OK;