/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

Changed ForkingMixIn in favor of multiprocessing
Added approval functionallity

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
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
 
                                   EXIT_SUCCESS, realloc() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   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(), strcmp(), strncmp() */
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, EX_IOERR,
 
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
68
73
 
69
74
#define BUFFER_SIZE 256
70
75
 
81
86
  char **environ;
82
87
  int envc;
83
88
  bool disabled;
84
 
 
 
89
  
85
90
  /* Variables used for running processes*/
86
91
  pid_t pid;
87
92
  int fd;
99
104
/* Gets an existing plugin based on name,
100
105
   or if none is found, creates a new one */
101
106
static plugin *getplugin(char *name){
102
 
  /* Check for exiting plugin with that name */
 
107
  /* Check for existing plugin with that name */
103
108
  for(plugin *p = plugin_list; p != NULL; p = p->next){
104
109
    if((p->name == name)
105
110
       or (p->name and name and (strcmp(p->name, name) == 0))){
107
112
    }
108
113
  }
109
114
  /* Create a new plugin */
110
 
  plugin *new_plugin = malloc(sizeof(plugin));
 
115
  plugin *new_plugin = NULL;
 
116
  do {
 
117
    new_plugin = malloc(sizeof(plugin));
 
118
  } while(new_plugin == NULL and errno == EINTR);
111
119
  if(new_plugin == NULL){
112
120
    return NULL;
113
121
  }
114
122
  char *copy_name = NULL;
115
123
  if(name != NULL){
116
 
    copy_name = strdup(name);
 
124
    do {
 
125
      copy_name = strdup(name);
 
126
    } while(copy_name == NULL and errno == EINTR);
117
127
    if(copy_name == NULL){
 
128
      int e = errno;
118
129
      free(new_plugin);
 
130
      errno = e;
119
131
      return NULL;
120
132
    }
121
133
  }
125
137
                          .disabled = false,
126
138
                          .next = plugin_list };
127
139
  
128
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
 
140
  do {
 
141
    new_plugin->argv = malloc(sizeof(char *) * 2);
 
142
  } while(new_plugin->argv == NULL and errno == EINTR);
129
143
  if(new_plugin->argv == NULL){
 
144
    int e = errno;
130
145
    free(copy_name);
131
146
    free(new_plugin);
 
147
    errno = e;
132
148
    return NULL;
133
149
  }
134
150
  new_plugin->argv[0] = copy_name;
135
151
  new_plugin->argv[1] = NULL;
136
152
  
137
 
  new_plugin->environ = malloc(sizeof(char *));
 
153
  do {
 
154
    new_plugin->environ = malloc(sizeof(char *));
 
155
  } while(new_plugin->environ == NULL and errno == EINTR);
138
156
  if(new_plugin->environ == NULL){
 
157
    int e = errno;
139
158
    free(copy_name);
140
159
    free(new_plugin->argv);
141
160
    free(new_plugin);
 
161
    errno = e;
142
162
    return NULL;
143
163
  }
144
164
  new_plugin->environ[0] = NULL;
