/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/password-prompt.c

* plugins.d/password-prompt.c: Some white space fixes.  Break some
                               long lines.  Add more debug output.

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
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
                                */
53
51
#include <stdbool.h>            /* bool, false, true */
54
52
#include <inttypes.h>           /* strtoumax() */
55
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
56
 
#include <string.h>             /* strlen, rindex, memcmp, strerror()
57
 
                                 */
 
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
    }
135
105
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
136
106
                   proc_entry->d_name);
137
107
    if(ret == -1){
138
 
      error_plus(0, errno, "asprintf");
 
108
      error(0, errno, "asprintf");
139
109
      return 0;
140
110
    }
141
111
    
143
113
    cl_fd = open(cmdline_filename, O_RDONLY);
144
114
    free(cmdline_filename);
145
115
    if(cl_fd == -1){
146
 
      if(errno != ENOENT){
147
 
        error_plus(0, errno, "open");
148
 
      }
 
116
      error(0, errno, "open");
149
117
      return 0;
150
118
    }
151
119
    
161
129
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
162
130
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
163
131
          if(tmp == NULL){
164
 
            error_plus(0, errno, "realloc");
 
132
            error(0, errno, "realloc");
165
133
            free(cmdline);
166
134
            close(cl_fd);
167
135
            return 0;
174
142
        sret = read(cl_fd, cmdline + cmdline_len,
175
143
                    cmdline_allocated - cmdline_len);
176
144
        if(sret == -1){
177
 
          error_plus(0, errno, "read");
 
145
          error(0, errno, "read");
178
146
          free(cmdline);
179
147
          close(cl_fd);
180
148
          return 0;
183
151
      } while(sret != 0);
184
152
      ret = close(cl_fd);
185
153
      if(ret == -1){
186
 
        error_plus(0, errno, "close");
 
154
        error(0, errno, "close");
187
155
        free(cmdline);
188
156
        return 0;
189
157
      }
199
167
      cmdline_base = cmdline;
200
168
    }
201
169
    
202
 
    if(strcmp(cmdline_base, plymouth_name) != 0){
 
170
    if((strcmp(cmdline_base, plymouth_name) != 0)
 
171
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
203
172
      if(debug){
204
 
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
205
 
                plymouth_name);
 
173
        fprintf(stderr, "\"%s\" is not \"%s\" or \"%s\"\n",
 
174
                cmdline_base, plymouth_name, plymouth_alt_name);
206
175
      }
207
176
      free(cmdline);
208
177
      return 0;
209
178
    }
210
 
    if(debug){
211
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
212
 
              plymouth_name);
213
 
    }
 
179
    fprintf(stderr, "\"%s\" equals \"%s\" or \"%s\"\n",
 
180
            cmdline_base, plymouth_name, plymouth_alt_name);
214
181
    free(cmdline);
215
182
    return 1;
216
183
  }
217
184
  
218
 
  struct dirent **direntries = NULL;
 
185
  struct dirent **direntries;
219
186
  int ret;
220
187
  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);
 
188
  if (ret == -1){
 
189
    error(1, errno, "scandir");
 
190
  }
231
191
  return ret > 0;
232
192
}
233
193
 
239
199
  struct termios t_new, t_old;
240
200
  char *buffer = NULL;
241
201
  char *prefix = NULL;
242
 
  char *prompt = NULL;
243
202
  int status = EXIT_SUCCESS;
244
203
  struct sigaction old_action,
