| 1 | |||
| 2 | package HTTP::DAVServer::AuthDigest; | ||
| 3 | |||
| 4 | our $VERSION=0.1; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Digest::MD5 qw(); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | HTTP::DAVServer::AuthDigest - Allows for customized password lookups when using | ||
| 14 | the Digest authorization mechanism. | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This module is called as part of the DAV handler when it is a non-public server that | ||
| 19 | does not have a REMOTE_USER set already. You can pass in a subreference which will be | ||
| 20 | called to lookup the user's password. | ||
| 21 | |||
| 22 | For testing this is simply all users are valid and their password is their userid. | ||
| 23 | |||
| 24 | In order for this code to work it must get the "Authorization" header passed in via CGI. | ||
| 25 | To do this you need to compile Apache with the ominous define "SECURITY_HOLE_PASS_AUTHORIZATION" | ||
| 26 | |||
| 27 | CFLAGS="-DSECURITY_HOLE_PASS_AUTHORIZATION" ./configure | ||
| 28 | |||
| 29 | Like people haven't sniffed your basic authorization header before it got to the server already. | ||
| 30 | At least with Digest this header is a bit less useful for the hostial sniffer. | ||
| 31 | |||
| 32 | This code is not done and is only a sketch. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub headerParts { | ||
| 37 | |||
| 38 | my $header=$_[0]; | ||
| 39 | $header =~ s/^Digest /", /; | ||
| 40 | $header =~ s/"\s*$//; | ||
| 41 | |||
| 42 | my @parts=split /", ([a-z]+)="/, $header; | ||
| 43 | shift @parts; | ||
| 44 | my %parts=@parts; | ||
| 45 | return \%parts; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | # Code inspired from LWP::Authen::Digest | ||
| 50 | # - to conform to RFC 2617 | ||
| 51 | # | ||
| 52 | # Send in | ||
| 53 | # password callback (fed username) (default to username for passwd) | ||
| 54 | # parts hashref (computed for CGI) | ||
| 55 | # request method (computed for CGI) | ||
| 56 | |||
| 57 | sub authenticate { | ||
| 58 | |||
| 59 | my ($passwd, $parts, $method) = @_; | ||
| 60 | |||
| 61 | $passwd ||= sub { return $_[0] }; | ||
| 62 | |||
| 63 | if ($ENV{'GATEWAY_INTERFACE'}=~/^CGI/) { | ||
| 64 | $method ||= $ENV{'REQUEST_METHOD'}; | ||
| 65 | $parts ||= headerParts( $ENV{'HTTP_AUTHORIZATION'} ); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $md5=Digest::MD5->new; | ||
| 69 | my @digest=(); | ||
| 70 | |||
| 71 | $md5->add( join(":", $parts->{'username'}, $parts->{'realm'}, $passwd->( $parts->{'username'} ) )); | ||
| 72 | push @digest, $md5->hexdigest; | ||
| 73 | $md5->reset; | ||
| 74 | |||
| 75 | push @digest, $parts->{'nonce'}; | ||
| 76 | if ( $parts->{'qop'} ) { | ||
| 77 | push @digest, $parts->{'nc'}, $parts->{'cnonce'}, $parts->{'qop'}; | ||
| 78 | } | ||
| 79 | |||
| 80 | $md5->add(join( ":", $method, $parts->{'uri'} ) ); | ||
| 81 | push @digest, $md5->hexdigest; | ||
| 82 | $md5->reset; | ||
| 83 | |||
| 84 | $md5->add( join ":", @digest); | ||
| 85 | |||
| 86 | $ENV{'REMOTE_USER'} = $parts->{'username'} and return 1 if ( $md5->hexdigest eq $parts->{'response'} ); | ||
| 87 | return 0; | ||
| 88 | |||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | # Usage | ||
| 94 | # $authenticated=authenticate( \&passwordLookup, [ $parts, $method] ) | ||
| 95 | # - sets REMOTE_USER environment variable | ||
| 96 | # | ||
| 97 | # passwordLookup is a callback - supplied the rqeuested userid - returns cleartext password | ||
| 98 | # | ||
| 99 | |||
| 100 | =head1 SUPPORT | ||
| 101 | |||
| 102 | For technical support please email to jlawrenc@cpan.org ... | ||
| 103 | for faster service please include "HTTP::DAVServer" and "help" in your subject line. | ||
| 104 | |||
| 105 | =head1 AUTHOR | ||
| 106 | |||
| 107 | Jay J. Lawrence - jlawrenc@cpan.org | ||
| 108 | Infonium Inc., Canada | ||
| 109 | http://www.infonium.ca/ | ||
| 110 | |||
| 111 | =head1 COPYRIGHT | ||
| 112 | |||
| 113 | Copyright (c) 2003 Jay J. Lawrence, Infonium Inc. All rights reserved. | ||
| 114 | This program is free software; you can redistribute | ||
| 115 | it and/or modify it under the same terms as Perl itself. | ||
| 116 | |||
| 117 | The full text of the license can be found in the | ||
| 118 | LICENSE file included with this module. | ||
| 119 | |||
| 120 | =head1 ACKNOWLEDGEMENTS | ||
| 121 | |||
| 122 | Thank you to the authors of my prequisite modules. With out your help this code | ||
| 123 | would be much more difficult to write! | ||
| 124 | |||
| 125 | XML::Simple - Grant McLean | ||
| 126 | XML::SAX - Matt Sergeant | ||
| 127 | DateTime - Dave Rolsky | ||
| 128 | |||
| 129 | Also the authors of litmus, a very helpful tool indeed! | ||
| 130 | |||
| 131 | =head1 SEE ALSO | ||
| 132 | |||
| 133 | HTTP::DAV, HTTP::Webdav, http://www.webdav.org/, RFC 2518 | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | 1; | ||
| 138 |