/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2014-03-28 22:32:21 UTC
  • Revision ID: teddy@recompile.se-20140328223221-w579qlvlex5if3dn
Fix location and format of signing key file.

* debian/upstream/signing-key.asc: Added.
* debian/upstream-signing-key.pgp: Removed.

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2013 Teddy Hogeborn
 
6
 * Copyright © 2008-2013 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 */
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
 
32
                                   stderr, STDOUT_FILENO, fclose() */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), fstat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
41
                                   WEXITSTATUS(), WTERMSIG(),
42
42
                                   WCOREDUMP() */
43
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
43
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
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() */
 
47
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
48
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
49
                                   struct stat, fstat(), close(),
 
50
                                   setgid(), setuid(), S_ISREG(),
 
51
                                   faccessat() pipe(), fork(),
 
52
                                   _exit(), dup2(), fexecve(), read()
 
53
                                */
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
 
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
55
                                   FD_CLOEXEC, openat() */
 
56
#include <string.h>             /* strsep, strlen(), strsignal(),
 
57
                                   strcmp(), strncmp() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
72
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
73
#include <errno.h>              /* errno */
74
74
#include <error.h>              /* error() */
 
75
#include <fnmatch.h>            /* fnmatch() */
75
76
 
76
77
#define BUFFER_SIZE 256
77
78
 
79
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
81
 
81
82
const char *argp_program_version = "plugin-runner " VERSION;
82
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
83
const char *argp_program_bug_address = "<mandos@recompile.se>";
83
84
 
84
85
typedef struct plugin{
85
86
  char *name;                   /* can be NULL or any plugin name */
105
106
 
106
107
/* Gets an existing plugin based on name,
107
108
   or if none is found, creates a new one */
 
109
__attribute__((warn_unused_result))
108
110
static plugin *getplugin(char *name){
109
111
  /* Check for existing plugin with that name */
110
112
  for(plugin *p = plugin_list; p != NULL; p = p->next){
171
173
}
172
174
 
173
175
/* Helper function for add_argument and add_environment */
 
176
__attribute__((nonnull, warn_unused_result))
174
177
static bool add_to_char_array(const char *new, char ***array,
175
178
                              int *len){
176
179
  /* Resize the pointed-to array to hold one more pointer */
 
180
  char **new_array = NULL;
177
181
  do {
178
 
    *array = realloc(*array, sizeof(char *)
179
 
                     * (size_t) ((*len) + 2));
180
 
  } while(*array == NULL and errno == EINTR);
 
182
    new_array = realloc(*array, sizeof(char *)
 
183
                        * (size_t) ((*len) + 2));
 
184
  } while(new_array == NULL and errno == EINTR);
181
185
  /* Malloc check */
182
 
  if(*array == NULL){
 
186
  if(new_array == NULL){
183
187
    return false;
184
188
  }
 
189
  *array = new_array;
185
190
  /* Make a copy of the new string */
186
191
  char *copy;
187
192
  do {
199
204
}
200
205
 
201
206
/* Add to a plugin's argument vector */
 
207
__attribute__((nonnull(2), warn_unused_result))
202
208
static bool add_argument(plugin *p, const char *arg){
203
209
  if(p == NULL){
204
210
    return false;
207
213
}
208
214
 
209
215
/* Add to a plugin's environment */
 
216
__attribute__((nonnull(2), warn_unused_result))
210
217
static bool add_environment(plugin *p, const char *def, bool replace){
211
218
  if(p == NULL){
212
219
    return false;
214
221
  /* namelen = length of name of environment variable */
215
222
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
216
223
  /* Search for this environment variable */
217
 
  for(char **e = p->environ; *e != NULL; e++){
218
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
224
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
225
    if(strncmp(*envdef, def, namelen + 1) == 0){
219
226
      /* It already exists */
220
227
      if(replace){
221
 
        char *new;
 
228
        char *new_envdef;
222
229
        do {
223
 
          new = realloc(*e, strlen(def) + 1);
224
 
        } while(new == NULL and errno == EINTR);
225
 
        if(new == NULL){
 
230
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
231
        } while(new_envdef == NULL and errno == EINTR);
 
232
        if(new_envdef == NULL){
226
233
          return false;
227
234
        }
228
 
        *e = new;
229
 
        strcpy(*e, def);
 
235
        *envdef = new_envdef;
 
236
        strcpy(*envdef, def);
230
237
      }
231
238
      return true;
232
239
    }
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. */
286
294
}
287
295
 
288
296
/* Prints out a password to stdout */
 
297
__attribute__((nonnull, warn_unused_result))
289
298
static bool print_out_password(const char *buffer, size_t length){
290
299
  ssize_t ret;
291
300
  for(size_t written = 0; written < length; written += (size_t)ret){
299
308
}
300
309
 
301
310
/* Removes and free a plugin from the plugin list */
 
311
__attribute__((nonnull))
302
312
static void free_plugin(plugin *plugin_node){
303
313
  
304
314
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
338
348
  char *plugindir = NULL;
339
349
  char *argfile = NULL;
340
350
  FILE *conffp;
341
 
  size_t d_name_len;
342
351
  DIR *dir = NULL;
343
352
  struct dirent *dirst;
344
353
  struct stat st;
354
363
                                      .sa_flags = SA_NOCLDSTOP };
355
364
  char **custom_argv = NULL;
356
365
  int custom_argc = 0;
 
366
  int dir_fd;
357
367
  
358
368
  /* Establish a signal handler */
359
369
  sigemptyset(&sigchld_action.sa_mask);
416
426
    { .name = NULL }
417
427
  };
418
428
  
 
429
  __attribute__((nonnull(3)))
419
430
  error_t parse_opt(int key, char *arg, struct argp_state *state){
420
431
    errno = 0;
421
432
    switch(key){
422
433
      char *tmp;
423
 
      intmax_t tmpmax;
 
434
      intmax_t tmp_id;
424
435
    case 'g':                   /* --global-options */
425
436
      {
426
437
        char *plugin_option;
429
440
            break;
430
441
          }
431
442
        }
 
443
        errno = 0;
432
444
      }
433
445
      break;
434
446
    case 'G':                   /* --global-env */
435
 
      add_environment(getplugin(NULL), arg, true);
 
447
      if(add_environment(getplugin(NULL), arg, true)){
 
448
        errno = 0;
 
449
      }
436
450
      break;
437
451
    case 'o':                   /* --options-for */
438
452
      {
455
469
            break;
456
470
          }
457
471
        }
 
472
        errno = 0;
458
473
      }
459
474
      break;
460
475
    case 'E':                   /* --env-for */
472
487
          errno = EINVAL;
473
488
          break;
474
489
        }
475
 
        add_environment(getplugin(arg), envdef, true);
 
490
        if(add_environment(getplugin(arg), envdef, true)){
 
491
          errno = 0;
 
492
        }
476
493
      }
477
494
      break;
478
495
    case 'd':                   /* --disable */
480
497
        plugin *p = getplugin(arg);
481
498
        if(p != NULL){
482
499
          p->disabled = true;
 
500
          errno = 0;
483
501
        }
484
502
      }
485
503
      break;
488
506
        plugin *p = getplugin(arg);
489
507
        if(p != NULL){
490
508
          p->disabled = false;
 
509
          errno = 0;
491
510
        }
492
511
      }
