/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-09-07 15:42:11 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080907154211-sc90y38lazc4ewfq
* initramfs-tools-script: Fix permissions of "/tmp" in initrd.

* plugin-runner.c (parse_opt): Ignore empty argument strings.

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;
256
258
  for(size_t written = 0; written < length; written += (size_t)ret){
257
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
701
703
      
702
704
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
705
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
706
                                           ".dpkg-divert", NULL };
706
707
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
708
        size_t pre_len = strlen(*pre);
848
849
        perror("sigaction");
849
850
        _exit(EXIT_FAILURE);
850
851
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
852
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
853
      if(ret < 0){
853
854
        perror("sigprocmask");
854
855
        _exit(EXIT_FAILURE);
855
856
      }
856
 
      
 
857
 
857
858
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
859
      if(ret == -1){
859
860
        perror("dup2");
909
910
    if (maxfd < new_plugin->fd){
910
911
      maxfd = new_plugin->fd;
911
912
    }
 
913
    
912
914
  }
913
915
  
914
916
  closedir(dir);
915
917
  dir = NULL;
916
 
  
 
918
 
917
919
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
920
    if(p->pid != 0){
919
921
      break;
924
926
      free_plugin_list();
925
927
    }
926
928
  }
927
 
  
 
929
 
928
930
  /* Main loop while running plugins exist */
929
931
  while(plugin_list){
930
932
    fd_set rfds = rfds_all;
970
972
            goto fallback;
971
973
          }
972
974
          
973
 
          plugin *next_plugin = proc->next;
974
 
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
975
          /* We are done modifying process list, so unblock signal */
978
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
977
                             NULL);
987
985
            break;
988
986
          }
989
987
          
 
988
          plugin *next_plugin = proc->next;
 
989
          free_plugin(proc);
 
990
          proc = next_plugin;
990
991
          continue;
991
992
        }
992
993
        
993
994
        /* This process exited nicely, so print its buffer */
994
 
        
 
995
 
995
996
        bool bret = print_out_password(proc->buffer,
996
997
                                       proc->buffer_length);
997
998
        if(not bret){
1063
1064
    perror("sigaction");
1064
1065
    exitstatus = EXIT_FAILURE;
1065
1066
  }
1066
 
  
 
1067
 
1067
1068
  if(custom_argv != NULL){
1068
1069
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1070
      free(*arg);
1075
1076
    closedir(dir);
1076
1077
  }
1077
1078
  
1078
 
  /* Kill the processes */
 
1079
  /* Free the process list and kill the processes */
1079
1080
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1081
    if(p->pid != 0){
1081
1082
      close(p->fd);
1094
1095
  if(errno != ECHILD){
1095
1096
    perror("wait");
1096
1097
  }
1097
 
  
 
1098
 
1098
1099
  free_plugin_list();
1099
1100
  
1100
1101
  free(plugindir);