/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-02-09 19:26:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209192619-7rse82x9pypq4ihk
* plugin-runner.c: Comment change.

* plugins.d/mandos-client.c: Comment changes.
  (quit_now): New global flag.
  (handle_sigterm): Only call avahi_simple_poll_quit once.
  (main): Also handle SIGINT and SIGHUP.

Show diffs side-by-side

added added

removed removed

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