/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

Change "fukt.bsnet.se" to "recompile.se" throughout.

* README: - '' -
* debian/control: - '' -
* debian/copyright: - '' -
* debian/mandos-client.README.Debian: - '' - and some rewriting.
* debian/mandos.README.Debian: - '' -
* debian/watch: Change "fukt.bsnet.se" to "recompile.se".
* init.d-mandos: - '' -
* intro.xml: - '' -
* mandos: - '' -
* mandos-clients.conf.xml: - '' -
* mandos-ctl: - '' -
* mandos-ctl.xml: - '' -
* mandos-keygen: - '' -
* mandos-keygen.xml: - '' -
* mandos-monitor: - '' -
* mandos-monitor.xml: - '' -
* mandos.conf.xml: - '' -
* mandos.lsm: - '' -
* mandos.xml: - '' -
* plugin-runner.c: - '' -
* plugin-runner.xml: - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/askpass-fifo.xml: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/mandos-client.xml: - '' -
* plugins.d/password-prompt.c: - '' -
* plugins.d/password-prompt.xml: - '' -
* plugins.d/plymouth.c: - '' -
* plugins.d/plymouth.xml: - '' -
* plugins.d/splashy.c: - '' -
* plugins.d/splashy.xml: - '' -
* plugins.d/usplash.c: - '' -
* plugins.d/usplash.xml: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
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
 
75
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
76
80
 
77
81
const char *argp_program_version = "plugin-runner " VERSION;
78
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.se>";
79
83
 
80
84
typedef struct plugin{
81
85
  char *name;                   /* can be NULL or any plugin name */
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;
258
268
        /* No child processes */
259
269
        break;
260
270
      }
261
 
      perror("waitpid");
 
271
      error(0, errno, "waitpid");
262
272
    }
263
273
    
264
274
    /* A child exited, find it in process_list */
349
359
  sigemptyset(&sigchld_action.sa_mask);
350
360
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
351
361
  if(ret == -1){
352
 
    perror("sigaddset");
353
 
    exitstatus = EXIT_FAILURE;
 
362
    error(0, errno, "sigaddset");
 
363
    exitstatus = EX_OSERR;
354
364
    goto fallback;
355
365
  }
356
366
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
357
367
  if(ret == -1){
358
 
    perror("sigaction");
359
 
    exitstatus = EXIT_FAILURE;
 
368
    error(0, errno, "sigaction");
 
369
    exitstatus = EX_OSERR;
360
370
    goto fallback;
361
371
  }
362
372
  
394
404
      .doc = "Group ID the plugins will run as", .group = 3 },
395
405
    { .name = "debug", .key = 132,
396
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 },
397
416
    { .name = NULL }
398
417
  };
399
418
  
