/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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