This file is indexed.

/usr/share/perl5/Apache/Htpasswd.pm is in libapache-htpasswd-perl 1.8-1.2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
package Apache::Htpasswd;

use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
use warnings;
use strict;    # Restrict unsafe variables, references, barewords
use Carp;

use POSIX qw ( SEEK_SET SEEK_END );
use Fcntl qw ( LOCK_EX LOCK_UN );

@ISA = qw(Exporter);

@EXPORT = qw();

@EXPORT_OK =
  qw(htpasswd htDelete fetchPass fetchInfo writeInfo htCheckPassword error Version);

%EXPORT_TAGS = ( all => [@EXPORT_OK] );

$VERSION = '1.8';

sub Version {
    return $VERSION;
}

#-----------------------------------------------------------#
# Public Methods
#-----------------------------------------------------------#

sub new {
    my $proto = shift;
    my $args  = shift;
    my $passwdFile;

    if ( ref $args eq 'HASH' ) {
        $passwdFile = $args->{'passwdFile'};
    }
    else {
        $passwdFile = $args;
    }

    my $class = ref($proto) || $proto;
    my ($self) = {};
    bless( $self, $class );

    $self->{'PASSWD'}   = $passwdFile;
    $self->{'ERROR'}    = "";
    $self->{'LOCK'}     = 0;
    $self->{'OPEN'}     = 0;
    $self->{'READONLY'} = $args->{'ReadOnly'} if ref $args eq 'HASH';
    $self->{'USEMD5'} = $args->{'UseMD5'} if ref $args eq 'HASH';
    $self->{'USEPLAIN'} = $args->{'UsePlain'} if ref $args eq 'HASH';

    return $self;
}

#-----------------------------------------------------------#

sub error {
    my $self = shift;
    return $self->{'ERROR'};
}

#-----------------------------------------------------------#

sub htCheckPassword {
    my $self = shift;
    my $Id   = shift;
    my $pass = shift;
    my $MD5Magic = '$apr1$';
    my $SHA1Magic = '{SHA}';

    my $cryptPass = $self->fetchPass($Id);
    if ( !$cryptPass ) { return undef; }

    if (index($cryptPass, $MD5Magic) == 0) {
        # This is an MD5 password
        require Crypt::PasswdMD5;
        my $salt = $cryptPass;
        $salt =~ s/^\Q$MD5Magic//;      # Take care of the magic string if present
        $salt =~ s/^(.*)\$/$1/;         # Salt can have up to 8 chars...
        $salt = substr( $salt, 0, 8 );  # That means no more than 8 chars too.
        return 1 if Crypt::PasswdMD5::apache_md5_crypt( $pass, $salt ) eq $cryptPass;
    }
    elsif (index($cryptPass, $SHA1Magic) == 0) {
        # This is an SHA1 password
        require Digest::SHA;
        require MIME::Base64;
        return 1 if '{SHA}'.MIME::Base64::encode_base64( Digest::SHA::sha1( $pass ), '' ) eq $cryptPass;
    }

    # See if it is encrypted using crypt
    return 1 if crypt($pass, $cryptPass) eq $cryptPass;

    # See if it is a plain, unencrypted password
    return 1 if $self->{USEPLAIN} && $pass eq $cryptPass;
    
    $self->{'ERROR'} =
        __PACKAGE__ . "::htCheckPassword - Passwords do not match.";
    carp $self->error() unless caller ne $self;
    return 0;
}

#-----------------------------------------------------------#

