/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: Björn Påhlsson
  • Date: 2010-09-07 18:41:35 UTC
  • mfrom: (417 mandos)
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184135-hcd2aou8r2trz8cy
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
#include <stdio.h>              /* 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(),
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
#include <errno.h>              /* errno */
 
74
#include <error.h>              /* error() */
71
75
 
72
76
#define BUFFER_SIZE 256
73
77
 
102
106
/* Gets an existing plugin based on name,
103
107
   or if none is found, creates a new one */
104
108
static plugin *getplugin(char *name){
105
 
  /* Check for exiting plugin with that name */
 
109
  /* Check for existing plugin with that name */
106
110
  for(plugin *p = plugin_list; p != NULL; p = p->next){
107
111
    if((p->name == name)
108
112
       or (p->name and name and (strcmp(p->name, name) == 0))){
123
127
      copy_name = strdup(name);
124
128
    } while(copy_name == NULL and errno == EINTR);
125
129
    if(copy_name == NULL){
 
130
      int e = errno;
126
131
      free(new_plugin);
 
132
      errno = e;
127
133
      return NULL;
128
134
    }
129
135
  }
137
143
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
144
  } while(new_plugin->argv == NULL and errno == EINTR);
139
145
  if(new_plugin->argv == NULL){
 
146
    int e = errno;
140
147
    free(copy_name);
141
148
    free(new_plugin);
 
149
    errno = e;
142
150
    return NULL;
143
151
  }
144
152
  new_plugin->argv[0] = copy_name;
148
156
    new_plugin->environ = malloc(sizeof(char *));
149
157
  } while(new_plugin->environ == NULL and errno == EINTR);
150
158
  if(new_plugin->environ == NULL){
 
159
    int e = errno;
151
160
    free(copy_name);
152
161
    free(new_plugin->argv);
153
162
    free(new_plugin);
 
163
    errno = e;
154
164
    return NULL;
155
165
  }
156
166
  new_plugin->environ[0] = NULL;
230
240
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
231
241
 */
232
242
static int set_cloexec_flag(int fd){
233
 
  int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
243
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
234
244
  /* If reading the flags failed, return error indication now. */
235
245
  if(ret < 0){
236
246
    return ret;
237
247
  }
238
248
  /* Store modified flag word in the descriptor. */
239
 
  return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
 
249
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
250
                                       ret | FD_CLOEXEC));
240
251
}
241
252
 
242
253
 
257
268
        /* No child processes */
258
269
        break;
259
270
      }
260
 
      perror("waitpid");
 
271
      error(0, errno, "waitpid");
261
272
    }
262
273
    
263
274
    /* A child exited, find it in process_list */
348
359
  sigemptyset(&sigchld_action.sa_mask);
349
360
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
350
361
  if(ret == -1){
351
 
    perror("sigaddset");
352
 
    exitstatus = EXIT_FAILURE;
 
362
    error(0, errno, "sigaddset");
 
363
    exitstatus = EX_OSERR;
353
364
    goto fallback;
354
365
  }
355
366
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
356
367
  if(ret == -1){
357
 
    perror("sigaction");
358
 
    exitstatus = EXIT_FAILURE;
 
368
    error(0, errno, "sigaction");
 
369
    exitstatus = EX_OSERR;
359
370
    goto fallback;
360
371
  }
361
372
  
393
404
      .doc = "Group ID the plugins will run as", .group = 3 },
394
405
    { .name = "debug", .key = 132,
395
406
      .doc = "Debug mode", .group = 4 },
 
407
    /*
 
408
     * These reproduce what we would get without ARGP_NO_HELP
 
409
     */
 
410
    { .name = "help", .key = '?',
 
411
      .doc = "Give this help list", .group = -1 },
 
412
    { .name = "usage", .key = -3,
 
413
      .doc = "Give a short usage message", .group = -1 },
 
414
    { .name = "version", .key = 'V',
 
415
      .doc = "Print program version", .group = -1 },
396
416
    { .name = NULL }
397
417
  };
