/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

Changed ForkingMixIn in favor of multiprocessing
Added approval functionallity

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf() */
 
26
                                   asprintf(), O_CLOEXEC */
27
27
#include <stddef.h>             /* size_t, NULL */
28
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
 
                                   EXIT_SUCCESS, realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, fdopendir(), 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, opendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
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() */
 
57
                                   strsignal(), strcmp(), strncmp() */
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 */
71
73
 
72
74
#define BUFFER_SIZE 256
73
75
 
102
104
/* Gets an existing plugin based on name,
103
105
   or if none is found, creates a new one */
104
106
static plugin *getplugin(char *name){
105
 
  /* Check for exiting plugin with that name */
 
107
  /* Check for existing plugin with that name */
106
108
  for(plugin *p = plugin_list; p != NULL; p = p->next){
107
109
    if((p->name == name)
108
110
       or (p->name and name and (strcmp(p->name, name) == 0))){
123
125
      copy_name = strdup(name);
124
126
    } while(copy_name == NULL and errno == EINTR);
125
127
    if(copy_name == NULL){
 
128
      int e = errno;
126
129
      free(new_plugin);
 
130
      errno = e;
127
131
      return NULL;
128
132
    }
129
133
  }
137
141
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
142
  } while(new_plugin->argv == NULL and errno == EINTR);
139
143
  if(new_plugin->argv == NULL){
 
144
    int e = errno;
140
145
    free(copy_name);
141
146
    free(new_plugin);
 
147
    errno = e;
142
148
    return NULL;
143
149
  }
144
150
  new_plugin->argv[0] = copy_name;
148
154
    new_plugin->environ = malloc(sizeof(char *));
149
155
  } while(new_plugin->environ == NULL and errno == EINTR);
150
156
  if(new_plugin->environ == NULL){
 
157
    int e = errno;
151
158
    free(copy_name);
152
159
    free(new_plugin->argv);
153
160
    free(new_plugin);
 
161
    errno = e;
154
162
    return NULL;
155
163
  }
156
164
  new_plugin->environ[0] = NULL;
230
238
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
231
239
 */
232
240
static int set_cloexec_flag(int fd){
233
 
  int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
241
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
234
242
  /* If reading the flags failed, return error indication now. */
235
243
  if(ret < 0){
236
244
    return ret;
237
245
  }
238
246
  /* Store modified flag word in the descriptor. */
239
 
  return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
 
247
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
248
                                       ret | FD_CLOEXEC));
240
249
}
241
250
 
242
251
 
349
358
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
350
359
  if(ret == -1){
351
360
    perror("sigaddset");
352
 
    exitstatus = EXIT_FAILURE;
 
361
    exitstatus = EX_OSERR;
353
362
    goto fallback;
354
363
  }
355
364
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
356
365
  if(ret == -1){
357
366
    perror("sigaction");
358
 
    exitstatus = EXIT_FAILURE;
 
367
    exitstatus = EX_OSERR;
359
368
    goto fallback;
360
369
  }
361
370
  
393
402
      .doc = "Group ID the plugins will run as", .group = 3 },
394
403
    { .name = "debug", .key = 132,
395
404
      .doc = "Debug mode", .group = 4 },
 
405
    /*
 
406
     * These reproduce what we would get without ARGP_NO_HELP
 
407
     */
 
408
    { .name = "help", .key = '?',
 
409
      .doc = "Give this help list", .group = -1 },
 
410
    { .name = "usage", .key = -3,
 
411
      .doc = "Give a short usage message", .group = -1 },
 
412
    { .name = "version", .key = 'V',
 
413
      .doc = "Print program version", .group = -1 },
396
414
    { .name = NULL }
397
415
  };
398
416
  
