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
8
* This program is free software: you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License as
10
* published by the Free Software Foundation, either version 3 of the
11
* License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
5
* Copyright © 2008-2018 Teddy Hogeborn
6
* Copyright © 2008-2018 Björn Påhlsson
8
* This file is part of Mandos.
10
* Mandos is free software: you can redistribute it and/or modify it
11
* under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* Mandos is distributed in the hope that it will be useful, but
14
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
18
* General Public License for more details.
18
20
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see
20
* <http://www.gnu.org/licenses/>.
21
* along with Mandos. If not, see <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
23
* Contact the authors at <mandos@recompile.se>.
25
26
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
35
36
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
37
38
#include <stddef.h> /* NULL */
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
39
#include <string.h> /* strlen(), memcmp(), strerror() */
40
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
40
42
#include <unistd.h> /* close(), write(), readlink(),
41
43
read(), STDOUT_FILENO, sleep(),
42
44
fork(), setuid(), geteuid(),
50
52
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
53
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
54
#include <argz.h> /* argz_count(), argz_extract() */
55
#include <stdarg.h> /* va_list, va_start(), ... */
54
57
sig_atomic_t interrupted_by_signal = 0;
55
58
int signal_received;
56
59
const char usplash_name[] = "/sbin/usplash";
61
/* Function to use when printing errors */
62
__attribute__((format (gnu_printf, 3, 4)))
63
void error_plus(int status, int errnum, const char *formatstring,
69
va_start(ap, formatstring);
70
ret = vasprintf(&text, formatstring, ap);
72
fprintf(stderr, "Mandos plugin %s: ",
73
program_invocation_short_name);
74
vfprintf(stderr, formatstring, ap);
75
fprintf(stderr, ": ");
76
fprintf(stderr, "%s\n", strerror(errnum));
77
error(status, errno, "vasprintf while printing error");
80
fprintf(stderr, "Mandos plugin ");
81
error(status, errnum, "%s", text);
58
85
static void termination_handler(int signum){
59
86
if(interrupted_by_signal){
155
182
size_t cmdline_len = 0;
156
183
DIR *proc_dir = opendir("/proc");
157
184
if(proc_dir == NULL){
158
error(0, errno, "opendir");
185
error_plus(0, errno, "opendir");
184
211
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
186
error(0, errno, "asprintf");
213
error_plus(0, errno, "asprintf");
187
214
goto fail_find_usplash;
226
253
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
254
proc_ent->d_name);
229
error(0, errno, "asprintf");
256
error_plus(0, errno, "asprintf");
230
257
goto fail_find_usplash;
232
259
cl_fd = open(cmdline_filename, O_RDONLY);
233
260
free(cmdline_filename);
235
error(0, errno, "open");
262
error_plus(0, errno, "open");
236
263
goto fail_find_usplash;
244
271
if(cmdline_len + blocksize > cmdline_allocated){
245
272
tmp = realloc(cmdline, cmdline_allocated + blocksize);
247
error(0, errno, "realloc");
274
error_plus(0, errno, "realloc");
249
276
goto fail_find_usplash;
255
282
sret = read(cl_fd, cmdline + cmdline_len,
256
283
cmdline_allocated - cmdline_len);
258
error(0, errno, "read");
285
error_plus(0, errno, "read");
260
287
goto fail_find_usplash;
263
290
} while(sret != 0);
264
291
ret = close(cl_fd);
266
error(0, errno, "close");
293
error_plus(0, errno, "close");
267
294
goto fail_find_usplash;
270
297
/* Close directory */
271
298
ret = closedir(proc_dir);
273
error(0, errno, "closedir");
300
error_plus(0, errno, "closedir");
274
301
goto fail_find_usplash;
325
352
sigemptyset(&new_action.sa_mask);
326
353
ret = sigaddset(&new_action.sa_mask, SIGINT);
328
error(0, errno, "sigaddset");
355
error_plus(0, errno, "sigaddset");
329
356
status = EX_OSERR;
332
359
ret = sigaddset(&new_action.sa_mask, SIGHUP);
334
error(0, errno, "sigaddset");
361
error_plus(0, errno, "sigaddset");
335
362
status = EX_OSERR;
338
365
ret = sigaddset(&new_action.sa_mask, SIGTERM);
340
error(0, errno, "sigaddset");
367
error_plus(0, errno, "sigaddset");
341
368
status = EX_OSERR;
344
371
ret = sigaction(SIGINT, NULL, &old_action);
346
373
if(errno != EINTR){
347
error(0, errno, "sigaction");
374
error_plus(0, errno, "sigaction");
348
375
status = EX_OSERR;
353
380
ret = sigaction(SIGINT, &new_action, NULL);
355
382
if(errno != EINTR){
356
error(0, errno, "sigaction");
383
error_plus(0, errno, "sigaction");
357
384
status = EX_OSERR;
362
389
ret = sigaction(SIGHUP, NULL, &old_action);
364
391
if(errno != EINTR){
365
error(0, errno, "sigaction");
392
error_plus(0, errno, "sigaction");
366
393
status = EX_OSERR;
371
398
ret = sigaction(SIGHUP, &new_action, NULL);
373
400
if(errno != EINTR){
374
error(0, errno, "sigaction");
401
error_plus(0, errno, "sigaction");
375
402
status = EX_OSERR;
380
407
ret = sigaction(SIGTERM, NULL, &old_action);
382
409
if(errno != EINTR){
383
error(0, errno, "sigaction");
410
error_plus(0, errno, "sigaction");
384
411
status = EX_OSERR;
389
416
ret = sigaction(SIGTERM, &new_action, NULL);
391
418
if(errno != EINTR){
392
error(0, errno, "sigaction");
419
error_plus(0, errno, "sigaction");
393
420
status = EX_OSERR;
401
428
/* Write command to FIFO */
402
429
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
430
if(errno != EINTR){
404
error(0, errno, "usplash_write");
431
error_plus(0, errno, "usplash_write");
405
432
status = EX_OSERR;
414
441
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
442
if(errno != EINTR){
416
error(0, errno, "usplash_write");
443
error_plus(0, errno, "usplash_write");
417
444
status = EX_OSERR;
450
477
char *tmp = realloc(buf, buf_allocated + blocksize);
452
479
if(errno != EINTR){
453
error(0, errno, "realloc");
480
error_plus(0, errno, "realloc");
454
481
status = EX_OSERR;
462
489
buf_allocated - buf_len);
464
491
if(errno != EINTR){
465
error(0, errno, "read");
492
error_plus(0, errno, "read");
466
493
status = EX_OSERR;
468
TEMP_FAILURE_RETRY(close(outfifo_fd));
471
498
if(interrupted_by_signal){
491
518
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
519
if(errno != EINTR){
493
error(0, errno, "usplash_write");
520
error_plus(0, errno, "usplash_write");
494
521
status = EX_OSERR;
517
544
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
519
546
if(errno != EINTR){
520
error(0, errno, "write");
547
error_plus(0, errno, "write");
521
548
status = EX_OSERR;
554
581
if(fifo_fd != -1){
555
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
ret = close(fifo_fd);
556
583
if(ret == -1 and errno != EINTR){
557
error(0, errno, "close");
584
error_plus(0, errno, "close");
562
589
/* Close output FIFO */
563
590
if(outfifo_fd != -1){
564
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
591
ret = close(outfifo_fd);
566
error(0, errno, "close");
593
error_plus(0, errno, "close");
571
598
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
599
* sizeof(char *)); /* Count args */
573
600
if(cmdline_argv == NULL){
574
error(0, errno, "malloc");
601
error_plus(0, errno, "malloc");
577
604
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
592
619
the real user ID (_mandos) */
593
620
ret = setuid(geteuid());
595
error(0, errno, "setuid");
622
error_plus(0, errno, "setuid");
599
626
ret = chdir("/");
601
error(0, errno, "chdir");
628
error_plus(0, errno, "chdir");
604
631
/* if(fork() != 0){ */
607
634
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
609
error(0, errno, "dup2");
636
error_plus(0, errno, "dup2");
613
640
execv(usplash_name, cmdline_argv);
614
641
if(not interrupted_by_signal){
615
error(0, errno, "execv");
642
error_plus(0, errno, "execv");
618
645
free(cmdline_argv);
624
651
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
652
if(errno != EINTR){
626
error(0, errno, "usplash_write");
653
error_plus(0, errno, "usplash_write");
630
657
/* Close FIFO (again) */
631
658
if(fifo_fd != -1){
632
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
659
ret = close(fifo_fd);
633
660
if(ret == -1 and errno != EINTR){
634
error(0, errno, "close");
661
error_plus(0, errno, "close");
642
669
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
670
&signal_action, NULL));
645
error(0, errno, "sigaction");
672
error_plus(0, errno, "sigaction");
648
675
ret = raise(signal_received);
649
676
} while(ret != 0 and errno == EINTR);
651
error(0, errno, "raise");
678
error_plus(0, errno, "raise");
654
681
TEMP_FAILURE_RETRY(pause());