chiark / gitweb /
Update to GPLv3+; update copyright notices everywhere.
[userv-utils.git] / misc / checkpasswd-service
1 #!/usr/bin/perl -w
2 # checkpasswd-service
3 # part of userv-utils
4
5 # protocols:
6 #
7 #   userv root checkpasswd-self <<'END'
8 # < PASSWORD
9 # < ^D
10 # > STATUS MESSAGE...
11 #
12 #   userv root checkpasswd-other USERNAME <<'END'
13 # < PASSWORD
14 # < ^D
15 # > STATUS MESSAGE...
16 #
17 # STATUS MESSAGE may be
18 #   0 ok
19 #   2 incorrect password
20 #   4 no such user
21 #   5 password disabled
22
23 # Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
24 # Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
25 # Copyright 1999,2003
26 #    Chancellor Masters and Scholars of the University of Cambridge
27 # Copyright 2010 Tony Finch <fanf@dotat.at>
28 #
29 # This is free software; you can redistribute it and/or modify it
30 # under the terms of the GNU General Public License as published by
31 # the Free Software Foundation; either version 3 of the License, or
32 # (at your option) any later version.
33 #
34 # This program is distributed in the hope that it will be useful, but
35 # WITHOUT ANY WARRANTY; without even the implied warranty of
36 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37 # General Public License for more details.
38 #
39 # You should have received a copy of the GNU General Public License
40 # along with userv-utils; if not, see http://www.gnu.org/licenses/.
41
42
43 use strict;
44 use IO::File;
45 use Fcntl qw(:flock);
46
47 my ($lockpath, $delay, $separator, $username) = @ARGV;
48
49 die "$0: bad usage\n" unless
50     @ARGV == 4 ||
51     $lockpath =~ m#^/# ||
52     $delay =~ m/^[0-9.]+$/ ||
53     $separator eq '--' ||
54     $username =~ m/^\w/;
55
56 $username = $ENV{'USERV_USER'} if $username eq 'SELF';
57
58 sub result {
59     print "@_\n" or die $!;
60     exit 0;
61 }
62
63 my @pwent = getpwnam($username);
64 result 4, "no such user" unless @pwent;
65
66 my $encrpw= $pwent[1];
67 result 5, "password disabled" unless length $encrpw >= 13;
68
69 $!=0; my $pw = <STDIN>;
70 chomp $pw or die "reading password: $!\n";
71
72 my $lockf = new IO::File $lockpath, "w+" or die "open $lockpath: $!\n";
73 flock($lockf, LOCK_EX) or die "lock $lockpath: $!\n";
74 select(undef,undef,undef,0.5);
75 close $lockf;
76
77 my $crval = crypt($pw,$encrpw);
78
79 result 2, "incorrect password" unless  $crval eq $encrpw;
80
81 result 0, "ok";