/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: 2016-06-03 17:27:03 UTC
  • Revision ID: teddy@recompile.se-20160603172703-mc6tjor6rhq4xy74
mandos: Bug fix: Do multiprocessing cleanup correctly on exit

* mandos (main): Save module "multiprocessing" and open file "wnull"
                 as scope variables accessible by function cleanup(),
                 since the module and global variable may not be
                 accessible when the cleanup() function is run as
                 scheduled by atexit().

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-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
 
5
 * Copyright © 2008-2016 Teddy Hogeborn
 
6
 * Copyright © 2008-2016 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
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   O_CLOEXEC, pipe2() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#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() */
 
32
                                   stderr, STDOUT_FILENO, fclose() */
 
33
#include <sys/types.h>          /* fstat(), struct stat, waitpid(),
 
34
                                   WIFEXITED(), WEXITSTATUS(), wait(),
 
35
                                   pid_t, uid_t, gid_t, getuid(),
 
36
                                   getgid() */
38
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
40
                                   WEXITSTATUS(), WTERMSIG() */
 
41
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
44
42
#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
 
                                   execv(), write(), read(),
53
 
                                   close() */
 
43
#include <dirent.h>             /* struct dirent, scandirat() */
 
44
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
45
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
46
                                   struct stat, fstat(), close(),
 
47
                                   setgid(), setuid(), S_ISREG(),
 
48
                                   faccessat() pipe2(), fork(),
 
49
                                   _exit(), dup2(), fexecve(), read()
 
50
                                */
54
51
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
52
                                   FD_CLOEXEC, openat(), scandirat(),
 
53
                                   pipe2() */
 
54
#include <string.h>             /* strsep, strlen(), strsignal(),
 
55
                                   strcmp(), strncmp() */
58
56
#include <errno.h>              /* errno */
59
57
#include <argp.h>               /* struct argp_option, struct
60
58
                                   argp_state, struct argp,
72
70
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
71
#include <errno.h>              /* errno */
74
72
#include <error.h>              /* error() */
 
73
#include <fnmatch.h>            /* fnmatch() */
75
74
 
76
75
#define BUFFER_SIZE 256
77
76
 
78
77
#define PDIR "/lib/mandos/plugins.d"
 
78
#define PHDIR "/lib/mandos/plugin-helpers"
79
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
80
 
81
81
const char *argp_program_version = "plugin-runner " VERSION;
105
105
 
106
106
/* Gets an existing plugin based on name,
107
107
   or if none is found, creates a new one */
 
