/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/usplash.c

  • Committer: teddy at bsnet
  • Date: 2010-09-09 18:16:14 UTC
  • mfrom: (237.2.35 mandos-empty-device)
  • Revision ID: teddy@fukt.bsnet.se-20100909181614-oanlmvkzsiodbo3c
Merge in branch to interpret an empty device name to mean
"autodetect".

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Usplash - Read a password from usplash and output it
4
4
 * 
5
 
 * Copyright © 2008-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 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
31
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
32
32
#include <iso646.h>             /* and, or, not*/
33
33
#include <errno.h>              /* errno, EINTR */
34
 
#include <error.h>
35
34
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
36
35
                                   dirent */
37
36
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp(), strerror() */
39
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
40
 
                                   fprintf() */
 
37
#include <string.h>             /* strlen(), memcmp() */
 
38
#include <stdio.h>              /* asprintf(), perror() */
41
39
#include <unistd.h>             /* close(), write(), readlink(),
42
40
                                   read(), STDOUT_FILENO, sleep(),
43
41
                                   fork(), setuid(), geteuid(),
44
42
                                   setsid(), chdir(), dup2(),
45
43
                                   STDERR_FILENO, execv() */
46
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
47
 
                                   EXIT_SUCCESS, malloc(), _exit(),
48
 
                                   getenv() */
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
 
46
#include <stdlib.h>             /* getenv() */
49
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
50
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
51
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
52
50
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
53
 
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
51
 
56
52
sig_atomic_t interrupted_by_signal = 0;
57
53
int signal_received;
58
54
const char usplash_name[] = "/sbin/usplash";
59
55
 
60
 
/* Function to use when printing errors */
61
 
void error_plus(int status, int errnum, const char *formatstring,
62
 
                ...){
63
 
  va_list ap;
64
 
  char *text;
65
 
  int ret;
66
 
  
67
 
  va_start(ap, formatstring);
68
 
  ret = vasprintf(&text, formatstring, ap);
69
 
  if (ret == -1){
70
 
    fprintf(stderr, "Mandos plugin %s: ",
71
 
            program_invocation_short_name);
72
 
    vfprintf(stderr, formatstring, ap);
73
 
    fprintf(stderr, ": ");
74
 
    fprintf(stderr, "%s\n", strerror(errnum));
75
 
    error(status, errno, "vasprintf while printing error");
76
 
    return;
77
 
  }
78
 
  fprintf(stderr, "Mandos plugin ");
79
 
  error(status, errnum, "%s", text);
80
 
  free(text);
81
 
}
82
 
 
83
56
static void termination_handler(int signum){
84
57
  if(interrupted_by_signal){
85
58
    return;
180
153
  size_t cmdline_len = 0;
181
154
  DIR *proc_dir = opendir("/proc");
182
155
  if(proc_dir == NULL){
183
 
    error_plus(0, errno, "opendir");
 
156
    perror("opendir");
184
157
    return -1;
185
158
  }
186
159
  errno = 0;
208
181
      char *exe_link;
209
182
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
210
183
      if(ret == -1){
211
 
        error_plus(0, errno, "asprintf");
 
184
        perror("asprintf");
212
185
        goto fail_find_usplash;
213
186
      }
214
187
      
220
193
          free(exe_link);
221
194
          continue;
222
195
        }
223
 
        error_plus(0, errno, "lstat");
 
196
        perror("lstat");
224
197
        free(exe_link);
225
198
        goto fail_find_usplash;
226
199
      }
251
224
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
252
225
                       proc_ent->d_name);
253
226
        if(ret == -1){
254
 
          error_plus(0, errno, "asprintf");
 
227
          perror("asprintf");
255
228
          goto fail_find_usplash;
256
229
        }
257
230
        cl_fd = open(cmdline_filename, O_RDONLY);
258
231
        free(cmdline_filename);
259
232
        if(cl_fd == -1){
260
 
          error_plus(0, errno, "open");
 
233
          perror("open");
261
234
          goto fail_find_usplash;
262
235
        }
263
236
      }
269
242
        if(cmdline_len + blocksize > cmdline_allocated){
270
243
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
271
244
          if(tmp == NULL){
272
 
            error_plus(0, errno, "realloc");
 
245
            perror("realloc");
273
246
            close(cl_fd);
274
247
            goto fail_find_usplash;
275
248
          }
280
253
        sret = read(cl_fd, cmdline + cmdline_len,
281
254
                    cmdline_allocated - cmdline_len);
282
255
        if(sret == -1){
283
 
          error_plus(0, errno, "read");
 
256
          perror("read");
284
257
          close(cl_fd);
285
258
          goto fail_find_usplash;
286
259
        }
288
261
      } while(sret != 0);
289
262
      ret = close(cl_fd);
290
263
      if(ret == -1){
291
 
        error_plus(0, errno, "close");
 
264
        perror("close");
292
265
        goto fail_find_usplash;
293
266
      }
