/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-10-22 00:05:01 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091022000501-g792e99q5g1wkyet
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
2
/*
 
3
 * Password-prompt - Read a password from the terminal and print it
 
4
 * 
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 * 
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
 
21
 * 
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
23
 */
 
24
 
 
25
#define _GNU_SOURCE             /* getline() */
 
26
 
 
27
#include <termios.h>            /* struct termios, tcsetattr(),
 
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
 
29
#include <unistd.h>             /* struct termios, tcsetattr(),
 
30
                                   STDIN_FILENO, TCSAFLUSH,
 
31
                                   tcgetattr(), ECHO */
 
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
 
33
                                   sigaction, sigemptyset(),
 
34
                                   sigaction(), sigaddset(), SIGINT,
 
35
                                   SIGQUIT, SIGHUP, SIGTERM,
 
36
                                   raise() */
 
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
38
#include <sys/types.h>          /* ssize_t */
 
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
 
40
                                   getopt_long, getenv() */
 
41
#include <stdio.h>              /* fprintf(), stderr, getline(),
 
42
                                   stdin, feof(), perror(), fputc(),
 
43
                                   getopt_long */
 
44
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
 
45
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
 
46
                                */
 
47
#include <iso646.h>             /* or, not */
 
48
#include <stdbool.h>            /* bool, false, true */
 
49
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
 
50
#include <argp.h>               /* struct argp_option, struct
 
51
                                   argp_state, struct argp,
 
52
                                   argp_parse(), error_t,
 
53
                                   ARGP_KEY_ARG, ARGP_KEY_END,
 
54
                                   ARGP_ERR_UNKNOWN */
 
55
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
 
56
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
 
57
 
 
58
volatile sig_atomic_t quit_now = 0;
 
59
int signal_received;
 
60
bool debug = false;
 
61
const char *argp_program_version = "password-prompt " VERSION;
 
62
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
63
 
 
64
static void termination_handler(int signum){
 
65
  if(quit_now){
 
66
    return;
 
67
  }
 
68
  quit_now = 1;
 
69
  signal_received = signum;
 
70
}
 
71
 
 
72
int main(int argc, char **argv){
 
73
  ssize_t ret;
 
74
  size_t n;
 
75
  struct termios t_new, t_old;
 
76
  char *buffer = NULL;
 
77
  char *prefix = NULL;
 
78
  int status = EXIT_SUCCESS;
 
79
  struct sigaction old_action,
 
80
    new_action = { .sa_handler = termination_handler,
 
81
                   .sa_flags = 0 };
 
82
  {
 
83
    struct argp_option options[] = {
 
84
      { .name = "prefix", .key = 'p',
 
85
        .arg = "PREFIX", .flags = 0,
 
86
        .doc = "Prefix shown before the prompt", .group = 2 },
 
87
      { .name = "debug", .key = 128,
 
88
        .doc = "Debug mode", .group = 3 },
 
89
      { .name = NULL }
 
90
    };
 
91
    
 
92
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
93
      switch (key){
 
94
      case 'p':
 
95
        prefix = arg;
 
96
        break;
 
97
      case 128:
 
98
        debug = true;
 
99
        break;
 
100
      case ARGP_KEY_ARG:
 
101
        argp_usage(state);
 
102
        break;
 
103
      case ARGP_KEY_END:
 
104
        break;
 
105
      default:
 
106
        return ARGP_ERR_UNKNOWN;
 
107
      }
 
108
      return 0;
 
109
    }
 
110
    
 
111
    struct argp argp = { .options = options, .parser = parse_opt,
 
112
                         .args_doc = "",
 
113
                         .doc = "Mandos password-prompt -- Read and"
 
114
                         " output a password" };
 
115
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
116
    if(ret == ARGP_ERR_UNKNOWN){
 
117
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
118
      return EX_SOFTWARE;
 
119
    }
 
120
  }
 
121
  
 
122
  if(debug){
 
123
    fprintf(stderr, "Starting %s\n", argv[0]);
 
124
  }
 
