/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

merge

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
 *
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() */
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
42
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h>             /* and, or, not */
44
45
#include <dirent.h>             /* DIR, struct dirent, opendir(),
52
53
                                   close() */
53
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
54
55
                                   FD_CLOEXEC */
55
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
56
#include <string.h>             /* strsep, strlen(), asprintf(),
 
57
                                   strsignal() */
56
58
#include <errno.h>              /* errno */
57
59
#include <argp.h>               /* struct argp_option, struct
58
60
                                   argp_state, struct argp,
65
67
                                   SIG_UNBLOCK, kill(), sig_atomic_t
66
68
                                */
67
69
#include <errno.h>              /* errno, EBADF */
68
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
 
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
69
71
 
70
72
#define BUFFER_SIZE 256
71
73
 
82
84
  char **environ;
83
85
  int envc;
84
86
  bool disabled;
85
 
 
 
87
  
86
88
  /* Variables used for running processes*/
87
89
  pid_t pid;
88
90
  int fd;
108
110
    }
109
111
  }
110
112
  /* Create a new plugin */
111
 
  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);
112
117
  if(new_plugin == NULL){
113
118
    return NULL;
114
119
  }
115
120
  char *copy_name = NULL;
116
121
  if(name != NULL){
117
 
    copy_name = strdup(name);
 
122
    do {
 
123
      copy_name = strdup(name);
 
124
    } while(copy_name == NULL and errno == EINTR);
118
125
    if(copy_name == NULL){
119
126
      free(new_plugin);
120
127
      return NULL;
126
133
                          .disabled = false,
127
134
                          .next = plugin_list };
128
135
  
129
 
  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);