399
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
400
 
                    struct argp_state *state){
 
417
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
418
    errno = 0;
401
419
    switch(key){
402
420
      char *tmp;
403
421
      intmax_t tmpmax;
404
422
    case 'g':                   /* --global-options */
405
 
      if(arg != NULL){
 
423
      {
406
424
        char *plugin_option;
407
425
        while((plugin_option = strsep(&arg, ",")) != NULL){
408
 
          if(plugin_option[0] == '\0'){
409
 
            continue;
410
 
          }
411
426
          if(not add_argument(getplugin(NULL), plugin_option)){
412
 
            perror("add_argument");
413
 
            return ARGP_ERR_UNKNOWN;
 
427
            break;
414
428
          }
415
429
        }
416
430
      }
417
431
      break;
418
432
    case 'G':                   /* --global-env */
419
 
      if(arg == NULL){
420
 
        break;
421
 
      }
422
 
      if(not add_environment(getplugin(NULL), arg, true)){
423
 
        perror("add_environment");
424
 
      }
 
433
      add_environment(getplugin(NULL), arg, true);
425
434
      break;
426
435
    case 'o':                   /* --options-for */
427
 
      if(arg != NULL){
428
 
        char *plugin_name = strsep(&arg, ":");
429
 
        if(plugin_name[0] == '\0'){
430
 
          break;
431
 
        }
432
 
        char *plugin_option;
433
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
434
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
435
 
            perror("add_argument");
436
 
            return ARGP_ERR_UNKNOWN;
 
436
      {
 
437
        char *option_list = strchr(arg, ':');
 
438
        if(option_list == NULL){
 
439
          argp_error(state, "No colon in \"%s\"", arg);
 
440
          errno = EINVAL;
 
441
          break;
 
442
        }
 
443
        *option_list = '\0';
 
444
        option_list++;
 
445
        if(arg[0] == '\0'){
 
446
          argp_error(state, "Empty plugin name");
 
447
          errno = EINVAL;
 
448
          break;
 
449
        }
 
450
        char *option;
 
451
        while((option = strsep(&option_list, ",")) != NULL){
 
452
          if(not add_argument(getplugin(arg), option)){
 
453
            break;
437
454
          }
438
455
        }
439
456
      }
440
457
      break;
441
458
    case 'E':                   /* --env-for */
442
 
      if(arg == NULL){
443
 
        break;
444
 
      }
445
459
      {
446
460
        char *envdef = strchr(arg, ':');
447
461
        if(envdef == NULL){
 
462
          argp_error(state, "No colon in \"%s\"", arg);
 
463
          errno = EINVAL;
448
464
          break;
449
465
        }
450
466
        *envdef = '\0';
451
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
452
 
          perror("add_environment");
 
467
        envdef++;
 
468
        if(arg[0] == '\0'){
 
469
          argp_error(state, "Empty plugin name");
 
470
          errno = EINVAL;
 
471
          break;
453
472
        }
 
473
        add_environment(getplugin(arg), envdef, true);
454
474
      }
455
475
      break;
456
476
    case 'd':                   /* --disable */
457
 
      if(arg != NULL){
 
477
      {
458
478
        plugin *p = getplugin(arg);
459
 
        if(p == NULL){
460
 
          return ARGP_ERR_UNKNOWN;
 
479
        if(p != NULL){
 
480
          p->disabled = true;
461
481
        }
462
 
        p->disabled = true;
463
482
      }
464
483
      break;
465
484
    case 'e':                   /* --enable */
466
 
      if(arg != NULL){
 
485
      {
467
486
        plugin *p = getplugin(arg);
468
 
        if(p == NULL){
469
 
          return ARGP_ERR_UNKNOWN;
 
487
        if(p != NULL){
 
488
          p->disabled = false;
470
489
        }
471
 
        p->disabled = false;
472
490
      }
473
491
      break;
474
492
    case 128:                   /* --plugin-dir */
475
493
      free(plugindir);
476
494
      plugindir = strdup(arg);
477
 
      if(plugindir == NULL){
478
 
        perror("strdup");
479
 
      }
480
495
      break;
481
496
    case 129:                   /* --config-file */
482
497
      /* This is already done by parse_opt_config_file() */
483
498
      break;
484
499
    case 130:                   /* --userid */
485
 
      errno = 0;
486
500
      tmpmax = strtoimax(arg, &tmp, 10);
487
501
      if(errno != 0 or tmp == arg or *tmp != '\0'
488
502
         or tmpmax != (uid_t)tmpmax){
489
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
490
 
                PRIdMAX "\n", arg, (intmax_t)uid);
491
 
      } else {
492
 
        uid = (uid_t)tmpmax;
 
503
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
504
                   PRIdMAX, arg, (intmax_t)uid);
 
505
        break;
493
506
      }
 
507
      uid = (uid_t)tmpmax;
494
508
      break;
495
509
    case 131:                   /* --groupid */
496
 
      errno = 0;
497
510
      tmpmax = strtoimax(arg, &tmp, 10);
498
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
499
512
         or tmpmax != (gid_t)tmpmax){
500
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
501
 
                PRIdMAX "\n", arg, (intmax_t)gid);
502
 
      } else {
503
 
        gid = (gid_t)tmpmax;
 
513
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
514
                   PRIdMAX, arg, (intmax_t)gid);
 
515
        break;
504
516
      }
 
517
      gid = (gid_t)tmpmax;
505
518
      break;
506
519
    case 132:                   /* --debug */
507
520
      debug = true;
508
521
      break;
 
522
      /*
 
523
       * These reproduce what we would get without ARGP_NO_HELP
 
524
       */
 
525
    case '?':                   /* --help */
 
526
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
527
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
528
    case -3:                    /* --usage */
 
529
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
530
      argp_state_help(state, state->out_stream,
 
531
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
532
    case 'V':                   /* --version */
 
533
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
534
      exit(EXIT_SUCCESS);
 
535
      break;
509
536
/*
510
537
 * When adding more options before this line, remember to also add a
511
538
 * "case" to the "parse_opt_config_file" function below.
514
541
      /* Cryptsetup always passes an argument, which is an empty
515
542
         string if "none" was specified in /etc/crypttab.  So if
516
543
         argument was empty, we ignore it silently. */
517
 
      if(arg[0] != '\0'){
518
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
544
      if(arg[0] == '\0'){
 
545
        break;
519
546
      }
520
 
      break;
521
 
    case ARGP_KEY_END:
522
 
      break;
523
547
    default:
524
548
      return ARGP_ERR_UNKNOWN;
525
549
    }
526
 
    return 0;
 
550
    return errno;               /* Set to 0 at start */
527
551
  }
528
552
  
529
553
  /* This option parser is the same as parse_opt() above, except it
531
555
  error_t parse_opt_config_file(int key, char *arg,
532
556
                                __attribute__((unused))
533
557
                                struct argp_state *state){
 
558
    errno = 0;
534
559
    switch(key){
535
560
    case 'g':                   /* --global-options */
536
561
    case 'G':                   /* --global-env */
543
568
    case 129:                   /* --config-file */
544
569
      free(argfile);
545
570
      argfile = strdup(arg);
546
 
      if(argfile == NULL){
547
 
        perror("strdup");
548
 
      }
549
571
      break;
550
572
    case 130:                   /* --userid */
551
573
    case 131:                   /* --groupid */
552
574
    case 132:                   /* --debug */
 
575
    case '?':                   /* --help */
 
576
    case -3:                    /* --usage */
 
577
    case 'V':                   /* --version */
553
578
    case ARGP_KEY_ARG:
554
 
    case ARGP_KEY_END:
555
579
      break;
556
580
    default:
557
581
      return ARGP_ERR_UNKNOWN;
558
582
    }
559
 
    return 0;
 
583
    return errno;
560
584
  }
561
585
  
562
586
  struct argp argp = { .options = options,
566
590
  
567
591
  /* Parse using parse_opt_config_file() in order to get the custom
568
592
     config file location, if any. */
569
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
570
 
  if(ret == ARGP_ERR_UNKNOWN){
571
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
572
 
    exitstatus = EXIT_FAILURE;
 
593
  ret = argp_parse(&argp, argc, argv,
 
594
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
595
                   NULL, NULL);
 
596
  switch(ret){
 
597
  case 0:
 
598
    break;
 
599
  case ENOMEM:
 
600
  default:
 
601
    errno = ret;
 
602
    perror("argp_parse");
 
603
    exitstatus = EX_OSERR;
 
604
    goto fallback;
 
605
  case EINVAL:
 
606
    exitstatus = EX_USAGE;
573
607
    goto fallback;
574
608
  }
575
609
  
593
627
    custom_argv = malloc(sizeof(char*) * 2);
594
628
    if(custom_argv == NULL){
595
629
      perror("malloc");
596
 
      exitstatus = EXIT_FAILURE;
 
630
      exitstatus = EX_OSERR;
597
631
      goto fallback;
598
632
    }
599
633
    custom_argv[0] = argv[0];
616
650
        new_arg = strdup(p);
617
651
        if(new_arg == NULL){
618
652
          perror("strdup");
619
 
          exitstatus = EXIT_FAILURE;
 
653
          exitstatus = EX_OSERR;
620
654
          free(org_line);
621
655
          goto fallback;
622
656
        }
626
660
                              * ((unsigned int) custom_argc + 1));
627
661
        if(custom_argv == NULL){
628
662
          perror("realloc");
629
 
          exitstatus = EXIT_FAILURE;
 
663
          exitstatus = EX_OSERR;
630
664
          free(org_line);
631
665
          goto fallback;
632
666
        }
639
673
    } while(ret == EOF and errno == EINTR);
640
674
    if(ret == EOF){
641
675
      perror("fclose");
642
 
      exitstatus = EXIT_FAILURE;
 
676
      exitstatus = EX_IOERR;
643
677
      goto fallback;
644
678
    }
645
679
    free(org_line);
648
682
       not affect opening plugins */
649
683
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
650
684
      perror("fopen");
651
 
      exitstatus = EXIT_FAILURE;
 
685
      exitstatus = EX_OSERR;
652
686
      goto fallback;
653
687
    }
