/mandos/trunk

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2012-02-21 21:27:14 UTC
  • mfrom: (237.6.1 nmu)
  • mto: (237.4.31 release)
  • mto: This revision was merged to the branch mainline in revision 560.
  • Revision ID: teddy@recompile.se-20120221212714-40iub281d4yvrk9r
Tags: version-1.5.3-1.1
Merge NMU change.

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-2012 Teddy Hogeborn
 
6
 * Copyright © 2008-2012 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;
161
171
}
162
172
 
163
173
/* Helper function for add_argument and add_environment */
 
174
__attribute__((nonnull))
164
175
static bool add_to_char_array(const char *new, char ***array,
165
176
                              int *len){
166
177
  /* Resize the pointed-to array to hold one more pointer */
189
200
}
190
201
 
191
202
/* Add to a plugin's argument vector */
 
203
__attribute__((nonnull(2)))
192
204
static bool add_argument(plugin *p, const char *arg){
193
205
  if(p == NULL){
194
206
    return false;
197
209
}
198
210
 
199
211
/* Add to a plugin's environment */
 
212
__attribute__((nonnull(2)))
200
213
static bool add_environment(plugin *p, const char *def, bool replace){
201
214
  if(p == NULL){
202
215
    return false;
258
271
        /* No child processes */
259
272
        break;
260
273
      }
261
 
      perror("waitpid");
 
274
      error(0, errno, "waitpid");
262
275
    }
263
276
    
264
277
    /* A child exited, find it in process_list */
276
289
}
277
290
 
278
291
/* Prints out a password to stdout */
 
292
__attribute__((nonnull))
279
293
static bool print_out_password(const char *buffer, size_t length){
280
294
  ssize_t ret;
281
295
  for(size_t written = 0; written < length; written += (size_t)ret){
289
303
}
290
304
 
291
305
/* Removes and free a plugin from the plugin list */
 
306
__attribute__((nonnull))
292
307
static void free_plugin(plugin *plugin_node){
293
308
  
294
309
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
349
364
  sigemptyset(&sigchld_action.sa_mask);
350
365
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
351
366
  if(ret == -1){
352
 
    perror("sigaddset");
353
 
    exitstatus = EXIT_FAILURE;
 
367
    error(0, errno, "sigaddset");
 
368
    exitstatus = EX_OSERR;
354
369
    goto fallback;
355
370
  }
356
371
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
357
372
  if(ret == -1){
358
 
    perror("sigaction");
359
 
    exitstatus = EXIT_FAILURE;
 
373
    error(0, errno, "sigaction");
 
374
    exitstatus = EX_OSERR;
360
375
    goto fallback;
361
376
  }
362
377
  
394
409
      .doc = "Group ID the plugins will run as", .group = 3 },
395
410
    { .name = "debug", .key = 132,
396
411
      .doc = "Debug mode", .group = 4 },
 
412
    /*
 
413
     * These reproduce what we would get without ARGP_NO_HELP
 
414
     */
 
415
    { .name = "help", .key = '?',
 
416
      .doc = "Give this help list", .group = -1 },
 
417
    { .name = "usage", .key = -3,
 
418
      .doc = "Give a short usage message", .group = -1 },
 
419
    { .name = "version", .key = 'V',
 
420
      .doc = "Print program version", .group = -1 },
397
421
    { .name = NULL }
398
422
  };
399
423
  
