19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
23
* <https://www.fukt.bsnet.se/~teddy/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
26
25
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
27
26
#include <sys/types.h> /* ssize_t */
28
27
#include <sys/stat.h> /* mkfifo(), S_IRUSR, S_IWUSR */
29
28
#include <iso646.h> /* and */
30
#include <errno.h> /* errno, EEXIST */
29
#include <errno.h> /* errno, EACCES, ENOTDIR, ELOOP,
30
ENAMETOOLONG, ENOSPC, EROFS,
31
ENOENT, EEXIST, EFAULT, EMFILE,
32
ENFILE, ENOMEM, EBADF, EINVAL, EIO,
31
34
#include <stdio.h> /* perror() */
32
#include <stdlib.h> /* EXIT_FAILURE, NULL, size_t, free(),
35
#include <stdlib.h> /* EXIT_FAILURE, NULL, size_t, free(),
33
36
realloc(), EXIT_SUCCESS */
34
37
#include <fcntl.h> /* open(), O_RDONLY */
35
38
#include <unistd.h> /* read(), close(), write(),
40
#include <sysexits.h> /* EX_OSERR, EX_OSFILE,
41
EX_UNAVAILABLE, EX_IOERR */
39
44
int main(__attribute__((unused))int argc,
45
50
const char passfifo[] = "/lib/cryptsetup/passfifo";
46
ret = (int)TEMP_FAILURE_RETRY(mkfifo(passfifo, S_IRUSR | S_IWUSR));
47
if(ret == -1 and errno != EEXIST){
51
ret = mkfifo(passfifo, S_IRUSR | S_IWUSR);
66
return EX_UNAVAILABLE; /* no "/lib/cryptsetup"? */
68
break; /* not an error */
53
int fifo_fd = (int)TEMP_FAILURE_RETRY(open(passfifo, O_RDONLY));
73
int fifo_fd = open(passfifo, O_RDONLY);
81
return EX_UNAVAILABLE;
59
94
/* Read from FIFO */
63
98
size_t buf_allocated = 0;
64
99
const size_t blocksize = 1024;
66
101
if(buf_len + blocksize > buf_allocated){
67
102
char *tmp = realloc(buf, buf_allocated + blocksize);
69
104
perror("realloc");
74
109
buf_allocated += blocksize;
76
sret = TEMP_FAILURE_RETRY(read(fifo_fd, buf + buf_len,
77
buf_allocated - buf_len));
111
sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
126
return EX_UNAVAILABLE;
83
129
buf_len += (size_t)sret;
88
TEMP_FAILURE_RETRY(close(fifo_fd));
90
136
/* Print password to stdout */
91
137
size_t written = 0;
92
138
while(written < buf_len){
93
sret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buf + written,
139
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
100
157
written += (size_t)sret;
161
ret = close(STDOUT_FILENO);
104
173
return EXIT_SUCCESS;