/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 01:46:42 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905014642-36108sqop1ci0z0m
* plugins.d/mandos-client.c (start_mandos_communication): Check
                                                         "quit_now"
                                                         throughout.
                                                         Always close
                                                         socket and
                                                         deinit GnuTLS
                                                         properly.
  (browse_callback): Return immediately if "quit_now" is set.

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;
89
92
  size_t buffer_size;
90
93
  size_t buffer_length;
91
94
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
 
95
  volatile sig_atomic_t completed;
 
96
  int status;
94
97
  struct plugin *next;
95
98
} plugin;
96
99
 
115
118
  if(name != NULL){
116
119
    copy_name = strdup(name);
117
120
    if(copy_name == NULL){
 
121
      free(new_plugin);
118
122
      return NULL;
119
123
    }
120
124
  }
121
125
  
122
 
  *new_plugin = (plugin) { .name = copy_name,
123
 
                           .argc = 1,
124
 
                           .disabled = false,
125
 
                           .next = plugin_list };
 
126
  *new_plugin = (plugin){ .name = copy_name,
 
127
                          .argc = 1,
 
128
                          .disabled = false,
 
129
                          .next = plugin_list };
126
130
  
127
131
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
132
  if(new_plugin->argv == NULL){
222
226
/* Mark processes as completed when they exit, and save their exit
223
227
   status. */
224
228
static void handle_sigchld(__attribute__((unused)) int sig){
 
229
  int old_errno = errno;
225
230
  while(true){
226
231
    plugin *proc = plugin_list;
227
232
    int status;
231
236
      break;
232
237
    }
233
238
    if(pid == -1){
234
 
      if(errno != ECHILD){
235
 
        perror("waitpid");
 
239
      if(errno == ECHILD){
 
240
        /* No child processes */
 
241
        break;
236
242
      }
237
 
      /* No child processes */
238
 
      break;
 
243
      perror("waitpid");
239
244
    }
240
245
    
241
246
    /* A child exited, find it in process_list */
247
252
      continue;
248
253
    }
249
254
    proc->status = status;
250
 
    proc->completed = true;
 
255
    proc->completed = 1;
251
256
  }
 
257
  errno = old_errno;
252
258
}
253
259
 
254
260
/* Prints out a password to stdout */
276
282
  }
277
283
  free(plugin_node->environ);
278
284
  free(plugin_node->buffer);
279
 
 
 
285
  
280
286
  /* Removes the plugin from the singly-linked list */
281
287
  if(plugin_node == plugin_list){
282
288
    /* First one - simple */
309
315
  struct dirent *dirst;
310
316
  struct stat st;
311
317
  fd_set rfds_all;
312
 
  int ret, numchars, maxfd = 0;
 
318
  int ret, maxfd = 0;
313
319
  ssize_t sret;
314
320
  intmax_t tmpmax;
315
321
  uid_t uid = 65534;
375
381
  };
376
382
  
