/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2019-04-09 19:33:36 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190409193336-6sddo74ybosqog38
Installation: Fix UMASK even if set by some other initramfs hook

* Makefile (install-client-nokey): Install initramfs-tools-conf-hook
               as "/usr/share/initramfs-tools/conf-hooks.d/zz-mandos".
* debian/mandos-client.dirs: Add
  "usr/share/initramfs-tools/conf-hooks.d".
* initramfs-tools-conf-hook: New.

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-2013 Teddy Hogeborn
6
 
 * Copyright © 2008-2013 Björn Påhlsson
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2018 Teddy Hogeborn
 
6
 * Copyright © 2008-2018 Björn Påhlsson
 
7
 * 
 
8
 * This file is part of Mandos.
 
9
 * 
 
10
 * Mandos is free software: you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * Mandos is distributed in the hope that it will be useful, but
14
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
18
 * General Public License for more details.
17
19
 * 
18
20
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21
22
 * 
22
23
 * Contact the authors at <mandos@recompile.se>.
23
24
 */
24
25
 
25
26
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
27
                                   O_CLOEXEC, pipe2() */
27
28
#include <stddef.h>             /* size_t, NULL */
28
29
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
30
                                   realloc() */
30
31
#include <stdbool.h>            /* bool, true, false */
31
32
#include <stdio.h>              /* fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
 
                                   stat, waitpid(), WIFEXITED(),
35
 
                                   WEXITSTATUS(), wait(), pid_t,
36
 
                                   uid_t, gid_t, getuid(), getgid(),
37
 
                                   dirfd() */
 
33
                                   stderr, STDOUT_FILENO, fclose() */
 
34
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
 
35
                                   WIFEXITED(), WEXITSTATUS(), wait(),
 
36
                                   pid_t, uid_t, gid_t, getuid(),
 
37
                                   getgid() */
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
41
                                   WEXITSTATUS(), WTERMSIG() */
 
42
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
 
                                   readdir(), closedir(), dirfd() */
47
 
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
 
                                   fcntl(), setuid(), setgid(),
49
 
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
50
 
                                   access(), pipe(), fork(), close()
51
 
                                   dup2(), STDOUT_FILENO, _exit(),
52
 
                                   execve(), write(), read(),
53
 
                                   close() */
 
44
#include <dirent.h>             /* struct dirent, scandirat() */
 
45
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
46
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
47
                                   struct stat, fstat(), close(),
 
48
                                   setgid(), setuid(), S_ISREG(),
 
49
                                   faccessat() pipe2(), fork(),
 
50
                                   _exit(), dup2(), fexecve(), read()
 
51
                                */
54
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
53
                                   FD_CLOEXEC, openat(), scandirat(),
 
54
                                   pipe2() */
 
55
#include <string.h>             /* strsep, strlen(), strsignal(),
 
56
                                   strcmp(), strncmp() */
58
57
#include <errno.h>              /* errno */
59
58
#include <argp.h>               /* struct argp_option, struct
60
59
                                   argp_state, struct argp,
72
71
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
72
#include <errno.h>              /* errno */
74
73
#include <error.h>              /* error() */
 
74
#include <fnmatch.h>            /* fnmatch() */
75
75
 
76
76
#define BUFFER_SIZE 256
77
77
 
78
78
#define PDIR "/lib/mandos/plugins.d"
 
79
#define PHDIR "/lib/mandos/plugin-helpers"
79
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
81
 
81
82
const char *argp_program_version = "plugin-runner " VERSION;
240
241
  return add_to_char_array(def, &(p->environ), &(p->envc));
241
242
}
242
243
 
 
244
#ifndef O_CLOEXEC
243
245
/*
244
246
 * Based on the example in the GNU LibC manual chapter 13.13 "File
245
247
 * Descriptor Flags".
256
258
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
257
259
                                       ret | FD_CLOEXEC));
258
260
}
 
261
#endif  /* not O_CLOEXEC */
259
262
 
260
263
 
