chiark / gitweb /
checkpasswd: initial implementation
[userv-utils.git] / misc / checkpasswd-service
1 #!/usr/bin/perl -w
2 use strict;
3 use IO::File;
4 use Fcntl qw(:flock);
5
6 die "$0: bad usage\n" unless @ARGV==1 && $ARGV[0] !~ m/^-/;
7 my $username = shift @ARGV;
8 $username = $ENV{'USERV_USER'} if $username eq 'SELF';
9
10 sub result {
11     print "@_\n" or die $!;
12     exit 0;
13 }
14
15 my @pwent = getpwnam($username);
16 result 4, "no such user" unless @pwent;
17
18 my $encrpw= $pwent[1];
19 result 5, "password disabled" unless length $encrpw >= 13;
20
21 $!=0; my $pw = <STDIN>;
22 chomp $pw or die "reading password: $!\n";
23
24 my $lockpath = "/var/run/checkpasswd.synch";
25 my $lockf = new IO::File $lockpath, "w+" or die "open $lockpath: $!\n";
26 flock($lockf, LOCK_EX) or die "lock $lockpath: $!\n";
27 select(undef,undef,undef,0.5);
28 close $lockf;
29
30 my $crval = crypt($pw,$encrpw);
31
32 result 2, "incorrect password" unless  $crval eq $encrpw;
33
34 result 0, "ok";