152
172
static bool add_to_char_array(const char *new, char ***array,
153
173
                              int *len){
154
174
  /* Resize the pointed-to array to hold one more pointer */
155
 
  *array = realloc(*array, sizeof(char *)
156
 
                   * (size_t) ((*len) + 2));
 
175
  do {
 
176
    *array = realloc(*array, sizeof(char *)
 
177
                     * (size_t) ((*len) + 2));
 
178
  } while(*array == NULL and errno == EINTR);
157
179
  /* Malloc check */
158
180
  if(*array == NULL){
159
181
    return false;
160
182
  }
161
183
  /* Make a copy of the new string */
162
 
  char *copy = strdup(new);
 
184
  char *copy;
 
185
  do {
 
186
    copy = strdup(new);
 
187
  } while(copy == NULL and errno == EINTR);
163
188
  if(copy == NULL){
164
189
    return false;
165
190
  }
191
216
    if(strncmp(*e, def, namelen + 1) == 0){
192
217
      /* It already exists */
193
218
      if(replace){
194
 
        char *new = realloc(*e, strlen(def) + 1);
 
219
        char *new;
 
220
        do {
 
221
          new = realloc(*e, strlen(def) + 1);
 
222
        } while(new == NULL and errno == EINTR);
195
223
        if(new == NULL){
196
224
          return false;
197
225
        }
207
235
/*
208
236
 * Based on the example in the GNU LibC manual chapter 13.13 "File
209
237
 * Descriptor Flags".
210
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
238
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
211
239
 */
212
240
static int set_cloexec_flag(int fd){
213
 
  int ret = fcntl(fd, F_GETFD, 0);
 
241
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
214
242
  /* If reading the flags failed, return error indication now. */
215
243
  if(ret < 0){
216
244
    return ret;
217
245
  }
218
246
  /* Store modified flag word in the descriptor. */
219
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
247
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
248
                                       ret | FD_CLOEXEC));
220
249
}
221
250
 
222
251
 
279
308
  }
280
309
  free(plugin_node->environ);
281
310
  free(plugin_node->buffer);
282
 
 
 
311
  
283
312
  /* Removes the plugin from the singly-linked list */
284
313
  if(plugin_node == plugin_list){
285
314
    /* First one - simple */
312
341
  struct dirent *dirst;
313
342
  struct stat st;
314
343
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
344
  int ret, maxfd = 0;
316
345
  ssize_t sret;
317
 
  intmax_t tmpmax;
318
346
  uid_t uid = 65534;
319
347
  gid_t gid = 65534;
320
348
  bool debug = false;
330
358
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
331
359
  if(ret == -1){
332
360
    perror("sigaddset");
333
 
    exitstatus = EXIT_FAILURE;
 
361
    exitstatus = EX_OSERR;
334
362
    goto fallback;
335
363
  }
336
364
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
337
365
  if(ret == -1){
338
366
    perror("sigaction");
339
 
    exitstatus = EXIT_FAILURE;
 
367
    exitstatus = EX_OSERR;
340
368
    goto fallback;
341
369
  }
342
370
  
374
402
      .doc = "Group ID the plugins will run as", .group = 3 },
375
403
    { .name = "debug", .key = 132,
376
404
      .doc = "Debug mode", .group = 4 },
 
405
    /*
 
406
     * These reproduce what we would get without ARGP_NO_HELP
 
407
     */
 
408
    { .name = "help", .key = '?',
 
409
      .doc = "Give this help list", .group = -1 },
 
410
    { .name = "usage", .key = -3,
 
411
      .doc = "Give a short usage message", .group = -1 },
 
412
    { .name = "version", .key = 'V',
 
413
      .doc = "Print program version", .group = -1 },
377
414
    { .name = NULL }
378
415
  };
379
416
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
 
417
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
418
    errno = 0;
382
419
    switch(key){
 
420
      char *tmp;
 
421
      intmax_t tmpmax;
383
422
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
 
423
      {
385
424
        char *plugin_option;
386
425
        while((plugin_option = strsep(&arg, ",")) != NULL){
387
 
          if(plugin_option[0] == '\0'){
388
 
            continue;
389
 
          }
390
426
          if(not add_argument(getplugin(NULL), plugin_option)){
391
 
            perror("add_argument");
392
 
            return ARGP_ERR_UNKNOWN;
 
427
            break;
393
428
          }
394
429
        }
395
430
      }
396
431
      break;
397
432
    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
 
      }
 
433
      add_environment(getplugin(NULL), arg, true);
404
434
      break;
405
435
    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;
 
436
      {
 
437
        char *option_list = strchr(arg, ':');
 
438
        if(option_list == NULL){
 
439
          argp_error(state, "No colon in \"%s\"", arg);
 
440
          errno = EINVAL;
 
441
          break;
 
442
        }
 
443
        *option_list = '\0';
 
444
        option_list++;
 
445
        if(arg[0] == '\0'){
 
446
          argp_error(state, "Empty plugin name");
 
447
          errno = EINVAL;
 
448
          break;
 
449
        }
 
450
        char *option;
 
451
        while((option = strsep(&option_list, ",")) != NULL){
 
452
          if(not add_argument(getplugin(arg), option)){
 
453
            break;
416
454
          }
417
455
        }
418
456
      }
