/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: 2018-08-19 20:59:06 UTC
  • mfrom: (237.4.100 release)
  • Revision ID: teddy@recompile.se-20180819205906-c2nlh8f4f7ylxers
Merge from release branch

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-2019, 2021-2022 Teddy Hogeborn
6
 
 * Copyright © 2008-2019, 2021-2022 Björn Påhlsson
 
5
 * Copyright © 2008-2018 Teddy Hogeborn
 
6
 * Copyright © 2008-2018 Björn Påhlsson
7
7
 * 
8
8
 * This file is part of Mandos.
9
9
 * 
23
23
 * Contact the authors at <mandos@recompile.se>.
24
24
 */
25
25
 
26
 
#define _GNU_SOURCE             /* vasprintf(),
27
 
                                   program_invocation_short_name,
28
 
                                   asprintf(), getline() */
29
 
#include <sys/types.h>          /* sig_atomic_t, pid_t */
 
26
#define _GNU_SOURCE             /* getline(), asprintf() */
 
27
 
 
28
#include <termios.h>            /* struct termios, tcsetattr(),
 
29
                                   TCSAFLUSH, tcgetattr(), ECHO */
 
30
#include <unistd.h>             /* struct termios, tcsetattr(),
 
31
                                   STDIN_FILENO, TCSAFLUSH,
 
32
                                   tcgetattr(), ECHO, readlink() */
 
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
 
34
                                   sigaction, sigemptyset(),
 
35
                                   sigaction(), sigaddset(), SIGINT,
 
36
                                   SIGQUIT, SIGHUP, SIGTERM,
 
37
                                   raise() */
 
38
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
39
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t,
 
40
                                   ssize_t, open() */
 
41
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
 
42
                                   getenv(), free() */
 
43
#include <dirent.h>             /* scandir(), alphasort() */
 
44
#include <stdio.h>              /* fprintf(), stderr, getline(),
 
45
                                   stdin, feof(), fputc(), vfprintf(),
 
46
                                   vasprintf() */
 
47
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
 
48
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
 
49
                                */
 
50
#include <error.h>              /* error() */
 
51
#include <iso646.h>             /* or, not */
30
52
#include <stdbool.h>            /* bool, false, true */
31
 
#include <argp.h>               /* argp_program_version,
32
 
                                   argp_program_bug_address,
33
 
                                   struct argp_option,
34
 
                                   struct argp_state, argp_state_help,
35
 
                                   ARGP_HELP_STD_HELP,
36
 
                                   ARGP_HELP_EXIT_ERR,
37
 
                                   ARGP_HELP_EXIT_OK, ARGP_HELP_USAGE,
38
 
                                   argp_err_exit_status,
39
 
                                   ARGP_ERR_UNKNOWN, argp_parse(),
40
 
                                   ARGP_IN_ORDER, ARGP_NO_HELP */
41
 
#include <stdarg.h>             /* va_list, va_start(), vfprintf() */
42
 
#include <stdio.h>              /* vasprintf(), fprintf(), stderr,
43
 
                                   vfprintf(), asprintf(), getline(),
44
 
                                   stdin, feof(), clearerr(),
45
 
                                   fputc() */
46
 
#include <errno.h>              /* program_invocation_short_name,
47
 
                                   errno, ENOENT, error_t, ENOMEM,
48
 
                                   EINVAL, EBADF, ENOTTY, EFAULT,
49
 
                                   EFBIG, EIO, ENOSPC, EINTR */
50
 
#include <string.h>             /* strerror(), strrchr(), strcmp() */
51
 
#include <error.h>              /* error() */
52
 
#include <stdlib.h>             /* free(), realloc(), EXIT_SUCCESS,
53
 
                                   EXIT_FAILURE, getenv() */
54
 
#include <unistd.h>             /* access(), R_OK, ssize_t, close(),
55
 
                                   read(), STDIN_FILENO, write(),
56
 
                                   STDOUT_FILENO */
57
 
#include <dirent.h>             /* struct dirent, scandir(),
58
 
                                   alphasort() */
59
 
#include <inttypes.h>           /* uintmax_t, strtoumax() */
60
 
#include <iso646.h>             /* or, and, not */
61
 
#include <fcntl.h>              /* open(), O_RDONLY */
62
 
#include <stddef.h>             /* NULL, size_t */
63
 
#include <termios.h>            /* struct termios, tcgetattr(),
64
 
                                   tcflag_t, ECHO, tcsetattr(),
65
 
                                   TCSAFLUSH */
66
 
#include <signal.h>             /* struct sigaction, sigemptyset(),
67
 
                                   sigaddset(), SIGINT, SIGHUP,
68
 
                                   SIGTERM, SIG_IGN, SIG_DFL,