sub htpasswd {
    my $self    = shift;
    my $Id      = shift;
    my $newPass = shift;
    my $oldPass = shift;
    my $noOld = 0;

    if ( $self->{READONLY} ) {
        $self->{'ERROR'} =
          __PACKAGE__ . "::htpasswd - Can't change passwords in ReadOnly mode";
        carp $self->error();
        return undef;
    }

    if ( !defined($oldPass) ) { 
			$noOld = 1; 
		}

    if ( defined($oldPass) && ref $oldPass eq 'HASH' ) {
        if ($oldPass->{'overwrite'}) {
            $newPass = $Id unless $newPass;
            my $newEncrypted = $self->CryptPasswd($newPass);
            return $self->writePassword( $Id, $newEncrypted );
        }
    }

    # New Entry
    if ($noOld) {
        my $passwdFile = $self->{'PASSWD'};

        # Encrypt new password string

        my $passwordCrypted = $self->CryptPasswd($newPass);

        $self->_open();

        if ( $self->fetchPass($Id) ) {

            # User already has a password in the file.
            $self->{'ERROR'} =
              __PACKAGE__ . "::htpasswd - $Id already exists in $passwdFile";
            carp $self->error();
            $self->_close();
            return undef;
        }
        else {

            # If we can add the user.
            seek( FH, 0, SEEK_END );
            print FH "$Id\:$passwordCrypted\n";

            $self->_close();
            return 1;
        }

        $self->_close();

    }
    else {
        $self->_open();

        my $exists = $self->htCheckPassword( $Id, $oldPass );

        if ($exists) {
            my ($newCrypted) = $self->CryptPasswd($newPass);
            return $self->writePassword( $Id, $newCrypted );
        }
        else {

            # ERROR returned from htCheckPass
            $self->{'ERROR'} =
              __PACKAGE__ . "::htpasswd - Password not changed.";
            carp $self->error();
            return undef;
        }

        $self->_close();
    }
}    # end htpasswd

#-----------------------------------------------------------#

sub htDelete {
    my $self       = shift;
    my $Id         = shift;
    my $passwdFile = $self->{'PASSWD'};
    my @cache;
    my $return;

    # Loop through the file, building a cache of exising records
    # which don't match the Id.

    $self->_open();

    seek( FH, 0, SEEK_SET );
    while (<FH>) {

        if (/^$Id\:/) {
            $return = 1;
        }
        else {
            push ( @cache, $_ );
        }
    }

    # Write out the @cache if needed.

    if ($return) {

        # Return to beginning of file
        seek( FH, 0, SEEK_SET );

        while (@cache) {
            print FH shift (@cache);
        }

        # Cut everything beyond current position
        truncate( FH, tell(FH) );

    }
    else {
        $self->{'ERROR'} =
          __PACKAGE__ . "::htDelete - User $Id not found in $passwdFile: $!";
        carp $self->error();
    }

    $self->_close();

    return $return;
}

#-----------------------------------------------------------#

sub fetchPass {
    my $self       = shift;
    my $Id         = shift;
    my $passwdFile = $self->{'PASSWD'};

    my $passwd = 0;

    $self->_open();

    while (<FH>) {
        chop;
        my @tmp = split ( /:/, $_, 3 );
        if ( $tmp[0] eq $Id ) {
            $passwd = $tmp[1];
            last;
        }
    }

    $self->_close();

    return $passwd;
}

#-----------------------------------------------------------#

sub writePassword {
    my $self    = shift;
    my $Id      = shift;
    my $newPass = shift;

    my $passwdFile = $self->{'PASSWD'};
    my @cache;
    my $return;

    $self->_open();
    seek( FH, 0, SEEK_SET );

    while (<FH>) {

        my @tmp = split ( /:/, $_, 3 );
        if ( $tmp[0] eq $Id ) {
            my $info = $tmp[2] ? $tmp[2] : "";
            chomp $info;
            push ( @cache, "$Id\:$newPass\:$info\n" );
            $return = 1;

        }
        else {
            push ( @cache, $_ );
        }
    }

    # Write out the @cache, if needed.

    if ($return) {

        # Return to beginning of file
        seek( FH, 0, SEEK_SET );

        while (@cache) {
            print FH shift (@cache);
        }

        # Cut everything beyond current position
        truncate( FH, tell(FH) );

    }
    else {
        $self->{'ERROR'} = __PACKAGE__
          . "::writePassword - User $Id not found in $passwdFile: $!";
        carp $self->error() . "\n";
    }

    $self->_close();

    return $return;
}

#-----------------------------------------------------------#

sub fetchInfo {
    my $self       = shift;
    my $Id         = shift;
    my $passwdFile = $self->{'PASSWD'};

    my $info = 0;

    $self->_open();

    while (<FH>) {
        chop;
        my @tmp = split ( /:/, $_, 3 );
        if ( $tmp[0] eq $Id ) {
            $info = $tmp[2];
            last;
        }
    }

    $self->_close();

    return $info;
}

