/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-10-18 08:47:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018084740-fa1qgm22lg125r10
* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

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
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_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
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(), 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
  
745
691
  /* Strip permissions down to nobody */
746
692
  setgid(gid);
747
693
  if(ret == -1){
748
 
    error(0, errno, "setgid");
 
694
    perror("setgid");
749
695
  }
750
696
  ret = setuid(uid);
751
697
  if(ret == -1){
752
 
    error(0, errno, "setuid");
 
698
    perror("setuid");
753
699
  }
754
700
  
755
701
  /* Open plugin directory with close_on_exec flag */
773
719
                    );
774
720
    }
775
721
    if(dir_fd == -1){
776
 
      error(0, errno, "Could not open plugin dir");
777
 
      exitstatus = EX_UNAVAILABLE;
 
722
      perror("Could not open plugin dir");
 
723
      exitstatus = EXIT_FAILURE;
778
724
      goto fallback;
779
725
    }
780
726
    
782
728
  /* Set the FD_CLOEXEC flag on the directory */
783
729
    ret = set_cloexec_flag(dir_fd);
784
730
    if(ret < 0){
785
 
      error(0, errno, "set_cloexec_flag");
 
731
      perror("set_cloexec_flag");
786
732
      TEMP_FAILURE_RETRY(close(dir_fd));
787
 
      exitstatus = EX_OSERR;
 
733
      exitstatus = EXIT_FAILURE;
788
734
      goto fallback;
789
735
    }
790
736
#endif  /* O_CLOEXEC */
791
737
    
792
738
    dir = fdopendir(dir_fd);
793
739
    if(dir == NULL){
794
 
      error(0, errno, "Could not open plugin dir");
 
740
      perror("Could not open plugin dir");
795
741
      TEMP_FAILURE_RETRY(close(dir_fd));
796
 
      exitstatus = EX_OSERR;
 
742
      exitstatus = EXIT_FAILURE;
797
743
      goto fallback;
798
744
    }
799
745
  }
809
755
    /* All directory entries have been processed */
810
756
    if(dirst == NULL){
811
757
      if(errno == EBADF){
812
 
        error(0, errno, "readdir");
813
 
        exitstatus = EX_IOERR;
 
758
        perror("readdir");
 
759
        exitstatus = EXIT_FAILURE;
814
760
        goto fallback;
815
761
      }
816
762
      break;
846
792
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
847
793
        size_t suf_len = strlen(*suf);
848
794
        if((d_name_len >= suf_len)
849
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
795
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
850
796
                == 0)){
851
797
          if(debug){
852
798
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
872
818
                                             dirst->d_name));
873
819
    }
874
820
    if(ret < 0){
875
 
      error(0, errno, "asprintf");
 
821
      perror("asprintf");
876
822
      continue;
877
823
    }
878
824
    
879
825
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
880
826
    if(ret == -1){
881
 
      error(0, errno, "stat");
 
827
      perror("stat");
882
828
      free(filename);
883
829
      continue;
884
830
    }
896
842
    
897
843
    plugin *p = getplugin(dirst->d_name);
898
844
    if(p == NULL){
899
 
      error(0, errno, "getplugin");
 
845
      perror("getplugin");
900
846
      free(filename);
901
847
      continue;
902
848
    }
914
860
      if(g != NULL){
915
861
        for(char **a = g->argv + 1; *a != NULL; a++){
916
862
          if(not add_argument(p, *a)){
917
 
            error(0, errno, "add_argument");
 
863
            perror("add_argument");
918
864
          }
919
865
        }
920
866
        /* Add global environment variables */
921
867
        for(char **e = g->environ; *e != NULL; e++){
922
868
          if(not add_environment(p, *e, false)){
923
 
            error(0, errno, "add_environment");
 
869
            perror("add_environment");
924
870
          }
925
871
        }
926
872
      }
931
877
    if(p->environ[0] != NULL){
932
878
      for(char **e = environ; *e != NULL; e++){
933
879
        if(not add_environment(p, *e, false)){
934
 
          error(0, errno, "add_environment");
 
880
          perror("add_environment");
935
881
        }
936
882
      }
937
883
    }
939
885
    int pipefd[2];
