Debian Codegraph @

cgi/login.cgi

raw
1 #!/usr/bin/perl -w
2 #
3 # mod_auth_tkt sample login script - runs as a vanilla CGI, under
4 # mod_perl 1 via Apache::Registry, and under mod_perl2 via
5 # ModPerl::Registry.
6 #
7 # This script can run in a few different modes, depending on how it is
8 # named. Copy the script to a cgi-bin area, and create appropriately
9 # named symlinks to access the different behaviours.
10 # Modes:
11 # - login mode (default): request a username and password and test via
12 # $AuthTktConfig::validate_sub - if successful, issue an auth ticket
13 # and redirect to the back location
14 # - autologin mode ('autologin.cgi'): [typically used to allow tickets
15 # across multiple domains] if no valid auth ticket exists, redirect
16 # to the login (or guest) version; otherwise automatically redirect
17 # to the back location passing the current auth ticket as a GET
18 # argument. mod_auth_tkt (>= 1.3.8) will turn this new ticket into
19 # an auth cookie for the new domain if none already exists.
20 # - guest mode ('guest.cgi'): [DEPRECATED - use TktAuthGuestLogin instead]
21 # automatically issues an auth ticket a special username (as defined in
22 # $AuthTktConfig::guest_sub, default 'guest'), and redirect to the back
23 # location
24 #
25
26 use File::Basename;
27 use lib dirname($ENV{SCRIPT_FILENAME});
28 use Apache::AuthTkt 0.03;
29 use AuthTktConfig;
30 use CGI qw(:standard);
31 use CGI::Cookie;
32 use URI::Escape;
33 use URI;
34 use strict;
35
36 # ------------------------------------------------------------------------
37 # Configuration settings in AuthTktConfig.pm
38
39 # ------------------------------------------------------------------------
40 # Main code begins
41 my $at = Apache::AuthTkt->new(conf => $ENV{MOD_AUTH_TKT_CONF});
42 my $q = CGI->new;
43 my ($server_name, $server_port) = split /:/, $ENV{HTTP_HOST} if $ENV{HTTP_HOST};
44 $server_name ||= $ENV{SERVER_NAME} if $ENV{SERVER_NAME};
45 $server_port ||= $ENV{SERVER_PORT} if $ENV{SERVER_PORT};
46 my $AUTH_DOMAIN = $at->domain || $server_name;
47 my @auth_domain = $AUTH_DOMAIN ? ( -domain => $AUTH_DOMAIN ) : ();
48 my $ticket = $q->cookie($at->cookie_name);
49 my $probe = $q->cookie('auth_probe');
50 my $back_cookie = $q->cookie($at->back_cookie_name) if $at->back_cookie_name;
51 my $have_cookies = $ticket || $probe || $back_cookie || '';
52 my $back = '';
53 $back = $AuthTktConfig::FIXED_BACK_LOCATION if $AuthTktConfig::FIXED_BACK_LOCATION;
54 $back ||= $back_cookie;
55 $back ||= $q->param($at->back_arg_name) if $at->back_arg_name;
56 $back ||= $AuthTktConfig::DEFAULT_BACK_LOCATION if $AuthTktConfig::DEFAULT_BACK_LOCATION;
57 $back ||= $ENV{HTTP_REFERER} if $ENV{HTTP_REFERER} && $AuthTktConfig::BACK_REFERER;
58 if ($back && $back =~ m!^/!) {
59 my $hostname = $server_name;
60 my $port = $server_port;
61 $hostname .= ':' . $port if $port && $port != 80 && $port != 443;
62 $back = sprintf "http%s://%s%s", ($port == 443 ? 's' : ''), $hostname, $back;
63 } elsif ($back && $back !~ m/^http/i) {
64 $back = 'http://' . $back;
65 }
66 $back = uri_unescape($back) if $back && $back =~ m/^https?%3A%2F%2F/;
67 my $back_esc = uri_escape($back) if $back;
68 my $back_html = escapeHTML($back) if $back;
69
70 my ($fatal, @errors);
71 my ($mode, $location, $suffix) = fileparse($ENV{SCRIPT_NAME}, '\.cgi', '\.pl');
72 $mode = 'login' unless $mode eq 'guest' || $mode eq 'autologin';
73 my $self_redirect = $q->param('redirect') || 0;
74 my $username = lc($q->param('username'));
75 my $password = $q->param('password');
76 my $timeout = $q->param('timeout');
77 my $unauth = $q->param('unauth');
78 my $ip_addr = $at->ignore_ip ? '' : $ENV{REMOTE_ADDR};
79 my $redirected = 0;
80
81 # ------------------------------------------------------------------------
82 # Set the auth cookie and redirect to $back
83 my $set_cookie_redirect = sub {
84 my ($tkt, $back) = @_;
85 my @expires = $at->cookie_expires ?
86 ( -expires => sprintf("+%ss", $at->cookie_expires) ) :
87 ();
88 my $cookie = CGI::Cookie->new(
89 -name => $at->cookie_name,
90 -value => $tkt,
91 -path => '/',
92 -secure => $at->require_ssl,
93 @expires,
94 @auth_domain,
95 );
96
97 # If no $back, just set the auth cookie and hope for the best
98 if (! $back) {
99 print $q->header( -cookie => $cookie );
100 print $q->start_html, $q->p(Login successful), $q->end_html;
101 return 0;
102 }
103
104 # Set (local) cookie, and redirect to $back
105 print $q->header( -cookie => $cookie );
106 return 0 if $AuthTktConfig::DEBUG;
107
108 my $b = URI->new($back);
109 # If $back domain doesn't match $AUTH_DOMAIN, pass ticket via back GET param
110 my $domain = $AUTH_DOMAIN || $server_name;
111 if ($b->host !~ m/\b$domain$/i) {
112 $back .= $b->query ? '&' : '?';
113 $back .= $at->cookie_name . '=' . $tkt;
114 }
115
116 # For some reason, using a Location: header doesn't seem to then see the
117 # cookie, but a meta refresh one does - weird
118 print $q->start_html(
119 -head => meta({ -http_equiv => 'refresh', -content => "0;URL=$back" }),
120 ),
121 $q->end_html;
122 return 1;
123 };
124
125 # ------------------------------------------------------------------------
126 # Actual processing
127
128 # If no cookies found, first check whether cookies are supported
129 if (! $have_cookies) {
130 # If this is a self redirect warn the user about cookie support
131 if ($self_redirect) {
132 $fatal = "Your browser does not appear to support cookies or has cookie support disabled.<br />\nThis site requires cookies - please turn cookie support on or try again using a different browser.";
133 }
134 # If no cookies and not a redirect, redirect to self to test cookies
135 else {
136 my $extra = '';
137 $extra .= 'timeout=1' if $timeout;
138 $extra .= 'unauth=1' if $unauth;
139 $extra = "&$extra" if $extra;
140 print $q->header(
141 -cookie => CGI::Cookie->new(-name => 'auth_probe', -value => 1, @auth_domain),
142 );
143 # For some reason, a Location: redirect doesn't seem to then see the cookie,
144 # but a meta refresh one does - go figure
145 print $q->start_html(
146 -head => meta({
147 -http_equiv => 'refresh', -content => ("0;URL=" . sprintf("%s%s%s?redirect=%s&%s=%s%s",
148 $location, $mode, $suffix, $self_redirect + 1, $at->back_arg_name,
149 $back_esc || '', $extra))
150 }));
151 $redirected = 1;
152 }
153 }
154
155 elsif ($mode eq 'autologin') {
156 # If we have a ticket, redirect to $back, including ticket as GET param
157 if ($ticket && $back && ! $timeout) {
158 my $b = URI->new($back);
159 $back .= $b->query ? '&' : '?';
160 $back .= $at->cookie_name . '=' . $ticket;
161 print $q->redirect($back);
162 $redirected = 1;
163 }
164 # Can't autologin - change mode to either guest or login
165 else {
166 $mode = $AuthTktConfig::AUTOLOGIN_FALLBACK_MODE;
167 }
168 }
169
170 unless ($fatal || $redirected) {
171 if (! $at) {
172 $fatal = "AuthTkt error: " . $at->errstr;
173 }
174 elsif ($mode eq 'login') {
175 if ($username) {
176 my ($valid, $tokens) = $AuthTktConfig::validate_sub->($username, $password);
177 if ($valid) {
178 # my $user_data = join(':', encrypt($password), time(), ($ip_addr ? $ip_addr : ''));
179 my $user_data = join(':', time(), ($ip_addr ? $ip_addr : '')); # Optional
180 my $tkt = $at->ticket(uid => $username, data => $user_data,
181 ip_addr => $ip_addr, tokens => $tokens, debug => $AuthTktConfig::DEBUG);
182 if (! @errors) {
183 $redirected = $set_cookie_redirect->($tkt, $back);
184 $fatal = "Login successful.";
185 }
186 }
187 else {
188 push @errors, "Invalid username or password.";
189 }
190 }
191 }
192
193 elsif ($mode eq 'guest') {
194 # Generate a guest ticket and redirect to $back
195 my $tkt = $at->ticket(uid => $AuthTktConfig::guest_sub->(), ip_addr => $ip_addr);
196 if (! @errors) {
197 $redirected = $set_cookie_redirect->($tkt, $back);
198 $fatal = "No back link found.";
199 }
200 }
201 }
202
203 my @style = ();
204 @style = ( '-style' => { src => $AuthTktConfig::STYLESHEET } )
205 if $AuthTktConfig::STYLESHEET;
206 my $title = $AuthTktConfig::TITLE || "\u$mode Page";
207 unless ($redirected) {
208 # If here, either some kind of error or a login page
209 if ($fatal) {
210 print $q->header,
211 $q->start_html(
212 -title => $title,
213 @style,
214 );
215 }
216 else {
217 push @errors, qq(Your session has timed out.) if $timeout;
218 push @errors, qq(You are not authorised to access this area.) if $unauth;
219 print $q->header,
220 $q->start_html(
221 -title => $title,
222 -onLoad => "getFocus()",
223 @style,
224 -script => qq(
225 function getFocus() {
226 document.forms[0].elements[0].focus();
227 document.forms[0].elements[0].select();
228 }));
229 }
230 print <<EOD;
231 <div align="center">
232 <h1>$title</h1>
233 <p class="warning">Authorized Use Only</p>
234 EOD
235
236 if ($AuthTktConfig::DEBUG) {
237 my $cookie_name = $at->cookie_name;
238 my $back_cookie_name = $at->back_cookie_name || '';
239 my $back_arg_name = $at->back_arg_name || '';
240 my $cookie_expires = $at->cookie_expires || 0;
241 print <<EOD;
242 <pre>
243 server_name: $server_name
244 server_port: $server_port
245 domain: $AUTH_DOMAIN
246 mode: $mode
247 suffix: $suffix
248 cookie_name: $cookie_name
249 cookie_expires: $cookie_expires
250 back_cookie_name: $back_cookie_name
251 back_arg_name: $back_arg_name
252 back: $back
253 back_esc: $back_esc
254 back_html: $back_html
255 have_cookies: $have_cookies
256 ip_addr: $ip_addr
257 EOD
258 if ($Apache::AuthTkt::VERSION >= 2.1) {
259 printf "digest_type: %s\n", $at->digest_type;
260 }
261 print "</pre>\n";
262 }
263
264 if ($fatal) {
265 print qq(<p class="error">$fatal</p>\n);
266 }
267
268 else {
269 print qq(<p class="error">\n), join(qq(<br />\n), @errors), "</p>\n"
270 if @errors;
271 print <<EOD;
272 <form name="login" method="post" action="$mode$suffix">
273 <table border="0" cellpadding="5">
274 <tr><th>Username:</th><td><input type="text" name="username" /></td></tr>
275 <tr><th>Password:</th><td><input type="password" name="password" /></td></tr>
276 <tr><td colspan="2" align="center">
277 <input type="submit" value="Login" />
278 </td></tr>
279 </table>
280 EOD
281 print qq(<input type="hidden" name="back" value="$back_html" />\n) if $back_html;
282 print qq(</form>\n);
283 }
284
285 print qq(<p><a href="$back_html">Previous Page</a></p>\n) if $back_html;
286 print <<EOD;
287 </div>
288 </body>
289 </html>
290 EOD
291 }
292
293 # vim:sw=2:sm:cin
294