/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: 2008-11-01 02:26:00 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081101022600-bacs9fxmsliqrah1
* initramfs-tools-hook: Also ignore plugins named "*.dpkg-bak".
* plugin-runner.c: - '' -

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 © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 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, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
                                   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 1.0";
 
72
const char *argp_program_version = "plugin-runner " VERSION;
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
75
 
struct plugin;
76
 
 
77
75
typedef struct plugin{
78
76
  char *name;                   /* can be NULL or any plugin name */
79
77
  char **argv;
208
206
 * Descriptor Flags".
209
207
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
210
208
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
 
209
static int set_cloexec_flag(int fd){
213
210
  int ret = fcntl(fd, F_GETFD, 0);
214
211
  /* If reading the flags failed, return error indication now. */
215
212
  if(ret < 0){
222
219
 
223
220
/* Mark processes as completed when they exit, and save their exit
224
221
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
222
static void handle_sigchld(__attribute__((unused)) int sig){
226
223
  while(true){
227
224
    plugin *proc = plugin_list;
228
225
    int status;
253
250
}
254
251
 
255
252
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
253
static bool print_out_password(const char *buffer, size_t length){
257
254
  ssize_t ret;
258
255
  for(size_t written = 0; written < length; written += (size_t)ret){
259
256
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
473
470
      debug = true;
474
471
      break;
475
472
    case ARGP_KEY_ARG:
476
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", 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
      }
477
479
      break;
478
480
    case ARGP_KEY_END:
479
481
      break;
698
700
      
699
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
700
702
                                           ".dpkg-old",
 
703
                                           ".dpkg-bak",
701
704
                                           ".dpkg-divert", NULL };
702
705
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
703
706
        size_t pre_len = strlen(*pre);
844
847
        perror("sigaction");
845
848
        _exit(EXIT_FAILURE);
846
849
      }
847
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
850
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
848
851
      if(ret < 0){
849
852
        perror("sigprocmask");
850
853
        _exit(EXIT_FAILURE);
851
854
      }
852
 
 
 
855
      
853
856
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
854
857
      if(ret == -1){
855
858
        perror("dup2");
905
908
    if (maxfd < new_plugin->fd){
906
909
      maxfd = new_plugin->fd;
907
910
    }
908
 
    
909
911
  }
910
912
  
911
913
  closedir(dir);
912
914
  dir = NULL;
913
 
 
 
915
  
914
916
  for(plugin *p = plugin_list; p != NULL; p = p->next){
915
917
    if(p->pid != 0){
916
918
      break;
921
923
      free_plugin_list();
922
924
    }
923
925
  }
924
 
 
 
926
  
925
927
  /* Main loop while running plugins exist */
926
928
  while(plugin_list){
927
929
    fd_set rfds = rfds_all;
967
969
            goto fallback;
968
970
          }
969
971
          
 
972
          plugin *next_plugin = proc->next;
 
973
          free_plugin(proc);
 
974
          proc = next_plugin;
 
975
          
970
976
          /* We are done modifying process list, so unblock signal */
971
977
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
972
978
                             NULL);
980
986
            break;
981
987
          }
982
988
          
983
 
          plugin *next_plugin = proc->next;
984
 
          free_plugin(proc);
985
 
          proc = next_plugin;
986
989
          continue;
987
990
        }
988
991
        
989
992
        /* This process exited nicely, so print its buffer */
990
 
 
 
993
        
991
994
        bool bret = print_out_password(proc->buffer,
992
995
                                       proc->buffer_length);
993
996
        if(not bret){
1059
1062
    perror("sigaction");
1060
1063
    exitstatus = EXIT_FAILURE;
1061
1064
  }
1062
 
 
 
1065
  
1063
1066
  if(custom_argv != NULL){
1064
1067
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1065
1068
      free(*arg);
1071
1074
    closedir(dir);
1072
1075
  }
1073
1076
  
1074
 
  /* Free the process list and kill the processes */
 
1077
  /* Kill the processes */
1075
1078
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1076
1079
    if(p->pid != 0){
1077
1080
      close(p->fd);
1090
1093
  if(errno != ECHILD){
1091
1094
    perror("wait");
1092
1095
  }
1093
 
 
 
1096
  
1094
1097
  free_plugin_list();
1095
1098
  
1096
1099
  free(plugindir);