| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use 5.006001; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use File::Basename qw(basename); | ||
| 7 | use Getopt::Long qw(GetOptions); | ||
| 8 | use String::MkPasswd qw(mkpasswd); | ||
| 9 | use Text::Wrap qw(wrap); | ||
| 10 | |||
| 11 | # Defaults. | ||
| 12 | use constant LENGTH => 9; | ||
| 13 | use constant MINNUM => 2; | ||
| 14 | use constant MINLOWER => 2; | ||
| 15 | use constant MINUPPER => 2; | ||
| 16 | use constant MINSPECIAL => 1; | ||
| 17 | use constant DISTRIBUTE => ""; | ||
| 18 | use constant NOAMBIGUOUS => 0; | ||
| 19 | #use constant VERBOSE => ""; | ||
| 20 | #use constant PASSWD => "/bin/passwd"; | ||
| 21 | |||
| 22 | # Configuration. | ||
| 23 | my $length = LENGTH; | ||
| 24 | my $minnum = MINNUM; | ||
| 25 | my $minlower = MINLOWER; | ||
| 26 | my $minupper = MINUPPER; | ||
| 27 | my $minspecial = MINSPECIAL; | ||
| 28 | my $distribute = DISTRIBUTE; | ||
| 29 | my $noambiguous = NOAMBIGUOUS; | ||
| 30 | #my $verbose = VERBOSE; | ||
| 31 | #my $passwd = PASSWD; | ||
| 32 | my $help = ""; | ||
| 33 | |||
| 34 | Getopt::Long::Configure("bundling"); | ||
| 35 | my $getopt = GetOptions( | ||
| 36 | "length|l=i" => \$length, | ||
| 37 | "digits|d=i" => \$minnum, | ||
| 38 | "lower|c=i" => \$minlower, | ||
| 39 | "upper|C=i" => \$minupper, | ||
| 40 | "special|s=i" => \$minspecial, | ||
| 41 | "distribute|2" => \$distribute, | ||
| 42 | "noambiguous|n" => \$noambiguous, | ||
| 43 | #"verbose|v" => \$verbose, | ||
| 44 | #"passwd|p" => \$passwd, | ||
| 45 | "help|h" => \$help, | ||
| 46 | |||
| 47 | # Getopt::Long doesn't support combining '--no' with options that take | ||
| 48 | # arguments, so this is just my way of faking it. | ||
| 49 | "nodigits|no-digits" => sub { $minnum = 0 }, | ||
| 50 | "nolower|no-lower" => sub { $minlower = 0 }, | ||
| 51 | "noupper|no-upper" => sub { $minupper = 0 }, | ||
| 52 | "nospecial|no-special" => sub { $minspecial = 0 }, | ||
| 53 | "noambiguous|no-ambiguous|no-lookalike|nolookalike" => sub { $noambiguous = 1 }, | ||
| 54 | ); | ||
| 55 | |||
| 56 | if ( $help ) { | ||
| 57 | &usage(); | ||
| 58 | exit 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | if ( !$getopt ) { | ||
| 62 | &usage(); | ||
| 63 | exit 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | my $pass = mkpasswd( | ||
| 67 | -length => $length, | ||
| 68 | -minnum => $minnum, | ||
| 69 | -minlower => $minlower, | ||
| 70 | -minupper => $minupper, | ||
| 71 | -minspecial => $minspecial, | ||
| 72 | -distribute => $distribute, | ||
| 73 | -noambiguous => $noambiguous, | ||
| 74 | ); | ||
| 75 | |||
| 76 | if ( !$pass ) { | ||
| 77 | $Text::Wrap::columns = 72; | ||
| 78 | print STDERR wrap("", "", | ||
| 79 | "Impossible to generate $length-character password with $minnum " | ||
| 80 | . "numbers, $minlower lowercase letters, $minupper uppercase letters " | ||
| 81 | . "and $minspecial special characters.\n" | ||
| 82 | ); | ||
| 83 | exit 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | print "$pass\n"; | ||
| 87 | exit 0; | ||
| 88 | |||
| 89 | sub usage { | ||
| 90 | print <<EOF; | ||
| 91 | Usage: @{[ basename $0 ]} [-options] | ||
| 92 | -l # | --length=# length of password (default = @{[ LENGTH ]}) | ||
| 93 | -d # | --digits=# min # of digits (default = @{[ MINNUM ]}) | ||
| 94 | -c # | --lower=# min # of lowercase chars (default = @{[ MINLOWER ]}) | ||
| 95 | -C # | --upper=# min # of uppercase chars (default = @{[ MINUPPER ]}) | ||
| 96 | -s # | --special=# min # of special chars (default = @{[ MINSPECIAL ]}) | ||
| 97 | -2 | --distribute alternate hands | ||
| 98 | -n | --noambiguous don't include chars that might be mistaken for others | ||
| 99 | --nodigits alias for --digits=0 | ||
| 100 | --nolower alias for --lower=0 | ||
| 101 | --noupper alias for --upper=0 | ||
| 102 | --nospecial alias for --special=0 | ||
| 103 | --nolookalike alias for --noambiguous | ||
| 104 | EOF | ||
| 105 | } | ||
| 106 | |||
| 107 | __END__ |