400
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
401
 
                    struct argp_state *state){
 
424
  __attribute__((nonnull(3)))
 
425
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
426
    errno = 0;
402
427
    switch(key){
403
428
      char *tmp;
404
 
      intmax_t tmpmax;
 
429
      intmax_t tmp_id;
405
430
    case 'g':                   /* --global-options */
406
 
      if(arg != NULL){
 
431
      {
407
432
        char *plugin_option;
408
433
        while((plugin_option = strsep(&arg, ",")) != NULL){
409
 
          if(plugin_option[0] == '\0'){
410
 
            continue;
411
 
          }
412
434
          if(not add_argument(getplugin(NULL), plugin_option)){
413
 
            perror("add_argument");
414
 
            return ARGP_ERR_UNKNOWN;
 
435
            break;
415
436
          }
416
437
        }
417
438
      }
418
439
      break;
419
440
    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
 
      }
 
441
      add_environment(getplugin(NULL), arg, true);
426
442
      break;
427
443
    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;
 
444
      {
 
445
        char *option_list = strchr(arg, ':');
 
446
        if(option_list == NULL){
 
447
          argp_error(state, "No colon in \"%s\"", arg);
 
448
          errno = EINVAL;
 
449
          break;
 
450
        }
 
451
        *option_list = '\0';
 
452
        option_list++;
 
453
        if(arg[0] == '\0'){
 
454
          argp_error(state, "Empty plugin name");
 
455
          errno = EINVAL;
 
456
          break;
 
457
        }
 
458
        char *option;
 
459
        while((option = strsep(&option_list, ",")) != NULL){
 
460
          if(not add_argument(getplugin(arg), option)){
 
461
            break;
438
462
          }
439
463
        }
440
464
      }
441
465
      break;
442
466
    case 'E':                   /* --env-for */
443
 
      if(arg == NULL){
444
 
        break;
445
 
      }
446
467
      {
447
468
        char *envdef = strchr(arg, ':');
448
469
        if(envdef == NULL){
 
470
          argp_error(state, "No colon in \"%s\"", arg);
 
471
          errno = EINVAL;
449
472
          break;
450
473
        }
451
474
        *envdef = '\0';
452
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
453
 
          perror("add_environment");
 
475
        envdef++;
 
476
        if(arg[0] == '\0'){
 
477
          argp_error(state, "Empty plugin name");
 
478
          errno = EINVAL;
 
479
          break;
454
480
        }
 
481
        add_environment(getplugin(arg), envdef, true);
455
482
      }
456
483
      break;
457
484
    case 'd':                   /* --disable */
458
 
      if(arg != NULL){
 
485
      {
459
486
        plugin *p = getplugin(arg);
460
 
        if(p == NULL){
461
 
          return ARGP_ERR_UNKNOWN;
 
487
        if(p != NULL){
 
488
          p->disabled = true;
462
489
        }
463
 
        p->disabled = true;
464
490
      }
465
491
      break;
466
492
    case 'e':                   /* --enable */
467
 
      if(arg != NULL){
 
493
      {
468
494
        plugin *p = getplugin(arg);
469
 
        if(p == NULL){
470
 
          return ARGP_ERR_UNKNOWN;
 
495
        if(p != NULL){
 
496
          p->disabled = false;
471
497
        }
472
 
        p->disabled = false;
473
498
      }
474
499
      break;
475
500
    case 128:                   /* --plugin-dir */
476
501
      free(plugindir);
477
502
      plugindir = strdup(arg);
478
 
      if(plugindir == NULL){
479
 
        perror("strdup");
480
 
      }
481
503
      break;
482
504
    case 129:                   /* --config-file */
483
505
      /* This is already done by parse_opt_config_file() */
484
506
      break;
485
507
    case 130:                   /* --userid */
486
 
      errno = 0;
487
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
508
      tmp_id = strtoimax(arg, &tmp, 10);
488
509
      if(errno != 0 or tmp == arg or *tmp != '\0'
489
 
         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;
 
510
         or tmp_id != (uid_t)tmp_id){
 
511
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
512
                   PRIdMAX, arg, (intmax_t)uid);
 
513
        break;
494
514
      }
 
515
      uid = (uid_t)tmp_id;
495
516
      break;
496
517
    case 131:                   /* --groupid */
497
 
      errno = 0;
498
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
518
      tmp_id = strtoimax(arg, &tmp, 10);
499
519
      if(errno != 0 or tmp == arg or *tmp != '\0'
500
 
         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;
 
520
         or tmp_id != (gid_t)tmp_id){
 
521
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
522
                   PRIdMAX, arg, (intmax_t)gid);
 
523
        break;
505
524
      }
 
525
      gid = (gid_t)tmp_id;
506
526
      break;
507
527
    case 132:                   /* --debug */
508
528
      debug = true;
509
529
      break;
 
530
      /*
 
531
       * These reproduce what we would get without ARGP_NO_HELP
 
532
       */
 
533
    case '?':                   /* --help */
 
534
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
535
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
 
536
    case -3:                    /* --usage */
 
537
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
 
538
      argp_state_help(state, state->out_stream,
 
539
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
 
540
    case 'V':                   /* --version */
 
541
      fprintf(state->out_stream, "%s\n", argp_program_version);
 
542
      exit(EXIT_SUCCESS);
 
543
      break;
510
544
/*
511
545
 * When adding more options before this line, remember to also add a
512
546
 * "case" to the "parse_opt_config_file" function below.
515
549
      /* Cryptsetup always passes an argument, which is an empty
516
550
         string if "none" was specified in /etc/crypttab.  So if
517
551
         argument was empty, we ignore it silently. */
518
 
      if(arg[0] != '\0'){
519
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
552
      if(arg[0] == '\0'){
 
553
        break;
520
554
      }
521
 
      break;
522
 
    case ARGP_KEY_END:
523
 
      break;
524
555
    default:
525
556
      return ARGP_ERR_UNKNOWN;
526
557
    }
527
 
    return 0;
 
558
    return errno;               /* Set to 0 at start */
528
559
  }
529
560
  
530
561
  /* This option parser is the same as parse_opt() above, except it
532
563
  error_t parse_opt_config_file(int key, char *arg,
533
564
                                __attribute__((unused))
534
565
                                struct argp_state *state){
 
566
    errno = 0;
535
567
    switch(key){
536
568
    case 'g':                   /* --global-options */
537
569
    case 'G':                   /* --global-env */
544
576
    case 129:                   /* --config-file */
545
577
      free(argfile);
546
578
      argfile = strdup(arg);
547
 
      if(argfile == NULL){
548
 
        perror("strdup");
549
 
      }
550
579
      break;
551
580
    case 130:                   /* --userid */
552
581
    case 131:                   /* --groupid */
553
582
    case 132:                   /* --debug */
 
583
    case '?':                   /* --help */
 
584
    case -3:                    /* --usage */
 
585
    case 'V':                   /* --version */
554
586
    case ARGP_KEY_ARG:
555
 
    case ARGP_KEY_END:
556
587
      break;
557
588
    default:
558
589
      return ARGP_ERR_UNKNOWN;
559
590
    }
560
 
    return 0;
 
591
    return errno;
561
592
  }
562
593
  
563
594
  struct argp argp = { .options = options,
567
598
  
568
599
  /* Parse using parse_opt_config_file() in order to get the custom
569
600
     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;
 
601
  ret = argp_parse(&argp, argc, argv,
 
602
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
603
                   NULL, NULL);
 
604
  switch(ret){
 
605
  case 0:
 
606
    break;
 
607
  case ENOMEM:
 
608
  default:
 
609
    errno = ret;
 
610
    error(0, errno, "argp_parse");
 
611
    exitstatus = EX_OSERR;
 
612
    goto fallback;
 
613
  case EINVAL:
 
614
    exitstatus = EX_USAGE;
574
615
    goto fallback;
575
616
  }
576
617
  
593
634
    custom_argc = 1;
594
635
    custom_argv = malloc(sizeof(char*) * 2);
595
636
    if(custom_argv == NULL){
596
 
      perror("malloc");
597
 
      exitstatus = EXIT_FAILURE;
 
637
      error(0, errno, "malloc");
 
638
      exitstatus = EX_OSERR;
598
639
      goto fallback;
599
640
    }
600
641
    custom_argv[0] = argv[0];
616
657
        }
617
658
        new_arg = strdup(p);
618
659
        if(new_arg == NULL){
619
 
          perror("strdup");
620
 
          exitstatus = EXIT_FAILURE;
 
660
          error(0, errno, "strdup");
 
661
          exitstatus = EX_OSERR;
621
662
          free(org_line);
622
663
          goto fallback;
623
664
        }
626
667
        custom_argv = realloc(custom_argv, sizeof(char *)
627
668
                              * ((unsigned int) custom_argc + 1));
628
669
        if(custom_argv == NULL){
629
 
          perror("realloc");
630
 
          exitstatus = EXIT_FAILURE;
 
670
          error(0, errno, "realloc");
 
671
          exitstatus = EX_OSERR;
631
672
          free(org_line);
632
673
          goto fallback;
633
674
        }
639
680
      ret = fclose(conffp);
640
681
    } while(ret == EOF and errno == EINTR);
641
682
    if(ret == EOF){
642
 
      perror("fclose");
643
 
      exitstatus = EXIT_FAILURE;
 
683
      error(0, errno, "fclose");
 
684
      exitstatus = EX_IOERR;
644
685
      goto fallback;
645
686
    }
646
687
    free(org_line);
648
689
    /* Check for harmful errors and go to fallback. Other errors might
649
690
       not affect opening plugins */
650
691
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
651
 
      perror("fopen");
652
 
      exitstatus = EXIT_FAILURE;
 
692
      error(0, errno, "fopen");
 
693
      exitstatus = EX_OSERR;
653
694
      goto fallback;
654
695
    }
655
696
  }
656
 
  /* If there was any arguments from configuration file,
657
 
     pass them to parser as command arguments */
 
697
  /* If there were any arguments from the configuration file, pass
 
698
     them to parser as command line arguments */
658
699
  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;
 
700
    ret = argp_parse(&argp, custom_argc, custom_argv,
 
701
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
702
                     NULL, NULL);
 
703
    switch(ret){
 
704
    case 0:
 
705
      break;
 
706
    case ENOMEM:
 
707
    default:
 
708
      errno = ret;
 
709
      error(0, errno, "argp_parse");
 
710
      exitstatus = EX_OSERR;
 
711
      goto fallback;
 
712
    case EINVAL:
 
713
      exitstatus = EX_CONFIG;
664
714
      goto fallback;
665
715
    }
666
716
  }