654
688
  }
655
 
  /* If there was any arguments from configuration file,
656
 
     pass them to parser as command arguments */
 
689
  /* If there were any arguments from the configuration file, pass
 
690
     them to parser as command line arguments */
657
691
  if(custom_argv != NULL){
658
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
659
 
                     0, NULL);
660
 
    if(ret == ARGP_ERR_UNKNOWN){
661
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
662
 
      exitstatus = EXIT_FAILURE;
 
692
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
693
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
694
                     NULL, NULL);
 
695
    switch(ret){
 
696
    case 0:
 
697
      break;
 
698
    case ENOMEM:
 
699
    default:
 
700
      errno = ret;
 
701
      perror("argp_parse");
 
702
      exitstatus = EX_OSERR;
 
703
      goto fallback;
 
704
    case EINVAL:
 
705
      exitstatus = EX_CONFIG;
663
706
      goto fallback;
664
707
    }
665
708
  }
666
709
  
667
710
  /* Parse actual command line arguments, to let them override the
668
711
     config file */
669
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
670
 
  if(ret == ARGP_ERR_UNKNOWN){
671
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
672
 
    exitstatus = EXIT_FAILURE;
 
712
  ret = argp_parse(&argp, argc, argv,
 
713
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
714
                   NULL, NULL);
 
715
  switch(ret){
 
716
  case 0:
 
717
    break;
 
718
  case ENOMEM:
 
719
  default:
 
720
    errno = ret;
 
721
    perror("argp_parse");
 
722
    exitstatus = EX_OSERR;
 
723
    goto fallback;
 
724
  case EINVAL:
 
725
    exitstatus = EX_USAGE;
673
726
    goto fallback;
674
727
  }
675
728
  
697
750
    perror("setuid");
698
751
  }
