3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008-2014 Teddy Hogeborn
6
* Copyright © 2008-2014 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
19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@recompile.se>.
22
* Contact the authors at <mandos@fukt.bsnet.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 */
35
34
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
37
36
#include <stddef.h> /* NULL */
38
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
37
#include <string.h> /* strlen(), memcmp() */
38
#include <stdio.h> /* asprintf(), perror() */
41
39
#include <unistd.h> /* close(), write(), readlink(),
42
40
read(), STDOUT_FILENO, sleep(),
43
41
fork(), setuid(), geteuid(),
50
48
#include <inttypes.h> /* intmax_t, strtoimax() */
51
49
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
52
50
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
56
52
sig_atomic_t interrupted_by_signal = 0;
57
53
int signal_received;
58
54
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);
84
56
static void termination_handler(int signum){
85
57
if(interrupted_by_signal){
252
224
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
253
225
proc_ent->d_name);
255
error_plus(0, errno, "asprintf");
256
228
goto fail_find_usplash;
258
230
cl_fd = open(cmdline_filename, O_RDONLY);
259
231
free(cmdline_filename);
261
error_plus(0, errno, "open");
262
234
goto fail_find_usplash;
270
242
if(cmdline_len + blocksize > cmdline_allocated){
271
243
tmp = realloc(cmdline, cmdline_allocated + blocksize);
273
error_plus(0, errno, "realloc");
275
247
goto fail_find_usplash;
281
253
sret = read(cl_fd, cmdline + cmdline_len,
282
254
cmdline_allocated - cmdline_len);
284
error_plus(0, errno, "read");
286
258
goto fail_find_usplash;
289
261
} while(sret != 0);
290
262
ret = close(cl_fd);
292
error_plus(0, errno, "close");
293
265
goto fail_find_usplash;
296
268
/* Close directory */
297
269
ret = closedir(proc_dir);
299
error_plus(0, errno, "closedir");
300
272
goto fail_find_usplash;
351
323
sigemptyset(&new_action.sa_mask);
352
324
ret = sigaddset(&new_action.sa_mask, SIGINT);
354
error_plus(0, errno, "sigaddset");
355
327
status = EX_OSERR;
358
330
ret = sigaddset(&new_action.sa_mask, SIGHUP);
360
error_plus(0, errno, "sigaddset");
361
333
status = EX_OSERR;
364
336
ret = sigaddset(&new_action.sa_mask, SIGTERM);
366
error_plus(0, errno, "sigaddset");
367
339
status = EX_OSERR;
370
342
ret = sigaction(SIGINT, NULL, &old_action);
372
344
if(errno != EINTR){
373
error_plus(0, errno, "sigaction");
374
346
status = EX_OSERR;
427
399
/* Write command to FIFO */
428
400
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
429
401
if(errno != EINTR){
430
error_plus(0, errno, "usplash_write");
402
perror("usplash_write");
431
403
status = EX_OSERR;
589
561
if(outfifo_fd != -1){
590
562
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
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 */
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;
605
589
/* Kill old usplash */
606
590
kill(usplash_pid, SIGTERM);
618
602
the real user ID (_mandos) */
619
603
ret = setuid(geteuid());
621
error_plus(0, errno, "setuid");
625
609
ret = chdir("/");
627
error_plus(0, errno, "chdir");
630
614
/* if(fork() != 0){ */
633
617
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
635
error_plus(0, errno, "dup2");
639
623
execv(usplash_name, cmdline_argv);
640
624
if(not interrupted_by_signal){
641
error_plus(0, errno, "execv");
644
628
free(cmdline_argv);
668
652
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
669
653
&signal_action, NULL));
671
error_plus(0, errno, "sigaction");
674
658
ret = raise(signal_received);
675
659
} while(ret != 0 and errno == EINTR);
677
error_plus(0, errno, "raise");
680
664
TEMP_FAILURE_RETRY(pause());