/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: 2009-09-17 01:21:27 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090917012127-4kwzquwuvudwucuv
* debian/control (Standards-Version): Updated to "2.8.3".

* mandos-client.conf.xml (OPTIONS/timeout): Add that a successful
                                            client request will also
                                            reset the timeout.
* mandos.xml (CHECKING): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
29
                                   EXIT_SUCCESS, realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), 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, fdopendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
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 */
72
71
 
73
72
#define BUFFER_SIZE 256
74
73
 
103
102
/* Gets an existing plugin based on name,
104
103
   or if none is found, creates a new one */
105
104
static plugin *getplugin(char *name){
106
 
  /* Check for existing plugin with that name */
 
105
  /* Check for exiting plugin with that name */
107
106
  for(plugin *p = plugin_list; p != NULL; p = p->next){
108
107
    if((p->name == name)
109
108
       or (p->name and name and (strcmp(p->name, name) == 0))){
124
123
      copy_name = strdup(name);
125
124
    } while(copy_name == NULL and errno == EINTR);
126
125
    if(copy_name == NULL){
127
 
      int e = errno;
128
126
      free(new_plugin);
129
 
      errno = e;
130
127
      return NULL;
131
128
    }
132
129
  }
140
137
    new_plugin->argv = malloc(sizeof(char *) * 2);
141
138
  } while(new_plugin->argv == NULL and errno == EINTR);
142
139
  if(new_plugin->argv == NULL){
143
 
    int e = errno;
144
140
    free(copy_name);
145
141
    free(new_plugin);
146
 
    errno = e;
147
142
    return NULL;
148
143
  }
149
144
  new_plugin->argv[0] = copy_name;
153
148
    new_plugin->environ = malloc(sizeof(char *));
154
149
  } while(new_plugin->environ == NULL and errno == EINTR);
155
150
  if(new_plugin->environ == NULL){
156
 
    int e = errno;
157
151
    free(copy_name);
158
152
    free(new_plugin->argv);
159
153
    free(new_plugin);
160
 
    errno = e;
161
154
    return NULL;
162
155
  }
163
156
  new_plugin->environ[0] = NULL;
401
394
      .doc = "Group ID the plugins will run as", .group = 3 },
402
395
    { .name = "debug", .key = 132,
403
396
      .doc = "Debug mode", .group = 4 },
404
 
    /*
405
 
     * These reproduce what we would get without ARGP_NO_HELP
406
 
     */
407
 
    { .name = "help", .key = '?',
408
 
      .doc = "Give this help list", .group = -1 },
409
 
    { .name = "usage", .key = -3,
410
 
      .doc = "Give a short usage message", .group = -1 },
411
 
    { .name = "version", .key = 'V',
412
 
      .doc = "Print program version", .group = -1 },
413
397
    { .name = NULL }
414
398
  };
415
399
  
416
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
 
    errno = 0;
 
