piping password to veracrypt (Linux)
Hello!
To simplify mounting multiple veracrypt volumes with the same password, I wrote a little C program, which prompts for the password and then pipes it to multiple veracrypt instances. The program works but I'd like to get some technical advice from this community to ensure that the program works as intended and to make a few improvements:
1. I wonder, whether the entered password remains anywhere in the storage after the execution of the program is finished (this is the whole point of this code, because otherwise I could have used just a shell script).
2. In the current state the password is read and passed to veracrypt, which is executed in the background. This was not my intention, since I don't have any control about the success of the operation nor about it's duration. I tried waiting (int status; waitpid(pid, &status, 0); ) (line 72) for the first forked process (which forks into the others), in order to continue the execution until the volumes are mounted. But for some reason the piping the password stopped working correctly then (without any error message).
3. When [2] is solved, I'd like to condition the further excecution/output on the the return value of the veracrypt process. How do I get this value?
Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define READ_END 0
#define WRITE_END 1
int main(int argc, char* argv[])
{
if(argc > 1 && argc%2==1)
{
int inst = (argc-1)/2;
int fd[inst][2];
pid_t pid;
char scmd[] = "veracrypt";
for(int i = 0; i<inst;i++){
pipe(fd[i]);
}
pid = fork();
if(pid==0)
{
freopen("/dev/null", "w", stdout);
int j=0;
dup2(fd[0][READ_END], STDIN_FILENO);
close(fd[0][WRITE_END]);
close(fd[0][READ_END]);
bool b = true;
for(int i=0;i<inst-1 && b;i++){
pid = fork();
if(pid==0){
j = i+1;
dup2(fd[j][READ_END], STDIN_FILENO);
close(fd[j][WRITE_END]);
close(fd[j][READ_END]);
}
else{
j = i;
b = false;
}
}
execlp(scmd, scmd, "-t","--protect-hidden=no","--keyfiles=","--pim=123",argv[j*2+1],argv[j*2+2],(char*) NULL);
fprintf(stderr, "Failed to execute '%s'\n", scmd);
exit(1);
}
else
{
char *buffer = getpass("Veracrypt Password:");
for(int i=0;i<inst;i++)
{
close(fd[i][READ_END]);
write(fd[i][WRITE_END], buffer, (strlen(buffer)+1));
}
for(int i=0;i<inst;i++){close(fd[i][WRITE_END]);}
srand(time(0));
for(int i = 0; i<strlen(buffer); i++) buffer[i] = rand();
free(buffer);
}
}
return(0);
}
Thank you very much,
oneone