mirror of
https://gitlab.kit.edu/kit/scc/sys/mail/exim-encrypt-dlfunc.git
synced 2025-12-06 09:33:56 +01:00
Always read password from environment.
Factored base64-decoding into its own function.
This commit is contained in:
29
src/common.c
29
src/common.c
@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user