400
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
401
                    struct argp_state *state){
418
402
    switch(key){
419
403
      char *tmp;
420
404
      intmax_t tmpmax;
421
405
    case 'g':                   /* --global-options */
422
 
      {
 
406
      if(arg != NULL){
423
407
        char *plugin_option;
424
408
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
409
          if(plugin_option[0] == '\0'){
 
410
            continue;
 
411
          }
425
412
          if(not add_argument(getplugin(NULL), plugin_option)){
426
 
            break;
 
413
            perror("add_argument");
 
414
            return ARGP_ERR_UNKNOWN;
427
415
          }
428
416
        }
429
417
      }
430
418
      break;
431
419
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
420
      if(arg == NULL){
 
421
        break;
 
422
      }
 
423
      if(not add_environment(getplugin(NULL), arg, true)){
 
424
        perror("add_environment");
 
425
      }
433
426
      break;
434
427
    case 'o':                   /* --options-for */
435
 
      {
436
 
        char *option_list = strchr(arg, ':');
437
 
        if(option_list == NULL){
438
 
          argp_error(state, "No colon in \"%s\"", arg);
439
 
          errno = EINVAL;
440
 
          break;
441
 
        }
442
 
        *option_list = '\0';
443
 
        option_list++;
444
 
        if(arg[0] == '\0'){
445
 
          argp_error(state, "Empty plugin name");
446
 
          errno = EINVAL;
447
 
          break;
448
 
        }
449
 
        char *option;
450
 
        while((option = strsep(&option_list, ",")) != NULL){
451
 
          if(not add_argument(getplugin(arg), option)){
452
 
            break;
 
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;
453
438
          }
454
439
        }
455
440
      }
456
441
      break;
457
442
    case 'E':                   /* --env-for */
 
443
      if(arg == NULL){
 
444
        break;
 
445
      }
458
446
      {
459
447
        char *envdef = strchr(arg, ':');
460
448
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
449
          break;
464
450
        }
465
451
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
452
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
453
          perror("add_environment");
471
454
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
455
      }
474
456
      break;
475
457
    case 'd':                   /* --disable */
476
 
      {
 
458
      if(arg != NULL){
477
459
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
460
        if(p == NULL){
 
461
          return ARGP_ERR_UNKNOWN;
480
462
        }
 
463
        p->disabled = true;
481
464
      }
482
465
      break;
483
466
    case 'e':                   /* --enable */
484
 
      {
 
467
      if(arg != NULL){
485
468
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
469
        if(p == NULL){
 
470
          return ARGP_ERR_UNKNOWN;
488
471
        }
 
472
        p->disabled = false;
489
473
      }
490
474
      break;
491
475
    case 128:                   /* --plugin-dir */
492
476
      free(plugindir);
493
477
      plugindir = strdup(arg);
 
478
      if(plugindir == NULL){
 
479
        perror("strdup");
 
480
      }
494
481
      break;
495
482
    case 129:                   /* --config-file */
496
483
      /* This is already done by parse_opt_config_file() */
497
484
      break;
498
485
    case 130:                   /* --userid */
 
486
      errno = 0;
499
487
      tmpmax = strtoimax(arg, &tmp, 10);
500
488
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
489
         or tmpmax != (uid_t)tmpmax){
502
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
 
                   PRIdMAX, arg, (intmax_t)uid);
504
 
        break;
 
490
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
491
                PRIdMAX "\n", arg, (intmax_t)uid);
 
492
      } else {
 
493
        uid = (uid_t)tmpmax;
505
494
      }
506
 
      uid = (uid_t)tmpmax;
507
495
      break;
508
496
    case 131:                   /* --groupid */
 
497
      errno = 0;
509
498
      tmpmax = strtoimax(arg, &tmp, 10);
510
499
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
500
         or tmpmax != (gid_t)tmpmax){
512
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
 
                   PRIdMAX, arg, (intmax_t)gid);
514
 
        break;
 
501
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
502
                PRIdMAX "\n", arg, (intmax_t)gid);
 
503
      } else {
 
504
        gid = (gid_t)tmpmax;
515
505
      }
516
 
      gid = (gid_t)tmpmax;
517
506
      break;
518
507
    case 132:                   /* --debug */
519
508
      debug = true;
520
509
      break;
521
 
      /*
522
 
       * These reproduce what we would get without ARGP_NO_HELP
523
 
       */
524
 
    case '?':                   /* --help */
525
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
526
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
527
 
    case -3:                    /* --usage */
528
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
529
 
      argp_state_help(state, state->out_stream,
530
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
531
 
    case 'V':                   /* --version */
532
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
533
 
      exit(EXIT_SUCCESS);
534
 
      break;
535
510
/*
536
511
 * When adding more options before this line, remember to also add a
537
512
 * "case" to the "parse_opt_config_file" function below.
540
515
      /* Cryptsetup always passes an argument, which is an empty
541
516
         string if "none" was specified in /etc/crypttab.  So if
542
517
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
518
      if(arg[0] != '\0'){
 
519
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
520
      }
 
521
      break;
 
522
    case ARGP_KEY_END:
 
523
      break;
546
524
    default:
547
525
      return ARGP_ERR_UNKNOWN;
548
526
    }
549
 
    return errno;               /* Set to 0 at start */
 
527
    return 0;
550
528
  }
551
529
  
552
530
  /* This option parser is the same as parse_opt() above, except it
554
532
  error_t parse_opt_config_file(int key, char *arg,
555
533
                                __attribute__((unused))
556
534
                                struct argp_state *state){
557
 
    errno = 0;
558
535
    switch(key){
559
536
    case 'g':                   /* --global-options */
560
537
    case 'G':                   /* --global-env */
567
544
    case 129:                   /* --config-file */
568
545
      free(argfile);
569
546
      argfile = strdup(arg);
 
547
      if(argfile == NULL){
 
548
        perror("strdup");
 
549
      }
570
550
      break;
571
551
    case 130:                   /* --userid */
572
552
    case 131:                   /* --groupid */
573
553
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
554
    case ARGP_KEY_ARG:
 
555
    case ARGP_KEY_END:
