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