/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

todo

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(), strcmp(), strncmp() */
56
58
#include <errno.h>              /* errno */
57
59
#include <argp.h>               /* struct argp_option, struct
58
60
                                   argp_state, struct argp,
62
64
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
65
                                   sigaddset(), sigaction(),
64
66
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
67
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
68
                                */
66
69
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
 
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
68
71
 
69
72
#define BUFFER_SIZE 256
70
73
 
81
84
  char **environ;
82
85
  int envc;
83
86
  bool disabled;
84
 
 
 
87
  
85
88
  /* Variables used for running processes*/
86
89
  pid_t pid;
87
90
  int fd;
107
110
    }
108
111
  }
109
112
  /* Create a new plugin */
110
 
  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);
111
117
  if(new_plugin == NULL){
112
118
    return NULL;
113
119
  }
114
120
  char *copy_name = NULL;
115
121
  if(name != NULL){
116
 
    copy_name = strdup(name);
 
122
    do {
 
123
      copy_name = strdup(name);
 
124
    } while(copy_name == NULL and errno == EINTR);
117
125
    if(copy_name == NULL){
118
126
      free(new_plugin);
119
127
      return NULL;
125
133
                          .disabled = false,
126
134
                          .next = plugin_list };
127
135
  
128
 
  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);
129
139
  if(new_plugin->argv == NULL){
130
140
    free(copy_name);
131
141
    free(new_plugin);
134
144
  new_plugin->argv[0] = copy_name;
135
145
  new_plugin->argv[1] = NULL;
136
146
  
137
 
  new_plugin->environ = malloc(sizeof(char *));
 
147
  do {
 
148
    new_plugin->environ = malloc(sizeof(char *));
 
149
  } while(new_plugin->environ == NULL and errno == EINTR);
138
150
  if(new_plugin->environ == NULL){
139
151
    free(copy_name);
140
152
    free(new_plugin->argv);
152
164
static bool add_to_char_array(const char *new, char ***array,
153
165
                              int *len){
154
166
  /* Resize the pointed-to array to hold one more pointer */
155
 
  *array = realloc(*array, sizeof(char *)
156
 
                   * (size_t) ((*len) + 2));
 
167
  do {
 
168
    *array = realloc(*array, sizeof(char *)
 
169
                     * (size_t) ((*len) + 2));
 
170
  } while(*array == NULL and errno == EINTR);
157
171
  /* Malloc check */
158
172
  if(*array == NULL){
159
173
    return false;
160
174
  }
161
175
  /* Make a copy of the new string */
162
 
  char *copy = strdup(new);
 
176
  char *copy;
 
177
  do {
 
178
    copy = strdup(new);
 
179
  } while(copy == NULL and errno == EINTR);
163
180
  if(copy == NULL){
164
181
    return false;
165
182
  }
191
208
    if(strncmp(*e, def, namelen + 1) == 0){
192
209
      /* It already exists */
193
210
      if(replace){
194
 
        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);
195
215
        if(new == NULL){
196
216
          return false;
197
217
        }
207
227
/*
208
228
 * Based on the example in the GNU LibC manual chapter 13.13 "File
209
229
 * Descriptor Flags".
210
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
230
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
211
231
 */
212
232
static int set_cloexec_flag(int fd){
213
 
  int ret = fcntl(fd, F_GETFD, 0);
 
233
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
214
234
  /* If reading the flags failed, return error indication now. */
215
235
  if(ret < 0){
216
236
    return ret;
217
237
  }
218
238
  /* Store modified flag word in the descriptor. */
219
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
239
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
240
                                       ret | FD_CLOEXEC));
220
241
}
221
242
 
222
243
 
279
300
  }
280
301
  free(plugin_node->environ);
281
302
  free(plugin_node->buffer);
282
 
 
 
303
  
283
304
  /* Removes the plugin from the singly-linked list */
284
305
  if(plugin_node == plugin_list){
285
306
    /* First one - simple */
312
333
  struct dirent *dirst;
313
334
  struct stat st;
314
335
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
336
  int ret, maxfd = 0;
316
337
  ssize_t sret;
317
 
  intmax_t tmpmax;
318
338
  uid_t uid = 65534;
319
339
  gid_t gid = 65534;
320
340
  bool debug = false;
380
400
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
401
                    struct argp_state *state){
382
402
    switch(key){
 
403
      char *tmp;
 
404
      intmax_t tmpmax;
383
405
    case 'g':                   /* --global-options */
384
406
      if(arg != NULL){
385
 
        char *p;
386
 
        while((p = strsep(&arg, ",")) != NULL){
387
 
          if(p[0] == '\0'){
 
407
        char *plugin_option;
 
408
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
409
          if(plugin_option[0] == '\0'){
388
410
            continue;
389
411
          }
390
 
          if(not add_argument(getplugin(NULL), p)){
 
412
          if(not add_argument(getplugin(NULL), plugin_option)){
391
413
            perror("add_argument");
392
414
            return ARGP_ERR_UNKNOWN;
393
415
          }
404
426
      break;
405
427
    case 'o':                   /* --options-for */
406
428
      if(arg != NULL){
407
 
        char *p_name = strsep(&arg, ":");
408
 
        if(p_name[0] == '\0' or arg == NULL){
409
 
          break;
410
 
        }
411
 
        char *opt = strsep(&arg, ":");
412
 
        if(opt[0] == '\0' or opt == NULL){
413
 
          break;
414
 
        }
415
 
        char *p;
416
 
        while((p = strsep(&opt, ",")) != NULL){
417
 
          if(p[0] == '\0'){
418
 
            continue;
419
 
          }
420
 
          if(not add_argument(getplugin(p_name), p)){
 
429
        char *plugin_name = strsep(&arg, ":");
 
430
        if(plugin_name[0] == '\0'){
 
431
          break;
 
432
        }
 
433
        char *plugin_option;
 
434
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
435
          if(not add_argument(getplugin(plugin_name), plugin_option)){
421
436
            perror("add_argument");
422
437
            return ARGP_ERR_UNKNOWN;
423
438
          }
462
477
      plugindir = strdup(arg);
463
478
      if(plugindir == NULL){
464
479
        perror("strdup");
465
 
      }      
 
480
      }
466
481
      break;
467
482
    case 129:                   /* --config-file */
468
483
      /* This is already done by parse_opt_config_file() */
469
484
      break;
470
485
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
 
486
      errno = 0;
 
487
      tmpmax = strtoimax(arg, &tmp, 10);
 
488
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
489
         or tmpmax != (uid_t)tmpmax){
474
490
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
491
                PRIdMAX "\n", arg, (intmax_t)uid);
476
492
      } else {
478
494
      }
479
495
      break;
480
496
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
 
497
      errno = 0;
 
498
      tmpmax = strtoimax(arg, &tmp, 10);
 
499
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
500
         or tmpmax != (gid_t)tmpmax){
484
501
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
502
                PRIdMAX "\n", arg, (intmax_t)gid);
486
503
      } else {
530
547
      if(argfile == NULL){
531
548
        perror("strdup");
532
549
      }
533
 
      break;      
 
550
      break;
534
551
    case 130:                   /* --userid */
535
552
    case 131:                   /* --groupid */
536
553
    case 132:                   /* --debug */
565
582
    conffp = fopen(AFILE, "r");
566
583
  } else {
567
584
    conffp = fopen(argfile, "r");
568
 
  }  
 
585
  }
569
586
  if(conffp != NULL){
570
587
    char *org_line = NULL;
571
588
    char *p, *arg, *new_arg, *line;
572
589
    size_t size = 0;
573
590
    const char whitespace_delims[] = " \r\t\f\v\n";
574
591
    const char comment_delim[] = "#";
575
 
 
 
592
    
576
593
    custom_argc = 1;
577
594
    custom_argv = malloc(sizeof(char*) * 2);
578
595
    if(custom_argv == NULL){
582
599
    }
583
600
    custom_argv[0] = argv[0];
584
601
    custom_argv[1] = NULL;
585
 
 
 
602
    
586
603
    /* for each line in the config file, strip whitespace and ignore
587
604
       commented text */
588
605
    while(true){
590
607
      if(sret == -1){
591
608
        break;
592
609
      }
593
 
 
 
610
      
594
611
      line = org_line;
595
612
      arg = strsep(&line, comment_delim);
596
613
      while((p = strsep(&arg, whitespace_delims)) != NULL){
615
632
          goto fallback;
616
633
        }
617
634
        custom_argv[custom_argc-1] = new_arg;
618
 
        custom_argv[custom_argc] = NULL;        
 
635
        custom_argv[custom_argc] = NULL;
619
636
      }
620
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
    }
621
646
    free(org_line);
622
647
  } else {
623
648
    /* Check for harmful errors and go to fallback. Other errors might
702
727
  
703
728
  /* Read and execute any executable in the plugin directory*/
704
729
  while(true){
705
 
    dirst = readdir(dir);
 
730
    do {
 
731
      dirst = readdir(dir);
 
732
    } while(dirst == NULL and errno == EINTR);
706
733
    
707
734
    /* All directory entries have been processed */
708
735
    if(dirst == NULL){
744
771
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
745
772
        size_t suf_len = strlen(*suf);
746
773
        if((d_name_len >= suf_len)
747
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
 
774
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
748
775
                == 0)){
749
776
          if(debug){
750
777
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
759
786
        continue;
760
787
      }
761
788
    }
762
 
 
 
789
    
763
790
    char *filename;
764
791
    if(plugindir == NULL){
765
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
792
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
793
                                             dirst->d_name));
766
794
    } else {
767
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
795
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
796
                                             plugindir,
 
797
                                             dirst->d_name));
768
798
    }
769
799
    if(ret < 0){
770
800
      perror("asprintf");
771
801
      continue;
772
802
    }
773
803
    
774
 
    ret = stat(filename, &st);
 
804
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
775
805
    if(ret == -1){
776
806
      perror("stat");
777
807
      free(filename);
778
808
      continue;
779
809
    }
780
 
 
 
810
    
781
811
    /* Ignore non-executable files */
782
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
812
    if(not S_ISREG(st.st_mode)
 
813
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
783
814
      if(debug){
784
815
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
785
816
                " with bad type or mode\n", filename);
831
862
    }
832
863
    
833
864
    int pipefd[2];
834
 
    ret = pipe(pipefd);
 
865
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
835
866
    if(ret == -1){
836
867
      perror("pipe");
837
868
      exitstatus = EXIT_FAILURE;
851
882
      goto fallback;
852
883
    }
853
884
    /* Block SIGCHLD until process is safely in process list */
854
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
885
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
886
                                              &sigchld_action.sa_mask,
 
887
                                              NULL));
