/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
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
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
#include <stdio.h>              /* fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
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 */
 
73
#include <errno.h>              /* errno */
 
74
#include <error.h>              /* error() */
71
75
 
72
76
#define BUFFER_SIZE 256
73
77
 
102
106
/* Gets an existing plugin based on name,
103
107
   or if none is found, creates a new one */
104
108
static plugin *getplugin(char *name){
105
 
  /* Check for exiting plugin with that name */
 
109
  /* Check for existing plugin with that name */
106
110
  for(plugin *p = plugin_list; p != NULL; p = p->next){
107
111
    if((p->name == name)
108
112
       or (p->name and name and (strcmp(p->name, name) == 0))){
123
127
      copy_name = strdup(name);
124
128
    } while(copy_name == NULL and errno == EINTR);
125
129
    if(copy_name == NULL){
 
130
      int e = errno;
126
131
      free(new_plugin);
 
132
      errno = e;
127
133
      return NULL;
128
134
    }
129
135
  }
137
143
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
144
  } while(new_plugin->argv == NULL and errno == EINTR);
139
145
  if(new_plugin->argv == NULL){
 
146
    int e = errno;
140
147
    free(copy_name);
141
148
    free(new_plugin);
 
149
    errno = e;
142
150
    return NULL;
143
151
  }
144
152
  new_plugin->argv[0] = copy_name;
148
156
    new_plugin->environ = malloc(sizeof(char *));
149
157
  } while(new_plugin->environ == NULL and errno == EINTR);
150
158
  if(new_plugin->environ == NULL){
 
159
    int e = errno;
151
160
    free(copy_name);
152
161
    free(new_plugin->argv);
153
162
    free(new_plugin);
 
163
    errno = e;
154
164
    return NULL;
155
165
  }
156
166
  new_plugin->environ[0] = NULL;
258
268
        /* No child processes */
259
269
        break;
260
270
      }
261
 
      perror("waitpid");
 
271
      error(0, errno, "waitpid");
262
272
    }
263
273
    
264
274
    /* A child exited, find it in process_list */
349
359
  sigemptyset(&sigchld_action.sa_mask);
350
360
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
351
361
  if(ret == -1){
352
 
    perror("sigaddset");
353
 
    exitstatus = EXIT_FAILURE;
 
362
    error(0, errno, "sigaddset");
 
363
    exitstatus = EX_OSERR;
354
364
    goto fallback;
355
365
  }
356
366
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
357
367
  if(ret == -1){
358
 
    perror("sigaction");
359
 
    exitstatus = EXIT_FAILURE;
 
368
    error(0, errno, "sigaction");
 
369
    exitstatus = EX_OSERR;
360
370
    goto fallback;
361
371
  }
362
372
  
394
404
      .doc = "Group ID the plugins will run as", .group = 3 },
395
405
    { .name = "debug", .key = 132,
396
406
      .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 },
397
416
    { .name = NULL }
398
417
  };
399
418
  
400
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
401
 
                    struct argp_state *state){
 
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
420
    errno = 0;
402
421
    switch(key){
403
422
      char *tmp;
404
423
      intmax_t tmpmax;
405
424
    case 'g':                   /* --global-options */
406
 
      if(arg != NULL){
 
425
      {
407
426
        char *plugin_option;
408
427
        while((plugin_option = strsep(&arg, ",")) != NULL){
409
 
          if(plugin_option[0] == '\0'){
410
 
            continue;
411
 
          }
412
428
          if(not add_argument(getplugin(NULL), plugin_option)){
413
 
            perror("add_argument");
414
 
            return ARGP_ERR_UNKNOWN;
 
429
            break;
415
430
          }
416
431
        }
417
432
      }
418
433
      break;
419
434
    case 'G':                   /* --global-env */
420
 
      if(arg == NULL){
421
 
        break;
422
 
      }
423
 
      if(not add_environment(getplugin(NULL), arg, true)){
424
 
        perror("add_environment");
425
 
      }
 
435
      add_environment(getplugin(NULL), arg, true);
426
436
      break;
427
437
    case 'o':                   /* --options-for */
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;
 
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;
438
456
          }
439
457
        }
440
458
      }
