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,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 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(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(), fprintf() */
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
40
40
#include <unistd.h> /* close(), write(), readlink(),
41
41
read(), STDOUT_FILENO, sleep(),
42
42
fork(), setuid(), geteuid(),
49
49
#include <inttypes.h> /* intmax_t, strtoimax() */
50
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
#include <argz.h> /* argz_count(), argz_extract() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
55
53
sig_atomic_t interrupted_by_signal = 0;
56
54
int signal_received;
57
55
const char usplash_name[] = "/sbin/usplash";
59
/* Function to use when printing errors */
60
void error_plus(int status, int errnum, const char *formatstring, ...){
65
va_start(ap, formatstring);
66
ret = vasprintf(&text, formatstring, ap);
68
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
69
vfprintf(stderr, formatstring, ap);
70
fprintf(stderr, ": ");
71
fprintf(stderr, "%s\n", strerror(errnum));
72
error(status, errno, "vasprintf while printing error");
75
fprintf(stderr, "Mandos plugin ");
76
error(status, errnum, "%s", text);
80
57
static void termination_handler(int signum){
81
58
if(interrupted_by_signal){
177
154
size_t cmdline_len = 0;
178
155
DIR *proc_dir = opendir("/proc");
179
156
if(proc_dir == NULL){
180
error_plus(0, errno, "opendir");
157
error(0, errno, "opendir");
206
183
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
208
error_plus(0, errno, "asprintf");
185
error(0, errno, "asprintf");
209
186
goto fail_find_usplash;
248
225
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
249
226
proc_ent->d_name);
251
error_plus(0, errno, "asprintf");
228
error(0, errno, "asprintf");
252
229
goto fail_find_usplash;
254
231
cl_fd = open(cmdline_filename, O_RDONLY);
255
232
free(cmdline_filename);
257
error_plus(0, errno, "open");
234
error(0, errno, "open");
258
235
goto fail_find_usplash;
266
243
if(cmdline_len + blocksize > cmdline_allocated){
267
244
tmp = realloc(cmdline, cmdline_allocated + blocksize);
269
error_plus(0, errno, "realloc");
246
error(0, errno, "realloc");
271
248
goto fail_find_usplash;
277
254
sret = read(cl_fd, cmdline + cmdline_len,
278
255
cmdline_allocated - cmdline_len);
280
error_plus(0, errno, "read");
257
error(0, errno, "read");
282
259
goto fail_find_usplash;
285
262
} while(sret != 0);
286
263
ret = close(cl_fd);
288
error_plus(0, errno, "close");
265
error(0, errno, "close");
289
266
goto fail_find_usplash;
292
269
/* Close directory */
293
270
ret = closedir(proc_dir);
295
error_plus(0, errno, "closedir");
272
error(0, errno, "closedir");
296
273
goto fail_find_usplash;
347
324
sigemptyset(&new_action.sa_mask);
348
325
ret = sigaddset(&new_action.sa_mask, SIGINT);
350
error_plus(0, errno, "sigaddset");
327
error(0, errno, "sigaddset");
351
328
status = EX_OSERR;
354
331
ret = sigaddset(&new_action.sa_mask, SIGHUP);
356
error_plus(0, errno, "sigaddset");
333
error(0, errno, "sigaddset");
357
334
status = EX_OSERR;
360
337
ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
error_plus(0, errno, "sigaddset");
339
error(0, errno, "sigaddset");
363
340
status = EX_OSERR;
366
343
ret = sigaction(SIGINT, NULL, &old_action);
368
345
if(errno != EINTR){
369
error_plus(0, errno, "sigaction");
346
error(0, errno, "sigaction");
370
347
status = EX_OSERR;
375
352
ret = sigaction(SIGINT, &new_action, NULL);
377
354
if(errno != EINTR){
378
error_plus(0, errno, "sigaction");
355
error(0, errno, "sigaction");
379
356
status = EX_OSERR;
384
361
ret = sigaction(SIGHUP, NULL, &old_action);
386
363
if(errno != EINTR){
387
error_plus(0, errno, "sigaction");
364
error(0, errno, "sigaction");
388
365
status = EX_OSERR;
393
370
ret = sigaction(SIGHUP, &new_action, NULL);
395
372
if(errno != EINTR){
396
error_plus(0, errno, "sigaction");
373
error(0, errno, "sigaction");
397
374
status = EX_OSERR;
402
379
ret = sigaction(SIGTERM, NULL, &old_action);
404
381
if(errno != EINTR){
405
error_plus(0, errno, "sigaction");
382
error(0, errno, "sigaction");
406
383
status = EX_OSERR;
411
388
ret = sigaction(SIGTERM, &new_action, NULL);
413
390
if(errno != EINTR){
414
error_plus(0, errno, "sigaction");
391
error(0, errno, "sigaction");
415
392
status = EX_OSERR;
423
400
/* Write command to FIFO */
424
401
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
425
402
if(errno != EINTR){
426
error_plus(0, errno, "usplash_write");
403
error(0, errno, "usplash_write");
427
404
status = EX_OSERR;
436
413
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
437
414
if(errno != EINTR){
438
error_plus(0, errno, "usplash_write");
415
error(0, errno, "usplash_write");
439
416
status = EX_OSERR;
472
449
char *tmp = realloc(buf, buf_allocated + blocksize);
474
451
if(errno != EINTR){
475
error_plus(0, errno, "realloc");
452
error(0, errno, "realloc");
476
453
status = EX_OSERR;
513
490
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
514
491
if(errno != EINTR){
515
error_plus(0, errno, "usplash_write");
492
error(0, errno, "usplash_write");
516
493
status = EX_OSERR;
539
516
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
541
518
if(errno != EINTR){
542
error_plus(0, errno, "write");
519
error(0, errno, "write");
543
520
status = EX_OSERR;
576
553
if(fifo_fd != -1){
577
554
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
578
555
if(ret == -1 and errno != EINTR){
579
error_plus(0, errno, "close");
556
error(0, errno, "close");
585
562
if(outfifo_fd != -1){
586
563
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
588
error_plus(0, errno, "close");
592
/* Create argv for new usplash*/
593
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
594
* sizeof(char *)); /* Count args */
595
if(cmdline_argv == NULL){
596
error_plus(0, errno, "malloc");
599
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
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;
601
590
/* Kill old usplash */
602
591
kill(usplash_pid, SIGTERM);
614
603
the real user ID (_mandos) */
615
604
ret = setuid(geteuid());
617
error_plus(0, errno, "setuid");
606
error(0, errno, "setuid");
621
610
ret = chdir("/");
623
error_plus(0, errno, "chdir");
612
error(0, errno, "chdir");
626
615
/* if(fork() != 0){ */
629
618
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
631
error_plus(0, errno, "dup2");
620
error(0, errno, "dup2");
635
624
execv(usplash_name, cmdline_argv);
636
625
if(not interrupted_by_signal){
637
error_plus(0, errno, "execv");
626
error(0, errno, "execv");
640
629
free(cmdline_argv);
646
635
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
647
636
if(errno != EINTR){
648
error_plus(0, errno, "usplash_write");
637
error(0, errno, "usplash_write");
653
642
if(fifo_fd != -1){
654
643
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
655
644
if(ret == -1 and errno != EINTR){
656
error_plus(0, errno, "close");
645
error(0, errno, "close");
664
653
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
665
654
&signal_action, NULL));
667
error_plus(0, errno, "sigaction");
656
error(0, errno, "sigaction");
670
659
ret = raise(signal_received);
671
660
} while(ret != 0 and errno == EINTR);
673
error_plus(0, errno, "raise");
662
error(0, errno, "raise");
676
665
TEMP_FAILURE_RETRY(pause());