398
418
  
399
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
400
 
                    struct argp_state *state){
 
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
420
    errno = 0;
401
421
    switch(key){
402
422
      char *tmp;
403
423
      intmax_t tmpmax;
404
424
    case 'g':                   /* --global-options */
405
 
      if(arg != NULL){
 
425
      {
406
426
        char *plugin_option;
407
427
        while((plugin_option = strsep(&arg, ",")) != NULL){
408
 
          if(plugin_option[0] == '\0'){
409
 
            continue;
410
 
          }
411
428
          if(not add_argument(getplugin(NULL), plugin_option)){
412
 
            perror("add_argument");
413
 
            return ARGP_ERR_UNKNOWN;
 
429
            break;
414
430
          }
415
431
        }
416
432
      }
417
433
      break;
418
434
    case 'G':                   /* --global-env */
419
 
      if(arg == NULL){
420
 
        break;
421
 
      }
422
 
      if(not add_environment(getplugin(NULL), arg, true)){
423
 
        perror("add_environment");
424
 
      }
 
435
      add_environment(getplugin(NULL), arg, true);
425
436
      break;
426
437
    case 'o':                   /* --options-for */
427
 
      if(arg != NULL){
428
 
        char *plugin_name = strsep(&arg, ":");
429
 
        if(plugin_name[0] == '\0'){
430
 
          break;
431
 
        }
432
 
        char *plugin_option;
433
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
434
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
435
 
            perror("add_argument");
436
 
            return ARGP_ERR_UNKNOWN;
 
438
      {
 
439
        char *option_list = strchr(arg, ':');
 
440
        if(option_list == NULL){
 
441
          argp_error(state, "No colon in \"%s\"", arg);
 
442
          errno = EINVAL;
 
443
          break;
 
444
        }
 
445
        *option_list = '\0';
 
446
        option_list++;
 
447
        if(arg[0] == '\0'){
 
448
          argp_error(state, "Empty plugin name");
 
449
          errno = EINVAL;
 
450
          break;
 
451
        }
 
452
        char *option;
 
453
        while((option = strsep(&option_list, ",")) != NULL){
 
454
          if(not add_argument(getplugin(arg), option)){
 
455
            break;
437
456
          }
438
457
        }
439
458
      }
440
459
      break;
441
460
    case 'E':                   /* --env-for */
442
 
      if(arg == NULL){
443
 
        break;
444
 
      }
445
461
      {
446
462
        char *envdef = strchr(arg, ':');
447
463
        if(envdef == NULL){
 
464
          argp_error(state, "No colon in \"%s\"", arg);
 
465
          errno = EINVAL;
448
466
          break;
449
467
        }
450
468
        *envdef = '\0';
451
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
452
 
          perror("add_environment");
 
469
        envdef++;
 
470
        if(arg[0] == '\0'){
 
471
          argp_error(state, "Empty plugin name");
 
472
          errno = EINVAL;
 
473
          break;
453
474
        }
 
475
        add_environment(getplugin(arg), envdef, true);
454
476
      }
455
477
      break;
456
478
    case 'd':                   /* --disable */
457
 
      if(arg != NULL){
 
479
      {
458
480
        plugin *p = getplugin(arg);
459
 
        if(p == NULL){
460
 
          return ARGP_ERR_UNKNOWN;
 
481
        if(p != NULL){
 
482
          p->disabled = true;
461
483
        }
462
 
        p->disabled = true;
463
484
      }
464
485
      break;
465
486
    case 'e':                   /* --enable */
466
 
      if(arg != NULL){
 
487
      {
467
488
        plugin *p = getplugin(arg);
468
 
        if(p == NULL){
469
 
          return ARGP_ERR_UNKNOWN;
 
489
        if(p != NULL){
 
490
          p->disabled = false;
470
491
        }
471
 
        p->disabled = false;
472
492
      }
473
493
      break;
474
494
    case 128:                   /* --plugin-dir */
475
495
      free(plugindir);
476
496
      plugindir = strdup(arg);
477
 
      if(plugindir == NULL){
478
 
        perror("strdup");
479
 
      }
480
497
      break;
481
498
    case 129:                   /* --config-file */
482
499
      /* This is already done by parse_opt_config_file() */
483
500
      break;
484
501
    case 130:                   /* --userid */
485
 
      errno = 0;
486
502
      tmpmax = strtoimax(arg, &tmp, 10);
487
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
488
504
         or tmpmax != (uid_t)tmpmax){
489
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
490
 
                PRIdMAX "\n", arg, (intmax_t)uid);
491
 
      } else {
492
 
        uid = (uid_t)tmpmax;
 
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
506
                   PRIdMAX, arg, (intmax_t)uid);
 
507
        break;
493
508
      }
 
509
      uid = (uid_t)tmpmax;
494
510
      break;
495
511
    case 131:                   /* --groupid */
496
 
      errno = 0;
497
512
      tmpmax = strtoimax(arg, &tmp, 10);
498
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
499
514
         or tmpmax != (gid_t)tmpmax){
500
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
501
 
                PRIdMAX "\n", arg, (intmax_t)gid);
502
 
      } else {
503
 
        gid = (gid_t)tmpmax;
 
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
516
                   PRIdMAX, arg, (intmax_t)gid);
 
517
        break;
504
518
      }
 
519
      gid = (gid_t)tmpmax;
505
520
      break;
506
521
    case 132:                   /* --debug */
507
522
      debug = true;
508
523
      break;
 
524
      /*
 
525
       * These reproduce what we would get without ARGP_NO_HELP
 
526
       */
 
527
    case '?':                   /* --help */
 
528
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
529
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
530
    case -3:                    /* --usage */
 
531
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
532
      argp_state_help(state, state->out_stream,
 
533
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
534
    case 'V':                   /* --version */
 
535
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
536
      exit(EXIT_SUCCESS);
 
537
      break;
509
538
/*
510
539
 * When adding more options before this line, remember to also add a
511
540
 * "case" to the "parse_opt_config_file" function below.
514
543
      /* Cryptsetup always passes an argument, which is an empty
515
544
         string if "none" was specified in /etc/crypttab.  So if
516
545
         argument was empty, we ignore it silently. */
517
 
      if(arg[0] != '\0'){
518
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
546
      if(arg[0] == '\0'){
 
547
        break;
519
548
      }
520
 
      break;
521
 
    case ARGP_KEY_END:
522
 
      break;
523
549
    default:
524
550
      return ARGP_ERR_UNKNOWN;
525
551
    }
526
 
    return 0;
 
552
    return errno;               /* Set to 0 at start */
527
553
  }
528
554
  
529
555
  /* This option parser is the same as parse_opt() above, except it
531
557
  error_t parse_opt_config_file(int key, char *arg,
532
558
                                __attribute__((unused))
533
559
                                struct argp_state *state){
 
560
    errno = 0;
534
561
    switch(key){
535
562
    case 'g':                   /* --global-options */
536
563
    case 'G':                   /* --global-env */
543
570
    case 129:                   /* --config-file */
544
571
      free(argfile);
545
572
      argfile = strdup(arg);
546
 
      if(argfile == NULL){
547
 
        perror("strdup");
548
 
      }
549
573
      break;
550
574
    case 130:                   /* --userid */
551
575
    case 131:                   /* --groupid */
552
576
    case 132:                   /* --debug */
 
577
    case '?':                   /* --help */
 
578
    case -3:                    /* --usage */
 
579
    case 'V':                   /* --version */
553
580
    case ARGP_KEY_ARG:
554
 
    case ARGP_KEY_END:
555
581
      break;
556
582
    default:
557
583
      return ARGP_ERR_UNKNOWN;
558
584
    }
559
 
    return 0;
 
585
    return errno;
560
586
  }
