/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-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,
132
132
  }
133
133
  new_plugin->argv[0] = copy_name;
134
134
  new_plugin->argv[1] = NULL;
135
 
 
 
135
  
136
136
  new_plugin->environ = malloc(sizeof(char *));
137
137
  if(new_plugin->environ == NULL){
138
138
    free(copy_name);
141
141
    return NULL;
142
142
  }
143
143
  new_plugin->environ[0] = NULL;
144
 
 
 
144
  
145
145
  /* Append the new plugin to the list */
146
146
  plugin_list = new_plugin;
147
147
  return new_plugin;
179
179
}
180
180
 
181
181
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def){
 
182
static bool add_environment(plugin *p, const char *def, bool replace){
183
183
  if(p == NULL){
184
184
    return false;
185
185
  }
 
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 */
 
192
      if(replace){
 
193
        char *new = realloc(*e, strlen(def) + 1);
 
194
        if(new == NULL){
 
195
          return false;
 
196
        }
 
197
        *e = new;
 
198
        strcpy(*e, def);
 
199
      }
 
200
      return true;
 
201
    }
 
202
  }
186
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
204
}
188
205
 
221
238
      /* No child processes */
222
239
      break;
223
240
    }
224
 
 
 
241
    
225
242
    /* A child exited, find it in process_list */
226
243
    while(proc != NULL and proc->pid != pid){
227
244
      proc = proc->next;
327
344
    { .name = "global-options", .key = 'g',
328
345
      .arg = "OPTION[,OPTION[,...]]",
329
346
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
347
    { .name = "global-env", .key = 'G',
331
348
      .arg = "VAR=value",
332
349
      .doc = "Environment variable passed to all plugins" },
333
350
    { .name = "options-for", .key = 'o',
334
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
352
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
353
    { .name = "env-for", .key = 'E',
337
354
      .arg = "PLUGIN:ENV=value",
338
355
      .doc = "Environment variable passed to specified plugin" },
339
356
    { .name = "disable", .key = 'd',
340
357
      .arg = "PLUGIN",
341
358
      .doc = "Disable a specific plugin", .group = 1 },
 
359
    { .name = "enable", .key = 'e',
 
360
      .arg = "PLUGIN",
 
361
      .doc = "Enable a specific plugin", .group = 1 },
342
362
    { .name = "plugin-dir", .key = 128,
343
363
      .arg = "DIRECTORY",
344
364
      .doc = "Specify a different plugin directory", .group = 2 },
356
376
    { .name = NULL }
357
377
  };
358
378
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
360
 
    /* Get the INPUT argument from `argp_parse', which we know is a
361
 
       pointer to our plugin list pointer. */
 
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
380
                     struct argp_state *state) {
362
381
    switch (key) {
363
382
    case 'g':                   /* --global-options */
364
383
      if (arg != NULL){
374
393
        }
375
394
      }
376
395
      break;
377
 
    case 'e':                   /* --global-envs */
 
396
    case 'G':                   /* --global-env */
378
397
      if(arg == NULL){
379
398
        break;
380
399
      }
383
402
        if(envdef == NULL){
384
403
          break;
385
404
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
 
405
        if(not add_environment(getplugin(NULL), envdef, true)){
387
406
          perror("add_environment");
388
407
        }
389
408
      }
391
410
    case 'o':                   /* --options-for */
392
411
      if (arg != NULL){
393
412
        char *p_name = strsep(&arg, ":");
394
 
        if(p_name[0] == '\0'){
 
413
        if(p_name[0] == '\0' or arg == NULL){
395
414
          break;
396
415
        }
397
416
        char *opt = strsep(&arg, ":");
398
 
        if(opt[0] == '\0'){
 
417
        if(opt[0] == '\0' or opt == NULL){
399
418
          break;
400
419
        }
401
 
        if(opt != NULL){
402
 
          char *p;
403
 
          while((p = strsep(&opt, ",")) != NULL){
404
 
            if(p[0] == '\0'){
405
 
              continue;
406
 
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
408
 
              perror("add_argument");
409
 
              return ARGP_ERR_UNKNOWN;
410
 
            }
 
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;
411
428
          }
412
429
        }
413
430
      }
414
431
      break;
415
 
    case 'f':                   /* --envs-for */
 
432
    case 'E':                   /* --env-for */
416
433
      if(arg == NULL){
417
434
        break;
418
435
      }
426
443
          break;
427
444
        }
428
445
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
446
        if(not add_environment(getplugin(p_name), envdef, true)){
430
447
          perror("add_environment");
431
448
        }
432
449
      }
440
457
        p->disabled = true;
441
458
      }
442
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;
443
469
    case 128:                   /* --plugin-dir */
444
470
      plugindir = strdup(arg);
445
471
      if(plugindir == NULL){
447
473
      }      
448
474
      break;
449
475
    case 129:                   /* --config-file */
450
 
      argfile = strdup(arg);
451
 
      if(argfile == NULL){
452
 
        perror("strdup");
453
 
      }
454
 
      break;      
 
476
      /* This is already done by parse_opt_config_file() */
 
477
      break;
455
478
    case 130:                   /* --userid */
456
479
      uid = (uid_t)strtol(arg, NULL, 10);
457
480
      break;
472
495
    return 0;
473
496
  }
