1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
# -*- perl -*-
# Net::FTPServer A Perl FTP Server
# Copyright (C) 2000 Bibliotech Ltd., Unit 2-3, 50 Carnwath Road,
# London, SW6 3EG, United Kingdom.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# $Id: Server.pm,v 1.9 2001/10/24 14:40:06 rich Exp $
=pod
=head1 NAME
Net::FTPServer::Full::Server - The full FTP server personality
=head1 SYNOPSIS
ftpd [-d] [-v] [-p port] [-s] [-S] [-V] [-C conf_file]
=head1 DESCRIPTION
C<Net::FTPServer::Full::Server> is the full FTP server
personality. This personality implements a complete
FTP server with similar functionality to I<wu-ftpd>.
=head1 METHODS
=over 4
=cut
package Net::FTPServer::Full::Server;
use strict;
# Authen::PAM is an optional module.
BEGIN { eval "use Authen::PAM;"; }
use vars qw($VERSION);
( $VERSION ) = '$Revision: 1.9 $ ' =~ /\$Revision:\s+([^\s]+)/;
use Net::FTPServer;
use Net::FTPServer::Full::FileHandle;
use Net::FTPServer::Full::DirHandle;
use vars qw(@ISA);
@ISA = qw(Net::FTPServer);
=pod
=item $rv = $self->authentication_hook ($user, $pass, $user_is_anon)
Perform login against C</etc/passwd> or the PAM database.
=cut
sub authentication_hook
{
my $self = shift;
my $user = shift;
my $pass = shift;
my $user_is_anon = shift;
# Allow anonymous users. By this point we have already checked
# that allow anonymous is true in the configuration file.
return 0 if $user_is_anon;
unless ($self->config ("pam authentication"))
{
# Verify user information against the password file.
my $hashed_pass = (getpwnam $user)[1] or return -1;
# Check password.
return -1 if crypt ($pass, $hashed_pass) ne $hashed_pass;
}
else
{
return -1 if $self->_pam_check_password ($user, $pass) < 0;
}
# Successful login.
return 0;
}
sub _pam_check_password
{
my $self = shift;
my $user = shift;
my $pass = shift;
# Give a nice error message in the logs if PAM is not available.
unless (exists $INC{"Authen/PAM.pm"})
{
die
"Authen::PAM module is not available, yet PAM authentication ",
"was requested in the configuration file. You will not be able ",
"to log in to this server. Fetch and install Authen::PAM from ",
"CPAN."
}
# As noted in the source to wu-ftpd, this is something
# of an abuse of the PAM protocol. However the FTP protocol
# gives us little choice in the matter.
eval
{
no strict; # Otherwise Perl complains about barewords.
my $pam_conv_func = sub
{
my @res;
while (@_)
{
my $msg_type = shift;
my $msg = shift;
if ($msg_type == PAM_PROMPT_ECHO_ON)
{
return ( PAM_CONV_ERR );
}
elsif ($msg_type == PAM_PROMPT_ECHO_OFF)
{
push @res, PAM_SUCCESS;
push @res, $pass;
}
elsif ($msg_type == PAM_TEXT_INFO)
{
push @res, PAM_SUCCESS;
push @res, "";
}
elsif ($msg_type == PAM_ERROR_MSG)
{
push @res, PAM_SUCCESS;
push @res, "";
}
else
{
return ( PAM_CONV_ERR );
}
}
push @res, PAM_SUCCESS;
return @res;
};
my $pam_appl = $self->config ("pam application name") || "ftp";
my $pamh = Authen::PAM->new ($pam_appl, $user, $pam_conv_func);
ref ($pamh) || die "PAM error: pam_start: $pamh";
$pamh->pam_set_item (PAM_RHOST, $self->{peeraddrstring})
== PAM_SUCCESS
or die "PAM error: pam_set_item";
$pamh->pam_authenticate (0) == PAM_SUCCESS
or die "PAM error: pam_authenticate";
$pamh->pam_acct_mgmt (0) == PAM_SUCCESS
or die "PAM error: pam_acct_mgmt";
$pamh->pam_setcred (PAM_ESTABLISH_CRED) == PAM_SUCCESS
or die "PAM error: pam_setcred";
}; # eval
if ($@)
{
$self->log ("info", "PAM authentication error: $@");
return -1;
}
return 0;
}
=pod
=item $self->user_login_hook ($user, $user_is_anon)
Hook: Called just after user C<$user> has successfully logged in.
=cut
sub user_login_hook
{
my $self = shift;
my $user = shift;
my $user_is_anon = shift;
my ($login, $pass, $uid, $gid, $quota, $comment, $gecos, $homedir);
# For non-anonymous users, just get the uid/gid.
if (! $user_is_anon)
{
($login, $pass, $uid, $gid) = getpwnam $user
or die "no user $user in password file";
# Chroot for this non-anonymous user?
my $root_directory = $self->config ("root directory");
if (defined $root_directory)
{
$root_directory =~ s/%m/(getpwnam $user)[7]/ge;
$root_directory =~ s/%U/$user/ge;
$root_directory =~ s/%%/%/g;
chroot $root_directory
or die "cannot chroot: $root_directory: $!";
}
}
# For anonymous users, chroot to ftp directory.
else
{
($login, $pass, $uid, $gid, $quota, $comment, $gecos, $homedir)
= getpwnam "ftp"
or die "no ftp user in password file";
chroot $homedir or die "cannot chroot: $homedir: $!";
}
# We don't allow users to relogin, so completely change to
# the user specified.
$self->_drop_privs ($uid, $gid, $login);
}
=pod
=item $dirh = $self->root_directory_hook;
Hook: Return an instance of Net::FTPServer::FullDirHandle
corresponding to the root directory.
=cut
sub root_directory_hook
{
my $self = shift;
return new Net::FTPServer::Full::DirHandle ($self);
}
1 # So that the require or use succeeds.
__END__
=back 4
=head1 FILES
/etc/ftpd.conf
/usr/lib/perl5/site_perl/5.005/Net/FTPServer.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/DirHandle.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/FileHandle.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/Handle.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/Full/Server.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/Full/DirHandle.pm
/usr/lib/perl5/site_perl/5.005/Net/FTPServer/Full/FileHandle.pm
=head1 AUTHORS
Richard Jones (rich@annexia.org).
=head1 COPYRIGHT
Copyright (C) 2000 Biblio@Tech Ltd., Unit 2-3, 50 Carnwath Road,
London, SW6 3EG, UK
=head1 SEE ALSO
L<Net::FTPServer(3)>,
L<Authen::PAM(3)>,
L<Net::FTP(3)>,
L<perl(1)>,
RFC 765,
RFC 959,
RFC 1579,
RFC 2389,
RFC 2428,
RFC 2577,
RFC 2640,
Extensions to FTP Internet Draft draft-ietf-ftpext-mlst-NN.txt.
=cut
|