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