/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 17:46:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110227174647-oc7prq1d63lfkn2e
* debian/control (mandos-client): Fix syntax of conflicts line.

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>";
73
 
 
74
 
/* Needed for conflict resolution */
75
 
const char plymouth_name[] = "plymouthd";
76
 
 
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
 
}
 
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
69
 
 
70
/* Needed for conflic resolution */
 
71
const char plymouthd_name[] = "plymouthd";
 
72
 
99
73
 
100
74
static void termination_handler(int signum){
101
75
  if(quit_now){
111
85
     from the terminal.  Password-prompt will exit if it detects
112
86
     plymouth since plymouth performs the same functionality.
113
87
   */
114
 
  if(access("/run/plymouth/pid", R_OK) == 0){
115
 
    return true;
116
 
  }
117
 
  
118
 
  __attribute__((nonnull))
119
88
  int is_plymouth(const struct dirent *proc_entry){
120
89
    int ret;
121
90
    int cl_fd;
122
91
    {
123
 
      uintmax_t proc_id;
 
92
      uintmax_t maxvalue;
124
93
      char *tmp;
125
94
      errno = 0;
126
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
95
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
127
96
      
128
97
      if(errno != 0 or *tmp != '\0'
129
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
98
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
130
99
        return 0;
131
100
      }
132
101
    }
133
102
    
134
103
    char *cmdline_filename;
135
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
136
 
                   proc_entry->d_name);
 
104
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
137
105
    if(ret == -1){
138
 
      error_plus(0, errno, "asprintf");
 
106
      error(0, errno, "asprintf");
139
107
      return 0;
140
108
    }
141
109
    
142
 
    /* Open /proc/<pid>/cmdline */
 
110
    /* Open /proc/<pid>/cmdline  */
143
111
    cl_fd = open(cmdline_filename, O_RDONLY);
144
112
    free(cmdline_filename);
145
113
    if(cl_fd == -1){
146
 
      if(errno != ENOENT){
147
 
        error_plus(0, errno, "open");
148
 
      }
 
114
      error(0, errno, "open");
149
115
      return 0;
150
116
    }
151
117
    
161
127
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
162
128
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
163
129
          if(tmp == NULL){
164
 
            error_plus(0, errno, "realloc");
 
130
            error(0, errno, "realloc");
165
131
            free(cmdline);
166
132
            close(cl_fd);
167
133
            return 0;
174
140
        sret = read(cl_fd, cmdline + cmdline_len,
175
141
                    cmdline_allocated - cmdline_len);
176
142
        if(sret == -1){
177
 
          error_plus(0, errno, "read");
 
143
          error(0, errno, "read");
178
144
          free(cmdline);
179
145
          close(cl_fd);
180
146
          return 0;
183
149
      } while(sret != 0);
184
150
      ret = close(cl_fd);
185
151
      if(ret == -1){
186
 
        error_plus(0, errno, "close");
 
152
        error(0, errno, "close");
187
153
        free(cmdline);
188
154
        return 0;
189
155
      }
199
165
      cmdline_base = cmdline;
200
166
    }
201
167
    
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
 
      }
 
168
    if(strcmp(cmdline_base, plymouthd_name) != 0){
207
169
      free(cmdline);
208
170
      return 0;
209
171
    }
210
 
    if(debug){
211
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
212
 
              plymouth_name);
213
 
    }
214
172
    free(cmdline);
215
173
    return 1;
216
174
  }
217
 
  
218
 
  struct dirent **direntries = NULL;
 
175
 
 
176
  struct dirent **direntries;
219
177
  int ret;
220
178
  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);
 
179
  if (ret == -1){
 
180
    error(1, errno, "scandir");
 
181
  }
231
182
  return ret > 0;
232
183
}
233
184
 
239
190
  struct termios t_new, t_old;
240
191
  char *buffer = NULL;
241
192
  char *prefix = NULL;
242
 
  char *prompt = NULL;
243
193
  int status = EXIT_SUCCESS;
244
194
  struct sigaction old_action,
