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