/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: 2014-03-23 19:24:40 UTC
  • Revision ID: teddy@recompile.se-20140323192440-d71iiqxebsxf9u2v
Update GCC warning flags and function attributes to GCC 4.7.

* Makefile (WARN): Update to include almost all warning flags.
* plugin-runner.c (getplugin, add_to_char_array, add_argument,
                   add_environment, set_cloexec_flag,
                   print_out_password): Add attribute
                                        "warn_unused_result".
  (main/parse_opt): Bug fix: Add error checking to --global-env,
                    --env-for, --plugin-dir, and --config-file, and
                    make sure errno does not "leak" from unrelated
                    functions.
* plugins.d/mandos-client.c
  (fprintf_plus, debuggnutls, resolve_callback): Add "nonnull"
                                                 attribute.
  (incbuffer, add_server, init_gpgme): Add "nonnull" and
                                       "warn_unused_result"
                                       attributes.
  (pgp_packet_decrypt, init_gnutls_global): - '' -
  (init_gnutls_session start_mandos_communication, get_flags): - '' -
  (good_flags, good_interface, interface_is_up): - '' -
  (interface_is_running, runnable_hook): - '' -
  (avahi_loop_with_timeout, bring_up_interface): : - '' -
  (safer_gnutls_strerror): Add "warn_unused_result" attribute.
  (notdotentries): Set "nonnull", "pure", and "warn_unused_result"
                   attributes.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  lower_privileges_permanently): Set "warn_unused_result" attribute.
  (run_network_hooks): Exit child process if it fails to do anything
                       it needs to do.  Make explicit cast to double
                       when passing float value to asprintf().  Change
                       return type to void - all callers changed.
  (bring_up_interface): Move variables "sd", "ret_errno", and
                        "ret_setflags" to innermost scope.  Bug fix:
                        Fail if could not get interface flags also in
                        non-debug mode, and restore old errno
                        correctly.  Print message if could not raise
                        (or later lower) privileges.
  (take_down_interface): Bug fix: When failing because it could not
                         get interface flags, restore old errno
                         correctly.  Print message if it could not
                         raise (or later lower) privileges.
  (main): Complain if failed to raise or lower privileges.  Only run
          network hooks or lower privileges if raising privileges was
          successful.

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-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
 
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
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
22
 * Contact the authors at <mandos@recompile.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
27
 
                                   O_CLOEXEC, pipe2() */
 
26
                                   asprintf(), O_CLOEXEC */
28
27
#include <stddef.h>             /* size_t, NULL */
29
 
#include <stdlib.h>             /* malloc(), reallocarray(), realloc(),
30
 
                                   EXIT_SUCCESS, exit() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   realloc() */
31
30
#include <stdbool.h>            /* bool, true, false */
32
31
#include <stdio.h>              /* fileno(), fprintf(),
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() */
 
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() */
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
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
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
 
                                */
 
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
                                   execv(), write(), read(),
 
53
                                   close() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal(), strcmp(), strncmp() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
71
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
73
#include <errno.h>              /* errno */
73
74
#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"
80
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
81
80
 
82
81
const char *argp_program_version = "plugin-runner " VERSION;
179
178
  /* Resize the pointed-to array to hold one more pointer */
180
179
  char **new_array = NULL;
181
180
  do {
182
 
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
183
 
    new_array = reallocarray(*array, (size_t)((*len) + 2),
184
 
                             sizeof(char *));
185
 
#else
186
 
    if(((size_t)((*len) + 2)) > (SIZE_MAX / sizeof(char *))){
187
 
      /* overflow */
188
 
      new_array = NULL;
189
 
      errno = ENOMEM;
190
 
    } else {
191
 
      new_array = realloc(*array, (size_t)((*len) + 2)
192
 
                          * sizeof(char *));
193
 
    }
194
 
#endif
 
181
    new_array = realloc(*array, sizeof(char *)
 
182
                        * (size_t) ((*len) + 2));
195
183
  } while(new_array == NULL and errno == EINTR);
196
184
  /* Malloc check */
