From bec943d06db41e1ba2a4832fe10924a10ea91166 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 15 Jun 2020 23:57:28 +0000 Subject: [PATCH] Read key from standard input if there are no arguments --- cli.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cli.c b/cli.c index 5d060f4..3be0610 100644 --- a/cli.c +++ b/cli.c @@ -26,6 +26,8 @@ #include "mystrtonum.h" #include "otp.h" +#define LINE_SIZE 16384 + extern const char *__progname; static void @@ -42,6 +44,8 @@ int main(int argc, char *argv[]) { const char *errstr; + char *key; + size_t key_len; uint64_t counter; unsigned int step; int32_t r; @@ -99,16 +103,32 @@ main(int argc, char *argv[]) usage(); } - if (argc != 1) + if (argc > 1) usage(); + if (argc == 1) { + key = argv[0]; + key_len = strlen(key); + } else { + key = malloc(sizeof(uint8_t) * (LINE_SIZE + 1)); + if (key == NULL) + err(1, "malloc"); + if (fgets(key, LINE_SIZE + 1, stdin) == NULL) + err(1, "fgets"); + key_len = strlen(key); + if (key[key_len - 1] == '\n') { + key[key_len - 1] = '\0'; + key_len--; + } + } + if (do_hotp) { - r = hotp(hmac, argv[0], strlen(argv[0]), counter, digits); + r = hotp(hmac, key, key_len, counter, digits); if (r == -1) errx(1, "couldn't calculate HOTP"); printf("%0*" PRId32 "\n", digits, r); } else { - r = totp(hmac, argv[0], strlen(argv[0]), counter, step, digits); + r = totp(hmac, key, key_len, counter, step, digits); if (r == -1) errx(1, "couldn't calculate TOTP"); printf("%0*" PRId32 "\n", digits, r);