667
717
  
668
718
  /* Parse actual command line arguments, to let them override the
669
719
     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;
 
720
  ret = argp_parse(&argp, argc, argv,
 
721
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
 
722
                   NULL, NULL);
 
723
  switch(ret){
 
724
  case 0:
 
725
    break;
 
726
  case ENOMEM:
 
727
  default:
 
728
    errno = ret;
 
729
    error(0, errno, "argp_parse");
 
730
    exitstatus = EX_OSERR;
 
731
    goto fallback;
 
732
  case EINVAL:
 
733
    exitstatus = EX_USAGE;
674
734
    goto fallback;
675
735
  }
676
736
  
688
748
    }
689
749
  }
690
750
  
691
 
  /* Strip permissions down to nobody */
 
751
  if(getuid() == 0){
 
752
    /* Work around Debian bug #633582:
 
753
       <http://bugs.debian.org/633582> */
 
754
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
755
    if(plugindir_fd == -1){
 
756
      error(0, errno, "open");
 
757
    } else {
 
758
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
759
      if(ret == -1){
 
760
        error(0, errno, "fstat");
 
761
      } else {
 
762
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
763
          ret = fchown(plugindir_fd, uid, gid);
 
764
          if(ret == -1){
 
765
            error(0, errno, "fchown");
 
766
          }
 
767
        }
 
768
      }
 
769
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
770
    }
 
