/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-06 22:54:01 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090106225401-7l9f7s3hw9eissra
* mandos (main): Try to find non-privileged user+group in pairs, so
                 that mismatched pairs are never used.

Show diffs side-by-side

added added

removed removed

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