source: trunk/LATMOS-Accounts/bin/la-passwd @ 2338

Last change on this file since 2338 was 2053, checked in by nanardon, 7 years ago

Remove end of line when using --input

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use Term::ReadKey;
9
10=head1 NAME
11
12    la-passwd - set user password                                         
13                                                                           
14=head1 SYNOPSIS                                                           
15                                                                             
16    la-passwd [options] userid                                             
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 -t|--test
35
36Don't change the password but check its validity using CrackLib.
37
38=item -f|--force
39
40Change the even it is weak (has no effect with --crypt)
41
42=item --crypt
43
44The given password is crypt using standard unix method and will be injected
45as is inside the base. Works only for base supporting this, and some form of
46password may not be updated.
47
48=item --input FILENAME
49
50Read login/password from a file in form
51
52    username:password
53
54or
55    username:password:...
56
57Everything after the second colon is ignored
58
59If C<FILENAME> is C<->, standard input is used
60
61=back
62
63=cut
64
65GetOptions(
66    'c|config=s' => \my $config,
67    'b|base=s'   => \my $base,
68    't|test'     => \my $test,
69    's|sync=s'   => \my $sync,
70    'f|force'    => \my $force,
71    'crypt'      => \my $crypt,
72    'i|input=s'  => \my $input,
73    'help'       => sub { pod2usage(0) },
74) or pod2usage();
75
76my $LA = LATMOS::Accounts->new($config, noacl => 1);
77my $labase = $base ? $LA->base($base) : $LA->sync_access($sync);
78$labase && $labase->load or die "Cannot load base";
79
80$labase->wexported(1);
81
82sub set_passwd {
83    my ($obj, $password) = @_;
84    my $res = $obj->check_password($password);
85
86    if ($res !~ /^ok$/) {
87        print "Password quality: " . $res . "\n";
88        unless($force) {
89            print "Cannot set bad password, use --force to bypass security\n";
90            return 0;
91        }
92    }
93
94    return 1  if($test);
95
96    if ($obj->set_password($password)) {
97        print "Password succefully changed\n";
98        $labase->commit;
99        return 1;
100    } else {
101        warn "Error when trying to change password\n";
102        return 0;
103    }
104}
105
106if (!$ARGV[0] && !$input) {warn "You must specify 'userid', aborting\n"; pod2usage(); }
107
108my ($username, $password) = @ARGV;
109
110my $otype = 'user';
111
112
113if ($input) {
114    my $handle;
115    if ($input eq '-') {
116        $handle = \*STDIN;
117    } else {
118        open($handle, '<', $input) or die "Cannot open $input: $!\n";
119    }
120
121    while (<$handle>) {
122        chomp();
123        my ($username, $password) = split(/:/);
124        $username or next;
125        $password or next;
126
127        my $obj = $labase->get_object($otype, $username) or do {
128            warn "Object $otype $ARGV[0] not found\n";
129            next;
130        };
131
132        print "Updating user $username\n";
133        if ($crypt) {
134            if ($obj->InjectCryptPasswd($password)) {
135                $labase->commit;
136            }
137        } else {
138            set_passwd($obj, $password);
139        }
140    }
141
142} else {
143    my $obj = $labase->get_object($otype, $username) or do {
144        die "Object $otype $ARGV[0] not found\n";
145    };
146
147    unless($password) {
148        ReadMode('noecho');
149        print "Enter password: ";
150        $password = ReadLine(0);
151        ReadMode 0;
152        print "\n";
153        chomp($password);
154    }
155
156    if ($crypt) {
157        if ($obj->InjectCryptPasswd($password)) {
158            $labase->commit;
159            exit(0);
160        }
161    } else {
162        exit (!set_passwd($obj, $password));
163    } 
164};
165
Note: See TracBrowser for help on using the repository browser.