Changeset 1935 for trunk


Ignore:
Timestamp:
01/17/17 17:38:36 (7 years ago)
Author:
nanardon
Message:

Add a way to inject UNIX password into base supporting it

Location:
trunk/LATMOS-Accounts
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/bin/la-passwd

    r1755 r1935  
    3636Don't change the password but check its validity using CrackLib. 
    3737 
     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 
    3861=back 
    3962 
     
    4669    's|sync=s'   => \my $sync, 
    4770    'f|force'    => \my $force, 
     71    'crypt'      => \my $crypt, 
     72    'i|input=s'  => \my $input, 
    4873    'help'       => sub { pod2usage(0) }, 
    4974) or pod2usage(); 
    50  
    51 if (!$ARGV[0]) {warn "You must specify 'userid', aborting\n"; pod2usage(); } 
    52  
    53 my $otype = 'user'; 
    5475 
    5576my $LA = LATMOS::Accounts->new($config, noacl => 1); 
     
    5980$labase->wexported(1); 
    6081 
    61 my $obj = $labase->get_object($otype, $ARGV[0]) or do { 
    62     die "Object $otype $ARGV[0] not found\n"; 
     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        print "Cannot set bad password, use --force to bypass security\n" unless($force); 
     89        return 0; 
     90    } 
     91 
     92    return 1  if($test); 
     93 
     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    } 
     102} 
     103 
     104if (!$ARGV[0] && !$input) {warn "You must specify 'userid', aborting\n"; pod2usage(); } 
     105 
     106my ($username, $password) = @ARGV; 
     107 
     108my $otype = 'user'; 
     109 
     110 
     111if ($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 
     139} else { 
     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    }  
    63161}; 
    64162 
    65 ReadMode('noecho'); 
    66 print "Enter password: "; 
    67 my $password = ReadLine(0); 
    68 ReadMode 0; 
    69 print "\n"; 
    70 chomp($password); 
    71  
    72 my $res = $obj->check_password($password); 
    73  
    74 if ($res !~ /^ok$/) { 
    75     print "Password quality: " . $res . "\n"; 
    76     die "Cannot set bad password, use --force to bypass security\n" unless($force); 
    77 } 
    78  
    79 exit(0)  if($test); 
    80  
    81 if ($obj->set_password($password)) { 
    82     print "Password succefully changed\n"; 
    83     $labase->commit; 
    84     exit 0; 
    85 } else { 
    86     warn "Error when trying to change password\n"; 
    87     exit 1; 
    88 } 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/User.pm

    r1865 r1935  
    101101        }, 
    102102        homeDirectory => { }, 
    103         userPassword => { }, 
    104103        loginShell => { }, 
    105104        gecos => { }, 
     
    298297} 
    299298 
     299=head2 _InjectCryptPasswd($cryptpasswd) 
     300 
     301Inject a password encrypted using standard UNIX method. 
     302 
     303Works only for unix authentification method inside LDAP 
     304 
     305=cut 
     306 
     307sub _InjectCryptPasswd { 
     308    my ($self, $cryptpasswd) = @_; 
     309 
     310    my $res = $self->set_fields( 
     311        userPassword => '{CRYPT}' . $cryptpasswd, 
     312    ); 
     313 
     314    if ($res) { 
     315        $self->base->log(LA_NOTICE, 'Crypted password injected for %s', $self->id); 
     316        return 1; 
     317    } else { 
     318        $self->base->log(LA_ERR, 'Cannot inject crypted password for %s', $self->id); 
     319        return 0; 
     320    } 
     321} 
     322 
    3003231; 
    301324 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r1865 r1935  
    551551 
    552552    return fascist_check($password, $dictionary); 
     553} 
     554 
     555=head2 InjectCryptPasswd($cryptpasswd) 
     556 
     557Inject a password encrypted using standard UNIX method. 
     558 
     559=cut 
     560 
     561sub InjectCryptPasswd { 
     562    my ($self, $cryptpasswd) = @_; 
     563 
     564    if ($self->can('_InjectCryptPasswd')) { 
     565        return $self->_InjectCryptPasswd($cryptpasswd); 
     566    } else { 
     567        $self->base->log('Injecting unix crypt password is not supported'); 
     568        return; 
     569    } 
    553570} 
    554571 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1922 r1935  
    18691869} 
    18701870 
     1871=head2 _InjectCryptPasswd($cryptpasswd) 
     1872 
     1873Inject a password encrypted using standard UNIX method. 
     1874 
     1875The passwrod will be used to authenticate user inside the application but it 
     1876will not be transmit to any other database. 
     1877 
     1878=cut 
     1879 
     1880sub _InjectCryptPasswd { 
     1881    my ($self, $cryptpasswd) = @_; 
     1882 
     1883    if (my $current = $self->get_field('userPassword')) { 
     1884        if ($cryptpasswd eq $current) { 
     1885            return 1; 
     1886        } 
     1887    } 
     1888    my $res = $self->set_fields('userPassword', $cryptpasswd); 
     1889 
     1890    if ($res) { 
     1891        $self->base->log(LA_NOTICE, 'Crypted password injected for %s', $self->id); 
     1892        return 1; 
     1893    } else { 
     1894        $self->base->log(LA_ERR, 'Cannot inject crypted password for %s', $self->id); 
     1895        return 0; 
     1896    } 
     1897} 
    18711898 
    18721899=head2 GenPasswordResetId 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Unix/User.pm

    r1931 r1935  
    138138} 
    139139 
     140=head2 _InjectCryptPasswd($cryptpasswd) 
     141 
     142Inject a password encrypted using standard UNIX method. 
     143 
     144=cut 
     145 
     146sub _InjectCryptPasswd { 
     147    my ($self, $cryptpasswd) = @_; 
     148 
     149    my $res = $self->set_c_fields( 
     150        userPassword => $cryptpasswd, 
     151    ); 
     152 
     153    if ($res) { 
     154        $self->base->log(LA_NOTICE, 'Crypted password injected for %s', $self->id); 
     155        return 1; 
     156    } else { 
     157        $self->base->log(LA_ERR, 'Cannot inject crypted password for %s', $self->id); 
     158        return 0; 
     159    } 
     160} 
     161 
    1401621; 
    141163 
Note: See TracChangeset for help on using the changeset viewer.