Allen Jones wrote:
> 
> I am writing a script that will need to add user to a protected section on
> our unix server. I'm curious as the formats you can use when adding a user
> to the .htpasswd file.
> 
> What characters can be used, and what character cannot be used in the
> username and in the password?
> 
> I've looked all over for this information and just can't seem to find
> details on it. Can anyone give me the scoop on this?

Here's my Win32 notes on crypt/passwd/etc.  They should apply equally to UNIX.
Strip what you don;t need.

use strict;

#use Crypt;             # if no native crypt function, use the crypt module

# these next two vrbls need to be input from somewhere

my $username = 'JohnDoe';       # used to look up encrypted passwd in file
                                # gotten from input form or ?
$username = shift if @ARGV;

my $passwd = 'fubar';           # plain text passwd gotten from input form or ?
$passwd = shift if @ARGV;

my $use_crypt = 1;              # set if Perl has crypt function
my $dbg = 1;                    # debug
my $encrypted_passwd;           # place to store encrypted passwd

my $create_passwd = 0;          # 1 if creating a new passwd else 0
my $check_passwd = 1;           # 1 if checking a passwd else 0

print "username='$username'; passwd='$passwd'\n" if $dbg;

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

# Password adding part:

if ($create_passwd) {

        # this part is to create a new encrypted password.  Basically the same 
        # as htpasswd would do.  Create a salt and encrypt the password.  
        # Would allow you to add new users to your passwd file.  

        # if $use_crypt is set, it will use the crypt function to encrypt the 
        # passwd, else the htpasswd program is used to add the user directly 
        # to the passwd file.

        my $htpasswd = 'htpasswd';
        $htpasswd = 'C:\\Apache\\bin\\htpasswd.exe' if ($^O =~ /Win32/i);
        my $passwd_file = 'my_web_passwds';

        if ($use_crypt) {

                # use plain text password

                $encrypted_passwd = &crypt_passwd ($passwd);

                # Now add code to add it to your passwd file and you're done.
                # For now you should check to see if the user is already in 
                # there before appending the new one since the first one 
                # encountered will be used.

        } else {        # else use htpasswd to add direct to passwd file

                print "$htpasswd -b $passwd_file $username $passwd\n" if $dbg;

                my $ret = system "$htpasswd -b $passwd_file $username $passwd";
                print "ret=$ret\n" if $dbg;

                # Now you would open the passwd file and find the user you 
                # added and get the encrypted passwd back if you needed it.

                # On Win32, Apache htpasswd uses MD5 as default method, so it 
                # won't help for the rest of this example.  The -d option 
                # should make it use crypt instead.

                $encrypted_passwd = 'needs to be gotten from file';
        }
        print "encrypted_passwd='$encrypted_passwd'\n" if $dbg;
}

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

# Password checking part:

if ($check_passwd) {

        # now the user logs in and gives his plain text passwd again
        # retrieve encrypted password from passwd file for this user

        # encrypted passwd retrieved from passwd file normally on line that 
        # contains user name

        $encrypted_passwd = 'ZIqhptCBwAQS6';    # gotten from passwd file for this user
        $encrypted_passwd = shift if @ARGV;

        # encrypt the plaintext password using first 2 letters of encrypted 
        # password as salt

        my $crypt_passwd = crypt ($passwd, $encrypted_passwd);

        print "crypt_passwd='$encrypted_passwd'\n" if $dbg;

        if (crypt ($passwd, $encrypted_passwd) eq $encrypted_passwd) {
                print "You're OK\n";
        } else {
                print "You're not allowed\n";
        }
        exit 0;
}

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

sub crypt_passwd {   # $crypted_passwd = crypt_passwd ($plainpasswd [, $salt]);
        my $passwd = shift;
        my $salt;
        my @legal = ('.', '/', '0'..'9', 'A'..'Z', 'a'..'z');

# if salt supplied

if (defined $_[0]) {

        $salt = substr $_[0], 0, 2;     # get first 2 chars for salt

# else create a salt using time, pid and rand

} else {

        my $tmp = (time + $$) % 65536;
        srand ($tmp);
        $salt = $legal[sprintf "%u", rand (@legal)];
        $salt .= $legal[sprintf "%u", rand (@legal)];
}

my $new_passwd = crypt ($passwd, $salt);
return $new_passwd;

}

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

__END__

-- 
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
 (_/   /  )    // //       DBE Collectibles   http://www.todbe.com/
  / ) /--<  o // //      Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/
-/-' /___/_<_</_</_    http://www.freeyellow.com/members/dbecoll/
_______________________________________________
Perl-Unix-Users mailing list. To unsubscribe go to 
http://listserv.ActiveState.com/mailman/subscribe/perl-unix-users

Reply via email to