493
512
      break;
494
513
    case 128:                   /* --plugin-dir */
495
514
      free(plugindir);
496
515
      plugindir = strdup(arg);
 
516
      if(plugindir != NULL){
 
517
        errno = 0;
 
518
      }
497
519
      break;
498
520
    case 129:                   /* --config-file */
499
521
      /* This is already done by parse_opt_config_file() */
500
522
      break;
501
523
    case 130:                   /* --userid */
502
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
524
      tmp_id = strtoimax(arg, &tmp, 10);
503
525
      if(errno != 0 or tmp == arg or *tmp != '\0'
504
 
         or tmpmax != (uid_t)tmpmax){
 
526
         or tmp_id != (uid_t)tmp_id){
505
527
        argp_error(state, "Bad user ID number: \"%s\", using %"
506
528
                   PRIdMAX, arg, (intmax_t)uid);
507
529
        break;
508
530
      }
509
 
      uid = (uid_t)tmpmax;
 
531
      uid = (uid_t)tmp_id;
 
532
      errno = 0;
510
533
      break;
511
534
    case 131:                   /* --groupid */
512
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
535
      tmp_id = strtoimax(arg, &tmp, 10);
513
536
      if(errno != 0 or tmp == arg or *tmp != '\0'
514
 
         or tmpmax != (gid_t)tmpmax){
 
537
         or tmp_id != (gid_t)tmp_id){
515
538
        argp_error(state, "Bad group ID number: \"%s\", using %"
516
539
                   PRIdMAX, arg, (intmax_t)gid);
517
540
        break;
518
541
      }
519
 
      gid = (gid_t)tmpmax;
 
542
      gid = (gid_t)tmp_id;
 
543
      errno = 0;
520
544
      break;
521
545
    case 132:                   /* --debug */
522
546
      debug = true;
570
594
    case 129:                   /* --config-file */
