Merge branch 'bug_10074' into 3.14-master
[koha.git] / installer / InstallAuth.pm
1 # -*- tab-width: 8 -*-
2 # NOTE: This file uses 8-character tabs; do not change the tab size!
3
4 package InstallAuth;
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
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
13 # version.
14 #
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.
18 #
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.
22
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use Digest::MD5 qw(md5_base64);
26
27 require Exporter;
28 use C4::Context;
29 use C4::Templates;
30 use C4::Koha;
31 use CGI::Session;
32
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
34
35 # set the version for version checking
36 $VERSION = 3.00;
37
38 =head1 NAME
39
40 InstallAuth - Authenticates Koha users for Install process
41
42 =head1 SYNOPSIS
43
44   use CGI;
45   use InstallAuth;
46   use C4::Output;
47
48   my $query = new CGI;
49
50   my ($template, $borrowernumber, $cookie) 
51     = get_template_and_user({template_name   => "opac-main.tmpl",
52                              query           => $query,
53                              type            => "opac",
54                              authnotrequired => 1,
55                              flagsrequired   => {borrow => 1},
56                           });
57
58   output_html_with_http_headers $query, $cookie, $template->output;
59
60 =head1 DESCRIPTION
61
62     The main function of this module is to provide
63     authentification. However the get_template_and_user function has
64     been provided so that a users login information is passed along
65     automatically. This gets loaded into the template.
66     This package is different from C4::Auth in so far as 
67     C4::Auth uses many preferences which are supposed NOT to be obtainable when installing the database.
68     
69     As in C4::Auth, Authentication is based on cookies.
70
71 =head1 FUNCTIONS
72
73 =over 2
74
75 =cut
76
77 @ISA    = qw(Exporter);
78 @EXPORT = qw(
79   &checkauth
80   &get_template_and_user
81 );
82
83 =item get_template_and_user
84
85   my ($template, $borrowernumber, $cookie)
86     = get_template_and_user({template_name   => "opac-main.tmpl",
87                              query           => $query,
88                              type            => "opac",
89                              authnotrequired => 1,
90                              flagsrequired   => {borrow => 1},
91                           });
92
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.
96
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
102     authenticated page.
103
104     More information on the C<gettemplate> sub can be found in the
105     Templates.pm module.
106
107 =cut
108
109 sub get_template_and_user {
110     my $in       = shift;
111     my $query    = $in->{'query'};
112     my $language =_get_template_language($query->cookie('KohaOpacLanguage'));
113     my $path     = C4::Context->config('intrahtdocs'). "/prog/". $language;
114
115     my $tmplbase = $in->{template_name};
116     $tmplbase=~ s/\.tmpl$/.tt/;
117     my $filename = "$path/modules/" . $tmplbase;
118     my $interface = 'intranet';
119     my $template = C4::Templates->new( $interface, $filename, $tmplbase, $query);
120     
121     my ( $user, $cookie, $sessionID, $flags ) = checkauth(
122         $in->{'query'},
123         $in->{'authnotrequired'},
124         $in->{'flagsrequired'},
125         $in->{'type'}
126     );
127
128     #     use Data::Dumper;warn "utilisateur $user cookie : ".Dumper($cookie);
129
130     my $borrowernumber;
131     if ($user) {
132         $template->param( loggedinusername => $user );
133         $template->param( sessionID        => $sessionID );
134
135         # We are going to use the $flags returned by checkauth
136         # to create the template's parameters that will indicate
137         # which menus the user can access.
138         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_editcatalogue    => 1 );
147             $template->param( CAN_user_updatecharges    => 1 );
148             $template->param( CAN_user_acquisition      => 1 );
149             $template->param( CAN_user_management       => 1 );
150             $template->param( CAN_user_tools            => 1 );
151             $template->param( CAN_user_editauthorities  => 1 );
152             $template->param( CAN_user_serials          => 1 );
153             $template->param( CAN_user_reports          => 1 );
154         }
155     }
156     return ( $template, $borrowernumber, $cookie );
157 }
158
159 sub _get_template_language {
160
161     #verify if opac language exists in staff (bug 5660)
162     #conditions are 1) dir exists and 2) enabled in prefs
163     my ($opaclang) = @_;
164     return 'en' unless $opaclang;
165     $opaclang =~ s/[^a-zA-Z_-]*//g;
166     my $path = C4::Context->config('intrahtdocs') . "/prog/$opaclang";
167     -d $path ? $opaclang : 'en';
168 }
169
170 =item checkauth
171
172   ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
173
174 Verifies that the user is authorized to run this script.  If
175 the user is authorized, a (userid, cookie, session-id, flags)
176 quadruple is returned.  If the user is not authorized but does
177 not have the required privilege (see $flagsrequired below), it
178 displays an error page and exits.  Otherwise, it displays the
179 login page and exits.
180
181 Note that C<&checkauth> will return if and only if the user
182 is authorized, so it should be called early on, before any
183 unfinished operations (e.g., if you've opened a file, then
184 C<&checkauth> won't close it for you).
185
186 C<$query> is the CGI object for the script calling C<&checkauth>.
187
188 The C<$noauth> argument is optional. If it is set, then no
189 authorization is required for the script.
190
191 C<&checkauth> fetches user and session information from C<$query> and
192 ensures that the user is authorized to run scripts that require
193 authorization.
194
195 The C<$flagsrequired> argument specifies the required privileges
196 the user must have if the username and password are correct.
197 It should be specified as a reference-to-hash; keys in the hash
198 should be the "flags" for the user, as specified in the Members
199 intranet module. Any key specified must correspond to a "flag"
200 in the userflags table. E.g., { circulate => 1 } would specify
201 that the user must have the "circulate" privilege in order to
202 proceed. To make sure that access control is correct, the
203 C<$flagsrequired> parameter must be specified correctly.
204
205 The C<$type> argument specifies whether the template should be
206 retrieved from the opac or intranet directory tree.  "opac" is
207 assumed if it is not specified; however, if C<$type> is specified,
208 "intranet" is assumed if it is not "opac".
209
210 If C<$query> does not have a valid session ID associated with it
211 (i.e., the user has not logged in) or if the session has expired,
212 C<&checkauth> presents the user with a login page (from the point of
213 view of the original script, C<&checkauth> does not return). Once the
214 user has authenticated, C<&checkauth> restarts the original script
215 (this time, C<&checkauth> returns).
216
217 The login page is provided using a HTML::Template, which is set in the
218 systempreferences table or at the top of this file. The variable C<$type>
219 selects which template to use, either the opac or the intranet 
220 authentification template.
221
222 C<&checkauth> returns a user ID, a cookie, and a session ID. The
223 cookie should be sent back to the browser; it verifies that the user
224 has authenticated.
225
226 =cut
227
228 sub checkauth {
229     my $query = shift;
230
231 # $authnotrequired will be set for scripts which will run without authentication
232     my $authnotrequired = shift;
233     my $flagsrequired   = shift;
234     my $type            = shift;
235     $type = 'intranet' unless $type;
236
237     my $dbh = C4::Context->dbh();
238     my $template_name;
239     $template_name = "installer/auth.tmpl";
240
241     # state variables
242     my $loggedin = 0;
243     my %info;
244     my ( $userid, $cookie, $sessionID, $flags, $envcookie );
245     my $logout = $query->param('logout.x');
246     if ( $sessionID = $query->cookie("CGISESSID") ) {
247         C4::Context->_new_userenv($sessionID);
248         my $session =
249           new CGI::Session( "driver:File;serializer:yaml", $sessionID,
250             { Directory => '/tmp' } );
251         if ( $session->param('cardnumber') ) {
252             C4::Context::set_userenv(
253                 $session->param('number'),
254                 $session->param('id'),
255                 $session->param('cardnumber'),
256                 $session->param('firstname'),
257                 $session->param('surname'),
258                 $session->param('branch'),
259                 $session->param('branchname'),
260                 $session->param('flags'),
261                 $session->param('emailaddress'),
262                 $session->param('branchprinter')
263             );
264             $cookie = $query->cookie(
265                 -name     => 'CGISESSID',
266                 -value    => $session->id,
267                 -HttpOnly => 1,
268             );
269             $loggedin = 1;
270             $userid   = $session->param('cardnumber');
271         }
272         my ( $ip, $lasttime );
273
274         if ($logout) {
275
276             # voluntary logout the user
277             C4::Context->_unset_userenv($sessionID);
278             $sessionID = undef;
279             $userid    = undef;
280            # Commented out due to its lack of usefulness
281            # open L, ">>/tmp/sessionlog";
282            # my $time = localtime( time() );
283            # printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
284            #   $ip, $time;
285            # close L;
286         }
287     }
288     unless ($userid) {
289         my $session =
290           new CGI::Session( "driver:File;serializer:yaml", undef, { Directory => '/tmp' } );
291         $sessionID = $session->id;
292         $userid    = $query->param('userid');
293         C4::Context->_new_userenv($sessionID);
294         my $password = $query->param('password');
295         C4::Context->_new_userenv($sessionID);
296         my ( $return, $cardnumber ) = checkpw( $userid, $password );
297         if ($return) {
298             $loggedin = 1;
299             # open L, ">>/tmp/sessionlog";
300             # my $time = localtime( time() );
301             # printf L "%20s from %16s logged in  at %30s.\n", $userid,
302             #  $ENV{'REMOTE_ADDR'}, $time;
303             # close L;
304             $cookie = $query->cookie(
305                 -name     => 'CGISESSID',
306                 -value    => $sessionID,
307                 -HttpOnly => 1,
308             );
309             if ( $return == 2 ) {
310
311            #Only superlibrarian should have access to this page.
312            #Since if it is a user, it is supposed that there is a borrower table
313            #And thus that data structure is loaded.
314                 my $hash = C4::Context::set_userenv(
315                     0,                           0,
316                     C4::Context->config('user'), C4::Context->config('user'),
317                     C4::Context->config('user'), "",
318                     "NO_LIBRARY_SET",            1,
319                     ""
320                 );
321                 $session->param( 'number',     0 );
322                 $session->param( 'id',         C4::Context->config('user') );
323                 $session->param( 'cardnumber', C4::Context->config('user') );
324                 $session->param( 'firstname',  C4::Context->config('user') );
325                 $session->param( 'surname',    C4::Context->config('user'), );
326                 $session->param( 'branch',     'NO_LIBRARY_SET' );
327                 $session->param( 'branchname', 'NO_LIBRARY_SET' );
328                 $session->param( 'flags',      1 );
329                 $session->param( 'emailaddress',
330                     C4::Context->preference('KohaAdminEmailAddress') );
331                 $session->param( 'ip',       $session->remote_addr() );
332                 $session->param( 'lasttime', time() );
333                 $userid = C4::Context->config('user');
334             }
335         }
336         else {
337             if ($userid) {
338                 $info{'invalid_username_or_password'} = 1;
339                 C4::Context->_unset_userenv($sessionID);
340             }
341         }
342     }
343
344     # finished authentification, now respond
345     if ($loggedin) {
346
347         # successful login
348         unless ($cookie) {
349             $cookie = $query->cookie(
350                 -name    => 'CGISESSID',
351                 -value   => '',
352                 -HttpOnly => 1,
353                 -expires => ''
354             );
355         }
356         if ($envcookie) {
357             return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
358         }
359         else {
360             return ( $userid, $cookie, $sessionID, $flags );
361         }
362     }
363
364     # else we have a problem...
365     # get the inputs from the incoming query
366     my @inputs = ();
367     foreach my $name ( param $query) {
368         (next) if ( $name eq 'userid' || $name eq 'password' );
369         my $value = $query->param($name);
370         push @inputs, { name => $name, value => $value };
371     }
372
373     my $path =
374       C4::Context->config('intrahtdocs') . "/prog/"
375       . ( $query->param('language') ? $query->param('language') : "en" );
376     my $filename = "$path/modules/$template_name";
377     $filename =~ s/\.tmpl$/.tt/;
378     my $interface = 'intranet';
379     my $template = C4::Templates->new( $interface, $filename, '', $query);
380     $template->param(
381         INPUTS => \@inputs,
382
383     );
384     $template->param( login => 1 );
385     $template->param( loginprompt => 1 ) unless $info{'nopermission'};
386
387     my $self_url = $query->url( -absolute => 1 );
388     $template->param( url => $self_url, );
389     $template->param( \%info );
390     $cookie = $query->cookie(
391         -name    => 'CGISESSID',
392         -value   => $sessionID,
393         -HttpOnly => 1,
394         -expires => ''
395     );
396
397     require C4::Output;
398     C4::Output::output_html_with_http_headers($query,$cookie,$template->output);
399     exit;
400 }
401
402 sub checkpw {
403
404     my ( $userid, $password ) = @_;
405
406     if (   $userid
407         && $userid     eq C4::Context->config('user')
408         && "$password" eq C4::Context->config('pass') )
409     {
410
411         # Koha superuser account
412         C4::Context->set_userenv(
413             0, 0,
414             C4::Context->config('user'),
415             C4::Context->config('user'),
416             C4::Context->config('user'),
417             "", 1
418         );
419         return 2;
420     }
421     if (   $userid
422         && $userid     eq 'demo'
423         && "$password" eq 'demo'
424         && C4::Context->config('demo') )
425     {
426
427 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
428 # some features won't be effective : modify systempref, modify MARC structure,
429         return 2;
430     }
431     return 0;
432 }
433
434 END { }    # module clean-up code here (global destructor)
435 1;
436 __END__
437
438 =back
439
440 =head1 SEE ALSO
441
442 CGI(3)
443
444 C4::Output(3)
445
446 Digest::MD5(3)
447
448 =cut