1
#define _GNU_SOURCE             /* asprintf() */
 
 
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
 
 
3
                                   sigemptyset(), sigaddset(), SIGINT,
 
 
4
                                   SIGHUP, SIGTERM, sigaction,
 
 
5
                                   SIG_IGN, kill(), SIGKILL */
 
 
6
#include <stddef.h>             /* NULL */
 
 
7
#include <stdlib.h>             /* getenv() */
 
 
8
#include <stdio.h>              /* asprintf(), perror() */
 
 
9
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
 
11
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
 
 
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
 
14
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
 
15
#include <iso646.h>             /* not, or, and */
 
 
16
#include <unistd.h>             /* readlink(), fork(), execl(),
 
 
17
                                   sleep(), dup2() STDERR_FILENO,
 
 
18
                                   STDOUT_FILENO, _exit() */
 
 
19
#include <string.h>             /* memcmp() */
 
 
20
#include <errno.h>              /* errno */
 
 
21
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
 
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
  /* Create prompt string */
 
 
37
    const char *const cryptsource = getenv("cryptsource");
 
 
38
    const char *const crypttarget = getenv("crypttarget");
 
 
39
    const char *const prompt_start = "getpass "
 
 
40
      "Enter passphrase to unlock the disk";
 
 
42
    if(cryptsource == NULL){
 
 
43
      if(crypttarget == NULL){
 
 
44
        ret = asprintf(&prompt, "%s: ", prompt_start);
 
 
46
        ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
 
50
      if(crypttarget == NULL){
 
 
51
        ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
 
 
53
        ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
 
54
                       cryptsource, crypttarget);
 
 
62
  /* Find splashy process */
 
 
63
  pid_t splashy_pid = 0;
 
 
65
    const char splashy_name[] = "/sbin/splashy";
 
 
66
    DIR *proc_dir = opendir("/proc");
 
 
72
    for(struct dirent *proc_ent = readdir(proc_dir);
 
 
74
        proc_ent = readdir(proc_dir)){
 
 
75
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
 
 
80
      /* Find the executable name by doing readlink() on the
 
 
81
         /proc/<pid>/exe link */
 
 
82
      char exe_target[sizeof(splashy_name)];
 
 
86
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
 
94
        /* Check that it refers to a symlink owned by root:root */
 
 
96
        ret = lstat(exe_link, &exe_stat);
 
 
104
        if(not S_ISLNK(exe_stat.st_mode)
 
 
105
           or exe_stat.st_uid != 0
 
 
106
           or exe_stat.st_gid != 0){
 
 
111
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
 
114
      if((sret == ((ssize_t)sizeof(exe_target)-1))
 
 
115
         and (memcmp(splashy_name, exe_target,
 
 
116
                     sizeof(exe_target)-1) == 0)){
 
 
123
  if(splashy_pid == 0){
 
 
128
  /* Set up the signal handler */
 
 
130
    struct sigaction old_action,
 
 
131
      new_action = { .sa_handler = termination_handler,
 
 
133
    sigemptyset(&new_action.sa_mask);
 
 
134
    sigaddset(&new_action.sa_mask, SIGINT);
 
 
135
    sigaddset(&new_action.sa_mask, SIGHUP);
 
 
136
    sigaddset(&new_action.sa_mask, SIGTERM);
 
 
137
    ret = sigaction(SIGINT, NULL, &old_action);
 
 
143
    if(old_action.sa_handler != SIG_IGN){
 
 
144
      ret = sigaction(SIGINT, &new_action, NULL);
 
 
151
    ret = sigaction(SIGHUP, NULL, &old_action);
 
 
157
    if(old_action.sa_handler != SIG_IGN){
 
 
158
      ret = sigaction(SIGHUP, &new_action, NULL);
 
 
165
    ret = sigaction(SIGTERM, NULL, &old_action);
 
 
171
    if(old_action.sa_handler != SIG_IGN){
 
 
172
      ret = sigaction(SIGTERM, &new_action, NULL);
 
 
181
  /* Fork off the splashy command to prompt for password */
 
 
182
  pid_t splashy_command_pid = 0;
 
 
183
  if(not interrupted_by_signal){
 
 
184
    splashy_command_pid = fork();
 
 
185
    if(splashy_command_pid == -1){
 
 
186
      if(not interrupted_by_signal){
 
 
192
    if(splashy_command_pid == 0){
 
 
193
      const char splashy_command[] = "/sbin/splashy_update";
 
 
194
      ret = execl(splashy_command, splashy_command, prompt,
 
 
196
      if(not interrupted_by_signal){
 
 
207
  /* Wait for command to complete */
 
 
208
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
 
210
    ret = waitpid(splashy_command_pid, &status, 0);
 
 
216
        splashy_command_pid = 0;
 
 
219
      /* The child process has exited */
 
 
220
      splashy_command_pid = 0;
 
 
221
      if(not interrupted_by_signal and WIFEXITED(status)
 
 
222
         and WEXITSTATUS(status)==0){
 
 
227
  kill(splashy_pid, SIGTERM);
 
 
228
  if(interrupted_by_signal and splashy_command_pid != 0){
 
 
229
    kill(splashy_command_pid, SIGTERM);
 
 
232
  while(kill(splashy_pid, 0) == 0){
 
 
233
    kill(splashy_pid, SIGKILL);
 
 
236
  pid_t new_splashy_pid = fork();
 
 
237
  if(new_splashy_pid == 0){
 
 
238
    /* Child; will become new splashy process */
 
 
240
    /* Make the effective user ID (root) the only user ID instead of
 
 
241
       the real user ID (mandos) */
 
 
242
    ret = setuid(geteuid());
 
 
249
/*     if(fork() != 0){ */
 
 
250
/*       _exit(EXIT_SUCCESS); */
 
 
252
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
 
258
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
 
259
    if(not interrupted_by_signal){