699
752
  
700
 
  if(plugindir == NULL){
701
 
    dir = opendir(PDIR);
702
 
  } else {
703
 
    dir = opendir(plugindir);
704
 
  }
705
 
  
706
 
  if(dir == NULL){
707
 
    perror("Could not open plugin dir");
708
 
    exitstatus = EXIT_FAILURE;
709
 
    goto fallback;
710
 
  }
711
 
  
712
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
753
  /* Open plugin directory with close_on_exec flag */
713
754
  {
714
 
    int dir_fd = dirfd(dir);
715
 
    if(dir_fd >= 0){
716
 
      ret = set_cloexec_flag(dir_fd);
717
 
      if(ret < 0){
718
 
        perror("set_cloexec_flag");
719
 
        exitstatus = EXIT_FAILURE;
720
 
        goto fallback;
721
 
      }
 
755
    int dir_fd = -1;
 
756
    if(plugindir == NULL){
 
757
      dir_fd = open(PDIR, O_RDONLY |
 
758
#ifdef O_CLOEXEC
 
759
                    O_CLOEXEC
 
760
#else  /* not O_CLOEXEC */
 
761
                    0
 
762
#endif  /* not O_CLOEXEC */
 
763
                    );
 
764
    } else {
 
765
      dir_fd = open(plugindir, O_RDONLY |
 
766
#ifdef O_CLOEXEC
 
767
                    O_CLOEXEC
 
768
#else  /* not O_CLOEXEC */
 
769
                    0
 
770
#endif  /* not O_CLOEXEC */
 
771
                    );
 
772
    }
 
773
    if(dir_fd == -1){
 
774
      perror("Could not open plugin dir");
 
775
      exitstatus = EX_UNAVAILABLE;
 
776
      goto fallback;
 
777
    }
 
778
    
 
779
#ifndef O_CLOEXEC
 
780
  /* Set the FD_CLOEXEC flag on the directory */
 
781
    ret = set_cloexec_flag(dir_fd);
 
782
    if(ret < 0){
 
783
      perror("set_cloexec_flag");
 
784
      TEMP_FAILURE_RETRY(close(dir_fd));
 
785
      exitstatus = EX_OSERR;
 
786
      goto fallback;
 
787
    }
 
788
#endif  /* O_CLOEXEC */
 
789
    
 
790
    dir = fdopendir(dir_fd);
 
791
    if(dir == NULL){
 
792
      perror("Could not open plugin dir");
 
793
      TEMP_FAILURE_RETRY(close(dir_fd));
 
794
      exitstatus = EX_OSERR;
 
795
      goto fallback;
722
796
    }
723
797
  }
724
798
  
734
808
    if(dirst == NULL){
735
809
      if(errno == EBADF){
736
810
        perror("readdir");
737
 
        exitstatus = EXIT_FAILURE;
 
811
        exitstatus = EX_IOERR;
738
812
        goto fallback;
739
813
      }
740
814
      break;
770
844
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
771
845
        size_t suf_len = strlen(*suf);
772
846
        if((d_name_len >= suf_len)
773
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
847
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
774
848
                == 0)){
775
849
          if(debug){
776
850
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
788
862
    
789
863
    char *filename;
790
864
    if(plugindir == NULL){
791
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
792
 
                                        dirst->d_name));
 
865
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
866
                                             dirst->d_name));