441
459
      break;
442
460
    case 'E':                   /* --env-for */
443
 
      if(arg == NULL){
444
 
        break;
445
 
      }
446
461
      {
447
462
        char *envdef = strchr(arg, ':');
448
463
        if(envdef == NULL){
 
464
          argp_error(state, "No colon in \"%s\"", arg);
 
465
          errno = EINVAL;
449
466
          break;
450
467
        }
451
468
        *envdef = '\0';
452
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
453
 
          perror("add_environment");
 
469
        envdef++;
 
470
        if(arg[0] == '\0'){
 
471
          argp_error(state, "Empty plugin name");
 
472
          errno = EINVAL;
 
473
          break;
454
474
        }
 
475
        add_environment(getplugin(arg), envdef, true);
455
476
      }
456
477
      break;
457
478
    case 'd':                   /* --disable */
458
 
      if(arg != NULL){
 
479
      {
459
480
        plugin *p = getplugin(arg);
460
 
        if(p == NULL){
461
 
          return ARGP_ERR_UNKNOWN;
 
481
        if(p != NULL){
 
482
          p->disabled = true;
462
483
        }
463
 
        p->disabled = true;
464
484
      }
465
485
      break;
466
486
    case 'e':                   /* --enable */
467
 
      if(arg != NULL){
 
487
      {
468
488
        plugin *p = getplugin(arg);
469
 
        if(p == NULL){
470
 
          return ARGP_ERR_UNKNOWN;
 
489
        if(p != NULL){
 
490
          p->disabled = false;
471
491
        }
472
 
        p->disabled = false;
473
492
      }
474
493
      break;
475
494
    case 128:                   /* --plugin-dir */
476
495
      free(plugindir);
477
496
      plugindir = strdup(arg);
478
 
      if(plugindir == NULL){
479
 
        perror("strdup");
480
 
      }
481
497
      break;
482
498
    case 129:                   /* --config-file */
483
499
      /* This is already done by parse_opt_config_file() */
484
500
      break;
485
501
    case 130:                   /* --userid */
486
 
      errno = 0;
487
502
      tmpmax = strtoimax(arg, &tmp, 10);
488
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
489
504
         or tmpmax != (uid_t)tmpmax){
490
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
491
 
                PRIdMAX "\n", arg, (intmax_t)uid);
492
 
      } else {
493
 
        uid = (uid_t)tmpmax;
 
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
506
                   PRIdMAX, arg, (intmax_t)uid);
 
507
        break;
494
508
      }
 
509
      uid = (uid_t)tmpmax;
495
510
      break;
496
511
    case 131:                   /* --groupid */
497
 
      errno = 0;
498
512
      tmpmax = strtoimax(arg, &tmp, 10);
499
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
500
514
         or tmpmax != (gid_t)tmpmax){
501
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
502
 
                PRIdMAX "\n", arg, (intmax_t)gid);
503
 
      } else {
504
 
        gid = (gid_t)tmpmax;
 
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
516
                   PRIdMAX, arg, (intmax_t)gid);
 
517
        break;
505
518
      }
 
519
      gid = (gid_t)tmpmax;
506
520
      break;
507
521
    case 132:                   /* --debug */
508
522
      debug = true;
509
523
      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;
510
538
/*
511
539
 * When adding more options before this line, remember to also add a
512
540
 * "case" to the "parse_opt_config_file" function below.
515
543
      /* Cryptsetup always passes an argument, which is an empty
516
544
         string if "none" was specified in /etc/crypttab.  So if
517
545
         argument was empty, we ignore it silently. */
518
 
      if(arg[0] != '\0'){
519
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
546
      if(arg[0] == '\0'){
 
547
        break;
520
548
      }
521
 
      break;
522
 
    case ARGP_KEY_END:
523
 
      break;
524
549
    default:
525
550
      return ARGP_ERR_UNKNOWN;
526
551
    }
527
 
    return 0;
 
552
    return errno;               /* Set to 0 at start */
528
553
  }
529
554
  
