/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
29
                                   EXIT_SUCCESS, realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
 
41
                                   WEXITSTATUS() */
43
42
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
44
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
45
                                   readdir(), closedir(), dirfd() */
47
46
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
47
                                   fcntl(), setuid(), setgid(),
53
52
                                   close() */
54
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
54
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
55
#include <string.h>             /* strsep, strlen(), asprintf() */
58
56
#include <errno.h>              /* errno */
59
57
#include <argp.h>               /* struct argp_option, struct
60
58
                                   argp_state, struct argp,
64
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
65
63
                                   sigaddset(), sigaction(),
66
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
67
 
                                   SIG_UNBLOCK, kill(), sig_atomic_t
68
 
                                */
 
65
                                   SIG_UNBLOCK, kill() */
69
66
#include <errno.h>              /* errno, EBADF */
70
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
72
68
 
73
69
#define BUFFER_SIZE 256
74
70
 
85
81
  char **environ;
86
82
  int envc;
87
83
  bool disabled;
88
 
  
 
84
 
89
85
  /* Variables used for running processes*/
90
86
  pid_t pid;
91
87
  int fd;
103
99
/* Gets an existing plugin based on name,
104
100
   or if none is found, creates a new one */
105
101
static plugin *getplugin(char *name){
106
 
  /* Check for existing plugin with that name */
 
102
  /* Check for exiting plugin with that name */
107
103
  for(plugin *p = plugin_list; p != NULL; p = p->next){
108
104
    if((p->name == name)
109
105
       or (p->name and name and (strcmp(p->name, name) == 0))){
111
107
    }
112
108
  }
113
109
  /* Create a new plugin */
114
 
  plugin *new_plugin = NULL;
115
 
  do {
116
 
    new_plugin = malloc(sizeof(plugin));
117
 
  } while(new_plugin == NULL and errno == EINTR);
 
110
  plugin *new_plugin = malloc(sizeof(plugin));
118
111
  if(new_plugin == NULL){
119
112
    return NULL;
120
113
  }
121
114
  char *copy_name = NULL;
122
115
  if(name != NULL){
123
 
    do {
124
 
      copy_name = strdup(name);
125
 
    } while(copy_name == NULL and errno == EINTR);
 
116
    copy_name = strdup(name);
126
117
    if(copy_name == NULL){
127
 
      int e = errno;
128
118
      free(new_plugin);
129
 
      errno = e;
130
119
      return NULL;
131
120
    }
132
121
  }
136
125
                          .disabled = false,
137
126
                          .next = plugin_list };
138
127
  
139
 
  do {
140
 
    new_plugin->argv = malloc(sizeof(char *) * 2);
141
 
  } while(new_plugin->argv == NULL and errno == EINTR);
 
128
  new_plugin->argv = malloc(sizeof(char *) * 2);
142
129
  if(new_plugin->argv == NULL){
143
 
    int e = errno;
144
130
    free(copy_name);
145
131
    free(new_plugin);
146
 
    errno = e;
147
132
    return NULL;
148
133
  }
149
134
  new_plugin->argv[0] = copy_name;
150
135
  new_plugin->argv[1] = NULL;
151
136
  
152
 
  do {
153
 
    new_plugin->environ = malloc(sizeof(char *));
154
 
  } while(new_plugin->environ == NULL and errno == EINTR);
 
137
  new_plugin->environ = malloc(sizeof(char *));
155
138
  if(new_plugin->environ == NULL){
156
 
    int e = errno;
157
139
    free(copy_name);
158
140
    free(new_plugin->argv);
159
141
    free(new_plugin);
160
 
    errno = e;
161
142
    return NULL;
162
143
  }
163
144
  new_plugin->environ[0] = NULL;
