/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/usplash.c

  • Committer: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _GNU_SOURCE             /* asprintf() */
 
1
/*  -*- coding: utf-8 -*- */
 
2
/*
 
3
 * Usplash - Read a password from usplash and output it
 
4
 * 
 
5
 * Copyright © 2008-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 * 
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
 
21
 * 
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
23
 */
 
24
 
 
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
2
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
3
 
                                   sigemptyset(), sigaddset(),
4
 
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
5
 
                                   SIGTERM, kill(), SIGKILL */
 
27
                                   sigemptyset(), sigaddset(), SIGINT,
 
28
                                   SIGHUP, SIGTERM, sigaction(),
 
29
                                   SIG_IGN, kill(), SIGKILL */
 
30
#include <stdbool.h>            /* bool, false, true */
 
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
 
32
#include <iso646.h>             /* and, or, not*/
 
33
#include <errno.h>              /* errno, EINTR */
 
34
#include <error.h>
 
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
36
                                   dirent */
6
37
#include <stddef.h>             /* NULL */
7
 
#include <stdlib.h>             /* getenv() */
8
 
#include <stdio.h>              /* asprintf(), perror() */
9
 
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
10
 
                                   strtoul(), free() */
11
 
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
12
 
                                   ssize_t */
 
38
#include <string.h>             /* strlen(), memcmp(), strerror() */
 
39
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
 
40
#include <unistd.h>             /* close(), write(), readlink(),
 
41
                                   read(), STDOUT_FILENO, sleep(),
 
42
                                   fork(), setuid(), geteuid(),
 
43
                                   setsid(), chdir(), dup2(),
 
44
                                   STDERR_FILENO, execv() */
 
45
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
46
                                   EXIT_SUCCESS, malloc(), _exit(),
 
47
                                   getenv() */
13
48
#include <dirent.h>             /* opendir(), readdir(), closedir() */
14
 
#include <unistd.h>             /* readlink(), fork(), execl(),
15
 
                                   _exit */
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(),
21
 
                                   WEXITSTATUS() */
22
 
#include <fcntl.h>              /* open(), O_RDONLY */
 
49
#include <inttypes.h>           /* intmax_t, strtoimax() */
 
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
52
#include <argz.h>               /* argz_count(), argz_extract() */
 
53
#include <stdarg.h>             /* va_list, va_start(), ... */
23
54
 
24
55
sig_atomic_t interrupted_by_signal = 0;
25
 
 
26
 