130
139
  if(new_plugin->argv == NULL){
131
140
    free(copy_name);
132
141
    free(new_plugin);
135
144
  new_plugin->argv[0] = copy_name;
136
145
  new_plugin->argv[1] = NULL;
137
146
  
138
 
  new_plugin->environ = malloc(sizeof(char *));
 
147
  do {
 
148
    new_plugin->environ = malloc(sizeof(char *));
 
149
  } while(new_plugin->environ == NULL and errno == EINTR);
139
150
  if(new_plugin->environ == NULL){
140
151
    free(copy_name);
141
152
    free(new_plugin->argv);
153
164
static bool add_to_char_array(const char *new, char ***array,
154
165
                              int *len){
155
166
  /* Resize the pointed-to array to hold one more pointer */
156
 
  *array = realloc(*array, sizeof(char *)
157
 
                   * (size_t) ((*len) + 2));
 
167
  do {
 
168
    *array = realloc(*array, sizeof(char *)
 
169
                     * (size_t) ((*len) + 2));
 
170
  } while(*array == NULL and errno == EINTR);
158
171
  /* Malloc check */
159
172
  if(*array == NULL){
160
173
    return false;
161
174
  }
162
175
  /* Make a copy of the new string */
163
 
  char *copy = strdup(new);
 
176
  char *copy;
 
177
  do {
 
178
    copy = strdup(new);
 
179
  } while(copy == NULL and errno == EINTR);
164
180
  if(copy == NULL){
165
181
    return false;
166
182
  }
192
208
    if(strncmp(*e, def, namelen + 1) == 0){
193
209
      /* It already exists */
194
210
      if(replace){
195
 
        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);
196
215
        if(new == NULL){
197
216
          return false;
198
217
        }
208
227
/*
209
228
 * Based on the example in the GNU LibC manual chapter 13.13 "File
210
229
 * Descriptor Flags".
211
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
212
231
 */
213
232
static int set_cloexec_flag(int fd){
214
 
  int ret = fcntl(fd, F_GETFD, 0);
 
233
  int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
215
234
  /* If reading the flags failed, return error indication now. */
216
235
  if(ret < 0){
217
236
    return ret;
218
237
  }
219
238
  /* Store modified flag word in the descriptor. */
220
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
239
  return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
221
240
}
222
241
 
223
242
 
280
299
  }
281
300
  free(plugin_node->environ);
282
301
  free(plugin_node->buffer);
283
 
 
 
302
  
284
303
  /* Removes the plugin from the singly-linked list */
285
304
  if(plugin_node == plugin_list){
286
305
    /* First one - simple */
313
332
  struct dirent *dirst;
314
333
  struct stat st;
315
334
  fd_set rfds_all;
316
 
  int ret, numchars, maxfd = 0;
 
335
  int ret, maxfd = 0;
317
336
  ssize_t sret;
318
 
  intmax_t tmpmax;
319
337
  uid_t uid = 65534;
320
338
  gid_t gid = 65534;
321
339
  bool debug = false;
381
399
  error_t parse_opt(int key, char *arg, __attribute__((unused))
382
400
                    struct argp_state *state){
383
401
    switch(key){
 
402
      char *tmp;
 
403
      intmax_t tmpmax;
384
404
    case 'g':                   /* --global-options */
385
405
      if(arg != NULL){
386
406
        char *plugin_option;
456
476
      plugindir = strdup(arg);
457
477
      if(plugindir == NULL){
458
478
        perror("strdup");
459
 
      }      
 
479
      }
460
480
      break;
461
481
    case 129:                   /* --config-file */
462
482
      /* This is already done by parse_opt_config_file() */
463
483
      break;
464
484
    case 130:                   /* --userid */
465
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
466
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
467
 
         or arg[numchars] != '\0'){
 
485
      errno = 0;
 
486
      tmpmax = strtoimax(arg, &tmp, 10);
 
487
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
488
         or tmpmax != (uid_t)tmpmax){
468
489
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
469
490
                PRIdMAX "\n", arg, (intmax_t)uid);
470
491
      } else {
472
493
      }
473
494
      break;
474
495
    case 131:                   /* --groupid */
475
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
476
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
477
 
         or arg[numchars] != '\0'){
 
496
      errno = 0;
 
497
      tmpmax = strtoimax(arg, &tmp, 10);
 
498
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
499
         or tmpmax != (gid_t)tmpmax){
478
500
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
479
501
                PRIdMAX "\n", arg, (intmax_t)gid);
480
502
      } else {
524
546
      if(argfile == NULL){
525
547
        perror("strdup");
526
548
      }
527
 
      break;      
 
549
      break;
528
550
    case 130:                   /* --userid */
529
551
    case 131:                   /* --groupid */
530
552
    case 132:                   /* --debug */
559
581
    conffp = fopen(AFILE, "r");
560
582
  } else {
561
583
    conffp = fopen(argfile, "r");
562
 
  }  
 
584
  }
563
585
  if(conffp != NULL){
564
586
    char *org_line = NULL;
565
587
    char *p, *arg, *new_arg, *line;
566
588
    size_t size = 0;
567
589
    const char whitespace_delims[] = " \r\t\f\v\n";
568
590
    const char comment_delim[] = "#";
569
 
 
 
591
    
570
592
    custom_argc = 1;
571
593
    custom_argv = malloc(sizeof(char*) * 2);
572
594
    if(custom_argv == NULL){
576
598
    }
577
599
    custom_argv[0] = argv[0];
578
600
    custom_argv[1] = NULL;
579
 
 
 
601
    
580
602
    /* for each line in the config file, strip whitespace and ignore
581
603
       commented text */
582
604
    while(true){
584
606
      if(sret == -1){
585
607
        break;
586
608
      }
587
 
 
 
609
      
588
610
      line = org_line;
589
611
      arg = strsep(&line, comment_delim);
590
612
      while((p = strsep(&arg, whitespace_delims)) != NULL){
609
631
          goto fallback;
610
632
        }
611
633
        custom_argv[custom_argc-1] = new_arg;
612
 
        custom_argv[custom_argc] = NULL;        
 
634
        custom_argv[custom_argc] = NULL;
613
635
      }
614
636
    }
 
637
    do {
 
638
      ret = fclose(conffp);
 
639
    } while(ret == EOF and errno == EINTR);
 
640
    if(ret == EOF){
 
641
      perror("fclose");
 
642
      exitstatus = EXIT_FAILURE;
 
643
      goto fallback;
 
644
    }
615
645
    free(org_line);
616
646
  } else {
617
647
    /* Check for harmful errors and go to fallback. Other errors might
696
726
  
697
727
  /* Read and execute any executable in the plugin directory*/
698
728
  while(true){
699
 
    dirst = readdir(dir);
 
729
    do {
 
730
      dirst = readdir(dir);
 
731
    } while(dirst == NULL and errno == EINTR);
700
732
    
701
733
    /* All directory entries have been processed */
702
734
    if(dirst == NULL){
753
785
        continue;
754
786
      }
755
787
    }
756
 
 
 
788
    
757
789
    char *filename;
758
790
    if(plugindir == NULL){
759
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
791
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
792
                                        dirst->d_name));
760
793
    } else {
761
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
794
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
 
795
                                        dirst->d_name));
762
796
    }
763
797
    if(ret < 0){
764
798
      perror("asprintf");
765
799
      continue;
766
800
    }
767
801
    
768
 
    ret = stat(filename, &st);
 
802
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
769
803
    if(ret == -1){
770
804
      perror("stat");
771
805
      free(filename);
772
806
      continue;
773
807
    }
774
 
 
 
808
    
775
809
    /* Ignore non-executable files */
776
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
810
    if(not S_ISREG(st.st_mode)
 
811
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
777
812
      if(debug){
778
813
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
779
814
                " with bad type or mode\n", filename);
825
860
    }
826
861
    
827
862
    int pipefd[2];
828
 
    ret = pipe(pipefd);
 
863
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
829
864
    if(ret == -1){
830
865
      perror("pipe");
831
866
      exitstatus = EXIT_FAILURE;
845
880
      goto fallback;
846
881
    }
