/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Björn Påhlsson
  • Date: 2008-09-05 19:30:21 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 176.
  • Revision ID: belorn@braxen-20080905193021-vie7b22ootg6r2t8
Several memory leaks detected by valgrind fixed
A bugg fixed that could cause seg fault.

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 },
358
378
  
359
379
  error_t parse_opt (int key, char *arg, __attribute__((unused))
360
380
                     struct argp_state *state) {
361
 
    /* Get the INPUT argument from `argp_parse', which we know is a
362
 
       pointer to our plugin list pointer. */
363
381
    switch (key) {
364
382
    case 'g':                   /* --global-options */
365
383
      if (arg != NULL){
375
393
        }
376
394
      }
377
395
      break;
378
 
    case 'e':                   /* --global-envs */
 
396
    case 'G':                   /* --global-env */
379
397
      if(arg == NULL){
380
398
        break;
381
399
      }
382
 
      {
383
 
        char *envdef = strdup(arg);
384
 
        if(envdef == NULL){
385
 
          break;
386
 
        }
387
 
        if(not add_environment(getplugin(NULL), envdef)){
388
 
          perror("add_environment");
389
 
        }
 
400
      if(not add_environment(getplugin(NULL), arg, true)){
 
401
        perror("add_environment");
390
402
      }
391
403
      break;
392
404
    case 'o':                   /* --options-for */
393
405
      if (arg != NULL){
394
406
        char *p_name = strsep(&arg, ":");
395
 
        if(p_name[0] == '\0'){
 
407
        if(p_name[0] == '\0' or arg == NULL){
396
408
          break;
397
409
        }
398
410
        char *opt = strsep(&arg, ":");
399
 
        if(opt[0] == '\0'){
 
411
        if(opt[0] == '\0' or opt == NULL){
400
412
          break;
401
413
        }
402
 
        if(opt != NULL){
403
 
          char *p;
404
 
          while((p = strsep(&opt, ",")) != NULL){
405
 
            if(p[0] == '\0'){
406
 
              continue;
407
 
            }
408
 
            if(not add_argument(getplugin(p_name), p)){
409
 
              perror("add_argument");
410
 
              return ARGP_ERR_UNKNOWN;
411
 
            }
 
414
        char *p;
 
415
        while((p = strsep(&opt, ",")) != NULL){
 
416
          if(p[0] == '\0'){
 
417
            continue;
 
418
          }
 
419
          if(not add_argument(getplugin(p_name), p)){
 
420
            perror("add_argument");
 
421
            return ARGP_ERR_UNKNOWN;
412
422
          }
413
423
        }
414
424
      }
415
425
      break;
416
 
    case 'f':                   /* --envs-for */
 
426
    case 'E':                   /* --env-for */
417
427
      if(arg == NULL){
418
428
        break;
419
429
      }
422
432
        if(envdef == NULL){
423
433
          break;
424
434
        }
425
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
426
 
        if(p_name == NULL){
427
 
          break;
428
 
        }
429
 
        envdef++;
430
 
        if(not add_environment(getplugin(p_name), envdef)){
 
435
        *envdef = '\0';
 
436
        if(not add_environment(getplugin(arg), envdef+1, true)){
431
437
          perror("add_environment");
432
438
        }
433
439
      }
441
447
        p->disabled = true;
442
448
      }
443
449
      break;
 
450
    case 'e':                   /* --enable */
 
451
      if (arg != NULL){
 
452
        plugin *p = getplugin(arg);
 
453
        if(p == NULL){
 
454
          return ARGP_ERR_UNKNOWN;
 
455
        }
 
456
        p->disabled = false;
 
457
      }
 
458
      break;
444
459
    case 128:                   /* --plugin-dir */
 
460
      free(plugindir);
445
461
      plugindir = strdup(arg);
446
462
      if(plugindir == NULL){
447
463
        perror("strdup");
448
464
      }      
449
465
      break;
450
466
    case 129:                   /* --config-file */
451
 
      argfile = strdup(arg);
452
 
      if(argfile == NULL){
453
 
        perror("strdup");
454
 
      }
455
 
      break;      
 
467
      /* This is already done by parse_opt_config_file() */
 
468
      break;
456
469
    case 130:                   /* --userid */
457
470
      uid = (uid_t)strtol(arg, NULL, 10);
458
471
      break;
473
486
    return 0;
474
487
  }
475
488
  
476
 
  struct argp argp = { .options = options, .parser = parse_opt,
477
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
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) {
 
494
    switch (key) {
 
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 */
 
502
      break;
 
503
    case 129:                   /* --config-file */
 
504
      free(argfile);
 
505
      argfile = strdup(arg);
 
506
      if(argfile == NULL){
 
507
        perror("strdup");
 
508
      }
 
509
      break;      
 
510
    case 130:                   /* --userid */
 
511
    case 131:                   /* --groupid */
 
512
    case 132:                   /* --debug */
 
513
    case ARGP_KEY_ARG:
 
514
    case ARGP_KEY_END:
 
515
      break;
 
516
    default:
 
517
      return ARGP_ERR_UNKNOWN;
 
518
    }
 
519
    return 0;
 
520
  }
 
521
  
 
522
  struct argp argp = { .options = options,
 
523
                       .parser = parse_opt_config_file,
 
524
                       .args_doc = "",
478
525
                       .doc = "Mandos plugin runner -- Run plugins" };
479
526
  
480
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
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);
481
530
  if (ret == ARGP_ERR_UNKNOWN){
482
531
    fprintf(stderr, "Unknown error while parsing arguments\n");
483
532
    exitstatus = EXIT_FAILURE;
484
533
    goto fallback;
485
534
  }
486
 
 
487
 
  /* Opens the configfile if aviable */
 
535
  
 
536
  /* Reset to the normal argument parser */
 
537
  argp.parser = parse_opt;
 
538
  
 
539
  /* Open the configfile if available */
488
540
  if (argfile == NULL){
489
541
    conffp = fopen(AFILE, "r");
490
542
  } else {
544
596
      }
545
597
    }
546
598
    free(org_line);
547
 
  } else{
 
599
  } else {
548
600
    /* Check for harmful errors and go to fallback. Other errors might
549
601
       not affect opening plugins */
550
602
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
556
608
  /* If there was any arguments from configuration file,
557
609
     pass them to parser as command arguments */
558
610
  if(custom_argv != NULL){
559
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
611
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
612
                      0, NULL);
560
613
    if (ret == ARGP_ERR_UNKNOWN){
561
614
      fprintf(stderr, "Unknown error while parsing arguments\n");
562
615
      exitstatus = EXIT_FAILURE;
564
617
    }
565
618
  }
566
619
  
 
620
  /* Parse actual command line arguments, to let them override the
 
621
     config file */
 
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;
 
626
    goto fallback;
 
627
  }
 
