/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

Merge from trunk.  Notable changes:

 1. Server package now depends on "python-gobject".
 2. Permission fix for /lib64.
 3. Support for DEVICE setting from initramfs.conf, kernel parameters
    "ip=" and "mandos=connect".
 4. Fix for the bug where the server would stop responding, with a
    zombie checker process.
 5. Add support for disabling IPv6 in the server
 6. Fix for the bug which made the server, plugin-runner and
    mandos-client fail to change group ID.
 7. Add GnuTLS debugging to server debug output.
 8. Fix for the bug of the "--options-for" option of plugin-runner,
    where it would cut the value at the first colon character.
 9. Stop using sscanf() throughout, since it does not detect overflow.
10. Fix for the bug where plugin-runner would not go to the fallback
    if all plugins failed.
11. Fix for the bug where mandos-client would not clean up after a
    signal.
12. Added support for connecting to IPv4 addresses in mandos-client.
13. Added support for not using a specific network interface in
    mandos-client.
14. Kernel log level will be lowered by mandos-client while bringing
    up the network interface.
15. Add an option for the maximum time for mandos-client to wait for
    the network interface to come up.
16. Fix for the bug where mandos-client would not clean the temporary
    directory on some filesystems.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
63
                                   sigaddset(), sigaction(),
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
66
67
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
 
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
68
69
 
69
70
#define BUFFER_SIZE 256
70
71
 
81
82
  char **environ;
82
83
  int envc;
83
84
  bool disabled;
84
 
 
 
85
  
85
86
  /* Variables used for running processes*/
86
87
  pid_t pid;
87
88
  int fd;
89
90
  size_t buffer_size;
90
91
  size_t buffer_length;
91
92
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
 
93
  volatile sig_atomic_t completed;
 
94
  int status;
94
95
  struct plugin *next;
95
96
} plugin;
96
97
 
120
121
    }
121
122
  }
122
123
  
123
 
  *new_plugin = (plugin) { .name = copy_name,
124
 
                           .argc = 1,
125
 
                           .disabled = false,
126
 
                           .next = plugin_list };
 
124
  *new_plugin = (plugin){ .name = copy_name,
 
125
                          .argc = 1,
 
126
                          .disabled = false,
 
127
                          .next = plugin_list };
127
128
  
128
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
130
  if(new_plugin->argv == NULL){
223
224
/* Mark processes as completed when they exit, and save their exit
224
225
   status. */
225
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
226
228
  while(true){
227
229
    plugin *proc = plugin_list;
228
230
    int status;
232
234
      break;
233
235
    }
234
236
    if(pid == -1){
235
 
      if(errno != ECHILD){
236
 
        perror("waitpid");
 
237
      if(errno == ECHILD){
 
238
        /* No child processes */
 
239
        break;
237
240
      }
238
 
      /* No child processes */
239
 
      break;
 
241
      perror("waitpid");
240
242
    }
241
243
    
242
244
    /* A child exited, find it in process_list */
248
250
      continue;
249
251
    }
250
252
    proc->status = status;
251
 
    proc->completed = true;
 
253
    proc->completed = 1;
252
254
  }
 
255
  errno = old_errno;
253
256
}
254
257
 
255
258
/* Prints out a password to stdout */
277
280
  }
278
281
  free(plugin_node->environ);
279
282
  free(plugin_node->buffer);
280
 
 
 
283
  
281
284
  /* Removes the plugin from the singly-linked list */
282
285
  if(plugin_node == plugin_list){
283
286
    /* First one - simple */
310
313
  struct dirent *dirst;
311
314
  struct stat st;
312
315
  fd_set rfds_all;
313
 
  int ret, numchars, maxfd = 0;
 
316
  int ret, maxfd = 0;
314
317
  ssize_t sret;
315
318
  intmax_t tmpmax;
316
319
  uid_t uid = 65534;
376
379
  };
377
380
  
