/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: 2009-01-17 00:26:13 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090117002613-lg5x2b8mnmi3alqy
Fix problem found using cppcheck, thanks to George Danchev
<danchev@spnet.net> for the report and fix:

* plugin-runner.c (getplugin): Bug fix: Free the plugin struct in case
                               of strdup failure.

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;
93
89
  size_t buffer_size;
94
90
  size_t buffer_length;
95
91
  bool eof;
96
 
  volatile sig_atomic_t completed;
97
 
  int status;
 
92
  volatile bool completed;
 
93
  volatile int status;
98
94
  struct plugin *next;
99
95
} plugin;
100
96
 
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
  }
133
122
  
134
 
  *new_plugin = (plugin){ .name = copy_name,
135
 
                          .argc = 1,
136
 
                          .disabled = false,
137
 
                          .next = plugin_list };
 
123
  *new_plugin = (plugin) { .name = copy_name,
 
124
                           .argc = 1,
 
125
                           .disabled = false,
 
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
 
251
223
/* Mark processes as completed when they exit, and save their exit
252
224
   status. */
253
225
static void handle_sigchld(__attribute__((unused)) int sig){
254
 
  int old_errno = errno;
255
226
  while(true){
256
227
    plugin *proc = plugin_list;
257
228
    int status;
261
232
      break;
262
233
    }
263
234
    if(pid == -1){
264
 
      if(errno == ECHILD){
265
 
        /* No child processes */
266
 
        break;
 
235
      if(errno != ECHILD){
 
236
        perror("waitpid");
267
237
      }
268
 
      perror("waitpid");
 
238
      /* No child processes */
 
239
      break;
269
240
    }
270
241
    
271
242
    /* A child exited, find it in process_list */
277
248
      continue;
278
249
    }
279
250
    proc->status = status;
280
 
    proc->completed = 1;
 
251
    proc->completed = true;
281
252
  }
282
 
  errno = old_errno;
283
253
}
284
254
 
285
255
/* Prints out a password to stdout */
307
277
  }
308
278
  free(plugin_node->environ);
309
279
  free(plugin_node->buffer);
310
 
  
 
280
 
311
281
  /* Removes the plugin from the singly-linked list */
312
282
  if(plugin_node == plugin_list){
313
283
    /* First one - simple */
340
310
  struct dirent *dirst;
341
311
  struct stat st;
342
312
  fd_set rfds_all;
343
 
  int ret, maxfd = 0;
 
313
  int ret, numchars, maxfd = 0;
344
314
  ssize_t sret;
 
315
  intmax_t tmpmax;
345
316
  uid_t uid = 65534;
346
317
  gid_t gid = 65534;
347
318
  bool debug = false;
401
372
      .doc = "Group ID the plugins will run as", .group = 3 },
402
373
    { .name = "debug", .key = 132,
403
374
      .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
375
    { .name = NULL }
414
376
  };
415
377
  
416
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
 
    errno = 0;
