/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/usplash.c

  • Committer: Teddy Hogeborn
  • Date: 2009-09-16 23:28:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090916232839-3o7i8qmcdcz5j1ya
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
                 "$syslog", thanks to Petter Reinholdtsen
                 <pere@hungry.com> (Debian bug #546928).

* initramfs-tools-script: Removed erroneous comment.

* plugins.d/askpass-fifo.c: Removed TEMP_FAILURE_RETRY since it is
                            not needed.

* plugins.d/mandos-client.c (main): Bug fix: Initialize
                                    "old_sigterm_action".

* plugins.d/splashy.c (main): Bug fix: really check return value from
                              "sigaddset".  Fix some warnings on
                              64-bit systems.

* plugins.d/usplash.c (termination_handler, main): Save received
                                                   signal and
                                                   re-raise it on
                                                   exit.
  (usplash_write): Do not close FIFO, instead, take an additional file
                   descriptor pointer to it and open only when needed
                   (all callers changed).  Abort immediately on EINTR.
                   Bug fix:  Add NUL byte on single-word commands.
                   Ignore "interrupted_by_signal".
  (makeprompt, find_usplash): New; broken out from "main()".
  (find_usplash): Bug fix: close /proc/<pid>/cmdline FD on error.
  (main): Reorganized to jump to a new "failure" label on any error.
          Bug fix: check return values from sigaddset.
          New variable "usplash_accessed" to flag if usplash(8) needs
          to be killed and restarted.  Removed the "an_error_occured"
          variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Usplash - Read a password from usplash and output it
4
4
 * 
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
7
7
 * 
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
31
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
32
32
#include <iso646.h>             /* and, or, not*/
33
33
#include <errno.h>              /* errno, EINTR */
34
 
#include <error.h>
35
34
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
36
35
                                   dirent */
37
36
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp(), strerror() */
39
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
40
 
                                   fprintf() */
 
37
#include <string.h>             /* strlen(), memcmp() */
 
38
#include <stdio.h>              /* asprintf(), perror() */
41
39
#include <unistd.h>             /* close(), write(), readlink(),
42
40
                                   read(), STDOUT_FILENO, sleep(),
43
41
                                   fork(), setuid(), geteuid(),
44
42
                                   setsid(), chdir(), dup2(),
45
43
                                   STDERR_FILENO, execv() */
46
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
47
 
                                   EXIT_SUCCESS, malloc(), _exit(),
48
 
                                   getenv() */
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
 
46
#include <stdlib.h>             /* getenv() */
49
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
50
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
51
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
52
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
53
 
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
50
 
56
51
sig_atomic_t interrupted_by_signal = 0;
57
52
int signal_received;
58
53
const char usplash_name[] = "/sbin/usplash";
59
54
 
60
 
/* Function to use when printing errors */
61
 
void error_plus(int status, int errnum, const char *formatstring,
62
 
                ...){
63
 
  va_list ap;
64
 
  char *text;
65
 
  int ret;
66
 
  
67
 
  va_start(ap, formatstring);
68
 
  ret = vasprintf(&text, formatstring, ap);
69
 
  if (ret == -1){
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");
76
 
    return;
77
 
  }
78
 
  fprintf(stderr, "Mandos plugin ");
79
 
  error(status, errnum, "%s", text);
80
 
  free(text);
81
 
}
82
 
 
83
55
static void termination_handler(int signum){
84
56
  if(interrupted_by_signal){
85
57
    return;
180
152
  size_t cmdline_len = 0;
181
153
  DIR *proc_dir = opendir("/proc");
182
154
  if(proc_dir == NULL){
183
 
    error_plus(0, errno, "opendir");
 
155
    perror("opendir");
184
156
    return -1;
185
157
  }
186
158
  errno = 0;
208
180
      char *exe_link;
209
181
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
210
182
      if(ret == -1){
211
 
        error_plus(0, errno, "asprintf");
 
183
        perror("asprintf");
212
184
        goto fail_find_usplash;
213
185
      }
214
186
      
220
192
          free(exe_link);
221
193
          continue;
222
194
        }
223
 
        error_plus(0, errno, "lstat");
 
195
        perror("lstat");
224
196
        free(exe_link);
225
197
        goto fail_find_usplash;
226
198
      }
251
223
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
252
224
                       proc_ent->d_name);
