/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-05 17:40:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905174018-3w6q5uqk8ti5fqq6
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
                                          innermost scope possible.
  (start_mandos_communication): Move variable "decrypted_buffer_size"
                                into the innermost scope 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,
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 */
315
317
  fd_set rfds_all;
316
318
  int ret, maxfd = 0;
317
319
  ssize_t sret;
318
 
  intmax_t tmpmax;
319
320
  uid_t uid = 65534;
320
321
  gid_t gid = 65534;
321
322
  bool debug = false;
380
381
  
381
382
  error_t parse_opt(int key, char *arg, __attribute__((unused))
382
383
                    struct argp_state *state){
383
 
    char *tmp;
384
384
    switch(key){
 
385
      char *tmp;
 
386
      intmax_t tmpmax;
385
387
    case 'g':                   /* --global-options */
386
388
      if(arg != NULL){
387
389
        char *plugin_option;
457
459
      plugindir = strdup(arg);
458
460
      if(plugindir == NULL){
459
461
        perror("strdup");
460
 
      }      
 
462
      }
461
463
      break;
462
464
    case 129:                   /* --config-file */
463
465
      /* This is already done by parse_opt_config_file() */
527
529
      if(argfile == NULL){
528
530
        perror("strdup");
529
531
      }
530
 
      break;      
 
532
      break;
531
533
    case 130:                   /* --userid */
532
534
    case 131:                   /* --groupid */
533
535
    case 132:                   /* --debug */
562
564
    conffp = fopen(AFILE, "r");
563
565
  } else {
564
566
    conffp = fopen(argfile, "r");
565
 
  }  
 
567
  }
566
568
  if(conffp != NULL){
567
569
    char *org_line = NULL;
568
570
    char *p, *arg, *new_arg, *line;
569
571
    size_t size = 0;
570
572
    const char whitespace_delims[] = " \r\t\f\v\n";
571
573
    const char comment_delim[] = "#";
572
 
 
 
574
    
573
575
    custom_argc = 1;
574
576
    custom_argv = malloc(sizeof(char*) * 2);
575
577
    if(custom_argv == NULL){
579
581
    }
580
582
    custom_argv[0] = argv[0];
581
583
    custom_argv[1] = NULL;
582
 
 
 
584
    
583
585
    /* for each line in the config file, strip whitespace and ignore
584
586
       commented text */
585
587
    while(true){
587
589
      if(sret == -1){
588
590
        break;
589
591
      }
590
 
 
 
592
      
591
593
      line = org_line;
592
594
      arg = strsep(&line, comment_delim);
593
595
      while((p = strsep(&arg, whitespace_delims)) != NULL){
612
614
          goto fallback;
613
615
        }
614
616
        custom_argv[custom_argc-1] = new_arg;
615
 
        custom_argv[custom_argc] = NULL;        
 
617
        custom_argv[custom_argc] = NULL;
616
618
      }
617
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
    }
618
628
    free(org_line);
619
629
  } else {
620
630
    /* Check for harmful errors and go to fallback. Other errors might
756
766
        continue;
757
767
      }
758
768
    }
759
 
 
 
769
    
760
770
    char *filename;
761
771
    if(plugindir == NULL){
762
772
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
774
784
      free(filename);
775
785
      continue;
776
786
    }
777
 
 
 
787
    
778
788
    /* Ignore non-executable files */
779
789
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
780
790
      if(debug){
964
974
        if(not WIFEXITED(proc->status)
965
975
           or WEXITSTATUS(proc->status) != 0){
966
976
          /* Bad exit by plugin */
967
 
 
 
977
          
968
978
          if(debug){
969
979
            if(WIFEXITED(proc->status)){
970
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
973
983
                      WEXITSTATUS(proc->status));
974
984
            } else if(WIFSIGNALED(proc->status)){
975
985
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
976
 
                      " signal %d\n", proc->name,
 
986
                      " signal %d: %s\n", proc->name,
977
987
                      (intmax_t) (proc->pid),
978
 
                      WTERMSIG(proc->status));
 
988
                      WTERMSIG(proc->status),
 
989
                      strsignal(WTERMSIG(proc->status)));
979
990
            } else if(WCOREDUMP(proc->status)){
980
991
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
981
992
                      " core\n", proc->name, (intmax_t) (proc->pid));
984
995
          
985
996
          /* Remove the plugin */
986
997
          FD_CLR(proc->fd, &rfds_all);
987
 
 
 
998
          
988
999
          /* Block signal while modifying process_list */
989
1000
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
990
1001
          if(ret < 0){
1057
1068
      }
1058
1069
    }
1059
1070
  }
1060
 
 
1061
 
 
 
1071
  
 
1072
  
1062
1073
 fallback:
1063
1074
  
1064
1075
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){