3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 Björn Påhlsson
5
* Copyright © 2008-2011 Teddy Hogeborn
6
* Copyright © 2008-2011 Björn Påhlsson
8
8
* This program is free software: you can redistribute it and/or
9
9
* modify it under the terms of the GNU General Public License as
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(),
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){
224
248
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
225
249
proc_ent->d_name);
251
error_plus(0, errno, "asprintf");
228
252
goto fail_find_usplash;
230
254
cl_fd = open(cmdline_filename, O_RDONLY);
231
255
free(cmdline_filename);
257
error_plus(0, errno, "open");
234
258
goto fail_find_usplash;
242
266
if(cmdline_len + blocksize > cmdline_allocated){
243
267
tmp = realloc(cmdline, cmdline_allocated + blocksize);
269
error_plus(0, errno, "realloc");
247
271
goto fail_find_usplash;
253
277
sret = read(cl_fd, cmdline + cmdline_len,
254
278
cmdline_allocated - cmdline_len);
280
error_plus(0, errno, "read");
258
282
goto fail_find_usplash;
261
285
} while(sret != 0);
262
286
ret = close(cl_fd);
288
error_plus(0, errno, "close");
265
289
goto fail_find_usplash;
268
292
/* Close directory */
269
293
ret = closedir(proc_dir);
295
error_plus(0, errno, "closedir");
272
296
goto fail_find_usplash;
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;
399
423
/* Write command to FIFO */
400
424
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
401
425
if(errno != EINTR){
402
perror("usplash_write");
426
error_plus(0, errno, "usplash_write");
403
427
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");
614
626
/* if(fork() != 0){ */
617
629
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
631
error_plus(0, errno, "dup2");
623
635
execv(usplash_name, cmdline_argv);
624
636
if(not interrupted_by_signal){
637
error_plus(0, errno, "execv");
628
640
free(cmdline_argv);
652
664
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
653
665
&signal_action, NULL));
667
error_plus(0, errno, "sigaction");
658
670
ret = raise(signal_received);
659
671
} while(ret != 0 and errno == EINTR);
673
error_plus(0, errno, "raise");
664
676
TEMP_FAILURE_RETRY(pause());