/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 plugin-runner.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
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 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
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
 
                                   realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
29
                                   EXIT_SUCCESS, realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* fileno(), fprintf(),
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
42
42
                                   WCOREDUMP() */
43
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
48
                                   fcntl(), setuid(), setgid(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
57
                                   strsignal() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
68
68
                                */
69
69
#include <errno.h>              /* errno, EBADF */
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
72
 
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
 
#include <errno.h>              /* errno */
74
 
#include <error.h>              /* error() */
75
71
 
76
72
#define BUFFER_SIZE 256
77
73
 
106
102
/* Gets an existing plugin based on name,
107
103
   or if none is found, creates a new one */
108
104
static plugin *getplugin(char *name){
109
 
  /* Check for existing plugin with that name */
 
105
  /* Check for exiting plugin with that name */
110
106
  for(plugin *p = plugin_list; p != NULL; p = p->next){
111
107
    if((p->name == name)
112
108
       or (p->name and name and (strcmp(p->name, name) == 0))){
127
123
      copy_name = strdup(name);
128
124
    } while(copy_name == NULL and errno == EINTR);
129
125
    if(copy_name == NULL){
130
 
      int e = errno;
131
126
      free(new_plugin);
132
 
      errno = e;
133
127
      return NULL;
134
128
    }
135
129
  }
143
137
    new_plugin->argv = malloc(sizeof(char *) * 2);
144
138
  } while(new_plugin->argv == NULL and errno == EINTR);
145
139
  if(new_plugin->argv == NULL){
146
 
    int e = errno;
147
140
    free(copy_name);
148
141
    free(new_plugin);
149
 
    errno = e;
150
142
    return NULL;
151
143
  }
152
144
  new_plugin->argv[0] = copy_name;
156
148
    new_plugin->environ = malloc(sizeof(char *));
157
149
  } while(new_plugin->environ == NULL and errno == EINTR);
158
150
  if(new_plugin->environ == NULL){
159
 
    int e = errno;
160
151
    free(copy_name);
161
152
    free(new_plugin->argv);
162
153
    free(new_plugin);
163
 
    errno = e;
164
154
    return NULL;
165
155
  }
166
156
  new_plugin->environ[0] = NULL;
268
258
        /* No child processes */
269
259
        break;
270
260
      }
271
 
      error(0, errno, "waitpid");
 
261
      perror("waitpid");
272
262
    }
273
263
    
274
264
    /* A child exited, find it in process_list */
359
349
  sigemptyset(&sigchld_action.sa_mask);
360
350
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
361
351
  if(ret == -1){
362
 
    error(0, errno, "sigaddset");
363
 
    exitstatus = EX_OSERR;
 
352
    perror("sigaddset");
 
353
    exitstatus = EXIT_FAILURE;
364
354
    goto fallback;
365
355
  }
366
356
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
367
357
  if(ret == -1){
368
 
    error(0, errno, "sigaction");
369
 
    exitstatus = EX_OSERR;
 
358
    perror("sigaction");
 
359
    exitstatus = EXIT_FAILURE;
370
360
    goto fallback;
371
361
  }
372
362
  
404
394
      .doc = "Group ID the plugins will run as", .group = 3 },
405
395
    { .name = "debug", .key = 132,
406
396
      .doc = "Debug mode", .group = 4 },
407
 
    /*
408
 
     * These reproduce what we would get without ARGP_NO_HELP
409
 
     */
410
 
    { .name = "help", .key = '?',
411
 
      .doc = "Give this help list", .group = -1 },
412
 
    { .name = "usage", .key = -3,
413
 
      .doc = "Give a short usage message", .group = -1 },
414
 
    { .name = "version", .key = 'V',
415
 
      .doc = "Print program version", .group = -1 },
416
397
    { .name = NULL }
417
398
  };
418
399
  
419
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
420
 
    errno = 0;
 
