/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

  • Committer: teddy at bsnet
  • Date: 2010-09-02 19:07:20 UTC
  • mto: (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 270.
  • Revision ID: teddy@fukt.bsnet.se-20100902190720-sp0pitu30isx4pvi
* mandos (ClientDBus.approvals_pending): Bug fix: Only send D-Bus
                                         signals if connected to the
                                         D-Bus.

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