Initial fix-ups.

- Switch to tab indentation.
- Add error handling to script.
This commit is contained in:
Érico Rolim 2020-10-08 18:21:15 -03:00
parent 54aeb505e5
commit 13a596b7ea
2 changed files with 72 additions and 64 deletions

View File

@ -14,87 +14,87 @@
static void usage(void)
{
fputs("Usage: cbd-file lock|unlock <file>", stderr);
exit(1);
fputs("Usage: cbd-file lock|unlock <file>", stderr);
exit(1);
}
static void read_password(uint8_t *key)
{
fprintf(stderr, "input password (%d): ", INPUT_PASS_LENGTH);
fread(key, 1, INPUT_PASS_LENGTH, stdin);
fprintf(stderr, "input password (%d): ", INPUT_PASS_LENGTH);
fread(key, 1, INPUT_PASS_LENGTH, stdin);
}
static off_t file_size(int fd)
{
struct stat s = { 0 };
int rv = fstat(fd, &s);
if (rv != 0) {
fprintf(stderr, "couldn't stat file: %m\n");
exit(100);
}
return s.st_size;
struct stat s = { 0 };
int rv = fstat(fd, &s);
if (rv != 0) {
fprintf(stderr, "couldn't stat file: %m\n");
exit(100);
}
return s.st_size;
}
static ssize_t open_file_for_read(char *name, uint8_t **buffer)
{
int f = open(name, O_RDONLY);
if (f < 0) {
fputs("couldn't open file!", stderr);
exit(100);
}
int f = open(name, O_RDONLY);
if (f < 0) {
fputs("couldn't open file!", stderr);
exit(100);
}
off_t size = file_size(f);
ssize_t blocks = size / br_aes_big_BLOCK_SIZE;
if (blocks * br_aes_big_BLOCK_SIZE < size) blocks++;
fprintf(stderr, "total blocks: %lu\n", blocks);
off_t size = file_size(f);
ssize_t blocks = size / br_aes_big_BLOCK_SIZE;
if (blocks * br_aes_big_BLOCK_SIZE < size) blocks++;
fprintf(stderr, "total blocks: %lu\n", blocks);
FILE *file = fdopen(f, "r");
*buffer = calloc(blocks, br_aes_big_BLOCK_SIZE);
if (buffer == 0) {
fprintf(stderr, "couldn't allocate buffer: %m\n");
exit(101);
}
fread(*buffer, size, 1, file);
fclose(file);
FILE *file = fdopen(f, "r");
*buffer = calloc(blocks, br_aes_big_BLOCK_SIZE);
if (buffer == 0) {
fprintf(stderr, "couldn't allocate buffer: %m\n");
exit(101);
}
fread(*buffer, size, 1, file);
fclose(file);
return blocks * br_aes_big_BLOCK_SIZE;
return blocks * br_aes_big_BLOCK_SIZE;
}
int main(int argc, char **argv)
{
if (argc < 3) {
usage();
}
if (argc < 3) {
usage();
}
if (strcmp(argv[1], "lock") == 0) {
// locking code
uint8_t *buffer;
ssize_t bytes = open_file_for_read(argv[2], &buffer);
if (strcmp(argv[1], "lock") == 0) {
// locking code
uint8_t *buffer;
ssize_t bytes = open_file_for_read(argv[2], &buffer);
uint8_t key[PASS_LENGTH] = { 0 };
uint8_t iv[32] = { 0 };
read_password(key);
uint8_t key[PASS_LENGTH] = { 0 };
uint8_t iv[32] = { 0 };
read_password(key);
br_aes_big_cbcenc_keys br = { 0 };
br_aes_big_cbcenc_init(&br, key, PASS_LENGTH);
br_aes_big_cbcenc_run(&br, iv, buffer, bytes);
fwrite(buffer, 1, bytes, stdout);
} else if (strcmp(argv[1], "unlock") == 0) {
// unlocking code
uint8_t *buffer;
ssize_t bytes = open_file_for_read(argv[2], &buffer);
br_aes_big_cbcenc_keys br = { 0 };
br_aes_big_cbcenc_init(&br, key, PASS_LENGTH);
br_aes_big_cbcenc_run(&br, iv, buffer, bytes);
fwrite(buffer, 1, bytes, stdout);
} else if (strcmp(argv[1], "unlock") == 0) {
// unlocking code
uint8_t *buffer;
ssize_t bytes = open_file_for_read(argv[2], &buffer);
uint8_t key[PASS_LENGTH] = { 0 };
uint8_t iv[32] = { 0 };
read_password(key);
uint8_t key[PASS_LENGTH] = { 0 };
uint8_t iv[32] = { 0 };
read_password(key);
br_aes_big_cbcdec_keys br = { 0 };
br_aes_big_cbcdec_init(&br, key, PASS_LENGTH);
br_aes_big_cbcdec_run(&br, iv, buffer, bytes);
fwrite(buffer, 1, bytes, stdout);
} else {
usage();
}
br_aes_big_cbcdec_keys br = { 0 };
br_aes_big_cbcdec_init(&br, key, PASS_LENGTH);
br_aes_big_cbcdec_run(&br, iv, buffer, bytes);
fwrite(buffer, 1, bytes, stdout);
} else {
usage();
}
return 0;
return 0;
}

View File

@ -1,16 +1,24 @@
#!/bin/sh
file="${OTP_ACCOUNTS:-$HOME/.local/share/otp_accounts}"
json="$(@PREFIX@/libexec/cbc-file unlock "$file")"
label="$(printf %s "$json" |
jq '.[].label' |
pre_label="$(printf %s "$json" |
jq '.[].label' 2>/dev/null)"
[ -z "$pre_label" ] && exit 1
label="$(printf %s "$pre_label" |
fzf --cycle -1 ${1:+--query "$1"}
)"
[ -z "$label" ] && exit 1
secret="$(printf %s "$json" |
jq --raw-output ".[] | select (.label | contains($label)) | .secret"
)"
token="$(oathtool --totp -b -d 6 "$secret")"
printf %s $token
[ -z "$secret" ] && exit 1
if [ "$WAYLAND_DISPLAY" ]; then
printf %s $token | wl-copy
token="$(oathtool --totp -b -d 6 "$secret")"
printf %s "$token"
if [ "$WAYLAND_DISPLAY" ] && command -v wl-copy >/dev/null; then
printf %s "$token" | wl-copy
fi