561
587
  
562
588
  struct argp argp = { .options = options,
566
592
  
567
593
  /* Parse using parse_opt_config_file() in order to get the custom
568
594
     config file location, if any. */
569
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
570
 
  if(ret == ARGP_ERR_UNKNOWN){
571
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
572
 
    exitstatus = EXIT_FAILURE;
 
595
  ret = argp_parse(&argp, argc, argv,
 
596
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
597
                   NULL, NULL);
 
598
  switch(ret){
 
599
  case 0:
 
600
    break;
 
601
  case ENOMEM:
 
602
  default:
 
603
    errno = ret;
 
604
    error(0, errno, "argp_parse");
 
605
    exitstatus = EX_OSERR;
 
606
    goto fallback;
 
607
  case EINVAL:
 
608
    exitstatus = EX_USAGE;
573
609
    goto fallback;
574
610
  }
575
611
  
592
628
    custom_argc = 1;
593
629
    custom_argv = malloc(sizeof(char*) * 2);
594
630
    if(custom_argv == NULL){
595
 
      perror("malloc");
596
 
      exitstatus = EXIT_FAILURE;
 
631
      error(0, errno, "malloc");
 
632
      exitstatus = EX_OSERR;
597
633
      goto fallback;
598
634
    }
599
635
    custom_argv[0] = argv[0];
615
651
        }
616
652
        new_arg = strdup(p);
617
653
        if(new_arg == NULL){
618
 
          perror("strdup");
619
 
          exitstatus = EXIT_FAILURE;
 
654
          error(0, errno, "strdup");
 
655
          exitstatus = EX_OSERR;
620
656
          free(org_line);
621
657
          goto fallback;
622
658
        }
625
661
        custom_argv = realloc(custom_argv, sizeof(char *)
626
662
                              * ((unsigned int) custom_argc + 1));
627
663
        if(custom_argv == NULL){
628
 
          perror("realloc");
629
 
          exitstatus = EXIT_FAILURE;
 
664
          error(0, errno, "realloc");
 
665
          exitstatus = EX_OSERR;
630
666
          free(org_line);
631
667
          goto fallback;
632
668
        }
638
674
      ret = fclose(conffp);
639
675
    } while(ret == EOF and errno == EINTR);