419
457
      break;
420
458
    case 'E':                   /* --env-for */
421
 
      if(arg == NULL){
422
 
        break;
423
 
      }
424
459
      {
425
460
        char *envdef = strchr(arg, ':');
426
461
        if(envdef == NULL){
 
462
          argp_error(state, "No colon in \"%s\"", arg);
 
463
          errno = EINVAL;
427
464
          break;
428
465
        }
429
466
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
431
 
          perror("add_environment");
 
467
        envdef++;
 
468
        if(arg[0] == '\0'){
 
469
          argp_error(state, "Empty plugin name");
 
470
          errno = EINVAL;
 
471
          break;
432
472
        }
 
473
        add_environment(getplugin(arg), envdef, true);
433
474
      }
434
475
      break;
435
476
    case 'd':                   /* --disable */
436
 
      if(arg != NULL){
 
477
      {
437
478
        plugin *p = getplugin(arg);
438
 
        if(p == NULL){
439
 
          return ARGP_ERR_UNKNOWN;
 
479
        if(p != NULL){
 
480
          p->disabled = true;
440
481
        }
441
 
        p->disabled = true;
442
482
      }
443
483
      break;
444
484
    case 'e':                   /* --enable */
445
 
      if(arg != NULL){
 
485
      {
446
486
        plugin *p = getplugin(arg);
447
 
        if(p == NULL){
448
 
          return ARGP_ERR_UNKNOWN;
 
487
        if(p != NULL){
 
488
          p->disabled = false;
449
489
        }
450
 
        p->disabled = false;
451
490
      }
452
491
      break;
453
492
    case 128:                   /* --plugin-dir */
454
493
      free(plugindir);
455
494
      plugindir = strdup(arg);
456
 
      if(plugindir == NULL){
457
 
        perror("strdup");
458
 
      }      
459
495
      break;
460
496
    case 129:                   /* --config-file */
461
497
      /* This is already done by parse_opt_config_file() */
462
498
      break;
463
499
    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;
 
500
      tmpmax = strtoimax(arg, &tmp, 10);
 
501
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
502
         or tmpmax != (uid_t)tmpmax){
 
503
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
504
                   PRIdMAX, arg, (intmax_t)uid);
 
505
        break;
471
506
      }
 
507
      uid = (uid_t)tmpmax;
472
508
      break;
473
509
    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;
 
510
      tmpmax = strtoimax(arg, &tmp, 10);
 
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
512
         or tmpmax != (gid_t)tmpmax){
 
513
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
514
                   PRIdMAX, arg, (intmax_t)gid);
 
515
        break;
481
516
      }
 
517
      gid = (gid_t)tmpmax;
482
518
      break;
483
519
    case 132:                   /* --debug */
484
520
      debug = true;
485
521
      break;
 
522
      /*
 
523
       * These reproduce what we would get without ARGP_NO_HELP
 
524
       */
 
525
    case '?':                   /* --help */
 
526
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
527
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
528
    case -3:                    /* --usage */
 
529
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
530
      argp_state_help(state, state->out_stream,
 
531
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
532
    case 'V':                   /* --version */
 
533
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
534
      exit(EXIT_SUCCESS);
 
535
      break;
486
536
/*
487
537
 * When adding more options before this line, remember to also add a
488
538
 * "case" to the "parse_opt_config_file" function below.
491
541
      /* Cryptsetup always passes an argument, which is an empty
492
542
         string if "none" was specified in /etc/crypttab.  So if
493
543
         argument was empty, we ignore it silently. */
494
 
      if(arg[0] != '\0'){
495
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
544
      if(arg[0] == '\0'){
 
545
        break;
496
546
      }
497
 
      break;
498
 
    case ARGP_KEY_END:
499
 
      break;
500
547
    default:
501
548
      return ARGP_ERR_UNKNOWN;
502
549
    }
503
 
    return 0;
 
550
    return errno;               /* Set to 0 at start */
504
551
  }
