1
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
2
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
sigemptyset(), sigaddset(), SIGINT,
4
SIGHUP, SIGTERM, sigaction(),
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 */
6
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() */
7
73
#include <fcntl.h> /* open(), O_RDONLY */
8
#include <iso646.h> /* and, or, not*/
9
#include <sys/types.h> /* size_t, ssize_t, pid_t, struct dirent,
11
#include <sys/wait.h> /* waitpid() */
12
#include <stddef.h> /* NULL */
13
#include <string.h> /* strchr(), memcmp() */
14
#include <stdio.h> /* asprintf(), perror(), fopen(), fscanf() */
15
#include <unistd.h> /* close(), readlink(), read(), fork()
16
setsid(), chdir(), dup2()
17
STDERR_FILENO, execv(), access() */
18
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
19
EXIT_SUCCESS, malloc(), _exit(),
21
#include <dirent.h> /* scandir(), alphasort() */
22
#include <inttypes.h> /* intmax_t, strtoumax(), SCNuMAX */
23
#include <sys/stat.h> /* struct stat, lstat() */
24
#include <sysexits.h> /* EX_OSERR */
25
#include <error.h> /* error() */
26
#include <errno.h> /* TEMP_FAILURE_RETRY */
27
74
#include <argz.h> /* argz_count(), argz_extract() */
29
76
sig_atomic_t interrupted_by_signal = 0;
30
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
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";
31
87
const char plymouth_path[] = "/bin/plymouth";
32
88
const char plymouthd_path[] = "/sbin/plymouthd";
33
const char *plymouthd_default_argv[] = {"/sbin/plymouthd", "--mode=boot",
89
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
34
91
"--attach-to-session",
35
"--pid-file=/dev/.initramfs/plymouth.pid",
38
95
static void termination_handler(__attribute__((unused))int signum){
39
96
if(interrupted_by_signal){
42
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);
45
134
/* Create prompt string */
46
135
char *makeprompt(void){
49
138
const char *const cryptsource = getenv("cryptsource");
50
139
const char *const crypttarget = getenv("crypttarget");
51
const char prompt_start[] = "Enter passphrase to unlock the disk";
140
const char prompt_start[] = "Unlocking the disk";
141
const char prompt_end[] = "Enter passphrase";
53
143
if(cryptsource == NULL){
54
144
if(crypttarget == NULL){
55
ret = asprintf(&prompt, "%s: ", prompt_start);
145
ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
57
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
147
ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
148
crypttarget, prompt_end);
61
151
if(crypttarget == NULL){
62
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
152
ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
64
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
65
cryptsource, crypttarget);
155
ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
156
cryptsource, crypttarget, prompt_end);
79
170
bool become_a_daemon(void){
80
171
int ret = setuid(geteuid());
82
error(0, errno, "setuid");
173
error_plus(0, errno, "setuid");
88
error(0, errno, "chdir");
179
error_plus(0, errno, "chdir");
91
182
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
93
error(0, errno, "dup2");
184
error_plus(0, errno, "dup2");
190
__attribute__((nonnull (2, 3)))
99
191
bool exec_and_wait(pid_t *pid_return, const char *path,
100
const char **argv, bool interruptable,
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");
107
error(0, errno, "fork");
206
error_plus(0, errno, "fork");
143
257
and ((not interrupted_by_signal)
144
258
or (not interruptable)));
145
259
if(interrupted_by_signal and interruptable){
261
fprintf_plus(stderr, "Interrupted by signal\n");
149
error(0, errno, "waitpid");
266
error_plus(0, errno, "waitpid");
152
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
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))
158
285
int is_plymouth(const struct dirent *proc_entry){
164
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
291
proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
166
if(errno != 0 or *tmp != '\0' or maxvalue != (uintmax_t)((pid_t)maxvalue)){
293
if(errno != 0 or *tmp != '\0'
294
or proc_id != (uintmax_t)((pid_t)proc_id)){
170
char exe_target[sizeof(plymouth_path)];
298
char exe_target[sizeof(plymouthd_path)];
172
300
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
174
error(0, errno, "asprintf");
302
error_plus(0, errno, "asprintf");
178
306
struct stat exe_stat;
179
307
ret = lstat(exe_link, &exe_stat);
182
310
if(errno != ENOENT){
183
error(0, errno, "lstat");
311
error_plus(0, errno, "lstat");
205
333
pid_t get_pid(void){
335
uintmax_t proc_id = 0;
207
336
FILE *pidfile = fopen(plymouth_pid, "r");
208
uintmax_t maxvalue = 0;
337
/* Try the new pid file location */
209
338
if(pidfile != NULL){
210
ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
339
ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
217
struct dirent **direntries;
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;
218
370
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
219
sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
372
error_plus(0, errno, "scandir");
375
const int num_entries = ret;
376
for(int i = 0; i < num_entries; i++){
378
ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
380
error_plus(0, errno, "sscanf");
386
/* scandir might preallocate for this variable (man page unclear).
387
even if ret == 0, therefore we need to free it. */
222
pid = (pid_t)maxvalue;
223
if((uintmax_t)pid == maxvalue){
391
pid = (pid_t)proc_id;
392
if((uintmax_t)pid == proc_id){
230
const char **getargv(pid_t pid){
399
char **getargv(pid_t pid){
232
401
char *cmdline_filename;
289
458
char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
290
459
* sizeof(char *)); /* Get number of args */
291
460
if(argv == NULL){
292
error(0, errno, "argv = malloc()");
461
error_plus(0, errno, "argv = malloc()");
296
465
argz_extract(cmdline, cmdline_len, argv); /* Create argv */
297
return (const char **)argv;
300
469
int main(__attribute__((unused))int argc,
301
470
__attribute__((unused))char **argv){
303
472
char *prompt_arg;
304
473
pid_t plymouth_command_pid;
478
struct argp_option options[] = {
479
{ .name = "prompt", .key = 128, .arg = "PROMPT",
480
.doc = "The prompt to show" },
481
{ .name = "debug", .key = 129,
482
.doc = "Debug mode" },
486
__attribute__((nonnull(3)))
487
error_t parse_opt (int key, char *arg, __attribute__((unused))
488
struct argp_state *state){
491
case 128: /* --prompt */
494
fprintf_plus(stderr, "Custom prompt \"%s\"\n", prompt);
497
case 129: /* --debug */
501
return ARGP_ERR_UNKNOWN;
506
struct argp argp = { .options = options, .parser = parse_opt,
508
.doc = "Mandos plymouth -- Read and"
509
" output a password" };
510
ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
517
error_plus(0, errno, "argp_parse");
520
error_plus(0, errno, "argp_parse");
308
525
/* test -x /bin/plymouth */
309
526
ret = access(plymouth_path, X_OK);
528
/* Plymouth is probably not installed. Don't print an error
529
message, just exit. */
531
fprintf_plus(stderr, "Plymouth (%s) not found\n",
534
exit(EX_UNAVAILABLE);
314
537
{ /* Add signal handlers */
315
538
struct sigaction old_action,
316
539
new_action = { .sa_handler = termination_handler,
318
541
sigemptyset(&new_action.sa_mask);
319
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
542
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
320
544
ret = sigaddset(&new_action.sa_mask, *sig);
322
error(0, errno, "sigaddset");
546
error_plus(EX_OSERR, errno, "sigaddset");
325
548
ret = sigaction(*sig, NULL, &old_action);
327
error(0, errno, "sigaction");
550
error_plus(EX_OSERR, errno, "sigaction");
330
552
if(old_action.sa_handler != SIG_IGN){
331
553
ret = sigaction(*sig, &new_action, NULL);
333
error(0, errno, "sigaction");
555
error_plus(EX_OSERR, errno, "sigaction");
340
561
/* plymouth --ping */
341
562
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
342
(const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
564
{ plymouth_path, "--ping", NULL },
345
567
if(interrupted_by_signal){
346
568
kill_and_wait(plymouth_command_pid);
571
/* Plymouth is probably not running. Don't print an error
572
message, just exit. */
574
fprintf_plus(stderr, "Plymouth not running\n");
576
exit(EX_UNAVAILABLE);
351
prompt = makeprompt();
352
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
580
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
582
char *made_prompt = makeprompt();
583
ret = asprintf(&prompt_arg, "--prompt=%s", made_prompt);
355
error(0, errno, "asprintf");
587
error_plus(EX_OSERR, errno, "asprintf");
359
590
/* plymouth ask-for-password --prompt="$prompt" */
360
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
361
(const char *[]){plymouth_path, "ask-for-password", prompt_arg, NULL},
592
fprintf_plus(stderr, "Prompting for password via Plymouth\n");
594
bret = exec_and_wait(&plymouth_command_pid,
595
plymouth_path, (const char *[])
596
{ plymouth_path, "ask-for-password",
363
599
free(prompt_arg);
365
if(interrupted_by_signal){
366
kill_and_wait(plymouth_command_pid);
373
601
exit(EXIT_SUCCESS);
603
if(not interrupted_by_signal){
604
/* exec_and_wait failed for some other reason */
607
kill_and_wait(plymouth_command_pid);
376
const char **plymouthd_argv = NULL;
609
char **plymouthd_argv = NULL;
377
610
pid_t pid = get_pid();
379
error(0, 0, "plymouthd pid not found");
612
error_plus(0, 0, "plymouthd pid not found");
381
614
plymouthd_argv = getargv(pid);
383
if(plymouthd_argv == NULL){
384
plymouthd_argv = plymouthd_default_argv;
387
bret = exec_and_wait(NULL, plymouth_path,
388
(const char *[]){plymouth_path, "quit", NULL}, false, false);
392
bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv, false, true);
396
exec_and_wait(NULL, plymouth_path,
397
(const char *[]){ plymouth_path, "show-splash", NULL }, false, false);
617
bret = exec_and_wait(NULL, plymouth_path, (const char *[])
618
{ plymouth_path, "quit", NULL },
621
if(plymouthd_argv != NULL){
622
free(*plymouthd_argv);
623
free(plymouthd_argv);
627
bret = exec_and_wait(NULL, plymouthd_path,
628
(plymouthd_argv != NULL)
629
? (const char * const *)plymouthd_argv
630
: plymouthd_default_argv,
632
if(plymouthd_argv != NULL){
633
free(*plymouthd_argv);
634
free(plymouthd_argv);
639
exec_and_wait(NULL, plymouth_path, (const char *[])
640
{ plymouth_path, "show-splash", NULL },
398
642
exit(EXIT_FAILURE);