197
185
  if(new_array == NULL){
252
240
  return add_to_char_array(def, &(p->environ), &(p->envc));
253
241
}
254
242
 
255
 
#ifndef O_CLOEXEC
256
243
/*
257
244
 * Based on the example in the GNU LibC manual chapter 13.13 "File
258
245
 * Descriptor Flags".
269
256
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
270
257
                                       ret | FD_CLOEXEC));
271
258
}
272
 
#endif  /* not O_CLOEXEC */
273
259
 
274
260
 
275
261
/* Mark processes as completed when they exit, and save their exit
324
310
__attribute__((nonnull))
325
311
static void free_plugin(plugin *plugin_node){
326
312
  
327
 
  for(char **arg = (plugin_node->argv)+1; *arg != NULL; arg++){
 
313
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
328
314
    free(*arg);
329
315
  }
330
 
  free(plugin_node->name);
331
316
  free(plugin_node->argv);
332
317
  for(char **env = plugin_node->environ; *env != NULL; env++){
333
318
    free(*env);
360
345
 
361
346
int main(int argc, char *argv[]){
362
347
  char *plugindir = NULL;
363
 
  char *pluginhelperdir = NULL;
364
348
  char *argfile = NULL;
365
349
  FILE *conffp;
366
 
  struct dirent **direntries = NULL;
 
350
  size_t d_name_len;
 
351
  DIR *dir = NULL;
 
352
  struct dirent *dirst;
367
353
  struct stat st;
368
354
  fd_set rfds_all;
369
355
  int ret, maxfd = 0;
377
363
                                      .sa_flags = SA_NOCLDSTOP };
378
364
  char **custom_argv = NULL;
379
365
  int custom_argc = 0;
380
 
  int dir_fd = -1;
381
366
  
382
367
  /* Establish a signal handler */
383
368
  sigemptyset(&sigchld_action.sa_mask);
428
413
      .doc = "Group ID the plugins will run as", .group = 3 },
429
414
    { .name = "debug", .key = 132,
430
415
      .doc = "Debug mode", .group = 4 },
431
 
    { .name = "plugin-helper-dir", .key = 133,
432
 
      .arg = "DIRECTORY",
433
 
      .doc = "Specify a different plugin helper directory",
434
 
      .group = 2 },
435
416
    /*
436
417
     * These reproduce what we would get without ARGP_NO_HELP
437
418
     */
563
544
    case 132:                   /* --debug */
564
545
      debug = true;
565
546
      break;
566
 
    case 133:                   /* --plugin-helper-dir */
567
 
      free(pluginhelperdir);
568
 
      pluginhelperdir = strdup(arg);
569
 
      if(pluginhelperdir != NULL){
570
 
        errno = 0;
571
 
      }
572
 
      break;
573
547
      /*
574
548
       * These reproduce what we would get without ARGP_NO_HELP
575
549
       */
576
550
    case '?':                   /* --help */
577
551
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
578
552
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
579
 
      __builtin_unreachable();
580
553
    case -3:                    /* --usage */
581
554
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
582
555
      argp_state_help(state, state->out_stream,
583
556
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
584
 
      __builtin_unreachable();
585
557
    case 'V':                   /* --version */
586
558
      fprintf(state->out_stream, "%s\n", argp_program_version);
587
559
      exit(EXIT_SUCCESS);
597
569
      if(arg[0] == '\0'){
598
570
        break;
599
571
      }
600
 
#if __GNUC__ >= 7
601
 
      __attribute__((fallthrough));
602
 
#else
603
 
          /* FALLTHROUGH */
604
 
#endif
605
572
    default:
606
573
      return ARGP_ERR_UNKNOWN;
607
574
    }
633
600
    case 130:                   /* --userid */
634
601
    case 131:                   /* --groupid */
635
602
    case 132:                   /* --debug */
636
 
    case 133:                   /* --plugin-helper-dir */
637
603
    case '?':                   /* --help */
638
604
    case -3:                    /* --usage */
639
605
    case 'V':                   /* --version */
719
685
        