108
__attribute__((warn_unused_result))
108
109
static plugin *getplugin(char *name){
109
110
  /* Check for existing plugin with that name */
110
111
  for(plugin *p = plugin_list; p != NULL; p = p->next){
171
172
}
172
173
 
173
174
/* Helper function for add_argument and add_environment */
 
175
__attribute__((nonnull, warn_unused_result))
174
176
static bool add_to_char_array(const char *new, char ***array,
175
177
                              int *len){
176
178
  /* Resize the pointed-to array to hold one more pointer */
 
179
  char **new_array = NULL;
177
180
  do {
178
 
    *array = realloc(*array, sizeof(char *)
179
 
                     * (size_t) ((*len) + 2));
180
 
  } while(*array == NULL and errno == EINTR);
 
181
    new_array = realloc(*array, sizeof(char *)
 
182
                        * (size_t) ((*len) + 2));
 
183
  } while(new_array == NULL and errno == EINTR);
181
184
  /* Malloc check */
182
 
  if(*array == NULL){
 
185
  if(new_array == NULL){
183
186
    return false;
184
187
  }
 
188
  *array = new_array;
185
189
  /* Make a copy of the new string */
186
190
  char *copy;
187
191
  do {
199
203
}
200
204
 
201
205
/* Add to a plugin's argument vector */
 
206
__attribute__((nonnull(2), warn_unused_result))
202
207
static bool add_argument(plugin *p, const char *arg){
203
208
  if(p == NULL){
204
209
    return false;
207
212
}
208
213
 
209
214
/* Add to a plugin's environment */
 
215
__attribute__((nonnull(2), warn_unused_result))
210
216
static bool add_environment(plugin *p, const char *def, bool replace){
211
217
  if(p == NULL){
212
218
    return false;
214
220
  /* namelen = length of name of environment variable */
215
221
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
216
222
  /* Search for this environment variable */
217
 
  for(char **e = p->environ; *e != NULL; e++){
218
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
223
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
224
    if(strncmp(*envdef, def, namelen + 1) == 0){
219
225
      /* It already exists */
220
226
      if(replace){
221
 
        char *new;
 
227
        char *new_envdef;
222
228
        do {
223
 
          new = realloc(*e, strlen(def) + 1);
224
 
        } while(new == NULL and errno == EINTR);
225
 
        if(new == NULL){
 
229
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
230
        } while(new_envdef == NULL and errno == EINTR);
 
231
        if(new_envdef == NULL){
226
232
          return false;
227
233
        }
228
 
        *e = new;
229
 
        strcpy(*e, def);
 
234
        *envdef = new_envdef;
 
235
        strcpy(*envdef, def);
230
236
      }
231
237
      return true;
232
238
    }
234
240
  return add_to_char_array(def, &(p->environ), &(p->envc));
235
241
}
236
242
 
 
243
#ifndef O_CLOEXEC
237
244
/*
238
245
 * Based on the example in the GNU LibC manual chapter 13.13 "File
239
246
 * Descriptor Flags".
240
247
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
241
248
 */
 
249
__attribute__((warn_unused_result))
242
250
static int set_cloexec_flag(int fd){
243
251
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
244
252
  /* If reading the flags failed, return error indication now. */
249
257
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
250
258
                                       ret | FD_CLOEXEC));
251
259
}
 
260
#endif  /* not O_CLOEXEC */
252
261
 
253
262
 
254
263
/* Mark processes as completed when they exit, and save their exit
286
295
}
287
296
 
288
297
/* Prints out a password to stdout */
 
298
__attribute__((nonnull, warn_unused_result))
289
299
static bool print_out_password(const char *buffer, size_t length){
290
300
  ssize_t ret;
291
301
  for(size_t written = 0; written < length; written += (size_t)ret){
299
309
}
300
310
 
301
311
/* Removes and free a plugin from the plugin list */
 
312
__attribute__((nonnull))
302
313
static void free_plugin(plugin *plugin_node){
303
314
  
304
315
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
336
347
 
337
348
int main(int argc, char *argv[]){
338
349
  char *plugindir = NULL;
 
350
  char *pluginhelperdir = NULL;
339
351
  char *argfile = NULL;
340
352
  FILE *conffp;
341
 
  size_t d_name_len;
342
 
  DIR *dir = NULL;
343
 
  struct dirent *dirst;
 
353
  struct dirent **direntries = NULL;
344
354
  struct stat st;
345
355
  fd_set rfds_all;
346
356
  int ret, maxfd = 0;
354
364
                                      .sa_flags = SA_NOCLDSTOP };
355
365
  char **custom_argv = NULL;
356
366
  int custom_argc = 0;
 
367
  int dir_fd = -1;
357
368
  
358
369
  /* Establish a signal handler */
359
370
  sigemptyset(&sigchld_action.sa_mask);
404
415
      .doc = "Group ID the plugins will run as", .group = 3 },
405
416
    { .name = "debug", .key = 132,
406
417
      .doc = "Debug mode", .group = 4 },
 
418
    { .name = "plugin-helper-dir", .key = 133,
 
419
      .arg = "DIRECTORY",
 
420
      .doc = "Specify a different plugin helper directory",
 
421
      .group = 2 },
407
422
    /*
408
423
     * These reproduce what we would get without ARGP_NO_HELP
409
424
     */
416
431
    { .name = NULL }
417
432
  };
418
433
  
 
434
  __attribute__((nonnull(3)))
419
435
  error_t parse_opt(int key, char *arg, struct argp_state *state){
420
436
    errno = 0;
421
437
    switch(key){
422
438
      char *tmp;
423
 
      intmax_t tmpmax;
 
439
      intmax_t tmp_id;
424
440
    case 'g':                   /* --global-options */
425
441
      {
426
442
        char *plugin_option;
429
445
            break;
430
446
          }
431
447
        }
 
448
        errno = 0;
432
449
      }
433
450
      break;
434
451
    case 'G':                   /* --global-env */
435
 
      add_environment(getplugin(NULL), arg, true);
 
452
      if(add_environment(getplugin(NULL), arg, true)){
 
453
        errno = 0;
 
454
      }
436
455
      break;
437
456
    case 'o':                   /* --options-for */
438
457
      {
455
474
            break;
456
475
          }
457
476
        }
 
477
        errno = 0;
458
478
      }
459
479
      break;
460
480
    case 'E':                   /* --env-for */
472
492
          errno = EINVAL;
473
493
          break;
474
494
        }