474
497
  
475
 
  struct argp argp = { .options = options, .parser = parse_opt,
476
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
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,
 
532
                       .args_doc = "",
477
533
                       .doc = "Mandos plugin runner -- Run plugins" };
478
534
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
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);
480
538
  if (ret == ARGP_ERR_UNKNOWN){
481
539
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
540
    exitstatus = EXIT_FAILURE;
483
541
    goto fallback;
484
542
  }
485
 
 
486
 
  /* Opens the configfile if aviable */
 
543
  
 
544
  /* Reset to the normal argument parser */
 
545
  argp.parser = parse_opt;
 
546
  
 
547
  /* Open the configfile if available */
487
548
  if (argfile == NULL){
488
549
    conffp = fopen(AFILE, "r");
489
550
  } else {
507
568
    custom_argv[0] = argv[0];
508
569
    custom_argv[1] = NULL;
509
570
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
571
    /* for each line in the config file, strip whitespace and ignore
 
572
       commented text */
511
573
    while(true){
512
574
      sret = getline(&org_line, &size, conffp);
513
575
      if(sret == -1){
542
604
      }
543
605
    }
544
606
    free(org_line);
545
 
  } else{
 
607
  } else {
546
608
    /* Check for harmful errors and go to fallback. Other errors might
547
609
       not affect opening plugins */
548
610
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
554
616
  /* If there was any arguments from configuration file,
555
617
     pass them to parser as command arguments */
556
618
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
619
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
620
                      0, NULL);
558
621
    if (ret == ARGP_ERR_UNKNOWN){
559
622
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
623
      exitstatus = EXIT_FAILURE;
562
625
    }
563
626
  }
564
627
  
 
628
  /* Parse actual command line arguments, to let them override the
 
629
     config file */
 
630
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
631
  if (ret == ARGP_ERR_UNKNOWN){
 
632
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
633
    exitstatus = EXIT_FAILURE;
 
634
    goto fallback;
 
635
  }
 
636
  
565
637
  if(debug){
566
638
    for(plugin *p = plugin_list; p != NULL; p=p->next){
567
639
      fprintf(stderr, "Plugin: %s has %d arguments\n",
575
647
      }
576
648
    }
577
649
  }
578
 
 
 
650
  
579
651
  /* Strip permissions down to nobody */
580
652
  ret = setuid(uid);
581
653
  if (ret == -1){
585
657
  if (ret == -1){
586
658
    perror("setgid");
587
659
  }
588
 
 
 
660
  
589
661
  if (plugindir == NULL){
590
662
    dir = opendir(PDIR);
591
663
  } else {
612
684
  }
613
685
  
614
686
  FD_ZERO(&rfds_all);
615
 
 
 
687
  
616
688
  /* Read and execute any executable in the plugin directory*/
617
689
  while(true){
618
690
    dirst = readdir(dir);
619
691
    
620
 
    // All directory entries have been processed
 
692
    /* All directory entries have been processed */
621
693
    if(dirst == NULL){
622
694
      if (errno == EBADF){
623
695
        perror("readdir");
629
701
    
630
702
    d_name_len = strlen(dirst->d_name);
631
703
    
632
 
    // Ignore dotfiles, backup files and other junk
 
704
    /* Ignore dotfiles, backup files and other junk */
633
705
    {
634
706
      bool bad_name = false;
635
707
      
721
793
        }
722
794
        /* Add global environment variables */
723
795
        for(char **e = g->environ; *e != NULL; e++){
724
 
          if(not add_environment(p, *e)){
 
796
          if(not add_environment(p, *e, false)){
725
797
            perror("add_environment");
726
798
          }
727
799
        }
732
804
       process, too. */
733
805
    if(p->environ[0] != NULL){
734
806
      for(char **e = environ; *e != NULL; e++){
735
 
        char *copy = strdup(*e);
736
 
        if(copy == NULL){
737
 
          perror("strdup");
738
 
          continue;
739
 
        }
740
 
        if(not add_environment(p, copy)){
 
807
        if(not add_environment(p, *e, false)){
741
808
          perror("add_environment");
742
809
        }
743
810
      }
770
837
      exitstatus = EXIT_FAILURE;
771
838
      goto fallback;
772
839
    }
773
 
    // Starting a new process to be watched
 
840
    /* Starting a new process to be watched */
774
841
    pid_t pid = fork();
775
842
    if(pid == -1){
776
843
      perror("fork");