171
152
static bool add_to_char_array(const char *new, char ***array,
172
153
                              int *len){
173
154
  /* Resize the pointed-to array to hold one more pointer */
174
 
  do {
175
 
    *array = realloc(*array, sizeof(char *)
176
 
                     * (size_t) ((*len) + 2));
177
 
  } while(*array == NULL and errno == EINTR);
 
155
  *array = realloc(*array, sizeof(char *)
 
156
                   * (size_t) ((*len) + 2));
178
157
  /* Malloc check */
179
158
  if(*array == NULL){
180
159
    return false;
181
160
  }
182
161
  /* Make a copy of the new string */
183
 
  char *copy;
184
 
  do {
185
 
    copy = strdup(new);
186
 
  } while(copy == NULL and errno == EINTR);
 
162
  char *copy = strdup(new);
187
163
  if(copy == NULL){
188
164
    return false;
189
165
  }
215
191
    if(strncmp(*e, def, namelen + 1) == 0){
216
192
      /* It already exists */
217
193
      if(replace){
218
 
        char *new;
219
 
        do {
220
 
          new = realloc(*e, strlen(def) + 1);
221
 
        } while(new == NULL and errno == EINTR);
 
194
        char *new = realloc(*e, strlen(def) + 1);
222
195
        if(new == NULL){
223
196
          return false;
224
197
        }
234
207
/*
235
208
 * Based on the example in the GNU LibC manual chapter 13.13 "File
236
209
 * Descriptor Flags".
237
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
210
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
238
211
 */
239
212
static int set_cloexec_flag(int fd){
240
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
213
  int ret = fcntl(fd, F_GETFD, 0);
241
214
  /* If reading the flags failed, return error indication now. */
242
215
  if(ret < 0){
243
216
    return ret;
244
217
  }
245
218
  /* Store modified flag word in the descriptor. */
246
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
247
 
                                       ret | FD_CLOEXEC));
 
219
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
248
220
}
249
221
 
250
222
 
307
279
  }
308
280
  free(plugin_node->environ);
309
281
  free(plugin_node->buffer);
310
 
  
 
282
 
311
283
  /* Removes the plugin from the singly-linked list */
312
284
  if(plugin_node == plugin_list){
313
285
    /* First one - simple */
340
312
  struct dirent *dirst;
341
313
  struct stat st;
342
314
  fd_set rfds_all;
343
 
  int ret, maxfd = 0;
 
315
  int ret, numchars, maxfd = 0;
344
316
  ssize_t sret;
 
317
  intmax_t tmpmax;
345
318
  uid_t uid = 65534;
346
319
  gid_t gid = 65534;
347
320
  bool debug = false;
401
374
      .doc = "Group ID the plugins will run as", .group = 3 },
402
375
    { .name = "debug", .key = 132,
403
376
      .doc = "Debug mode", .group = 4 },
404
 
    /*
405
 
     * These reproduce what we would get without ARGP_NO_HELP
406
 
     */
407
 
    { .name = "help", .key = '?',
408
 
      .doc = "Give this help list", .group = -1 },
409
 
    { .name = "usage", .key = -3,
410
 
      .doc = "Give a short usage message", .group = -1 },
411
 
    { .name = "version", .key = 'V',
412
 
      .doc = "Print program version", .group = -1 },
413
377
    { .name = NULL }
414
378
  };
415
379
  
416
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
 
    errno = 0;
 
