/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-01-08 03:54:06 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090108035406-01uqkv9xqgxlnw7c
Change the default value of the "checker" option command to make the
checker command be run-time dependent on the "host" client attribute
instead of reading it only once on startup:

* clients.conf (checker): Changed default commented-out value to
                          "fping -q -- %%(host)s".
* mandos (main): Changed default value for the "checker" config option
                 to "fping -q -- %%(host)s".
* mandos-clients.conf.xml (OPTIONS): Document changed default value.
  (EXAMPLE): Changed value of "checker".

* plugins.d/password-prompt.c: Whitespace changes only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
                                   SIG_UNBLOCK, kill() */
66
66
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
67
 
69
68
#define BUFFER_SIZE 256
70
69
 
89
88
  size_t buffer_size;
90
89
  size_t buffer_length;
91
90
  bool eof;
92
 
  volatile sig_atomic_t completed;
93
 
  int status;
 
91
  volatile bool completed;
 
92
  volatile int status;
94
93
  struct plugin *next;
95
94
} plugin;
96
95
 
100
99
   or if none is found, creates a new one */
101
100
static plugin *getplugin(char *name){
102
101
  /* Check for exiting plugin with that name */
103
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
104
 
    if((p->name == name)
105
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
102
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
103
    if ((p->name == name)
 
104
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
105
      return p;
107
106
    }
108
107
  }
109
108
  /* Create a new plugin */
110
109
  plugin *new_plugin = malloc(sizeof(plugin));
111
 
  if(new_plugin == NULL){
 
110
  if (new_plugin == NULL){
112
111
    return NULL;
113
112
  }
114
113
  char *copy_name = NULL;
115
114
  if(name != NULL){
116
115
    copy_name = strdup(name);
117
116
    if(copy_name == NULL){
118
 
      free(new_plugin);
119
117
      return NULL;
120
118
    }
121
119
  }
122
120
  
123
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
121
  *new_plugin = (plugin) { .name = copy_name,
 
122
                           .argc = 1,
 
123
                           .disabled = false,
 
124
                           .next = plugin_list };
127
125
  
128
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
 
  if(new_plugin->argv == NULL){
 
127
  if (new_plugin->argv == NULL){
130
128
    free(copy_name);
131
129
    free(new_plugin);
132
130
    return NULL;
223
221
/* Mark processes as completed when they exit, and save their exit
224
222
   status. */
225
223
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
227
224
  while(true){
228
225
    plugin *proc = plugin_list;
229
226
    int status;
233
230
      break;
234
231
    }
235
232
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
233
      if (errno != ECHILD){
 
234
        perror("waitpid");
239
235
      }
240
 
      perror("waitpid");
 
236
      /* No child processes */
 
237
      break;
241
238
    }
242
239
    
243
240
    /* A child exited, find it in process_list */
249
246
      continue;
250
247
    }
251
248
    proc->status = status;
252
 
    proc->completed = 1;
 
249
    proc->completed = true;
253
250
  }
254
 
  errno = old_errno;
255
251
}
256
252
 
257
253
/* Prints out a password to stdout */
312
308
  struct dirent *dirst;
313
309
  struct stat st;
314
310
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
311
  int ret, maxfd = 0;
316
312
  ssize_t sret;
317
 
  intmax_t tmpmax;
318
313
  uid_t uid = 65534;
319
314
  gid_t gid = 65534;
320
315
  bool debug = false;
377
372
    { .name = NULL }
378
373
  };
