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
5
* Copyright © 2008-2016 Teddy Hogeborn
6
* Copyright © 2008-2016 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@fukt.bsnet.se>.
22
* Contact the authors at <mandos@recompile.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() */
39
#include <stdio.h> /* asprintf()*/
38
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(),
40
41
#include <unistd.h> /* close(), write(), readlink(),
41
42
read(), STDOUT_FILENO, sleep(),
42
43
fork(), setuid(), geteuid(),
49
50
#include <inttypes.h> /* intmax_t, strtoimax() */
50
51
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
53
56
sig_atomic_t interrupted_by_signal = 0;
54
57
int signal_received;
55
58
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);
57
84
static void termination_handler(int signum){
58
85
if(interrupted_by_signal){
154
181
size_t cmdline_len = 0;
155
182
DIR *proc_dir = opendir("/proc");
156
183
if(proc_dir == NULL){
157
error(0, errno, "opendir");
184
error_plus(0, errno, "opendir");
183
210
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
error(0, errno, "asprintf");
212
error_plus(0, errno, "asprintf");
186
213
goto fail_find_usplash;
225
252
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
226
253
proc_ent->d_name);
228
error(0, errno, "asprintf");
255
error_plus(0, errno, "asprintf");
229
256
goto fail_find_usplash;
231
258
cl_fd = open(cmdline_filename, O_RDONLY);
232
259
free(cmdline_filename);
234
error(0, errno, "open");
261
error_plus(0, errno, "open");
235
262
goto fail_find_usplash;
243
270
if(cmdline_len + blocksize > cmdline_allocated){
244
271
tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
error(0, errno, "realloc");
273
error_plus(0, errno, "realloc");
248
275
goto fail_find_usplash;
254
281
sret = read(cl_fd, cmdline + cmdline_len,
255
282
cmdline_allocated - cmdline_len);
257
error(0, errno, "read");
284
error_plus(0, errno, "read");
259
286
goto fail_find_usplash;
262
289
} while(sret != 0);
263
290
ret = close(cl_fd);
265
error(0, errno, "close");
292
error_plus(0, errno, "close");
266
293
goto fail_find_usplash;
269
296
/* Close directory */
270
297
ret = closedir(proc_dir);
272
error(0, errno, "closedir");
299
error_plus(0, errno, "closedir");
273
300
goto fail_find_usplash;
324
351
sigemptyset(&new_action.sa_mask);
325
352
ret = sigaddset(&new_action.sa_mask, SIGINT);
327
error(0, errno, "sigaddset");
354
error_plus(0, errno, "sigaddset");
328
355
status = EX_OSERR;
331
358
ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
error(0, errno, "sigaddset");
360
error_plus(0, errno, "sigaddset");
334
361
status = EX_OSERR;
337
364
ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
error(0, errno, "sigaddset");
366
error_plus(0, errno, "sigaddset");
340
367
status = EX_OSERR;
343
370
ret = sigaction(SIGINT, NULL, &old_action);
345
372
if(errno != EINTR){
346
error(0, errno, "sigaction");
373
error_plus(0, errno, "sigaction");
347
374
status = EX_OSERR;
352
379
ret = sigaction(SIGINT, &new_action, NULL);
354
381
if(errno != EINTR){
355
error(0, errno, "sigaction");
382
error_plus(0, errno, "sigaction");
356
383
status = EX_OSERR;
361
388
ret = sigaction(SIGHUP, NULL, &old_action);
363
390
if(errno != EINTR){
364
error(0, errno, "sigaction");
391
error_plus(0, errno, "sigaction");
365
392
status = EX_OSERR;
370
397
ret = sigaction(SIGHUP, &new_action, NULL);
372
399
if(errno != EINTR){
373
error(0, errno, "sigaction");
400
error_plus(0, errno, "sigaction");
374
401
status = EX_OSERR;
379
406
ret = sigaction(SIGTERM, NULL, &old_action);
381
408
if(errno != EINTR){
382
error(0, errno, "sigaction");
409
error_plus(0, errno, "sigaction");
383
410
status = EX_OSERR;
388
415
ret = sigaction(SIGTERM, &new_action, NULL);
390
417
if(errno != EINTR){
391
error(0, errno, "sigaction");
418
error_plus(0, errno, "sigaction");
392
419
status = EX_OSERR;
400
427
/* Write command to FIFO */
401
428
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
402
429
if(errno != EINTR){
403
error(0, errno, "usplash_write");
430
error_plus(0, errno, "usplash_write");
404
431
status = EX_OSERR;
413
440
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
414
441
if(errno != EINTR){
415
error(0, errno, "usplash_write");
442
error_plus(0, errno, "usplash_write");
416
443
status = EX_OSERR;
449
476
char *tmp = realloc(buf, buf_allocated + blocksize);
451
478
if(errno != EINTR){
452
error(0, errno, "realloc");
479
error_plus(0, errno, "realloc");
453
480
status = EX_OSERR;
461
488
buf_allocated - buf_len);
463
490
if(errno != EINTR){
464
error(0, errno, "read");
491
error_plus(0, errno, "read");
465
492
status = EX_OSERR;
467
TEMP_FAILURE_RETRY(close(outfifo_fd));
470
497
if(interrupted_by_signal){
490
517
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
491
518
if(errno != EINTR){
492
error(0, errno, "usplash_write");
519
error_plus(0, errno, "usplash_write");
493
520
status = EX_OSERR;
516
543
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
545
if(errno != EINTR){
519
error(0, errno, "write");
546
error_plus(0, errno, "write");
520
547
status = EX_OSERR;
553
580
if(fifo_fd != -1){
554
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
581
ret = close(fifo_fd);
555
582
if(ret == -1 and errno != EINTR){
556
error(0, errno, "close");
583
error_plus(0, errno, "close");
561
588
/* Close output FIFO */
562
589
if(outfifo_fd != -1){
563
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
590
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;
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 */
590
605
/* Kill old usplash */
591
606
kill(usplash_pid, SIGTERM);
603
618
the real user ID (_mandos) */
604
619
ret = setuid(geteuid());
606
error(0, errno, "setuid");
621
error_plus(0, errno, "setuid");
610
625
ret = chdir("/");
612
error(0, errno, "chdir");
627
error_plus(0, errno, "chdir");
615
630
/* if(fork() != 0){ */
618
633
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
620
error(0, errno, "dup2");
635
error_plus(0, errno, "dup2");
624
639
execv(usplash_name, cmdline_argv);
625
640
if(not interrupted_by_signal){
626
error(0, errno, "execv");
641
error_plus(0, errno, "execv");
629
644
free(cmdline_argv);
635
650
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
636
651
if(errno != EINTR){
637
error(0, errno, "usplash_write");
652
error_plus(0, errno, "usplash_write");
641
656
/* Close FIFO (again) */
642
657
if(fifo_fd != -1){
643
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
658
ret = close(fifo_fd);
644
659
if(ret == -1 and errno != EINTR){
645
error(0, errno, "close");
660
error_plus(0, errno, "close");
653
668
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
654
669
&signal_action, NULL));
656
error(0, errno, "sigaction");
671
error_plus(0, errno, "sigaction");
659
674
ret = raise(signal_received);
660
675
} while(ret != 0 and errno == EINTR);
662
error(0, errno, "raise");
677
error_plus(0, errno, "raise");
665
680
TEMP_FAILURE_RETRY(pause());