400
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
401
 
                    struct argp_state *state){
 
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
420
    errno = 0;
402
421
    switch(key){
403
422
      char *tmp;
404
423
      intmax_t tmpmax;
405
424
    case 'g':                   /* --global-options */
406
 
      if(arg != NULL){
 
425
      {
407
426
        char *plugin_option;
408
427
        while((plugin_option = strsep(&arg, ",")) != NULL){
409
 
          if(plugin_option[0] == '\0'){
410
 
            continue;
411
 
          }
412
428
          if(not add_argument(getplugin(NULL), plugin_option)){
413
 
            perror("add_argument");
414
 
            return ARGP_ERR_UNKNOWN;
 
429
            break;
415
430
          }
416
431
        }
417
432
      }
418
433
      break;
419
434
    case 'G':                   /* --global-env */
420
 
      if(arg == NULL){
421
 
        break;
422
 
      }
423
 
      if(not add_environment(getplugin(NULL), arg, true)){
424
 
        perror("add_environment");
425
 
      }
 
435
      add_environment(getplugin(NULL), arg, true);
426
436
      break;
427
437
    case 'o':                   /* --options-for */
428
 
      if(arg != NULL){
429
 
        char *plugin_name = strsep(&arg, ":");
430
 
        if(plugin_name[0] == '\0'){
431
 
          break;
432
 
        }
433
 
        char *plugin_option;
434
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
435
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
436
 
            perror("add_argument");
437
 
            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;
438
456
          }
439
457
        }
440
458
      }
441
459
      break;
442
460
    case 'E':                   /* --env-for */
443
 
      if(arg == NULL){
444
 
        break;
445
 
      }
446
461
      {
447
462
        char *envdef = strchr(arg, ':');
448
463
        if(envdef == NULL){
 
464
          argp_error(state, "No colon in \"%s\"", arg);
 
465
          errno = EINVAL;
449
466
          break;
450
467
        }
451
468
        *envdef = '\0';
452
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
453
 
          perror("add_environment");
 
469
        envdef++;
 
470
        if(arg[0] == '\0'){
 
471
          argp_error(state, "Empty plugin name");
 
472
          errno = EINVAL;
 
473
          break;
454
474
        }
 
475
        add_environment(getplugin(arg), envdef, true);
455
476
      }
456
477
      break;
457
478
    case 'd':                   /* --disable */
458
 
      if(arg != NULL){
 
479
      {
459
480
        plugin *p = getplugin(arg);
460
 
        if(p == NULL){
461
 
          return ARGP_ERR_UNKNOWN;
 
481
        if(p != NULL){
 
482
          p->disabled = true;
462
483
        }
463
 
        p->disabled = true;
464
484
      }
465
485
      break;
466
486
    case 'e':                   /* --enable */
467
 
      if(arg != NULL){
 
487
      {
468
488
        plugin *p = getplugin(arg);
469
 
        if(p == NULL){
470
 
          return ARGP_ERR_UNKNOWN;
 
489
        if(p != NULL){
 
490
          p->disabled = false;
471
491
        }
472
 
        p->disabled = false;
473
492
      }
474
493
      break;
475
494
    case 128:                   /* --plugin-dir */
476
495
      free(plugindir);
477
496
      plugindir = strdup(arg);
478
 
      if(plugindir == NULL){
479
 
        perror("strdup");
480
 
      }
481
497
      break;
482
498
    case 129:                   /* --config-file */
483
499
      /* This is already done by parse_opt_config_file() */
484
500
      break;
485
501
    case 130:                   /* --userid */
486
 
      errno = 0;
487
502
      tmpmax = strtoimax(arg, &tmp, 10);
488
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
489
504
         or tmpmax != (uid_t)tmpmax){
490
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
491
 
                PRIdMAX "\n", arg, (intmax_t)uid);
492
 
      } else {
493
 
        uid = (uid_t)tmpmax;
 
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
506
                   PRIdMAX, arg, (intmax_t)uid);
 
507
        break;
494
508
      }
 
509
      uid = (uid_t)tmpmax;
495
510
      break;
496
511
    case 131:                   /* --groupid */
497
 
      errno = 0;
498
512
      tmpmax = strtoimax(arg, &tmp, 10);
499
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
500
514
         or tmpmax != (gid_t)tmpmax){
501
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
502
 
                PRIdMAX "\n", arg, (intmax_t)gid);
503
 
      } else {
504
 
        gid = (gid_t)tmpmax;
 
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
516
                   PRIdMAX, arg, (intmax_t)gid);
 
517
        break;
505
518
      }
 
519
      gid = (gid_t)tmpmax;
506
520
      break;
507
521
    case 132:                   /* --debug */
508
522
      debug = true;
509
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;
510
538
/*
511
539
 * When adding more options before this line, remember to also add a
512
540
 * "case" to the "parse_opt_config_file" function below.
515
543
      /* Cryptsetup always passes an argument, which is an empty
516
544
         string if "none" was specified in /etc/crypttab.  So if
517
545
         argument was empty, we ignore it silently. */
518
 
      if(arg[0] != '\0'){
519
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
546
      if(arg[0] == '\0'){
 
547
        break;
520
548
      }
521
 
      break;
522
 
    case ARGP_KEY_END:
523
 
      break;
524
549
    default:
525
550
      return ARGP_ERR_UNKNOWN;
526
551
    }
527
 
    return 0;
 
552
    return errno;               /* Set to 0 at start */
528
553
  }
529
554
  
530
555
  /* This option parser is the same as parse_opt() above, except it
532
557
  error_t parse_opt_config_file(int key, char *arg,
533
558
                                __attribute__((unused))
534
559
                                struct argp_state *state){
 
560
    errno = 0;
535
561
    switch(key){
536
562
    case 'g':                   /* --global-options */
537
563
    case 'G':                   /* --global-env */
544
570
    case 129:                   /* --config-file */
545
571
      free(argfile);
546
572
      argfile = strdup(arg);
547
 
      if(argfile == NULL){
548
 
        perror("strdup");
549
 
      }
550
573
      break;
551
574
    case 130:                   /* --userid */
552
575
    case 131:                   /* --groupid */