253
225
        if(ret == -1){
254
 
          error_plus(0, errno, "asprintf");
 
226
          perror("asprintf");
255
227
          goto fail_find_usplash;
256
228
        }
257
229
        cl_fd = open(cmdline_filename, O_RDONLY);
258
230
        free(cmdline_filename);
259
231
        if(cl_fd == -1){
260
 
          error_plus(0, errno, "open");
 
232
          perror("open");
261
233
          goto fail_find_usplash;
262
234
        }
263
235
      }
269
241
        if(cmdline_len + blocksize > cmdline_allocated){
270
242
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
271
243
          if(tmp == NULL){
272
 
            error_plus(0, errno, "realloc");
 
244
            perror("realloc");
273
245
            close(cl_fd);
274
246
            goto fail_find_usplash;
275
247
          }
280
252
        sret = read(cl_fd, cmdline + cmdline_len,
281
253
                    cmdline_allocated - cmdline_len);
282
254
        if(sret == -1){
283
 
          error_plus(0, errno, "read");
 
255
          perror("read");
284
256
          close(cl_fd);
285
257
          goto fail_find_usplash;
286
258
        }
288
260
      } while(sret != 0);
289
261
      ret = close(cl_fd);
290
262
      if(ret == -1){
291
 
        error_plus(0, errno, "close");
 
263
        perror("close");
292
264
        goto fail_find_usplash;
293
265
      }
294
266
    }
295
267
    /* Close directory */
296
268
    ret = closedir(proc_dir);
297
269
    if(ret == -1){
298
 
      error_plus(0, errno, "closedir");
 
270
      perror("closedir");
299
271
      goto fail_find_usplash;
300
272
    }
301
273
    /* Success */
325
297
  size_t buf_len = 0;
326
298
  pid_t usplash_pid = -1;
327
299
  bool usplash_accessed = false;
328
 
  int status = EXIT_FAILURE;    /* Default failure exit status */
329
300
  
330
301
  char *prompt = makeprompt();
331
302
  if(prompt == NULL){
332
 
    status = EX_OSERR;
333
303
    goto failure;
334
304
  }
335
305
  
338
308
  size_t cmdline_len = 0;
339
309
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
340
310
  if(usplash_pid == 0){
341
 
    status = EX_UNAVAILABLE;
342
311
    goto failure;
343
312
  }
344
313
  
350
319
    sigemptyset(&new_action.sa_mask);
351
320
    ret = sigaddset(&new_action.sa_mask, SIGINT);
352
321
    if(ret == -1){
353
 
      error_plus(0, errno, "sigaddset");
354
 
      status = EX_OSERR;
 
322
      perror("sigaddset");
355
323
      goto failure;
356
324
    }
357
325
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
358
326
    if(ret == -1){
359
 
      error_plus(0, errno, "sigaddset");
360
 
      status = EX_OSERR;
 
327
      perror("sigaddset");
361
328
      goto failure;
362
329
    }
363
330
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
364
331
    if(ret == -1){
365
 
      error_plus(0, errno, "sigaddset");
366
 
      status = EX_OSERR;
 
332
      perror("sigaddset");
367
333
      goto failure;
368
334
    }
369
335
    ret = sigaction(SIGINT, NULL, &old_action);
370
336
    if(ret == -1){
371
337
      if(errno != EINTR){
372
 
        error_plus(0, errno, "sigaction");
373
 
        status = EX_OSERR;
 
338
        perror("sigaction");
374
339
      }
375
340
      goto failure;
376
341
    }
378
343
      ret = sigaction(SIGINT, &new_action, NULL);
379
344
      if(ret == -1){
380
345
        if(errno != EINTR){
381
 
          error_plus(0, errno, "sigaction");
382
 
          status = EX_OSERR;
 
346
          perror("sigaction");
383
347
        }
384
348
        goto failure;
385
349
      }
387
351
    ret = sigaction(SIGHUP, NULL, &old_action);
388
352
    if(ret == -1){
389
353
      if(errno != EINTR){
390
 
        error_plus(0, errno, "sigaction");
391
 
        status = EX_OSERR;
 
354
        perror("sigaction");
392
355
      }
393
356
      goto failure;
394
357
    }
396
359
      ret = sigaction(SIGHUP, &new_action, NULL);