261
264
/* Mark processes as completed when they exit, and save their exit
310
313
__attribute__((nonnull))
311
314
static void free_plugin(plugin *plugin_node){
312
315
  
313
 
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
316
  for(char **arg = (plugin_node->argv)+1; *arg != NULL; arg++){
314
317
    free(*arg);
315
318
  }
 
319
  free(plugin_node->name);
316
320
  free(plugin_node->argv);
317
321
  for(char **env = plugin_node->environ; *env != NULL; env++){
318
322
    free(*env);
345
349
 
346
350
int main(int argc, char *argv[]){
347
351
  char *plugindir = NULL;
 
352
  char *pluginhelperdir = NULL;
348
353
  char *argfile = NULL;
349
354
  FILE *conffp;
350
 
  size_t d_name_len;
351
 
  DIR *dir = NULL;
352
 
  struct dirent *dirst;
 
355
  struct dirent **direntries = NULL;
353
356
  struct stat st;
354
357
  fd_set rfds_all;
355
358
  int ret, maxfd = 0;
363
366
                                      .sa_flags = SA_NOCLDSTOP };
364
367
  char **custom_argv = NULL;
365
368
  int custom_argc = 0;
 
369
  int dir_fd = -1;
366
370
  
367
371
  /* Establish a signal handler */
368
372
  sigemptyset(&sigchld_action.sa_mask);
413
417
      .doc = "Group ID the plugins will run as", .group = 3 },
414
418
    { .name = "debug", .key = 132,
415
419
      .doc = "Debug mode", .group = 4 },
 
420
    { .name = "plugin-helper-dir", .key = 133,
 
421
      .arg = "DIRECTORY",
 
422
      .doc = "Specify a different plugin helper directory",
 
423
      .group = 2 },
416
424
    /*
417
425
     * These reproduce what we would get without ARGP_NO_HELP
418
426
     */
544
552
    case 132:                   /* --debug */
545
553
      debug = true;
546
554
      break;
 
555
    case 133:                   /* --plugin-helper-dir */
 
556
      free(pluginhelperdir);
 
557
      pluginhelperdir = strdup(arg);
 
558
      if(pluginhelperdir != NULL){
 
559
        errno = 0;
 
560
      }
 
561
      break;
547
562
      /*
548
563
       * These reproduce what we would get without ARGP_NO_HELP
549
564
       */
550
565
    case '?':                   /* --help */
551
566
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
552
567
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
568
      __builtin_unreachable();
553
569
    case -3:                    /* --usage */
554
570
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
555
571
      argp_state_help(state, state->out_stream,
556
572
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
573
      __builtin_unreachable();
557
574
    case 'V':                   /* --version */
558
575
      fprintf(state->out_stream, "%s\n", argp_program_version);
559
576
      exit(EXIT_SUCCESS);
569
586
      if(arg[0] == '\0'){
570
587
        break;
571
588
      }
 
589
      /* FALLTHROUGH */
572
590
    default:
573
591
      return ARGP_ERR_UNKNOWN;
574
592
    }
600
618
    case 130:                   /* --userid */
601
619
    case 131:                   /* --groupid */
602
620
    case 132:                   /* --debug */
 
621
    case 133:                   /* --plugin-helper-dir */
603
622
    case '?':                   /* --help */
604
623
    case -3:                    /* --usage */
605
624
    case 'V':                   /* --version */
686
705
        custom_argc += 1;
687
706
        {
688
707
          char **new_argv = realloc(custom_argv, sizeof(char *)
689
 
                                    * ((unsigned int)
690
 
                                       custom_argc + 1));
 
708
                                    * ((size_t)custom_argc + 1));
691
709
          if(new_argv == NULL){
692
710
            error(0, errno, "realloc");
693
711
            exitstatus = EX_OSERR;
760
778
    goto fallback;
761
779
  }
762
780
  
 
781
  {
 
782
    char *pluginhelperenv;
 
783
    bool bret = true;
 
784
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
 
785
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
786
    if(ret != -1){
 
787
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
 
788
    }
 
789
    if(ret == -1 or not bret){
 
790
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
 
791
            " environment variable to \"%s\" for all plugins\n",
 
792
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
793
    }
 
794
    if(ret != -1){
 
795
      free(pluginhelperenv);
 
796
    }
 
797
  }
 
798
  
