1
 
/*  -*- coding: utf-8 -*- */
 
3
 
 * Plymouth - Read a password from Plymouth and output it
 
5
 
 * Copyright © 2010-2022 Teddy Hogeborn
 
6
 
 * Copyright © 2010-2022 Björn Påhlsson
 
8
 
 * This file is part of Mandos.
 
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.
 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
16
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 
 * General Public License for more details.
 
20
 
 * You should have received a copy of the GNU General Public License
 
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
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,
 
35
 
                                   ARGP_ERR_UNKNOWN, struct argp,
 
36
 
                                   argp_parse(), ARGP_IN_ORDER */
 
37
 
#include <stddef.h>             /* NULL, size_t */
 
38
 
#include <stdbool.h>            /* bool, false, true */
 
39
 
#include <stdio.h>              /* FILE, fprintf(), vfprintf(),
 
40
 
                                   vasprintf(), stderr, asprintf(),
 
41
 
                                   fopen(), fscanf(), fclose(),
 
43
 
#include <stdarg.h>             /* va_list, va_start(), vfprintf() */
 
44
 
#include <errno.h>              /* program_invocation_short_name,
 
45
 
                                   errno, ENOMEM, EINTR, ENOENT,
 
47
 
#include <string.h>             /* strerror(), strdup(), memcmp() */
 
48
 
#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(),
 
64
 
#include <iso646.h>             /* not, and, or */
 
65
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE,
 
67
 
#include <stdint.h>             /* SIZE_MAX */
 
68
 
#include <dirent.h>             /* struct dirent, scandir(),
 
70
 
#include <inttypes.h>           /* uintmax_t, strtoumax(), SCNuMAX,
 
72
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK() */
 
73
 
#include <fcntl.h>              /* open(), O_RDONLY */
 
74
 
#include <argz.h>               /* argz_count(), argz_extract() */
 
76
 
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>";
 
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";
 
87
 
const char plymouth_path[] = "/bin/plymouth";
 
88
 
const char plymouthd_path[] = "/sbin/plymouthd";
 
89
 
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
 
91
 
                                        "--attach-to-session",
 
95
 