475
 
        add_environment(getplugin(arg), envdef, true);
 
495
        if(add_environment(getplugin(arg), envdef, true)){
 
496
          errno = 0;
 
497
        }
476
498
      }
477
499
      break;
478
500
    case 'd':                   /* --disable */
480
502
        plugin *p = getplugin(arg);
481
503
        if(p != NULL){
482
504
          p->disabled = true;
 
505
          errno = 0;
483
506
        }
484
507
      }
485
508
      break;
488
511
        plugin *p = getplugin(arg);
489
512
        if(p != NULL){
490
513
          p->disabled = false;
 
514
          errno = 0;
491
515
        }
492
516
      }
493
517
      break;
494
518
    case 128:                   /* --plugin-dir */
495
519
      free(plugindir);
496
520
      plugindir = strdup(arg);
 
521
      if(plugindir != NULL){
 
522
        errno = 0;
 
523
      }
497
524
      break;
498
525
    case 129:                   /* --config-file */
499
526
      /* This is already done by parse_opt_config_file() */
500
527
      break;
501
528
    case 130:                   /* --userid */
502
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
529
      tmp_id = strtoimax(arg, &tmp, 10);
503
530
      if(errno != 0 or tmp == arg or *tmp != '\0'
504
 
         or tmpmax != (uid_t)tmpmax){
 
531
         or tmp_id != (uid_t)tmp_id){
505
532
        argp_error(state, "Bad user ID number: \"%s\", using %"
506
533
                   PRIdMAX, arg, (intmax_t)uid);
507
534
        break;
508
535
      }
509
 
      uid = (uid_t)tmpmax;
 
536
      uid = (uid_t)tmp_id;
 
537
      errno = 0;
510
538
      break;
511
539
    case 131:                   /* --groupid */
512
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
540
      tmp_id = strtoimax(arg, &tmp, 10);
513
541
      if(errno != 0 or tmp == arg or *tmp != '\0'
514
 
         or tmpmax != (gid_t)tmpmax){
 
542
         or tmp_id != (gid_t)tmp_id){
515
543
        argp_error(state, "Bad group ID number: \"%s\", using %"
516
544
                   PRIdMAX, arg, (intmax_t)gid);
517
545
        break;
518
546
      }
519
 
      gid = (gid_t)tmpmax;
 
547
      gid = (gid_t)tmp_id;
 
548
      errno = 0;
520
549
      break;
521
550
    case 132:                   /* --debug */
522
551
      debug = true;
523
552
      break;
 
553
    case 133:                   /* --plugin-helper-dir */
 
554
      free(pluginhelperdir);
 
555
      pluginhelperdir = strdup(arg);
 
556
      if(pluginhelperdir != NULL){
 
557
        errno = 0;
 
558
      }
 
559
      break;
524
560
      /*
525
561
       * These reproduce what we would get without ARGP_NO_HELP
526
562
       */
570
606
    case 129:                   /* --config-file */
571
607
      free(argfile);
572
608
      argfile = strdup(arg);
 
609
      if(argfile != NULL){
 
610
        errno = 0;
 
611
      }
573
612
      break;
574
613
    case 130:                   /* --userid */
575
614
    case 131:                   /* --groupid */
576
615
    case 132:                   /* --debug */
 
616
    case 133:                   /* --plugin-helper-dir */
577
617
    case '?':                   /* --help */
578
618
    case -3:                    /* --usage */
579
619
    case 'V':                   /* --version */
658
698
        }
659
699
        
660
700
        custom_argc += 1;
661
 
        custom_argv = realloc(custom_argv, sizeof(char *)
662
 
                              * ((unsigned int) custom_argc + 1));
663
 
        if(custom_argv == NULL){
664
 
          error(0, errno, "realloc");
665
 
          exitstatus = EX_OSERR;
666
 
          free(org_line);
667
 
          goto fallback;
 
701
        {
 
702
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
703
                                    * ((size_t)custom_argc + 1));
 
704
          if(new_argv == NULL){
 
705
            error(0, errno, "realloc");
 
706
            exitstatus = EX_OSERR;
 
707
            free(new_arg);
 
708
            free(org_line);
 
709
            goto fallback;
 
710
          } else {
 
711
            custom_argv = new_argv;
 
712
          }
668
713
        }
