/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: Björn Påhlsson
  • Date: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
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
41
                                   WEXITSTATUS(), WTERMSIG(),
41
42
                                   WCOREDUMP() */
42
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
 
#include <dirent.h>             /* struct dirent, scandirat() */
45
 
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
46
 
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
47
 
                                   struct stat, fstat(), close(),
48
 
                                   setgid(), setuid(), S_ISREG(),
49
 
                                   faccessat() pipe2(), fork(),
50
 
                                   _exit(), dup2(), fexecve(), read()
51
 
                                */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
 
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
 
48
                                   fcntl(), setuid(), setgid(),
 
49
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
50
                                   access(), pipe(), fork(), close()
 
51
                                   dup2(), STDOUT_FILENO, _exit(),
 
52
                                   execv(), write(), read(),
 
53
                                   close() */
52
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC, openat(), scandirat(),
54
 
                                   pipe2() */
55
 
#include <string.h>             /* strsep, strlen(), strsignal(),
56
 
                                   strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal(), strcmp(), strncmp() */
57
58
#include <errno.h>              /* errno */
58
59
#include <argp.h>               /* struct argp_option, struct
59
60
                                   argp_state, struct argp,
71
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
72
73
#include <errno.h>              /* errno */
73
74
#include <error.h>              /* error() */
74
 
#include <fnmatch.h>            /* fnmatch() */
75
75
 
76
76
#define BUFFER_SIZE 256
77
77
 
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@recompile.se>";
 
82
const char *argp_program_bug_address = "<mandos@fukt.bsnet.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))
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++){
349
338
  char *plugindir = NULL;
350
339
  char *argfile = NULL;
351
340
  FILE *conffp;
352
 
  struct dirent **direntries = NULL;
 
341
  size_t d_name_len;
 
342
  DIR *dir = NULL;
 
343
  struct dirent *dirst;
353
344
  struct stat st;
354
345
  fd_set rfds_all;
355
346
  int ret, maxfd = 0;
363
354
                                      .sa_flags = SA_NOCLDSTOP };
364
355
  char **custom_argv = NULL;
365
356
  int custom_argc = 0;
366
 
  int dir_fd = -1;
367
357
  
368
358
  /* Establish a signal handler */
369
359
  sigemptyset(&sigchld_action.sa_mask);
426
416
    { .name = NULL }
427
417
  };
428
418
  
429
 
  __attribute__((nonnull(3)))
430
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
431
420
    errno = 0;
432
421
    switch(key){
433
422
      char *tmp;
434
 
      intmax_t tmp_id;
 
423
      intmax_t tmpmax;
435
424
    case 'g':                   /* --global-options */
436
425
      {
437
426
        char *plugin_option;
440
429
            break;
441
430
          }
442
431
        }
443
 
        errno = 0;
444
432
      }
445
433
      break;
446
434
    case 'G':                   /* --global-env */
447
 
      if(add_environment(getplugin(NULL), arg, true)){
448
 
        errno = 0;
449
 
      }
 
435
      add_environment(getplugin(NULL), arg, true);
450
436
      break;
451
437
    case 'o':                   /* --options-for */
452
438
      {
469
455
            break;
470
456
          }
471
457
        }
472
 
        errno = 0;
473
458
      }
474
459
      break;
475
460
    case 'E':                   /* --env-for */
487
472
          errno = EINVAL;
488
473
          break;
489
474
        }
490
 
        if(add_environment(getplugin(arg), envdef, true)){
491
 
          errno = 0;
492
 
        }
 
475
        add_environment(getplugin(arg), envdef, true);
493
476
      }
494
477
      break;
495
478
    case 'd':                   /* --disable */
497
480
        plugin *p = getplugin(arg);
498
481
        if(p != NULL){
499
482
          p->disabled = true;
500
 
          errno = 0;
501
483
        }
502
484
      }
503
485
      break;
506
488
        plugin *p = getplugin(arg);
507
489
        if(p != NULL){
508
490
          p->disabled = false;
509
 
          errno = 0;
510
491
        }