771
  }
 
772
  
 
773
  /* Lower permissions */
692
774
  setgid(gid);
693
775
  if(ret == -1){
694
 
    perror("setgid");
 
776
    error(0, errno, "setgid");
695
777
  }
696
778
  ret = setuid(uid);
697
779
  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 */
 
780
    error(0, errno, "setuid");
 
781
  }
 
782
  
 
783
  /* Open plugin directory with close_on_exec flag */
714
784
  {
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
 
      }
 
785
    int dir_fd = -1;
 
786
    if(plugindir == NULL){
 
787
      dir_fd = open(PDIR, O_RDONLY |
 
788
#ifdef O_CLOEXEC
 
789
                    O_CLOEXEC
 
790
#else  /* not O_CLOEXEC */
 
791
                    0
 
792
#endif  /* not O_CLOEXEC */
 
793
                    );
 
794
    } else {
 
795
      dir_fd = open(plugindir, O_RDONLY |
 
796
#ifdef O_CLOEXEC
 
797
                    O_CLOEXEC
 
798
#else  /* not O_CLOEXEC */
 
799
                    0
 
800
#endif  /* not O_CLOEXEC */
 
801
                    );
 
802
    }
 
803
    if(dir_fd == -1){
 
804
      error(0, errno, "Could not open plugin dir");
 
805
      exitstatus = EX_UNAVAILABLE;
 
806
      goto fallback;
 
807
    }
 
