1
# Fetch.pm - This module includes functions to fetch remote files
3
#<!-- RCS Identication ; $Revision: 1.2 $ ; $Date: 2006/04/11 13:58:03 $ -->
6
# Sympa - SYsteme de Multi-Postage Automatique
7
# Copyright (c) 1997, 1998, 1999, 2000, 2001 Comite Reseau des Universites
8
# Copyright (c) 1997,1998, 1999 Institut Pasteur & Christophe Wolfhugel
10
# This program is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation; either version 2 of the License, or
13
# (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with this program; if not, write to the Free Software
22
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
## Good documentation : http://articles.mongueurs.net/magazines/linuxmag57.html
29
@ISA = qw (LWP::UserAgent);
31
my ($web_user, $web_passwd);
33
sub get_basic_credentials {
34
my ( $self, $realm, $uri ) = @_;
36
return ( $web_user, $web_passwd );
39
sub set_basic_credentials {
40
($web_user, $web_passwd ) = @_;
51
# request a document using https, return status and content
56
my $client_cert = shift;
57
my $client_key = shift;
60
my $key_passwd = $ssl_data->{'key_passwd'};
61
my $trusted_ca_file = $ssl_data->{'cafile'};
62
my $trusted_ca_path = $ssl_data->{'capath'};
64
do_log ('debug','get_https (%s,%s,%s,%s,%s,%s,%s,%s)',$host,$port,$path,$client_cert,$client_key,$key_passwd,$trusted_ca_file,$trusted_ca_path );
66
unless ( -r ($trusted_ca_file) || (-d $trusted_ca_path )) {
67
do_log ('err',"error : incorrect access to cafile $trusted_ca_file bor capath $trusted_ca_path");
71
unless (eval "require IO::Socket::SSL") {
72
do_log('err',"Unable to use SSL library, IO::Socket::SSL required, install IO-Socket-SSL (CPAN) first");
75
require IO::Socket::SSL;
77
unless (eval "require LWP::UserAgent") {
78
do_log('err',"Unable to use LWP library, LWP::UserAgent required, install LWP (CPAN) first");
81
require LWP::UserAgent;
85
$ssl_socket = new IO::Socket::SSL(SSL_use_cert => 1,
86
SSL_verify_mode => 0x01,
87
SSL_cert_file => $client_cert,
88
SSL_key_file => $client_key,
89
SSL_passwd_cb => sub { return ($key_passwd)},
90
SSL_ca_file => $trusted_ca_file,
91
SSL_ca_path => $trusted_ca_path,
98
unless ($ssl_socket) {
99
do_log ('err','error %s unable to connect https://%s:%s/',&IO::Socket::SSL::errstr,$host,$port);
102
do_log ('debug','connected to https://%s:%s/',&IO::Socket::SSL::errstr,$host,$port);
104
if( ref($ssl_socket) eq "IO::Socket::SSL") {
105
my $subject_name = $ssl_socket->peer_certificate("subject");
106
my $issuer_name = $ssl_socket->peer_certificate("issuer");
107
my $cipher = $ssl_socket->get_cipher();
108
do_log ('debug','ssl peer certificat %s issued by %s. Cipher used %s',$subject_name,$issuer_name,$cipher);
111
print $ssl_socket "GET $path HTTP/1.0\nHost: $host\n\n";
113
do_log ('debug',"requested GET $path HTTP/1.1");
114
#my ($buffer) = $ssl_socket->getlines;
115
# print STDERR $buffer;
116
#do_log ('debug',"return");
119
do_log ('debug',"get_https reading answer");
121
while (my $line = $ssl_socket->getline) {
125
$ssl_socket->close(SSL_no_shutdown => 1);
126
do_log ('debug',"disconnected");
132
# request a document using https, return status and content
140
my $trusted_ca_file = $ssl_data->{'cafile'};
141
$trusted_ca_file ||= $Conf{'cafile'};
142
my $trusted_ca_path = $ssl_data->{'capath'};
143
$trusted_ca_path ||= $Conf{'capath'};
145
do_log ('debug','Fetch::get_https2 (%s,%s,%s,%s,%s)',$host,$port,$path,$trusted_ca_file,$trusted_ca_path );
147
unless ( -r ($trusted_ca_file) || (-d $trusted_ca_path )) {
148
do_log ('err',"error : incorrect access to cafile $trusted_ca_file bor capath $trusted_ca_path");
152
unless (eval "require IO::Socket::SSL") {
153
do_log('err',"Unable to use SSL library, IO::Socket::SSL required, install IO-Socket-SSL (CPAN) first");
156
require IO::Socket::SSL;
158
unless (eval "require LWP::UserAgent") {
159
do_log('err',"Unable to use LWP library, LWP::UserAgent required, install LWP (CPAN) first");
162
require LWP::UserAgent;
166
$ssl_socket = new IO::Socket::SSL(SSL_use_cert => 0,
167
SSL_verify_mode => 0x01,
168
SSL_ca_file => $trusted_ca_file,
169
SSL_ca_path => $trusted_ca_path,
176
unless ($ssl_socket) {
177
do_log ('err','error %s unable to connect https://%s:%s/',&IO::Socket::SSL::errstr,$host,$port);
180
do_log ('debug',"connected to https://$host:$port/");
182
# if( ref($ssl_socket) eq "IO::Socket::SSL") {
183
# my $subject_name = $ssl_socket->peer_certificate("subject");
184
# my $issuer_name = $ssl_socket->peer_certificate("issuer");
185
# my $cipher = $ssl_socket->get_cipher();
186
# do_log ('debug','ssl peer certificat %s issued by %s. Cipher used %s',$subject_name,$issuer_name,$cipher);
189
my $request = "GET $path HTTP/1.0\nHost: $host\n\n";
190
print $ssl_socket "$request\n\n";
192
do_log ('debug',"requesting $request");
193
#my ($buffer) = $ssl_socket->getlines;
194
# print STDERR $buffer;
195
#do_log ('debug',"return");
198
do_log ('debug',"get_https reading answer returns :");
200
while (my $line = $ssl_socket->getline) {
201
do_log ('debug',"$line");
205
$ssl_socket->close(SSL_no_shutdown => 1);
206
do_log ('debug',"disconnected");
213
#################################################################
215
## Packages must return true.