/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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 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
37
#include <string.h>             /* strlen(), memcmp() */
39
 
#include <stdio.h>              /* asprintf()*/
 
38
#include <stdio.h>              /* asprintf(), perror() */
40
39
#include <unistd.h>             /* close(), write(), readlink(),
41
40
                                   read(), STDOUT_FILENO, sleep(),
42
41
                                   fork(), setuid(), geteuid(),
43
42
                                   setsid(), chdir(), dup2(),
44
43
                                   STDERR_FILENO, execv() */
45
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
46
 
                                   EXIT_SUCCESS, malloc(), _exit(),
47
 
                                   getenv() */
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
 
46
#include <stdlib.h>             /* getenv() */
48
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
49
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
50
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
51
50
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
52
 
#include <argz.h>               /* argz_count(), argz_extract() */
53
51
 
54
52
sig_atomic_t interrupted_by_signal = 0;
55
53
int signal_received;
155
153
  size_t cmdline_len = 0;
156
154
  DIR *proc_dir = opendir("/proc");
157
155
  if(proc_dir == NULL){
158
 
    error(0, errno, "opendir");
 
156
    perror("opendir");
159
157
    return -1;
160
158
  }
161
159
  errno = 0;
183
181
      char *exe_link;
184
182
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
183
      if(ret == -1){
186
 
        error(0, errno, "asprintf");
 
184
        perror("asprintf");
187
185
        goto fail_find_usplash;
188
186
      }
189
187
      
195
193
          free(exe_link);
196
194
          continue;
197
195
        }
198
 
        error(0, errno, "lstat");
 
196
        perror("lstat");
199
197
        free(exe_link);
200
198
        goto fail_find_usplash;
201
199
      }
226
224
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
225
                       proc_ent->d_name);
228
226
        if(ret == -1){
229
 
          error(0, errno, "asprintf");
 
227
          perror("asprintf");
230
228
          goto fail_find_usplash;
231
229
        }
232
230
        cl_fd = open(cmdline_filename, O_RDONLY);
233
231
        free(cmdline_filename);
234
232
        if(cl_fd == -1){
235
 
          error(0, errno, "open");
 
233
          perror("open");
236
234
          goto fail_find_usplash;
237
235
        }
238
236
      }
244
242
        if(cmdline_len + blocksize > cmdline_allocated){
245
243
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
244
          if(tmp == NULL){
247
 
            error(0, errno, "realloc");
 
245
            perror("realloc");
248
246
            close(cl_fd);
249
247
            goto fail_find_usplash;
250
248
          }
255
253
        sret = read(cl_fd, cmdline + cmdline_len,
256
254
                    cmdline_allocated - cmdline_len);
257
255
        if(sret == -1){
258
 
          error(0, errno, "read");
 
256
          perror("read");
259
257
          close(cl_fd);
260
258
          goto fail_find_usplash;
261
259
        }
263
261
      } while(sret != 0);
264
262
      ret = close(cl_fd);
265
263
      if(ret == -1){
266
 
        error(0, errno, "close");
 
264
        perror("close");
267
265
        goto fail_find_usplash;
268
266
      }
269
267
    }
270
268
    /* Close directory */
271
269
    ret = closedir(proc_dir);
272
270
    if(ret == -1){
273
 
      error(0, errno, "closedir");
 
271
      perror("closedir");
274
272
      goto fail_find_usplash;
275
273
    }
276
274
    /* Success */
325
323
    sigemptyset(&new_action.sa_mask);
326
324
    ret = sigaddset(&new_action.sa_mask, SIGINT);
327
325
    if(ret == -1){
328
 
      error(0, errno, "sigaddset");
 
326
      perror("sigaddset");
329
327
      status = EX_OSERR;
330
328
      goto failure;
331
329
    }
332
330
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
331
    if(ret == -1){
334
 
      error(0, errno, "sigaddset");
 
332
      perror("sigaddset");
335
333
      status = EX_OSERR;
336
334
      goto failure;
337
335
    }
338
336
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
337
    if(ret == -1){
340
 
      error(0, errno, "sigaddset");
 
338
      perror("sigaddset");
341
339
      status = EX_OSERR;
342
340
      goto failure;
343
341
    }
344
342
    ret = sigaction(SIGINT, NULL, &old_action);
