/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-04-17 01:16:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417011637-bcqjpm7fskm4370v
Code cleanup and one bug fix.

* mandos (Client.enable): Bug fix: Do not enable if already enabled.
  (MandosServer.__init__): Create empty set of clients if none passed.
  (main): Do not create clients set; do not pass a clients set to
          MandosServer.  Use tcp_server.clients throughout.

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
 *
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(),
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
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
44
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
45
                                   readdir(), closedir(), dirfd() */
47
46
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
47
                                   fcntl(), setuid(), setgid(),
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() */
 
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
698
670
    perror("setuid");
699
671
  }
700
672
  
701
 
  /* Open plugin directory with close_on_exec flag */
 
673
  if(plugindir == NULL){
 
674
    dir = opendir(PDIR);
 
675
  } else {
 
676
    dir = opendir(plugindir);
 
677
  }
 
678
  
 
679
  if(dir == NULL){
 
680
    perror("Could not open plugin dir");
 
681
    exitstatus = EXIT_FAILURE;
 
682
    goto fallback;
 
683
  }
 
684
  
 
685
  /* Set the FD_CLOEXEC flag on the directory, if possible */
702
686
  {
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;
 
687
    int dir_fd = dirfd(dir);
 
688
    if(dir_fd >= 0){
 
689
      ret = set_cloexec_flag(dir_fd);
 
690
      if(ret < 0){
 
691
        perror("set_cloexec_flag");
 
692
        exitstatus = EXIT_FAILURE;
 
693
        goto fallback;
 
694
      }
744
695
    }
745
696
  }
746
697
  
748
699
  
749
700
  /* Read and execute any executable in the plugin directory*/
750
701
  while(true){
751
 
    do {
752
 
      dirst = readdir(dir);
753
 
    } while(dirst == NULL and errno == EINTR);
 
702
    dirst = readdir(dir);
754
703
    
755
704
    /* All directory entries have been processed */
756
705
    if(dirst == NULL){
810
759
    
811
760
    char *filename;
812
761
    if(plugindir == NULL){
813
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
814
 
                                             dirst->d_name));
 
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
815
763
    } else {
816
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
817
 
                                             plugindir,
818
 
                                             dirst->d_name));
 
764
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
819
765
    }
820
766
    if(ret < 0){
821
767
      perror("asprintf");
822
768
      continue;
823
769
    }
824
770
    
825
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
771
    ret = stat(filename, &st);
826
772
    if(ret == -1){
827
773
      perror("stat");
828
774
      free(filename);
830
776
    }
831
777
    
832
778
    /* Ignore non-executable files */
833
 
    if(not S_ISREG(st.st_mode)
834
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
835
780
      if(debug){
836
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
837
782
                " with bad type or mode\n", filename);
883
828
    }
884
829
    
885
830
    int pipefd[2];
886
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
831
    ret = pipe(pipefd);
887
832
    if(ret == -1){
888
833
      perror("pipe");
889
834
      exitstatus = EXIT_FAILURE;
903
848
      goto fallback;
904
849
    }
905
850
    /* 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));
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
909
852
    if(ret < 0){
910
853
      perror("sigprocmask");
911
854
      exitstatus = EXIT_FAILURE;
912
855
      goto fallback;
913
856
    }
914
857
    /* Starting a new process to be watched */
915
 
    pid_t pid;
916
 
    do {
917
 
      pid = fork();
918
 
    } while(pid == -1 and errno == EINTR);
 
858
    pid_t pid = fork();
919
859
    if(pid == -1){
920
860
      perror("fork");
921
861
      exitstatus = EXIT_FAILURE;
959
899
      /* no return */
960
900
    }
961
901
    /* Parent process */
962
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
963
 
                                             pipe */
 
902
    close(pipefd[1]);           /* Close unused write end of pipe */
964
903
    free(filename);
965
904
    plugin *new_plugin = getplugin(dirst->d_name);
