Htpasswd(3pm)

πŸ“¦
version 1.9 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Htpasswd β€” Apache::Htpasswd πŸ’–

okay bestie, welcome to Htpasswd(3pm) β€” that's the User Contributed Perl Documentation page for Apache::Htpasswd, and we are about to make crypt-style password files make sense. grab a snack, we're doing this πŸ“


✨ NAME ✨

Apache::Htpasswd β€” manage a Unix crypt-style password file. yes. that's the whole job. and it does it beautifully. πŸ’…


🌸 SYNOPSIS 🌸

here's the vibe, straight up:

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");

like… look at it. every one of those is a little moment. 🫢


πŸ‘‘ DESCRIPTION πŸ‘‘

so this module ships with a whole lil set of methods you use with htaccess password files. these files (and htaccess itself, obvi) are what you use to do Basic Authentication on a web server. it's giving gatekeeper energy but like, in a helpful way.

the passwords file is a flat-file with a login name and that login's associated crypted password. you can use it for non-Apache files if you want β€” nobody's stopping you β€” but ngl it was written specifically for .htaccess style files. that's its whole origin story.


πŸ¦‹ FUNCTIONS πŸ¦‹

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

okay so as of version 1.5.4, named params got added, and it's suggested you use them from here on out. like, please. for me. πŸ™

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

"path-to-file" should be the path and name of the file containing the login/password information. (yes, both. the whole thing. ✨)

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

this one? this is the perferred way to instantiate an object. (that's the original's spelling, and honestly? iconic. we keep it. πŸŽ€)

  • the 'ReadOnly' param is optional, and it will open the file in read-only mode if you use it.
  • the 'UseMD5' is also optional: it will force MD5 password under Unix.

and if you want to support plain un-encrypted passwords, then you need to set the UsePlain option. this is NOT recommended, but it might be necessary in some situations. lowkey risky, highkey your call. πŸ’€


error;

if a method returns an error, or a method fails, the error can be retrieved by calling error(). it's the "what went wrong bestie" button. πŸ”₯


htCheckPassword("login", "password");

finds if the password is valid for the given login.

  • returns 1 if it passes.
  • returns 0 if it fails.

vibe check: passed or not passed, no in-between. ⭐


htpasswd("login", "password");

this will add a new user to the password file.

  • returns 1 if it succeeds.
  • returns undef on failure.

welcome to the club, new user 🫢


htDelete("login")

delete the user's entry in the password file.

  • returns 1 on success
  • returns undef on failure.

bye bestie πŸ‘‹ (affectionate)


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

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

it's literally asking for ID before it lets you change anything. slay. πŸ”₯


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.

⚠️ no ID check. no questions. just vibes and a new password. use responsibly bestie.
  • returns 1 if it succeeds
  • returns undef if it fails

fetchPass("login");

returns the encrypted password if it succeeds.

  • returns 0 if login is invalid.
  • returns undef otherwise.

three possible outcomes, keep track of which one you got, okay? 😭


fetchInfo("login");

returns additional information if it succeeds.

  • returns 0 if login is invalid.
  • returns undef otherwise.

same energy as above, different field. πŸ’œ


fetchUsers();

will return either a list of all the user names, or a count of all the users. it reads the room, basically. 🧚

the following will return a list:

my @users = $Htpasswd->fetchUsers();

the following will return the count:

my $user_count = $Htpasswd->fetchUsers();

same method, different context, different result. that's not a bug it's range. ✨


writeInfo("login", "info");

will replace the additional information for the login.

  • returns 0 if login is invalid.
  • returns undef otherwise.

CryptPasswd("password", "salt");

will return an encrypted password using 'crypt'. if salt is omitted, a salt will be created. it just… makes one. for you. unprompted. king behavior. πŸ‘‘


🌈 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

that's it. that's the ritual. say it with me: configure, build, test, install, clean. periodt. πŸ’…

if you are going to use MD5 encrypted passwords, you need to install Crypt::PasswdMD5.

if you need to support SHA1 encrypted passwords, you need to install Digest::SHA and MIME::Base64.

like, don't skip these. the module will not manifest them for you. 🌸


πŸŽ€ 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.

read the docs. the changelog literally yells about this. i'm just saying. πŸ™ˆ


πŸ’– 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. go get it bestie πŸ¦‹


⭐ CHANGES ⭐

the full lore, in order:

  • Revision 1.9.0 β€” SHA dependency 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.

the newline saga across 1.5, 1.5.1, 1.5.2 and 1.5.5? that's a journey. and revision 1.7.0 is yelling in all caps and honestly? deserved. 😭


πŸ’€ BUGS πŸ’€

None known at time of writing.

(we love a clean slate. absolutely no notes. ✨)


🫢 AUTHOR INFORMATION 🫢

Copyright 1998..2005, Kevin Meltzer. All rights reserved. it may be used and modified freely, but he does 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 guarantees of this software. as with all software, use at your own risk. 🫢

🌈 SEE ALSO 🌈

  • Apache::Htgroup
  • Crypt::PasswdMD5
  • Digest::SHA
  • MIME::Base64

and that's the whole page bestie, no cap. this beauty came to you as part of perl v5.36.0, dated 2022-12-08, filed under Htpasswd(3pm) β€” so go forth, guard those password files, and remember: read the docs before you email Kevin. πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from