/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-07 01:44:44 UTC
  • mfrom: (24.1.93 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080907014444-cf4ilzndc0tbn8va
Merge & resolve.

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 & Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
6
6
 * 
7
7
 * This program is free software: you can redistribute it and/or
8
8
 * modify it under the terms of the GNU General Public License as
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
29
29
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
31
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
32
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
33
                                   stat, waitpid(), WIFEXITED(),
34
34
                                   WEXITSTATUS(), wait(), pid_t,
46
46
                                   fcntl(), setuid(), setgid(),
47
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
48
                                   access(), pipe(), fork(), close()
49
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
50
50
                                   execv(), write(), read(),
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
69
69
#define PDIR "/lib/mandos/plugins.d"
70
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
71
 
72
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
 
75
struct plugin;
 
76
 
75
77
typedef struct plugin{
76
78
  char *name;                   /* can be NULL or any plugin name */
77
79
  char **argv;
206
208
 * Descriptor Flags".
207
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
208
210
 */
209
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
210
213
  int ret = fcntl(fd, F_GETFD, 0);
211
214
  /* If reading the flags failed, return error indication now. */
212
215
  if(ret < 0){
219
222
 
220
223
/* Mark processes as completed when they exit, and save their exit
221
224
   status. */
222
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
223
226
  while(true){
224
227
    plugin *proc = plugin_list;
225
228
    int status;
250
253
}
251
254
 
252
255
/* Prints out a password to stdout */
253
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
254
257
  ssize_t ret;
255
258
  for(size_t written = 0; written < length; written += (size_t)ret){
256
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
470
473
      debug = true;
471
474
      break;
472
475
    case ARGP_KEY_ARG:
473
 
      /* Cryptsetup always passes an argument, which is an empty
474
 
         string if "none" was specified in /etc/crypttab.  So if
475
 
         argument was empty, we ignore it silently. */
476
 
      if(arg[0] != '\0'){
477
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
478
 
      }
 
476
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
477
      break;
480
478
    case ARGP_KEY_END:
481
479
      break;
846
844
        perror("sigaction");
847
845
        _exit(EXIT_FAILURE);
848
846
      }
849
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
847
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
848
      if(ret < 0){
851
849
        perror("sigprocmask");
852
850
        _exit(EXIT_FAILURE);
853
851
      }
854
 
      
 
852
 
855
853
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
854
      if(ret == -1){
857
855
        perror("dup2");
907
905
    if (maxfd < new_plugin->fd){
908
906
      maxfd = new_plugin->fd;
909
907
    }
 
908
    
910
909
  }
911
910
  
912
911
  closedir(dir);
913
912
  dir = NULL;
914
 
  
 
913
 
915
914
  for(plugin *p = plugin_list; p != NULL; p = p->next){
916
915
    if(p->pid != 0){
917
916
      break;
922
921
      free_plugin_list();
923
922
    }
924
923
  }
925
 
  
 
924
 
926
925
  /* Main loop while running plugins exist */
927
926
  while(plugin_list){
928
927
    fd_set rfds = rfds_all;
968
967
            goto fallback;
969
968
          }
970
969
          
971
 
          plugin *next_plugin = proc->next;
972
 
          free_plugin(proc);
973
 
          proc = next_plugin;
974
 
          
975
970
          /* We are done modifying process list, so unblock signal */
976
971
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
972
                             NULL);
985
980
            break;
986
981
          }
987
982
          
 
983
          plugin *next_plugin = proc->next;
 
984
          free_plugin(proc);
 
985
          proc = next_plugin;
988
986
          continue;
989
987
        }
990
988
        
991
989
        /* This process exited nicely, so print its buffer */
992
 
        
 
990
 
993
991
        bool bret = print_out_password(proc->buffer,
994
992
                                       proc->buffer_length);
995
993
        if(not bret){
1061
1059
    perror("sigaction");
1062
1060
    exitstatus = EXIT_FAILURE;
1063
1061
  }
1064
 
  
 
1062
 
1065
1063
  if(custom_argv != NULL){
1066
1064
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1065
      free(*arg);
1073
1071
    closedir(dir);
1074
1072
  }
1075
1073
  
1076
 
  /* Kill the processes */
 
1074
  /* Free the process list and kill the processes */
1077
1075
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1076
    if(p->pid != 0){
1079
1077
      close(p->fd);
1092
1090
  if(errno != ECHILD){
1093
1091
    perror("wait");
1094
1092
  }
1095
 
  
 
1093
 
1096
1094
  free_plugin_list();
1097
1095
  
1098
1096
  free(plugindir);