1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
Copyright (C) 2005 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="file_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=\"pfSense\"");
|
41
|
header("HTTP/1.0 401 Unauthorized");
|
42
|
echo "You must enter valid credentials to access this resource.";
|
43
|
exit;
|
44
|
}
|
45
|
|
46
|
function file_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 PHP_AUTH_USER */
|
55
|
if (!isset($HTTP_SERVER_VARS['PHP_AUTH_USER']))
|
56
|
continue;
|
57
|
|
58
|
/* Check to see if user even exists */
|
59
|
$username = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
|
60
|
if(!($line = array_shift(preg_grep("/$username:.*$/", $authfile))))
|
61
|
continue;
|
62
|
|
63
|
/* Get crypted password */
|
64
|
preg_match("/$username:((...[0-9A-Za-z_]{8}.)[0-9A-Za-z_]{22})/", $line, $matches);
|
65
|
$pass = $matches[1];
|
66
|
$salt = $matches[2];
|
67
|
|
68
|
/* Encrypt entered password with salt */
|
69
|
$authpass = crypt($HTTP_SERVER_VARS['PHP_AUTH_PW'], $salt);
|
70
|
|
71
|
/* And finally validate password */
|
72
|
if($authpass == $pass)
|
73
|
return true;
|
74
|
else
|
75
|
continue;
|
76
|
}
|
77
|
|
78
|
/* Should only get here if user fails login three times */
|
79
|
return false;
|
80
|
}
|
81
|
|
82
|
?>
|