640
676
    if(ret == EOF){
641
 
      perror("fclose");
642
 
      exitstatus = EXIT_FAILURE;
 
677
      error(0, errno, "fclose");
 
678
      exitstatus = EX_IOERR;
643
679
      goto fallback;
644
680
    }
645
681
    free(org_line);
647
683
    /* Check for harmful errors and go to fallback. Other errors might
648
684
       not affect opening plugins */
649
685
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
650
 
      perror("fopen");
651
 
      exitstatus = EXIT_FAILURE;
 
686
      error(0, errno, "fopen");
 
687
      exitstatus = EX_OSERR;
652
688
      goto fallback;
653
689
    }
654
690
  }
655
 
  /* If there was any arguments from configuration file,
656
 
     pass them to parser as command arguments */
 
691
  /* If there were any arguments from the configuration file, pass
 
692
     them to parser as command line arguments */
657
693
  if(custom_argv != NULL){
658
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
659
 
                     0, NULL);
660
 
    if(ret == ARGP_ERR_UNKNOWN){
661
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
662
 
      exitstatus = EXIT_FAILURE;
 
694
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
695
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
696
                     NULL, NULL);
 
697
    switch(ret){
 
698
    case 0:
 
699
      break;
 
700
    case ENOMEM:
 
701
    default:
 
702
      errno = ret;
 
703
      error(0, errno, "argp_parse");
 
704
      exitstatus = EX_OSERR;
 
705
      goto fallback;
 
706
    case EINVAL:
 
707
      exitstatus = EX_CONFIG;
663
708
      goto fallback;
664
709
    }
665
710
  }
666
711
  
667
712
  /* Parse actual command line arguments, to let them override the
668
713
     config file */