720
686
        custom_argc += 1;
721
687
        {
722
 
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
723
 
          char **new_argv = reallocarray(custom_argv, (size_t)custom_argc + 1,
724
 
                                         sizeof(char *));
725
 
#else
726
 
          char **new_argv = NULL;
727
 
          if(((size_t)custom_argc + 1) > (SIZE_MAX / sizeof(char *))){
728
 
            /* overflow */
729
 
            errno = ENOMEM;
730
 
          } else {
731
 
            new_argv = realloc(custom_argv, ((size_t)custom_argc + 1)
732
 
                               * sizeof(char *));
733
 
          }
734
 
#endif
 
688
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
689
                                    * ((unsigned int)
 
690
                                       custom_argc + 1));
735
691
          if(new_argv == NULL){
736
 
            error(0, errno, "reallocarray");
 
692
            error(0, errno, "realloc");
737
693
            exitstatus = EX_OSERR;
738
694
            free(new_arg);
739
695
            free(org_line);
804
760
    goto fallback;
805
761
  }
806
762
  
807
 
  {
808
 
    char *pluginhelperenv;
809
 
    bool bret = true;
810
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
811
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
812
 
    if(ret != -1){
813
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
814
 
    }
815
 
    if(ret == -1 or not bret){
816
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
817
 
            " environment variable to \"%s\" for all plugins\n",
818
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
819
 
    }
820
 
    if(ret != -1){
821
 
      free(pluginhelperenv);
822
 
    }
823
 
  }
824
 
  
825
763
  if(debug){
826
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
764
    for(plugin *p = plugin_list; p != NULL; p=p->next){
827
765
      fprintf(stderr, "Plugin: %s has %d arguments\n",
828
766
              p->name ? p->name : "Global", p->argc - 1);
829
767
      for(char **a = p->argv; *a != NULL; a++){
838
776
  
839
777
  if(getuid() == 0){
840
778
    /* Work around Debian bug #633582:
841
 
       <https://bugs.debian.org/633582> */
 
779
       <http://bugs.debian.org/633582> */
842
780
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
843
781
    if(plugindir_fd == -1){
844
 
      if(errno != ENOENT){
845
 
        error(0, errno, "open(\"" PDIR "\")");
846
 
      }
 
782
      error(0, errno, "open");
847
783
    } else {
848
784
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
849
785
      if(ret == -1){
856
792
          }
857
793
        }
858
794
      }
859
 
      close(plugindir_fd);
 
795
      TEMP_FAILURE_RETRY(close(plugindir_fd));
860
796
    }
861
797
  }
862
798
  
872
808
  
873
809
  /* Open plugin directory with close_on_exec flag */
874
810
  {
875
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
876
 
#ifdef O_CLOEXEC
877
 
                  O_CLOEXEC
878
 
#else  /* not O_CLOEXEC */
879
 
                  0
880
 
#endif  /* not O_CLOEXEC */
881
 
                  );
 
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
    }
882
829
    if(dir_fd == -1){
883
830
      error(0, errno, "Could not open plugin dir");
884
831
      exitstatus = EX_UNAVAILABLE;
890
837
    ret = set_cloexec_flag(dir_fd);
891
838
    if(ret < 0){
892
839
      error(0, errno, "set_cloexec_flag");
 
840
      TEMP_FAILURE_RETRY(close(dir_fd));
893
841
      exitstatus = EX_OSERR;
894
842
      goto fallback;
895
843
    }
896
844
#endif  /* O_CLOEXEC */
897
 
  }
898
 
  
899
 
  int good_name(const struct dirent * const dirent){
900
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
901
 
                                      "*.dpkg-old", "*.dpkg-bak",
902
 
                                      "*.dpkg-divert", NULL };
903
 
#ifdef __GNUC__
904
 
#pragma GCC diagnostic push
905
 
#pragma GCC diagnostic ignored "-Wcast-qual"
906
 
