/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: Björn Påhlsson
  • Date: 2010-09-07 18:48:56 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184856-waz6cvxbm7ranha2
added the actually plugin file for plymouth

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