847
882
    /* Block SIGCHLD until process is safely in process list */
848
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
883
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
884
                                         &sigchld_action.sa_mask,
 
885
                                         NULL));
849
886
    if(ret < 0){
850
887
      perror("sigprocmask");
851
888
      exitstatus = EXIT_FAILURE;
852
889
      goto fallback;
853
890
    }
854
891
    /* Starting a new process to be watched */
855
 
    pid_t pid = fork();
 
892
    pid_t pid;
 
893
    do {
 
894
      pid = fork();
 
895
    } while(pid == -1 and errno == EINTR);
856
896
    if(pid == -1){
857
897
      perror("fork");
858
898
      exitstatus = EXIT_FAILURE;
896
936
      /* no return */
897
937
    }
898
938
    /* Parent process */
899
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
939
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
940
                                             pipe */
900
941
    free(filename);
901
942
    plugin *new_plugin = getplugin(dirst->d_name);
902
943
    if(new_plugin == NULL){
903
944
      perror("getplugin");
904
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
945
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
946
                                           &sigchld_action.sa_mask,
 
947
                                           NULL));
905
948
      if(ret < 0){
906
949
        perror("sigprocmask");
907
950
      }
914
957
    
915
958
    /* Unblock SIGCHLD so signal handler can be run if this process
916
959
       has already completed */
917
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
960
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
961
                                         &sigchld_action.sa_mask,
 
962
                                         NULL));
918
963
    if(ret < 0){
919
964
      perror("sigprocmask");
920
965
      exitstatus = EXIT_FAILURE;
928
973
    }
929
974
  }
930
975
  
931
 
  closedir(dir);
 
976
  TEMP_FAILURE_RETRY(closedir(dir));
932
977
  dir = NULL;
933
978
  free_plugin(getplugin(NULL));
934
979
  
947
992
  while(plugin_list){
948
993
    fd_set rfds = rfds_all;
949
994
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
950
 
    if(select_ret == -1){
 
995
    if(select_ret == -1 and errno != EINTR){
951
996
      perror("select");
952
997
      exitstatus = EXIT_FAILURE;
953
998
      goto fallback;
961
1006
        if(not WIFEXITED(proc->status)
962
1007
           or WEXITSTATUS(proc->status) != 0){
963
1008
          /* Bad exit by plugin */
964
 
 
 
1009
          
965
1010
          if(debug){
966
1011
            if(WIFEXITED(proc->status)){
967
1012
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
970
1015
                      WEXITSTATUS(proc->status));
971
1016
            } else if(WIFSIGNALED(proc->status)){
972
1017
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
973
 
                      " signal %d\n", proc->name,
 
1018
                      " signal %d: %s\n", proc->name,
974
1019
                      (intmax_t) (proc->pid),
975
 
                      WTERMSIG(proc->status));
 
1020
                      WTERMSIG(proc->status),
 
1021
                      strsignal(WTERMSIG(proc->status)));
976
1022
            } else if(WCOREDUMP(proc->status)){
977
1023
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
978
1024
                      " core\n", proc->name, (intmax_t) (proc->pid));
981
1027
          
982
1028
          /* Remove the plugin */
983
1029
          FD_CLR(proc->fd, &rfds_all);
984
 
 
 
1030
          
985
1031
          /* Block signal while modifying process_list */
986
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1032
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
1033
                                               &sigchld_action.sa_mask,
 
1034
                                               NULL));
987
1035
          if(ret < 0){
988
1036
            perror("sigprocmask");
989
1037
            exitstatus = EXIT_FAILURE;
995
1043
          proc = next_plugin;
996
1044
          
997
1045
          /* We are done modifying process list, so unblock signal */
998
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
999
 
                            NULL);
 
1046
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1047
                                               &sigchld_action.sa_mask,
 
1048
                                               NULL));
1000
1049
          if(ret < 0){
1001
1050
            perror("sigprocmask");
1002
1051
            exitstatus = EXIT_FAILURE;
1039
1088
        proc->buffer_size += BUFFER_SIZE;
1040
1089
      }
1041
1090
      /* Read from the process */
1042
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1043
 
                  BUFFER_SIZE);
 
1091
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1092
                                     proc->buffer
 
1093
                                     + proc->buffer_length,
 
1094
                                     BUFFER_SIZE));
1044
1095
      if(sret < 0){
1045
1096
        /* Read error from this process; ignore the error */
1046
1097
        proc = proc->next;
1054
1105
      }
1055
1106
    }
1056
1107
  }
1057
 
 
1058
 
 
 
1108
  
 
1109
  
1059
1110
 fallback:
1060
1111
  
1061
1112
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1108
1159
  }
1109
1160
  
1110
1161
  /* Wait for any remaining child processes to terminate */
1111
 
  do{
 
1162
  do {
1112
1163
    ret = wait(NULL);
1113
1164
  } while(ret >= 0);
1114
1165
  if(errno != ECHILD){