/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/plymouth.c

  • Committer: Björn Påhlsson
  • Date: 2010-11-05 18:30:07 UTC
  • mto: This revision was merged to the branch mainline in revision 463.
  • Revision ID: belorn@fukt.bsnet.se-20101105183007-20us6fal38a60w4u
password-prompt: added conflic handling with plymouth daemon.
plymouth: bug fix when --ping dont return sucessful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Plymouth - Read a password from Plymouth and output it
4
 
 * 
5
 
 * Copyright © 2010-2021 Teddy Hogeborn
6
 
 * Copyright © 2010-2021 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
3
 * Usplash - Read a password from usplash and output it
 
4
 * 
 
5
 * Copyright © 2010 Teddy Hogeborn
 
6
 * Copyright © 2010 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
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
 
#define _GNU_SOURCE             /* program_invocation_short_name,
27
 
                                   vasprintf(), asprintf(),
28
 
                                   TEMP_FAILURE_RETRY() */
29
 
#include <sys/types.h>          /* sig_atomic_t, pid_t, setuid(),
30
 
                                   geteuid(), setsid() */
31
 
#include <argp.h>               /* argp_program_version,
32
 
                                   argp_program_bug_address,
33
 
                                   struct argp_option,
34
 
                                   struct argp_state,
35
 
                                   ARGP_ERR_UNKNOWN, struct argp,
36
 
                                   argp_parse(), ARGP_IN_ORDER */
37
 
#include <stddef.h>             /* NULL, size_t */
 
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
 
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
 
27
                                   sigemptyset(), sigaddset(), SIGINT,
 
28
                                   SIGHUP, SIGTERM, sigaction(),
 
29
                                   kill(), SIG_IGN */
38
30
#include <stdbool.h>            /* bool, false, true */
39
 
#include <stdio.h>              /* FILE, fprintf(), vfprintf(),
40
 
                                   vasprintf(), stderr, asprintf(),
41
 
                                   fopen(), fscanf(), fclose(),
42
 
                                   sscanf() */
43
 
#include <stdarg.h>             /* va_list, va_start(), vfprintf() */
44
 
#include <errno.h>              /* program_invocation_short_name,
45
 
                                   errno, ENOMEM, EINTR, ENOENT,
46
 
                                   error_t, EINVAL */
47
 
#include <string.h>             /* strerror(), strdup(), memcmp() */
 
31
#include <fcntl.h>              /* open(), O_RDONLY */
 
32
#include <iso646.h>             /* and, or, not*/
 
33
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct
 
34
                                   dirent, waitpid() */
 
35
#include <sys/wait.h>           /* waitpid() */
 
36
#include <stddef.h>             /* NULL */
 
37
#include <string.h>             /* strchr(), memcmp() */
 
38
#include <stdio.h>              /* asprintf(), perror(), fopen(),
 
39
                                   fscanf() */
 
40
#include <unistd.h>             /* close(), readlink(), read(),
 
41
                                   fork(), setsid(), chdir(), dup2(),
 
42
                                   STDERR_FILENO, execv(), access() */
 
43
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
44
                                   EXIT_SUCCESS, malloc(), _exit(),
 
45
                                   getenv() */
 
46
#include <dirent.h>             /* scandir(), alphasort() */
 
47
#include <inttypes.h>           /* intmax_t, strtoumax(), SCNuMAX */
 
48
#include <sys/stat.h>           /* struct stat, lstat() */
 
49
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
48
50
#include <error.h>              /* error() */
49
 
#include <stdlib.h>             /* free(), getenv(), malloc(),
50
 
                                   reallocarray(), realloc(),
51
 
                                   EXIT_FAILURE, EXIT_SUCCESS */
52
 
#include <unistd.h>             /* TEMP_FAILURE_RETRY(), setuid(),
53
 
                                   geteuid(), setsid(), chdir(),
54
 
                                   dup2(), STDERR_FILENO,
55
 
                                   STDOUT_FILENO, fork(), _exit(),
56
 
                                   execv(), ssize_t, readlink(),
57
 
                                   close(), read(), access(), X_OK */
58
 
#include <signal.h>             /* kill(), SIGTERM, struct sigaction,
59
 
                                   sigemptyset(), SIGINT, SIGHUP,