578
556
      break;
579
557
    default:
580
558
      return ARGP_ERR_UNKNOWN;
581
559
    }
582
 
    return errno;
 
560
    return 0;
583
561
  }
584
562
  
585
563
  struct argp argp = { .options = options,
589
567
  
590
568
  /* Parse using parse_opt_config_file() in order to get the custom
591
569
     config file location, if any. */
592
 
  ret = argp_parse(&argp, argc, argv,
593
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
594
 
                   NULL, NULL);
595
 
  switch(ret){
596
 
  case 0:
597
 
    break;
598
 
  case ENOMEM:
599
 
  default:
600
 
    errno = ret;
601
 
    perror("argp_parse");
602
 
    exitstatus = EX_OSERR;
603
 
    goto fallback;
604
 
  case EINVAL:
605
 
    exitstatus = EX_USAGE;
 
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;
606
574
    goto fallback;
607
575
  }
608
576
  
685
653
      goto fallback;
686
654
    }
687
655
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
656
  /* If there was any arguments from configuration file,
 
657
     pass them to parser as command arguments */
690
658
  if(custom_argv != NULL){
691
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
692
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
693
 
                     NULL, NULL);
694
 
    switch(ret){
695
 
    case 0:
696
 
      break;
697
 
    case ENOMEM:
698
 
    default:
699
 
      errno = ret;
700
 
      perror("argp_parse");
701
 
      exitstatus = EX_OSERR;
702
 
      goto fallback;
703
 
    case EINVAL:
704
 
      exitstatus = EX_CONFIG;
 
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;
705
664
      goto fallback;
706
665
    }
707
666
  }
708
667
  
709
668
  /* Parse actual command line arguments, to let them override the
710
669
     config file */
711
 
  ret = argp_parse(&argp, argc, argv,
712
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
713
 
                   NULL, NULL);
714
 
  switch(ret){
715
 
  case 0:
716
 
    break;
717
 
  case ENOMEM:
718
 
  default:
719
 
    errno = ret;
720
 
    perror("argp_parse");
721
 
    exitstatus = EX_OSERR;
722
 
    goto fallback;
723
 
  case EINVAL:
724
 
    exitstatus = EX_USAGE;
 
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;
725
674
    goto fallback;
726
675
  }
727
676
  
749
698
    perror("setuid");
750
699
  }
751
700
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
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 */
753
714
  {
754
 
    int dir_fd = -1;
755
 
    if(plugindir == NULL){
756
 
      dir_fd = open(PDIR, O_RDONLY |
757
 
#ifdef O_CLOEXEC
758
 
                    O_CLOEXEC
759
 
#else  /* not O_CLOEXEC */
760
 
                    0
761
 
#endif  /* not O_CLOEXEC */
762
 
                    );
763
 
    } else {
764
 
      dir_fd = open(plugindir, O_RDONLY |
765
 
#ifdef O_CLOEXEC
766
 
                    O_CLOEXEC
767
 
#else  /* not O_CLOEXEC */
768
 
                    0
769
 
#endif  /* not O_CLOEXEC */
770
 
                    );
771
 
    }
772
 
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
775
 
      goto fallback;
776
 
    }
777
 
    
778
 
#ifndef O_CLOEXEC
779
 
  /* Set the FD_CLOEXEC flag on the directory */
780
 
    ret = set_cloexec_flag(dir_fd);
781
 
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
783
 
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
785
 
      goto fallback;
786
 
    }
787
 
#endif  /* O_CLOEXEC */
788
 
    
789
 
    dir = fdopendir(dir_fd);
790
 
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
792
 
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
794
 
      goto fallback;
 
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
      }
795
723
    }
796
724
  }
797
725
  
1040
968
      goto fallback;
1041
969
    }
1042
970
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
971
    FD_SET(new_plugin->fd, &rfds_all);
1045
972
    
1046
973
    if(maxfd < new_plugin->fd){
1047
974
      maxfd = new_plugin->fd;
1101
1028
          }
1102
1029
          
1103
1030
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
 
1031
          FD_CLR(proc->fd, &rfds_all);
1106
1032
          
1107
1033
          /* Block signal while modifying process_list */
1108
1034
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1148
1074
      }
1149
1075
      
1150
1076
      /* This process has not completed.  Does it have any output? */
1151
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1152
 
                                                         warning from
1153
 
                                                         -Wconversion */
 
1077
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1078
        /* This process had nothing to say at this time */
1155
1079
        proc = proc->next;
1156
1080
        continue;