#!/usr/bin/perl -w

#push(@INC, $ENV{'HOME'} . "/scripts");
#
#require 'randompasswd.pl';


#
# $Id: randompasswd,v 1.1 2002/07/06 22:35:29 dgregor Exp $
#

use FileHandle;


sub myrand {
	# XXX need to make  $urandomfh local, but static
	local($multiplier) = 1;
	local($readbuffer) = "";
	local($urandomfile) = "/dev/urandom";

	if (defined(@_)) {
		$multiplier = shift(@_);
	}
	
#	warn "multiplier = $multiplier\n";

	if (-r $urandomfile) {
		if (!defined($urandomfh)) {
#			warn "opening $urandomfile\n";
			defined($urandomfh = new FileHandle($urandomfile, "r"))
				|| die "could not open $urandomfile: $!\n";
		}
		sysread($urandomfh, $readbuffer, 4) ||
			die "could not read from $urandomfile: $!\n";
		local($randomlong) = unpack('L', $readbuffer);
#		warn "unpack returned: $randomlong\n";
		local($randommult) = (($randomlong/2**32) * $multiplier);
#		warn "random with multiplier: $randommult\n";
		return $randommult;
	} else {
		return rand($multiplier);
	}
}

#------------------------------------------------------------------------------
# Generate funky random password:
#------------------------------------------------------------------------------
sub random_pw_generate {
	local(@passset, $rnd_passwd, $randum_num);
	local($randum_num);

	# Since 1 ~= l and 0 =~ O, don't generate passwords with them.
	# This will just confuse people using ugly fonts.
	local(@passset) = ('a'..'h', 'j'..'k', 'm'..'n', 'p'..'z', '2'..'9');

	if (@_) {
		@passset = split('', shift(@_));
	}

#	warn join(' ', @passset) . "\n";

	$rnd_passwd = "";
	for ($i = 0; $i < 8; $i++) {
	        $randum_num = int(myrand($#passset + 1));
	        $rnd_passwd .= $passset[$randum_num];
	}
	return $rnd_passwd;
}


#-----------------------------------------------------------------------------
# Make a random two character salt
#-----------------------------------------------------------------------------

sub random_salt_generate {

	local(@saltset) = ('a'..'z', 'A'..'Z', '0'..'9', '.', '/');
	local($random_num);

	for ($random_num = myrand(39); $random_num > 0; $random_num--) {
		myrand($random_num);
	}
	return $saltset[int(myrand(64))] . $saltset[int(myrand(64))];

}

#-----------------------------------------------------------------------------
# Returns its argument encrypted with a random salt.
#-----------------------------------------------------------------------------
sub pw_encrypt {
	local($passwd) = shift(@_);
	local($salt) = shift(@_);

	if (!defined($salt)) {
		$salt = random_salt_generate();
	}
 
	return crypt($passwd, $salt);

}


1;

#
# $Log: randompasswd,v $
# Revision 1.1  2002/07/06 22:35:29  dgregor
# xtelnet:
# 	counterpart to xssh, can be used on its own or with xtelnetchoose
#
# base64.pl:
# 	library for base64dec base64enc
#
# base64dec base64enc:
# 	base 64 encoder and decoder
#
# randompasswd:
# 	random 8 character password generator, includes crypt()
# 	hash of password.  Does not dependon base64.pl.
#
# Revision 1.1.1.1  1997/01/24 20:16:59  root
# Mail-related Perl 'library' files
#
# Revision 1.1  1997/01/23 21:00:40  root
# Initial revision
#
# Revision 1.1  1997/01/17 20:17:56  root
# Initial revision
#
# Revision 1.2  1996/09/18 13:36:10  gregord
# Add Id and Log
#
#

$password = random_pw_generate(@ARGV);
$cryptpassword = pw_encrypt($password);

print "$password\t$cryptpassword\n";
