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