/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: 2016-06-03 17:27:03 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 343.
  • 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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   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;
82
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
83
83
 
84
84
typedef struct plugin{
85
85
  char *name;                   /* can be NULL or any plugin name */
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
 
  /* Strip permissions down to nobody */
746
 
  setgid(gid);
 
808
  if(getuid() == 0){
 
809
    /* Work around Debian bug #633582:
 
810
       <http://bugs.debian.org/633582> */
 
811
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
812
    if(plugindir_fd == -1){
 
813
      if(errno != ENOENT){
 
814
        error(0, errno, "open(\"" PDIR "\")");
 
815
      }
 
816
    } else {
 
817
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
818
      if(ret == -1){
 
819
        error(0, errno, "fstat");
 
820
      } else {
 
821
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
822
          ret = fchown(plugindir_fd, uid, gid);
 
823
          if(ret == -1){
 
824
            error(0, errno, "fchown");
 
825
          }
 
826
        }
 
827
      }
 
828
      close(plugindir_fd);
 
829
    }
 
830
  }
 
831
  
 
832
  /* Lower permissions */
 
833
  ret = setgid(gid);
747
834
  if(ret == -1){
748
835
    error(0, errno, "setgid");
749
836
  }
754
841
  
755
842
  /* Open plugin directory with close_on_exec flag */
756
843
  {
757
 
    int dir_fd = -1;
758
 
    if(plugindir == NULL){
759
 
      dir_fd = open(PDIR, O_RDONLY |
760
 
#ifdef O_CLOEXEC
761
 
                    O_CLOEXEC
762
 
#else  /* not O_CLOEXEC */
763
 
                    0
764
 
#endif  /* not O_CLOEXEC */
765
 
                    );
766
 
    } else {
767
 
      dir_fd = open(plugindir, O_RDONLY |
768
 
#ifdef O_CLOEXEC
769
 
                    O_CLOEXEC
770
 
#else  /* not O_CLOEXEC */
771
 
                    0
772
 
#endif  /* not O_CLOEXEC */
773
 
                    );
774
 
    }
 
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
                  );
775
851
    if(dir_fd == -1){
776
852
      error(0, errno, "Could not open plugin dir");
777
853
      exitstatus = EX_UNAVAILABLE;
783
859
    ret = set_cloexec_flag(dir_fd);
784
860
    if(ret < 0){
785
861
      error(0, errno, "set_cloexec_flag");
786
 
      TEMP_FAILURE_RETRY(close(dir_fd));
787
862
      exitstatus = EX_OSERR;
788
863
      goto fallback;
789
864
    }
790
865
#endif  /* O_CLOEXEC */
791
 
    
792
 
    dir = fdopendir(dir_fd);
793
 
    if(dir == NULL){
794
 
      error(0, errno, "Could not open plugin dir");
795
 
      TEMP_FAILURE_RETRY(close(dir_fd));
796
 
      exitstatus = EX_OSERR;
797
 
      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
      }
798
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;
799
900
  }
800
901
  
801
902
  FD_ZERO(&rfds_all);
802
903
  
803
904
  /* Read and execute any executable in the plugin directory*/
804
 
  while(true){
805
 
    do {
806
 
      dirst = readdir(dir);
807
 
    } while(dirst == NULL and errno == EINTR);
808
 
    
809
 
    /* All directory entries have been processed */
810
 
    if(dirst == NULL){
811
 
      if(errno == EBADF){
812
 
        error(0, errno, "readdir");
813
 
        exitstatus = EX_IOERR;
814
 
        goto fallback;
815
 
      }
816
 
      break;
817
 
    }
818
 
    
819
 
    d_name_len = strlen(dirst->d_name);
820
 
    
821
 
    /* Ignore dotfiles, backup files and other junk */
822
 
    {
823
 
      bool bad_name = false;
824
 
      
825
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
826
 
      
827
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
828
 
                                           ".dpkg-old",
829
 
                                           ".dpkg-bak",
830
 
                                           ".dpkg-divert", NULL };
831
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
832
 
        size_t pre_len = strlen(*pre);
833
 
        if((d_name_len >= pre_len)
834
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
835
 
          if(debug){
836
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
837
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
838
 
          }
839
 
          bad_name = true;
840
 
          break;
841
 
        }
842
 
      }
843
 
      if(bad_name){
844
 
        continue;
845
 
      }
846
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
847
 
        size_t suf_len = strlen(*suf);