669
714
        custom_argv[custom_argc-1] = new_arg;
670
715
        custom_argv[custom_argc] = NULL;
728
773
    goto fallback;
729
774
  }
730
775
  
 
776
  {
 
777
    char *pluginhelperenv;
 
778
    bool bret = true;
 
779
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
 
780
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
781
    if(ret != -1){
 
782
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
 
783
    }
 
784
    if(ret == -1 or not bret){
 
785
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
 
786
            " environment variable to \"%s\" for all plugins\n",
 
787
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
788
    }
 
789
    if(ret != -1){
 
790
      free(pluginhelperenv);
 
791
    }
 
792
  }
 
793
  
731
794
  if(debug){
732
795
    for(plugin *p = plugin_list; p != NULL; p=p->next){
733
796
      fprintf(stderr, "Plugin: %s has %d arguments\n",
742
805
    }
743
806
  }
744
807
  
745
 
  {
 
808
  if(getuid() == 0){
746
809
    /* Work around Debian bug #633582:
747
810
       <http://bugs.debian.org/633582> */
748
811
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
749
812
    if(plugindir_fd == -1){
750
 
      error(0, errno, "open");
 
813
      if(errno != ENOENT){
 
814
        error(0, errno, "open(\"" PDIR "\")");
 
815
      }
751
816
    } else {
752
817
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
753
818
      if(ret == -1){
760
825
          }
761
826
        }
762
827
      }
763
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
828
      close(plugindir_fd);
764
829
    }
765
830
  }
766
831
  
767
832
  /* Lower permissions */
768
 
  setgid(gid);
 
833
  ret = setgid(gid);
769
834
  if(ret == -1){
770
835
    error(0, errno, "setgid");
771
836
  }
776
841
  
777
842
  /* Open plugin directory with close_on_exec flag */
778
843
  {
779
 
    int dir_fd = -1;
780
 
    if(plugindir == NULL){
781
 
      dir_fd = open(PDIR, O_RDONLY |
782
 
#ifdef O_CLOEXEC
783
 
                    O_CLOEXEC
784
 
#else  /* not O_CLOEXEC */
785
 
                    0
786
 
#endif  /* not O_CLOEXEC */
787
 
                    );
788
 
    } else {
789
 
      dir_fd = open(plugindir, O_RDONLY |
790
 
#ifdef O_CLOEXEC
791
 
                    O_CLOEXEC
792
 
#else  /* not O_CLOEXEC */
793
 
                    0
794
 
#endif  /* not O_CLOEXEC */
795
 
                    );
796
 
    }
 
844
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
 
845
#ifdef O_CLOEXEC
 
846
                  O_CLOEXEC
 
847
#else  /* not O_CLOEXEC */
 
848
                  0
 
849
#endif  /* not O_CLOEXEC */
 
850
                  );
797
851
    if(dir_fd == -1){
798
852
      error(0, errno, "Could not open plugin dir");
799
853
      exitstatus = EX_UNAVAILABLE;
805
859
    ret = set_cloexec_flag(dir_fd);
806
860
    if(ret < 0){
807
861
      error(0, errno, "set_cloexec_flag");
808
 
      TEMP_FAILURE_RETRY(close(dir_fd));
809
862
      exitstatus = EX_OSERR;
810
863
      goto fallback;
811
864
    }
812
865
#endif  /* O_CLOEXEC */
813
 
    
814
 
    dir = fdopendir(dir_fd);
815
 
    if(dir == NULL){
816
 
      error(0, errno, "Could not open plugin dir");
817
 
      TEMP_FAILURE_RETRY(close(dir_fd));
818
 
      exitstatus = EX_OSERR;
819
 
      goto fallback;
 
866
  }
 
867
  
 
868
  int good_name(const struct dirent * const dirent){
 
869
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
 
870
                                      "*.dpkg-old", "*.dpkg-bak",
 
871
                                      "*.dpkg-divert", NULL };
 
872
#ifdef __GNUC__
 
873
#pragma GCC diagnostic push
 
874
#pragma GCC diagnostic ignored "-Wcast-qual"
 
875
#endif
 
876
    for(const char **pat = (const char **)patterns;
 
877
        *pat != NULL; pat++){
 
878
#ifdef __GNUC__
 
879
#pragma GCC diagnostic pop
 
880
#endif
 
881
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
 
882
         != FNM_NOMATCH){
 
883
        if(debug){
 
884
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
885
                    " matching pattern %s\n", dirent->d_name, *pat);
 
886
        }
 
887
        return 0;
 
888
      }