static void termination_handler(__attribute__((unused))int signum){
 
56
int signal_received;
 
57
const char usplash_name[] = "/sbin/usplash";
 
58
 
 
59
/* Function to use when printing errors */
 
60
void error_plus(int status, int errnum, const char *formatstring, ...){
 
61
  va_list ap;
 
62
  char *text;
 
63
  int ret;
 
64
  
 
65
  va_start(ap, formatstring);
 
66
  ret = vasprintf(&text, formatstring, ap);
 
67
  if (ret == -1){
 
68
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
 
69
    vfprintf(stderr, formatstring, ap);
 
70
    fprintf(stderr, ": ");
 
71
    fprintf(stderr, "%s\n", strerror(errnum));
 
72
    error(status, errno, "vasprintf while printing error");
 
73
    return;
 
74
  }
 
75
  fprintf(stderr, "Mandos plugin ");
 
76
  error(status, errnum, "%s", text);
 
77
  free(text);
 
78
}
 
79
 
 
80
static void termination_handler(int signum){
 
81
  if(interrupted_by_signal){
 
82
    return;
 
83
  }
27
84
  interrupted_by_signal = 1;
 
85
  signal_received = signum;
28
86
}
29
87
 
30
 
static bool usplash_write(const char *cmd, const char *arg){
 
88
static bool usplash_write(int *fifo_fd_r,
 
89
                          const char *cmd, const char *arg){
31
90
  /* 
32
 
   * usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
33
 
   * usplash_write("PULSATE", NULL); -> "PULSATE\0"
 
91
   * usplash_write(&fd, "TIMEOUT", "15") will write "TIMEOUT 15\0"
 
92
   * usplash_write(&fd, "PULSATE", NULL) will write "PULSATE\0"
34
93
   * SEE ALSO
35
94
   *         usplash_write(8)
36
95
   */
37
96
  int ret;
38
 
  int fifo_fd;
39
 
  do{
40
 
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
41
 
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
 
97
  if(*fifo_fd_r == -1){
 
98
    ret = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
 
99
    if(ret == -1){
42
100
      return false;
43
101
    }
44
 
  }while(fifo_fd == -1);
 
102
    *fifo_fd_r = ret;
 
103
  }
45
104
  
46
105
  const char *cmd_line;
 
106
  size_t cmd_line_len;
47
107
  char *cmd_line_alloc = NULL;
48
108
  if(arg == NULL){
49
109
    cmd_line = cmd;
50
 
    ret = (int)strlen(cmd);
51
 
  }else{
52
 
    do{
 
110
    cmd_line_len = strlen(cmd) + 1;
 
111
  } else {
 
112
    do {
53
113
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
54
 
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
 
114
      if(ret == -1){
55
115
        int e = errno;
56
 
        close(fifo_fd);
 
116
        TEMP_FAILURE_RETRY(close(*fifo_fd_r));
57
117
        errno = e;
58
118
        return false;
59
119
      }
60
 
    }while(ret == -1);
 
120
    } while(ret == -1);
61
121
    cmd_line = cmd_line_alloc;
 
122
    cmd_line_len = (size_t)ret + 1;
62
123
  }
63
124
  
64
 
  size_t cmd_line_len = (size_t)ret + 1;
65
125
  size_t written = 0;
66
 
  while(not interrupted_by_signal and written < cmd_line_len){
67
 
    ret = write(fifo_fd, cmd_line + written,
68
 
                cmd_line_len - written);
69
 
    if(ret == -1){
70
 
      if(errno != EINTR or interrupted_by_signal){
71
 
        int e = errno;
72
 
        close(fifo_fd);
73
 
        free(cmd_line_alloc);
74
 
        errno = e;
75
 
        return false;
76
 
      } else {
77
 
        continue;
78
 
      }
 
126
  ssize_t sret = 0;
 
127
  while(written < cmd_line_len){
 
128
    sret = write(*fifo_fd_r, cmd_line + written,
 
129
                 cmd_line_len - written);
 
130
    if(sret == -1){
 
131
      int e = errno;
 
132
      TEMP_FAILURE_RETRY(close(*fifo_fd_r));
 
133
      free(cmd_line_alloc);
 
134
      errno = e;
 
135
      return false;
79
136
    }
80
 
    written += (size_t)ret;
 
137
    written += (size_t)sret;
81
138
  }
82
139
  free(cmd_line_alloc);
83
 
  do{
84
 
    ret = close(fifo_fd);
85
 
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
86
 
      return false;
87
 
    }
88
 
  }while(ret == -1);
89
 
  if(interrupted_by_signal){
90
 
    return false;
91
 
  }
 
140
  
92
141
  return true;
93
142
}
94
143
 
95
 
int main(__attribute__((unused))int argc,
96
 
         __attribute__((unused))char **argv){
97
 
  int ret = 0;
98
 
  ssize_t sret;
99
 
  bool an_error_occured = false;
100
 
  
101
 
  /* Create prompt string */
102
 
  char *prompt = NULL;
103
 
  {
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";
107
 
    
108
 
    if(cryptsource == NULL){
109
 
      if(crypttarget == NULL){
110
 
        ret = asprintf(&prompt, "%s: ", prompt_start);
111
 
      } else {
112
 
        ret = asprintf(&prompt, "%s (%s): ", prompt_start,
113
 
                       crypttarget);
114
 
      }
115
 
    } else {
116
 
      if(crypttarget == NULL){
117
 
        ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
118
 
      } else {
119
 
        ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
120
 
                       cryptsource, crypttarget);
121
 
      }
122
 
    }
123
 
    if(ret == -1){
124
 
      return EXIT_FAILURE;
125
 
    }
126
 
  }
127
 
  
128
 
  /* Find usplash process */
129
 
  pid_t usplash_pid = 0;
 
144
/* Create prompt string */
 
145
char *makeprompt(void){
 
146
  int ret = 0;
 
147
  char *prompt;
 
148
  const char *const cryptsource = getenv("cryptsource");
 
149
  const char *const crypttarget = getenv("crypttarget");
 
150
  const char prompt_start[] = "Enter passphrase to unlock the disk";
 
151
  
 
152
  if(cryptsource == NULL){
 
153
    if(crypttarget == NULL){
 
154
      ret = asprintf(&prompt, "%s: ", prompt_start);
 
155
    } else {
 
156
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
157
                     crypttarget);
 
158
    }
 
159
  } else {
 
160
    if(crypttarget == NULL){
 
161
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
 
162
    } else {
 
163
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
164
                     cryptsource, crypttarget);
 
165
    }
 
166
  }
 
167
  if(ret == -1){
 
168
    return NULL;
 
169
  }
 
170
  return prompt;
 
171
}
 
172
 
 
173
pid_t find_usplash(char **cmdline_r, size_t *cmdline_len_r){
 
174
  int ret = 0;
 
175
  ssize_t sret = 0;
130
176
  char *cmdline = NULL;
131
177
  size_t cmdline_len = 0;
132
 
  const char usplash_name[] = "/sbin/usplash";
133
 
  {
134
 
    DIR *proc_dir = opendir("/proc");
135
 
    if(proc_dir == NULL){
136
 
      free(prompt);
137
 
      perror("opendir");
138
 
      return EXIT_FAILURE;
139
 
    }
140
 
    for(struct dirent *proc_ent = readdir(proc_dir);
141
 
        proc_ent != NULL;
142
 
        proc_ent = readdir(proc_dir)){
143
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
144
 
      if(pid == 0){
 
178
  DIR *proc_dir = opendir("/proc");
 
179
  if(proc_dir == NULL){
 
180
    error_plus(0, errno, "opendir");
 
181
    return -1;
 
182
  }
 
183
  errno = 0;
 
184
  for(struct dirent *proc_ent = readdir(proc_dir);
 
185
      proc_ent != NULL;
 
186
      proc_ent = readdir(proc_dir)){
 
187
    pid_t pid;
 
188
    {
 
189
      intmax_t tmpmax;
 
190
      char *tmp;
 
191
      tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
 
192
      if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
 
193
         or tmpmax != (pid_t)tmpmax){
145
194
        /* Not a process */
146
 
        continue;
147
 
      }
148
 
      /* Find the executable name by doing readlink() on the
149
 
         /proc/<pid>/exe link */
150
 
      char exe_target[sizeof(usplash_name)];
 
195
        errno = 0;
 
196
        continue;
 
197
      }
 
198
      pid = (pid_t)tmpmax;
 
199
    }
 
200
    /* Find the executable name by doing readlink() on the
 
201
       /proc/<pid>/exe link */
 
202
    char exe_target[sizeof(usplash_name)];
 
203
    {
 
204
      /* create file name string */
 
205
      char *exe_link;
 
206
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
207
      if(ret == -1){
 
208
        error_plus(0, errno, "asprintf");
 
209
        goto fail_find_usplash;
 
210
      }
 
211
      
 
212
      /* Check that it refers to a symlink owned by root:root */
 
213
      struct stat exe_stat;
 
214
      ret = lstat(exe_link, &exe_stat);
 
215
      if(ret == -1){
 
216
        if(errno == ENOENT){
 
217
          free(exe_link);
 
218
          continue;
 
219
        }
 
220
        error_plus(0, errno, "lstat");
 
221
        free(exe_link);
 
222
        goto fail_find_usplash;
 
223
      }
 
224
      if(not S_ISLNK(exe_stat.st_mode)
 
225
         or exe_stat.st_uid != 0
 
226
         or exe_stat.st_gid != 0){
 
227
        free(exe_link);
 
228
        continue;
 
229
      }
 
230
        
 
231
      sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
232
      free(exe_link);
 
233
    }
 
234
    /* Compare executable name */
 
235
    if((sret != ((ssize_t)sizeof(exe_target)-1))
 
236
       or (memcmp(usplash_name, exe_target,
 
237
                  sizeof(exe_target)-1) != 0)){
 
238
      /* Not it */
 
239
      continue;
 
240
    }
 
241
    /* Found usplash */
 
242
    /* Read and save the command line of usplash in "cmdline" */
 
243
    {
 
244
      /* Open /proc/<pid>/cmdline  */
 
245
      int cl_fd;
151
246
      {
152
 
        char *exe_link;
153
 
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
247
        char *cmdline_filename;
 
248
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
249
                       proc_ent->d_name);
154
250
        if(ret == -1){
155
 
          perror("asprintf");
156
 
          free(prompt);
157
 
          closedir(proc_dir);
158
 
          return EXIT_FAILURE;
159
 
        }
160
 
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
161
 
        free(exe_link);
 
251
          error_plus(0, errno, "asprintf");
 
252
          goto fail_find_usplash;
 
253
        }
 
254
        cl_fd = open(cmdline_filename, O_RDONLY);
 
255
        free(cmdline_filename);
 
256
        if(cl_fd == -1){
 
257
          error_plus(0, errno, "open");
 
258
          goto fail_find_usplash;
 
259
        }
162
260
      }
163
 
      if((sret == ((ssize_t)sizeof(exe_target)-1))
164
 
         and (memcmp(usplash_name, exe_target,
165
 
                     sizeof(exe_target)-1) == 0)){
166
 
        usplash_pid = pid;
167
 
        /* Read and save the command line of usplash in "cmdline" */
168
 
        {
169
 
          /* Open /proc/<pid>/cmdline  */
170
 
          int cl_fd;
171
 
          {
172
 
            char *cmdline_filename;
173
 
            ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
174
 
                           proc_ent->d_name);
175
 
            if(ret == -1){
176
 
              perror("asprintf");
177
 
              free(prompt);
178
 
              closedir(proc_dir);
179
 
              return EXIT_FAILURE;
180
 
            }
181
 
            cl_fd = open(cmdline_filename, O_RDONLY);
182
 
            if(cl_fd == -1){
183
 
              perror("open");
184
 
              free(cmdline_filename);
185
 
              free(prompt);
186
 
              closedir(proc_dir);
187
 
              return EXIT_FAILURE;
188
 
            }
189
 
            free(cmdline_filename);
 
261
      size_t cmdline_allocated = 0;
 
262
      char *tmp;
 
263
      const size_t blocksize = 1024;
 
264
      do {
 
265
        /* Allocate more space? */
 
266
        if(cmdline_len + blocksize > cmdline_allocated){
 
267
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
 
268
          if(tmp == NULL){
 
269
            error_plus(0, errno, "realloc");
 
270
            close(cl_fd);
 
271
            goto fail_find_usplash;
190
272
          }
191
 
          size_t cmdline_allocated = 0;
192
 
          char *tmp;
193
 
          const size_t blocksize = 1024;
194
 
          do{
195
 
            if(cmdline_len + blocksize > cmdline_allocated){
196
 
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
197
 
              if(tmp == NULL){
198
 
                perror("realloc");
199
 
                free(cmdline);
200
 
                free(prompt);
201
 
                closedir(proc_dir);
202
 
                return EXIT_FAILURE;
203
 
              }
204
 
              cmdline = tmp;
205
 
              cmdline_allocated += blocksize;
206
 
            }
207
 
            sret = read(cl_fd, cmdline + cmdline_len,
208
 
                        cmdline_allocated - cmdline_len);
209
 
            if(sret == -1){
210
 
              perror("read");
211
 
              free(cmdline);
212
 
              free(prompt);
213
 
              closedir(proc_dir);
214
 
              return EXIT_FAILURE;
215
 
            }
216
 
            cmdline_len += (size_t)sret;
217
 
          } while(sret != 0);
 
273
          cmdline = tmp;
 
274
          cmdline_allocated += blocksize;
 
275
        }
 
276
        /* Read data */
 
277
        sret = read(cl_fd, cmdline + cmdline_len,
 
278
                    cmdline_allocated - cmdline_len);
 
279
        if(sret == -1){
 
280
          error_plus(0, errno, "read");
218
281
          close(cl_fd);
 
282
          goto fail_find_usplash;
219
283
        }
220
 
        break;
 
284
        cmdline_len += (size_t)sret;
 
285
      } while(sret != 0);
 
286
      ret = close(cl_fd);
 
287
      if(ret == -1){
 
288
        error_plus(0, errno, "close");
 
289
        goto fail_find_usplash;
221
290
      }
222
291
    }
 
292
    /* Close directory */
 
293
    ret = closedir(proc_dir);
 
294
    if(ret == -1){
 
295
      error_plus(0, errno, "closedir");
 
296
      goto fail_find_usplash;
 
297
    }
 
298
    /* Success */
 
299
    *cmdline_r = cmdline;
 
300
    *cmdline_len_r = cmdline_len;
 
301
    return pid;
 
302
  }
 
303
  
 
304
 fail_find_usplash:
 
305
  
 
306
  free(cmdline);
 
307
  if(proc_dir != NULL){
 
308
    int e = errno;
223
309
    closedir(proc_dir);
224
 
  }
 
310
    errno = e;
 
311
  }
 
312
  return 0;
 
313
}
 
314
 
 
315
int main(__attribute__((unused))int argc,
 
316
         __attribute__((unused))char **argv){
 
317
  int ret = 0;
 
318
  ssize_t sret;
 
319
  int fifo_fd = -1;
 
320
  int outfifo_fd = -1;
 
321
  char *buf = NULL;
 
322
  size_t buf_len = 0;
 
323
  pid_t usplash_pid = -1;
 
324
  bool usplash_accessed = false;
 
325
  int status = EXIT_FAILURE;    /* Default failure exit status */
 
326
  
 
327
  char *prompt = makeprompt();
 
328
  if(prompt == NULL){
 
329
    status = EX_OSERR;
 
330
    goto failure;
 
331
  }
 
332
  
 
333
  /* Find usplash process */
 
334
  char *cmdline = NULL;
 
335
  size_t cmdline_len = 0;
 
336
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
225
337
  if(usplash_pid == 0){
226
 
    free(prompt);
227
 
    return EXIT_FAILURE;
 
338
    status = EX_UNAVAILABLE;
 
339
    goto failure;
228
340
  }
229
341
  
230
342
  /* Set up the signal handler */
233
345
      new_action = { .sa_handler = termination_handler,
234
346
                     .sa_flags = 0 };
235
347
    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);
 
348
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
349
    if(ret == -1){
 
350
      error_plus(0, errno, "sigaddset");
 
351
      status = EX_OSERR;
 
352
      goto failure;
 
353
    }
 
354
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
355
    if(ret == -1){
 
356
      error_plus(0, errno, "sigaddset");
 
357
      status = EX_OSERR;
 
358
      goto failure;
 
359
    }
 
360
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
361
    if(ret == -1){
 
362
      error_plus(0, errno, "sigaddset");
 
363
      status = EX_OSERR;
 
364
      goto failure;
 
365
    }
239
366
    ret = sigaction(SIGINT, NULL, &old_action);
240
367
    if(ret == -1){
241
 
      perror("sigaction");
242
 
      free(prompt);
243
 
      return EXIT_FAILURE;
 
368
      if(errno != EINTR){
 
369
        error_plus(0, errno, "sigaction");
 
370
        status = EX_OSERR;
 
371
      }
 
372
      goto failure;
244
373
    }
245
 
    if (old_action.sa_handler != SIG_IGN){
 
374
    if(old_action.sa_handler != SIG_IGN){
246
375
      ret = sigaction(SIGINT, &new_action, NULL);
247
376
      if(ret == -1){
248
 
        perror("sigaction");
249
 
        free(prompt);
250
 
        return EXIT_FAILURE;
 
377
        if(errno != EINTR){
 
378
          error_plus(0, errno, "sigaction");
 
379
          status = EX_OSERR;
 
380
        }
 
381
        goto failure;
251
382
      }
252
383
    }
253
384
    ret = sigaction(SIGHUP, NULL, &old_action);
254
385
    if(ret == -1){
255
 
      perror("sigaction");
256
 
      free(prompt);
257
 
      return EXIT_FAILURE;
 
386
      if(errno != EINTR){
 
387
        error_plus(0, errno, "sigaction");
 
388
        status = EX_OSERR;
 
389
      }
 
390
      goto failure;
258
391
    }
259
 
    if (old_action.sa_handler != SIG_IGN){
 
392
    if(old_action.sa_handler != SIG_IGN){
260
393
      ret = sigaction(SIGHUP, &new_action, NULL);
261
394
      if(ret == -1){
262
 
        perror("sigaction");
263
 
        free(prompt);
264
 
        return EXIT_FAILURE;
 
395
        if(errno != EINTR){
 
396
          error_plus(0, errno, "sigaction");
 
397
          status = EX_OSERR;
 
398
        }
 
399
        goto failure;
265
400
      }
266
401
    }
267
402
    ret = sigaction(SIGTERM, NULL, &old_action);
268
403
    if(ret == -1){
269
 
      perror("sigaction");
270
 
      free(prompt);
271
 
      return EXIT_FAILURE;
 
404
      if(errno != EINTR){
 
405
        error_plus(0, errno, "sigaction");
 
406
        status = EX_OSERR;
 
407
      }
 
408
      goto failure;
272
409
    }
273
 
    if (old_action.sa_handler != SIG_IGN){
 
410
    if(old_action.sa_handler != SIG_IGN){
274
411
      ret = sigaction(SIGTERM, &new_action, NULL);
275
412
      if(ret == -1){
276
 
        perror("sigaction");
277
 
        free(prompt);
278
 
        return EXIT_FAILURE;
 
413
        if(errno != EINTR){
 
414
          error_plus(0, errno, "sigaction");
 
415
          status = EX_OSERR;
 
416
        }
 
417
        goto failure;
279
418
      }
280
419
    }
281
420
  }
282
421
  
 
422
  usplash_accessed = true;
283
423
  /* 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;
289
 
    }
290
 
  }
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;
296
 
    }
297
 
  }
 
424
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
 
425
    if(errno != EINTR){
 
426
      error_plus(0, errno, "usplash_write");
 
427
      status = EX_OSERR;
 
428
    }
 
429
    goto failure;
 
430
  }
 
431
  
 
432
  if(interrupted_by_signal){
 
433
    goto failure;
 
434
  }
 
435
  
 
436
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
 
437
    if(errno != EINTR){
 
438
      error_plus(0, errno, "usplash_write");
 
439
      status = EX_OSERR;
 
440
    }
 
441
    goto failure;
 
442
  }
 
443
  
 
444
  if(interrupted_by_signal){
 
445
    goto failure;
 
446
  }
 
447
  
298
448
  free(prompt);
299
 
  
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){
303
 
    char *buf = NULL;
304
 
    size_t buf_len = 0;
305
 
    
306
 
    /* Open FIFO */
307
 
    int fifo_fd;
308
 
    do{
309
 
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
310
 
      if(fifo_fd == -1){
311
 
        if(errno != EINTR){
312
 
          perror("open");
313
 
          an_error_occured = true;
314
 
          break;
315
 
        }
316
 
        if(interrupted_by_signal){
317
 
          break;
318
 
        }
319
 
      }
320
 
    }while(fifo_fd == -1);
321
 
    if(interrupted_by_signal or an_error_occured){
322
 
      break;                    /* Big */
323
 
    }
324
 
    
325
 
    /* Read from FIFO */
326
 
    size_t buf_allocated = 0;
327
 
    const size_t blocksize = 1024;
328
 
    do{
329
 
      if(buf_len + blocksize > buf_allocated){
330
 
        char *tmp = realloc(buf, buf_allocated + blocksize);
331
 
        if(tmp == NULL){
332
 
          perror("realloc");
333
 
          an_error_occured = true;
334
 
          break;
335
 
        }
336
 
        buf = tmp;
337
 
        buf_allocated += blocksize;
338
 
      }
339
 
      do{
340
 
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
341
 
        if(sret == -1){
342
 
          if(errno != EINTR){
343
 
            perror("read");
344
 
            an_error_occured = true;
345
 
            break;
346
 
          }
347
 
          if(interrupted_by_signal){
348
 
            break;
349
 
          }
350
 
        }
351
 
      }while(sret == -1);
352
 
      if(interrupted_by_signal or an_error_occured){
353
 
        break;
354
 
      }
355
 
      
356
 
      buf_len += (size_t)sret;
357
 
    }while(sret != 0);
358
 
    close(fifo_fd);
359
 
    if(interrupted_by_signal or an_error_occured){
360
 
      break;                    /* Big */
361
 
    }
362
 
    
363
 
    if(not usplash_write("TIMEOUT", "15")
364
 
       and (errno != EINTR)){
365
 
        perror("usplash_write");
366
 
        an_error_occured = true;
367
 
    }
368
 
    if(interrupted_by_signal or an_error_occured){
369
 
      break;                    /* Big */
370
 
    }
371
 
    
372
 
    /* Print password to stdout */
373
 
    size_t written = 0;
374
 
    do{
375
 
      do{
376
 
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
377
 
        if(sret == -1){
378
 
          if(errno != EINTR){
379
 
            perror("write");
380
 
            an_error_occured = true;
381
 
            break;
382
 
          }
383
 
          if(interrupted_by_signal){
384
 
            break;
385
 
          }
386
 
        }
387
 
      }while(sret == -1);
388
 
      if(interrupted_by_signal or an_error_occured){
389
 
        break;
390
 
      }
391
 
      
392
 
      written += (size_t)sret;
393
 
    }while(written < buf_len);
394
 
    if(not interrupted_by_signal and not an_error_occured){
395
 
      return EXIT_SUCCESS;
396
 
    }
397
 
    break;                      /* Big */
398
 
  }
399
 
  
400
 
  /* If we got here, an error or interrupt must have happened */
401
 
  
402
 
  int cmdline_argc = 0;
403
 
  char **cmdline_argv = malloc(sizeof(char *));
404
 
  /* Create argv and argc for new usplash*/
405
 
  {
406
 
    size_t position = 0;
407
 
    while(position < cmdline_len){
408
 
      char **tmp = realloc(cmdline_argv,
409
 
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
 
449
  prompt = NULL;
 
450
  
 
451
  /* Read reply from usplash */
 
452
  /* Open FIFO */
 
453
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
 
454
  if(outfifo_fd == -1){
 
455
    if(errno != EINTR){
 
456
      error_plus(0, errno, "open");
 
457
      status = EX_OSERR;
 
458
    }
 
459
    goto failure;
 
460
  }
 
461
  
 
462
  if(interrupted_by_signal){
 
463
    goto failure;
 
464
  }
 
465
  
 
466
  /* Read from FIFO */
 
467
  size_t buf_allocated = 0;
 
468
  const size_t blocksize = 1024;
 
469
  do {
 
470
    /* Allocate more space */
 
471
    if(buf_len + blocksize > buf_allocated){
 
472
      char *tmp = realloc(buf, buf_allocated + blocksize);
410
473
      if(tmp == NULL){
411
 
        perror("realloc");
412
 
        free(cmdline_argv);
413
 
        return EXIT_FAILURE;
414
 
      }
415
 
      cmdline_argv = tmp;
416
 
      cmdline_argv[cmdline_argc] = cmdline + position;
417
 
      cmdline_argc++;
418
 
      position += strlen(cmdline + position) + 1;
419
 
    }
420
 
    cmdline_argv[cmdline_argc] = NULL;
421
 
  }
 
474
        if(errno != EINTR){
 
475
          error_plus(0, errno, "realloc");
 
476
          status = EX_OSERR;
 
477
        }
 
478
        goto failure;
 
479
      }
 
480
      buf = tmp;
 
481
      buf_allocated += blocksize;
 
482
    }
 
483
    sret = read(outfifo_fd, buf + buf_len,
 
484
                buf_allocated - buf_len);
 
485
    if(sret == -1){
 
486
      if(errno != EINTR){
 
487
        error_plus(0, errno, "read");
 
488
        status = EX_OSERR;
 
489
      }
 
490
      TEMP_FAILURE_RETRY(close(outfifo_fd));
 
491
      goto failure;
 
492
    }
 
493
    if(interrupted_by_signal){
 
494
      break;
 
495
    }
 
496
    
 
497
    buf_len += (size_t)sret;
 
498
  } while(sret != 0);
 
499
  ret = close(outfifo_fd);
 
500
  if(ret == -1){
 
501
    if(errno != EINTR){
 
502
      error_plus(0, errno, "close");
 
503
      status = EX_OSERR;
 
504
    }
 
505
    goto failure;
 
506
  }
 
507
  outfifo_fd = -1;
 
508
  
 
509
  if(interrupted_by_signal){
 
510
    goto failure;
 
511
  }
 
512
  
 
513
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
 
514
    if(errno != EINTR){
 
515
      error_plus(0, errno, "usplash_write");
 
516
      status = EX_OSERR;
 
517
    }
 
518
    goto failure;
 
519
  }
 
520
  
 
521
  if(interrupted_by_signal){
 
522
    goto failure;
 
523
  }
 
524
  
 
525
  ret = close(fifo_fd);
 
526
  if(ret == -1){
 
527
    if(errno != EINTR){
 
528
      error_plus(0, errno, "close");
 
529
      status = EX_OSERR;
 
530
    }
 
531
    goto failure;
 
532
  }
 
533
  fifo_fd = -1;
 
534
  
 
535
  /* Print password to stdout */
 
536
  size_t written = 0;
 
537
  while(written < buf_len){
 
538
    do {
 
539
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
 
540
      if(sret == -1){
 
541
        if(errno != EINTR){
 
542
          error_plus(0, errno, "write");
 
543
          status = EX_OSERR;
 
544
        }
 
545
        goto failure;
 
546
      }
 
547
    } while(sret == -1);
 
548
    
 
549
    if(interrupted_by_signal){
 
550
      goto failure;
 
551
    }
 
552
    written += (size_t)sret;
 
553
  }
 
554
  free(buf);
 
555
  buf = NULL;
 
556
  
 
557
  if(interrupted_by_signal){
 
558
    goto failure;
 
559
  }
 
560
  
 
561
  free(cmdline);
 
562
  return EXIT_SUCCESS;
 
563
  
 
564
 failure:
 
565
  
 
566
  free(buf);
 
567
  
 
568
  free(prompt);
 
569
  
 
570
  /* If usplash was never accessed, we can stop now */
 
571
  if(not usplash_accessed){
 
572
    return status;
 
573
  }
 
574
  
 
575
  /* Close FIFO */
 
576
  if(fifo_fd != -1){
 
577
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
578
    if(ret == -1 and errno != EINTR){
 
579
      error_plus(0, errno, "close");
 
580
    }
 
581
    fifo_fd = -1;
 
582
  }
 
583
  
 
584
  /* Close output FIFO */
 
585
  if(outfifo_fd != -1){
 
586
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
 
587
    if(ret == -1){
 
588
      error_plus(0, errno, "close");
 
589
    }
 
590
  }
 
591
  
 
592
  /* Create argv for new usplash*/
 
593
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
 
594
                               * sizeof(char *)); /* Count args */
 