#endif
907
 
    for(const char **pat = (const char **)patterns;
908
 
        *pat != NULL; pat++){
909
 
#ifdef __GNUC__
910
 
#pragma GCC diagnostic pop
911
 
#endif
912
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
913
 
         != FNM_NOMATCH){
914
 
        if(debug){
915
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
916
 
                    " matching pattern %s\n", dirent->d_name, *pat);
917
 
        }
918
 
        return 0;
919
 
      }
 
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;
920
852
    }
921
 
    return 1;
922
 
  }
923
 
  
924
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
925
 
                             alphasort);
926
 
  if(numplugins == -1){
927
 
    error(0, errno, "Could not scan plugin dir");
928
 
    direntries = NULL;
929
 
    exitstatus = EX_OSERR;
930
 
    goto fallback;
931
853
  }
932
854
  
933
855
  FD_ZERO(&rfds_all);
934
856
  
935
857
  /* Read and execute any executable in the plugin directory*/
936
 
  for(int i = 0; i < numplugins; i++){
937
 
    
938
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
939
 
    if(plugin_fd == -1){
940
 
      error(0, errno, "Could not open plugin");
941
 
      free(direntries[i]);
 
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");
942
946
      continue;
943
947
    }
944
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
948
    
 
949
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
945
950
    if(ret == -1){
946
951
      error(0, errno, "stat");
947
 
      close(plugin_fd);
948
 
      free(direntries[i]);
 
952
      free(filename);
949
953
      continue;
950
954
    }
951
955
    
952
956
    /* Ignore non-executable files */
953
957
    if(not S_ISREG(st.st_mode)
954
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
955
 
                                        X_OK, 0)) != 0)){
 
958
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
956
959
      if(debug){
957
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
958
 
                " with bad type or mode\n",
959
 
                plugindir != NULL ? plugindir : PDIR,
960
 
                direntries[i]->d_name);
 
960
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
961
                " with bad type or mode\n", filename);
961
962
      }
962
 
      close(plugin_fd);
963
 
      free(direntries[i]);
 
963
      free(filename);
964
964
      continue;
965
965
    }
966
966
    
967
 
    plugin *p = getplugin(direntries[i]->d_name);
 
967
    plugin *p = getplugin(dirst->d_name);
968
968
    if(p == NULL){
969
969
      error(0, errno, "getplugin");
970
 
      close(plugin_fd);
971
 
      free(direntries[i]);
 
970
      free(filename);
972
971
      continue;
973
972
    }
974
973
    if(p->disabled){
975
974
      if(debug){
976
975
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
977
 
                direntries[i]->d_name);
 
976
                dirst->d_name);
978
977
      }
979
 
      close(plugin_fd);
980
 
      free(direntries[i]);
 
978
      free(filename);
981
979
      continue;
982
980
    }
983
981
    {
997
995
        }
998
996
      }
999
997
    }
1000
 
    /* If this plugin has any environment variables, we need to
1001
 
       duplicate the environment from this process, too. */
 
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. */
1002
1001
    if(p->environ[0] != NULL){
1003
1002
      for(char **e = environ; *e != NULL; e++){
1004
1003
        if(not add_environment(p, *e, false)){
1008
1007
    }
1009
1008
    
1010
1009
    int pipefd[2];
1011
 
#ifndef O_CLOEXEC
1012
1010
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
1013
 
#else  /* O_CLOEXEC */
1014
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
1015
 
#endif  /* O_CLOEXEC */
1016
1011
    if(ret == -1){
1017
1012
      error(0, errno, "pipe");
1018
1013
      exitstatus = EX_OSERR;
1019
 
      free(direntries[i]);
1020
 
      goto fallback;
1021
 
    }
1022
 
    if(pipefd[0] >= FD_SETSIZE){
1023
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
1024
 
              FD_SETSIZE);
1025
 
      close(pipefd[0]);
1026
 
      close(pipefd[1]);
1027
 
      exitstatus = EX_OSERR;
1028
 
      free(direntries[i]);
1029
 
      goto fallback;
1030
 
    }
1031
 
#ifndef O_CLOEXEC
 
1014
      goto fallback;
 
1015
    }
