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-2015 Teddy Hogeborn
6
* Copyright © 2008-2015 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
19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
22
* Contact the authors at <mandos@recompile.se>.
25
25
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
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(),
39
41
#include <unistd.h> /* close(), write(), readlink(),
40
42
read(), STDOUT_FILENO, sleep(),
41
43
fork(), setuid(), geteuid(),
48
50
#include <inttypes.h> /* intmax_t, strtoimax() */
49
51
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
50
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
52
56
sig_atomic_t interrupted_by_signal = 0;
53
57
int signal_received;
54
58
const char usplash_name[] = "/sbin/usplash";
60
/* Function to use when printing errors */
61
__attribute__((format (gnu_printf, 3, 4)))
62
void error_plus(int status, int errnum, const char *formatstring,
68
va_start(ap, formatstring);
69
ret = vasprintf(&text, formatstring, ap);
71
fprintf(stderr, "Mandos plugin %s: ",
72
program_invocation_short_name);
73
vfprintf(stderr, formatstring, ap);
74
fprintf(stderr, ": ");
75
fprintf(stderr, "%s\n", strerror(errnum));
76
error(status, errno, "vasprintf while printing error");
79
fprintf(stderr, "Mandos plugin ");
80
error(status, errnum, "%s", text);
56
84
static void termination_handler(int signum){
57
85
if(interrupted_by_signal){
224
252
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
225
253
proc_ent->d_name);
255
error_plus(0, errno, "asprintf");
228
256
goto fail_find_usplash;
230
258
cl_fd = open(cmdline_filename, O_RDONLY);
231
259
free(cmdline_filename);
261
error_plus(0, errno, "open");
234
262
goto fail_find_usplash;
242
270
if(cmdline_len + blocksize > cmdline_allocated){
243
271
tmp = realloc(cmdline, cmdline_allocated + blocksize);
273
error_plus(0, errno, "realloc");
247
275
goto fail_find_usplash;
253
281
sret = read(cl_fd, cmdline + cmdline_len,
254
282
cmdline_allocated - cmdline_len);
284
error_plus(0, errno, "read");
258
286
goto fail_find_usplash;
261
289
} while(sret != 0);
262
290
ret = close(cl_fd);
292
error_plus(0, errno, "close");
265
293
goto fail_find_usplash;
268
296
/* Close directory */
269
297
ret = closedir(proc_dir);
299
error_plus(0, errno, "closedir");
272
300
goto fail_find_usplash;
323
351
sigemptyset(&new_action.sa_mask);
324
352
ret = sigaddset(&new_action.sa_mask, SIGINT);
354
error_plus(0, errno, "sigaddset");
327
355
status = EX_OSERR;
330
358
ret = sigaddset(&new_action.sa_mask, SIGHUP);
360
error_plus(0, errno, "sigaddset");
333
361
status = EX_OSERR;
336
364
ret = sigaddset(&new_action.sa_mask, SIGTERM);
366
error_plus(0, errno, "sigaddset");
339
367
status = EX_OSERR;
342
370
ret = sigaction(SIGINT, NULL, &old_action);
344
372
if(errno != EINTR){
373
error_plus(0, errno, "sigaction");
346
374
status = EX_OSERR;
399
427
/* Write command to FIFO */
400
428
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
401
429
if(errno != EINTR){
402
perror("usplash_write");
430
error_plus(0, errno, "usplash_write");
403
431
status = EX_OSERR;
460
488
buf_allocated - buf_len);
462
490
if(errno != EINTR){
491
error_plus(0, errno, "read");
464
492
status = EX_OSERR;
466
TEMP_FAILURE_RETRY(close(outfifo_fd));
469
497
if(interrupted_by_signal){
552
580
if(fifo_fd != -1){
553
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
581
ret = close(fifo_fd);
554
582
if(ret == -1 and errno != EINTR){
583
error_plus(0, errno, "close");
560
588
/* Close output FIFO */
561
589
if(outfifo_fd != -1){
562
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
590
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;
592
error_plus(0, errno, "close");
596
/* Create argv for new usplash*/
597
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
598
* sizeof(char *)); /* Count args */
599
if(cmdline_argv == NULL){
600
error_plus(0, errno, "malloc");
603
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
589
605
/* Kill old usplash */
590
606
kill(usplash_pid, SIGTERM);
602
618
the real user ID (_mandos) */
603
619
ret = setuid(geteuid());
621
error_plus(0, errno, "setuid");
609
625
ret = chdir("/");
627
error_plus(0, errno, "chdir");
614
630
/* if(fork() != 0){ */
617
633
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
635
error_plus(0, errno, "dup2");
623
639
execv(usplash_name, cmdline_argv);
624
640
if(not interrupted_by_signal){
641
error_plus(0, errno, "execv");
628
644
free(cmdline_argv);
634
650
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
635
651
if(errno != EINTR){
636
perror("usplash_write");
652
error_plus(0, errno, "usplash_write");
640
656
/* Close FIFO (again) */
641
657
if(fifo_fd != -1){
642
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
658
ret = close(fifo_fd);
643
659
if(ret == -1 and errno != EINTR){
660
error_plus(0, errno, "close");
652
668
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
653
669
&signal_action, NULL));
671
error_plus(0, errno, "sigaction");
658
674
ret = raise(signal_received);
659
675
} while(ret != 0 and errno == EINTR);
677
error_plus(0, errno, "raise");
664
680
TEMP_FAILURE_RETRY(pause());