/mandos/trunk

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2009-09-04 16:32:22 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090904163222-l9wp25ng1e5ym0yq
* plugin-runner.c (main): When a plugin is killed by a signal, show
                          the signal name, not just the number, in the
                          debug log message.

* plugins.d/password-prompt.c (termination_handler): Store received
                                                     signal in
                                                     "signal_received".
  (main): If exiting due to signal, re-raise signal received instead
          of returning.

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