/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-05 18:19:52 UTC
  • mfrom: (24.1.91 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080905181952-qfuwtyvefanh9v3v
* mandos: Open the PID file before daemonizing, but write to it
          afterwards.

* plugin-runner.xml (SECURITY): Improved grammar.

Also merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets a existing plugin based on name,
 
99
/* Gets an existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
118
118
      return NULL;
119
119
    }
120
120
  }
121
 
 
 
121
  
122
122
  *new_plugin = (plugin) { .name = copy_name,
123
123
                           .argc = 1,
124
124
                           .disabled = false,
132
132
  }
133
133
  new_plugin->argv[0] = copy_name;
134
134
  new_plugin->argv[1] = NULL;
135
 
 
 
135
  
136
136
  new_plugin->environ = malloc(sizeof(char *));
137
137
  if(new_plugin->environ == NULL){
138
138
    free(copy_name);
141
141
    return NULL;
142
142
  }
143
143
  new_plugin->environ[0] = NULL;
144
 
 
 
144
  
145
145
  /* Append the new plugin to the list */
146
146
  plugin_list = new_plugin;
147
147
  return new_plugin;
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
  }
 
186
  /* namelen = length of name of environment variable */
 
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
188
  /* Search for this environment variable */
 
189
  for(char **e = p->environ; *e != NULL; e++){
 
190
    if(strncmp(*e, def, namelen + 1) == 0){
 
191
      /* It already exists */
 
192
      if(replace){
 
193
        char *new = realloc(*e, strlen(def) + 1);
 
194
        if(new == NULL){
 
195
          return false;
 
196
        }
 
197
        *e = new;
 
198
        strcpy(*e, def);
 
199
      }
 
200
      return true;
 
201
    }
 
202
  }
186
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
204
}
188
205
 
221
238
      /* No child processes */
222
239
      break;
223
240
    }
224
 
 
 
241
    
225
242
    /* A child exited, find it in process_list */