380
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
381
                    struct argp_state *state){
418
382
    switch(key){
419
 
      char *tmp;
420
 
      intmax_t tmpmax;
421
383
    case 'g':                   /* --global-options */
422
 
      {
423
 
        char *plugin_option;
424
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
425
 
          if(not add_argument(getplugin(NULL), plugin_option)){
426
 
            break;
 
384
      if(arg != NULL){
 
385
        char *p;
 
386
        while((p = strsep(&arg, ",")) != NULL){
 
387
          if(p[0] == '\0'){
 
388
            continue;
 
389
          }
 
390
          if(not add_argument(getplugin(NULL), p)){
 
391
            perror("add_argument");
 
392
            return ARGP_ERR_UNKNOWN;
427
393
          }
428
394
        }
429
395
      }
430
396
      break;
431
397
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
398
      if(arg == NULL){
 
399
        break;
 
400
      }
 
401
      if(not add_environment(getplugin(NULL), arg, true)){
 
402
        perror("add_environment");
 
403
      }
433
404
      break;
434
405
    case 'o':                   /* --options-for */
435
 
      {
436
 
        char *option_list = strchr(arg, ':');
437
 
        if(option_list == NULL){
438
 
          argp_error(state, "No colon in \"%s\"", arg);
439
 
          errno = EINVAL;
440
 
          break;
441
 
        }
442
 
        *option_list = '\0';
443
 
        option_list++;
444
 
        if(arg[0] == '\0'){
445
 
          argp_error(state, "Empty plugin name");
446
 
          errno = EINVAL;
447
 
          break;
448
 
        }
449
 
        char *option;
450
 
        while((option = strsep(&option_list, ",")) != NULL){
451
 
          if(not add_argument(getplugin(arg), option)){
452
 
            break;
 
406
      if(arg != NULL){
 
407
        char *p_name = strsep(&arg, ":");
 
408
        if(p_name[0] == '\0' or arg == NULL){
 
409
          break;
 
410
        }
 
411
        char *opt = strsep(&arg, ":");
 
412
        if(opt[0] == '\0' or opt == NULL){
 
413
          break;
 
414
        }
 
415
        char *p;
 
416
        while((p = strsep(&opt, ",")) != NULL){
 
417
          if(p[0] == '\0'){
 
418
            continue;
 
419
          }
 
420
          if(not add_argument(getplugin(p_name), p)){
 
421
            perror("add_argument");
 
422
            return ARGP_ERR_UNKNOWN;
453
423
          }
454
424
        }
455
425
      }
456
426
      break;
457
427
    case 'E':                   /* --env-for */
 
428
      if(arg == NULL){
 
429
        break;
 
430
      }
458
431
      {
459
432
        char *envdef = strchr(arg, ':');
460
433
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
434
          break;
464
435
        }
465
436
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
437
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
438
          perror("add_environment");
471
439
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
440
      }
474
441
      break;
475
442
    case 'd':                   /* --disable */
476
 
      {
 
443
      if(arg != NULL){
477
444
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
445
        if(p == NULL){
 
446
          return ARGP_ERR_UNKNOWN;
480
447
        }
 
448
        p->disabled = true;
481
449
      }
482
450
      break;
483
451
    case 'e':                   /* --enable */
484
 
      {
 
452
      if(arg != NULL){
485
453
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
454
        if(p == NULL){
 
455
          return ARGP_ERR_UNKNOWN;
488
456
        }
 
457
        p->disabled = false;
489
458
      }
490
459
      break;
491
460
    case 128:                   /* --plugin-dir */
492
461
      free(plugindir);
493
462
      plugindir = strdup(arg);
 
463
      if(plugindir == NULL){
 
464
        perror("strdup");
 
465
      }      
494
466
      break;
495
467
    case 129:                   /* --config-file */
496
468
      /* This is already done by parse_opt_config_file() */
497
469
      break;
498
470
    case 130:                   /* --userid */
499
 
      tmpmax = strtoimax(arg, &tmp, 10);
500
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
 
         or tmpmax != (uid_t)tmpmax){
502
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
 
                   PRIdMAX, arg, (intmax_t)uid);
504
 
        break;
 
471
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
472
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
473
         or arg[numchars] != '\0'){
 
474
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
475
                PRIdMAX "\n", arg, (intmax_t)uid);
 
476
      } else {
 
477
        uid = (uid_t)tmpmax;
505
478
      }
506
 
      uid = (uid_t)tmpmax;
507
479
      break;
508
480
    case 131:                   /* --groupid */
509
 
      tmpmax = strtoimax(arg, &tmp, 10);
510
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
 
         or tmpmax != (gid_t)tmpmax){
512
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
 
                   PRIdMAX, arg, (intmax_t)gid);
514
 
        break;
 
481
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
482
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
483
         or arg[numchars] != '\0'){
 
484
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
485
                PRIdMAX "\n", arg, (intmax_t)gid);
 
486
      } else {
 
487
        gid = (gid_t)tmpmax;
515
488
      }
516
 
      gid = (gid_t)tmpmax;
517
489
      break;
518
490
    case 132:                   /* --debug */
519
491
      debug = true;
520
492
      break;
521
 
      /*
522
 
       * These reproduce what we would get without ARGP_NO_HELP
523
 
       */
524
 
    case '?':                   /* --help */
525
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
526
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
527
 
    case -3:                    /* --usage */
528
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
529
 
      argp_state_help(state, state->out_stream,
530
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
531
 
    case 'V':                   /* --version */
532
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
533
 
      exit(EXIT_SUCCESS);
534
 
      break;
535
493
/*
536
494
 * When adding more options before this line, remember to also add a
537
495
 * "case" to the "parse_opt_config_file" function below.
540
498
      /* Cryptsetup always passes an argument, which is an empty
541
499
         string if "none" was specified in /etc/crypttab.  So if
542
500
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
501
      if(arg[0] != '\0'){
 
502
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
503
      }
 
504
      break;
 
505
    case ARGP_KEY_END:
 
506
      break;
546
507
    default:
547
508
      return ARGP_ERR_UNKNOWN;
548
509
    }
549
 
    return errno;               /* Set to 0 at start */
 
510
    return 0;
550
511
  }
551
512
  
552
513
  /* This option parser is the same as parse_opt() above, except it
554
515
  error_t parse_opt_config_file(int key, char *arg,
555
516
                                __attribute__((unused))
556
517
                                struct argp_state *state){
557
 
    errno = 0;
558
518
    switch(key){
559
519
    case 'g':                   /* --global-options */
560
520
    case 'G':                   /* --global-env */
567
527
    case 129:                   /* --config-file */
568
528
      free(argfile);
569
529
      argfile = strdup(arg);
570
 
      break;
 
530
      if(argfile == NULL){
 
531
        perror("strdup");
 
532
      }
 
533
      break;      
571
534
    case 130:                   /* --userid */
572
535
    case 131:                   /* --groupid */
573
536
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
537
    case ARGP_KEY_ARG:
 
538
    case ARGP_KEY_END:
578
539
      break;
579
540
    default:
580
541
      return ARGP_ERR_UNKNOWN;
581
542
    }
582
 
    return errno;
 
543
    return 0;
583
544
  }
