Skip to content

Commit

Permalink
Initial fix-ups.
Browse files Browse the repository at this point in the history
- Switch to tab indentation.
- Add error handling to script.
  • Loading branch information
ericonr committed Oct 8, 2020
1 parent 54aeb50 commit 13a596b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
134 changes: 67 additions & 67 deletions cbc-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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);

return blocks * br_aes_big_BLOCK_SIZE;
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);

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;
}

int main(int argc, char **argv)
{
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);

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);

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();
}

return 0;
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);

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);

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();
}

return 0;
}
18 changes: 13 additions & 5 deletions get-otp.in
Original file line number Diff line number Diff line change
@@ -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"
)"
[ -z "$secret" ] && exit 1

token="$(oathtool --totp -b -d 6 "$secret")"
printf %s $token
printf %s "$token"

if [ "$WAYLAND_DISPLAY" ]; then
printf %s $token | wl-copy
if [ "$WAYLAND_DISPLAY" ] && command -v wl-copy >/dev/null; then
printf %s "$token" | wl-copy
fi

0 comments on commit 13a596b

Please sign in to comment.