294
267
    }
295
268
    /* Close directory */
296
269
    ret = closedir(proc_dir);
297
270
    if(ret == -1){
298
 
      error_plus(0, errno, "closedir");
 
271
      perror("closedir");
299
272
      goto fail_find_usplash;
300
273
    }
301
274
    /* Success */
350
323
    sigemptyset(&new_action.sa_mask);
351
324
    ret = sigaddset(&new_action.sa_mask, SIGINT);
352
325
    if(ret == -1){
353
 
      error_plus(0, errno, "sigaddset");
 
326
      perror("sigaddset");
354
327
      status = EX_OSERR;
355
328
      goto failure;
356
329
    }
357
330
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
358
331
    if(ret == -1){
359
 
      error_plus(0, errno, "sigaddset");
 
332
      perror("sigaddset");
360
333
      status = EX_OSERR;
361
334
      goto failure;
362
335
    }
363
336
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
364
337
    if(ret == -1){
365
 
      error_plus(0, errno, "sigaddset");
 
338
      perror("sigaddset");
366
339
      status = EX_OSERR;
367
340
      goto failure;
368
341
    }
369
342
    ret = sigaction(SIGINT, NULL, &old_action);
370
343
    if(ret == -1){
371
344
      if(errno != EINTR){
372
 
        error_plus(0, errno, "sigaction");
 
345
        perror("sigaction");
373
346
        status = EX_OSERR;
374
347
      }
375
348
      goto failure;
378
351
      ret = sigaction(SIGINT, &new_action, NULL);
379
352
      if(ret == -1){
380
353
        if(errno != EINTR){
381
 
          error_plus(0, errno, "sigaction");
 
354
          perror("sigaction");
382
355
          status = EX_OSERR;
383
356
        }
384
357
        goto failure;
387
360
    ret = sigaction(SIGHUP, NULL, &old_action);
388
361
    if(ret == -1){
389
362
      if(errno != EINTR){
390
 
        error_plus(0, errno, "sigaction");
 
363
        perror("sigaction");
391
364
        status = EX_OSERR;
392
365
      }
393
366
      goto failure;
396
369
      ret = sigaction(SIGHUP, &new_action, NULL);
397
370
      if(ret == -1){
398
371
        if(errno != EINTR){
399
 
          error_plus(0, errno, "sigaction");
 
372
          perror("sigaction");
400
373
          status = EX_OSERR;
401
374
        }
402
375
        goto failure;
405
378
    ret = sigaction(SIGTERM, NULL, &old_action);
406
379
    if(ret == -1){
407
380
      if(errno != EINTR){
408
 
        error_plus(0, errno, "sigaction");
 
381
        perror("sigaction");
409
382
        status = EX_OSERR;
410
383
      }
411
384
      goto failure;
414
387
      ret = sigaction(SIGTERM, &new_action, NULL);
415
388
      if(ret == -1){
416
389
        if(errno != EINTR){
417
 
          error_plus(0, errno, "sigaction");
 
390
          perror("sigaction");
418
391
          status = EX_OSERR;
419
392
        }
420
393
        goto failure;
426
399
  /* Write command to FIFO */
427
400
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
428
401
    if(errno != EINTR){
429
 
      error_plus(0, errno, "usplash_write");
 
402
      perror("usplash_write");
430
403
      status = EX_OSERR;
431
404
    }
432
405
    goto failure;
438
411
  
439
412
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
440
413
    if(errno != EINTR){
441
 
      error_plus(0, errno, "usplash_write");
 
414
      perror("usplash_write");
442
415
      status = EX_OSERR;
443
416
    }
444
417
    goto failure;
456
429
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
457
430
  if(outfifo_fd == -1){
458
431
    if(errno != EINTR){
459
 
      error_plus(0, errno, "open");
 
432
      perror("open");
460
433
      status = EX_OSERR;
461
434
    }
462
435
    goto failure;
475
448
      char *tmp = realloc(buf, buf_allocated + blocksize);
476
449
      if(tmp == NULL){
477
450
        if(errno != EINTR){
478
 
          error_plus(0, errno, "realloc");
 
451
          perror("realloc");
479
452
          status = EX_OSERR;
480
453
        }
481
454
        goto failure;
487
460
                buf_allocated - buf_len);
488
461
    if(sret == -1){
489
462
      if(errno != EINTR){
490
 
        error_plus(0, errno, "read");
 
463
        perror("read");
491
464
        status = EX_OSERR;
492
465
      }
493
466
      TEMP_FAILURE_RETRY(close(outfifo_fd));
502
475
  ret = close(outfifo_fd);
503
476
  if(ret == -1){
504
477
    if(errno != EINTR){
505
 
      error_plus(0, errno, "close");
 
478
      perror("close");
506
479
      status = EX_OSERR;
507
480
    }
508
481
    goto failure;
515
488
  
516
489
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
517
490
    if(errno != EINTR){
518
 
      error_plus(0, errno, "usplash_write");
 
491
      perror("usplash_write");
519
492
      status = EX_OSERR;
520
493
    }
521
494
    goto failure;
528
501
  ret = close(fifo_fd);
529
502
  if(ret == -1){
530
503
    if(errno != EINTR){
531
 
      error_plus(0, errno, "close");
 
504
      perror("close");
532
505
      status = EX_OSERR;
533
506
    }
534
507
    goto failure;
542
515
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
543
516
      if(sret == -1){
544
517
        if(errno != EINTR){
545
 
          error_plus(0, errno, "write");
 
518
          perror("write");
546
519
          status = EX_OSERR;
547
520
        }
548
521
        goto failure;
579
552
  if(fifo_fd != -1){
580
553
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
581
554
    if(ret == -1 and errno != EINTR){
582
 
      error_plus(0, errno, "close");
 
555
      perror("close");
583
556
    }
584
557
    fifo_fd = -1;
585
558
  }
588
561
  if(outfifo_fd != -1){
589
562
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
590
563
    if(ret == -1){
591
 
      error_plus(0, errno, "close");
592
 
    }
593
 
  }
594
 
  
595
 
  /* Create argv for new usplash*/
596
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
597
 
                               * sizeof(char *)); /* Count args */
598
 
  if(cmdline_argv == NULL){
599
 
    error_plus(0, errno, "malloc");
600
 
    return status;
601
 
  }
602
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
603
 
  
 
564
      perror("close");
 
565
    }
 
566
  }
 
567
  
 
568
  /* Create argc and argv for new usplash*/
 
569
  int cmdline_argc = 0;
 
570
  char **cmdline_argv = malloc(sizeof(char *));
 
571
  {
 
572
    size_t position = 0;
 
573
    while(position < cmdline_len){
 
574
      char **tmp = realloc(cmdline_argv,
 
575
                           (sizeof(char *)
 
576
                            * (size_t)(cmdline_argc + 2)));
 
577
      if(tmp == NULL){
 
578
        perror("realloc");
 
579
        free(cmdline_argv);
 
580
        return status;
 
581
      }
 
582
      cmdline_argv = tmp;
 
583
      cmdline_argv[cmdline_argc] = cmdline + position;
 
584
      cmdline_argc++;
 
585
      position += strlen(cmdline + position) + 1;
 
586
    }
 
587
    cmdline_argv[cmdline_argc] = NULL;
 
588
  }
604
589
  /* Kill old usplash */
605
590
  kill(usplash_pid, SIGTERM);
606
591
  sleep(2);
617
602
       the real user ID (_mandos) */
618
603
    ret = setuid(geteuid());
619
604
    if(ret == -1){
620
 
      error_plus(0, errno, "setuid");
 
605
      perror("setuid");
621
606
    }
622
607
    
623
608
    setsid();
624
609
    ret = chdir("/");
625
 
    if(ret == -1){
626
 
      error_plus(0, errno, "chdir");
627
 
      _exit(EX_OSERR);
628
 
    }
629
610
/*     if(fork() != 0){ */
630
611
/*       _exit(EXIT_SUCCESS); */
631
612
/*     } */
632
613
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
633
614
    if(ret == -1){
634
 
      error_plus(0, errno, "dup2");
 
615
      perror("dup2");
635
616
      _exit(EX_OSERR);
636
617
    }
637
618
    
638
619
    execv(usplash_name, cmdline_argv);
639
620
    if(not interrupted_by_signal){
640
 
      error_plus(0, errno, "execv");
 
621
      perror("execv");
641
622
    }
642
623
    free(cmdline);
643
624
    free(cmdline_argv);
648
629
  sleep(2);
649
630
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
650
631
    if(errno != EINTR){
651
 
      error_plus(0, errno, "usplash_write");
 
632
      perror("usplash_write");
652
633
    }
653
634
  }
654
635
  
656
637
  if(fifo_fd != -1){
657
638
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
658
639
    if(ret == -1 and errno != EINTR){
659
 
      error_plus(0, errno, "close");
 
640
      perror("close");
660
641
    }
661
642
    fifo_fd = -1;
662
643
  }
667
648
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
668
649
                                            &signal_action, NULL));
669
650
    if(ret == -1){
670
 
      error_plus(0, errno, "sigaction");
 
651
      perror("sigaction");
671
652
    }
672
653
    do {
673
654
      ret = raise(signal_received);
674
655
    } while(ret != 0 and errno == EINTR);
675
656
    if(ret != 0){
676
 
      error_plus(0, errno, "raise");
 
657
      perror("raise");
677
658
      abort();
678
659
    }
679
660
    TEMP_FAILURE_RETRY(pause());