584
545
  
585
546
  struct argp argp = { .options = options,
589
550
  
590
551
  /* Parse using parse_opt_config_file() in order to get the custom
591
552
     config file location, if any. */
592
 
  ret = argp_parse(&argp, argc, argv,
593
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
594
 
                   NULL, NULL);
595
 
  switch(ret){
596
 
  case 0:
597
 
    break;
598
 
  case ENOMEM:
599
 
  default:
600
 
    errno = ret;
601
 
    perror("argp_parse");
602
 
    exitstatus = EX_OSERR;
603
 
    goto fallback;
604
 
  case EINVAL:
605
 
    exitstatus = EX_USAGE;
 
553
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
554
  if(ret == ARGP_ERR_UNKNOWN){
 
555
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
556
    exitstatus = EXIT_FAILURE;
606
557
    goto fallback;
607
558
  }
608
559
  
614
565
    conffp = fopen(AFILE, "r");
615
566
  } else {
616
567
    conffp = fopen(argfile, "r");
617
 
  }
 
568
  }  
618
569
  if(conffp != NULL){
619
570
    char *org_line = NULL;
620
571
    char *p, *arg, *new_arg, *line;
621
572
    size_t size = 0;
622
573
    const char whitespace_delims[] = " \r\t\f\v\n";
623
574
    const char comment_delim[] = "#";
624
 
    
 
575
 
625
576
    custom_argc = 1;
626
577
    custom_argv = malloc(sizeof(char*) * 2);
627
578
    if(custom_argv == NULL){
631
582
    }
632
583
    custom_argv[0] = argv[0];
633
584
    custom_argv[1] = NULL;
634
 
    
 
585
 
635
586
    /* for each line in the config file, strip whitespace and ignore
636
587
       commented text */
637
588
    while(true){
639
590
      if(sret == -1){
640
591
        break;
641
592
      }
642
 
      
 
593
 
643
594
      line = org_line;
644
595
      arg = strsep(&line, comment_delim);
645
596
      while((p = strsep(&arg, whitespace_delims)) != NULL){
664
615
          goto fallback;
665
616
        }
666
617
        custom_argv[custom_argc-1] = new_arg;
667
 
        custom_argv[custom_argc] = NULL;
 
618
        custom_argv[custom_argc] = NULL;        
668
619
      }
669
620
    }
