/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

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){
385
 
        char *plugin_option;
386
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
387
 
          if(plugin_option[0] == '\0'){
 
379
      if (arg != NULL){
 
380
        char *p;
 
381
        while((p = strsep(&arg, ",")) != NULL){
 
382
          if(p[0] == '\0'){
388
383
            continue;
389
384
          }
390
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
385
          if(not add_argument(getplugin(NULL), p)){
391
386
            perror("add_argument");
392
387
            return ARGP_ERR_UNKNOWN;
393
388
          }
403
398
      }
404
399
      break;
405
400
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
407
 
        char *plugin_name = strsep(&arg, ":");
408
 
        if(plugin_name[0] == '\0'){
409
 
          break;
410
 
        }
411
 
        char *plugin_option;
412
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
413
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
401
      if (arg != NULL){
 
402
        char *p_name = strsep(&arg, ":");
 
403
        if(p_name[0] == '\0' or arg == NULL){
 
404
          break;
 
405
        }
 
406
        char *opt = strsep(&arg, ":");
 
407
        if(opt[0] == '\0' or opt == NULL){
 
408
          break;
 
409
        }
 
410
        char *p;
 
411
        while((p = strsep(&opt, ",")) != NULL){
 
412
          if(p[0] == '\0'){
 
413
            continue;
 
414
          }
 
415
          if(not add_argument(getplugin(p_name), p)){
414
416
            perror("add_argument");
415
417
            return ARGP_ERR_UNKNOWN;
416
418
          }
433
435
      }
434
436
      break;
435
437
    case 'd':                   /* --disable */
436
 
      if(arg != NULL){
 
438
      if (arg != NULL){
437
439
        plugin *p = getplugin(arg);
438
440
        if(p == NULL){
439
441
          return ARGP_ERR_UNKNOWN;
442
444
      }
443
445
      break;
444
446
    case 'e':                   /* --enable */
445
 
      if(arg != NULL){
 
447
      if (arg != NULL){
446
448
        plugin *p = getplugin(arg);
447
449
        if(p == NULL){
448
450
          return ARGP_ERR_UNKNOWN;
461
463
      /* This is already done by parse_opt_config_file() */
462
464
      break;
463
465
    case 130:                   /* --userid */
464
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
465
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
466
 
         or arg[numchars] != '\0'){
467
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
 
                PRIdMAX "\n", arg, (intmax_t)uid);
469
 
      } else {
470
 
        uid = (uid_t)tmpmax;
471
 
      }
 
466
      uid = (uid_t)strtol(arg, NULL, 10);
472
467
      break;
473
468
    case 131:                   /* --groupid */
474
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
475
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
476
 
         or arg[numchars] != '\0'){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
 
                PRIdMAX "\n", arg, (intmax_t)gid);
479
 
      } else {
480
 
        gid = (gid_t)tmpmax;
481
 
      }
 
469
      gid = (gid_t)strtol(arg, NULL, 10);
482
470
      break;
483
471
    case 132:                   /* --debug */
484
472
      debug = true;
485
473
      break;
486
 
/*
487
 
 * When adding more options before this line, remember to also add a
488
 
 * "case" to the "parse_opt_config_file" function below.
489
 
 */
490
474
    case ARGP_KEY_ARG:
491
475
      /* Cryptsetup always passes an argument, which is an empty
492
476
         string if "none" was specified in /etc/crypttab.  So if
505
489
  
506
490
  /* This option parser is the same as parse_opt() above, except it
507
491
     ignores everything but the --config-file option. */
508
 
  error_t parse_opt_config_file(int key, char *arg,
509
 
                                __attribute__((unused))
510
 
                                struct argp_state *state){
511
 
    switch(key){
 
492
  error_t parse_opt_config_file (int key, char *arg,
 
493
                                 __attribute__((unused))
 
494
                                 struct argp_state *state) {
 
495
    switch (key) {
512
496
    case 'g':                   /* --global-options */
513
497
    case 'G':                   /* --global-env */
514
498
    case 'o':                   /* --options-for */
541
525
                       .args_doc = "",
542
526
                       .doc = "Mandos plugin runner -- Run plugins" };
543
527
  
544
 
  /* 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
545
529
     config file location, if any. */
546
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
547
 
  if(ret == ARGP_ERR_UNKNOWN){
 
530
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
531
  if (ret == ARGP_ERR_UNKNOWN){
548
532
    fprintf(stderr, "Unknown error while parsing arguments\n");
549
533
    exitstatus = EXIT_FAILURE;
550
534
    goto fallback;
554
538
  argp.parser = parse_opt;
555
539
  
556
540
  /* Open the configfile if available */
557
 
  if(argfile == NULL){
 
541
  if (argfile == NULL){
558
542
    conffp = fopen(AFILE, "r");
559
543
  } else {
560
544
    conffp = fopen(argfile, "r");
615
599
  } else {
616
600
    /* Check for harmful errors and go to fallback. Other errors might
617
601
       not affect opening plugins */
618
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
602
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
619
603
      perror("fopen");
620
604
      exitstatus = EXIT_FAILURE;
621
605
      goto fallback;
624
608
  /* If there was any arguments from configuration file,
625
609
     pass them to parser as command arguments */
626
610
  if(custom_argv != NULL){
627
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
628
 
                     0, NULL);
629
 
    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){
630
614
      fprintf(stderr, "Unknown error while parsing arguments\n");
631
615
      exitstatus = EXIT_FAILURE;
632
616
      goto fallback;
635
619
  
636
620
  /* Parse actual command line arguments, to let them override the
637
621
     config file */
638
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
639
 
  if(ret == ARGP_ERR_UNKNOWN){
 
622
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
623
  if (ret == ARGP_ERR_UNKNOWN){
640
624
    fprintf(stderr, "Unknown error while parsing arguments\n");
641
625
    exitstatus = EXIT_FAILURE;
642
626
    goto fallback;
649
633
      for(char **a = p->argv; *a != NULL; a++){
650
634
        fprintf(stderr, "\tArg: %s\n", *a);
651
635
      }
652
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
636
      fprintf(stderr, "...and %u environment variables\n", p->envc);
653
637
      for(char **a = p->environ; *a != NULL; a++){
654
638
        fprintf(stderr, "\t%s\n", *a);
655
639
      }
657
641
  }
658
642
  
659
643
  /* Strip permissions down to nobody */
 
644
  ret = setuid(uid);
 
645
  if (ret == -1){
 
646
    perror("setuid");
 
647
  }  
660
648
  setgid(gid);
661
 
  if(ret == -1){
 
649
  if (ret == -1){
662
650
    perror("setgid");
663
651
  }
664
 
  ret = setuid(uid);
665
 
  if(ret == -1){
666
 
    perror("setuid");
667
 
  }
668
652
  
669
 
  if(plugindir == NULL){
 
653
  if (plugindir == NULL){
670
654
    dir = opendir(PDIR);
671
655
  } else {
672
656
    dir = opendir(plugindir);
699
683
    
700
684
    /* All directory entries have been processed */
701
685
    if(dirst == NULL){
702
 
      if(errno == EBADF){
 
686
      if (errno == EBADF){
703
687
        perror("readdir");
704
688
        exitstatus = EXIT_FAILURE;
705
689
        goto fallback;
765
749
    }
766
750
    
767
751
    ret = stat(filename, &st);
768
 
    if(ret == -1){
 
752
    if (ret == -1){
769
753
      perror("stat");
770
754
      free(filename);
771
755
      continue;
772
756
    }
773
757
 
774
758
    /* Ignore non-executable files */
775
 
    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)){
776
760
      if(debug){
777
761
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
762
                " with bad type or mode\n", filename);
825
809
    
826
810
    int pipefd[2];
827
811
    ret = pipe(pipefd);
828
 
    if(ret == -1){
 
812
    if (ret == -1){
829
813
      perror("pipe");
830
814
      exitstatus = EXIT_FAILURE;
831
815
      goto fallback;
844
828
      goto fallback;
845
829
    }
846
830
    /* Block SIGCHLD until process is safely in process list */
847
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
831
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
848
832
    if(ret < 0){
849
833
      perror("sigprocmask");
850
834
      exitstatus = EXIT_FAILURE;
898
882
    close(pipefd[1]);           /* Close unused write end of pipe */
899
883
    free(filename);
900
884
    plugin *new_plugin = getplugin(dirst->d_name);
901
 
    if(new_plugin == NULL){
 
885
    if (new_plugin == NULL){
902
886
      perror("getplugin");
903
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
887
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
904
888
      if(ret < 0){
905
889
        perror("sigprocmask");
906
890
      }
913
897
    
914
898
    /* Unblock SIGCHLD so signal handler can be run if this process
915
899
       has already completed */
916
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
900
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
917
901
    if(ret < 0){
918
902
      perror("sigprocmask");
919
903
      exitstatus = EXIT_FAILURE;
922
906
    
923
907
    FD_SET(new_plugin->fd, &rfds_all);
924
908
    
925
 
    if(maxfd < new_plugin->fd){
 
909
    if (maxfd < new_plugin->fd){
926
910
      maxfd = new_plugin->fd;
927
911
    }
928
912
  }
929
913
  
930
914
  closedir(dir);
931
915
  dir = NULL;
932
 
  free_plugin(getplugin(NULL));
933
916
  
934
917
  for(plugin *p = plugin_list; p != NULL; p = p->next){
935
918
    if(p->pid != 0){
946
929
  while(plugin_list){
947
930
    fd_set rfds = rfds_all;
948
931
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
932
    if (select_ret == -1){
950
933
      perror("select");
951
934
      exitstatus = EXIT_FAILURE;
952
935
      goto fallback;
955
938
       from one of them */
956
939
    for(plugin *proc = plugin_list; proc != NULL;){
957
940
      /* Is this process completely done? */
958
 
      if(proc->completed and proc->eof){
 
941
      if(proc->eof and proc->completed){
959
942
        /* Only accept the plugin output if it exited cleanly */
960
943
        if(not WIFEXITED(proc->status)
961
944
           or WEXITSTATUS(proc->status) != 0){
963
946
 
964
947
          if(debug){
965
948
            if(WIFEXITED(proc->status)){
966
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
967
 
                      " status %d\n", proc->name,
968
 
                      (intmax_t) (proc->pid),
 
949
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
950
                      (unsigned int) (proc->pid),
969
951
                      WEXITSTATUS(proc->status));
970
 
            } else if(WIFSIGNALED(proc->status)){
971
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
973
 
                      (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),
974
955
                      WTERMSIG(proc->status));
975
956
            } else if(WCOREDUMP(proc->status)){
976
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
957
              fprintf(stderr, "Plugin %d dumped core\n",
 
958
                      (unsigned int) (proc->pid));
978
959
            }
979
960
          }
980
961
          
994
975
          proc = next_plugin;
995
976
          
996
977
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
979
                             NULL);
999
980
          if(ret < 0){
1000
981
            perror("sigprocmask");
1001
982
            exitstatus = EXIT_FAILURE;
1030
1011
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1031
1012
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1032
1013
                               + (size_t) BUFFER_SIZE);
1033
 
        if(proc->buffer == NULL){
 
1014
        if (proc->buffer == NULL){
1034
1015
          perror("malloc");
1035
1016
          exitstatus = EXIT_FAILURE;
1036
1017
          goto fallback;