245
195
    new_action = { .sa_handler = termination_handler,
249
199
      { .name = "prefix", .key = 'p',
250
200
        .arg = "PREFIX", .flags = 0,
251
201
        .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
202
      { .name = "debug", .key = 128,
256
203
        .doc = "Debug mode", .group = 3 },
257
204
      /*
266
213
      { .name = NULL }
267
214
    };
268
215
    
269
 
    __attribute__((nonnull(3)))
270
216
    error_t parse_opt (int key, char *arg, struct argp_state *state){
271
217
      errno = 0;
272
218
      switch (key){
273
 
      case 'p':                 /* --prefix */
 
219
      case 'p':
274
220
        prefix = arg;
275
221
        break;
276
 
      case 128:                 /* --debug */
 
222
      case 128:
277
223
        debug = true;
278
224
        break;
279
 
      case 129:                 /* --prompt */
280
 
        prompt = arg;
281
 
        break;
282
225
        /*
283
226
         * These reproduce what we would get without ARGP_NO_HELP
284
227
         */
286
229
        argp_state_help(state, state->out_stream,
287
230
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
288
231
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
289
 
        __builtin_unreachable();
290
232
      case -3:                  /* --usage */
291
233
        argp_state_help(state, state->out_stream,
292
234
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
293
 
        __builtin_unreachable();
294
235
      case 'V':                 /* --version */
295
236
        fprintf(state->out_stream, "%s\n", argp_program_version);
296
237
        exit(argp_err_exit_status);
313
254
    case ENOMEM:
314
255
    default:
315
256
      errno = ret;
316
 
      error_plus(0, errno, "argp_parse");
 
257
      error(0, errno, "argp_parse");
317
258
      return EX_OSERR;
318
259
    case EINVAL:
319
260
      return EX_USAGE;
324
265
    fprintf(stderr, "Starting %s\n", argv[0]);
325
266
  }
326
267
 
327
 
  if(conflict_detection()){
 
268
  if (conflict_detection()){
328
269
    if(debug){
329
 
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
 
270
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
330
271
    }
331
272
    return EXIT_FAILURE;
332
273
  }
337
278
  
338
279
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
339
280
    int e = errno;
340
 
    error_plus(0, errno, "tcgetattr");
 
281
    error(0, errno, "tcgetattr");
341
282
    switch(e){
342
283
    case EBADF:
343
284
    case ENOTTY:
350
291
  sigemptyset(&new_action.sa_mask);
351
292
  ret = sigaddset(&new_action.sa_mask, SIGINT);
352
293
  if(ret == -1){
353
 
    error_plus(0, errno, "sigaddset");
 
294
    error(0, errno, "sigaddset");
354
295
    return EX_OSERR;
355
296
  }
356
297
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
357
298
  if(ret == -1){
358
 
    error_plus(0, errno, "sigaddset");
 
299
    error(0, errno, "sigaddset");
359
300
    return EX_OSERR;
360
301
  }
361
302
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
303
  if(ret == -1){
363
 
    error_plus(0, errno, "sigaddset");
 
304
    error(0, errno, "sigaddset");
364
305
    return EX_OSERR;
365
306
  }
366
307
  /* Need to check if the handler is SIG_IGN before handling:
369
310
  */
370
311
  ret = sigaction(SIGINT, NULL, &old_action);
371
312
  if(ret == -1){
372
 
    error_plus(0, errno, "sigaction");
 
313
    error(0, errno, "sigaction");
373
314
    return EX_OSERR;
374
315
  }
375
316
  if(old_action.sa_handler != SIG_IGN){
376
317
    ret = sigaction(SIGINT, &new_action, NULL);
377
318
    if(ret == -1){
378
 
      error_plus(0, errno, "sigaction");
 
319
      error(0, errno, "sigaction");
379
320
      return EX_OSERR;
380
321
    }
381
322
  }
382
323
  ret = sigaction(SIGHUP, NULL, &old_action);
383
324
  if(ret == -1){
384
 
    error_plus(0, errno, "sigaction");
 
325
    error(0, errno, "sigaction");
385
326
    return EX_OSERR;
386
327
  }
387
328
  if(old_action.sa_handler != SIG_IGN){
388
329
    ret = sigaction(SIGHUP, &new_action, NULL);
389
330
    if(ret == -1){
390
 
      error_plus(0, errno, "sigaction");
 
331
      error(0, errno, "sigaction");
391
332
      return EX_OSERR;
392
333
    }
393
334
  }
394
335
  ret = sigaction(SIGTERM, NULL, &old_action);
395
336
  if(ret == -1){
396
 
    error_plus(0, errno, "sigaction");
 
337
    error(0, errno, "sigaction");
397
338
    return EX_OSERR;
398
339
  }
399
340
  if(old_action.sa_handler != SIG_IGN){
400
341
    ret = sigaction(SIGTERM, &new_action, NULL);
401
342
    if(ret == -1){
402
 
      error_plus(0, errno, "sigaction");
 
343
      error(0, errno, "sigaction");
403
344
      return EX_OSERR;
404
345
    }
405
346
  }
413
354
  t_new.c_lflag &= ~(tcflag_t)ECHO;
414
355
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
415
356
    int e = errno;
416
 
    error_plus(0, errno, "tcsetattr-echo");
 
357
    error(0, errno, "tcsetattr-echo");
417
358
    switch(e){
418
359
    case EBADF:
419
360
    case ENOTTY:
439
380
    if(prefix){
440
381
      fprintf(stderr, "%s ", prefix);
441
382
    }
442
 
    if(prompt != NULL){
443
 
      fprintf(stderr, "%s: ", prompt);
444
 
    } else {
 
383
    {
445
384
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
446
385
      const char *crypttarget = getenv("CRYPTTAB_NAME");
447
386
      /* Before cryptsetup 1.1.0~rc2 */
485
424
        sret = write(STDOUT_FILENO, buffer + written, n - written);
486
425
        if(sret < 0){
487
426
          int e = errno;
488
 
          error_plus(0, errno, "write");
 
427
          error(0, errno, "write");
489
428
          switch(e){
490
429
          case EBADF:
491
430
          case EFAULT:
507
446
      sret = close(STDOUT_FILENO);
508
447
      if(sret == -1){
509
448
        int e = errno;
510
 
        error_plus(0, errno, "close");
 
449
        error(0, errno, "close");
511
450
        switch(e){
512
451
        case EBADF:
513
452
          status = EX_OSFILE;
522
461
    }
523
462
    if(sret < 0){
524
463
      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
 
          }
 
464
      if(errno != EINTR and not feof(stdin)){
 
465
        error(0, errno, "getline");
 
466
        switch(e){
 
467
        case EBADF:
 
468
          status = EX_UNAVAILABLE;
 
469
        case EIO:
 
470
        case EINVAL:
 
471
        default:
 
472
          status = EX_IOERR;
538
473
          break;
539
 
        } else {
540
 
          clearerr(stdin);
541
474
        }
 
475
        break;
542
476
      }
543
477
    }
544
 
    /* if(sret == 0), then the only sensible thing to do is to retry
545
 
       to read from stdin */
 
478
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
479
       read from stdin */
546
480
    fputc('\n', stderr);
547
481
    if(debug and not quit_now){
548
482
      /* If quit_now is nonzero, we were interrupted by a signal, and
557
491
    fprintf(stderr, "Restoring terminal attributes\n");
558
492
  }
559
493
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
560
 
    error_plus(0, errno, "tcsetattr+echo");
 
494
    error(0, errno, "tcsetattr+echo");
561
495
  }
562
496
  
563
497
  if(quit_now){
565
499
    old_action.sa_handler = SIG_DFL;
566
500
    ret = sigaction(signal_received, &old_action, NULL);
567
501
    if(ret == -1){
568
 
      error_plus(0, errno, "sigaction");
 
502
      error(0, errno, "sigaction");
569
503
    }
570
504
    raise(signal_received);
571
505
  }