505
552
  
506
553
  /* This option parser is the same as parse_opt() above, except it
508
555
  error_t parse_opt_config_file(int key, char *arg,
509
556
                                __attribute__((unused))
510
557
                                struct argp_state *state){
 
558
    errno = 0;
511
559
    switch(key){
512
560
    case 'g':                   /* --global-options */
513
561
    case 'G':                   /* --global-env */
520
568
    case 129:                   /* --config-file */
521
569
      free(argfile);
522
570
      argfile = strdup(arg);
523
 
      if(argfile == NULL){
524
 
        perror("strdup");
525
 
      }
526
 
      break;      
 
571
      break;
527
572
    case 130:                   /* --userid */
528
573
    case 131:                   /* --groupid */
529
574
    case 132:                   /* --debug */
 
575
    case '?':                   /* --help */
 
576
    case -3:                    /* --usage */
 
577
    case 'V':                   /* --version */
530
578
    case ARGP_KEY_ARG:
531
 
    case ARGP_KEY_END:
532
579
      break;
533
580
    default:
534
581
      return ARGP_ERR_UNKNOWN;
535
582
    }
536
 
    return 0;
 
583
    return errno;
537
584
  }
538
585
  
539
586
  struct argp argp = { .options = options,
543
590
  
544
591
  /* Parse using parse_opt_config_file() in order to get the custom
545
592
     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;
 
593
  ret = argp_parse(&argp, argc, argv,
 
594
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
595
                   NULL, NULL);
 
596
  switch(ret){
 
597
  case 0:
 
598
    break;
 
599
  case ENOMEM:
 
600
  default:
 
601
    errno = ret;
 
602
    perror("argp_parse");
 
603
    exitstatus = EX_OSERR;
 
604
    goto fallback;
 
605
  case EINVAL:
 
606
    exitstatus = EX_USAGE;
550
607
    goto fallback;
551
608
  }
552
609
  
558
615
    conffp = fopen(AFILE, "r");
559
616
  } else {
560
617
    conffp = fopen(argfile, "r");
561
 
  }  
 
618
  }
562
619
  if(conffp != NULL){
563
620
    char *org_line = NULL;
564
621
    char *p, *arg, *new_arg, *line;
565
622
    size_t size = 0;
566
623
    const char whitespace_delims[] = " \r\t\f\v\n";
567
624
    const char comment_delim[] = "#";
568
 
 
 
625
    
569
626
    custom_argc = 1;
570
627
    custom_argv = malloc(sizeof(char*) * 2);
571
628
    if(custom_argv == NULL){
572
629
      perror("malloc");
573
 
      exitstatus = EXIT_FAILURE;
 
630
      exitstatus = EX_OSERR;
574
631
      goto fallback;
575
632
    }
576
633
    custom_argv[0] = argv[0];
577
634
    custom_argv[1] = NULL;
578
 
 
 
635
    
579
636
    /* for each line in the config file, strip whitespace and ignore
580
637
       commented text */
581
638
    while(true){
583
640
      if(sret == -1){
584
641
        break;
585
642
      }
586
 
 
 
643
      
587
644
      line = org_line;
588
645
      arg = strsep(&line, comment_delim);
589
646
      while((p = strsep(&arg, whitespace_delims)) != NULL){
593
650
        new_arg = strdup(p);
594
651
        if(new_arg == NULL){
595
652
          perror("strdup");
596
 
          exitstatus = EXIT_FAILURE;
 
653
          exitstatus = EX_OSERR;
597
654
          free(org_line);
598
655
          goto fallback;
599
656
        }
603
660
                              * ((unsigned int) custom_argc + 1));
604
661
        if(custom_argv == NULL){
605
662
          perror("realloc");
606
 
          exitstatus = EXIT_FAILURE;
 
663
          exitstatus = EX_OSERR;
607
664
          free(org_line);
608
665
          goto fallback;
609
666
        }
610
667
        custom_argv[custom_argc-1] = new_arg;
611
 
        custom_argv[custom_argc] = NULL;        
 
668
        custom_argv[custom_argc] = NULL;
612
669
      }
613
670
    }
 
