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(loggedinusername => $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 = 600 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=?",
228 # voluntary logout the user
229 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
232 open L, ">>/tmp/sessionlog";
233 my $time=localtime(time());
234 printf L "%20s from %16s logged out at %30s (manually).\n", $userid, $ip, $time;
238 if ($lasttime<time()-$timeout) {
240 $info{'timed_out'} = 1;
241 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
244 open L, ">>/tmp/sessionlog";
245 my $time=localtime(time());
246 printf L "%20s from %16s logged out at %30s (inactivity).\n", $userid, $ip, $time;
248 } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
249 # Different ip than originally logged in from
250 $info{'oldip'} = $ip;
251 $info{'newip'} = $ENV{'REMOTE_ADDR'};
252 $info{'different_ip'} = 1;
253 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
256 open L, ">>/tmp/sessionlog";
257 my $time=localtime(time());
258 printf L "%20s from logged out at %30s (ip changed from %16s to %16s).\n", $userid, $time, $ip, $info{'newip'};
261 $cookie=$query->cookie(-name => 'sessionID',
262 -value => $sessionID,
264 $dbh->do("UPDATE sessions SET lasttime=? WHERE sessionID=?",
265 undef, (time(), $sessionID));
266 $flags = haspermission($dbh, $userid, $flagsrequired);
270 $info{'nopermission'} = 1;
276 $sessionID=int(rand()*100000).'-'.time();
277 $userid=$query->param('userid');
278 my $password=$query->param('password');
279 my ($return, $cardnumber) = checkpw($dbh,$userid,$password);
281 $dbh->do("DELETE FROM sessions WHERE sessionID=? AND userid=?",
282 undef, ($sessionID, $userid));
283 $dbh->do("INSERT INTO sessions (sessionID, userid, ip,lasttime) VALUES (?, ?, ?, ?)",
284 undef, ($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time()));
285 open L, ">>/tmp/sessionlog";
286 my $time=localtime(time());
287 printf L "%20s from %16s logged in at %30s.\n", $userid, $ENV{'REMOTE_ADDR'}, $time;
289 $cookie=$query->cookie(-name => 'sessionID',
290 -value => $sessionID,
292 if ($flags = haspermission($dbh, $userid, $flagsrequired)) {
295 $info{'nopermission'} = 1;
299 $info{'invalid_username_or_password'} = 1;
303 my $insecure = C4::Context->boolean_preference('insecure');
304 # finished authentification, now respond
305 if ($loggedin || $authnotrequired || (defined($insecure) && $insecure)) {
308 $cookie=$query->cookie(-name => 'sessionID',
312 return ($userid, $cookie, $sessionID, $flags);
314 # else we have a problem...
315 # get the inputs from the incoming query
317 foreach my $name (param $query) {
318 (next) if ($name eq 'userid' || $name eq 'password');
319 my $value = $query->param($name);
320 push @inputs, {name => $name , value => $value};
323 my $template = gettemplate($template_name, $type);
324 $template->param(INPUTS => \@inputs);
325 $template->param(loginprompt => 1) unless $info{'nopermission'};
327 my $self_url = $query->url(-absolute => 1);
328 $template->param(url => $self_url);
329 $template->param(\%info);
330 $cookie=$query->cookie(-name => 'sessionID',
331 -value => $sessionID,
333 print $query->header(
334 -type => guesstype($template->output),
336 ), $template->output;
345 # This should be modified to allow a selection of authentication schemes
346 # (e.g. LDAP), as well as local authentication through the borrowers
347 # tables passwd field
349 my ($dbh, $userid, $password) = @_;
350 my $sth=$dbh->prepare("select password,cardnumber from borrowers where userid=?");
351 $sth->execute($userid);
353 my ($md5password,$cardnumber) = $sth->fetchrow;
354 if (md5_base64($password) eq $md5password) {
355 return 1,$cardnumber;
358 my $sth=$dbh->prepare("select password from borrowers where cardnumber=?");
359 $sth->execute($userid);
361 my ($md5password) = $sth->fetchrow;
362 if (md5_base64($password) eq $md5password) {
366 if ($userid eq C4::Context->config('user') && $password eq C4::Context->config('pass')) {
367 # Koha superuser account
370 if ($userid eq 'demo' && $password eq 'demo' && C4::Context->config('demo')) {
371 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
372 # some features won't be effective : modify systempref, modify MARC structure,
381 my $cardnumber=shift;
384 my $sth=$dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
385 $sth->execute($cardnumber);
386 my ($flags) = $sth->fetchrow;
387 $sth=$dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
389 while (my ($bit, $flag, $defaulton) = $sth->fetchrow) {
390 if (($flags & (2**$bit)) || $defaulton) {
391 $userflags->{$flag}=1;
398 my ($dbh, $userid, $flagsrequired) = @_;
399 my $sth=$dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
400 $sth->execute($userid);
401 my ($cardnumber) = $sth->fetchrow;
402 ($cardnumber) || ($cardnumber=$userid);
403 my $flags=getuserflags($cardnumber,$dbh);
405 if ($userid eq C4::Context->config('user')) {
406 # Super User Account from /etc/koha.conf
407 $flags->{'superlibrarian'}=1;
409 if ($userid eq 'demo' && C4::Context->config('demo')) {
410 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
411 $flags->{'superlibrarian'}=1;
413 return $flags if $flags->{superlibrarian};
414 foreach (keys %$flagsrequired) {
415 return $flags if $flags->{$_};
420 sub getborrowernumber {
422 my $dbh = C4::Context->dbh;
423 for my $field ('userid', 'cardnumber') {
424 my $sth=$dbh->prepare
425 ("select borrowernumber from borrowers where $field=?");
426 $sth->execute($userid);
428 my ($bnumber) = $sth->fetchrow;
437 END { } # module clean-up code here (global destructor)