400
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
401
                    struct argp_state *state){
421
402
    switch(key){
422
403
      char *tmp;
423
404
      intmax_t tmpmax;
424
405
    case 'g':                   /* --global-options */
425
 
      {
 
406
      if(arg != NULL){
426
407
        char *plugin_option;
427
408
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
409
          if(plugin_option[0] == '\0'){
 
410
            continue;
 
411
          }
428
412
          if(not add_argument(getplugin(NULL), plugin_option)){
429
 
            break;
 
413
            perror("add_argument");
 
414
            return ARGP_ERR_UNKNOWN;
430
415
          }
431
416
        }
432
417
      }
433
418
      break;
434
419
    case 'G':                   /* --global-env */
435
 
      add_environment(getplugin(NULL), arg, true);
 
420
      if(arg == NULL){
 
421
        break;
 
422
      }
 
423
      if(not add_environment(getplugin(NULL), arg, true)){
 
424
        perror("add_environment");
 
425
      }
436
426
      break;
437
427
    case 'o':                   /* --options-for */
438
 
      {
439
 
        char *option_list = strchr(arg, ':');
440
 
        if(option_list == NULL){
441
 
          argp_error(state, "No colon in \"%s\"", arg);
442
 
          errno = EINVAL;
443
 
          break;
444
 
        }
445
 
        *option_list = '\0';
446
 
        option_list++;
447
 
        if(arg[0] == '\0'){
448
 
          argp_error(state, "Empty plugin name");
449
 
          errno = EINVAL;
450
 
          break;
451
 
        }
452
 
        char *option;
453
 
        while((option = strsep(&option_list, ",")) != NULL){
454
 
          if(not add_argument(getplugin(arg), option)){
455
 
            break;
 
428
      if(arg != NULL){
 
429
        char *plugin_name = strsep(&arg, ":");
 
430
        if(plugin_name[0] == '\0'){
 
431
          break;
 
432
        }
 
433
        char *plugin_option;
 
434
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
435
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
436
            perror("add_argument");
 
437
            return ARGP_ERR_UNKNOWN;
456
438
          }
457
439
        }
458
440
      }
459
441
      break;
460
442
    case 'E':                   /* --env-for */
 
443
      if(arg == NULL){
 
444
        break;
 
445
      }
461
446
      {
462
447
        char *envdef = strchr(arg, ':');
463
448
        if(envdef == NULL){
464
 
          argp_error(state, "No colon in \"%s\"", arg);
465
 
          errno = EINVAL;
466
449
          break;
467
450
        }
468
451
        *envdef = '\0';
469
 
        envdef++;
470
 
        if(arg[0] == '\0'){
471
 
          argp_error(state, "Empty plugin name");
472
 
          errno = EINVAL;
473
 
          break;
 
452
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
453
          perror("add_environment");
474
454
        }
475
 
        add_environment(getplugin(arg), envdef, true);
476
455
      }
477
456
      break;
478
457
    case 'd':                   /* --disable */
479
 
      {
 
458
      if(arg != NULL){
480
459
        plugin *p = getplugin(arg);
481
 
        if(p != NULL){
482
 
          p->disabled = true;
 
460
        if(p == NULL){
 
461
          return ARGP_ERR_UNKNOWN;
483
462
        }
 
463
        p->disabled = true;
484
464
      }
485
465
      break;
486
466
    case 'e':                   /* --enable */
487
 
      {
 
467
      if(arg != NULL){
488
468
        plugin *p = getplugin(arg);
489
 
        if(p != NULL){
490
 
          p->disabled = false;
 
469
        if(p == NULL){
 
470
          return ARGP_ERR_UNKNOWN;
491
471
        }
 
472
        p->disabled = false;
492
473
      }
493
474
      break;
494
475
    case 128:                   /* --plugin-dir */
495
476
      free(plugindir);
496
477
      plugindir = strdup(arg);
 
478
      if(plugindir == NULL){
 
479
        perror("strdup");
 
480
      }
497
481
      break;
498
482
    case 129:                   /* --config-file */
499
483
      /* This is already done by parse_opt_config_file() */
500
484
      break;
501
485
    case 130:                   /* --userid */
 
486
      errno = 0;
502
487
      tmpmax = strtoimax(arg, &tmp, 10);
503
488
      if(errno != 0 or tmp == arg or *tmp != '\0'
504
489
         or tmpmax != (uid_t)tmpmax){
505
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
506
 
                   PRIdMAX, arg, (intmax_t)uid);
507
 
        break;
 
490
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
491
                PRIdMAX "\n", arg, (intmax_t)uid);
 
492
      } else {
 
493
        uid = (uid_t)tmpmax;
508
494
      }
509
 
      uid = (uid_t)tmpmax;
510
495
      break;
511
496
    case 131:                   /* --groupid */
 
497
      errno = 0;
512
498
      tmpmax = strtoimax(arg, &tmp, 10);
513
499
      if(errno != 0 or tmp == arg or *tmp != '\0'
514
500
         or tmpmax != (gid_t)tmpmax){
515
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
516
 
                   PRIdMAX, arg, (intmax_t)gid);
517
 
        break;
 
501
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
502
                PRIdMAX "\n", arg, (intmax_t)gid);
 
503
      } else {
 
504
        gid = (gid_t)tmpmax;
518
505
      }
519
 
      gid = (gid_t)tmpmax;
520
506
      break;
521
507
    case 132:                   /* --debug */
522
508
      debug = true;
523
509
      break;
524
 
      /*
525
 
       * These reproduce what we would get without ARGP_NO_HELP
526
 
       */
527
 
    case '?':                   /* --help */
528
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
529
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
530
 
    case -3:                    /* --usage */
531
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
532
 
      argp_state_help(state, state->out_stream,
533
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
534
 
    case 'V':                   /* --version */
535
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
536
 
      exit(EXIT_SUCCESS);
537
 
      break;
538
510
/*
539
511
 * When adding more options before this line, remember to also add a
540
512
 * "case" to the "parse_opt_config_file" function below.
543
515
      /* Cryptsetup always passes an argument, which is an empty
544
516
         string if "none" was specified in /etc/crypttab.  So if
545
517
         argument was empty, we ignore it silently. */
546
 
      if(arg[0] == '\0'){
547
 
        break;
 
518
      if(arg[0] != '\0'){
 
519
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
548
520
      }
 
521
      break;
 
522
    case ARGP_KEY_END:
 
523
      break;
549
524
    default:
550
525
      return ARGP_ERR_UNKNOWN;
551
526
    }
552
 
    return errno;               /* Set to 0 at start */
 
527
    return 0;
553
528
  }
554
529
  
555
530
  /* This option parser is the same as parse_opt() above, except it
557
532
  error_t parse_opt_config_file(int key, char *arg,
558
533
                                __attribute__((unused))
559
534
                                struct argp_state *state){
560
 
    errno = 0;
561
535
    switch(key){
562
536
    case 'g':                   /* --global-options */
563
537
    case 'G':                   /* --global-env */
570
544
    case 129:                   /* --config-file */
571
545
      free(argfile);
572
546
      argfile = strdup(arg);
 
547
      if(argfile == NULL){
 
548
        perror("strdup");
 
549
      }
573
550
      break;
574
551
    case 130:                   /* --userid */
575
552
    case 131:                   /* --groupid */
576
553
    case 132:                   /* --debug */
577
 
    case '?':                   /* --help */
578
 
    case -3:                    /* --usage */
579
 
    case 'V':                   /* --version */
580
554
    case ARGP_KEY_ARG:
 
555
    case ARGP_KEY_END:
581
556
      break;
582
557
    default:
583
558
      return ARGP_ERR_UNKNOWN;
584
559
    }
585
 
    return errno;
 
560
    return 0;
586
561
  }
587
562
  
588
563
  struct argp argp = { .options = options,
592
567
  
593
568
  /* Parse using parse_opt_config_file() in order to get the custom
594
569
     config file location, if any. */
595
 
  ret = argp_parse(&argp, argc, argv,
596
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
597
 
                   NULL, NULL);
598
 
  switch(ret){
599
 
  case 0:
600
 
    break;
601
 
  case ENOMEM:
602
 
  default:
603
 
    errno = ret;
604
 
    error(0, errno, "argp_parse");
605
 
    exitstatus = EX_OSERR;
606
 
    goto fallback;
607
 
  case EINVAL:
608
 
    exitstatus = EX_USAGE;
 
570
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
571
  if(ret == ARGP_ERR_UNKNOWN){
 
572
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
573
    exitstatus = EXIT_FAILURE;
609
574
    goto fallback;
610
575
  }
611
576
  
628
593
    custom_argc = 1;
629
594
    custom_argv = malloc(sizeof(char*) * 2);
630
595
    if(custom_argv == NULL){
631
 
      error(0, errno, "malloc");
632
 
      exitstatus = EX_OSERR;
 
596
      perror("malloc");
 
597
      exitstatus = EXIT_FAILURE;
633
598
      goto fallback;
634
599
    }
635
600
    custom_argv[0] = argv[0];
651
616
        }
652
617
        new_arg = strdup(p);
653
618
        if(new_arg == NULL){
654
 
          error(0, errno, "strdup");
655
 
          exitstatus = EX_OSERR;
 
619
          perror("strdup");
 
620
          exitstatus = EXIT_FAILURE;
656
621
          free(org_line);
657
622
          goto fallback;
658
623
        }
661
626
        custom_argv = realloc(custom_argv, sizeof(char *)
662
627
                              * ((unsigned int) custom_argc + 1));
663
628
        if(custom_argv == NULL){
664
 
          error(0, errno, "realloc");
665
 
          exitstatus = EX_OSERR;
 
629
          perror("realloc");
 
630
          exitstatus = EXIT_FAILURE;
666
631
          free(org_line);
667
632
          goto fallback;
668
633
        }
674
639
      ret = fclose(conffp);
675
640
    } while(ret == EOF and errno == EINTR);
676
641
    if(ret == EOF){
677
 
      error(0, errno, "fclose");
678
 
      exitstatus = EX_IOERR;
 
642
      perror("fclose");
 
643
      exitstatus = EXIT_FAILURE;
679
644
      goto fallback;
680
645
    }
681
646
    free(org_line);
683
648
    /* Check for harmful errors and go to fallback. Other errors might
684
649
       not affect opening plugins */
685
650
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
686
 
      error(0, errno, "fopen");
687
 
      exitstatus = EX_OSERR;
 
651
      perror("fopen");
 
652
      exitstatus = EXIT_FAILURE;
688
653
      goto fallback;
689
654
    }