397
360
      if(ret == -1){
398
361
        if(errno != EINTR){
399
 
          error_plus(0, errno, "sigaction");
400
 
          status = EX_OSERR;
 
362
          perror("sigaction");
401
363
        }
402
364
        goto failure;
403
365
      }
405
367
    ret = sigaction(SIGTERM, NULL, &old_action);
406
368
    if(ret == -1){
407
369
      if(errno != EINTR){
408
 
        error_plus(0, errno, "sigaction");
409
 
        status = EX_OSERR;
 
370
        perror("sigaction");
410
371
      }
411
372
      goto failure;
412
373
    }
414
375
      ret = sigaction(SIGTERM, &new_action, NULL);
415
376
      if(ret == -1){
416
377
        if(errno != EINTR){
417
 
          error_plus(0, errno, "sigaction");
418
 
          status = EX_OSERR;
 
378
          perror("sigaction");
419
379
        }
420
380
        goto failure;
421
381
      }
426
386
  /* Write command to FIFO */
427
387
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
428
388
    if(errno != EINTR){
429
 
      error_plus(0, errno, "usplash_write");
430
 
      status = EX_OSERR;
 
389
      perror("usplash_write");
431
390
    }
432
391
    goto failure;
433
392
  }
438
397
  
439
398
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
440
399
    if(errno != EINTR){
441
 
      error_plus(0, errno, "usplash_write");
442
 
      status = EX_OSERR;
 
400
      perror("usplash_write");
443
401
    }
444
402
    goto failure;
445
403
  }
456
414
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
457
415
  if(outfifo_fd == -1){
458
416
    if(errno != EINTR){
459
 
      error_plus(0, errno, "open");
460
 
      status = EX_OSERR;
 
417
      perror("open");
461
418
    }
462
419
    goto failure;
463
420
  }
475
432
      char *tmp = realloc(buf, buf_allocated + blocksize);
476
433
      if(tmp == NULL){
477
434
        if(errno != EINTR){
478
 
          error_plus(0, errno, "realloc");
479
 
          status = EX_OSERR;
 
435
          perror("realloc");
480
436
        }
481
437
        goto failure;
482
438
      }
487
443
                buf_allocated - buf_len);
488
444
    if(sret == -1){
489
445
      if(errno != EINTR){
490
 
        error_plus(0, errno, "read");
491
 
        status = EX_OSERR;
 
446
        perror("read");
492
447
      }
493
448
      TEMP_FAILURE_RETRY(close(outfifo_fd));
494
449
      goto failure;
502
457
  ret = close(outfifo_fd);
503
458
  if(ret == -1){
504
459
    if(errno != EINTR){
505
 
      error_plus(0, errno, "close");
506
 
      status = EX_OSERR;
 
460
      perror("close");
507
461
    }
508
462
    goto failure;
509
463
  }
515
469
  
516
470
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
517
471
    if(errno != EINTR){
518
 
      error_plus(0, errno, "usplash_write");
519
 
      status = EX_OSERR;
 
472
      perror("usplash_write");
520
473
    }
521
474
    goto failure;
522
475
  }
528
481
  ret = close(fifo_fd);
529
482
  if(ret == -1){
530
483
    if(errno != EINTR){
531
 
      error_plus(0, errno, "close");
532
 
      status = EX_OSERR;
 
484
      perror("close");
533
485
    }
534
486
    goto failure;
535
487
  }
542
494
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
543
495
      if(sret == -1){
544
496
        if(errno != EINTR){
545
 
          error_plus(0, errno, "write");
546
 
          status = EX_OSERR;
 
497
          perror("write");
547
498
        }
548
499
        goto failure;
549
500
      }
572
523
  
573
524
  /* If usplash was never accessed, we can stop now */
574
525
  if(not usplash_accessed){
575
 
    return status;
 
526
    return EXIT_FAILURE;
576
527
  }
577
528
  
578
529
  /* Close FIFO */
579
530
  if(fifo_fd != -1){
580
531
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
581
532
    if(ret == -1 and errno != EINTR){
582
 
      error_plus(0, errno, "close");
 
533
      perror("close");
583
534
    }
584
535
    fifo_fd = -1;
585
536
  }
588
539
  if(outfifo_fd != -1){
589
540
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
590
541
    if(ret == -1){
591
 
      error_plus(0, errno, "close");
592
 
    }
593
 
  }
