| 1 |
package Authen::Htpasswd; |
| 2 |
use 5.005; |
| 3 |
use strict; |
| 4 |
use base 'Class::Accessor::Fast'; |
| 5 |
use Carp; |
| 6 |
use IO::File; |
| 7 |
use IO::LockedFile; |
| 8 |
use Authen::Htpasswd::User; |
| 9 |
use Scalar::Util qw(blessed); |
| 10 |
|
| 11 |
use vars qw{$VERSION $SUFFIX}; |
| 12 |
|
| 13 |
$VERSION = '0.171'; |
| 14 |
$VERSION = eval $VERSION; |
| 15 |
$SUFFIX = '.new'; |
| 16 |
|
| 17 |
__PACKAGE__->mk_accessors(qw/ file encrypt_hash check_hashes /); |
| 18 |
|
| 19 |
=head1 NAME |
| 20 |
|
| 21 |
Authen::Htpasswd - interface to read and modify Apache .htpasswd files |
| 22 |
|
| 23 |
=head1 SYNOPSIS |
| 24 |
|
| 25 |
my $pwfile = Authen::Htpasswd->new('user.txt', { encrypt_hash => 'md5' }); |
| 26 |
|
| 27 |
# authenticate a user (checks all hash methods by default) |
| 28 |
if ($pwfile->check_user_password('bob', 'foo')) { ... } |
| 29 |
|
| 30 |
# modify the file (writes immediately) |
| 31 |
$pwfile->update_user('bob', $password, $info); |
| 32 |
$pwfile->add_user('jim', $password); |
| 33 |
$pwfile->delete_user('jim'); |
| 34 |
|
| 35 |
# get user objects tied to a file |
| 36 |
my $user = $pwfile->lookup_user('bob'); |
| 37 |
if ($user->check_password('vroom', [qw/ md5 sha1 /])) { ... } # only use secure hashes |
| 38 |
$user->password('foo'); # writes to file |
| 39 |
$user->set(password => 'bar', extra_info => 'editor'); # change more than one thing at once |
| 40 |
|
| 41 |
# or manage the file yourself |
| 42 |
my $user = Authen::Htpasswd::User->new('bill', { hashed_password => 'iQ.IuWbUIhlPE' }); |
| 43 |
my $user = Authen::Htpasswd::User->new('bill', 'bar', 'staff', { encrypt_hash => 'crypt' }); |
| 44 |
print PASSWD $user->to_line, "\n"; |
| 45 |
|
| 46 |
=head1 DESCRIPTION |
| 47 |
|
| 48 |
This module provides a convenient, object-oriented interface to Apache-style |
| 49 |
F<.htpasswd> files. |
| 50 |
|
| 51 |
It supports passwords encrypted via MD5, SHA1, and crypt, as well as plain |
| 52 |
(cleartext) passwords. |
| 53 |
|
| 54 |
Additional fields after username and password, if present, are accessible via |
| 55 |
the C<extra_info> array. |
| 56 |
|
| 57 |
=head1 METHODS |
| 58 |
|
| 59 |
=head2 new |
| 60 |
|
| 61 |
my $pwfile = Authen::Htpasswd->new($filename, \%options); |
| 62 |
|
| 63 |
Creates an object for a given F<.htpasswd> file. Options: |
| 64 |
|
| 65 |
=over 4 |
| 66 |
|
| 67 |
=item encrypt_hash |
| 68 |
|
| 69 |
How passwords should be encrypted if a user is added or changed. Valid values are C<md5>, C<sha1>, |
| 70 |
C<crypt>, and C<plain>. Default is C<crypt>. |
| 71 |
|
| 72 |
=item check_hashes |
| 73 |
|
| 74 |
An array of hash methods to try when checking a password. The methods will be tried in the order |
| 75 |
given. Default is C<md5>, C<sha1>, C<crypt>, C<plain>. |
| 76 |
|
| 77 |
=back |
| 78 |
|
| 79 |
=cut |
| 80 |
|
| 81 |
sub new { |
| 82 |
my $class = shift; |
| 83 |
my $self = ref $_[-1] eq 'HASH' ? pop @_ : {}; |
| 84 |
$self->{file} = $_[0] if $_[0]; |
| 85 |
croak "no file specified" unless $self->{file}; |
| 86 |
if (!-e $self->{file}) { |
| 87 |
open my $file, '>', $self->{file} or die $!; |
| 88 |
close $file or die $!; |
| 89 |
} |
| 90 |
|
| 91 |
$self->{encrypt_hash} ||= 'crypt'; |
| 92 |
$self->{check_hashes} ||= [ Authen::Htpasswd::Util::supported_hashes() ]; |
| 93 |
unless ( defined $self->{write_locking} ) { |
| 94 |
if ( $^O eq 'MSWin32' or $^O eq 'cygwin' ) { |
| 95 |
$self->{write_locking} = 0; |
| 96 |
} else { |
| 97 |
$self->{write_locking} = 1; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
bless $self, $class; |
| 102 |
} |
| 103 |
|
| 104 |
=head2 lookup_user |
| 105 |
|
| 106 |
my $userobj = $pwfile->lookup_user($username); |
| 107 |
|
| 108 |
Returns an L<Authen::Htpasswd::User> object for the given user in the password file. |
| 109 |
|
| 110 |
=cut |
| 111 |
|
| 112 |
sub lookup_user { |
| 113 |
my ($self,$search_username) = @_; |
| 114 |
|
| 115 |
my $file = IO::LockedFile->new($self->file, 'r') or die $!; |
| 116 |
while (defined(my $line = <$file>)) { |
| 117 |
chomp $line; |
| 118 |
my ($username,$hashed_password,@extra_info) = split /:/, $line; |
| 119 |
if ($username eq $search_username) { |
| 120 |
$file->close or die $!; |
| 121 |
return Authen::Htpasswd::User->new($username,undef,@extra_info, { |
| 122 |
file => $self, |
| 123 |
hashed_password => $hashed_password, |
| 124 |
encrypt_hash => $self->encrypt_hash, |
| 125 |
check_hashes => $self->check_hashes |
| 126 |
}); |
| 127 |
} |
| 128 |
} |
| 129 |
$file->close or die $!; |
| 130 |
return undef; |
| 131 |
} |
| 132 |
|
| 133 |
=head2 all_users |
| 134 |
|
| 135 |
my @users = $pwfile->all_users; |
| 136 |
|
| 137 |
=cut |
| 138 |
|
| 139 |
sub all_users { |
| 140 |
my $self = shift; |
| 141 |
|
| 142 |
my @users; |
| 143 |
my $file = IO::LockedFile->new($self->file, 'r') or die $!; |
| 144 |
while (defined(my $line = <$file>)) { |
| 145 |
chomp $line; |
| 146 |
my ($username,$hashed_password,@extra_info) = split /:/, $line; |
| 147 |
push(@users, Authen::Htpasswd::User->new($username,undef,@extra_info, { |
| 148 |
file => $self, |
| 149 |
hashed_password => $hashed_password, |
| 150 |
encrypt_hash => $self->encrypt_hash, |
| 151 |
check_hashes => $self->check_hashes |
| 152 |
})); |
| 153 |
} |
| 154 |
$file->close or die $!; |
| 155 |
return @users; |
| 156 |
} |
| 157 |
|
| 158 |
=head2 check_user_password |
| 159 |
|
| 160 |
$pwfile->check_user_password($username,$password); |
| 161 |
|
| 162 |
Returns whether the password is valid. Shortcut for |
| 163 |
C<< $pwfile->lookup_user($username)->check_password($password) >>. |
| 164 |
|
| 165 |
=cut |
| 166 |
|
| 167 |
sub check_user_password { |
| 168 |
my ($self,$username,$password) = @_; |
| 169 |
my $user = $self->lookup_user($username); |
| 170 |
croak "could not find user $username" unless $user; |
| 171 |
return $user->check_password($password); |
| 172 |
} |
| 173 |
|
| 174 |
=head2 update_user |
| 175 |
|
| 176 |
$pwfile->update_user($userobj); |
| 177 |
$pwfile->update_user($username, $password[, @extra_info], \%options); |
| 178 |
|
| 179 |
Modifies the entry for a user saves it to the file. If the user entry does not |
| 180 |
exist, it is created. The options in the second form are passed to L<Authen::Htpasswd::User>. |
| 181 |
|
| 182 |
=cut |
| 183 |
|
| 184 |
sub update_user { |
| 185 |
my $self = shift; |
| 186 |
my $user = $self->_get_user(@_); |
| 187 |
my $username = $user->username; |
| 188 |
|
| 189 |
my ($old,$new) = $self->_start_rewrite; |
| 190 |
my $seen = 0; |
| 191 |
while (defined(my $line = <$old>)) { |
| 192 |
if ($line =~ /^\Q$username\E:/) { |
| 193 |
chomp $line; |
| 194 |
my (undef,undef,@extra_info) = split /:/, $line; |
| 195 |
$user->{extra_info} ||= [ @extra_info ] if scalar @extra_info; |
| 196 |
$self->_print( $new, $user->to_line . "\n" ); |
| 197 |
$seen++; |
| 198 |
} else { |
| 199 |
$self->_print( $new, $line ); |
| 200 |
} |
| 201 |
} |
| 202 |
$self->_print( $new, $user->to_line . "\n" ) unless $seen; |
| 203 |
$self->_finish_rewrite($old,$new); |
| 204 |
} |
| 205 |
|
| 206 |
=head2 add_user |
| 207 |
|
| 208 |
$pwfile->add_user($userobj); |
| 209 |
$pwfile->add_user($username, $password[, @extra_info], \%options); |
| 210 |
|
| 211 |
Adds a user entry to the file. If the user entry already exists, an exception is raised. |
| 212 |
The options in the second form are passed to L<Authen::Htpasswd::User>. |
| 213 |
|
| 214 |
=cut |
| 215 |
|
| 216 |
sub add_user { |
| 217 |
my $self = shift; |
| 218 |
my $user = $self->_get_user(@_); |
| 219 |
my $username = $user->username; |
| 220 |
|
| 221 |
my ($old,$new) = $self->_start_rewrite; |
| 222 |
while (defined(my $line = <$old>)) { |
| 223 |
if ($line =~ /^\Q$username\E:/) { |
| 224 |
$self->_abort_rewrite($old,$new); |
| 225 |
croak "user $username already exists in " . $self->file . "!"; |
| 226 |
} |
| 227 |
$self->_print( $new, $line ); |
| 228 |
} |
| 229 |
$self->_print( $new, $user->to_line . "\n" ); |
| 230 |
$self->_finish_rewrite($old,$new); |
| 231 |
} |
| 232 |
|
| 233 |
=head2 delete_user |
| 234 |
|
| 235 |
$pwfile->delete_user($userobj); |
| 236 |
$pwfile->delete_user($username); |
| 237 |
|
| 238 |
Removes a user entry from the file. |
| 239 |
|
| 240 |
=cut |
| 241 |
|
| 242 |
sub delete_user { |
| 243 |
my $self = shift; |
| 244 |
my $username = blessed($_[0]) && $_[0]->isa('Authen::Htpasswd::User') ? $_[0]->username : $_[0]; |
| 245 |
|
| 246 |
my ($old,$new) = $self->_start_rewrite; |
| 247 |
while (defined(my $line = <$old>)) { |
| 248 |
next if $line =~ /^\Q$username\E:/; |
| 249 |
$self->_print( $new, $line ); |
| 250 |
} |
| 251 |
$self->_finish_rewrite($old,$new); |
| 252 |
} |
| 253 |
|
| 254 |
sub _print { |
| 255 |
my ($self,$new,$string) = @_; |
| 256 |
if ( $self->{write_locking} ) { |
| 257 |
print $new $string; |
| 258 |
} else { |
| 259 |
$$new .= $string; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
sub _get_user { |
| 264 |
my $self = shift; |
| 265 |
return $_[0] if blessed($_[0]) && $_[0]->isa('Authen::Htpasswd::User'); |
| 266 |
my $attr = ref $_[-1] eq 'HASH' ? pop @_ : {}; |
| 267 |
$attr->{encrypt_hash} ||= $self->encrypt_hash; |
| 268 |
$attr->{check_hashes} ||= $self->check_hashes; |
| 269 |
return Authen::Htpasswd::User->new(@_, $attr); |
| 270 |
} |
| 271 |
|
| 272 |
sub _start_rewrite { |
| 273 |
my $self = shift; |
| 274 |
if ( $self->{write_locking} ) { |
| 275 |
my $old = IO::LockedFile->new($self->file, 'r+') or die $!; |
| 276 |
my $new = IO::File->new($self->file . $SUFFIX, 'w') or die $!; |
| 277 |
return ($old,$new); |
| 278 |
} else { |
| 279 |
my $old = IO::File->new( $self->file, 'r' ) or die $!; |
| 280 |
my $new = ""; |
| 281 |
return ($old, \$new); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
sub _finish_rewrite { |
| 286 |
my ($self,$old,$new) = @_; |
| 287 |
if ( $self->{write_locking} ) { |
| 288 |
$new->close or die $!; |
| 289 |
rename $self->file . $SUFFIX, $self->file or die $!; |
| 290 |
$old->close or die $!; |
| 291 |
} else { |
| 292 |
$old->close or die $!; |
| 293 |
$old = IO::File->new( $self->file, 'w' ) or die $!; |
| 294 |
print $old $$new; |
| 295 |
$old->close or die $!; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
sub _abort_rewrite { |
| 300 |
my ($self,$old,$new) = @_; |
| 301 |
if ( $self->{write_locking} ) { |
| 302 |
$new->close; |
| 303 |
$old->close; |
| 304 |
unlink $self->file . $SUFFIX; |
| 305 |
} else { |
| 306 |
$old->close; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
=head1 AUTHOR |
| 311 |
|
| 312 |
David Kamholz C<[email protected]> |
| 313 |
|
| 314 |
Yuval Kogman |
| 315 |
|
| 316 |
=head1 SEE ALSO |
| 317 |
|
| 318 |
L<Apache::Htpasswd>. |
| 319 |
|
| 320 |
=head1 COPYRIGHT & LICENSE |
| 321 |
|
| 322 |
Copyright (c) 2005 - 2007 the aforementioned authors. |
| 323 |
|
| 324 |
This program is free software; you can redistribute |
| 325 |
it and/or modify it under the same terms as Perl itself. |
| 326 |
|
| 327 |
=cut |
| 328 |
|
| 329 |
1; |