/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

  • Committer: Teddy Hogeborn
  • Date: 2009-09-04 16:32:22 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090904163222-l9wp25ng1e5ym0yq
* plugin-runner.c (main): When a plugin is killed by a signal, show
                          the signal name, not just the number, in the
                          debug log message.

* plugins.d/password-prompt.c (termination_handler): Store received
                                                     signal in
                                                     "signal_received".
  (main): If exiting due to signal, re-raise signal received instead
          of returning.

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,
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;
279
282
  }
280
283
  free(plugin_node->environ);
281
284
  free(plugin_node->buffer);
282
 
 
 
285
  
283
286
  /* Removes the plugin from the singly-linked list */
284
287
  if(plugin_node == plugin_list){
285
288
    /* First one - simple */
312
315
  struct dirent *dirst;
313
316
  struct stat st;
314
317
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
 
318
  int ret, maxfd = 0;
316
319
  ssize_t sret;
317
320
  intmax_t tmpmax;
318
321
  uid_t uid = 65534;
379
382
  
380
383
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
384
                    struct argp_state *state){
 
385
    char *tmp;
382
386
    switch(key){
383
387
    case 'g':                   /* --global-options */
384
388
      if(arg != NULL){
455
459
      plugindir = strdup(arg);
456
460
      if(plugindir == NULL){
457
461
        perror("strdup");
458
 
      }      
 
462
      }
459
463
      break;
460
464
    case 129:                   /* --config-file */
461
465
      /* This is already done by parse_opt_config_file() */
462
466
      break;
463
467
    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'){
 
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){
467
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
473
                PRIdMAX "\n", arg, (intmax_t)uid);
469
474
      } else {
471
476
      }
472
477
      break;
473
478
    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'){
 
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){
477
483
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
484
                PRIdMAX "\n", arg, (intmax_t)gid);
479
485
      } else {
523
529
      if(argfile == NULL){
524
530
        perror("strdup");
525
531
      }
526
 
      break;      
 
532
      break;
527
533
    case 130:                   /* --userid */
528
534
    case 131:                   /* --groupid */
529
535
    case 132:                   /* --debug */
558
564
    conffp = fopen(AFILE, "r");
559
565
  } else {
560
566
    conffp = fopen(argfile, "r");
561
 
  }  
 
567
  }
562
568
  if(conffp != NULL){
563
569
    char *org_line = NULL;
564
570
    char *p, *arg, *new_arg, *line;
565
571
    size_t size = 0;
566
572
    const char whitespace_delims[] = " \r\t\f\v\n";
567
573
    const char comment_delim[] = "#";
568
 
 
 
574
    
569
575
    custom_argc = 1;
570
576
    custom_argv = malloc(sizeof(char*) * 2);
571
577
    if(custom_argv == NULL){
575
581
    }
576
582
    custom_argv[0] = argv[0];
577
583
    custom_argv[1] = NULL;
578
 
 
 
584
    
579
585
    /* for each line in the config file, strip whitespace and ignore
580
586
       commented text */
581
587
    while(true){
583
589
      if(sret == -1){
584
590
        break;
585
591
      }
586
 
 
 
592
      
587
593
      line = org_line;
588
594
      arg = strsep(&line, comment_delim);
589
595
      while((p = strsep(&arg, whitespace_delims)) != NULL){
608
614
          goto fallback;
609
615
        }
610
616
        custom_argv[custom_argc-1] = new_arg;
611
 
        custom_argv[custom_argc] = NULL;        
 
617
        custom_argv[custom_argc] = NULL;
612
618
      }
613
619
    }
614
620
    free(org_line);
752
758
        continue;
753
759
      }
754
760
    }
755
 
 
 
761
    
756
762
    char *filename;
757
763
    if(plugindir == NULL){
758
764
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
770
776
      free(filename);
771
777
      continue;
772
778
    }
773
 
 
 
779
    
774
780
    /* Ignore non-executable files */
775
781
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
776
782
      if(debug){
960
966
        if(not WIFEXITED(proc->status)
961
967
           or WEXITSTATUS(proc->status) != 0){
962
968
          /* Bad exit by plugin */
963
 
 
 
969
          
964
970
          if(debug){
965
971
            if(WIFEXITED(proc->status)){
966
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
969
975
                      WEXITSTATUS(proc->status));
970
976
            } else if(WIFSIGNALED(proc->status)){
971
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
 
978
                      " signal %d: %s\n", proc->name,
973
979
                      (intmax_t) (proc->pid),
974
 
                      WTERMSIG(proc->status));
 
980
                      WTERMSIG(proc->status),
 
981
                      strsignal(WTERMSIG(proc->status)));
975
982
            } else if(WCOREDUMP(proc->status)){
976
983
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
984
                      " core\n", proc->name, (intmax_t) (proc->pid));
980
987
          
981
988
          /* Remove the plugin */
982
989
          FD_CLR(proc->fd, &rfds_all);
983
 
 
 
990
          
984
991
          /* Block signal while modifying process_list */
985
992
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
986
993
          if(ret < 0){
1053
1060
      }
1054
1061
    }
1055
1062
  }
1056
 
 
1057
 
 
 
1063
  
 
1064
  
1058
1065
 fallback:
1059
1066
  
1060
1067
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){