553
576
    case 132:                   /* --debug */
 
577
    case '?':                   /* --help */
 
578
    case -3:                    /* --usage */
 
579
    case 'V':                   /* --version */
554
580
    case ARGP_KEY_ARG:
555
 
    case ARGP_KEY_END:
556
581
      break;
557
582
    default:
558
583
      return ARGP_ERR_UNKNOWN;
559
584
    }
560
 
    return 0;
 
585
    return errno;
561
586
  }
562
587
  
563
588
  struct argp argp = { .options = options,
567
592
  
568
593
  /* Parse using parse_opt_config_file() in order to get the custom
569
594
     config file location, if any. */
570
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
571
 
  if(ret == ARGP_ERR_UNKNOWN){
572
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
573
 
    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;
574
609
    goto fallback;
575
610
  }
576
611
  
593
628
    custom_argc = 1;
594
629
    custom_argv = malloc(sizeof(char*) * 2);
595
630
    if(custom_argv == NULL){
596
 
      perror("malloc");
597
 
      exitstatus = EXIT_FAILURE;
 
631
      error(0, errno, "malloc");
 
632
      exitstatus = EX_OSERR;
598
633
      goto fallback;
599
634
    }
600
635
    custom_argv[0] = argv[0];
616
651
        }
617
652
        new_arg = strdup(p);
618
653
        if(new_arg == NULL){
619
 
          perror("strdup");
620
 
          exitstatus = EXIT_FAILURE;
 
654
          error(0, errno, "strdup");
 
655
          exitstatus = EX_OSERR;
621
656
          free(org_line);
622
657
          goto fallback;
623
658
        }
626
661
        custom_argv = realloc(custom_argv, sizeof(char *)
627
662
                              * ((unsigned int) custom_argc + 1));
628
663
        if(custom_argv == NULL){
629
 
          perror("realloc");
630
 
          exitstatus = EXIT_FAILURE;
 
664
          error(0, errno, "realloc");
 
665
          exitstatus = EX_OSERR;
631
666
          free(org_line);
632
667
          goto fallback;
633
668
        }
639
674
      ret = fclose(conffp);
640
675
    } while(ret == EOF and errno == EINTR);
641
676
    if(ret == EOF){
642
 
      perror("fclose");
643
 
      exitstatus = EXIT_FAILURE;
 
677
      error(0, errno, "fclose");
 
678
      exitstatus = EX_IOERR;
644
679
      goto fallback;
645
680
    }
646
681
    free(org_line);
648
683
    /* Check for harmful errors and go to fallback. Other errors might
649
684
       not affect opening plugins */
650
685
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
651
 
      perror("fopen");
652
 
      exitstatus = EXIT_FAILURE;
 
686
      error(0, errno, "fopen");
 
687
      exitstatus = EX_OSERR;
653
688
      goto fallback;
654
689
    }
655
690
  }
656
 
  /* If there was any arguments from configuration file,
657
 
     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 */
658
693
  if(custom_argv != NULL){
659
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
660
 
                     0, NULL);
661
 
    if(ret == ARGP_ERR_UNKNOWN){
662
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
663
 
      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;
664
708
      goto fallback;
665
709
    }
666
710
  }
667
711
  
668
712
  /* Parse actual command line arguments, to let them override the
669
713
     config file */
670
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
671
 
  if(ret == ARGP_ERR_UNKNOWN){
672
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
673
 
    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;
674
728
    goto fallback;
675
729
  }
676
730
  
688
742
    }
689
743
  }
690
744
  
691
 
  /* Strip permissions down to nobody */
 
745
  {
 
746
    /* Work around Debian bug #633582:
 
747
       <http://bugs.debian.org/633582> */
 
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
749
    if(plugindir_fd == -1){
 
750
      error(0, errno, "open");
 
751
    } else {
 
752
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
753
      if(ret == -1){
 
754
        error(0, errno, "fstat");
 
755
      } else {
 
756
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
757
          ret = fchown(plugindir_fd, uid, gid);
 
758
          if(ret == -1){
 
759
            error(0, errno, "fchown");
 
760
          }
 
761
        }
 
762
      }
 
763
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
764
    }
 
765
  }
 
766
  
 
767
  /* Lower permissions */
692
768
  setgid(gid);
693
769
  if(ret == -1){
694
 
    perror("setgid");
 
770
    error(0, errno, "setgid");
695
771
  }
696
772
  ret = setuid(uid);
