/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 02:01:13 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209020113-726hq380zvp8zt97
Four new interrelated features:

1. Support using a different network interface via both initramfs.conf
   (the DEVICE setting) and the kernel command line (sixth field of
   the "ip=" option as in Linux' Documentation/nfsroot.txt).

2. Support connecting to a specified Mandos server directly using a
   kernel command line option ("mandos=connect:<ADDRESS>:<PORT>").

3. Support connecting directly to an IPv4 address (and port) using the
   "--connect" option of mandos-client.

4. Support an empty string to the --interface option to mandos-client.

* Makefile (WARN): Increase strictness by changing to
                   "-Wstrict-aliasing=1".

* debian/mandos-client.README.Debian (Use the Correct Network
  Interface): Changed to refer to initramfs.conf and nfsroot.txt.
  (Test the Server): Improve wording.
  (Non-local Connection): New section.
* initramfs-tools-script: Obey DEVICE environment variable and setting
                          from "/conf/initramfs.conf".  Also let any
                          "ip=" kernel command line option override
                          it.  Support new "mandos=connect" option.
                          Call "configure_networking" to set up IP
                          address on interface if necessary.
* plugin-runner.conf: Change example.
* plugins.d/mandos-client.c: Some whitespace and comment changes.
  (start_mandos_communication): Take an additional argument for
                                address family, all callers changed.
                                Connect to an IPv4 address if address
                                family is AF_INET.  Only set IPv6
                                scope_id for link-local addresses.
  (main): Accept empty interface name; this will not bring up any
         interface and leave the interface as unspecified.  Also do
         not restore kernel log level if lowering it failed.
* plugins.d/mandos-client.xml (OPTIONS): Document that the
                                         "--interface" option accepts
                                         an empty string.
  (EXAMPLE): Change example IPv6 address to a link-local address.

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(), 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,
64
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
65
63
                                   sigaddset(), sigaction(),
66
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
67
 
                                   SIG_UNBLOCK, kill(), sig_atomic_t
68
 
                                */
 
65
                                   SIG_UNBLOCK, kill() */
69
66
#include <errno.h>              /* errno, EBADF */
70
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
71
68
 
72
69
#define BUFFER_SIZE 256
73
70
 
84
81
  char **environ;
85
82
  int envc;
86
83
  bool disabled;
87
 
  
 
84
 
88
85
  /* Variables used for running processes*/
89
86
  pid_t pid;
90
87
  int fd;
282
279
  }
283
280
  free(plugin_node->environ);
284
281
  free(plugin_node->buffer);
285
 
  
 
282
 
286
283
  /* Removes the plugin from the singly-linked list */