820
889
    }
 
890
    return 1;
 
891
  }
 
892
  
 
893
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
 
894
                             alphasort);
 
895
  if(numplugins == -1){
 
896
    error(0, errno, "Could not scan plugin dir");
 
897
    direntries = NULL;
 
898
    exitstatus = EX_OSERR;
 
899
    goto fallback;
821
900
  }
822
901
  
823
902
  FD_ZERO(&rfds_all);
824
903
  
825
904
  /* Read and execute any executable in the plugin directory*/
826
 
  while(true){
827
 
    do {
828
 
      dirst = readdir(dir);
829
 
    } while(dirst == NULL and errno == EINTR);
830
 
    
831
 
    /* All directory entries have been processed */
832
 
    if(dirst == NULL){
833
 
      if(errno == EBADF){
834
 
        error(0, errno, "readdir");
835
 
        exitstatus = EX_IOERR;
836
 
        goto fallback;
837
 
      }
838
 
      break;
839
 
    }
840
 
    
841
 
    d_name_len = strlen(dirst->d_name);
842
 
    
843
 
    /* Ignore dotfiles, backup files and other junk */
844
 
    {
845
 
      bool bad_name = false;
846
 
      
847
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
848
 
      
849
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
850
 
                                           ".dpkg-old",
851
 
                                           ".dpkg-bak",
852
 
                                           ".dpkg-divert", NULL };
853
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
854
 
        size_t pre_len = strlen(*pre);
855
 
        if((d_name_len >= pre_len)
856
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
857
 
          if(debug){
858
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
859
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
860
 
          }
861
 
          bad_name = true;
862
 
          break;
863
 
        }
864
 
      }
865
 
      if(bad_name){
866
 
        continue;
867
 
      }
868
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
869
 
        size_t suf_len = strlen(*suf);
870
 
        if((d_name_len >= suf_len)
871
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
872
 
                == 0)){
873
 
          if(debug){
874
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
875
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
876
 
          }
877
 
          bad_name = true;
878
 
          break;
879
 
        }
880
 
      }
881
 
      
882
 
      if(bad_name){
883
 
        continue;
884
 
      }
885
 
    }
886
 
    
887
 
    char *filename;
888
 
    if(plugindir == NULL){
889
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
890
 
                                             dirst->d_name));
891
 
    } else {
892
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
893
 
                                             plugindir,
894
 
                                             dirst->d_name));
895
 
    }
896
 
    if(ret < 0){
897
 
      error(0, errno, "asprintf");
 
905
  for(int i = 0; i < numplugins; i++){
 
906
    
 
907
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
908
    if(plugin_fd == -1){
 
909
      error(0, errno, "Could not open plugin");
 
910
      free(direntries[i]);
898
911
      continue;
899
912
    }
900
 
    
901
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
913
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
902
914
    if(ret == -1){
903
915
      error(0, errno, "stat");
904
 
      free(filename);
 
916
      close(plugin_fd);
 
917
      free(direntries[i]);
905
918
      continue;
906
919
    }
907
920
    
908
921
    /* Ignore non-executable files */
909
922
    if(not S_ISREG(st.st_mode)
910
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
923
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
 
924
                                        X_OK, 0)) != 0)){
911
925
      if(debug){
912
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
913
 
                " with bad type or mode\n", filename);
 
926
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
 
927
                " with bad type or mode\n",
 
928
                plugindir != NULL ? plugindir : PDIR,
 
929
                direntries[i]->d_name);
914
930
      }
915
 
      free(filename);
 
931
      close(plugin_fd);
 
932
      free(direntries[i]);
916
933
      continue;
917
934
    }
918
935
    
919
 
    plugin *p = getplugin(dirst->d_name);
 
936
    plugin *p = getplugin(direntries[i]->d_name);
