3
3
* Password-prompt - Read a password from the terminal and print it
5
* Copyright © 2008-2010 Teddy Hogeborn
6
* Copyright © 2008-2010 Björn Påhlsson
5
* Copyright © 2008,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 Björn Påhlsson
8
8
* This program is free software: you can redistribute it and/or
9
9
* modify it under the terms of the GNU General Public License as
22
22
* Contact the authors at <mandos@fukt.bsnet.se>.
25
#define _GNU_SOURCE /* getline(), asprintf() */
25
#define _GNU_SOURCE /* getline() */
27
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,
31
tcgetattr(), ECHO, readlink() */
32
32
#include <signal.h> /* sig_atomic_t, raise(), struct
33
33
sigaction, sigemptyset(),
34
34
sigaction(), sigaddset(), SIGINT,
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 */
39
39
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE,
41
#include <dirent.h> /* scandir(), alphasort() */
42
41
#include <stdio.h> /* fprintf(), stderr, getline(),
43
stdin, feof(), fputc()
42
stdin, feof(), perror(), fputc()
45
44
#include <errno.h> /* errno, EBADF, ENOTTY, EINVAL,
46
45
EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
#include <error.h> /* error() */
49
47
#include <iso646.h> /* or, not */
50
48
#include <stdbool.h> /* bool, false, true */
51
#include <inttypes.h> /* strtoumax() */
52
#include <sys/stat.h> /* struct stat, lstat() */
53
#include <string.h> /* strlen, rindex, memcmp */
49
#include <string.h> /* strlen, rindex, strncmp, strcmp */
54
50
#include <argp.h> /* struct argp_option, struct
55
51
argp_state, struct argp,
56
52
argp_parse(), error_t,
77
69
signal_received = signum;
80
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
86
int is_plymouth(const struct dirent *proc_entry){
92
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
94
if(errno != 0 or *tmp != '\0'
95
or maxvalue != (uintmax_t)((pid_t)maxvalue)){
99
char exe_target[sizeof(plymouthd_path)];
101
ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
103
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)){
134
struct dirent **direntries;
136
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
138
error(1, errno, "scandir");
148
72
int main(int argc, char **argv){
152
75
struct termios t_new, t_old;
153
76
char *buffer = NULL;
227
150
fprintf(stderr, "Starting %s\n", argv[0]);
230
if (conflict_detection()){
232
fprintf(stderr, "Stopping %s because of conflict", argv[0]);
238
153
fprintf(stderr, "Storing current terminal attributes\n");
241
156
if(tcgetattr(STDIN_FILENO, &t_old) != 0){
243
error(0, errno, "tcgetattr");
253
168
sigemptyset(&new_action.sa_mask);
254
169
ret = sigaddset(&new_action.sa_mask, SIGINT);
256
error(0, errno, "sigaddset");
259
174
ret = sigaddset(&new_action.sa_mask, SIGHUP);
261
error(0, errno, "sigaddset");
264
179
ret = sigaddset(&new_action.sa_mask, SIGTERM);
266
error(0, errno, "sigaddset");
269
184
/* Need to check if the handler is SIG_IGN before handling:
273
188
ret = sigaction(SIGINT, NULL, &old_action);
275
error(0, errno, "sigaction");
278
193
if(old_action.sa_handler != SIG_IGN){
279
194
ret = sigaction(SIGINT, &new_action, NULL);
281
error(0, errno, "sigaction");
285
200
ret = sigaction(SIGHUP, NULL, &old_action);
287
error(0, errno, "sigaction");
290
205
if(old_action.sa_handler != SIG_IGN){
291
206
ret = sigaction(SIGHUP, &new_action, NULL);
293
error(0, errno, "sigaction");
297
212
ret = sigaction(SIGTERM, NULL, &old_action);
299
error(0, errno, "sigaction");
302
217
if(old_action.sa_handler != SIG_IGN){
303
218
ret = sigaction(SIGTERM, &new_action, NULL);
305
error(0, errno, "sigaction");
374
sret = getline(&buffer, &n, stdin);
289
ret = getline(&buffer, &n, stdin);
376
291
status = EXIT_SUCCESS;
377
292
/* Make n = data size instead of allocated buffer size */
379
294
/* Strip final newline */
380
295
if(n > 0 and buffer[n-1] == '\n'){
381
296
buffer[n-1] = '\0'; /* not strictly necessary */