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