/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2009-05-23 05:59:52 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090523055952-wvnp7le0io6ttqni
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
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/>.
23
24
 */
24
25
 
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
 
                                   raise() */
 
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,
44
44
#include <errno.h>              /* errno, EINVAL */
45
45
#include <iso646.h>             /* or, not */
46
46
#include <stdbool.h>            /* bool, false, true */
47
 
#include <string.h>             /* strlen, rindex */
 
47
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
48
48
#include <argp.h>               /* struct argp_option, struct
49
49
                                   argp_state, struct argp,
50
50
                                   argp_parse(), error_t,
52
52
                                   ARGP_ERR_UNKNOWN */
53
53
 
54
54
volatile sig_atomic_t quit_now = 0;
55
 
int signal_received;
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>";
59
58
 
60
 
static void termination_handler(int signum){
61
 
  if(quit_now){
62
 
    return;
63
 
  }
 
59
static void termination_handler(__attribute__((unused))int signum){
64
60
  quit_now = 1;
65
 
  signal_received = signum;
66
61
}
67
62
 
68
63
int main(int argc, char **argv){
128
123
  }
129
124
  
130
125
  sigemptyset(&new_action.sa_mask);
131
 
  ret = sigaddset(&new_action.sa_mask, SIGINT);
132
 
  if(ret == -1){
133
 
    perror("sigaddset");
134
 
    return EXIT_FAILURE;
135
 
  }
136
 
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
137
 
  if(ret == -1){
138
 
    perror("sigaddset");
139
 
    return EXIT_FAILURE;
140
 
  }
141
 
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
142
 
  if(ret == -1){
143
 
    perror("sigaddset");
144
 
    return EXIT_FAILURE;
145
 
  }
146
 
  /* Need to check if the handler is SIG_IGN before handling:
147
 
     | [[info:libc:Initial Signal Actions]] |
148
 
     | [[info:libc:Basic Signal Handling]]  |
149
 
  */
 
126
  sigaddset(&new_action.sa_mask, SIGINT);
 
127
  sigaddset(&new_action.sa_mask, SIGHUP);
 
128
  sigaddset(&new_action.sa_mask, SIGTERM);
150
129
  ret = sigaction(SIGINT, NULL, &old_action);
151
130
  if(ret == -1){
152
131
    perror("sigaction");
215
194
      const char *cryptsource = getenv("cryptsource");
216
195
      const char *crypttarget = getenv("crypttarget");
217
196
      const char *const prompt
218
 
        = "Enter passphrase to unlock the disk";
 
197
        = "Enter passphrase to unlock the disk";      
219
198
      if(cryptsource == NULL){
220
199
        if(crypttarget == NULL){
221
200
          fprintf(stderr, "%s: ", prompt);
263
242
    /* if(ret == 0), then the only sensible thing to do is to retry to
264
243
       read from stdin */
265
244
    fputc('\n', stderr);
266
 
    if(debug and not quit_now){
 
245
    if(debug and quit_now == 0){
267
246
      /* If quit_now is nonzero, we were interrupted by a signal, and
268
247
         will print that later, so no need to show this too. */
269
248
      fprintf(stderr, "getline() returned 0, retrying.\n");
279
258
    perror("tcsetattr+echo");
280
259
  }
281
260
  
282
 
  if(quit_now){
283
 
    sigemptyset(&old_action.sa_mask);
284
 
    old_action.sa_handler = SIG_DFL;
285
 
    ret = sigaction(signal_received, &old_action, NULL);
286
 
    if(ret == -1){
287
 
      perror("sigaction");
288
 
    }
289
 
    raise(signal_received);
290
 
  }
291
 
  
292
261
  if(debug){
293
262
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
294
263
            status);