/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-23 20:31:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090923203147-mbs7klc1tugrrdwf
* mandos (Client.__init__): Open "secfile" file in binary mode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf() */
 
26
                                   asprintf(), O_CLOEXEC */
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, opendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, fdopendir(), 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, opendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
48
                                   fcntl(), setuid(), setgid(),
110
110
    }
111
111
  }
112
112
  /* Create a new plugin */
113
 
  plugin *new_plugin = malloc(sizeof(plugin));
 
113
  plugin *new_plugin = NULL;
 
114
  do {
 
115
    new_plugin = malloc(sizeof(plugin));
 
116
  } while(new_plugin == NULL and errno == EINTR);
114
117
  if(new_plugin == NULL){
115
118
    return NULL;
116
119
  }
117
120
  char *copy_name = NULL;
118
121
  if(name != NULL){
119
 
    copy_name = strdup(name);
 
122
    do {
 
123
      copy_name = strdup(name);
 
124
    } while(copy_name == NULL and errno == EINTR);
120
125
    if(copy_name == NULL){
121
126
      free(new_plugin);
122
127
      return NULL;
128
133
                          .disabled = false,
129
134
                          .next = plugin_list };
130
135
  
131
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
 
136
  do {
 
137
    new_plugin->argv = malloc(sizeof(char *) * 2);
 
138
  } while(new_plugin->argv == NULL and errno == EINTR);
132
139
  if(new_plugin->argv == NULL){
133
140
    free(copy_name);
134
141
    free(new_plugin);
137
144
  new_plugin->argv[0] = copy_name;
138
145
  new_plugin->argv[1] = NULL;
139
146
  
140
 
  new_plugin->environ = malloc(sizeof(char *));
 
147
  do {
 
148
    new_plugin->environ = malloc(sizeof(char *));
 
149
  } while(new_plugin->environ == NULL and errno == EINTR);
141
150
  if(new_plugin->environ == NULL){
142
151
    free(copy_name);
143
152
    free(new_plugin->argv);
155
164
static bool add_to_char_array(const char *new, char ***array,
156
165
                              int *len){
157
166
  /* Resize the pointed-to array to hold one more pointer */
158
 
  *array = realloc(*array, sizeof(char *)
159
 
                   * (size_t) ((*len) + 2));
 
167
  do {
 
168
    *array = realloc(*array, sizeof(char *)
 
169
                     * (size_t) ((*len) + 2));
 
170
  } while(*array == NULL and errno == EINTR);
160
171
  /* Malloc check */
161
172
  if(*array == NULL){
162
173
    return false;
163
174
  }
164
175
  /* Make a copy of the new string */
165
 
  char *copy = strdup(new);
 
176
  char *copy;
 
177
  do {
 
178
    copy = strdup(new);
 
179
  } while(copy == NULL and errno == EINTR);
166
180
  if(copy == NULL){
167
181
    return false;
168
182
  }
194
208
    if(strncmp(*e, def, namelen + 1) == 0){
195
209
      /* It already exists */
196
210
      if(replace){
197
 
        char *new = realloc(*e, strlen(def) + 1);
 
211
        char *new;
 
212
        do {
 
213
          new = realloc(*e, strlen(def) + 1);
 
214
        } while(new == NULL and errno == EINTR);
198
215
        if(new == NULL){
199
216
          return false;
200
217
        }
210
227
/*
211
228
 * Based on the example in the GNU LibC manual chapter 13.13 "File
212
229
 * Descriptor Flags".
213
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
214
231
 */
215
232
static int set_cloexec_flag(int fd){
216
 
  int ret = fcntl(fd, F_GETFD, 0);
 
233
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
217
234
  /* If reading the flags failed, return error indication now. */
218
235
  if(ret < 0){
219
236
    return ret;
220
237
  }
221
238
  /* Store modified flag word in the descriptor. */
222
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
239
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
240
                                       ret | FD_CLOEXEC));
223
241
}
224
242
 
225
243
 
317
335
  fd_set rfds_all;
318
336
  int ret, maxfd = 0;
319
337
  ssize_t sret;
320
 
  intmax_t tmpmax;
321
338
  uid_t uid = 65534;
322
339
  gid_t gid = 65534;
323
340
  bool debug = false;
382
399
  
383
400
  error_t parse_opt(int key, char *arg, __attribute__((unused))
384
401
                    struct argp_state *state){
385
 
    char *tmp;
386
402
    switch(key){
 
403
      char *tmp;
 
404
      intmax_t tmpmax;
387
405
    case 'g':                   /* --global-options */
388
406
      if(arg != NULL){
389
407
        char *plugin_option;
617
635
        custom_argv[custom_argc] = NULL;
618
636
      }
619
637
    }
 
638
    do {
 
639
      ret = fclose(conffp);
 
640
    } while(ret == EOF and errno == EINTR);
 
641
    if(ret == EOF){
 
642
      perror("fclose");
 
643
      exitstatus = EXIT_FAILURE;
 
644
      goto fallback;
 
645
    }
620
646
    free(org_line);
621
647
  } else {
622
648
    /* Check for harmful errors and go to fallback. Other errors might
672
698
    perror("setuid");
673
699
  }
674
700
  
675
 
  if(plugindir == NULL){
676
 
    dir = opendir(PDIR);
677
 
  } else {
678
 
    dir = opendir(plugindir);
679
 
  }
680
 
  
681
 
  if(dir == NULL){
682
 
    perror("Could not open plugin dir");
683
 
    exitstatus = EXIT_FAILURE;
684
 
    goto fallback;
685
 
  }
686
 
  
687
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
701
  /* Open plugin directory with close_on_exec flag */
688
702
  {
689
 
    int dir_fd = dirfd(dir);
690
 
    if(dir_fd >= 0){
691
 
      ret = set_cloexec_flag(dir_fd);
692
 
      if(ret < 0){
693
 
        perror("set_cloexec_flag");
694
 
        exitstatus = EXIT_FAILURE;
695
 
        goto fallback;
696
 
      }
 
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;
697
744
    }
698
745
  }
699
746
  
701
748
  
702
749
  /* Read and execute any executable in the plugin directory*/
703
750
  while(true){
704
 
    dirst = readdir(dir);
 
751
    do {
 
752
      dirst = readdir(dir);
 
753
    } while(dirst == NULL and errno == EINTR);
705
754
    
706
755
    /* All directory entries have been processed */
707
756
    if(dirst == NULL){
761
810
    
762
811
    char *filename;
763
812
    if(plugindir == NULL){
764
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
813
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
814
                                             dirst->d_name));
765
815
    } else {
766
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
816
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
817
                                             plugindir,
 
818
                                             dirst->d_name));
767
819
    }
768
820
    if(ret < 0){
769
821
      perror("asprintf");
770
822
      continue;
771
823
    }
772
824
    
773
 
    ret = stat(filename, &st);
 
825
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
774
826
    if(ret == -1){
775
827
      perror("stat");
776
828
      free(filename);
778
830
    }
779
831
    
780
832
    /* Ignore non-executable files */
781
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
833
    if(not S_ISREG(st.st_mode)
 
834
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
782
835
      if(debug){
783
836
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
784
837
                " with bad type or mode\n", filename);
830
883
    }
831
884
    
832
885
    int pipefd[2];
833
 
    ret = pipe(pipefd);
 
886
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
834
887
    if(ret == -1){
835
888
      perror("pipe");
836
889
      exitstatus = EXIT_FAILURE;
850
903
      goto fallback;
851
904
    }
852
905
    /* Block SIGCHLD until process is safely in process list */
853
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
906
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
907
                                              &sigchld_action.sa_mask,
 
908
                                              NULL));
854
909
    if(ret < 0){
855
910
      perror("sigprocmask");
856
911
      exitstatus = EXIT_FAILURE;
857
912
      goto fallback;
858
913
    }
859
914
    /* Starting a new process to be watched */
860
 
    pid_t pid = fork();
 
915
    pid_t pid;
 
916
    do {
 
917
      pid = fork();
 
918
    } while(pid == -1 and errno == EINTR);
861
919
    if(pid == -1){
862
920
      perror("fork");
863
921
      exitstatus = EXIT_FAILURE;
901
959
      /* no return */
902
960
    }
903
961
    /* Parent process */
904
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
962
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
963
                                             pipe */
905
964
    free(filename);
906
965
    plugin *new_plugin = getplugin(dirst->d_name);
907
966
    if(new_plugin == NULL){
908
967
      perror("getplugin");
909
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
968
      ret = (int)(TEMP_FAILURE_RETRY
 
969
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
970
                               NULL)));
