/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-12 06:09:24 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090212060924-vwxgitxnwr14w9e4
* mandos (Client.start_checker): Bug fix: Add extra check in case the
                                 checker process completed quickly.

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
68
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
69
 
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
 
115
116
  if(name != NULL){
116
117
    copy_name = strdup(name);
117
118
    if(copy_name == NULL){
 
119
      free(new_plugin);
118
120
      return NULL;
119
121
    }
120
122
  }
121
123
  
122
 
  *new_plugin = (plugin) { .name = copy_name,
123
 
                           .argc = 1,
124
 
                           .disabled = false,
125
 
                           .next = plugin_list };
 
124
  *new_plugin = (plugin){ .name = copy_name,
 
125
                          .argc = 1,
 
126
                          .disabled = false,
 
127
                          .next = plugin_list };
126
128
  
127
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
130
  if(new_plugin->argv == NULL){
222
224
/* Mark processes as completed when they exit, and save their exit
223
225
   status. */
224
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
225
228
  while(true){
226
229
    plugin *proc = plugin_list;
227
230
    int status;
231
234
      break;
232
235
    }
233
236
    if(pid == -1){
234
 
      if(errno != ECHILD){
235
 
        perror("waitpid");
 
237
      if(errno == ECHILD){
 
238
        /* No child processes */
 
239
        break;
236
240
      }
237
 
      /* No child processes */
238
 
      break;
 
241
      perror("waitpid");
239
242
    }
240
243
    
241
244
    /* A child exited, find it in process_list */
247
250
      continue;
248
251
    }
249
252
    proc->status = status;
250
 
    proc->completed = true;
 
253
    proc->completed = 1;
251
254
  }
 
255
  errno = old_errno;
252
256
}
253
257
 
254
258
/* Prints out a password to stdout */
375
379
  };
376
380
  
377
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
378
 
                    struct argp_state *state) {
379
 
    switch(key) {
 
382
                    struct argp_state *state){
 
383
    switch(key){
380
384
    case 'g':                   /* --global-options */
381
385
      if(arg != NULL){
382
 
        char *p;
383
 
        while((p = strsep(&arg, ",")) != NULL){
384
 
          if(p[0] == '\0'){
 
386
        char *plugin_option;
 
387
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
388
          if(plugin_option[0] == '\0'){
385
389
            continue;
386
390
          }
387
 
          if(not add_argument(getplugin(NULL), p)){
 
391
          if(not add_argument(getplugin(NULL), plugin_option)){
388
392
            perror("add_argument");
389
393
            return ARGP_ERR_UNKNOWN;
390
394
          }
401
405
      break;
402
406
    case 'o':                   /* --options-for */
403
407
      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)){
 
408
        char *plugin_name = strsep(&arg, ":");
 
409
        if(plugin_name[0] == '\0'){
 
410
          break;
 
411
        }
 
412
        char *plugin_option;
 
413
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
414
          if(not add_argument(getplugin(plugin_name), plugin_option)){
418
415
            perror("add_argument");
419
416
            return ARGP_ERR_UNKNOWN;
420
417
          }
511
508
     ignores everything but the --config-file option. */
512
509
  error_t parse_opt_config_file(int key, char *arg,
513
510
                                __attribute__((unused))
514
 
                                struct argp_state *state) {
515
 
    switch(key) {
 
511
                                struct argp_state *state){
 
512
    switch(key){
516
513
    case 'g':                   /* --global-options */
517
514
    case 'G':                   /* --global-env */
518
515
    case 'o':                   /* --options-for */
661
658
  }
662
659
  
663
660
  /* Strip permissions down to nobody */
 
661
  setgid(gid);
 
662
  if(ret == -1){
 
663
    perror("setgid");
 
664
  }
664
665
  ret = setuid(uid);
665
666
  if(ret == -1){
666
667
    perror("setuid");
667
 
  }  
668
 
  setgid(gid);
669
 
  if(ret == -1){
670
 
    perror("setgid");
671
668
  }
672
669
  
673
670
  if(plugindir == NULL){
933
930
  
934
931
  closedir(dir);
935
932
  dir = NULL;
 
933
  free_plugin(getplugin(NULL));
936
934
  
937
935
  for(plugin *p = plugin_list; p != NULL; p = p->next){
938
936
    if(p->pid != 0){
958
956
       from one of them */
959
957
    for(plugin *proc = plugin_list; proc != NULL;){
960
958
      /* Is this process completely done? */
961
 
      if(proc->eof and proc->completed){
 
959
      if(proc->completed and proc->eof){
962
960
        /* Only accept the plugin output if it exited cleanly */
963
961
        if(not WIFEXITED(proc->status)
964
962
           or WEXITSTATUS(proc->status) != 0){
966
964
 
967
965
          if(debug){
968
966
            if(WIFEXITED(proc->status)){
969
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
970
 
                      " %d\n", (intmax_t) (proc->pid),
 
967
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
968
                      " status %d\n", proc->name,
 
969
                      (intmax_t) (proc->pid),
971
970
                      WEXITSTATUS(proc->status));
972
 
            } else if(WIFSIGNALED(proc->status)) {
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
974
 
                      " %d\n", (intmax_t) (proc->pid),
 
971
            } else if(WIFSIGNALED(proc->status)){
 
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
973
                      " signal %d\n", proc->name,
 
974
                      (intmax_t) (proc->pid),
975
975
                      WTERMSIG(proc->status));
976
976
            } else if(WCOREDUMP(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
978
 
                      (intmax_t) (proc->pid));
 
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
978
                      " core\n", proc->name, (intmax_t) (proc->pid));
979
979
            }
980
980
          }
981
981