#-----------------------------------------------------------#

sub fetchUsers {
    my $self       = shift;
    my $passwdFile = $self->{'PASSWD'};
    my $count      = 0;
    my @users;

    $self->_open();

    while (<FH>) {
        chop;
        my @tmp = split ( /:/, $_, 3 );
        push ( @users, $tmp[0] ) unless !$tmp[0];
    }

    $self->_close();

    return wantarray() ? @users : scalar @users;
}

#-----------------------------------------------------------#

sub writeInfo {
    my $self    = shift;
    my $Id      = shift;
    my $newInfo = shift;

    my ($passwdFile) = $self->{'PASSWD'};
    my (@cache);

    my ($return);

    $self->_open();
    seek( FH, 0, SEEK_SET );

    while (<FH>) {

        my @tmp = split ( /:/, $_, 3 );

        if ( $tmp[0] eq $Id ) {
            chomp $tmp[1] if ( @tmp == 2 );   # Cut out EOL if there was no info
            push ( @cache, "$Id\:$tmp[1]\:$newInfo\n" );
            $return = 1;

        }
        else {
            push ( @cache, $_ );
        }
    }

    # Write out the @cache, if needed.

    if ($return) {

        # Return to beginning of file
        seek( FH, 0, SEEK_SET );

        while (@cache) {
            print FH shift (@cache);
        }

        # Cut everything beyond current position
        truncate( FH, tell(FH) );

    }
    else {
        $self->{'ERROR'} =
          __PACKAGE__ . "::writeInfo - User $Id not found in $passwdFile: $!";
        carp $self->error() . "\n";
    }

    $self->_close();

    return $return;
}

#-----------------------------------------------------------#

sub CryptPasswd {
    my $self   = shift;
    my $passwd = shift;
    my $salt   = shift;
    my @chars  = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
    my $Magic = '$apr1$';    # Apache specific Magic chars
    my $cryptType = (  $^O =~ /^MSWin/i || $self->{'USEMD5'} ) ? "MD5" : "crypt";

    if ( $salt && $cryptType =~ /MD5/i && $salt =~ /^\Q$Magic/ ) {

        # Borrowed from Crypt::PasswdMD5
        $salt =~ s/^\Q$Magic//;       # Take care of the magic string if present
        $salt =~ s/^(.*)\$.*$/$1/;    # Salt can have up to 8 chars...
        $salt = substr( $salt, 0, 8 );    # That means no more than 8 chars too.
                                          # For old crypt only
    }
    elsif ( $salt && $cryptType =~ /crypt/i ) {
        if ($salt =~ /\$2a\$\d+\$(.{23})/) {
            $salt = $1;
        } else {
            # Make sure only use 2 chars
            $salt = substr( $salt, 0, 2 );
        }  
    }
    else {

# If we use MD5, create apache MD5 with 8 char salt: 3 randoms, 5 dots
        if ( $cryptType =~ /MD5/i ) {
            $salt =
              join ( '', map { $chars[ int rand @chars ] } ( 0 .. 2 ) )
              . "." x 5;

            # Otherwise fallback to standard archaic crypt
        }
        else {
            $salt = join ( '', map { $chars[ int rand @chars ] } ( 0 .. 1 ) );
        }
    }

    if ( $cryptType =~ /MD5/i ) {
				require Crypt::PasswdMD5;
        return Crypt::PasswdMD5::apache_md5_crypt( $passwd, $salt );
    }
    else {
        return crypt( $passwd, $salt );
    }
}

#-----------------------------------------------------------#

sub DESTROY { close(FH); };

#-----------------------------------------------------------#

sub _lock {
    my $self = shift;

    # Lock if we don't have the lock
    flock( FH, LOCK_EX ) if ( $self->{'LOCK'} == 0 );

    # We have the lock
    $self->{'LOCK'} = 1;

    # Seek to head
    seek( FH, 0, SEEK_SET );
}

#-----------------------------------------------------------#

sub _unlock {
    my $self = shift;

    flock( FH, LOCK_UN );

    $self->{'LOCK'} = 0;
}

#-----------------------------------------------------------#

