/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: 2008-09-01 16:53:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901165317-77dccp81zyqimt2j
* plugin-runner.c (add_environment): Override existing environment
                                     variables when asked to do so.
                                     All callers changed.
  (main): Removed old argp.args_doc string.  Parse argument file
          before normal command line arguments.  Use ARGP_IN_ORDER
          flag in both calls to argp_parse.  Do not strdup() strings
          to be sent to add_environment().

* plugin-runner.xml (OPTIONS): Document "--global-env" and "--envs-for".
  (FILES): Note that the config file is parsed before the command line
           options.

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