/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Björn Påhlsson
  • Date: 2009-02-08 01:50:24 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: belorn@braxen-20090208015024-jewaulcqtxvbopq6
Fixed a bug in fallback handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
                                   SIG_UNBLOCK, kill() */
66
66
#include <errno.h>              /* errno, EBADF */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
67
68
 
68
69
#define BUFFER_SIZE 256
69
70
 
88
89
  size_t buffer_size;
89
90
  size_t buffer_length;
90
91
  bool eof;
91
 
  volatile bool completed;
92
 
  volatile int status;
 
92
  volatile sig_atomic_t completed;
 
93
  int status;
93
94
  struct plugin *next;
94
95
} plugin;
95
96
 
114
115
  if(name != NULL){
115
116
    copy_name = strdup(name);
116
117
    if(copy_name == NULL){
 
118
      free(new_plugin);
117
119
      return NULL;
118
120
    }
119
121
  }
120
122
  
121
 
  *new_plugin = (plugin) { .name = copy_name,
122
 
                           .argc = 1,
123
 
                           .disabled = false,
124
 
                           .next = plugin_list };
 
123
  *new_plugin = (plugin){ .name = copy_name,
 
124
                          .argc = 1,
 
125
                          .disabled = false,
 
126
                          .next = plugin_list };
125
127
  
126
128
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
129
  if(new_plugin->argv == NULL){
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
225
static void handle_sigchld(__attribute__((unused)) int sig){
 
226
  int old_errno = errno;
224
227
  while(true){
225
228
    plugin *proc = plugin_list;
226
229
    int status;
230
233
      break;
231
234
    }
232
235
    if(pid == -1){
233
 
      if(errno != ECHILD){
234
 
        perror("waitpid");
 
236
      if(errno == ECHILD){
 
237
        /* No child processes */
 
238
        break;
235
239
      }
236
 
      /* No child processes */
237
 
      break;
 
240
      perror("waitpid");
238
241
    }
239
242
    
240
243
    /* A child exited, find it in process_list */
246
249
      continue;
247
250
    }
248
251
    proc->status = status;
249
 
    proc->completed = true;
 
252
    proc->completed = 1;
250
253
  }
 
254
  errno = old_errno;
251
255
}
252
256
 
253
257
/* Prints out a password to stdout */
308
312
  struct dirent *dirst;
309
313
  struct stat st;
310
314
  fd_set rfds_all;
311
 
  int ret, maxfd = 0;
 
315
  int ret, numchars, maxfd = 0;
312
316
  ssize_t sret;
 
317
  intmax_t tmpmax;
313
318
  uid_t uid = 65534;
314
319
  gid_t gid = 65534;
315
320
  bool debug = false;
373
378
  };
374
379
  
375
380
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    switch(key) {
 
381
                    struct argp_state *state){
 
382
    switch(key){
378
383
    case 'g':                   /* --global-options */
379
384
      if(arg != NULL){
380
385
        char *p;
463
468
      /* This is already done by parse_opt_config_file() */
464
469
      break;
465
470
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%u", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
470
 
                uid);
 
471
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
472
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
473
         or arg[numchars] != '\0'){
 
474
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
475
                PRIdMAX "\n", arg, (intmax_t)uid);
 
476
      } else {
 
477
        uid = (uid_t)tmpmax;
471
478
      }
472
479
      break;
473
480
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%u", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
478
 
                arg, gid);
 
481
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
482
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
483
         or arg[numchars] != '\0'){
 
484
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
485
                PRIdMAX "\n", arg, (intmax_t)gid);
 
486
      } else {
 
487
        gid = (gid_t)tmpmax;
479
488
      }
480
489
      break;
481
490
    case 132:                   /* --debug */
505
514
     ignores everything but the --config-file option. */
506
515
  error_t parse_opt_config_file(int key, char *arg,
507
516
                                __attribute__((unused))
508
 
                                struct argp_state *state) {
509
 
    switch(key) {
 
517
                                struct argp_state *state){
 
518
    switch(key){
510
519
    case 'g':                   /* --global-options */
511
520
    case 'G':                   /* --global-env */
512
521
    case 'o':                   /* --options-for */
647
656
      for(char **a = p->argv; *a != NULL; a++){
648
657
        fprintf(stderr, "\tArg: %s\n", *a);
649
658
      }
650
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
659
      fprintf(stderr, "...and %d environment variables\n", p->envc);
651
660
      for(char **a = p->environ; *a != NULL; a++){
652
661
        fprintf(stderr, "\t%s\n", *a);
653
662
      }
655
664
  }
656
665
  
657
666
  /* Strip permissions down to nobody */
 
667
  setgid(gid);
 
668
  if(ret == -1){
 
669
    perror("setgid");
 
670
  }
658
671
  ret = setuid(uid);
659
672
  if(ret == -1){
660
673
    perror("setuid");
661
 
  }  
662
 
  setgid(gid);
663
 
  if(ret == -1){
664
 
    perror("setgid");
665
674
  }
666
675
  
667
676
  if(plugindir == NULL){
927
936
  
928
937
  closedir(dir);
929
938
  dir = NULL;
 
939
  free_plugin(getplugin(NULL));
930
940
  
931
941
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
942
    if(p->pid != 0){
952
962
       from one of them */
953
963
    for(plugin *proc = plugin_list; proc != NULL;){
954
964
      /* Is this process completely done? */
955
 
      if(proc->eof and proc->completed){
 
965
      if(proc->completed and proc->eof){
956
966
        /* Only accept the plugin output if it exited cleanly */
957
967
        if(not WIFEXITED(proc->status)
958
968
           or WEXITSTATUS(proc->status) != 0){
960
970
 
961
971
          if(debug){
962
972
            if(WIFEXITED(proc->status)){
963
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
964
 
                      (unsigned int) (proc->pid),
 
973
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
974
                      " %d\n", (intmax_t) (proc->pid),
965
975
                      WEXITSTATUS(proc->status));
966
 
            } else if(WIFSIGNALED(proc->status)) {
967
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
968
 
                      (unsigned int) (proc->pid),
 
976
            } else if(WIFSIGNALED(proc->status)){
 
977
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
978
                      " %d\n", (intmax_t) (proc->pid),
969
979
                      WTERMSIG(proc->status));
970
980
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
972
 
                      (unsigned int) (proc->pid));
 
981
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
982
                      (intmax_t) (proc->pid));
973
983
            }
974
984
          }
975
985