Additions to authentication scheme. Logs to /tmp/sessionlog. Will move this
[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     my $sessionID=$query->cookie('sessionID');
22     my $message='';
23     warn "SID: ".$sessionID;
24
25     my $dbh=C4Connect();
26     my $sth=$dbh->prepare("select userid,ip,lasttime from sessions where sessionid=?");
27     $sth->execute($sessionID);
28     if ($sth->rows) {
29         my ($userid, $ip, $lasttime) = $sth->fetchrow;
30         if ($lasttime<time()-20) {
31             # timed logout
32             warn "$sessionID logged out due to inactivity.";
33             $message="You have been logged out due to inactivity.";
34             my $sti=$dbh->prepare("delete from sessions where sessionID=?");
35             $sti->execute($sessionID);
36             open L, ">>/tmp/sessionlog";
37             print L "$userid from $ip logged out at ".localtime(time())." (inactivity).\n";
38             close L;
39         } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
40             # Different ip than originally logged in from
41             warn "$sessionID came from a new ip address.";
42             $message="ERROR ERROR ERROR ERROR<br>Attempt to re-use a cookie from a different ip address.";
43         } else {
44             my $cookie=$query->cookie(-name => 'sessionID',
45                                       -value => $sessionID,
46                                       -expires => '+1y');
47             warn "$sessionID had a valid cookie.";
48             my $sti=$dbh->prepare("update sessions set lasttime=? where sessionID=?");
49             $sti->execute(time(), $sessionID);
50             return ($userid, $cookie, $sessionID);
51         }
52     }
53
54
55
56     warn "$sessionID wasn't in sessions table.";
57     
58     ($sessionID) || ($sessionID=int(rand()*100000).'-'.time());
59     my $userid=$query->param('userid');
60     my $password=$query->param('password');
61     if ($userid eq 'librarian' && $password eq 'koha') {
62         my $sti=$dbh->prepare("insert into sessions (sessionID, userid, ip,lasttime) values (?, ?, ?, ?)");
63         $sti->execute($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time());
64         open L, ">>/tmp/sessionlog";
65         print L "$userid from ".$ENV{'REMOTE_ADDR'}." logged in at ".localtime(time()).".\n";
66         close L;
67         return ($userid, $sessionID, $sessionID);
68     } elsif ($userid eq 'patron' && $password eq 'koha') {
69         my $sti=$dbh->prepare("insert into sessions (sessionID, userid, ip,lasttime) values (?, ?, ?, ?)");
70         $sti->execute($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time());
71         open L, ">>/tmp/sessionlog";
72         print L "$userid from ".$ENV{'REMOTE_ADDR'}." at ".localtime(time()).".\n";
73         close L;
74         return ($userid, $sessionID, $sessionID);
75     } else {
76         if ($userid) {
77             $message="Invalid userid or password entered.";
78         }
79         my $parameters;
80         foreach (param $query) {
81             $parameters->{$_}=$query->{$_};
82         }
83         my $cookie=$query->cookie(-name => 'sessionID',
84                                   -value => $sessionID,
85                                   -expires => '+1y');
86         print $query->header(-cookie=>$cookie);
87         print qq|
88 <html>
89 <body background=/images/kohaback.jpg>
90 <center>
91 <h2>$message</h2>
92 <form method=post>
93 <table border=1>
94 <tr><th colspan=2><font size=+2>Koha Login</font></th></tr>
95 <tr><td>Name:</td><td><input name=userid></td></tr>
96 <tr><td>Password:</td><td><input type=password name=password></td></tr>
97 <tr><td colspan=2 align=center><input type=submit value=login></td></tr>
98 </table>
99 </form>
100 </body>
101 </html>
102 |;
103         exit
104     }
105 }
106
107
108 END { }       # module clean-up code here (global destructor)