690
655
  }
691
 
  /* If there were any arguments from the configuration file, pass
692
 
     them to parser as command line arguments */
 
656
  /* If there was any arguments from configuration file,
 
657
     pass them to parser as command arguments */
693
658
  if(custom_argv != NULL){
694
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
695
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
696
 
                     NULL, NULL);
697
 
    switch(ret){
698
 
    case 0:
699
 
      break;
700
 
    case ENOMEM:
701
 
    default:
702
 
      errno = ret;
703
 
      error(0, errno, "argp_parse");
704
 
      exitstatus = EX_OSERR;
705
 
      goto fallback;
706
 
    case EINVAL:
707
 
      exitstatus = EX_CONFIG;
 
659
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
660
                     0, NULL);
 
661
    if(ret == ARGP_ERR_UNKNOWN){
 
662
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
663
      exitstatus = EXIT_FAILURE;
708
664
      goto fallback;
709
665
    }
710
666
  }
711
667
  
712
668
  /* Parse actual command line arguments, to let them override the
713
669
     config file */
714
 
  ret = argp_parse(&argp, argc, argv,
715
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
716
 
                   NULL, NULL);
717
 
  switch(ret){
718
 
  case 0:
719
 
    break;
720
 
  case ENOMEM:
721
 
  default:
722
 
    errno = ret;
723
 
    error(0, errno, "argp_parse");
724
 
    exitstatus = EX_OSERR;
725
 
    goto fallback;
726
 
  case EINVAL:
727
 
    exitstatus = EX_USAGE;
 
670
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
671
  if(ret == ARGP_ERR_UNKNOWN){
 
672
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
673
    exitstatus = EXIT_FAILURE;
728
674
    goto fallback;
729
675
  }
730
676
  
742
688
    }