530
555
  /* This option parser is the same as parse_opt() above, except it
532
557
  error_t parse_opt_config_file(int key, char *arg,
533
558
                                __attribute__((unused))
534
559
                                struct argp_state *state){
 
560
    errno = 0;
535
561
    switch(key){
536
562
    case 'g':                   /* --global-options */
537
563
    case 'G':                   /* --global-env */
544
570
    case 129:                   /* --config-file */
545
571
      free(argfile);
546
572
      argfile = strdup(arg);
547
 
      if(argfile == NULL){
548
 
        perror("strdup");
549
 
      }
550
573
      break;
551
574
    case 130:                   /* --userid */
552
575
    case 131:                   /* --groupid */
553
576
    case 132:                   /* --debug */
 
577
    case '?':                   /* --help */
 
578
    case -3:                    /* --usage */
 
579
    case 'V':                   /* --version */
554
580
    case ARGP_KEY_ARG:
555
 
    case ARGP_KEY_END:
556
581
      break;
557
582
    default:
558
583
      return ARGP_ERR_UNKNOWN;
559
584
    }
560
 
    return 0;
 
585
    return errno;
561
586
  }
562
587
  
563
588
  struct argp argp = { .options = options,
567
592
  
568
593
  /* Parse using parse_opt_config_file() in order to get the custom
569
594
     config file location, if any. */
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;
 
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;
574
609
    goto fallback;
575
610
  }
576
611
  
593
628
    custom_argc = 1;
594
629
    custom_argv = malloc(sizeof(char*) * 2);
595
630
    if(custom_argv == NULL){
596
 
      perror("malloc");
597
 
      exitstatus = EXIT_FAILURE;
 
631
      error(0, errno, "malloc");
 
632
      exitstatus = EX_OSERR;
598
633
      goto fallback;
599
634
    }
600
635
    custom_argv[0] = argv[0];
616
651
        }
617
652
        new_arg = strdup(p);
618
653
        if(new_arg == NULL){
619
 
          perror("strdup");
620
 
          exitstatus = EXIT_FAILURE;
 
654
          error(0, errno, "strdup");
 
655
          exitstatus = EX_OSERR;
621
656
          free(org_line);
622
657
          goto fallback;
623
658
        }
626
661
        custom_argv = realloc(custom_argv, sizeof(char *)
627
662
                              * ((unsigned int) custom_argc + 1));
628
663
        if(custom_argv == NULL){
629
 
          perror("realloc");
630
 
          exitstatus = EXIT_FAILURE;
 
664
          error(0, errno, "realloc");
 
665
          exitstatus = EX_OSERR;
631
666
          free(org_line);
632
667
          goto fallback;
633
668
        }
639
674
      ret = fclose(conffp);
640
675
    } while(ret == EOF and errno == EINTR);
641
676
    if(ret == EOF){
642
 
      perror("fclose");
643
 
      exitstatus = EXIT_FAILURE;
 
677
      error(0, errno, "fclose");
 
678
      exitstatus = EX_IOERR;
644
679
      goto fallback;
645
680
    }
646
681
    free(org_line);
648
683
    /* Check for harmful errors and go to fallback. Other errors might
649
684
       not affect opening plugins */
650
685
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
651
 
      perror("fopen");
652
 
      exitstatus = EXIT_FAILURE;
 
686
      error(0, errno, "fopen");
 
687
      exitstatus = EX_OSERR;
653
688
      goto fallback;
654
689
    }
655
690
  }
656
 
  /* If there was any arguments from configuration file,
657
 
     pass them to parser as command arguments */
 
691
  /* If there were any arguments from the configuration file, pass
 
692
     them to parser as command line arguments */
658
693
  if(custom_argv != NULL){
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;
 
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;
664
708
      goto fallback;
665
709
    }
666
710
  }
667
711
  
668
712
  /* Parse actual command line arguments, to let them override the
669
713
     config file */
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;
 
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;
674
728
    goto fallback;
675
729
  }
676
730
  
691
745
  /* Strip permissions down to nobody */
692
746
  setgid(gid);
693
747
  if(ret == -1){
694
 
    perror("setgid");
 
748
    error(0, errno, "setgid");
695
749
  }
