/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 16:19:32 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901161932-ostp7tulh9aijulh
* plugin-runner.c (add_environment): Never insert existing environment
                                     variables.
  (main): Rename "--global-envs" to "--global-env" and "--envs-for" to
          "--env-for".

* plugin-runner.xml (SYNOPSIS): Rename "--global-envs" to
                                "--global-env" and "--envs-for" to
                                "--env-for".
  (OPTIONS): Added "--global-env" and "--env-for".
  (FALLBACK): Add id attribute.
  (EXIT STATUS): Add text.
  (ENVIRONMENT): New section.
  (FILES): Document configuration file.

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,
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
  }
187
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
188
  /* Search for this environment variable */
189
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
 
      }
 
190
    if(strncmp(*e, def, namelen+1) == 0){
 
191
      /* Refuse to add an existing variable */
200
192
      return true;
201
193
    }
202
194
  }
238
230
      /* No child processes */
239
231
      break;
240
232
    }
241
 
    
 
233
 
242
234
    /* A child exited, find it in process_list */
243
235
    while(proc != NULL and proc->pid != pid){
244
236
      proc = proc->next;
255
247
/* Prints out a password to stdout */
256
248
bool print_out_password(const char *buffer, size_t length){
257
249
  ssize_t ret;
 
250
  if(length>0 and buffer[length-1] == '\n'){
 
251
    length--;
 
252
  }
258
253
  for(size_t written = 0; written < length; written += (size_t)ret){
259
254
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
260
255
                                   length - written));
341
336
    { .name = "global-options", .key = 'g',
342
337
      .arg = "OPTION[,OPTION[,...]]",
343
338
      .doc = "Options passed to all plugins" },
344
 
    { .name = "global-env", .key = 'G',
 
339
    { .name = "global-env", .key = 'e',
345
340
      .arg = "VAR=value",
346
341
      .doc = "Environment variable passed to all plugins" },
347
342
    { .name = "options-for", .key = 'o',
348
343
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
349
344
      .doc = "Options passed only to specified plugin" },
350
 
    { .name = "env-for", .key = 'E',
 
345
    { .name = "env-for", .key = 'f',
351
346
      .arg = "PLUGIN:ENV=value",
352
347
      .doc = "Environment variable passed to specified plugin" },
353
348
    { .name = "disable", .key = 'd',
354
349
      .arg = "PLUGIN",
355
350
      .doc = "Disable a specific plugin", .group = 1 },
356
 
    { .name = "enable", .key = 'e',
357
 
      .arg = "PLUGIN",
358
 
      .doc = "Enable a specific plugin", .group = 1 },
359
351
    { .name = "plugin-dir", .key = 128,
360
352
      .arg = "DIRECTORY",
361
353
      .doc = "Specify a different plugin directory", .group = 2 },
375
367
  
376
368
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
369
                     struct argp_state *state) {
 
370
    /* Get the INPUT argument from `argp_parse', which we know is a
 
371
       pointer to our plugin list pointer. */
378
372
    switch (key) {
379
373
    case 'g':                   /* --global-options */
380
374
      if (arg != NULL){
390
384
        }
391
385
      }
392
386
      break;
393
 
    case 'G':                   /* --global-env */
 
387
    case 'e':                   /* --global-env */
394
388
      if(arg == NULL){
395
389
        break;
396
390
      }
397
 
      if(not add_environment(getplugin(NULL), arg, true)){
398
 
        perror("add_environment");
 
391
      {
 
392
        char *envdef = strdup(arg);
 
393
        if(envdef == NULL){
 
394
          break;
 
395
        }
 
396
        if(not add_environment(getplugin(NULL), envdef)){
 
397
          perror("add_environment");
 
398
        }
399
399
      }
400
400
      break;
401
401
    case 'o':                   /* --options-for */
402
402
      if (arg != NULL){
403
403
        char *p_name = strsep(&arg, ":");
404
 
        if(p_name[0] == '\0' or arg == NULL){
 
404
        if(p_name[0] == '\0'){
405
405
          break;
406
406
        }
407
407
        char *opt = strsep(&arg, ":");
408
 
        if(opt[0] == '\0' or opt == NULL){
 
408
        if(opt[0] == '\0'){
409
409
          break;
410
410
        }
411
 
        char *p;
412
 
        while((p = strsep(&opt, ",")) != NULL){
413
 
          if(p[0] == '\0'){
414
 
            continue;
415
 
          }
416
 
          if(not add_argument(getplugin(p_name), p)){
417
 
            perror("add_argument");
418
 
            return ARGP_ERR_UNKNOWN;
 
411
        if(opt != NULL){
 
412
          char *p;
 
413
          while((p = strsep(&opt, ",")) != NULL){
 
414
            if(p[0] == '\0'){
 
415
              continue;
 
416
            }
 
417
            if(not add_argument(getplugin(p_name), p)){
 
418
              perror("add_argument");
 
419
              return ARGP_ERR_UNKNOWN;
 
420
            }
419
421
          }
420
422
        }
421
423
      }
422
424
      break;
423
 
    case 'E':                   /* --env-for */
 
425
    case 'f':                   /* --env-for */
424
426
      if(arg == NULL){
425
427
        break;
426
428
      }
429
431
        if(envdef == NULL){
430
432
          break;
431
433
        }
432
 
        *envdef = '\0';
433
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
434
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
435
        if(p_name == NULL){
 
436
          break;
 
437
        }
 
438
        envdef++;
 
439
        if(not add_environment(getplugin(p_name), envdef)){
434
440
          perror("add_environment");
435
441
        }
436
442
      }
444
450
        p->disabled = true;
445
451
      }
446
452
      break;
447
 
    case 'e':                   /* --enable */
448
 
      if (arg != NULL){
449
 
        plugin *p = getplugin(arg);
450
 
        if(p == NULL){
451
 
          return ARGP_ERR_UNKNOWN;
452
 
        }
453
 
        p->disabled = false;
454
 
      }
455
 
      break;
456
453
    case 128:                   /* --plugin-dir */
457
 
      free(plugindir);
458
454
      plugindir = strdup(arg);
459
455
      if(plugindir == NULL){
460
456
        perror("strdup");
461
457
      }      
462
458
      break;
463
459
    case 129:                   /* --config-file */
464
 
      /* This is already done by parse_opt_config_file() */
465
 
      break;
 
460
      argfile = strdup(arg);
 
461
      if(argfile == NULL){
 
462
        perror("strdup");
 
463
      }
 
464
      break;      
466
465
    case 130:                   /* --userid */
467
466
      uid = (uid_t)strtol(arg, NULL, 10);
468
467
      break;
483
482
    return 0;
484
483
  }
485
484
  
486
 
  /* This option parser is the same as parse_opt() above, except it
487
 
     ignores everything but the --config-file option. */
488
 
  error_t parse_opt_config_file (int key, char *arg,
489
 
                                 __attribute__((unused))
490
 
                                 struct argp_state *state) {
491
 
    switch (key) {
492
 
    case 'g':                   /* --global-options */
493
 
    case 'G':                   /* --global-env */
494
 
    case 'o':                   /* --options-for */
495
 
    case 'E':                   /* --env-for */
496
 
    case 'd':                   /* --disable */
497
 
    case 'e':                   /* --enable */
498
 
    case 128:                   /* --plugin-dir */
499
 
      break;
500
 
    case 129:                   /* --config-file */
501
 
      free(argfile);
502
 
      argfile = strdup(arg);
503
 
      if(argfile == NULL){
504
 
        perror("strdup");
505
 
      }
506
 
      break;      
507
 
    case 130:                   /* --userid */
508
 
    case 131:                   /* --groupid */
509
 
    case 132:                   /* --debug */
510
 
    case ARGP_KEY_ARG:
511
 
    case ARGP_KEY_END:
512
 
      break;
513
 
    default:
514
 
      return ARGP_ERR_UNKNOWN;
515
 
    }
516
 
    return 0;
517
 
  }
518
 
  
519
 
  struct argp argp = { .options = options,
520
 
                       .parser = parse_opt_config_file,
521
 
                       .args_doc = "",
 
485
  struct argp argp = { .options = options, .parser = parse_opt,
 
486
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
522
487
                       .doc = "Mandos plugin runner -- Run plugins" };
523
488
  
524
 
  /* Parse using the parse_opt_config_file in order to get the custom
525
 
     config file location, if any. */
526
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
489
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
527
490
  if (ret == ARGP_ERR_UNKNOWN){
528
491
    fprintf(stderr, "Unknown error while parsing arguments\n");
529
492
    exitstatus = EXIT_FAILURE;
530
493
    goto fallback;
531
494
  }
532
 
  
533
 
  /* Reset to the normal argument parser */
534
 
  argp.parser = parse_opt;
535
 
  
536
 
  /* Open the configfile if available */
 
495
 
 
496
  /* Opens the configfile if aviable */
537
497
  if (argfile == NULL){
538
498
    conffp = fopen(AFILE, "r");
539
499
  } else {
593
553
      }
594
554
    }
595
555
    free(org_line);
596
 
  } else {
 
556
  } else{
597
557
    /* Check for harmful errors and go to fallback. Other errors might
598
558
       not affect opening plugins */
599
559
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
605
565
  /* If there was any arguments from configuration file,
606
566
     pass them to parser as command arguments */
607
567
  if(custom_argv != NULL){
608
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
609
 
                      0, NULL);
 
568
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
610
569
    if (ret == ARGP_ERR_UNKNOWN){
611
570
      fprintf(stderr, "Unknown error while parsing arguments\n");
612
571
      exitstatus = EXIT_FAILURE;
614
573
    }
615
574
  }
616
575
  
617
 
  /* Parse actual command line arguments, to let them override the
618
 
     config file */
619
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
620
 
  if (ret == ARGP_ERR_UNKNOWN){
621
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
622
 
    exitstatus = EXIT_FAILURE;
623
 
    goto fallback;
624
 
  }
625
 
  
626
576
  if(debug){
627
577
    for(plugin *p = plugin_list; p != NULL; p=p->next){
628
578
      fprintf(stderr, "Plugin: %s has %d arguments\n",
636
586
      }
637
587
    }
638
588
  }
639
 
  
 
589
 
640
590
  /* Strip permissions down to nobody */
641
591
  ret = setuid(uid);
642
592
  if (ret == -1){
646
596
  if (ret == -1){
647
597
    perror("setgid");
648
598
  }
649
 
  
 
599
 
650
600
  if (plugindir == NULL){
651
601
    dir = opendir(PDIR);
652
602
  } else {
673
623
  }
674
624
  
675
625
  FD_ZERO(&rfds_all);
676
 
  
 
626
 
677
627
  /* Read and execute any executable in the plugin directory*/
678
628
  while(true){
679
629
    dirst = readdir(dir);
680
630
    
681
 
    /* All directory entries have been processed */
 
631
    // All directory entries have been processed
682
632
    if(dirst == NULL){
683
633
      if (errno == EBADF){
684
634
        perror("readdir");
690
640
    
691
641
    d_name_len = strlen(dirst->d_name);
692
642
    
693
 
    /* Ignore dotfiles, backup files and other junk */
 
643
    // Ignore dotfiles, backup files and other junk
694
644
    {
695
645
      bool bad_name = false;
696
646
      
734
684
    }
735
685
 
736
686
    char *filename;
737
 
    if(plugindir == NULL){
738
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
739
 
    } else {
740
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
741
 
    }
 
687
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
742
688
    if(ret < 0){
743
689
      perror("asprintf");
744
690
      continue;
786
732
        }
787
733
        /* Add global environment variables */
788
734
        for(char **e = g->environ; *e != NULL; e++){
789
 
          if(not add_environment(p, *e, false)){
 
735
          if(not add_environment(p, *e)){
790
736
            perror("add_environment");
791
737
          }
792
738
        }
797
743
       process, too. */
798
744
    if(p->environ[0] != NULL){
799
745
      for(char **e = environ; *e != NULL; e++){
800
 
        if(not add_environment(p, *e, false)){
 
746
        char *copy = strdup(*e);
 
747
        if(copy == NULL){
 
748
          perror("strdup");
 
749
          continue;
 
750
        }
 
751
        if(not add_environment(p, copy)){
801
752
          perror("add_environment");
802
753
        }
803
754
      }
830
781
      exitstatus = EXIT_FAILURE;
831
782
      goto fallback;
832
783
    }
833
 
    /* Starting a new process to be watched */
 
784
    // Starting a new process to be watched
834
785
    pid_t pid = fork();
835
786
    if(pid == -1){
836
787
      perror("fork");
933
884
    }
934
885
    /* OK, now either a process completed, or something can be read
935
886
       from one of them */
936
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
887
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
937
888
      /* Is this process completely done? */
938
889
      if(proc->eof and proc->completed){
939
890
        /* Only accept the plugin output if it exited cleanly */
966
917
            exitstatus = EXIT_FAILURE;
967
918
            goto fallback;
968
919
          }
969
 
          
 
920
          free_plugin(proc);
970
921
          /* We are done modifying process list, so unblock signal */
971
922
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
972
923
                             NULL);
979
930
          if(plugin_list == NULL){
980
931
            break;
981
932
          }
982
 
          
983
 
          plugin *next_plugin = proc->next;
984
 
          free_plugin(proc);
985
 
          proc = next_plugin;
986
933
          continue;
987
934
        }
988
935
        
1000
947
      /* This process has not completed.  Does it have any output? */
1001
948
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1002
949
        /* This process had nothing to say at this time */
1003
 
        proc = proc->next;
1004
950
        continue;
1005
951
      }
1006
952
      /* Before reading, make the process' data buffer large enough */
1019
965
                 BUFFER_SIZE);
1020
966
      if(ret < 0){
1021
967
        /* Read error from this process; ignore the error */
1022
 
        proc = proc->next;
1023
968
        continue;
1024
969
      }
1025
970
      if(ret == 0){
1040
985
    bool bret;
1041
986
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1042
987
    char *passwordbuffer = getpass("Password: ");
1043
 
    size_t len = strlen(passwordbuffer);
1044
 
    /* Strip trailing newline */
1045
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1046
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1047
 
      len--;
1048
 
    }
1049
 
    bret = print_out_password(passwordbuffer, len);
 
988
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1050
989
    if(not bret){
1051
990
      perror("print_out_password");
1052
991
      exitstatus = EXIT_FAILURE;