/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: 2017-08-20 14:41:20 UTC
  • Revision ID: teddy@recompile.se-20170820144120-ee0hsyhvo1geg8ms
Handle multiple lines better in cryptroot file.

* initramfs-tools-script: Avoid running plugin-runner more than once
  if the root file system device is specially marked in the cryptroot
  file.  Also never run plugin-runner for a resume (usually swap)
  device.

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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 Björn Påhlsson
 
5
 * Copyright © 2008-2017 Teddy Hogeborn
 
6
 * Copyright © 2008-2017 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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
67
67
int signal_received;
68
68
bool debug = false;
69
69
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
71
71
 
72
72
/* Needed for conflict resolution */
73
73
const char plymouth_name[] = "plymouthd";
74
74
 
75
75
/* Function to use when printing errors */
 
76
__attribute__((format (gnu_printf, 3, 4)))
76
77
void error_plus(int status, int errnum, const char *formatstring,
77
78
                ...){
78
79
  va_list ap;
81
82
  
82
83
  va_start(ap, formatstring);
83
84
  ret = vasprintf(&text, formatstring, ap);
84
 
  if (ret == -1){
 
85
  if(ret == -1){
85
86
    fprintf(stderr, "Mandos plugin %s: ",
86
87
            program_invocation_short_name);
87
88
    vfprintf(stderr, formatstring, ap);
88
 
    fprintf(stderr, ": ");
89
 
    fprintf(stderr, "%s\n", strerror(errnum));
 
89
    fprintf(stderr, ": %s\n", strerror(errnum));
90
90
    error(status, errno, "vasprintf while printing error");
91
91
    return;
92
92
  }
109
109
     from the terminal.  Password-prompt will exit if it detects
110
110
     plymouth since plymouth performs the same functionality.
111
111
   */
 
112
  __attribute__((nonnull))
112
113
  int is_plymouth(const struct dirent *proc_entry){
113
114
    int ret;
114
115
    int cl_fd;
115
116
    {
116
 
      uintmax_t maxvalue;
 
117
      uintmax_t proc_id;
117
118
      char *tmp;
118
119
      errno = 0;
119
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
120
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
120
121
      
121
122
      if(errno != 0 or *tmp != '\0'
122
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
123
         or proc_id != (uintmax_t)((pid_t)proc_id)){
123
124
        return 0;
124
125
      }
125
126
    }
128
129
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
129
130
                   proc_entry->d_name);
130
131
    if(ret == -1){
131
 
      error(0, errno, "asprintf");
 
132
      error_plus(0, errno, "asprintf");
132
133
      return 0;
133
134
    }
134
135
    
137
138
    free(cmdline_filename);
138
139
    if(cl_fd == -1){
139
140
      if(errno != ENOENT){
140
 
        error(0, errno, "open");
 
141
        error_plus(0, errno, "open");
141
142
      }
142
143
      return 0;
143
144
    }
154
155
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
155
156
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
156
157
          if(tmp == NULL){
157
 
            error(0, errno, "realloc");
 
158
            error_plus(0, errno, "realloc");
158
159
            free(cmdline);
159
160
            close(cl_fd);
160
161
            return 0;
167
168
        sret = read(cl_fd, cmdline + cmdline_len,
168
169
                    cmdline_allocated - cmdline_len);
169
170
        if(sret == -1){
170
 
          error(0, errno, "read");
 
171
          error_plus(0, errno, "read");
171
172
          free(cmdline);
172
173
          close(cl_fd);
173
174
          return 0;
176
177
      } while(sret != 0);
177
178
      ret = close(cl_fd);
178
179
      if(ret == -1){
179
 
        error(0, errno, "close");
 
180
        error_plus(0, errno, "close");
180
181
        free(cmdline);
181
182
        return 0;
182
183
      }
208
209
    return 1;
209
210
  }
210
211
  
211
 
  struct dirent **direntries;
 
212
  struct dirent **direntries = NULL;
212
213
  int ret;
213
214
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
214
 
  if (ret == -1){
215
 
    error(1, errno, "scandir");
216
 
  }
 
215
  if(ret == -1){
 
216
    error_plus(1, errno, "scandir");
 
217
  }
 
218
  {
 
219
    int i = ret;
 
220
    while(i--){
 
221
      free(direntries[i]);
 
222
    }
 
223
  }
 
224
  free(direntries);
217
225
  return ret > 0;
218
226
}
219
227
 
248
256
      { .name = NULL }
249
257
    };
250
258
    
 
259
    __attribute__((nonnull(3)))