763
799
  if(debug){
764
 
    for(plugin *p = plugin_list; p != NULL; p=p->next){
 
800
    for(plugin *p = plugin_list; p != NULL; p = p->next){
765
801
      fprintf(stderr, "Plugin: %s has %d arguments\n",
766
802
              p->name ? p->name : "Global", p->argc - 1);
767
803
      for(char **a = p->argv; *a != NULL; a++){
776
812
  
777
813
  if(getuid() == 0){
778
814
    /* Work around Debian bug #633582:
779
 
       <http://bugs.debian.org/633582> */
 
815
       <https://bugs.debian.org/633582> */
780
816
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
781
817
    if(plugindir_fd == -1){
782
 
      error(0, errno, "open(\"" PDIR "\")");
 
818
      if(errno != ENOENT){
 
819
        error(0, errno, "open(\"" PDIR "\")");
 
820
      }
783
821
    } else {
784
822
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
785
823
      if(ret == -1){
792
830
          }
793
831
        }
794
832
      }
795
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
833
      close(plugindir_fd);
796
834
    }
797
835
  }
798
836
  
808
846
  
809
847
  /* Open plugin directory with close_on_exec flag */
810
848
  {
811
 
    int dir_fd = -1;
812
 
    if(plugindir == NULL){
813
 
      dir_fd = open(PDIR, O_RDONLY |
814
 
#ifdef O_CLOEXEC
815
 
                    O_CLOEXEC
816
 
#else  /* not O_CLOEXEC */
817
 
                    0
818
 
#endif  /* not O_CLOEXEC */
819
 
                    );
820
 
    } else {
821
 
      dir_fd = open(plugindir, O_RDONLY |
822
 
#ifdef O_CLOEXEC
823
 
                    O_CLOEXEC
824
 
#else  /* not O_CLOEXEC */
825
 
                    0
826
 
#endif  /* not O_CLOEXEC */
827
 
                    );
828
 
    }
 
849
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
 
850
#ifdef O_CLOEXEC
 
851
                  O_CLOEXEC
 
852
#else  /* not O_CLOEXEC */
 
853
                  0
 
854
#endif  /* not O_CLOEXEC */
 
855
                  );
829
856
    if(dir_fd == -1){
830
857
      error(0, errno, "Could not open plugin dir");
831
858
      exitstatus = EX_UNAVAILABLE;
837
864
    ret = set_cloexec_flag(dir_fd);
838
865
    if(ret < 0){
839
866
      error(0, errno, "set_cloexec_flag");
840
 
      TEMP_FAILURE_RETRY(close(dir_fd));
841
867
      exitstatus = EX_OSERR;
842
868
      goto fallback;
843
869
    }
844
870
#endif  /* O_CLOEXEC */
845
 
    
846
 
    dir = fdopendir(dir_fd);
847
 
    if(dir == NULL){
848
 
      error(0, errno, "Could not open plugin dir");
849
 
      TEMP_FAILURE_RETRY(close(dir_fd));
850
 
      exitstatus = EX_OSERR;
851
 
      goto fallback;
 
871
  }
 
872
  
 
873
  int good_name(const struct dirent * const dirent){
 
874
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
 
875
                                      "*.dpkg-old", "*.dpkg-bak",
 
876
                                      "*.dpkg-divert", NULL };
 
877
#ifdef __GNUC__
 
878
#pragma GCC diagnostic push
 
879
#pragma GCC diagnostic ignored "-Wcast-qual"
 
880
#endif
 
881
    for(const char **pat = (const char **)patterns;
 
882
        *pat != NULL; pat++){
 
883
#ifdef __GNUC__
 
884
#pragma GCC diagnostic pop
 
885
#endif
 
886
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
 
887
         != FNM_NOMATCH){
 
888
        if(debug){
 
889
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
890
                    " matching pattern %s\n", dirent->d_name, *pat);
 
891
        }
 
892
        return 0;
 
893
      }
852
894
    }
 
895
    return 1;
 
896
  }
 
897
  
 
898
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
 
899
                             alphasort);
 
900
  if(numplugins == -1){
 
901
    error(0, errno, "Could not scan plugin dir");
 
902
    direntries = NULL;
 
903
    exitstatus = EX_OSERR;
 
904
    goto fallback;
853
905
  }
854
906
  
855
907
  FD_ZERO(&rfds_all);
856
908
  
857
909
  /* Read and execute any executable in the plugin directory*/
