/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

working new feature: network-hooks - Enables user-scripts to take up
                     interfaces during bootup

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
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
 
75
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
76
80
 
77
81
const char *argp_program_version = "plugin-runner " VERSION;
78
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
79
83
 
80
84
typedef struct plugin{
81
85
  char *name;                   /* can be NULL or any plugin name */
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
  
688
742
    }
689
743
  }
690
744
  
691
 
  /* Strip permissions down to nobody */
 
745
  if(getuid() == 0){
 
746
    /* Work around Debian bug #633582:
 
747
       <http://bugs.debian.org/633582> */
 
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
749
    if(plugindir_fd == -1){
 
750
      error(0, errno, "open");
 
751
    } else {
 
752
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
753
      if(ret == -1){
 
754
        error(0, errno, "fstat");
 
755
      } else {
 
756
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
757
          ret = fchown(plugindir_fd, uid, gid);
 
758
          if(ret == -1){
 
759
            error(0, errno, "fchown");
 
760
          }
 
761
        }
 
762
      }
 
763
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
764
    }
 
765
  }
 
766
  
 
767
  /* Lower permissions */
692
768
  setgid(gid);
693
769
  if(ret == -1){
694
 
    perror("setgid");
 
770
    error(0, errno, "setgid");
695
771
  }
696
772
  ret = setuid(uid);
697
773
  if(ret == -1){
698
 
    perror("setuid");
 
774
    error(0, errno, "setuid");
699
775
  }
700
776
  
701
777
  /* Open plugin directory with close_on_exec flag */
719
795
                    );
720
796
    }
721
797
    if(dir_fd == -1){
722
 
      perror("Could not open plugin dir");
723
 
      exitstatus = EXIT_FAILURE;
 
798
      error(0, errno, "Could not open plugin dir");
 
799
      exitstatus = EX_UNAVAILABLE;
724
800
      goto fallback;
725
801
    }
726
802
    
728
804
  /* Set the FD_CLOEXEC flag on the directory */
729
805
    ret = set_cloexec_flag(dir_fd);
730
806
    if(ret < 0){
731
 
      perror("set_cloexec_flag");
 
807
      error(0, errno, "set_cloexec_flag");
732
808
      TEMP_FAILURE_RETRY(close(dir_fd));
733
 
      exitstatus = EXIT_FAILURE;
 
809
      exitstatus = EX_OSERR;
734
810
      goto fallback;
735
811
    }
736
812
#endif  /* O_CLOEXEC */
737
813
    
738
814
    dir = fdopendir(dir_fd);
739
815
    if(dir == NULL){
740
 
      perror("Could not open plugin dir");
 
816
      error(0, errno, "Could not open plugin dir");
741
817
      TEMP_FAILURE_RETRY(close(dir_fd));
742
 
      exitstatus = EXIT_FAILURE;
 
818
      exitstatus = EX_OSERR;
743
819
      goto fallback;
744
820
    }
745
821
  }
755
831
    /* All directory entries have been processed */
756
832
    if(dirst == NULL){
757
833
      if(errno == EBADF){
758
 
        perror("readdir");
759
 
        exitstatus = EXIT_FAILURE;
 
834
        error(0, errno, "readdir");
 
835
        exitstatus = EX_IOERR;
760
836
        goto fallback;
761
837
      }
762
838
      break;
792
868
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
793
869
        size_t suf_len = strlen(*suf);
794
870
        if((d_name_len >= suf_len)
795
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
871
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
796
872
                == 0)){
797
873
          if(debug){
798
874
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
818
894
                                             dirst->d_name));
819
895
    }
820
896
    if(ret < 0){
821
 
      perror("asprintf");
 
897
      error(0, errno, "asprintf");
822
898
      continue;
823
899
    }
824
900
    
825
901
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
826
902
    if(ret == -1){
827
 
      perror("stat");
 
903
      error(0, errno, "stat");
828
904
      free(filename);
829
905
      continue;
830
906
    }
842
918
    
843
919
    plugin *p = getplugin(dirst->d_name);
844
920
    if(p == NULL){
845
 
      perror("getplugin");
 
921
      error(0, errno, "getplugin");
846
922
      free(filename);
847
923
      continue;
848
924
    }
860
936
      if(g != NULL){
861
937
        for(char **a = g->argv + 1; *a != NULL; a++){
862
938
          if(not add_argument(p, *a)){
863
 
            perror("add_argument");
 
939
            error(0, errno, "add_argument");
864
940
          }
865
941
        }
866
942
        /* Add global environment variables */
867
943
        for(char **e = g->environ; *e != NULL; e++){
868
944
          if(not add_environment(p, *e, false)){
869
 
            perror("add_environment");
 
945
            error(0, errno, "add_environment");
870
946
          }
871
947
        }
872
948
      }