671
    do {
 
672
      ret = fclose(conffp);
 
673
    } while(ret == EOF and errno == EINTR);
 
674
    if(ret == EOF){
 
675
      perror("fclose");
 
676
      exitstatus = EX_IOERR;
 
677
      goto fallback;
 
678
    }
614
679
    free(org_line);
615
680
  } else {
616
681
    /* Check for harmful errors and go to fallback. Other errors might
617
682
       not affect opening plugins */
618
683
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
619
684
      perror("fopen");
620
 
      exitstatus = EXIT_FAILURE;
 
685
      exitstatus = EX_OSERR;
621
686
      goto fallback;
622
687
    }
623
688
  }
624
 
  /* If there was any arguments from configuration file,
625
 
     pass them to parser as command arguments */
 
689
  /* If there were any arguments from the configuration file, pass
 
690
     them to parser as command line arguments */
626
691
  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;
 
692
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
693
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
694
                     NULL, NULL);
 
695
    switch(ret){
 
696
    case 0:
 
697
      break;
 
698
    case ENOMEM:
 
699
    default:
 
700
      errno = ret;
 
701
      perror("argp_parse");
 
702
      exitstatus = EX_OSERR;
 
703
      goto fallback;
 
704
    case EINVAL:
 
705
      exitstatus = EX_CONFIG;
632
706
      goto fallback;
633
707
    }
634
708
  }
635
709
  
636
710
  /* Parse actual command line arguments, to let them override the
637
711
     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;
 
712
  ret = argp_parse(&argp, argc, argv,
 
713
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
714
                   NULL, NULL);
 
715
  switch(ret){
 
716
  case 0:
 
717
    break;
 
718
  case ENOMEM:
 
719
  default:
 
720
    errno = ret;
 
721
    perror("argp_parse");
 
722
    exitstatus = EX_OSERR;
 
723
    goto fallback;
 
724
  case EINVAL:
 
725
    exitstatus = EX_USAGE;
642
726
    goto fallback;
643
727
  }
644
728
  
666
750
    perror("setuid");
667
751
  }
668
752
  
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 */
 
753
  /* Open plugin directory with close_on_exec flag */
682
754
  {
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
 
      }
 
755
    int dir_fd = -1;
 
756
    if(plugindir == NULL){
 
757
      dir_fd = open(PDIR, O_RDONLY |
 
758
#ifdef O_CLOEXEC
 
759
                    O_CLOEXEC
 
760
#else  /* not O_CLOEXEC */
 
761
                    0
 
762
#endif  /* not O_CLOEXEC */
 
763
                    );
 
764
    } else {
 
765
      dir_fd = open(plugindir, O_RDONLY |
 
766
#ifdef O_CLOEXEC
 
767
                    O_CLOEXEC
 
768
#else  /* not O_CLOEXEC */
 
769
                    0
 
770
#endif  /* not O_CLOEXEC */
 
771
                    );
 
772
    }
 
773
    if(dir_fd == -1){
 
774
      perror("Could not open plugin dir");
 
775
      exitstatus = EX_UNAVAILABLE;
 
776
      goto fallback;
 
777
    }
 
778
    
 
779
#ifndef O_CLOEXEC
 
780
  /* Set the FD_CLOEXEC flag on the directory */
 
781
    ret = set_cloexec_flag(dir_fd);
 
782
    if(ret < 0){
 
783
      perror("set_cloexec_flag");
 
784
      TEMP_FAILURE_RETRY(close(dir_fd));
 
785
      exitstatus = EX_OSERR;
 
786
      goto fallback;
 
787
    }
 
788
#endif  /* O_CLOEXEC */
 
789
    
 
790
    dir = fdopendir(dir_fd);
 