418
 
    switch(key){
419
 
      char *tmp;
420
 
      intmax_t tmpmax;
 
378
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
379
                    struct argp_state *state) {
 
380
    switch(key) {
421
381
    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;
 
382
      if(arg != NULL){
 
383
        char *p;
 
384
        while((p = strsep(&arg, ",")) != NULL){
 
385
          if(p[0] == '\0'){
 
386
            continue;
 
387
          }
 
388
          if(not add_argument(getplugin(NULL), p)){
 
389
            perror("add_argument");
 
390
            return ARGP_ERR_UNKNOWN;
427
391
          }
428
392
        }
429
393
      }
430
394
      break;
431
395
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
396
      if(arg == NULL){
 
397
        break;
 
398
      }
 
399
      if(not add_environment(getplugin(NULL), arg, true)){
 
400
        perror("add_environment");
 
401
      }
433
402
      break;
434
403
    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;
 
404
      if(arg != NULL){
 
405
        char *p_name = strsep(&arg, ":");
 
406
        if(p_name[0] == '\0' or arg == NULL){
 
407
          break;
 
408
        }
 
409
        char *opt = strsep(&arg, ":");
 
410
        if(opt[0] == '\0' or opt == NULL){
 
411
          break;
 
412
        }
 
413
        char *p;
 
414
        while((p = strsep(&opt, ",")) != NULL){
 
415
          if(p[0] == '\0'){
 
416
            continue;
 
417
          }
 
418
          if(not add_argument(getplugin(p_name), p)){
 
419
            perror("add_argument");
 
420
            return ARGP_ERR_UNKNOWN;
453
421
          }
454
422
        }
455
423
      }
456
424
      break;
457
425
    case 'E':                   /* --env-for */
 
426
      if(arg == NULL){
 
427
        break;
 
428
      }
458
429
      {
459
430
        char *envdef = strchr(arg, ':');
460
431
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
432
          break;
464
433
        }
465
434
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
435
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
436
          perror("add_environment");
471
437
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
438
      }
474
439
      break;
475
440
    case 'd':                   /* --disable */
476
 
      {
 
441
      if(arg != NULL){
477
442
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
443
        if(p == NULL){
 
444
          return ARGP_ERR_UNKNOWN;
480
445
        }
 
446
        p->disabled = true;
481
447
      }
482
448
      break;
483
449
    case 'e':                   /* --enable */
484
 
      {
 
450
      if(arg != NULL){
485
451
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
452
        if(p == NULL){
 
453
          return ARGP_ERR_UNKNOWN;
488
454
        }
 
455
        p->disabled = false;
489
456
      }
490
457
      break;
491
458
    case 128:                   /* --plugin-dir */
492
459
      free(plugindir);
493
460
      plugindir = strdup(arg);
 
461
      if(plugindir == NULL){
 
462
        perror("strdup");
 
463
      }      
494
464
      break;
495
465
    case 129:                   /* --config-file */
496
466
      /* This is already done by parse_opt_config_file() */
497
467
      break;
498
468
    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;
 
469
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
470
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
471
         or arg[numchars] != '\0'){
 
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
473
                PRIdMAX "\n", arg, (intmax_t)uid);
 
474
      } else {
 
475
        uid = (uid_t)tmpmax;
505
476
      }
506
 
      uid = (uid_t)tmpmax;
507
477
      break;
508
478
    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;
 
479
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
480
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
481
         or arg[numchars] != '\0'){
 
482
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
483
                PRIdMAX "\n", arg, (intmax_t)gid);
 
484
      } else {
 
485
        gid = (gid_t)tmpmax;
515
486
      }
516
 
      gid = (gid_t)tmpmax;
517
487
      break;
518
488
    case 132:                   /* --debug */
519
489
      debug = true;
520
490
      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
491
/*
536
492
 * When adding more options before this line, remember to also add a
537
493
 * "case" to the "parse_opt_config_file" function below.
540
496
      /* Cryptsetup always passes an argument, which is an empty
541
497
         string if "none" was specified in /etc/crypttab.  So if
542
498
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
499
      if(arg[0] != '\0'){
 
500
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
501
      }
 
502
      break;
 
503
    case ARGP_KEY_END:
 
504
      break;
546
505
    default:
547
506
      return ARGP_ERR_UNKNOWN;
548
507
    }
549
 
    return errno;               /* Set to 0 at start */
 
508
    return 0;
550
509
  }
551
510
  
552
511
  /* This option parser is the same as parse_opt() above, except it
553
512
     ignores everything but the --config-file option. */
