97
97
static plugin *plugin_list = NULL;
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 */
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){
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 */
193
char *new = realloc(*e, strlen(def) + 1);
203
186
return add_to_char_array(def, &(p->environ), &(p->envc));
344
327
{ .name = "global-options", .key = 'g',
345
328
.arg = "OPTION[,OPTION[,...]]",
346
329
.doc = "Options passed to all plugins" },
347
{ .name = "global-env", .key = 'G',
330
{ .name = "global-envs", .key = 'e',
348
331
.arg = "VAR=value",
349
332
.doc = "Environment variable passed to all plugins" },
350
333
{ .name = "options-for", .key = 'o',
351
334
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
352
335
.doc = "Options passed only to specified plugin" },
353
{ .name = "env-for", .key = 'E',
336
{ .name = "envs-for", .key = 'f',
354
337
.arg = "PLUGIN:ENV=value",
355
338
.doc = "Environment variable passed to specified plugin" },
356
339
{ .name = "disable", .key = 'd',
358
341
.doc = "Disable a specific plugin", .group = 1 },
359
{ .name = "enable", .key = 'e',
361
.doc = "Enable a specific plugin", .group = 1 },
362
342
{ .name = "plugin-dir", .key = 128,
363
343
.arg = "DIRECTORY",
364
344
.doc = "Specify a different plugin directory", .group = 2 },
379
359
error_t parse_opt (int key, char *arg, __attribute__((unused))
380
360
struct argp_state *state) {
361
/* Get the INPUT argument from `argp_parse', which we know is a
362
pointer to our plugin list pointer. */
382
364
case 'g': /* --global-options */
383
365
if (arg != NULL){
396
case 'G': /* --global-env */
378
case 'e': /* --global-envs */
400
if(not add_environment(getplugin(NULL), arg, true)){
401
perror("add_environment");
383
char *envdef = strdup(arg);
387
if(not add_environment(getplugin(NULL), envdef)){
388
perror("add_environment");
404
392
case 'o': /* --options-for */
405
393
if (arg != NULL){
406
394
char *p_name = strsep(&arg, ":");
407
if(p_name[0] == '\0' or arg == NULL){
395
if(p_name[0] == '\0'){
410
398
char *opt = strsep(&arg, ":");
411
if(opt[0] == '\0' or opt == NULL){
415
while((p = strsep(&opt, ",")) != NULL){
419
if(not add_argument(getplugin(p_name), p)){
420
perror("add_argument");
421
return ARGP_ERR_UNKNOWN;
404
while((p = strsep(&opt, ",")) != NULL){
408
if(not add_argument(getplugin(p_name), p)){
409
perror("add_argument");
410
return ARGP_ERR_UNKNOWN;
426
case 'E': /* --env-for */
416
case 'f': /* --envs-for */
432
422
if(envdef == NULL){
436
if(not add_environment(getplugin(arg), envdef+1, true)){
425
char *p_name = strndup(arg, (size_t) (envdef-arg));
430
if(not add_environment(getplugin(p_name), envdef)){
437
431
perror("add_environment");
447
441
p->disabled = true;
450
case 'e': /* --enable */
452
plugin *p = getplugin(arg);
454
return ARGP_ERR_UNKNOWN;
459
444
case 128: /* --plugin-dir */
461
445
plugindir = strdup(arg);
462
446
if(plugindir == NULL){
463
447
perror("strdup");
466
450
case 129: /* --config-file */
467
/* This is already done by parse_opt_config_file() */
451
argfile = strdup(arg);
469
456
case 130: /* --userid */
470
457
uid = (uid_t)strtol(arg, NULL, 10);
489
/* This option parser is the same as parse_opt() above, except it
490
ignores everything but the --config-file option. */
491
error_t parse_opt_config_file (int key, char *arg,
492
__attribute__((unused))
493
struct argp_state *state) {
495
case 'g': /* --global-options */
496
case 'G': /* --global-env */
497
case 'o': /* --options-for */
498
case 'E': /* --env-for */
499
case 'd': /* --disable */
500
case 'e': /* --enable */
501
case 128: /* --plugin-dir */
503
case 129: /* --config-file */
505
argfile = strdup(arg);
510
case 130: /* --userid */
511
case 131: /* --groupid */
512
case 132: /* --debug */
517
return ARGP_ERR_UNKNOWN;
522
struct argp argp = { .options = options,
523
.parser = parse_opt_config_file,
476
struct argp argp = { .options = options, .parser = parse_opt,
477
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
525
478
.doc = "Mandos plugin runner -- Run plugins" };
527
/* Parse using the parse_opt_config_file in order to get the custom
528
config file location, if any. */
529
ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
480
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
530
481
if (ret == ARGP_ERR_UNKNOWN){
531
482
fprintf(stderr, "Unknown error while parsing arguments\n");
532
483
exitstatus = EXIT_FAILURE;
536
/* Reset to the normal argument parser */
537
argp.parser = parse_opt;
539
/* Open the configfile if available */
487
/* Opens the configfile if aviable */
540
488
if (argfile == NULL){
541
489
conffp = fopen(AFILE, "r");
608
556
/* If there was any arguments from configuration file,
609
557
pass them to parser as command arguments */
610
558
if(custom_argv != NULL){
611
ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
559
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
613
560
if (ret == ARGP_ERR_UNKNOWN){
614
561
fprintf(stderr, "Unknown error while parsing arguments\n");
615
562
exitstatus = EXIT_FAILURE;
620
/* Parse actual command line arguments, to let them override the
622
ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
623
if (ret == ARGP_ERR_UNKNOWN){
624
fprintf(stderr, "Unknown error while parsing arguments\n");
625
exitstatus = EXIT_FAILURE;
630
568
for(plugin *p = plugin_list; p != NULL; p=p->next){
631
569
fprintf(stderr, "Plugin: %s has %d arguments\n",
786
724
/* Add global environment variables */
787
725
for(char **e = g->environ; *e != NULL; e++){
788
if(not add_environment(p, *e, false)){
726
if(not add_environment(p, *e)){
789
727
perror("add_environment");
797
735
if(p->environ[0] != NULL){
798
736
for(char **e = environ; *e != NULL; e++){
799
if(not add_environment(p, *e, false)){
737
char *copy = strdup(*e);
742
if(not add_environment(p, copy)){
800
743
perror("add_environment");
933
876
/* OK, now either a process completed, or something can be read
934
877
from one of them */
935
for(plugin *proc = plugin_list; proc != NULL;){
878
for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
936
879
/* Is this process completely done? */
937
880
if(proc->eof and proc->completed){
938
881
/* Only accept the plugin output if it exited cleanly */
965
908
exitstatus = EXIT_FAILURE;
969
912
/* We are done modifying process list, so unblock signal */
970
913
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
999
938
/* This process has not completed. Does it have any output? */
1000
939
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1001
940
/* This process had nothing to say at this time */
1005
943
/* Before reading, make the process' data buffer large enough */