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