877
953
    if(p->environ[0] != NULL){
878
954
      for(char **e = environ; *e != NULL; e++){
879
955
        if(not add_environment(p, *e, false)){
880
 
          perror("add_environment");
 
956
          error(0, errno, "add_environment");
881
957
        }
882
958
      }
883
959
    }
885
961
    int pipefd[2];
886
962
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
887
963
    if(ret == -1){
888
 
      perror("pipe");
889
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "pipe");
 
965
      exitstatus = EX_OSERR;
890
966
      goto fallback;
891
967
    }
892
968
    /* Ask OS to automatic close the pipe on exec */
893
969
    ret = set_cloexec_flag(pipefd[0]);
894
970
    if(ret < 0){
895
 
      perror("set_cloexec_flag");
896
 
      exitstatus = EXIT_FAILURE;
 
971
      error(0, errno, "set_cloexec_flag");
 
972
      exitstatus = EX_OSERR;
897
973
      goto fallback;
898
974
    }
899
975
    ret = set_cloexec_flag(pipefd[1]);
900
976
    if(ret < 0){
901
 
      perror("set_cloexec_flag");
902
 
      exitstatus = EXIT_FAILURE;
 
977
      error(0, errno, "set_cloexec_flag");
 
978
      exitstatus = EX_OSERR;
903
979
      goto fallback;
904
980
    }
905
981
    /* Block SIGCHLD until process is safely in process list */
907
983
                                              &sigchld_action.sa_mask,
908
984
                                              NULL));
909
985
    if(ret < 0){
910
 
      perror("sigprocmask");
911
 
      exitstatus = EXIT_FAILURE;
 
986
      error(0, errno, "sigprocmask");
 
987
      exitstatus = EX_OSERR;
912
988
      goto fallback;
913
989
    }
914
990
    /* Starting a new process to be watched */
917
993
      pid = fork();
918
994
    } while(pid == -1 and errno == EINTR);
919
995
    if(pid == -1){
920
 
      perror("fork");
921
 
      exitstatus = EXIT_FAILURE;
 
996
      error(0, errno, "fork");
 
997
      exitstatus = EX_OSERR;
922
998
      goto fallback;
923
999
    }
924
1000
    if(pid == 0){
925
1001
      /* this is the child process */
926
1002
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
927
1003
      if(ret < 0){
928
 
        perror("sigaction");
929
 
        _exit(EXIT_FAILURE);
 
1004
        error(0, errno, "sigaction");
 
1005
        _exit(EX_OSERR);
930
1006
      }
931
1007
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
932
1008
      if(ret < 0){
933
 
        perror("sigprocmask");
934
 
        _exit(EXIT_FAILURE);
 
1009
        error(0, errno, "sigprocmask");
 
1010
        _exit(EX_OSERR);
935
1011
      }
936
1012
      
937
1013
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
938
1014
      if(ret == -1){
939
 
        perror("dup2");
940
 
        _exit(EXIT_FAILURE);
 
1015
        error(0, errno, "dup2");
 
1016
        _exit(EX_OSERR);
941
1017
      }
942
1018
      
943
1019
      if(dirfd(dir) < 0){
947
1023
      }
948
1024
      if(p->environ[0] == NULL){
949
1025
        if(execv(filename, p->argv) < 0){
950
 
          perror("execv");
951
 
          _exit(EXIT_FAILURE);
 
1026
          error(0, errno, "execv for %s", filename);
 
1027
          _exit(EX_OSERR);
952
1028
        }
953
1029
      } else {
954
1030
        if(execve(filename, p->argv, p->environ) < 0){
955
 
          perror("execve");
956
 
          _exit(EXIT_FAILURE);
 
1031
          error(0, errno, "execve for %s", filename);
 
1032
          _exit(EX_OSERR);
957
1033
        }
958
1034
      }
959
1035
      /* no return */
964
1040
    free(filename);
965
1041
    plugin *new_plugin = getplugin(dirst->d_name);
966
1042
    if(new_plugin == NULL){
967
 
      perror("getplugin");
 
1043
      error(0, errno, "getplugin");
968
1044
      ret = (int)(TEMP_FAILURE_RETRY
969
1045
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
970
1046
                               NULL)));
971
1047
      if(ret < 0){
972
 
        perror("sigprocmask");
 
1048
        error(0, errno, "sigprocmask");
973
1049
      }
