/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

  • Committer: Teddy Hogeborn
  • Date: 2009-02-24 11:49:59 UTC
  • mfrom: (237.2.84 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090224114959-q1hayb11wx27sqn0
Merge from trunk.  Notable changes:

1. Fix version numbers in manual pages.
2. Bug fix: Do not depend on GNU awk.
3. Dependency on Python 2.5 documented.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
63
                                   sigaddset(), sigaction(),
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
66
67
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
 
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
68
69
 
69
70
#define BUFFER_SIZE 256
70
71
 
81
82
  char **environ;
82
83
  int envc;
83
84
  bool disabled;
84
 
 
 
85
  
85
86
  /* Variables used for running processes*/
86
87
  pid_t pid;
87
88
  int fd;
279
280
  }
280
281
  free(plugin_node->environ);
281
282
  free(plugin_node->buffer);
282
 
 
 
283
  
283
284
  /* Removes the plugin from the singly-linked list */
284
285
  if(plugin_node == plugin_list){
285
286
    /* First one - simple */
312
313
  struct dirent *dirst;
313
314
  struct stat st;
314
315
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
316
  int ret, maxfd = 0;
316
317
  ssize_t sret;
317
318
  intmax_t tmpmax;
318
319
  uid_t uid = 65534;
379
380
  
380
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
382
                    struct argp_state *state){
 
383
    char *tmp;
382
384
    switch(key){
383
385
    case 'g':                   /* --global-options */
384
386
      if(arg != NULL){
385
 
        char *p;
386
 
        while((p = strsep(&arg, ",")) != NULL){
387
 
          if(p[0] == '\0'){
 
387
        char *plugin_option;
 
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
388
390
            continue;
389
391
          }
390
 
          if(not add_argument(getplugin(NULL), p)){
 
392
          if(not add_argument(getplugin(NULL), plugin_option)){
391
393
            perror("add_argument");
392
394
            return ARGP_ERR_UNKNOWN;
393
395
          }
404
406
      break;
405
407
    case 'o':                   /* --options-for */
406
408
      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)){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
421
416
            perror("add_argument");
422
417
            return ARGP_ERR_UNKNOWN;
423
418
          }
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'){
 
466
      errno = 0;
 
467
      tmpmax = strtoimax(arg, &tmp, 10);
 
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
469
         or tmpmax != (uid_t)tmpmax){
474
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
471
                PRIdMAX "\n", arg, (intmax_t)uid);
476
472
      } else {
478
474
      }
479
475
      break;
480
476
    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'){
 
477
      errno = 0;
 
478
      tmpmax = strtoimax(arg, &tmp, 10);
 
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
480
         or tmpmax != (gid_t)tmpmax){
484
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
482
                PRIdMAX "\n", arg, (intmax_t)gid);
486
483
      } else {
572
569
    size_t size = 0;
573
570
    const char whitespace_delims[] = " \r\t\f\v\n";
574
571
    const char comment_delim[] = "#";
575
 
 
 
572
    
576
573
    custom_argc = 1;
577
574
    custom_argv = malloc(sizeof(char*) * 2);
578
575
    if(custom_argv == NULL){
582
579
    }
583
580
    custom_argv[0] = argv[0];
584
581
    custom_argv[1] = NULL;
585
 
 
 
582
    
586
583
    /* for each line in the config file, strip whitespace and ignore
587
584
       commented text */
588
585
    while(true){
590
587
      if(sret == -1){
591
588
        break;
592
589
      }
593
 
 
 
590
      
594
591
      line = org_line;
595
592
      arg = strsep(&line, comment_delim);
596
593
      while((p = strsep(&arg, whitespace_delims)) != NULL){
759
756
        continue;
760
757
      }
761
758
    }
762
 
 
 
759
    
763
760
    char *filename;
764
761
    if(plugindir == NULL){
765
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
777
774
      free(filename);
778
775
      continue;
779
776
    }
780
 
 
 
777
    
781
778
    /* Ignore non-executable files */
782
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
780
      if(debug){
967
964
        if(not WIFEXITED(proc->status)
968
965
           or WEXITSTATUS(proc->status) != 0){
969
966
          /* Bad exit by plugin */
970
 
 
 
967
          
971
968
          if(debug){
972
969
            if(WIFEXITED(proc->status)){
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
974
 
                      " %d\n", (intmax_t) (proc->pid),
 
970
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
971
                      " status %d\n", proc->name,
 
972
                      (intmax_t) (proc->pid),
975
973
                      WEXITSTATUS(proc->status));
976
974
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
978
 
                      " %d\n", (intmax_t) (proc->pid),
 
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
976
                      " signal %d\n", proc->name,
 
977
                      (intmax_t) (proc->pid),
979
978
                      WTERMSIG(proc->status));
980
979
            } else if(WCOREDUMP(proc->status)){
981
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
982
 
                      (intmax_t) (proc->pid));
 
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
983
982
            }
984
983
          }
985
984
          
986
985
          /* Remove the plugin */
987
986
          FD_CLR(proc->fd, &rfds_all);
988
 
 
 
987
          
989
988
          /* Block signal while modifying process_list */
990
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
991
990
          if(ret < 0){
1058
1057
      }
1059
1058
    }
1060
1059
  }
1061
 
 
1062
 
 
 
1060
  
 
1061
  
1063
1062
 fallback:
1064
1063
  
1065
1064
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){