60
 
                                   sigaddset(), SIG_IGN */
61
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
62
 
                                   WEXITSTATUS(), WIFSIGNALED(),
63
 
                                   WTERMSIG() */
64
 
#include <iso646.h>             /* not, and, or */
65
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE,
66
 
                                   EX_UNAVAILABLE */
67
 
#include <stdint.h>             /* SIZE_MAX */
68
 
#include <dirent.h>             /* struct dirent, scandir(),
69
 
                                   alphasort() */
70
 
#include <inttypes.h>           /* uintmax_t, strtoumax(), SCNuMAX,
71
 
                                   PRIuMAX */
72
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK() */
73
 
#include <fcntl.h>              /* open(), O_RDONLY */
 
51
#include <errno.h>              /* TEMP_FAILURE_RETRY */
74
52
#include <argz.h>               /* argz_count(), argz_extract() */
75
53
 
76
54
sig_atomic_t interrupted_by_signal = 0;
77
 
const char *argp_program_version = "plymouth " VERSION;
78
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
79
 
 
80
 
/* Used by Ubuntu 11.04 (Natty Narwahl) */
81
 
const char plymouth_old_old_pid[] = "/dev/.initramfs/plymouth.pid";
82
 
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
83
 
const char plymouth_old_pid[] = "/run/initramfs/plymouth.pid";
84
 
/* Used by Debian 9 (stretch) */
85
 
const char plymouth_pid[] = "/run/plymouth/pid";
86
 
 
 
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
87
56
const char plymouth_path[] = "/bin/plymouth";
88
57
const char plymouthd_path[] = "/sbin/plymouthd";
89
58
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
90
59
                                        "--mode=boot",
91
60
                                        "--attach-to-session",
 
61
                                        "--pid-file="
 
62
                                        "/dev/.initramfs/"
 
63
                                        "plymouth.pid",
92
64
                                        NULL };
93
 
bool debug = false;
94
65
 
95
66
static void termination_handler(__attribute__((unused))int signum){
96
67
  if(interrupted_by_signal){
99
70
  interrupted_by_signal = 1;
100
71
}
101
72
 
102
 
__attribute__((format (gnu_printf, 2, 3), nonnull))
103
 
int fprintf_plus(FILE *stream, const char *format, ...){
104
 
  va_list ap;
105
 
  va_start (ap, format);
106
 
  fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name);
107
 
  return vfprintf(stream, format, ap);
108
 
}
109
 
 
110
 
/* Function to use when printing errors */
111
 
__attribute__((format (gnu_printf, 3, 4)))
112
 
void error_plus(int status, int errnum, const char *formatstring,
113
 
                ...){
114
 
  va_list ap;
115
 
  char *text;
116
 
  int ret;
117
 
  
118
 
  va_start(ap, formatstring);
119
 
  ret = vasprintf(&text, formatstring, ap);
120
 
  if(ret == -1){
121
 
    fprintf(stderr, "Mandos plugin %s: ",
122
 
            program_invocation_short_name);
123
 
    vfprintf(stderr, formatstring, ap);
124
 
    fprintf(stderr, ": ");
125
 
    fprintf(stderr, "%s\n", strerror(errnum));
126
 
    error(status, errno, "vasprintf while printing error");
127
 
    return;
128
 
  }
129
 
  fprintf(stderr, "Mandos plugin ");
130
 
  error(status, errnum, "%s", text);
131
 
  free(text);
132
 
}
133
 
 
134
73
/* Create prompt string */
135
74
char *makeprompt(void){
136
75
  int ret = 0;
137
76
  char *prompt;
138
77
  const char *const cryptsource = getenv("cryptsource");
139
78
  const char *const crypttarget = getenv("crypttarget");
140
 
  const char prompt_start[] = "Unlocking the disk";
141
 
  const char prompt_end[] = "Enter passphrase";
 
79
  const char prompt_start[] = "Enter passphrase to unlock the disk";
142
80
  
143
81
  if(cryptsource == NULL){
144
82
    if(crypttarget == NULL){
145
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
83
      ret = asprintf(&prompt, "%s: ", prompt_start);
146
84
    } else {
147
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
148
 
                     crypttarget, prompt_end);
 
85
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
86
                     crypttarget);
149
87
    }