379
374
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
375
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
376
                     struct argp_state *state) {
 
377
    switch (key) {
383
378
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
 
379
      if (arg != NULL){
385
380
        char *p;
386
381
        while((p = strsep(&arg, ",")) != NULL){
387
382
          if(p[0] == '\0'){
403
398
      }
404
399
      break;
405
400
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
 
401
      if (arg != NULL){
407
402
        char *p_name = strsep(&arg, ":");
408
403
        if(p_name[0] == '\0' or arg == NULL){
409
404
          break;
440
435
      }
441
436
      break;
442
437
    case 'd':                   /* --disable */
443
 
      if(arg != NULL){
 
438
      if (arg != NULL){
444
439
        plugin *p = getplugin(arg);
445
440
        if(p == NULL){
446
441
          return ARGP_ERR_UNKNOWN;
449
444
      }
450
445
      break;
451
446
    case 'e':                   /* --enable */
452
 
      if(arg != NULL){
 
447
      if (arg != NULL){
453
448
        plugin *p = getplugin(arg);
454
449
        if(p == NULL){
455
450
          return ARGP_ERR_UNKNOWN;
468
463
      /* This is already done by parse_opt_config_file() */
469
464
      break;
470
465
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
474
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
 
                PRIdMAX "\n", arg, (intmax_t)uid);
476
 
      } else {
477
 
        uid = (uid_t)tmpmax;
478
 
      }
 
466
      uid = (uid_t)strtol(arg, NULL, 10);
479
467
      break;
480
468
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
484
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
 
                PRIdMAX "\n", arg, (intmax_t)gid);
486
 
      } else {
487
 
        gid = (gid_t)tmpmax;
488
 
      }
 
469
      gid = (gid_t)strtol(arg, NULL, 10);
489
470
      break;
490
471
    case 132:                   /* --debug */
491
472
      debug = true;
492
473
      break;
493
 
/*
494
 
 * When adding more options before this line, remember to also add a
495
 
 * "case" to the "parse_opt_config_file" function below.
496
 
 */
497
474
    case ARGP_KEY_ARG:
498
475
      /* Cryptsetup always passes an argument, which is an empty
499
476
         string if "none" was specified in /etc/crypttab.  So if
512
489
  
513
490
  /* This option parser is the same as parse_opt() above, except it
514
491
     ignores everything but the --config-file option. */
515
 
  error_t parse_opt_config_file(int key, char *arg,
516
 
                                __attribute__((unused))
517
 
                                struct argp_state *state){
518
 
    switch(key){
 
492
  error_t parse_opt_config_file (int key, char *arg,
 
493
                                 __attribute__((unused))
 
494
                                 struct argp_state *state) {
 
495
    switch (key) {
519
496
    case 'g':                   /* --global-options */
520
497
    case 'G':                   /* --global-env */
521
498
    case 'o':                   /* --options-for */
548
525
                       .args_doc = "",
549
526
                       .doc = "Mandos plugin runner -- Run plugins" };
550
527
  
551
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
528
  /* Parse using the parse_opt_config_file in order to get the custom
552
529
     config file location, if any. */
553
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
554
 
  if(ret == ARGP_ERR_UNKNOWN){
 
530
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
531
  if (ret == ARGP_ERR_UNKNOWN){
555
532
    fprintf(stderr, "Unknown error while parsing arguments\n");
556
533
    exitstatus = EXIT_FAILURE;
557
534
    goto fallback;
561
538
  argp.parser = parse_opt;
562
539
  
563
540
  /* Open the configfile if available */
564
 
  if(argfile == NULL){
 
541
  if (argfile == NULL){
565
542
    conffp = fopen(AFILE, "r");
566
543
  } else {
567
544
    conffp = fopen(argfile, "r");
622
599
  } else {
623
600
    /* Check for harmful errors and go to fallback. Other errors might
624
601
       not affect opening plugins */
625
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
602
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
626
603
      perror("fopen");
627
604
      exitstatus = EXIT_FAILURE;
628
605
      goto fallback;
631
608
  /* If there was any arguments from configuration file,
632
609
     pass them to parser as command arguments */
633
610
  if(custom_argv != NULL){
634
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
635
 
                     0, NULL);
636
 
    if(ret == ARGP_ERR_UNKNOWN){
 
611
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
612
                      0, NULL);
 
613
    if (ret == ARGP_ERR_UNKNOWN){
637
614
      fprintf(stderr, "Unknown error while parsing arguments\n");
638
615
      exitstatus = EXIT_FAILURE;
639
616
      goto fallback;
642
619
  
643
620
  /* Parse actual command line arguments, to let them override the
644
621
     config file */
645
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
646
 
  if(ret == ARGP_ERR_UNKNOWN){
 
622
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
623
  if (ret == ARGP_ERR_UNKNOWN){
647
624
    fprintf(stderr, "Unknown error while parsing arguments\n");
648
625
    exitstatus = EXIT_FAILURE;
649
626
    goto fallback;
656
633
      for(char **a = p->argv; *a != NULL; a++){
657
634
        fprintf(stderr, "\tArg: %s\n", *a);
658
635
      }
659
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
636
      fprintf(stderr, "...and %u environment variables\n", p->envc);
660
637
      for(char **a = p->environ; *a != NULL; a++){
661
638
        fprintf(stderr, "\t%s\n", *a);
662
639
      }
665
642
  
666
643
  /* Strip permissions down to nobody */
667
644
  ret = setuid(uid);
668
 
  if(ret == -1){
 
645
  if (ret == -1){
669
646
    perror("setuid");
670
647
  }  
671
648
  setgid(gid);
672
 
  if(ret == -1){
 
649
  if (ret == -1){
673
650
    perror("setgid");
674
651
  }
675
652
  
676
 
  if(plugindir == NULL){
 
653
  if (plugindir == NULL){
677
654
    dir = opendir(PDIR);
678
655
  } else {
679
656
    dir = opendir(plugindir);
706
683
    
707
684
    /* All directory entries have been processed */
708
685
    if(dirst == NULL){
709
 
      if(errno == EBADF){
 
686
      if (errno == EBADF){
710
687
        perror("readdir");
711
688
        exitstatus = EXIT_FAILURE;
712
689
        goto fallback;
772
749
    }
773
750
    
774
751
    ret = stat(filename, &st);
775
 
    if(ret == -1){
 
752
    if (ret == -1){
776
753
      perror("stat");
777
754
      free(filename);
778
755
      continue;
779
756
    }
780
757
 
781
758
    /* Ignore non-executable files */
782
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
759
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
760
      if(debug){
784
761
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
785
762
                " with bad type or mode\n", filename);
832
809
    
833
810
    int pipefd[2];
834
811
    ret = pipe(pipefd);
835
 
    if(ret == -1){
 
812
    if (ret == -1){
836
813
      perror("pipe");
837
814
      exitstatus = EXIT_FAILURE;
838
815
      goto fallback;
851
828
      goto fallback;
852
829
    }
853
830
    /* Block SIGCHLD until process is safely in process list */
854
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
831
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
855
832
    if(ret < 0){
856
833
      perror("sigprocmask");
857
834
      exitstatus = EXIT_FAILURE;
905
882
    close(pipefd[1]);           /* Close unused write end of pipe */
906
883
    free(filename);
907
884
    plugin *new_plugin = getplugin(dirst->d_name);
908
 
    if(new_plugin == NULL){
 
885
    if (new_plugin == NULL){
909
886
      perror("getplugin");
910
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
887
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
888
      if(ret < 0){
912
889
        perror("sigprocmask");
913
890
      }
920
897
    
921
898
    /* Unblock SIGCHLD so signal handler can be run if this process
922
899
       has already completed */
923
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
900
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
924
901
    if(ret < 0){
925
902
      perror("sigprocmask");
926
903
      exitstatus = EXIT_FAILURE;
929
906
    
930
907
    FD_SET(new_plugin->fd, &rfds_all);
931
908
    
932
 
    if(maxfd < new_plugin->fd){
 
909
    if (maxfd < new_plugin->fd){
933
910
      maxfd = new_plugin->fd;
934
911
    }
935
912
  }
952
929
  while(plugin_list){
953
930
    fd_set rfds = rfds_all;
954
931
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
955
 
    if(select_ret == -1){
 
932
    if (select_ret == -1){
956
933
      perror("select");
957
934
      exitstatus = EXIT_FAILURE;
958
935
      goto fallback;
961
938
       from one of them */
962
939
    for(plugin *proc = plugin_list; proc != NULL;){
963
940
      /* Is this process completely done? */
964
 
      if(proc->completed and proc->eof){
 
941
      if(proc->eof and proc->completed){
965
942
        /* Only accept the plugin output if it exited cleanly */
966
943
        if(not WIFEXITED(proc->status)
967
944
           or WEXITSTATUS(proc->status) != 0){
969
946
 
970
947
          if(debug){
971
948
            if(WIFEXITED(proc->status)){
972
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
973
 
                      " %d\n", (intmax_t) (proc->pid),
 
949
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
950
                      (unsigned int) (proc->pid),
974
951
                      WEXITSTATUS(proc->status));
975
 
            } else if(WIFSIGNALED(proc->status)){
976
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
977
 
                      " %d\n", (intmax_t) (proc->pid),
 
952
            } else if(WIFSIGNALED(proc->status)) {
 
953
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
954
                      (unsigned int) (proc->pid),
978
955
                      WTERMSIG(proc->status));
979
956
            } else if(WCOREDUMP(proc->status)){
980
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
981
 
                      (intmax_t) (proc->pid));
 
957
              fprintf(stderr, "Plugin %d dumped core\n",
 
958
                      (unsigned int) (proc->pid));
982
959
            }
983
960
          }
984
961
          
998
975
          proc = next_plugin;
999
976
          
1000
977
          /* We are done modifying process list, so unblock signal */
1001
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1002
 
                            NULL);
 
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
979
                             NULL);
1003
980
          if(ret < 0){
1004
981
            perror("sigprocmask");
1005
982
            exitstatus = EXIT_FAILURE;
1034
1011
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1035
1012
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1036
1013
                               + (size_t) BUFFER_SIZE);
1037
 
        if(proc->buffer == NULL){
 
1014
        if (proc->buffer == NULL){
1038
1015
          perror("malloc");
1039
1016
          exitstatus = EXIT_FAILURE;
1040
1017
          goto fallback;