1032
1016
    /* Ask OS to automatic close the pipe on exec */
1033
1017
    ret = set_cloexec_flag(pipefd[0]);
1034
1018
    if(ret < 0){
1035
1019
      error(0, errno, "set_cloexec_flag");
1036
 
      close(pipefd[0]);
1037
 
      close(pipefd[1]);
1038
1020
      exitstatus = EX_OSERR;
1039
 
      free(direntries[i]);
1040
1021
      goto fallback;
1041
1022
    }
1042
1023
    ret = set_cloexec_flag(pipefd[1]);
1043
1024
    if(ret < 0){
1044
1025
      error(0, errno, "set_cloexec_flag");
1045
 
      close(pipefd[0]);
1046
 
      close(pipefd[1]);
1047
1026
      exitstatus = EX_OSERR;
1048
 
      free(direntries[i]);
1049
1027
      goto fallback;
1050
1028
    }
1051
 
#endif  /* not O_CLOEXEC */
1052
1029
    /* Block SIGCHLD until process is safely in process list */
1053
1030
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1054
1031
                                              &sigchld_action.sa_mask,
1056
1033
    if(ret < 0){
1057
1034
      error(0, errno, "sigprocmask");
1058
1035
      exitstatus = EX_OSERR;
1059
 
      free(direntries[i]);
1060
1036
      goto fallback;
1061
1037
    }
1062
1038
    /* Starting a new process to be watched */
1066
1042
    } while(pid == -1 and errno == EINTR);
1067
1043
    if(pid == -1){
1068
1044
      error(0, errno, "fork");
1069
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1070
 
                                     &sigchld_action.sa_mask, NULL));
1071
 
      close(pipefd[0]);
1072
 
      close(pipefd[1]);
1073
1045
      exitstatus = EX_OSERR;
1074
 
      free(direntries[i]);
1075
1046
      goto fallback;
1076
1047
    }
1077
1048
    if(pid == 0){
1093
1064
        _exit(EX_OSERR);
1094
1065
      }
1095
1066
      
1096
 
      if(fexecve(plugin_fd, p->argv,
1097
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1098
 
        error(0, errno, "fexecve for %s/%s",
1099
 
              plugindir != NULL ? plugindir : PDIR,
1100
 
              direntries[i]->d_name);
1101
 
        _exit(EX_OSERR);
 
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(p->environ[0] == NULL){
 
1073
        if(execv(filename, p->argv) < 0){
 
1074
          error(0, errno, "execv for %s", filename);
 
1075
          _exit(EX_OSERR);
 
1076
        }
 
1077
      } else {
 
1078
        if(execve(filename, p->argv, p->environ) < 0){
 
1079
          error(0, errno, "execve for %s", filename);
 
1080
          _exit(EX_OSERR);
 
1081
        }
1102
1082
      }
1103
1083
      /* no return */
1104
1084
    }
1105
1085
    /* Parent process */
1106
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1107
 
    close(plugin_fd);
1108
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1086
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1087
                                             pipe */
 
1088
    free(filename);
 
1089
    plugin *new_plugin = getplugin(dirst->d_name);
1109
1090
    if(new_plugin == NULL){
1110
1091
      error(0, errno, "getplugin");
1111
1092
      ret = (int)(TEMP_FAILURE_RETRY
1115
1096
        error(0, errno, "sigprocmask");
1116
1097
      }
1117
1098
      exitstatus = EX_OSERR;
1118
 
      free(direntries[i]);
1119
1099
      goto fallback;
1120
1100
    }
1121
 
    free(direntries[i]);
1122
1101
    
1123
1102
    new_plugin->pid = pid;
1124
1103
    new_plugin->fd = pipefd[0];
1125
 
 
1126
 
    if(debug){
1127
 
      fprintf(stderr, "Plugin %s started (PID %" PRIdMAX ")\n",
1128
 
              new_plugin->name, (intmax_t) (new_plugin->pid));
1129
 
    }
1130
 
 
 
1104
    
1131
1105
    /* Unblock SIGCHLD so signal handler can be run if this process
1132
1106
       has already completed */
1133
1107
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1139
1113
      goto fallback;
1140
1114
    }