sub _open {
    my $self = shift;

    if ( $self->{'OPEN'} > 0 ) {
        $self->{'OPEN'}++;
        $self->_lock();
        return;
    }

    my $passwdFile = $self->{'PASSWD'};

    if ( $self->{READONLY} ) {
        if ( !open( FH, $passwdFile ) ) {
            $self->{'ERROR'} =
              __PACKAGE__ . "::fetchPass - Cannot open $passwdFile: $!";
            croak $self->error();
        }
    }
    else {
        if ( !open( FH, "+<$passwdFile" ) ) {
            $self->{'ERROR'} =
              __PACKAGE__ . "::fetchPass - Cannot open $passwdFile: $!";
            croak $self->error();
        }
    }

    binmode(FH);
    $self->{'OPEN'}++;
    $self->_lock() unless $self->{READONLY};    # No lock on r/o
}

#-----------------------------------------------------------#

sub _close {
    my $self = shift;
    $self->_unlock() unless $self->{READONLY};

    $self->{'OPEN'}--;

    if ( $self->{'OPEN'} > 0 ) { return; }

    if ( !close(FH) ) {
        my $passwdFile = $self->{'PASSWD'};
        $self->{'ERROR'} =
          __PACKAGE__ . "::htDelete - Cannot close $passwdFile: $!";
        carp $self->error();
        return undef;
    }
}

#-----------------------------------------------------------#

1;

__END__

=head1 NAME

Apache::Htpasswd - Manage Unix crypt-style password file.

=head1 SYNOPSIS

    use Apache::Htpasswd;

    $foo = new Apache::Htpasswd("path-to-file");

    $foo = new Apache::Htpasswd({passwdFile => "path-to-file",
				 ReadOnly   => 1}
				);

    # Add an entry
    $foo->htpasswd("zog", "password");

    # Change a password
    $foo->htpasswd("zog", "new-password", "old-password");

    # Change a password without checking against old password

    $foo->htpasswd("zog", "new-password", {'overwrite' => 1});

    # Check that a password is correct
    $foo->htCheckPassword("zog", "password");

    # Fetch an encrypted password
    $foo->fetchPass("foo");

    # Delete entry
    $foo->htDelete("foo");

    # If something fails, check error
    $foo->error;

    # Write in the extra info field
    $foo->writeInfo("login", "info");

    # Get extra info field for a user
    $foo->fetchInfo("login");

=head1 DESCRIPTION

This module comes with a set of methods to use with htaccess password
files. These files (and htaccess) are used to do Basic Authentication
on a web server.

The passwords file is a flat-file with login name and their associated
crypted password. You can use this for non-Apache files if you wish, but
it was written specifically for .htaccess style files.

=head2 FUNCTIONS

=over 4

=item Apache::Htpasswd->new(...);

As of version 1.5.4 named params have been added, and it is suggested that
you use them from here on out.

	Apache::Htpasswd->new("path-to-file");

"path-to-file" should be the path and name of the file containing
the login/password information.

	Apache::Htpasswd->new({passwdFile => "path-to-file",
			       ReadOnly   => 1,
			       UseMD5     => 1,
			     });

This is the prefered way to instantiate an object. The 'ReadOnly' param
is optional, and will open the file in read-only mode if used. The 'UseMD5'
is also optional: it will force MD5 password under Unix.

If you want to support plain un-encrypted passwords, then you need to set
the UsePlain option (this is NOT recommended, but might be necesary in some
situations)

=item error;

If a method returns an error, or a method fails, the error can
be retrieved by calling error()


=item htCheckPassword("login", "password");

Finds if the password is valid for the given login.

Returns 1 if passes.
Returns 0 if fails.


=item htpasswd("login", "password");

This will add a new user to the password file.
Returns 1 if succeeds.
Returns undef on failure.


=item htDelete("login")

Delete users entry in password file.

Returns 1 on success
Returns undef on failure.


=item htpasswd("login", "new-password", "old-password");

If the I<old-password> matches the I<login's> password, then
it will replace it with I<new-password>. If the I<old-password>
is not correct, will return 0.


=item htpasswd("login", "new-password", {'overwrite' => 1});

Will replace the password for the login. This will force the password
to be changed. It does no verification of old-passwords.

Returns 1 if succeeds
Returns undef if fails

