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::Members; # getpatroninformation
31 use C4::Koha;## to get branch
33 # use Net::LDAP qw(:all);
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37 # set the version for version checking
42 C4::Auth - Authenticates Koha users
51 my ($template, $borrowernumber, $cookie)
52 = get_template_and_user({template_name => "opac-main.tmpl",
56 flagsrequired => {borrow => 1},
68 The main function of this module is to provide
69 authentification. However the get_template_and_user function has
70 been provided so that a users login information is passed along
71 automatically. This gets loaded into the template.
84 &get_template_and_user
87 =item get_template_and_user
89 my ($template, $borrowernumber, $cookie)
90 = get_template_and_user({template_name => "opac-main.tmpl",
94 flagsrequired => {borrow => 1},
97 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
98 to C<&checkauth> (in this module) to perform authentification.
99 See C<&checkauth> for an explanation of these parameters.
101 The C<template_name> is then used to find the correct template for
102 the page. The authenticated users details are loaded onto the
103 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
104 C<sessionID> is passed to the template. This can be used in templates
105 if cookies are disabled. It needs to be put as and input to every
108 More information on the C<gettemplate> sub can be found in the
114 sub get_template_and_user {
116 my $template = gettemplate($in->{'template_name'}, $in->{'type'},$in->{'query'});
117 my ($user, $cookie, $sessionID, $flags)
118 = checkauth($in->{'query'}, $in->{'authnotrequired'}, $in->{'flagsrequired'}, $in->{'type'});
122 $template->param(loggedinusername => $user);
123 $template->param(sessionID => $sessionID);
125 $borrowernumber = getborrowernumber($user);
126 my ($borr, $alternativeflags) = getpatroninformation(undef, $borrowernumber);
129 $template->param(USER_INFO => \@bordat,
131 my $branches=GetBranches();
132 $template->param(branchname=>$branches->{$borr->{branchcode}}->{branchname},);
134 # We are going to use the $flags returned by checkauth
135 # to create the template's parameters that will indicate
136 # which menus the user can access.
137 if ($flags && $flags->{superlibrarian} == 1)
139 $template->param(CAN_user_circulate => 1);
140 $template->param(CAN_user_catalogue => 1);
141 $template->param(CAN_user_parameters => 1);
142 $template->param(CAN_user_borrowers => 1);
143 $template->param(CAN_user_permission => 1);
144 $template->param(CAN_user_reserveforothers => 1);
145 $template->param(CAN_user_borrow => 1);
146 $template->param(CAN_user_reserveforself => 1);
147 $template->param(CAN_user_editcatalogue => 1);
148 $template->param(CAN_user_updatecharge => 1);
149 $template->param(CAN_user_acquisition => 1);
150 $template->param(CAN_user_management => 1);
151 $template->param(CAN_user_tools => 1); }
153 if ($flags && $flags->{circulate} == 1) {
154 $template->param(CAN_user_circulate => 1); }
156 if ($flags && $flags->{catalogue} == 1) {
157 $template->param(CAN_user_catalogue => 1); }
160 if ($flags && $flags->{parameters} == 1) {
161 $template->param(CAN_user_parameters => 1);
162 $template->param(CAN_user_management => 1);
163 $template->param(CAN_user_tools => 1); }
166 if ($flags && $flags->{borrowers} == 1) {
167 $template->param(CAN_user_borrowers => 1); }
170 if ($flags && $flags->{permissions} == 1) {
171 $template->param(CAN_user_permission => 1); }
173 if ($flags && $flags->{reserveforothers} == 1) {
174 $template->param(CAN_user_reserveforothers => 1); }
177 if ($flags && $flags->{borrow} == 1) {
178 $template->param(CAN_user_borrow => 1); }
181 if ($flags && $flags->{reserveforself} == 1) {
182 $template->param(CAN_user_reserveforself => 1); }
185 if ($flags && $flags->{editcatalogue} == 1) {
186 $template->param(CAN_user_editcatalogue => 1); }
189 if ($flags && $flags->{updatecharges} == 1) {
190 $template->param(CAN_user_updatecharge => 1); }
192 if ($flags && $flags->{acquisition} == 1) {
193 $template->param(CAN_user_acquisition => 1); }
195 if ($flags && $flags->{management} == 1) {
196 $template->param(CAN_user_management => 1);
197 $template->param(CAN_user_tools => 1); }
199 if ($flags && $flags->{tools} == 1) {
200 $template->param(CAN_user_tools => 1); }
203 if ($in->{'type'} eq "intranet") {
205 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
206 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
207 IntranetNav => C4::Context->preference("IntranetNav"),
214 suggestion => C4::Context->preference("suggestion"),
215 virtualshelves => C4::Context->preference("virtualshelves"),
216 OpacNav => C4::Context->preference("OpacNav"),
217 opacheader => C4::Context->preference("opacheader"),
218 opaccredits => C4::Context->preference("opaccredits"),
219 opacsmallimage => C4::Context->preference("opacsmallimage"),
220 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
221 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
222 opaclanguagesdisplay => C4::Context->preference("opaclanguagesdisplay"),
223 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
224 opacuserlogin => C4::Context->preference("opacuserlogin"),
225 opacbookbag => C4::Context->preference("opacbookbag"),
229 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
230 AmazonContent => C4::Context->preference("AmazonContent"),
231 LibraryName => C4::Context->preference("LibraryName"),
233 return ($template, $borrowernumber, $cookie);
239 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
241 Verifies that the user is authorized to run this script. If
242 the user is authorized, a (userid, cookie, session-id, flags)
243 quadruple is returned. If the user is not authorized but does
244 not have the required privilege (see $flagsrequired below), it
245 displays an error page and exits. Otherwise, it displays the
246 login page and exits.
248 Note that C<&checkauth> will return if and only if the user
249 is authorized, so it should be called early on, before any
250 unfinished operations (e.g., if you've opened a file, then
251 C<&checkauth> won't close it for you).
253 C<$query> is the CGI object for the script calling C<&checkauth>.
255 The C<$noauth> argument is optional. If it is set, then no
256 authorization is required for the script.
258 C<&checkauth> fetches user and session information from C<$query> and
259 ensures that the user is authorized to run scripts that require
262 The C<$flagsrequired> argument specifies the required privileges
263 the user must have if the username and password are correct.
264 It should be specified as a reference-to-hash; keys in the hash
265 should be the "flags" for the user, as specified in the Members
266 intranet module. Any key specified must correspond to a "flag"
267 in the userflags table. E.g., { circulate => 1 } would specify
268 that the user must have the "circulate" privilege in order to
269 proceed. To make sure that access control is correct, the
270 C<$flagsrequired> parameter must be specified correctly.
272 The C<$type> argument specifies whether the template should be
273 retrieved from the opac or intranet directory tree. "opac" is
274 assumed if it is not specified; however, if C<$type> is specified,
275 "intranet" is assumed if it is not "opac".
277 If C<$query> does not have a valid session ID associated with it
278 (i.e., the user has not logged in) or if the session has expired,
279 C<&checkauth> presents the user with a login page (from the point of
280 view of the original script, C<&checkauth> does not return). Once the
281 user has authenticated, C<&checkauth> restarts the original script
282 (this time, C<&checkauth> returns).
284 The login page is provided using a HTML::Template, which is set in the
285 systempreferences table or at the top of this file. The variable C<$type>
286 selects which template to use, either the opac or the intranet
287 authentification template.
289 C<&checkauth> returns a user ID, a cookie, and a session ID. The
290 cookie should be sent back to the browser; it verifies that the user
299 # $authnotrequired will be set for scripts which will run without authentication
300 my $authnotrequired = shift;
301 my $flagsrequired = shift;
303 $type = 'opac' unless $type;
305 my $dbh = C4::Context->dbh;
306 my $timeout = C4::Context->preference('timeout');
307 $timeout = 600 unless $timeout;
310 if ($type eq 'opac') {
311 $template_name = "opac-auth.tmpl";
313 $template_name = "auth.tmpl";
319 my ($userid, $cookie, $sessionID, $flags,$envcookie);
320 my $logout = $query->param('logout.x');
321 if ($userid = $ENV{'REMOTE_USER'}) {
322 # Using Basic Authentication, no cookies required
323 $cookie=$query->cookie(-name => 'sessionID',
327 } elsif ($sessionID=$query->cookie('sessionID')) {
328 C4::Context->_new_userenv($sessionID);
329 if (my %hash=$query->cookie('userenv')){
330 C4::Context::set_userenv(
342 my ($ip , $lasttime);
344 ($userid, $ip, $lasttime) = $dbh->selectrow_array(
345 "SELECT userid,ip,lasttime FROM sessions WHERE sessionid=?",
348 # voluntary logout the user
349 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
350 C4::Context->_unset_userenv($sessionID);
353 open L, ">>/tmp/sessionlog";
354 my $time=localtime(time());
355 printf L "%20s from %16s logged out at %30s (manually).\n", $userid, $ip, $time;
359 if ($lasttime<time()-$timeout) {
361 $info{'timed_out'} = 1;
362 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
363 C4::Context->_unset_userenv($sessionID);
366 open L, ">>/tmp/sessionlog";
367 my $time=localtime(time());
368 printf L "%20s from %16s logged out at %30s (inactivity).\n", $userid, $ip, $time;
370 } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
371 # Different ip than originally logged in from
372 $info{'oldip'} = $ip;
373 $info{'newip'} = $ENV{'REMOTE_ADDR'};
374 $info{'different_ip'} = 1;
375 $dbh->do("DELETE FROM sessions WHERE sessionID=?", undef, $sessionID);
376 C4::Context->_unset_userenv($sessionID);
379 open L, ">>/tmp/sessionlog";
380 my $time=localtime(time());
381 printf L "%20s from logged out at %30s (ip changed from %16s to %16s).\n", $userid, $time, $ip, $info{'newip'};
384 $cookie=$query->cookie(-name => 'sessionID',
385 -value => $sessionID,
387 $dbh->do("UPDATE sessions SET lasttime=? WHERE sessionID=?",
388 undef, (time(), $sessionID));
389 $flags = haspermission($dbh, $userid, $flagsrequired);
393 $info{'nopermission'} = 1;
399 $sessionID=int(rand()*100000).'-'.time();
400 $userid=$query->param('userid');
401 my $password=$query->param('password');
402 C4::Context->_new_userenv($sessionID);
403 my ($return, $cardnumber) = checkpw($dbh,$userid,$password);
405 $dbh->do("DELETE FROM sessions WHERE sessionID=? AND userid=?",
406 undef, ($sessionID, $userid));
407 $dbh->do("INSERT INTO sessions (sessionID, userid, ip,lasttime) VALUES (?, ?, ?, ?)",
408 undef, ($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time()));
409 open L, ">>/tmp/sessionlog";
410 my $time=localtime(time());
411 printf L "%20s from %16s logged in at %30s.\n", $userid, $ENV{'REMOTE_ADDR'}, $time;
413 $cookie=$query->cookie(-name => 'sessionID',
414 -value => $sessionID,
416 if ($flags = haspermission($dbh, $userid, $flagsrequired)) {
419 $info{'nopermission'} = 1;
420 C4::Context->_unset_userenv($sessionID);
423 my ($bornum,$firstname,$surname,$userflags,$branchcode,$branchname,$emailaddress);
424 my $sth=$dbh->prepare("select borrowernumber,firstname,surname,flags,borrowers.branchcode,branchname,emailaddress from borrowers left join branches on borrowers.branchcode=branches.branchcode where userid=?");
425 $sth->execute($userid);
426 ($bornum,$firstname,$surname,$userflags,$branchcode,$branchname, $emailaddress) = $sth->fetchrow if ($sth->rows);
427 # warn "$cardnumber,$bornum,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
429 my $sth=$dbh->prepare("select borrowernumber,firstname,surname,flags,borrowers.branchcode,branchname,emailaddress from borrowers left join branches on borrowers.branchcode=branches.branchcode where cardnumber=?");
430 $sth->execute($cardnumber);
431 ($bornum,$firstname,$surname,$userflags,$branchcode, $branchname,$emailaddress) = $sth->fetchrow if ($sth->rows);
432 # warn "$cardnumber,$bornum,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
434 $sth->execute($userid);
435 ($bornum,$firstname,$surname,$userflags,$branchcode, $branchname, $emailaddress) = $sth->fetchrow if ($sth->rows);
437 # warn "$cardnumber,$bornum,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
439 my $hash = C4::Context::set_userenv(
450 # warn "$cardnumber,$bornum,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
451 $envcookie=$query->cookie(-name => 'userenv',
454 } elsif ($return == 2) {
455 #We suppose the user is the superlibrarian
456 my $hash = C4::Context::set_userenv(
458 C4::Context->config('user'),
459 C4::Context->config('user'),
460 C4::Context->config('user'),
461 "","",1,C4::Context->preference('KohaAdminEmailAddress')
463 $envcookie=$query->cookie(-name => 'userenv',
469 $info{'invalid_username_or_password'} = 1;
470 C4::Context->_unset_userenv($sessionID);
474 my $insecure = C4::Context->boolean_preference('insecure');
475 # finished authentification, now respond
476 if ($loggedin || $authnotrequired || (defined($insecure) && $insecure)) {
479 $cookie=$query->cookie(-name => 'sessionID',
484 return ($userid, [$cookie,$envcookie], $sessionID, $flags)
486 return ($userid, $cookie, $sessionID, $flags);
489 # else we have a problem...
490 # get the inputs from the incoming query
492 foreach my $name (param $query) {
493 (next) if ($name eq 'userid' || $name eq 'password');
494 my $value = $query->param($name);
495 push @inputs, {name => $name , value => $value};
498 my $template = gettemplate($template_name, $type,$query);
499 $template->param(INPUTS => \@inputs,
500 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
501 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
502 IntranetNav => C4::Context->preference("IntranetNav"),
503 opacnav => C4::Context->preference("OpacNav"),
504 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
507 $template->param(loginprompt => 1) unless $info{'nopermission'};
509 my $self_url = $query->url(-absolute => 1);
510 $template->param(url => $self_url, LibraryName=> => C4::Context->preference("LibraryName"),);
511 $template->param(\%info);
512 $cookie=$query->cookie(-name => 'sessionID',
513 -value => $sessionID,
515 print $query->header(
516 -type => "text/html",
519 ), $template->output;
528 my ($dbh, $userid, $password) = @_;
530 my $sth=$dbh->prepare("select password,cardnumber from borrowers where userid=?");
531 $sth->execute($userid);
533 my ($md5password,$cardnumber) = $sth->fetchrow;
534 if (md5_base64($password) eq $md5password) {
535 return 1,$cardnumber;
538 my $sth=$dbh->prepare("select password from borrowers where cardnumber=?");
539 $sth->execute($userid);
541 my ($md5password) = $sth->fetchrow;
542 if (md5_base64($password) eq $md5password) {
546 if ($userid eq C4::Context->config('user') && $password eq C4::Context->config('pass')) {
547 # Koha superuser account
550 if ($userid eq 'demo' && $password eq 'demo' && C4::Context->config('demo')) {
551 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
552 # some features won't be effective : modify systempref, modify MARC structure,
559 my $cardnumber=shift;
562 my $sth=$dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
563 $sth->execute($cardnumber);
564 my ($flags) = $sth->fetchrow;
565 $sth=$dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
567 while (my ($bit, $flag, $defaulton) = $sth->fetchrow) {
568 if (($flags & (2**$bit)) || $defaulton) {
569 $userflags->{$flag}=1;
576 my ($dbh, $userid, $flagsrequired) = @_;
577 my $sth=$dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
578 $sth->execute($userid);
579 my ($cardnumber) = $sth->fetchrow;
580 ($cardnumber) || ($cardnumber=$userid);
581 my $flags=getuserflags($cardnumber,$dbh);
583 if ($userid eq C4::Context->config('user')) {
584 # Super User Account from /etc/koha.conf
585 $flags->{'superlibrarian'}=1;
587 if ($userid eq 'demo' && C4::Context->config('demo')) {
588 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
589 $flags->{'superlibrarian'}=1;
591 return $flags if $flags->{superlibrarian};
592 foreach (keys %$flagsrequired) {
593 return $flags if $flags->{$_};
598 sub getborrowernumber {
600 my $dbh = C4::Context->dbh;
601 for my $field ('userid', 'cardnumber') {
602 my $sth=$dbh->prepare
603 ("select borrowernumber from borrowers where $field=?");
604 $sth->execute($userid);
606 my ($bnumber) = $sth->fetchrow;
613 END { } # module clean-up code here (global destructor)