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(plymouthd_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(plymouthd_path)-1) or
227
(memcmp(plymouthd_path, exe_target,
228
sizeof(plymouthd_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);
249
error(0, errno, "scandir");
252
ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
254
error(0, errno, "sscanf");
259
pid = (pid_t)maxvalue;
260
if((uintmax_t)pid == maxvalue){
267
const char **getargv(pid_t pid){
269
char *cmdline_filename;
273
ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
276
error(0, errno, "asprintf");
280
/* Open /proc/<pid>/cmdline */
281
cl_fd = open(cmdline_filename, O_RDONLY);
282
free(cmdline_filename);
284
error(0, errno, "open");
288
size_t cmdline_allocated = 0;
289
size_t cmdline_len = 0;
290
char *cmdline = NULL;
292
const size_t blocksize = 1024;
294
/* Allocate more space? */
295
if(cmdline_len + blocksize > cmdline_allocated){
296
tmp = realloc(cmdline, cmdline_allocated + blocksize);
298
error(0, errno, "realloc");
304
cmdline_allocated += blocksize;
308
sret = read(cl_fd, cmdline + cmdline_len,
309
cmdline_allocated - cmdline_len);
311
error(0, errno, "read");
316
cmdline_len += (size_t)sret;
320
error(0, errno, "close");
325
/* we got cmdline and cmdline_len, ignore rest... */
326
char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
327
* sizeof(char *)); /* Get number of args */
329
error(0, errno, "argv = malloc()");
333
argz_extract(cmdline, cmdline_len, argv); /* Create argv */
334
return (const char **)argv;
337
int main(__attribute__((unused))int argc,
338
__attribute__((unused))char **argv){
341
pid_t plymouth_command_pid;
345
/* test -x /bin/plymouth */
346
ret = access(plymouth_path, X_OK);
348
/* Plymouth is probably not installed. Don't print an error
349
message, just exit. */
350
exit(EX_UNAVAILABLE);
353
{ /* Add signal handlers */
354
struct sigaction old_action,
355
new_action = { .sa_handler = termination_handler,
357
sigemptyset(&new_action.sa_mask);
358
for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
360
ret = sigaddset(&new_action.sa_mask, *sig);
362
error(EX_OSERR, errno, "sigaddset");
364
ret = sigaction(*sig, NULL, &old_action);
366
error(EX_OSERR, errno, "sigaction");
368
if(old_action.sa_handler != SIG_IGN){
369
ret = sigaction(*sig, &new_action, NULL);
371
error(EX_OSERR, errno, "sigaction");
377
/* plymouth --ping */
378
bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
380
{ plymouth_path, "--ping", NULL },
383
if(interrupted_by_signal){
384
kill_and_wait(plymouth_command_pid);
387
/* Plymouth is probably not running. Don't print an error
388
message, just exit. */
389
exit(EX_UNAVAILABLE);
392
prompt = makeprompt();
393
ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
396
error(EX_OSERR, errno, "asprintf");
399
/* plymouth ask-for-password --prompt="$prompt" */
400
bret = exec_and_wait(&plymouth_command_pid,
401
plymouth_path, (const char *[])
402
{ plymouth_path, "ask-for-password",
409
if(not interrupted_by_signal){
410
/* exec_and_wait failed for some other reason */
413
kill_and_wait(plymouth_command_pid);
415
const char **plymouthd_argv;
416
pid_t pid = get_pid();
418
error(0, 0, "plymouthd pid not found");
419
plymouthd_argv = plymouthd_default_argv;
421
plymouthd_argv = getargv(pid);
424
bret = exec_and_wait(NULL, plymouth_path, (const char *[])
425
{ plymouth_path, "quit", NULL },
430
bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
435
exec_and_wait(NULL, plymouth_path, (const char *[])
436
{ plymouth_path, "show-splash", NULL },