670
 
    do {
671
 
      ret = fclose(conffp);
672
 
    } while(ret == EOF and errno == EINTR);
673
 
    if(ret == EOF){
674
 
      perror("fclose");
675
 
      exitstatus = EXIT_FAILURE;
676
 
      goto fallback;
677
 
    }
678
621
    free(org_line);
679
622
  } else {
680
623
    /* Check for harmful errors and go to fallback. Other errors might
685
628
      goto fallback;
686
629
    }
687
630
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
631
  /* If there was any arguments from configuration file,
 
632
     pass them to parser as command arguments */
690
633
  if(custom_argv != NULL){
691
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
692
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
693
 
                     NULL, NULL);
694
 
    switch(ret){
695
 
    case 0:
696
 
      break;
697
 
    case ENOMEM:
698
 
    default:
699
 
      errno = ret;
700
 
      perror("argp_parse");
701
 
      exitstatus = EX_OSERR;
702
 
      goto fallback;
703
 
    case EINVAL:
704
 
      exitstatus = EX_CONFIG;
 
634
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
635
                     0, NULL);
 
636
    if(ret == ARGP_ERR_UNKNOWN){
 
637
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
638
      exitstatus = EXIT_FAILURE;
705
639
      goto fallback;
706
640
    }
707
641
  }
708
642
  
709
643
  /* Parse actual command line arguments, to let them override the
710
644
     config file */
711
 
  ret = argp_parse(&argp, argc, argv,
712
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
713
 
                   NULL, NULL);
714
 
  switch(ret){
715
 
  case 0:
716
 
    break;
717
 
  case ENOMEM:
718
 
  default:
719
 
    errno = ret;
720
 
    perror("argp_parse");
721
 
    exitstatus = EX_OSERR;
722
 
    goto fallback;
723
 
  case EINVAL:
724
 
    exitstatus = EX_USAGE;
 
645
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
646
  if(ret == ARGP_ERR_UNKNOWN){
 
647
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
648
    exitstatus = EXIT_FAILURE;
725
649
    goto fallback;
726
650
  }
727
651
  
749
673
    perror("setuid");
750
674
  }
751
675
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
676
  if(plugindir == NULL){
 
677
    dir = opendir(PDIR);
 
678
  } else {
 
679
    dir = opendir(plugindir);
 
680
  }
 
681
  
 
682
  if(dir == NULL){
 
683
    perror("Could not open plugin dir");
 
684
    exitstatus = EXIT_FAILURE;
 
685
    goto fallback;
 
686
  }
 
687
  
 
688
  /* Set the FD_CLOEXEC flag on the directory, if possible */
753
689
  {
754
 
    int dir_fd = -1;
755
 
    if(plugindir == NULL){
756
 
      dir_fd = open(PDIR, O_RDONLY |
757
 
#ifdef O_CLOEXEC
758
 
                    O_CLOEXEC
759
 
#else  /* not O_CLOEXEC */
760
 
                    0
761
 
#endif  /* not O_CLOEXEC */
762
 
                    );
763
 
    } else {
764
 
      dir_fd = open(plugindir, O_RDONLY |
765
 
#ifdef O_CLOEXEC
766
 
                    O_CLOEXEC
767
 
#else  /* not O_CLOEXEC */
768
 
                    0
769
 
#endif  /* not O_CLOEXEC */
770
 
                    );
771
 
    }
772
 
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
775
 
      goto fallback;
776
 
    }
777
 
    
778
 
#ifndef O_CLOEXEC
779
 
  /* Set the FD_CLOEXEC flag on the directory */
