/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-26 23:47:44 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090126234744-c19lb7zn1qsrcwmv
* debian/control (mandos/Depends): Added "python-gobject".

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,  */
67
68
 
68
69
#define BUFFER_SIZE 256
69
70
 
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* Check for exiting plugin with that name */
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))){
 
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))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
111
  if(new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
114
115
  if(name != NULL){
115
116
    copy_name = strdup(name);
116
117
    if(copy_name == NULL){
 
118
      free(new_plugin);
117
119
      return NULL;
118
120
    }
119
121
  }
124
126
                           .next = plugin_list };
125
127
  
126
128
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
129
  if(new_plugin->argv == NULL){
128
130
    free(copy_name);
129
131
    free(new_plugin);
130
132
    return NULL;
230
232
      break;
231
233
    }
232
234
    if(pid == -1){
233
 
      if (errno != ECHILD){
 
235
      if(errno != ECHILD){
234
236
        perror("waitpid");
235
237
      }
236
238
      /* No child processes */
308
310
  struct dirent *dirst;
309
311
  struct stat st;
310
312
  fd_set rfds_all;
311
 
  int ret, maxfd = 0;
 
313
  int ret, numchars, maxfd = 0;
312
314
  ssize_t sret;
 
315
  intmax_t tmpmax;
313
316
  uid_t uid = 65534;
314
317
  gid_t gid = 65534;
315
318
  bool debug = false;
372
375
    { .name = NULL }
373
376
  };
374
377
  
375
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
376
 
                     struct argp_state *state) {
377
 
    switch (key) {
 
378
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
379
                    struct argp_state *state) {
 
380
    switch(key) {
378
381
    case 'g':                   /* --global-options */
379
 
      if (arg != NULL){
 
382
      if(arg != NULL){
380
383
        char *p;
381
384
        while((p = strsep(&arg, ",")) != NULL){
382
385
          if(p[0] == '\0'){
398
401
      }
399
402
      break;
400
403
    case 'o':                   /* --options-for */
401
 
      if (arg != NULL){
 
404
      if(arg != NULL){
402
405
        char *p_name = strsep(&arg, ":");
403
406
        if(p_name[0] == '\0' or arg == NULL){
404
407
          break;
435
438
      }
436
439
      break;
437
440
    case 'd':                   /* --disable */
438
 
      if (arg != NULL){
 
441
      if(arg != NULL){
439
442
        plugin *p = getplugin(arg);
440
443
        if(p == NULL){
441
444
          return ARGP_ERR_UNKNOWN;
444
447
      }
445
448
      break;
446
449
    case 'e':                   /* --enable */
447
 
      if (arg != NULL){
 
450
      if(arg != NULL){
448
451
        plugin *p = getplugin(arg);
449
452
        if(p == NULL){
450
453
          return ARGP_ERR_UNKNOWN;
463
466
      /* This is already done by parse_opt_config_file() */
464
467
      break;
465
468
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
 
469
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
470
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
471
         or arg[numchars] != '\0'){
 
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
473
                PRIdMAX "\n", arg, (intmax_t)uid);
 
474
      } else {
 
475
        uid = (uid_t)tmpmax;
471
476
      }
472
477
      break;
473
478
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
 
479
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
480
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
481
         or arg[numchars] != '\0'){
 
482
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
483
                PRIdMAX "\n", arg, (intmax_t)gid);
 
484
      } else {
 
485
        gid = (gid_t)tmpmax;
479
486
      }
480
487
      break;
481
488
    case 132:                   /* --debug */
503
510
  
504
511
  /* This option parser is the same as parse_opt() above, except it
505
512
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file (int key, char *arg,
507
 
                                 __attribute__((unused))
508
 
                                 struct argp_state *state) {
509
 
    switch (key) {
 
513
  error_t parse_opt_config_file(int key, char *arg,
 
514
                                __attribute__((unused))
 
515
                                struct argp_state *state) {
 
516
    switch(key) {
510
517
    case 'g':                   /* --global-options */
511
518
    case 'G':                   /* --global-env */
512
519
    case 'o':                   /* --options-for */
541
548
  
542
549
  /* Parse using parse_opt_config_file() in order to get the custom
543
550
     config file location, if any. */
544
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if (ret == ARGP_ERR_UNKNOWN){
 
551
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
552
  if(ret == ARGP_ERR_UNKNOWN){
546
553
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
554
    exitstatus = EXIT_FAILURE;
548
555
    goto fallback;
552
559
  argp.parser = parse_opt;
553
560
  
554
561
  /* Open the configfile if available */
555
 
  if (argfile == NULL){
 
562
  if(argfile == NULL){
556
563
    conffp = fopen(AFILE, "r");
557
564
  } else {
558
565
    conffp = fopen(argfile, "r");
613
620
  } else {
614
621
    /* Check for harmful errors and go to fallback. Other errors might
615
622
       not affect opening plugins */
616
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
623
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
624
      perror("fopen");
618
625
      exitstatus = EXIT_FAILURE;
619
626
      goto fallback;
622
629
  /* If there was any arguments from configuration file,
623
630
     pass them to parser as command arguments */
624
631
  if(custom_argv != NULL){
625
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                      0, NULL);
627
 
    if (ret == ARGP_ERR_UNKNOWN){
 
632
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
633
                     0, NULL);
 
634
    if(ret == ARGP_ERR_UNKNOWN){
628
635
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
636
      exitstatus = EXIT_FAILURE;
630
637
      goto fallback;
633
640
  
634
641
  /* Parse actual command line arguments, to let them override the
635
642
     config file */
636
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if (ret == ARGP_ERR_UNKNOWN){
 
643
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
644
  if(ret == ARGP_ERR_UNKNOWN){
638
645
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
646
    exitstatus = EXIT_FAILURE;
640
647
    goto fallback;
647
654
      for(char **a = p->argv; *a != NULL; a++){
648
655
        fprintf(stderr, "\tArg: %s\n", *a);
649
656
      }
650
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
657
      fprintf(stderr, "...and %d environment variables\n", p->envc);
651
658
      for(char **a = p->environ; *a != NULL; a++){
652
659
        fprintf(stderr, "\t%s\n", *a);
653
660
      }
656
663
  
657
664
  /* Strip permissions down to nobody */
658
665
  ret = setuid(uid);
659
 
  if (ret == -1){
 
666
  if(ret == -1){
660
667
    perror("setuid");
661
668
  }  
662
669
  setgid(gid);
663
 
  if (ret == -1){
 
670
  if(ret == -1){
664
671
    perror("setgid");
665
672
  }
666
673
  
667
 
  if (plugindir == NULL){
 
674
  if(plugindir == NULL){
668
675
    dir = opendir(PDIR);
669
676
  } else {
670
677
    dir = opendir(plugindir);
697
704
    
698
705
    /* All directory entries have been processed */
699
706
    if(dirst == NULL){
700
 
      if (errno == EBADF){
 
707
      if(errno == EBADF){
701
708
        perror("readdir");
702
709
        exitstatus = EXIT_FAILURE;
703
710
        goto fallback;
763
770
    }
764
771
    
765
772
    ret = stat(filename, &st);
766
 
    if (ret == -1){
 
773
    if(ret == -1){
767
774
      perror("stat");
768
775
      free(filename);
769
776
      continue;
770
777
    }
771
778
 
772
779
    /* Ignore non-executable files */
773
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
780
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
781
      if(debug){
775
782
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
783
                " with bad type or mode\n", filename);
823
830
    
824
831
    int pipefd[2];
825
832
    ret = pipe(pipefd);
826
 
    if (ret == -1){
 
833
    if(ret == -1){
827
834
      perror("pipe");
828
835
      exitstatus = EXIT_FAILURE;
829
836
      goto fallback;
842
849
      goto fallback;
843
850
    }
844
851
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
852
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
853
    if(ret < 0){
847
854
      perror("sigprocmask");
848
855
      exitstatus = EXIT_FAILURE;
896
903
    close(pipefd[1]);           /* Close unused write end of pipe */
897
904
    free(filename);
898
905
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if (new_plugin == NULL){
 
906
    if(new_plugin == NULL){
900
907
      perror("getplugin");
901
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
908
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
909
      if(ret < 0){
903
910
        perror("sigprocmask");
904
911
      }
911
918
    
912
919
    /* Unblock SIGCHLD so signal handler can be run if this process
913
920
       has already completed */
914
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
921
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
922
    if(ret < 0){
916
923
      perror("sigprocmask");
917
924
      exitstatus = EXIT_FAILURE;
920
927
    
921
928
    FD_SET(new_plugin->fd, &rfds_all);
922
929
    
923
 
    if (maxfd < new_plugin->fd){
 
930
    if(maxfd < new_plugin->fd){
924
931
      maxfd = new_plugin->fd;
925
932
    }
926
933
  }
943
950
  while(plugin_list){
944
951
    fd_set rfds = rfds_all;
945
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if (select_ret == -1){
 
953
    if(select_ret == -1){
947
954
      perror("select");
948
955
      exitstatus = EXIT_FAILURE;
949
956
      goto fallback;
960
967
 
961
968
          if(debug){
962
969
            if(WIFEXITED(proc->status)){
963
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
964
 
                      (unsigned int) (proc->pid),
 
970
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
971
                      " %d\n", (intmax_t) (proc->pid),
965
972
                      WEXITSTATUS(proc->status));
966
973
            } else if(WIFSIGNALED(proc->status)) {
967
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
968
 
                      (unsigned int) (proc->pid),
 
974
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
975
                      " %d\n", (intmax_t) (proc->pid),
969
976
                      WTERMSIG(proc->status));
970
977
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
972
 
                      (unsigned int) (proc->pid));
 
978
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
979
                      (intmax_t) (proc->pid));
973
980
            }
974
981
          }
975
982
          
989
996
          proc = next_plugin;
990
997
          
991
998
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                             NULL);
 
999
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1000
                            NULL);
994
1001
          if(ret < 0){
995
1002
            perror("sigprocmask");
996
1003
            exitstatus = EXIT_FAILURE;
1025
1032
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1033
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1034
                               + (size_t) BUFFER_SIZE);
1028
 
        if (proc->buffer == NULL){
 
1035
        if(proc->buffer == NULL){
1029
1036
          perror("malloc");
1030
1037
          exitstatus = EXIT_FAILURE;
1031
1038
          goto fallback;