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;
32 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
34 # set the version for version checking
35 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
36 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
41 InstallAuth - Authenticates Koha users for Install process
50 my ($template, $borrowernumber, $cookie)
51 = get_template_and_user({template_name => "opac-main.tmpl",
55 flagsrequired => {borrow => 1},
59 -type => guesstype($template->output),
66 The main function of this module is to provide
67 authentification. However the get_template_and_user function has
68 been provided so that a users login information is passed along
69 automatically. This gets loaded into the template.
70 This package is different from C4::Auth in so far as
71 C4::Auth uses many preferences which are supposed NOT to be obtainable when installing the database.
73 As in C4::Auth, Authentication is based on cookies.
84 &get_template_and_user
88 =item get_template_and_user
90 my ($template, $borrowernumber, $cookie)
91 = get_template_and_user({template_name => "opac-main.tmpl",
95 flagsrequired => {borrow => 1},
98 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
99 to C<&checkauth> (in this module) to perform authentification.
100 See C<&checkauth> for an explanation of these parameters.
102 The C<template_name> is then used to find the correct template for
103 the page. The authenticated users details are loaded onto the
104 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
105 C<sessionID> is passed to the template. This can be used in templates
106 if cookies are disabled. It needs to be put as and input to every
109 More information on the C<gettemplate> sub can be found in the
114 sub get_template_and_user {
116 my $query=$in->{'query'};
117 my $language=$query->cookie('KohaOpacLanguage');
118 my $path=C4::Context->config('intrahtdocs')."/prog/".($language?$language:"en");
119 my $template = HTML::Template::Pro->new(
120 filename => "$path/".$in->{template_name},
121 die_on_bad_params => 1,
124 path => ["$path/includes"]
127 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
129 $in->{'authnotrequired'},
130 $in->{'flagsrequired'},
133 # use Data::Dumper;warn "utilisateur $user cookie : ".Dumper($cookie);
137 $template->param( loggedinusername => $user );
138 $template->param( sessionID => $sessionID );
141 # We are going to use the $flags returned by checkauth
142 # to create the template's parameters that will indicate
143 # which menus the user can access.
144 if (( $flags && $flags->{superlibrarian}==1)) {
145 $template->param( CAN_user_circulate => 1 );
146 $template->param( CAN_user_catalogue => 1 );
147 $template->param( CAN_user_parameters => 1 );
148 $template->param( CAN_user_borrowers => 1 );
149 $template->param( CAN_user_permission => 1 );
150 $template->param( CAN_user_reserveforothers => 1 );
151 $template->param( CAN_user_borrow => 1 );
152 $template->param( CAN_user_editcatalogue => 1 );
153 $template->param( CAN_user_updatecharge => 1 );
154 $template->param( CAN_user_acquisition => 1 );
155 $template->param( CAN_user_management => 1 );
156 $template->param( CAN_user_tools => 1 );
157 $template->param( CAN_user_editauthorities => 1 );
158 $template->param( CAN_user_serials => 1 );
159 $template->param( CAN_user_reports => 1 );
162 return ( $template, $borrowernumber, $cookie );
167 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
169 Verifies that the user is authorized to run this script. If
170 the user is authorized, a (userid, cookie, session-id, flags)
171 quadruple is returned. If the user is not authorized but does
172 not have the required privilege (see $flagsrequired below), it
173 displays an error page and exits. Otherwise, it displays the
174 login page and exits.
176 Note that C<&checkauth> will return if and only if the user
177 is authorized, so it should be called early on, before any
178 unfinished operations (e.g., if you've opened a file, then
179 C<&checkauth> won't close it for you).
181 C<$query> is the CGI object for the script calling C<&checkauth>.
183 The C<$noauth> argument is optional. If it is set, then no
184 authorization is required for the script.
186 C<&checkauth> fetches user and session information from C<$query> and
187 ensures that the user is authorized to run scripts that require
190 The C<$flagsrequired> argument specifies the required privileges
191 the user must have if the username and password are correct.
192 It should be specified as a reference-to-hash; keys in the hash
193 should be the "flags" for the user, as specified in the Members
194 intranet module. Any key specified must correspond to a "flag"
195 in the userflags table. E.g., { circulate => 1 } would specify
196 that the user must have the "circulate" privilege in order to
197 proceed. To make sure that access control is correct, the
198 C<$flagsrequired> parameter must be specified correctly.
200 The C<$type> argument specifies whether the template should be
201 retrieved from the opac or intranet directory tree. "opac" is
202 assumed if it is not specified; however, if C<$type> is specified,
203 "intranet" is assumed if it is not "opac".
205 If C<$query> does not have a valid session ID associated with it
206 (i.e., the user has not logged in) or if the session has expired,
207 C<&checkauth> presents the user with a login page (from the point of
208 view of the original script, C<&checkauth> does not return). Once the
209 user has authenticated, C<&checkauth> restarts the original script
210 (this time, C<&checkauth> returns).
212 The login page is provided using a HTML::Template, which is set in the
213 systempreferences table or at the top of this file. The variable C<$type>
214 selects which template to use, either the opac or the intranet
215 authentification template.
217 C<&checkauth> returns a user ID, a cookie, and a session ID. The
218 cookie should be sent back to the browser; it verifies that the user
226 # $authnotrequired will be set for scripts which will run without authentication
227 my $authnotrequired = shift;
228 my $flagsrequired = shift;
230 $type = 'intranet' unless $type;
234 $template_name = "installer/auth.tmpl";
239 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
240 my $logout = $query->param('logout.x');
241 if ( $sessionID = $query->cookie('sessionID') ) {
242 C4::Context->_new_userenv($sessionID);
243 if ( my %hash = $query->cookie('userenv') ) {
244 C4::Context::set_userenv(
245 $hash{number}, $hash{id},
246 $hash{cardnumber}, $hash{firstname},
247 $hash{surname}, $hash{branch},
248 $hash{branchname}, $hash{flags},
249 $hash{emailaddress}, $hash{branchprinter}
251 $cookie = $query->cookie(
252 -name => 'sessionID',
253 -value => $sessionID,
257 $userid = $hash{cardnumber};
259 my ( $ip, $lasttime );
263 # voluntary logout the user
264 C4::Context->_unset_userenv($sessionID);
267 open L, ">>/tmp/sessionlog";
268 my $time = localtime( time() );
269 printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
275 $sessionID = int( rand() * 100000 ) . '-' . time();
276 $userid = $query->param('userid');
277 C4::Context->_new_userenv($sessionID);
278 my $password = $query->param('password');
279 C4::Context->_new_userenv($sessionID);
280 my ( $return, $cardnumber ) = checkpw( $userid, $password );
283 open L, ">>/tmp/sessionlog";
284 my $time = localtime( time() );
285 printf L "%20s from %16s logged in at %30s.\n", $userid,
286 $ENV{'REMOTE_ADDR'}, $time;
288 $cookie = $query->cookie(
289 -name => 'sessionID',
290 -value => $sessionID,
293 if ( $return == 2 ) {
294 #Only superlibrarian should have access to this page.
295 #Since if it is a user, it is supposed that there is a borrower table
296 #And thus that data structure is loaded.
297 my $hash = C4::Context::set_userenv(
300 C4::Context->config('user'),
301 C4::Context->config('user'),
302 C4::Context->config('user'),
307 $envcookie = $query->cookie(
312 $userid=C4::Context->config('user');
317 $info{'invalid_username_or_password'} = 1;
318 C4::Context->_unset_userenv($sessionID);
323 # finished authentification, now respond
329 $cookie = $query->cookie(
330 -name => 'sessionID',
336 return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
339 return ( $userid, $cookie, $sessionID, $flags );
343 # else we have a problem...
344 # get the inputs from the incoming query
346 foreach my $name ( param $query) {
347 (next) if ( $name eq 'userid' || $name eq 'password' );
348 my $value = $query->param($name);
349 push @inputs, { name => $name, value => $value };
352 my $path=C4::Context->config('intrahtdocs')."/prog/".($query->param('language')?$query->param('language'):"en");
353 my $template = HTML::Template::Pro->new(
354 filename => "$path/$template_name",
355 die_on_bad_params => 1,
358 path => ["$path/includes"]
364 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
366 my $self_url = $query->url( -absolute => 1 );
370 $template->param( \%info );
371 $cookie = $query->cookie(
372 -name => 'sessionID',
373 -value => $sessionID,
376 print $query->header(
377 -type => guesstype( $template->output ),
386 my ( $userid, $password ) = @_;
388 if ( $userid && $userid eq C4::Context->config('user')
389 && "$password" eq C4::Context->config('pass') )
391 # Koha superuser account
392 C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
395 if ( $userid && $userid eq 'demo'
396 && "$password" eq 'demo'
397 && C4::Context->config('demo') )
400 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
401 # some features won't be effective : modify systempref, modify MARC structure,
408 END { } # module clean-up code here (global destructor)