511
492
      }
512
493
      break;
513
494
    case 128:                   /* --plugin-dir */
514
495
      free(plugindir);
515
496
      plugindir = strdup(arg);
516
 
      if(plugindir != NULL){
517
 
        errno = 0;
518
 
      }
519
497
      break;
520
498
    case 129:                   /* --config-file */
521
499
      /* This is already done by parse_opt_config_file() */
522
500
      break;
523
501
    case 130:                   /* --userid */
524
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
502
      tmpmax = strtoimax(arg, &tmp, 10);
525
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
526
 
         or tmp_id != (uid_t)tmp_id){
 
504
         or tmpmax != (uid_t)tmpmax){
527
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
528
506
                   PRIdMAX, arg, (intmax_t)uid);
529
507
        break;
530
508
      }
531
 
      uid = (uid_t)tmp_id;
532
 
      errno = 0;
 
509
      uid = (uid_t)tmpmax;
533
510
      break;
534
511
    case 131:                   /* --groupid */
535
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
512
      tmpmax = strtoimax(arg, &tmp, 10);
536
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
537
 
         or tmp_id != (gid_t)tmp_id){
 
514
         or tmpmax != (gid_t)tmpmax){
538
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
539
516
                   PRIdMAX, arg, (intmax_t)gid);
540
517
        break;
541
518
      }
542
 
      gid = (gid_t)tmp_id;
543
 
      errno = 0;
 
519
      gid = (gid_t)tmpmax;
544
520
      break;
545
521
    case 132:                   /* --debug */
546
522
      debug = true;
594
570
    case 129:                   /* --config-file */
595
571
      free(argfile);
596
572
      argfile = strdup(arg);
597
 
      if(argfile != NULL){
598
 
        errno = 0;
599
 
      }
600
573
      break;
601
574
    case 130:                   /* --userid */
602
575
    case 131:                   /* --groupid */
685
658
        }
686
659
        
687
660
        custom_argc += 1;
688
 
        {
689
 
          char **new_argv = realloc(custom_argv, sizeof(char *)
690
 
                                    * ((unsigned int)
691
 
                                       custom_argc + 1));
692
 
          if(new_argv == NULL){
693
 
            error(0, errno, "realloc");
694
 
            exitstatus = EX_OSERR;
695
 
            free(new_arg);
696
 
            free(org_line);
697
 
            goto fallback;
698
 
          } else {
699
 
            custom_argv = new_argv;
700
 
          }
 
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
668
        }
702
669
        custom_argv[custom_argc-1] = new_arg;
703
670
        custom_argv[custom_argc] = NULL;
775
742
    }
776
743
  }
777
744
  
778
 
  if(getuid() == 0){
 
745
  {
779
746
    /* Work around Debian bug #633582:
780
747
       <http://bugs.debian.org/633582> */
781
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
782
749
    if(plugindir_fd == -1){
783
 
      if(errno != ENOENT){
784
 
        error(0, errno, "open(\"" PDIR "\")");
785
 
      }
 
750
      error(0, errno, "open");
786
751
    } else {
787
752
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
788
753
      if(ret == -1){
800
765
  }
801
766
  
802
767
  /* Lower permissions */
803
 
  ret = setgid(gid);
 
768
  setgid(gid);
804
769
  if(ret == -1){
805
770
    error(0, errno, "setgid");
806
771
  }
811
776
  
812
777
  /* Open plugin directory with close_on_exec flag */
813
778
  {
814
 
    dir_fd = open(plugindir != NULL ? plugindir : PDIR, O_RDONLY |
815
 
#ifdef O_CLOEXEC
816
 
                  O_CLOEXEC
817
 
#else  /* not O_CLOEXEC */
818
 
                  0
819
 
#endif  /* not O_CLOEXEC */
820
 
                  );
 
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
    }
821
797
    if(dir_fd == -1){
822
798
      error(0, errno, "Could not open plugin dir");
823
799
      exitstatus = EX_UNAVAILABLE;
829
805
    ret = set_cloexec_flag(dir_fd);
830
806
    if(ret < 0){
831
807
      error(0, errno, "set_cloexec_flag");
 
808
      TEMP_FAILURE_RETRY(close(dir_fd));
832
809
      exitstatus = EX_OSERR;
833
810
      goto fallback;
834
811
    }
835
812
#endif  /* O_CLOEXEC */
836
 
  }
837
 
  
838
 
  int good_name(const struct dirent * const dirent){
839
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
840
 
                                      "*.dpkg-old", "*.dpkg-bak",
841
 
                                      "*.dpkg-divert", NULL };
842
 
#ifdef __GNUC__
843
 
#pragma GCC diagnostic push
844
 
#pragma GCC diagnostic ignored "-Wcast-qual"
845
 
#endif
846
 
    for(const char **pat = (const char **)patterns;
847
 
        *pat != NULL; pat++){
848
 
#ifdef __GNUC__
849
 
#pragma GCC diagnostic pop
850
 
#endif
851
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
852
 
         != FNM_NOMATCH){
853
 
        if(debug){
854
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
855
 
                    " matching pattern %s\n", dirent->d_name, *pat);
856
 
        }
857
 
        return 0;
858
 
      }
 
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;
859
820
    }
860
 
    return 1;
861
 
  }