743
689
  }
744
690
  
745
 
  {
746
 
    /* Work around Debian bug #633582:
747
 
       <http://bugs.debian.org/633582> */
748
 
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
749
 
    if(plugindir_fd == -1){
750
 
      error(0, errno, "open");
751
 
    } else {
752
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
753
 
      if(ret == -1){
754
 
        error(0, errno, "fstat");
755
 
      } else {
756
 
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
757
 
          ret = fchown(plugindir_fd, uid, gid);
758
 
          if(ret == -1){
759
 
            error(0, errno, "fchown");
760
 
          }
761
 
        }
762
 
      }
763
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
764
 
    }
765
 
  }
766
 
  
767
 
  /* Lower permissions */
 
691
  /* Strip permissions down to nobody */
768
692
  setgid(gid);
769
693
  if(ret == -1){
770
 
    error(0, errno, "setgid");
 
694
    perror("setgid");
771
695
  }
772
696
  ret = setuid(uid);
773
697
  if(ret == -1){
774
 
    error(0, errno, "setuid");
775
 
  }
776
 
  
777
 
  /* Open plugin directory with close_on_exec flag */
 
698
    perror("setuid");
 
699
  }
 
700
  
 
701
  if(plugindir == NULL){
 
702
    dir = opendir(PDIR);
 
703
  } else {
 
704
    dir = opendir(plugindir);
 
705
  }
 
