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-2013 Teddy Hogeborn
6
* Copyright © 2008-2013 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
19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
22
* Contact the authors at <mandos@recompile.se>.
25
25
#define _GNU_SOURCE /* getline(), asprintf() */
41
41
getenv(), free() */
42
42
#include <dirent.h> /* scandir(), alphasort() */
43
43
#include <stdio.h> /* fprintf(), stderr, getline(),
44
stdin, feof(), fputc(), vfprintf(), vasprintf()
44
stdin, feof(), fputc(), vfprintf(),
46
46
#include <errno.h> /* errno, EBADF, ENOTTY, EINVAL,
47
47
EFAULT, EFBIG, EIO, ENOSPC, EINTR
51
51
#include <stdbool.h> /* bool, false, true */
52
52
#include <inttypes.h> /* strtoumax() */
53
53
#include <sys/stat.h> /* struct stat, lstat(), open() */
54
#include <string.h> /* strlen, rindex, memcmp, strerror() */
54
#include <string.h> /* strlen, rindex, memcmp, strerror()
55
56
#include <argp.h> /* struct argp_option, struct
56
57
argp_state, struct argp,
57
58
argp_parse(), error_t,
66
67
int signal_received;
67
68
bool debug = false;
68
69
const char *argp_program_version = "password-prompt " VERSION;
69
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
71
72
/* Needed for conflict resolution */
72
73
const char plymouth_name[] = "plymouthd";
75
__attribute__((format (gnu_printf, 2, 3), nonnull(1)))
76
int fprintf_plus(FILE *stream, const char *format, ...){
78
va_start (ap, format);
80
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
81
program_invocation_short_name));
82
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
74
85
/* Function to use when printing errors */
75
void error_plus(int status, int errnum, const char *formatstring, ...){
86
__attribute__((format (gnu_printf, 3, 4)))
87
void error_plus(int status, int errnum, const char *formatstring,
80
93
va_start(ap, formatstring);
81
94
ret = vasprintf(&text, formatstring, ap);
83
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
96
fprintf(stderr, "Mandos plugin %s: ",
97
program_invocation_short_name);
84
98
vfprintf(stderr, formatstring, ap);
85
fprintf(stderr, ": ");
86
fprintf(stderr, "%s\n", strerror(errnum));
99
fprintf(stderr, ": %s\n", strerror(errnum));
87
100
error(status, errno, "vasprintf while printing error");
106
119
from the terminal. Password-prompt will exit if it detects
107
120
plymouth since plymouth performs the same functionality.
122
__attribute__((nonnull))
109
123
int is_plymouth(const struct dirent *proc_entry){
116
maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
130
proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
118
132
if(errno != 0 or *tmp != '\0'
119
or maxvalue != (uintmax_t)((pid_t)maxvalue)){
133
or proc_id != (uintmax_t)((pid_t)proc_id)){
125
139
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
126
140
proc_entry->d_name);
128
error(0, errno, "asprintf");
142
error_plus(0, errno, "asprintf");
151
165
if(cmdline_len + blocksize + 1 > cmdline_allocated){
152
166
tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
154
error(0, errno, "realloc");
168
error_plus(0, errno, "realloc");
164
178
sret = read(cl_fd, cmdline + cmdline_len,
165
179
cmdline_allocated - cmdline_len);
167
error(0, errno, "read");
181
error_plus(0, errno, "read");
208
struct dirent **direntries;
222
struct dirent **direntries = NULL;
210
224
ret = scandir("/proc", &direntries, is_plymouth, alphasort);
212
error(1, errno, "scandir");
226
error_plus(1, errno, "scandir");
311
327
if(tcgetattr(STDIN_FILENO, &t_old) != 0){
313
error(0, errno, "tcgetattr");
329
error_plus(0, errno, "tcgetattr");
323
339
sigemptyset(&new_action.sa_mask);
324
340
ret = sigaddset(&new_action.sa_mask, SIGINT);
326
error(0, errno, "sigaddset");
342
error_plus(0, errno, "sigaddset");
329
345
ret = sigaddset(&new_action.sa_mask, SIGHUP);
331
error(0, errno, "sigaddset");
347
error_plus(0, errno, "sigaddset");
334
350
ret = sigaddset(&new_action.sa_mask, SIGTERM);
336
error(0, errno, "sigaddset");
352
error_plus(0, errno, "sigaddset");
339
355
/* Need to check if the handler is SIG_IGN before handling:
343
359
ret = sigaction(SIGINT, NULL, &old_action);
345
error(0, errno, "sigaction");
361
error_plus(0, errno, "sigaction");
348
364
if(old_action.sa_handler != SIG_IGN){
349
365
ret = sigaction(SIGINT, &new_action, NULL);
351
error(0, errno, "sigaction");
367
error_plus(0, errno, "sigaction");
355
371
ret = sigaction(SIGHUP, NULL, &old_action);
357
error(0, errno, "sigaction");
373
error_plus(0, errno, "sigaction");
360
376
if(old_action.sa_handler != SIG_IGN){
361
377
ret = sigaction(SIGHUP, &new_action, NULL);
363
error(0, errno, "sigaction");
379
error_plus(0, errno, "sigaction");
367
383
ret = sigaction(SIGTERM, NULL, &old_action);
369
error(0, errno, "sigaction");
385
error_plus(0, errno, "sigaction");
372
388
if(old_action.sa_handler != SIG_IGN){
373
389
ret = sigaction(SIGTERM, &new_action, NULL);
375
error(0, errno, "sigaction");
391
error_plus(0, errno, "sigaction");
386
402
t_new.c_lflag &= ~(tcflag_t)ECHO;
387
403
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
389
error(0, errno, "tcsetattr-echo");
405
error_plus(0, errno, "tcsetattr-echo");
456
472
sret = write(STDOUT_FILENO, buffer + written, n - written);
459
error(0, errno, "write");
475
error_plus(0, errno, "write");
478
494
sret = close(STDOUT_FILENO);
481
error(0, errno, "close");
497
error_plus(0, errno, "close");
484
500
status = EX_OSFILE;
496
512
if(errno != EINTR and not feof(stdin)){
497
error(0, errno, "getline");
513
error_plus(0, errno, "getline");
500
516
status = EX_UNAVAILABLE;
523
540
fprintf(stderr, "Restoring terminal attributes\n");
525
542
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
526
error(0, errno, "tcsetattr+echo");
543
error_plus(0, errno, "tcsetattr+echo");
531
548
old_action.sa_handler = SIG_DFL;
532
549
ret = sigaction(signal_received, &old_action, NULL);
534
error(0, errno, "sigaction");
551
error_plus(0, errno, "sigaction");
536
553
raise(signal_received);