855
888
    if(ret < 0){
856
889
      perror("sigprocmask");
857
890
      exitstatus = EXIT_FAILURE;
858
891
      goto fallback;
859
892
    }
860
893
    /* Starting a new process to be watched */
861
 
    pid_t pid = fork();
 
894
    pid_t pid;
 
895
    do {
 
896
      pid = fork();
 
897
    } while(pid == -1 and errno == EINTR);
862
898
    if(pid == -1){
863
899
      perror("fork");
864
900
      exitstatus = EXIT_FAILURE;
902
938
      /* no return */
903
939
    }
904
940
    /* Parent process */
905
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
941
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
942
                                             pipe */
906
943
    free(filename);
907
944
    plugin *new_plugin = getplugin(dirst->d_name);
908
945
    if(new_plugin == NULL){
909
946
      perror("getplugin");
910
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
947
      ret = (int)(TEMP_FAILURE_RETRY
 
948
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
949
                               NULL)));
911
950
      if(ret < 0){
912
951
        perror("sigprocmask");
913
952
      }
920
959
    
921
960
    /* Unblock SIGCHLD so signal handler can be run if this process
922
961
       has already completed */
923
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
962
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
963
                                              &sigchld_action.sa_mask,
 
964
                                              NULL));
924
965
    if(ret < 0){
925
966
      perror("sigprocmask");
926
967
      exitstatus = EXIT_FAILURE;
927
968
      goto fallback;
928
969
    }