808
    
 
809
#ifndef O_CLOEXEC
 
810
  /* Set the FD_CLOEXEC flag on the directory */
 
811
    ret = set_cloexec_flag(dir_fd);
 
812
    if(ret < 0){
 
813
      error(0, errno, "set_cloexec_flag");
 
814
      TEMP_FAILURE_RETRY(close(dir_fd));
 
815
      exitstatus = EX_OSERR;
 
816
      goto fallback;
 
817
    }
 
818
#endif  /* O_CLOEXEC */
 
819
    
 
820
    dir = fdopendir(dir_fd);
 
821
    if(dir == NULL){
 
822
      error(0, errno, "Could not open plugin dir");
 
823
      TEMP_FAILURE_RETRY(close(dir_fd));
 
824
      exitstatus = EX_OSERR;
 
825
      goto fallback;
723
826
    }
724
827
  }
725
828
  
734
837
    /* All directory entries have been processed */
735
838
    if(dirst == NULL){
736
839
      if(errno == EBADF){
737
 
        perror("readdir");
738
 
        exitstatus = EXIT_FAILURE;
 
840
        error(0, errno, "readdir");
 
841
        exitstatus = EX_IOERR;
739
842
        goto fallback;
740
843
      }
741
844
      break;
797
900
                                             dirst->d_name));
798
901
    }
799
902
    if(ret < 0){
800
 
      perror("asprintf");
 
903
      error(0, errno, "asprintf");
801
904
      continue;
802
905
    }
803
906
    
804
907
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
805
908
    if(ret == -1){
806
 
      perror("stat");
 
909
      error(0, errno, "stat");
807
910
      free(filename);
808
911
      continue;
809
912
    }
821
924
    
822
925
    plugin *p = getplugin(dirst->d_name);
823
926
    if(p == NULL){
824
 
      perror("getplugin");
 
927
      error(0, errno, "getplugin");
825
928
      free(filename);
826
929
      continue;
827
930
    }
839
942
      if(g != NULL){
840
943
        for(char **a = g->argv + 1; *a != NULL; a++){
841
944
          if(not add_argument(p, *a)){
842
 
            perror("add_argument");
 
945
            error(0, errno, "add_argument");
843
946
          }
844
947
        }
845
948
        /* Add global environment variables */
846
949
        for(char **e = g->environ; *e != NULL; e++){
847
950
          if(not add_environment(p, *e, false)){
848
 
            perror("add_environment");
 
951
            error(0, errno, "add_environment");
849
952
          }
850
953
        }
851
954
      }
856
959
    if(p->environ[0] != NULL){
857
960
      for(char **e = environ; *e != NULL; e++){
858
961
        if(not add_environment(p, *e, false)){
859
 
          perror("add_environment");
 
962
          error(0, errno, "add_environment");
860
963
        }
861
964
      }
862
965
    }
864
967
    int pipefd[2];
865
968
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
866
969
    if(ret == -1){
867
 
      perror("pipe");
868
 
      exitstatus = EXIT_FAILURE;
 
970
      error(0, errno, "pipe");
 
971
      exitstatus = EX_OSERR;
869
972
      goto fallback;
870
973
    }
871
974
    /* Ask OS to automatic close the pipe on exec */
872
975
    ret = set_cloexec_flag(pipefd[0]);
