/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:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets an existing plugin based on name,
 
99
/* Gets a 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) + 1);
 
193
        char *new = realloc(*e, strlen(def));
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;
255
255
/* Prints out a password to stdout */
256
256
bool print_out_password(const char *buffer, size_t length){
257
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
258
261
  for(size_t written = 0; written < length; written += (size_t)ret){
259
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
260
263
                                   length - written));
341
344
    { .name = "global-options", .key = 'g',
342
345
      .arg = "OPTION[,OPTION[,...]]",
343
346
      .doc = "Options passed to all plugins" },
344
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
345
348
      .arg = "VAR=value",
346
349
      .doc = "Environment variable passed to all plugins" },
347
350
    { .name = "options-for", .key = 'o',
348
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
349
352
      .doc = "Options passed only to specified plugin" },
350
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
351
354
      .arg = "PLUGIN:ENV=value",
352
355
      .doc = "Environment variable passed to specified plugin" },
353
356
    { .name = "disable", .key = 'd',
354
357
      .arg = "PLUGIN",
355
358
      .doc = "Disable a specific plugin", .group = 1 },
356
 
    { .name = "enable", .key = 'e',
357
 
      .arg = "PLUGIN",
358
 
      .doc = "Enable a specific plugin", .group = 1 },
359
359
    { .name = "plugin-dir", .key = 128,
360
360
      .arg = "DIRECTORY",
361
361
      .doc = "Specify a different plugin directory", .group = 2 },
375
375
  
376
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
377
                     struct argp_state *state) {
 
378
    /* Get the INPUT argument from `argp_parse', which we know is a
 
379
       pointer to our plugin list pointer. */
378
380
    switch (key) {
379
381
    case 'g':                   /* --global-options */
380
382
      if (arg != NULL){
390
392
        }
391
393
      }
392
394
      break;
393
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
394
396
      if(arg == NULL){
395
397
        break;
396
398
      }
407
409
    case 'o':                   /* --options-for */
408
410
      if (arg != NULL){
409
411
        char *p_name = strsep(&arg, ":");
410
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
411
413
          break;
412
414
        }
413
415
        char *opt = strsep(&arg, ":");
414
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
415
417
          break;
416
418
        }
417
 
        char *p;
418
 
        while((p = strsep(&opt, ",")) != NULL){
419
 
          if(p[0] == '\0'){
420
 
            continue;
421
 
          }
422
 
          if(not add_argument(getplugin(p_name), p)){
423
 
            perror("add_argument");
424
 
            return ARGP_ERR_UNKNOWN;
 
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
            }
425
429
          }
426
430
        }
427
431
      }
428
432
      break;
429
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
430
434
      if(arg == NULL){
431
435
        break;
432
436
      }
454
458
        p->disabled = true;
455
459
      }
456
460
      break;
457
 
    case 'e':                   /* --enable */
458
 
      if (arg != NULL){
459
 
        plugin *p = getplugin(arg);
460
 
        if(p == NULL){
461
 
          return ARGP_ERR_UNKNOWN;
462
 
        }
463
 
        p->disabled = false;
464
 
      }
465
 
      break;
466
461
    case 128:                   /* --plugin-dir */
467
462
      plugindir = strdup(arg);
468
463
      if(plugindir == NULL){
470
465
      }      
471
466
      break;
472
467
    case 129:                   /* --config-file */
473
 
      /* This is already done by parse_opt_config_file() */
474
 
      break;
 
468
      argfile = strdup(arg);
 
469
      if(argfile == NULL){
 
470
        perror("strdup");
 
471
      }
 
472
      break;      
475
473
    case 130:                   /* --userid */
476
474
      uid = (uid_t)strtol(arg, NULL, 10);
477
475
      break;
492
490
    return 0;
493
491
  }
494
492
  
495
 
  /* This option parser is the same as parse_opt() above, except it
496
 
     ignores everything but the --config-file option. */
497
 
  error_t parse_opt_config_file (int key, char *arg,
498
 
                                 __attribute__((unused))
499
 
                                 struct argp_state *state) {
500
 
    switch (key) {
501
 
    case 'g':                   /* --global-options */
502
 
    case 'G':                   /* --global-env */
503
 
    case 'o':                   /* --options-for */
504
 
    case 'E':                   /* --env-for */
505
 
    case 'd':                   /* --disable */
506
 
    case 'e':                   /* --enable */
507
 
    case 128:                   /* --plugin-dir */
508
 
      break;
509
 
    case 129:                   /* --config-file */
510
 
      argfile = strdup(arg);
511
 
      if(argfile == NULL){
512
 
        perror("strdup");
513
 
      }
514
 
      break;      
515
 
    case 130:                   /* --userid */
516
 
    case 131:                   /* --groupid */
517
 
    case 132:                   /* --debug */
518
 
    case ARGP_KEY_ARG:
519
 
    case ARGP_KEY_END:
520
 
      break;
521
 
    default:
522
 
      return ARGP_ERR_UNKNOWN;
523
 
    }
524
 
    return 0;
525
 
  }
526
 
  
527
 
  struct argp argp = { .options = options,
528
 
                       .parser = parse_opt_config_file,
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
529
494
                       .args_doc = "",
530
495
                       .doc = "Mandos plugin runner -- Run plugins" };
531
496
  
532
 
  /* Parse using the parse_opt_config_file in order to get the custom
533
 
     config file location, if any. */
534
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
535
 
  if (ret == ARGP_ERR_UNKNOWN){
536
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
537
 
    exitstatus = EXIT_FAILURE;
538
 
    goto fallback;
539
 
  }
540
 
  
541
 
  /* Reset to the normal argument parser */
542
 
  argp.parser = parse_opt;
543
 
  
544
497
  /* Open the configfile if available */
545
498
  if (argfile == NULL){
546
499
    conffp = fopen(AFILE, "r");
742
695
    }
743
696
 
744
697
    char *filename;
745
 
    if(plugindir == NULL){
746
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
747
 
    } else {
748
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
749
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
750
699
    if(ret < 0){
751
700
      perror("asprintf");
752
701
      continue;
1042
991
    bool bret;
1043
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
993
    char *passwordbuffer = getpass("Password: ");
1045
 
    size_t len = strlen(passwordbuffer);
1046
 
    /* Strip trailing newline */
1047
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1048
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1049
 
      len--;
1050
 
    }
1051
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
995
    if(not bret){
1053
996
      perror("print_out_password");
1054
997
      exitstatus = EXIT_FAILURE;