/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: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 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>
34
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
35
36
                                   dirent */
36
37
#include <stddef.h>             /* NULL */
37
 
#include <string.h>             /* strlen(), memcmp() */
38
 
#include <stdio.h>              /* asprintf(), perror() */
 
38
#include <string.h>             /* strlen(), memcmp(), strerror() */
 
39
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
 
40
                                   fprintf() */
39
41
#include <unistd.h>             /* close(), write(), readlink(),
40
42
                                   read(), STDOUT_FILENO, sleep(),
41
43
                                   fork(), setuid(), geteuid(),
42
44
                                   setsid(), chdir(), dup2(),
43
45
                                   STDERR_FILENO, execv() */
44
46
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit() */
46
 
#include <stdlib.h>             /* getenv() */
 
47
                                   EXIT_SUCCESS, malloc(), _exit(),
 
48
                                   getenv() */
47
49
#include <dirent.h>             /* opendir(), readdir(), closedir() */
48
50
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
51
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
52
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
53
#include <argz.h>               /* argz_count(), argz_extract() */
 
54
#include <stdarg.h>             /* va_list, va_start(), ... */
50
55
 
51
56
sig_atomic_t interrupted_by_signal = 0;
52
57
int signal_received;
53
58
const char usplash_name[] = "/sbin/usplash";
54
59
 
 
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
 
55
83
static void termination_handler(int signum){
56
84
  if(interrupted_by_signal){
57
85
    return;
152
180
  size_t cmdline_len = 0;
153
181
  DIR *proc_dir = opendir("/proc");
154
182
  if(proc_dir == NULL){
155
 
    perror("opendir");
 
183
    error_plus(0, errno, "opendir");
156
184
    return -1;
157
185
  }
158
186
  errno = 0;
180
208
      char *exe_link;
181
209
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
182
210
      if(ret == -1){
183
 
        perror("asprintf");
 
211
        error_plus(0, errno, "asprintf");
184
212
        goto fail_find_usplash;
185
213
      }
186
214
      
192
220
          free(exe_link);
193
221
          continue;
194
222
        }
195
 
        perror("lstat");
 
223
        error_plus(0, errno, "lstat");
196
224
        free(exe_link);
197
225
        goto fail_find_usplash;
198
226
      }
223
251
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
224
252
                       proc_ent->d_name);
225
253
        if(ret == -1){
226
 
          perror("asprintf");
 
254
          error_plus(0, errno, "asprintf");
227
255
          goto fail_find_usplash;
228
256
        }
229
257
        cl_fd = open(cmdline_filename, O_RDONLY);
230
258
        free(cmdline_filename);
231
259
        if(cl_fd == -1){
232
 
          perror("open");
 
260
          error_plus(0, errno, "open");
233
261
          goto fail_find_usplash;
234
262
        }
235
263
      }
241
269
        if(cmdline_len + blocksize > cmdline_allocated){
242
270
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
243
271
          if(tmp == NULL){
244
 
            perror("realloc");
 
272
            error_plus(0, errno, "realloc");
245
273
            close(cl_fd);
246
274
            goto fail_find_usplash;
247
275
          }
252
280
        sret = read(cl_fd, cmdline + cmdline_len,
253
281
                    cmdline_allocated - cmdline_len);
254
282
        if(sret == -1){
255
 
          perror("read");
 
283
          error_plus(0, errno, "read");
256
284
          close(cl_fd);
257
285
          goto fail_find_usplash;
258
286
        }
260
288
      } while(sret != 0);
261
289
      ret = close(cl_fd);
262
290
      if(ret == -1){
263
 
        perror("close");
 
291
        error_plus(0, errno, "close");
264
292
        goto fail_find_usplash;
265
293
      }
266
294
    }
267
295
    /* Close directory */
268
296
    ret = closedir(proc_dir);
269
297
    if(ret == -1){
270
 
      perror("closedir");
 
298
      error_plus(0, errno, "closedir");
271
299
      goto fail_find_usplash;
272
300
    }
273
301
    /* Success */
297
325
  size_t buf_len = 0;
298
326
  pid_t usplash_pid = -1;
299
327
  bool usplash_accessed = false;
 
328
  int status = EXIT_FAILURE;    /* Default failure exit status */
300
329
  
301
330
  char *prompt = makeprompt();
302
331
  if(prompt == NULL){
 
332
    status = EX_OSERR;
303
333
    goto failure;
304
334
  }
305
335
  
308
338
  size_t cmdline_len = 0;
309
339
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
310
340
  if(usplash_pid == 0){
 
341
    status = EX_UNAVAILABLE;
311
342
    goto failure;
312
343
  }
313
344
  
319
350
    sigemptyset(&new_action.sa_mask);
320
351
    ret = sigaddset(&new_action.sa_mask, SIGINT);
321
352
    if(ret == -1){
322
 
      perror("sigaddset");
 
353
      error_plus(0, errno, "sigaddset");
 
354
      status = EX_OSERR;
323
355
      goto failure;
324
356
    }