920
937
    if(p == NULL){
921
938
      error(0, errno, "getplugin");
922
 
      free(filename);
 
939
      close(plugin_fd);
 
940
      free(direntries[i]);
923
941
      continue;
924
942
    }
925
943
    if(p->disabled){
926
944
      if(debug){
927
945
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
928
 
                dirst->d_name);
 
946
                direntries[i]->d_name);
929
947
      }
930
 
      free(filename);
 
948
      close(plugin_fd);
 
949
      free(direntries[i]);
931
950
      continue;
932
951
    }
933
952
    {
947
966
        }
948
967
      }
949
968
    }
950
 
    /* If this plugin has any environment variables, we will call
951
 
       using execve and need to duplicate the environment from this
952
 
       process, too. */
 
969
    /* If this plugin has any environment variables, we need to
 
970
       duplicate the environment from this process, too. */
953
971
    if(p->environ[0] != NULL){
954
972
      for(char **e = environ; *e != NULL; e++){
955
973
        if(not add_environment(p, *e, false)){
959
977
    }
960
978
    
961
979
    int pipefd[2];
 
980
#ifndef O_CLOEXEC
962
981
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
982
#else  /* O_CLOEXEC */
 
983
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
 
984
#endif  /* O_CLOEXEC */
963
985
    if(ret == -1){
964
986
      error(0, errno, "pipe");
965
987
      exitstatus = EX_OSERR;
966
 
      goto fallback;
967
 
    }
 
988
      free(direntries[i]);
 
989
      goto fallback;
 
990
    }
 
991
    if(pipefd[0] >= FD_SETSIZE){
 
992
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
 
993
              FD_SETSIZE);
 
994
      close(pipefd[0]);
 
995
      close(pipefd[1]);
 
996
      exitstatus = EX_OSERR;
 
997
      free(direntries[i]);
 
998
      goto fallback;
 
999
    }
 
1000
#ifndef O_CLOEXEC
968
1001
    /* Ask OS to automatic close the pipe on exec */
969
1002
    ret = set_cloexec_flag(pipefd[0]);
970
1003
    if(ret < 0){
971
1004
      error(0, errno, "set_cloexec_flag");
 
1005
      close(pipefd[0]);
 
1006
      close(pipefd[1]);
972
1007
      exitstatus = EX_OSERR;
 
1008
      free(direntries[i]);
973
1009
      goto fallback;
974
1010
    }
975
1011
    ret = set_cloexec_flag(pipefd[1]);
976
1012
    if(ret < 0){
977
1013
      error(0, errno, "set_cloexec_flag");
 
1014
      close(pipefd[0]);
 
1015
      close(pipefd[1]);
978
1016
      exitstatus = EX_OSERR;
 
1017
      free(direntries[i]);
979
1018
      goto fallback;
980
1019
    }
 
1020
#endif  /* not O_CLOEXEC */
981
1021
    /* Block SIGCHLD until process is safely in process list */
982
1022
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
983
1023
                                              &sigchld_action.sa_mask,
985
1025
    if(ret < 0){
986
1026
      error(0, errno, "sigprocmask");
987
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
988
1029
      goto fallback;
989
1030
    }
990
1031
    /* Starting a new process to be watched */
994
1035
    } while(pid == -1 and errno == EINTR);
995
1036
    if(pid == -1){
996
1037
      error(0, errno, "fork");
 
1038
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1039
                                     &sigchld_action.sa_mask, NULL));
 
1040
      close(pipefd[0]);
 
1041
      close(pipefd[1]);
997
1042
      exitstatus = EX_OSERR;
 
1043
      free(direntries[i]);
998
1044
      goto fallback;
999
1045
    }
1000
1046
    if(pid == 0){
1016
1062
        _exit(EX_OSERR);
1017
1063
      }
1018
1064
      
1019
 
      if(dirfd(dir) < 0){
1020
 
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
1021
 
           above and must now close it manually here. */
1022
 
        closedir(dir);
1023
 
      }
