/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-01-15 03:35:34 UTC
  • mfrom: (24.1.122 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090115033534-wcldj74x0jgvv9o0
Merged newer version of mandos-list (now renamed mandos-ctl) from
Björn.

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