696
750
  ret = setuid(uid);
697
751
  if(ret == -1){
698
 
    perror("setuid");
 
752
    error(0, errno, "setuid");
699
753
  }
700
754
  
701
755
  /* Open plugin directory with close_on_exec flag */
719
773
                    );
720
774
    }
721
775
    if(dir_fd == -1){
722
 
      perror("Could not open plugin dir");
723
 
      exitstatus = EXIT_FAILURE;
 
776
      error(0, errno, "Could not open plugin dir");
 
777
      exitstatus = EX_UNAVAILABLE;
724
778
      goto fallback;
725
779
    }
726
780
    
728
782
  /* Set the FD_CLOEXEC flag on the directory */
729
783
    ret = set_cloexec_flag(dir_fd);
730
784
    if(ret < 0){
731
 
      perror("set_cloexec_flag");
 
785
      error(0, errno, "set_cloexec_flag");
732
786
      TEMP_FAILURE_RETRY(close(dir_fd));
733
 
      exitstatus = EXIT_FAILURE;
 
787
      exitstatus = EX_OSERR;
734
788
      goto fallback;
735
789
    }
736
790
#endif  /* O_CLOEXEC */
737
791
    
738
792
    dir = fdopendir(dir_fd);
739
793
    if(dir == NULL){
740
 
      perror("Could not open plugin dir");
 
794
      error(0, errno, "Could not open plugin dir");
741
795
      TEMP_FAILURE_RETRY(close(dir_fd));
742
 
      exitstatus = EXIT_FAILURE;
 
796
      exitstatus = EX_OSERR;
743
797
      goto fallback;
744
798
    }
745
799
  }
755
809
    /* All directory entries have been processed */
756
810
    if(dirst == NULL){
757
811
      if(errno == EBADF){
758
 
        perror("readdir");
759
 
        exitstatus = EXIT_FAILURE;
 
812
        error(0, errno, "readdir");
 
813
        exitstatus = EX_IOERR;
760
814
        goto fallback;
761
815
      }
762
816
      break;
792
846
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
793
847
        size_t suf_len = strlen(*suf);
794
848
        if((d_name_len >= suf_len)
795
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
849
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
796
850
                == 0)){
797
851
          if(debug){
798
852
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
818
872
                                             dirst->d_name));
819
873
    }
820
874
    if(ret < 0){
821
 
      perror("asprintf");
 
875
      error(0, errno, "asprintf");
822
876
      continue;
823
877
    }
824
878
    
825
879
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
826
880
    if(ret == -1){
827
 
      perror("stat");
 
881
      error(0, errno, "stat");
828
882
      free(filename);
829
883
      continue;
830
884
    }
842
896
    
843
897
    plugin *p = getplugin(dirst->d_name);
844
898
    if(p == NULL){
845
 
      perror("getplugin");
 
899
      error(0, errno, "getplugin");
846
900
      free(filename);
847
901
      continue;
848
902
    }
860
914
      if(g != NULL){
861
915
        for(char **a = g->argv + 1; *a != NULL; a++){
862
916
          if(not add_argument(p, *a)){
863
 
            perror("add_argument");
 
917
            error(0, errno, "add_argument");
864
918
          }
865
919
        }
866
920
        /* Add global environment variables */
867
921
        for(char **e = g->environ; *e != NULL; e++){
868
922
          if(not add_environment(p, *e, false)){
869
 
            perror("add_environment");
 
923
            error(0, errno, "add_environment");
870
924
          }
871
925
        }
872
926
      }
877
931
    if(p->environ[0] != NULL){
878
932
      for(char **e = environ; *e != NULL; e++){
879
933
        if(not add_environment(p, *e, false)){
880
 
          perror("add_environment");
 
934
          error(0, errno, "add_environment");
881
935
        }
882
936
      }
883
937
    }
885
939
    int pipefd[2];
886
940
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
887
941
    if(ret == -1){
888
 
      perror("pipe");
889
 
      exitstatus = EXIT_FAILURE;
 
942
      error(0, errno, "pipe");
 
943
      exitstatus = EX_OSERR;
890
944
      goto fallback;
891
945
    }