69
 
                                   raise() */
70
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
71
 
                                   EX_IOERR, EX_OSFILE, EX_OK */
 
53
#include <inttypes.h>           /* strtoumax() */
 
54
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
55
#include <string.h>             /* strlen, rindex, memcmp, strerror()
 
56
                                 */
 
57
#include <argp.h>               /* struct argp_option, struct
 
58
                                   argp_state, struct argp,
 
59
                                   argp_parse(), error_t,
 
60
                                   ARGP_KEY_ARG, ARGP_KEY_END,
 
61
                                   ARGP_ERR_UNKNOWN */
 
62
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
 
63
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
 
64
#include <fcntl.h>              /* open() */
 
65
#include <stdarg.h>             /* va_list, va_start(), ... */
72
66
 
73
67
volatile sig_atomic_t quit_now = 0;
74
68
int signal_received;
116
110
     from the terminal.  Password-prompt will exit if it detects
117
111
     plymouth since plymouth performs the same functionality.
118
112
   */
119
 
  if(access("/run/plymouth/pid", R_OK) == 0){
120
 
    return true;
121
 
  }
122
 
  
123
113
  __attribute__((nonnull))
124
114
  int is_plymouth(const struct dirent *proc_entry){
125
115
    int ret;
244
234
  struct termios t_new, t_old;
245
235
  char *buffer = NULL;
246
236
  char *prefix = NULL;
247
 
  char *prompt = NULL;
248
237
  int status = EXIT_SUCCESS;
249
238
  struct sigaction old_action,
250
239
    new_action = { .sa_handler = termination_handler,
254
243
      { .name = "prefix", .key = 'p',
255
244
        .arg = "PREFIX", .flags = 0,
256
245
        .doc = "Prefix shown before the prompt", .group = 2 },
257
 
      { .name = "prompt", .key = 129,
258
 
        .arg = "PROMPT", .flags = 0,
259
 
        .doc = "The prompt to show", .group = 2 },
260
246
      { .name = "debug", .key = 128,
261
247
        .doc = "Debug mode", .group = 3 },
262
248
      /*
275
261
    error_t parse_opt (int key, char *arg, struct argp_state *state){
276
262
      errno = 0;
277
263
      switch (key){
278
 
      case 'p':                 /* --prefix */
 
264
      case 'p':
279
265
        prefix = arg;
280
266
        break;
281
 
      case 128:                 /* --debug */
 
267
      case 128:
282
268
        debug = true;
283
269
        break;
284
 
      case 129:                 /* --prompt */
285
 
        prompt = arg;
286
 
        break;
287
270
        /*
288
271
         * These reproduce what we would get without ARGP_NO_HELP
289
272
         */
291
274
        argp_state_help(state, state->out_stream,
292
275
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
293
276
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
294
 
        __builtin_unreachable();
295
277
      case -3:                  /* --usage */
296
278
        argp_state_help(state, state->out_stream,
297
279
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
298
 
        __builtin_unreachable();
299
280
      case 'V':                 /* --version */
300
281
        fprintf(state->out_stream, "%s\n", argp_program_version);
301
282
        exit(argp_err_exit_status);
444
425
    if(prefix){
445
426
      fprintf(stderr, "%s ", prefix);
446
427
    }
447
 
    if(prompt != NULL){
448
 
      fprintf(stderr, "%s: ", prompt);
449
 
    } else {
 
428
    {
450
429
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
451
430
      const char *crypttarget = getenv("CRYPTTAB_NAME");
452
431
      /* Before cryptsetup 1.1.0~rc2 */
527
506
    }
528
507
    if(sret < 0){
529
508
      int e = errno;
530
 
      if(errno != EINTR){
531
 
        if(not feof(stdin)){
532
 
          error_plus(0, errno, "getline");
533
 
          switch(e){
534
 
          case EBADF:
535
 
            status = EX_UNAVAILABLE;
536
 
            break;
537
 
          case EIO:
538
 
          case EINVAL:
539
 
          default:
540
 
            status = EX_IOERR;
541
 
            break;
542
 
          }
543
 
          break;
544
 
        } else {
545
 
          clearerr(stdin);
 
509
      if(errno != EINTR and not feof(stdin)){
 
510
        error_plus(0, errno, "getline");
 
511
        switch(e){
 
512
        case EBADF:
 
513
          status = EX_UNAVAILABLE;
 
514
          break;
 
515
        case EIO:
 
516
        case EINVAL:
 
517
        default:
 
518
          status = EX_IOERR;
 
519
          break;
546
520
        }
 
521
        break;
547
522
      }
548
523
    }
549
524
    /* if(sret == 0), then the only sensible thing to do is to retry