706
  
 
707
  if(dir == NULL){
 
708
    perror("Could not open plugin dir");
 
709
    exitstatus = EXIT_FAILURE;
 
710
    goto fallback;
 
711
  }
 
712
  
 
713
  /* Set the FD_CLOEXEC flag on the directory, if possible */
778
714
  {
779
 
    int dir_fd = -1;
780
 
    if(plugindir == NULL){
781
 
      dir_fd = open(PDIR, O_RDONLY |
782
 
#ifdef O_CLOEXEC
783
 
                    O_CLOEXEC
784
 
#else  /* not O_CLOEXEC */
785
 
                    0
786
 
#endif  /* not O_CLOEXEC */
787
 
                    );
788
 
    } else {
789
 
      dir_fd = open(plugindir, O_RDONLY |
790
 
#ifdef O_CLOEXEC
791
 
                    O_CLOEXEC
792
 
#else  /* not O_CLOEXEC */
793
 
                    0
794
 
#endif  /* not O_CLOEXEC */
795
 
                    );
796
 
    }
797
 
    if(dir_fd == -1){
798
 
      error(0, errno, "Could not open plugin dir");
799
 
      exitstatus = EX_UNAVAILABLE;
800
 
      goto fallback;
801
 
    }
802
 
    
803
 
#ifndef O_CLOEXEC
804
 
  /* Set the FD_CLOEXEC flag on the directory */
805
 
    ret = set_cloexec_flag(dir_fd);
806
 
    if(ret < 0){
807
 
      error(0, errno, "set_cloexec_flag");
808
 
      TEMP_FAILURE_RETRY(close(dir_fd));
809
 
      exitstatus = EX_OSERR;
810
 
      goto fallback;
811
 
    }
812
 
#endif  /* O_CLOEXEC */
813
 
    
814
 
    dir = fdopendir(dir_fd);
815
 
    if(dir == NULL){
816
 
      error(0, errno, "Could not open plugin dir");
817
 
      TEMP_FAILURE_RETRY(close(dir_fd));
818
 
      exitstatus = EX_OSERR;
819
 
      goto fallback;
 
715
    int dir_fd = dirfd(dir);
 
716
    if(dir_fd >= 0){
 
717
      ret = set_cloexec_flag(dir_fd);
 
718
      if(ret < 0){
 
719
        perror("set_cloexec_flag");
 
720
        exitstatus = EXIT_FAILURE;
 
721
        goto fallback;
 
722
      }
820
723
    }
821
724
  }
822
725
  
831
734
    /* All directory entries have been processed */
832
735
    if(dirst == NULL){
833
736
      if(errno == EBADF){
834
 
        error(0, errno, "readdir");
835
 
        exitstatus = EX_IOERR;
 
737
        perror("readdir");
 
738
        exitstatus = EXIT_FAILURE;
836
739
        goto fallback;
837
740
      }
838
741
      break;
868
771
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
869
772
        size_t suf_len = strlen(*suf);
870
773
        if((d_name_len >= suf_len)
871
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
774
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
872
775
                == 0)){
873
776
          if(debug){
874
777
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
894
797
                                             dirst->d_name));
895
798
    }