669
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
670
 
  if(ret == ARGP_ERR_UNKNOWN){
671
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
672
 
    exitstatus = EXIT_FAILURE;
 
714
  ret = argp_parse(&argp, argc, argv,
 
715
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
716
                   NULL, NULL);
 
717
  switch(ret){
 
718
  case 0:
 
719
    break;
 
720
  case ENOMEM:
 
721
  default:
 
722
    errno = ret;
 
723
    error(0, errno, "argp_parse");
 
724
    exitstatus = EX_OSERR;
 
725
    goto fallback;
 
726
  case EINVAL:
 
727
    exitstatus = EX_USAGE;
673
728
    goto fallback;
674
729
  }
675
730
  
690
745
  /* Strip permissions down to nobody */
691
746
  setgid(gid);
692
747
  if(ret == -1){
693
 
    perror("setgid");
 
748
    error(0, errno, "setgid");
694
749
  }
695
750
  ret = setuid(uid);
696
751
  if(ret == -1){
697
 
    perror("setuid");
698
 
  }
699
 
  
700
 
  if(plugindir == NULL){
701
 
    dir = opendir(PDIR);
702
 
  } else {
703
 
    dir = opendir(plugindir);
704
 
  }
705
 
  
706
 
  if(dir == NULL){
707
 
    perror("Could not open plugin dir");
708
 
    exitstatus = EXIT_FAILURE;
709
 
    goto fallback;
710
 
  }
711
 
  
712
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
752
    error(0, errno, "setuid");
 
753
  }
 
754
  
 
755
  /* Open plugin directory with close_on_exec flag */
713
756
  {
714
 
    int dir_fd = dirfd(dir);
715
 
    if(dir_fd >= 0){
716
 
      ret = set_cloexec_flag(dir_fd);
717
 
      if(ret < 0){
718
 
        perror("set_cloexec_flag");
719
 
        exitstatus = EXIT_FAILURE;
720
 
        goto fallback;
721
 
      }
 
757
    int dir_fd = -1;
 
758
    if(plugindir == NULL){
 
759
      dir_fd = open(PDIR, O_RDONLY |
 
760
#ifdef O_CLOEXEC
 
761
                    O_CLOEXEC
 
762
#else  /* not O_CLOEXEC */
 
763
                    0
 
764
#endif  /* not O_CLOEXEC */
 
765
                    );
 
766
    } else {
 
767
      dir_fd = open(plugindir, O_RDONLY |
 
768
#ifdef O_CLOEXEC
 
769
                    O_CLOEXEC
 
770
#else  /* not O_CLOEXEC */
 
771
                    0
 
772
#endif  /* not O_CLOEXEC */
 
773
                    );
 
774
    }
 
775
    if(dir_fd == -1){
 
776
      error(0, errno, "Could not open plugin dir");
 
777
      exitstatus = EX_UNAVAILABLE;
 
778
      goto fallback;
 
779
    }
 
780
    
 
781
#ifndef O_CLOEXEC
 
782
  /* Set the FD_CLOEXEC flag on the directory */
 
783
    ret = set_cloexec_flag(dir_fd);
 
784
    if(ret < 0){
 
785
      error(0, errno, "set_cloexec_flag");
 
786
      TEMP_FAILURE_RETRY(close(dir_fd));
 
787
      exitstatus = EX_OSERR;
 
788
      goto fallback;
 
789
    }
 
790
#endif  /* O_CLOEXEC */
 
791
    
 
792
    dir = fdopendir(dir_fd);
 
793
    if(dir == NULL){
 
794
      error(0, errno, "Could not open plugin dir");
 
795
      TEMP_FAILURE_RETRY(close(dir_fd));
 
796
      exitstatus = EX_OSERR;
 
797
      goto fallback;
722
798
    }
723
799
  }
724
800
  
733
809
    /* All directory entries have been processed */
734
810
    if(dirst == NULL){
735
811
      if(errno == EBADF){
736
 
        perror("readdir");
737
 
        exitstatus = EXIT_FAILURE;
 
812
        error(0, errno, "readdir");
 
813
        exitstatus = EX_IOERR;
738
814
        goto fallback;
739
815
      }
740
816
      break;
788
864
    
789
865
    char *filename;
790
866
    if(plugindir == NULL){
791
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
792
 
                                        dirst->d_name));
 
867
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
868
                                             dirst->d_name));
