/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

* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
                                                   GnuTLS functions.
  (main): Drop privileges early, and raise them only where needed and
          lower them whenever possible.

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,
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
407
        char *plugin_option;
455
477
      plugindir = strdup(arg);
456
478
      if(plugindir == NULL){
457
479
        perror("strdup");
458
 
      }      
 
480
      }
459
481
      break;
460
482
    case 129:                   /* --config-file */
461
483
      /* This is already done by parse_opt_config_file() */
462
484
      break;
463
485
    case 130:                   /* --userid */
464
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
465
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
466
 
         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){
467
490
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
491
                PRIdMAX "\n", arg, (intmax_t)uid);
469
492
      } else {
471
494
      }
472
495
      break;
473
496
    case 131:                   /* --groupid */
474
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
475
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
476
 
         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){
477
501
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
502
                PRIdMAX "\n", arg, (intmax_t)gid);
479
503
      } else {
523
547
      if(argfile == NULL){
524
548
        perror("strdup");
525
549
      }
526
 
      break;      
 
550
      break;
527
551
    case 130:                   /* --userid */
528
552
    case 131:                   /* --groupid */
529
553
    case 132:                   /* --debug */
558
582
    conffp = fopen(AFILE, "r");
559
583
  } else {
560
584
    conffp = fopen(argfile, "r");
561
 
  }  
 
585
  }
562
586
  if(conffp != NULL){
563
587
    char *org_line = NULL;
564
588
    char *p, *arg, *new_arg, *line;
565
589
    size_t size = 0;
566
590
    const char whitespace_delims[] = " \r\t\f\v\n";
567
591
    const char comment_delim[] = "#";
568
 
 
 
592
    
569
593
    custom_argc = 1;
570
594
    custom_argv = malloc(sizeof(char*) * 2);
571
595
    if(custom_argv == NULL){
575
599
    }
576
600
    custom_argv[0] = argv[0];
577
601
    custom_argv[1] = NULL;
578
 
 
 
602
    
579
603
    /* for each line in the config file, strip whitespace and ignore
580
604
       commented text */
581
605
    while(true){
583
607
      if(sret == -1){
584
608
        break;
585
609
      }
586
 
 
 
610
      
587
611
      line = org_line;
588
612
      arg = strsep(&line, comment_delim);
589
613
      while((p = strsep(&arg, whitespace_delims)) != NULL){
608
632
          goto fallback;
609
633
        }
610
634
        custom_argv[custom_argc-1] = new_arg;
611
 
        custom_argv[custom_argc] = NULL;        
 
635
        custom_argv[custom_argc] = NULL;
612
636
      }
613
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
    }
614
646
    free(org_line);
615
647
  } else {
616
648
    /* Check for harmful errors and go to fallback. Other errors might
695
727
  
696
728
  /* Read and execute any executable in the plugin directory*/
697
729
  while(true){
698
 
    dirst = readdir(dir);
 
730
    do {
 
731
      dirst = readdir(dir);
 
732
    } while(dirst == NULL and errno == EINTR);
699
733
    
700
734
    /* All directory entries have been processed */
701
735
    if(dirst == NULL){
752
786
        continue;
753
787
      }
754
788
    }
755
 
 
 
789
    
756
790
    char *filename;
757
791
    if(plugindir == NULL){
758
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
792
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
793
                                             dirst->d_name));
759
794
    } else {
760
 
      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));
761
798
    }
762
799
    if(ret < 0){
763
800
      perror("asprintf");
764
801
      continue;
765
802
    }
766
803
    
767
 
    ret = stat(filename, &st);
 
804
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
768
805
    if(ret == -1){
769
806
      perror("stat");
770
807
      free(filename);
771
808
      continue;
772
809
    }
773
 
 
 
810
    
774
811
    /* Ignore non-executable files */
775
 
    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)){
776
814
      if(debug){
777
815
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
816
                " with bad type or mode\n", filename);
824
862
    }
825
863
    
826
864
    int pipefd[2];
827
 
    ret = pipe(pipefd);
 
865
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
828
866
    if(ret == -1){
829
867
      perror("pipe");
830
868
      exitstatus = EXIT_FAILURE;
844
882
      goto fallback;
845
883
    }
846
884
    /* Block SIGCHLD until process is safely in process list */
847
 
    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));
848
888
    if(ret < 0){
849
889
      perror("sigprocmask");
850
890
      exitstatus = EXIT_FAILURE;
851
891
      goto fallback;
852
892
    }
