Add support for specifying the input and output file

This commit is contained in:
Juhani Krekelä 2021-04-09 23:00:56 +03:00
parent 5393e64c18
commit 41a74402f0
1 changed files with 37 additions and 12 deletions

49
puer.c
View File

@ -543,8 +543,8 @@ ssize_t passphrase_prompt(unsigned char *passphrase, size_t size, const char *pr
return index - 1;
}
ssize_t passphrase_file(char *passfile, unsigned char passphrase[], size_t size) {
int file = open(passfile, O_RDONLY);
ssize_t passphrase_file(char *passfilepath, unsigned char passphrase[], size_t size) {
int file = open(passfilepath, O_RDONLY);
// Read until newline
size_t index = 0;
@ -579,21 +579,25 @@ ssize_t passphrase_file(char *passfile, unsigned char passphrase[], size_t size)
}
void usage(char *name) {
fprintf(stderr, "Usage: %s -d | -e [-f] [-p passfile]\n\n", name);
fprintf(stderr, "-d Decrypt\n");
fprintf(stderr, "-e Encrypt\n");
fprintf(stderr, "-f Force output to terminal\n");
fprintf(stderr, "-p Read passphrase from a file instead of the terminal.\n");
fprintf(stderr, "Usage: %s -d | -e [-f] [-p passfile] [-i infile] [-o outfile]\n\n", name);
fprintf(stderr, "-d Decrypt\n");
fprintf(stderr, "-e Encrypt\n");
fprintf(stderr, "-f Force output to terminal\n");
fprintf(stderr, "-p passfile Read passphrase from a file instead of the terminal.\n");
fprintf(stderr, "-i infile Read from a file instead of the terminal.\n");
fprintf(stderr, "-o outfile Write to a file instead of the terminal.\n");
}
int main(int argc, char *argv[]) {
bool encrypting = false;
bool decrypting = false;
bool force = false;
char *passfile = NULL;
char *passfilepath = NULL;
char *infilepath = NULL;
char *outfilepath = NULL;
int opt;
while ((opt = getopt(argc, argv, "defp:")) != -1) {
while ((opt = getopt(argc, argv, "defp:i:o:")) != -1) {
switch (opt) {
case 'd':
decrypting = true;
@ -605,7 +609,13 @@ int main(int argc, char *argv[]) {
force = true;
break;
case 'p':
passfile = optarg;
passfilepath = optarg;
break;
case 'i':
infilepath = optarg;
break;
case 'o':
outfilepath = optarg;
break;
default:
usage(argv[0]);
@ -626,6 +636,21 @@ int main(int argc, char *argv[]) {
FILE *infile = stdin;
FILE *outfile = stdout;
if (infilepath != NULL) {
infile = fopen(infilepath, "r");
if (infile == NULL) {
perror("Failed to open input file");
exit(1);
}
}
if (outfilepath != NULL) {
outfile = fopen(outfilepath, "w");
if (outfile == NULL) {
perror("Failed to open output file");
exit(1);
}
}
if (encrypting && !force && isatty(fileno(outfile))) {
fprintf(stderr, "Refusing to print encrypted (binary) data to terminal. Use -f to force output.\n");
exit(1);
@ -655,7 +680,7 @@ int main(int argc, char *argv[]) {
// Read passphrase
unsigned char passphrase[128];
ssize_t passphrase_len;
if (passfile == NULL) {
if (passfilepath == NULL) {
// Read from terminal if no passfile specified
passphrase_len = passphrase_prompt(passphrase, sizeof(passphrase), "passphrase: ");
if (passphrase_len == -1) {
@ -683,7 +708,7 @@ int main(int argc, char *argv[]) {
explicit_bzero(confirm, sizeof(confirm));
}
} else {
passphrase_len = passphrase_file(passfile, passphrase, sizeof(passphrase));
passphrase_len = passphrase_file(passfilepath, passphrase, sizeof(passphrase));
if (passphrase_len == -1) {
explicit_bzero(passphrase, sizeof(passphrase));
exit(1);