287
284
  if(plugin_node == plugin_list){
288
285
    /* First one - simple */
315
312
  struct dirent *dirst;
316
313
  struct stat st;
317
314
  fd_set rfds_all;
318
 
  int ret, maxfd = 0;
 
315
  int ret, numchars, maxfd = 0;
319
316
  ssize_t sret;
320
317
  intmax_t tmpmax;
321
318
  uid_t uid = 65534;
382
379
  
383
380
  error_t parse_opt(int key, char *arg, __attribute__((unused))
384
381
                    struct argp_state *state){
385
 
    char *tmp;
386
382
    switch(key){
387
383
    case 'g':                   /* --global-options */
388
384
      if(arg != NULL){
389
 
        char *plugin_option;
390
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
391
 
          if(plugin_option[0] == '\0'){
 
385
        char *p;
 
386
        while((p = strsep(&arg, ",")) != NULL){
 
387
          if(p[0] == '\0'){
392
388
            continue;
393
389
          }
394
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
390
          if(not add_argument(getplugin(NULL), p)){
395
391
            perror("add_argument");
396
392
            return ARGP_ERR_UNKNOWN;
397
393
          }
408
404
      break;
409
405
    case 'o':                   /* --options-for */
410
406
      if(arg != NULL){
411
 
        char *plugin_name = strsep(&arg, ":");
412
 
        if(plugin_name[0] == '\0'){
413
 
          break;
414
 
        }
415
 
        char *plugin_option;
416
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
417
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
407
        char *p_name = strsep(&arg, ":");
 
408
        if(p_name[0] == '\0' or arg == NULL){
 
409
          break;
 
410
        }
 
411
        char *opt = strsep(&arg, ":");
 
412
        if(opt[0] == '\0' or opt == NULL){
 
413
          break;
 
414
        }
 
415
        char *p;
 
416
        while((p = strsep(&opt, ",")) != NULL){
 
417
          if(p[0] == '\0'){
 
418
            continue;
 
419
          }
 
420
          if(not add_argument(getplugin(p_name), p)){
418
421
            perror("add_argument");
419
422
            return ARGP_ERR_UNKNOWN;
420
423
          }
459
462
      plugindir = strdup(arg);
460
463
      if(plugindir == NULL){
461
464
        perror("strdup");
462
 
      }
 
465
      }      
463
466
      break;
464
467
    case 129:                   /* --config-file */
465
468
      /* This is already done by parse_opt_config_file() */
466
469
      break;
467
470
    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){
 
471
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
472
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
473
         or arg[numchars] != '\0'){
472
474
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
475
                PRIdMAX "\n", arg, (intmax_t)uid);
474
476
      } else {
476
478
      }
477
479
      break;
478
480
    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){
 
481
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
482
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
483
         or arg[numchars] != '\0'){
483
484
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
484
485
                PRIdMAX "\n", arg, (intmax_t)gid);
485
486
      } else {
529
530
      if(argfile == NULL){
530
531
        perror("strdup");
531
532
      }
532
 
      break;
 
533
      break;      
533
534
    case 130:                   /* --userid */
534
535
    case 131:                   /* --groupid */
535
536
    case 132:                   /* --debug */
564
565
    conffp = fopen(AFILE, "r");
565
566
  } else {
566
567
    conffp = fopen(argfile, "r");
567
 
  }
 
568
  }  
568
569
  if(conffp != NULL){
569
570
    char *org_line = NULL;
570
571
    char *p, *arg, *new_arg, *line;
571
572
    size_t size = 0;
572
573
    const char whitespace_delims[] = " \r\t\f\v\n";
573
574
    const char comment_delim[] = "#";
574
 
    
 
575
 
575
576
    custom_argc = 1;
576
577
    custom_argv = malloc(sizeof(char*) * 2);
577
578
    if(custom_argv == NULL){
581
582
    }
582
583
    custom_argv[0] = argv[0];
583
584
    custom_argv[1] = NULL;
584
 
    
 
585
 
585
586
    /* for each line in the config file, strip whitespace and ignore
586
587
       commented text */
587
588
    while(true){
589
590
      if(sret == -1){
590
591
        break;
591
592
      }
592
 
      
 
593
 
593
594
      line = org_line;
594
595
      arg = strsep(&line, comment_delim);
595
596
      while((p = strsep(&arg, whitespace_delims)) != NULL){
614
615
          goto fallback;
615
616
        }
616
617
        custom_argv[custom_argc-1] = new_arg;
617
 
        custom_argv[custom_argc] = NULL;
 
618
        custom_argv[custom_argc] = NULL;        
618
619
      }
619
620
    }
620
621
    free(org_line);
758
759
        continue;
759
760
      }
760
761
    }
761
 
    
 
762
 
762
763
    char *filename;
763
764
    if(plugindir == NULL){
764
765
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
776
777
      free(filename);
777
778
      continue;
778
779
    }
779
 
    
 
780
 
780
781
    /* Ignore non-executable files */
781
782
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
782
783
      if(debug){
935
936
  
936
937
  closedir(dir);
937
938
  dir = NULL;
938
 
  free_plugin(getplugin(NULL));
939
939
  
940
940
  for(plugin *p = plugin_list; p != NULL; p = p->next){
941
941
    if(p->pid != 0){
966
966
        if(not WIFEXITED(proc->status)
967
967
           or WEXITSTATUS(proc->status) != 0){
968
968
          /* Bad exit by plugin */
969
 
          
 
969
 
970
970
          if(debug){
971
971
            if(WIFEXITED(proc->status)){
972
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
975
975
                      WEXITSTATUS(proc->status));
976
976
            } else if(WIFSIGNALED(proc->status)){
977
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
 
                      " signal %d: %s\n", proc->name,
 
978
                      " signal %d\n", proc->name,
979
979
                      (intmax_t) (proc->pid),
980
 
                      WTERMSIG(proc->status),
981
 
                      strsignal(WTERMSIG(proc->status)));
 
980
                      WTERMSIG(proc->status));
982
981
            } else if(WCOREDUMP(proc->status)){
983
982
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
984
983
                      " core\n", proc->name, (intmax_t) (proc->pid));
987
986
          
988
987
          /* Remove the plugin */
989
988
          FD_CLR(proc->fd, &rfds_all);
990
 
          
 
989
 
991
990
          /* Block signal while modifying process_list */
992
991
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
993
992
          if(ret < 0){
1060
1059
      }
1061
1060
    }
1062
1061
  }
1063
 
  
1064
 
  
 
1062
 
 
1063
 
1065
1064
 fallback:
1066
1065
  
1067
1066
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){