]> granicus.if.org Git - apache-authnz-external/blob - mod_authnz_external/mysql/mysql-auth.pl
Fixed an exploitable SQL injection flaw.
[apache-authnz-external] / mod_authnz_external / mysql / mysql-auth.pl
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
69 my $row = $dbq->fetchrow_hashref();
70
71 if ($row->{username} eq "") {
72         print STDERR "$logprefix: could not find user $user - Rejected\n";
73         exit 1;
74 }
75 if ($row->{password} eq "") {
76         print STDERR "$logprefix: empty password for user $user - Rejected\n";
77         exit 1;
78 }
79
80 if ($row->{password} eq crypt($pass,substr($row->{password},0,2))) {
81         print STDERR "$logprefix: password for user $user matches - Accepted\n";
82         exit 0;
83 } else {
84         print STDERR "$logprefix: password for user $user does not match - Rejected\n";
85         exit 1;
86 }
87
88 $dbq->finish;
89 $dbh->disconnect;