125
  if(debug){
 
126
    fprintf(stderr, "Storing current terminal attributes\n");
 
127
  }
 
128
  
 
129
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
 
130
    int e = errno;
 
131
    perror("tcgetattr");
 
132
    switch(e){
 
133
    case EBADF:
 
134
    case ENOTTY:
 
135
      return EX_UNAVAILABLE;
 
136
    default:
 
137
      return EX_OSERR;
 
138
    }
 
139
  }
 
140
  
 
141
  sigemptyset(&new_action.sa_mask);
 
142
  ret = sigaddset(&new_action.sa_mask, SIGINT);
 
143
  if(ret == -1){
 
144
    perror("sigaddset");
 
145
    return EX_OSERR;
 
146
  }
 
147
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
148
  if(ret == -1){
 
149
    perror("sigaddset");
 
150
    return EX_OSERR;
 
151
  }
 
152
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
153
  if(ret == -1){
 
154
    perror("sigaddset");
 
155
    return EX_OSERR;
 
156
  }
 
157
  /* Need to check if the handler is SIG_IGN before handling:
 
158
     | [[info:libc:Initial Signal Actions]] |
 
159
     | [[info:libc:Basic Signal Handling]]  |
 
160
  */
 
161
  ret = sigaction(SIGINT, NULL, &old_action);
 
162
  if(ret == -1){
 
163
    perror("sigaction");
 
164
    return EX_OSERR;
 
165
  }
 
166
  if(old_action.sa_handler != SIG_IGN){
 
167
    ret = sigaction(SIGINT, &new_action, NULL);
 
168
    if(ret == -1){
 
169
      perror("sigaction");
 
170
      return EX_OSERR;
 
171
    }
 
172
  }
 
173
  ret = sigaction(SIGHUP, NULL, &old_action);
 
174
  if(ret == -1){
 
175
    perror("sigaction");
 
176
    return EX_OSERR;
 
177
  }
 
178
  if(old_action.sa_handler != SIG_IGN){
 
179
    ret = sigaction(SIGHUP, &new_action, NULL);
 
180
    if(ret == -1){
 
181
      perror("sigaction");
 
182
      return EX_OSERR;
 
183
    }
 
184
  }
 
185
  ret = sigaction(SIGTERM, NULL, &old_action);
 
186
  if(ret == -1){
 
187
    perror("sigaction");
 
188
    return EX_OSERR;
 
189
  }
 
190
  if(old_action.sa_handler != SIG_IGN){
 
191
    ret = sigaction(SIGTERM, &new_action, NULL);
 
192
    if(ret == -1){
 
193
      perror("sigaction");
 
194
      return EX_OSERR;
 
195
    }
 
196
  }
 
197
  
 
198
  
 
199
  if(debug){
 
200
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
 
201
  }
 
202
  
 
203
  t_new = t_old;
 
204
  t_new.c_lflag &= ~(tcflag_t)ECHO;
 
205
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
206
    int e = errno;
 
207
    perror("tcsetattr-echo");
 
208
    switch(e){
 
209
    case EBADF:
 
210
    case ENOTTY:
 
211
      return EX_UNAVAILABLE;
 
212
    case EINVAL:
 
213
    default:
 
214
      return EX_OSERR;
 
215
    }
 
216
  }
 
217
  
 
218
  if(debug){
 
219
    fprintf(stderr, "Waiting for input from stdin \n");
 
220
  }
 
221
  while(true){
 
222
    if(quit_now){
 
223
      if(debug){
 
224
        fprintf(stderr, "Interrupted by signal, exiting.\n");
 
225
      }
 
226
      status = EXIT_FAILURE;
 
227
      break;
 
228
    }
 
229
 
 
230
    if(prefix){
 
231
      fprintf(stderr, "%s ", prefix);
 
232
    }
 
233
    {
 
234
      const char *cryptsource = getenv("cryptsource");
 
235
      const char *crypttarget = getenv("crypttarget");
 
236
      const char *const prompt
 
237
        = "Enter passphrase to unlock the disk";
 
238
      if(cryptsource == NULL){
 
239
        if(crypttarget == NULL){
 
240
          fprintf(stderr, "%s: ", prompt);
 
241
        } else {
 
242
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
 
243
        }
 
244
      } else {
 
245
        if(crypttarget == NULL){
 
246
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
 
247
        } else {
 
248
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
 
249
                  crypttarget);
 
250
        }
 
251
      }
 
252
    }
 
