source: LATMOS-Accounts/bin/la-crypt-passwd @ 664

Last change on this file since 664 was 664, checked in by nanardon, 14 years ago
  • tools now show non exported objects by default, use --noexp to hide them
  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts::Maintenance;
6use Getopt::Long;
7use Pod::Usage;
8use Term::ReadKey;
9use Crypt::RSA;
10
11=head1 NAME
12
13    la-crypt-passwd - Tools to managed rsa crypted password in LATMOS Account system
14
15=head1 SYNOPSIS
16
17    la-crypt-passwd [options] [--genkey|--regen] [--set BASE]
18
19=cut
20
21GetOptions(
22    'c|config=s' => \my $config,
23    'help'       => sub { pod2usage(0) },
24    'genkey'     => \my $genkey,
25    'regen'      => \my $regen,
26    'set=s'      => \my $set,
27) or pod2usage();
28
29=head1 OPTIONS
30
31=over 4
32
33=item -c|--config configfile
34
35Use this configuration file instead of the default one.
36
37=item --genkey
38
39Generate a RSA key and store it into database
40
41If one is already present, use regen to force generation of a new one
42
43=item --regen
44
45Like --genkey but a new key will replace the current one if already present.
46Stored password will be read and encrypted again using the new key.
47
48=item --set BASE
49
50Read password from database, decrypt it and then set it in BASE given as
51argument.
52
53=back
54
55=cut
56
57my $LA = LATMOS::Accounts::Maintenance->new($config);
58$LA->wexported(1);
59
60my $clear;
61
62sub get_clear_password {
63    $clear and return $clear;
64    my %encpasswd = $LA->get_rsa_password;
65    scalar(keys %encpasswd) or return {};
66    ReadMode('noecho');
67    print "Enter password for current passphrase: ";
68    my $password = ReadLine(0);
69    ReadMode 0;
70    print "\n";
71    my $private_key = $LA->private_key($password) or
72        die "Cannot get private key\n";
73    my $rsa = new Crypt::RSA ES => 'PKCS1v15';
74    my %clear_passwd;
75    foreach (keys %encpasswd) {
76        my $clearp = $rsa->decrypt (
77                Cyphertext => $encpasswd{$_},
78                Key        => $private_key,
79                Armour     => 1,
80        );
81        if (defined $clearp) {
82            $clear_passwd{$_} = $clearp;
83        } else {
84            warn "$_ :" . $rsa->errstr();
85        }
86    }
87    return \%clear_passwd;
88}
89
90if ($set) {
91    if (!$LA->_base->get_global_value('rsa_private_key')) {
92        warn "No rsa key found in database\n";
93    }
94    my $destbase = $LA->base($set) or die "Cannot get base $set\n";
95    my $clearpasswd = get_clear_password();
96    foreach (keys %$clearpasswd) {
97        my $obj = $destbase->get_object('user', $_) or do {
98            warn "Cannot find user $_ in destination base, need sync ?\n";
99            next;
100        };
101        $obj->set_password($clearpasswd->{$_}) and
102            print "Password set for $_\n";
103    }
104    $destbase->commit;
105} elsif ($regen || $genkey) {
106    if ($LA->_base->get_global_value('rsa_private_key') && !$regen) {
107        die <<EOF;
108A rsa key were found in database please use --regen to force a new key
109generation. Notice thiss will force decrypt current stored password to encypted
110it again
111EOF
112    }
113
114    my $clearpasswd = get_clear_password();
115    ReadMode('noecho');
116    print "Enter password for new passphrase: ";
117    my $password = ReadLine(0);
118    ReadMode 0;
119    print "\n";
120    my ($public, $private) = $LA->generate_rsa_key($password);
121
122    $LA->store_rsa_key($public, $private);
123    my $base = $LA->_base;
124    $base->wexported(1);
125    foreach (keys %$clearpasswd) {
126        my $obj = $base->get_object('user', $_);
127        $obj->set_password($clearpasswd->{$_});
128    }
129    $base->commit;
130} else {
131    if ($LA->_base->get_global_value('rsa_private_key')) {
132        my $clearpasswd = get_clear_password();
133        foreach (keys %$clearpasswd) {
134            printf("%s: %s\n", $_, $clearpasswd->{$_});
135        }
136    } else {
137        warn "No rsa key found in database\n";
138    }
139}
Note: See TracBrowser for help on using the repository browser.