/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: 2008-09-05 18:37:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080905183728-l0m8v3vqa302uwrw
* mandos (main): Use EAFP with pidfile.

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,
187
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
188
  /* Search for this environment variable */
189
189
  for(char **e = p->environ; *e != NULL; e++){
190
 
    if(strncmp(*e, def, namelen+1) == 0){
 
190
    if(strncmp(*e, def, namelen + 1) == 0){
191
191
      /* It already exists */
192
192
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
 
193
        char *new = realloc(*e, strlen(def) + 1);
194
194
        if(new == NULL){
195
195
          return false;
196
196
        }
238
238
      /* No child processes */
239
239
      break;
240
240
    }
241
 
 
 
241
    
242
242
    /* A child exited, find it in process_list */
243
243
    while(proc != NULL and proc->pid != pid){
244
244
      proc = proc->next;
344
344
    { .name = "global-options", .key = 'g',
345
345
      .arg = "OPTION[,OPTION[,...]]",
346
346
      .doc = "Options passed to all plugins" },
347
 
    { .name = "global-env", .key = 'e',
 
347
    { .name = "global-env", .key = 'G',
348
348
      .arg = "VAR=value",
349
349
      .doc = "Environment variable passed to all plugins" },
350
350
    { .name = "options-for", .key = 'o',
351
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
352
      .doc = "Options passed only to specified plugin" },
353
 
    { .name = "env-for", .key = 'f',
 
353
    { .name = "env-for", .key = 'E',
354
354
      .arg = "PLUGIN:ENV=value",
355
355
      .doc = "Environment variable passed to specified plugin" },
356
356
    { .name = "disable", .key = 'd',
357
357
      .arg = "PLUGIN",
358
358
      .doc = "Disable a specific plugin", .group = 1 },
 
359
    { .name = "enable", .key = 'e',
 
360
      .arg = "PLUGIN",
 
361
      .doc = "Enable a specific plugin", .group = 1 },
359
362
    { .name = "plugin-dir", .key = 128,
360
363
      .arg = "DIRECTORY",
361
364
      .doc = "Specify a different plugin directory", .group = 2 },
375
378
  
376
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
380
                     struct argp_state *state) {
378
 
    /* Get the INPUT argument from `argp_parse', which we know is a
379
 
       pointer to our plugin list pointer. */
380
381
    switch (key) {
381
382
    case 'g':                   /* --global-options */
382
383
      if (arg != NULL){
392
393
        }
393
394
      }
394
395
      break;
395
 
    case 'e':                   /* --global-env */
 
396
    case 'G':                   /* --global-env */
396
397
      if(arg == NULL){
397
398
        break;
398
399
      }
409
410
    case 'o':                   /* --options-for */
410
411
      if (arg != NULL){
411
412
        char *p_name = strsep(&arg, ":");
412
 
        if(p_name[0] == '\0'){
 
413
        if(p_name[0] == '\0' or arg == NULL){
413
414
          break;
414
415
        }
415
416
        char *opt = strsep(&arg, ":");
416
 
        if(opt[0] == '\0'){
 
417
        if(opt[0] == '\0' or opt == NULL){
417
418
          break;
418
419
        }
419
 
        if(opt != NULL){
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;
428
 
            }
 
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;
429
428
          }
430
429
        }
431
430
      }
432
431
      break;
433
 
    case 'f':                   /* --env-for */
 
432
    case 'E':                   /* --env-for */
434
433
      if(arg == NULL){
435
434
        break;
436
435
      }
458
457
        p->disabled = true;
459
458
      }
460
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;
461
469
    case 128:                   /* --plugin-dir */
462
470
      plugindir = strdup(arg);
463
471
      if(plugindir == NULL){
465
473
      }      
466
474
      break;
467
475
    case 129:                   /* --config-file */
468
 
      argfile = strdup(arg);
469
 
      if(argfile == NULL){
470
 
        perror("strdup");
471
 
      }
472
 
      break;      
 
476
      /* This is already done by parse_opt_config_file() */
 
477
      break;
473
478
    case 130:                   /* --userid */
474
479
      uid = (uid_t)strtol(arg, NULL, 10);
475
480
      break;
490
495
    return 0;
491
496
  }
492
497
  
493
 
  struct argp argp = { .options = options, .parser = parse_opt,
 
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,
494
532
                       .args_doc = "",
495
533
                       .doc = "Mandos plugin runner -- Run plugins" };
496
534
  
 
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);
 
538
  if (ret == ARGP_ERR_UNKNOWN){
 
539
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
540
    exitstatus = EXIT_FAILURE;
 
541
    goto fallback;
 
542
  }
 
543
  
 
544
  /* Reset to the normal argument parser */
 
545
  argp.parser = parse_opt;
 
546
  
497
547
  /* Open the configfile if available */
498
548
  if (argfile == NULL){
499
549
    conffp = fopen(AFILE, "r");