3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008-2017 Teddy Hogeborn
6
* Copyright © 2008-2017 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
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
16
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
16
* General Public License for more details.
20
18
* You should have received a copy of the GNU General Public License
21
* along with Mandos. If not, see <http://www.gnu.org/licenses/>.
19
* along with this program. If not, see
20
* <http://www.gnu.org/licenses/>.
23
* Contact the authors at <mandos@recompile.se>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
26
25
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
32
31
#include <fcntl.h> /* open(), O_WRONLY, O_RDONLY */
33
32
#include <iso646.h> /* and, or, not*/
34
33
#include <errno.h> /* errno, EINTR */
36
34
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
38
36
#include <stddef.h> /* NULL */
39
#include <string.h> /* strlen(), memcmp(), strerror() */
40
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
37
#include <string.h> /* strlen(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror() */
42
39
#include <unistd.h> /* close(), write(), readlink(),
43
40
read(), STDOUT_FILENO, sleep(),
44
41
fork(), setuid(), geteuid(),
51
48
#include <inttypes.h> /* intmax_t, strtoimax() */
52
49
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
53
50
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
54
#include <argz.h> /* argz_count(), argz_extract() */
55
#include <stdarg.h> /* va_list, va_start(), ... */
57
52
sig_atomic_t interrupted_by_signal = 0;
58
53
int signal_received;
59
54
const char usplash_name[] = "/sbin/usplash";
61
/* Function to use when printing errors */
62
__attribute__((format (gnu_printf, 3, 4)))
63
void error_plus(int status, int errnum, const char *formatstring,
69
va_start(ap, formatstring);
70
ret = vasprintf(&text, formatstring, ap);
72
fprintf(stderr, "Mandos plugin %s: ",
73
program_invocation_short_name);
74
vfprintf(stderr, formatstring, ap);
75
fprintf(stderr, ": ");
76
fprintf(stderr, "%s\n", strerror(errnum));
77
error(status, errno, "vasprintf while printing error");
80
fprintf(stderr, "Mandos plugin ");
81
error(status, errnum, "%s", text);
85
56
static void termination_handler(int signum){
86
57
if(interrupted_by_signal){
253
224
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
254
225
proc_ent->d_name);
256
error_plus(0, errno, "asprintf");
257
228
goto fail_find_usplash;
259
230
cl_fd = open(cmdline_filename, O_RDONLY);
260
231
free(cmdline_filename);
262
error_plus(0, errno, "open");
263
234
goto fail_find_usplash;
271
242
if(cmdline_len + blocksize > cmdline_allocated){
272
243
tmp = realloc(cmdline, cmdline_allocated + blocksize);
274
error_plus(0, errno, "realloc");
276
247
goto fail_find_usplash;
282
253
sret = read(cl_fd, cmdline + cmdline_len,
283
254
cmdline_allocated - cmdline_len);
285
error_plus(0, errno, "read");
287
258
goto fail_find_usplash;
290
261
} while(sret != 0);
291
262
ret = close(cl_fd);
293
error_plus(0, errno, "close");
294
265
goto fail_find_usplash;
297
268
/* Close directory */
298
269
ret = closedir(proc_dir);
300
error_plus(0, errno, "closedir");
301
272
goto fail_find_usplash;
352
323
sigemptyset(&new_action.sa_mask);
353
324
ret = sigaddset(&new_action.sa_mask, SIGINT);
355
error_plus(0, errno, "sigaddset");
356
327
status = EX_OSERR;
359
330
ret = sigaddset(&new_action.sa_mask, SIGHUP);
361
error_plus(0, errno, "sigaddset");
362
333
status = EX_OSERR;
365
336
ret = sigaddset(&new_action.sa_mask, SIGTERM);
367
error_plus(0, errno, "sigaddset");
368
339
status = EX_OSERR;
371
342
ret = sigaction(SIGINT, NULL, &old_action);
373
344
if(errno != EINTR){
374
error_plus(0, errno, "sigaction");
375
346
status = EX_OSERR;
428
399
/* Write command to FIFO */
429
400
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
430
401
if(errno != EINTR){
431
error_plus(0, errno, "usplash_write");
402
perror("usplash_write");
432
403
status = EX_OSERR;
489
460
buf_allocated - buf_len);
491
462
if(errno != EINTR){
492
error_plus(0, errno, "read");
493
464
status = EX_OSERR;
466
TEMP_FAILURE_RETRY(close(outfifo_fd));
498
469
if(interrupted_by_signal){
581
552
if(fifo_fd != -1){
582
ret = close(fifo_fd);
553
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
583
554
if(ret == -1 and errno != EINTR){
584
error_plus(0, errno, "close");
589
560
/* Close output FIFO */
590
561
if(outfifo_fd != -1){
591
ret = close(outfifo_fd);
562
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
593
error_plus(0, errno, "close");
597
/* Create argv for new usplash*/
598
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
599
* sizeof(char *)); /* Count args */
600
if(cmdline_argv == NULL){
601
error_plus(0, errno, "malloc");
604
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
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;
606
589
/* Kill old usplash */
607
590
kill(usplash_pid, SIGTERM);
619
602
the real user ID (_mandos) */
620
603
ret = setuid(geteuid());
622
error_plus(0, errno, "setuid");
626
609
ret = chdir("/");
628
error_plus(0, errno, "chdir");
631
614
/* if(fork() != 0){ */
634
617
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
636
error_plus(0, errno, "dup2");
640
623
execv(usplash_name, cmdline_argv);
641
624
if(not interrupted_by_signal){
642
error_plus(0, errno, "execv");
645
628
free(cmdline_argv);
651
634
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
652
635
if(errno != EINTR){
653
error_plus(0, errno, "usplash_write");
636
perror("usplash_write");
657
640
/* Close FIFO (again) */
658
641
if(fifo_fd != -1){
659
ret = close(fifo_fd);
642
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
660
643
if(ret == -1 and errno != EINTR){
661
error_plus(0, errno, "close");
669
652
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
670
653
&signal_action, NULL));
672
error_plus(0, errno, "sigaction");
675
658
ret = raise(signal_received);
676
659
} while(ret != 0 and errno == EINTR);
678
error_plus(0, errno, "raise");
681
664
TEMP_FAILURE_RETRY(pause());