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