/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 (start_mandos_communication): Check
                                                         "quit_now"
                                                         throughout.
                                                         Always close
                                                         socket and
                                                         deinit GnuTLS
                                                         properly.
  (browse_callback): Return immediately if "quit_now" is set.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
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
    }
615
620
    free(org_line);
753
758
        continue;
754
759
      }
755
760
    }
756
 
 
 
761
    
757
762
    char *filename;
758
763
    if(plugindir == NULL){
759
764
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
771
776
      free(filename);
772
777
      continue;
773
778
    }
774
 
 
 
779
    
775
780
    /* Ignore non-executable files */
776
781
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
777
782
      if(debug){
961
966
        if(not WIFEXITED(proc->status)
962
967
           or WEXITSTATUS(proc->status) != 0){
963
968
          /* Bad exit by plugin */
964
 
 
 
969
          
965
970
          if(debug){
966
971
            if(WIFEXITED(proc->status)){
967
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
970
975
                      WEXITSTATUS(proc->status));
971
976
            } else if(WIFSIGNALED(proc->status)){
972
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
973
 
                      " signal %d\n", proc->name,
 
978
                      " signal %d: %s\n", proc->name,
974
979
                      (intmax_t) (proc->pid),
975
 
                      WTERMSIG(proc->status));
 
980
                      WTERMSIG(proc->status),
 
981
                      strsignal(WTERMSIG(proc->status)));
976
982
            } else if(WCOREDUMP(proc->status)){
977
983
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
978
984
                      " core\n", proc->name, (intmax_t) (proc->pid));
981
987
          
982
988
          /* Remove the plugin */
983
989
          FD_CLR(proc->fd, &rfds_all);
984
 
 
 
990
          
985
991
          /* Block signal while modifying process_list */
986
992
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
987
993
          if(ret < 0){
1054
1060
      }
1055
1061
    }
1056
1062
  }
1057
 
 
1058
 
 
 
1063
  
 
1064
  
1059
1065
 fallback:
1060
1066
  
1061
1067
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){