3
3
 * Usplash - Read a password from usplash and output it
 
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
 
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 */
 
35
34
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
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(),
 
 
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() */
 
54
52
sig_atomic_t interrupted_by_signal = 0;
 
55
53
int signal_received;
 
 
226
224
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
227
225
                       proc_ent->d_name);
 
229
 
          error(0, errno, "asprintf");
 
230
228
          goto fail_find_usplash;
 
232
230
        cl_fd = open(cmdline_filename, O_RDONLY);
 
233
231
        free(cmdline_filename);
 
235
 
          error(0, errno, "open");
 
236
234
          goto fail_find_usplash;
 
 
325
323
    sigemptyset(&new_action.sa_mask);
 
326
324
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
328
 
      error(0, errno, "sigaddset");
 
329
327
      status = EX_OSERR;
 
332
330
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
334
 
      error(0, errno, "sigaddset");
 
335
333
      status = EX_OSERR;
 
338
336
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
340
 
      error(0, errno, "sigaddset");
 
341
339
      status = EX_OSERR;
 
344
342
    ret = sigaction(SIGINT, NULL, &old_action);
 
346
344
      if(errno != EINTR){
 
347
 
        error(0, errno, "sigaction");
 
348
346
        status = EX_OSERR;
 
 
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;
 
 
563
561
  if(outfifo_fd != -1){
 
564
562
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
 
566
 
      error(0, errno, "close");
 
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");
 
577
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
 
 
568
  /* Create argc and argv for new usplash*/
 
 
569
  int cmdline_argc = 0;
 
 
570
  char **cmdline_argv = malloc(sizeof(char *));
 
 
573
    while(position < cmdline_len){
 
 
574
      char **tmp = realloc(cmdline_argv,
 
 
576
                            * (size_t)(cmdline_argc + 2)));
 
 
583
      cmdline_argv[cmdline_argc] = cmdline + position;
 
 
585
      position += strlen(cmdline + position) + 1;
 
 
587
    cmdline_argv[cmdline_argc] = NULL;
 
579
589
  /* Kill old usplash */
 
580
590
  kill(usplash_pid, SIGTERM);
 
 
592
602
       the real user ID (_mandos) */
 
593
603
    ret = setuid(geteuid());
 
595
 
      error(0, errno, "setuid");
 
599
609
    ret = chdir("/");
 
601
 
      error(0, errno, "chdir");
 
604
610
/*     if(fork() != 0){ */
 
605
611
/*       _exit(EXIT_SUCCESS); */
 
607
613
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
609
 
      error(0, errno, "dup2");
 
613
619
    execv(usplash_name, cmdline_argv);
 
614
620
    if(not interrupted_by_signal){
 
615
 
      error(0, errno, "execv");
 
618
624
    free(cmdline_argv);
 
 
642
648
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
643
649
                                            &signal_action, NULL));
 
645
 
      error(0, errno, "sigaction");
 
648
654
      ret = raise(signal_received);
 
649
655
    } while(ret != 0 and errno == EINTR);
 
651
 
      error(0, errno, "raise");
 
654
660
    TEMP_FAILURE_RETRY(pause());