1
/* -*- coding: utf-8 -*- */
3
* Plymouth - Read a password from Plymouth and output it
5
* Copyright © 2010-2011 Teddy Hogeborn
6
* Copyright © 2010-2011 Björn Påhlsson
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.
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.
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/>.
22
* Contact the authors at <mandos@recompile.se>.
1
25
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
2
26
#include <signal.h> /* sig_atomic_t, struct sigaction,
3
27
sigemptyset(), sigaddset(), SIGINT,
6
30
#include <stdbool.h> /* bool, false, true */
7
31
#include <fcntl.h> /* open(), O_RDONLY */
8
32
#include <iso646.h> /* and, or, not*/
9
#include <sys/types.h> /* size_t, ssize_t, pid_t, struct dirent,
33
#include <sys/types.h> /* size_t, ssize_t, pid_t, struct
11
35
#include <sys/wait.h> /* waitpid() */
12
36
#include <stddef.h> /* NULL */
13
37
#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()
38
#include <stdio.h> /* asprintf(), perror(), fopen(),
39
fscanf(), vasprintf(), fprintf(),
41
#include <unistd.h> /* close(), readlink(), read(),
42
fork(), setsid(), chdir(), dup2(),
17
43
STDERR_FILENO, execv(), access() */
18
44
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
19
45
EXIT_SUCCESS, malloc(), _exit(),
21
47
#include <dirent.h> /* scandir(), alphasort() */
22
48
#include <inttypes.h> /* intmax_t, strtoumax(), SCNuMAX */
23
49
#include <sys/stat.h> /* struct stat, lstat() */
24
#include <sysexits.h> /* EX_OSERR */
50
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
25
51
#include <error.h> /* error() */
26
52
#include <errno.h> /* TEMP_FAILURE_RETRY */
27
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
29
56
sig_atomic_t interrupted_by_signal = 0;
30
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
58
/* Used by Ubuntu 11.04 (Natty Narwahl) */
59
const char plymouth_old_pid[] = "/dev/.initramfs/plymouth.pid";
60
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
61
const char plymouth_pid[] = "/run/initramfs/plymouth.pid";
31
63
const char plymouth_path[] = "/bin/plymouth";
32
64
const char plymouthd_path[] = "/sbin/plymouthd";
33
const char *plymouthd_default_argv[] = {"/sbin/plymouthd", "--mode=boot",
65
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
34
67
"--attach-to-session",
35
"--pid-file=/dev/.initramfs/plymouth.pid",
38
70
static void termination_handler(__attribute__((unused))int signum){
42
74
interrupted_by_signal = 1;
77
/* Function to use when printing errors */
78
void error_plus(int status, int errnum, const char *formatstring,
84
va_start(ap, formatstring);
85
ret = vasprintf(&text, formatstring, ap);
87
fprintf(stderr, "Mandos plugin %s: ",
88
program_invocation_short_name);
89
vfprintf(stderr, formatstring, ap);
90
fprintf(stderr, ": ");
91
fprintf(stderr, "%s\n", strerror(errnum));
92
error(status, errno, "vasprintf while printing error");
95
fprintf(stderr, "Mandos plugin ");
96
error(status, errnum, "%s", text);
45
100
/* Create prompt string */
46
101
char *makeprompt(void){
49
104
const char *const cryptsource = getenv("cryptsource");
50
105
const char *const crypttarget = getenv("crypttarget");
51
const char prompt_start[] = "Enter passphrase to unlock the disk";
106
const char prompt_start[] = "Unlocking the disk";
107
const char prompt_end[] = "Enter passphrase";
53
109
if(cryptsource == NULL){
54
110
if(crypttarget == NULL){
55
ret = asprintf(&prompt, "%s: ", prompt_start);
111
ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
57
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
113
ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
114
crypttarget, prompt_end);
61
117
if(crypttarget == NULL){
62
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
118
ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
64
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
65
cryptsource, crypttarget);
121
ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
122
cryptsource, crypttarget, prompt_end);
164
221
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
166
if(errno != 0 or *tmp != '\0' or maxvalue != (uintmax_t)((pid_t)maxvalue)){
223
if(errno != 0 or *tmp != '\0'
224
or maxvalue != (uintmax_t)((pid_t)maxvalue)){
170
char exe_target[sizeof(plymouth_path)];
228
char exe_target[sizeof(plymouthd_path)];
172
230
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
174
error(0, errno, "asprintf");
232
error_plus(0, errno, "asprintf");
178
236
struct stat exe_stat;
179
237
ret = lstat(exe_link, &exe_stat);
182
240
if(errno != ENOENT){
183
error(0, errno, "lstat");
241
error_plus(0, errno, "lstat");
217
struct dirent **direntries;
275
/* Try the old pid file location */
277
pidfile = fopen(plymouth_pid, "r");
279
ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
286
/* Look for a plymouth process */
288
struct dirent **direntries = NULL;
218
289
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
219
sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
291
error_plus(0, errno, "scandir");
294
ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
296
error_plus(0, errno, "sscanf");
299
/* scandir might preallocate for this variable (man page unclear).
300
even if ret == 0, therefore we need to free it. */
222
304
pid = (pid_t)maxvalue;
308
390
/* test -x /bin/plymouth */
309
391
ret = access(plymouth_path, X_OK);
393
/* Plymouth is probably not installed. Don't print an error
394
message, just exit. */
395
exit(EX_UNAVAILABLE);
314
398
{ /* Add signal handlers */
315
399
struct sigaction old_action,
316
400
new_action = { .sa_handler = termination_handler,
318
402
sigemptyset(&new_action.sa_mask);
319
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
403
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
320
405
ret = sigaddset(&new_action.sa_mask, *sig);
322
error(0, errno, "sigaddset");
407
error_plus(EX_OSERR, errno, "sigaddset");
325
409
ret = sigaction(*sig, NULL, &old_action);
327
error(0, errno, "sigaction");
411
error_plus(EX_OSERR, errno, "sigaction");
330
413
if(old_action.sa_handler != SIG_IGN){
331
414
ret = sigaction(*sig, &new_action, NULL);
333
error(0, errno, "sigaction");
416
error_plus(EX_OSERR, errno, "sigaction");
340
422
/* plymouth --ping */
341
423
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
342
(const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
425
{ plymouth_path, "--ping", NULL },
345
428
if(interrupted_by_signal){
346
429
kill_and_wait(plymouth_command_pid);
432
/* Plymouth is probably not running. Don't print an error
433
message, just exit. */
434
exit(EX_UNAVAILABLE);
351
437
prompt = makeprompt();
352
438
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
355
error(0, errno, "asprintf");
441
error_plus(EX_OSERR, errno, "asprintf");
359
444
/* 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},
445
bret = exec_and_wait(&plymouth_command_pid,
446
plymouth_path, (const char *[])
447
{ plymouth_path, "ask-for-password",
363
450
free(prompt_arg);
365
if(interrupted_by_signal){
366
kill_and_wait(plymouth_command_pid);
373
452
exit(EXIT_SUCCESS);
454
if(not interrupted_by_signal){
455
/* exec_and_wait failed for some other reason */
458
kill_and_wait(plymouth_command_pid);
376
const char **plymouthd_argv = NULL;
460
const char **plymouthd_argv;
377
461
pid_t pid = get_pid();
379
error(0, 0, "plymouthd pid not found");
463
error_plus(0, 0, "plymouthd pid not found");
464
plymouthd_argv = plymouthd_default_argv;
381
466
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);
469
bret = exec_and_wait(NULL, plymouth_path, (const char *[])
470
{ plymouth_path, "quit", NULL },
475
bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
480
exec_and_wait(NULL, plymouth_path, (const char *[])
481
{ plymouth_path, "show-splash", NULL },
398
483
exit(EXIT_FAILURE);