1024
 
      if(p->environ[0] == NULL){
1025
 
        if(execv(filename, p->argv) < 0){
1026
 
          error(0, errno, "execv for %s", filename);
1027
 
          _exit(EX_OSERR);
1028
 
        }
1029
 
      } else {
1030
 
        if(execve(filename, p->argv, p->environ) < 0){
1031
 
          error(0, errno, "execve for %s", filename);
1032
 
          _exit(EX_OSERR);
1033
 
        }
 
1065
      if(fexecve(plugin_fd, p->argv,
 
1066
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
 
1067
        error(0, errno, "fexecve for %s/%s",
 
1068
              plugindir != NULL ? plugindir : PDIR,
 
1069
              direntries[i]->d_name);
 
1070
        _exit(EX_OSERR);
1034
1071
      }
1035
1072
      /* no return */
1036
1073
    }
1037
1074
    /* Parent process */
1038
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1039
 
                                             pipe */
1040
 
    free(filename);
1041
 
    plugin *new_plugin = getplugin(dirst->d_name);
 
1075
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1076
    close(plugin_fd);
 
1077
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1042
1078
    if(new_plugin == NULL){
1043
1079
      error(0, errno, "getplugin");
1044
1080
      ret = (int)(TEMP_FAILURE_RETRY
1048
1084
        error(0, errno, "sigprocmask");
1049
1085
      }
1050
1086
      exitstatus = EX_OSERR;
 
1087
      free(direntries[i]);
1051
1088
      goto fallback;
1052
1089
    }
 
1090
    free(direntries[i]);
1053
1091
    
1054
1092
    new_plugin->pid = pid;
1055
1093
    new_plugin->fd = pipefd[0];
1065
1103
      goto fallback;
1066
1104
    }
1067
1105
    
1068
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1069
 
                                          -Wconversion */
 
1106
    FD_SET(new_plugin->fd, &rfds_all);
1070
1107
    
1071
1108
    if(maxfd < new_plugin->fd){
1072
1109
      maxfd = new_plugin->fd;
1073
1110
    }
1074
1111
  }
1075
1112
  
1076
 
  TEMP_FAILURE_RETRY(closedir(dir));
1077
 
  dir = NULL;
 
1113
  free(direntries);
 
1114
  direntries = NULL;
 
1115
  close(dir_fd);
 
1116
  dir_fd = -1;
1078
1117
  free_plugin(getplugin(NULL));
1079
1118
  
1080
1119
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1119
1158
                      (intmax_t) (proc->pid),
1120
1159
                      WTERMSIG(proc->status),
1121
1160
                      strsignal(WTERMSIG(proc->status)));
1122
 
            } else if(WCOREDUMP(proc->status)){
1123
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1124
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
1125
1161
            }
1126
1162
          }
1127
1163
          
1128
1164
          /* Remove the plugin */
1129
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1130
 
                                          -Wconversion */
 
1165
          FD_CLR(proc->fd, &rfds_all);
1131
1166
          
1132
1167
          /* Block signal while modifying process_list */
1133
1168
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1173
1208
      }
1174
1209
      
1175
1210
      /* This process has not completed.  Does it have any output? */
1176
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1177
 
                                                         warning from
1178
 
                                                         -Wconversion */
 
1211
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1179
1212
        /* This process had nothing to say at this time */
1180
1213
        proc = proc->next;
1181
1214
        continue;
1182
1215
      }
1183
1216
      /* Before reading, make the process' data buffer large enough */
1184
1217
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1185
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1186
 
                               + (size_t) BUFFER_SIZE);
1187
 
        if(proc->buffer == NULL){
 
1218
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1219
                                   + (size_t) BUFFER_SIZE);
 
1220
        if(new_buffer == NULL){
1188
1221
          error(0, errno, "malloc");
1189
1222
          exitstatus = EX_OSERR;
1190
1223
          goto fallback;
1191
1224
        }
 
1225
        proc->buffer = new_buffer;
1192
1226
        proc->buffer_size += BUFFER_SIZE;
1193
1227
      }
1194
1228
      /* Read from the process */
1247
1281
    free(custom_argv);
1248
1282
  }
1249
1283
  
1250
 
  if(dir != NULL){
1251
 
    closedir(dir);
 
1284
  free(direntries);
 
1285
  
 
1286
  if(dir_fd != -1){
 
1287
    close(dir_fd);
1252
1288
  }
1253
1289
  
1254
1290
  /* Kill the processes */
1274
1310
  free_plugin_list();
1275
1311
  
1276
1312
  free(plugindir);
 
1313
  free(pluginhelperdir);
1277
1314
  free(argfile);
1278
1315
  
1279
1316
  return exitstatus;