Always read password from environment.

Factored base64-decoding into its own function.
This commit is contained in:
Heiko Reese
2021-09-11 00:24:44 +02:00
parent 0b01283cd9
commit 41e7c43ab8
2 changed files with 62 additions and 48 deletions

View File

@ -2,15 +2,17 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sodium.h>
#include "common.h"
char * read_first_line(const char * filename) {
char *read_first_line(const char *filename) {
int fd;
char *endptr;
char *cipherstring;
// open file
fd = open(filename, O_RDONLY, (mode_t)0600);
fd = open(filename, O_RDONLY, (mode_t) 0600);
if (fd == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
@ -41,8 +43,7 @@ char * read_first_line(const char * filename) {
strncpy(cipherstring, map, cipherstring_len);
// munmap and close file
if (munmap(map, fileInfo.st_size) == -1)
{
if (munmap(map, fileInfo.st_size) == -1) {
close(fd);
perror("Error un-mmapping the file");
exit(EXIT_FAILURE);
@ -50,4 +51,24 @@ char * read_first_line(const char * filename) {
close(fd);
return cipherstring;
}
typedef struct {
unsigned char *string;
size_t length;
} Password;
Password base64_decode_string(const char *input) {
Password p;
size_t input_len = strlen(input);
size_t outmaxlen = input_len / 4 * 3;
p.string = malloc(outmaxlen);
int b64err = sodium_base642bin(p.string, outmaxlen, (const char *) input, input_len,
NULL, &p.length, NULL, sodium_base64_VARIANT_ORIGINAL);
if (b64err != 0) {
fprintf(stderr, "[ERROR] Unable to base64-decode the password\n");
exit(EXIT_FAILURE);
}
return p;
}