/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-19 00:00:51 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080919000051-tgm5erxuj0zhs867
* debian/mandos-client.README.Debian: Document "eth0" default and how
                                      to change it.

* debian/mandos-client.postrm (update_initramfs): Use "type" instead
                                                  of "which".

* plugin-runner.c (handle_sigchld, print_out_password): Declared
                                                        static.

* plugin-runner.xml (SYNOPSIS, OPTIONS): Changed "VAR" to "ENV" in the
                                         "--global-env" option to be
                                         consistent with the
                                         "--env-for" option.

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 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
 
76
75
typedef struct plugin{
309
308
  struct stat st;
310
309
  fd_set rfds_all;
311
310
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
311
  uid_t uid = 65534;
314
312
  gid_t gid = 65534;
315
313
  bool debug = false;
547
545
    char *org_line = NULL;
548
546
    char *p, *arg, *new_arg, *line;
549
547
    size_t size = 0;
 
548
    ssize_t sret;
550
549
    const char whitespace_delims[] = " \r\t\f\v\n";
551
550
    const char comment_delim[] = "#";
552
551
 
701
700
      
702
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
702
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
703
                                           ".dpkg-divert", NULL };
706
704
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
705
        size_t pre_len = strlen(*pre);
848
846
        perror("sigaction");
849
847
        _exit(EXIT_FAILURE);
850
848
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
849
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
850
      if(ret < 0){
853
851
        perror("sigprocmask");
854
852
        _exit(EXIT_FAILURE);
855
853
      }
856
 
      
 
854
 
857
855
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
856
      if(ret == -1){
859
857
        perror("dup2");
909
907
    if (maxfd < new_plugin->fd){
910
908
      maxfd = new_plugin->fd;
911
909
    }
 
910
    
912
911
  }
913
912
  
914
913
  closedir(dir);
915
914
  dir = NULL;
916
 
  
 
915
 
917
916
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
917
    if(p->pid != 0){
919
918
      break;
924
923
      free_plugin_list();
925
924
    }
926
925
  }
927
 
  
 
926
 
928
927
  /* Main loop while running plugins exist */
929
928
  while(plugin_list){
930
929
    fd_set rfds = rfds_all;
970
969
            goto fallback;
971
970
          }
972
971
          
973
 
          plugin *next_plugin = proc->next;
974
 
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
972
          /* We are done modifying process list, so unblock signal */
978
973
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
974
                             NULL);
987
982
            break;
988
983
          }
989
984
          
 
985
          plugin *next_plugin = proc->next;
 
986
          free_plugin(proc);
 
987
          proc = next_plugin;
990
988
          continue;
991
989
        }
992
990
        
993
991
        /* This process exited nicely, so print its buffer */
994
 
        
 
992
 
995
993
        bool bret = print_out_password(proc->buffer,
996
994
                                       proc->buffer_length);
997
995
        if(not bret){
1019
1017
        proc->buffer_size += BUFFER_SIZE;
1020
1018
      }
1021
1019
      /* Read from the process */
1022
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1023
 
                  BUFFER_SIZE);
1024
 
      if(sret < 0){
 
1020
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1021
                 BUFFER_SIZE);
 
1022
      if(ret < 0){
1025
1023
        /* Read error from this process; ignore the error */
1026
1024
        proc = proc->next;
1027
1025
        continue;
1028
1026
      }
1029
 
      if(sret == 0){
 
1027
      if(ret == 0){
1030
1028
        /* got EOF */
1031
1029
        proc->eof = true;
1032
1030
      } else {
1033
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1034
1032
      }
1035
1033
    }
1036
1034
  }
1063
1061
    perror("sigaction");
1064
1062
    exitstatus = EXIT_FAILURE;
1065
1063
  }
1066
 
  
 
1064
 
1067
1065
  if(custom_argv != NULL){
1068
1066
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1067
      free(*arg);
1075
1073
    closedir(dir);
1076
1074
  }
1077
1075
  
1078
 
  /* Kill the processes */
 
1076
  /* Free the process list and kill the processes */
1079
1077
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1078
    if(p->pid != 0){
1081
1079
      close(p->fd);
1094
1092
  if(errno != ECHILD){
1095
1093
    perror("wait");
1096
1094
  }
1097
 
  
 
1095
 
1098
1096
  free_plugin_list();
1099
1097
  
1100
1098
  free(plugindir);