226
243
    while(proc != NULL and proc->pid != pid){
227
244
      proc = proc->next;
327
344
    { .name = "global-options", .key = 'g',
328
345
      .arg = "OPTION[,OPTION[,...]]",
329
346
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
347
    { .name = "global-env", .key = 'G',
331
348
      .arg = "VAR=value",
332
349
      .doc = "Environment variable passed to all plugins" },
333
350
    { .name = "options-for", .key = 'o',
334
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
352
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
353
    { .name = "env-for", .key = 'E',
337
354
      .arg = "PLUGIN:ENV=value",
338
355
      .doc = "Environment variable passed to specified plugin" },
339
356
    { .name = "disable", .key = 'd',
340
357
      .arg = "PLUGIN",
341
358
      .doc = "Disable a specific plugin", .group = 1 },
 
359
    { .name = "enable", .key = 'e',
 
360
      .arg = "PLUGIN",
 
361
      .doc = "Enable a specific plugin", .group = 1 },
342
362
    { .name = "plugin-dir", .key = 128,
343
363
      .arg = "DIRECTORY",
344
364
      .doc = "Specify a different plugin directory", .group = 2 },
358
378
  
359
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
360
380
                     struct argp_state *state) {
361
 
    /* Get the INPUT argument from `argp_parse', which we know is a
362
 
       pointer to our plugin list pointer. */
363
381
    switch (key) {
364
382
    case 'g':                   /* --global-options */
365
383
      if (arg != NULL){
375
393
        }
376
394
      }
377
395
      break;
378
 
    case 'e':                   /* --global-envs */
 
396
    case 'G':                   /* --global-env */
379
397
      if(arg == NULL){
380
398
        break;
381
399
      }
384
402
        if(envdef == NULL){
385
403
          break;
386
404
        }
387
 
        if(not add_environment(getplugin(NULL), envdef)){
 
405
        if(not add_environment(getplugin(NULL), envdef, true)){
388
406
          perror("add_environment");
389
407
        }
390
408
      }
392
410
    case 'o':                   /* --options-for */
393
411
      if (arg != NULL){
394
412
        char *p_name = strsep(&arg, ":");
395
 
        if(p_name[0] == '\0'){
 
413
        if(p_name[0] == '\0' or arg == NULL){
396
414
          break;
397
415
        }
398
416
        char *opt = strsep(&arg, ":");
399
 
        if(opt[0] == '\0'){
 
417
        if(opt[0] == '\0' or opt == NULL){
400
418
          break;
401
419
        }
402
 
        if(opt != NULL){
403
 
          char *p;
404
 
          while((p = strsep(&opt, ",")) != NULL){
405
 
            if(p[0] == '\0'){
406
 
              continue;
407
 
            }
408
 
            if(not add_argument(getplugin(p_name), p)){
409
 
              perror("add_argument");
410
 
              return ARGP_ERR_UNKNOWN;
411
 
            }
 
420
        char *p;
 
421
        while((p = strsep(&opt, ",")) != NULL){
 
422
          if(p[0] == '\0'){
 
423
            continue;
 
424
          }
 
425
          if(not add_argument(getplugin(p_name), p)){
 
426
            perror("add_argument");
 
427
            return ARGP_ERR_UNKNOWN;
412
428
          }
413
429
        }
414
430
      }
415
431
      break;
416
 
    case 'f':                   /* --envs-for */
 
432
    case 'E':                   /* --env-for */
417
433
      if(arg == NULL){
418
434
        break;
419
435
      }
427
443
          break;
428
444
        }
429
445
        envdef++;
430
 
        if(not add_environment(getplugin(p_name), envdef)){
 
446
        if(not add_environment(getplugin(p_name), envdef, true)){
431
447
          perror("add_environment");
432
448
        }
433
449
      }
441
457
        p->disabled = true;
442
458
      }
443
459
      break;
 
460
    case 'e':                   /* --enable */
 
461
      if (arg != NULL){
 
462
        plugin *p = getplugin(arg);
 
463
        if(p == NULL){
 
464
          return ARGP_ERR_UNKNOWN;
 
465
        }
 
466
        p->disabled = false;
 
467
      }
 
468
      break;
444
469
    case 128:                   /* --plugin-dir */
445
470
      plugindir = strdup(arg);
446
471
      if(plugindir == NULL){
448
473
      }      
449
474
      break;
450
475
    case 129:                   /* --config-file */
451
 
      argfile = strdup(arg);
452
 
      if(argfile == NULL){
453
 
        perror("strdup");
454
 
      }
455
 
      break;      
 
476
      /* This is already done by parse_opt_config_file() */
 
477
      break;
456
478
    case 130:                   /* --userid */
457
479
      uid = (uid_t)strtol(arg, NULL, 10);
458
480
      break;
473
495
    return 0;
474
496
  }
475
497
  
476
 
  struct argp argp = { .options = options, .parser = parse_opt,
477
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
498
  /* This option parser is the same as parse_opt() above, except it
 
499
     ignores everything but the --config-file option. */
 
500
  error_t parse_opt_config_file (int key, char *arg,
 
501
                                 __attribute__((unused))
 
502
                                 struct argp_state *state) {
 
503
    switch (key) {
 
504
    case 'g':                   /* --global-options */
 
505
    case 'G':                   /* --global-env */
 
506
    case 'o':                   /* --options-for */
 
507
    case 'E':                   /* --env-for */
 
508
    case 'd':                   /* --disable */
 
509
    case 'e':                   /* --enable */
 
510
    case 128:                   /* --plugin-dir */
 
511
      break;
 
512
    case 129:                   /* --config-file */
 
513
      argfile = strdup(arg);
 
514
      if(argfile == NULL){
 
515
        perror("strdup");
 
516
      }
 
517
      break;      
 
518
    case 130:                   /* --userid */
 
519
    case 131:                   /* --groupid */
 
520
    case 132:                   /* --debug */
 
521
    case ARGP_KEY_ARG:
 
522
    case ARGP_KEY_END:
 
523
      break;
 
524
    default:
 
525
      return ARGP_ERR_UNKNOWN;
 
526
    }
 
527
    return 0;
 
528
  }
 
529
  
 
530
  struct argp argp = { .options = options,
 
531
                       .parser = parse_opt_config_file,
 
532
                       .args_doc = "",
478
533
                       .doc = "Mandos plugin runner -- Run plugins" };
479
534
  
480
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
535
  /* Parse using the parse_opt_config_file in order to get the custom
 
536
     config file location, if any. */
 
537
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
481
538
  if (ret == ARGP_ERR_UNKNOWN){
482
539
    fprintf(stderr, "Unknown error while parsing arguments\n");
483
540
    exitstatus = EXIT_FAILURE;
484
541
    goto fallback;
485
542
  }
486
 
 
487
 
  /* Opens the configfile if aviable */
 
543
  
 
544
  /* Reset to the normal argument parser */
 
545
  argp.parser = parse_opt;
 
546
  
 
547
  /* Open the configfile if available */
488
548
  if (argfile == NULL){
489
549
    conffp = fopen(AFILE, "r");
490
550
  } else {
544
604
      }
545
605
    }
546
606
    free(org_line);
547
 
  } else{
 
607
  } else {
548
608
    /* Check for harmful errors and go to fallback. Other errors might
549
609
       not affect opening plugins */
550
610
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
556
616
  /* If there was any arguments from configuration file,
557
617
     pass them to parser as command arguments */
558
618
  if(custom_argv != NULL){
559
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
619
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
620
                      0, NULL);
560
621
    if (ret == ARGP_ERR_UNKNOWN){
561
622
      fprintf(stderr, "Unknown error while parsing arguments\n");
562
623
      exitstatus = EXIT_FAILURE;
564
625
    }
565
626
  }
566
627
  
 
628
  /* Parse actual command line arguments, to let them override the
 
629
     config file */
 
630
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
631
  if (ret == ARGP_ERR_UNKNOWN){
 
632
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
633
    exitstatus = EXIT_FAILURE;
 
634
    goto fallback;
 
635
  }
 
636
  
567
637
  if(debug){
568
638
    for(plugin *p = plugin_list; p != NULL; p=p->next){
569
639
      fprintf(stderr, "Plugin: %s has %d arguments\n",
577
647
      }
578
648
    }
579
649
  }
580
 
 
 
650
  
581
651
  /* Strip permissions down to nobody */
582
652
  ret = setuid(uid);
583
653
  if (ret == -1){
587
657
  if (ret == -1){
588
658
    perror("setgid");
589
659
  }
590
 
 
 
660
  
591
661
  if (plugindir == NULL){
592
662
    dir = opendir(PDIR);
593
663
  } else {
614
684
  }
615
685
  
616
686
  FD_ZERO(&rfds_all);
617
 
 
 
687
  
618
688
  /* Read and execute any executable in the plugin directory*/
619
689
  while(true){
620
690
    dirst = readdir(dir);
621
691
    
622
 
    // All directory entries have been processed
 
692
    /* All directory entries have been processed */
623
693
    if(dirst == NULL){
624
694
      if (errno == EBADF){
625
695
        perror("readdir");
631
701
    
632
702
    d_name_len = strlen(dirst->d_name);
633
703
    
634
 
    // Ignore dotfiles, backup files and other junk
 
704
    /* Ignore dotfiles, backup files and other junk */
635
705
    {
636
706
      bool bad_name = false;
637
707
      
723
793
        }
724
794
        /* Add global environment variables */
725
795
        for(char **e = g->environ; *e != NULL; e++){
726
 
          if(not add_environment(p, *e)){
 
796
          if(not add_environment(p, *e, false)){
727
797
            perror("add_environment");
728
798
          }
729
799
        }
734
804
       process, too. */
735
805
    if(p->environ[0] != NULL){
736
806
      for(char **e = environ; *e != NULL; e++){
737
 
        char *copy = strdup(*e);
738
 
        if(copy == NULL){
739
 
          perror("strdup");
740
 
          continue;
741
 
        }
742
 
        if(not add_environment(p, copy)){
 
807
        if(not add_environment(p, *e, false)){
743
808
          perror("add_environment");
744
809
        }
745
810
      }
772
837
      exitstatus = EXIT_FAILURE;
773
838
      goto fallback;
774
839
    }
775
 
    // Starting a new process to be watched
 
840
    /* Starting a new process to be watched */
776
841
    pid_t pid = fork();
777
842
    if(pid == -1){
778
843
      perror("fork");