/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: 2011-02-27 18:57:58 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110227185758-vie4xhfaevhpqtr6
* plugins.d/password-prompt.c (conflict_detection): Check for both
                                                    "plymouth" and
                                                    "plymouthd".

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 Teddy Hogeborn
6
 
 * Copyright © 2008-2019 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* getline(), asprintf() */
27
26
 
28
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
27
#include <termios.h>            /* struct termios, tcsetattr(),
29
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
30
 
#include <unistd.h>             /* access(), struct termios,
31
 
                                   tcsetattr(), STDIN_FILENO,
32
 
                                   TCSAFLUSH, tcgetattr(), ECHO,
33
 
                                   readlink() */
 
29
#include <unistd.h>             /* struct termios, tcsetattr(),
 
30
                                   STDIN_FILENO, TCSAFLUSH,
 
31
                                   tcgetattr(), ECHO, readlink() */
34
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
35
33
                                   sigaction, sigemptyset(),
36
34
                                   sigaction(), sigaddset(), SIGINT,
43
41
                                   getenv(), free() */
44
42
#include <dirent.h>             /* scandir(), alphasort() */
45
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
46
 
                                   stdin, feof(), fputc(), vfprintf(),
47
 
                                   vasprintf() */
 
44
                                   stdin, feof(), fputc()
 
45
                                */
48
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
49
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
50
48
                                */
52
50
#include <iso646.h>             /* or, not */
53
51
#include <stdbool.h>            /* bool, false, true */
54
52
#include <inttypes.h>           /* strtoumax() */
55
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
56
 
#include <string.h>             /* strlen, rindex, memcmp, strerror()
57
 
                                 */
 
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
54
#include <string.h>             /* strlen, rindex, memcmp */
58
55
#include <argp.h>               /* struct argp_option, struct
59
56
                                   argp_state, struct argp,
60
57
                                   argp_parse(), error_t,
63
60
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
64
61
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
65
62
#include <fcntl.h>              /* open() */
66
 
#include <stdarg.h>             /* va_list, va_start(), ... */
67
63
 
68
64
volatile sig_atomic_t quit_now = 0;
69
65
int signal_received;
70
66
bool debug = false;
71
67
const char *argp_program_version = "password-prompt " VERSION;
72
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
73
69
 
74
70
/* Needed for conflict resolution */
75
71
const char plymouth_name[] = "plymouthd";
 
72
const char plymouth_alt_name[] = "plymouthd";
76
73
 
77
 
/* Function to use when printing errors */
78
 
__attribute__((format (gnu_printf, 3, 4)))
79
 
void error_plus(int status, int errnum, const char *formatstring,
80
 
                ...){
81
 
  va_list ap;
82
 
  char *text;
83
 
  int ret;
84
 
  
85
 
  va_start(ap, formatstring);
86
 
  ret = vasprintf(&text, formatstring, ap);
87
 
  if(ret == -1){
88
 
    fprintf(stderr, "Mandos plugin %s: ",
89
 
            program_invocation_short_name);
90
 
    vfprintf(stderr, formatstring, ap);
91
 
    fprintf(stderr, ": %s\n", strerror(errnum));
92
 
    error(status, errno, "vasprintf while printing error");
93
 
    return;
94
 
  }
95
 
  fprintf(stderr, "Mandos plugin ");
96
 
  error(status, errnum, "%s", text);
97
 
  free(text);
98
 
}
99
74
 