896
799
    if(ret < 0){
897
 
      error(0, errno, "asprintf");
 
800
      perror("asprintf");
898
801
      continue;
899
802
    }
900
803
    
901
804
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
902
805
    if(ret == -1){
903
 
      error(0, errno, "stat");
 
806
      perror("stat");
904
807
      free(filename);
905
808
      continue;
906
809
    }
918
821
    
919
822
    plugin *p = getplugin(dirst->d_name);
920
823
    if(p == NULL){
921
 
      error(0, errno, "getplugin");
 
824
      perror("getplugin");
922
825
      free(filename);
923
826
      continue;
924
827
    }
936
839
      if(g != NULL){
937
840
        for(char **a = g->argv + 1; *a != NULL; a++){
938
841
          if(not add_argument(p, *a)){
939
 
            error(0, errno, "add_argument");
 
842
            perror("add_argument");
940
843
          }
941
844
        }
942
845
        /* Add global environment variables */
943
846
        for(char **e = g->environ; *e != NULL; e++){
944
847
          if(not add_environment(p, *e, false)){
945
 
            error(0, errno, "add_environment");
 
848
            perror("add_environment");
946
849
          }
947
850
        }
948
851
      }
953
856
    if(p->environ[0] != NULL){
954
857
      for(char **e = environ; *e != NULL; e++){
955
858
        if(not add_environment(p, *e, false)){
956
 
          error(0, errno, "add_environment");
 
859
          perror("add_environment");
957
860
        }
958
861
      }
959
862
    }
961
864
    int pipefd[2];
962
865
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
963
866
    if(ret == -1){
964
 
      error(0, errno, "pipe");
965
 
      exitstatus = EX_OSERR;
 
867
      perror("pipe");
 
868
      exitstatus = EXIT_FAILURE;
966
869
      goto fallback;
967
870
    }
968
871
    /* Ask OS to automatic close the pipe on exec */
969
872
    ret = set_cloexec_flag(pipefd[0]);
970
873
    if(ret < 0){
971
 
      error(0, errno, "set_cloexec_flag");
972
 
      exitstatus = EX_OSERR;
 
874
      perror("set_cloexec_flag");
 
875
      exitstatus = EXIT_FAILURE;
973
876
      goto fallback;
974
877
    }
975
878
    ret = set_cloexec_flag(pipefd[1]);
976
879
    if(ret < 0){
977
 
      error(0, errno, "set_cloexec_flag");
978
 
      exitstatus = EX_OSERR;
 
880
      perror("set_cloexec_flag");
 
881
      exitstatus = EXIT_FAILURE;
979
882
      goto fallback;
980
883
    }
981
884
    /* Block SIGCHLD until process is safely in process list */
983
886
                                              &sigchld_action.sa_mask,
984
887
                                              NULL));
985
888
    if(ret < 0){
986
 
      error(0, errno, "sigprocmask");
987
 
      exitstatus = EX_OSERR;
 
889
      perror("sigprocmask");
 
890
      exitstatus = EXIT_FAILURE;
988
891
      goto fallback;
989
892
    }
