35
35
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
37
37
#include <stddef.h> /* NULL */
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
38
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(), fprintf() */
40
40
#include <unistd.h> /* close(), write(), readlink(),
41
41
read(), STDOUT_FILENO, sleep(),
42
42
fork(), setuid(), geteuid(),
50
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
52
#include <argz.h> /* argz_count(), argz_extract() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
54
55
sig_atomic_t interrupted_by_signal = 0;
55
56
int signal_received;
56
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);
58
80
static void termination_handler(int signum){
59
81
if(interrupted_by_signal){
155
177
size_t cmdline_len = 0;
156
178
DIR *proc_dir = opendir("/proc");
157
179
if(proc_dir == NULL){
158
error(0, errno, "opendir");
180
error_plus(0, errno, "opendir");
184
206
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
186
error(0, errno, "asprintf");
208
error_plus(0, errno, "asprintf");
187
209
goto fail_find_usplash;
226
248
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
249
proc_ent->d_name);
229
error(0, errno, "asprintf");
251
error_plus(0, errno, "asprintf");
230
252
goto fail_find_usplash;
232
254
cl_fd = open(cmdline_filename, O_RDONLY);
233
255
free(cmdline_filename);
235
error(0, errno, "open");
257
error_plus(0, errno, "open");
236
258
goto fail_find_usplash;
244
266
if(cmdline_len + blocksize > cmdline_allocated){
245
267
tmp = realloc(cmdline, cmdline_allocated + blocksize);
247
error(0, errno, "realloc");
269
error_plus(0, errno, "realloc");
249
271
goto fail_find_usplash;
255
277
sret = read(cl_fd, cmdline + cmdline_len,
256
278
cmdline_allocated - cmdline_len);
258
error(0, errno, "read");
280
error_plus(0, errno, "read");
260
282
goto fail_find_usplash;
263
285
} while(sret != 0);
264
286
ret = close(cl_fd);
266
error(0, errno, "close");
288
error_plus(0, errno, "close");
267
289
goto fail_find_usplash;
270
292
/* Close directory */
271
293
ret = closedir(proc_dir);
273
error(0, errno, "closedir");
295
error_plus(0, errno, "closedir");
274
296
goto fail_find_usplash;
325
347
sigemptyset(&new_action.sa_mask);
326
348
ret = sigaddset(&new_action.sa_mask, SIGINT);
328
error(0, errno, "sigaddset");
350
error_plus(0, errno, "sigaddset");
329
351
status = EX_OSERR;
332
354
ret = sigaddset(&new_action.sa_mask, SIGHUP);
334
error(0, errno, "sigaddset");
356
error_plus(0, errno, "sigaddset");
335
357
status = EX_OSERR;
338
360
ret = sigaddset(&new_action.sa_mask, SIGTERM);
340
error(0, errno, "sigaddset");
362
error_plus(0, errno, "sigaddset");
341
363
status = EX_OSERR;
344
366
ret = sigaction(SIGINT, NULL, &old_action);
346
368
if(errno != EINTR){
347
error(0, errno, "sigaction");
369
error_plus(0, errno, "sigaction");
348
370
status = EX_OSERR;
353
375
ret = sigaction(SIGINT, &new_action, NULL);
355
377
if(errno != EINTR){
356
error(0, errno, "sigaction");
378
error_plus(0, errno, "sigaction");
357
379
status = EX_OSERR;
362
384
ret = sigaction(SIGHUP, NULL, &old_action);
364
386
if(errno != EINTR){
365
error(0, errno, "sigaction");
387
error_plus(0, errno, "sigaction");
366
388
status = EX_OSERR;
371
393
ret = sigaction(SIGHUP, &new_action, NULL);
373
395
if(errno != EINTR){
374
error(0, errno, "sigaction");
396
error_plus(0, errno, "sigaction");
375
397
status = EX_OSERR;
380
402
ret = sigaction(SIGTERM, NULL, &old_action);
382
404
if(errno != EINTR){
383
error(0, errno, "sigaction");
405
error_plus(0, errno, "sigaction");
384
406
status = EX_OSERR;
389
411
ret = sigaction(SIGTERM, &new_action, NULL);
391
413
if(errno != EINTR){
392
error(0, errno, "sigaction");
414
error_plus(0, errno, "sigaction");
393
415
status = EX_OSERR;
401
423
/* Write command to FIFO */
402
424
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
425
if(errno != EINTR){
404
error(0, errno, "usplash_write");
426
error_plus(0, errno, "usplash_write");
405
427
status = EX_OSERR;
414
436
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
437
if(errno != EINTR){
416
error(0, errno, "usplash_write");
438
error_plus(0, errno, "usplash_write");
417
439
status = EX_OSERR;
450
472
char *tmp = realloc(buf, buf_allocated + blocksize);
452
474
if(errno != EINTR){
453
error(0, errno, "realloc");
475
error_plus(0, errno, "realloc");
454
476
status = EX_OSERR;
491
513
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
514
if(errno != EINTR){
493
error(0, errno, "usplash_write");
515
error_plus(0, errno, "usplash_write");
494
516
status = EX_OSERR;
517
539
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
519
541
if(errno != EINTR){
520
error(0, errno, "write");
542
error_plus(0, errno, "write");
521
543
status = EX_OSERR;
554
576
if(fifo_fd != -1){
555
577
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
556
578
if(ret == -1 and errno != EINTR){
557
error(0, errno, "close");
579
error_plus(0, errno, "close");
563
585
if(outfifo_fd != -1){
564
586
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
566
error(0, errno, "close");
588
error_plus(0, errno, "close");
571
593
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
594
* sizeof(char *)); /* Count args */
573
595
if(cmdline_argv == NULL){
574
error(0, errno, "malloc");
596
error_plus(0, errno, "malloc");
577
599
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
592
614
the real user ID (_mandos) */
593
615
ret = setuid(geteuid());
595
error(0, errno, "setuid");
617
error_plus(0, errno, "setuid");
599
621
ret = chdir("/");
601
error(0, errno, "chdir");
623
error_plus(0, errno, "chdir");
604
626
/* if(fork() != 0){ */
607
629
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
609
error(0, errno, "dup2");
631
error_plus(0, errno, "dup2");
613
635
execv(usplash_name, cmdline_argv);
614
636
if(not interrupted_by_signal){
615
error(0, errno, "execv");
637
error_plus(0, errno, "execv");
618
640
free(cmdline_argv);
624
646
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
647
if(errno != EINTR){
626
error(0, errno, "usplash_write");
648
error_plus(0, errno, "usplash_write");
631
653
if(fifo_fd != -1){
632
654
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
633
655
if(ret == -1 and errno != EINTR){
634
error(0, errno, "close");
656
error_plus(0, errno, "close");
642
664
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
665
&signal_action, NULL));
645
error(0, errno, "sigaction");
667
error_plus(0, errno, "sigaction");
648
670
ret = raise(signal_received);
649
671
} while(ret != 0 and errno == EINTR);
651
error(0, errno, "raise");
673
error_plus(0, errno, "raise");
654
676
TEMP_FAILURE_RETRY(pause());