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);
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
33 # set the version for version checking
34 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
35 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
40 InstallAuth - Authenticates Koha users for Install process
49 my ($template, $borrowernumber, $cookie)
50 = get_template_and_user({template_name => "opac-main.tmpl",
54 flagsrequired => {borrow => 1},
65 The main function of this module is to provide
66 authentification. However the get_template_and_user function has
67 been provided so that a users login information is passed along
68 automatically. This gets loaded into the template.
69 This package is different from C4::Auth in so far as
70 C4::Auth uses many preferences which are supposed NOT to be obtainable when installing the database.
72 As in C4::Auth, Authentication is based on cookies.
83 &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
113 sub get_template_and_user {
115 my $query=$in->{'query'};
116 my $language=$query->cookie('KohaOpacLanguage');
117 my $path=C4::Context->config('intrahtdocs')."/prog/".($language?$language:"en");
118 my $template = HTML::Template::Pro->new(
119 filename => "$path/".$in->{template_name},
120 die_on_bad_params => 1,
123 path => ["$path/includes"]
126 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
128 $in->{'authnotrequired'},
129 $in->{'flagsrequired'},
132 # use Data::Dumper;warn "utilisateur $user cookie : ".Dumper($cookie);
136 $template->param( loggedinusername => $user );
137 $template->param( sessionID => $sessionID );
140 # We are going to use the $flags returned by checkauth
141 # to create the template's parameters that will indicate
142 # which menus the user can access.
143 if (( $flags && $flags->{superlibrarian}==1)) {
144 $template->param( CAN_user_circulate => 1 );
145 $template->param( CAN_user_catalogue => 1 );
146 $template->param( CAN_user_parameters => 1 );
147 $template->param( CAN_user_borrowers => 1 );
148 $template->param( CAN_user_permission => 1 );
149 $template->param( CAN_user_reserveforothers => 1 );
150 $template->param( CAN_user_borrow => 1 );
151 $template->param( CAN_user_editcatalogue => 1 );
152 $template->param( CAN_user_updatecharge => 1 );
153 $template->param( CAN_user_acquisition => 1 );
154 $template->param( CAN_user_management => 1 );
155 $template->param( CAN_user_tools => 1 );
156 $template->param( CAN_user_editauthorities => 1 );
157 $template->param( CAN_user_serials => 1 );
158 $template->param( CAN_user_reports => 1 );
161 return ( $template, $borrowernumber, $cookie );
166 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
168 Verifies that the user is authorized to run this script. If
169 the user is authorized, a (userid, cookie, session-id, flags)
170 quadruple is returned. If the user is not authorized but does
171 not have the required privilege (see $flagsrequired below), it
172 displays an error page and exits. Otherwise, it displays the
173 login page and exits.
175 Note that C<&checkauth> will return if and only if the user
176 is authorized, so it should be called early on, before any
177 unfinished operations (e.g., if you've opened a file, then
178 C<&checkauth> won't close it for you).
180 C<$query> is the CGI object for the script calling C<&checkauth>.
182 The C<$noauth> argument is optional. If it is set, then no
183 authorization is required for the script.
185 C<&checkauth> fetches user and session information from C<$query> and
186 ensures that the user is authorized to run scripts that require
189 The C<$flagsrequired> argument specifies the required privileges
190 the user must have if the username and password are correct.
191 It should be specified as a reference-to-hash; keys in the hash
192 should be the "flags" for the user, as specified in the Members
193 intranet module. Any key specified must correspond to a "flag"
194 in the userflags table. E.g., { circulate => 1 } would specify
195 that the user must have the "circulate" privilege in order to
196 proceed. To make sure that access control is correct, the
197 C<$flagsrequired> parameter must be specified correctly.
199 The C<$type> argument specifies whether the template should be
200 retrieved from the opac or intranet directory tree. "opac" is
201 assumed if it is not specified; however, if C<$type> is specified,
202 "intranet" is assumed if it is not "opac".
204 If C<$query> does not have a valid session ID associated with it
205 (i.e., the user has not logged in) or if the session has expired,
206 C<&checkauth> presents the user with a login page (from the point of
207 view of the original script, C<&checkauth> does not return). Once the
208 user has authenticated, C<&checkauth> restarts the original script
209 (this time, C<&checkauth> returns).
211 The login page is provided using a HTML::Template, which is set in the
212 systempreferences table or at the top of this file. The variable C<$type>
213 selects which template to use, either the opac or the intranet
214 authentification template.
216 C<&checkauth> returns a user ID, a cookie, and a session ID. The
217 cookie should be sent back to the browser; it verifies that the user
225 # $authnotrequired will be set for scripts which will run without authentication
226 my $authnotrequired = shift;
227 my $flagsrequired = shift;
229 $type = 'intranet' unless $type;
233 $template_name = "installer/auth.tmpl";
238 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
239 my $logout = $query->param('logout.x');
240 if ( $sessionID = $query->cookie('sessionID') ) {
241 C4::Context->_new_userenv($sessionID);
242 if ( my %hash = $query->cookie('userenv') ) {
243 C4::Context::set_userenv(
244 $hash{number}, $hash{id},
245 $hash{cardnumber}, $hash{firstname},
246 $hash{surname}, $hash{branch},
247 $hash{branchname}, $hash{flags},
248 $hash{emailaddress}, $hash{branchprinter}
250 $cookie = $query->cookie(
251 -name => 'sessionID',
252 -value => $sessionID,
256 $userid = $hash{cardnumber};
258 my ( $ip, $lasttime );
262 # voluntary logout the user
263 C4::Context->_unset_userenv($sessionID);
266 open L, ">>/tmp/sessionlog";
267 my $time = localtime( time() );
268 printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
274 $sessionID = int( rand() * 100000 ) . '-' . time();
275 $userid = $query->param('userid');
276 C4::Context->_new_userenv($sessionID);
277 my $password = $query->param('password');
278 C4::Context->_new_userenv($sessionID);
279 my ( $return, $cardnumber ) = checkpw( $userid, $password );
282 open L, ">>/tmp/sessionlog";
283 my $time = localtime( time() );
284 printf L "%20s from %16s logged in at %30s.\n", $userid,
285 $ENV{'REMOTE_ADDR'}, $time;
287 $cookie = $query->cookie(
288 -name => 'sessionID',
289 -value => $sessionID,
292 if ( $return == 2 ) {
293 #Only superlibrarian should have access to this page.
294 #Since if it is a user, it is supposed that there is a borrower table
295 #And thus that data structure is loaded.
296 my $hash = C4::Context::set_userenv(
299 C4::Context->config('user'),
300 C4::Context->config('user'),
301 C4::Context->config('user'),
306 $envcookie = $query->cookie(
311 $userid=C4::Context->config('user');
316 $info{'invalid_username_or_password'} = 1;
317 C4::Context->_unset_userenv($sessionID);
322 # finished authentification, now respond
328 $cookie = $query->cookie(
329 -name => 'sessionID',
335 return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
338 return ( $userid, $cookie, $sessionID, $flags );
342 # else we have a problem...
343 # get the inputs from the incoming query
345 foreach my $name ( param $query) {
346 (next) if ( $name eq 'userid' || $name eq 'password' );
347 my $value = $query->param($name);
348 push @inputs, { name => $name, value => $value };
351 my $path=C4::Context->config('intrahtdocs')."/prog/".($query->param('language')?$query->param('language'):"en");
352 my $template = HTML::Template::Pro->new(
353 filename => "$path/$template_name",
354 die_on_bad_params => 1,
357 path => ["$path/includes"]
363 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
365 my $self_url = $query->url( -absolute => 1 );
369 $template->param( \%info );
370 $cookie = $query->cookie(
371 -name => 'sessionID',
372 -value => $sessionID,
375 print $query->header(
385 my ( $userid, $password ) = @_;
387 if ( $userid && $userid eq C4::Context->config('user')
388 && "$password" eq C4::Context->config('pass') )
390 # Koha superuser account
391 C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
394 if ( $userid && $userid eq 'demo'
395 && "$password" eq 'demo'
396 && C4::Context->config('demo') )
399 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
400 # some features won't be effective : modify systempref, modify MARC structure,
407 END { } # module clean-up code here (global destructor)