791
    if(dir == NULL){
 
792
      perror("Could not open plugin dir");
 
793
      TEMP_FAILURE_RETRY(close(dir_fd));
 
794
      exitstatus = EX_OSERR;
 
795
      goto fallback;
691
796
    }
692
797
  }
693
798
  
695
800
  
696
801
  /* Read and execute any executable in the plugin directory*/
697
802
  while(true){
698
 
    dirst = readdir(dir);
 
803
    do {
 
804
      dirst = readdir(dir);
 
805
    } while(dirst == NULL and errno == EINTR);
699
806
    
700
807
    /* All directory entries have been processed */
701
808
    if(dirst == NULL){
702
809
      if(errno == EBADF){
703
810
        perror("readdir");
704
 
        exitstatus = EXIT_FAILURE;
 
811
        exitstatus = EX_IOERR;
705
812
        goto fallback;
706
813
      }
707
814
      break;
737
844
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
738
845
        size_t suf_len = strlen(*suf);
739
846
        if((d_name_len >= suf_len)
740
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
847
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
741
848
                == 0)){
742
849
          if(debug){
743
850
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
752
859
        continue;
753
860
      }
754
861
    }
755
 
 
 
862
    
756
863
    char *filename;
757
864
    if(plugindir == NULL){
758
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
865
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
866
                                             dirst->d_name));
759
867
    } else {
760
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
868
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
869
                                             plugindir,
 
870
                                             dirst->d_name));
761
871
    }
762
872
    if(ret < 0){
763
873
      perror("asprintf");
764
874
      continue;
765
875
    }
766
876
    
767
 
    ret = stat(filename, &st);
 
877
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
768
878
    if(ret == -1){
769
879
      perror("stat");
770
880
      free(filename);
771
881
      continue;
772
882
    }
773
 
 
 
883
    
774
884
    /* Ignore non-executable files */
775
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
885
    if(not S_ISREG(st.st_mode)
 
886
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
776
887
      if(debug){
777
888
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
889
                " with bad type or mode\n", filename);
824
935
    }
825
936
    
826
937
    int pipefd[2];
827
 
    ret = pipe(pipefd);
 
938
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
828
939
    if(ret == -1){
829
940
      perror("pipe");
830
 
      exitstatus = EXIT_FAILURE;
 
941
      exitstatus = EX_OSERR;
831
942
      goto fallback;
832
943
    }
833
944
    /* Ask OS to automatic close the pipe on exec */
834
945
    ret = set_cloexec_flag(pipefd[0]);
835
946
    if(ret < 0){
836
947
      perror("set_cloexec_flag");
837
 
      exitstatus = EXIT_FAILURE;
 
948
      exitstatus = EX_OSERR;
838
949
      goto fallback;
839
950
    }
840
951
    ret = set_cloexec_flag(pipefd[1]);
841
952
    if(ret < 0){
842
953
      perror("set_cloexec_flag");
843
 
      exitstatus = EXIT_FAILURE;
 
954
      exitstatus = EX_OSERR;
844
955
      goto fallback;
845
956
    }
846
957
    /* Block SIGCHLD until process is safely in process list */
847
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
958
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
959
                                              &sigchld_action.sa_mask,
 
960
                                              NULL));
848
961
    if(ret < 0){
849
962
      perror("sigprocmask");
850
 
      exitstatus = EXIT_FAILURE;
 
963
      exitstatus = EX_OSERR;
851
964
      goto fallback;
852
965
    }
853
966
    /* Starting a new process to be watched */
854
 
    pid_t pid = fork();
 
967
    pid_t pid;
 
968
    do {
 
969
      pid = fork();
 
970
    } while(pid == -1 and errno == EINTR);
855
971
    if(pid == -1){
856
972
      perror("fork");
857
 
      exitstatus = EXIT_FAILURE;
 
973
      exitstatus = EX_OSERR;
858
974
      goto fallback;
859
975
    }