862
 
  
863
 
#ifdef __GLIBC__
864
 
#if __GLIBC_PREREQ(2, 15)
865
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
866
 
                             alphasort);
867
 
#else  /* not __GLIBC_PREREQ(2, 15) */
868
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
869
 
                           &direntries, good_name, alphasort);
870
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
871
 
#else   /* not __GLIBC__ */
872
 
  int numplugins = scandir(plugindir != NULL ? plugindir : PDIR,
873
 
                           &direntries, good_name, alphasort);
874
 
#endif  /* not __GLIBC__ */
875
 
  if(numplugins == -1){
876
 
    error(0, errno, "Could not scan plugin dir");
877
 
    direntries = NULL;
878
 
    exitstatus = EX_OSERR;
879
 
    goto fallback;
880
821
  }
881
822
  
882
823
  FD_ZERO(&rfds_all);
883
824
  
884
825
  /* Read and execute any executable in the plugin directory*/
885
 
  for(int i = 0; i < numplugins; i++){
886
 
    
887
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
888
 
    if(plugin_fd == -1){
889
 
      error(0, errno, "Could not open plugin");
890
 
      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");
891
898
      continue;
892
899
    }
893
 
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
 
900
    
 
901
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
894
902
    if(ret == -1){
895
903
      error(0, errno, "stat");
896
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
897
 
      free(direntries[i]);
 
904
      free(filename);
898
905
      continue;
899
906
    }
900
907
    
901
908
    /* Ignore non-executable files */
902
909
    if(not S_ISREG(st.st_mode)
903
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
904
 
                                        X_OK, 0)) != 0)){
 
910
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
905
911
      if(debug){
906
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
907
 
                " with bad type or mode\n",
908
 
                plugindir != NULL ? plugindir : PDIR,
909
 
                direntries[i]->d_name);
 
912
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
913
                " with bad type or mode\n", filename);
910
914
      }
911
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
912
 
      free(direntries[i]);
 
915
      free(filename);
913
916
      continue;
914
917
    }
915
918
    
916
 
    plugin *p = getplugin(direntries[i]->d_name);
 
919
    plugin *p = getplugin(dirst->d_name);
917
920
    if(p == NULL){
918
921
      error(0, errno, "getplugin");
919
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
920
 
      free(direntries[i]);
 
922
      free(filename);
921
923
      continue;
922
924
    }
923
925
    if(p->disabled){
924
926
      if(debug){
925
927
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
926
 
                direntries[i]->d_name);
 
928
                dirst->d_name);
927
929
      }
928
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
929
 
      free(direntries[i]);
 
930
      free(filename);
930
931
      continue;
931
932
    }
932
933
    {
946
947
        }
947
948
      }
948
949
    }
949
 
    /* If this plugin has any environment variables, we need to
950
 
       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. */