873
976
    if(ret < 0){
874
 
      perror("set_cloexec_flag");
875
 
      exitstatus = EXIT_FAILURE;
 
977
      error(0, errno, "set_cloexec_flag");
 
978
      exitstatus = EX_OSERR;
876
979
      goto fallback;
877
980
    }
878
981
    ret = set_cloexec_flag(pipefd[1]);
879
982
    if(ret < 0){
880
 
      perror("set_cloexec_flag");
881
 
      exitstatus = EXIT_FAILURE;
 
983
      error(0, errno, "set_cloexec_flag");
 
984
      exitstatus = EX_OSERR;
882
985
      goto fallback;
883
986
    }
884
987
    /* Block SIGCHLD until process is safely in process list */
886
989
                                              &sigchld_action.sa_mask,
887
990
                                              NULL));
888
991
    if(ret < 0){
889
 
      perror("sigprocmask");
890
 
      exitstatus = EXIT_FAILURE;
 
992
      error(0, errno, "sigprocmask");
 
993
      exitstatus = EX_OSERR;
891
994
      goto fallback;
892
995
    }
893
996
    /* Starting a new process to be watched */
896
999
      pid = fork();
897
1000
    } while(pid == -1 and errno == EINTR);
898
1001
    if(pid == -1){
899
 
      perror("fork");
900
 
      exitstatus = EXIT_FAILURE;
 
1002
      error(0, errno, "fork");
 
1003
      exitstatus = EX_OSERR;
901
1004
      goto fallback;
902
1005
    }
903
1006
    if(pid == 0){
904
1007
      /* this is the child process */
905
1008
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
906
1009
      if(ret < 0){
907
 
        perror("sigaction");
908
 
        _exit(EXIT_FAILURE);
 
1010
        error(0, errno, "sigaction");
 
1011
        _exit(EX_OSERR);
909
1012
      }
910
1013
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
1014
      if(ret < 0){
912
 
        perror("sigprocmask");
913
 
        _exit(EXIT_FAILURE);
 
1015
        error(0, errno, "sigprocmask");
 
1016
        _exit(EX_OSERR);
914
1017
      }
915
1018
      
916
1019
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
917
1020
      if(ret == -1){
918
 
        perror("dup2");
919
 
        _exit(EXIT_FAILURE);
 
1021
        error(0, errno, "dup2");
 
1022
        _exit(EX_OSERR);
920
1023
      }
921
1024
      
922
1025
      if(dirfd(dir) < 0){
926
1029
      }
927
1030
      if(p->environ[0] == NULL){
928
1031
        if(execv(filename, p->argv) < 0){
929
 
          perror("execv");
930
 
          _exit(EXIT_FAILURE);
 
1032
          error(0, errno, "execv for %s", filename);
 
1033
          _exit(EX_OSERR);
931
1034
        }
932
1035
      } else {
933
1036
        if(execve(filename, p->argv, p->environ) < 0){
934
 
          perror("execve");
935
 
          _exit(EXIT_FAILURE);
 
1037
          error(0, errno, "execve for %s", filename);
 
1038
          _exit(EX_OSERR);
936
1039
        }
937
1040
      }
938
1041
      /* no return */
943
1046
    free(filename);
944
1047
    plugin *new_plugin = getplugin(dirst->d_name);
945
1048
    if(new_plugin == NULL){
946
 
      perror("getplugin");
 
1049
      error(0, errno, "getplugin");
947
1050
      ret = (int)(TEMP_FAILURE_RETRY
948
1051
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
949
1052
                               NULL)));
950
1053
      if(ret < 0){
951
 
        perror("sigprocmask");
 
1054
        error(0, errno, "sigprocmask");
952
1055
      }
953
 
      exitstatus = EXIT_FAILURE;
 
1056
      exitstatus = EX_OSERR;
954
1057
      goto fallback;
955
1058
    }
956
1059
    
963
1066
                                              &sigchld_action.sa_mask,
964
1067
                                              NULL));
965
1068
    if(ret < 0){
966
 
      perror("sigprocmask");
967
 
      exitstatus = EXIT_FAILURE;
 
1069
      error(0, errno, "sigprocmask");
 
1070
      exitstatus = EX_OSERR;
968
1071
      goto fallback;
969
1072
    }
