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
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #use warnings; FIXME - Bug 2505
25 use Digest::MD5 qw(md5_base64);
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 # set the version for version checking
41 InstallAuth - Authenticates Koha users for Install process
51 my ($template, $borrowernumber, $cookie)
52 = get_template_and_user({template_name => "opac-main.tmpl",
56 flagsrequired => {borrow => 1},
59 output_html_with_http_headers $query, $cookie, $template->output;
63 The main function of this module is to provide
64 authentification. However the get_template_and_user function has
65 been provided so that a users login information is passed along
66 automatically. This gets loaded into the template.
67 This package is different from C4::Auth in so far as
68 C4::Auth uses many preferences which are supposed NOT to be obtainable when installing the database.
70 As in C4::Auth, Authentication is based on cookies.
81 &get_template_and_user
84 =item get_template_and_user
86 my ($template, $borrowernumber, $cookie)
87 = get_template_and_user({template_name => "opac-main.tmpl",
91 flagsrequired => {borrow => 1},
94 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
95 to C<&checkauth> (in this module) to perform authentification.
96 See C<&checkauth> for an explanation of these parameters.
98 The C<template_name> is then used to find the correct template for
99 the page. The authenticated users details are loaded onto the
100 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
101 C<sessionID> is passed to the template. This can be used in templates
102 if cookies are disabled. It needs to be put as and input to every
105 More information on the C<gettemplate> sub can be found in the
110 sub get_template_and_user {
112 my $query = $in->{'query'};
113 my $language = $query->cookie('KohaOpacLanguage');
115 C4::Context->config('intrahtdocs') . "/prog/"
116 . ( $language ? $language : "en" );
118 my $filename = "$path/modules/" . $in->{template_name};
119 $filename =~ s/\.tmpl$/.tt/;
120 my $interface = 'intranet';
121 my $template = C4::Templates->new( $interface, $filename);
124 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
126 $in->{'authnotrequired'},
127 $in->{'flagsrequired'},
131 # use Data::Dumper;warn "utilisateur $user cookie : ".Dumper($cookie);
135 $template->param( loggedinusername => $user );
136 $template->param( sessionID => $sessionID );
138 # We are going to use the $flags returned by checkauth
139 # to create the template's parameters that will indicate
140 # which menus the user can access.
141 if ( ( $flags && $flags->{superlibrarian} == 1 ) ) {
142 $template->param( CAN_user_circulate => 1 );
143 $template->param( CAN_user_catalogue => 1 );
144 $template->param( CAN_user_parameters => 1 );
145 $template->param( CAN_user_borrowers => 1 );
146 $template->param( CAN_user_permission => 1 );
147 $template->param( CAN_user_reserveforothers => 1 );
148 $template->param( CAN_user_borrow => 1 );
149 $template->param( CAN_user_editcatalogue => 1 );
150 $template->param( CAN_user_updatecharges => 1 );
151 $template->param( CAN_user_acquisition => 1 );
152 $template->param( CAN_user_management => 1 );
153 $template->param( CAN_user_tools => 1 );
154 $template->param( CAN_user_editauthorities => 1 );
155 $template->param( CAN_user_serials => 1 );
156 $template->param( CAN_user_reports => 1 );
159 return ( $template, $borrowernumber, $cookie );
164 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
166 Verifies that the user is authorized to run this script. If
167 the user is authorized, a (userid, cookie, session-id, flags)
168 quadruple is returned. If the user is not authorized but does
169 not have the required privilege (see $flagsrequired below), it
170 displays an error page and exits. Otherwise, it displays the
171 login page and exits.
173 Note that C<&checkauth> will return if and only if the user
174 is authorized, so it should be called early on, before any
175 unfinished operations (e.g., if you've opened a file, then
176 C<&checkauth> won't close it for you).
178 C<$query> is the CGI object for the script calling C<&checkauth>.
180 The C<$noauth> argument is optional. If it is set, then no
181 authorization is required for the script.
183 C<&checkauth> fetches user and session information from C<$query> and
184 ensures that the user is authorized to run scripts that require
187 The C<$flagsrequired> argument specifies the required privileges
188 the user must have if the username and password are correct.
189 It should be specified as a reference-to-hash; keys in the hash
190 should be the "flags" for the user, as specified in the Members
191 intranet module. Any key specified must correspond to a "flag"
192 in the userflags table. E.g., { circulate => 1 } would specify
193 that the user must have the "circulate" privilege in order to
194 proceed. To make sure that access control is correct, the
195 C<$flagsrequired> parameter must be specified correctly.
197 The C<$type> argument specifies whether the template should be
198 retrieved from the opac or intranet directory tree. "opac" is
199 assumed if it is not specified; however, if C<$type> is specified,
200 "intranet" is assumed if it is not "opac".
202 If C<$query> does not have a valid session ID associated with it
203 (i.e., the user has not logged in) or if the session has expired,
204 C<&checkauth> presents the user with a login page (from the point of
205 view of the original script, C<&checkauth> does not return). Once the
206 user has authenticated, C<&checkauth> restarts the original script
207 (this time, C<&checkauth> returns).
209 The login page is provided using a HTML::Template, which is set in the
210 systempreferences table or at the top of this file. The variable C<$type>
211 selects which template to use, either the opac or the intranet
212 authentification template.
214 C<&checkauth> returns a user ID, a cookie, and a session ID. The
215 cookie should be sent back to the browser; it verifies that the user
223 # $authnotrequired will be set for scripts which will run without authentication
224 my $authnotrequired = shift;
225 my $flagsrequired = shift;
227 $type = 'intranet' unless $type;
229 my $dbh = C4::Context->dbh();
231 $template_name = "installer/auth.tmpl";
236 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
237 my $logout = $query->param('logout.x');
238 if ( $sessionID = $query->cookie("CGISESSID") ) {
239 C4::Context->_new_userenv($sessionID);
241 new CGI::Session( "driver:File;serializer:yaml", $sessionID,
242 { Directory => '/tmp' } );
243 if ( $session->param('cardnumber') ) {
244 C4::Context::set_userenv(
245 $session->param('number'),
246 $session->param('id'),
247 $session->param('cardnumber'),
248 $session->param('firstname'),
249 $session->param('surname'),
250 $session->param('branch'),
251 $session->param('branchname'),
252 $session->param('flags'),
253 $session->param('emailaddress'),
254 $session->param('branchprinter')
256 $cookie = $query->cookie( CGISESSID => $session->id );
258 $userid = $session->param('cardnumber');
260 my ( $ip, $lasttime );
264 # voluntary logout the user
265 C4::Context->_unset_userenv($sessionID);
268 open L, ">>/tmp/sessionlog";
269 my $time = localtime( time() );
270 printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
277 new CGI::Session( "driver:File;serializer:yaml", undef, { Directory => '/tmp' } );
278 $sessionID = $session->id;
279 $userid = $query->param('userid');
280 C4::Context->_new_userenv($sessionID);
281 my $password = $query->param('password');
282 C4::Context->_new_userenv($sessionID);
283 my ( $return, $cardnumber ) = checkpw( $userid, $password );
286 open L, ">>/tmp/sessionlog";
287 my $time = localtime( time() );
288 printf L "%20s from %16s logged in at %30s.\n", $userid,
289 $ENV{'REMOTE_ADDR'}, $time;
291 $cookie = $query->cookie( CGISESSID => $sessionID );
292 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(
299 C4::Context->config('user'), C4::Context->config('user'),
300 C4::Context->config('user'), "",
304 $session->param( 'number', 0 );
305 $session->param( 'id', C4::Context->config('user') );
306 $session->param( 'cardnumber', C4::Context->config('user') );
307 $session->param( 'firstname', C4::Context->config('user') );
308 $session->param( 'surname', C4::Context->config('user'), );
309 $session->param( 'branch', 'NO_LIBRARY_SET' );
310 $session->param( 'branchname', 'NO_LIBRARY_SET' );
311 $session->param( 'flags', 1 );
312 $session->param( 'emailaddress',
313 C4::Context->preference('KohaAdminEmailAddress') );
314 $session->param( 'ip', $session->remote_addr() );
315 $session->param( 'lasttime', time() );
316 $userid = C4::Context->config('user');
321 $info{'invalid_username_or_password'} = 1;
322 C4::Context->_unset_userenv($sessionID);
327 # finished authentification, now respond
332 $cookie = $query->cookie(
333 -name => 'CGISESSID',
339 return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
342 return ( $userid, $cookie, $sessionID, $flags );
346 # else we have a problem...
347 # get the inputs from the incoming query
349 foreach my $name ( param $query) {
350 (next) if ( $name eq 'userid' || $name eq 'password' );
351 my $value = $query->param($name);
352 push @inputs, { name => $name, value => $value };
356 C4::Context->config('intrahtdocs') . "/prog/"
357 . ( $query->param('language') ? $query->param('language') : "en" );
358 my $filename = "$path/modules/$template_name";
359 $filename =~ s/\.tmpl$/.tt/;
360 my $interface = 'intranet';
361 my $template = C4::Templates->new( $interface, $filename);
366 $template->param( login => 1 );
367 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
369 my $self_url = $query->url( -absolute => 1 );
370 $template->param( url => $self_url, );
371 $template->param( \%info );
372 $cookie = $query->cookie(
373 -name => 'CGISESSID',
374 -value => $sessionID,
377 print $query->header(
378 -type => 'text/html; charset=utf-8',
387 my ( $userid, $password ) = @_;
390 && $userid eq C4::Context->config('user')
391 && "$password" eq C4::Context->config('pass') )
394 # Koha superuser account
395 C4::Context->set_userenv(
397 C4::Context->config('user'),
398 C4::Context->config('user'),
399 C4::Context->config('user'),
406 && "$password" eq 'demo'
407 && C4::Context->config('demo') )
410 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
411 # some features won't be effective : modify systempref, modify MARC structure,
417 END { } # module clean-up code here (global destructor)