1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
Copyright (C) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
|
5
|
All rights reserved.
|
6
|
|
7
|
Redistribution and use in source and binary forms, with or without
|
8
|
modification, are permitted provided that the following conditions are met:
|
9
|
|
10
|
1. Redistributions of source code must retain the above copyright notice,
|
11
|
this list of conditions and the following disclaimer.
|
12
|
|
13
|
2. Redistributions in binary form must reproduce the above copyright
|
14
|
notice, this list of conditions and the following disclaimer in the
|
15
|
documentation and/or other materials provided with the distribution.
|
16
|
|
17
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
18
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
19
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
20
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
POSSIBILITY OF SUCH DAMAGE.
|
27
|
*/
|
28
|
|
29
|
require_once("config.inc");
|
30
|
require_once("globals.inc");
|
31
|
|
32
|
/* We only support file backed HTTP Basic auth right now */
|
33
|
$auth_method="htpasswd_backed_basic_auth";
|
34
|
|
35
|
/* Authenticate user - exit if failed (we should have a callback for this maybe) */
|
36
|
if (!$auth_method())
|
37
|
exit;
|
38
|
|
39
|
function basic_auth_prompt(){
|
40
|
header("WWW-Authenticate: Basic realm=\".\"");
|
41
|
header("HTTP/1.0 401 Unauthorized");
|
42
|
echo "You must enter valid credentials to access this resource.";
|
43
|
exit;
|
44
|
}
|
45
|
|
46
|
function passwd_backed_basic_auth() {
|
47
|
global $HTTP_SERVER_VARS;
|
48
|
|
49
|
$authfile = file("/etc/master.passwd");
|
50
|
|
51
|
/* Prompt three times and give up */
|
52
|
for($attempt = 0; $attempt <= 3; basic_auth_prompt()){
|
53
|
$attempt++;
|
54
|
/* Check for AUTH_USER */
|
55
|
if ($HTTP_SERVER_VARS['PHP_AUTH_USER'] <> "") {
|
56
|
$HTTP_SERVER_VARS['AUTH_USER'] = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
|
57
|
$HTTP_SERVER_VARS['AUTH_PW'] = $HTTP_SERVER_VARS['PHP_AUTH_PW'];
|
58
|
}
|
59
|
if (!isset($HTTP_SERVER_VARS['AUTH_USER']))
|
60
|
continue;
|
61
|
|
62
|
/* Check to see if user even exists */
|
63
|
$username = $HTTP_SERVER_VARS['AUTH_USER'];
|
64
|
if(!($line = array_shift(preg_grep("/^$username:.*$/", $authfile))))
|
65
|
continue;
|
66
|
|
67
|
/* Get crypted password */
|
68
|
$matches = "";
|
69
|
preg_match("/^$username:((\\$1\\$[.\d\w_\/]{8}\\$)[.\d\w_\/]{22})$/", $line, $matches);
|
70
|
$pass = $matches[1];
|
71
|
$salt = $matches[2];
|
72
|
|
73
|
/* Encrypt entered password with salt */
|
74
|
$authpass = crypt($HTTP_SERVER_VARS['AUTH_PW'], $salt);
|
75
|
|
76
|
/* And finally validate password */
|
77
|
if($authpass == $pass)
|
78
|
return true;
|
79
|
else
|
80
|
continue;
|
81
|
}
|
82
|
|
83
|
/* Should only get here if user fails login three times */
|
84
|
return false;
|
85
|
}
|
86
|
|
87
|
function htpasswd_backed_basic_auth() {
|
88
|
global $HTTP_SERVER_VARS;
|
89
|
|
90
|
$authfile = file("/var/run/htpasswd");
|
91
|
|
92
|
/* sanity check to ensure that /usr/local/www/.htpasswd doesn't exist */
|
93
|
unlink_if_exists("/usr/local/www/.htpasswd");
|
94
|
|
95
|
/* Prompt three times and give up */
|
96
|
for($attempt = 0; $attempt <= 3; basic_auth_prompt()){
|
97
|
$attempt++;
|
98
|
|
99
|
/* Check for AUTH_USER */
|
100
|
if ($HTTP_SERVER_VARS['PHP_AUTH_USER'] <> "") {
|
101
|
$HTTP_SERVER_VARS['AUTH_USER'] = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
|
102
|
$HTTP_SERVER_VARS['AUTH_PW'] = $HTTP_SERVER_VARS['PHP_AUTH_PW'];
|
103
|
}
|
104
|
if (!isset($HTTP_SERVER_VARS['AUTH_USER']))
|
105
|
continue;
|
106
|
|
107
|
/* Check to see if user even exists */
|
108
|
$username = $HTTP_SERVER_VARS['AUTH_USER'];
|
109
|
if(!($line = array_shift(preg_grep("/^$username:.*$/", $authfile))))
|
110
|
continue;
|
111
|
|
112
|
/* Get crypted password */
|
113
|
$matches = "";
|
114
|
preg_match("/^$username:((\\$1\\$[.\d\w_\/]{8}\\$)[.\d\w_\/]{22})$/", $line, $matches);
|
115
|
$pass = $matches[1];
|
116
|
$salt = $matches[2];
|
117
|
|
118
|
/* Encrypt entered password with salt */
|
119
|
$authpass = crypt($HTTP_SERVER_VARS['AUTH_PW'], $salt);
|
120
|
|
121
|
/* And finally validate password */
|
122
|
if($authpass == $pass)
|
123
|
return true;
|
124
|
else
|
125
|
continue;
|
126
|
}
|
127
|
|
128
|
/* Should only get here if user fails login three times */
|
129
|
return false;
|
130
|
}
|
131
|
|
132
|
?>
|