554
513
  error_t parse_opt_config_file(int key, char *arg,
555
514
                                __attribute__((unused))
556
 
                                struct argp_state *state){
557
 
    errno = 0;
558
 
    switch(key){
 
515
                                struct argp_state *state) {
 
516
    switch(key) {
559
517
    case 'g':                   /* --global-options */
560
518
    case 'G':                   /* --global-env */
561
519
    case 'o':                   /* --options-for */
567
525
    case 129:                   /* --config-file */
568
526
      free(argfile);
569
527
      argfile = strdup(arg);
570
 
      break;
 
528
      if(argfile == NULL){
 
529
        perror("strdup");
 
530
      }
 
531
      break;      
571
532
    case 130:                   /* --userid */
572
533
    case 131:                   /* --groupid */
573
534
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
535
    case ARGP_KEY_ARG:
 
536
    case ARGP_KEY_END:
578
537
      break;
579
538
    default:
580
539
      return ARGP_ERR_UNKNOWN;
581
540
    }
582
 
    return errno;
 
541
    return 0;
583
542
  }
584
543
  
585
544
  struct argp argp = { .options = options,
589
548
  
590
549
  /* Parse using parse_opt_config_file() in order to get the custom
591
550
     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;
 
551
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
552
  if(ret == ARGP_ERR_UNKNOWN){
 
553
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
554
    exitstatus = EXIT_FAILURE;
606
555
    goto fallback;
607
556
  }
608
557
  
614
563
    conffp = fopen(AFILE, "r");
615
564
  } else {
616
565
    conffp = fopen(argfile, "r");
617
 
  }
 
566
  }  
618
567
  if(conffp != NULL){
619
568
    char *org_line = NULL;
620
569
    char *p, *arg, *new_arg, *line;
621
570
    size_t size = 0;
622
571
    const char whitespace_delims[] = " \r\t\f\v\n";
623
572
    const char comment_delim[] = "#";
624
 
    
 
573
 
625
574
    custom_argc = 1;
626
575
    custom_argv = malloc(sizeof(char*) * 2);
627
576
    if(custom_argv == NULL){
631
580
    }
632
581
    custom_argv[0] = argv[0];
633
582
    custom_argv[1] = NULL;
634
 
    
 
583
 
635
584
    /* for each line in the config file, strip whitespace and ignore
636
585
       commented text */
637
586
    while(true){
639
588
      if(sret == -1){
640
589
        break;
641
590
      }
642
 
      
 
591
 
643
592
      line = org_line;
644
593
      arg = strsep(&line, comment_delim);
645
594
      while((p = strsep(&arg, whitespace_delims)) != NULL){
664
613
          goto fallback;
665
614
        }
666
615
        custom_argv[custom_argc-1] = new_arg;
667
 
        custom_argv[custom_argc] = NULL;
 
616
        custom_argv[custom_argc] = NULL;        
668
617
      }
669
618
    }
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
619
    free(org_line);
679
620
  } else {
680
621
    /* Check for harmful errors and go to fallback. Other errors might
685
626
      goto fallback;
686
627
    }
687
628
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
629
  /* If there was any arguments from configuration file,
 
630
     pass them to parser as command arguments */
690
631
  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;
 
632
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
633
                     0, NULL);
 
634
    if(ret == ARGP_ERR_UNKNOWN){
 
635
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
636
      exitstatus = EXIT_FAILURE;
705
637
      goto fallback;
706
638
    }
707
639
  }
708
640
  
709
641
  /* Parse actual command line arguments, to let them override the
710
642
     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;
 
643
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
644
  if(ret == ARGP_ERR_UNKNOWN){
 
645
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
646
    exitstatus = EXIT_FAILURE;
725
647
    goto fallback;
726
648
  }
727
649
  
740
662
  }
741
663
  
742
664
  /* Strip permissions down to nobody */
 
665
  ret = setuid(uid);
 
666
  if(ret == -1){
 
667
    perror("setuid");
 
668
  }  
743
669
  setgid(gid);
744
670
  if(ret == -1){
745
671
    perror("setgid");
746
672
  }
747
 
  ret = setuid(uid);
748
 
  if(ret == -1){
749
 
    perror("setuid");
750
 
  }
751
 
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
673
  
 
674
  if(plugindir == NULL){
 
675
    dir = opendir(PDIR);
 
676
  } else {
 
677
    dir = opendir(plugindir);
 
678
  }
 
679
  
 
680
  if(dir == NULL){
 
681
    perror("Could not open plugin dir");
 
682
    exitstatus = EXIT_FAILURE;
 
683
    goto fallback;
 
684
  }
 
