/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: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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
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(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
57
                                   strsignal(), strcmp(), strncmp() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
68
68
                                */
69
69
#include <errno.h>              /* errno, EBADF */
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
 
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
 
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
  
687
742
    }
688
743
  }
689
744
  
690
 
  /* 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 */
691
768
  setgid(gid);
692
769
  if(ret == -1){
693
 
    perror("setgid");
 
770
    error(0, errno, "setgid");
694
771
  }
695
772
  ret = setuid(uid);
696
773
  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 */
 
774
    error(0, errno, "setuid");
 
775
  }
 
776
  
 
777
  /* Open plugin directory with close_on_exec flag */
713
778
  {
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
 
      }
 
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;
722
820
    }
723
821
  }
724
822
  
733
831
    /* All directory entries have been processed */
734
832
    if(dirst == NULL){
735
833
      if(errno == EBADF){
736
 
        perror("readdir");
737
 
        exitstatus = EXIT_FAILURE;
 
834
        error(0, errno, "readdir");
 
835
        exitstatus = EX_IOERR;
738
836
        goto fallback;
739
837
      }
740
838
      break;
770
868
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
771
869
        size_t suf_len = strlen(*suf);
772
870
        if((d_name_len >= suf_len)
773
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
871
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
774
872
                == 0)){
775
873
          if(debug){
776
874
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
788
886
    
789
887
    char *filename;
790
888
    if(plugindir == NULL){
791
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
792
 
                                        dirst->d_name));
 
889
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
890
                                             dirst->d_name));
793
891
    } else {
794
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
795
 
                                        dirst->d_name));
 
892
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
893
                                             plugindir,
 
894
                                             dirst->d_name));
796
895
    }
797
896
    if(ret < 0){
798
 
      perror("asprintf");
 
897
      error(0, errno, "asprintf");
799
898
      continue;
800
899
    }
801
900
    
802
 
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
 
901
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
803
902
    if(ret == -1){
804
 
      perror("stat");
 
903
      error(0, errno, "stat");
805
904
      free(filename);
806
905
      continue;
807
906
    }
819
918
    
820
919
    plugin *p = getplugin(dirst->d_name);
821
920
    if(p == NULL){
822
 
      perror("getplugin");
 
921
      error(0, errno, "getplugin");
823
922
      free(filename);
824
923
      continue;
825
924
    }
837
936
      if(g != NULL){
838
937
        for(char **a = g->argv + 1; *a != NULL; a++){
839
938
          if(not add_argument(p, *a)){
840
 
            perror("add_argument");
 
939
            error(0, errno, "add_argument");
841
940
          }
842
941
        }
843
942
        /* Add global environment variables */
844
943
        for(char **e = g->environ; *e != NULL; e++){
845
944
          if(not add_environment(p, *e, false)){
846
 
            perror("add_environment");
 
945
            error(0, errno, "add_environment");
847
946
          }
848
947
        }
849
948
      }
854
953
    if(p->environ[0] != NULL){
855
954
      for(char **e = environ; *e != NULL; e++){
856
955
        if(not add_environment(p, *e, false)){
857
 
          perror("add_environment");
 
956
          error(0, errno, "add_environment");
858
957
        }
859
958
      }
860
959
    }
861
960
    
862
961
    int pipefd[2];
863
 
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
 
962
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
864
963
    if(ret == -1){
865
 
      perror("pipe");
866
 
      exitstatus = EXIT_FAILURE;
 
964
      error(0, errno, "pipe");
 
965
      exitstatus = EX_OSERR;
867
966
      goto fallback;
868
967
    }
869
968
    /* Ask OS to automatic close the pipe on exec */
870
969
    ret = set_cloexec_flag(pipefd[0]);
871
970
    if(ret < 0){
872
 
      perror("set_cloexec_flag");
873
 
      exitstatus = EXIT_FAILURE;
 
971
      error(0, errno, "set_cloexec_flag");
 
972
      exitstatus = EX_OSERR;
874
973
      goto fallback;
875
974
    }
876
975
    ret = set_cloexec_flag(pipefd[1]);
877
976
    if(ret < 0){
878
 
      perror("set_cloexec_flag");
879
 
      exitstatus = EXIT_FAILURE;
 
977
      error(0, errno, "set_cloexec_flag");
 
978
      exitstatus = EX_OSERR;
880
979
      goto fallback;
881
980
    }
882
981
    /* Block SIGCHLD until process is safely in process list */
883
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
884
 
                                         &sigchld_action.sa_mask,
885
 
                                         NULL));
 
