/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS() */
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
42
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
52
53
                                   close() */
53
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
54
55
                                   FD_CLOEXEC */
55
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal() */
56
58
#include <errno.h>              /* errno */
57
59
#include <argp.h>               /* struct argp_option, struct
58
60
                                   argp_state, struct argp,
62
64
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
65
                                   sigaddset(), sigaction(),
64
66
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
67
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
68
                                */
66
69
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
 
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
68
71
 
69
72
#define BUFFER_SIZE 256
70
73
 
81
84
  char **environ;
82
85
  int envc;
83
86
  bool disabled;
84
 
 
 
87
  
85
88
  /* Variables used for running processes*/
86
89
  pid_t pid;
87
90
  int fd;
279
282
  }
280
283
  free(plugin_node->environ);
281
284
  free(plugin_node->buffer);
282
 
 
 
285
  
283
286
  /* Removes the plugin from the singly-linked list */
284
287
  if(plugin_node == plugin_list){
285
288
    /* First one - simple */
312
315
  struct dirent *dirst;
313
316
  struct stat st;
314
317
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
318
  int ret, maxfd = 0;
316
319
  ssize_t sret;
317
320
  intmax_t tmpmax;
318
321
  uid_t uid = 65534;
379
382
  
380
383
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
384
                    struct argp_state *state){
 
385
    char *tmp;
382
386
    switch(key){
383
387
    case 'g':                   /* --global-options */
384
388
      if(arg != NULL){
385
 
        char *p;
386
 
        while((p = strsep(&arg, ",")) != NULL){
387
 
          if(p[0] == '\0'){
 
389
        char *plugin_option;
 
390
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
391
          if(plugin_option[0] == '\0'){
388
392
            continue;
389
393
          }
390
 
          if(not add_argument(getplugin(NULL), p)){
 
394
          if(not add_argument(getplugin(NULL), plugin_option)){
391
395
            perror("add_argument");
392
396
            return ARGP_ERR_UNKNOWN;
393
397
          }
404
408
      break;
405
409
    case 'o':                   /* --options-for */
406
410
      if(arg != NULL){
407
 
        char *p_name = strsep(&arg, ":");
408
 
        if(p_name[0] == '\0' or arg == NULL){
409
 
          break;
410
 
        }
411
 
        char *opt = strsep(&arg, ":");
412
 
        if(opt[0] == '\0' or opt == NULL){
413
 
          break;
414
 
        }
415
 
        char *p;
416
 
        while((p = strsep(&opt, ",")) != NULL){
417
 
          if(p[0] == '\0'){
418
 
            continue;
419
 
          }
420
 
          if(not add_argument(getplugin(p_name), p)){
 
411
        char *plugin_name = strsep(&arg, ":");
 
412
        if(plugin_name[0] == '\0'){
 
413
          break;
 
414
        }
 
415
        char *plugin_option;
 
416
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
417
          if(not add_argument(getplugin(plugin_name), plugin_option)){
421
418
            perror("add_argument");
422
419
            return ARGP_ERR_UNKNOWN;
423
420
          }
462
459
      plugindir = strdup(arg);
463
460
      if(plugindir == NULL){
464
461
        perror("strdup");
465
 
      }      
 
462
      }
466
463
      break;
467
464
    case 129:                   /* --config-file */
468
465
      /* This is already done by parse_opt_config_file() */
469
466
      break;
470
467
    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'){
 
468
      errno = 0;
 
469
      tmpmax = strtoimax(arg, &tmp, 10);
 
470
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
471
         or tmpmax != (uid_t)tmpmax){
474
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
473
                PRIdMAX "\n", arg, (intmax_t)uid);
476
474
      } else {
478
476
      }
479
477
      break;
480
478
    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'){
 
479
      errno = 0;
 
480
      tmpmax = strtoimax(arg, &tmp, 10);
 
481
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
482
         or tmpmax != (gid_t)tmpmax){
484
483
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
484
                PRIdMAX "\n", arg, (intmax_t)gid);
486
485
      } else {
530
529
      if(argfile == NULL){
531
530
        perror("strdup");
532
531
      }
533
 
      break;      
 
532
      break;
534
533
    case 130:                   /* --userid */
535
534
    case 131:                   /* --groupid */
536
535
    case 132:                   /* --debug */
565
564
    conffp = fopen(AFILE, "r");
566
565
  } else {
567
566
    conffp = fopen(argfile, "r");
568
 
  }  
 
567
  }
569
568
  if(conffp != NULL){
570
569
    char *org_line = NULL;
571
570
    char *p, *arg, *new_arg, *line;
572
571
    size_t size = 0;
573
572
    const char whitespace_delims[] = " \r\t\f\v\n";
574
573
    const char comment_delim[] = "#";
575
 
 
 
574
    
576
575
    custom_argc = 1;
577
576
    custom_argv = malloc(sizeof(char*) * 2);
578
577
    if(custom_argv == NULL){
582
581
    }
583
582
    custom_argv[0] = argv[0];
584
583
    custom_argv[1] = NULL;
585
 
 
 
584
    
586
585
    /* for each line in the config file, strip whitespace and ignore
587
586
       commented text */
588
587
    while(true){
590
589
      if(sret == -1){
591
590
        break;
592
591
      }
593
 
 
 
592
      
594
593
      line = org_line;
595
594
      arg = strsep(&line, comment_delim);
596
595
      while((p = strsep(&arg, whitespace_delims)) != NULL){
615
614
          goto fallback;
616
615
        }
617
616
        custom_argv[custom_argc-1] = new_arg;
618
 
        custom_argv[custom_argc] = NULL;        
 
617
        custom_argv[custom_argc] = NULL;
619
618
      }
620
619
    }
621
620
    free(org_line);
759
758
        continue;
760
759
      }
761
760
    }
762
 
 
 
761
    
763
762
    char *filename;
764
763
    if(plugindir == NULL){
765
764
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
777
776
      free(filename);
778
777
      continue;
779
778
    }
780
 
 
 
779
    
781
780
    /* Ignore non-executable files */
782
781
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
782
      if(debug){
936
935
  
937
936
  closedir(dir);
938
937
  dir = NULL;
 
938
  free_plugin(getplugin(NULL));
939
939
  
940
940
  for(plugin *p = plugin_list; p != NULL; p = p->next){
941
941
    if(p->pid != 0){
966
966
        if(not WIFEXITED(proc->status)
967
967
           or WEXITSTATUS(proc->status) != 0){
968
968
          /* Bad exit by plugin */
969
 
 
 
969
          
970
970
          if(debug){
971
971
            if(WIFEXITED(proc->status)){
972
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
975
975
                      WEXITSTATUS(proc->status));
976
976
            } else if(WIFSIGNALED(proc->status)){
977
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
 
                      " signal %d\n", proc->name,
 
978
                      " signal %d: %s\n", proc->name,
979
979
                      (intmax_t) (proc->pid),
980
 
                      WTERMSIG(proc->status));
 
980
                      WTERMSIG(proc->status),
 
981
                      strsignal(WTERMSIG(proc->status)));
981
982
            } else if(WCOREDUMP(proc->status)){
982
983
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
983
984
                      " core\n", proc->name, (intmax_t) (proc->pid));
986
987
          
987
988
          /* Remove the plugin */
988
989
          FD_CLR(proc->fd, &rfds_all);
989
 
 
 
990
          
990
991
          /* Block signal while modifying process_list */
991
992
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
992
993
          if(ret < 0){
1059
1060
      }
1060
1061
    }
1061
1062
  }
1062
 
 
1063
 
 
 
1063
  
 
1064
  
1064
1065
 fallback:
1065
1066
  
1066
1067
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){