685
  
 
686
  /* Set the FD_CLOEXEC flag on the directory, if possible */
753
687
  {
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;
 
688
    int dir_fd = dirfd(dir);
 
689
    if(dir_fd >= 0){
 
690
      ret = set_cloexec_flag(dir_fd);
 
691
      if(ret < 0){
 
692
        perror("set_cloexec_flag");
 
693
        exitstatus = EXIT_FAILURE;
 
694
        goto fallback;
 
695
      }
795
696
    }
796
697
  }
797
698
  
799
700
  
800
701
  /* Read and execute any executable in the plugin directory*/
801
702
  while(true){
802
 
    do {
803
 
      dirst = readdir(dir);
804
 
    } while(dirst == NULL and errno == EINTR);
 
703
    dirst = readdir(dir);
805
704
    
806
705
    /* All directory entries have been processed */
807
706
    if(dirst == NULL){
858
757
        continue;
859
758
      }
860
759
    }
861
 
    
 
760
 
862
761
    char *filename;
863
762
    if(plugindir == NULL){
864
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
865
 
                                             dirst->d_name));
 
763
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
866
764
    } else {
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
868
 
                                             plugindir,
869
 
                                             dirst->d_name));
 
765
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
870
766
    }
871
767
    if(ret < 0){
872
768
      perror("asprintf");
873
769
      continue;
874
770
    }
875
771
    
876
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
772
    ret = stat(filename, &st);
877
773
    if(ret == -1){
878
774
      perror("stat");
879
775
      free(filename);
880
776
      continue;
881
777
    }
882
 
    
 
778
 
883
779
    /* Ignore non-executable files */
884
 
    if(not S_ISREG(st.st_mode)
885
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
780
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
886
781
      if(debug){
887
782
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
888
783
                " with bad type or mode\n", filename);
934
829
    }
935
830
    
936
831
    int pipefd[2];
937
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
832
    ret = pipe(pipefd);
938
833
    if(ret == -1){
939
834
      perror("pipe");
940
835
      exitstatus = EXIT_FAILURE;
954
849
      goto fallback;
955
850
    }
956
851
    /* 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));
 
852
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
960
853
    if(ret < 0){
961
854
      perror("sigprocmask");
962
855
      exitstatus = EXIT_FAILURE;
963
856
      goto fallback;
964
857
    }
965
858
    /* Starting a new process to be watched */
966
 
    pid_t pid;
967
 
    do {
968
 
      pid = fork();
969
 
    } while(pid == -1 and errno == EINTR);
 
859
    pid_t pid = fork();
970
860
    if(pid == -1){
971
861
      perror("fork");
972
862
      exitstatus = EXIT_FAILURE;
1010
900
      /* no return */
1011
901
    }
1012
902
    /* Parent process */
1013
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1014
 
                                             pipe */
 
903
    close(pipefd[1]);           /* Close unused write end of pipe */
1015
904
    free(filename);
1016
905
    plugin *new_plugin = getplugin(dirst->d_name);
1017
906
    if(new_plugin == NULL){
1018
907
      perror("getplugin");
1019
 
      ret = (int)(TEMP_FAILURE_RETRY
1020
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
 
                               NULL)));
 
908
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1022
909
      if(ret < 0){
1023
910
        perror("sigprocmask");
1024
911
      }
1031
918
    
1032
919
    /* Unblock SIGCHLD so signal handler can be run if this process
1033
920
       has already completed */
1034
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1035
 
                                              &sigchld_action.sa_mask,
1036
 
                                              NULL));
 
921
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1037
922
    if(ret < 0){
1038
923
      perror("sigprocmask");
1039
924
      exitstatus = EXIT_FAILURE;
1040
925
      goto fallback;
1041
926
    }
1042
927
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
928
    FD_SET(new_plugin->fd, &rfds_all);
1045
929
    
1046
930
    if(maxfd < new_plugin->fd){
1047
931
      maxfd = new_plugin->fd;
1048
932
    }
1049
933
  }
1050
934
  
