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
static bool usplash_write(const char *cmd, const char *arg){
 
 
32
   * usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
 
 
33
   * usplash_write("PULSATE", NULL); -> "PULSATE\0"
 
 
40
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
 
 
41
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
 
 
44
  }while(fifo_fd == -1);
 
 
47
  char *cmd_line_alloc = NULL;
 
 
50
    ret = (int)strlen(cmd);
 
 
53
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
 
 
54
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
 
 
61
    cmd_line = cmd_line_alloc;
 
 
64
  size_t cmd_line_len = (size_t)ret + 1;
 
 
66
  while(not interrupted_by_signal and written < cmd_line_len){
 
 
67
    ret = write(fifo_fd, cmd_line + written,
 
 
68
                cmd_line_len - written);
 
 
70
      if(errno != EINTR or interrupted_by_signal){
 
 
80
    written += (size_t)ret;
 
 
85
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
 
 
89
  if(interrupted_by_signal){
 
 
95
int main(__attribute__((unused))int argc,
 
 
96
         __attribute__((unused))char **argv){
 
 
99
  bool an_error_occured = false;
 
 
101
  /* Create prompt string */
 
 
104
    const char *const cryptsource = getenv("cryptsource");
 
 
105
    const char *const crypttarget = getenv("crypttarget");
 
 
106
    const char *const prompt_start = "Enter passphrase to unlock the disk";
 
 
108
    if(cryptsource == NULL){
 
 
109
      if(crypttarget == NULL){
 
 
110
        ret = asprintf(&prompt, "%s: ", prompt_start);
 
 
112
        ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
 
116
      if(crypttarget == NULL){
 
 
117
        ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
 
 
119
        ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
 
120
                       cryptsource, crypttarget);
 
 
128
  /* Find usplash process */
 
 
129
  pid_t usplash_pid = 0;
 
 
130
  char *cmdline = NULL;
 
 
131
  size_t cmdline_len = 0;
 
 
132
  const char usplash_name[] = "/sbin/usplash";
 
 
134
    DIR *proc_dir = opendir("/proc");
 
 
135
    if(proc_dir == NULL){
 
 
140
    for(struct dirent *proc_ent = readdir(proc_dir);
 
 
142
        proc_ent = readdir(proc_dir)){
 
 
143
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
 
 
148
      /* Find the executable name by doing readlink() on the
 
 
149
         /proc/<pid>/exe link */
 
 
150
      char exe_target[sizeof(usplash_name)];
 
 
153
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
 
160
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
 
163
      if((sret == ((ssize_t)sizeof(exe_target)-1))
 
 
164
         and (memcmp(usplash_name, exe_target,
 
 
165
                     sizeof(exe_target)-1) == 0)){
 
 
167
        /* Read and save the command line of usplash in "cmdline" */
 
 
169
          /* Open /proc/<pid>/cmdline  */
 
 
172
            char *cmdline_filename;
 
 
173
            ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
 
181
            cl_fd = open(cmdline_filename, O_RDONLY);
 
 
184
              free(cmdline_filename);
 
 
189
            free(cmdline_filename);
 
 
191
          size_t cmdline_allocated = 0;
 
 
193
          const size_t blocksize = 1024;
 
 
195
            if(cmdline_len + blocksize > cmdline_allocated){
 
 
196
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
 
 
205
              cmdline_allocated += blocksize;
 
 
207
            sret = read(cl_fd, cmdline + cmdline_len,
 
 
208
                        cmdline_allocated - cmdline_len);
 
 
216
            cmdline_len += (size_t)sret;
 
 
225
  if(usplash_pid == 0){
 
 
230
  /* Set up the signal handler */
 
 
232
    struct sigaction old_action,
 
 
233
      new_action = { .sa_handler = termination_handler,
 
 
235
    sigemptyset(&new_action.sa_mask);
 
 
236
    sigaddset(&new_action.sa_mask, SIGINT);
 
 
237
    sigaddset(&new_action.sa_mask, SIGHUP);
 
 
238
    sigaddset(&new_action.sa_mask, SIGTERM);
 
 
239
    ret = sigaction(SIGINT, NULL, &old_action);
 
 
245
    if (old_action.sa_handler != SIG_IGN){
 
 
246
      ret = sigaction(SIGINT, &new_action, NULL);
 
 
253
    ret = sigaction(SIGHUP, NULL, &old_action);
 
 
259
    if (old_action.sa_handler != SIG_IGN){
 
 
260
      ret = sigaction(SIGHUP, &new_action, NULL);
 
 
267
    ret = sigaction(SIGTERM, NULL, &old_action);
 
 
273
    if (old_action.sa_handler != SIG_IGN){
 
 
274
      ret = sigaction(SIGTERM, &new_action, NULL);
 
 
283
  /* Write command to FIFO */
 
 
284
  if(not interrupted_by_signal){
 
 
285
    if(not usplash_write("TIMEOUT", "0")
 
 
286
       and (errno != EINTR)){
 
 
287
      perror("usplash_write");
 
 
288
      an_error_occured = true;
 
 
291
  if(not interrupted_by_signal and not an_error_occured){
 
 
292
    if(not usplash_write("INPUTQUIET", prompt)
 
 
293
       and (errno != EINTR)){
 
 
294
      perror("usplash_write");
 
 
295
      an_error_occured = true;
 
 
300
  /* This is not really a loop; while() is used to be able to "break"
 
 
301
     out of it; those breaks are marked "Big" */
 
 
302
  while(not interrupted_by_signal and not an_error_occured){
 
 
309
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
 
 
313
          an_error_occured = true;
 
 
316
        if(interrupted_by_signal){
 
 
320
    }while(fifo_fd == -1);
 
 
321
    if(interrupted_by_signal or an_error_occured){
 
 
326
    size_t buf_allocated = 0;
 
 
327
    const size_t blocksize = 1024;
 
 
329
      if(buf_len + blocksize > buf_allocated){
 
 
330
        char *tmp = realloc(buf, buf_allocated + blocksize);
 
 
333
          an_error_occured = true;
 
 
337
        buf_allocated += blocksize;
 
 
340
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
 
 
344
            an_error_occured = true;
 
 
347
          if(interrupted_by_signal){
 
 
352
      if(interrupted_by_signal or an_error_occured){
 
 
356
      buf_len += (size_t)sret;
 
 
359
    if(interrupted_by_signal or an_error_occured){
 
 
363
    if(not usplash_write("TIMEOUT", "15")
 
 
364
       and (errno != EINTR)){
 
 
365
        perror("usplash_write");
 
 
366
        an_error_occured = true;
 
 
368
    if(interrupted_by_signal or an_error_occured){
 
 
372
    /* Print password to stdout */
 
 
376
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
 
 
380
            an_error_occured = true;
 
 
383
          if(interrupted_by_signal){
 
 
388
      if(interrupted_by_signal or an_error_occured){
 
 
392
      written += (size_t)sret;
 
 
393
    }while(written < buf_len);
 
 
394
    if(not interrupted_by_signal and not an_error_occured){
 
 
400
  /* If we got here, an error or interrupt must have happened */
 
 
402
  int cmdline_argc = 0;
 
 
403
  char **cmdline_argv = malloc(sizeof(char *));
 
 
404
  /* Create argv and argc for new usplash*/
 
 
407
    while(position < cmdline_len){
 
 
408
      char **tmp = realloc(cmdline_argv,
 
 
409
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
 
 
416
      cmdline_argv[cmdline_argc] = cmdline + position;
 
 
418
      position += strlen(cmdline + position) + 1;
 
 
420
    cmdline_argv[cmdline_argc] = NULL;
 
 
422
  /* Kill old usplash */
 
 
423
    kill(usplash_pid, SIGTERM);
 
 
425
  while(kill(usplash_pid, 0) == 0){
 
 
426
    kill(usplash_pid, SIGKILL);
 
 
429
  pid_t new_usplash_pid = fork();
 
 
430
  if(new_usplash_pid == 0){
 
 
431
    /* Child; will become new usplash process */
 
 
433
    /* Make the effective user ID (root) the only user ID instead of
 
 
434
       the real user ID (mandos) */
 
 
435
    ret = setuid(geteuid());
 
 
442
/*     if(fork() != 0){ */
 
 
443
/*       _exit(EXIT_SUCCESS); */
 
 
445
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
 
451
    execv(usplash_name, cmdline_argv);
 
 
454
  if(not usplash_write("PULSATE", NULL)
 
 
455
     and (errno != EINTR)){
 
 
456
    perror("usplash_write");