929
970
    
930
 
    FD_SET(new_plugin->fd, &rfds_all);
 
971
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
972
                                          -Wconversion */
931
973
    
932
974
    if(maxfd < new_plugin->fd){
933
975
      maxfd = new_plugin->fd;
934
976
    }
935
977
  }
936
978
  
937
 
  closedir(dir);
 
979
  TEMP_FAILURE_RETRY(closedir(dir));
938
980
  dir = NULL;
939
981
  free_plugin(getplugin(NULL));
940
982
  
953
995
  while(plugin_list){
954
996
    fd_set rfds = rfds_all;
955
997
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
956
 
    if(select_ret == -1){
 
998
    if(select_ret == -1 and errno != EINTR){
957
999
      perror("select");
958
1000
      exitstatus = EXIT_FAILURE;
959
1001
      goto fallback;
967
1009
        if(not WIFEXITED(proc->status)
968
1010
           or WEXITSTATUS(proc->status) != 0){
969
1011
          /* Bad exit by plugin */
970
 
 
 
1012
          
971
1013
          if(debug){
972
1014
            if(WIFEXITED(proc->status)){
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
974
 
                      " %d\n", (intmax_t) (proc->pid),
 
1015
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
1016
                      " status %d\n", proc->name,
 
1017
                      (intmax_t) (proc->pid),
975
1018
                      WEXITSTATUS(proc->status));
976
1019
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
978
 
                      " %d\n", (intmax_t) (proc->pid),
979
 
                      WTERMSIG(proc->status));
 
1020
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
1021
                      " signal %d: %s\n", proc->name,
 
1022
                      (intmax_t) (proc->pid),
 
1023
                      WTERMSIG(proc->status),
 
1024
                      strsignal(WTERMSIG(proc->status)));
980
1025
            } else if(WCOREDUMP(proc->status)){
981
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
982
 
                      (intmax_t) (proc->pid));
 
1026
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1027
                      " core\n", proc->name, (intmax_t) (proc->pid));
983
1028
            }
