2 # NOTE: This file uses 8-character tabs; do not change the tab size!
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA 02111-1307 USA
24 use Digest::MD5 qw(md5_base64);
28 use C4::Output; # to get the template
29 use C4::Interface::CGI::Output;
30 use C4::Circulation::Circ2; # getpatroninformation
32 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
34 # set the version for version checking
39 C4::Auth - Authenticates Koha users
48 my ($template, $borrowernumber, $cookie)
49 = get_template_and_user({template_name => "opac-main.tmpl",
53 flagsrequired => {borrow => 1},
57 -type => guesstype($template->output),
64 The main function of this module is to provide
65 authentification. However the get_template_and_user function has
66 been provided so that a users login information is passed along
67 automatically. This gets loaded into the template.
80 &get_template_and_user
83 =item get_template_and_user
85 my ($template, $borrowernumber, $cookie)
86 = get_template_and_user({template_name => "opac-main.tmpl",
90 flagsrequired => {borrow => 1},
93 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
94 to C<&checkauth> (in this module) to perform authentification.
95 See C<&checkauth> for an explanation of these parameters.
97 The C<template_name> is then used to find the correct template for
98 the page. The authenticated users details are loaded onto the
99 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
100 C<sessionID> is passed to the template. This can be used in templates
101 if cookies are disabled. It needs to be put as and input to every
104 More information on the C<gettemplate> sub can be found in the
110 sub get_template_and_user {
112 my $template = gettemplate($in->{'template_name'}, $in->{'type'});
113 my ($user, $cookie, $sessionID, $flags)
114 = checkauth($in->{'query'}, $in->{'authnotrequired'}, $in->{'flagsrequired'}, $in->{'type'});
118 $template->param(loggedinuser => $user);
119 $template->param(sessionID => $sessionID);
121 $borrowernumber = getborrowernumber($user);
122 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
126 $template->param(USER_INFO => \@bordat);
128 return ($template, $borrowernumber, $cookie);
134 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
136 Verifies that the user is authorized to run this script. If
137 the user is authorized, a (userid, cookie, session-id, flags)
138 quadruple is returned. If the user is not authorized but does
139 not have the required privilege (see $flagsrequired below), it
140 displays an error page and exits. Otherwise, it displays the
141 login page and exits.
143 Note that C<&checkauth> will return if and only if the user
144 is authorized, so it should be called early on, before any
145 unfinished operations (e.g., if you've opened a file, then
146 C<&checkauth> won't close it for you).
148 C<$query> is the CGI object for the script calling C<&checkauth>.
150 The C<$noauth> argument is optional. If it is set, then no
151 authorization is required for the script.
153 C<&checkauth> fetches user and session information from C<$query> and
154 ensures that the user is authorized to run scripts that require
157 The C<$flagsrequired> argument specifies the required privileges
158 the user must have if the username and password are correct.
159 It should be specified as a reference-to-hash; keys in the hash
160 should be the "flags" for the user, as specified in the Members
161 intranet module. Any key specified must correspond to a "flag"
162 in the userflags table. E.g., { circulate => 1 } would specify
163 that the user must have the "circulate" privilege in order to
164 proceed. To make sure that access control is correct, the
165 C<$flagsrequired> parameter must be specified correctly.
167 The C<$type> argument specifies whether the template should be
168 retrieved from the opac or intranet directory tree. "opac" is
169 assumed if it is not specified; however, if C<$type> is specified,
170 "intranet" is assumed if it is not "opac".
172 If C<$query> does not have a valid session ID associated with it
173 (i.e., the user has not logged in) or if the session has expired,
174 C<&checkauth> presents the user with a login page (from the point of
175 view of the original script, C<&checkauth> does not return). Once the
176 user has authenticated, C<&checkauth> restarts the original script
177 (this time, C<&checkauth> returns).
179 The login page is provided using a HTML::Template, which is set in the
180 systempreferences table or at the top of this file. The variable C<$type>
181 selects which template to use, either the opac or the intranet
182 authentification template.
184 C<&checkauth> returns a user ID, a cookie, and a session ID. The
185 cookie should be sent back to the browser; it verifies that the user
194 # $authnotrequired will be set for scripts which will run without authentication
195 my $authnotrequired = shift;
196 my $flagsrequired = shift;
198 $type = 'opac' unless $type;
200 my $dbh = C4::Context->dbh;
201 my $timeout = C4::Context->preference('timeout');
202 $timeout = 120 unless $timeout;
205 if ($type eq 'opac') {
206 $template_name = "opac-auth.tmpl";
208 $template_name = "auth.tmpl";
214 my ($userid, $cookie, $sessionID, $flags);
215 my $logout = $query->param('logout.x');
216 if ($userid = $ENV{'REMOTE_USER'}) {
217 # Using Basic Authentication, no cookies required
218 $cookie=$query->cookie(-name => 'sessionID',
222 } elsif ($sessionID=$query->cookie('sessionID')) {
223 my ($ip , $lasttime);
224 ($userid, $ip, $lasttime) = $dbh->selectrow_array(
225 "SELECT userid,ip,lasttime FROM sessions WHERE sessionid=?",
229 # voluntary logout the user
230 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
233 open L, ">>/tmp/sessionlog";
234 my $time=localtime(time());
235 printf L "%20s from %16s logged out at %30s (manually).\n", $userid, $ip, $time;
239 if ($lasttime<time()-$timeout) {
241 $info{'timed_out'} = 1;
242 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
245 open L, ">>/tmp/sessionlog";
246 my $time=localtime(time());
247 printf L "%20s from %16s logged out at %30s (inactivity).\n", $userid, $ip, $time;
249 } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
250 # Different ip than originally logged in from
251 $info{'oldip'} = $ip;
252 $info{'newip'} = $ENV{'REMOTE_ADDR'};
253 $info{'different_ip'} = 1;
254 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
257 open L, ">>/tmp/sessionlog";
258 my $time=localtime(time());
259 printf L "%20s from logged out at %30s (ip changed from %16s to %16s).\n", $userid, $time, $ip, $info{'newip'};
262 $cookie=$query->cookie(-name => 'sessionID',
263 -value => $sessionID,
265 $dbh->do("UPDATE sessions SET lasttime=? WHERE sessionID=?",
266 undef, (time(), $sessionID));
267 $flags = haspermission($dbh, $userid, $flagsrequired);
271 $info{'nopermission'} = 1;
277 $sessionID=int(rand()*100000).'-'.time();
278 $userid=$query->param('userid');
279 my $password=$query->param('password');
280 my ($return, $cardnumber) = checkpw($dbh,$userid,$password);
282 $dbh->do("DELETE FROM sessions WHERE sessionID=? AND userid=?",
283 undef, ($sessionID, $userid));
284 $dbh->do("INSERT INTO sessions (sessionID, userid, ip,lasttime) VALUES (?, ?, ?, ?)",
285 undef, ($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time()));
286 open L, ">>/tmp/sessionlog";
287 my $time=localtime(time());
288 printf L "%20s from %16s logged in at %30s.\n", $userid, $ENV{'REMOTE_ADDR'}, $time;
290 $cookie=$query->cookie(-name => 'sessionID',
291 -value => $sessionID,
293 if ($flags = haspermission($dbh, $userid, $flagsrequired)) {
296 $info{'nopermission'} = 1;
300 $info{'invalid_username_or_password'} = 1;
304 my $insecure = C4::Context->boolean_preference('insecure');
305 # finished authentification, now respond
306 if ($loggedin || $authnotrequired || (defined($insecure) && $insecure)) {
309 $cookie=$query->cookie(-name => 'sessionID',
313 return ($userid, $cookie, $sessionID, $flags);
315 # else we have a problem...
316 # get the inputs from the incoming query
318 foreach my $name (param $query) {
319 (next) if ($name eq 'userid' || $name eq 'password');
320 my $value = $query->param($name);
321 push @inputs, {name => $name , value => $value};
324 my $template = gettemplate($template_name, $type);
325 $template->param(INPUTS => \@inputs);
326 $template->param(loginprompt => 1) unless $info{'nopermission'};
328 my $self_url = $query->url(-absolute => 1);
329 $template->param(url => $self_url);
330 $template->param(\%info);
331 $cookie=$query->cookie(-name => 'sessionID',
332 -value => $sessionID,
334 print $query->header(
335 -type => guesstype($template->output),
337 ), $template->output;
346 # This should be modified to allow a selection of authentication schemes
347 # (e.g. LDAP), as well as local authentication through the borrowers
348 # tables passwd field
350 my ($dbh, $userid, $password) = @_;
351 warn "$userid / $password";
352 my $sth=$dbh->prepare("select password,cardnumber from borrowers where userid=?");
353 $sth->execute($userid);
355 my ($md5password,$cardnumber) = $sth->fetchrow;
356 warn "==> ".md5_base64($password)." = $md5password";
357 if (md5_base64($password) eq $md5password) {
358 return 1,$cardnumber;
361 my $sth=$dbh->prepare("select password from borrowers where cardnumber=?");
362 $sth->execute($userid);
364 my ($md5password) = $sth->fetchrow;
365 if (md5_base64($password) eq $md5password) {
369 if ($userid eq C4::Context->config('user') && $password eq C4::Context->config('pass')) {
370 # Koha superuser account
379 my $cardnumber=shift;
382 my $sth=$dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
383 $sth->execute($cardnumber);
384 my ($flags) = $sth->fetchrow;
385 $sth=$dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
387 while (my ($bit, $flag, $defaulton) = $sth->fetchrow) {
388 if (($flags & (2**$bit)) || $defaulton) {
389 $userflags->{$flag}=1;
396 my ($dbh, $userid, $flagsrequired) = @_;
397 my $sth=$dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
398 $sth->execute($userid);
399 my ($cardnumber) = $sth->fetchrow;
400 ($cardnumber) || ($cardnumber=$userid);
401 my $flags=getuserflags($cardnumber,$dbh);
403 if ($userid eq C4::Context->config('user')) {
404 # Super User Account from /etc/koha.conf
405 $flags->{'superlibrarian'}=1;
407 return $flags if $flags->{superlibrarian};
408 foreach (keys %$flagsrequired) {
409 return $flags if $flags->{$_};
414 sub getborrowernumber {
416 my $dbh = C4::Context->dbh;
417 for my $field ('userid', 'cardnumber') {
418 my $sth=$dbh->prepare
419 ("select borrowernumber from borrowers where $field=?");
420 $sth->execute($userid);
422 my ($bnumber) = $sth->fetchrow;
431 END { } # module clean-up code here (global destructor)