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
38
#include <string.h> /* strlen(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror() */
39
#include <stdio.h> /* asprintf()*/
39
40
#include <unistd.h> /* close(), write(), readlink(),
40
41
read(), STDOUT_FILENO, sleep(),
41
42
fork(), setuid(), geteuid(),
42
43
setsid(), chdir(), dup2(),
43
44
STDERR_FILENO, execv() */
44
45
#include <stdlib.h> /* free(), EXIT_FAILURE, realloc(),
45
EXIT_SUCCESS, malloc(), _exit() */
46
#include <stdlib.h> /* getenv() */
46
EXIT_SUCCESS, malloc(), _exit(),
47
48
#include <dirent.h> /* opendir(), readdir(), closedir() */
48
49
#include <inttypes.h> /* intmax_t, strtoimax() */
49
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
#include <argz.h> /* argz_count(), argz_extract() */
51
54
sig_atomic_t interrupted_by_signal = 0;
52
55
int signal_received;
223
226
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
224
227
proc_ent->d_name);
229
error(0, errno, "asprintf");
227
230
goto fail_find_usplash;
229
232
cl_fd = open(cmdline_filename, O_RDONLY);
230
233
free(cmdline_filename);
235
error(0, errno, "open");
233
236
goto fail_find_usplash;
319
325
sigemptyset(&new_action.sa_mask);
320
326
ret = sigaddset(&new_action.sa_mask, SIGINT);
328
error(0, errno, "sigaddset");
325
332
ret = sigaddset(&new_action.sa_mask, SIGHUP);
334
error(0, errno, "sigaddset");
330
338
ret = sigaddset(&new_action.sa_mask, SIGTERM);
340
error(0, errno, "sigaddset");
335
344
ret = sigaction(SIGINT, NULL, &old_action);
337
346
if(errno != EINTR){
347
error(0, errno, "sigaction");
386
401
/* Write command to FIFO */
387
402
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
388
403
if(errno != EINTR){
389
perror("usplash_write");
404
error(0, errno, "usplash_write");
524
548
/* If usplash was never accessed, we can stop now */
525
549
if(not usplash_accessed){
530
554
if(fifo_fd != -1){
531
555
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
532
556
if(ret == -1 and errno != EINTR){
557
error(0, errno, "close");
539
563
if(outfifo_fd != -1){
540
564
ret = (int)TEMP_FAILURE_RETRY(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;
566
error(0, errno, "close");
570
/* Create argv for new usplash*/
571
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
* sizeof(char *)); /* Count args */
573
if(cmdline_argv == NULL){
574
error(0, errno, "malloc");
577
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
567
579
/* Kill old usplash */
568
580
kill(usplash_pid, SIGTERM);
580
592
the real user ID (_mandos) */
581
593
ret = setuid(geteuid());
595
error(0, errno, "setuid");
587
599
ret = chdir("/");
601
error(0, errno, "chdir");
588
604
/* if(fork() != 0){ */
589
605
/* _exit(EXIT_SUCCESS); */
591
607
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
609
error(0, errno, "dup2");
597
613
execv(usplash_name, cmdline_argv);
598
614
if(not interrupted_by_signal){
615
error(0, errno, "execv");
602
618
free(cmdline_argv);
606
622
free(cmdline_argv);
608
624
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
609
625
if(errno != EINTR){
610
perror("usplash_write");
626
error(0, errno, "usplash_write");
614
630
/* Close FIFO (again) */
615
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
616
if(ret == -1 and errno != EINTR){
632
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
633
if(ret == -1 and errno != EINTR){
634
error(0, errno, "close");
621
639
if(interrupted_by_signal){
622
640
struct sigaction signal_action = { .sa_handler = SIG_DFL };
624
642
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
625
643
&signal_action, NULL));
645
error(0, errno, "sigaction");
630
648
ret = raise(signal_received);
631
649
} while(ret != 0 and errno == EINTR);
651
error(0, errno, "raise");
636
654
TEMP_FAILURE_RETRY(pause());