/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

Several memory leaks detected by valgrind fixed
A bugg fixed that could cause seg fault.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008 Teddy Hogeborn
6
 
 * Copyright © 2008 Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
33
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
47
46
                                   fcntl(), setuid(), setgid(),
48
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
49
48
                                   access(), pipe(), fork(), close()
50
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
51
50
                                   execv(), write(), read(),
52
51
                                   close() */
53
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
70
69
#define PDIR "/lib/mandos/plugins.d"
71
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
72
71
 
73
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
74
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
75
74
 
 
75
struct plugin;
 
76
 
76
77
typedef struct plugin{
77
78
  char *name;                   /* can be NULL or any plugin name */
78
79
  char **argv;
207
208
 * Descriptor Flags".
208
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
209
210
 */
210
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
211
213
  int ret = fcntl(fd, F_GETFD, 0);
212
214
  /* If reading the flags failed, return error indication now. */
213
215
  if(ret < 0){
220
222
 
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
224
226
  while(true){
225
227
    plugin *proc = plugin_list;
226
228
    int status;
251
253
}
252
254
 
253
255
/* Prints out a password to stdout */
254
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
255
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
256
261
  for(size_t written = 0; written < length; written += (size_t)ret){
257
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
258
263
                                   length - written));
471
476
      debug = true;
472
477
      break;
473
478
    case ARGP_KEY_ARG:
474
 
      /* Cryptsetup always passes an argument, which is an empty
475
 
         string if "none" was specified in /etc/crypttab.  So if
476
 
         argument was empty, we ignore it silently. */
477
 
      if(arg[0] != '\0'){
478
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
 
      }
 
479
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
480
480
      break;
481
481
    case ARGP_KEY_END:
482
482
      break;
701
701
      
702
702
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
703
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
704
                                           ".dpkg-divert", NULL };
706
705
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
706
        size_t pre_len = strlen(*pre);
738
737
    }
739
738
 
740
739
    char *filename;
741
 
    if(plugindir == NULL){
742
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
743
 
    } else {
744
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
 
    }
 
740
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
746
741
    if(ret < 0){
747
742
      perror("asprintf");
748
743
      continue;
848
843
        perror("sigaction");
849
844
        _exit(EXIT_FAILURE);
850
845
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
846
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
847
      if(ret < 0){
853
848
        perror("sigprocmask");
854
849
        _exit(EXIT_FAILURE);
855
850
      }
856
 
      
 
851
 
857
852
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
853
      if(ret == -1){
859
854
        perror("dup2");
909
904
    if (maxfd < new_plugin->fd){
910
905
      maxfd = new_plugin->fd;
911
906
    }
 
907
    
912
908
  }
913
909
  
914
910
  closedir(dir);
915
911
  dir = NULL;
916
 
  
 
912
 
917
913
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
914
    if(p->pid != 0){
919
915
      break;
924
920
      free_plugin_list();
925
921
    }
926
922
  }
927
 
  
 
923
 
928
924
  /* Main loop while running plugins exist */
929
925
  while(plugin_list){
930
926
    fd_set rfds = rfds_all;
970
966
            goto fallback;
971
967
          }
972
968
          
973
 
          plugin *next_plugin = proc->next;
974
 
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
969
          /* We are done modifying process list, so unblock signal */
978
970
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
971
                             NULL);
987
979
            break;
988
980
          }
989
981
          
 
982
          plugin *next_plugin = proc->next;
 
983
          free_plugin(proc);
 
984
          proc = next_plugin;
990
985
          continue;
991
986
        }
992
987
        
993
988
        /* This process exited nicely, so print its buffer */
994
 
        
 
989
 
995
990
        bool bret = print_out_password(proc->buffer,
996
991
                                       proc->buffer_length);
997
992
        if(not bret){
1044
1039
    bool bret;
1045
1040
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1046
1041
    char *passwordbuffer = getpass("Password: ");
1047
 
    size_t len = strlen(passwordbuffer);
1048
 
    /* Strip trailing newline */
1049
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1050
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1051
 
      len--;
1052
 
    }
1053
 
    bret = print_out_password(passwordbuffer, len);
 
1042
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1054
1043
    if(not bret){
1055
1044
      perror("print_out_password");
1056
1045
      exitstatus = EXIT_FAILURE;
1063
1052
    perror("sigaction");
1064
1053
    exitstatus = EXIT_FAILURE;
1065
1054
  }
1066
 
  
 
1055
 
1067
1056
  if(custom_argv != NULL){
1068
1057
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1058
      free(*arg);
1075
1064
    closedir(dir);
1076
1065
  }
1077
1066
  
1078
 
  /* Kill the processes */
 
1067
  /* Free the process list and kill the processes */
1079
1068
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1069
    if(p->pid != 0){
1081
1070
      close(p->fd);
1094
1083
  if(errno != ECHILD){
1095
1084
    perror("wait");
1096
1085
  }
1097
 
  
 
1086
 
1098
1087
  free_plugin_list();
1099
1088
  
1100
1089
  free(plugindir);