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