=item fetchPass("login");

Returns I<encrypted> password if succeeds.
Returns 0 if login is invalid.
Returns undef otherwise.

=item fetchInfo("login");

Returns additional information if succeeds.
Returns 0 if login is invalid.
Returns undef otherwise.

=item fetchUsers();

Will return either a list of all the user names, or a count of all the
users.

The following will return a list:
my @users = $Htpasswd->fetchUsers();

The following will return the count:
my $user_count = $Htpasswd->fetchUsers();

=item writeInfo("login", "info");

Will replace the additional information for the login.
Returns 0 if login is invalid.
Returns undef otherwise.


=item CryptPasswd("password", "salt");

Will return an encrypted password using 'crypt'. If I<salt> is
ommitted, a salt will be created.

=back

=head1 INSTALLATION

You install Apache::Htpasswd, as you would install any perl module library,
by running these commands:

   perl Makefile.PL
   make
   make test
   make install
   make clean

If you are going to use MD5 encrypted passwords, you need to install L<Crypt::PasswdMD5>.

If you need to support SHA1 encrypted passwords, you need to install L<Digest::SHA> and L<MIME::Base64>.

=head1 DOCUMENTATION

POD style documentation is included in the module.
These are normally converted to manual pages and installed as part
of the "make install" process.  You should also be able to use
the 'perldoc' utility to extract and read documentation from the
module files directly.


=head1 AVAILABILITY

The latest version of Apache::Htpasswd should always be available from:

    $CPAN/modules/by-authors/id/K/KM/KMELTZ/

Visit <URL:http://www.perl.com/CPAN/> to find a CPAN
site near you.

=head1 CHANGES

Revision 1.8.0  Added proper PREREQ_PM

Revision 1.7.0  Handle SHA1 and plaintext. Also change the interface
for allowing change of password without first checking old password. IF
YOU DON'T READ THE DOCS AND SEE I DID THIS DON'T EMAIL ME!

Revision 1.6.0  Handle Blowfish hashes when that's the mechanism crypt() uses.

Revision 1.5.9  MD5 for *nix with new UseMD5 arg for new()

Revision 1.5.8  Bugfix to htpasswd().

Revision 1.5.7  MD5 for Windows, and other minor changes.

Revision 1.5.6  Minor enhancements.

Revision 1.5.5  2002/08/14 11:27:05 Newline issue fixed for certain conditions.

Revision 1.5.4  2002/07/26 12:17:43 kevin doc fixes, new fetchUsers method,
new ReadOnly option, named params for new(), various others

Revision 1.5.3  2001/05/02 08:21:18 kevin
Minor bugfix

Revision 1.5.2  2001/04/03 09:14:57 kevin
Really fixed newline problem :)

Revision 1.5.1  2001/03/26 08:25:38 kevin
Fixed another newline problem

Revision 1.5  2001/03/15 01:50:12 kevin
Fixed bug to remove newlines

Revision 1.4  2001/02/23 08:23:46 kevin
Added support for extra info fields

Revision 1.3  2000/04/04 15:00:15 meltzek
Made file locking safer to avoid race conditions. Fixed
typo in docs.

Revision 1.2  1999/01/28 22:43:45  meltzek
Added slightly more verbose error croaks. Made sure error from htCheckPassword is only called when called directly, and not by $self.

Revision 1.1  1998/10/22 03:12:08  meltzek
Slightly changed how files lock.
Made more use out of carp and croak.
Made sure there were no ^M's as per Randal Schwartz's request.


=head1 BUGS

None known at time of writting.

=head1 AUTHOR INFORMATION

Copyright 1998..2005, Kevin Meltzer.  All rights reserved.  It may
be used and modified freely, but I do request that this copyright
notice remain attached to the file.  You may modify this module as you
wish, but if you redistribute a modified version, please attach a note
listing the modifications you have made.

This is released under the same terms as Perl itself.

Address bug reports and comments to:
kmeltz@cpan.org

The author makes no warranties, promises, or gaurentees of this software. As with all
software, use at your own risk.

=head1 SEE ALSO

L<Apache::Htgroup>, L<Crypt::PasswdMD5>, L<Digest::SHA>, L<MIME::Base64>

=cut