100
75
static void termination_handler(int signum){
101
76
  if(quit_now){
111
86
     from the terminal.  Password-prompt will exit if it detects
112
87
     plymouth since plymouth performs the same functionality.
113
88
   */
114
 
  if(access("/run/plymouth/pid", R_OK) == 0){
115
 
    return true;
116
 
  }
117
 
  
118
 
  __attribute__((nonnull))
119
89
  int is_plymouth(const struct dirent *proc_entry){
120
90
    int ret;
121
91
    int cl_fd;
122
92
    {
123
 
      uintmax_t proc_id;
 
93
      uintmax_t maxvalue;
124
94
      char *tmp;
125
95
      errno = 0;
126
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
96
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
127
97
      
128
98
      if(errno != 0 or *tmp != '\0'
129
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
99
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
130
100
        return 0;
131
101
      }
132
102
    }
133
103
    
134
104
    char *cmdline_filename;
135
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
136
 
                   proc_entry->d_name);
 
105
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
137
106
    if(ret == -1){
138
 
      error_plus(0, errno, "asprintf");
 
107
      error(0, errno, "asprintf");
139
108
      return 0;
140
109
    }
141
110
    
142
 
    /* Open /proc/<pid>/cmdline */
 
111
    /* Open /proc/<pid>/cmdline  */
143
112
    cl_fd = open(cmdline_filename, O_RDONLY);
144
113
    free(cmdline_filename);
145
114
    if(cl_fd == -1){
146
 
      if(errno != ENOENT){
147
 
        error_plus(0, errno, "open");
148
 
      }
 
115
      error(0, errno, "open");
149
116
      return 0;
150
117
    }
151
118
    
161
128
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
162
129
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
163
130
          if(tmp == NULL){
164
 
            error_plus(0, errno, "realloc");
 
131
            error(0, errno, "realloc");
165
132
            free(cmdline);
166
133
            close(cl_fd);
167
134
            return 0;
174
141
        sret = read(cl_fd, cmdline + cmdline_len,
175
142
                    cmdline_allocated - cmdline_len);
176
143
        if(sret == -1){
177
 
          error_plus(0, errno, "read");
 
144
          error(0, errno, "read");
178
145
          free(cmdline);
179
146
          close(cl_fd);
180
147
          return 0;
183
150
      } while(sret != 0);
184
151
      ret = close(cl_fd);
185
152
      if(ret == -1){
186
 
        error_plus(0, errno, "close");
 
153
        error(0, errno, "close");
187
154
        free(cmdline);
188
155
        return 0;
189
156
      }
199
166
      cmdline_base = cmdline;
200
167
    }
201
168
    
202
 
    if(strcmp(cmdline_base, plymouth_name) != 0){
203
 
      if(debug){
204
 
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
205
 
                plymouth_name);
206
 
      }
 
169
    if((strcmp(cmdline_base, plymouth_name) != 0)
 
170
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
207
171
      free(cmdline);
208
172
      return 0;
209
173
    }
210
 
    if(debug){
211
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
212
 
              plymouth_name);
213
 
    }
214
174
    free(cmdline);
215
175
    return 1;
216
176
  }
217
 
  
218
 
  struct dirent **direntries = NULL;
 
177
 
 
178
  struct dirent **direntries;
219
179
  int ret;
220
180
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
221
 
  if(ret == -1){
222
 
    error_plus(1, errno, "scandir");
223
 
  }
224
 
  {
225
 
    int i = ret;
226
 
    while(i--){
227
 
      free(direntries[i]);
228
 
    }
229
 
  }
230
 
  free(direntries);
 
181
  if (ret == -1){
 
182
    error(1, errno, "scandir");
 
183
  }
231
184
  return ret > 0;
232
185
}
233
186
 
239
192
  struct termios t_new, t_old;
240
193
  char *buffer = NULL;
241
194
  char *prefix = NULL;
242
 
  char *prompt = NULL;
243
195
  int status = EXIT_SUCCESS;
244
196
  struct sigaction old_action,
