1
/* -*- coding: utf-8; mode: c; mode: orgtbl -*- */
1
/* -*- coding: utf-8 -*- */
3
* Password-prompt - Read a password from the terminal and print it
3
* Passprompt - Read a password from the terminal and print it
5
* Copyright © 2008,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 Björn Påhlsson
5
* Copyright © 2008 Teddy Hogeborn
6
* Copyright © 2008 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 <https://www.fukt.bsnet.se/~belorn/> and
23
* <https://www.fukt.bsnet.se/~teddy/>.
25
26
#define _GNU_SOURCE /* getline() */
32
33
#include <signal.h> /* sig_atomic_t, raise(), struct
33
34
sigaction, sigemptyset(),
34
35
sigaction(), sigaddset(), SIGINT,
35
SIGQUIT, SIGHUP, SIGTERM,
36
SIGQUIT, SIGHUP, SIGTERM */
37
37
#include <stddef.h> /* NULL, size_t, ssize_t */
38
38
#include <sys/types.h> /* ssize_t */
39
39
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE,
51
51
ARGP_KEY_ARG, ARGP_KEY_END,
52
52
ARGP_ERR_UNKNOWN */
54
volatile sig_atomic_t quit_now = 0;
54
volatile bool quit_now = false;
56
55
bool debug = false;
57
56
const char *argp_program_version = "password-prompt " VERSION;
58
57
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
60
static void termination_handler(int signum){
65
signal_received = signum;
59
static void termination_handler(__attribute__((unused))int signum){
68
63
int main(int argc, char **argv){
84
79
.doc = "Debug mode", .group = 3 },
88
error_t parse_opt (int key, char *arg, struct argp_state *state){
83
error_t parse_opt (int key, char *arg, struct argp_state *state) {
84
/* Get the INPUT argument from `argp_parse', which we know is a
85
pointer to our plugin list pointer. */
107
104
struct argp argp = { .options = options, .parser = parse_opt,
109
106
.doc = "Mandos password-prompt -- Read and"
110
107
" output a password" };
111
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
112
if(ret == ARGP_ERR_UNKNOWN){
108
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
109
if (ret == ARGP_ERR_UNKNOWN){
113
110
fprintf(stderr, "Unknown error while parsing arguments\n");
114
111
return EXIT_FAILURE;
119
116
fprintf(stderr, "Starting %s\n", argv[0]);
122
119
fprintf(stderr, "Storing current terminal attributes\n");
125
if(tcgetattr(STDIN_FILENO, &t_old) != 0){
122
if (tcgetattr(STDIN_FILENO, &t_old) != 0){
126
123
perror("tcgetattr");
127
124
return EXIT_FAILURE;
130
127
sigemptyset(&new_action.sa_mask);
131
ret = sigaddset(&new_action.sa_mask, SIGINT);
136
ret = sigaddset(&new_action.sa_mask, SIGHUP);
141
ret = sigaddset(&new_action.sa_mask, SIGTERM);
146
/* Need to check if the handler is SIG_IGN before handling:
147
| [[info:libc:Initial Signal Actions]] |
148
| [[info:libc:Basic Signal Handling]] |
128
sigaddset(&new_action.sa_mask, SIGINT);
129
sigaddset(&new_action.sa_mask, SIGHUP);
130
sigaddset(&new_action.sa_mask, SIGTERM);
150
131
ret = sigaction(SIGINT, NULL, &old_action);
152
133
perror("sigaction");
153
134
return EXIT_FAILURE;
155
if(old_action.sa_handler != SIG_IGN){
136
if (old_action.sa_handler != SIG_IGN){
156
137
ret = sigaction(SIGINT, &new_action, NULL);
158
139
perror("sigaction");
189
170
fprintf(stderr, "Removing echo flag from terminal attributes\n");
193
174
t_new.c_lflag &= ~ECHO;
194
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
175
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
195
176
perror("tcsetattr-echo");
196
177
return EXIT_FAILURE;
200
181
fprintf(stderr, "Waiting for input from stdin \n");
205
186
fprintf(stderr, "Interrupted by signal, exiting.\n");
264
245
read from stdin */
265
246
fputc('\n', stderr);
266
247
if(debug and not quit_now){
267
/* If quit_now is nonzero, we were interrupted by a signal, and
248
/* If quit_now is true, we were interrupted by a signal, and
268
249
will print that later, so no need to show this too. */
269
250
fprintf(stderr, "getline() returned 0, retrying.\n");
276
257
fprintf(stderr, "Restoring terminal attributes\n");
278
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
259
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
279
260
perror("tcsetattr+echo");
283
sigemptyset(&old_action.sa_mask);
284
old_action.sa_handler = SIG_DFL;
285
ret = sigaction(signal_received, &old_action, NULL);
289
raise(signal_received);
293
264
fprintf(stderr, "%s is exiting with status %d\n", argv[0],