/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-09-08 04:41:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908044137-4cxubotvn4etoxxl
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
                                    Bug fix: If taking down network
                                    interface, do not drop privileges
                                    completely; save them and reassert
                                    privileges when needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
29
                                   EXIT_SUCCESS, realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
42
42
                                   WCOREDUMP() */
43
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
48
                                   fcntl(), setuid(), setgid(),
230
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
231
231
 */
232
232
static int set_cloexec_flag(int fd){
233
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
233
  int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
234
234
  /* If reading the flags failed, return error indication now. */
235
235
  if(ret < 0){
236
236
    return ret;
237
237
  }
238
238
  /* Store modified flag word in the descriptor. */
239
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
240
 
                                       ret | FD_CLOEXEC));
 
239
  return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
241
240
}
242
241
 
243
242
 
698
697
    perror("setuid");
699
698
  }
700
699
  
701
 
  /* Open plugin directory with close_on_exec flag */
 
700
  if(plugindir == NULL){
 
701
    dir = opendir(PDIR);
 
702
  } else {
 
703
    dir = opendir(plugindir);
 
704
  }
 
705
  
 
706
  if(dir == NULL){
 
707
    perror("Could not open plugin dir");
 
708
    exitstatus = EXIT_FAILURE;
 
709
    goto fallback;
 
710
  }
 
711
  
 
712
  /* Set the FD_CLOEXEC flag on the directory, if possible */
702
713
  {
703
 
    int dir_fd = -1;
704
 
    if(plugindir == NULL){
705
 
      dir_fd = open(PDIR, O_RDONLY |
706
 
#ifdef O_CLOEXEC
707
 
                    O_CLOEXEC
708
 
#else  /* not O_CLOEXEC */
709
 
                    0
710
 
#endif  /* not O_CLOEXEC */
711
 
                    );
712
 
    } else {
713
 
      dir_fd = open(plugindir, O_RDONLY |
714
 
#ifdef O_CLOEXEC
715
 
                    O_CLOEXEC
716
 
#else  /* not O_CLOEXEC */
717
 
                    0
718
 
#endif  /* not O_CLOEXEC */
719
 
                    );
720
 
    }
721
 
    if(dir_fd == -1){
722
 
      perror("Could not open plugin dir");
723
 
      exitstatus = EXIT_FAILURE;
724
 
      goto fallback;
725
 
    }
726
 
    
727
 
#ifndef O_CLOEXEC
728
 
  /* Set the FD_CLOEXEC flag on the directory */
729
 
    ret = set_cloexec_flag(dir_fd);
730
 
    if(ret < 0){
731
 
      perror("set_cloexec_flag");
732
 
      TEMP_FAILURE_RETRY(close(dir_fd));
733
 
      exitstatus = EXIT_FAILURE;
734
 
      goto fallback;
735
 
    }
736
 
#endif  /* O_CLOEXEC */
737
 
    
738
 
    dir = fdopendir(dir_fd);
739
 
    if(dir == NULL){
740
 
      perror("Could not open plugin dir");
741
 
      TEMP_FAILURE_RETRY(close(dir_fd));
742
 
      exitstatus = EXIT_FAILURE;
743
 
      goto fallback;
 
714
    int dir_fd = dirfd(dir);
 
715
    if(dir_fd >= 0){
 
716
      ret = set_cloexec_flag(dir_fd);
 
717
      if(ret < 0){
 
718
        perror("set_cloexec_flag");
 
719
        exitstatus = EXIT_FAILURE;
 
720
        goto fallback;
 
721
      }
744
722
    }
745
723
  }
746
724
  
810
788
    
811
789
    char *filename;
812
790
    if(plugindir == NULL){
813
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
814
 
                                             dirst->d_name));
 
791
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
792
                                        dirst->d_name));
815
793
    } else {
816
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
817
 
                                             plugindir,
818
 
                                             dirst->d_name));
 
794
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
 
795
                                        dirst->d_name));
819
796
    }
820
797
    if(ret < 0){
821
798
      perror("asprintf");
822
799
      continue;
823
800
    }
824
801
    
825
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
802
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
826
803
    if(ret == -1){
827
804
      perror("stat");
828
805
      free(filename);
883
860
    }
884
861
    
885
862
    int pipefd[2];
886
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
863
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
887
864
    if(ret == -1){
888
865
      perror("pipe");
889
866
      exitstatus = EXIT_FAILURE;
903
880
      goto fallback;
904
881
    }
905
882
    /* Block SIGCHLD until process is safely in process list */
906
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
907
 
                                              &sigchld_action.sa_mask,
908
 
                                              NULL));
 
883
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
884
                                         &sigchld_action.sa_mask,
 
885
                                         NULL));
909
886
    if(ret < 0){
910
887
      perror("sigprocmask");
911
888
      exitstatus = EXIT_FAILURE;
965
942
    plugin *new_plugin = getplugin(dirst->d_name);
966
943
    if(new_plugin == NULL){
967
944
      perror("getplugin");
968
 
      ret = (int)(TEMP_FAILURE_RETRY
969
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
970
 
                               NULL)));
 
945
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
946
                                           &sigchld_action.sa_mask,
 
947
                                           NULL));
971
948
      if(ret < 0){
972
949
        perror("sigprocmask");
973
950
      }
980
957
    
981
958
    /* Unblock SIGCHLD so signal handler can be run if this process
982
959
       has already completed */
983
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
984
 
                                              &sigchld_action.sa_mask,
985
 
                                              NULL));
 
960
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
961
                                         &sigchld_action.sa_mask,
 
962
                                         NULL));
986
963
    if(ret < 0){
987
964
      perror("sigprocmask");
988
965
      exitstatus = EXIT_FAILURE;
989
966
      goto fallback;
990
967
    }
991
968
    
992
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
993
 
                                          -Wconversion */
 
969
    FD_SET(new_plugin->fd, &rfds_all);
994
970
    
995
971
    if(maxfd < new_plugin->fd){
996
972
      maxfd = new_plugin->fd;
1050
1026
          }
1051
1027
          
1052
1028
          /* Remove the plugin */
1053
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1054
 
                                          -Wconversion */
 
1029
          FD_CLR(proc->fd, &rfds_all);
1055
1030
          
1056
1031
          /* Block signal while modifying process_list */
1057
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1058
 
                                        (SIG_BLOCK,
1059
 
                                         &sigchld_action.sa_mask,
1060
 
                                         NULL));
 
1032
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
1033
                                               &sigchld_action.sa_mask,
 
1034
                                               NULL));
1061
1035
          if(ret < 0){
1062
1036
            perror("sigprocmask");
1063
1037
            exitstatus = EXIT_FAILURE;
1069
1043
          proc = next_plugin;
1070
1044
          
1071
1045
          /* We are done modifying process list, so unblock signal */
1072
 
          ret = (int)(TEMP_FAILURE_RETRY
1073
 
                      (sigprocmask(SIG_UNBLOCK,
1074
 
                                   &sigchld_action.sa_mask, NULL)));
 
1046
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1047
                                               &sigchld_action.sa_mask,
 
1048
                                               NULL));
1075
1049
          if(ret < 0){
1076
1050
            perror("sigprocmask");
1077
1051
            exitstatus = EXIT_FAILURE;
1097
1071
      }
1098
1072
      
1099
1073
      /* This process has not completed.  Does it have any output? */
1100
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1101
 
                                                         warning from
1102
 
                                                         -Wconversion */
 
1074
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1103
1075
        /* This process had nothing to say at this time */
1104
1076
        proc = proc->next;
1105
1077
        continue;