/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/usplash.c

Convert some programs to use the exit codes from <sysexits.h>.  Change
all programs using the "argp" parsing functions to use them correctly;
checking return value, using argp_error() to report parse errors etc.

* plugin-runner.c: Use <sysexits.h> exit codes.  Always use fallback,
                   even on option errors, except for "--help", etc.
  (getplugin): Make sure "errno" is set correctly on return.
  (main): Declare our own "--help", "--usage", and "--version"
          options which do not cause the fallback to be invoked.
          In all other options, use fallback on any error.
  (parse_opt, parse_opt_config_file): Reset errno at start and return
                                      errno.  No need to check "arg"
                                      for NULL.  New "--help",
                                      "--usage", and "--version"
                                      options.
  (parse_opt): Accept empty string as global option.  Do not print
               errors which will be detected and reported later.  Do
               "argp_error()" on parse error or empty plugin names.
* plugins.d/mandos-client.c: Use <sysexits.h> exit codes.  Do not
                             return successful exit code on "--help",
                             etc. since this would give the wrong
                             message to "plugin-runner".
  (main): Declare our own "--help", "--usage", and "--version"
          options which do not return a successful exit code.
  (parse_opt): Reset errno at start and return errno.  Do
               "argp_error()" on parse errors.  New "--help",
               "--usage", and "--version" options.
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>.  Do
                               not return successful exit code on
                               "--help", etc. since this would give
                               the wrong message to "plugin-runner".
  (main): Declare our own "--help", "--usage", and "--version" options
          which do not return a successful exit code.  Do
          close(STDOUT_FILENO) after writing to check its return code.
  (parse_opt): Reset errno at start and return errno.

Show diffs side-by-side

added added

removed removed

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