325
357
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
326
358
    if(ret == -1){
327
 
      perror("sigaddset");
 
359
      error_plus(0, errno, "sigaddset");
 
360
      status = EX_OSERR;
328
361
      goto failure;
329
362
    }
330
363
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
331
364
    if(ret == -1){
332
 
      perror("sigaddset");
 
365
      error_plus(0, errno, "sigaddset");
 
366
      status = EX_OSERR;
333
367
      goto failure;
334
368
    }
335
369
    ret = sigaction(SIGINT, NULL, &old_action);
336
370
    if(ret == -1){
337
371
      if(errno != EINTR){
338
 
        perror("sigaction");
 
372
        error_plus(0, errno, "sigaction");
 
373
        status = EX_OSERR;
339
374
      }
340
375
      goto failure;
341
376
    }
343
378
      ret = sigaction(SIGINT, &new_action, NULL);
344
379
      if(ret == -1){
345
380
        if(errno != EINTR){
346
 
          perror("sigaction");
 
381
          error_plus(0, errno, "sigaction");
 
382
          status = EX_OSERR;
347
383
        }
348
384
        goto failure;
349
385
      }
351
387
    ret = sigaction(SIGHUP, NULL, &old_action);
352
388
    if(ret == -1){
353
389
      if(errno != EINTR){
354
 
        perror("sigaction");
 
390
        error_plus(0, errno, "sigaction");
 
391
        status = EX_OSERR;
355
392
      }
356
393
      goto failure;
357
394
    }
359
396
      ret = sigaction(SIGHUP, &new_action, NULL);
360
397
      if(ret == -1){
361
398
        if(errno != EINTR){
362
 
          perror("sigaction");
 
399
          error_plus(0, errno, "sigaction");
 
400
          status = EX_OSERR;
363
401
        }
364
402
        goto failure;
365
403
      }
367
405
    ret = sigaction(SIGTERM, NULL, &old_action);
368
406
    if(ret == -1){
369
407
      if(errno != EINTR){
370
 
        perror("sigaction");
 
408
        error_plus(0, errno, "sigaction");
 
409
        status = EX_OSERR;
371
410
      }
372
411
      goto failure;
373
412
    }
375
414
      ret = sigaction(SIGTERM, &new_action, NULL);
376
415
      if(ret == -1){
377
416
        if(errno != EINTR){
378
 
          perror("sigaction");
 
417
          error_plus(0, errno, "sigaction");
 
418
          status = EX_OSERR;
379
419
        }
380
420
        goto failure;
381
421
      }
386
426
  /* Write command to FIFO */
387
427
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
388
428
    if(errno != EINTR){
389
 
      perror("usplash_write");
 
429
      error_plus(0, errno, "usplash_write");
 
430
      status = EX_OSERR;
390
431
    }
391
432
    goto failure;
392
433
  }
397
438
  
398
439
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
399
440
    if(errno != EINTR){
400
 
      perror("usplash_write");
 
441
      error_plus(0, errno, "usplash_write");
 
442
      status = EX_OSERR;
401
443
    }
402
444
    goto failure;
403
445
  }
414
456
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
415
457
  if(outfifo_fd == -1){
416
458
    if(errno != EINTR){
417
 
      perror("open");
 
459
      error_plus(0, errno, "open");
 
460
      status = EX_OSERR;
418
461
    }
419
462
    goto failure;
420
463
  }
432
475
      char *tmp = realloc(buf, buf_allocated + blocksize);
433
476
      if(tmp == NULL){
434
477
        if(errno != EINTR){
435
 
          perror("realloc");
 
478
          error_plus(0, errno, "realloc");
 
479
          status = EX_OSERR;
436
480
        }
437
481
        goto failure;
438
482
      }
443
487
                buf_allocated - buf_len);
444
488
    if(sret == -1){
445
489
      if(errno != EINTR){
446
 
        perror("read");
 
490
        error_plus(0, errno, "read");
 
491
        status = EX_OSERR;
447
492
      }
448
493
      TEMP_FAILURE_RETRY(close(outfifo_fd));
449
494
      goto failure;
457
502
  ret = close(outfifo_fd);
458
503
  if(ret == -1){
459
504
    if(errno != EINTR){
460
 
      perror("close");
 
505
      error_plus(0, errno, "close");
 
506
      status = EX_OSERR;
461
507
    }
462
508
    goto failure;
463
509
  }
469
515
  
470
516
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
471
517
    if(errno != EINTR){
472
 
      perror("usplash_write");
 
518
      error_plus(0, errno, "usplash_write");
 
519
      status = EX_OSERR;
473
520
    }
474
521
    goto failure;
475
522
  }
481
528
  ret = close(fifo_fd);
482
529
  if(ret == -1){
483
530
    if(errno != EINTR){
484
 
      perror("close");
 
531
      error_plus(0, errno, "close");
 
532
      status = EX_OSERR;
485
533
    }
486
534
    goto failure;
487
535
  }