245
204
    new_action = { .sa_handler = termination_handler,
249
208
      { .name = "prefix", .key = 'p',
250
209
        .arg = "PREFIX", .flags = 0,
251
210
        .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
211
      { .name = "debug", .key = 128,
256
212
        .doc = "Debug mode", .group = 3 },
257
213
      /*
266
222
      { .name = NULL }
267
223
    };
268
224
    
269
 
    __attribute__((nonnull(3)))
270
225
    error_t parse_opt (int key, char *arg, struct argp_state *state){
271
226
      errno = 0;
272
227
      switch (key){
273
 
      case 'p':                 /* --prefix */
 
228
      case 'p':
274
229
        prefix = arg;
275
230
        break;
276
 
      case 128:                 /* --debug */
 
231
      case 128:
277
232
        debug = true;
278
233
        break;
279
 
      case 129:                 /* --prompt */
280
 
        prompt = arg;
281
 
        break;
282
234
        /*
283
235
         * These reproduce what we would get without ARGP_NO_HELP
284
236
         */
286
238
        argp_state_help(state, state->out_stream,
287
239
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
288
240
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
289
 
        __builtin_unreachable();
290
241
      case -3:                  /* --usage */
291
242
        argp_state_help(state, state->out_stream,
292
243
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
293
 
        __builtin_unreachable();
294
244
      case 'V':                 /* --version */
295
245
        fprintf(state->out_stream, "%s\n", argp_program_version);
296
246
        exit(argp_err_exit_status);
313
263
    case ENOMEM:
314
264
    default:
315
265
      errno = ret;
316
 
      error_plus(0, errno, "argp_parse");
 
266
      error(0, errno, "argp_parse");
317
267
      return EX_OSERR;
318
268
    case EINVAL:
319
269
      return EX_USAGE;
324
274
    fprintf(stderr, "Starting %s\n", argv[0]);
325
275
  }
326
276
 
327
 
  if(conflict_detection()){
 
277
  if (conflict_detection()){
328
278
    if(debug){
329
279
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
330
280
    }
337
287
  
338
288
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
339
289
    int e = errno;
340
 
    error_plus(0, errno, "tcgetattr");
 
290
    error(0, errno, "tcgetattr");
341
291
    switch(e){
342
292
    case EBADF:
343
293
    case ENOTTY:
350
300
  sigemptyset(&new_action.sa_mask);
351
301
  ret = sigaddset(&new_action.sa_mask, SIGINT);
352
302
  if(ret == -1){
353
 
    error_plus(0, errno, "sigaddset");
 
303
    error(0, errno, "sigaddset");
354
304
    return EX_OSERR;
355
305
  }
356
306
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
357
307
  if(ret == -1){
358
 
    error_plus(0, errno, "sigaddset");
 
308
    error(0, errno, "sigaddset");
359
309
    return EX_OSERR;
360
310
  }
361
311
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
312
  if(ret == -1){
363
 
    error_plus(0, errno, "sigaddset");
 
313
    error(0, errno, "sigaddset");
364
314
    return EX_OSERR;
365
315
  }
366
316
  /* Need to check if the handler is SIG_IGN before handling:
369
319
  */
370
320
  ret = sigaction(SIGINT, NULL, &old_action);
371
321
  if(ret == -1){
372
 
    error_plus(0, errno, "sigaction");
 
322
    error(0, errno, "sigaction");
373
323
    return EX_OSERR;
374
324
  }
375
325
  if(old_action.sa_handler != SIG_IGN){
376
326
    ret = sigaction(SIGINT, &new_action, NULL);
377
327
    if(ret == -1){
378
 
      error_plus(0, errno, "sigaction");
 
328
      error(0, errno, "sigaction");
379
329
      return EX_OSERR;
380
330
    }
381
331
  }
382
332
  ret = sigaction(SIGHUP, NULL, &old_action);
383
333
  if(ret == -1){
384
 
    error_plus(0, errno, "sigaction");
 
334
    error(0, errno, "sigaction");
385
335
    return EX_OSERR;
386
336
  }
387
337
  if(old_action.sa_handler != SIG_IGN){
388
338
    ret = sigaction(SIGHUP, &new_action, NULL);
389
339
    if(ret == -1){
390
 
      error_plus(0, errno, "sigaction");
 
340
      error(0, errno, "sigaction");
391
341
      return EX_OSERR;
392
342
    }
393
343
  }
394
344
  ret = sigaction(SIGTERM, NULL, &old_action);
395
345
  if(ret == -1){
396
 
    error_plus(0, errno, "sigaction");
 
346
    error(0, errno, "sigaction");
397
347
    return EX_OSERR;
398
348
  }
399
349
  if(old_action.sa_handler != SIG_IGN){
400
350
    ret = sigaction(SIGTERM, &new_action, NULL);
401
351
    if(ret == -1){
402
 
      error_plus(0, errno, "sigaction");
 
352
      error(0, errno, "sigaction");
403
353
      return EX_OSERR;
404
354
    }
405
355
  }
413
363
  t_new.c_lflag &= ~(tcflag_t)ECHO;
414
364
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
415
365
    int e = errno;
416
 
    error_plus(0, errno, "tcsetattr-echo");
 
366
    error(0, errno, "tcsetattr-echo");
417
367
    switch(e){
418
368
    case EBADF:
419
369
    case ENOTTY:
439
389
    if(prefix){
440
390
      fprintf(stderr, "%s ", prefix);
441
391
    }
442
 
    if(prompt != NULL){
443
 
      fprintf(stderr, "%s: ", prompt);
444
 
    } else {
 
392
    {
445
393
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
446
394
      const char *crypttarget = getenv("CRYPTTAB_NAME");
447
395
      /* Before cryptsetup 1.1.0~rc2 */
485
433
        sret = write(STDOUT_FILENO, buffer + written, n - written);
486
434
        if(sret < 0){
487
435
          int e = errno;
488
 
          error_plus(0, errno, "write");
 
436
          error(0, errno, "write");
489
437
          switch(e){
490
438
          case EBADF:
491
439
          case EFAULT:
507
455
      sret = close(STDOUT_FILENO);
508
456
      if(sret == -1){
509
457
        int e = errno;
510
 
        error_plus(0, errno, "close");
 
458
        error(0, errno, "close");
511
459
        switch(e){
512
460
        case EBADF:
513
461
          status = EX_OSFILE;
522
470
    }
523
471
    if(sret < 0){
524
472
      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
 
          }
 
473
      if(errno != EINTR and not feof(stdin)){
 
474
        error(0, errno, "getline");
 
475
        switch(e){
 
476
        case EBADF:
 
477
          status = EX_UNAVAILABLE;
 
478
        case EIO:
 
479
        case EINVAL:
 
480
        default:
 
481
          status = EX_IOERR;
538
482
          break;
539
 
        } else {
540
 
          clearerr(stdin);
541
483
        }
 
484
        break;
542
485
      }
543
486
    }
544
487
    /* if(sret == 0), then the only sensible thing to do is to retry
557
500
    fprintf(stderr, "Restoring terminal attributes\n");
558
501
  }
559
502
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
560
 
    error_plus(0, errno, "tcsetattr+echo");
 
503
    error(0, errno, "tcsetattr+echo");
561
504
  }
562
505
  
563
506
  if(quit_now){
565
508
    old_action.sa_handler = SIG_DFL;
566
509
    ret = sigaction(signal_received, &old_action, NULL);
567
510
    if(ret == -1){
568
 
      error_plus(0, errno, "sigaction");
 
511
      error(0, errno, "sigaction");
569
512
    }
570
513
    raise(signal_received);
571
514
  }