/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-runner.c (main): Bug fix: Close config file.

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;
208
210
/*
209
211
 * Based on the example in the GNU LibC manual chapter 13.13 "File
210
212
 * Descriptor Flags".
211
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
213
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
212
214
 */
213
215
static int set_cloexec_flag(int fd){
214
216
  int ret = fcntl(fd, F_GETFD, 0);
280
282
  }
281
283
  free(plugin_node->environ);
282
284
  free(plugin_node->buffer);
283
 
 
 
285
  
284
286
  /* Removes the plugin from the singly-linked list */
285
287
  if(plugin_node == plugin_list){
286
288
    /* First one - simple */
313
315
  struct dirent *dirst;
314
316
  struct stat st;
315
317
  fd_set rfds_all;
316
 
  int ret, numchars, maxfd = 0;
 
318
  int ret, maxfd = 0;
317
319
  ssize_t sret;
318
320
  intmax_t tmpmax;
319
321
  uid_t uid = 65534;
380
382
  
381
383
  error_t parse_opt(int key, char *arg, __attribute__((unused))
382
384
                    struct argp_state *state){
 
385
    char *tmp;
383
386
    switch(key){
384
387
    case 'g':                   /* --global-options */
385
388
      if(arg != NULL){
456
459
      plugindir = strdup(arg);
457
460
      if(plugindir == NULL){
458
461
        perror("strdup");
459
 
      }      
 
462
      }
460
463
      break;
461
464
    case 129:                   /* --config-file */
462
465
      /* This is already done by parse_opt_config_file() */
463
466
      break;
464
467
    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'){
 
468
      errno = 0;
 
469
      tmpmax = strtoimax(arg, &tmp, 10);
 
470
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
471
         or tmpmax != (uid_t)tmpmax){
468
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
469
473
                PRIdMAX "\n", arg, (intmax_t)uid);
470
474
      } else {
472
476
      }
473
477
      break;
474
478
    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'){
 
479
      errno = 0;
 
480
      tmpmax = strtoimax(arg, &tmp, 10);
 
481
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
482
         or tmpmax != (gid_t)tmpmax){
478
483
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
479
484
                PRIdMAX "\n", arg, (intmax_t)gid);
480
485
      } else {
524
529
      if(argfile == NULL){
525
530
        perror("strdup");
526
531
      }
527
 
      break;      
 
532
      break;
528
533
    case 130:                   /* --userid */
529
534
    case 131:                   /* --groupid */
530
535
    case 132:                   /* --debug */
559
564
    conffp = fopen(AFILE, "r");
560
565
  } else {
561
566
    conffp = fopen(argfile, "r");
562
 
  }  
 
567
  }
563
568
  if(conffp != NULL){
564
569
    char *org_line = NULL;
565
570
    char *p, *arg, *new_arg, *line;
566
571
    size_t size = 0;
567
572
    const char whitespace_delims[] = " \r\t\f\v\n";
568
573
    const char comment_delim[] = "#";
569
 
 
 
574
    
570
575
    custom_argc = 1;
571
576
    custom_argv = malloc(sizeof(char*) * 2);
572
577
    if(custom_argv == NULL){
576
581
    }
577
582
    custom_argv[0] = argv[0];
578
583
    custom_argv[1] = NULL;
579
 
 
 
584
    
580
585
    /* for each line in the config file, strip whitespace and ignore
581
586
       commented text */
582
587
    while(true){
584
589
      if(sret == -1){
585
590
        break;
586
591
      }
587
 
 
 
592
      
588
593
      line = org_line;
589
594
      arg = strsep(&line, comment_delim);
590
595
      while((p = strsep(&arg, whitespace_delims)) != NULL){
609
614
          goto fallback;
610
615
        }
611
616
        custom_argv[custom_argc-1] = new_arg;
612
 
        custom_argv[custom_argc] = NULL;        
 
617
        custom_argv[custom_argc] = NULL;
613
618
      }
614
619
    }
 
620
    do{
 
621
      ret = fclose(conffp);
 
622
    }while(ret == EOF and errno == EINTR);
 
623
    if(ret == EOF){
 
624
      perror("fclose");
 
625
      exitstatus = EXIT_FAILURE;
 
626
      goto fallback;
 
627
    }
615
628
    free(org_line);
616
629
  } else {
617
630
    /* Check for harmful errors and go to fallback. Other errors might
753
766
        continue;
754
767
      }
755
768
    }
756
 
 
 
769
    
757
770
    char *filename;
758
771
    if(plugindir == NULL){
759
772
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
771
784
      free(filename);
772
785
      continue;
773
786
    }
774
 
 
 
787
    
775
788
    /* Ignore non-executable files */
776
789
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
777
790
      if(debug){
961
974
        if(not WIFEXITED(proc->status)
962
975
           or WEXITSTATUS(proc->status) != 0){
963
976
          /* Bad exit by plugin */
964
 
 
 
977
          
965
978
          if(debug){
966
979
            if(WIFEXITED(proc->status)){
967
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
970
983
                      WEXITSTATUS(proc->status));
971
984
            } else if(WIFSIGNALED(proc->status)){
972
985
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
973
 
                      " signal %d\n", proc->name,
 
986
                      " signal %d: %s\n", proc->name,
974
987
                      (intmax_t) (proc->pid),
975
 
                      WTERMSIG(proc->status));
 
988
                      WTERMSIG(proc->status),
 
989
                      strsignal(WTERMSIG(proc->status)));
976
990
            } else if(WCOREDUMP(proc->status)){
977
991
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
978
992
                      " core\n", proc->name, (intmax_t) (proc->pid));
981
995
          
982
996
          /* Remove the plugin */
983
997
          FD_CLR(proc->fd, &rfds_all);
984
 
 
 
998
          
985
999
          /* Block signal while modifying process_list */
986
1000
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
987
1001
          if(ret < 0){
1054
1068
      }
1055
1069
    }
1056
1070
  }
1057
 
 
1058
 
 
 
1071
  
 
1072
  
1059
1073
 fallback:
1060
1074
  
1061
1075
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){