150
88
  } else {
151
89
    if(crypttarget == NULL){
152
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
153
 
                     prompt_end);
 
90
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
154
91
    } else {
155
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
156
 
                     cryptsource, crypttarget, prompt_end);
 
92
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
93
                     cryptsource, crypttarget);
157
94
    }
158
95
  }
159
96
  if(ret == -1){
170
107
bool become_a_daemon(void){
171
108
  int ret = setuid(geteuid());
172
109
  if(ret == -1){
173
 
    error_plus(0, errno, "setuid");
 
110
    error(0, errno, "setuid");
174
111
  }
175
112
    
176
113
  setsid();
177
114
  ret = chdir("/");
178
115
  if(ret == -1){
179
 
    error_plus(0, errno, "chdir");
 
116
    error(0, errno, "chdir");
180
117
    return false;
181
118
  }
182
119
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
183
120
  if(ret == -1){
184
 
    error_plus(0, errno, "dup2");
 
121
    error(0, errno, "dup2");
185
122
    return false;
186
123
  }
187
124
  return true;
188
125
}
189
126
 
190
 
__attribute__((nonnull (2, 3)))
191
127
bool exec_and_wait(pid_t *pid_return, const char *path,
192
 
                   const char * const * const argv, bool interruptable,
 
128
                   const char **argv, bool interruptable,
193
129
                   bool daemonize){
194
130
  int status;
195
131
  int ret;
196
132
  pid_t pid;
197
 
  if(debug){
198
 
    for(const char * const *arg = argv; *arg != NULL; arg++){
199
 
      fprintf_plus(stderr, "exec_and_wait arg: %s\n", *arg);
200
 
    }
201
 
    fprintf_plus(stderr, "exec_and_wait end of args\n");
202
 
  }
203
 
 
204
133
  pid = fork();
205
134
  if(pid == -1){
206
 
    error_plus(0, errno, "fork");
 
135
    error(0, errno, "fork");
207
136
    return false;
208
137
  }
209
138
  if(pid == 0){
213
142
        _exit(EX_OSERR);
214
143
      }
215
144
    }
216
 
    
217
 
    char **new_argv = malloc(sizeof(const char *));
218
 
    if(new_argv == NULL){
219
 
      error_plus(0, errno, "malloc");
220
 
      _exit(EX_OSERR);
221
 
    }
222
 
    char **tmp;
 
145
 
 
146
    char **new_argv = NULL;
 
147
    char *tmp;
223
148
    int i = 0;
224
 
    for (; argv[i] != NULL; i++){
225
 
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
226
 
      tmp = reallocarray(new_argv, ((size_t)i + 2),
227
 
                         sizeof(const char *));
228
 
#else
229
 
      if(((size_t)i + 2) > (SIZE_MAX / sizeof(const char *))){
230
 
        /* overflow */
231
 
        tmp = NULL;
232
 
        errno = ENOMEM;
233
 
      } else {
234
 
        tmp = realloc(new_argv, ((size_t)i + 2) * sizeof(const char *));
235
 
      }
236
 
#endif
237
 
      if(tmp == NULL){
238
 
        error_plus(0, errno, "reallocarray");
 
149
    for (; argv[i]!=(char *)NULL; i++){
 
150
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
151
      if (tmp == NULL){
 
152
        error(0, errno, "realloc");
239
153
        free(new_argv);
240
154
        _exit(EX_OSERR);
241
155
      }
242
 
      new_argv = tmp;
 
156
      new_argv = (char **)tmp;
243
157
      new_argv[i] = strdup(argv[i]);
244
158
    }
245
 
    new_argv[i] = NULL;
 
159
    new_argv[i] = (char *) NULL;
246
160
    
247
161
    execv(path, (char *const *)new_argv);
248
 
    error_plus(0, errno, "execv");
 
162
    error(0, errno, "execv");
249
163
    _exit(EXIT_FAILURE);
250
164
  }
251
165
  if(pid_return != NULL){
257
171
          and ((not interrupted_by_signal)
258
172
               or (not interruptable)));
259
173
  if(interrupted_by_signal and interruptable){
260
 
    if(debug){
261
 
      fprintf_plus(stderr, "Interrupted by signal\n");
262
 
    }
263
174
    return false;
264
175
  }
265
176
  if(ret == -1){
266
 
    error_plus(0, errno, "waitpid");
 
177
    error(0, errno, "waitpid");
267
178
    return false;
268
179
  }
269
 
  if(debug){
270
 
    if(WIFEXITED(status)){
271
 
      fprintf_plus(stderr, "exec_and_wait exited: %d\n",
272
 
                   WEXITSTATUS(status));
273
 
    } else if(WIFSIGNALED(status)) {
274
 
      fprintf_plus(stderr, "exec_and_wait signaled: %d\n",
275
 
                   WTERMSIG(status));
276
 
    }
277
 
  }
278
180
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
279
181
    return true;
280
182
  }
281
183
  return false;
282
184
}
283
185
 