377
383
  error_t parse_opt(int key, char *arg, __attribute__((unused))
378
 
                    struct argp_state *state) {
379
 
    switch(key) {
 
384
                    struct argp_state *state){
 
385
    char *tmp;
 
386
    switch(key){
380
387
    case 'g':                   /* --global-options */
381
388
      if(arg != NULL){
382
 
        char *p;
383
 
        while((p = strsep(&arg, ",")) != NULL){
384
 
          if(p[0] == '\0'){
 
389
        char *plugin_option;
 
390
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
391
          if(plugin_option[0] == '\0'){
385
392
            continue;
386
393
          }
387
 
          if(not add_argument(getplugin(NULL), p)){
 
394
          if(not add_argument(getplugin(NULL), plugin_option)){
388
395
            perror("add_argument");
389
396
            return ARGP_ERR_UNKNOWN;
390
397
          }
401
408
      break;
402
409
    case 'o':                   /* --options-for */
403
410
      if(arg != NULL){
404
 
        char *p_name = strsep(&arg, ":");
405
 
        if(p_name[0] == '\0' or arg == NULL){
406
 
          break;
407
 
        }
408
 
        char *opt = strsep(&arg, ":");
409
 
        if(opt[0] == '\0' or opt == NULL){
410
 
          break;
411
 
        }
412
 
        char *p;
413
 
        while((p = strsep(&opt, ",")) != NULL){
414
 
          if(p[0] == '\0'){
415
 
            continue;
416
 
          }
417
 
          if(not add_argument(getplugin(p_name), p)){
 
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)){
418
418
            perror("add_argument");
419
419
            return ARGP_ERR_UNKNOWN;
420
420
          }
459
459
      plugindir = strdup(arg);
460
460
      if(plugindir == NULL){
461
461
        perror("strdup");
462
 
      }      
 
462
      }
463
463
      break;
464
464
    case 129:                   /* --config-file */
465
465
      /* This is already done by parse_opt_config_file() */
466
466
      break;
467
467
    case 130:                   /* --userid */
468
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
469
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
470
 
         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){
471
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
472
473
                PRIdMAX "\n", arg, (intmax_t)uid);
473
474
      } else {
475
476
      }
476
477
      break;
477
478
    case 131:                   /* --groupid */
478
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
479
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
480
 
         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){
481
483
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
482
484
                PRIdMAX "\n", arg, (intmax_t)gid);
483
485
      } else {
511
513
     ignores everything but the --config-file option. */
512
514
  error_t parse_opt_config_file(int key, char *arg,
513
515
                                __attribute__((unused))
514
 
                                struct argp_state *state) {
515
 
    switch(key) {
 
516
                                struct argp_state *state){
 
517
    switch(key){
516
518
    case 'g':                   /* --global-options */
517
519
    case 'G':                   /* --global-env */
518
520
    case 'o':                   /* --options-for */
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
    }
618
620
    free(org_line);
661
663
  }
662
664
  
663
665
  /* Strip permissions down to nobody */
 
666
  setgid(gid);
 
667
  if(ret == -1){
 
668
    perror("setgid");
 
669
  }
664
670
  ret = setuid(uid);
665
671
  if(ret == -1){
666
672
    perror("setuid");
667
 
  }  
668
 
  setgid(gid);
669
 
  if(ret == -1){
670
 
    perror("setgid");
671
673
  }
672
674
  
673
675
  if(plugindir == NULL){
756
758
        continue;
757
759
      }
758
760
    }
759
 
 
 
761
    
760
762
    char *filename;
761
763
    if(plugindir == NULL){
762
764
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
774
776
      free(filename);
775
777
      continue;
776
778
    }
777
 
 
 
779
    
778
780
    /* Ignore non-executable files */
779
781
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
780
782
      if(debug){
933
935
  
934
936
  closedir(dir);
935
937
  dir = NULL;
 
938
  free_plugin(getplugin(NULL));
936
939
  
937
940
  for(plugin *p = plugin_list; p != NULL; p = p->next){
938
941
    if(p->pid != 0){
958
961
       from one of them */
959
962
    for(plugin *proc = plugin_list; proc != NULL;){
960
963
      /* Is this process completely done? */
961
 
      if(proc->eof and proc->completed){
 
964
      if(proc->completed and proc->eof){
962
965
        /* Only accept the plugin output if it exited cleanly */
963
966
        if(not WIFEXITED(proc->status)
964
967
           or WEXITSTATUS(proc->status) != 0){
965
968
          /* Bad exit by plugin */
966
 
 
 
969
          
967
970
          if(debug){
968
971
            if(WIFEXITED(proc->status)){
969
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
970
 
                      " %d\n", (intmax_t) (proc->pid),
 
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
973
                      " status %d\n", proc->name,
 
974
                      (intmax_t) (proc->pid),
971
975
                      WEXITSTATUS(proc->status));
972
 
            } else if(WIFSIGNALED(proc->status)) {
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
974
 
                      " %d\n", (intmax_t) (proc->pid),
975
 
                      WTERMSIG(proc->status));
 
976
            } else if(WIFSIGNALED(proc->status)){
 
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
978
                      " signal %d: %s\n", proc->name,
 
979
                      (intmax_t) (proc->pid),
 
980
                      WTERMSIG(proc->status),
 
981
                      strsignal(WTERMSIG(proc->status)));
976
982
            } else if(WCOREDUMP(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
978
 
                      (intmax_t) (proc->pid));
 
983
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
984
                      " core\n", proc->name, (intmax_t) (proc->pid));
979
985
            }
980
986
          }
981
987
          
982
988
          /* Remove the plugin */
983
989
          FD_CLR(proc->fd, &rfds_all);
984
 
 
 
990
          
985
991
          /* Block signal while modifying process_list */
986
992
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
987
993
          if(ret < 0){
1054
1060
      }
1055
1061
    }
1056
1062
  }
1057
 
 
1058
 
 
 
1063
  
 
1064
  
1059
1065
 fallback:
1060
1066
  
1061
1067
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){