990
893
    /* Starting a new process to be watched */
993
896
      pid = fork();
994
897
    } while(pid == -1 and errno == EINTR);
995
898
    if(pid == -1){
996
 
      error(0, errno, "fork");
997
 
      exitstatus = EX_OSERR;
 
899
      perror("fork");
 
900
      exitstatus = EXIT_FAILURE;
998
901
      goto fallback;
999
902
    }
1000
903
    if(pid == 0){
1001
904
      /* this is the child process */
1002
905
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1003
906
      if(ret < 0){
1004
 
        error(0, errno, "sigaction");
1005
 
        _exit(EX_OSERR);
 
907
        perror("sigaction");
 
908
        _exit(EXIT_FAILURE);
1006
909
      }
1007
910
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1008
911
      if(ret < 0){
1009
 
        error(0, errno, "sigprocmask");
1010
 
        _exit(EX_OSERR);
 
912
        perror("sigprocmask");
 
913
        _exit(EXIT_FAILURE);
1011
914
      }
1012
915
      
1013
916
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1014
917
      if(ret == -1){
1015
 
        error(0, errno, "dup2");
1016
 
        _exit(EX_OSERR);
 
918
        perror("dup2");
 
919
        _exit(EXIT_FAILURE);
1017
920
      }
1018
921
      
1019
922
      if(dirfd(dir) < 0){
1023
926
      }
1024
927
      if(p->environ[0] == NULL){
1025
928
        if(execv(filename, p->argv) < 0){
1026
 
          error(0, errno, "execv for %s", filename);
1027
 
          _exit(EX_OSERR);
 
929
          perror("execv");
 
930
          _exit(EXIT_FAILURE);
1028
931
        }
1029
932
      } else {
1030
933
        if(execve(filename, p->argv, p->environ) < 0){
1031
 
          error(0, errno, "execve for %s", filename);
1032
 
          _exit(EX_OSERR);
 
934
          perror("execve");
 
935
          _exit(EXIT_FAILURE);
1033
936
        }
1034
937
      }
1035
938
      /* no return */
1040
943
    free(filename);
1041
944
    plugin *new_plugin = getplugin(dirst->d_name);
1042
945
    if(new_plugin == NULL){
1043
 
      error(0, errno, "getplugin");
 
946
      perror("getplugin");
1044
947
      ret = (int)(TEMP_FAILURE_RETRY
1045
948
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1046
949
                               NULL)));
1047
950
      if(ret < 0){
1048
 
        error(0, errno, "sigprocmask");
 
951
        perror("sigprocmask");
1049
952
      }
1050
 
      exitstatus = EX_OSERR;
 
953
      exitstatus = EXIT_FAILURE;
1051
954
      goto fallback;
1052
955
    }
1053
956
    
1060
963
                                              &sigchld_action.sa_mask,
1061
964
                                              NULL));
1062
965
    if(ret < 0){
1063
 
      error(0, errno, "sigprocmask");
1064
 
      exitstatus = EX_OSERR;
 
966
      perror("sigprocmask");
 
967
      exitstatus = EXIT_FAILURE;
1065
968
      goto fallback;
1066
969
    }
1067
970
    
1068
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1069
 
                                          -Wconversion */
 
971
    FD_SET(new_plugin->fd, &rfds_all);
1070
972
    
1071
973
    if(maxfd < new_plugin->fd){
1072
974
      maxfd = new_plugin->fd;
1093
995
    fd_set rfds = rfds_all;
1094
996
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1095
997
    if(select_ret == -1 and errno != EINTR){
1096
 
      error(0, errno, "select");
1097
 
      exitstatus = EX_OSERR;
 
998
      perror("select");
 
999
      exitstatus = EXIT_FAILURE;
1098
1000
      goto fallback;
1099
1001
    }
1100
1002
    /* OK, now either a process completed, or something can be read
1126
1028
          }
1127
1029
          
1128
1030
          /* Remove the plugin */
1129
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1130
 
                                          -Wconversion */
 
1031
          FD_CLR(proc->fd, &rfds_all);
1131
1032
          
1132
1033
          /* Block signal while modifying process_list */
1133
1034
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1135
1036
                                         &sigchld_action.sa_mask,
1136
1037
                                         NULL));
