source: trunk/LATMOS-Accounts/bin/la-gen-passwd @ 1427

Last change on this file since 1427 was 1151, checked in by nanardon, 12 years ago

add tools to generate random password

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use LATMOS::Accounts::Utils;
7use Getopt::Long;
8use 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
24Use this configuration directory instead of the default one.
25
26=item -b|--base basename
27
28Query this specific base instead of the default one.
29
30=item -s|--sync syncname
31
32Use this synchronisation
33
34=item -l length
35
36Generate password of C<length> characters
37
38=item -p
39
40Include non alpha-numeric characters (such as C<#>, C<:>, etc.).
41
42=item --syl
43
44Use syllable instead full random characters
45
46=item -o file
47
48Store the list of username and generated password into F<file>.
49
50=back
51
52=cut
53
54my %gen_options;
55
56GetOptions(
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
67my $otype = 'user';
68
69my $LA = LATMOS::Accounts->new($config, noacl => 1);
70my $labase = $base ? $LA->base($base) : $LA->sync_access($sync);
71$labase && $labase->load or die "Cannot load base";
72
73$labase->wexported(0);
74
75my $handle;
76if ($output) {
77    open($handle, '>', $output) ||
78        die "Cannot open $output file $!\n";
79}
80
81foreach 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
100close($handle) if ($handle);
101
102exit 0;
Note: See TracBrowser for help on using the repository browser.