780
 
    ret = set_cloexec_flag(dir_fd);
781
 
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
783
 
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
785
 
      goto fallback;
786
 
    }
787
 
#endif  /* O_CLOEXEC */
788
 
    
789
 
    dir = fdopendir(dir_fd);
790
 
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
792
 
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
794
 
      goto fallback;
 
690
    int dir_fd = dirfd(dir);
 
691
    if(dir_fd >= 0){
 
692
      ret = set_cloexec_flag(dir_fd);
 
693
      if(ret < 0){
 
694
        perror("set_cloexec_flag");
 
695
        exitstatus = EXIT_FAILURE;
 
696
        goto fallback;
 
697
      }
795
698
    }
796
699
  }
797
700
  
799
702
  
800
703
  /* Read and execute any executable in the plugin directory*/
801
704
  while(true){
802
 
    do {
803
 
      dirst = readdir(dir);
804
 
    } while(dirst == NULL and errno == EINTR);
 
705
    dirst = readdir(dir);
805
706
    
806
707
    /* All directory entries have been processed */
807
708
    if(dirst == NULL){
858
759
        continue;
859
760
      }
860
761
    }
861
 
    
 
762
 
862
763
    char *filename;
863
764
    if(plugindir == NULL){
864
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
865
 
                                             dirst->d_name));
 
765
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
866
766
    } else {
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
868
 
                                             plugindir,
869
 
                                             dirst->d_name));
 
767
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
870
768
    }
871
769
    if(ret < 0){
872
770
      perror("asprintf");
873
771
      continue;
874
772
    }
875
773
    
876
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
774
    ret = stat(filename, &st);
877
775
    if(ret == -1){
878
776
      perror("stat");
879
777
      free(filename);
880
778
      continue;
881
779
    }
882
 
    
 
780
 
883
781
    /* Ignore non-executable files */
884
 
    if(not S_ISREG(st.st_mode)
885
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
782
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
886
783
      if(debug){
887
784
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
888
785
                " with bad type or mode\n", filename);
934
831
    }
935
832
    
936
833
    int pipefd[2];
937
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
834
    ret = pipe(pipefd);
938
835
    if(ret == -1){
939
836
      perror("pipe");
940
837
      exitstatus = EXIT_FAILURE;
954
851
      goto fallback;
955
852
    }
956
853
    /* Block SIGCHLD until process is safely in process list */
957
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
958
 
                                              &sigchld_action.sa_mask,
959
 
                                              NULL));
 
854
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
960
855
    if(ret < 0){
961
856
      perror("sigprocmask");
962
857
      exitstatus = EXIT_FAILURE;
963
858
      goto fallback;
964
859
    }
965
860
    /* Starting a new process to be watched */
966
 
    pid_t pid;
967
 
    do {
968
 
      pid = fork();
969
 
    } while(pid == -1 and errno == EINTR);
 
861
    pid_t pid = fork();
970
862
    if(pid == -1){
971
863
      perror("fork");
972
864
      exitstatus = EXIT_FAILURE;
1010
902
      /* no return */
1011
903
    }
1012
904
    /* Parent process */
1013
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1014
 
                                             pipe */
 
905
    close(pipefd[1]);           /* Close unused write end of pipe */
1015
906
    free(filename);
1016
907
    plugin *new_plugin = getplugin(dirst->d_name);
1017
908
    if(new_plugin == NULL){
1018
909
      perror("getplugin");
1019
 
      ret = (int)(TEMP_FAILURE_RETRY
1020
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
 
                               NULL)));
 
910
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1022
911
      if(ret < 0){
1023
912
        perror("sigprocmask");
1024
913
      }
1031
920
    
1032
921
    /* Unblock SIGCHLD so signal handler can be run if this process
1033
922
       has already completed */
1034
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1035
 
                                              &sigchld_action.sa_mask,
1036
 
                                              NULL));
 
923
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1037
924
    if(ret < 0){
1038
925
      perror("sigprocmask");
1039
926
      exitstatus = EXIT_FAILURE;
1040
927
      goto fallback;
1041
928
    }