1137
1038
          if(ret < 0){
1138
 
            error(0, errno, "sigprocmask");
1139
 
            exitstatus = EX_OSERR;
 
1039
            perror("sigprocmask");
 
1040
            exitstatus = EXIT_FAILURE;
1140
1041
            goto fallback;
1141
1042
          }
1142
1043
          
1149
1050
                      (sigprocmask(SIG_UNBLOCK,
1150
1051
                                   &sigchld_action.sa_mask, NULL)));
1151
1052
          if(ret < 0){
1152
 
            error(0, errno, "sigprocmask");
1153
 
            exitstatus = EX_OSERR;
 
1053
            perror("sigprocmask");
 
1054
            exitstatus = EXIT_FAILURE;
1154
1055
            goto fallback;
1155
1056
          }
1156
1057
          
1166
1067
        bool bret = print_out_password(proc->buffer,
1167
1068
                                       proc->buffer_length);
1168
1069
        if(not bret){
1169
 
          error(0, errno, "print_out_password");
1170
 
          exitstatus = EX_IOERR;
 
1070
          perror("print_out_password");
 
1071
          exitstatus = EXIT_FAILURE;
1171
1072
        }
1172
1073
        goto fallback;
1173
1074
      }
1174
1075
      
1175
1076
      /* This process has not completed.  Does it have any output? */
1176
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1177
 
                                                         warning from
1178
 
                                                         -Wconversion */
 
1077
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1179
1078
        /* This process had nothing to say at this time */
1180
1079
        proc = proc->next;
1181
1080
        continue;
1185
1084
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1186
1085
                               + (size_t) BUFFER_SIZE);
1187
1086
        if(proc->buffer == NULL){
1188
 
          error(0, errno, "malloc");
1189
 
          exitstatus = EX_OSERR;
 
1087
          perror("malloc");
 
1088
          exitstatus = EXIT_FAILURE;
1190
1089
          goto fallback;
1191
1090
        }
1192
1091
        proc->buffer_size += BUFFER_SIZE;
1213
1112
  
1214
1113
 fallback:
1215
1114
  
1216
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1217
 
                             and exitstatus != EX_OK)){
 
1115
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1218
1116
    /* Fallback if all plugins failed, none are found or an error
1219
1117
       occured */
1220
1118
    bool bret;
1228
1126
    }
1229
1127
    bret = print_out_password(passwordbuffer, len);
1230
1128
    if(not bret){
1231
 
      error(0, errno, "print_out_password");
1232
 
      exitstatus = EX_IOERR;
 
1129
      perror("print_out_password");
 
1130
      exitstatus = EXIT_FAILURE;
1233
1131
    }
1234
1132
  }
1235
1133
  
1236
1134
  /* Restore old signal handler */
1237
1135
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1238
1136
  if(ret == -1){
1239
 
    error(0, errno, "sigaction");
1240
 
    exitstatus = EX_OSERR;
 
1137
    perror("sigaction");
 
1138
    exitstatus = EXIT_FAILURE;
1241
1139
  }
1242
1140
  
1243
1141
  if(custom_argv != NULL){
1258
1156
      ret = kill(p->pid, SIGTERM);
1259
1157
      if(ret == -1 and errno != ESRCH){
1260
1158
        /* Set-uid proccesses might not get closed */
1261
 
        error(0, errno, "kill");
 
1159
        perror("kill");
1262
1160
      }
1263
1161
    }
1264
1162
  }
1268
1166
    ret = wait(NULL);
1269
1167
  } while(ret >= 0);
1270
1168
  if(errno != ECHILD){
1271
 
    error(0, errno, "wait");
 
1169
    perror("wait");
1272
1170
  }
1273
1171
  
1274
1172
  free_plugin_list();