966
905
    if(new_plugin == NULL){
967
906
      perror("getplugin");
968
 
      ret = (int)(TEMP_FAILURE_RETRY
969
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
970
 
                               NULL)));
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
971
908
      if(ret < 0){
972
909
        perror("sigprocmask");
973
910
      }
980
917
    
981
918
    /* Unblock SIGCHLD so signal handler can be run if this process
982
919
       has already completed */
983
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
984
 
                                              &sigchld_action.sa_mask,
985
 
                                              NULL));
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
986
921
    if(ret < 0){
987
922
      perror("sigprocmask");
988
923
      exitstatus = EXIT_FAILURE;
989
924
      goto fallback;
990
925
    }
991
926
    
992
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
993
 
                                          -Wconversion */
 
927
    FD_SET(new_plugin->fd, &rfds_all);
994
928
    
995
929
    if(maxfd < new_plugin->fd){
996
930
      maxfd = new_plugin->fd;
997
931
    }
998
932
  }
999
933
  
1000
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
934
  closedir(dir);
1001
935
  dir = NULL;
1002
936
  free_plugin(getplugin(NULL));
1003
937
  
1016
950
  while(plugin_list){
1017
951
    fd_set rfds = rfds_all;
1018
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1019
 
    if(select_ret == -1 and errno != EINTR){
 
953
    if(select_ret == -1){
1020
954
      perror("select");
1021
955
      exitstatus = EXIT_FAILURE;
1022
956
      goto fallback;
1039
973
                      WEXITSTATUS(proc->status));
1040
974
            } else if(WIFSIGNALED(proc->status)){
1041
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1042
 
                      " signal %d: %s\n", proc->name,
 
976
                      " signal %d\n", proc->name,
1043
977
                      (intmax_t) (proc->pid),
1044
 
                      WTERMSIG(proc->status),
1045
 
                      strsignal(WTERMSIG(proc->status)));
 
978
                      WTERMSIG(proc->status));
1046
979
            } else if(WCOREDUMP(proc->status)){
1047
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1048
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
1050
983
          }
1051
984
          
1052
985
          /* Remove the plugin */
1053
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1054
 
                                          -Wconversion */
 
986
          FD_CLR(proc->fd, &rfds_all);
1055
987
          
1056
988
          /* Block signal while modifying process_list */
1057
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1058
 
                                        (SIG_BLOCK,
1059
 
                                         &sigchld_action.sa_mask,
1060
 
                                         NULL));
 
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1061
990
          if(ret < 0){
1062
991
            perror("sigprocmask");
1063
992
            exitstatus = EXIT_FAILURE;
1069
998
          proc = next_plugin;
1070
999
          
1071
1000
          /* 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)));
 
1001
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1002
                            NULL);
1075
1003
          if(ret < 0){
1076
1004
            perror("sigprocmask");
1077
1005
            exitstatus = EXIT_FAILURE;
1097
1025
      }
1098
1026
      
1099
1027
      /* 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 */
 
1028
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1103
1029
        /* This process had nothing to say at this time */
1104
1030
        proc = proc->next;
1105
1031
        continue;
1116
1042
        proc->buffer_size += BUFFER_SIZE;
1117
1043
      }
1118
1044
      /* Read from the process */
1119
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1120
 
                                     proc->buffer
1121
 
                                     + proc->buffer_length,
1122
 
                                     BUFFER_SIZE));
 
1045
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1046
                  BUFFER_SIZE);
1123
1047
      if(sret < 0){
1124
1048
        /* Read error from this process; ignore the error */
1125
1049
        proc = proc->next;
1187
1111
  }
1188
1112
  
1189
1113
  /* Wait for any remaining child processes to terminate */
1190
 
  do {
 
1114
  do{
1191
1115
    ret = wait(NULL);
1192
1116
  } while(ret >= 0);
1193
1117
  if(errno != ECHILD){