892
946
    /* Ask OS to automatic close the pipe on exec */
893
947
    ret = set_cloexec_flag(pipefd[0]);
894
948
    if(ret < 0){
895
 
      perror("set_cloexec_flag");
896
 
      exitstatus = EXIT_FAILURE;
 
949
      error(0, errno, "set_cloexec_flag");
 
950
      exitstatus = EX_OSERR;
897
951
      goto fallback;
898
952
    }
899
953
    ret = set_cloexec_flag(pipefd[1]);
900
954
    if(ret < 0){
901
 
      perror("set_cloexec_flag");
902
 
      exitstatus = EXIT_FAILURE;
 
955
      error(0, errno, "set_cloexec_flag");
 
956
      exitstatus = EX_OSERR;
903
957
      goto fallback;
904
958
    }
905
959
    /* Block SIGCHLD until process is safely in process list */
907
961
                                              &sigchld_action.sa_mask,
908
962
                                              NULL));
909
963
    if(ret < 0){
910
 
      perror("sigprocmask");
911
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "sigprocmask");
 
965
      exitstatus = EX_OSERR;
912
966
      goto fallback;
913
967
    }
914
968
    /* Starting a new process to be watched */
917
971
      pid = fork();
918
972
    } while(pid == -1 and errno == EINTR);
919
973
    if(pid == -1){
920
 
      perror("fork");
921
 
      exitstatus = EXIT_FAILURE;
 
974
      error(0, errno, "fork");
 
975
      exitstatus = EX_OSERR;
922
976
      goto fallback;
923
977
    }
924
978
    if(pid == 0){
925
979
      /* this is the child process */
926
980
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
927
981
      if(ret < 0){
928
 
        perror("sigaction");
929
 
        _exit(EXIT_FAILURE);
 
982
        error(0, errno, "sigaction");
 
983
        _exit(EX_OSERR);
930
984
      }
931
985
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
932
986
      if(ret < 0){
933
 
        perror("sigprocmask");
934
 
        _exit(EXIT_FAILURE);
 
987
        error(0, errno, "sigprocmask");
 
988
        _exit(EX_OSERR);
935
989
      }
936
990
      
937
991
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
938
992
      if(ret == -1){
939
 
        perror("dup2");
940
 
        _exit(EXIT_FAILURE);
 
993
        error(0, errno, "dup2");
 
994
        _exit(EX_OSERR);
941
995
      }
942
996
      
943
997
      if(dirfd(dir) < 0){
947
1001
      }
948
1002
      if(p->environ[0] == NULL){
949
1003
        if(execv(filename, p->argv) < 0){
950
 
          perror("execv");
951
 
          _exit(EXIT_FAILURE);
 
1004
          error(0, errno, "execv for %s", filename);
 
1005
          _exit(EX_OSERR);
952
1006
        }
953
1007
      } else {
954
1008
        if(execve(filename, p->argv, p->environ) < 0){
955
 
          perror("execve");
956
 
          _exit(EXIT_FAILURE);
 
1009
          error(0, errno, "execve for %s", filename);
 
1010
          _exit(EX_OSERR);
957
1011
        }
958
1012
      }
959
1013
      /* no return */
964
1018
    free(filename);
965
1019
    plugin *new_plugin = getplugin(dirst->d_name);
966
1020
    if(new_plugin == NULL){
967
 
      perror("getplugin");
 
1021
      error(0, errno, "getplugin");
968
1022
      ret = (int)(TEMP_FAILURE_RETRY
969
1023
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
970
1024
                               NULL)));
971
1025
      if(ret < 0){
972
 
        perror("sigprocmask");
 
1026
        error(0, errno, "sigprocmask");
973
1027
      }
974
 
      exitstatus = EXIT_FAILURE;
 
1028
      exitstatus = EX_OSERR;
975
1029
      goto fallback;
976
1030
    }
977
1031
    
984
1038
                                              &sigchld_action.sa_mask,
985
1039
                                              NULL));
986
1040
    if(ret < 0){
987
 
      perror("sigprocmask");
988
 
      exitstatus = EXIT_FAILURE;
 
1041
      error(0, errno, "sigprocmask");
 
1042
      exitstatus = EX_OSERR;
989
1043
      goto fallback;
990
1044
    }