951
953
    if(p->environ[0] != NULL){
952
954
      for(char **e = environ; *e != NULL; e++){
953
955
        if(not add_environment(p, *e, false)){
957
959
    }
958
960
    
959
961
    int pipefd[2];
960
 
#ifndef O_CLOEXEC
961
962
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
962
 
#else  /* O_CLOEXEC */
963
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
964
 
#endif  /* O_CLOEXEC */
965
963
    if(ret == -1){
966
964
      error(0, errno, "pipe");
967
965
      exitstatus = EX_OSERR;
968
 
      free(direntries[i]);
969
 
      goto fallback;
970
 
    }
971
 
    if(pipefd[0] >= FD_SETSIZE){
972
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
973
 
              FD_SETSIZE);
974
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
975
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
976
 
      exitstatus = EX_OSERR;
977
 
      free(direntries[i]);
978
 
      goto fallback;
979
 
    }
980
 
#ifndef O_CLOEXEC
 
966
      goto fallback;
 
967
    }
981
968
    /* Ask OS to automatic close the pipe on exec */
982
969
    ret = set_cloexec_flag(pipefd[0]);
983
970
    if(ret < 0){
984
971
      error(0, errno, "set_cloexec_flag");
985
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
986
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
987
972
      exitstatus = EX_OSERR;
988
 
      free(direntries[i]);
989
973
      goto fallback;
990
974
    }
991
975
    ret = set_cloexec_flag(pipefd[1]);
992
976
    if(ret < 0){
993
977
      error(0, errno, "set_cloexec_flag");
994
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
995
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
996
978
      exitstatus = EX_OSERR;
997
 
      free(direntries[i]);
998
979
      goto fallback;
999
980
    }
1000
 
#endif  /* not O_CLOEXEC */
1001
981
    /* Block SIGCHLD until process is safely in process list */
1002
982
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1003
983
                                              &sigchld_action.sa_mask,
1005
985
    if(ret < 0){
1006
986
      error(0, errno, "sigprocmask");
1007
987
      exitstatus = EX_OSERR;
1008
 
      free(direntries[i]);
1009
988
      goto fallback;
1010
989
    }
1011
990
    /* Starting a new process to be watched */
1015
994
    } while(pid == -1 and errno == EINTR);
1016
995
    if(pid == -1){
1017
996
      error(0, errno, "fork");
1018
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1019
 
                                     &sigchld_action.sa_mask, NULL));
1020
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1021
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1022
997
      exitstatus = EX_OSERR;
1023
 
      free(direntries[i]);
1024
998
      goto fallback;
1025
999
    }
1026
1000
    if(pid == 0){
1042
1016
        _exit(EX_OSERR);
1043
1017
      }
1044
1018
      
1045
 
      if(fexecve(plugin_fd, p->argv,
1046
 
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1047
 
        error(0, errno, "fexecve for %s/%s",
1048
 
              plugindir != NULL ? plugindir : PDIR,
1049
 
              direntries[i]->d_name);
1050
 
        _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
        }
1051
1034
      }
1052
1035
      /* no return */
1053
1036
    }
1054
1037
    /* Parent process */
1055
1038
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1056
1039
                                             pipe */
1057
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
1058
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1040
    free(filename);
 
1041
    plugin *new_plugin = getplugin(dirst->d_name);
1059
1042
    if(new_plugin == NULL){
1060
1043
      error(0, errno, "getplugin");
1061
1044
      ret = (int)(TEMP_FAILURE_RETRY
1065
1048
        error(0, errno, "sigprocmask");
1066
1049
      }
1067
1050
      exitstatus = EX_OSERR;
1068
 
      free(direntries[i]);
1069
1051
      goto fallback;
1070
1052
    }
1071
 
    free(direntries[i]);
1072
1053
    
1073
1054
    new_plugin->pid = pid;
1074
1055
    new_plugin->fd = pipefd[0];
1084
1065
      goto fallback;
1085
1066
    }
1086
1067
    
1087
 
