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
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
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(),
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
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
56
51
sig_atomic_t interrupted_by_signal = 0;
57
52
int signal_received;
58
53
const char usplash_name[] = "/sbin/usplash";
60
/* Function to use when printing errors */
61
__attribute__((format (gnu_printf, 3, 4)))
62
void error_plus(int status, int errnum, const char *formatstring,
68
va_start(ap, formatstring);
69
ret = vasprintf(&text, formatstring, ap);
71
fprintf(stderr, "Mandos plugin %s: ",
72
program_invocation_short_name);
73
vfprintf(stderr, formatstring, ap);
74
fprintf(stderr, ": ");
75
fprintf(stderr, "%s\n", strerror(errnum));
76
error(status, errno, "vasprintf while printing error");
79
fprintf(stderr, "Mandos plugin ");
80
error(status, errnum, "%s", text);
84
55
static void termination_handler(int signum){
85
56
if(interrupted_by_signal){
351
319
sigemptyset(&new_action.sa_mask);
352
320
ret = sigaddset(&new_action.sa_mask, SIGINT);
354
error_plus(0, errno, "sigaddset");
358
325
ret = sigaddset(&new_action.sa_mask, SIGHUP);
360
error_plus(0, errno, "sigaddset");
364
330
ret = sigaddset(&new_action.sa_mask, SIGTERM);
366
error_plus(0, errno, "sigaddset");
370
335
ret = sigaction(SIGINT, NULL, &old_action);
372
337
if(errno != EINTR){
373
error_plus(0, errno, "sigaction");
574
524
/* If usplash was never accessed, we can stop now */
575
525
if(not usplash_accessed){
580
530
if(fifo_fd != -1){
581
ret = close(fifo_fd);
531
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
532
if(ret == -1 and errno != EINTR){
583
error_plus(0, errno, "close");
588
538
/* Close output FIFO */
589
539
if(outfifo_fd != -1){
590
ret = close(outfifo_fd);
540
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
592
error_plus(0, errno, "close");
596
/* Create argv for new usplash*/
597
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
598
* sizeof(char *)); /* Count args */
599
if(cmdline_argv == NULL){
600
error_plus(0, errno, "malloc");
603
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
546
/* Create argc and argv for new usplash*/
547
int cmdline_argc = 0;
548
char **cmdline_argv = malloc(sizeof(char *));
551
while(position < cmdline_len){
552
char **tmp = realloc(cmdline_argv,
554
* (size_t)(cmdline_argc + 2)));
561
cmdline_argv[cmdline_argc] = cmdline + position;
563
position += strlen(cmdline + position) + 1;
565
cmdline_argv[cmdline_argc] = NULL;
605
567
/* Kill old usplash */
606
568
kill(usplash_pid, SIGTERM);
618
580
the real user ID (_mandos) */
619
581
ret = setuid(geteuid());
621
error_plus(0, errno, "setuid");
625
587
ret = chdir("/");
627
error_plus(0, errno, "chdir");
630
588
/* if(fork() != 0){ */
631
589
/* _exit(EXIT_SUCCESS); */
633
591
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
635
error_plus(0, errno, "dup2");
639
597
execv(usplash_name, cmdline_argv);
640
598
if(not interrupted_by_signal){
641
error_plus(0, errno, "execv");
644
602
free(cmdline_argv);
648
606
free(cmdline_argv);
650
608
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
651
609
if(errno != EINTR){
652
error_plus(0, errno, "usplash_write");
610
perror("usplash_write");
656
614
/* Close FIFO (again) */
658
ret = close(fifo_fd);
659
if(ret == -1 and errno != EINTR){
660
error_plus(0, errno, "close");
615
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
616
if(ret == -1 and errno != EINTR){
665
621
if(interrupted_by_signal){
666
622
struct sigaction signal_action = { .sa_handler = SIG_DFL };