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
35
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
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(), fprintf() */
39
40
#include <unistd.h> /* close(), write(), readlink(),
40
41
read(), STDOUT_FILENO, sleep(),
41
42
fork(), setuid(), geteuid(),
42
43
setsid(), chdir(), dup2(),
43
44
STDERR_FILENO, execv() */
44
45
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
45
EXIT_SUCCESS, malloc(), _exit() */
46
#include <stdlib.h> /* getenv() */
46
EXIT_SUCCESS, malloc(), _exit(),
47
48
#include <dirent.h> /* opendir(), readdir(), closedir() */
48
49
#include <inttypes.h> /* intmax_t, strtoimax() */
49
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
#include <argz.h> /* argz_count(), argz_extract() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
51
55
sig_atomic_t interrupted_by_signal = 0;
52
56
int signal_received;
53
57
const char usplash_name[] = "/sbin/usplash";
59
/* Function to use when printing errors */
60
void error_plus(int status, int errnum, const char *formatstring, ...){
65
va_start(ap, formatstring);
66
ret = vasprintf(&text, formatstring, ap);
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");
75
fprintf(stderr, "Mandos plugin ");
76
error(status, errnum, "%s", text);
55
80
static void termination_handler(int signum){
56
81
if(interrupted_by_signal){
319
347
sigemptyset(&new_action.sa_mask);
320
348
ret = sigaddset(&new_action.sa_mask, SIGINT);
350
error_plus(0, errno, "sigaddset");
325
354
ret = sigaddset(&new_action.sa_mask, SIGHUP);
356
error_plus(0, errno, "sigaddset");
330
360
ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
error_plus(0, errno, "sigaddset");
335
366
ret = sigaction(SIGINT, NULL, &old_action);
337
368
if(errno != EINTR){
369
error_plus(0, errno, "sigaction");
539
585
if(outfifo_fd != -1){
540
586
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
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;
588
error_plus(0, errno, "close");
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");
599
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
567
601
/* Kill old usplash */
568
602
kill(usplash_pid, SIGTERM);
580
614
the real user ID (_mandos) */
581
615
ret = setuid(geteuid());
617
error_plus(0, errno, "setuid");
587
621
ret = chdir("/");
623
error_plus(0, errno, "chdir");
588
626
/* if(fork() != 0){ */
589
627
/* _exit(EXIT_SUCCESS); */
591
629
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
631
error_plus(0, errno, "dup2");
597
635
execv(usplash_name, cmdline_argv);
598
636
if(not interrupted_by_signal){
637
error_plus(0, errno, "execv");
602
640
free(cmdline_argv);
606
644
free(cmdline_argv);
608
646
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
609
647
if(errno != EINTR){
610
perror("usplash_write");
648
error_plus(0, errno, "usplash_write");