/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/password-prompt.c

* plugin-runner.c (main): When a plugin is killed by a signal, show
                          the signal name, not just the number, in the
                          debug log message.

* plugins.d/password-prompt.c (termination_handler): Store received
                                                     signal in
                                                     "signal_received".
  (main): If exiting due to signal, re-raise signal received instead
          of returning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
2
2
/*
3
 
 * Passprompt - Read a password from the terminal and print it
 
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
5
 * Copyright © 2008,2009 Teddy Hogeborn
6
6
 * Copyright © 2008,2009 Björn Påhlsson
33
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
34
34
                                   sigaction, sigemptyset(),
35
35
                                   sigaction(), sigaddset(), SIGINT,
36
 
                                   SIGQUIT, SIGHUP, SIGTERM */
 
36
                                   SIGQUIT, SIGHUP, SIGTERM,
 
37
                                   raise() */
37
38
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
39
#include <sys/types.h>          /* ssize_t */
39
40
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
51
52
                                   ARGP_KEY_ARG, ARGP_KEY_END,
52
53
                                   ARGP_ERR_UNKNOWN */
53
54
 
54
 
volatile bool quit_now = false;
 
55
volatile sig_atomic_t quit_now = 0;
 
56
int signal_received;
55
57
bool debug = false;
56
58
const char *argp_program_version = "password-prompt " VERSION;
57
59
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
58
60
 
59
 
static void termination_handler(__attribute__((unused))int signum){
60
 
  quit_now = true;
 
61
static void termination_handler(int signum){
 
62
  if(quit_now){
 
63
    return;
 
64
  }
 
65
  quit_now = 1;
 
66
  signal_received = signum;
61
67
}
62
68
 
63
69
int main(int argc, char **argv){
80
86
      { .name = NULL }
81
87
    };
82
88
    
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. */
86
 
      switch (key) {
 
89
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
90
      switch (key){
87
91
      case 'p':
88
92
        prefix = arg;
89
93
        break;
91
95
        debug = true;
92
96
        break;
93
97
      case ARGP_KEY_ARG:
94
 
        argp_usage (state);
 
98
        argp_usage(state);
95
99
        break;
96
100
      case ARGP_KEY_END:
97
101
        break;
105
109
                         .args_doc = "",
106
110
                         .doc = "Mandos password-prompt -- Read and"
107
111
                         " output a password" };
108
 
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
109
 
    if (ret == ARGP_ERR_UNKNOWN){
 
112
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
113
    if(ret == ARGP_ERR_UNKNOWN){
110
114
      fprintf(stderr, "Unknown error while parsing arguments\n");
111
115
      return EXIT_FAILURE;
112
116
    }
113
117
  }
114
118
  
115
 
  if (debug){
 
119
  if(debug){
116
120
    fprintf(stderr, "Starting %s\n", argv[0]);
117
121
  }
118
 
  if (debug){
 
122
  if(debug){
119
123
    fprintf(stderr, "Storing current terminal attributes\n");
120
124
  }
121
125
  
122
 
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
 
126
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
123
127
    perror("tcgetattr");
124
128
    return EXIT_FAILURE;
125
129
  }
133
137
    perror("sigaction");
134
138
    return EXIT_FAILURE;
135
139
  }
136
 
  if (old_action.sa_handler != SIG_IGN){
 
140
  /* Need to check if the handler is SIG_IGN before handling:
 
141
     | [[info:libc:Initial Signal Actions]] |
 
142
     | [[info:libc:Basic Signal Handling]]  |
 
143
  */
 
144
  if(old_action.sa_handler != SIG_IGN){
137
145
    ret = sigaction(SIGINT, &new_action, NULL);
138
146
    if(ret == -1){
139
147
      perror("sigaction");
145
153
    perror("sigaction");
146
154
    return EXIT_FAILURE;
147
155
  }
148
 
  if (old_action.sa_handler != SIG_IGN){
 
156
  if(old_action.sa_handler != SIG_IGN){
149
157
    ret = sigaction(SIGHUP, &new_action, NULL);
150
158
    if(ret == -1){
151
159
      perror("sigaction");
157
165
    perror("sigaction");
158
166
    return EXIT_FAILURE;
159
167
  }
160
 
  if (old_action.sa_handler != SIG_IGN){
 
168
  if(old_action.sa_handler != SIG_IGN){
161
169
    ret = sigaction(SIGTERM, &new_action, NULL);
162
170
    if(ret == -1){
163
171
      perror("sigaction");
166
174
  }
167
175
  
168
176
  
169
 
  if (debug){
 
177
  if(debug){
170
178
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
171
179
  }
172
180
  
173
181
  t_new = t_old;
174
182
  t_new.c_lflag &= ~ECHO;
175
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
183
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
176
184
    perror("tcsetattr-echo");
177
185
    return EXIT_FAILURE;
178
186
  }
179
187
 
180
 
  if (debug){
 
188
  if(debug){
181
189
    fprintf(stderr, "Waiting for input from stdin \n");
182
190
  }
183
191
  while(true){
184
 
    if (quit_now){
 
192
    if(quit_now){
185
193
      if(debug){
186
194
        fprintf(stderr, "Interrupted by signal, exiting.\n");
187
195
      }
213
221
      }
214
222
    }
215
223
    ret = getline(&buffer, &n, stdin);
216
 
    if (ret > 0){
 
224
    if(ret > 0){
217
225
      status = EXIT_SUCCESS;
218
226
      /* Make n = data size instead of allocated buffer size */
219
227
      n = (size_t)ret;
234
242
      }
235
243
      break;
236
244
    }
237
 
    if (ret < 0){
238
 
      if (errno != EINTR and not feof(stdin)){
 
245
    if(ret < 0){
 
246
      if(errno != EINTR and not feof(stdin)){
239
247
        perror("getline");
240
248
        status = EXIT_FAILURE;
241
249
        break;
245
253
       read from stdin */
246
254
    fputc('\n', stderr);
247
255
    if(debug and not quit_now){
248
 
      /* If quit_now is true, we were interrupted by a signal, and
 
256
      /* If quit_now is nonzero, we were interrupted by a signal, and
249
257
         will print that later, so no need to show this too. */
250
258
      fprintf(stderr, "getline() returned 0, retrying.\n");
251
259
    }
253
261
  
254
262
  free(buffer);
255
263
  
256
 
  if (debug){
 
264
  if(debug){
257
265
    fprintf(stderr, "Restoring terminal attributes\n");
258
266
  }
259
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
267
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
260
268
    perror("tcsetattr+echo");
261
269
  }
262
270
  
263
 
  if (debug){
 
271
  if(quit_now){
 
272
    sigemptyset(&old_action.sa_mask);
 
273
    old_action.sa_handler = SIG_DFL;
 
274
    ret = sigaction(signal_received, &old_action, NULL);
 
275
    if(ret == -1){
 
276
      perror("sigaction");
 
277
    }
 
278
    raise(signal_received);
 
279
  }
 
280
  
 
281
  if(debug){
264
282
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
265
283
            status);
266
284
  }