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
8
* This program is free software: you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License as
10
* published by the Free Software Foundation, either version 3 of the
11
* License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
5
* Copyright © 2008-2018, 2021-2022 Teddy Hogeborn
6
* Copyright © 2008-2018, 2021-2022 Björn Påhlsson
8
* This file is part of Mandos.
10
* Mandos is free software: you can redistribute it and/or modify it
11
* under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* Mandos is distributed in the hope that it will be useful, but
14
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
18
* General Public License for more details.
18
20
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see
20
* <http://www.gnu.org/licenses/>.
21
* along with Mandos. If not, see <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
23
* Contact the authors at <mandos@recompile.se>.
25
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
26
#include <signal.h> /* sig_atomic_t, struct sigaction,
27
sigemptyset(), sigaddset(), SIGINT,
28
SIGHUP, SIGTERM, sigaction(),
29
SIG_IGN, kill(), SIGKILL */
26
#define _GNU_SOURCE /* vasprintf(),
27
program_invocation_short_name,
28
asprintf(), TEMP_FAILURE_RETRY() */
29
#include <sys/types.h> /* sig_atomic_t, pid_t, setuid(),
30
geteuid(), setsid() */
31
#include <stdarg.h> /* va_list, va_start(), vfprintf() */
32
#include <stdio.h> /* vasprintf(), fprintf(), stderr,
33
vfprintf(), asprintf() */
34
#include <errno.h> /* program_invocation_short_name,
35
errno, ENOENT, EINTR */
36
#include <string.h> /* strerror(), strlen(), memcmp() */
37
#include <error.h> /* error() */
38
#include <stdlib.h> /* free(), getenv(), realloc(),
39
EXIT_FAILURE, EXIT_SUCCESS,
30
41
#include <stdbool.h> /* bool, false, true */
31
42
#include <fcntl.h> /* open(), O_WRONLY, O_RDONLY */
32
#include <iso646.h> /* and, or, not*/
33
#include <errno.h> /* errno, EINTR */
34
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
36
#include <stddef.h> /* NULL */
37
#include <string.h> /* strlen(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror() */
39
#include <unistd.h> /* close(), write(), readlink(),
40
read(), STDOUT_FILENO, sleep(),
41
fork(), setuid(), geteuid(),
42
setsid(), chdir(), dup2(),
43
STDERR_FILENO, execv() */
44
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
45
EXIT_SUCCESS, malloc(), _exit() */
46
#include <stdlib.h> /* getenv() */
47
#include <dirent.h> /* opendir(), readdir(), closedir() */
43
#include <stddef.h> /* size_t, NULL */
44
#include <unistd.h> /* close(), ssize_t, write(),
45
readlink(), read(), STDOUT_FILENO,
46
sleep(), fork(), setuid(),
47
geteuid(), setsid(), chdir(),
48
_exit(), dup2(), STDERR_FILENO,
49
execv(), TEMP_FAILURE_RETRY(),
51
#include <dirent.h> /* DIR, opendir(), struct dirent,
52
readdir(), closedir() */
48
53
#include <inttypes.h> /* intmax_t, strtoimax() */
49
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
54
#include <iso646.h> /* or, not, and */
55
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK() */
56
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
57
#include <signal.h> /* struct sigaction, sigemptyset(),
58
sigaddset(), SIGINT, SIGHUP,
59
SIGTERM, SIG_IGN, kill(), SIGKILL,
61
#include <argz.h> /* argz_count(), argz_extract() */
51
63
sig_atomic_t interrupted_by_signal = 0;
52
64
int signal_received;
53
65
const char usplash_name[] = "/sbin/usplash";
67
/* Function to use when printing errors */
68
__attribute__((format (gnu_printf, 3, 4)))
69
void error_plus(int status, int errnum, const char *formatstring,
75
va_start(ap, formatstring);
76
ret = vasprintf(&text, formatstring, ap);
78
fprintf(stderr, "Mandos plugin %s: ",
79
program_invocation_short_name);
80
vfprintf(stderr, formatstring, ap);
81
fprintf(stderr, ": ");
82
fprintf(stderr, "%s\n", strerror(errnum));
83
error(status, errno, "vasprintf while printing error");
86
fprintf(stderr, "Mandos plugin ");
87
error(status, errnum, "%s", text);
55
91
static void termination_handler(int signum){
56
92
if(interrupted_by_signal){
524
581
/* If usplash was never accessed, we can stop now */
525
582
if(not usplash_accessed){
530
587
if(fifo_fd != -1){
531
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
588
ret = close(fifo_fd);
532
589
if(ret == -1 and errno != EINTR){
590
error_plus(0, errno, "close");
538
595
/* Close output FIFO */
539
596
if(outfifo_fd != -1){
540
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
597
ret = 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;
599
error_plus(0, errno, "close");
603
/* Create argv for new usplash*/
604
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
605
* sizeof(char *)); /* Count args */
606
if(cmdline_argv == NULL){
607
error_plus(0, errno, "malloc");
610
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
567
612
/* Kill old usplash */
568
613
kill(usplash_pid, SIGTERM);