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 */
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
51
56
sig_atomic_t interrupted_by_signal = 0;
52
57
int signal_received;
53
58
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);
55
84
static void termination_handler(int signum){
56
85
if(interrupted_by_signal){
319
351
sigemptyset(&new_action.sa_mask);
320
352
ret = sigaddset(&new_action.sa_mask, SIGINT);
354
error_plus(0, errno, "sigaddset");
325
358
ret = sigaddset(&new_action.sa_mask, SIGHUP);
360
error_plus(0, errno, "sigaddset");
330
364
ret = sigaddset(&new_action.sa_mask, SIGTERM);
366
error_plus(0, errno, "sigaddset");
335
370
ret = sigaction(SIGINT, NULL, &old_action);
337
372
if(errno != EINTR){
373
error_plus(0, errno, "sigaction");
539
589
if(outfifo_fd != -1){
540
590
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;
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 */
567
605
/* Kill old usplash */
568
606
kill(usplash_pid, SIGTERM);
580
618
the real user ID (_mandos) */
581
619
ret = setuid(geteuid());
621
error_plus(0, errno, "setuid");
587
625
ret = chdir("/");
627
error_plus(0, errno, "chdir");
588
630
/* if(fork() != 0){ */
589
631
/* _exit(EXIT_SUCCESS); */
591
633
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
635
error_plus(0, errno, "dup2");
597
639
execv(usplash_name, cmdline_argv);
598
640
if(not interrupted_by_signal){
641
error_plus(0, errno, "execv");
602
644
free(cmdline_argv);
606
648
free(cmdline_argv);
608
650
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
609
651
if(errno != EINTR){
610
perror("usplash_write");
652
error_plus(0, errno, "usplash_write");