571
595
      free(argfile);
572
596
      argfile = strdup(arg);
 
597
      if(argfile != NULL){
 
598
        errno = 0;
 
599
      }
573
600
      break;
574
601
    case 130:                   /* --userid */
575
602
    case 131:                   /* --groupid */
658
685
        }
659
686
        
660
687
        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;
 
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
          }
668
701
        }
669
702
        custom_argv[custom_argc-1] = new_arg;
670
703
        custom_argv[custom_argc] = NULL;
742
775
    }
743
776
  }
744
777
  
745
 
  /* Strip permissions down to nobody */
746
 
  setgid(gid);
 
778
  if(getuid() == 0){
 
779
    /* Work around Debian bug #633582:
 
780
       <http://bugs.debian.org/633582> */
 
781
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
782
    if(plugindir_fd == -1){
 
783
      if(errno != ENOENT){
 
784
        error(0, errno, "open(\"" PDIR "\")");
 
785
      }
 
786
    } else {
 
787
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
788
      if(ret == -1){
 
789
        error(0, errno, "fstat");
 
790
      } else {
 
791
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
792
          ret = fchown(plugindir_fd, uid, gid);
 
793
          if(ret == -1){
 
794
            error(0, errno, "fchown");
 
795
          }
 
796
        }
 
797
      }
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
799
    }
 
800
  }
 
801
  
 
802
  /* Lower permissions */
 
803
  ret = setgid(gid);
747
804
  if(ret == -1){
748
805
    error(0, errno, "setgid");
749
806
  }
754
811
  
755
812
  /* Open plugin directory with close_on_exec flag */
756
813
  {
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
 
    }
 
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
                  );
775
821
    if(dir_fd == -1){
776
822
      error(0, errno, "Could not open plugin dir");
777
823
      exitstatus = EX_UNAVAILABLE;
816
862
      break;
817
863
    }
818
864
    
819
 
    d_name_len = strlen(dirst->d_name);
820
 
    
821
865
    /* Ignore dotfiles, backup files and other junk */
822
866
    {
823
867
      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
 
      
 
868
      const char * const patterns[] = { ".*", "#*#", "*~",
 
869
                                        "*.dpkg-new", "*.dpkg-old",
 
870
                                        "*.dpkg-bak", "*.dpkg-divert",
 
871
                                        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, dirst->d_name,
 
882
                   FNM_FILE_NAME | FNM_PERIOD) != FNM_NOMATCH){
 
883
          if(debug){
 
884
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
 
885
                    " matching pattern %s\n", dirst->d_name, *pat);
 
886
          }
 
887
          bad_name = true;
 
888
          break;
 
889
        }
 
890
      }
860
891
      if(bad_name){
861
892
        continue;
862
893
      }
863
894
    }
864
895
    
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));
 
896
    int plugin_fd = openat(dir_fd, dirst->d_name, O_RDONLY |
 
897
#ifdef O_CLOEXEC
 
898
                            O_CLOEXEC
 
899
#else  /* not O_CLOEXEC */
 
900
                            0
 
901
#endif  /* not O_CLOEXEC */
 
902
                            );
 
903
    if(plugin_fd == -1){
 
904
      error(0, errno, "Could not open plugin");
 
905
      continue;
873
906
    }
 
907
#ifndef O_CLOEXEC
 
908
  /* Set the FD_CLOEXEC flag on the plugin FD */
 
909
    ret = set_cloexec_flag(plugin_fd);
874
910
    if(ret < 0){
875
 
      error(0, errno, "asprintf");
 
911
      error(0, errno, "set_cloexec_flag");
 
912
      TEMP_FAILURE_RETRY(close(plugin_fd));
876
913
      continue;
877
914
    }
878
 
    
879
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
915
#endif  /* O_CLOEXEC */
 
916
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
880
917
    if(ret == -1){
881
918
      error(0, errno, "stat");
882
 
      free(filename);
 
919
      TEMP_FAILURE_RETRY(close(plugin_fd));
883
920
      continue;
884
921
    }
885
922
    
886
923
    /* Ignore non-executable files */
887
924
    if(not S_ISREG(st.st_mode)
888
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
925
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
 
926
                                        0)) != 0)){
889
927
      if(debug){
890
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
891
 
                " with bad type or mode\n", filename);
 
928
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
 
929
                " with bad type or mode\n",
 
930
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
892
931
      }
893
 
      free(filename);
 
932
      TEMP_FAILURE_RETRY(close(plugin_fd));
894
933
      continue;
895
934
    }
896
935
    
897
936
    plugin *p = getplugin(dirst->d_name);
