Benutzerauthentifizierung: Unterscheidung zwischen "Falsches Passwort" und "Session...
[kivitendo-erp.git] / am.pl
1 #!/usr/bin/perl
2 #
3 ######################################################################
4 # SQL-Ledger Accounting
5 # Copyright (C) 2001
6 #
7 #  Author: Dieter Simader
8 #   Email: dsimader@sql-ledger.org
9 #     Web: http://www.sql-ledger.org
10 #
11 #  Contributors:
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #######################################################################
26 #
27 # this script is the frontend called from bin/$terminal/$script
28 # all the accounting modules are linked to this script which in
29 # turn execute the same script in bin/$terminal/
30 #
31 #######################################################################
32
33 BEGIN {
34   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
35   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
36 }
37
38 # setup defaults, DO NOT CHANGE
39 $userspath  = "users";
40 $templates  = "templates";
41 $memberfile = "users/members";
42 $sendmail   = "| /usr/sbin/sendmail -t";
43 ########## end ###########################################
44
45 $| = 1;
46
47 use SL::LXDebug;
48 $lxdebug = LXDebug->new();
49
50 use CGI qw( -no_xhtml);
51 use SL::Auth;
52 use SL::Form;
53 use SL::Locale;
54
55 eval { require "config/lx-erp.conf"; };
56 eval { require "config/lx-erp-local.conf"; } if -f "config/lx-erp-local.conf";
57
58 our $cgi  = new CGI('');
59 our $form = new Form;
60
61 our $auth = SL::Auth->new();
62 if (!$auth->session_tables_present()) {
63   _show_error('login/auth_db_unreachable');
64 }
65 $auth->expire_sessions();
66 my $session_result = $auth->restore_session();
67
68 require "bin/mozilla/common.pl";
69
70 if (defined($latex) && !defined($latex_templates)) {
71   $latex_templates = $latex;
72   undef($latex);
73 }
74
75 # this prevents most of the tabindexes being created by CGI.
76 # note: most. popup menus and selecttables will still have tabindexes
77 # use common.pl's NTI function to get rid of those
78 local $CGI::TABINDEX = 0;
79
80 # name of this script
81 $0 =~ tr/\\/\//;
82 $pos = rindex $0, '/';
83 $script = substr($0, $pos + 1);
84
85 # we use $script for the language module
86 $form->{script} = $script;
87
88 # strip .pl for translation files
89 $script =~ s/\.pl//;
90
91 # pull in DBI
92 use DBI;
93
94 # locale messages
95 $locale = new Locale($language, "$script");
96
97 # did sysadmin lock us out
98 if (-e "$userspath/nologin") {
99   $form->error($locale->text('System currently down for maintenance!'));
100 }
101
102 if (SL::Auth::SESSION_EXPIRED == $session_result) {
103   _show_error('login/password_error', 'session');
104 }
105
106 $form->{login} =~ s|.*/||;
107
108 %myconfig = $auth->read_user($form->{login});
109
110 if (!$myconfig{login}) {
111   _show_error('login/password_error', 'password');
112 }
113
114 # locale messages
115 $locale = new Locale "$myconfig{countrycode}", "$script";
116
117 if (SL::Auth::OK != $auth->authenticate($form->{login}, $form->{password}, 0)) {
118   _show_error('login/password_error', 'password');
119 }
120
121 $auth->set_session_value('login', $form->{login}, 'password', $form->{password});
122 $auth->create_or_refresh_session();
123
124 delete $form->{password};
125
126 map { $form->{$_} = $myconfig{$_} } qw(stylesheet charset)
127   unless (($form->{action} eq 'save') && ($form->{type} eq 'preferences'));
128
129 # pull in the main code
130 require "bin/mozilla/$form->{script}";
131
132 # customized scripts
133 if (-f "bin/mozilla/custom_$form->{script}") {
134   eval { require "bin/mozilla/custom_$form->{script}"; };
135   $form->error($@) if ($@);
136 }
137
138 # customized scripts for login
139 if (-f "bin/mozilla/$form->{login}_$form->{script}") {
140   eval { require "bin/mozilla/$form->{login}_$form->{script}"; };
141   $form->error($@) if ($@);
142 }
143
144 if ($form->{action}) {
145
146   # window title bar, user info
147   $form->{titlebar} =
148       "Lx-Office "
149     . $locale->text('Version')
150     . " $form->{version} - $myconfig{name} - $myconfig{dbname}";
151
152   call_sub($locale->findsub($form->{action}));
153 } else {
154   $form->error($locale->text('action= not defined!'));
155 }
156
157 sub _show_error {
158   my $template           = shift;
159   my $error_type         = shift;
160   $locale                = Locale->new($language, 'all');
161   $form->{error}         = $locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
162   $form->{error}         = $locale->text('Incorrect password!.')                   if ($error_type eq 'password');
163   $myconfig{countrycode} = $language;
164   $form->{stylesheet}    = 'css/lx-office-erp.css';
165
166   $form->header();
167   print $form->parse_html_template($template);
168   exit;
169 }
170
171 # end
172