594
 
  
595
 
  /* Create argv for new usplash*/
596
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
597
 
                               * sizeof(char *)); /* Count args */
598
 
  if(cmdline_argv == NULL){
599
 
    error_plus(0, errno, "malloc");
600
 
    return status;
601
 
  }
602
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
603
 
  
 
542
      perror("close");
 
543
    }
 
544
  }
 
545
  
 
546
  /* Create argc and argv for new usplash*/
 
547
  int cmdline_argc = 0;
 
548
  char **cmdline_argv = malloc(sizeof(char *));
 
549
  {
 
550
    size_t position = 0;
 
551
    while(position < cmdline_len){
 
552
      char **tmp = realloc(cmdline_argv,
 
553
                           (sizeof(char *)
 
554
                            * (size_t)(cmdline_argc + 2)));
 
555
      if(tmp == NULL){
 
556
        perror("realloc");
 
557
        free(cmdline_argv);
 
558
        return EXIT_FAILURE;
 
559
      }
 
560
      cmdline_argv = tmp;
 
561
      cmdline_argv[cmdline_argc] = cmdline + position;
 
562
      cmdline_argc++;
 
563
      position += strlen(cmdline + position) + 1;
 
564
    }
 
565
    cmdline_argv[cmdline_argc] = NULL;
 
566
  }
604
567
  /* Kill old usplash */
605
568
  kill(usplash_pid, SIGTERM);
606
569
  sleep(2);
617
580
       the real user ID (_mandos) */
618
581
    ret = setuid(geteuid());
619
582
    if(ret == -1){
620
 
      error_plus(0, errno, "setuid");
 
583
      perror("setuid");
621
584
    }
622
585
    
623
586
    setsid();
624
587
    ret = chdir("/");
625
 
    if(ret == -1){
626
 
      error_plus(0, errno, "chdir");
627
 
      _exit(EX_OSERR);
628
 
    }
629
588
/*     if(fork() != 0){ */
630
589
/*       _exit(EXIT_SUCCESS); */
631
590
/*     } */
632
591
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
633
592
    if(ret == -1){
634
 
      error_plus(0, errno, "dup2");
635
 
      _exit(EX_OSERR);
 
593
      perror("dup2");
 
594
      _exit(EXIT_FAILURE);
636
595
    }
637
596
    
638
597
    execv(usplash_name, cmdline_argv);
639
598
    if(not interrupted_by_signal){
640
 
      error_plus(0, errno, "execv");
 
599
      perror("execv");
641
600
    }
642
601
    free(cmdline);
643
602
    free(cmdline_argv);
644
 
    _exit(EX_OSERR);
 
603
    _exit(EXIT_FAILURE);
645
604
  }
646
605
  free(cmdline);
647
606
  free(cmdline_argv);
648
607
  sleep(2);
649
608
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
650
609
    if(errno != EINTR){
651
 
      error_plus(0, errno, "usplash_write");
 
610
      perror("usplash_write");
652
611
    }
653
612
  }
654
613
  
655
614
  /* Close FIFO (again) */
656
 
  if(fifo_fd != -1){
657
 
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
658
 
    if(ret == -1 and errno != EINTR){
659
 
      error_plus(0, errno, "close");
660
 
    }
661
 
    fifo_fd = -1;
 
615
  ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
616
  if(ret == -1 and errno != EINTR){
 
617
    perror("close");
662
618
  }
 
619
  fifo_fd = -1;
663
620
  
664
621
  if(interrupted_by_signal){
665
622
    struct sigaction signal_action = { .sa_handler = SIG_DFL };
667
624
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
668
625
                                            &signal_action, NULL));
669
626
    if(ret == -1){
670
 
      error_plus(0, errno, "sigaction");
 
627
      perror("sigaction");
671
628
    }
672
629
    do {
673
630
      ret = raise(signal_received);
674
631
    } while(ret != 0 and errno == EINTR);
675
632
    if(ret != 0){
676
 
      error_plus(0, errno, "raise");
 
633
      perror("raise");
677
634
      abort();
678
635
    }
679
636
    TEMP_FAILURE_RETRY(pause());
680
637
  }
681
638
  
682
 
  return status;
 
639
  return EXIT_FAILURE;
683
640
}