858
 
  while(true){
859
 
    do {
860
 
      dirst = readdir(dir);
861
 
    } while(dirst == NULL and errno == EINTR);
862
 
    
863
 
    /* All directory entries have been processed */
864
 
    if(dirst == NULL){
865
 
      if(errno == EBADF){
866
 
        error(0, errno, "readdir");
867
 
        exitstatus = EX_IOERR;
868
 
        goto fallback;
869
 
      }
870
 
      break;
871
 
    }
872
 
    
873
 
    d_name_len = strlen(dirst->d_name);
874
 
    
875
 
    /* Ignore dotfiles, backup files and other junk */
876
 
    {
877
 
      bool bad_name = false;
878
 
      
879
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
880
 
      
881
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
882
 
                                           ".dpkg-old",
883
 
                                           ".dpkg-bak",
884
 
                                           ".dpkg-divert", NULL };
885
 
#ifdef __GNUC__
886
 
#pragma GCC diagnostic push
887
 
#pragma GCC diagnostic ignored "-Wcast-qual"
888
 
#endif
889
 
      for(const char **pre = (const char **)bad_prefixes;
890
 
          *pre != NULL; pre++){
891
 
#ifdef __GNUC__
892
 
#pragma GCC diagnostic pop
893
 
#endif
894
 
        size_t pre_len = strlen(*pre);
895
 
        if((d_name_len >= pre_len)
896
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
897
 
          if(debug){
898
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
899
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
900
 
          }
901
 
          bad_name = true;
902
 
          break;
903
 
        }
904
 
      }
905
 
      if(bad_name){
906
 
        continue;
907
 
      }
908
 
#ifdef __GNUC__
909
 
#pragma GCC diagnostic push
910
 
#pragma GCC diagnostic ignored "-Wcast-qual"
911
 
#endif
912
 
      for(const char **suf = (const char **)bad_suffixes;
913
 
          *suf != NULL; suf++){
914
 
#ifdef __GNUC__
915
 
#pragma GCC diagnostic pop
916
 
#endif
917
 
        size_t suf_len = strlen(*suf);
918
 
        if((d_name_len >= suf_len)
919
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
920
 
                == 0)){
921
 
          if(debug){
922
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
923
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
924
 
          }
925
 
          bad_name = true;
926
 
          break;
927
 
        }
928
 
      }
929
 
      
930
 
      if(bad_name){
931
 
        continue;
932
 
      }
933
 
    }
934
 
    
935
 
    char *filename;
936
 
    if(plugindir == NULL){
937
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
938
 
                                             dirst->d_name));
939
 
    } else {
940
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
941
 
                                             plugindir,
942
 
                                             dirst->d_name));
943
 
    }
944
 
    if(ret < 0){
945
 
      error(0, errno, "asprintf");
 
910
  for(int i = 0; i < numplugins; i++){
 
911
    
 
912
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
913
    if(plugin_fd == -1){
 
914
      error(0, errno, "Could not open plugin");
 
915
      free(direntries[i]);
946
916
      continue;
947
917
    }
948
 
    
949
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
918
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
950
919
    if(ret == -1){
951
920
      error(0, errno, "stat");
952
 
      free(filename);
 
921
      close(plugin_fd);
 
922
      free(direntries[i]);
953
923
      continue;
954
924
    }
955
925
    
956
926
    /* Ignore non-executable files */
957
927
    if(not S_ISREG(st.st_mode)
958
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
928
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
 
929
                                        X_OK, 0)) != 0)){
959
930
      if(debug){
960
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
961
 
                " with bad type or mode\n", filename);
 
931
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
 
932
                " with bad type or mode\n",
 
933
                plugindir != NULL ? plugindir : PDIR,
 
934
                direntries[i]->d_name);
962
935
      }
963
 
      free(filename);
 
936
      close(plugin_fd);
 
937
      free(direntries[i]);
964
938
      continue;
965
939
    }
966
940
    
967
 
    plugin *p = getplugin(dirst->d_name);
 
941
    plugin *p = getplugin(direntries[i]->d_name);
968
942
    if(p == NULL){
969
943
      error(0, errno, "getplugin");
970
 
      free(filename);
 
944
      close(plugin_fd);
 
945
      free(direntries[i]);
971
946
      continue;
972
947
    }
973
948
    if(p->disabled){
974
949
      if(debug){
975
950
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
976
 
                dirst->d_name);
 
951
                direntries[i]->d_name);
977
952
      }
978
 
      free(filename);
 
953
      close(plugin_fd);
 
954
      free(direntries[i]);
979
955
      continue;
980
956
    }
981
957
    {
995
971
        }
996
972
      }
997
973
    }
998
 
    /* If this plugin has any environment variables, we will call
999
 
       using execve and need to duplicate the environment from this
1000
 
       process, too. */
 
974
    /* If this plugin has any environment variables, we need to
 
975
       duplicate the environment from this process, too. */