628
  
567
629
  if(debug){
568
630
    for(plugin *p = plugin_list; p != NULL; p=p->next){
569
631
      fprintf(stderr, "Plugin: %s has %d arguments\n",
577
639
      }
578
640
    }
579
641
  }
580
 
 
 
642
  
581
643
  /* Strip permissions down to nobody */
582
644
  ret = setuid(uid);
583
645
  if (ret == -1){
587
649
  if (ret == -1){
588
650
    perror("setgid");
589
651
  }
590
 
 
 
652
  
591
653
  if (plugindir == NULL){
592
654
    dir = opendir(PDIR);
593
655
  } else {
614
676
  }
615
677
  
616
678
  FD_ZERO(&rfds_all);
617
 
 
 
679
  
618
680
  /* Read and execute any executable in the plugin directory*/
619
681
  while(true){
620
682
    dirst = readdir(dir);
621
683
    
622
 
    // All directory entries have been processed
 
684
    /* All directory entries have been processed */
623
685
    if(dirst == NULL){
624
686
      if (errno == EBADF){
625
687
        perror("readdir");
631
693
    
632
694
    d_name_len = strlen(dirst->d_name);
633
695
    
634
 
    // Ignore dotfiles, backup files and other junk
 
696
    /* Ignore dotfiles, backup files and other junk */
635
697
    {
636
698
      bool bad_name = false;
637
699
      
723
785
        }
724
786
        /* Add global environment variables */
725
787
        for(char **e = g->environ; *e != NULL; e++){
726
 
          if(not add_environment(p, *e)){
 
788
          if(not add_environment(p, *e, false)){
727
789
            perror("add_environment");
728
790
          }
729
791
        }
734
796
       process, too. */
735
797
    if(p->environ[0] != NULL){
736
798
      for(char **e = environ; *e != NULL; e++){
737
 
        char *copy = strdup(*e);
738
 
        if(copy == NULL){
739
 
          perror("strdup");
740
 
          continue;
741
 
        }
742
 
        if(not add_environment(p, copy)){
 
799
        if(not add_environment(p, *e, false)){
743
800
          perror("add_environment");
744
801
        }
745
802
      }
772
829
      exitstatus = EXIT_FAILURE;
773
830
      goto fallback;
774
831
    }
775
 
    // Starting a new process to be watched
 
832
    /* Starting a new process to be watched */
776
833
    pid_t pid = fork();
777
834
    if(pid == -1){
778
835
      perror("fork");
875
932
    }
876
933
    /* OK, now either a process completed, or something can be read
877
934
       from one of them */
878
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
935
    for(plugin *proc = plugin_list; proc != NULL;){
879
936
      /* Is this process completely done? */
880
937
      if(proc->eof and proc->completed){
881
938
        /* Only accept the plugin output if it exited cleanly */
908
965
            exitstatus = EXIT_FAILURE;
909
966
            goto fallback;
910
967
          }
911
 
          free_plugin(proc);
 
968
          
912
969
          /* We are done modifying process list, so unblock signal */
913
970
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
914
971
                             NULL);
921
978
          if(plugin_list == NULL){
922
979
            break;
923
980
          }
 
981
          
 
982
          plugin *next_plugin = proc->next;
 
983
          free_plugin(proc);
 
984
          proc = next_plugin;
924
985
          continue;
925
986
        }
926
987
        
938
999
      /* This process has not completed.  Does it have any output? */
939
1000
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
940
1001
        /* This process had nothing to say at this time */
 
1002
        proc = proc->next;
941
1003
        continue;
942
1004
      }
943
1005
      /* Before reading, make the process' data buffer large enough */
956
1018
                 BUFFER_SIZE);
957
1019
      if(ret < 0){
958
1020
        /* Read error from this process; ignore the error */
 
1021
        proc = proc->next;
959
1022
        continue;
960
1023
      }
961
1024
      if(ret == 0){