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(), fprintf() */
37
#include <string.h> /* strlen(), memcmp() */
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() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
55
52
sig_atomic_t interrupted_by_signal = 0;
56
53
int signal_received;
57
54
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);
80
56
static void termination_handler(int signum){
81
57
if(interrupted_by_signal){
347
323
sigemptyset(&new_action.sa_mask);
348
324
ret = sigaddset(&new_action.sa_mask, SIGINT);
350
error_plus(0, errno, "sigaddset");
351
327
status = EX_OSERR;
354
330
ret = sigaddset(&new_action.sa_mask, SIGHUP);
356
error_plus(0, errno, "sigaddset");
357
333
status = EX_OSERR;
360
336
ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
error_plus(0, errno, "sigaddset");
363
339
status = EX_OSERR;
366
342
ret = sigaction(SIGINT, NULL, &old_action);
368
344
if(errno != EINTR){
369
error_plus(0, errno, "sigaction");
370
346
status = EX_OSERR;
585
561
if(outfifo_fd != -1){
586
562
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
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 */
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;
601
589
/* Kill old usplash */
602
590
kill(usplash_pid, SIGTERM);
614
602
the real user ID (_mandos) */
615
603
ret = setuid(geteuid());
617
error_plus(0, errno, "setuid");
621
609
ret = chdir("/");
623
error_plus(0, errno, "chdir");
626
610
/* if(fork() != 0){ */
627
611
/* _exit(EXIT_SUCCESS); */
629
613
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
631
error_plus(0, errno, "dup2");
635
619
execv(usplash_name, cmdline_argv);
636
620
if(not interrupted_by_signal){
637
error_plus(0, errno, "execv");
640
624
free(cmdline_argv);