1
/* -*- coding: utf-8 -*- */
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2010 Teddy Hogeborn
6
* Copyright © 2010 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@fukt.bsnet.se>.
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(),
30
#include <stdbool.h> /* bool, false, true */
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
35
#include <sys/wait.h> /* waitpid() */
36
#include <stddef.h> /* NULL */
37
#include <string.h> /* strchr(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror(), fopen(),
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(),
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 */
50
#include <error.h> /* error() */
51
#include <errno.h> /* TEMP_FAILURE_RETRY */
52
#include <argz.h> /* argz_count(), argz_extract() */
54
sig_atomic_t interrupted_by_signal = 0;
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
56
const char plymouth_path[] = "/bin/plymouth";
57
const char plymouthd_path[] = "/sbin/plymouthd";
58
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
60
"--attach-to-session",
66
static void termination_handler(__attribute__((unused))int signum){
67
if(interrupted_by_signal){
70
interrupted_by_signal = 1;
73
/* Create prompt string */
74
char *makeprompt(void){
77
const char *const cryptsource = getenv("cryptsource");
78
const char *const crypttarget = getenv("crypttarget");
79
const char prompt_start[] = "Enter passphrase to unlock the disk";
81
if(cryptsource == NULL){
82
if(crypttarget == NULL){
83
ret = asprintf(&prompt, "%s: ", prompt_start);
85
ret = asprintf(&prompt, "%s (%s): ", prompt_start,
89
if(crypttarget == NULL){
90
ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
92
ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
93
cryptsource, crypttarget);
102
void kill_and_wait(pid_t pid){
103
TEMP_FAILURE_RETRY(kill(pid, SIGTERM));
104
TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
107
bool become_a_daemon(void){
108
int ret = setuid(geteuid());
110
error(0, errno, "setuid");
116
error(0, errno, "chdir");
119
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
121
error(0, errno, "dup2");
127
bool exec_and_wait(pid_t *pid_return, const char *path,
128
const char **argv, bool interruptable,
135
error(0, errno, "fork");
141
if(not become_a_daemon()){
146
char **new_argv = NULL;
149
for (; argv[i]!=(char *)NULL; i++){
150
tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
152
error(0, errno, "realloc");
156
new_argv = (char **)tmp;
157
new_argv[i] = strdup(argv[i]);
159
new_argv[i] = (char *) NULL;
161
execv(path, (char *const *)new_argv);
162
error(0, errno, "execv");
165
if(pid_return != NULL){
169
ret = waitpid(pid, &status, 0);
170
} while(ret == -1 and errno == EINTR
171
and ((not interrupted_by_signal)
172
or (not interruptable)));
173
if(interrupted_by_signal and interruptable){
177
error(0, errno, "waitpid");
180
if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
186
int is_plymouth(const struct dirent *proc_entry){
192
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
194
if(errno != 0 or *tmp != '\0'
195
or maxvalue != (uintmax_t)((pid_t)maxvalue)){
199
char exe_target[sizeof(plymouth_path)];
201
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
203
error(0, errno, "asprintf");
207
struct stat exe_stat;
208
ret = lstat(exe_link, &exe_stat);
212
error(0, errno, "lstat");
217
if(not S_ISLNK(exe_stat.st_mode)
218
or exe_stat.st_uid != 0
219
or exe_stat.st_gid != 0){
224
ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
226
if((sret != (ssize_t)sizeof(plymouth_path)-1) or
227
(memcmp(plymouth_path, exe_target,
228
sizeof(plymouth_path)-1) != 0)){
236
FILE *pidfile = fopen(plymouth_pid, "r");
237
uintmax_t maxvalue = 0;
239
ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
246
struct dirent **direntries;
247
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
248
sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
251
pid = (pid_t)maxvalue;
252
if((uintmax_t)pid == maxvalue){
259
const char **getargv(pid_t pid){
261
char *cmdline_filename;
265
ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
268
error(0, errno, "asprintf");
272
/* Open /proc/<pid>/cmdline */
273
cl_fd = open(cmdline_filename, O_RDONLY);
274
free(cmdline_filename);
276
error(0, errno, "open");
280
size_t cmdline_allocated = 0;
281
size_t cmdline_len = 0;
282
char *cmdline = NULL;
284
const size_t blocksize = 1024;
286
/* Allocate more space? */
287
if(cmdline_len + blocksize > cmdline_allocated){
288
tmp = realloc(cmdline, cmdline_allocated + blocksize);
290
error(0, errno, "realloc");
296
cmdline_allocated += blocksize;
300
sret = read(cl_fd, cmdline + cmdline_len,
301
cmdline_allocated - cmdline_len);
303
error(0, errno, "read");
308
cmdline_len += (size_t)sret;
312
error(0, errno, "close");
317
/* we got cmdline and cmdline_len, ignore rest... */
318
char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
319
* sizeof(char *)); /* Get number of args */
321
error(0, errno, "argv = malloc()");
325
argz_extract(cmdline, cmdline_len, argv); /* Create argv */
326
return (const char **)argv;
329
int main(__attribute__((unused))int argc,
330
__attribute__((unused))char **argv){
333
pid_t plymouth_command_pid;
337
/* test -x /bin/plymouth */
338
ret = access(plymouth_path, X_OK);
340
/* Plymouth is probably not installed. Don't print an error
341
message, just exit. */
342
exit(EX_UNAVAILABLE);
345
{ /* Add signal handlers */
346
struct sigaction old_action,
347
new_action = { .sa_handler = termination_handler,
349
sigemptyset(&new_action.sa_mask);
350
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
352
ret = sigaddset(&new_action.sa_mask, *sig);
354
error(EX_OSERR, errno, "sigaddset");
356
ret = sigaction(*sig, NULL, &old_action);
358
error(EX_OSERR, errno, "sigaction");
360
if(old_action.sa_handler != SIG_IGN){
361
ret = sigaction(*sig, &new_action, NULL);
363
error(EX_OSERR, errno, "sigaction");
369
/* plymouth --ping */
370
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
372
{ plymouth_path, "--ping", NULL },
375
if(interrupted_by_signal){
376
kill_and_wait(plymouth_command_pid);
379
/* Plymouth is probably not running. Don't print an error
380
message, just exit. */
381
exit(EX_UNAVAILABLE);
384
prompt = makeprompt();
385
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
388
error(EX_OSERR, errno, "asprintf");
391
/* plymouth ask-for-password --prompt="$prompt" */
392
bret = exec_and_wait(&plymouth_command_pid,
393
plymouth_path, (const char *[])
394
{ plymouth_path, "ask-for-password",
401
if(not interrupted_by_signal){
402
/* exec_and_wait failed for some other reason */
405
kill_and_wait(plymouth_command_pid);
407
const char **plymouthd_argv;
408
pid_t pid = get_pid();
410
error(0, 0, "plymouthd pid not found");
411
plymouthd_argv = plymouthd_default_argv;
413
plymouthd_argv = getargv(pid);
416
bret = exec_and_wait(NULL, plymouth_path, (const char *[])
417
{ plymouth_path, "quit", NULL },
422
bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
427
exec_and_wait(NULL, plymouth_path, (const char *[])
428
{ plymouth_path, "show-splash", NULL },