/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-01 08:29:23 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901082923-i2liq6t7warmu9xe
* mandos.xml: Enclose "RAM" with <acronym>.
* overview.xml: - '' -

* plugin-runner.xml (DESCRIPTION): Improved wording.
  (PURPOSE): New section.
  (OPTIONS): Improved wording.
  (OVERVIEW, PLUGINS): New section.
  (FALLBACK): New empty placeholder section.

* plugins.d/password-prompt.xml: Enclose "RAM" with <acronym>.

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 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 */
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, bool replace){
 
182
static bool add_environment(plugin *p, const char *def){
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
 
  }
203
186
  return add_to_char_array(def, &(p->environ), &(p->envc));
204
187
}
205
188
 
238
221
      /* No child processes */
239
222
      break;
240
223
    }
241
 
    
 
224
 
242
225
    /* A child exited, find it in process_list */
243
226
    while(proc != NULL and proc->pid != pid){
244
227
      proc = proc->next;
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',
357
340
      .arg = "PLUGIN",
358
341
      .doc = "Disable a specific plugin", .group = 1 },
359
 
    { .name = "enable", .key = 'e',
360
 
      .arg = "PLUGIN",
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 },
378
358
  
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. */
381
363
    switch (key) {
382
364
    case 'g':                   /* --global-options */
383
365
      if (arg != NULL){
393
375
        }
394
376
      }
395
377
      break;
396
 
    case 'G':                   /* --global-env */
 
378
    case 'e':                   /* --global-envs */
397
379
      if(arg == NULL){
398
380
        break;
399
381
      }
400
 
      if(not add_environment(getplugin(NULL), arg, true)){
401
 
        perror("add_environment");
 
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
        }
402
390
      }
403
391
      break;
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'){
408
396
          break;
409
397
        }
410
398
        char *opt = strsep(&arg, ":");
411
 
        if(opt[0] == '\0' or opt == NULL){
 
399
        if(opt[0] == '\0'){
412
400
          break;
413
401
        }
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;
 
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
            }
422
412
          }
423
413
        }
424
414
      }
425
415
      break;
426
 
    case 'E':                   /* --env-for */
 
416
    case 'f':                   /* --envs-for */
427
417
      if(arg == NULL){
428
418
        break;
429
419
      }
432
422
        if(envdef == NULL){
433
423
          break;
434
424
        }
435
 
        *envdef = '\0';
436
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
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)){
437
431
          perror("add_environment");
438
432
        }
439
433
      }
447
441
        p->disabled = true;
448
442
      }
449
443
      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;
459
444
    case 128:                   /* --plugin-dir */
460
 
      free(plugindir);
461
445
      plugindir = strdup(arg);
462
446
      if(plugindir == NULL){
463
447
        perror("strdup");
464
448
      }      
465
449
      break;
466
450
    case 129:                   /* --config-file */
467
 
      /* This is already done by parse_opt_config_file() */
468
 
      break;
 
451
      argfile = strdup(arg);
 
452
      if(argfile == NULL){
 
453
        perror("strdup");
 
454
      }
 
455
      break;      
469
456
    case 130:                   /* --userid */
470
457
      uid = (uid_t)strtol(arg, NULL, 10);
471
458
      break;
486
473
    return 0;
487
474
  }
488
475
  
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 = "",
 
476
  struct argp argp = { .options = options, .parser = parse_opt,
 
477
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
525
478
                       .doc = "Mandos plugin runner -- Run plugins" };
526
479
  
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;
533
484
    goto fallback;
534
485
  }
535
 
  
536
 
  /* Reset to the normal argument parser */
537
 
  argp.parser = parse_opt;
538
 
  
539
 
  /* Open the configfile if available */
 
486
 
 
487
  /* Opens the configfile if aviable */
540
488
  if (argfile == NULL){
541
489
    conffp = fopen(AFILE, "r");
542
490
  } else {
596
544
      }
597
545
    }
598
546
    free(org_line);
599
 
  } else {
 
547
  } else{
600
548
    /* Check for harmful errors and go to fallback. Other errors might
601
549
       not affect opening plugins */
602
550
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
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,
612
 
                      0, NULL);
 
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;
617
564
    }
618
565
  }
619
566
  
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
 
  
629
567
  if(debug){
630
568
    for(plugin *p = plugin_list; p != NULL; p=p->next){
631
569
      fprintf(stderr, "Plugin: %s has %d arguments\n",
639
577
      }
640
578
    }
641
579
  }
642
 
  
 
580
 
643
581
  /* Strip permissions down to nobody */
644
582
  ret = setuid(uid);
645
583
  if (ret == -1){
649
587
  if (ret == -1){
650
588
    perror("setgid");
651
589
  }
652
 
  
 
590
 
653
591
  if (plugindir == NULL){
654
592
    dir = opendir(PDIR);
655
593
  } else {
676
614
  }
677
615
  
678
616
  FD_ZERO(&rfds_all);
679
 
  
 
617
 
680
618
  /* Read and execute any executable in the plugin directory*/
681
619
  while(true){
682
620
    dirst = readdir(dir);
683
621
    
684
 
    /* All directory entries have been processed */
 
622
    // All directory entries have been processed
685
623
    if(dirst == NULL){
686
624
      if (errno == EBADF){
687
625
        perror("readdir");
693
631
    
694
632
    d_name_len = strlen(dirst->d_name);
695
633
    
696
 
    /* Ignore dotfiles, backup files and other junk */
 
634
    // Ignore dotfiles, backup files and other junk
697
635
    {
698
636
      bool bad_name = false;
699
637
      
785
723
        }
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");
790
728
          }
791
729
        }
796
734
       process, too. */
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);
 
738
        if(copy == NULL){
 
739
          perror("strdup");
 
740
          continue;
 
741
        }
 
742
        if(not add_environment(p, copy)){
800
743
          perror("add_environment");
801
744
        }
802
745
      }
829
772
      exitstatus = EXIT_FAILURE;
830
773
      goto fallback;
831
774
    }
832
 
    /* Starting a new process to be watched */
 
775
    // Starting a new process to be watched
833
776
    pid_t pid = fork();
834
777
    if(pid == -1){
835
778
      perror("fork");
932
875
    }
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;
966
909
            goto fallback;
967
910
          }
968
 
          
 
911
          free_plugin(proc);
969
912
          /* We are done modifying process list, so unblock signal */
970
913
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
971
914
                             NULL);
978
921
          if(plugin_list == NULL){
979
922
            break;
980
923
          }
981
 
          
982
 
          plugin *next_plugin = proc->next;
983
 
          free_plugin(proc);
984
 
          proc = next_plugin;
985
924
          continue;
986
925
        }
987
926
        
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 */
1002
 
        proc = proc->next;
1003
941
        continue;
1004
942
      }
1005
943
      /* Before reading, make the process' data buffer large enough */
1018
956
                 BUFFER_SIZE);
1019
957
      if(ret < 0){
1020
958
        /* Read error from this process; ignore the error */
1021
 
        proc = proc->next;
1022
959
        continue;
1023
960
      }
1024
961
      if(ret == 0){