793
869
    } else {
794
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
795
 
                                        dirst->d_name));
 
870
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
871
                                             plugindir,
 
872
                                             dirst->d_name));
796
873
    }
797
874
    if(ret < 0){
798
 
      perror("asprintf");
 
875
      error(0, errno, "asprintf");
799
876
      continue;
800
877
    }
801
878
    
802
 
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
 
879
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
803
880
    if(ret == -1){
804
 
      perror("stat");
 
881
      error(0, errno, "stat");
805
882
      free(filename);
806
883
      continue;
807
884
    }
819
896
    
820
897
    plugin *p = getplugin(dirst->d_name);
821
898
    if(p == NULL){
822
 
      perror("getplugin");
 
899
      error(0, errno, "getplugin");
823
900
      free(filename);
824
901
      continue;
825
902
    }
837
914
      if(g != NULL){
838
915
        for(char **a = g->argv + 1; *a != NULL; a++){
839
916
          if(not add_argument(p, *a)){
840
 
            perror("add_argument");
 
917
            error(0, errno, "add_argument");
841
918
          }
842
919
        }
843
920
        /* Add global environment variables */
844
921
        for(char **e = g->environ; *e != NULL; e++){
845
922
          if(not add_environment(p, *e, false)){
846
 
            perror("add_environment");
 
923
            error(0, errno, "add_environment");
847
924
          }
848
925
        }
849
926
      }
854
931
    if(p->environ[0] != NULL){
855
932
      for(char **e = environ; *e != NULL; e++){
856
933
        if(not add_environment(p, *e, false)){
857
 
          perror("add_environment");
 
934
          error(0, errno, "add_environment");
858
935
        }
859
936
      }
860
937
    }
861
938
    
862
939
    int pipefd[2];
863
 
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
 
940
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
864
941
    if(ret == -1){
865
 
      perror("pipe");
866
 
      exitstatus = EXIT_FAILURE;
 
942
      error(0, errno, "pipe");
 
943
      exitstatus = EX_OSERR;
867
944
      goto fallback;
868
945
    }
869
946
    /* Ask OS to automatic close the pipe on exec */
870
947
    ret = set_cloexec_flag(pipefd[0]);
871
948
    if(ret < 0){
872
 
      perror("set_cloexec_flag");
873
 
      exitstatus = EXIT_FAILURE;
 
949
      error(0, errno, "set_cloexec_flag");
 
950
      exitstatus = EX_OSERR;
874
951
      goto fallback;
875
952
    }
876
953
    ret = set_cloexec_flag(pipefd[1]);
877
954
    if(ret < 0){
878
 
      perror("set_cloexec_flag");
879
 
      exitstatus = EXIT_FAILURE;
 
955
      error(0, errno, "set_cloexec_flag");
 
956
      exitstatus = EX_OSERR;
880
957
      goto fallback;
881
958
    }
882
959
    /* Block SIGCHLD until process is safely in process list */
883
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
884
 
                                         &sigchld_action.sa_mask,
885
 
                                         NULL));
 
960
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
961
                                              &sigchld_action.sa_mask,
 
962
                                              NULL));
886
963
    if(ret < 0){
887
 
      perror("sigprocmask");
888
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "sigprocmask");
 
965
      exitstatus = EX_OSERR;
889
966
      goto fallback;
890
967
    }
891
968
    /* Starting a new process to be watched */
894
971
      pid = fork();
895
972
    } while(pid == -1 and errno == EINTR);
896
973
    if(pid == -1){
897
 
      perror("fork");
898
 
      exitstatus = EXIT_FAILURE;
 
974
      error(0, errno, "fork");
 
975
      exitstatus = EX_OSERR;
899
976
      goto fallback;
900
977
    }
