/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-02-25 01:14:29 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090225011429-dwz2bj41zqn0w8eo
* initramfs-tools-hook: Add missing terminating `" when setting
                        mandos_user and mandos_group.

* mandos.conf.xml (SEE ALSO): Improved grammar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
 
41
                                   WEXITSTATUS() */
43
42
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
44
#include <dirent.h>             /* DIR, struct dirent, opendir(),
53
52
                                   close() */
54
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
54
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
55
#include <string.h>             /* strsep, strlen(), asprintf() */
58
56
#include <errno.h>              /* errno */
59
57
#include <argp.h>               /* struct argp_option, struct
60
58
                                   argp_state, struct argp,
110
108
    }
111
109
  }
112
110
  /* Create a new plugin */
113
 
  plugin *new_plugin = NULL;
114
 
  do {
115
 
    new_plugin = malloc(sizeof(plugin));
116
 
  } while(new_plugin == NULL and errno == EINTR);
 
111
  plugin *new_plugin = malloc(sizeof(plugin));
117
112
  if(new_plugin == NULL){
118
113
    return NULL;
119
114
  }
120
115
  char *copy_name = NULL;
121
116
  if(name != NULL){
122
 
    do {
123
 
      copy_name = strdup(name);
124
 
    } while(copy_name == NULL and errno == EINTR);
 
117
    copy_name = strdup(name);
125
118
    if(copy_name == NULL){
126
119
      free(new_plugin);
127
120
      return NULL;
133
126
                          .disabled = false,
134
127
                          .next = plugin_list };
135
128
  
136
 
  do {
137
 
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
 
  } while(new_plugin->argv == NULL and errno == EINTR);
 
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
139
130
  if(new_plugin->argv == NULL){
140
131
    free(copy_name);
141
132
    free(new_plugin);
144
135
  new_plugin->argv[0] = copy_name;
145
136
  new_plugin->argv[1] = NULL;
146
137
  
147
 
  do {
148
 
    new_plugin->environ = malloc(sizeof(char *));
149
 
  } while(new_plugin->environ == NULL and errno == EINTR);
 
138
  new_plugin->environ = malloc(sizeof(char *));
150
139
  if(new_plugin->environ == NULL){
151
140
    free(copy_name);
152
141
    free(new_plugin->argv);
164
153
static bool add_to_char_array(const char *new, char ***array,
165
154
                              int *len){
166
155
  /* Resize the pointed-to array to hold one more pointer */
167
 
  do {
168
 
    *array = realloc(*array, sizeof(char *)
169
 
                     * (size_t) ((*len) + 2));
170
 
  } while(*array == NULL and errno == EINTR);
 
156
  *array = realloc(*array, sizeof(char *)
 
157
                   * (size_t) ((*len) + 2));
171
158
  /* Malloc check */
172
159
  if(*array == NULL){
173
160
    return false;
174
161
  }
175
162
  /* Make a copy of the new string */
176
 
  char *copy;
177
 
  do {
178
 
    copy = strdup(new);
179
 
  } while(copy == NULL and errno == EINTR);
 
163
  char *copy = strdup(new);
180
164
  if(copy == NULL){
181
165
    return false;
182
166
  }
208
192
    if(strncmp(*e, def, namelen + 1) == 0){
209
193
      /* It already exists */
210
194
      if(replace){
211
 
        char *new;
212
 
        do {
213
 
          new = realloc(*e, strlen(def) + 1);
214
 
        } while(new == NULL and errno == EINTR);
 
195
        char *new = realloc(*e, strlen(def) + 1);
215
196
        if(new == NULL){
216
197
          return false;
217
198
        }
227
208
/*
228
209
 * Based on the example in the GNU LibC manual chapter 13.13 "File
229
210
 * Descriptor Flags".
230
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
211
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
231
212
 */
232
213
static int set_cloexec_flag(int fd){
233
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
214
  int ret = fcntl(fd, F_GETFD, 0);
234
215
  /* If reading the flags failed, return error indication now. */
235
216
  if(ret < 0){
236
217
    return ret;
237
218
  }
238
219
  /* Store modified flag word in the descriptor. */
239
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
240
 
                                       ret | FD_CLOEXEC));
 
220
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
241
221
}
242
222
 
243
223
 
335
315
  fd_set rfds_all;
336
316
  int ret, maxfd = 0;
337
317
  ssize_t sret;
 
318
  intmax_t tmpmax;
338
319
  uid_t uid = 65534;
339
320
  gid_t gid = 65534;
340
321
  bool debug = false;
399
380
  
400
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
401
382
                    struct argp_state *state){
 
383
    char *tmp;
402
384
    switch(key){
403
 
      char *tmp;
404
 
      intmax_t tmpmax;
405
385
    case 'g':                   /* --global-options */
406
386
      if(arg != NULL){
407
387
        char *plugin_option;
477
457
      plugindir = strdup(arg);
478
458
      if(plugindir == NULL){
479
459
        perror("strdup");
480
 
      }
 
460
      }      
481
461
      break;
482
462
    case 129:                   /* --config-file */
483
463
      /* This is already done by parse_opt_config_file() */
547
527
      if(argfile == NULL){
548
528
        perror("strdup");
549
529
      }
550
 
      break;
 
530
      break;      
551
531
    case 130:                   /* --userid */
552
532
    case 131:                   /* --groupid */
553
533
    case 132:                   /* --debug */
582
562
    conffp = fopen(AFILE, "r");
583
563
  } else {
584
564
    conffp = fopen(argfile, "r");
585
 
  }
 
565
  }  
586
566
  if(conffp != NULL){
587
567
    char *org_line = NULL;
588
568
    char *p, *arg, *new_arg, *line;
632
612
          goto fallback;
633
613
        }
634
614
        custom_argv[custom_argc-1] = new_arg;
635
 
        custom_argv[custom_argc] = NULL;
 
615
        custom_argv[custom_argc] = NULL;        
636
616
      }
637
617
    }
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
 
    }
646
618
    free(org_line);
647
619
  } else {
648
620
    /* Check for harmful errors and go to fallback. Other errors might
727
699
  
728
700
  /* Read and execute any executable in the plugin directory*/
729
701
  while(true){
730
 
    do {
731
 
      dirst = readdir(dir);
732
 
    } while(dirst == NULL and errno == EINTR);
 
702
    dirst = readdir(dir);
733
703
    
734
704
    /* All directory entries have been processed */
735
705
    if(dirst == NULL){
771
741
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
772
742
        size_t suf_len = strlen(*suf);
773
743
        if((d_name_len >= suf_len)
774
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
744
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
775
745
                == 0)){
776
746
          if(debug){
777
747
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
789
759
    
790
760
    char *filename;
791
761
    if(plugindir == NULL){
792
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
793
 
                                             dirst->d_name));
 
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
794
763
    } else {
795
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
796
 
                                             plugindir,
797
 
                                             dirst->d_name));
 
764
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
798
765
    }
799
766
    if(ret < 0){
800
767
      perror("asprintf");
801
768
      continue;
802
769
    }
803
770
    
804
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
771
    ret = stat(filename, &st);
805
772
    if(ret == -1){
806
773
      perror("stat");
807
774
      free(filename);
809
776
    }
810
777
    
811
778
    /* Ignore non-executable files */
812
 
    if(not S_ISREG(st.st_mode)
813
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
814
780
      if(debug){
815
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
816
782
                " with bad type or mode\n", filename);
862
828
    }
863
829
    
864
830
    int pipefd[2];
865
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
831
    ret = pipe(pipefd);
866
832
    if(ret == -1){
867
833
      perror("pipe");
868
834
      exitstatus = EXIT_FAILURE;
882
848
      goto fallback;
883
849
    }
884
850
    /* Block SIGCHLD until process is safely in process list */
885
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
886
 
                                              &sigchld_action.sa_mask,
887
 
                                              NULL));
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
888
852
    if(ret < 0){
889
853
      perror("sigprocmask");
890
854
      exitstatus = EXIT_FAILURE;
891
855
      goto fallback;
892
856
    }
893
857
    /* Starting a new process to be watched */
894
 
    pid_t pid;
895
 
    do {
896
 
      pid = fork();
897
 
    } while(pid == -1 and errno == EINTR);
 
858
    pid_t pid = fork();
898
859
    if(pid == -1){
899
860
      perror("fork");
900
861
      exitstatus = EXIT_FAILURE;
938
899
      /* no return */
939
900
    }
940
901
    /* Parent process */
941
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
942
 
                                             pipe */
 
902
    close(pipefd[1]);           /* Close unused write end of pipe */
943
903
    free(filename);
944
904
    plugin *new_plugin = getplugin(dirst->d_name);
945
905
    if(new_plugin == NULL){
946
906
      perror("getplugin");
947
 
      ret = (int)(TEMP_FAILURE_RETRY
948
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
949
 
                               NULL)));
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
950
908
      if(ret < 0){
951
909
        perror("sigprocmask");
952
910
      }
959
917
    
960
918
    /* Unblock SIGCHLD so signal handler can be run if this process
961
919
       has already completed */
962
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
963
 
                                              &sigchld_action.sa_mask,
964
 
                                              NULL));
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
965
921
    if(ret < 0){
966
922
      perror("sigprocmask");
967
923
      exitstatus = EXIT_FAILURE;
968
924
      goto fallback;
969
925
    }
970
926
    
971
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
972
 
                                          -Wconversion */
 
927
    FD_SET(new_plugin->fd, &rfds_all);
973
928
    
974
929
    if(maxfd < new_plugin->fd){
975
930
      maxfd = new_plugin->fd;
976
931
    }
977
932
  }
978
933
  
979
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
934
  closedir(dir);
980
935
  dir = NULL;
981
936
  free_plugin(getplugin(NULL));
982
937
  
995
950
  while(plugin_list){
996
951
    fd_set rfds = rfds_all;
997
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
998
 
    if(select_ret == -1 and errno != EINTR){
 
953
    if(select_ret == -1){
999
954
      perror("select");
1000
955
      exitstatus = EXIT_FAILURE;
1001
956
      goto fallback;
1018
973
                      WEXITSTATUS(proc->status));
1019
974
            } else if(WIFSIGNALED(proc->status)){
1020
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1021
 
                      " signal %d: %s\n", proc->name,
 
976
                      " signal %d\n", proc->name,
1022
977
                      (intmax_t) (proc->pid),
1023
 
                      WTERMSIG(proc->status),
1024
 
                      strsignal(WTERMSIG(proc->status)));
 
978
                      WTERMSIG(proc->status));
1025
979
            } else if(WCOREDUMP(proc->status)){
1026
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1027
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
1029
983
          }
1030
984
          
1031
985
          /* Remove the plugin */
1032
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1033
 
                                          -Wconversion */
 
986
          FD_CLR(proc->fd, &rfds_all);
1034
987
          
1035
988
          /* Block signal while modifying process_list */
1036
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1037
 
                                        (SIG_BLOCK,
1038
 
                                         &sigchld_action.sa_mask,
1039
 
                                         NULL));
 
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1040
990
          if(ret < 0){
1041
991
            perror("sigprocmask");
1042
992
            exitstatus = EXIT_FAILURE;
1048
998
          proc = next_plugin;
1049
999
          
1050
1000
          /* We are done modifying process list, so unblock signal */
1051
 
          ret = (int)(TEMP_FAILURE_RETRY
1052
 
                      (sigprocmask(SIG_UNBLOCK,
1053
 
                                   &sigchld_action.sa_mask, NULL)));
 
1001
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1002
                            NULL);
1054
1003
          if(ret < 0){
1055
1004
            perror("sigprocmask");
1056
1005
            exitstatus = EXIT_FAILURE;
1076
1025
      }
1077
1026
      
1078
1027
      /* This process has not completed.  Does it have any output? */
1079
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1080
 
                                                         warning from
1081
 
                                                         -Wconversion */
 
1028
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1082
1029
        /* This process had nothing to say at this time */
1083
1030
        proc = proc->next;
1084
1031
        continue;
1095
1042
        proc->buffer_size += BUFFER_SIZE;
1096
1043
      }
1097
1044
      /* Read from the process */
1098
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1099
 
                                     proc->buffer
1100
 
                                     + proc->buffer_length,
1101
 
                                     BUFFER_SIZE));
 
1045
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1046
                  BUFFER_SIZE);
1102
1047
      if(sret < 0){
1103
1048
        /* Read error from this process; ignore the error */
1104
1049
        proc = proc->next;
1166
1111
  }
1167
1112
  
1168
1113
  /* Wait for any remaining child processes to terminate */
1169
 
  do {
 
1114
  do{
1170
1115
    ret = wait(NULL);
1171
1116
  } while(ret >= 0);
1172
1117
  if(errno != ECHILD){