853
893
    /* Starting a new process to be watched */
854
 
    pid_t pid = fork();
 
894
    pid_t pid;
 
895
    do {
 
896
      pid = fork();
 
897
    } while(pid == -1 and errno == EINTR);
855
898
    if(pid == -1){
856
899
      perror("fork");
857
900
      exitstatus = EXIT_FAILURE;
895
938
      /* no return */
896
939
    }
897
940
    /* Parent process */
898
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
941
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
942
                                             pipe */
899
943
    free(filename);
900
944
    plugin *new_plugin = getplugin(dirst->d_name);
901
945
    if(new_plugin == NULL){
902
946
      perror("getplugin");
903
 
      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)));
904
950
      if(ret < 0){
905
951
        perror("sigprocmask");
906
952
      }
913
959
    
914
960
    /* Unblock SIGCHLD so signal handler can be run if this process
915
961
       has already completed */
916
 
    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));
917
965
    if(ret < 0){
918
966
      perror("sigprocmask");
919
967
      exitstatus = EXIT_FAILURE;
927
975
    }
928
976
  }
929
977
  
930
 
  closedir(dir);
 
978
  TEMP_FAILURE_RETRY(closedir(dir));
931
979
  dir = NULL;
932
980
  free_plugin(getplugin(NULL));
933
981
  
946
994
  while(plugin_list){
947
995
    fd_set rfds = rfds_all;
948
996
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
997
    if(select_ret == -1 and errno != EINTR){
950
998
      perror("select");
951
999
      exitstatus = EXIT_FAILURE;
952
1000
      goto fallback;
960
1008
        if(not WIFEXITED(proc->status)
961
1009
           or WEXITSTATUS(proc->status) != 0){
962
1010
          /* Bad exit by plugin */
963
 
 
 
1011
          
964
1012
          if(debug){
965
1013
            if(WIFEXITED(proc->status)){
966
1014
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
969
1017
                      WEXITSTATUS(proc->status));
970
1018
            } else if(WIFSIGNALED(proc->status)){
971
1019
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
 
1020
                      " signal %d: %s\n", proc->name,
973
1021
                      (intmax_t) (proc->pid),
974
 
                      WTERMSIG(proc->status));
 
1022
                      WTERMSIG(proc->status),
 
1023
                      strsignal(WTERMSIG(proc->status)));
975
1024
            } else if(WCOREDUMP(proc->status)){
976
1025
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
1026
                      " core\n", proc->name, (intmax_t) (proc->pid));
980
1029
          
981
1030
          /* Remove the plugin */
982
1031
          FD_CLR(proc->fd, &rfds_all);
983
 
 
 
1032
          
984
1033
          /* Block signal while modifying process_list */
985
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1034
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1035
                                        (SIG_BLOCK,
 
1036
                                         &sigchld_action.sa_mask,
 
1037
                                         NULL));
986
1038
          if(ret < 0){
987
1039
            perror("sigprocmask");
988
1040
            exitstatus = EXIT_FAILURE;
994
1046
          proc = next_plugin;
995
1047
          
996
1048
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
1049
          ret = (int)(TEMP_FAILURE_RETRY
 
1050
                      (sigprocmask(SIG_UNBLOCK,
 
1051
                                   &sigchld_action.sa_mask, NULL)));
999
1052
          if(ret < 0){
1000
1053
            perror("sigprocmask");
1001
1054
            exitstatus = EXIT_FAILURE;
1038
1091
        proc->buffer_size += BUFFER_SIZE;
1039
1092
      }
1040
1093
      /* Read from the process */
1041
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1042
 
                  BUFFER_SIZE);
 
1094
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1095
                                     proc->buffer
 
1096
                                     + proc->buffer_length,
 
1097
                                     BUFFER_SIZE));
1043
1098
      if(sret < 0){
1044
1099
        /* Read error from this process; ignore the error */
1045
1100
        proc = proc->next;
1053
1108
      }
1054
1109
    }
1055
1110
  }
1056
 
 
1057
 
 
 
1111
  
 
1112
  
1058
1113
 fallback:
1059
1114
  
1060
1115
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1107
1162
  }
1108
1163
  
1109
1164
  /* Wait for any remaining child processes to terminate */
1110
 
  do{
 
1165
  do {
1111
1166
    ret = wait(NULL);
1112
1167
  } while(ret >= 0);
1113
1168
  if(errno != ECHILD){