#if defined (__GNUC__) and defined (__GLIBC__)
1088
 
#if not __GLIBC_PREREQ(2, 16)
1089
 
#pragma GCC diagnostic push
1090
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1091
 
#endif
1092
 
#endif
1093
1068
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1094
 
                                          -Wconversion in GNU libc
1095
 
                                          before 2.16 */
1096
 
#if defined (__GNUC__) and defined (__GLIBC__)
1097
 
#if not __GLIBC_PREREQ(2, 16)
1098
 
#pragma GCC diagnostic pop
1099
 
#endif
1100
 
#endif
 
1069
                                          -Wconversion */
1101
1070
    
1102
1071
    if(maxfd < new_plugin->fd){
1103
1072
      maxfd = new_plugin->fd;
1104
1073
    }
1105
1074
  }
1106
1075
  
1107
 
  free(direntries);
1108
 
  direntries = NULL;
1109
 
  TEMP_FAILURE_RETRY(close(dir_fd));
1110
 
  dir_fd = -1;
 
1076
  TEMP_FAILURE_RETRY(closedir(dir));
 
1077
  dir = NULL;
1111
1078
  free_plugin(getplugin(NULL));
1112
1079
  
1113
1080
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1159
1126
          }
1160
1127
          
1161
1128
          /* Remove the plugin */
1162
 
#if defined (__GNUC__) and defined (__GLIBC__)
1163
 
#if not __GLIBC_PREREQ(2, 16)
1164
 
#pragma GCC diagnostic push
1165
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1166
 
#endif
1167
 
#endif
1168
1129
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1169
 
                                          -Wconversion in GNU libc
1170
 
                                          before 2.16 */
1171
 
#if defined (__GNUC__) and defined (__GLIBC__)
1172
 
#if not __GLIBC_PREREQ(2, 16)
1173
 
#pragma GCC diagnostic pop
1174
 
#endif
1175
 
#endif
 
1130
                                          -Wconversion */
1176
1131
          
1177
1132
          /* Block signal while modifying process_list */
1178
1133
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1218
1173
      }
1219
1174
      
1220
1175
      /* This process has not completed.  Does it have any output? */
1221
 
#if defined (__GNUC__) and defined (__GLIBC__)
1222
 
#if not __GLIBC_PREREQ(2, 16)
1223
 
#pragma GCC diagnostic push
1224
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1225
 
#endif
1226
 
#endif
1227
1176
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1228
1177
                                                         warning from
1229
 
                                                         -Wconversion
1230
 
                                                         in GNU libc
1231
 
                                                         before
1232
 
                                                         2.16 */
1233
 
#if defined (__GNUC__) and defined (__GLIBC__)
1234
 
#if not __GLIBC_PREREQ(2, 16)
1235
 
#pragma GCC diagnostic pop
1236
 
#endif
1237
 
#endif
 
1178
                                                         -Wconversion */
1238
1179
        /* This process had nothing to say at this time */
1239
1180
        proc = proc->next;
1240
1181
        continue;
1241
1182
      }
1242
1183
      /* Before reading, make the process' data buffer large enough */
1243
1184
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1244
 
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
1245
 
                                   + (size_t) BUFFER_SIZE);
1246
 
        if(new_buffer == NULL){
 
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
1186
                               + (size_t) BUFFER_SIZE);
 
1187
        if(proc->buffer == NULL){
1247
1188
          error(0, errno, "malloc");
1248
1189
          exitstatus = EX_OSERR;
1249
1190
          goto fallback;
1250
1191
        }
1251
 
        proc->buffer = new_buffer;
1252
1192
        proc->buffer_size += BUFFER_SIZE;
1253
1193
      }
1254
1194
      /* Read from the process */
1307
1247
    free(custom_argv);
1308
1248
  }
1309
1249
  
1310
 
  free(direntries);
1311
 
  
1312
 
  if(dir_fd != -1){
1313
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1250
  if(dir != NULL){
 
1251
    closedir(dir);
1314
1252
  }
1315
1253
  
1316
1254
  /* Kill the processes */