793
867
    } else {
794
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
795
 
                                        dirst->d_name));
 
868
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
869
                                             plugindir,
 
870
                                             dirst->d_name));
796
871
    }
797
872
    if(ret < 0){
798
873
      perror("asprintf");
799
874
      continue;
800
875
    }
801
876
    
802
 
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
 
877
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
803
878
    if(ret == -1){
804
879
      perror("stat");
805
880
      free(filename);
860
935
    }
861
936
    
862
937
    int pipefd[2];
863
 
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
 
938
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
864
939
    if(ret == -1){
865
940
      perror("pipe");
866
 
      exitstatus = EXIT_FAILURE;
 
941
      exitstatus = EX_OSERR;
867
942
      goto fallback;
868
943
    }
869
944
    /* Ask OS to automatic close the pipe on exec */
870
945
    ret = set_cloexec_flag(pipefd[0]);
871
946
    if(ret < 0){
872
947
      perror("set_cloexec_flag");
873
 
      exitstatus = EXIT_FAILURE;
 
948
      exitstatus = EX_OSERR;
874
949
      goto fallback;
875
950
    }
876
951
    ret = set_cloexec_flag(pipefd[1]);
877
952
    if(ret < 0){
878
953
      perror("set_cloexec_flag");
879
 
      exitstatus = EXIT_FAILURE;
 
954
      exitstatus = EX_OSERR;
880
955
      goto fallback;
881
956
    }
