/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: 2010-10-11 17:45:21 UTC
  • mto: (237.4.29 release)
  • mto: This revision was merged to the branch mainline in revision 459.
  • Revision ID: teddy@fukt.bsnet.se-20101011174521-e8we3lh7p0ns3oba
Tags: version-1.2.3-1
* Makefile (version): Changed to "1.2.3".
* NEWS (Version 1.2.3): New entry.
* debian/changelog (1.2.3-1): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 Björn Påhlsson
7
7
 * 
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
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
40
40
                                   getenv() */
41
41
#include <stdio.h>              /* fprintf(), stderr, getline(),
42
 
                                   stdin, feof(), perror(), fputc()
 
42
                                   stdin, feof(), fputc()
43
43
                                */
44
44
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
45
45
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
46
46
                                */
 
47
#include <error.h>              /* error() */
47
48
#include <iso646.h>             /* or, not */
48
49
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
 
50
#include <string.h>             /* strlen, rindex */
50
51
#include <argp.h>               /* struct argp_option, struct
51
52
                                   argp_state, struct argp,
52
53
                                   argp_parse(), error_t,
70
71
}
71
72
 
72
73
int main(int argc, char **argv){
73
 
  ssize_t ret;
 
74
  ssize_t sret;
 
75
  int ret;
74
76
  size_t n;
75
77
  struct termios t_new, t_old;
76
78
  char *buffer = NULL;
139
141
    case ENOMEM:
140
142
    default:
141
143
      errno = ret;
142
 
      perror("argp_parse");
 
144
      error(0, errno, "argp_parse");
143
145
      return EX_OSERR;
144
146
    case EINVAL:
145
147
      return EX_USAGE;
155
157
  
156
158
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
157
159
    int e = errno;
158
 
    perror("tcgetattr");
 
160
    error(0, errno, "tcgetattr");
159
161
    switch(e){
160
162
    case EBADF:
161
163
    case ENOTTY:
168
170
  sigemptyset(&new_action.sa_mask);
169
171
  ret = sigaddset(&new_action.sa_mask, SIGINT);
170
172
  if(ret == -1){
171
 
    perror("sigaddset");
 
173
    error(0, errno, "sigaddset");
172
174
    return EX_OSERR;
173
175
  }
174
176
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
175
177
  if(ret == -1){
176
 
    perror("sigaddset");
 
178
    error(0, errno, "sigaddset");
177
179
    return EX_OSERR;
178
180
  }
179
181
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
180
182
  if(ret == -1){
181
 
    perror("sigaddset");
 
183
    error(0, errno, "sigaddset");
182
184
    return EX_OSERR;
183
185
  }
184
186
  /* Need to check if the handler is SIG_IGN before handling:
187
189
  */
188
190
  ret = sigaction(SIGINT, NULL, &old_action);
189
191
  if(ret == -1){
190
 
    perror("sigaction");
 
192
    error(0, errno, "sigaction");
191
193
    return EX_OSERR;
192
194
  }
193
195
  if(old_action.sa_handler != SIG_IGN){
194
196
    ret = sigaction(SIGINT, &new_action, NULL);
195
197
    if(ret == -1){
196
 
      perror("sigaction");
 
198
      error(0, errno, "sigaction");
197
199
      return EX_OSERR;
198
200
    }
199
201
  }
200
202
  ret = sigaction(SIGHUP, NULL, &old_action);
201
203
  if(ret == -1){
202
 
    perror("sigaction");
 
204
    error(0, errno, "sigaction");
203
205
    return EX_OSERR;
204
206
  }
205
207
  if(old_action.sa_handler != SIG_IGN){
206
208
    ret = sigaction(SIGHUP, &new_action, NULL);
207
209
    if(ret == -1){
208
 
      perror("sigaction");
 
210
      error(0, errno, "sigaction");
209
211
      return EX_OSERR;
210
212
    }
211
213
  }
212
214
  ret = sigaction(SIGTERM, NULL, &old_action);
213
215
  if(ret == -1){
214
 
    perror("sigaction");
 
216
    error(0, errno, "sigaction");
215
217
    return EX_OSERR;
216
218
  }
217
219
  if(old_action.sa_handler != SIG_IGN){
218
220
    ret = sigaction(SIGTERM, &new_action, NULL);
219
221
    if(ret == -1){
220
 
      perror("sigaction");
 
222
      error(0, errno, "sigaction");
221
223
      return EX_OSERR;
222
224
    }
223
225
  }
231
233
  t_new.c_lflag &= ~(tcflag_t)ECHO;
232
234
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
233
235
    int e = errno;
234
 
    perror("tcsetattr-echo");
 
236
    error(0, errno, "tcsetattr-echo");
235
237
    switch(e){
236
238
    case EBADF:
237
239
    case ENOTTY:
258
260
      fprintf(stderr, "%s ", prefix);
259
261
    }
260
262
    {
261
 
      const char *cryptsource = getenv("cryptsource");
262
 
      const char *crypttarget = getenv("crypttarget");
263
 
      const char *const prompt
264
 
        = "Enter passphrase to unlock the disk";
 
263
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
 
264
      const char *crypttarget = getenv("CRYPTTAB_NAME");
 
265
      /* Before cryptsetup 1.1.0~rc2 */
 
266
      if(cryptsource == NULL){
 
267
        cryptsource = getenv("cryptsource");
 
268
      }
 
269
      if(crypttarget == NULL){
 
270
        crypttarget = getenv("crypttarget");
 
271
      }
 
272
      const char *const prompt1 = "Unlocking the disk";
 
273
      const char *const prompt2 = "Enter passphrase";
265
274
      if(cryptsource == NULL){
266
275
        if(crypttarget == NULL){
267
 
          fprintf(stderr, "%s: ", prompt);
 
276
          fprintf(stderr, "%s to unlock the disk: ", prompt2);
268
277
        } else {
269
 
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
 
278
          fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
 
279
                  prompt2);
270
280
        }
271
281
      } else {
272
282
        if(crypttarget == NULL){
273
 
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
 
283
          fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
 
284
                  prompt2);
274
285
        } else {
275
 
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
276
 
                  crypttarget);
 
286
          fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
 
287
                  crypttarget, prompt2);
277
288
        }