901
978
    if(pid == 0){
902
979
      /* this is the child process */
903
980
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
904
981
      if(ret < 0){
905
 
        perror("sigaction");
906
 
        _exit(EXIT_FAILURE);
 
982
        error(0, errno, "sigaction");
 
983
        _exit(EX_OSERR);
907
984
      }
908
985
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
909
986
      if(ret < 0){
910
 
        perror("sigprocmask");
911
 
        _exit(EXIT_FAILURE);
 
987
        error(0, errno, "sigprocmask");
 
988
        _exit(EX_OSERR);
912
989
      }
913
990
      
914
991
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
915
992
      if(ret == -1){
916
 
        perror("dup2");
917
 
        _exit(EXIT_FAILURE);
 
993
        error(0, errno, "dup2");
 
994
        _exit(EX_OSERR);
918
995
      }
919
996
      
920
997
      if(dirfd(dir) < 0){
924
1001
      }
925
1002
      if(p->environ[0] == NULL){
926
1003
        if(execv(filename, p->argv) < 0){
927
 
          perror("execv");
928
 
          _exit(EXIT_FAILURE);
 
1004
          error(0, errno, "execv");
 
1005
          _exit(EX_OSERR);
929
1006
        }
930
1007
      } else {
931
1008
        if(execve(filename, p->argv, p->environ) < 0){
932
 
          perror("execve");
933
 
          _exit(EXIT_FAILURE);
 
1009
          error(0, errno, "execve");
 
1010
          _exit(EX_OSERR);
934
1011
        }
935
1012
      }
936
1013
      /* no return */
941
1018
    free(filename);
942
1019
    plugin *new_plugin = getplugin(dirst->d_name);
943
1020
    if(new_plugin == NULL){
944
 
      perror("getplugin");
945
 
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
946
 
                                           &sigchld_action.sa_mask,
947
 
                                           NULL));
 
1021
      error(0, errno, "getplugin");
 
1022
      ret = (int)(TEMP_FAILURE_RETRY
 
1023
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1024
                               NULL)));
948
1025
      if(ret < 0){
949
 
        perror("sigprocmask");
 
1026
        error(0, errno, "sigprocmask");
950
1027
      }
951
 
      exitstatus = EXIT_FAILURE;
 
1028
      exitstatus = EX_OSERR;
952
1029
      goto fallback;
953
1030
    }
954
1031
    
957
1034
    
958
1035
    /* Unblock SIGCHLD so signal handler can be run if this process
959
1036
       has already completed */
960
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
961
 
                                         &sigchld_action.sa_mask,
962
 
                                         NULL));
 
1037
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1038
                                              &sigchld_action.sa_mask,
 
1039
                                              NULL));
963
1040
    if(ret < 0){
964
 
      perror("sigprocmask");
965
 
      exitstatus = EXIT_FAILURE;
 
1041
      error(0, errno, "sigprocmask");
 
1042
      exitstatus = EX_OSERR;
966
1043
      goto fallback;
967
1044
    }
968
1045
    
969
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1046
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1047
                                          -Wconversion */
970
1048
    
971
1049
    if(maxfd < new_plugin->fd){
972
1050
      maxfd = new_plugin->fd;
993
1071
    fd_set rfds = rfds_all;
994
1072
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
995
1073
    if(select_ret == -1 and errno != EINTR){
996
 
      perror("select");
997
 
      exitstatus = EXIT_FAILURE;
 
1074
      error(0, errno, "select");
 
1075
      exitstatus = EX_OSERR;
998
1076
      goto fallback;
999
1077
    }
1000
1078
    /* OK, now either a process completed, or something can be read
1026
1104
          }
1027
1105
          
1028
1106
          /* Remove the plugin */
1029
 
          FD_CLR(proc->fd, &rfds_all);
 
1107
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1108
                                          -Wconversion */
1030
1109
          
1031
1110
          /* Block signal while modifying process_list */
1032
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1033
 
                                               &sigchld_action.sa_mask,
1034
 
                                               NULL));
 
1111
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1112
                                        (SIG_BLOCK,
 
1113
                                         &sigchld_action.sa_mask,
 
1114
                                         NULL));
