|
|
|
Listing 4
#!/usr/bin/perl
# FILENAME: newhtuser
# DESCRIPTION: Creates new entries for httpd users and appends to filename
# supplied as input
#
# USAGE: newhtuser [file]
#
# LABEN S.p.A. - 29-jan-1998
#
# HISTORY:
# 0.0 Luca Salvadori <salvadori.l@laben.it> 29-jan-1998
# - Functions and behaviour
#
#
########################## S U B R O U T I N E S ###########################
sub rndpass()
# Generates a random string long as requested and composed by
# consonant-vowel or vowel-consonant pairs.
{
local(@cons=('b','c','d','f','g','h','k','l','m','n','p','q','r','s','t','v','w','x','z'));
local(@voc=('a','e','i','j','o','u','y'));
local($pwdlen,$pwd);
local($i,$ii,$a,$nc,$nv,$even);
if (! $_[0] ) {$pwdlen=8} else {$pwdlen=$_[0]};
if ((int($pwdlen / 2) * 2) != $pwdlen) {$even="false"};
$a=int($pwdlen /2);
srand(abs(time * getppid));
$ii=int(rand 2);
$i=1;
if ($ii == 1)
{
while ($i <= $a)
{
$nc=int(rand 19);
$nv=int(rand 7);
$pwd.=$cons[$nc].$voc[$nv];
$i++
}
if ($even eq "false") {$pwd.=$cons[$nc]};
}
else
{
while ($i <= $a)
{
$nc=int(rand 19);
$nv=int(rand 7);
$pwd.=$voc[$nv].$cons[$nc];
$i++
}
if ($even eq "false") {$pwd.=$voc[$nv]};
}
return $pwd;
}
sub cleanup()
# Closes output file and exits
{
close(OUTFILE);
exit(0);
}
##################### E N D O F S U B R O U T I N E S ####################
########################## M A I N P R O G R A M ##########################
# Default output file
$def_outfile="htpasswd";
# If no argument is supplied, then assign defaults
if (! $ARGV[0] ) {$outfile=$def_outfile} else {$outfile=$ARGV[0]};
# Change protection to 600 to output file, just in case
chmod 600, $outfile;
open(OUTFILE,">> $outfile");
# Loop until keyboard input is over
while ()
{
print "Enter username: " ;
chomp($user=<STDIN>);
if ($user eq "") {&cleanup};
print "Enter e-mail address: " ;
chomp($email=<STDIN>);
if ($email eq "") {&cleanup};
# Generate random password
$pwd=&rndpass(8);
# Sleep 1 sec to allow proper srand() re-initialization
sleep 1;
# Generate random salt
$salt=&rndpass(2);
# Crypt password
$cpwd=crypt $pwd,$salt;
# Write record to output file
print OUTFILE "$user:$pwd:$cpwd:$email\n";
}
exit(0) ;
#################### E N D O F M A I N P R O G R A M ####################
|