974
 
      exitstatus = EXIT_FAILURE;
 
1050
      exitstatus = EX_OSERR;
975
1051
      goto fallback;
976
1052
    }
977
1053
    
984
1060
                                              &sigchld_action.sa_mask,
985
1061
                                              NULL));
986
1062
    if(ret < 0){
987
 
      perror("sigprocmask");
988
 
      exitstatus = EXIT_FAILURE;
 
1063
      error(0, errno, "sigprocmask");
 
1064
      exitstatus = EX_OSERR;
989
1065
      goto fallback;
990
1066
    }
991
1067
    
1017
1093
    fd_set rfds = rfds_all;
1018
1094
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1019
1095
    if(select_ret == -1 and errno != EINTR){
1020
 
      perror("select");
1021
 
      exitstatus = EXIT_FAILURE;
 
1096
      error(0, errno, "select");
 
1097
      exitstatus = EX_OSERR;
1022
1098
      goto fallback;
1023
1099
    }
1024
1100
    /* OK, now either a process completed, or something can be read
1059
1135
                                         &sigchld_action.sa_mask,
1060
1136
                                         NULL));
1061
1137
          if(ret < 0){
1062
 
            perror("sigprocmask");
1063
 
            exitstatus = EXIT_FAILURE;
 
1138
            error(0, errno, "sigprocmask");
 
1139
            exitstatus = EX_OSERR;
1064
1140
            goto fallback;
1065
1141
          }
1066
1142
          
1073
1149
                      (sigprocmask(SIG_UNBLOCK,
1074
1150
                                   &sigchld_action.sa_mask, NULL)));
1075
1151
          if(ret < 0){
1076
 
            perror("sigprocmask");
1077
 
            exitstatus = EXIT_FAILURE;
 
1152
            error(0, errno, "sigprocmask");
 
1153
            exitstatus = EX_OSERR;
1078
1154
            goto fallback;
1079
1155
          }
1080
1156
          
1090
1166
        bool bret = print_out_password(proc->buffer,
1091
1167
                                       proc->buffer_length);
1092
1168
        if(not bret){
1093
 
          perror("print_out_password");
1094
 
          exitstatus = EXIT_FAILURE;
 
1169
          error(0, errno, "print_out_password");
 
1170
          exitstatus = EX_IOERR;
1095
1171
        }
1096
1172
        goto fallback;
1097
1173
      }
1109
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1110
1186
                               + (size_t) BUFFER_SIZE);
1111
1187
        if(proc->buffer == NULL){
1112
 
          perror("malloc");
1113
 
          exitstatus = EXIT_FAILURE;
 
1188
          error(0, errno, "malloc");
 
1189
          exitstatus = EX_OSERR;
1114
1190
          goto fallback;
1115
1191
        }
1116
1192
        proc->buffer_size += BUFFER_SIZE;
1137
1213
  
1138
1214
 fallback:
1139
1215
  
1140
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1216
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1217
                             and exitstatus != EX_OK)){
1141
1218
    /* Fallback if all plugins failed, none are found or an error
1142
1219
       occured */
1143
1220
    bool bret;
1151
1228
    }
1152
1229
    bret = print_out_password(passwordbuffer, len);
1153
1230
    if(not bret){
1154
 
      perror("print_out_password");
1155
 
      exitstatus = EXIT_FAILURE;
 
1231
      error(0, errno, "print_out_password");
 
1232
      exitstatus = EX_IOERR;
1156
1233
    }
1157
1234
  }
1158
1235
  
1159
1236
  /* Restore old signal handler */
1160
1237
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1161
1238
  if(ret == -1){
1162
 
    perror("sigaction");
1163
 
    exitstatus = EXIT_FAILURE;
 
1239
    error(0, errno, "sigaction");
 
1240
    exitstatus = EX_OSERR;
1164
1241
  }
1165
1242
  
1166
1243
  if(custom_argv != NULL){
1181
1258
      ret = kill(p->pid, SIGTERM);
1182
1259
      if(ret == -1 and errno != ESRCH){
1183
1260
        /* Set-uid proccesses might not get closed */
1184
 
        perror("kill");
 
1261
        error(0, errno, "kill");
1185
1262
      }
1186
1263
    }
1187
1264
  }
1191
1268
    ret = wait(NULL);
1192
1269
  } while(ret >= 0);
1193
1270
  if(errno != ECHILD){
1194
 
    perror("wait");
 
1271
    error(0, errno, "wait");
1195
1272
  }
1196
1273
  
1197
1274
  free_plugin_list();