1042
929
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
930
    FD_SET(new_plugin->fd, &rfds_all);
1045
931
    
1046
932
    if(maxfd < new_plugin->fd){
1047
933
      maxfd = new_plugin->fd;
1048
934
    }
1049
935
  }
1050
936
  
1051
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
937
  closedir(dir);
1052
938
  dir = NULL;
1053
939
  free_plugin(getplugin(NULL));
1054
940
  
1067
953
  while(plugin_list){
1068
954
    fd_set rfds = rfds_all;
1069
955
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
 
    if(select_ret == -1 and errno != EINTR){
 
956
    if(select_ret == -1){
1071
957
      perror("select");
1072
958
      exitstatus = EXIT_FAILURE;
1073
959
      goto fallback;
1081
967
        if(not WIFEXITED(proc->status)
1082
968
           or WEXITSTATUS(proc->status) != 0){
1083
969
          /* Bad exit by plugin */
1084
 
          
 
970
 
1085
971
          if(debug){
1086
972
            if(WIFEXITED(proc->status)){
1087
973
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
1090
976
                      WEXITSTATUS(proc->status));
1091
977
            } else if(WIFSIGNALED(proc->status)){
1092
978
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1093
 
                      " signal %d: %s\n", proc->name,
 
979
                      " signal %d\n", proc->name,
1094
980
                      (intmax_t) (proc->pid),
1095
 
                      WTERMSIG(proc->status),
1096
 
                      strsignal(WTERMSIG(proc->status)));
 
981
                      WTERMSIG(proc->status));
1097
982
            } else if(WCOREDUMP(proc->status)){
1098
983
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1099
984
                      " core\n", proc->name, (intmax_t) (proc->pid));
1101
986
          }
1102
987
          
1103
988
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
1106
 
          
 
989
          FD_CLR(proc->fd, &rfds_all);
 
990
 
1107
991
          /* Block signal while modifying process_list */
1108
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1109
 
                                        (SIG_BLOCK,
1110
 
                                         &sigchld_action.sa_mask,
1111
 
                                         NULL));
 
992
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1112
993
          if(ret < 0){
1113
994
            perror("sigprocmask");
1114
995
            exitstatus = EXIT_FAILURE;
1120
1001
          proc = next_plugin;
1121
1002
          
1122
1003
          /* We are done modifying process list, so unblock signal */
1123
 
          ret = (int)(TEMP_FAILURE_RETRY
1124
 
                      (sigprocmask(SIG_UNBLOCK,
1125
 
                                   &sigchld_action.sa_mask, NULL)));
 
1004
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1005
                            NULL);
1126
1006
          if(ret < 0){
1127
1007
            perror("sigprocmask");
1128
1008
            exitstatus = EXIT_FAILURE;
1148
1028
      }
1149
1029
      
1150
1030
      /* This process has not completed.  Does it have any output? */
1151
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1152
 
                                                         warning from
1153
 
                                                         -Wconversion */
 
1031
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1032
        /* This process had nothing to say at this time */
1155
1033
        proc = proc->next;
1156
1034
        continue;
1167
1045
        proc->buffer_size += BUFFER_SIZE;
1168
1046
      }
1169
1047
      /* Read from the process */
1170
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1171
 
                                     proc->buffer
1172
 
                                     + proc->buffer_length,
1173
 
                                     BUFFER_SIZE));
 
1048
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1049
                  BUFFER_SIZE);
1174
1050
      if(sret < 0){
1175
1051
        /* Read error from this process; ignore the error */
1176
1052
        proc = proc->next;
1184
1060
      }
1185
1061
    }
1186
1062
  }
1187
 
  
1188
 
  
 
1063
 
 
1064
 
1189
1065
 fallback:
1190
1066
  
1191
1067
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1238
1114
  }
1239
1115
  
1240
1116
  /* Wait for any remaining child processes to terminate */
1241
 
  do {
 
1117
  do{
1242
1118
    ret = wait(NULL);
1243
1119
  } while(ret >= 0);
1244
1120
  if(errno != ECHILD){