#!/usr/bin/perl -w
# this is /usr/local/bin/pwgen
# From slashdot user ajs318 <sd_resp2 AT earthshod DOT co DOT uk> 2006-4-25

my ($password, $salt, $scram, $user, @stuff);

$user = shift || "";

$password = &consonant . &consonant . &vowel . &consonant . &digit . &consonant . &vowel . &consonant;
$salt = '$1$';
foreach (1 .. 8) {
	$salt .= &saltchar;
};
$salt .= '$';
$scram = crypt $password, $salt;

print "\nAJS's password generator - now with no Os or ls!\n";
print "-" x 48 . "\n\n";
print "Password is $password.\n";
print "Scrambled form is '$scram'.\n";

if ($user) {
	if (@stuff = getpwnam $user) {
		system "usermod -p'$scram' $user";
		print "Set password for '$user' to '$password'.\n";
	}
	else {
		print "There is no such user as '$user'.\n";
	};
};

print "\n";

exit;

sub consonant {
	$_ = substr "bcdfghjkLmnpqrstvwxyz", int rand 21, 1;
	tr/a-z/A-Z/ if rand > .75;
	return $_;
}

sub digit {
	$_ = int rand 10;
}

sub saltchar {
	$_ = substr "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ./", int rand 64, 1;
}

sub vowel {
	$_ = substr "aeiou", int rand 5, 1;
	tr/aeiu/AEIU/ if rand > .75;
	return $_;
}

# Copyright 2005-2006 AJS.
#
# Distribution of this program in  Source  Code  form  is  allowed,  with  or
# without  modification, provided that this licence accompanies every copy of
# the program.  Distribution in binary executable form, where applicable,  is
# permitted  only  in conjunction with complete corresponding Source Code and
# build instructions.
#
# Statement of Warranty: the copyright holders  warrant  that  this  program,
# when  run on a properly-functioning computer, will perform substantially as
# indicated by the source code.  No other warranty is made in respect of  the
# program.   If  you  are  in  doubt as to what this program does, you should
# consult a competent programmer.
#
# This licence is in addition to, and is not to be construed as  prejudicing,
# any statutory rights granted to you under the Law of the Land.
