examples/htdocs/login.pl untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 136 CC 2 SCAR 11.0
CC
2
Cov
0%
BEGIN
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.0
1 #!/usr/bin/perl
2 #
3 # $Id: login.pl,v 1.9 2008/05/03 06:43:24 pauldoom Exp $
4  
5 # Decide which mod_perl to load
6 BEGIN {
7 use vars qw($MP);
8 if (eval{require mod_perl2;}) {
9 $MP = 2;
10 } else {
11 require mod_perl;
12 $MP = 1;
13 }
14 }
15  
16 use strict;
17 use warnings;
18 # Use the session key maker and session ID computer (which is just a
19 # HMAC) for CSRF protection in the login form
20 use Apache::AppSamurai::Util qw(CreateSessionAuthKey ComputeSessionId
21 CheckSidFormat HashPass);
22  
23  
24 # Point to HTML login page
25 my $formsource = "login.html";
26  
27 # Mod_Perl 2 does not chdir to the script's folder, so you must use
28 # a full path. The list below includes common base paths. Remove
29 # the other array items and enter your local path if none of these
30 # match you setup.
31 my @formpaths = ( "/var/www/htdocs/AppSamurai",
32 "/var/www/html/AppSamurai",
33 "/htdocs/AppSamurai",
34 "/html/AppSamurai"
35 );
36  
37 # This is lame. Just cycles the paths looking for the form source
38 # template ($formsource)
39 my $ffound = 0;
40 foreach (@formpaths) {
41 if (-f "$_/$formsource") {
42 $formsource = "$_/$formsource";
43 $ffound = 1;
44 last;
45 }
46 }
47  
48 ($ffound) or die "FATAL: Could not find form source template file $formsource\n";
49  
50 # These will replace any __NAME__ values in the form
51 my %params = ( MESSAGE => '',
52 REASON => '',
53 URI => '',
54 FORMACTION => '/AppSamurai/LOGIN',
55 USERNAME => ''
56 );
57  
58  
59 my $r = shift;
60 ($r) or die "FATAL: NO REQUEST SENT TO SCRIPT!\n";
61  
62 # if there are args, append that to the uri after checking for and removing
63 # any ASERRCODE code.
64 $params{URI} = $r->prev->uri || '';
65  
66 my $args = $r->prev->args || '';
67  
68 if (($args) && ($args =~ s/&?ASERRCODE\=(bad_credentials|no_cookie|bad_cookie|expired_cookie)//)) {
69 $params{REASON} = $1;
70 }
71  
72 if ($args) {
73 $params{URI} .= '?' . $args;
74 }
75  
76 # These messages have HTML in them with CSS. (Update as needed, or add a
77 # JavaScript snippet to check a hidden value and display the corresponding
78 # message, then just set a variable.)
79  
80 # Default message
81 $params{MESSAGE} = "<span class=\"infonormal\">Please log in</span>";
82  
83 if ($params{REASON} eq 'bad_credentials') {
84 # Login failure
85 $params{MESSAGE} = "<span class=\"infored\">Access Denied - The credentials supplied were invalid. Please try again.</span>";
86 } elsif ($params{REASON} eq 'expired_cookie') {
87 # Expired session
88 $params{MESSAGE} = "<span class=\"infored\">Access Denied - Your session has expired. Please log in.</span>";
89 }
90  
91 # Build nonce and HMAC (using server key) fro CSRF protection. (Note - this
92 # only protects the login form.... once logged in, the app must protect itself.
93 # Yet another place where having bidirectional filtering would be useful)
94 # Required for CSRF protection
95  
96 # Note - Pulling session config code out of the main module would allow this
97 # to be much shorter/simpler. Strike 90834895345 against the giant module.
98 # TODO - This should be in a module!!!
99 my $auth_name = ($r->auth_name()) || (die("login.pl(): No auth name defined!\n"));
100 my $dirconfig = $r->dir_config;
101 my $serverkey = '';
102 if (exists($dirconfig->{$auth_name . "SessionServerPass"})) {
103 my $serverpass = $dirconfig->{$auth_name . "SessionServerPass"};
104 ($serverpass =~ s/^\s*([[:print:]]{8,}?)\s*$/$1/s) ||
105 die('error', "login.pl(): Invalid ${auth_name}SessionServerPass (must be use at least 8 printable characters\n");
106 ($serverpass =~ /^(password|serverkey|serverpass|12345678)$/i) &&
107 die("login.pl: ${auth_name}SessionServerPass is $1... That is too lousy\n");
108
109 ($serverkey = HashPass($serverpass)) || die("login.pl: Problem computing server key hash for $auth_name");
110  
111 } elsif (exists($dirconfig->{$auth_name . "SessionServerKey"})) {
112 $serverkey = $dirconfig->{$auth_name . "SessionServerKey"};
113  
114 } else {
115 die("login.pl(): You must configure either ${auth_name}SessionServerPass or ${auth_name}SessionServerKey in your Apache configuration\n");
116 }
117  
118 # Check for valid key format
119 (CheckSidFormat($serverkey)) || die("login.pl(): You must a valid ${auth_name}SessionServerPass or ${auth_name}SessionServerKey configured!");
120  
121 # Get a nonce. Note - since this gets sent back, and it is the same as the alg
122 # used to get the random session key, PRNG weakness could be an issue.
123 $params{NONCE} = CreateSessionAuthKey();
124  
125 # Get HMAC of nonce with server key (this is just like we use the session key
126 # and server key to get the real session ID, though THIS time we are sending it
127 # to the browser.)
128 $params{SIG} = ComputeSessionId($params{NONCE}, $serverkey);
129  
130 # Read in form
131 my $form = '';
132 open(F, "$formsource") or die "FATAL: Could not find/open login page content\n";
133 while (<F>) {
134 $form .= $_;
135 }
136 close(F);
137  
138 # Apply parameters
139 foreach (keys %params) {
140 $form =~ s/__${_}__/$params{$_}/gs;
141 }
142  
143 $r->no_cache(1);
144  
145 $r->content_type("text/html");
146 $r->headers_out->set("Content-length", length($form));
147 $r->headers_out->set("Pragma", "no-cache");
148 # Only for mod_perl 1
149 ($MP eq 1) and $r->send_http_header;
150  
151 $r->print ($form);