1001
976
    if(p->environ[0] != NULL){
1002
977
      for(char **e = environ; *e != NULL; e++){
1003
978
        if(not add_environment(p, *e, false)){
1007
982
    }
1008
983
    
1009
984
    int pipefd[2];
 
985
#ifndef O_CLOEXEC
1010
986
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
987
#else  /* O_CLOEXEC */
 
988
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
 
989
#endif  /* O_CLOEXEC */
1011
990
    if(ret == -1){
1012
991
      error(0, errno, "pipe");
1013
992
      exitstatus = EX_OSERR;
1014
 
      goto fallback;
1015
 
    }
 
993
      free(direntries[i]);
 
994
      goto fallback;
 
995
    }
 
996
    if(pipefd[0] >= FD_SETSIZE){
 
997
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
 
998
              FD_SETSIZE);
 
999
      close(pipefd[0]);
 
1000
      close(pipefd[1]);
 
1001
      exitstatus = EX_OSERR;
 
1002
      free(direntries[i]);
 
1003
      goto fallback;
 
1004
    }
 
1005
#ifndef O_CLOEXEC
1016
1006
    /* Ask OS to automatic close the pipe on exec */
1017
1007
    ret = set_cloexec_flag(pipefd[0]);
1018
1008
    if(ret < 0){
1019
1009
      error(0, errno, "set_cloexec_flag");
 
1010
      close(pipefd[0]);
 
1011
      close(pipefd[1]);
1020
1012
      exitstatus = EX_OSERR;
 
1013
      free(direntries[i]);
1021
1014
      goto fallback;
1022
1015
    }
1023
1016
    ret = set_cloexec_flag(pipefd[1]);
1024
1017
    if(ret < 0){
1025
1018
      error(0, errno, "set_cloexec_flag");
 
1019
      close(pipefd[0]);
 
1020
      close(pipefd[1]);
1026
1021
      exitstatus = EX_OSERR;
 
1022
      free(direntries[i]);
1027
1023
      goto fallback;
1028
1024
    }
 
1025
#endif  /* not O_CLOEXEC */
1029
1026
    /* Block SIGCHLD until process is safely in process list */
1030
1027
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1031
1028
                                              &sigchld_action.sa_mask,
1033
1030
    if(ret < 0){
1034
1031
      error(0, errno, "sigprocmask");
1035
1032
      exitstatus = EX_OSERR;
 
1033
      free(direntries[i]);
1036
1034
      goto fallback;
1037
1035
    }
1038
1036
    /* Starting a new process to be watched */
1042
1040
    } while(pid == -1 and errno == EINTR);
1043
1041
    if(pid == -1){
1044
1042
      error(0, errno, "fork");
 
1043
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1044
                                     &sigchld_action.sa_mask, NULL));
 
1045
      close(pipefd[0]);
 
1046
      close(pipefd[1]);
1045
1047
      exitstatus = EX_OSERR;
 
1048
      free(direntries[i]);
1046
1049
      goto fallback;
1047
1050
    }
1048
1051
    if(pid == 0){
1064
1067
        _exit(EX_OSERR);
1065
1068
      }
1066
1069
      
1067
 
      if(dirfd(dir) < 0){
1068
 
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
1069
 
           above and must now close it manually here. */
1070
 
        closedir(dir);
1071
 
      }