898
937
    if(p == NULL){
899
938
      error(0, errno, "getplugin");
900
 
      free(filename);
 
939
      TEMP_FAILURE_RETRY(close(plugin_fd));
901
940
      continue;
902
941
    }
903
942
    if(p->disabled){
905
944
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
906
945
                dirst->d_name);
907
946
      }
908
 
      free(filename);
 
947
      TEMP_FAILURE_RETRY(close(plugin_fd));
909
948
      continue;
910
949
    }
911
950
    {
925
964
        }
926
965
      }
927
966
    }
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. */
 
967
    /* If this plugin has any environment variables, we need to
 
968
       duplicate the environment from this process, too. */
931
969
    if(p->environ[0] != NULL){
932
970
      for(char **e = environ; *e != NULL; e++){
933
971
        if(not add_environment(p, *e, false)){
999
1037
           above and must now close it manually here. */
1000
1038
        closedir(dir);
1001
1039
      }
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
 
        }
 
1040
      if(fexecve(plugin_fd, p->argv,
 
1041
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
 
1042
        error(0, errno, "fexecve for %s/%s",
 
1043
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
 
1044
        _exit(EX_OSERR);
1012
1045
      }
1013
1046
      /* no return */
1014
1047
    }
1015
1048
    /* Parent process */
1016
1049
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1017
1050
                                             pipe */
1018
 
    free(filename);
 
1051
    TEMP_FAILURE_RETRY(close(plugin_fd));
1019
1052
    plugin *new_plugin = getplugin(dirst->d_name);
1020
1053
    if(new_plugin == NULL){
1021
1054
      error(0, errno, "getplugin");
1043
1076
      goto fallback;
1044
1077
    }
1045
1078
    
 
1079
#if defined (__GNUC__) and defined (__GLIBC__)
 
1080
#if not __GLIBC_PREREQ(2, 16)
 
1081
#pragma GCC diagnostic push
 
1082
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1083
#endif
 
1084
#endif
1046
1085
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1047
 
                                          -Wconversion */
 
1086
                                          -Wconversion in GNU libc
 
1087
                                          before 2.16 */
 
1088
#if defined (__GNUC__) and defined (__GLIBC__)
 
1089
#if not __GLIBC_PREREQ(2, 16)
 
1090
#pragma GCC diagnostic pop
 
1091
#endif
 
1092
#endif
1048
1093
    
1049
1094
    if(maxfd < new_plugin->fd){
1050
1095
      maxfd = new_plugin->fd;
1104
1149
          }
1105
1150
          
1106
1151
          /* Remove the plugin */
 
1152
#if defined (__GNUC__) and defined (__GLIBC__)
 
1153
#if not __GLIBC_PREREQ(2, 16)
 
1154
#pragma GCC diagnostic push
 
1155
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1156
#endif
 
1157
#endif
1107
1158
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1108
 
                                          -Wconversion */
 
1159
                                          -Wconversion in GNU libc
 
1160
                                          before 2.16 */
 
1161
#if defined (__GNUC__) and defined (__GLIBC__)
 
1162
#if not __GLIBC_PREREQ(2, 16)
 
1163
#pragma GCC diagnostic pop
 
1164
#endif
 
1165
#endif
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? */
 
1211
#if defined (__GNUC__) and defined (__GLIBC__)
 
1212
#if not __GLIBC_PREREQ(2, 16)
 
1213
#pragma GCC diagnostic push
 
1214
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1215
#endif
 
1216
#endif
1154
1217
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1155
1218
                                                         warning from
1156
 
                                                         -Wconversion */
 
1219
                                                         -Wconversion
 
1220
                                                         in GNU libc
 
1221
                                                         before
 
1222
                                                         2.16 */
 
1223
#if defined (__GNUC__) and defined (__GLIBC__)
 
1224
#if not __GLIBC_PREREQ(2, 16)
 
1225
#pragma GCC diagnostic pop
 
1226
#endif
 
1227
#endif
1157
1228
        /* This process had nothing to say at this time */
1158
1229
        proc = proc->next;
1159
1230
        continue;
1160
1231
      }
1161
1232
      /* Before reading, make the process' data buffer large enough */
1162
1233
      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){
 
1234
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1235
                                   + (size_t) BUFFER_SIZE);
 
1236
        if(new_buffer == NULL){
1166
1237
          error(0, errno, "malloc");
1167
1238
          exitstatus = EX_OSERR;
1168
1239
          goto fallback;
1169
1240
        }
 
1241
        proc->buffer = new_buffer;
1170
1242
        proc->buffer_size += BUFFER_SIZE;
1171
1243
      }
1172
1244
      /* Read from the process */