/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-04 16:32:22 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090904163222-l9wp25ng1e5ym0yq
* plugin-runner.c (main): When a plugin is killed by a signal, show
                          the signal name, not just the number, in the
                          debug log message.

* plugins.d/password-prompt.c (termination_handler): Store received
                                                     signal in
                                                     "signal_received".
  (main): If exiting due to signal, re-raise signal received instead
          of returning.

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