19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
23
* <https://www.fukt.bsnet.se/~teddy/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
26
25
#define _GNU_SOURCE /* getline() */
33
32
#include <signal.h> /* sig_atomic_t, raise(), struct
34
33
sigaction, sigemptyset(),
35
34
sigaction(), sigaddset(), SIGINT,
36
SIGQUIT, SIGHUP, SIGTERM */
35
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 bool quit_now = false;
54
volatile sig_atomic_t quit_now = 0;
55
56
bool debug = false;
56
57
const char *argp_program_version = "password-prompt " VERSION;
57
58
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
59
static void termination_handler(__attribute__((unused))int signum){
60
static void termination_handler(int signum){
65
signal_received = signum;
63
68
int main(int argc, char **argv){
104
109
.doc = "Mandos password-prompt -- Read and"
105
110
" output a password" };
106
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
107
if (ret == ARGP_ERR_UNKNOWN){
111
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
112
if(ret == ARGP_ERR_UNKNOWN){
108
113
fprintf(stderr, "Unknown error while parsing arguments\n");
109
114
return EXIT_FAILURE;
114
119
fprintf(stderr, "Starting %s\n", argv[0]);
117
122
fprintf(stderr, "Storing current terminal attributes\n");
120
if (tcgetattr(STDIN_FILENO, &t_old) != 0){
125
if(tcgetattr(STDIN_FILENO, &t_old) != 0){
121
126
perror("tcgetattr");
122
127
return EXIT_FAILURE;
125
130
sigemptyset(&new_action.sa_mask);
126
sigaddset(&new_action.sa_mask, SIGINT);
127
sigaddset(&new_action.sa_mask, SIGHUP);
128
sigaddset(&new_action.sa_mask, SIGTERM);
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]] |
129
150
ret = sigaction(SIGINT, NULL, &old_action);
131
152
perror("sigaction");
132
153
return EXIT_FAILURE;
134
if (old_action.sa_handler != SIG_IGN){
155
if(old_action.sa_handler != SIG_IGN){
135
156
ret = sigaction(SIGINT, &new_action, NULL);
137
158
perror("sigaction");
168
189
fprintf(stderr, "Removing echo flag from terminal attributes\n");
172
t_new.c_lflag &= ~ECHO;
173
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
193
t_new.c_lflag &= ~(tcflag_t)ECHO;
194
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
174
195
perror("tcsetattr-echo");
175
196
return EXIT_FAILURE;
179
200
fprintf(stderr, "Waiting for input from stdin \n");
184
205
fprintf(stderr, "Interrupted by signal, exiting.\n");
243
264
read from stdin */
244
265
fputc('\n', stderr);
245
266
if(debug and not quit_now){
246
/* If quit_now is true, we were interrupted by a signal, and
267
/* If quit_now is nonzero, we were interrupted by a signal, and
247
268
will print that later, so no need to show this too. */
248
269
fprintf(stderr, "getline() returned 0, retrying.\n");
255
276
fprintf(stderr, "Restoring terminal attributes\n");
257
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
278
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
258
279
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);
262
293
fprintf(stderr, "%s is exiting with status %d\n", argv[0],