910
971
      if(ret < 0){
911
972
        perror("sigprocmask");
912
973
      }
919
980
    
920
981
    /* Unblock SIGCHLD so signal handler can be run if this process
921
982
       has already completed */
922
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
983
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
984
                                              &sigchld_action.sa_mask,
 
985
                                              NULL));
923
986
    if(ret < 0){
924
987
      perror("sigprocmask");
925
988
      exitstatus = EXIT_FAILURE;
926
989
      goto fallback;
927
990
    }
928
991
    
929
 
    FD_SET(new_plugin->fd, &rfds_all);
 
992
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
993
                                          -Wconversion */
930
994
    
931
995
    if(maxfd < new_plugin->fd){
932
996
      maxfd = new_plugin->fd;
933
997
    }
934
998
  }
935
999
  
936
 
  closedir(dir);
 
1000
  TEMP_FAILURE_RETRY(closedir(dir));
937
1001
  dir = NULL;
938
1002
  free_plugin(getplugin(NULL));
939
1003
  
952
1016
  while(plugin_list){
953
1017
    fd_set rfds = rfds_all;
954
1018
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
955
 
    if(select_ret == -1){
 
1019
    if(select_ret == -1 and errno != EINTR){
956
1020
      perror("select");
957
1021
      exitstatus = EXIT_FAILURE;
958
1022
      goto fallback;
986
1050
          }
987
1051
          
988
1052
          /* Remove the plugin */
989
 
          FD_CLR(proc->fd, &rfds_all);
 
1053
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1054
                                          -Wconversion */
990
1055
          
991
1056
          /* Block signal while modifying process_list */
992
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1057
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1058
                                        (SIG_BLOCK,
 
1059
                                         &sigchld_action.sa_mask,
 
1060
                                         NULL));
993
1061
          if(ret < 0){
994
1062
            perror("sigprocmask");
995
1063
            exitstatus = EXIT_FAILURE;
1001
1069
          proc = next_plugin;
1002
1070
          
1003
1071
          /* We are done modifying process list, so unblock signal */
1004
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1005
 
                            NULL);
 
1072
          ret = (int)(TEMP_FAILURE_RETRY
 
1073
                      (sigprocmask(SIG_UNBLOCK,
 
1074
                                   &sigchld_action.sa_mask, NULL)));
1006
1075
          if(ret < 0){
1007
1076
            perror("sigprocmask");
1008
1077
            exitstatus = EXIT_FAILURE;
1028
1097
      }
1029
1098
      
1030
1099
      /* This process has not completed.  Does it have any output? */
1031
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1100
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1101
                                                         warning from
 
1102
                                                         -Wconversion */
1032
1103
        /* This process had nothing to say at this time */
1033
1104
        proc = proc->next;
1034
1105
        continue;
1045
1116
        proc->buffer_size += BUFFER_SIZE;
1046
1117
      }
1047
1118
      /* Read from the process */
1048
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1049
 
                  BUFFER_SIZE);
 
1119
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1120
                                     proc->buffer
 
1121
                                     + proc->buffer_length,
 
1122
                                     BUFFER_SIZE));
1050
1123
      if(sret < 0){
1051
1124
        /* Read error from this process; ignore the error */
1052
1125
        proc = proc->next;
1114
1187
  }
1115
1188
  
1116
1189
  /* Wait for any remaining child processes to terminate */
1117
 
  do{
 
1190
  do {
1118
1191
    ret = wait(NULL);
1119
1192
  } while(ret >= 0);
1120
1193
  if(errno != ECHILD){