/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:19:32 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901161932-ostp7tulh9aijulh
* plugin-runner.c (add_environment): Never insert existing environment
                                     variables.
  (main): Rename "--global-envs" to "--global-env" and "--envs-for" to
          "--env-for".

* plugin-runner.xml (SYNOPSIS): Rename "--global-envs" to
                                "--global-env" and "--envs-for" to
                                "--env-for".
  (OPTIONS): Added "--global-env" and "--env-for".
  (FALLBACK): Add id attribute.
  (EXIT STATUS): Add text.
  (ENVIRONMENT): New section.
  (FILES): Document configuration file.

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
  }
344
336
    { .name = "global-options", .key = 'g',
345
337
      .arg = "OPTION[,OPTION[,...]]",
346
338
      .doc = "Options passed to all plugins" },
347
 
    { .name = "global-env", .key = 'G',
 
339
    { .name = "global-env", .key = 'e',
348
340
      .arg = "VAR=value",
349
341
      .doc = "Environment variable passed to all plugins" },
350
342
    { .name = "options-for", .key = 'o',
351
343
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
344
      .doc = "Options passed only to specified plugin" },
353
 
    { .name = "env-for", .key = 'E',
 
345
    { .name = "env-for", .key = 'f',
354
346
      .arg = "PLUGIN:ENV=value",
355
347
      .doc = "Environment variable passed to specified plugin" },
356
348
    { .name = "disable", .key = 'd',
357
349
      .arg = "PLUGIN",
358
350
      .doc = "Disable a specific plugin", .group = 1 },
359
 
    { .name = "enable", .key = 'e',
360
 
      .arg = "PLUGIN",
361
 
      .doc = "Enable a specific plugin", .group = 1 },
362
351
    { .name = "plugin-dir", .key = 128,
363
352
      .arg = "DIRECTORY",
364
353
      .doc = "Specify a different plugin directory", .group = 2 },
378
367
  
379
368
  error_t parse_opt (int key, char *arg, __attribute__((unused))
380
369
                     struct argp_state *state) {
 
370
    /* Get the INPUT argument from `argp_parse', which we know is a
 
371
       pointer to our plugin list pointer. */
381
372
    switch (key) {
382
373
    case 'g':                   /* --global-options */
383
374
      if (arg != NULL){
393
384
        }
394
385
      }
395
386
      break;
396
 
    case 'G':                   /* --global-env */
 
387
    case 'e':                   /* --global-env */
397
388
      if(arg == NULL){
398
389
        break;
399
390
      }
402
393
        if(envdef == NULL){
403
394
          break;
404
395
        }
405
 
        if(not add_environment(getplugin(NULL), envdef, true)){
 
396
        if(not add_environment(getplugin(NULL), envdef)){
406
397
          perror("add_environment");
407
398
        }
408
399
      }
431
422
        }
432
423
      }
433
424
      break;
434
 
    case 'E':                   /* --env-for */
 
425
    case 'f':                   /* --env-for */
435
426
      if(arg == NULL){
436
427
        break;
437
428
      }
445
436
          break;
446
437
        }
447
438
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
439
        if(not add_environment(getplugin(p_name), envdef)){
449
440
          perror("add_environment");
450
441
        }
451
442
      }
459
450
        p->disabled = true;
460
451
      }
461
452
      break;
462
 
    case 'e':                   /* --enable */
463
 
      if (arg != NULL){
464
 
        plugin *p = getplugin(arg);
465
 
        if(p == NULL){
466
 
          return ARGP_ERR_UNKNOWN;
467
 
        }
468
 
        p->disabled = false;
469
 
      }
470
 
      break;
471
453
    case 128:                   /* --plugin-dir */
472
454
      plugindir = strdup(arg);
473
455
      if(plugindir == NULL){
475
457
      }      
476
458
      break;
477
459
    case 129:                   /* --config-file */
478
 
      /* This is already done by parse_opt_config_file() */
479
 
      break;
 
460
      argfile = strdup(arg);
 
461
      if(argfile == NULL){
 
462
        perror("strdup");
 
463
      }
 
464
      break;      
480
465
    case 130:                   /* --userid */
481
466
      uid = (uid_t)strtol(arg, NULL, 10);
482
467
      break;
497
482
    return 0;
498
483
  }
499
484
  
500
 
  /* This option parser is the same as parse_opt() above, except it
501
 
     ignores everything but the --config-file option. */
