Debian Codegraph @

src/.svn/text-base/auth.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 ########################################################################
29
30 =item B<auth_brute_force>
31
32 Params: $auth_method, \%req, $user, \@passwords [, $domain, $fail_code ]
33
34 Return: $first_valid_password, undef if error/none found
35
36 Perform a HTTP authentication brute force against a server (host and URI
37 defined in %req). It will try every password in the password array for
38 the given user. The first password (in conjunction with the given user)
39 that doesn't return HTTP 401 is returned (and the brute force is stopped
40 at that point). You should retry the request with the given password and
41 double-check that you got a useful HTTP return code that indicates
42 successful authentication (200, 302), and not something a bit more
43 abnormal (407, 500, etc). $domain is optional, and is only used for NTLM
44 auth.
45
46 Note: set up any proxy settings and proxy auth in %req before calling
47 this function.
48
49 You can brute-force proxy authentication by setting up the target proxy
50 as proxy_host and proxy_port in %req, using an arbitrary host and uri
51 (preferably one that is reachable upon successful proxy authorization),
52 and setting the $fail_code to 407. The $auth_method passed to this
53 function should be a proxy-based one ('proxy-basic', 'proxy-ntlm', etc).
54
55 if your server returns something other than 401 upon auth failure, then
56 set $fail_code to whatever is returned (and it needs to be something
57 *different* than what is received on auth success, or this function
58 won't be able to tell the difference).
59
60 =cut
61
62 sub auth_brute_force {
63 my ( $auth_method, $hrin, $user, $pwordref, $dom, $fail_code ) = @_;
64 my ( $P, %hout );
65 $fail_code ||= 401;
66
67 return undef if ( !defined $auth_method || length($auth_method) == 0 );
68 return undef if ( !defined $user || length($user) == 0 );
69 return undef if ( !( defined $hrin && ref($hrin) ) );
70 return undef if ( !( defined $pwordref && ref($pwordref) ) );
71
72 map {
73 ( $P = $_ ) =~ tr/\r\n//d;
74 auth_set( $auth_method, $hrin, $user, $P, $dom );
75 return undef if ( http_do_request( $hrin, \%hout ) );
76 return $P if ( $hout{whisker}->{code} != $fail_code );
77 } @$pwordref;
78
79 return undef;
80 }
81
82 ########################################################################
83
84 =item B<auth_unset>
85
86 Params: \%req
87
88 Return: nothing (modifies %req)
89
90 Modifes %req to disable all authentication (regular and proxy).
91
92 Note: it only removes the values set by auth_set(). Manually-defined
93 [Proxy-]Authorization headers will also be deleted (but you shouldn't
94 be using the auth_* functions if you're manually handling your own auth...)
95
96 =cut
97
98 sub auth_unset {
99 my $href = shift;
100 return if ( !defined $href || !ref($href) );
101 delete $$href{Authorization};
102 delete $$href{'Proxy-Authorization'};
103 delete $$href{whisker}->{auth_callback};
104 delete $$href{whisker}->{auth_proxy_callback};
105 delete $$href{whisker}->{auth_data};
106 delete $$href{whisker}->{auth_proxy_data};
107 }
108
109 ########################################################################
110
111 =item B<auth_set>
112
113 Params: $auth_method, \%req, $user, $password [, $domain]
114
115 Return: nothing (modifies %req)
116
117 Modifes %req to use the indicated authentication info.
118
119 Auth_method can be: 'basic', 'proxy-basic', 'ntlm', 'proxy-ntlm'.
120
121 Note: this function may not necessarily set any headers after being called.
122 Also, proxy-ntlm with SSL is not currently supported.
123
124 =cut
125
126 sub auth_set {
127 my ( $method, $href, $user, $pass, $domain ) = ( lc(shift), @_ );
128
129 return if ( !( defined $href && ref($href) ) );
130 return if ( !defined $user || !defined $pass );
131
132 if ( $method eq 'basic' ) {
133 $$href{'Authorization'} =
134 'Basic ' . encode_base64( $user . ':' . $pass, '' );
135 }
136
137 if ( $method eq 'proxy-basic' ) {
138 $$href{'Proxy-Authorization'} =
139 'Basic ' . encode_base64( $user . ':' . $pass, '' );
140 }
141
142 if ( $method eq 'ntlm' ) {
143 http_close($href);
144 $$href{whisker}->{auth_data} = ntlm_new( $user, $pass, $domain );
145 $$href{whisker}->{auth_callback} = \&_ntlm_auth_callback;
146 }
147
148 if ( $method eq 'proxy-ntlm' ) {
149 utils_croak('',"auth_set: proxy-ntlm auth w/ SSL not currently supported")
150 if ( $href->{whisker}->{ssl} > 0 );
151 http_close($href);
152 $$href{whisker}->{auth_proxy_data} = ntlm_new( $user, $pass, $domain );
153 $$href{whisker}->{auth_proxy_callback} = \&_ntlm_auth_proxy_callback;
154 }
155
156 }
157