697
773
  if(ret == -1){
698
 
    perror("setuid");
699
 
  }
700
 
  
701
 
  if(plugindir == NULL){
702
 
    dir = opendir(PDIR);
703
 
  } else {
704
 
    dir = opendir(plugindir);
705
 
  }
706
 
  
707
 
  if(dir == NULL){
708
 
    perror("Could not open plugin dir");
709
 
    exitstatus = EXIT_FAILURE;
710
 
    goto fallback;
711
 
  }
712
 
  
713
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
774
    error(0, errno, "setuid");
 
775
  }
 
776
  
 
777
  /* Open plugin directory with close_on_exec flag */
714
778
  {
715
 
    int dir_fd = dirfd(dir);
716
 
    if(dir_fd >= 0){
717
 
      ret = set_cloexec_flag(dir_fd);
718
 
      if(ret < 0){
719
 
        perror("set_cloexec_flag");
720
 
        exitstatus = EXIT_FAILURE;
721
 
        goto fallback;
722
 
      }
 
779
    int dir_fd = -1;
 
780
    if(plugindir == NULL){
 
781
      dir_fd = open(PDIR, O_RDONLY |
 
782
#ifdef O_CLOEXEC
 
783
                    O_CLOEXEC
 
784
#else  /* not O_CLOEXEC */
 
785
                    0
 
786
#endif  /* not O_CLOEXEC */
 
787
                    );
 
788
    } else {
 
789
      dir_fd = open(plugindir, O_RDONLY |
 
790
#ifdef O_CLOEXEC
 
791
                    O_CLOEXEC
 
792
#else  /* not O_CLOEXEC */
 
793
                    0
 
794
#endif  /* not O_CLOEXEC */
 
795
                    );
 
796
    }
 
797
    if(dir_fd == -1){
 
798
      error(0, errno, "Could not open plugin dir");
 
799
      exitstatus = EX_UNAVAILABLE;
 
800
      goto fallback;
 
801
    }
 
802
    
 
803
#ifndef O_CLOEXEC
 
804
  /* Set the FD_CLOEXEC flag on the directory */
 
805
    ret = set_cloexec_flag(dir_fd);
 
806
    if(ret < 0){
 
807
      error(0, errno, "set_cloexec_flag");
 
808
      TEMP_FAILURE_RETRY(close(dir_fd));
 
809
      exitstatus = EX_OSERR;
 
810
      goto fallback;
 
811
    }
 
812
#endif  /* O_CLOEXEC */
 
813
    
 
814
    dir = fdopendir(dir_fd);
 
815
    if(dir == NULL){
 
816
      error(0, errno, "Could not open plugin dir");
 
817
      TEMP_FAILURE_RETRY(close(dir_fd));
 
818
      exitstatus = EX_OSERR;
 
819
      goto fallback;
723
820
    }
724
821
  }
725
822
  
734
831
    /* All directory entries have been processed */
735
832
    if(dirst == NULL){
736
833
      if(errno == EBADF){
737
 
        perror("readdir");
738
 
        exitstatus = EXIT_FAILURE;
 
834
        error(0, errno, "readdir");
 
835
        exitstatus = EX_IOERR;
739
836
        goto fallback;
740
837
      }
741
838
      break;
797
894
                                             dirst->d_name));
798
895
    }
799
896
    if(ret < 0){
800
 
      perror("asprintf");
 
897
      error(0, errno, "asprintf");
801
898
      continue;
802
899
    }
803
900
    
804
901
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
805
902
    if(ret == -1){
806
 
      perror("stat");
 
903
      error(0, errno, "stat");
807
904
      free(filename);
808
905
      continue;
809
906
    }
821
918
    
822
919
    plugin *p = getplugin(dirst->d_name);
823
920
    if(p == NULL){
824
 
      perror("getplugin");
 
921
      error(0, errno, "getplugin");
825
922
      free(filename);
826
923
      continue;
827
924
    }
839
936
      if(g != NULL){
840
937
        for(char **a = g->argv + 1; *a != NULL; a++){
841
938
          if(not add_argument(p, *a)){
842
 
            perror("add_argument");
 
939
            error(0, errno, "add_argument");
843
940
          }
844
941
        }
845
942
        /* Add global environment variables */
846
943
        for(char **e = g->environ; *e != NULL; e++){
847
944
          if(not add_environment(p, *e, false)){
848
 
            perror("add_environment");
 
945
            error(0, errno, "add_environment");
849
946
          }
850
947
        }
851
948
      }