345
343
    if(ret == -1){
346
344
      if(errno != EINTR){
347
 
        error(0, errno, "sigaction");
 
345
        perror("sigaction");
348
346
        status = EX_OSERR;
349
347
      }
350
348
      goto failure;
353
351
      ret = sigaction(SIGINT, &new_action, NULL);
354
352
      if(ret == -1){
355
353
        if(errno != EINTR){
356
 
          error(0, errno, "sigaction");
 
354
          perror("sigaction");
357
355
          status = EX_OSERR;
358
356
        }
359
357
        goto failure;
362
360
    ret = sigaction(SIGHUP, NULL, &old_action);
363
361
    if(ret == -1){
364
362
      if(errno != EINTR){
365
 
        error(0, errno, "sigaction");
 
363
        perror("sigaction");
366
364
        status = EX_OSERR;
367
365
      }
368
366
      goto failure;
371
369
      ret = sigaction(SIGHUP, &new_action, NULL);
372
370
      if(ret == -1){
373
371
        if(errno != EINTR){
374
 
          error(0, errno, "sigaction");
 
372
          perror("sigaction");
375
373
          status = EX_OSERR;
376
374
        }
377
375
        goto failure;
380
378
    ret = sigaction(SIGTERM, NULL, &old_action);
381
379
    if(ret == -1){
382
380
      if(errno != EINTR){
383
 
        error(0, errno, "sigaction");
 
381
        perror("sigaction");
384
382
        status = EX_OSERR;
385
383
      }
386
384
      goto failure;
389
387
      ret = sigaction(SIGTERM, &new_action, NULL);
390
388
      if(ret == -1){
391
389
        if(errno != EINTR){
392
 
          error(0, errno, "sigaction");
 
390
          perror("sigaction");
393
391
          status = EX_OSERR;
394
392
        }
395
393
        goto failure;
401
399
  /* Write command to FIFO */
402
400
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
401
    if(errno != EINTR){
404
 
      error(0, errno, "usplash_write");
 
402
      perror("usplash_write");
405
403
      status = EX_OSERR;
406
404
    }
407
405
    goto failure;
413
411
  
414
412
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
413
    if(errno != EINTR){
416
 
      error(0, errno, "usplash_write");
 
414
      perror("usplash_write");
417
415
      status = EX_OSERR;
418
416
    }
419
417
    goto failure;
431
429
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
432
430
  if(outfifo_fd == -1){
433
431
    if(errno != EINTR){
434
 
      error(0, errno, "open");
 
432
      perror("open");
435
433
      status = EX_OSERR;
436
434
    }
437
435
    goto failure;
450
448
      char *tmp = realloc(buf, buf_allocated + blocksize);
451
449
      if(tmp == NULL){
452
450
        if(errno != EINTR){
453
 
          error(0, errno, "realloc");
 
451
          perror("realloc");
454
452
          status = EX_OSERR;
455
453
        }
456
454
        goto failure;
462
460
                buf_allocated - buf_len);
463
461
    if(sret == -1){
464
462
      if(errno != EINTR){
465
 
        error(0, errno, "read");
 
463
        perror("read");
466
464
        status = EX_OSERR;
467
465
      }
468
466
      TEMP_FAILURE_RETRY(close(outfifo_fd));
477
475
  ret = close(outfifo_fd);
478
476
  if(ret == -1){
479
477
    if(errno != EINTR){
480
 
      error(0, errno, "close");
 
478
      perror("close");
481
479
      status = EX_OSERR;
482
480
    }
483
481
    goto failure;
490
488
  
491
489
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
490
    if(errno != EINTR){
493
 
      error(0, errno, "usplash_write");
 
491
      perror("usplash_write");
494
492
      status = EX_OSERR;
495
493
    }
496
494
    goto failure;
503
501
  ret = close(fifo_fd);
504
502
  if(ret == -1){
505
503
    if(errno != EINTR){
506
 
      error(0, errno, "close");
 
504
      perror("close");
507
505
      status = EX_OSERR;
508
506
    }
509
507
    goto failure;
517
515
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
516
      if(sret == -1){
519
517
        if(errno != EINTR){
520
 
          error(0, errno, "write");
 
518
          perror("write");
521
519
          status = EX_OSERR;
522
520
        }
523
521
        goto failure;
554
552
  if(fifo_fd != -1){
555
553
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
556
554
    if(ret == -1 and errno != EINTR){
557
 
      error(0, errno, "close");
 
555
      perror("close");
558
556
    }
559
557
    fifo_fd = -1;
560
558
  }
563
561
  if(outfifo_fd != -1){
564
562
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
565
563
    if(ret == -1){
566
 
      error(0, errno, "close");
567
 
    }
568
 
  }
569
 
  
570
 
  /* Create argv for new usplash*/
571
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
 
                               * sizeof(char *)); /* Count args */
573
 
  if(cmdline_argv == NULL){
574
 
    error(0, errno, "malloc");
575
 
    return status;
576
 
  }
577
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
578
 
  
 
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
  }
579
589
  /* Kill old usplash */
580
590
  kill(usplash_pid, SIGTERM);
581
591
  sleep(2);
592
602
       the real user ID (_mandos) */
593
603
    ret = setuid(geteuid());
594
604
    if(ret == -1){
595
 
      error(0, errno, "setuid");
 
605
      perror("setuid");
596
606
    }
597
607
    
598
608
    setsid();
599
609
    ret = chdir("/");
600
 
    if(ret == -1){
601
 
      error(0, errno, "chdir");
602
 
      _exit(EX_OSERR);
603
 
    }
604
610
/*     if(fork() != 0){ */
605
611
/*       _exit(EXIT_SUCCESS); */
606
612
/*     } */
607
613
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
608
614
    if(ret == -1){
609
 
      error(0, errno, "dup2");
 
615
      perror("dup2");
610
616
      _exit(EX_OSERR);
611
617
    }
612
618
    
613
619
    execv(usplash_name, cmdline_argv);
614
620
    if(not interrupted_by_signal){
615
 
      error(0, errno, "execv");
 
621
      perror("execv");
616
622
    }
617
623
    free(cmdline);
618
624
    free(cmdline_argv);
623
629
  sleep(2);
624
630
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
631
    if(errno != EINTR){
626
 
      error(0, errno, "usplash_write");
 
632
      perror("usplash_write");
627
633
    }
628
634
  }
629
635
  
631
637
  if(fifo_fd != -1){
632
638
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
633
639
    if(ret == -1 and errno != EINTR){
634
 
      error(0, errno, "close");
 
640
      perror("close");
635
641
    }
636
642
    fifo_fd = -1;
637
643
  }
642
648
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
649
                                            &signal_action, NULL));
644
650
    if(ret == -1){
645
 
      error(0, errno, "sigaction");
 
651
      perror("sigaction");
646
652
    }
647
653
    do {
648
654
      ret = raise(signal_received);
649
655
    } while(ret != 0 and errno == EINTR);
650
656
    if(ret != 0){
651
 
      error(0, errno, "raise");
 
657
      perror("raise");
652
658
      abort();
653
659
    }
654
660
    TEMP_FAILURE_RETRY(pause());