970
1073
    
996
1099
    fd_set rfds = rfds_all;
997
1100
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
998
1101
    if(select_ret == -1 and errno != EINTR){
999
 
      perror("select");
1000
 
      exitstatus = EXIT_FAILURE;
 
1102
      error(0, errno, "select");
 
1103
      exitstatus = EX_OSERR;
1001
1104
      goto fallback;
1002
1105
    }
1003
1106
    /* OK, now either a process completed, or something can be read
1038
1141
                                         &sigchld_action.sa_mask,
1039
1142
                                         NULL));
1040
1143
          if(ret < 0){
1041
 
            perror("sigprocmask");
1042
 
            exitstatus = EXIT_FAILURE;
 
1144
            error(0, errno, "sigprocmask");
 
1145
            exitstatus = EX_OSERR;
1043
1146
            goto fallback;
1044
1147
          }
1045
1148
          
1052
1155
                      (sigprocmask(SIG_UNBLOCK,
1053
1156
                                   &sigchld_action.sa_mask, NULL)));
1054
1157
          if(ret < 0){
1055
 
            perror("sigprocmask");
1056
 
            exitstatus = EXIT_FAILURE;
 
1158
            error(0, errno, "sigprocmask");
 
1159
            exitstatus = EX_OSERR;
1057
1160
            goto fallback;
1058
1161
          }
1059
1162
          
1069
1172
        bool bret = print_out_password(proc->buffer,
1070
1173
                                       proc->buffer_length);
1071
1174
        if(not bret){
1072
 
          perror("print_out_password");
1073
 
          exitstatus = EXIT_FAILURE;
 
1175
          error(0, errno, "print_out_password");
 
1176
          exitstatus = EX_IOERR;
1074
1177
        }
1075
1178
        goto fallback;
1076
1179
      }
1088
1191
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1089
1192
                               + (size_t) BUFFER_SIZE);
1090
1193
        if(proc->buffer == NULL){
1091
 
          perror("malloc");
1092
 
          exitstatus = EXIT_FAILURE;
 
1194
          error(0, errno, "malloc");
 
1195
          exitstatus = EX_OSERR;
1093
1196
          goto fallback;
1094
1197
        }
1095
1198
        proc->buffer_size += BUFFER_SIZE;
1116
1219
  
1117
1220
 fallback:
1118
1221
  
1119
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1222
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1223
                             and exitstatus != EX_OK)){
1120
1224
    /* Fallback if all plugins failed, none are found or an error
1121
1225
       occured */
1122
1226
    bool bret;
1130
1234
    }
1131
1235
    bret = print_out_password(passwordbuffer, len);
1132
1236
    if(not bret){
1133
 
      perror("print_out_password");
1134
 
      exitstatus = EXIT_FAILURE;
 
1237
      error(0, errno, "print_out_password");
 
1238
      exitstatus = EX_IOERR;
1135
1239
    }
1136
1240
  }
1137
1241
  
1138
1242
  /* Restore old signal handler */
1139
1243
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1140
1244
  if(ret == -1){
1141
 
    perror("sigaction");
1142
 
    exitstatus = EXIT_FAILURE;
 
1245
    error(0, errno, "sigaction");
 
1246
    exitstatus = EX_OSERR;
1143
1247
  }
1144
1248
  
1145
1249
  if(custom_argv != NULL){
1160
1264
      ret = kill(p->pid, SIGTERM);
1161
1265
      if(ret == -1 and errno != ESRCH){
1162
1266
        /* Set-uid proccesses might not get closed */
1163
 
        perror("kill");
 
1267
        error(0, errno, "kill");
1164
1268
      }
1165
1269
    }
1166
1270
  }
1170
1274
    ret = wait(NULL);
1171
1275
  } while(ret >= 0);
1172
1276
  if(errno != ECHILD){
1173
 
    perror("wait");
 
1277
    error(0, errno, "wait");
1174
1278
  }
1175
1279
  
1176
1280
  free_plugin_list();