856
953
    if(p->environ[0] != NULL){
857
954
      for(char **e = environ; *e != NULL; e++){
858
955
        if(not add_environment(p, *e, false)){
859
 
          perror("add_environment");
 
956
          error(0, errno, "add_environment");
860
957
        }
861
958
      }
862
959
    }
864
961
    int pipefd[2];
865
962
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
866
963
    if(ret == -1){
867
 
      perror("pipe");
868
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "pipe");
 
965
      exitstatus = EX_OSERR;
869
966
      goto fallback;
870
967
    }
871
968
    /* Ask OS to automatic close the pipe on exec */
872
969
    ret = set_cloexec_flag(pipefd[0]);
873
970
    if(ret < 0){
874
 
      perror("set_cloexec_flag");
875
 
      exitstatus = EXIT_FAILURE;
 
971
      error(0, errno, "set_cloexec_flag");
 
972
      exitstatus = EX_OSERR;
876
973
      goto fallback;
877
974
    }
878
975
    ret = set_cloexec_flag(pipefd[1]);
879
976
    if(ret < 0){
880
 
      perror("set_cloexec_flag");
881
 
      exitstatus = EXIT_FAILURE;
 
977
      error(0, errno, "set_cloexec_flag");
 
978
      exitstatus = EX_OSERR;
882
979
      goto fallback;
883
980
    }
884
981
    /* Block SIGCHLD until process is safely in process list */
886
983
                                              &sigchld_action.sa_mask,
887
984
                                              NULL));
888
985
    if(ret < 0){
889
 
      perror("sigprocmask");
890
 
      exitstatus = EXIT_FAILURE;
 
986
      error(0, errno, "sigprocmask");
 
987
      exitstatus = EX_OSERR;
891
988
      goto fallback;
892
989
    }
893
990
    /* Starting a new process to be watched */
896
993
      pid = fork();
897
994
    } while(pid == -1 and errno == EINTR);
898
995
    if(pid == -1){
899
 
      perror("fork");
900
 
      exitstatus = EXIT_FAILURE;
 
996
      error(0, errno, "fork");
 
997
      exitstatus = EX_OSERR;
901
998
      goto fallback;
902
999
    }
903
1000
    if(pid == 0){
904
1001
      /* this is the child process */
905
1002
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
906
1003
      if(ret < 0){
907
 
        perror("sigaction");
908
 
        _exit(EXIT_FAILURE);
 
1004
        error(0, errno, "sigaction");
 
1005
        _exit(EX_OSERR);
909
1006
      }
910
1007
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
1008
      if(ret < 0){
912
 
        perror("sigprocmask");
913
 
        _exit(EXIT_FAILURE);
 
1009
        error(0, errno, "sigprocmask");
 
1010
        _exit(EX_OSERR);
914
1011
      }
915
1012
      
916
1013
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
917
1014
      if(ret == -1){
918
 
        perror("dup2");
919
 
        _exit(EXIT_FAILURE);
 
1015
        error(0, errno, "dup2");
 
1016
        _exit(EX_OSERR);
920
1017
      }
921
1018
      
922
1019
      if(dirfd(dir) < 0){
926
1023
      }
927
1024
      if(p->environ[0] == NULL){
928
1025
        if(execv(filename, p->argv) < 0){
929
 
          perror("execv");
930
 
          _exit(EXIT_FAILURE);
 
1026
          error(0, errno, "execv for %s", filename);
 
1027
          _exit(EX_OSERR);
931
1028
        }
932
1029
      } else {
933
1030
        if(execve(filename, p->argv, p->environ) < 0){
934
 
          perror("execve");
935
 
          _exit(EXIT_FAILURE);
 
1031
          error(0, errno, "execve for %s", filename);
 
1032
          _exit(EX_OSERR);
936
1033
        }
937
1034
      }
938
1035
      /* no return */
943
1040
    free(filename);
944
1041
    plugin *new_plugin = getplugin(dirst->d_name);
945
1042
    if(new_plugin == NULL){
946
 
      perror("getplugin");
 
1043
      error(0, errno, "getplugin");
947
1044
      ret = (int)(TEMP_FAILURE_RETRY
948
1045
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
949
1046
                               NULL)));
950
1047
      if(ret < 0){
951
 
        perror("sigprocmask");
 
1048
        error(0, errno, "sigprocmask");
952
1049
      }
953
 
      exitstatus = EXIT_FAILURE;
 