static void termination_handler(__attribute__((unused))int signum){
 
96
 
  if(interrupted_by_signal){
 
99
 
  interrupted_by_signal = 1;
 
102
 
__attribute__((format (gnu_printf, 2, 3), nonnull))
 
103
 
int fprintf_plus(FILE *stream, const char *format, ...){
 
105
 
  va_start (ap, format);
 
106
 
  fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name);
 
107
 
  return vfprintf(stream, format, ap);
 
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,
 
118
 
  va_start(ap, formatstring);
 
119
 
  ret = vasprintf(&text, formatstring, ap);
 
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");
 
129
 
  fprintf(stderr, "Mandos plugin ");
 
130
 
  error(status, errnum, "%s", text);
 
134
 
/* Create prompt string */
 
135
 
char *makeprompt(void){
 
138
 
  const char *const cryptsource = getenv("cryptsource");
 
139
 
  const char *const crypttarget = getenv("crypttarget");
 
140
 
  const char prompt_start[] = "Unlocking the disk";
 
141
 
  const char prompt_end[] = "Enter passphrase";
 
143
 
  if(cryptsource == NULL){
 
144
 
    if(crypttarget == NULL){
 
145
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
147
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
 
148
 
                     crypttarget, prompt_end);
 
151
 
    if(crypttarget == NULL){
 
152
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
 
155
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
 
156
 
                     cryptsource, crypttarget, prompt_end);
 
165
 
void kill_and_wait(pid_t pid){
 
166
 
  TEMP_FAILURE_RETRY(kill(pid, SIGTERM));
 
167
 
  TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
 
170
 
bool become_a_daemon(void){
 
171
 
  int ret = setuid(geteuid());
 
173
 
    error_plus(0, errno, "setuid");
 
179
 
    error_plus(0, errno, "chdir");
 
182
 
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
184
 
    error_plus(0, errno, "dup2");
 
190
 
__attribute__((nonnull (2, 3)))
 
191
 
bool exec_and_wait(pid_t *pid_return, const char *path,
 
192
 
                   const char * const * const argv, bool interruptable,
 
198
 
    for(const char * const *arg = argv; *arg != NULL; arg++){
 
199
 
      fprintf_plus(stderr, "exec_and_wait arg: %s\n", *arg);
 
201
 
    fprintf_plus(stderr, "exec_and_wait end of args\n");
 
206
 
    error_plus(0, errno, "fork");
 
212
 
      if(not become_a_daemon()){
 
217
 
    char **new_argv = malloc(sizeof(const char *));
 
218
 
    if(new_argv == NULL){
 
219
 
      error_plus(0, errno, "malloc");
 
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 *));
 
229
 
      if(((size_t)i + 2) > (SIZE_MAX / sizeof(const char *))){
 
234
 
        tmp = realloc(new_argv, ((size_t)i + 2) * sizeof(const char *));
 
238
 
        error_plus(0, errno, "reallocarray");
 
243
 
      new_argv[i] = strdup(argv[i]);
 
247
 
    execv(path, (char *const *)new_argv);
 
248
 
    error_plus(0, errno, "execv");
 
251
 
  if(pid_return != NULL){
 
255
 
    ret = waitpid(pid, &status, 0);
 
256
 
  } while(ret == -1 and errno == EINTR
 
257
 
          and ((not interrupted_by_signal)
 
258
 
               or (not interruptable)));
 
259
 
  if(interrupted_by_signal and interruptable){
 
261
 
      fprintf_plus(stderr, "Interrupted by signal\n");
 
266
 
    error_plus(0, errno, "waitpid");
 
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",
 
278
 
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
 
284
 
__attribute__((nonnull))
 
285
 
int is_plymouth(const struct dirent *proc_entry){
 
291
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
293
 
    if(errno != 0 or *tmp != '\0'
 
294
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
298
 
  char exe_target[sizeof(plymouthd_path)];
 
300
 
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
 
302
 
    error_plus(0, errno, "asprintf");
 
306
 
  struct stat exe_stat;
 
307
 
  ret = lstat(exe_link, &exe_stat);
 
311
 
      error_plus(0, errno, "lstat");
 
316
 
  if(not S_ISLNK(exe_stat.st_mode)
 
317
 
     or exe_stat.st_uid != 0
 
318
 
     or exe_stat.st_gid != 0){
 
323
 
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
325
 
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
 
326
 
      (memcmp(plymouthd_path, exe_target,
 
327
 
              sizeof(plymouthd_path)-1) != 0)){
 
335
 
  uintmax_t proc_id = 0;
 
336
 
  FILE *pidfile = fopen(plymouth_pid, "r");
 
337
 
  /* Try the new pid file location */
 
339
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
345
 
  /* Try the old pid file location */
 
347
 
    pidfile = fopen(plymouth_old_pid, "r");
 
349
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
356
 
  /* Try the old old pid file location */
 
358
 
    pidfile = fopen(plymouth_old_old_pid, "r");
 
360
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
367
 
  /* Look for a plymouth process */
 
369
 
    struct dirent **direntries = NULL;
 
370
 
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
 
372
 
      error_plus(0, errno, "scandir");
 
375
 
      for(int i = ret-1; i >= 0; i--){
 
377
 
          ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
 
379
 
            error_plus(0, errno, "sscanf");
 
385
 
    /* scandir might preallocate for this variable (man page unclear).
 
386
 
       even if ret == 0, therefore we need to free it. */
 
390
 
  pid = (pid_t)proc_id;
 
391
 
  if((uintmax_t)pid == proc_id){
 
398
 
char **getargv(pid_t pid){
 
400
 
  char *cmdline_filename;
 
404
 
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
 
407
 
    error_plus(0, errno, "asprintf");
 
411
 
  /* Open /proc/<pid>/cmdline  */
 
412
 
  cl_fd = open(cmdline_filename, O_RDONLY);
 
413
 
  free(cmdline_filename);
 
415
 
    error_plus(0, errno, "open");
 
419
 
  size_t cmdline_allocated = 0;
 
420
 
  size_t cmdline_len = 0;
 
421
 
  char *cmdline = NULL;
 
423
 
  const size_t blocksize = 1024;
 
425
 
    /* Allocate more space? */
 
426
 
    if(cmdline_len + blocksize > cmdline_allocated){
 
427
 
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
 
429
 
        error_plus(0, errno, "realloc");
 
435
 
      cmdline_allocated += blocksize;
 
439
 
    sret = read(cl_fd, cmdline + cmdline_len,
 
440
 
                cmdline_allocated - cmdline_len);
 
442
 
      error_plus(0, errno, "read");
 
447
 
    cmdline_len += (size_t)sret;
 
451
 
    error_plus(0, errno, "close");
 
456
 
  /* we got cmdline and cmdline_len, ignore rest... */
 
457
 
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
 
458
 
                       * sizeof(char *)); /* Get number of args */
 
460
 
    error_plus(0, errno, "argv = malloc()");
 
464
 
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
 
468
 
int main(__attribute__((unused))int argc,
 
469
 
         __attribute__((unused))char **argv){
 
472
 
  pid_t plymouth_command_pid;
 
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" },
 
485
 
    __attribute__((nonnull(3)))
 
486
 
    error_t parse_opt (int key, char *arg, __attribute__((unused))
 
487
 
                       struct argp_state *state){
 
490
 
      case 128:                 /* --prompt */
 
493
 
          fprintf_plus(stderr, "Custom prompt \"%s\"\n", prompt);
 
496
 
      case 129:                 /* --debug */
 
500
 
        return ARGP_ERR_UNKNOWN;
 
505
 
    struct argp argp = { .options = options, .parser = parse_opt,
 
507
 
                         .doc = "Mandos plymouth -- Read and"
 
508
 
                         " output a password" };
 
509
 
    ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
 
516
 
      error_plus(0, errno, "argp_parse");
 
519
 
      error_plus(0, errno, "argp_parse");
 
524
 
  /* test -x /bin/plymouth */
 
525
 
  ret = access(plymouth_path, X_OK);
 
527
 
    /* Plymouth is probably not installed.  Don't print an error
 
528
 
       message, just exit. */
 
530
 
      fprintf_plus(stderr, "Plymouth (%s) not found\n",
 
533
 
    exit(EX_UNAVAILABLE);
 
536
 
  { /* Add signal handlers */
 
537
 
    struct sigaction old_action,
 
538
 
      new_action = { .sa_handler = termination_handler,
 
540
 
    sigemptyset(&new_action.sa_mask);
 
541
 
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
 
543
 
      ret = sigaddset(&new_action.sa_mask, *sig);
 
545
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
547
 
      ret = sigaction(*sig, NULL, &old_action);
 
549
 
        error_plus(EX_OSERR, errno, "sigaction");
 
551
 
      if(old_action.sa_handler != SIG_IGN){
 
552
 
        ret = sigaction(*sig, &new_action, NULL);
 
554
 
          error_plus(EX_OSERR, errno, "sigaction");
 
560
 
  /* plymouth --ping */
 
561
 
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
 
563
 
                       { plymouth_path, "--ping", NULL },
 
566
 
    if(interrupted_by_signal){
 
567
 
      kill_and_wait(plymouth_command_pid);
 
570
 
    /* Plymouth is probably not running.  Don't print an error
 
571
 
       message, just exit. */
 
573
 
      fprintf_plus(stderr, "Plymouth not running\n");
 
575
 
    exit(EX_UNAVAILABLE);
 
579
 
    ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
 
581
 
    char *made_prompt = makeprompt();
 
582
 
    ret = asprintf(&prompt_arg, "--prompt=%s", made_prompt);
 
586
 
    error_plus(EX_OSERR, errno, "asprintf");
 
589
 
  /* plymouth ask-for-password --prompt="$prompt" */
 
591
 
    fprintf_plus(stderr, "Prompting for password via Plymouth\n");
 
593
 
  bret = exec_and_wait(&plymouth_command_pid,
 
594
 
                       plymouth_path, (const char *[])
 
595
 
                       { plymouth_path, "ask-for-password",
 
602
 
  if(not interrupted_by_signal){
 
603
 
    /* exec_and_wait failed for some other reason */
 
606
 
  kill_and_wait(plymouth_command_pid);
 
608
 
  char **plymouthd_argv = NULL;
 
609
 
  pid_t pid = get_pid();
 
611
 
    error_plus(0, 0, "plymouthd pid not found");
 
613
 
    plymouthd_argv = getargv(pid);
 
616
 
  bret = exec_and_wait(NULL, plymouth_path, (const char *[])
 
617
 
                       { plymouth_path, "quit", NULL },
 
620
 
    if(plymouthd_argv != NULL){
 
621
 
      free(*plymouthd_argv);
 
622
 
      free(plymouthd_argv);
 
626
 
  bret = exec_and_wait(NULL, plymouthd_path,
 
627
 
                       (plymouthd_argv != NULL)
 
628
 
                       ? (const char * const *)plymouthd_argv
 
629
 
                       : plymouthd_default_argv,
 
631
 
  if(plymouthd_argv != NULL){
 
632
 
    free(*plymouthd_argv);
 
633
 
    free(plymouthd_argv);
 
638
 
  exec_and_wait(NULL, plymouth_path, (const char *[])
 
639
 
                { plymouth_path, "show-splash", NULL },