~ubuntu-branches/ubuntu/utopic/libapache2-mod-authnz-external/utopic-proposed

« back to all changes in this revision

Viewing changes to mysql/mysql-auth.pl

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2013-07-10 22:04:38 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130710220438-s0s188bm8b2z19rw
Tags: 3.3.1-0.1
* Non-maintainer upload.
* New upstream release, suitable for Apache 2.4 (closes: #633638).
* Port packaging to Apache 2.4 (closes: #666815).
* Update debian/watch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -Tw
2
 
# MySQL-auth version 1.0
3
 
# Anders Nordby <anders@fix.no>, 2002-01-20
4
 
# This script is usable for authenticating users against a MySQL database with
5
 
# the Apache module mod_auth_external or mod_authnz_external. See
6
 
# http://unixpapa.com/mod_auth_external/ for mod_auth_external.
7
 
#
8
 
# Updates to this script will be made available on:
9
 
# http://anders.fix.no/software/#unix
10
 
 
11
 
my $dbhost="localhost";
12
 
my $dbuser="validator";
13
 
my $dbpw="whatagoodpassword";
14
 
my $dbname="funkydb";
15
 
my $dbport="3306";
16
 
my $mychars="01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_,.";
17
 
 
18
 
# Below this, only the SQL query should be interesting to modify for users.
19
 
 
20
 
use DBI;
21
 
 
22
 
sub validchars
23
 
{
24
 
        # 0: string 1: valid characters
25
 
        my $streng = $_[0];
26
 
 
27
 
        my $ok = 1;
28
 
        my $i = 0;
29
 
        while ($ok && $i < length($_[0])) {
30
 
                if (index($_[1], substr($_[0],$i,1)) == -1) {
31
 
                        $ok = 0;
32
 
                }
33
 
                $i++;
34
 
        }
35
 
        return($ok);
36
 
}
37
 
 
38
 
# Get the name of this program
39
 
$prog= join ' ',$0,@ARGV;
40
 
$logprefix='[' . scalar localtime . '] ' . $prog;
41
 
 
42
 
# Get the user name
43
 
$user= <STDIN>;
44
 
chomp $user;
45
 
 
46
 
# Get the password name
47
 
$pass= <STDIN>;
48
 
chomp $pass;
49
 
 
50
 
# check for valid characters
51
 
if (!validchars($user, $mychars) || !validchars($pass, $mychars)) {
52
 
        print STDERR "$logprefix: invalid characters used in login/password - Rejected\n";
53
 
        exit 1;
54
 
}
55
 
 
56
 
# check for password in mysql database
57
 
#if 
58
 
my $dbh = DBI->connect("DBI:mysql:database=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpw,{PrintError=>0});
59
 
 
60
 
if (!$dbh) {
61
 
        print STDERR "$logprefix: could not connect to database - Rejected\n";
62
 
        exit 1;
63
 
}
64
 
 
65
 
my $dbq = $dbh->prepare("select username as username, password as password from users where username=?;");
66
 
$dbq->bind_param(1, $user);
67
 
$dbq->execute;
68
 
my $row = $dbq->fetchrow_hashref();
69
 
 
70
 
if ($row->{username} eq "") {
71
 
        print STDERR "$logprefix: could not find user $user - Rejected\n";
72
 
        exit 1;
73
 
}
74
 
if ($row->{password} eq "") {
75
 
        print STDERR "$logprefix: empty password for user $user - Rejected\n";
76
 
        exit 1;
77
 
}
78
 
 
79
 
if ($row->{password} eq crypt($pass,substr($row->{password},0,2))) {
80
 
        print STDERR "$logprefix: password for user $user matches - Accepted\n";
81
 
        exit 0;
82
 
} else {
83
 
        print STDERR "$logprefix: password for user $user does not match - Rejected\n";
84
 
        exit 1;
85
 
}
86
 
 
87
 
$dbq->finish;
88
 
$dbh->disconnect;