Auth.pm now checks the password againts a new field in the borrowers table
[koha.git] / C4 / Auth.pm
1 package C4::Auth;
2
3 use strict;
4 require Exporter;
5 use C4::Database;
6
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9 # set the version for version checking
10 $VERSION = 0.01;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw(
14              &checkauth
15 );
16
17
18
19 sub checkauth {
20     my $query=shift;
21     # $authnotrequired will be set for scripts which will run without authentication
22     my $authnotrequired=shift;
23     if (my $userid=$ENV{'REMOTE_USERNAME'}) {
24         # Using Basic Authentication, no cookies required
25         my $cookie=$query->cookie(-name => 'sessionID',
26                                   -value => '',
27                                   -expires => '+1y');
28         return ($userid, $cookie, '');
29     }
30     my $sessionID=$query->cookie('sessionID');
31     my $message='';
32     warn "SID: ".$sessionID;
33
34     my $dbh=C4Connect();
35     my $sth=$dbh->prepare("select userid,ip,lasttime from sessions where sessionid=?");
36     $sth->execute($sessionID);
37     if ($sth->rows) {
38         my ($userid, $ip, $lasttime) = $sth->fetchrow;
39         if ($lasttime<time()-40 && $userid ne 'tonnesen') {
40             # timed logout
41             warn "$sessionID logged out due to inactivity.";
42             $message="You have been logged out due to inactivity.";
43             my $sti=$dbh->prepare("delete from sessions where sessionID=?");
44             $sti->execute($sessionID);
45             open L, ">>/tmp/sessionlog";
46             my $time=localtime(time());
47             printf L "%20s from %16s logged out at %30s (inactivity).\n", $userid, $ip, $time;
48             close L;
49         } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
50             # Different ip than originally logged in from
51             my $newip=$ENV{'REMOTE_ADDR'};
52             warn "$sessionID came from a new ip address (authenticated from $ip, this request from $newip).";
53
54             $message="ERROR ERROR ERROR ERROR<br>Attempt to re-use a cookie from a different ip address.<br>(authenticated from $ip, this request from $newip)";
55         } else {
56             my $cookie=$query->cookie(-name => 'sessionID',
57                                       -value => $sessionID,
58                                       -expires => '+1y');
59             warn "$sessionID had a valid cookie.";
60             my $sti=$dbh->prepare("update sessions set lasttime=? where sessionID=?");
61             $sti->execute(time(), $sessionID);
62             return ($userid, $cookie, $sessionID);
63         }
64     }
65
66
67
68     warn "$sessionID wasn't in sessions table.";
69     if ($authnotrequired) {
70         my $cookie=$query->cookie(-name => 'sessionID',
71                                   -value => '',
72                                   -expires => '+1y');
73         return('', $cookie, '');
74     } else {
75         ($sessionID) || ($sessionID=int(rand()*100000).'-'.time());
76         my $userid=$query->param('userid');
77         my $password=$query->param('password');
78         if (checkpw($dbh, $userid, $password)) {
79         #if (($userid eq 'librarian' || $userid eq 'tonnesen' || $userid eq 'patron') && $password eq 'koha') {
80             my $sti=$dbh->prepare("insert into sessions (sessionID, userid, ip,lasttime) values (?, ?, ?, ?)");
81             $sti->execute($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time());
82             open L, ">>/tmp/sessionlog";
83             my $time=localtime(time());
84             printf L "%20s from %16s logged in  at %30s.\n", $userid, $ENV{'REMOTE_ADDR'}, $time;
85             close L;
86             return ($userid, $sessionID, $sessionID);
87         } else {
88             if ($userid) {
89                 $message="Invalid userid or password entered.";
90             }
91             my $parameters;
92             foreach (param $query) {
93                 $parameters->{$_}=$query->{$_};
94             }
95             my $cookie=$query->cookie(-name => 'sessionID',
96                                       -value => $sessionID,
97                                       -expires => '+1y');
98             print $query->header(-cookie=>$cookie);
99             print qq|
100 <html>
101 <body background=/images/kohaback.jpg>
102 <center>
103 <h2>$message</h2>
104
105 <form method=post>
106 <table border=0 cellpadding=10 cellspacing=0 width=60%>
107     <tr><td align=center valign=top>
108
109     <table border=0 bgcolor=#dddddd cellpadding=10 cellspacing=0>
110     <tr><th colspan=2 background=/images/background-mem.gif><font size=+2>Koha Login</font></th></tr>
111     <tr><td>Name:</td><td><input name=userid></td></tr>
112     <tr><td>Password:</td><td><input type=password name=password></td></tr>
113     <tr><td colspan=2 align=center><input type=submit value=login></td></tr>
114     </table>
115     
116     </td><td align=center valign=top>
117
118     <table border=0 bgcolor=#dddddd cellpadding=10 cellspacing=0>
119     <tr><th background=/images/background-mem.gif><font size=+2>Demo Information</font></th></tr>
120     <td>
121     Log in as librarian/koha or patron/koha.  The timeout is set to 40 seconds of
122     inactivity for the purposes of this demo.  You can navigate to the Circulation
123     or Acquisitions modules and you should see an indicator in the upper left of
124     the screen saying who you are logged in as.  If you want to try it out with
125     a longer timout period, log in as tonnesen/koha and the timeout period will
126     be 10 minutes.
127     </td>
128     </tr>
129     </table>
130     </td></tr>
131 </table>
132 </form>
133 </body>
134 </html>
135 |;
136             exit;
137         }
138     }
139 }
140
141
142 sub checkpw {
143
144 # This should be modified to allow a select of authentication schemes (ie LDAP)
145 # as well as local authentication through the borrowers tables passwd field
146 #
147     my ($dbh, $userid, $password) = @_;
148     my $sth=$dbh->prepare("select password from borrowers where userid=?");
149     $sth->execute($userid);
150     if ($sth->rows) {
151         my ($cryptpassword) = $sth->fetchrow;
152         if (crypt($password, $cryptpassword) eq $cryptpassword) {
153             return 1;
154         }
155     }
156     my $sth=$dbh->prepare("select password from borrowers where cardnumber=?");
157     $sth->execute($userid);
158     if ($sth->rows) {
159         my ($cryptpassword) = $sth->fetchrow;
160         if (crypt($password, $cryptpassword) eq $cryptpassword) {
161             return 1;
162         }
163     }
164     return 0;
165 }
166
167
168 END { }       # module clean-up code here (global destructor)