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 Teddy Hogeborn
6
* Copyright © 2008-2018 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
26
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
31
32
#include <fcntl.h> /* open(), O_WRONLY, O_RDONLY */
32
33
#include <iso646.h> /* and, or, not*/
33
34
#include <errno.h> /* errno, EINTR */
34
36
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
36
38
#include <stddef.h> /* NULL */
37
#include <string.h> /* strlen(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror() */
39
#include <string.h> /* strlen(), memcmp(), strerror() */
40
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
39
42
#include <unistd.h> /* close(), write(), readlink(),
40
43
read(), STDOUT_FILENO, sleep(),
41
44
fork(), setuid(), geteuid(),
48
51
#include <inttypes.h> /* intmax_t, strtoimax() */
49
52
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
50
53
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
54
#include <argz.h> /* argz_count(), argz_extract() */
55
#include <stdarg.h> /* va_list, va_start(), ... */
52
57
sig_atomic_t interrupted_by_signal = 0;
53
58
int signal_received;
54
59
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);
56
85
static void termination_handler(int signum){
57
86
if(interrupted_by_signal){
224
253
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
225
254
proc_ent->d_name);
256
error_plus(0, errno, "asprintf");
228
257
goto fail_find_usplash;
230
259
cl_fd = open(cmdline_filename, O_RDONLY);
231
260
free(cmdline_filename);
262
error_plus(0, errno, "open");
234
263
goto fail_find_usplash;
242
271
if(cmdline_len + blocksize > cmdline_allocated){
243
272
tmp = realloc(cmdline, cmdline_allocated + blocksize);
274
error_plus(0, errno, "realloc");
247
276
goto fail_find_usplash;
253
282
sret = read(cl_fd, cmdline + cmdline_len,
254
283
cmdline_allocated - cmdline_len);
285
error_plus(0, errno, "read");
258
287
goto fail_find_usplash;
261
290
} while(sret != 0);
262
291
ret = close(cl_fd);
293
error_plus(0, errno, "close");
265
294
goto fail_find_usplash;
268
297
/* Close directory */
269
298
ret = closedir(proc_dir);
300
error_plus(0, errno, "closedir");
272
301
goto fail_find_usplash;
323
352
sigemptyset(&new_action.sa_mask);
324
353
ret = sigaddset(&new_action.sa_mask, SIGINT);
355
error_plus(0, errno, "sigaddset");
327
356
status = EX_OSERR;
330
359
ret = sigaddset(&new_action.sa_mask, SIGHUP);
361
error_plus(0, errno, "sigaddset");
333
362
status = EX_OSERR;
336
365
ret = sigaddset(&new_action.sa_mask, SIGTERM);
367
error_plus(0, errno, "sigaddset");
339
368
status = EX_OSERR;
342
371
ret = sigaction(SIGINT, NULL, &old_action);
344
373
if(errno != EINTR){
374
error_plus(0, errno, "sigaction");
346
375
status = EX_OSERR;
399
428
/* Write command to FIFO */
400
429
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
401
430
if(errno != EINTR){
402
perror("usplash_write");
431
error_plus(0, errno, "usplash_write");
403
432
status = EX_OSERR;
460
489
buf_allocated - buf_len);
462
491
if(errno != EINTR){
492
error_plus(0, errno, "read");
464
493
status = EX_OSERR;
466
TEMP_FAILURE_RETRY(close(outfifo_fd));
469
498
if(interrupted_by_signal){
552
581
if(fifo_fd != -1){
553
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
ret = close(fifo_fd);
554
583
if(ret == -1 and errno != EINTR){
584
error_plus(0, errno, "close");
560
589
/* Close output FIFO */
561
590
if(outfifo_fd != -1){
562
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
591
ret = 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;
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 */
589
606
/* Kill old usplash */
590
607
kill(usplash_pid, SIGTERM);
602
619
the real user ID (_mandos) */
603
620
ret = setuid(geteuid());
622
error_plus(0, errno, "setuid");
609
626
ret = chdir("/");
628
error_plus(0, errno, "chdir");
614
631
/* if(fork() != 0){ */
617
634
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
636
error_plus(0, errno, "dup2");
623
640
execv(usplash_name, cmdline_argv);
624
641
if(not interrupted_by_signal){
642
error_plus(0, errno, "execv");
628
645
free(cmdline_argv);
634
651
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
635
652
if(errno != EINTR){
636
perror("usplash_write");
653
error_plus(0, errno, "usplash_write");
640
657
/* Close FIFO (again) */
641
658
if(fifo_fd != -1){
642
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
659
ret = close(fifo_fd);
643
660
if(ret == -1 and errno != EINTR){
661
error_plus(0, errno, "close");
652
669
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
653
670
&signal_action, NULL));
672
error_plus(0, errno, "sigaction");
658
675
ret = raise(signal_received);
659
676
} while(ret != 0 and errno == EINTR);
678
error_plus(0, errno, "raise");
664
681
TEMP_FAILURE_RETRY(pause());