860
976
    if(pid == 0){
862
978
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
863
979
      if(ret < 0){
864
980
        perror("sigaction");
865
 
        _exit(EXIT_FAILURE);
 
981
        _exit(EX_OSERR);
866
982
      }
867
983
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
868
984
      if(ret < 0){
869
985
        perror("sigprocmask");
870
 
        _exit(EXIT_FAILURE);
 
986
        _exit(EX_OSERR);
871
987
      }
872
988
      
873
989
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
874
990
      if(ret == -1){
875
991
        perror("dup2");
876
 
        _exit(EXIT_FAILURE);
 
992
        _exit(EX_OSERR);
877
993
      }
878
994
      
879
995
      if(dirfd(dir) < 0){
884
1000
      if(p->environ[0] == NULL){
885
1001
        if(execv(filename, p->argv) < 0){
886
1002
          perror("execv");
887
 
          _exit(EXIT_FAILURE);
 
1003
          _exit(EX_OSERR);
888
1004
        }
889
1005
      } else {
890
1006
        if(execve(filename, p->argv, p->environ) < 0){
891
1007
          perror("execve");
892
 
          _exit(EXIT_FAILURE);
 
1008
          _exit(EX_OSERR);
893
1009
        }
894
1010
      }
895
1011
      /* no return */
896
1012
    }
897
1013
    /* Parent process */
898
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1014
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1015
                                             pipe */
899
1016
    free(filename);
900
1017
    plugin *new_plugin = getplugin(dirst->d_name);
901
1018
    if(new_plugin == NULL){
902
1019
      perror("getplugin");
903
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1020
      ret = (int)(TEMP_FAILURE_RETRY
 
1021
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1022
                               NULL)));
904
1023
      if(ret < 0){
905
1024
        perror("sigprocmask");
906
1025
      }
907
 
      exitstatus = EXIT_FAILURE;
 
1026
      exitstatus = EX_OSERR;
908
1027
      goto fallback;
909
1028
    }
910
1029
    
913
1032
    
914
1033
    /* Unblock SIGCHLD so signal handler can be run if this process
915
1034
       has already completed */
916
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1035
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1036
                                              &sigchld_action.sa_mask,
 
1037
                                              NULL));
917
1038
    if(ret < 0){
918
1039
      perror("sigprocmask");
919
 
      exitstatus = EXIT_FAILURE;
 
1040
      exitstatus = EX_OSERR;
920
1041
      goto fallback;
921
1042
    }
922
1043
    
923
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1044
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1045
                                          -Wconversion */
924
1046
    
925
1047
    if(maxfd < new_plugin->fd){
926
1048
      maxfd = new_plugin->fd;
927
1049
    }
928
1050
  }
929
1051
  
930
 
  closedir(dir);
 
1052
  TEMP_FAILURE_RETRY(closedir(dir));
931
1053
  dir = NULL;
932
1054
  free_plugin(getplugin(NULL));
933
1055
  
946
1068
  while(plugin_list){
947
1069
    fd_set rfds = rfds_all;
948
1070
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
1071
    if(select_ret == -1 and errno != EINTR){
950
1072
      perror("select");
951
 
      exitstatus = EXIT_FAILURE;
 
1073
      exitstatus = EX_OSERR;
952
1074
      goto fallback;
953
1075
    }
954
1076
    /* OK, now either a process completed, or something can be read
960
1082
        if(not WIFEXITED(proc->status)
961
1083
           or WEXITSTATUS(proc->status) != 0){
962
1084
          /* Bad exit by plugin */
963
 
 
 
1085
          
964
1086
          if(debug){
965
1087
            if(WIFEXITED(proc->status)){
966
1088
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
969
1091
                      WEXITSTATUS(proc->status));
970
1092
            } else if(WIFSIGNALED(proc->status)){
971
1093
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
 
1094
                      " signal %d: %s\n", proc->name,
973
1095
                      (intmax_t) (proc->pid),
974
 
                      WTERMSIG(proc->status));
 
1096
                      WTERMSIG(proc->status),
 
1097
                      strsignal(WTERMSIG(proc->status)));
975
1098
            } else if(WCOREDUMP(proc->status)){
976
1099
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
1100
                      " core\n", proc->name, (intmax_t) (proc->pid));