284
 
__attribute__((nonnull))
285
186
int is_plymouth(const struct dirent *proc_entry){
286
187
  int ret;
287
188
  {
288
 
    uintmax_t proc_id;
 
189
    uintmax_t maxvalue;
289
190
    char *tmp;
290
191
    errno = 0;
291
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
192
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
292
193
 
293
194
    if(errno != 0 or *tmp != '\0'
294
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
195
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
295
196
      return 0;
296
197
    }
297
198
  }
299
200
  char *exe_link;
300
201
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
301
202
  if(ret == -1){
302
 
    error_plus(0, errno, "asprintf");
 
203
    error(0, errno, "asprintf");
303
204
    return 0;
304
205
  }
305
206
  
308
209
  if(ret == -1){
309
210
    free(exe_link);
310
211
    if(errno != ENOENT){
311
 
      error_plus(0, errno, "lstat");
 
212
      error(0, errno, "lstat");
312
213
    }
313
214
    return 0;
314
215
  }
332
233
 
333
234
pid_t get_pid(void){
334
235
  int ret;
335
 
  uintmax_t proc_id = 0;
336
236
  FILE *pidfile = fopen(plymouth_pid, "r");
337
 
  /* Try the new pid file location */
 
237
  uintmax_t maxvalue = 0;
338
238
  if(pidfile != NULL){
339
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
239
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
340
240
    if(ret != 1){
341
 
      proc_id = 0;
 
241
      maxvalue = 0;
342
242
    }
343
243
    fclose(pidfile);
344
244
  }
345
 
  /* Try the old pid file location */
346
 
  if(proc_id == 0){
347
 
    pidfile = fopen(plymouth_old_pid, "r");
348
 
    if(pidfile != NULL){
349
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
350
 
      if(ret != 1){
351
 
        proc_id = 0;
352
 
      }
353
 
      fclose(pidfile);
354
 
    }
355
 
  }
356
 
  /* Try the old old pid file location */
357
 
  if(proc_id == 0){
358
 
    pidfile = fopen(plymouth_old_old_pid, "r");
359
 
    if(pidfile != NULL){
360
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
361
 
      if(ret != 1){
362
 
        proc_id = 0;
363
 
      }
364
 
      fclose(pidfile);
365
 
    }
366
 
  }
367
 
  /* Look for a plymouth process */
368
 
  if(proc_id == 0){
369
 
    struct dirent **direntries = NULL;
 
245
  if(maxvalue == 0){
 
246
    struct dirent **direntries;
370
247
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
371
 
    if(ret == -1){
372
 
      error_plus(0, errno, "scandir");
 
248
    if (ret == -1){
 
249
      error(0, errno, "scandir");
373
250
    }
374
 
    if(ret > 0){
375
 
      for(int i = ret-1; i >= 0; i--){
376
 
        if(proc_id == 0){
377
 
          ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
378
 
          if(ret < 0){
379
 
            error_plus(0, errno, "sscanf");
380
 
          }
381
 
        }
382
 
        free(direntries[i]);
 
251
    if (ret > 0){
 
252
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
253
      if (ret < 0){
 
254
        error(0, errno, "sscanf");
383
255
      }
384
256
    }
385
 
    /* scandir might preallocate for this variable (man page unclear).
386
 
       even if ret == 0, therefore we need to free it. */
387
 
    free(direntries);
388
257
  }
389
258
  pid_t pid;
390
 
  pid = (pid_t)proc_id;
391
 
  if((uintmax_t)pid == proc_id){
 
259
  pid = (pid_t)maxvalue;
 
260
  if((uintmax_t)pid == maxvalue){
392
261
    return pid;
393
262
  }
394
263
  
395
264
  return 0;
396
265
}
397
266
 
398
 
char **getargv(pid_t pid){
 
267
const char **getargv(pid_t pid){
399
268
  int cl_fd;
400
269
  char *cmdline_filename;
401
270
  ssize_t sret;
404
273
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
405
274
                 (uintmax_t)pid);
406
275
  if(ret == -1){
407
 
    error_plus(0, errno, "asprintf");
 
276
    error(0, errno, "asprintf");
408
277
    return NULL;
409
278
  }
410
279
  
412
281
  cl_fd = open(cmdline_filename, O_RDONLY);
413
282
  free(cmdline_filename);
414
283
  if(cl_fd == -1){
415
 
    error_plus(0, errno, "open");
 
284
    error(0, errno, "open");
416
285
    return NULL;
417
286
  }
418
287
  
426
295
    if(cmdline_len + blocksize > cmdline_allocated){
427
296
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
428
297
      if(tmp == NULL){
429
 
        error_plus(0, errno, "realloc");
 
298
        error(0, errno, "realloc");
430
299
        free(cmdline);
431
300
        close(cl_fd);
432
301
        return NULL;
439
308
    sret = read(cl_fd, cmdline + cmdline_len,
440
309
                cmdline_allocated - cmdline_len);
441
310
    if(sret == -1){
442
 
      error_plus(0, errno, "read");
 
311
      error(0, errno, "read");
443
312
      free(cmdline);
444
313
      close(cl_fd);
445
314
      return NULL;
448
317
  } while(sret != 0);
449
318
  ret = close(cl_fd);
450
319
  if(ret == -1){
451
 
    error_plus(0, errno, "close");
 
320
    error(0, errno, "close");
452
321
    free(cmdline);
453
322
    return NULL;
454
323
  }
457
326
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
458
327
                       * sizeof(char *)); /* Get number of args */
459
328
  if(argv == NULL){
460
 
    error_plus(0, errno, "argv = malloc()");
 
329
    error(0, errno, "argv = malloc()");
461
330
    free(cmdline);
462
331
    return NULL;
463
332
  }
464
333
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
465
 
  return argv;
 
334
  return (const char **)argv;
466
335
}
467
336
 
468
337
int main(__attribute__((unused))int argc,
469
338
         __attribute__((unused))char **argv){
470
 
  char *prompt = NULL;
 
339
  char *prompt;
471
340
  char *prompt_arg;
472
341
  pid_t plymouth_command_pid;
473
342
  int ret;
474
343
  bool bret;
475
344
 
476
 
  {
477
 
    struct argp_option options[] = {
478
 
      { .name = "prompt", .key = 128, .arg = "PROMPT",
479
 
        .doc = "The prompt to show" },
480
 
      { .name = "debug", .key = 129,
481
 
        .doc = "Debug mode" },
482
 
      { .name = NULL }
483
 
    };
484
 
    
485
 
    __attribute__((nonnull(3)))
486
 
    error_t parse_opt (int key, char *arg, __attribute__((unused))
487
 
                       struct argp_state *state){
488
 
      errno = 0;
489
 
      switch (key){
490
 
      case 128:                 /* --prompt */
491
 
        prompt = arg;
492
 
        if(debug){
493
 
          fprintf_plus(stderr, "Custom prompt \"%s\"\n", prompt);
494
 
        }
495
 
        break;
496
 
      case 129:                 /* --debug */
497
 
        debug = true;
498
 
        break;
499
 
      default:
500
 
        return ARGP_ERR_UNKNOWN;
501
 
      }
502
 
      return errno;
503
 
    }
504
 
    
505
 
    struct argp argp = { .options = options, .parser = parse_opt,
506
 
                         .args_doc = "",
507
 
                         .doc = "Mandos plymouth -- Read and"
508
 
                         " output a password" };
509
 
    ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
510
 
    switch(ret){
511
 
    case 0:
512
 
      break;
513
 
    case ENOMEM:
514
 
    default:
515
 
      errno = ret;
516
 
      error_plus(0, errno, "argp_parse");
517
 
      return EX_OSERR;
518
 
    case EINVAL:
519
 
      error_plus(0, errno, "argp_parse");
520
 
      return EX_USAGE;
521
 
    }
522
 
  }
523
 
  
524
345
  /* test -x /bin/plymouth */
525
346
  ret = access(plymouth_path, X_OK);
526
347
  if(ret == -1){
527
348
    /* Plymouth is probably not installed.  Don't print an error
528
349
       message, just exit. */
529
 
    if(debug){
530
 
      fprintf_plus(stderr, "Plymouth (%s) not found\n",
531
 
                   plymouth_path);
532
 
    }
533
350
    exit(EX_UNAVAILABLE);
534
351
  }
535
352
  
542
359
        *sig != 0; sig++){
543
360
      ret = sigaddset(&new_action.sa_mask, *sig);
544
361
      if(ret == -1){
545
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
362
        error(EX_OSERR, errno, "sigaddset");
546
363
      }
547
364
      ret = sigaction(*sig, NULL, &old_action);
548
365
      if(ret == -1){
549
 
        error_plus(EX_OSERR, errno, "sigaction");
 
366
        error(EX_OSERR, errno, "sigaction");
550
367
      }
551
368
      if(old_action.sa_handler != SIG_IGN){
552
369
        ret = sigaction(*sig, &new_action, NULL);
553
370
        if(ret == -1){
554
 
          error_plus(EX_OSERR, errno, "sigaction");
 
371
          error(EX_OSERR, errno, "sigaction");
555
372
        }
556
373
      }
557
374
    }
569
386
    }
570
387
    /* Plymouth is probably not running.  Don't print an error
571
388
       message, just exit. */
572
 
    if(debug){
573
 
      fprintf_plus(stderr, "Plymouth not running\n");
574
 
    }
575
389
    exit(EX_UNAVAILABLE);
576
390
  }
577
391
  
578
 
  if(prompt != NULL){
579
 
    ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
580
 
  } else {
581
 
    char *made_prompt = makeprompt();
582
 
    ret = asprintf(&prompt_arg, "--prompt=%s", made_prompt);
583
 
    free(made_prompt);
584
 
  }
 
392
  prompt = makeprompt();
 
393
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
 
394
  free(prompt);
585
395
  if(ret == -1){
586
 
    error_plus(EX_OSERR, errno, "asprintf");
 
396
    error(EX_OSERR, errno, "asprintf");
587
397
  }
588
398
  
589
399
  /* plymouth ask-for-password --prompt="$prompt" */
590
 
  if(debug){
591
 
    fprintf_plus(stderr, "Prompting for password via Plymouth\n");
592
 
  }
593
400
  bret = exec_and_wait(&plymouth_command_pid,
594
401
                       plymouth_path, (const char *[])
595
402
                       { plymouth_path, "ask-for-password",
605
412
  }
606
413
  kill_and_wait(plymouth_command_pid);
607
414
  
608
 
  char **plymouthd_argv = NULL;
 
415
  const char **plymouthd_argv;
609
416
  pid_t pid = get_pid();
610
417
  if(pid == 0){
611
 
    error_plus(0, 0, "plymouthd pid not found");
 
418
    error(0, 0, "plymouthd pid not found");
 
419
    plymouthd_argv = plymouthd_default_argv;
612
420
  } else {
613
421
    plymouthd_argv = getargv(pid);
614
422
  }
617
425
                       { plymouth_path, "quit", NULL },
618
426
                       false, false);
619
427
  if(not bret){
620
 
    if(plymouthd_argv != NULL){
621
 
      free(*plymouthd_argv);
622
 
      free(plymouthd_argv);
623
 
    }
624
428
    exit(EXIT_FAILURE);
625
429
  }
626
 
  bret = exec_and_wait(NULL, plymouthd_path,
627
 
                       (plymouthd_argv != NULL)
628
 
                       ? (const char * const *)plymouthd_argv
629
 
                       : plymouthd_default_argv,
 
430
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
630
431
                       false, true);
631
 
  if(plymouthd_argv != NULL){
632
 
    free(*plymouthd_argv);
633
 
    free(plymouthd_argv);
634
 
  }
635
432
  if(not bret){
636
433
    exit(EXIT_FAILURE);
637
434
  }