253
    ret = getline(&buffer, &n, stdin);
 
254
    if(ret > 0){
 
255
      status = EXIT_SUCCESS;
 
256
      /* Make n = data size instead of allocated buffer size */
 
257
      n = (size_t)ret;
 
258
      /* Strip final newline */
 
259
      if(n > 0 and buffer[n-1] == '\n'){
 
260
        buffer[n-1] = '\0';     /* not strictly necessary */
 
261
        n--;
 
262
      }
 
263
      size_t written = 0;
 
264
      while(written < n){
 
265
        ret = write(STDOUT_FILENO, buffer + written, n - written);
 
266
        if(ret < 0){
 
267
          int e = errno;
 
268
          perror("write");
 
269
          switch(e){
 
270
          case EBADF:
 
271
          case EFAULT:
 
272
          case EINVAL:
 
273
          case EFBIG:
 
274
          case EIO:
 
275
          case ENOSPC:
 
276
          default:
 
277
            status = EX_IOERR;
 
278
            break;
 
279
          case EINTR:
 
280
            status = EXIT_FAILURE;
 
281
            break;
 
282
          }
 
283
          break;
 
284
        }
 
285
        written += (size_t)ret;
 
286
      }
 
287
      ret = close(STDOUT_FILENO);
 
288
      if(ret == -1){
 
289
        int e = errno;
 
290
        perror("close");
 
291
        switch(e){
 
292
        case EBADF:
 
293
          status = EX_OSFILE;
 
294
          break;
 
295
        case EIO:
 
296
        default:
 
297
          status = EX_IOERR;
 
298
          break;
 
299
        }
 
300
      }
 
301
      break;
 
302
    }
 
303
    if(ret < 0){
 
304
      int e = errno;
 
305
      if(errno != EINTR and not feof(stdin)){
 
306
        perror("getline");
 
307
        switch(e){
 
308
        case EBADF:
 
309
          status = EX_UNAVAILABLE;
 
310
        case EIO:
 
311
        case EINVAL:
 
312
        default:
 
313
          status = EX_IOERR;
 
314
          break;
 
315
        }
 
316
        break;
 
317
      }
 
318
    }
 
319
    /* if(ret == 0), then the only sensible thing to do is to retry to
 
320
       read from stdin */
 
321
    fputc('\n', stderr);
 
322
    if(debug and not quit_now){
 
323
      /* If quit_now is nonzero, we were interrupted by a signal, and
 
324
         will print that later, so no need to show this too. */
 
325
      fprintf(stderr, "getline() returned 0, retrying.\n");
 
326
    }
 
327
  }
 
328
  
 
329
  free(buffer);
 
330
  
 
331
  if(debug){
 
332
    fprintf(stderr, "Restoring terminal attributes\n");
 
333
  }
 
334
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
335
    perror("tcsetattr+echo");
 
336
  }
 
337
  
 
338
  if(quit_now){
 
339
    sigemptyset(&old_action.sa_mask);
 
340
    old_action.sa_handler = SIG_DFL;
 
341
    ret = sigaction(signal_received, &old_action, NULL);
 
342
    if(ret == -1){
 
343
      perror("sigaction");
 
344
    }
 
345
    raise(signal_received);
 
346
  }
 
347
  
 
348
  if(debug){
 
349
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
 
350
            status);
 
351
  }
 
352
  if(status == EXIT_SUCCESS or status == EX_OK){
 
353
    fputc('\n', stderr);
 
354
  }
 
355
  
 
356
  return status;
 
357
}