979
1102
          }
980
1103
          
981
1104
          /* Remove the plugin */
982
 
          FD_CLR(proc->fd, &rfds_all);
983
 
 
 
1105
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1106
                                          -Wconversion */
 
1107
          
984
1108
          /* Block signal while modifying process_list */
985
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1109
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1110
                                        (SIG_BLOCK,
 
1111
                                         &sigchld_action.sa_mask,
 
1112
                                         NULL));
986
1113
          if(ret < 0){
987
1114
            perror("sigprocmask");
988
 
            exitstatus = EXIT_FAILURE;
 
1115
            exitstatus = EX_OSERR;
989
1116
            goto fallback;
990
1117
          }
991
1118
          
994
1121
          proc = next_plugin;
995
1122
          
996
1123
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
1124
          ret = (int)(TEMP_FAILURE_RETRY
 
1125
                      (sigprocmask(SIG_UNBLOCK,
 
1126
                                   &sigchld_action.sa_mask, NULL)));
999
1127
          if(ret < 0){
1000
1128
            perror("sigprocmask");
1001
 
            exitstatus = EXIT_FAILURE;
 
1129
            exitstatus = EX_OSERR;
1002
1130
            goto fallback;
1003
1131
          }
1004
1132
          
1015
1143
                                       proc->buffer_length);
1016
1144
        if(not bret){
1017
1145
          perror("print_out_password");
1018
 
          exitstatus = EXIT_FAILURE;
 
1146
          exitstatus = EX_IOERR;
1019
1147
        }
1020
1148
        goto fallback;
1021
1149
      }
1022
1150
      
1023
1151
      /* This process has not completed.  Does it have any output? */
1024
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1152
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1153
                                                         warning from
 
1154
                                                         -Wconversion */
1025
1155
        /* This process had nothing to say at this time */
1026
1156
        proc = proc->next;
1027
1157
        continue;
1032
1162
                               + (size_t) BUFFER_SIZE);
1033
1163
        if(proc->buffer == NULL){
1034
1164
          perror("malloc");
1035
 
          exitstatus = EXIT_FAILURE;
 
1165
          exitstatus = EX_OSERR;
1036
1166
          goto fallback;
1037
1167
        }
1038
1168
        proc->buffer_size += BUFFER_SIZE;
1039
1169
      }
1040
1170
      /* Read from the process */
1041
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1042
 
                  BUFFER_SIZE);
 
1171
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1172
                                     proc->buffer
 
1173
                                     + proc->buffer_length,
 
1174
                                     BUFFER_SIZE));
1043
1175
      if(sret < 0){
1044
1176
        /* Read error from this process; ignore the error */
1045
1177
        proc = proc->next;
1053
1185
      }
1054
1186
    }
1055
1187
  }
1056
 
 
1057
 
 
 
1188
  
 
1189
  
1058
1190
 fallback:
1059
1191
  
1060
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1192
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1193
                             and exitstatus != EX_OK)){
1061
1194
    /* Fallback if all plugins failed, none are found or an error
1062
1195
       occured */
1063
1196
    bool bret;
1072
1205
    bret = print_out_password(passwordbuffer, len);
1073
1206
    if(not bret){
1074
1207
      perror("print_out_password");
1075
 
      exitstatus = EXIT_FAILURE;
 
1208
      exitstatus = EX_IOERR;
1076
1209
    }
1077
1210
  }
1078
1211
  
1080
1213
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1081
1214
  if(ret == -1){
1082
1215
    perror("sigaction");
1083
 
    exitstatus = EXIT_FAILURE;
 
1216
    exitstatus = EX_OSERR;
1084
1217
  }
1085
1218
  
1086
1219
  if(custom_argv != NULL){
1107
1240
  }
1108
1241
  
1109
1242
  /* Wait for any remaining child processes to terminate */
1110
 
  do{
 
1243
  do {
1111
1244
    ret = wait(NULL);
1112
1245
  } while(ret >= 0);
1113
1246
  if(errno != ECHILD){