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-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
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(),
50
51
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
54
56
sig_atomic_t interrupted_by_signal = 0;
55
57
int signal_received;
56
58
const char usplash_name[] = "/sbin/usplash";
60
/* Function to use when printing errors */
61
void error_plus(int status, int errnum, const char *formatstring,
67
va_start(ap, formatstring);
68
ret = vasprintf(&text, formatstring, ap);
70
fprintf(stderr, "Mandos plugin %s: ",
71
program_invocation_short_name);
72
vfprintf(stderr, formatstring, ap);
73
fprintf(stderr, ": ");
74
fprintf(stderr, "%s\n", strerror(errnum));
75
error(status, errno, "vasprintf while printing error");
78
fprintf(stderr, "Mandos plugin ");
79
error(status, errnum, "%s", text);
58
83
static void termination_handler(int signum){
59
84
if(interrupted_by_signal){
155
180
size_t cmdline_len = 0;
156
181
DIR *proc_dir = opendir("/proc");
157
182
if(proc_dir == NULL){
158
error(0, errno, "opendir");
183
error_plus(0, errno, "opendir");
184
209
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
186
error(0, errno, "asprintf");
211
error_plus(0, errno, "asprintf");
187
212
goto fail_find_usplash;
226
251
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
252
proc_ent->d_name);
229
error(0, errno, "asprintf");
254
error_plus(0, errno, "asprintf");
230
255
goto fail_find_usplash;
232
257
cl_fd = open(cmdline_filename, O_RDONLY);
233
258
free(cmdline_filename);
235
error(0, errno, "open");
260
error_plus(0, errno, "open");
236
261
goto fail_find_usplash;
244
269
if(cmdline_len + blocksize > cmdline_allocated){
245
270
tmp = realloc(cmdline, cmdline_allocated + blocksize);
247
error(0, errno, "realloc");
272
error_plus(0, errno, "realloc");
249
274
goto fail_find_usplash;
255
280
sret = read(cl_fd, cmdline + cmdline_len,
256
281
cmdline_allocated - cmdline_len);
258
error(0, errno, "read");
283
error_plus(0, errno, "read");
260
285
goto fail_find_usplash;
263
288
} while(sret != 0);
264
289
ret = close(cl_fd);
266
error(0, errno, "close");
291
error_plus(0, errno, "close");
267
292
goto fail_find_usplash;
270
295
/* Close directory */
271
296
ret = closedir(proc_dir);
273
error(0, errno, "closedir");
298
error_plus(0, errno, "closedir");
274
299
goto fail_find_usplash;
325
350
sigemptyset(&new_action.sa_mask);
326
351
ret = sigaddset(&new_action.sa_mask, SIGINT);
328
error(0, errno, "sigaddset");
353
error_plus(0, errno, "sigaddset");
329
354
status = EX_OSERR;
332
357
ret = sigaddset(&new_action.sa_mask, SIGHUP);
334
error(0, errno, "sigaddset");
359
error_plus(0, errno, "sigaddset");
335
360
status = EX_OSERR;
338
363
ret = sigaddset(&new_action.sa_mask, SIGTERM);
340
error(0, errno, "sigaddset");
365
error_plus(0, errno, "sigaddset");
341
366
status = EX_OSERR;
344
369
ret = sigaction(SIGINT, NULL, &old_action);
346
371
if(errno != EINTR){
347
error(0, errno, "sigaction");
372
error_plus(0, errno, "sigaction");
348
373
status = EX_OSERR;
353
378
ret = sigaction(SIGINT, &new_action, NULL);
355
380
if(errno != EINTR){
356
error(0, errno, "sigaction");
381
error_plus(0, errno, "sigaction");
357
382
status = EX_OSERR;
362
387
ret = sigaction(SIGHUP, NULL, &old_action);
364
389
if(errno != EINTR){
365
error(0, errno, "sigaction");
390
error_plus(0, errno, "sigaction");
366
391
status = EX_OSERR;
371
396
ret = sigaction(SIGHUP, &new_action, NULL);
373
398
if(errno != EINTR){
374
error(0, errno, "sigaction");
399
error_plus(0, errno, "sigaction");
375
400
status = EX_OSERR;
380
405
ret = sigaction(SIGTERM, NULL, &old_action);
382
407
if(errno != EINTR){
383
error(0, errno, "sigaction");
408
error_plus(0, errno, "sigaction");
384
409
status = EX_OSERR;
389
414
ret = sigaction(SIGTERM, &new_action, NULL);
391
416
if(errno != EINTR){
392
error(0, errno, "sigaction");
417
error_plus(0, errno, "sigaction");
393
418
status = EX_OSERR;
401
426
/* Write command to FIFO */
402
427
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
428
if(errno != EINTR){
404
error(0, errno, "usplash_write");
429
error_plus(0, errno, "usplash_write");
405
430
status = EX_OSERR;
414
439
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
440
if(errno != EINTR){
416
error(0, errno, "usplash_write");
441
error_plus(0, errno, "usplash_write");
417
442
status = EX_OSERR;
450
475
char *tmp = realloc(buf, buf_allocated + blocksize);
452
477
if(errno != EINTR){
453
error(0, errno, "realloc");
478
error_plus(0, errno, "realloc");
454
479
status = EX_OSERR;
491
516
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
517
if(errno != EINTR){
493
error(0, errno, "usplash_write");
518
error_plus(0, errno, "usplash_write");
494
519
status = EX_OSERR;
517
542
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
519
544
if(errno != EINTR){
520
error(0, errno, "write");
545
error_plus(0, errno, "write");
521
546
status = EX_OSERR;
554
579
if(fifo_fd != -1){
555
580
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
556
581
if(ret == -1 and errno != EINTR){
557
error(0, errno, "close");
582
error_plus(0, errno, "close");
563
588
if(outfifo_fd != -1){
564
589
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
566
error(0, errno, "close");
591
error_plus(0, errno, "close");
570
595
/* Create argv for new usplash*/
571
char **cmdline_argv = malloc(argz_count(cmdline, cmdline_len)
596
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
597
* sizeof(char *)); /* Count args */
573
598
if(cmdline_argv == NULL){
574
error(0, errno, "malloc");
599
error_plus(0, errno, "malloc");
577
602
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
592
617
the real user ID (_mandos) */
593
618
ret = setuid(geteuid());
595
error(0, errno, "setuid");
620
error_plus(0, errno, "setuid");
599
624
ret = chdir("/");
601
error(0, errno, "chdir");
626
error_plus(0, errno, "chdir");
604
629
/* if(fork() != 0){ */
607
632
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
609
error(0, errno, "dup2");
634
error_plus(0, errno, "dup2");
613
638
execv(usplash_name, cmdline_argv);
614
639
if(not interrupted_by_signal){
615
error(0, errno, "execv");
640
error_plus(0, errno, "execv");
618
643
free(cmdline_argv);
624
649
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
650
if(errno != EINTR){
626
error(0, errno, "usplash_write");
651
error_plus(0, errno, "usplash_write");
631
656
if(fifo_fd != -1){
632
657
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
633
658
if(ret == -1 and errno != EINTR){
634
error(0, errno, "close");
659
error_plus(0, errno, "close");
642
667
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
668
&signal_action, NULL));
645
error(0, errno, "sigaction");
670
error_plus(0, errno, "sigaction");
648
673
ret = raise(signal_received);
649
674
} while(ret != 0 and errno == EINTR);
651
error(0, errno, "raise");
676
error_plus(0, errno, "raise");
654
679
TEMP_FAILURE_RETRY(pause());