882
957
    /* Block SIGCHLD until process is safely in process list */
883
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
884
 
                                         &sigchld_action.sa_mask,
885
 
                                         NULL));
 
958
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
959
                                              &sigchld_action.sa_mask,
 
960
                                              NULL));
886
961
    if(ret < 0){
887
962
      perror("sigprocmask");
888
 
      exitstatus = EXIT_FAILURE;
 
963
      exitstatus = EX_OSERR;
889
964
      goto fallback;
890
965
    }
891
966
    /* Starting a new process to be watched */
895
970
    } while(pid == -1 and errno == EINTR);
896
971
    if(pid == -1){
897
972
      perror("fork");
898
 
      exitstatus = EXIT_FAILURE;
 
973
      exitstatus = EX_OSERR;
899
974
      goto fallback;
900
975
    }
901
976
    if(pid == 0){
903
978
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
904
979
      if(ret < 0){
905
980
        perror("sigaction");
906
 
        _exit(EXIT_FAILURE);
 
981
        _exit(EX_OSERR);
907
982
      }
908
983
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
909
984
      if(ret < 0){
910
985
        perror("sigprocmask");
911
 
        _exit(EXIT_FAILURE);
 
986
        _exit(EX_OSERR);
912
987
      }
913
988
      
914
989
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
915
990
      if(ret == -1){
916
991
        perror("dup2");
917
 
        _exit(EXIT_FAILURE);
 
992
        _exit(EX_OSERR);
918
993
      }
919
994
      
920
995
      if(dirfd(dir) < 0){
925
1000
      if(p->environ[0] == NULL){
926
1001
        if(execv(filename, p->argv) < 0){
927
1002
          perror("execv");
928
 
          _exit(EXIT_FAILURE);
 
1003
          _exit(EX_OSERR);
929
1004
        }
930
1005
      } else {
931
1006
        if(execve(filename, p->argv, p->environ) < 0){
932
1007
          perror("execve");
933
 
          _exit(EXIT_FAILURE);
 
1008
          _exit(EX_OSERR);
934
1009
        }
935
1010
      }
936
1011
      /* no return */
942
1017
    plugin *new_plugin = getplugin(dirst->d_name);
943
1018
    if(new_plugin == NULL){
944
1019
      perror("getplugin");
945
 
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
946
 
                                           &sigchld_action.sa_mask,
947
 
                                           NULL));
 
1020
      ret = (int)(TEMP_FAILURE_RETRY
 
1021
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1022
                               NULL)));
948
1023
      if(ret < 0){
949
1024
        perror("sigprocmask");
950
1025
      }
951
 
      exitstatus = EXIT_FAILURE;
 
1026
      exitstatus = EX_OSERR;
952
1027
      goto fallback;
953
1028
    }
954
1029
    
957
1032
    
958
1033
    /* Unblock SIGCHLD so signal handler can be run if this process
959
1034
       has already completed */
960
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
961
 
                                         &sigchld_action.sa_mask,
962
 
                                         NULL));
 
1035
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1036
                                              &sigchld_action.sa_mask,
 
1037
                                              NULL));
963
1038
    if(ret < 0){
964
1039
      perror("sigprocmask");
965
 
      exitstatus = EXIT_FAILURE;
 
1040
      exitstatus = EX_OSERR;
966
1041
      goto fallback;
967
1042
    }