1072
 
      if(execve(filename, p->argv,
 
1070
      if(fexecve(plugin_fd, p->argv,
1073
1071
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1074
 
        error(0, errno, "execve for %s", filename);
 
1072
        error(0, errno, "fexecve for %s/%s",
 
1073
              plugindir != NULL ? plugindir : PDIR,
 
1074
              direntries[i]->d_name);
1075
1075
        _exit(EX_OSERR);
1076
1076
      }
1077
1077
      /* no return */
1078
1078
    }
1079
1079
    /* Parent process */
1080
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1081
 
                                             pipe */
1082
 
    free(filename);
1083
 
    plugin *new_plugin = getplugin(dirst->d_name);
 
1080
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1081
    close(plugin_fd);
 
1082
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1084
1083
    if(new_plugin == NULL){
1085
1084
      error(0, errno, "getplugin");
1086
1085
      ret = (int)(TEMP_FAILURE_RETRY
1090
1089
        error(0, errno, "sigprocmask");
1091
1090
      }
1092
1091
      exitstatus = EX_OSERR;
 
1092
      free(direntries[i]);
1093
1093
      goto fallback;
1094
1094
    }
 
1095
    free(direntries[i]);
1095
1096
    
1096
1097
    new_plugin->pid = pid;
1097
1098
    new_plugin->fd = pipefd[0];
1098
 
    
 
1099
 
 
1100
    if(debug){
 
1101
      fprintf(stderr, "Plugin %s started (PID %" PRIdMAX ")\n",
 
1102
              new_plugin->name, (intmax_t) (new_plugin->pid));
 
1103
    }
 
1104
 
1099
1105
    /* Unblock SIGCHLD so signal handler can be run if this process
1100
1106
       has already completed */
1101
1107
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1107
1113
      goto fallback;
1108
1114
    }
1109
1115
    
1110
 
#if defined (__GNUC__) and defined (__GLIBC__)
1111
 
#if not __GLIBC_PREREQ(2, 16)
1112
 
#pragma GCC diagnostic push
1113
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1114
 
#endif
1115
 
#endif
1116
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1117
 
                                          -Wconversion in GNU libc
1118
 
                                          before 2.16 */
1119
 
#if defined (__GNUC__) and defined (__GLIBC__)
1120
 
#if not __GLIBC_PREREQ(2, 16)
1121
 
#pragma GCC diagnostic pop
1122
 
#endif
1123
 
#endif
 
1116
    FD_SET(new_plugin->fd, &rfds_all);
1124
1117
    
1125
1118
    if(maxfd < new_plugin->fd){
1126
1119
      maxfd = new_plugin->fd;
1127
1120
    }
1128
1121
  }
1129
1122
  
1130
 
  TEMP_FAILURE_RETRY(closedir(dir));
1131
 
  dir = NULL;
 
1123
  free(direntries);
 
1124
  direntries = NULL;
 
1125
  close(dir_fd);
 
1126
  dir_fd = -1;
1132
1127
  free_plugin(getplugin(NULL));
1133
1128
  
1134
1129
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1173
1168
                      (intmax_t) (proc->pid),
1174
1169
                      WTERMSIG(proc->status),
1175
1170
                      strsignal(WTERMSIG(proc->status)));
1176
 
            } else if(WCOREDUMP(proc->status)){
1177
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1178
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
1179
1171
            }
1180
1172
          }
1181
1173
          
1182
1174
          /* Remove the plugin */
1183
 
#if defined (__GNUC__) and defined (__GLIBC__)
1184
 
#if not __GLIBC_PREREQ(2, 16)
1185
 
#pragma GCC diagnostic push
1186
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1187
 
#endif
1188
 
#endif
1189
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1190
 
                                          -Wconversion in GNU libc
1191
 
                                          before 2.16 */
1192
 
#if defined (__GNUC__) and defined (__GLIBC__)
1193
 
#if not __GLIBC_PREREQ(2, 16)
1194
 
#pragma GCC diagnostic pop
1195
 
#endif
1196
 
#endif
 
1175
          FD_CLR(proc->fd, &rfds_all);
1197
1176
          
1198
1177
          /* Block signal while modifying process_list */
1199
1178
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1239
1218
      }
1240
1219
      
1241
1220
      /* This process has not completed.  Does it have any output? */
1242
 
#if defined (__GNUC__) and defined (__GLIBC__)
1243
 
#if not __GLIBC_PREREQ(2, 16)
1244
 
#pragma GCC diagnostic push
1245
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1246
 
#endif
1247
 
#endif
1248
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1249
 
                                                         warning from
1250
 
                                                         -Wconversion
1251
 
                                                         in GNU libc
1252
 
                                                         before
1253
 
                                                         2.16 */
1254
 
#if defined (__GNUC__) and defined (__GLIBC__)
1255
 
#if not __GLIBC_PREREQ(2, 16)
1256
 
#pragma GCC diagnostic pop
1257
 
#endif
1258
 
#endif
 
1221
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1259
1222
        /* This process had nothing to say at this time */
1260
1223
        proc = proc->next;
1261
1224
        continue;
1328
1291
    free(custom_argv);
1329
1292
  }
1330
1293
  
1331
 
  if(dir != NULL){
1332
 
    closedir(dir);
 
1294
  free(direntries);
 
1295
  
 
1296
  if(dir_fd != -1){
 
1297
    close(dir_fd);
1333
1298
  }
1334
1299
  
1335
1300
  /* Kill the processes */
1355
1320
  free_plugin_list();
1356
1321
  
1357
1322
  free(plugindir);
 
1323
  free(pluginhelperdir);
1358
1324
  free(argfile);
1359
1325
  
1360
1326
  return exitstatus;