kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 342ef1a01e0412f794b69202abdc008380b31801
parent 72c396ea7af595204725790b1bf377930193ad70
Author: lash <dev@holbrook.no>
Date:   Sat, 15 Jun 2024 16:05:39 +0100

Rename command to ledger, introduce flag options

Diffstat:
Msrc/cmd/cli.c | 33+++++++++++++++++++++++++++++++++
Msrc/cmd/cli.h | 7+++++++
Asrc/cmd/ledger.c | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/cmd/sign.c | 105-------------------------------------------------------------------------------
4 files changed, 178 insertions(+), 105 deletions(-)

diff --git a/src/cmd/cli.c b/src/cmd/cli.c @@ -40,6 +40,39 @@ static void cli_free(struct kee_cli_t *cli) { } } +int cli_args(struct kee_cli_t *cli, int argc, char **argv) { + int arg; + + arg = 0; + while (arg > -1) { + arg = getopt(argc, argv, "py"); + switch (arg) { + case 'p': + if (cli->act) { + return 1; + } + cli->act = ACT_PRINT; + break; + case 'y': + if (cli->act) { + return 1; + } + cli->act = ACT_SIGN; + break; + case '?': + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "invalid argument"); + break; + default: + break; + } + } + + cli->posarg = argv + optind; + cli->poslen = argc - optind; + + return 0; +} + int cli_init(struct kee_cli_t *cli, const char *passphrase) { memset(cli, 0, sizeof(struct kee_cli_t)); err_init(); diff --git a/src/cmd/cli.h b/src/cmd/cli.h @@ -6,16 +6,23 @@ #include "settings.h" #include "gpg.h" +#define ACT_PRINT 1 +#define ACT_SIGN 2 + struct kee_cli_t { struct kee_settings settings; struct gpg_store gpg; struct kee_transport_t trans; char *passphrase; char *result; + int act; + char **posarg; + int poslen; size_t result_len; }; int cli_init(struct kee_cli_t *cli, const char *passphrase); +int cli_args(struct kee_cli_t *cli, int argc, char **argv); int cli_exit(struct kee_cli_t *cli, int err); int cli_decode(struct kee_cli_t *cli, char *in, long unsigned int *in_size); int cli_encode(struct kee_cli_t *cli, char *out, long unsigned int *out_size); diff --git a/src/cmd/ledger.c b/src/cmd/ledger.c @@ -0,0 +1,138 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> + +#include "ledger.h" +#include "debug.h" +#include "transport.h" +//#include "llog.h" + +#include "cli.h" + + +void debug_log(int lvl, const char *s) { + //char *e; + + //e = llog_new(lvl, (char*)s); + fprintf(stderr, "%s\n", s); +} + + +int act_sign(struct kee_cli_t *cli, struct kee_ledger_t *ledger, char *buf) { + int r; + long unsigned int c; + + r = kee_ledger_sign(ledger, ledger->last_item, &cli->gpg, cli->passphrase); + if (r) { + debug_logerr(LLOG_CRITICAL, r, "ledger sign fail"); + return ERR_FAIL; + } + + c = KEE_CLI_BUFMAX; + r = kee_ledger_serialize_open(ledger, buf, &c); + if (r) { + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "cannot serialize ledger"); + return ERR_FAIL; + } + + r = cli_encode(cli, buf, &c); + if (r) { + return r; + } + + cli->result = buf; + cli->result_len = (size_t)c; + + return ERR_OK; +} + +int act_print(struct kee_cli_t *cli, struct kee_ledger_t *ledger, char *buf) { + return ERR_OK; +} + + +int main(int argc, char **argv) { + struct kee_ledger_t ledger; + struct kee_cli_t cli; + char dbg[4096]; + char b[KEE_CLI_BUFMAX]; + char *p; + int r; + int f; + long unsigned int c; + int l; + + r = cli_init(&cli, NULL); + if (r) { + return cli_exit(&cli, ERR_FAIL); + } + + r = cli_args(&cli, argc, argv); + if (r) { + return cli_exit(&cli, ERR_FAIL); + } + + if (cli.poslen != 1) { + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "usage: kee-sign [opts] <file>"); + return cli_exit(&cli, ERR_FAIL); + } + + f = open(*(cli.posarg), O_RDONLY); + if (f < 0) { + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "argument is not a file that can be opened"); + return cli_exit(&cli, ERR_FAIL); + } + + 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 cli_exit(&cli, ERR_FAIL); + } + } + close(f); + + c = KEE_CLI_BUFMAX - l; + sprintf(dbg, "Read %lu bytes from %s", c, *(argv+1)); + debug_log(DEBUG_INFO, dbg); + + r = cli_decode(&cli, b, &c); + if (r) { + return cli_exit(&cli, r); + } + + 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); + } + + sprintf(dbg, "parsed ledger: %s", ledger.content.subject); + debug_log(DEBUG_INFO, dbg); + + switch(cli.act) { + case ACT_SIGN: + r = act_sign(&cli, &ledger, b); + if (r) { + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "sign command fail"); + } + break; + case ACT_PRINT: + r = act_print(&cli, &ledger, b); + if (r) { + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "print command fail"); + } + default: + debug_logerr(LLOG_CRITICAL, ERR_FAIL, "invalid command"); + r = ERR_FAIL; + } + + return cli_exit(&cli, r); +} diff --git a/src/cmd/sign.c b/src/cmd/sign.c @@ -1,105 +0,0 @@ -#include <stdio.h> -#include <fcntl.h> -#include <unistd.h> - -#include "ledger.h" -#include "debug.h" -#include "transport.h" -//#include "llog.h" - -#include "cli.h" - - -void debug_log(int lvl, const char *s) { - //char *e; - - //e = llog_new(lvl, (char*)s); - fprintf(stderr, "%s\n", s); -} - - - -int main(int argc, char **argv) { - struct kee_ledger_t ledger; - struct kee_cli_t cli; - char dbg[4096]; - char b[KEE_CLI_BUFMAX]; - char *p; - int r; - int f; - long unsigned int c; - int l; - - 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>"); - return cli_exit(&cli, ERR_FAIL); - } - - 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 cli_exit(&cli, ERR_FAIL); - } - - 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 cli_exit(&cli, ERR_FAIL); - } - } - close(f); - - c = KEE_CLI_BUFMAX - l; - sprintf(dbg, "Read %lu bytes from %s", c, *(argv+1)); - debug_log(DEBUG_INFO, dbg); - - r = cli_decode(&cli, b, &c); - if (r) { - return cli_exit(&cli, r); - } - - 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); - } - - sprintf(dbg, "parsed ledger: %s", ledger.content.subject); - debug_log(DEBUG_INFO, dbg); - - 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); - } - - c = KEE_CLI_BUFMAX; - r = kee_ledger_serialize_open(&ledger, b, &c); - if (r) { - debug_logerr(LLOG_CRITICAL, ERR_FAIL, "cannot serialize ledger"); - return cli_exit(&cli, ERR_FAIL); - } - - r = cli_encode(&cli, b, &c); - if (r) { - return cli_exit(&cli, r); - } - - cli.result = b; - cli.result_len = (size_t)c; - - return cli_exit(&cli, 0); -}