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