982
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
983
                                              &sigchld_action.sa_mask,
 
984
                                              NULL));
886
985
    if(ret < 0){
887
 
      perror("sigprocmask");
888
 
      exitstatus = EXIT_FAILURE;
 
986
      error(0, errno, "sigprocmask");
 
987
      exitstatus = EX_OSERR;
889
988
      goto fallback;
890
989
    }
891
990
    /* Starting a new process to be watched */
894
993
      pid = fork();
895
994
    } while(pid == -1 and errno == EINTR);
896
995
    if(pid == -1){
897
 
      perror("fork");
898
 
      exitstatus = EXIT_FAILURE;
 
996
      error(0, errno, "fork");
 
997
      exitstatus = EX_OSERR;
899
998
      goto fallback;
900
999
    }
901
1000
    if(pid == 0){
902
1001
      /* this is the child process */
903
1002
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
904
1003
      if(ret < 0){
905
 
        perror("sigaction");
906
 
        _exit(EXIT_FAILURE);
 
1004
        error(0, errno, "sigaction");
 
1005
        _exit(EX_OSERR);
907
1006
      }
908
1007
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
909
1008
      if(ret < 0){
910
 
        perror("sigprocmask");
911
 
        _exit(EXIT_FAILURE);
 
1009
        error(0, errno, "sigprocmask");
 
1010
        _exit(EX_OSERR);
912
1011
      }
913
1012
      
914
1013
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
915
1014
      if(ret == -1){
916
 
        perror("dup2");
917
 
        _exit(EXIT_FAILURE);
 
1015
        error(0, errno, "dup2");
 
1016
        _exit(EX_OSERR);
918
1017
      }
919
1018
      
920
1019
      if(dirfd(dir) < 0){
924
1023
      }
925
1024
      if(p->environ[0] == NULL){
926
1025
        if(execv(filename, p->argv) < 0){
927
 
          perror("execv");
928
 
          _exit(EXIT_FAILURE);
 
1026
          error(0, errno, "execv for %s", filename);
 
1027
          _exit(EX_OSERR);
929
1028
        }
930
1029
      } else {
931
1030
        if(execve(filename, p->argv, p->environ) < 0){
932
 
          perror("execve");
933
 
          _exit(EXIT_FAILURE);
 
1031
          error(0, errno, "execve for %s", filename);
 
1032
          _exit(EX_OSERR);
934
1033
        }
935
1034
      }
936
1035
      /* no return */
941
1040
    free(filename);
942
1041
    plugin *new_plugin = getplugin(dirst->d_name);
943
1042
    if(new_plugin == NULL){
944
 
      perror("getplugin");
945
 
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
946
 
                                           &sigchld_action.sa_mask,
947
 
                                           NULL));
 
1043
      error(0, errno, "getplugin");
 
1044
      ret = (int)(TEMP_FAILURE_RETRY
 
1045
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1046
                               NULL)));
948
1047
      if(ret < 0){
949
 
        perror("sigprocmask");
 
1048
        error(0, errno, "sigprocmask");
950
1049
      }
951
 
      exitstatus = EXIT_FAILURE;
 
1050
      exitstatus = EX_OSERR;
952
1051
      goto fallback;
953
1052
    }
954
1053
    
957
1056
    
958
1057
    /* Unblock SIGCHLD so signal handler can be run if this process
959
1058
       has already completed */
960
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
961
 
                                         &sigchld_action.sa_mask,
962
 
                                         NULL));
 
1059
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1060
                                              &sigchld_action.sa_mask,
 
1061
                                              NULL));
963
1062
    if(ret < 0){
964
 
      perror("sigprocmask");
965
 
      exitstatus = EXIT_FAILURE;
 
1063
      error(0, errno, "sigprocmask");
 
1064
      exitstatus = EX_OSERR;
966
1065
      goto fallback;
967
1066
    }
968
1067
    
969
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1068
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1069
                                          -Wconversion */
970
1070
    
971
1071
    if(maxfd < new_plugin->fd){
972
1072
      maxfd = new_plugin->fd;
993
1093
    fd_set rfds = rfds_all;
994
1094
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
995
1095
    if(select_ret == -1 and errno != EINTR){
996
 
      perror("select");
997
 
      exitstatus = EXIT_FAILURE;
 
1096
      error(0, errno, "select");
 
1097
      exitstatus = EX_OSERR;
998
1098
      goto fallback;
999
1099
    }
1000
1100
    /* OK, now either a process completed, or something can be read
1026
1126
          }
1027
1127
          
1028
1128
          /* Remove the plugin */
1029
 
          FD_CLR(proc->fd, &rfds_all);
 
1129
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1130
                                          -Wconversion */
1030
1131
          
1031
1132
          /* Block signal while modifying process_list */
1032
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1033
 
                                               &sigchld_action.sa_mask,
1034
 
                                               NULL));
 