595
  if(cmdline_argv == NULL){
 
596
    error_plus(0, errno, "malloc");
 
597
    return status;
 
598
  }
 
599
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
 
600
  
422
601
  /* Kill old usplash */
423
 
    kill(usplash_pid, SIGTERM);
424
 
    sleep(2);
 
602
  kill(usplash_pid, SIGTERM);
 
603
  sleep(2);
425
604
  while(kill(usplash_pid, 0) == 0){
426
605
    kill(usplash_pid, SIGKILL);
427
606
    sleep(1);
428
607
  }
 
608
  
429
609
  pid_t new_usplash_pid = fork();
430
610
  if(new_usplash_pid == 0){
431
611
    /* Child; will become new usplash process */
432
612
    
433
613
    /* Make the effective user ID (root) the only user ID instead of
434
 
       the real user ID (mandos) */
 
614
       the real user ID (_mandos) */
435
615
    ret = setuid(geteuid());
436
 
    if (ret == -1){
437
 
      perror("setuid");
 
616
    if(ret == -1){
 
617
      error_plus(0, errno, "setuid");
438
618
    }
439
619
    
440
620
    setsid();
441
621
    ret = chdir("/");
 
622
    if(ret == -1){
 
623
      error_plus(0, errno, "chdir");
 
624
      _exit(EX_OSERR);
 
625
    }
442
626
/*     if(fork() != 0){ */
443
627
/*       _exit(EXIT_SUCCESS); */
444
628
/*     } */
445
629
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
446
630
    if(ret == -1){
447
 
      perror("dup2");
448
 
      _exit(EXIT_FAILURE);
 
631
      error_plus(0, errno, "dup2");
 
632
      _exit(EX_OSERR);
449
633
    }
450
634
    
451
635
    execv(usplash_name, cmdline_argv);
 
636
    if(not interrupted_by_signal){
 
637
      error_plus(0, errno, "execv");
 
638
    }
 
639
    free(cmdline);
 
640
    free(cmdline_argv);
 
641
    _exit(EX_OSERR);
452
642
  }
 
643
  free(cmdline);
 
644
  free(cmdline_argv);
453
645
  sleep(2);
454
 
  if(not usplash_write("PULSATE", NULL)
455
 
     and (errno != EINTR)){
456
 
    perror("usplash_write");
457
 
  }
458
 
  
459
 
  return EXIT_FAILURE;
 
646
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
 
647
    if(errno != EINTR){
 
648
      error_plus(0, errno, "usplash_write");
 
649
    }
 
650
  }
 
651
  
 
652
  /* Close FIFO (again) */
 
653
  if(fifo_fd != -1){
 
654
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
655
    if(ret == -1 and errno != EINTR){
 
656
      error_plus(0, errno, "close");
 
657
    }
 
658
    fifo_fd = -1;
 
659
  }
 
660
  
 
661
  if(interrupted_by_signal){
 
662
    struct sigaction signal_action = { .sa_handler = SIG_DFL };
 
663
    sigemptyset(&signal_action.sa_mask);
 
664
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
665
                                            &signal_action, NULL));
 
666
    if(ret == -1){
 
667
      error_plus(0, errno, "sigaction");
 
668
    }
 
669
    do {
 
670
      ret = raise(signal_received);
 
671
    } while(ret != 0 and errno == EINTR);
 
672
    if(ret != 0){
 
673
      error_plus(0, errno, "raise");
 
674
      abort();
 
675
    }
 
676
    TEMP_FAILURE_RETRY(pause());
 
677
  }
 
678
  
 
679
  return status;
460
680
}