1141
1115
    
1142
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1116
#if defined (__GNUC__) and defined (__GLIBC__)
 
1117
#if not __GLIBC_PREREQ(2, 16)
 
1118
#pragma GCC diagnostic push
 
1119
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1120
#endif
 
1121
#endif
 
1122
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1123
                                          -Wconversion in GNU libc
 
1124
                                          before 2.16 */
 
1125
#if defined (__GNUC__) and defined (__GLIBC__)
 
1126
#if not __GLIBC_PREREQ(2, 16)
 
1127
#pragma GCC diagnostic pop
 
1128
#endif
 
1129
#endif
1143
1130
    
1144
1131
    if(maxfd < new_plugin->fd){
1145
1132
      maxfd = new_plugin->fd;
1146
1133
    }
1147
1134
  }
1148
1135
  
1149
 
  free(direntries);
1150
 
  direntries = NULL;
1151
 
  close(dir_fd);
1152
 
  dir_fd = -1;
 
1136
  TEMP_FAILURE_RETRY(closedir(dir));
 
1137
  dir = NULL;
1153
1138
  free_plugin(getplugin(NULL));
1154
1139
  
1155
1140
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1194
1179
                      (intmax_t) (proc->pid),
1195
1180
                      WTERMSIG(proc->status),
1196
1181
                      strsignal(WTERMSIG(proc->status)));
 
1182
            } else if(WCOREDUMP(proc->status)){
 
1183
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1184
                      " core\n", proc->name, (intmax_t) (proc->pid));
1197
1185
            }
1198
1186
          }
1199
1187
          
1200
1188
          /* Remove the plugin */
1201
 
          FD_CLR(proc->fd, &rfds_all);
 
1189
#if defined (__GNUC__) and defined (__GLIBC__)
 
1190
#if not __GLIBC_PREREQ(2, 16)
 
1191
#pragma GCC diagnostic push
 
1192
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1193
#endif
 
1194
#endif
 
1195
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1196
                                          -Wconversion in GNU libc
 
1197
                                          before 2.16 */
 
1198
#if defined (__GNUC__) and defined (__GLIBC__)
 
1199
#if not __GLIBC_PREREQ(2, 16)
 
1200
#pragma GCC diagnostic pop
 
1201
#endif
 
1202
#endif
1202
1203
          
1203
1204
          /* Block signal while modifying process_list */
1204
1205
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1244
1245
      }
1245
1246
      
1246
1247
      /* This process has not completed.  Does it have any output? */
1247
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1248
#if defined (__GNUC__) and defined (__GLIBC__)
 
1249
#if not __GLIBC_PREREQ(2, 16)
 
1250
#pragma GCC diagnostic push
 
1251
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1252
#endif
 
1253
#endif
 
1254
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1255
                                                         warning from
 
1256
                                                         -Wconversion
 
1257
                                                         in GNU libc
 
1258
                                                         before
 
1259
                                                         2.16 */
 
1260
#if defined (__GNUC__) and defined (__GLIBC__)
 
1261
#if not __GLIBC_PREREQ(2, 16)
 
1262
#pragma GCC diagnostic pop
 
1263
#endif
 
1264
#endif
1248
1265
        /* This process had nothing to say at this time */
1249
1266
        proc = proc->next;
1250
1267
        continue;
1317
1334
    free(custom_argv);
1318
1335
  }
1319
1336
  
1320
 
  free(direntries);
1321
 
  
1322
 
  if(dir_fd != -1){
1323
 
    close(dir_fd);
 
1337
  if(dir != NULL){
 
1338
    closedir(dir);
1324
1339
  }
1325
1340
  
1326
1341
  /* Kill the processes */
1346
1361
  free_plugin_list();
1347
1362
  
1348
1363
  free(plugindir);
1349
 
  free(pluginhelperdir);
1350
1364
  free(argfile);
1351
1365
  
1352
1366
  return exitstatus;