940
886
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
941
887
    if(ret == -1){
942
 
      error(0, errno, "pipe");
943
 
      exitstatus = EX_OSERR;
 
888
      perror("pipe");
 
889
      exitstatus = EXIT_FAILURE;
944
890
      goto fallback;
945
891
    }
946
892
    /* Ask OS to automatic close the pipe on exec */
947
893
    ret = set_cloexec_flag(pipefd[0]);
948
894
    if(ret < 0){
949
 
      error(0, errno, "set_cloexec_flag");
950
 
      exitstatus = EX_OSERR;
 
895
      perror("set_cloexec_flag");
 
896
      exitstatus = EXIT_FAILURE;
951
897
      goto fallback;
952
898
    }
953
899
    ret = set_cloexec_flag(pipefd[1]);
954
900
    if(ret < 0){
955
 
      error(0, errno, "set_cloexec_flag");
956
 
      exitstatus = EX_OSERR;
 
901
      perror("set_cloexec_flag");
 
902
      exitstatus = EXIT_FAILURE;
957
903
      goto fallback;
958
904
    }
959
905
    /* Block SIGCHLD until process is safely in process list */
961
907
                                              &sigchld_action.sa_mask,
962
908
                                              NULL));
963
909
    if(ret < 0){
964
 
      error(0, errno, "sigprocmask");
965
 
      exitstatus = EX_OSERR;
 
910
      perror("sigprocmask");
 
911
      exitstatus = EXIT_FAILURE;
966
912
      goto fallback;
967
913
    }
968
914
    /* Starting a new process to be watched */
971
917
      pid = fork();
972
918
    } while(pid == -1 and errno == EINTR);
973
919
    if(pid == -1){
974
 
      error(0, errno, "fork");
975
 
      exitstatus = EX_OSERR;
 
920
      perror("fork");
 
921
      exitstatus = EXIT_FAILURE;
976
922
      goto fallback;
977
923
    }
978
924
    if(pid == 0){
979
925
      /* this is the child process */
980
926
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
981
927
      if(ret < 0){
982
 
        error(0, errno, "sigaction");
983
 
        _exit(EX_OSERR);
 
928
        perror("sigaction");
 
929
        _exit(EXIT_FAILURE);
984
930
      }
985
931
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
986
932
      if(ret < 0){
987
 
        error(0, errno, "sigprocmask");
988
 
        _exit(EX_OSERR);
 
933
        perror("sigprocmask");
 
934
        _exit(EXIT_FAILURE);
989
935
      }
990
936
      
991
937
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
992
938
      if(ret == -1){
993
 
        error(0, errno, "dup2");
994
 
        _exit(EX_OSERR);
 
939
        perror("dup2");
 
940
        _exit(EXIT_FAILURE);
995
941
      }
996
942
      
997
943
      if(dirfd(dir) < 0){
1001
947
      }
1002
948
      if(p->environ[0] == NULL){
1003
949
        if(execv(filename, p->argv) < 0){
1004
 
          error(0, errno, "execv for %s", filename);
1005
 
          _exit(EX_OSERR);
 
950
          perror("execv");
 
951
          _exit(EXIT_FAILURE);
1006
952
        }
1007
953
      } else {
1008
954
        if(execve(filename, p->argv, p->environ) < 0){
1009
 
          error(0, errno, "execve for %s", filename);
1010
 
          _exit(EX_OSERR);
 
955
          perror("execve");
 
956
          _exit(EXIT_FAILURE);
1011
957
        }
1012
958
      }
1013
959
      /* no return */
1018
964
    free(filename);
1019
965
    plugin *new_plugin = getplugin(dirst->d_name);
1020
966
    if(new_plugin == NULL){
1021
 
      error(0, errno, "getplugin");
 
967
      perror("getplugin");
1022
968
      ret = (int)(TEMP_FAILURE_RETRY
1023
969
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1024
970
                               NULL)));
1025
971
      if(ret < 0){
1026
 
        error(0, errno, "sigprocmask");
 
972
        perror("sigprocmask");
1027
973
      }
1028
 
      exitstatus = EX_OSERR;
 
974
      exitstatus = EXIT_FAILURE;
1029
975
      goto fallback;
1030
976
    }
1031
977
    
1038
984
                                              &sigchld_action.sa_mask,
1039
985
                                              NULL));
