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