/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: 2009-01-15 03:35:34 UTC
  • mfrom: (24.1.122 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090115033534-wcldj74x0jgvv9o0
Merged newer version of mandos-list (now renamed mandos-ctl) from
Björn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008 Teddy Hogeborn
6
 
 * Copyright © 2008 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
                                   SIG_UNBLOCK, kill() */
66
66
#include <errno.h>              /* errno, EBADF */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
67
68
 
68
69
#define BUFFER_SIZE 256
69
70
 
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* Check for exiting plugin with that name */
102
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if ((p->name == name)
104
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
103
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
104
    if((p->name == name)
 
105
       or (p->name and name and (strcmp(p->name, name) == 0))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
111
  if(new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
124
125
                           .next = plugin_list };
125
126
  
126
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
128
  if(new_plugin->argv == NULL){
128
129
    free(copy_name);
129
130
    free(new_plugin);
130
131
    return NULL;
230
231
      break;
231
232
    }
232
233
    if(pid == -1){
233
 
      if (errno != ECHILD){
 
234
      if(errno != ECHILD){
234
235
        perror("waitpid");
235
236
      }
236
237
      /* No child processes */
308
309
  struct dirent *dirst;
309
310
  struct stat st;
310
311
  fd_set rfds_all;
311
 
  int ret, maxfd = 0;
 
312
  int ret, numchars, maxfd = 0;
 
313
  ssize_t sret;
 
314
  intmax_t tmpmax;
312
315
  uid_t uid = 65534;
313
316
  gid_t gid = 65534;
314
317
  bool debug = false;
371
374
    { .name = NULL }
372
375
  };
373
376
  
374
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
375
 
                     struct argp_state *state) {
376
 
    switch (key) {
 
377
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
378
                    struct argp_state *state) {
 
379
    switch(key) {
377
380
    case 'g':                   /* --global-options */
378
 
      if (arg != NULL){
 
381
      if(arg != NULL){
379
382
        char *p;
380
383
        while((p = strsep(&arg, ",")) != NULL){
381
384
          if(p[0] == '\0'){
397
400
      }
398
401
      break;
399
402
    case 'o':                   /* --options-for */
400
 
      if (arg != NULL){
 
403
      if(arg != NULL){
401
404
        char *p_name = strsep(&arg, ":");
402
405
        if(p_name[0] == '\0' or arg == NULL){
403
406
          break;
434
437
      }
435
438
      break;
436
439
    case 'd':                   /* --disable */
437
 
      if (arg != NULL){
 
440
      if(arg != NULL){
438
441
        plugin *p = getplugin(arg);
439
442
        if(p == NULL){
440
443
          return ARGP_ERR_UNKNOWN;
443
446
      }
444
447
      break;
445
448
    case 'e':                   /* --enable */
446
 
      if (arg != NULL){
 
449
      if(arg != NULL){
447
450
        plugin *p = getplugin(arg);
448
451
        if(p == NULL){
449
452
          return ARGP_ERR_UNKNOWN;
462
465
      /* This is already done by parse_opt_config_file() */
463
466
      break;
464
467
    case 130:                   /* --userid */
465
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
468
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
469
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
470
         or arg[numchars] != '\0'){
 
471
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
472
                PRIdMAX "\n", arg, (intmax_t)uid);
 
473
      } else {
 
474
        uid = (uid_t)tmpmax;
 
475
      }
466
476
      break;
467
477
    case 131:                   /* --groupid */
468
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
478
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
479
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
480
         or arg[numchars] != '\0'){
 
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
482
                PRIdMAX "\n", arg, (intmax_t)gid);
 
483
      } else {
 
484
        gid = (gid_t)tmpmax;
 
485
      }
469
486
      break;
470
487
    case 132:                   /* --debug */
471
488
      debug = true;
472
489
      break;
 
490
/*
 
491
 * When adding more options before this line, remember to also add a
 
492
 * "case" to the "parse_opt_config_file" function below.
 
493
 */
473
494
    case ARGP_KEY_ARG:
474
495
      /* Cryptsetup always passes an argument, which is an empty
475
496
         string if "none" was specified in /etc/crypttab.  So if
488
509
  
489
510
  /* This option parser is the same as parse_opt() above, except it
490
511
     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) {
 
512
  error_t parse_opt_config_file(int key, char *arg,
 
513
                                __attribute__((unused))
 
514
                                struct argp_state *state) {
 
515
    switch(key) {
495
516
    case 'g':                   /* --global-options */
496
517
    case 'G':                   /* --global-env */
497
518
    case 'o':                   /* --options-for */
524
545
                       .args_doc = "",
525
546
                       .doc = "Mandos plugin runner -- Run plugins" };
526
547
  
527
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
548
  /* Parse using parse_opt_config_file() in order to get the custom
528
549
     config file location, if any. */
529
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
530
 
  if (ret == ARGP_ERR_UNKNOWN){
 
550
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
551
  if(ret == ARGP_ERR_UNKNOWN){
531
552
    fprintf(stderr, "Unknown error while parsing arguments\n");
532
553
    exitstatus = EXIT_FAILURE;
533
554
    goto fallback;
537
558
  argp.parser = parse_opt;
538
559
  
539
560
  /* Open the configfile if available */
540
 
  if (argfile == NULL){
 
561
  if(argfile == NULL){
541
562
    conffp = fopen(AFILE, "r");
542
563
  } else {
543
564
    conffp = fopen(argfile, "r");
546
567
    char *org_line = NULL;
547
568
    char *p, *arg, *new_arg, *line;
548
569
    size_t size = 0;
549
 
    ssize_t sret;
550
570
    const char whitespace_delims[] = " \r\t\f\v\n";
551
571
    const char comment_delim[] = "#";
552
572
 
599
619
  } else {
600
620
    /* Check for harmful errors and go to fallback. Other errors might
601
621
       not affect opening plugins */
602
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
622
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
603
623
      perror("fopen");
604
624
      exitstatus = EXIT_FAILURE;
605
625
      goto fallback;
608
628
  /* If there was any arguments from configuration file,
609
629
     pass them to parser as command arguments */
610
630
  if(custom_argv != NULL){
611
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
612
 
                      0, NULL);
613
 
    if (ret == ARGP_ERR_UNKNOWN){
 
631
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
632
                     0, NULL);
 
633
    if(ret == ARGP_ERR_UNKNOWN){
614
634
      fprintf(stderr, "Unknown error while parsing arguments\n");
615
635
      exitstatus = EXIT_FAILURE;
616
636
      goto fallback;
619
639
  
620
640
  /* Parse actual command line arguments, to let them override the
621
641
     config file */
622
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
623
 
  if (ret == ARGP_ERR_UNKNOWN){
 
642
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
643
  if(ret == ARGP_ERR_UNKNOWN){
624
644
    fprintf(stderr, "Unknown error while parsing arguments\n");
625
645
    exitstatus = EXIT_FAILURE;
626
646
    goto fallback;
633
653
      for(char **a = p->argv; *a != NULL; a++){
634
654
        fprintf(stderr, "\tArg: %s\n", *a);
635
655
      }
636
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
656
      fprintf(stderr, "...and %d environment variables\n", p->envc);
637
657
      for(char **a = p->environ; *a != NULL; a++){
638
658
        fprintf(stderr, "\t%s\n", *a);
639
659
      }
642
662
  
643
663
  /* Strip permissions down to nobody */
644
664
  ret = setuid(uid);
645
 
  if (ret == -1){
 
665
  if(ret == -1){
646
666
    perror("setuid");
647
667
  }  
648
668
  setgid(gid);
649
 
  if (ret == -1){
 
669
  if(ret == -1){
650
670
    perror("setgid");
651
671
  }
652
672
  
653
 
  if (plugindir == NULL){
 
673
  if(plugindir == NULL){
654
674
    dir = opendir(PDIR);
655
675
  } else {
656
676
    dir = opendir(plugindir);
683
703
    
684
704
    /* All directory entries have been processed */
685
705
    if(dirst == NULL){
686
 
      if (errno == EBADF){
 
706
      if(errno == EBADF){
687
707
        perror("readdir");
688
708
        exitstatus = EXIT_FAILURE;
689
709
        goto fallback;
749
769
    }
750
770
    
751
771
    ret = stat(filename, &st);
752
 
    if (ret == -1){
 
772
    if(ret == -1){
753
773
      perror("stat");
754
774
      free(filename);
755
775
      continue;
756
776
    }
757
777
 
758
778
    /* Ignore non-executable files */
759
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
760
780
      if(debug){
761
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
762
782
                " with bad type or mode\n", filename);
809
829
    
810
830
    int pipefd[2];
811
831
    ret = pipe(pipefd);
812
 
    if (ret == -1){
 
832
    if(ret == -1){
813
833
      perror("pipe");
814
834
      exitstatus = EXIT_FAILURE;
815
835
      goto fallback;
828
848
      goto fallback;
829
849
    }
830
850
    /* Block SIGCHLD until process is safely in process list */
831
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
832
852
    if(ret < 0){
833
853
      perror("sigprocmask");
834
854
      exitstatus = EXIT_FAILURE;
882
902
    close(pipefd[1]);           /* Close unused write end of pipe */
883
903
    free(filename);
884
904
    plugin *new_plugin = getplugin(dirst->d_name);
885
 
    if (new_plugin == NULL){
 
905
    if(new_plugin == NULL){
886
906
      perror("getplugin");
887
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
888
908
      if(ret < 0){
889
909
        perror("sigprocmask");
890
910
      }
897
917
    
898
918
    /* Unblock SIGCHLD so signal handler can be run if this process
899
919
       has already completed */
900
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
901
921
    if(ret < 0){
902
922
      perror("sigprocmask");
903
923
      exitstatus = EXIT_FAILURE;
906
926
    
907
927
    FD_SET(new_plugin->fd, &rfds_all);
908
928
    
909
 
    if (maxfd < new_plugin->fd){
 
929
    if(maxfd < new_plugin->fd){
910
930
      maxfd = new_plugin->fd;
911
931
    }
912
932
  }
929
949
  while(plugin_list){
930
950
    fd_set rfds = rfds_all;
931
951
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
932
 
    if (select_ret == -1){
 
952
    if(select_ret == -1){
933
953
      perror("select");
934
954
      exitstatus = EXIT_FAILURE;
935
955
      goto fallback;
946
966
 
947
967
          if(debug){
948
968
            if(WIFEXITED(proc->status)){
949
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
950
 
                      (unsigned int) (proc->pid),
 
969
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
970
                      " %d\n", (intmax_t) (proc->pid),
951
971
                      WEXITSTATUS(proc->status));
952
972
            } else if(WIFSIGNALED(proc->status)) {
953
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
954
 
                      (unsigned int) (proc->pid),
 
973
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
974
                      " %d\n", (intmax_t) (proc->pid),
955
975
                      WTERMSIG(proc->status));
956
976
            } else if(WCOREDUMP(proc->status)){
957
 
              fprintf(stderr, "Plugin %d dumped core\n",
958
 
                      (unsigned int) (proc->pid));
 
977
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
978
                      (intmax_t) (proc->pid));
959
979
            }
960
980
          }
961
981
          
975
995
          proc = next_plugin;
976
996
          
977
997
          /* We are done modifying process list, so unblock signal */
978
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
 
                             NULL);
 
998
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
999
                            NULL);
980
1000
          if(ret < 0){
981
1001
            perror("sigprocmask");
982
1002
            exitstatus = EXIT_FAILURE;
1011
1031
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1012
1032
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1013
1033
                               + (size_t) BUFFER_SIZE);
1014
 
        if (proc->buffer == NULL){
 
1034
        if(proc->buffer == NULL){
1015
1035
          perror("malloc");
1016
1036
          exitstatus = EXIT_FAILURE;
1017
1037
          goto fallback;
1019
1039
        proc->buffer_size += BUFFER_SIZE;
1020
1040
      }
1021
1041
      /* Read from the process */
1022
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1023
 
                 BUFFER_SIZE);
1024
 
      if(ret < 0){
 
1042
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1043
                  BUFFER_SIZE);
 
1044
      if(sret < 0){
1025
1045
        /* Read error from this process; ignore the error */
1026
1046
        proc = proc->next;
1027
1047
        continue;
1028
1048
      }
1029
 
      if(ret == 0){
 
1049
      if(sret == 0){
1030
1050
        /* got EOF */
1031
1051
        proc->eof = true;
1032
1052
      } else {
1033
 
        proc->buffer_length += (size_t) ret;
 
1053
        proc->buffer_length += (size_t) sret;
1034
1054
      }
1035
1055
    }
1036
1056
  }