65
65
#include <errno.h>              /* errno, EBADF */
 
67
67
#define BUFFER_SIZE 256
 
69
 
#define PDIR "/lib/mandos/plugins.d"
 
70
 
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
 
68
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
72
70
const char *argp_program_version = "plugin-runner 1.0";
 
73
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
 
 
250
char **add_to_argv(char **argv, int *argc, char *arg){
 
 
253
    argv = malloc(sizeof(char*) * 2);
 
 
257
    argv[0] = NULL;     /* Will be set to argv[0] in main before
 
 
262
  argv = realloc(argv, sizeof(char *)
 
 
263
                  * ((unsigned int) *argc + 1));
 
252
272
static void free_plugin_list(plugin *plugin_list){
 
253
273
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
254
274
    next = plugin_list->next;
 
 
319
339
    { .name = "plugin-dir", .key = 128,
 
320
340
      .arg = "DIRECTORY",
 
321
341
      .doc = "Specify a different plugin directory", .group = 2 },
 
322
 
    { .name = "config-file", .key = 129,
 
324
 
      .doc = "Specify a different configuration file", .group = 2 },
 
325
 
    { .name = "userid", .key = 130,
 
326
 
      .arg = "ID", .flags = 0,
 
327
 
      .doc = "User ID the plugins will run as", .group = 3 },
 
328
 
    { .name = "groupid", .key = 131,
 
329
 
      .arg = "ID", .flags = 0,
 
330
 
      .doc = "Group ID the plugins will run as", .group = 3 },
 
331
 
    { .name = "debug", .key = 132,
 
332
 
      .doc = "Debug mode", .group = 4 },
 
 
342
    { .name = "userid", .key = 129,
 
 
343
      .arg = "ID", .flags = 0,
 
 
344
      .doc = "User ID the plugins will run as", .group = 2 },
 
 
345
    { .name = "groupid", .key = 130,
 
 
346
      .arg = "ID", .flags = 0,
 
 
347
      .doc = "Group ID the plugins will run as", .group = 2 },
 
 
348
    { .name = "debug", .key = 131,
 
 
349
      .doc = "Debug mode", .group = 3 },
 
 
422
 
      plugindir = strdup(arg);
 
423
 
      if(plugindir == NULL){
 
428
 
      argfile = strdup(arg);
 
 
442
      uid = (uid_t)strtol(arg, NULL, 10);
 
434
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
 
445
      gid = (gid_t)strtol(arg, NULL, 10);
 
437
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
442
450
    case ARGP_KEY_ARG:
 
 
466
 
  if (argfile == NULL){
 
467
 
    conffp = fopen(AFILE, "r");
 
469
 
    conffp = fopen(argfile, "r");
 
 
474
  conffp = fopen(argfile, "r");
 
472
475
  if(conffp != NULL){
 
473
476
    char *org_line = NULL;
 
474
477
    char *p, *arg, *new_arg, *line;
 
 
477
480
    const char whitespace_delims[] = " \r\t\f\v\n";
 
478
481
    const char comment_delim[] = "#";
 
481
 
    custom_argv = malloc(sizeof(char*) * 2);
 
482
 
    if(custom_argv == NULL){
 
484
 
      exitstatus = EXIT_FAILURE;
 
487
 
    custom_argv[0] = argv[0];
 
488
 
    custom_argv[1] = NULL;
 
491
484
      sret = getline(&org_line, &size, conffp);
 
 
502
495
        new_arg = strdup(p);
 
505
 
          exitstatus = EXIT_FAILURE;
 
511
 
        custom_argv = realloc(custom_argv, sizeof(char *)
 
512
 
                              * ((unsigned int) custom_argc + 1));
 
513
 
        if(custom_argv == NULL){
 
515
 
          exitstatus = EXIT_FAILURE;
 
519
 
        custom_argv[custom_argc-1] = new_arg;
 
520
 
        custom_argv[custom_argc] = NULL;        
 
 
496
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
 
497
        if (custom_argv == NULL){
 
 
498
          perror("add_to_argv");
 
 
499
          exitstatus = EXIT_FAILURE;
 
 
534
515
  if(custom_argv != NULL){
 
535
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
 
516
    custom_argv[0] = argv[0];
 
 
517
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
 
536
519
    if (ret == ARGP_ERR_UNKNOWN){
 
537
520
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
538
521
      exitstatus = EXIT_FAILURE;