3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008-2016 Teddy Hogeborn
6
* Copyright © 2008-2016 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
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() */
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(),
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
41
40
#include <unistd.h> /* close(), write(), readlink(),
42
41
read(), STDOUT_FILENO, sleep(),
43
42
fork(), setuid(), geteuid(),
51
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
52
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
52
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
56
54
sig_atomic_t interrupted_by_signal = 0;
57
55
int signal_received;
58
56
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
58
static void termination_handler(int signum){
85
59
if(interrupted_by_signal){
181
155
size_t cmdline_len = 0;
182
156
DIR *proc_dir = opendir("/proc");
183
157
if(proc_dir == NULL){
184
error_plus(0, errno, "opendir");
158
error(0, errno, "opendir");
210
184
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
212
error_plus(0, errno, "asprintf");
186
error(0, errno, "asprintf");
213
187
goto fail_find_usplash;
252
226
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
253
227
proc_ent->d_name);
255
error_plus(0, errno, "asprintf");
229
error(0, errno, "asprintf");
256
230
goto fail_find_usplash;
258
232
cl_fd = open(cmdline_filename, O_RDONLY);
259
233
free(cmdline_filename);
261
error_plus(0, errno, "open");
235
error(0, errno, "open");
262
236
goto fail_find_usplash;
270
244
if(cmdline_len + blocksize > cmdline_allocated){
271
245
tmp = realloc(cmdline, cmdline_allocated + blocksize);
273
error_plus(0, errno, "realloc");
247
error(0, errno, "realloc");
275
249
goto fail_find_usplash;
281
255
sret = read(cl_fd, cmdline + cmdline_len,
282
256
cmdline_allocated - cmdline_len);
284
error_plus(0, errno, "read");
258
error(0, errno, "read");
286
260
goto fail_find_usplash;
289
263
} while(sret != 0);
290
264
ret = close(cl_fd);
292
error_plus(0, errno, "close");
266
error(0, errno, "close");
293
267
goto fail_find_usplash;
296
270
/* Close directory */
297
271
ret = closedir(proc_dir);
299
error_plus(0, errno, "closedir");
273
error(0, errno, "closedir");
300
274
goto fail_find_usplash;
351
325
sigemptyset(&new_action.sa_mask);
352
326
ret = sigaddset(&new_action.sa_mask, SIGINT);
354
error_plus(0, errno, "sigaddset");
328
error(0, errno, "sigaddset");
355
329
status = EX_OSERR;
358
332
ret = sigaddset(&new_action.sa_mask, SIGHUP);
360
error_plus(0, errno, "sigaddset");
334
error(0, errno, "sigaddset");
361
335
status = EX_OSERR;
364
338
ret = sigaddset(&new_action.sa_mask, SIGTERM);
366
error_plus(0, errno, "sigaddset");
340
error(0, errno, "sigaddset");
367
341
status = EX_OSERR;
370
344
ret = sigaction(SIGINT, NULL, &old_action);
372
346
if(errno != EINTR){
373
error_plus(0, errno, "sigaction");
347
error(0, errno, "sigaction");
374
348
status = EX_OSERR;
379
353
ret = sigaction(SIGINT, &new_action, NULL);
381
355
if(errno != EINTR){
382
error_plus(0, errno, "sigaction");
356
error(0, errno, "sigaction");
383
357
status = EX_OSERR;
388
362
ret = sigaction(SIGHUP, NULL, &old_action);
390
364
if(errno != EINTR){
391
error_plus(0, errno, "sigaction");
365
error(0, errno, "sigaction");
392
366
status = EX_OSERR;
397
371
ret = sigaction(SIGHUP, &new_action, NULL);
399
373
if(errno != EINTR){
400
error_plus(0, errno, "sigaction");
374
error(0, errno, "sigaction");
401
375
status = EX_OSERR;
406
380
ret = sigaction(SIGTERM, NULL, &old_action);
408
382
if(errno != EINTR){
409
error_plus(0, errno, "sigaction");
383
error(0, errno, "sigaction");
410
384
status = EX_OSERR;
415
389
ret = sigaction(SIGTERM, &new_action, NULL);
417
391
if(errno != EINTR){
418
error_plus(0, errno, "sigaction");
392
error(0, errno, "sigaction");
419
393
status = EX_OSERR;
427
401
/* Write command to FIFO */
428
402
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
429
403
if(errno != EINTR){
430
error_plus(0, errno, "usplash_write");
404
error(0, errno, "usplash_write");
431
405
status = EX_OSERR;
440
414
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
441
415
if(errno != EINTR){
442
error_plus(0, errno, "usplash_write");
416
error(0, errno, "usplash_write");
443
417
status = EX_OSERR;
476
450
char *tmp = realloc(buf, buf_allocated + blocksize);
478
452
if(errno != EINTR){
479
error_plus(0, errno, "realloc");
453
error(0, errno, "realloc");
480
454
status = EX_OSERR;
488
462
buf_allocated - buf_len);
490
464
if(errno != EINTR){
491
error_plus(0, errno, "read");
465
error(0, errno, "read");
492
466
status = EX_OSERR;
468
TEMP_FAILURE_RETRY(close(outfifo_fd));
497
471
if(interrupted_by_signal){
517
491
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
518
492
if(errno != EINTR){
519
error_plus(0, errno, "usplash_write");
493
error(0, errno, "usplash_write");
520
494
status = EX_OSERR;
543
517
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
545
519
if(errno != EINTR){
546
error_plus(0, errno, "write");
520
error(0, errno, "write");
547
521
status = EX_OSERR;
580
554
if(fifo_fd != -1){
581
ret = close(fifo_fd);
555
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
556
if(ret == -1 and errno != EINTR){
583
error_plus(0, errno, "close");
557
error(0, errno, "close");
588
562
/* Close output FIFO */
589
563
if(outfifo_fd != -1){
590
ret = close(outfifo_fd);
564
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
592
error_plus(0, errno, "close");
566
error(0, errno, "close");
597
571
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
598
572
* sizeof(char *)); /* Count args */
599
573
if(cmdline_argv == NULL){
600
error_plus(0, errno, "malloc");
574
error(0, errno, "malloc");
603
577
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
618
592
the real user ID (_mandos) */
619
593
ret = setuid(geteuid());
621
error_plus(0, errno, "setuid");
595
error(0, errno, "setuid");
625
599
ret = chdir("/");
627
error_plus(0, errno, "chdir");
601
error(0, errno, "chdir");
630
604
/* if(fork() != 0){ */
633
607
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
635
error_plus(0, errno, "dup2");
609
error(0, errno, "dup2");
639
613
execv(usplash_name, cmdline_argv);
640
614
if(not interrupted_by_signal){
641
error_plus(0, errno, "execv");
615
error(0, errno, "execv");
644
618
free(cmdline_argv);
650
624
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
651
625
if(errno != EINTR){
652
error_plus(0, errno, "usplash_write");
626
error(0, errno, "usplash_write");
656
630
/* Close FIFO (again) */
657
631
if(fifo_fd != -1){
658
ret = close(fifo_fd);
632
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
659
633
if(ret == -1 and errno != EINTR){
660
error_plus(0, errno, "close");
634
error(0, errno, "close");
668
642
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
669
643
&signal_action, NULL));
671
error_plus(0, errno, "sigaction");
645
error(0, errno, "sigaction");
674
648
ret = raise(signal_received);
675
649
} while(ret != 0 and errno == EINTR);
677
error_plus(0, errno, "raise");
651
error(0, errno, "raise");
680
654
TEMP_FAILURE_RETRY(pause());