3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008-2011 Teddy Hogeborn
6
* Copyright © 2008-2011 Björn Påhlsson
5
* Copyright © 2008-2010 Teddy Hogeborn
6
* Copyright © 2008-2010 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
35
35
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
37
37
#include <stddef.h> /* NULL */
38
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(), fprintf() */
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
40
40
#include <unistd.h> /* close(), write(), readlink(),
41
41
read(), STDOUT_FILENO, sleep(),
42
42
fork(), setuid(), geteuid(),
50
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
52
#include <argz.h> /* argz_count(), argz_extract() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
55
54
sig_atomic_t interrupted_by_signal = 0;
56
55
int signal_received;
57
56
const char usplash_name[] = "/sbin/usplash";
59
/* Function to use when printing errors */
60
void error_plus(int status, int errnum, const char *formatstring, ...){
65
va_start(ap, formatstring);
66
ret = vasprintf(&text, formatstring, ap);
68
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
69
vfprintf(stderr, formatstring, ap);
70
fprintf(stderr, ": ");
71
fprintf(stderr, "%s\n", strerror(errnum));
72
error(status, errno, "vasprintf while printing error");
75
fprintf(stderr, "Mandos plugin ");
76
error(status, errnum, "%s", text);
80
58
static void termination_handler(int signum){
81
59
if(interrupted_by_signal){
177
155
size_t cmdline_len = 0;
178
156
DIR *proc_dir = opendir("/proc");
179
157
if(proc_dir == NULL){
180
error_plus(0, errno, "opendir");
158
error(0, errno, "opendir");
206
184
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
208
error_plus(0, errno, "asprintf");
186
error(0, errno, "asprintf");
209
187
goto fail_find_usplash;
248
226
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
249
227
proc_ent->d_name);
251
error_plus(0, errno, "asprintf");
229
error(0, errno, "asprintf");
252
230
goto fail_find_usplash;
254
232
cl_fd = open(cmdline_filename, O_RDONLY);
255
233
free(cmdline_filename);
257
error_plus(0, errno, "open");
235
error(0, errno, "open");
258
236
goto fail_find_usplash;
266
244
if(cmdline_len + blocksize > cmdline_allocated){
267
245
tmp = realloc(cmdline, cmdline_allocated + blocksize);
269
error_plus(0, errno, "realloc");
247
error(0, errno, "realloc");
271
249
goto fail_find_usplash;
277
255
sret = read(cl_fd, cmdline + cmdline_len,
278
256
cmdline_allocated - cmdline_len);
280
error_plus(0, errno, "read");
258
error(0, errno, "read");
282
260
goto fail_find_usplash;
285
263
} while(sret != 0);
286
264
ret = close(cl_fd);
288
error_plus(0, errno, "close");
266
error(0, errno, "close");
289
267
goto fail_find_usplash;
292
270
/* Close directory */
293
271
ret = closedir(proc_dir);
295
error_plus(0, errno, "closedir");
273
error(0, errno, "closedir");
296
274
goto fail_find_usplash;
347
325
sigemptyset(&new_action.sa_mask);
348
326
ret = sigaddset(&new_action.sa_mask, SIGINT);
350
error_plus(0, errno, "sigaddset");
328
error(0, errno, "sigaddset");
351
329
status = EX_OSERR;
354
332
ret = sigaddset(&new_action.sa_mask, SIGHUP);
356
error_plus(0, errno, "sigaddset");
334
error(0, errno, "sigaddset");
357
335
status = EX_OSERR;
360
338
ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
error_plus(0, errno, "sigaddset");
340
error(0, errno, "sigaddset");
363
341
status = EX_OSERR;
366
344
ret = sigaction(SIGINT, NULL, &old_action);
368
346
if(errno != EINTR){
369
error_plus(0, errno, "sigaction");
347
error(0, errno, "sigaction");
370
348
status = EX_OSERR;
375
353
ret = sigaction(SIGINT, &new_action, NULL);
377
355
if(errno != EINTR){
378
error_plus(0, errno, "sigaction");
356
error(0, errno, "sigaction");
379
357
status = EX_OSERR;
384
362
ret = sigaction(SIGHUP, NULL, &old_action);
386
364
if(errno != EINTR){
387
error_plus(0, errno, "sigaction");
365
error(0, errno, "sigaction");
388
366
status = EX_OSERR;
393
371
ret = sigaction(SIGHUP, &new_action, NULL);
395
373
if(errno != EINTR){
396
error_plus(0, errno, "sigaction");
374
error(0, errno, "sigaction");
397
375
status = EX_OSERR;
402
380
ret = sigaction(SIGTERM, NULL, &old_action);
404
382
if(errno != EINTR){
405
error_plus(0, errno, "sigaction");
383
error(0, errno, "sigaction");
406
384
status = EX_OSERR;
411
389
ret = sigaction(SIGTERM, &new_action, NULL);
413
391
if(errno != EINTR){
414
error_plus(0, errno, "sigaction");
392
error(0, errno, "sigaction");
415
393
status = EX_OSERR;
423
401
/* Write command to FIFO */
424
402
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
425
403
if(errno != EINTR){
426
error_plus(0, errno, "usplash_write");
404
error(0, errno, "usplash_write");
427
405
status = EX_OSERR;
436
414
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
437
415
if(errno != EINTR){
438
error_plus(0, errno, "usplash_write");
416
error(0, errno, "usplash_write");
439
417
status = EX_OSERR;
472
450
char *tmp = realloc(buf, buf_allocated + blocksize);
474
452
if(errno != EINTR){
475
error_plus(0, errno, "realloc");
453
error(0, errno, "realloc");
476
454
status = EX_OSERR;
513
491
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
514
492
if(errno != EINTR){
515
error_plus(0, errno, "usplash_write");
493
error(0, errno, "usplash_write");
516
494
status = EX_OSERR;
539
517
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
541
519
if(errno != EINTR){
542
error_plus(0, errno, "write");
520
error(0, errno, "write");
543
521
status = EX_OSERR;
576
554
if(fifo_fd != -1){
577
555
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
578
556
if(ret == -1 and errno != EINTR){
579
error_plus(0, errno, "close");
557
error(0, errno, "close");
585
563
if(outfifo_fd != -1){
586
564
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
588
error_plus(0, errno, "close");
566
error(0, errno, "close");
593
571
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
594
572
* sizeof(char *)); /* Count args */
595
573
if(cmdline_argv == NULL){
596
error_plus(0, errno, "malloc");
574
error(0, errno, "malloc");
599
577
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
614
592
the real user ID (_mandos) */
615
593
ret = setuid(geteuid());
617
error_plus(0, errno, "setuid");
595
error(0, errno, "setuid");
621
599
ret = chdir("/");
623
error_plus(0, errno, "chdir");
601
error(0, errno, "chdir");
626
604
/* if(fork() != 0){ */
629
607
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
631
error_plus(0, errno, "dup2");
609
error(0, errno, "dup2");
635
613
execv(usplash_name, cmdline_argv);
636
614
if(not interrupted_by_signal){
637
error_plus(0, errno, "execv");
615
error(0, errno, "execv");
640
618
free(cmdline_argv);
646
624
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
647
625
if(errno != EINTR){
648
error_plus(0, errno, "usplash_write");
626
error(0, errno, "usplash_write");
653
631
if(fifo_fd != -1){
654
632
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
655
633
if(ret == -1 and errno != EINTR){
656
error_plus(0, errno, "close");
634
error(0, errno, "close");
664
642
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
665
643
&signal_action, NULL));
667
error_plus(0, errno, "sigaction");
645
error(0, errno, "sigaction");
670
648
ret = raise(signal_received);
671
649
} while(ret != 0 and errno == EINTR);
673
error_plus(0, errno, "raise");
651
error(0, errno, "raise");
676
654
TEMP_FAILURE_RETRY(pause());