25
25
#define _GNU_SOURCE /* getline(), asprintf() */
27
#include <termios.h> /* struct termios, tcsetattr(),
27
#include <termios.h> /* struct termios, tcsetattr(),
28
28
TCSAFLUSH, tcgetattr(), ECHO */
29
29
#include <unistd.h> /* struct termios, tcsetattr(),
30
30
STDIN_FILENO, TCSAFLUSH,
35
35
SIGQUIT, SIGHUP, SIGTERM,
37
37
#include <stddef.h> /* NULL, size_t, ssize_t */
38
#include <sys/types.h> /* ssize_t, struct dirent, pid_t, ssize_t */
38
#include <sys/types.h> /* ssize_t, struct dirent, pid_t,
39
40
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE,
40
41
getenv(), free() */
41
42
#include <dirent.h> /* scandir(), alphasort() */
49
50
#include <iso646.h> /* or, not */
50
51
#include <stdbool.h> /* bool, false, true */
51
52
#include <inttypes.h> /* strtoumax() */
52
#include <sys/stat.h> /* struct stat, lstat() */
53
#include <string.h> /* strlen, rindex, memcmp */
53
#include <sys/stat.h> /* struct stat, lstat(), open() */
54
#include <string.h> /* strlen, rindex, memcmp */
54
55
#include <argp.h> /* struct argp_option, struct
55
56
argp_state, struct argp,
56
57
argp_parse(), error_t,
65
67
const char *argp_program_version = "password-prompt " VERSION;
66
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
68
/* Needed for conflic resolution */
69
const char plymouthd_path[] = "/sbin/plymouth";
70
/* Needed for conflict resolution */
71
const char plymouth_name[] = "plymouthd";
72
73
static void termination_handler(int signum){
80
81
bool conflict_detection(void){
82
/* plymouth conflicts with password-promt since both want to control the
83
associated terminal. Password-prompt exit since plymouth perferms the same
83
/* plymouth conflicts with password-prompt since both want to read
84
from the terminal. Password-prompt will exit if it detects
85
plymouth since plymouth performs the same functionality.
86
87
int is_plymouth(const struct dirent *proc_entry){
89
91
uintmax_t maxvalue;
99
char exe_target[sizeof(plymouthd_path)];
101
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
102
char *cmdline_filename;
103
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
103
106
error(0, errno, "asprintf");
107
struct stat exe_stat;
108
ret = lstat(exe_link, &exe_stat);
112
error(0, errno, "lstat");
117
if(not S_ISLNK(exe_stat.st_mode)
118
or exe_stat.st_uid != 0
119
or exe_stat.st_gid != 0){
124
ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
126
if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
127
(memcmp(plymouthd_path, exe_target,
128
sizeof(plymouthd_path)-1) != 0)){
110
/* Open /proc/<pid>/cmdline */
111
cl_fd = open(cmdline_filename, O_RDONLY);
112
free(cmdline_filename);
114
error(0, errno, "open");
118
char *cmdline = NULL;
120
size_t cmdline_len = 0;
121
size_t cmdline_allocated = 0;
123
const size_t blocksize = 1024;
126
/* Allocate more space? */
127
if(cmdline_len + blocksize + 1 > cmdline_allocated){
128
tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
130
error(0, errno, "realloc");
136
cmdline_allocated += blocksize;
140
sret = read(cl_fd, cmdline + cmdline_len,
141
cmdline_allocated - cmdline_len);
143
error(0, errno, "read");
148
cmdline_len += (size_t)sret;
152
error(0, errno, "close");
156
cmdline[cmdline_len] = '\0'; /* Make sure it is terminated */
158
/* we now have cmdline */
161
char *cmdline_base = strrchr(cmdline, '/');
162
if(cmdline_base != NULL){
163
cmdline_base += 1; /* skip the slash */
165
cmdline_base = cmdline;
168
if(strcmp(cmdline_base, plymouth_name) != 0){
170
fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
177
fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
134
184
struct dirent **direntries;
136
186
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
138
188
error(1, errno, "scandir");