Debian Codegraph @

cgi/logout.cgi

raw
1 #!/usr/bin/perl -w
2 #
3 # mod_auth_tkt sample logout script
4 #
5 # Note that this needs script needs to be available locally on all domains
6 # if using multiple domains (unlike login.cgi, which only needs to exist
7 # on one domain).
8 #
9
10 use File::Basename;
11 use lib dirname($ENV{SCRIPT_FILENAME});
12 use Apache::AuthTkt 0.03;
13 use AuthTktConfig;
14 use CGI qw(:standard);
15 use URI::Escape;
16 use URI;
17 use strict;
18
19 # ------------------------------------------------------------------------
20 # Configuration settings in AuthTktConfig.pm
21
22 # ------------------------------------------------------------------------
23 # Main code begins
24 my $at = Apache::AuthTkt->new(conf => $ENV{MOD_AUTH_TKT_CONF});
25 my $q = CGI->new;
26 my ($server_name, $server_port) = split /:/, $ENV{HTTP_HOST};
27 $server_name ||= $ENV{SERVER_NAME};
28 $server_port ||= $ENV{SERVER_PORT};
29 my $AUTH_DOMAIN = $at->domain || $server_name;
30 my $back = '';
31 $back = $AuthTktConfig::FIXED_BACK_LOCATION if $AuthTktConfig::FIXED_BACK_LOCATION;
32 $back ||= $q->cookie($at->back_cookie_name) if $at->back_cookie_name;
33 $back ||= $q->param($at->back_arg_name) if $at->back_arg_name;
34 $back = $AuthTktConfig::DEFAULT_BACK_LOCATION if $AuthTktConfig::DEFAULT_BACK_LOCATION;
35 $back ||= $ENV{HTTP_REFERER} if $ENV{HTTP_REFERER} && $AuthTktConfig::BACK_REFERER;
36 my $back_html = '';
37 if ($back) {
38 if ($back =~ m!^/!) {
39 my $hostname = $server_name;
40 my $port = $server_port;
41 $hostname .= ':' . $port if $port && $port != 80 && $port != 443;
42 $back = sprintf "http%s://%s%s", ($port == 443 ? 's' : ''), $hostname, $back;
43 } elsif ($back !~ m/^http/i) {
44 $back = 'http://' . $back;
45 }
46 $back = uri_unescape($back) if $back =~ m/^https?%3A%2F%2F/;
47 $back_html = escapeHTML($back);
48 }
49
50 # Logout by resetting the auth cookie
51 my @cookies = cookie(-name => $at->cookie_name, -value => '', -expires => '-1h',
52 ($AUTH_DOMAIN ? (-domain => $AUTH_DOMAIN) : ()));
53 push @cookies, map { cookie(-name => $_, -value => '', -expires => '-1h') } @AuthTktConfig::NUKE_COOKIES;
54
55 my $redirected = 0;
56 if ($back) {
57 my $b = URI->new($back);
58 # If $back domain doesn't match $AUTH_DOMAIN, add ticket reset to back
59 if ($b->host !~ m/\b$AUTH_DOMAIN$/i) {
60 $back .= $b->query ? '&' : '?';
61 $back .= $at->cookie_name . '=';
62 }
63
64 if ($AuthTktConfig::DEBUG) {
65 print $q->header(-cookie => \@cookies);
66 }
67
68 else {
69 # Set (local) cookie, and redirect to $back
70 print $q->header(
71 -cookie => \@cookies,
72 # -location => $back,
73 );
74 # For some reason, a Location: redirect doesn't seem to then see the cookie,
75 # but a meta refresh one does - weird
76 print $q->start_html(
77 -head => meta({
78 -http_equiv => 'refresh', -content => "0;URL=$back"
79 }));
80 $redirected = 1;
81 }
82 }
83
84 # If no $back, just set the auth cookie and hope for the best
85 else {
86 print $q->header(-cookie => \@cookies);
87 }
88
89 my @style = ();
90 @style = ( '-style' => { src => $AuthTktConfig::STYLESHEET } )
91 if $AuthTktConfig::STYLESHEET;
92 my $title = $AuthTktConfig::TITLE || "Logout Page";
93 unless ($redirected) {
94 # If here, either some kind of error or no back ref found
95 print $q->start_html(
96 -title => $title,
97 @style,
98 );
99 print <<EOD;
100 <div align="center">
101 <h1>$title</h1>
102 EOD
103 if ($AuthTktConfig::DEBUG) {
104 print <<EOD;
105 <pre>
106 back: $back
107 back_html: $back_html
108 </pre>
109 EOD
110 }
111 print <<EOD;
112 <p>You are now logged out.</p>
113 EOD
114 print qq(<p><a href="$back_html">Previous Page</a></p>\n) if $back_html;
115 print <<EOD;
116 </div>
117 </body>
118 </html>
119 EOD
120 }
121
122 # vim:sw=2:sm:cin
123