848
 
        if((d_name_len >= suf_len)
849
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
850
 
                == 0)){
851
 
          if(debug){
852
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
853
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
854
 
          }
855
 
          bad_name = true;
856
 
          break;
857
 
        }
858
 
      }
859
 
      
860
 
      if(bad_name){
861
 
        continue;
862
 
      }
863
 
    }
864
 
    
865
 
    char *filename;
866
 
    if(plugindir == NULL){
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
868
 
                                             dirst->d_name));
869
 
    } else {
870
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
871
 
                                             plugindir,
872
 
                                             dirst->d_name));
873
 
    }
874
 
    if(ret < 0){
875
 
      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]);
876
911
      continue;
877
912
    }
878
 
    
879
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
913
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
880
914
    if(ret == -1){
881
915
      error(0, errno, "stat");
882
 
      free(filename);
 
916
      close(plugin_fd);
 
917
      free(direntries[i]);
883
918
      continue;
884
919
    }
885
920
    
886
921
    /* Ignore non-executable files */
887
922
    if(not S_ISREG(st.st_mode)
888
 
       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)){
889
925
      if(debug){
890
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
891
 
                " 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);
892
930
      }
893
 
      free(filename);
 
931
      close(plugin_fd);
 
932
      free(direntries[i]);
894
933
      continue;
895
934
    }
896
935
    
897
 
    plugin *p = getplugin(dirst->d_name);
 
936
    plugin *p = getplugin(direntries[i]->d_name);
898
937
    if(p == NULL){
899
938
      error(0, errno, "getplugin");
900
 
      free(filename);
 
939
      close(plugin_fd);
 
940
      free(direntries[i]);
901
941
      continue;
902
942
    }
903
943
    if(p->disabled){
904
944
      if(debug){
905
945
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
906
 
                dirst->d_name);
 
946
                direntries[i]->d_name);
907
947
      }
908
 
      free(filename);
 
948
      close(plugin_fd);
 
949
      free(direntries[i]);
909
950
      continue;
910
951
    }
911
952
    {
925
966
        }
926
967
      }
927
968
    }
928
 
    /* If this plugin has any environment variables, we will call
929
 
       using execve and need to duplicate the environment from this
930
 
       process, too. */
 
969
    /* If this plugin has any environment variables, we need to
 
970
       duplicate the environment from this process, too. */
931
971
    if(p->environ[0] != NULL){
932
972
      for(char **e = environ; *e != NULL; e++){
933
973
        if(not add_environment(p, *e, false)){
937
977
    }
938
978
    
939
979
    int pipefd[2];
 
980
#ifndef O_CLOEXEC
940
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 */
941
985
    if(ret == -1){
942
986
      error(0, errno, "pipe");
943
987
      exitstatus = EX_OSERR;
944
 
      goto fallback;
945
 
    }
 
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
946
1001
    /* Ask OS to automatic close the pipe on exec */
947
1002
    ret = set_cloexec_flag(pipefd[0]);
948
1003
    if(ret < 0){
949
1004
      error(0, errno, "set_cloexec_flag");
 
1005
      close(pipefd[0]);
 
1006
      close(pipefd[1]);
950
1007
      exitstatus = EX_OSERR;
 
1008
      free(direntries[i]);
951
1009
      goto fallback;
952
1010
    }
953
1011
    ret = set_cloexec_flag(pipefd[1]);
954
1012
    if(ret < 0){
955
1013
      error(0, errno, "set_cloexec_flag");
 
1014
      close(pipefd[0]);
 
1015
      close(pipefd[1]);
956
1016
      exitstatus = EX_OSERR;
 
1017
      free(direntries[i]);
957
1018
      goto fallback;
958
1019
    }
 
1020
#endif  /* not O_CLOEXEC */
959
1021
    /* Block SIGCHLD until process is safely in process list */
960
1022
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
961
1023
                                              &sigchld_action.sa_mask,
963
1025
    if(ret < 0){
964
1026
      error(0, errno, "sigprocmask");
965
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
966
1029
      goto fallback;
967
1030
    }
968
1031
    /* Starting a new process to be watched */
972
1035
    } while(pid == -1 and errno == EINTR);
973
1036
    if(pid == -1){
974
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]);
975
1042
      exitstatus = EX_OSERR;
 
1043
      free(direntries[i]);
976
1044
      goto fallback;
977
1045
    }
978
1046
    if(pid == 0){
994
1062
        _exit(EX_OSERR);
995
1063
      }
