Project

General

Profile

Download (4.16 KB) Statistics
| Branch: | Tag: | Revision:
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="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=\"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 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 (!isset($HTTP_SERVER_VARS['AUTH_USER']))
56
			continue;
57

    
58
		/* Check to see if user even exists */
59
		$username = $HTTP_SERVER_VARS['AUTH_USER'];
60
                if(!($line = array_shift(preg_grep("/^$username:.*$/", $authfile))))
61
			continue;
62

    
63
		/* Get crypted password */
64
                preg_match("/^$username:((\\$1\\$[.\d\w_\/]{8}\\$)[.\d\w_\/]{22})$/", $line, $matches);
65
		$pass = $matches[1];
66
		$salt = $matches[2];
67

    
68
		/* Encrypt entered password with salt */
69
		$authpass = crypt($HTTP_SERVER_VARS['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
function htpasswd_backed_basic_auth() {
83
        global $HTTP_SERVER_VARS;
84

    
85
        $authfile = file("/var/run/htpasswd");
86

    
87
	/* sanity check to ensure that /usr/local/www/.htpasswd doesn't exist */
88
	unlink_if_exists("/usr/local/www/.htpasswd");
89

    
90
        /* Prompt three times and give up */
91
        for($attempt = 0; $attempt <= 3; basic_auth_prompt()){
92
                $attempt++;
93
                /* Check for AUTH_USER */
94
                if (!isset($HTTP_SERVER_VARS['AUTH_USER']))
95
                        continue;
96

    
97
                /* Check to see if user even exists */
98
                $username = $HTTP_SERVER_VARS['AUTH_USER'];
99
                if(!($line = array_shift(preg_grep("/^$username:.*$/", $authfile))))
100
                        continue;
101

    
102
                /* Get crypted password */
103
                preg_match("/^$username:((\\$1\\$[.\d\w_\/]{8}\\$)[.\d\w_\/]{22})$/", $line, $matches);
104
                $pass = $matches[1];
105
                $salt = $matches[2];
106

    
107
                /* Encrypt entered password with salt */
108
                $authpass = crypt($HTTP_SERVER_VARS['AUTH_PW'], $salt);
109

    
110
                /* And finally validate password */
111
                if($authpass == $pass)
112
                        return true;
113
                else
114
                        continue;
115
        }
116

    
117
        /* Should only get here if user fails login three times */
118
        return false;
119
}
120

    
121
?>
(1-1/22)