| 1 | # WWW:Auth | ||
| 2 | # | ||
| 3 | # Copyright (c) 2002 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or | ||
| 7 | # modify it under the same terms as Perl itself. | ||
| 8 | |||
| 9 | |||
| 10 | package WWW::Auth; | ||
| 11 | use base 'WWW::Auth::Base'; | ||
| 12 | |||
| 13 | |||
| 14 | use strict; | ||
| 15 | use WWW::Auth::Base; | ||
| 16 | use WWW::Auth::Config; | ||
| 17 | use CGI qw(:standard); | ||
| 18 | use CGI::Cookie; | ||
| 19 | use LWP::UserAgent; | ||
| 20 | use HTTP::Request; | ||
| 21 | use MD5; | ||
| 22 | use vars qw($VERSION); | ||
| 23 | |||
| 24 | $VERSION = '1.01'; | ||
| 25 | |||
| 26 | |||
| 27 | sub _init { | ||
| 28 | my $self = shift; | ||
| 29 | my %params = @_; | ||
| 30 | |||
| 31 | $self->{_cgi} = CGI->new (); | ||
| 32 | |||
| 33 | $self->{_domain} = $params{Domain} | ||
| 34 | || WWW::Auth::Config->doimain; | ||
| 35 | |||
| 36 | $self->{_cgi_header} = $params{CGIHeader} || 1; | ||
| 37 | $self->{_login_param} = $params{LoginParam} | ||
| 38 | || WWW::Auth::Config->login_param || 'Login'; | ||
| 39 | $self->{_logout_param} = $params{LogoutParam} | ||
| 40 | || WWW::Auth::Config->logout_param || 'Logout'; | ||
| 41 | $self->{_uid_param} = $params{UIDParam} | ||
| 42 | || WWW::Auth::Config->uid_param || 'uid'; | ||
| 43 | $self->{_pwd_param} = $params{PwdParam} | ||
| 44 | || WWW::Auth::Config->pwd_param || 'pwd'; | ||
| 45 | |||
| 46 | $self->{_logout_url} = $params{LogoutURL} | ||
| 47 | || WWW::Auth::Config->logour_url | ||
| 48 | || return $self->error ('No Logout URL specified.'); | ||
| 49 | |||
| 50 | $self->{_secure} = $params{Secure} | ||
| 51 | || WWW::Auth::Config->secure || 0; | ||
| 52 | |||
| 53 | $self->{_auth} = $params{Auth} | ||
| 54 | || WWW::Auth::Config->auth (%params) | ||
| 55 | || return $self->error (WWW::Auth::Config->error); | ||
| 56 | |||
| 57 | $self->{_serverkey_src} = $params{ServerKeySrc} | ||
| 58 | || WWW::Auth::Config->serverkey_src | ||
| 59 | || return $self->error ('No Server Key Source specified.'); | ||
| 60 | |||
| 61 | $self->{_template} = $params{Template} | ||
| 62 | || WWW::Auth::Config->template | ||
| 63 | || return $self->error (WWW::Auth::Config->error); | ||
| 64 | $self->{_login_template} = $params{LoginTemplate} | ||
| 65 | || WWW::Auth::Config->login_template | ||
| 66 | || return $self->error ('No Login Template specified.'); | ||
| 67 | $self->{_logout_template} = $params{LogoutTemplate} | ||
| 68 | || WWW::Auth::Config->logout_template | ||
| 69 | || return $self->error ('No Logout Template specified.'); | ||
| 70 | $self->{_secure_template} = $params{SecureTemplate} | ||
| 71 | || WWW::Auth::Config->secure_template | ||
| 72 | || return $self->error ('No Secure Template specified.'); | ||
| 73 | |||
| 74 | $self->{_ticket_name} = $params{TicketName} | ||
| 75 | || WWW::Auth::Config->ticket_name || 'Ticket'; | ||
| 76 | |||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub get_serverkey { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | my $ua = LWP::UserAgent->new (); | ||
| 84 | my $request = HTTP::Request->new (GET => $self->{_serverkey_src}); | ||
| 85 | my $response = $ua->request ($request); | ||
| 86 | |||
| 87 | return $response->content; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub login { | ||
| 91 | my $self = shift; | ||
| 92 | my %params = @_; | ||
| 93 | |||
| 94 | my $login_param = $params{LoginParam} || $self->{_login_param}; | ||
| 95 | my $logout_param = $params{LogoutParam} || $self->{_logout_param}; | ||
| 96 | my $uid_param = $params{UIDParam} || $self->{_uid_param}; | ||
| 97 | my $pwd_param = $params{PwdParam} || $self->{_pwd_param}; | ||
| 98 | my $domain = $params{Domain} || $self->{_domain}; | ||
| 99 | my $logout_url = $params{LogoutURL} || $self->{_logout_url}; | ||
| 100 | my $secure = $params{Secure} || $self->{_secure}; | ||
| 101 | |||
| 102 | my $msg; | ||
| 103 | |||
| 104 | # Redirect to secure page. | ||
| 105 | if ($secure && $ENV{HTTPS} ne 'on') { | ||
| 106 | my $http_host = $ENV{HTTP_HOST}; | ||
| 107 | my $request_uri = $ENV{REQUEST_URI}; | ||
| 108 | print $self->{_cgi}->header ( | ||
| 109 | -refresh => "1; URL=https://$http_host$request_uri" | ||
| 110 | ); | ||
| 111 | $self->{_template}->process ($self->{_secure_template}, | ||
| 112 | {http_host => $http_host, | ||
| 113 | redirect_url => $request_uri}); | ||
| 114 | exit; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Logout. | ||
| 118 | if ($self->{_cgi}->param ($logout_param)) { | ||
| 119 | # Create a new empty ticket. | ||
| 120 | my $ticket = $self->make_ticket ($self->{_ticket_name}, $domain); | ||
| 121 | if ($ticket) { | ||
| 122 | print $self->{_cgi}->header ( | ||
| 123 | -cookie => $ticket, | ||
| 124 | -refresh => "1; URL=$logout_url" | ||
| 125 | ); | ||
| 126 | $self->{_template}->process ($self->{_logout_template}, | ||
| 127 | {http => $ENV{HTTPS} eq 'on' ? 'https' : 'http', | ||
| 128 | http_host => $ENV{HTTP_HOST}, | ||
| 129 | redirect_url => $logout_url}); | ||
| 130 | exit; | ||
| 131 | } | ||
| 132 | |||
| 133 | # Log in for the first time. | ||
| 134 | } elsif ($self->{_cgi}->param ($login_param)) { | ||
| 135 | # Get the user id and password entered. | ||
| 136 | my $uid = $self->{_cgi}->param ($uid_param); | ||
| 137 | my $pwd = $self->{_cgi}->param ($pwd_param); | ||
| 138 | |||
| 139 | # Return an error if username and password not given. | ||
| 140 | if (! $uid || ! $pwd) { | ||
| 141 | $msg = 'Enter your username and password.'; | ||
| 142 | |||
| 143 | # Authenticate. | ||
| 144 | } else { | ||
| 145 | my $result; | ||
| 146 | ($result, $msg) = $self->{_auth}->auth ($uid, $pwd, %params); | ||
| 147 | if ($result) { | ||
| 148 | # Create a new ticket. | ||
| 149 | my $serverkey = $self->get_serverkey (); | ||
| 150 | my $ticket = $self->make_ticket ($self->{_ticket_name}, | ||
| 151 | $domain, $uid, $serverkey); | ||
| 152 | if ($ticket) { | ||
| 153 | # Create uri to redirect to. | ||
| 154 | my $request_uri = $self->{_cgi}->param ('request_uri'); | ||
| 155 | |||
| 156 | # XXX Send post params. | ||
| 157 | # my $params; | ||
| 158 | # foreach my $param ('mode', 'action') { | ||
| 159 | # if (defined $self->{_cgi}->param ($param) && | ||
| 160 | # $request_uri !~ /[?|&]$param=/) { | ||
| 161 | # $params .= defined $params ? '&' : '?'; | ||
| 162 | # $params .= "$param=" . $self->{_cgi}->param ($param); | ||
| 163 | # } | ||
| 164 | # } | ||
| 165 | # $request_uri .= $params if defined $params; | ||
| 166 | |||
| 167 | print $self->{_cgi}->header ( | ||
| 168 | -cookie => $ticket, | ||
| 169 | -refresh => "1; URL=$request_uri" | ||
| 170 | ); | ||
| 171 | $self->{_template}->process ($self->{_login_template}, | ||
| 172 | {http => $ENV{HTTPS} eq 'on' ? 'https' : 'http', | ||
| 173 | http_host => $ENV{HTTP_HOST}, | ||
| 174 | redirect_url => $request_uri}); | ||
| 175 | exit; | ||
| 176 | } else { | ||
| 177 | $msg = 'Could not make ticket'; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } else { | ||
| 182 | # If there is a ticket, verify it. | ||
| 183 | my %ticket = $self->{_cgi}->cookie ($self->{_ticket_name}); | ||
| 184 | |||
| 185 | if (%ticket && ($ticket{uid} !~ /^\s*$/)) { | ||
| 186 | my $serverkey = $self->get_serverkey (); | ||
| 187 | my $result; | ||
| 188 | ($result, $msg) = $self->verify_ticket (\%ticket, $serverkey); | ||
| 189 | if ($result) { | ||
| 190 | # Update time on ticket. | ||
| 191 | my $new_ticket = $self->make_ticket ($self->{_ticket_name}, | ||
| 192 | $domain, $ticket{uid}, | ||
| 193 | $serverkey); | ||
| 194 | if ($new_ticket) { | ||
| 195 | print $self->{_cgi}->header ( | ||
| 196 | -cookie => $new_ticket | ||
| 197 | ); | ||
| 198 | return (1); | ||
| 199 | } else { | ||
| 200 | $msg = 'Could not make ticket'; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | print $self->{_cgi}->header if $self->{_cgiheader}; | ||
| 207 | |||
| 208 | return (0, $msg); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub authenticate { | ||
| 212 | my $self = shift; | ||
| 213 | my ($uid, $pwd, $params) = @_; | ||
| 214 | |||
| 215 | if (! defined $params->{Users} || | ||
| 216 | (defined $params->{Users} && | ||
| 217 | $params->{Users}->{$uid})) { | ||
| 218 | my $success = 0; | ||
| 219 | if ($params->{Auth} eq 'FTP') { | ||
| 220 | use Net::FTP; | ||
| 221 | my $ftp = Net::FTP->new ($params->{FTPHost}); | ||
| 222 | $success = $ftp->login ($uid, $pwd); | ||
| 223 | $ftp->quit (); | ||
| 224 | |||
| 225 | } else { | ||
| 226 | my ($upwd) = $self->{db}->select (Fields => 'pwd', | ||
| 227 | Table => 'users', | ||
| 228 | Where => "uid='$uid'"); | ||
| 229 | if ($upwd =~ /^\s*$/ || | ||
| 230 | $upwd ne crypt ($pwd, $upwd)) { | ||
| 231 | $success = 0; | ||
| 232 | } else { | ||
| 233 | $success = 1; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | if ($success) { | ||
| 238 | return (1); | ||
| 239 | } else { | ||
| 240 | return (0, 'Username/Password incorrect'); | ||
| 241 | } | ||
| 242 | } else { | ||
| 243 | return (0, 'Access denied.'); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | sub make_ticket { | ||
| 248 | my $self = shift; | ||
| 249 | my ($ticket_name, $domain, $uid, $pwd) = @_; | ||
| 250 | |||
| 251 | $ticket_name = 'Ticket' if ! defined $ticket_name; | ||
| 252 | |||
| 253 | if (defined $uid) { | ||
| 254 | my $ip_addr = $ENV{REMOTE_ADDR}; | ||
| 255 | my $expires = 60 * 30; | ||
| 256 | my $time = time; | ||
| 257 | |||
| 258 | my $hash = MD5->hexhash ($pwd . | ||
| 259 | MD5->hexhash (join (':', $pwd, $ip_addr, $time, $expires, | ||
| 260 | $uid))); | ||
| 261 | my %cookie = (-name => $ticket_name, | ||
| 262 | -value => {'ip_addr' => $ip_addr, | ||
| 263 | 'time' => $time, | ||
| 264 | 'uid' => $uid, | ||
| 265 | 'hash' => $hash, | ||
| 266 | 'expires' => $expires}); | ||
| 267 | $cookie{-domain} = $domain if defined $domain; | ||
| 268 | return $self->{_cgi}->cookie (%cookie); | ||
| 269 | } else { | ||
| 270 | my %cookie = (-name => $ticket_name, | ||
| 271 | -value => {uid => ''}); | ||
| 272 | $cookie{-domain} = $domain if defined $domain; | ||
| 273 | return $self->{_cgi}->cookie (%cookie); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | sub verify_ticket { | ||
| 278 | my $self = shift; | ||
| 279 | my ($ticket, $pwd) = @_; | ||
| 280 | |||
| 281 | # Check if all the fields are present. | ||
| 282 | unless ($ticket->{uid} && | ||
| 283 | $ticket->{time} && | ||
| 284 | $ticket->{hash} && | ||
| 285 | $ticket->{expires}) { | ||
| 286 | return (0, 'Malformed ticket'); | ||
| 287 | } | ||
| 288 | # Check if IP address matches. | ||
| 289 | if ($ticket->{ip_addr} ne $ENV{REMOTE_ADDR}) { | ||
| 290 | return (0, 'IP address mismatch'); | ||
| 291 | } | ||
| 292 | # Check if ticket has expires. | ||
| 293 | if (time - $ticket->{time} > $ticket->{expires}) { | ||
| 294 | return (0, 'Session has expired'); | ||
| 295 | } | ||
| 296 | |||
| 297 | my $newhash = MD5->hexhash ($pwd . | ||
| 298 | MD5->hexhash (join (':', $pwd, $ticket->{ip_addr}, | ||
| 299 | $ticket->{time}, $ticket->{expires}, | ||
| 300 | $ticket->{uid}))); | ||
| 301 | if ($newhash ne $ticket->{hash}) { | ||
| 302 | return (0, 'Ticket mismatch'); | ||
| 303 | } | ||
| 304 | |||
| 305 | # Store uid. | ||
| 306 | $self->{uid} = $ticket->{uid}; | ||
| 307 | |||
| 308 | return (1); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub uid { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | return $self->{uid}; | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | 1; |