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){
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 */
193
char *new = realloc(*e, strlen(def));
191
/* Refuse to add an existing variable */
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',
358
350
.doc = "Disable a specific plugin", .group = 1 },
359
{ .name = "enable", .key = 'e',
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 },
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. */
382
373
case 'g': /* --global-options */
383
374
if (arg != NULL){
402
393
if(envdef == NULL){
405
if(not add_environment(getplugin(NULL), envdef, true)){
396
if(not add_environment(getplugin(NULL), envdef)){
406
397
perror("add_environment");
448
if(not add_environment(getplugin(p_name), envdef, true)){
439
if(not add_environment(getplugin(p_name), envdef)){
449
440
perror("add_environment");
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) {
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 */
514
case 129: /* --config-file */
515
argfile = strdup(arg);
520
case 130: /* --userid */
521
case 131: /* --groupid */
522
case 132: /* --debug */
527
return ARGP_ERR_UNKNOWN;
532
struct argp argp = { .options = options,
533
.parser = parse_opt_config_file,
485
struct argp argp = { .options = options, .parser = parse_opt,
486
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
535
487
.doc = "Mandos plugin runner -- Run plugins" };
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;
546
/* Reset to the normal argument parser */
547
argp.parser = parse_opt;
549
/* Open the configfile if available */
496
/* Opens the configfile if aviable */
550
497
if (argfile == NULL){
551
498
conffp = fopen(AFILE, "r");
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,
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;
630
/* Parse actual command line arguments, to let them override the
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;
640
577
for(plugin *p = plugin_list; p != NULL; p=p->next){
641
578
fprintf(stderr, "Plugin: %s has %d arguments\n",
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");
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);
751
if(not add_environment(p, copy)){
810
752
perror("add_environment");