[1151] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts; |
---|
| 6 | use LATMOS::Accounts::Utils; |
---|
| 7 | use Getopt::Long; |
---|
| 8 | use Pod::Usage; |
---|
| 9 | |
---|
| 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | la-gen-passwd - set random password to user. |
---|
| 13 | |
---|
| 14 | =head1 SYNOPSIS |
---|
| 15 | |
---|
| 16 | la-gen-passwd [options] userid [userid1 [...]] |
---|
| 17 | |
---|
| 18 | =head1 OPTIONS |
---|
| 19 | |
---|
| 20 | =over 4 |
---|
| 21 | |
---|
| 22 | =item -c|--config configdir |
---|
| 23 | |
---|
| 24 | Use this configuration directory instead of the default one. |
---|
| 25 | |
---|
| 26 | =item -b|--base basename |
---|
| 27 | |
---|
| 28 | Query this specific base instead of the default one. |
---|
| 29 | |
---|
| 30 | =item -s|--sync syncname |
---|
| 31 | |
---|
| 32 | Use this synchronisation |
---|
| 33 | |
---|
| 34 | =item -l length |
---|
| 35 | |
---|
| 36 | Generate password of C<length> characters |
---|
| 37 | |
---|
| 38 | =item -p |
---|
| 39 | |
---|
| 40 | Include non alpha-numeric characters (such as C<#>, C<:>, etc.). |
---|
| 41 | |
---|
| 42 | =item --syl |
---|
| 43 | |
---|
| 44 | Use syllable instead full random characters |
---|
| 45 | |
---|
| 46 | =item -o file |
---|
| 47 | |
---|
| 48 | Store the list of username and generated password into F<file>. |
---|
| 49 | |
---|
| 50 | =back |
---|
| 51 | |
---|
| 52 | =cut |
---|
| 53 | |
---|
| 54 | my %gen_options; |
---|
| 55 | |
---|
| 56 | GetOptions( |
---|
| 57 | 'c|config=s' => \my $config, |
---|
| 58 | 'b|base=s' => \my $base, |
---|
| 59 | 's|sync=s' => \my $sync, |
---|
| 60 | 'p' => \$gen_options{nonalpha}, |
---|
| 61 | 'syl' => \$gen_options{syllables}, |
---|
| 62 | 'l=i' => \$gen_options{length}, |
---|
| 63 | 'o=s' => \my $output, |
---|
| 64 | 'help' => sub { pod2usage(0) }, |
---|
| 65 | ) or pod2usage(); |
---|
| 66 | |
---|
| 67 | my $otype = 'user'; |
---|
| 68 | |
---|
| 69 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
| 70 | my $labase = $base ? $LA->base($base) : $LA->sync_access($sync); |
---|
| 71 | $labase && $labase->load or die "Cannot load base"; |
---|
| 72 | |
---|
| 73 | $labase->wexported(0); |
---|
| 74 | |
---|
| 75 | my $handle; |
---|
| 76 | if ($output) { |
---|
| 77 | open($handle, '>', $output) || |
---|
| 78 | die "Cannot open $output file $!\n"; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | foreach my $user (@ARGV) { |
---|
| 82 | my $obj = $labase->get_object($otype, $user) or do { |
---|
| 83 | warn "Object $otype $user not found\n"; |
---|
| 84 | next; |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | my $password = LATMOS::Accounts::Utils::genpassword( |
---|
| 88 | %gen_options |
---|
| 89 | ); |
---|
| 90 | |
---|
| 91 | if ($obj->set_password($password)) { |
---|
| 92 | print "$user $password\n"; |
---|
| 93 | print $handle "$user $password\n" if ($handle); |
---|
| 94 | $labase->commit; |
---|
| 95 | } else { |
---|
| 96 | warn "Error when trying to change password for $user\n"; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | close($handle) if ($handle); |
---|
| 101 | |
---|
| 102 | exit 0; |
---|