984
1029
          }
985
1030
          
986
1031
          /* Remove the plugin */
987
 
          FD_CLR(proc->fd, &rfds_all);
988
 
 
 
1032
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1033
                                          -Wconversion */
 
1034
          
989
1035
          /* Block signal while modifying process_list */
990
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1036
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1037
                                        (SIG_BLOCK,
 
1038
                                         &sigchld_action.sa_mask,
 
1039
                                         NULL));
991
1040
          if(ret < 0){
992
1041
            perror("sigprocmask");
993
1042
            exitstatus = EXIT_FAILURE;
999
1048
          proc = next_plugin;
1000
1049
          
1001
1050
          /* We are done modifying process list, so unblock signal */
1002
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1003
 
                            NULL);
 
1051
          ret = (int)(TEMP_FAILURE_RETRY
 
1052
                      (sigprocmask(SIG_UNBLOCK,
 
1053
                                   &sigchld_action.sa_mask, NULL)));
1004
1054
          if(ret < 0){
1005
1055
            perror("sigprocmask");
1006
1056
            exitstatus = EXIT_FAILURE;
1026
1076
      }
1027
1077
      
1028
1078
      /* This process has not completed.  Does it have any output? */
1029
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1079
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1080
                                                         warning from
 
1081
                                                         -Wconversion */
1030
1082
        /* This process had nothing to say at this time */
1031
1083
        proc = proc->next;
1032
1084
        continue;
1043
1095
        proc->buffer_size += BUFFER_SIZE;
1044
1096
      }
1045
1097
      /* Read from the process */
1046
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1047
 
                  BUFFER_SIZE);
 
1098
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1099
                                     proc->buffer
 
1100
                                     + proc->buffer_length,
 
1101
                                     BUFFER_SIZE));
1048
1102
      if(sret < 0){
1049
1103
        /* Read error from this process; ignore the error */
1050
1104
        proc = proc->next;
1058
1112
      }
1059
1113
    }
1060
1114
  }
1061
 
 
1062
 
 
 
1115
  
 
1116
  
1063
1117
 fallback:
1064
1118
  
1065
1119
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1112
1166
  }
1113
1167
  
1114
1168
  /* Wait for any remaining child processes to terminate */
1115
 
  do{
 
1169
  do {
1116
1170
    ret = wait(NULL);
1117
1171
  } while(ret >= 0);
1118
1172
  if(errno != ECHILD){