502
 
  error_t parse_opt_config_file (int key, char *arg,
503
 
                                 __attribute__((unused))
504
 
                                 struct argp_state *state) {
505
 
    switch (key) {
506
 
    case 'g':                   /* --global-options */
507
 
    case 'G':                   /* --global-env */
508
 
    case 'o':                   /* --options-for */
509
 
    case 'E':                   /* --env-for */
510
 
    case 'd':                   /* --disable */
511
 
    case 'e':                   /* --enable */
512
 
    case 128:                   /* --plugin-dir */
513
 
      break;
514
 
    case 129:                   /* --config-file */
515
 
      argfile = strdup(arg);
516
 
      if(argfile == NULL){
517
 
        perror("strdup");
518
 
      }
519
 
      break;      
520
 
    case 130:                   /* --userid */
521
 
    case 131:                   /* --groupid */
522
 
    case 132:                   /* --debug */
523
 
    case ARGP_KEY_ARG:
524
 
    case ARGP_KEY_END:
525
 
      break;
526
 
    default:
527
 
      return ARGP_ERR_UNKNOWN;
528
 
    }
529
 
    return 0;
530
 
  }
531
 
  
532
 
  struct argp argp = { .options = options,
533
 
                       .parser = parse_opt_config_file,
534
 
                       .args_doc = "",
 
485
  struct argp argp = { .options = options, .parser = parse_opt,
 
486
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
535
487
                       .doc = "Mandos plugin runner -- Run plugins" };
536
488
  
537
 
  /* Parse using the parse_opt_config_file in order to get the custom
538
 
     config file location, if any. */
539
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
489
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
540
490
  if (ret == ARGP_ERR_UNKNOWN){
541
491
    fprintf(stderr, "Unknown error while parsing arguments\n");
542
492
    exitstatus = EXIT_FAILURE;
543
493
    goto fallback;
544
494
  }
545
 
  
546
 
  /* Reset to the normal argument parser */
547
 
  argp.parser = parse_opt;
548
 
  
549
 
  /* Open the configfile if available */
 
495
 
 
496
  /* Opens the configfile if aviable */
550
497
  if (argfile == NULL){
551
498
    conffp = fopen(AFILE, "r");
552
499
  } else {
606
553
      }
607
554
    }
608
555
    free(org_line);
609
 
  } else {
 
556
  } else{
610
557
    /* Check for harmful errors and go to fallback. Other errors might
611
558
       not affect opening plugins */
612
559
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
618
565
  /* If there was any arguments from configuration file,
619
566
     pass them to parser as command arguments */
620
567
  if(custom_argv != NULL){
621
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
622
 
                      0, NULL);
 
568
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
623
569
    if (ret == ARGP_ERR_UNKNOWN){
624
570
      fprintf(stderr, "Unknown error while parsing arguments\n");
625
571
      exitstatus = EXIT_FAILURE;
627
573
    }
628
574
  }
629
575
  
630
 
  /* Parse actual command line arguments, to let them override the
631
 
     config file */
632
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
633
 
  if (ret == ARGP_ERR_UNKNOWN){
634
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
635
 
    exitstatus = EXIT_FAILURE;
636
 
    goto fallback;
637
 
  }
638
 
  
639
576
  if(debug){
640
577
    for(plugin *p = plugin_list; p != NULL; p=p->next){
641
578
      fprintf(stderr, "Plugin: %s has %d arguments\n",
649
586
      }
650
587
    }
651
588
  }
652
 
  
 
589
 
653
590
  /* Strip permissions down to nobody */
654
591
  ret = setuid(uid);
655
592
  if (ret == -1){
659
596
  if (ret == -1){
660
597
    perror("setgid");
661
598
  }
662
 
  
 
599
 
663
600
  if (plugindir == NULL){
664
601
    dir = opendir(PDIR);
665
602
  } else {
686
623
  }
687
624
  
688
625
  FD_ZERO(&rfds_all);
689
 
  
 
626
 
690
627
  /* Read and execute any executable in the plugin directory*/
691
628
  while(true){
692
629
    dirst = readdir(dir);
693
630
    
694
 
    /* All directory entries have been processed */
 
631
    // All directory entries have been processed
695
632
    if(dirst == NULL){
696
633
      if (errno == EBADF){
697
634
        perror("readdir");
703
640
    
704
641
    d_name_len = strlen(dirst->d_name);
705
642
    
706
 
    /* Ignore dotfiles, backup files and other junk */
 
643
    // Ignore dotfiles, backup files and other junk
707
644
    {
708
645
      bool bad_name = false;
709
646
      
795
732
        }
796
733
        /* Add global environment variables */
797
734
        for(char **e = g->environ; *e != NULL; e++){
798
 
          if(not add_environment(p, *e, false)){
 
735
          if(not add_environment(p, *e)){
799
736
            perror("add_environment");
800
737
          }
801
738
        }
806
743
       process, too. */
807
744
    if(p->environ[0] != NULL){
808
745
      for(char **e = environ; *e != NULL; e++){
809
 
        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)){
810
752
          perror("add_environment");
811
753
        }
812
754
      }
839
781
      exitstatus = EXIT_FAILURE;
840
782
      goto fallback;
841
783
    }
842
 
    /* Starting a new process to be watched */
 
784
    // Starting a new process to be watched
843
785
    pid_t pid = fork();
844
786
    if(pid == -1){
845
787
      perror("fork");