1133
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1134
                                        (SIG_BLOCK,
 
1135
                                         &sigchld_action.sa_mask,
 
1136
                                         NULL));
1035
1137
          if(ret < 0){
1036
 
            perror("sigprocmask");
1037
 
            exitstatus = EXIT_FAILURE;
 
1138
            error(0, errno, "sigprocmask");
 
1139
            exitstatus = EX_OSERR;
1038
1140
            goto fallback;
1039
1141
          }
1040
1142
          
1043
1145
          proc = next_plugin;
1044
1146
          
1045
1147
          /* We are done modifying process list, so unblock signal */
1046
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1047
 
                                               &sigchld_action.sa_mask,
1048
 
                                               NULL));
 
1148
          ret = (int)(TEMP_FAILURE_RETRY
 
1149
                      (sigprocmask(SIG_UNBLOCK,
 
1150
                                   &sigchld_action.sa_mask, NULL)));
1049
1151
          if(ret < 0){
1050
 
            perror("sigprocmask");
1051
 
            exitstatus = EXIT_FAILURE;
 
1152
            error(0, errno, "sigprocmask");
 
1153
            exitstatus = EX_OSERR;
1052
1154
            goto fallback;
1053
1155
          }
1054
1156
          
1064
1166
        bool bret = print_out_password(proc->buffer,
1065
1167
                                       proc->buffer_length);
1066
1168
        if(not bret){
1067
 
          perror("print_out_password");
1068
 
          exitstatus = EXIT_FAILURE;
 
1169
          error(0, errno, "print_out_password");
 
1170
          exitstatus = EX_IOERR;
1069
1171
        }
1070
1172
        goto fallback;
1071
1173
      }
1072
1174
      
1073
1175
      /* This process has not completed.  Does it have any output? */
1074
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1176
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1177
                                                         warning from
 
1178
                                                         -Wconversion */
1075
1179
        /* This process had nothing to say at this time */
1076
1180
        proc = proc->next;
1077
1181
        continue;
1081
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1082
1186
                               + (size_t) BUFFER_SIZE);
1083
1187
        if(proc->buffer == NULL){
1084
 
          perror("malloc");
1085
 
          exitstatus = EXIT_FAILURE;
 
1188
          error(0, errno, "malloc");
 
1189
          exitstatus = EX_OSERR;
1086
1190
          goto fallback;
1087
1191
        }
1088
1192
        proc->buffer_size += BUFFER_SIZE;
1109
1213
  
1110
1214
 fallback:
1111
1215
  
1112
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1216
  if(plugin_list == NULL or (exitstatus != EXIT_SUCCESS
 
1217
                             and exitstatus != EX_OK)){
1113
1218
    /* Fallback if all plugins failed, none are found or an error
1114
1219
       occured */
1115
1220
    bool bret;
1123
1228
    }
1124
1229
    bret = print_out_password(passwordbuffer, len);
1125
1230
    if(not bret){
1126
 
      perror("print_out_password");
1127
 
      exitstatus = EXIT_FAILURE;
 
1231
      error(0, errno, "print_out_password");
 
1232
      exitstatus = EX_IOERR;
1128
1233
    }
1129
1234
  }
1130
1235
  
1131
1236
  /* Restore old signal handler */
1132
1237
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1133
1238
  if(ret == -1){
1134
 
    perror("sigaction");
1135
 
    exitstatus = EXIT_FAILURE;
 
1239
    error(0, errno, "sigaction");
 
1240
    exitstatus = EX_OSERR;
1136
1241
  }
1137
1242
  
1138
1243
  if(custom_argv != NULL){
1153
1258
      ret = kill(p->pid, SIGTERM);
1154
1259
      if(ret == -1 and errno != ESRCH){
1155
1260
        /* Set-uid proccesses might not get closed */
1156
 
        perror("kill");
 
1261
        error(0, errno, "kill");
1157
1262
      }
1158
1263
    }
1159
1264
  }
1163
1268
    ret = wait(NULL);
1164
1269
  } while(ret >= 0);
1165
1270
  if(errno != ECHILD){
1166
 
    perror("wait");
 
1271
    error(0, errno, "wait");
1167
1272
  }
1168
1273
  
1169
1274
  free_plugin_list();