1040
986
    if(ret < 0){
1041
 
      error(0, errno, "sigprocmask");
1042
 
      exitstatus = EX_OSERR;
 
987
      perror("sigprocmask");
 
988
      exitstatus = EXIT_FAILURE;
1043
989
      goto fallback;
1044
990
    }
1045
991
    
1071
1017
    fd_set rfds = rfds_all;
1072
1018
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1073
1019
    if(select_ret == -1 and errno != EINTR){
1074
 
      error(0, errno, "select");
1075
 
      exitstatus = EX_OSERR;
 
1020
      perror("select");
 
1021
      exitstatus = EXIT_FAILURE;
1076
1022
      goto fallback;
1077
1023
    }
1078
1024
    /* OK, now either a process completed, or something can be read
1113
1059
                                         &sigchld_action.sa_mask,
1114
1060
                                         NULL));
1115
1061
          if(ret < 0){
1116
 
            error(0, errno, "sigprocmask");
1117
 
            exitstatus = EX_OSERR;
 
1062
            perror("sigprocmask");
 
1063
            exitstatus = EXIT_FAILURE;
1118
1064
            goto fallback;
1119
1065
          }
1120
1066
          
1127
1073
                      (sigprocmask(SIG_UNBLOCK,
1128
1074
                                   &sigchld_action.sa_mask, NULL)));
1129
1075
          if(ret < 0){
1130
 
            error(0, errno, "sigprocmask");
1131
 
            exitstatus = EX_OSERR;
 
1076
            perror("sigprocmask");
 
1077
            exitstatus = EXIT_FAILURE;
1132
1078
            goto fallback;
1133
1079
          }
1134
1080
          
1144
1090
        bool bret = print_out_password(proc->buffer,
1145
1091
                                       proc->buffer_length);
1146
1092
        if(not bret){
1147
 
          error(0, errno, "print_out_password");
1148
 
          exitstatus = EX_IOERR;
 
1093
          perror("print_out_password");
 
1094
          exitstatus = EXIT_FAILURE;
1149
1095
        }
1150
1096
        goto fallback;
1151
1097
      }
1163
1109
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1164
1110
                               + (size_t) BUFFER_SIZE);
1165
1111
        if(proc->buffer == NULL){
1166
 
          error(0, errno, "malloc");
1167
 
          exitstatus = EX_OSERR;
 
1112
          perror("malloc");
 
1113
          exitstatus = EXIT_FAILURE;
1168
1114
          goto fallback;
1169
1115
        }
1170
1116
        proc->buffer_size += BUFFER_SIZE;
1191
1137
  
1192
1138
 fallback:
1193
1139
  
1194
 
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
1195
 
                             and exitstatus != EX_OK)){
 
1140
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1196
1141
    /* Fallback if all plugins failed, none are found or an error
1197
1142
       occured */
1198
1143
    bool bret;
1206
1151
    }
1207
1152
    bret = print_out_password(passwordbuffer, len);
1208
1153
    if(not bret){
1209
 
      error(0, errno, "print_out_password");
1210
 
      exitstatus = EX_IOERR;
 
1154
      perror("print_out_password");
 
1155
      exitstatus = EXIT_FAILURE;
1211
1156
    }
1212
1157
  }
1213
1158
  
1214
1159
  /* Restore old signal handler */
1215
1160
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1216
1161
  if(ret == -1){
1217
 
    error(0, errno, "sigaction");
1218
 
    exitstatus = EX_OSERR;
 
1162
    perror("sigaction");
 
1163
    exitstatus = EXIT_FAILURE;
1219
1164
  }
1220
1165
  
1221
1166
  if(custom_argv != NULL){
1236
1181
      ret = kill(p->pid, SIGTERM);
1237
1182
      if(ret == -1 and errno != ESRCH){
1238
1183
        /* Set-uid proccesses might not get closed */
1239
 
        error(0, errno, "kill");
 
1184
        perror("kill");
1240
1185
      }
1241
1186
    }
1242
1187
  }
1246
1191
    ret = wait(NULL);
1247
1192
  } while(ret >= 0);
1248
1193
  if(errno != ECHILD){
1249
 
    error(0, errno, "wait");
 
1194
    perror("wait");
1250
1195
  }
1251
1196
  
1252
1197
  free_plugin_list();