/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

Convert some programs to use the exit codes from <sysexits.h>.  Change
all programs using the "argp" parsing functions to use them correctly;
checking return value, using argp_error() to report parse errors etc.

* plugin-runner.c: Use <sysexits.h> exit codes.  Always use fallback,
                   even on option errors, except for "--help", etc.
  (getplugin): Make sure "errno" is set correctly on return.
  (main): Declare our own "--help", "--usage", and "--version"
          options which do not cause the fallback to be invoked.
          In all other options, use fallback on any error.
  (parse_opt, parse_opt_config_file): Reset errno at start and return
                                      errno.  No need to check "arg"
                                      for NULL.  New "--help",
                                      "--usage", and "--version"
                                      options.
  (parse_opt): Accept empty string as global option.  Do not print
               errors which will be detected and reported later.  Do
               "argp_error()" on parse error or empty plugin names.
* plugins.d/mandos-client.c: Use <sysexits.h> exit codes.  Do not
                             return successful exit code on "--help",
                             etc. since this would give the wrong
                             message to "plugin-runner".
  (main): Declare our own "--help", "--usage", and "--version"
          options which do not return a successful exit code.
  (parse_opt): Reset errno at start and return errno.  Do
               "argp_error()" on parse errors.  New "--help",
               "--usage", and "--version" options.
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>.  Do
                               not return successful exit code on
                               "--help", etc. since this would give
                               the wrong message to "plugin-runner".
  (main): Declare our own "--help", "--usage", and "--version" options
          which do not return a successful exit code.  Do
          close(STDOUT_FILENO) after writing to check its return code.
  (parse_opt): Reset errno at start and return errno.

Show diffs side-by-side

added added

removed removed

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