251
260
    error_t parse_opt (int key, char *arg, struct argp_state *state){
252
261
      errno = 0;
253
262
      switch (key){
289
298
    case ENOMEM:
290
299
    default:
291
300
      errno = ret;
292
 
      error(0, errno, "argp_parse");
 
301
      error_plus(0, errno, "argp_parse");
293
302
      return EX_OSERR;
294
303
    case EINVAL:
295
304
      return EX_USAGE;
300
309
    fprintf(stderr, "Starting %s\n", argv[0]);
301
310
  }
302
311
 
303
 
  if (conflict_detection()){
 
312
  if(conflict_detection()){
304
313
    if(debug){
305
314
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
306
315
    }
313
322
  
314
323
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
315
324
    int e = errno;
316
 
    error(0, errno, "tcgetattr");
 
325
    error_plus(0, errno, "tcgetattr");
317
326
    switch(e){
318
327
    case EBADF:
319
328
    case ENOTTY:
326
335
  sigemptyset(&new_action.sa_mask);
327
336
  ret = sigaddset(&new_action.sa_mask, SIGINT);
328
337
  if(ret == -1){
329
 
    error(0, errno, "sigaddset");
 
338
    error_plus(0, errno, "sigaddset");
330
339
    return EX_OSERR;
331
340
  }
332
341
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
342
  if(ret == -1){
334
 
    error(0, errno, "sigaddset");
 
343
    error_plus(0, errno, "sigaddset");
335
344
    return EX_OSERR;
336
345
  }
337
346
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
338
347
  if(ret == -1){
339
 
    error(0, errno, "sigaddset");
 
348
    error_plus(0, errno, "sigaddset");
340
349
    return EX_OSERR;
341
350
  }
342
351
  /* Need to check if the handler is SIG_IGN before handling:
345
354
  */
346
355
  ret = sigaction(SIGINT, NULL, &old_action);
347
356
  if(ret == -1){
348
 
    error(0, errno, "sigaction");
 
357
    error_plus(0, errno, "sigaction");
349
358
    return EX_OSERR;
350
359
  }
351
360
  if(old_action.sa_handler != SIG_IGN){
352
361
    ret = sigaction(SIGINT, &new_action, NULL);
353
362
    if(ret == -1){
354
 
      error(0, errno, "sigaction");
 
363
      error_plus(0, errno, "sigaction");
355
364
      return EX_OSERR;
356
365
    }
357
366
  }
358
367
  ret = sigaction(SIGHUP, NULL, &old_action);
359
368
  if(ret == -1){
360
 
    error(0, errno, "sigaction");
 
369
    error_plus(0, errno, "sigaction");
361
370
    return EX_OSERR;
362
371
  }
363
372
  if(old_action.sa_handler != SIG_IGN){
364
373
    ret = sigaction(SIGHUP, &new_action, NULL);
365
374
    if(ret == -1){
366
 
      error(0, errno, "sigaction");
 
375
      error_plus(0, errno, "sigaction");
367
376
      return EX_OSERR;
368
377
    }
369
378
  }
370
379
  ret = sigaction(SIGTERM, NULL, &old_action);
371
380
  if(ret == -1){
372
 
    error(0, errno, "sigaction");
 
381
    error_plus(0, errno, "sigaction");
373
382
    return EX_OSERR;
374
383
  }
375
384
  if(old_action.sa_handler != SIG_IGN){
376
385
    ret = sigaction(SIGTERM, &new_action, NULL);
377
386
    if(ret == -1){
378
 
      error(0, errno, "sigaction");
 
387
      error_plus(0, errno, "sigaction");
379
388
      return EX_OSERR;
380
389
    }
381
390
  }
389
398
  t_new.c_lflag &= ~(tcflag_t)ECHO;
390
399
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
391
400
    int e = errno;
392
 
    error(0, errno, "tcsetattr-echo");
 
401
    error_plus(0, errno, "tcsetattr-echo");
393
402
    switch(e){
394
403
    case EBADF:
395
404
    case ENOTTY:
459
468
        sret = write(STDOUT_FILENO, buffer + written, n - written);
460
469
        if(sret < 0){
461
470
          int e = errno;
462
 
          error(0, errno, "write");
 
471
          error_plus(0, errno, "write");
463
472
          switch(e){
464
473
          case EBADF:
465
474
          case EFAULT:
481
490
      sret = close(STDOUT_FILENO);
482
491
      if(sret == -1){
483
492
        int e = errno;
484
 
        error(0, errno, "close");
 
493
        error_plus(0, errno, "close");
485
494
        switch(e){
486
495
        case EBADF:
487
496
          status = EX_OSFILE;
497
506
    if(sret < 0){
498
507
      int e = errno;
499
508
      if(errno != EINTR and not feof(stdin)){
500
 
        error(0, errno, "getline");
 
509
        error_plus(0, errno, "getline");
501
510
        switch(e){
502
511
        case EBADF:
503
512
          status = EX_UNAVAILABLE;
 
513
          break;
504
514
        case EIO:
505
515
        case EINVAL:
506
516
        default:
526
536
    fprintf(stderr, "Restoring terminal attributes\n");
527
537
  }
528
538
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
529
 
    error(0, errno, "tcsetattr+echo");
 
539
    error_plus(0, errno, "tcsetattr+echo");
530
540
  }
531
541
  
532
542
  if(quit_now){
534
544
    old_action.sa_handler = SIG_DFL;
535
545
    ret = sigaction(signal_received, &old_action, NULL);
536
546
    if(ret == -1){
537
 
      error(0, errno, "sigaction");
 
547
      error_plus(0, errno, "sigaction");
538
548
    }
539
549
    raise(signal_received);
540
550
  }