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(),
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(),
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 */
50
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
52
56
sig_atomic_t interrupted_by_signal = 0;
53
57
int signal_received;
54
58
const char usplash_name[] = "/sbin/usplash";
60
/* Function to use when printing errors */
61
void error_plus(int status, int errnum, const char *formatstring,
67
va_start(ap, formatstring);
68
ret = vasprintf(&text, formatstring, ap);
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");
78
fprintf(stderr, "Mandos plugin ");
79
error(status, errnum, "%s", text);
56
83
static void termination_handler(int signum){
57
84
if(interrupted_by_signal){
323
350
sigemptyset(&new_action.sa_mask);
324
351
ret = sigaddset(&new_action.sa_mask, SIGINT);
353
error_plus(0, errno, "sigaddset");
327
354
status = EX_OSERR;
330
357
ret = sigaddset(&new_action.sa_mask, SIGHUP);
359
error_plus(0, errno, "sigaddset");
333
360
status = EX_OSERR;
336
363
ret = sigaddset(&new_action.sa_mask, SIGTERM);
365
error_plus(0, errno, "sigaddset");
339
366
status = EX_OSERR;
342
369
ret = sigaction(SIGINT, NULL, &old_action);
344
371
if(errno != EINTR){
372
error_plus(0, errno, "sigaction");
346
373
status = EX_OSERR;
561
588
if(outfifo_fd != -1){
562
589
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
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;
591
error_plus(0, errno, "close");
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");
602
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
589
604
/* Kill old usplash */
590
605
kill(usplash_pid, SIGTERM);
602
617
the real user ID (_mandos) */
603
618
ret = setuid(geteuid());
620
error_plus(0, errno, "setuid");
609
624
ret = chdir("/");
626
error_plus(0, errno, "chdir");
610
629
/* if(fork() != 0){ */
611
630
/* _exit(EXIT_SUCCESS); */
613
632
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
634
error_plus(0, errno, "dup2");
619
638
execv(usplash_name, cmdline_argv);
620
639
if(not interrupted_by_signal){
640
error_plus(0, errno, "execv");
624
643
free(cmdline_argv);