278
289
      }
279
290
    }
280
 
    ret = getline(&buffer, &n, stdin);
281
 
    if(ret > 0){
 
291
    sret = getline(&buffer, &n, stdin);
 
292
    if(sret > 0){
282
293
      status = EXIT_SUCCESS;
283
294
      /* Make n = data size instead of allocated buffer size */
284
 
      n = (size_t)ret;
 
295
      n = (size_t)sret;
285
296
      /* Strip final newline */
286
297
      if(n > 0 and buffer[n-1] == '\n'){
287
298
        buffer[n-1] = '\0';     /* not strictly necessary */
289
300
      }
290
301
      size_t written = 0;
291
302
      while(written < n){
292
 
        ret = write(STDOUT_FILENO, buffer + written, n - written);
293
 
        if(ret < 0){
 
303
        sret = write(STDOUT_FILENO, buffer + written, n - written);
 
304
        if(sret < 0){
294
305
          int e = errno;
295
 
          perror("write");
 
306
          error(0, errno, "write");
296
307
          switch(e){
297
308
          case EBADF:
298
309
          case EFAULT:
309
320
          }
310
321
          break;
311
322
        }
312
 
        written += (size_t)ret;
 
323
        written += (size_t)sret;
313
324
      }
314
 
      ret = close(STDOUT_FILENO);
315
 
      if(ret == -1){
 
325
      sret = close(STDOUT_FILENO);
 
326
      if(sret == -1){
316
327
        int e = errno;
317
 
        perror("close");
 
328
        error(0, errno, "close");
318
329
        switch(e){
319
330
        case EBADF:
320
331
          status = EX_OSFILE;
327
338
      }
328
339
      break;
329
340
    }
330
 
    if(ret < 0){
 
341
    if(sret < 0){
331
342
      int e = errno;
332
343
      if(errno != EINTR and not feof(stdin)){
333
 
        perror("getline");
 
344
        error(0, errno, "getline");
334
345
        switch(e){
335
346
        case EBADF:
336
347
          status = EX_UNAVAILABLE;
343
354
        break;
344
355
      }
345
356
    }
346
 
    /* if(ret == 0), then the only sensible thing to do is to retry to
 
357
    /* if(sret == 0), then the only sensible thing to do is to retry to
347
358
       read from stdin */
348
359
    fputc('\n', stderr);
349
360
    if(debug and not quit_now){
359
370
    fprintf(stderr, "Restoring terminal attributes\n");
360
371
  }
361
372
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
362
 
    perror("tcsetattr+echo");
 
373
    error(0, errno, "tcsetattr+echo");
363
374
  }
364
375
  
365
376
  if(quit_now){
367
378
    old_action.sa_handler = SIG_DFL;
368
379
    ret = sigaction(signal_received, &old_action, NULL);
369
380
    if(ret == -1){
370
 
      perror("sigaction");
 
381
      error(0, errno, "sigaction");
371
382
    }
372
383
    raise(signal_received);
373
384
  }