3
3
 * Usplash - Read a password from usplash and output it
 
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
 
 
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
 
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
16
 * General Public License for more details.
 
20
18
 * You should have received a copy of the GNU General Public License
 
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
 
19
 * along with this program.  If not, see
 
 
20
 * <http://www.gnu.org/licenses/>.
 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
26
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
 
 
36
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
38
37
#include <stddef.h>             /* NULL */
 
39
 
#include <string.h>             /* strlen(), memcmp(), strerror() */
 
40
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
 
 
38
#include <string.h>             /* strlen(), memcmp() */
 
 
39
#include <stdio.h>              /* asprintf()*/
 
42
40
#include <unistd.h>             /* close(), write(), readlink(),
 
43
41
                                   read(), STDOUT_FILENO, sleep(),
 
44
42
                                   fork(), setuid(), geteuid(),
 
 
51
49
#include <inttypes.h>           /* intmax_t, strtoimax() */
 
52
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
53
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
54
 
#include <argz.h>               /* argz_count(), argz_extract() */
 
55
 
#include <stdarg.h>             /* va_list, va_start(), ... */
 
57
53
sig_atomic_t interrupted_by_signal = 0;
 
58
54
int signal_received;
 
59
55
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);
 
85
57
static void termination_handler(int signum){
 
86
58
  if(interrupted_by_signal){
 
 
182
154
  size_t cmdline_len = 0;
 
183
155
  DIR *proc_dir = opendir("/proc");
 
184
156
  if(proc_dir == NULL){
 
185
 
    error_plus(0, errno, "opendir");
 
 
157
    error(0, errno, "opendir");
 
 
211
183
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
213
 
        error_plus(0, errno, "asprintf");
 
 
185
        error(0, errno, "asprintf");
 
214
186
        goto fail_find_usplash;
 
 
253
225
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
254
226
                       proc_ent->d_name);
 
256
 
          error_plus(0, errno, "asprintf");
 
 
228
          error(0, errno, "asprintf");
 
257
229
          goto fail_find_usplash;
 
259
231
        cl_fd = open(cmdline_filename, O_RDONLY);
 
260
232
        free(cmdline_filename);
 
262
 
          error_plus(0, errno, "open");
 
 
234
          error(0, errno, "open");
 
263
235
          goto fail_find_usplash;
 
 
271
243
        if(cmdline_len + blocksize > cmdline_allocated){
 
272
244
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
 
274
 
            error_plus(0, errno, "realloc");
 
 
246
            error(0, errno, "realloc");
 
276
248
            goto fail_find_usplash;
 
 
282
254
        sret = read(cl_fd, cmdline + cmdline_len,
 
283
255
                    cmdline_allocated - cmdline_len);
 
285
 
          error_plus(0, errno, "read");
 
 
257
          error(0, errno, "read");
 
287
259
          goto fail_find_usplash;
 
 
290
262
      } while(sret != 0);
 
291
263
      ret = close(cl_fd);
 
293
 
        error_plus(0, errno, "close");
 
 
265
        error(0, errno, "close");
 
294
266
        goto fail_find_usplash;
 
297
269
    /* Close directory */
 
298
270
    ret = closedir(proc_dir);
 
300
 
      error_plus(0, errno, "closedir");
 
 
272
      error(0, errno, "closedir");
 
301
273
      goto fail_find_usplash;
 
 
352
324
    sigemptyset(&new_action.sa_mask);
 
353
325
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
355
 
      error_plus(0, errno, "sigaddset");
 
 
327
      error(0, errno, "sigaddset");
 
356
328
      status = EX_OSERR;
 
359
331
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
361
 
      error_plus(0, errno, "sigaddset");
 
 
333
      error(0, errno, "sigaddset");
 
362
334
      status = EX_OSERR;
 
365
337
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
367
 
      error_plus(0, errno, "sigaddset");
 
 
339
      error(0, errno, "sigaddset");
 
368
340
      status = EX_OSERR;
 
371
343
    ret = sigaction(SIGINT, NULL, &old_action);
 
373
345
      if(errno != EINTR){
 
374
 
        error_plus(0, errno, "sigaction");
 
 
346
        error(0, errno, "sigaction");
 
375
347
        status = EX_OSERR;
 
 
380
352
      ret = sigaction(SIGINT, &new_action, NULL);
 
382
354
        if(errno != EINTR){
 
383
 
          error_plus(0, errno, "sigaction");
 
 
355
          error(0, errno, "sigaction");
 
384
356
          status = EX_OSERR;
 
 
389
361
    ret = sigaction(SIGHUP, NULL, &old_action);
 
391
363
      if(errno != EINTR){
 
392
 
        error_plus(0, errno, "sigaction");
 
 
364
        error(0, errno, "sigaction");
 
393
365
        status = EX_OSERR;
 
 
398
370
      ret = sigaction(SIGHUP, &new_action, NULL);
 
400
372
        if(errno != EINTR){
 
401
 
          error_plus(0, errno, "sigaction");
 
 
373
          error(0, errno, "sigaction");
 
402
374
          status = EX_OSERR;
 
 
407
379
    ret = sigaction(SIGTERM, NULL, &old_action);
 
409
381
      if(errno != EINTR){
 
410
 
        error_plus(0, errno, "sigaction");
 
 
382
        error(0, errno, "sigaction");
 
411
383
        status = EX_OSERR;
 
 
416
388
      ret = sigaction(SIGTERM, &new_action, NULL);
 
418
390
        if(errno != EINTR){
 
419
 
          error_plus(0, errno, "sigaction");
 
 
391
          error(0, errno, "sigaction");
 
420
392
          status = EX_OSERR;
 
 
428
400
  /* Write command to FIFO */
 
429
401
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
 
430
402
    if(errno != EINTR){
 
431
 
      error_plus(0, errno, "usplash_write");
 
 
403
      error(0, errno, "usplash_write");
 
432
404
      status = EX_OSERR;
 
 
441
413
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
 
442
414
    if(errno != EINTR){
 
443
 
      error_plus(0, errno, "usplash_write");
 
 
415
      error(0, errno, "usplash_write");
 
444
416
      status = EX_OSERR;
 
 
477
449
      char *tmp = realloc(buf, buf_allocated + blocksize);
 
479
451
        if(errno != EINTR){
 
480
 
          error_plus(0, errno, "realloc");
 
 
452
          error(0, errno, "realloc");
 
481
453
          status = EX_OSERR;
 
 
489
461
                buf_allocated - buf_len);
 
491
463
      if(errno != EINTR){
 
492
 
        error_plus(0, errno, "read");
 
 
464
        error(0, errno, "read");
 
493
465
        status = EX_OSERR;
 
 
467
      TEMP_FAILURE_RETRY(close(outfifo_fd));
 
498
470
    if(interrupted_by_signal){
 
 
518
490
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
 
519
491
    if(errno != EINTR){
 
520
 
      error_plus(0, errno, "usplash_write");
 
 
492
      error(0, errno, "usplash_write");
 
521
493
      status = EX_OSERR;
 
 
544
516
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
 
546
518
        if(errno != EINTR){
 
547
 
          error_plus(0, errno, "write");
 
 
519
          error(0, errno, "write");
 
548
520
          status = EX_OSERR;
 
 
581
553
  if(fifo_fd != -1){
 
582
 
    ret = close(fifo_fd);
 
 
554
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
583
555
    if(ret == -1 and errno != EINTR){
 
584
 
      error_plus(0, errno, "close");
 
 
556
      error(0, errno, "close");
 
589
561
  /* Close output FIFO */
 
590
562
  if(outfifo_fd != -1){
 
591
 
    ret = close(outfifo_fd);
 
 
563
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
 
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 */
 
 
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;
 
606
590
  /* Kill old usplash */
 
607
591
  kill(usplash_pid, SIGTERM);
 
 
619
603
       the real user ID (_mandos) */
 
620
604
    ret = setuid(geteuid());
 
622
 
      error_plus(0, errno, "setuid");
 
 
606
      error(0, errno, "setuid");
 
626
610
    ret = chdir("/");
 
628
 
      error_plus(0, errno, "chdir");
 
 
612
      error(0, errno, "chdir");
 
631
615
/*     if(fork() != 0){ */
 
 
634
618
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
636
 
      error_plus(0, errno, "dup2");
 
 
620
      error(0, errno, "dup2");
 
640
624
    execv(usplash_name, cmdline_argv);
 
641
625
    if(not interrupted_by_signal){
 
642
 
      error_plus(0, errno, "execv");
 
 
626
      error(0, errno, "execv");
 
645
629
    free(cmdline_argv);
 
 
651
635
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
 
652
636
    if(errno != EINTR){
 
653
 
      error_plus(0, errno, "usplash_write");
 
 
637
      error(0, errno, "usplash_write");
 
657
641
  /* Close FIFO (again) */
 
658
642
  if(fifo_fd != -1){
 
659
 
    ret = close(fifo_fd);
 
 
643
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
660
644
    if(ret == -1 and errno != EINTR){
 
661
 
      error_plus(0, errno, "close");
 
 
645
      error(0, errno, "close");
 
 
669
653
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
670
654
                                            &signal_action, NULL));
 
672
 
      error_plus(0, errno, "sigaction");
 
 
656
      error(0, errno, "sigaction");
 
675
659
      ret = raise(signal_received);
 
676
660
    } while(ret != 0 and errno == EINTR);
 
678
 
      error_plus(0, errno, "raise");
 
 
662
      error(0, errno, "raise");
 
681
665
    TEMP_FAILURE_RETRY(pause());