378
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
379
 
                    struct argp_state *state) {
380
 
    switch(key) {
 
382
                    struct argp_state *state){
 
383
    char *tmp;
 
384
    switch(key){
381
385
    case 'g':                   /* --global-options */
382
386
      if(arg != NULL){
383
 
        char *p;
384
 
        while((p = strsep(&arg, ",")) != NULL){
385
 
          if(p[0] == '\0'){
 
387
        char *plugin_option;
 
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
386
390
            continue;
387
391
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
 
392
          if(not add_argument(getplugin(NULL), plugin_option)){
389
393
            perror("add_argument");
390
394
            return ARGP_ERR_UNKNOWN;
391
395
          }
402
406
      break;
403
407
    case 'o':                   /* --options-for */
404
408
      if(arg != NULL){
405
 
        char *p_name = strsep(&arg, ":");
406
 
        if(p_name[0] == '\0' or arg == NULL){
407
 
          break;
408
 
        }
409
 
        char *opt = strsep(&arg, ":");
410
 
        if(opt[0] == '\0' or opt == NULL){
411
 
          break;
412
 
        }
413
 
        char *p;
414
 
        while((p = strsep(&opt, ",")) != NULL){
415
 
          if(p[0] == '\0'){
416
 
            continue;
417
 
          }
418
 
          if(not add_argument(getplugin(p_name), p)){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
419
416
            perror("add_argument");
420
417
            return ARGP_ERR_UNKNOWN;
421
418
          }
466
463
      /* This is already done by parse_opt_config_file() */
467
464
      break;
468
465
    case 130:                   /* --userid */
469
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
470
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
471
 
         or arg[numchars] != '\0'){
 
466
      errno = 0;
 
467
      tmpmax = strtoimax(arg, &tmp, 10);
 
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
469
         or tmpmax != (uid_t)tmpmax){
472
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
471
                PRIdMAX "\n", arg, (intmax_t)uid);
474
472
      } else {
476
474
      }
477
475
      break;
478
476
    case 131:                   /* --groupid */
479
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
480
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
481
 
         or arg[numchars] != '\0'){
 
477
      errno = 0;
 
478
      tmpmax = strtoimax(arg, &tmp, 10);
 
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
480
         or tmpmax != (gid_t)tmpmax){
482
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
483
482
                PRIdMAX "\n", arg, (intmax_t)gid);
484
483
      } else {
512
511
     ignores everything but the --config-file option. */
513
512
  error_t parse_opt_config_file(int key, char *arg,
514
513
                                __attribute__((unused))
515
 
                                struct argp_state *state) {
516
 
    switch(key) {
 
514
                                struct argp_state *state){
 
515
    switch(key){
517
516
    case 'g':                   /* --global-options */
518
517
    case 'G':                   /* --global-env */
519
518
    case 'o':                   /* --options-for */
570
569
    size_t size = 0;
571
570
    const char whitespace_delims[] = " \r\t\f\v\n";
572
571
    const char comment_delim[] = "#";
573
 
 
 
572
    
574
573
    custom_argc = 1;
575
574
    custom_argv = malloc(sizeof(char*) * 2);
576
575
    if(custom_argv == NULL){
580
579
    }
581
580
    custom_argv[0] = argv[0];
582
581
    custom_argv[1] = NULL;
583
 
 
 
582
    
584
583
    /* for each line in the config file, strip whitespace and ignore
585
584
       commented text */
586
585
    while(true){
588
587
      if(sret == -1){
589
588
        break;
590
589
      }
591
 
 
 
590
      
592
591
      line = org_line;
593
592
      arg = strsep(&line, comment_delim);
594
593
      while((p = strsep(&arg, whitespace_delims)) != NULL){
662
661
  }
663
662
  
664
663
  /* Strip permissions down to nobody */
 
664
  setgid(gid);
 
665
  if(ret == -1){
 
666
    perror("setgid");
 
667
  }
665
668
  ret = setuid(uid);
666
669
  if(ret == -1){
667
670
    perror("setuid");
668
 
  }  
669
 
  setgid(gid);
670
 
  if(ret == -1){
671
 
    perror("setgid");
672
671
  }
673
672
  
674
673
  if(plugindir == NULL){
757
756
        continue;
758
757
      }
759
758
    }
760
 
 
 
759
    
761
760
    char *filename;
762
761
    if(plugindir == NULL){
763
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
775
774
      free(filename);
776
775
      continue;
777
776
    }
778
 
 
 
777
    
779
778
    /* Ignore non-executable files */
780
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
781
780
      if(debug){
934
933
  
935
934
  closedir(dir);
936
935
  dir = NULL;
 
936
  free_plugin(getplugin(NULL));
937
937
  
938
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
939
939
    if(p->pid != 0){
959
959
       from one of them */
960
960
    for(plugin *proc = plugin_list; proc != NULL;){
961
961
      /* Is this process completely done? */
962
 
      if(proc->eof and proc->completed){
 
962
      if(proc->completed and proc->eof){
963
963
        /* Only accept the plugin output if it exited cleanly */
964
964
        if(not WIFEXITED(proc->status)
965
965
           or WEXITSTATUS(proc->status) != 0){
966
966
          /* Bad exit by plugin */
967
 
 
 
967
          
968
968
          if(debug){
969
969
            if(WIFEXITED(proc->status)){
970
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
971
 
                      " %d\n", (intmax_t) (proc->pid),
 
970
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
971
                      " status %d\n", proc->name,
 
972
                      (intmax_t) (proc->pid),
972
973
                      WEXITSTATUS(proc->status));
973
 
            } else if(WIFSIGNALED(proc->status)) {
974
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
975
 
                      " %d\n", (intmax_t) (proc->pid),
 
974
            } else if(WIFSIGNALED(proc->status)){
 
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
976
                      " signal %d\n", proc->name,
 
977
                      (intmax_t) (proc->pid),
976
978
                      WTERMSIG(proc->status));
977
979
            } else if(WCOREDUMP(proc->status)){
978
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
979
 
                      (intmax_t) (proc->pid));
 
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
980
982
            }
981
983
          }
982
984
          
983
985
          /* Remove the plugin */
984
986
          FD_CLR(proc->fd, &rfds_all);
985
 
 
 
987
          
986
988
          /* Block signal while modifying process_list */
987
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
988
990
          if(ret < 0){
1055
1057
      }
1056
1058
    }
1057
1059
  }
1058
 
 
1059
 
 
 
1060
  
 
1061
  
1060
1062
 fallback:
1061
1063
  
1062
1064
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){