494
542
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
495
543
      if(sret == -1){
496
544
        if(errno != EINTR){
497
 
          perror("write");
 
545
          error_plus(0, errno, "write");
 
546
          status = EX_OSERR;
498
547
        }
499
548
        goto failure;
500
549
      }
523
572
  
524
573
  /* If usplash was never accessed, we can stop now */
525
574
  if(not usplash_accessed){
526
 
    return EXIT_FAILURE;
 
575
    return status;
527
576
  }
528
577
  
529
578
  /* Close FIFO */
530
579
  if(fifo_fd != -1){
531
580
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
532
581
    if(ret == -1 and errno != EINTR){
533
 
      perror("close");
 
582
      error_plus(0, errno, "close");
534
583
    }
535
584
    fifo_fd = -1;
536
585
  }
539
588
  if(outfifo_fd != -1){
540
589
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
541
590
    if(ret == -1){
542
 
      perror("close");
543
 
    }
544
 
  }
545
 
  
546
 
  /* Create argc and argv for new usplash*/
547
 
  int cmdline_argc = 0;
548
 
  char **cmdline_argv = malloc(sizeof(char *));
549
 
  {
550
 
    size_t position = 0;
551
 
    while(position < cmdline_len){
552
 
      char **tmp = realloc(cmdline_argv,
553
 
                           (sizeof(char *)
554
 
                            * (size_t)(cmdline_argc + 2)));
555
 
      if(tmp == NULL){
556
 
        perror("realloc");
557
 
        free(cmdline_argv);
558
 
        return EXIT_FAILURE;
559
 
      }
560
 
      cmdline_argv = tmp;
561
 
      cmdline_argv[cmdline_argc] = cmdline + position;
562
 
      cmdline_argc++;
563
 
      position += strlen(cmdline + position) + 1;
564
 
    }
565
 
    cmdline_argv[cmdline_argc] = NULL;
566
 
  }
 
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
  
567
604
  /* Kill old usplash */
568
605
  kill(usplash_pid, SIGTERM);
569
606
  sleep(2);
580
617
       the real user ID (_mandos) */
581
618
    ret = setuid(geteuid());
582
619
    if(ret == -1){
583
 
      perror("setuid");
 
620
      error_plus(0, errno, "setuid");
584
621
    }
585
622
    
586
623
    setsid();
587
624
    ret = chdir("/");
 
625
    if(ret == -1){
 
626
      error_plus(0, errno, "chdir");
 
627
      _exit(EX_OSERR);
 
628
    }
588
629
/*     if(fork() != 0){ */
589
630
/*       _exit(EXIT_SUCCESS); */
590
631
/*     } */
591
632
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
592
633
    if(ret == -1){
593
 
      perror("dup2");
594
 
      _exit(EXIT_FAILURE);
 
634
      error_plus(0, errno, "dup2");
 
635
      _exit(EX_OSERR);
595
636
    }
596
637
    
597
638
    execv(usplash_name, cmdline_argv);
598
639
    if(not interrupted_by_signal){
599
 
      perror("execv");
 
640
      error_plus(0, errno, "execv");
600
641
    }
601
642
    free(cmdline);
602
643
    free(cmdline_argv);
603
 
    _exit(EXIT_FAILURE);
 
644
    _exit(EX_OSERR);
604
645
  }
605
646
  free(cmdline);
606
647
  free(cmdline_argv);
607
648
  sleep(2);
608
649
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
609
650
    if(errno != EINTR){
610
 
      perror("usplash_write");
 
651
      error_plus(0, errno, "usplash_write");
611
652
    }
612
653
  }
613
654
  
614
655
  /* Close FIFO (again) */
615
 
  ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
616
 
  if(ret == -1 and errno != EINTR){
617
 
    perror("close");
 
656
  if(fifo_fd != -1){
 
657
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
658
    if(ret == -1 and errno != EINTR){
 
659
      error_plus(0, errno, "close");
 
660
    }
 
661
    fifo_fd = -1;
618
662
  }
619
 
  fifo_fd = -1;
620
663
  
621
664
  if(interrupted_by_signal){
622
665
    struct sigaction signal_action = { .sa_handler = SIG_DFL };
624
667
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
625
668
                                            &signal_action, NULL));
626
669
    if(ret == -1){
627
 
      perror("sigaction");
 
670
      error_plus(0, errno, "sigaction");
628
671
    }
629
672
    do {
630
673
      ret = raise(signal_received);
631
674
    } while(ret != 0 and errno == EINTR);
632
675
    if(ret != 0){
633
 
      perror("raise");
 
676
      error_plus(0, errno, "raise");
634
677
      abort();
635
678
    }
636
679
    TEMP_FAILURE_RETRY(pause());
637
680
  }
638
681
  
639
 
  return EXIT_FAILURE;
 
682
  return status;
640
683
}