/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-13 04:35:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113043519-0yr39snphoegq8l1
* plugin-runner.c: Only space changes.
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
   or if none is found, creates a new one */
100
100
static plugin *getplugin(char *name){
101
101
  /* Check for exiting plugin with that name */
102
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if ((p->name == name)
104
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
102
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
103
    if((p->name == name)
 
104
       or (p->name and name and (strcmp(p->name, name) == 0))){
105
105
      return p;
106
106
    }
107
107
  }
108
108
  /* Create a new plugin */
109
109
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
110
  if(new_plugin == NULL){
111
111
    return NULL;
112
112
  }
113
113
  char *copy_name = NULL;
124
124
                           .next = plugin_list };
125
125
  
126
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
127
  if(new_plugin->argv == NULL){
128
128
    free(copy_name);
129
129
    free(new_plugin);
130
130
    return NULL;
230
230
      break;
231
231
    }
232
232
    if(pid == -1){
233
 
      if (errno != ECHILD){
 
233
      if(errno != ECHILD){
234
234
        perror("waitpid");
235
235
      }
236
236
      /* No child processes */
372
372
    { .name = NULL }
373
373
  };
374
374
  
375
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
376
 
                     struct argp_state *state) {
377
 
    switch (key) {
 
375
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
376
                    struct argp_state *state) {
 
377
    switch(key) {
378
378
    case 'g':                   /* --global-options */
379
 
      if (arg != NULL){
 
379
      if(arg != NULL){
380
380
        char *p;
381
381
        while((p = strsep(&arg, ",")) != NULL){
382
382
          if(p[0] == '\0'){
398
398
      }
399
399
      break;
400
400
    case 'o':                   /* --options-for */
401
 
      if (arg != NULL){
 
401
      if(arg != NULL){
402
402
        char *p_name = strsep(&arg, ":");
403
403
        if(p_name[0] == '\0' or arg == NULL){
404
404
          break;
435
435
      }
436
436
      break;
437
437
    case 'd':                   /* --disable */
438
 
      if (arg != NULL){
 
438
      if(arg != NULL){
439
439
        plugin *p = getplugin(arg);
440
440
        if(p == NULL){
441
441
          return ARGP_ERR_UNKNOWN;
444
444
      }
445
445
      break;
446
446
    case 'e':                   /* --enable */
447
 
      if (arg != NULL){
 
447
      if(arg != NULL){
448
448
        plugin *p = getplugin(arg);
449
449
        if(p == NULL){
450
450
          return ARGP_ERR_UNKNOWN;
503
503
  
504
504
  /* This option parser is the same as parse_opt() above, except it
505
505
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file (int key, char *arg,
507
 
                                 __attribute__((unused))
508
 
                                 struct argp_state *state) {
509
 
    switch (key) {
 
506
  error_t parse_opt_config_file(int key, char *arg,
 
507
                                __attribute__((unused))
 
508
                                struct argp_state *state) {
 
509
    switch(key) {
510
510
    case 'g':                   /* --global-options */
511
511
    case 'G':                   /* --global-env */
512
512
    case 'o':                   /* --options-for */
541
541
  
542
542
  /* Parse using parse_opt_config_file() in order to get the custom
543
543
     config file location, if any. */
544
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if (ret == ARGP_ERR_UNKNOWN){
 
544
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
545
  if(ret == ARGP_ERR_UNKNOWN){
546
546
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
547
    exitstatus = EXIT_FAILURE;
548
548
    goto fallback;
552
552
  argp.parser = parse_opt;
553
553
  
554
554
  /* Open the configfile if available */
555
 
  if (argfile == NULL){
 
555
  if(argfile == NULL){
556
556
    conffp = fopen(AFILE, "r");
557
557
  } else {
558
558
    conffp = fopen(argfile, "r");
613
613
  } else {
614
614
    /* Check for harmful errors and go to fallback. Other errors might
615
615
       not affect opening plugins */
616
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
616
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
617
      perror("fopen");
618
618
      exitstatus = EXIT_FAILURE;
619
619
      goto fallback;
622
622
  /* If there was any arguments from configuration file,
623
623
     pass them to parser as command arguments */
624
624
  if(custom_argv != NULL){
625
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                      0, NULL);
627
 
    if (ret == ARGP_ERR_UNKNOWN){
 
625
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
626
                     0, NULL);
 
627
    if(ret == ARGP_ERR_UNKNOWN){
628
628
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
629
      exitstatus = EXIT_FAILURE;
630
630
      goto fallback;
633
633
  
634
634
  /* Parse actual command line arguments, to let them override the
635
635
     config file */
636
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if (ret == ARGP_ERR_UNKNOWN){
 
636
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
637
  if(ret == ARGP_ERR_UNKNOWN){
638
638
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
639
    exitstatus = EXIT_FAILURE;
640
640
    goto fallback;
656
656
  
657
657
  /* Strip permissions down to nobody */
658
658
  ret = setuid(uid);
659
 
  if (ret == -1){
 
659
  if(ret == -1){
660
660
    perror("setuid");
661
661
  }  
662
662
  setgid(gid);
663
 
  if (ret == -1){
 
663
  if(ret == -1){
664
664
    perror("setgid");
665
665
  }
666
666
  
667
 
  if (plugindir == NULL){
 
667
  if(plugindir == NULL){
668
668
    dir = opendir(PDIR);
669
669
  } else {
670
670
    dir = opendir(plugindir);
697
697
    
698
698
    /* All directory entries have been processed */
699
699
    if(dirst == NULL){
700
 
      if (errno == EBADF){
 
700
      if(errno == EBADF){
701
701
        perror("readdir");
702
702
        exitstatus = EXIT_FAILURE;
703
703
        goto fallback;
763
763
    }
764
764
    
765
765
    ret = stat(filename, &st);
766
 
    if (ret == -1){
 
766
    if(ret == -1){
767
767
      perror("stat");
768
768
      free(filename);
769
769
      continue;
770
770
    }
771
771
 
772
772
    /* Ignore non-executable files */
773
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
773
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
774
      if(debug){
775
775
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
776
                " with bad type or mode\n", filename);
823
823
    
824
824
    int pipefd[2];
825
825
    ret = pipe(pipefd);
826
 
    if (ret == -1){
 
826
    if(ret == -1){
827
827
      perror("pipe");
828
828
      exitstatus = EXIT_FAILURE;
829
829
      goto fallback;
842
842
      goto fallback;
843
843
    }
844
844
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
845
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
846
    if(ret < 0){
847
847
      perror("sigprocmask");
848
848
      exitstatus = EXIT_FAILURE;
896
896
    close(pipefd[1]);           /* Close unused write end of pipe */
897
897
    free(filename);
898
898
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if (new_plugin == NULL){
 
899
    if(new_plugin == NULL){
900
900
      perror("getplugin");
901
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
901
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
902
      if(ret < 0){
903
903
        perror("sigprocmask");
904
904
      }
911
911
    
912
912
    /* Unblock SIGCHLD so signal handler can be run if this process
913
913
       has already completed */
914
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
914
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
915
    if(ret < 0){
916
916
      perror("sigprocmask");
917
917
      exitstatus = EXIT_FAILURE;
920
920
    
921
921
    FD_SET(new_plugin->fd, &rfds_all);
922
922
    
923
 
    if (maxfd < new_plugin->fd){
 
923
    if(maxfd < new_plugin->fd){
924
924
      maxfd = new_plugin->fd;
925
925
    }
926
926
  }
943
943
  while(plugin_list){
944
944
    fd_set rfds = rfds_all;
945
945
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if (select_ret == -1){
 
946
    if(select_ret == -1){
947
947
      perror("select");
948
948
      exitstatus = EXIT_FAILURE;
949
949
      goto fallback;
989
989
          proc = next_plugin;
990
990
          
991
991
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                             NULL);
 
992
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
993
                            NULL);
994
994
          if(ret < 0){
995
995
            perror("sigprocmask");
996
996
            exitstatus = EXIT_FAILURE;
1025
1025
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1026
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1027
                               + (size_t) BUFFER_SIZE);
1028
 
        if (proc->buffer == NULL){
 
1028
        if(proc->buffer == NULL){
1029
1029
          perror("malloc");
1030
1030
          exitstatus = EXIT_FAILURE;
1031
1031
          goto fallback;