1
#define _GNU_SOURCE /* asprintf() */
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
sigemptyset(), sigaddset(),
4
sigaction, SIGINT, SIG_IGN, SIGHUP,
5
SIGTERM, kill(), SIGKILL */
6
#include <stddef.h> /* NULL */
7
#include <stdlib.h> /* getenv() */
8
#include <stdio.h> /* asprintf(), perror() */
9
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS,
11
#include <sys/types.h> /* pid_t, DIR, struct dirent,
13
#include <dirent.h> /* opendir(), readdir(), closedir() */
14
#include <unistd.h> /* readlink(), fork(), execl(),
16
#include <string.h> /* memcmp() */
17
#include <iso646.h> /* and */
18
#include <stdbool.h> /* bool, false, true */
19
#include <errno.h> /* errno */
20
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
22
#include <fcntl.h> /* open(), O_RDONLY */
24
sig_atomic_t interrupted_by_signal = 0;
26
static void termination_handler(__attribute__((unused))int signum){
27
interrupted_by_signal = 1;
30
int main(__attribute__((unused))int argc,
31
__attribute__((unused))char **argv){
34
bool an_error_occured = false;
36
/* Create prompt string */
39
const char *const cryptsource = getenv("cryptsource");
40
const char *const crypttarget = getenv("crypttarget");
41
const char *const prompt_start = "Enter passphrase to unlock the disk";
43
if(cryptsource == NULL){
44
if(crypttarget == NULL){
45
ret = asprintf(&prompt, "%s: ", prompt_start);
47
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
51
if(crypttarget == NULL){
52
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
54
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
55
cryptsource, crypttarget);
63
/* Find usplash process */
64
pid_t usplash_pid = 0;
66
size_t cmdline_len = 0;
68
const char usplash_name[] = "/sbin/usplash";
69
DIR *proc_dir = opendir("/proc");
75
for(struct dirent *proc_ent = readdir(proc_dir);
77
proc_ent = readdir(proc_dir)){
78
pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
83
/* Find the executable name by doing readlink() on the
84
/proc/<pid>/exe link */
85
char exe_target[sizeof(usplash_name)];
88
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
95
sret = readlink(exe_link, exe_target, sizeof(exe_target));
98
if((sret == ((ssize_t)sizeof(exe_target)-1))
99
and (memcmp(usplash_name, exe_target,
100
sizeof(exe_target)-1) == 0)){
102
/* Read and save the command line of usplash in "cmdline" */
104
/* Open /proc/<pid>/cmdline */
107
char *cmdline_filename;
108
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
116
cl_fd = open(cmdline_filename, O_RDONLY);
119
free(cmdline_filename);
124
free(cmdline_filename);
126
size_t cmdline_allocated = 0;
128
const size_t blocksize = 1024;
130
if(cmdline_len + blocksize > cmdline_allocated){
131
tmp = realloc(cmdline, cmdline_allocated + blocksize);
140
cmdline_allocated += blocksize;
142
sret = read(cl_fd, cmdline + cmdline_len,
143
cmdline_allocated - cmdline_len);
151
cmdline_len += (size_t)sret;
160
if(usplash_pid == 0){
165
/* Set up the signal handler */
167
struct sigaction old_action,
168
new_action = { .sa_handler = termination_handler,
170
sigemptyset(&new_action.sa_mask);
171
sigaddset(&new_action.sa_mask, SIGINT);
172
sigaddset(&new_action.sa_mask, SIGHUP);
173
sigaddset(&new_action.sa_mask, SIGTERM);
174
ret = sigaction(SIGINT, NULL, &old_action);
180
if (old_action.sa_handler != SIG_IGN){
181
ret = sigaction(SIGINT, &new_action, NULL);
188
ret = sigaction(SIGHUP, NULL, &old_action);
194
if (old_action.sa_handler != SIG_IGN){
195
ret = sigaction(SIGHUP, &new_action, NULL);
202
ret = sigaction(SIGTERM, NULL, &old_action);
208
if (old_action.sa_handler != SIG_IGN){
209
ret = sigaction(SIGTERM, &new_action, NULL);
218
/* Write command to FIFO */
219
if(not interrupted_by_signal){
220
int fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
227
ret = asprintf(&command, "INPUTQUIET %s", prompt);
235
size_t command_len = (size_t)ret + 1;
237
while(not interrupted_by_signal and written < command_len){
238
ret = write(fifo_fd, command + written, command_len - written);
240
if(interrupted_by_signal){
248
an_error_occured = true;
251
written += (size_t)ret;
253
ret = close(fifo_fd);
254
if(ret == -1 and not interrupted_by_signal){
255
an_error_occured = true;
267
if(not interrupted_by_signal and not an_error_occured){
268
int fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
269
if(fifo_fd == -1 and not interrupted_by_signal){
273
size_t buf_allocated = 0;
274
const int blocksize = 1024;
276
if(buf_len + blocksize > buf_allocated){
277
char *tmp = realloc(buf, buf_allocated + blocksize);
280
an_error_occured = true;
284
buf_allocated += blocksize;
286
sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
289
an_error_occured = true;
292
buf_len += (size_t)sret;
293
}while(not interrupted_by_signal and sret != 0);
297
/* Print password to stdout */
298
if(not interrupted_by_signal and not an_error_occured){
301
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
302
if(sret == -1 and not interrupted_by_signal){
304
an_error_occured = true;
307
written += (size_t)sret;
308
}while(written < buf_len);
309
if(not interrupted_by_signal and not an_error_occured){
315
kill(usplash_pid, SIGTERM);
317
int cmdline_argc = 0;
318
char **cmdline_argv = malloc(sizeof(char *));
319
/* Create argv and argc for new usplash*/
321
ptrdiff_t position = 0;
322
while((size_t)position < cmdline_len){
323
char **tmp = realloc(cmdline_argv,
324
(sizeof(char *) * (size_t)(cmdline_argc + 2)));
331
cmdline_argv[cmdline_argc] = cmdline + position;
333
position = (char *)rawmemchr(cmdline + position, '\0')
336
cmdline_argv[cmdline_argc] = NULL;
338
pid_t new_usplash_pid = fork();
339
if(new_usplash_pid == 0){
340
/* Child; will become new usplash process */
341
while(kill(usplash_pid, 0)){
343
kill(usplash_pid, SIGKILL);
346
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
351
execv("/sbin/usplash", cmdline_argv);