/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

* debian/rules (install-indep): Removed "--no-start" from
                                dh_installinit.

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