3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 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-2017 Teddy Hogeborn
6
* Copyright © 2008-2017 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(),
49
51
#include <inttypes.h> /* intmax_t, strtoimax() */
50
52
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
53
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
54
#include <argz.h> /* argz_count(), argz_extract() */
55
#include <stdarg.h> /* va_list, va_start(), ... */
53
57
sig_atomic_t interrupted_by_signal = 0;
54
58
int signal_received;
55
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);
57
85
static void termination_handler(int signum){
58
86
if(interrupted_by_signal){
154
182
size_t cmdline_len = 0;
155
183
DIR *proc_dir = opendir("/proc");
156
184
if(proc_dir == NULL){
157
error(0, errno, "opendir");
185
error_plus(0, errno, "opendir");
183
211
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
error(0, errno, "asprintf");
213
error_plus(0, errno, "asprintf");
186
214
goto fail_find_usplash;
225
253
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
226
254
proc_ent->d_name);
228
error(0, errno, "asprintf");
256
error_plus(0, errno, "asprintf");
229
257
goto fail_find_usplash;
231
259
cl_fd = open(cmdline_filename, O_RDONLY);
232
260
free(cmdline_filename);
234
error(0, errno, "open");
262
error_plus(0, errno, "open");
235
263
goto fail_find_usplash;
243
271
if(cmdline_len + blocksize > cmdline_allocated){
244
272
tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
error(0, errno, "realloc");
274
error_plus(0, errno, "realloc");
248
276
goto fail_find_usplash;
254
282
sret = read(cl_fd, cmdline + cmdline_len,
255
283
cmdline_allocated - cmdline_len);
257
error(0, errno, "read");
285
error_plus(0, errno, "read");
259
287
goto fail_find_usplash;
262
290
} while(sret != 0);
263
291
ret = close(cl_fd);
265
error(0, errno, "close");
293
error_plus(0, errno, "close");
266
294
goto fail_find_usplash;
269
297
/* Close directory */
270
298
ret = closedir(proc_dir);
272
error(0, errno, "closedir");
300
error_plus(0, errno, "closedir");
273
301
goto fail_find_usplash;
324
352
sigemptyset(&new_action.sa_mask);
325
353
ret = sigaddset(&new_action.sa_mask, SIGINT);
327
error(0, errno, "sigaddset");
355
error_plus(0, errno, "sigaddset");
328
356
status = EX_OSERR;
331
359
ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
error(0, errno, "sigaddset");
361
error_plus(0, errno, "sigaddset");
334
362
status = EX_OSERR;
337
365
ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
error(0, errno, "sigaddset");
367
error_plus(0, errno, "sigaddset");
340
368
status = EX_OSERR;
343
371
ret = sigaction(SIGINT, NULL, &old_action);
345
373
if(errno != EINTR){
346
error(0, errno, "sigaction");
374
error_plus(0, errno, "sigaction");
347
375
status = EX_OSERR;
352
380
ret = sigaction(SIGINT, &new_action, NULL);
354
382
if(errno != EINTR){
355
error(0, errno, "sigaction");
383
error_plus(0, errno, "sigaction");
356
384
status = EX_OSERR;
361
389
ret = sigaction(SIGHUP, NULL, &old_action);
363
391
if(errno != EINTR){
364
error(0, errno, "sigaction");
392
error_plus(0, errno, "sigaction");
365
393
status = EX_OSERR;
370
398
ret = sigaction(SIGHUP, &new_action, NULL);
372
400
if(errno != EINTR){
373
error(0, errno, "sigaction");
401
error_plus(0, errno, "sigaction");
374
402
status = EX_OSERR;
379
407
ret = sigaction(SIGTERM, NULL, &old_action);
381
409
if(errno != EINTR){
382
error(0, errno, "sigaction");
410
error_plus(0, errno, "sigaction");
383
411
status = EX_OSERR;
388
416
ret = sigaction(SIGTERM, &new_action, NULL);
390
418
if(errno != EINTR){
391
error(0, errno, "sigaction");
419
error_plus(0, errno, "sigaction");
392
420
status = EX_OSERR;
400
428
/* Write command to FIFO */
401
429
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
402
430
if(errno != EINTR){
403
error(0, errno, "usplash_write");
431
error_plus(0, errno, "usplash_write");
404
432
status = EX_OSERR;
413
441
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
414
442
if(errno != EINTR){
415
error(0, errno, "usplash_write");
443
error_plus(0, errno, "usplash_write");
416
444
status = EX_OSERR;
449
477
char *tmp = realloc(buf, buf_allocated + blocksize);
451
479
if(errno != EINTR){
452
error(0, errno, "realloc");
480
error_plus(0, errno, "realloc");
453
481
status = EX_OSERR;
461
489
buf_allocated - buf_len);
463
491
if(errno != EINTR){
464
error(0, errno, "read");
492
error_plus(0, errno, "read");
465
493
status = EX_OSERR;
467
TEMP_FAILURE_RETRY(close(outfifo_fd));
470
498
if(interrupted_by_signal){
490
518
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
491
519
if(errno != EINTR){
492
error(0, errno, "usplash_write");
520
error_plus(0, errno, "usplash_write");
493
521
status = EX_OSERR;
516
544
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
546
if(errno != EINTR){
519
error(0, errno, "write");
547
error_plus(0, errno, "write");
520
548
status = EX_OSERR;
553
581
if(fifo_fd != -1){
554
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
ret = close(fifo_fd);
555
583
if(ret == -1 and errno != EINTR){
556
error(0, errno, "close");
584
error_plus(0, errno, "close");
561
589
/* Close output FIFO */
562
590
if(outfifo_fd != -1){
563
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
591
ret = close(outfifo_fd);
565
error(0, errno, "close");
569
/* Create argc and argv for new usplash*/
570
int cmdline_argc = 0;
571
char **cmdline_argv = malloc(sizeof(char *));
574
while(position < cmdline_len){
575
char **tmp = realloc(cmdline_argv,
577
* (size_t)(cmdline_argc + 2)));
579
error(0, errno, "realloc");
584
cmdline_argv[cmdline_argc] = cmdline + position;
586
position += strlen(cmdline + position) + 1;
588
cmdline_argv[cmdline_argc] = NULL;
593
error_plus(0, errno, "close");
597
/* Create argv for new usplash*/
598
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
599
* sizeof(char *)); /* Count args */
600
if(cmdline_argv == NULL){
601
error_plus(0, errno, "malloc");
604
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
590
606
/* Kill old usplash */
591
607
kill(usplash_pid, SIGTERM);
603
619
the real user ID (_mandos) */
604
620
ret = setuid(geteuid());
606
error(0, errno, "setuid");
622
error_plus(0, errno, "setuid");
610
626
ret = chdir("/");
612
error(0, errno, "chdir");
628
error_plus(0, errno, "chdir");
615
631
/* if(fork() != 0){ */
618
634
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
620
error(0, errno, "dup2");
636
error_plus(0, errno, "dup2");
624
640
execv(usplash_name, cmdline_argv);
625
641
if(not interrupted_by_signal){
626
error(0, errno, "execv");
642
error_plus(0, errno, "execv");
629
645
free(cmdline_argv);
635
651
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
636
652
if(errno != EINTR){
637
error(0, errno, "usplash_write");
653
error_plus(0, errno, "usplash_write");
641
657
/* Close FIFO (again) */
642
658
if(fifo_fd != -1){
643
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
659
ret = close(fifo_fd);
644
660
if(ret == -1 and errno != EINTR){
645
error(0, errno, "close");
661
error_plus(0, errno, "close");
653
669
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
654
670
&signal_action, NULL));
656
error(0, errno, "sigaction");
672
error_plus(0, errno, "sigaction");
659
675
ret = raise(signal_received);
660
676
} while(ret != 0 and errno == EINTR);
662
error(0, errno, "raise");
678
error_plus(0, errno, "raise");
665
681
TEMP_FAILURE_RETRY(pause());