| 1 | #! /usr/bin/env perl | ||
| 2 | # | ||
| 3 | # | ||
| 4 | # COPYRIGHT UNIVERSITY OF MANCHESTER, 2003 | ||
| 5 | # | ||
| 6 | # Author: Mark Mc Keown | ||
| 7 | # mark.mckeown@man.ac.uk | ||
| 8 | # | ||
| 9 | # LICENCE TERMS | ||
| 10 | # | ||
| 11 | # WSRF::Lite is free software; you can redistribute it | ||
| 12 | # and/or modify it under the same terms as Perl itself. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # | ||
| 16 | # | ||
| 17 | # Secure version of the Container script - uses SSL | ||
| 18 | # | ||
| 19 | |||
| 20 | |||
| 21 | use WSRF::Lite; | ||
| 22 | use Sys::Hostname::Long; | ||
| 23 | use WSRF::SSLDaemon; | ||
| 24 | use POSIX ":sys_wait_h"; | ||
| 25 | |||
| 26 | $ENV{'TZ'} = "GMT"; | ||
| 27 | |||
| 28 | #timeout for clients that want to keep an open tcp connection | ||
| 29 | my $TIMEOUT = "120"; | ||
| 30 | |||
| 31 | if ( defined($ENV{'WSRF_SOCKETS'}) ) | ||
| 32 | { | ||
| 33 | $WSRF::Constants::SOCKETS_DIRECTORY = $ENV{'WSRF_SOCKETS'} ; | ||
| 34 | } | ||
| 35 | |||
| 36 | if ( defined($ENV{'WSRF_DATA'}) ) | ||
| 37 | { | ||
| 38 | $WSRF::Constants::Data = $ENV{'WSRF_DATA'} ; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | if ( ! -d $WSRF::Constants::SOCKETS_DIRECTORY ) | ||
| 43 | { | ||
| 44 | die "Directory $WSRF::Constants::SOCKETS_DIRECTORY does not exist\n"; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ( ! -d $WSRF::Constants::Data ) | ||
| 48 | { | ||
| 49 | die "Directory $WSRF::Constants::Data does not exist\n"; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | my $port = 50000; | ||
| 54 | |||
| 55 | my ($hostname); | ||
| 56 | while ( my $arg = shift @ARGV ) | ||
| 57 | { | ||
| 58 | if( $arg =~ m/-p/o) | ||
| 59 | { | ||
| 60 | $port = shift @ARGV; | ||
| 61 | die "No Port number provided with -p option\n" unless defined $port; | ||
| 62 | die "\"$port\" does not look like a port number\n" unless ( $port =~ /\d+/o ); | ||
| 63 | } | ||
| 64 | elsif ( $arg =~ m/-h/o ) | ||
| 65 | { | ||
| 66 | $hostname = shift @ARGV; | ||
| 67 | die "No hostname provided with -h option\n" unless defined $hostname; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | $hostname = Sys::Hostname::Long::hostname_long() unless defined $hostname; | ||
| 72 | |||
| 73 | |||
| 74 | # REAPER kills of stry children. | ||
| 75 | # this REAPER is designed to be used with Perl 5.8 though | ||
| 76 | # it should still work with Perl 5.6 | ||
| 77 | sub REAPER { | ||
| 78 | local $!; | ||
| 79 | waitpid(-1,0); | ||
| 80 | $SIG{CHLD} = \&REAPER; # still loathe sysV | ||
| 81 | } | ||
| 82 | $SIG{CHLD} = \&REAPER; | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | #Check that the path to the Grid Service Modules is set | ||
| 87 | if ( !defined($ENV{'WSRF_MODULES'}) ) | ||
| 88 | { | ||
| 89 | die "Enviromental Variable WSRF_MODULES not defined"; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | #Not sure if we need to set this!! | ||
| 94 | $ENV{SSL}="TRUE"; | ||
| 95 | |||
| 96 | |||
| 97 | #Accept Proxies - later versions of OpenSSL are ment to | ||
| 98 | #accept proxies, I haven't seen this work :-( | ||
| 99 | $ENV{'OPENSSL_ALLOW_PROXY'} = "True"; | ||
| 100 | |||
| 101 | |||
| 102 | #create the Service Container - just a Web Server | ||
| 103 | #Certificate information is provided here - could | ||
| 104 | #use a personal certificate | ||
| 105 | my $d = WSRF::SSLDaemon->new( | ||
| 106 | LocalPort => $port, | ||
| 107 | Listen => SOMAXCONN, | ||
| 108 | Reuse => 1, | ||
| 109 | SSL_key_file => '/home/zzcgumk/.globus/hostkey.pem', | ||
| 110 | SSL_cert_file => '/home/zzcgumk/.globus/hostcert.pem', | ||
| 111 | SSL_ca_path => '/etc/grid-security/certificates/', | ||
| 112 | SSL_ca_file => '/etc/grid-security/certificates/01621954.0', | ||
| 113 | SSL_verify_mode => 0x01 | 0x02 | 0x04 | ||
| 114 | ) || do { print "Socket Error: Cannot create Socket\n"; exit; }; | ||
| 115 | |||
| 116 | |||
| 117 | #Store the Container Address in the ENV variables - child | ||
| 118 | #processes can then pick it up and use it | ||
| 119 | $ENV{'URL'} = "https://".$hostname.":".$port."/"; | ||
| 120 | |||
| 121 | print "\nContainer Contact Address: ".$ENV{'URL'}."\n"; | ||
| 122 | |||
| 123 | #SSLDaemon ISA IO::Socket::SSL so treat like a socket | ||
| 124 | #my $client = $d->accept || $!{EINTR} | ||
| 125 | while ( 1 ) { #wait for client to connect | ||
| 126 | my $client = $d->accept; | ||
| 127 | if ( defined $client ) | ||
| 128 | { | ||
| 129 | print "$$ Got Connection\n"; | ||
| 130 | if (my $pid = fork){ #fork a process to deal with request | ||
| 131 | print "$$ Parent forked\n"; #parent should go back to accept now | ||
| 132 | $client->close( SSL_no_shutdown => 1 ); | ||
| 133 | undef $client; | ||
| 134 | print "$$ Going back to accept\n"; | ||
| 135 | } | ||
| 136 | elsif (defined($pid) ) #child | ||
| 137 | { | ||
| 138 | print "$$ Child created ".scalar(localtime(time))."\n"; | ||
| 139 | print "$$ Connection Accepted\n"; | ||
| 140 | #Check for client certificate and set ENV variables | ||
| 141 | my $cert = $client->dump_peer_certificate(); | ||
| 142 | my($DN,$issuer) = split( /\n/, $cert); | ||
| 143 | $DN =~ s/Subject Name: //o; | ||
| 144 | $issuer =~ s/Issuer Name: //o; | ||
| 145 | print "$$ Client DN= $DN\n"; | ||
| 146 | print "$$ Client Issuer DN= $issuer\n"; | ||
| 147 | $ENV{SSL_CLIENT_DN} = $DN; | ||
| 148 | $ENV{SSL_CLIENT_ISSUER} = $issuer; | ||
| 149 | while ( my $r = $client->get_request ) | ||
| 150 | { | ||
| 151 | my $crap = alarm 0; | ||
| 152 | my $resp = WSRF::Container::handle($r,$client); | ||
| 153 | my $result = $client->send_response($resp); | ||
| 154 | print "$$ Sent Client response $result\n"; | ||
| 155 | alarm($TIMEOUT); | ||
| 156 | } | ||
| 157 | print "$$ Closing Socket ". $d->close." $?\n"; | ||
| 158 | undef ($d); | ||
| 159 | $client->close; | ||
| 160 | undef ($client); | ||
| 161 | print "$$ Exiting\n"; | ||
| 162 | exit; | ||
| 163 | } | ||
| 164 | else | ||
| 165 | { #fork failed | ||
| 166 | print "$$ fork failed\n"; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | else | ||
| 170 | { | ||
| 171 | next if $!{EINTR}; # just a child exiting, go back to sleep. | ||
| 172 | } | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 |