Bug 20727: Move temporary_directory() to C4::Context
[koha.git] / C4 / InstallAuth.pm
1 package C4::InstallAuth;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use Digest::MD5 qw(md5_base64);
23 use CGI::Session;
24 use File::Spec;
25
26 require Exporter;
27
28 use C4::Context;
29 use C4::Output;
30 use C4::Templates;
31 use C4::Koha;
32 use Koha::UploadedFile;
33
34 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35
36
37 =head1 NAME
38
39 InstallAuth - Authenticates Koha users for Install process
40
41 =head1 SYNOPSIS
42
43   use CGI qw ( -utf8 );
44   use InstallAuth;
45   use C4::Output;
46
47   my $query = new CGI;
48
49     my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50         {   template_name   => "opac-main.tt",
51             query           => $query,
52             type            => "opac",
53             authnotrequired => 1,
54             flagsrequired   => { acquisition => '*' },
55         }
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 ) = get_template_and_user(
86         {   template_name   => "opac-main.tt",
87             query           => $query,
88             type            => "opac",
89             authnotrequired => 1,
90             flagsrequired   => { acquisition => '*' },
91         }
92     );
93
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.
97
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
103     authenticated page.
104
105     More information on the C<gettemplate> sub can be found in the
106     Templates.pm module.
107
108 =cut
109
110 sub get_template_and_user {
111     my $in       = shift;
112     my $query    = $in->{'query'};
113     my $language =_get_template_language($query->cookie('KohaOpacLanguage'));
114     my $path     = C4::Context->config('intrahtdocs'). "/prog/". $language;
115
116     my $tmplbase = $in->{template_name};
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_editcatalogue    => 1 );
146             $template->param( CAN_user_updatecharges    => 1 );
147             $template->param( CAN_user_acquisition      => 1 );
148             $template->param( CAN_user_tools            => 1 );
149             $template->param( CAN_user_editauthorities  => 1 );
150             $template->param( CAN_user_serials          => 1 );
151             $template->param( CAN_user_reports          => 1 );
152         }
153
154         my $minPasswordLength = C4::Context->preference('minPasswordLength');
155         $minPasswordLength = 3 if not $minPasswordLength or $minPasswordLength < 3;
156         $template->param(minPasswordLength => $minPasswordLength,);
157     }
158     return ( $template, $borrowernumber, $cookie );
159 }
160
161 sub _get_template_language {
162
163     #verify if opac language exists in staff (bug 5660)
164     #conditions are 1) dir exists and 2) enabled in prefs
165     my ($opaclang) = @_;
166     return 'en' unless $opaclang;
167     $opaclang =~ s/[^a-zA-Z_-]*//g;
168     my $path = C4::Context->config('intrahtdocs') . "/prog/$opaclang";
169     -d $path ? $opaclang : 'en';
170 }
171
172 =item checkauth
173
174   ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
175
176 Verifies that the user is authorized to run this script.  If
177 the user is authorized, a (userid, cookie, session-id, flags)
178 quadruple is returned.  If the user is not authorized but does
179 not have the required privilege (see $flagsrequired below), it
180 displays an error page and exits.  Otherwise, it displays the
181 login page and exits.
182
183 Note that C<&checkauth> will return if and only if the user
184 is authorized, so it should be called early on, before any
185 unfinished operations (e.g., if you've opened a file, then
186 C<&checkauth> won't close it for you).
187
188 C<$query> is the CGI object for the script calling C<&checkauth>.
189
190 The C<$noauth> argument is optional. If it is set, then no
191 authorization is required for the script.
192
193 C<&checkauth> fetches user and session information from C<$query> and
194 ensures that the user is authorized to run scripts that require
195 authorization.
196
197 The C<$flagsrequired> argument specifies the required privileges
198 the user must have if the username and password are correct.
199 It should be specified as a reference-to-hash; keys in the hash
200 should be the "flags" for the user, as specified in the Members
201 intranet module. Any key specified must correspond to a "flag"
202 in the userflags table. E.g., { circulate => 1 } would specify
203 that the user must have the "circulate" privilege in order to
204 proceed. To make sure that access control is correct, the
205 C<$flagsrequired> parameter must be specified correctly.
206
207 The C<$type> argument specifies whether the template should be
208 retrieved from the opac or intranet directory tree.  "opac" is
209 assumed if it is not specified; however, if C<$type> is specified,
210 "intranet" is assumed if it is not "opac".
211
212 If C<$query> does not have a valid session ID associated with it
213 (i.e., the user has not logged in) or if the session has expired,
214 C<&checkauth> presents the user with a login page (from the point of
215 view of the original script, C<&checkauth> does not return). Once the
216 user has authenticated, C<&checkauth> restarts the original script
217 (this time, C<&checkauth> returns).
218
219 The login page is provided using a HTML::Template, which is set in the
220 systempreferences table or at the top of this file. The variable C<$type>
221 selects which template to use, either the opac or the intranet 
222 authentification template.
223
224 C<&checkauth> returns a user ID, a cookie, and a session ID. The
225 cookie should be sent back to the browser; it verifies that the user
226 has authenticated.
227
228 =cut
229
230 sub checkauth {
231     my $query = shift;
232
233 # $authnotrequired will be set for scripts which will run without authentication
234     my $authnotrequired = shift;
235     my $flagsrequired   = shift;
236     my $type            = shift;
237     $type = 'intranet' unless $type;
238
239     my $dbh = C4::Context->dbh();
240     my $template_name;
241     $template_name = "installer/auth.tt";
242     my $sessdir = File::Spec->catdir( C4::Context::temporary_directory, 'cgisess_' . C4::Context->config('database') ); # same construction as in C4/Auth
243
244     # state variables
245     my $loggedin = 0;
246     my %info;
247     my ( $userid, $cookie, $sessionID, $flags, $envcookie );
248     my $logout = $query->param('logout.x');
249     if ( $sessionID = $query->cookie("CGISESSID") ) {
250         C4::Context->_new_userenv($sessionID);
251         my $session =
252           new CGI::Session( "driver:File;serializer:yaml", $sessionID,
253             { Directory => $sessdir } );
254         if ( $session->param('cardnumber') ) {
255             C4::Context->set_userenv(
256                 $session->param('number'),
257                 $session->param('id'),
258                 $session->param('cardnumber'),
259                 $session->param('firstname'),
260                 $session->param('surname'),
261                 $session->param('branch'),
262                 $session->param('branchname'),
263                 $session->param('flags'),
264                 $session->param('emailaddress'),
265                 $session->param('branchprinter')
266             );
267             $cookie = $query->cookie(
268                 -name     => 'CGISESSID',
269                 -value    => $session->id,
270                 -HttpOnly => 1,
271             );
272             $loggedin = 1;
273             $userid   = $session->param('cardnumber');
274         }
275         my ( $ip, $lasttime );
276
277         if ($logout) {
278
279             # voluntary logout the user
280             C4::Context->_unset_userenv($sessionID);
281             $sessionID = undef;
282             $userid    = undef;
283            # Commented out due to its lack of usefulness
284            # open L, ">>/tmp/sessionlog";
285            # my $time = localtime( time() );
286            # printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
287            #   $ip, $time;
288            # close L;
289         }
290     }
291     unless ($userid) {
292         my $session =
293           new CGI::Session( "driver:File;serializer:yaml", undef, { Directory => $sessdir } );
294         $sessionID = $session->id;
295         $userid    = $query->param('userid');
296         C4::Context->_new_userenv($sessionID);
297         my $password = $query->param('password');
298         C4::Context->_new_userenv($sessionID);
299         my ( $return, $cardnumber ) = checkpw( $userid, $password );
300         if ($return) {
301             $loggedin = 1;
302             # open L, ">>/tmp/sessionlog";
303             # my $time = localtime( time() );
304             # printf L "%20s from %16s logged in  at %30s.\n", $userid,
305             #  $ENV{'REMOTE_ADDR'}, $time;
306             # close L;
307             $cookie = $query->cookie(
308                 -name     => 'CGISESSID',
309                 -value    => $sessionID,
310                 -HttpOnly => 1,
311             );
312             if ( $return == 2 ) {
313
314            #Only superlibrarian should have access to this page.
315            #Since if it is a user, it is supposed that there is a borrower table
316            #And thus that data structure is loaded.
317                 my $hash = C4::Context->set_userenv(
318                     0,                           0,
319                     C4::Context->config('user'), C4::Context->config('user'),
320                     C4::Context->config('user'), "",
321                     "NO_LIBRARY_SET",            1,
322                     ""
323                 );
324                 $session->param( 'number',     0 );
325                 $session->param( 'id',         C4::Context->config('user') );
326                 $session->param( 'cardnumber', C4::Context->config('user') );
327                 $session->param( 'firstname',  C4::Context->config('user') );
328                 $session->param( 'surname',    C4::Context->config('user'), );
329                 $session->param( 'branch',     'NO_LIBRARY_SET' );
330                 $session->param( 'branchname', 'NO_LIBRARY_SET' );
331                 $session->param( 'flags',      1 );
332                 $session->param( 'emailaddress',
333                     C4::Context->preference('KohaAdminEmailAddress') );
334                 $session->param( 'ip',       $session->remote_addr() );
335                 $session->param( 'lasttime', time() );
336                 $userid = C4::Context->config('user');
337             }
338         }
339         else {
340             if ($userid) {
341                 $info{'invalid_username_or_password'} = 1;
342                 C4::Context->_unset_userenv($sessionID);
343             }
344         }
345     }
346
347     # finished authentification, now respond
348     if ($loggedin) {
349
350         # successful login
351         unless ($cookie) {
352             $cookie = $query->cookie(
353                 -name    => 'CGISESSID',
354                 -value   => '',
355                 -HttpOnly => 1,
356                 -expires => ''
357             );
358         }
359         if ($envcookie) {
360             return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
361         }
362         else {
363             return ( $userid, $cookie, $sessionID, $flags );
364         }
365     }
366
367     # else we have a problem...
368     # get the inputs from the incoming query
369     my @inputs = ();
370     foreach my $name ( param $query) {
371         (next) if ( $name eq 'userid' || $name eq 'password' );
372         my $value = $query->param($name);
373         push @inputs, { name => $name, value => $value };
374     }
375
376     my $path =
377       C4::Context->config('intrahtdocs') . "/prog/"
378       . ( $query->param('language') ? $query->param('language') : "en" );
379     my $filename = "$path/modules/$template_name";
380     my $interface = 'intranet';
381     my $template = C4::Templates->new( $interface, $filename, '', $query);
382     $template->param(
383         INPUTS => \@inputs,
384
385     );
386     $template->param( login => 1 );
387     $template->param( loginprompt => 1 ) unless $info{'nopermission'};
388
389     if ($info{'invalid_username_or_password'} == 1) {
390                 $template->param( 'invalid_username_or_password' => $info{'invalid_username_or_password'});
391     }
392
393     $template->param( \%info );
394     $cookie = $query->cookie(
395         -name    => 'CGISESSID',
396         -value   => $sessionID,
397         -HttpOnly => 1,
398         -expires => ''
399     );
400     print $query->header(
401         -type    => 'text/html; charset=utf-8',
402         -cookie  => $cookie
403       ),
404       $template->output;
405     exit;
406 }
407
408 sub checkpw {
409
410     my ( $userid, $password ) = @_;
411
412     if (   $userid
413         && $userid     eq C4::Context->config('user')
414         && "$password" eq C4::Context->config('pass') )
415     {
416
417         # Koha superuser account
418         C4::Context->set_userenv(
419             0, 0,
420             C4::Context->config('user'),
421             C4::Context->config('user'),
422             C4::Context->config('user'),
423             "", "NO_LIBRARY_SET", 1
424         );
425         return 2;
426     }
427     return 0;
428 }
429
430 END { }    # module clean-up code here (global destructor)
431 1;
432 __END__
433
434 =back
435
436 =head1 SEE ALSO
437
438 CGI(3)
439
440 C4::Output(3)
441
442 Digest::MD5(3)
443
444 =cut