/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-07 07:48:59 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090907074859-nv2nni1wwib5hi10
* plugin-runner.c: Minor stylistic changes.

* plugins.d/askpass-fifo.c: Changed contact information and did minor
                            stylistic changes.

* plugins.d/mandos-client.c: Minor stylistic changes.

* plugins.d/password-prompt.c: Changed contact information.

* plugins.d/splashy.c: Changed contact information.
  (main): Changed error handling design.  Bug fix: Will now be much
          more tolerant against signals.  Bug fix: Will now re-raise
          signal received.

* plugins.d/usplash.c: Changed contact information and did minor
                       stylistic changes.

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