1035
1115
          if(ret < 0){
1036
 
            perror("sigprocmask");
1037
 
            exitstatus = EXIT_FAILURE;
 
1116
            error(0, errno, "sigprocmask");
 
1117
            exitstatus = EX_OSERR;
1038
1118
            goto fallback;
1039
1119
          }
1040
1120
          
1043
1123
          proc = next_plugin;
1044
1124
          
1045
1125
          /* We are done modifying process list, so unblock signal */
1046
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1047
 
                                               &sigchld_action.sa_mask,
1048
 
                                               NULL));
 
1126
          ret = (int)(TEMP_FAILURE_RETRY
 
1127
                      (sigprocmask(SIG_UNBLOCK,
 
1128
                                   &sigchld_action.sa_mask, NULL)));
1049
1129
          if(ret < 0){
1050
 
            perror("sigprocmask");
1051
 
            exitstatus = EXIT_FAILURE;
 
1130
            error(0, errno, "sigprocmask");
 
1131
            exitstatus = EX_OSERR;
1052
1132
            goto fallback;
1053
1133
          }
1054
1134
          
1064
1144
        bool bret = print_out_password(proc->buffer,
1065
1145
                                       proc->buffer_length);
1066
1146
        if(not bret){
1067
 
          perror("print_out_password");
1068
 
          exitstatus = EXIT_FAILURE;
 
1147
          error(0, errno, "print_out_password");
 
1148
          exitstatus = EX_IOERR;
1069
1149
        }
1070
1150
        goto fallback;
1071
1151
      }
1072
1152
      
1073
1153
      /* This process has not completed.  Does it have any output? */
1074
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1154
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1155
                                                         warning from
 
1156
                                                         -Wconversion */
1075
1157
        /* This process had nothing to say at this time */
1076
1158
        proc = proc->next;
1077
1159
        continue;
1081
1163
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1082
1164
                               + (size_t) BUFFER_SIZE);
1083
1165
        if(proc->buffer == NULL){
1084
 
          perror("malloc");
1085
 
          exitstatus = EXIT_FAILURE;
 
1166
          error(0, errno, "malloc");
 
1167
          exitstatus = EX_OSERR;
1086
1168
          goto fallback;
1087
1169
        }
1088
1170
        proc->buffer_size += BUFFER_SIZE;
1109
1191
  
1110
1192
 fallback:
1111
1193
  
1112
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1194
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1195
                             and exitstatus != EX_OK)){
1113
1196
    /* Fallback if all plugins failed, none are found or an error
1114
1197
       occured */
1115
1198
    bool bret;
1123
1206
    }
1124
1207
    bret = print_out_password(passwordbuffer, len);
1125
1208
    if(not bret){
1126
 
      perror("print_out_password");
1127
 
      exitstatus = EXIT_FAILURE;
 
1209
      error(0, errno, "print_out_password");
 
1210
      exitstatus = EX_IOERR;
1128
1211
    }
1129
1212
  }
1130
1213
  
1131
1214
  /* Restore old signal handler */
1132
1215
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1133
1216
  if(ret == -1){
1134
 
    perror("sigaction");
1135
 
    exitstatus = EXIT_FAILURE;
 
1217
    error(0, errno, "sigaction");
 
1218
    exitstatus = EX_OSERR;
1136
1219
  }
1137
1220
  
1138
1221
  if(custom_argv != NULL){
1153
1236
      ret = kill(p->pid, SIGTERM);
1154
1237
      if(ret == -1 and errno != ESRCH){
1155
1238
        /* Set-uid proccesses might not get closed */
1156
 
        perror("kill");
 
1239
        error(0, errno, "kill");
1157
1240
      }
1158
1241
    }
1159
1242
  }
1163
1246
    ret = wait(NULL);
1164
1247
  } while(ret >= 0);
1165
1248
  if(errno != ECHILD){
1166
 
    perror("wait");
 
1249
    error(0, errno, "wait");
1167
1250
  }
1168
1251
  
1169
1252
  free_plugin_list();