245
197
    new_action = { .sa_handler = termination_handler,
249
201
      { .name = "prefix", .key = 'p',
250
202
        .arg = "PREFIX", .flags = 0,
251
203
        .doc = "Prefix shown before the prompt", .group = 2 },
252
 
      { .name = "prompt", .key = 129,
253
 
        .arg = "PROMPT", .flags = 0,
254
 
        .doc = "The prompt to show", .group = 2 },
255
204
      { .name = "debug", .key = 128,
256
205
        .doc = "Debug mode", .group = 3 },
257
206
      /*
266
215
      { .name = NULL }
267
216
    };
268
217
    
269
 
    __attribute__((nonnull(3)))
270
218
    error_t parse_opt (int key, char *arg, struct argp_state *state){
271
219
      errno = 0;
272
220
      switch (key){
273
 
      case 'p':                 /* --prefix */
 
221
      case 'p':
274
222
        prefix = arg;
275
223
        break;
276
 
      case 128:                 /* --debug */
 
224
      case 128:
277
225
        debug = true;
278
226
        break;
279
 
      case 129:                 /* --prompt */
280
 
        prompt = arg;
281
 
        break;
282
227
        /*
283
228
         * These reproduce what we would get without ARGP_NO_HELP
284
229
         */
286
231
        argp_state_help(state, state->out_stream,
287
232
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
288
233
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
289
 
        __builtin_unreachable();
290
234
      case -3:                  /* --usage */
291
235
        argp_state_help(state, state->out_stream,
292
236
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
293
 
        __builtin_unreachable();
294
237
      case 'V':                 /* --version */
295
238
        fprintf(state->out_stream, "%s\n", argp_program_version);
296
239
        exit(argp_err_exit_status);
313
256
    case ENOMEM:
314
257
    default:
315
258
      errno = ret;
316
 
      error_plus(0, errno, "argp_parse");
 
259
      error(0, errno, "argp_parse");
317
260
      return EX_OSERR;
318
261
    case EINVAL:
319
262
      return EX_USAGE;
324
267
    fprintf(stderr, "Starting %s\n", argv[0]);
325
268
  }
326
269
 
327
 
  if(conflict_detection()){
 
270
  if (conflict_detection()){
328
271
    if(debug){
329
 
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
 
272
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
330
273
    }
331
274
    return EXIT_FAILURE;
332
275
  }
337
280
  
338
281
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
339
282
    int e = errno;
340
 
    error_plus(0, errno, "tcgetattr");
 
283
    error(0, errno, "tcgetattr");
341
284
    switch(e){
342
285
    case EBADF:
343
286
    case ENOTTY:
350
293
  sigemptyset(&new_action.sa_mask);
351
294
  ret = sigaddset(&new_action.sa_mask, SIGINT);
352
295
  if(ret == -1){
353
 
    error_plus(0, errno, "sigaddset");
 
296
    error(0, errno, "sigaddset");
354
297
    return EX_OSERR;
355
298
  }
356
299
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
357
300
  if(ret == -1){
358
 
    error_plus(0, errno, "sigaddset");
 
301
    error(0, errno, "sigaddset");
359
302
    return EX_OSERR;
360
303
  }
361
304
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
305
  if(ret == -1){
363
 
    error_plus(0, errno, "sigaddset");
 
306
    error(0, errno, "sigaddset");
364
307
    return EX_OSERR;
365
308
  }
366
309
  /* Need to check if the handler is SIG_IGN before handling:
369
312
  */
370
313
  ret = sigaction(SIGINT, NULL, &old_action);
371
314
  if(ret == -1){
372
 
    error_plus(0, errno, "sigaction");
 
315
    error(0, errno, "sigaction");
373
316
    return EX_OSERR;
374
317
  }
375
318
  if(old_action.sa_handler != SIG_IGN){
376
319
    ret = sigaction(SIGINT, &new_action, NULL);
377
320
    if(ret == -1){
378
 
      error_plus(0, errno, "sigaction");
 
321
      error(0, errno, "sigaction");
379
322
      return EX_OSERR;
380
323
    }
381
324
  }
382
325
  ret = sigaction(SIGHUP, NULL, &old_action);
383
326
  if(ret == -1){
384
 
    error_plus(0, errno, "sigaction");
 
327
    error(0, errno, "sigaction");
385
328
    return EX_OSERR;
386
329
  }
387
330
  if(old_action.sa_handler != SIG_IGN){
388
331
    ret = sigaction(SIGHUP, &new_action, NULL);
389
332
    if(ret == -1){
390
 
      error_plus(0, errno, "sigaction");
 
333
      error(0, errno, "sigaction");
391
334
      return EX_OSERR;
392
335
    }
393
336
  }
394
337
  ret = sigaction(SIGTERM, NULL, &old_action);
395
338
  if(ret == -1){
396
 
    error_plus(0, errno, "sigaction");
 
339
    error(0, errno, "sigaction");
397
340
    return EX_OSERR;
398
341
  }
399
342
  if(old_action.sa_handler != SIG_IGN){
400
343
    ret = sigaction(SIGTERM, &new_action, NULL);
401
344
    if(ret == -1){
402
 
      error_plus(0, errno, "sigaction");
 
345
      error(0, errno, "sigaction");
403
346
      return EX_OSERR;
404
347
    }
405
348
  }
413
356
  t_new.c_lflag &= ~(tcflag_t)ECHO;
414
357
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
415
358
    int e = errno;
416
 
    error_plus(0, errno, "tcsetattr-echo");
 
359
    error(0, errno, "tcsetattr-echo");
417
360
    switch(e){
418
361
    case EBADF:
419
362
    case ENOTTY:
439
382
    if(prefix){
440
383
      fprintf(stderr, "%s ", prefix);
441
384
    }
442
 
    if(prompt != NULL){
443
 
      fprintf(stderr, "%s: ", prompt);
444
 
    } else {
 
385
    {
445
386
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
446
387
      const char *crypttarget = getenv("CRYPTTAB_NAME");
447
388
      /* Before cryptsetup 1.1.0~rc2 */
485
426
        sret = write(STDOUT_FILENO, buffer + written, n - written);
486
427
        if(sret < 0){
487
428
          int e = errno;
488
 
          error_plus(0, errno, "write");
 
429
          error(0, errno, "write");
489
430
          switch(e){
490
431
          case EBADF:
491
432
          case EFAULT:
507
448
      sret = close(STDOUT_FILENO);
508
449
      if(sret == -1){
509
450
        int e = errno;
510
 
        error_plus(0, errno, "close");
 
451
        error(0, errno, "close");
511
452
        switch(e){
512
453
        case EBADF:
513
454
          status = EX_OSFILE;
522
463
    }
523
464
    if(sret < 0){
524
465
      int e = errno;
525
 
      if(errno != EINTR){
526
 
        if(not feof(stdin)){
527
 
          error_plus(0, errno, "getline");
528
 
          switch(e){
529
 
          case EBADF:
530
 
            status = EX_UNAVAILABLE;
531
 
            break;
532
 
          case EIO:
533
 
          case EINVAL:
534
 
          default:
535
 
            status = EX_IOERR;
536
 
            break;
537
 
          }
 
466
      if(errno != EINTR and not feof(stdin)){
 
467
        error(0, errno, "getline");
 
468
        switch(e){
 
469
        case EBADF:
 
470
          status = EX_UNAVAILABLE;
 
471
        case EIO:
 
472
        case EINVAL:
 
473
        default:
 
474
          status = EX_IOERR;
538
475
          break;
539
 
        } else {
540
 
          clearerr(stdin);
541
476
        }
 
477
        break;
542
478
      }
543
479
    }
544
 
    /* if(sret == 0), then the only sensible thing to do is to retry
545
 
       to read from stdin */
 
480
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
481
       read from stdin */
546
482
    fputc('\n', stderr);
547
483
    if(debug and not quit_now){
548
484
      /* If quit_now is nonzero, we were interrupted by a signal, and
557
493
    fprintf(stderr, "Restoring terminal attributes\n");
558
494
  }
559
495
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
560
 
    error_plus(0, errno, "tcsetattr+echo");
 
496
    error(0, errno, "tcsetattr+echo");
561
497
  }
562
498
  
563
499
  if(quit_now){
565
501
    old_action.sa_handler = SIG_DFL;
566
502
    ret = sigaction(signal_received, &old_action, NULL);
567
503
    if(ret == -1){
568
 
      error_plus(0, errno, "sigaction");
 
504
      error(0, errno, "sigaction");
569
505
    }
570
506
    raise(signal_received);
571
507
  }