1050
      exitstatus = EX_OSERR;
954
1051
      goto fallback;
955
1052
    }
956
1053
    
963
1060
                                              &sigchld_action.sa_mask,
964
1061
                                              NULL));
965
1062
    if(ret < 0){
966
 
      perror("sigprocmask");
967
 
      exitstatus = EXIT_FAILURE;
 
1063
      error(0, errno, "sigprocmask");
 
1064
      exitstatus = EX_OSERR;
968
1065
      goto fallback;
969
1066
    }
970
1067
    
996
1093
    fd_set rfds = rfds_all;
997
1094
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
998
1095
    if(select_ret == -1 and errno != EINTR){
999
 
      perror("select");
1000
 
      exitstatus = EXIT_FAILURE;
 
1096
      error(0, errno, "select");
 
1097
      exitstatus = EX_OSERR;
1001
1098
      goto fallback;
1002
1099
    }
1003
1100
    /* OK, now either a process completed, or something can be read
1038
1135
                                         &sigchld_action.sa_mask,
1039
1136
                                         NULL));
1040
1137
          if(ret < 0){
1041
 
            perror("sigprocmask");
1042
 
            exitstatus = EXIT_FAILURE;
 
1138
            error(0, errno, "sigprocmask");
 
1139
            exitstatus = EX_OSERR;
1043
1140
            goto fallback;
1044
1141
          }
1045
1142
          
1052
1149
                      (sigprocmask(SIG_UNBLOCK,
1053
1150
                                   &sigchld_action.sa_mask, NULL)));
1054
1151
          if(ret < 0){
1055
 
            perror("sigprocmask");
1056
 
            exitstatus = EXIT_FAILURE;
 
1152
            error(0, errno, "sigprocmask");
 
1153
            exitstatus = EX_OSERR;
1057
1154
            goto fallback;
1058
1155
          }
1059
1156
          
1069
1166
        bool bret = print_out_password(proc->buffer,
1070
1167
                                       proc->buffer_length);
1071
1168
        if(not bret){
1072
 
          perror("print_out_password");
1073
 
          exitstatus = EXIT_FAILURE;
 
1169
          error(0, errno, "print_out_password");
 
1170
          exitstatus = EX_IOERR;
1074
1171
        }
1075
1172
        goto fallback;
1076
1173
      }
1088
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1089
1186
                               + (size_t) BUFFER_SIZE);
1090
1187
        if(proc->buffer == NULL){
1091
 
          perror("malloc");
1092
 
          exitstatus = EXIT_FAILURE;
 
1188
          error(0, errno, "malloc");
 
1189
          exitstatus = EX_OSERR;
1093
1190
          goto fallback;
1094
1191
        }
1095
1192
        proc->buffer_size += BUFFER_SIZE;
1116
1213
  
1117
1214
 fallback:
1118
1215
  
1119
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1216
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1217
                             and exitstatus != EX_OK)){
1120
1218
    /* Fallback if all plugins failed, none are found or an error
1121
1219
       occured */
1122
1220
    bool bret;
1130
1228
    }
1131
1229
    bret = print_out_password(passwordbuffer, len);
1132
1230
    if(not bret){
1133
 
      perror("print_out_password");
1134
 
      exitstatus = EXIT_FAILURE;
 
1231
      error(0, errno, "print_out_password");
 
1232
      exitstatus = EX_IOERR;
1135
1233
    }
1136
1234
  }
1137
1235
  
1138
1236
  /* Restore old signal handler */
1139
1237
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1140
1238
  if(ret == -1){
1141
 
    perror("sigaction");
1142
 
    exitstatus = EXIT_FAILURE;
 
1239
    error(0, errno, "sigaction");
 
1240
    exitstatus = EX_OSERR;
1143
1241
  }
1144
1242
  
1145
1243
  if(custom_argv != NULL){
1160
1258
      ret = kill(p->pid, SIGTERM);
1161
1259
      if(ret == -1 and errno != ESRCH){
1162
1260
        /* Set-uid proccesses might not get closed */
1163
 
        perror("kill");
 
1261
        error(0, errno, "kill");
1164
1262
      }
1165
1263
    }
1166
1264
  }
1170
1268
    ret = wait(NULL);
1171
1269
  } while(ret >= 0);
1172
1270
  if(errno != ECHILD){
1173
 
    perror("wait");
 
1271
    error(0, errno, "wait");
1174
1272
  }
1175
1273
  
1176
1274
  free_plugin_list();