991
1045
    
1017
1071
    fd_set rfds = rfds_all;
1018
1072
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1019
1073
    if(select_ret == -1 and errno != EINTR){
1020
 
      perror("select");
1021
 
      exitstatus = EXIT_FAILURE;
 
1074
      error(0, errno, "select");
 
1075
      exitstatus = EX_OSERR;
1022
1076
      goto fallback;
1023
1077
    }
1024
1078
    /* OK, now either a process completed, or something can be read
1059
1113
                                         &sigchld_action.sa_mask,
1060
1114
                                         NULL));
1061
1115
          if(ret < 0){
1062
 
            perror("sigprocmask");
1063
 
            exitstatus = EXIT_FAILURE;
 
1116
            error(0, errno, "sigprocmask");
 
1117
            exitstatus = EX_OSERR;
1064
1118
            goto fallback;
1065
1119
          }
1066
1120
          
1073
1127
                      (sigprocmask(SIG_UNBLOCK,
1074
1128
                                   &sigchld_action.sa_mask, NULL)));
1075
1129
          if(ret < 0){
1076
 
            perror("sigprocmask");
1077
 
            exitstatus = EXIT_FAILURE;
 
1130
            error(0, errno, "sigprocmask");
 
1131
            exitstatus = EX_OSERR;
1078
1132
            goto fallback;
1079
1133
          }
1080
1134
          
1090
1144
        bool bret = print_out_password(proc->buffer,
1091
1145
                                       proc->buffer_length);
1092
1146
        if(not bret){
1093
 
          perror("print_out_password");
1094
 
          exitstatus = EXIT_FAILURE;
 
1147
          error(0, errno, "print_out_password");
 
1148
          exitstatus = EX_IOERR;
1095
1149
        }
1096
1150
        goto fallback;
1097
1151
      }
1109
1163
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1110
1164
                               + (size_t) BUFFER_SIZE);
1111
1165
        if(proc->buffer == NULL){
1112
 
          perror("malloc");
1113
 
          exitstatus = EXIT_FAILURE;
 
1166
          error(0, errno, "malloc");
 
1167
          exitstatus = EX_OSERR;
1114
1168
          goto fallback;
1115
1169
        }
1116
1170
        proc->buffer_size += BUFFER_SIZE;
1137
1191
  
1138
1192
 fallback:
1139
1193
  
1140
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1194
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1195
                             and exitstatus != EX_OK)){
1141
1196
    /* Fallback if all plugins failed, none are found or an error
1142
1197
       occured */
1143
1198
    bool bret;
1151
1206
    }
1152
1207
    bret = print_out_password(passwordbuffer, len);
1153
1208
    if(not bret){
1154
 
      perror("print_out_password");
1155
 
      exitstatus = EXIT_FAILURE;
 
1209
      error(0, errno, "print_out_password");
 
1210
      exitstatus = EX_IOERR;
1156
1211
    }
1157
1212
  }
1158
1213
  
1159
1214
  /* Restore old signal handler */
1160
1215
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1161
1216
  if(ret == -1){
1162
 
    perror("sigaction");
1163
 
    exitstatus = EXIT_FAILURE;
 
1217
    error(0, errno, "sigaction");
 
1218
    exitstatus = EX_OSERR;
1164
1219
  }
1165
1220
  
1166
1221
  if(custom_argv != NULL){
1181
1236
      ret = kill(p->pid, SIGTERM);
1182
1237
      if(ret == -1 and errno != ESRCH){
1183
1238
        /* Set-uid proccesses might not get closed */
1184
 
        perror("kill");
 
1239
        error(0, errno, "kill");
1185
1240
      }
1186
1241
    }
1187
1242
  }
1191
1246
    ret = wait(NULL);
1192
1247
  } while(ret >= 0);
1193
1248
  if(errno != ECHILD){
1194
 
    perror("wait");
 
1249
    error(0, errno, "wait");
1195
1250
  }
1196
1251
  
1197
1252
  free_plugin_list();