968
1043
    
969
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1044
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1045
                                          -Wconversion */
970
1046
    
971
1047
    if(maxfd < new_plugin->fd){
972
1048
      maxfd = new_plugin->fd;
994
1070
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
995
1071
    if(select_ret == -1 and errno != EINTR){
996
1072
      perror("select");
997
 
      exitstatus = EXIT_FAILURE;
 
1073
      exitstatus = EX_OSERR;
998
1074
      goto fallback;
999
1075
    }
1000
1076
    /* OK, now either a process completed, or something can be read
1026
1102
          }
1027
1103
          
1028
1104
          /* Remove the plugin */
1029
 
          FD_CLR(proc->fd, &rfds_all);
 
1105
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1106
                                          -Wconversion */
1030
1107
          
1031
1108
          /* Block signal while modifying process_list */
1032
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1033
 
                                               &sigchld_action.sa_mask,
1034
 
                                               NULL));
 
1109
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1110
                                        (SIG_BLOCK,
 
1111
                                         &sigchld_action.sa_mask,
 
1112
                                         NULL));
1035
1113
          if(ret < 0){
1036
1114
            perror("sigprocmask");
1037
 
            exitstatus = EXIT_FAILURE;
 
1115
            exitstatus = EX_OSERR;
1038
1116
            goto fallback;
1039
1117
          }
1040
1118
          
1043
1121
          proc = next_plugin;
1044
1122
          
1045
1123
          /* We are done modifying process list, so unblock signal */
1046
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1047
 
                                               &sigchld_action.sa_mask,
1048
 
                                               NULL));
 
1124
          ret = (int)(TEMP_FAILURE_RETRY
 
1125
                      (sigprocmask(SIG_UNBLOCK,
 
1126
                                   &sigchld_action.sa_mask, NULL)));
1049
1127
          if(ret < 0){
1050
1128
            perror("sigprocmask");
1051
 
            exitstatus = EXIT_FAILURE;
 
1129
            exitstatus = EX_OSERR;
1052
1130
            goto fallback;
1053
1131
          }
1054
1132
          
1065
1143
                                       proc->buffer_length);
1066
1144
        if(not bret){
1067
1145
          perror("print_out_password");
1068
 
          exitstatus = EXIT_FAILURE;
 
1146
          exitstatus = EX_IOERR;
1069
1147
        }
1070
1148
        goto fallback;
1071
1149
      }
1072
1150
      
1073
1151
      /* This process has not completed.  Does it have any output? */
1074
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1152
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1153
                                                         warning from
 
1154
                                                         -Wconversion */
1075
1155
        /* This process had nothing to say at this time */
1076
1156
        proc = proc->next;
1077
1157
        continue;
1082
1162
                               + (size_t) BUFFER_SIZE);
1083
1163
        if(proc->buffer == NULL){
1084
1164
          perror("malloc");
1085
 
          exitstatus = EXIT_FAILURE;
 
1165
          exitstatus = EX_OSERR;
1086
1166
          goto fallback;
1087
1167
        }
1088
1168
        proc->buffer_size += BUFFER_SIZE;
1109
1189
  
1110
1190
 fallback:
1111
1191
  
1112
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1192
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1193
                             and exitstatus != EX_OK)){
1113
1194
    /* Fallback if all plugins failed, none are found or an error
1114
1195
       occured */
1115
1196
    bool bret;
1124
1205
    bret = print_out_password(passwordbuffer, len);
1125
1206
    if(not bret){
1126
1207
      perror("print_out_password");
1127
 
      exitstatus = EXIT_FAILURE;
 
1208
      exitstatus = EX_IOERR;
1128
1209
    }
1129
1210
  }
1130
1211
  
1132
1213
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1133
1214
  if(ret == -1){
1134
1215
    perror("sigaction");
1135
 
    exitstatus = EXIT_FAILURE;
 
1216
    exitstatus = EX_OSERR;
1136
1217
  }
1137
1218
  
1138
1219
  if(custom_argv != NULL){