/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
179
179
}
180
180
 
181
181
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def, bool replace){
 
182
static bool add_environment(plugin *p, const char *def){
183
183
  if(p == NULL){
184
184
    return false;
185
185
  }
188
188
  /* Search for this environment variable */
189
189
  for(char **e = p->environ; *e != NULL; e++){
190
190
    if(strncmp(*e, def, namelen+1) == 0){
191
 
      /* It already exists */
192
 
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
194
 
        if(new == NULL){
195
 
          return false;
196
 
        }
197
 
        *e = new;
198
 
        strcpy(*e, def);
199
 
      }
 
191
      /* Refuse to add an existing variable */
200
192
      return true;
201
193
    }
202
194
  }
401
393
        if(envdef == NULL){
402
394
          break;
403
395
        }
404
 
        if(not add_environment(getplugin(NULL), envdef, true)){
 
396
        if(not add_environment(getplugin(NULL), envdef)){
405
397
          perror("add_environment");
406
398
        }
407
399
      }
444
436
          break;
445
437
        }
446
438
        envdef++;
447
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
439
        if(not add_environment(getplugin(p_name), envdef)){
448
440
          perror("add_environment");
449
441
        }
450
442
      }
491
483
  }
492
484
  
493
485
  struct argp argp = { .options = options, .parser = parse_opt,
494
 
                       .args_doc = "",
 
486
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
495
487
                       .doc = "Mandos plugin runner -- Run plugins" };
496
488
  
497
 
  /* Open the configfile if available */
 
489
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
490
  if (ret == ARGP_ERR_UNKNOWN){
 
491
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
492
    exitstatus = EXIT_FAILURE;
 
493
    goto fallback;
 
494
  }
 
495
 
 
496
  /* Opens the configfile if aviable */
498
497
  if (argfile == NULL){
499
498
    conffp = fopen(AFILE, "r");
500
499
  } else {
554
553
      }
555
554
    }
556
555
    free(org_line);
557
 
  } else {
 
556
  } else{
558
557
    /* Check for harmful errors and go to fallback. Other errors might
559
558
       not affect opening plugins */
560
559
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
566
565
  /* If there was any arguments from configuration file,
567
566
     pass them to parser as command arguments */
568
567
  if(custom_argv != NULL){
569
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
570
 
                      0, NULL);
 
568
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
571
569
    if (ret == ARGP_ERR_UNKNOWN){
572
570
      fprintf(stderr, "Unknown error while parsing arguments\n");
573
571
      exitstatus = EXIT_FAILURE;
575
573
    }
576
574
  }
577
575
  
578
 
  /* Parse actual command line arguments, to let them override the
579
 
     config file */
580
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
581
 
  if (ret == ARGP_ERR_UNKNOWN){
582
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
583
 
    exitstatus = EXIT_FAILURE;
584
 
    goto fallback;
585
 
  }
586
 
  
587
576
  if(debug){
588
577
    for(plugin *p = plugin_list; p != NULL; p=p->next){
589
578
      fprintf(stderr, "Plugin: %s has %d arguments\n",
597
586
      }
598
587
    }
599
588
  }
600
 
  
 
589
 
601
590
  /* Strip permissions down to nobody */
602
591
  ret = setuid(uid);
603
592
  if (ret == -1){
607
596
  if (ret == -1){
608
597
    perror("setgid");
609
598
  }
610
 
  
 
599
 
611
600
  if (plugindir == NULL){
612
601
    dir = opendir(PDIR);
613
602
  } else {
634
623
  }
635
624
  
636
625
  FD_ZERO(&rfds_all);
637
 
  
 
626
 
638
627
  /* Read and execute any executable in the plugin directory*/
639
628
  while(true){
640
629
    dirst = readdir(dir);
641
630
    
642
 
    /* All directory entries have been processed */
 
631
    // All directory entries have been processed
643
632
    if(dirst == NULL){
644
633
      if (errno == EBADF){
645
634
        perror("readdir");
651
640
    
652
641
    d_name_len = strlen(dirst->d_name);
653
642
    
654
 
    /* Ignore dotfiles, backup files and other junk */
 
643
    // Ignore dotfiles, backup files and other junk
655
644
    {
656
645
      bool bad_name = false;
657
646
      
743
732
        }
744
733
        /* Add global environment variables */
745
734
        for(char **e = g->environ; *e != NULL; e++){
746
 
          if(not add_environment(p, *e, false)){
 
735
          if(not add_environment(p, *e)){
747
736
            perror("add_environment");
748
737
          }
749
738
        }
754
743
       process, too. */
755
744
    if(p->environ[0] != NULL){
756
745
      for(char **e = environ; *e != NULL; e++){
757
 
        if(not add_environment(p, *e, false)){
 
746
        char *copy = strdup(*e);
 
747
        if(copy == NULL){
 
748
          perror("strdup");
 
749
          continue;
 
750
        }
 
751
        if(not add_environment(p, copy)){
758
752
          perror("add_environment");
759
753
        }
760
754
      }
787
781
      exitstatus = EXIT_FAILURE;
788
782
      goto fallback;
789
783
    }
790
 
    /* Starting a new process to be watched */
 
784
    // Starting a new process to be watched
791
785
    pid_t pid = fork();
792
786
    if(pid == -1){
793
787
      perror("fork");