996
1064
      
997
 
      if(dirfd(dir) < 0){
998
 
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
999
 
           above and must now close it manually here. */
1000
 
        closedir(dir);
1001
 
      }
1002
 
      if(p->environ[0] == NULL){
1003
 
        if(execv(filename, p->argv) < 0){
1004
 
          error(0, errno, "execv for %s", filename);
1005
 
          _exit(EX_OSERR);
1006
 
        }
1007
 
      } else {
1008
 
        if(execve(filename, p->argv, p->environ) < 0){
1009
 
          error(0, errno, "execve for %s", filename);
1010
 
          _exit(EX_OSERR);
1011
 
        }
 
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);
1012
1071
      }
1013
1072
      /* no return */
1014
1073
    }
1015
1074
    /* Parent process */
1016
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1017
 
                                             pipe */
1018
 
    free(filename);
1019
 
    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);
1020
1078
    if(new_plugin == NULL){
1021
1079
      error(0, errno, "getplugin");
1022
1080
      ret = (int)(TEMP_FAILURE_RETRY
1026
1084
        error(0, errno, "sigprocmask");
1027
1085
      }
1028
1086
      exitstatus = EX_OSERR;
 
1087
      free(direntries[i]);
1029
1088
      goto fallback;
1030
1089
    }
 
1090
    free(direntries[i]);
1031
1091
    
1032
1092
    new_plugin->pid = pid;
1033
1093
    new_plugin->fd = pipefd[0];
1043
1103
      goto fallback;
1044
1104
    }
1045
1105
    
1046
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1047
 
                                          -Wconversion */
 
1106
    FD_SET(new_plugin->fd, &rfds_all);
1048
1107
    
1049
1108
    if(maxfd < new_plugin->fd){
1050
1109
      maxfd = new_plugin->fd;
1051
1110
    }
1052
1111
  }
1053
1112
  
1054
 
  TEMP_FAILURE_RETRY(closedir(dir));
1055
 
  dir = NULL;
 
1113
  free(direntries);
 
1114
  direntries = NULL;
 
1115
  close(dir_fd);
 
1116
  dir_fd = -1;
1056
1117
  free_plugin(getplugin(NULL));
1057
1118
  
1058
1119
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1097
1158
                      (intmax_t) (proc->pid),
1098
1159
                      WTERMSIG(proc->status),
1099
1160
                      strsignal(WTERMSIG(proc->status)));
1100
 
            } else if(WCOREDUMP(proc->status)){
1101
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1102
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
1103
1161
            }
1104
1162
          }
1105
1163
          
1106
1164
          /* Remove the plugin */
1107
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1108
 
                                          -Wconversion */
 
1165
          FD_CLR(proc->fd, &rfds_all);
1109
1166
          
1110
1167
          /* Block signal while modifying process_list */
1111
1168
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1151
1208
      }
1152
1209
      
1153
1210
      /* This process has not completed.  Does it have any output? */
1154
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1155
 
                                                         warning from
1156
 
                                                         -Wconversion */
 
1211
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1157
1212
        /* This process had nothing to say at this time */
1158
1213
        proc = proc->next;
1159
1214
        continue;
1160
1215
      }
1161
1216
      /* Before reading, make the process' data buffer large enough */
1162
1217
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1163
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1164
 
                               + (size_t) BUFFER_SIZE);
1165
 
        if(proc->buffer == NULL){
 
1218
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1219
                                   + (size_t) BUFFER_SIZE);
 
1220
        if(new_buffer == NULL){
1166
1221
          error(0, errno, "malloc");
1167
1222
          exitstatus = EX_OSERR;
1168
1223
          goto fallback;
1169
1224
        }
 
1225
        proc->buffer = new_buffer;
1170
1226
        proc->buffer_size += BUFFER_SIZE;
1171
1227
      }
1172
1228
      /* Read from the process */
1225
1281
    free(custom_argv);
1226
1282
  }
1227
1283
  
1228
 
  if(dir != NULL){
1229
 
    closedir(dir);
 
1284
  free(direntries);
 
1285
  
 
1286
  if(dir_fd != -1){
 
1287
    close(dir_fd);
1230
1288
  }
1231
1289
  
1232
1290
  /* Kill the processes */
1252
1310
  free_plugin_list();
1253
1311
  
1254
1312
  free(plugindir);
 
1313
  free(pluginhelperdir);
1255
1314
  free(argfile);
1256
1315
  
1257
1316
  return exitstatus;