1051
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
935
  closedir(dir);
1052
936
  dir = NULL;
1053
 
  free_plugin(getplugin(NULL));
1054
937
  
1055
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1056
939
    if(p->pid != 0){
1067
950
  while(plugin_list){
1068
951
    fd_set rfds = rfds_all;
1069
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
 
    if(select_ret == -1 and errno != EINTR){
 
953
    if(select_ret == -1){
1071
954
      perror("select");
1072
955
      exitstatus = EXIT_FAILURE;
1073
956
      goto fallback;
1076
959
       from one of them */
1077
960
    for(plugin *proc = plugin_list; proc != NULL;){
1078
961
      /* Is this process completely done? */
1079
 
      if(proc->completed and proc->eof){
 
962
      if(proc->eof and proc->completed){
1080
963
        /* Only accept the plugin output if it exited cleanly */
1081
964
        if(not WIFEXITED(proc->status)
1082
965
           or WEXITSTATUS(proc->status) != 0){
1083
966
          /* Bad exit by plugin */
1084
 
          
 
967
 
1085
968
          if(debug){
1086
969
            if(WIFEXITED(proc->status)){
1087
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
1088
 
                      " status %d\n", proc->name,
1089
 
                      (intmax_t) (proc->pid),
 
970
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
971
                      " %d\n", (intmax_t) (proc->pid),
1090
972
                      WEXITSTATUS(proc->status));
1091
 
            } else if(WIFSIGNALED(proc->status)){
1092
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1093
 
                      " signal %d: %s\n", proc->name,
1094
 
                      (intmax_t) (proc->pid),
1095
 
                      WTERMSIG(proc->status),
1096
 
                      strsignal(WTERMSIG(proc->status)));
 
973
            } else if(WIFSIGNALED(proc->status)) {
 
974
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
975
                      " %d\n", (intmax_t) (proc->pid),
 
976
                      WTERMSIG(proc->status));
1097
977
            } else if(WCOREDUMP(proc->status)){
1098
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1099
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
978
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
979
                      (intmax_t) (proc->pid));
1100
980
            }
1101
981
          }
1102
982
          
1103
983
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
1106
 
          
 
984
          FD_CLR(proc->fd, &rfds_all);
 
985
 
1107
986
          /* Block signal while modifying process_list */
1108
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1109
 
                                        (SIG_BLOCK,
1110
 
                                         &sigchld_action.sa_mask,
1111
 
                                         NULL));
 
987
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1112
988
          if(ret < 0){
1113
989
            perror("sigprocmask");
1114
990
            exitstatus = EXIT_FAILURE;
1120
996
          proc = next_plugin;
1121
997
          
1122
998
          /* 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)));
 
999
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1000
                            NULL);
1126
1001
          if(ret < 0){
1127
1002
            perror("sigprocmask");
1128
1003
            exitstatus = EXIT_FAILURE;
1148
1023
      }
1149
1024
      
1150
1025
      /* 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 */
 
1026
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1027
        /* This process had nothing to say at this time */
1155
1028
        proc = proc->next;
1156
1029
        continue;
1167
1040
        proc->buffer_size += BUFFER_SIZE;
1168
1041
      }
1169
1042
      /* Read from the process */
1170
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1171
 
                                     proc->buffer
1172
 
                                     + proc->buffer_length,
1173
 
                                     BUFFER_SIZE));
 
1043
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1044
                  BUFFER_SIZE);
1174
1045
      if(sret < 0){
1175
1046
        /* Read error from this process; ignore the error */
1176
1047
        proc = proc->next;
1184
1055
      }
1185
1056
    }
1186
1057
  }
1187
 
  
1188
 
  
 
1058
 
 
1059
 
1189
1060
 fallback:
1190
1061
  
1191
1062
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1238
1109
  }
1239
1110
  
1240
1111
  /* Wait for any remaining child processes to terminate */
1241
 
  do {
 
1112
  do{
1242
1113
    ret = wait(NULL);
1243
1114
  } while(ret >= 0);
1244
1115
  if(errno != ECHILD){