Project

General

Profile

Download (3.62 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * ipsec.auth-user.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2008 Shrew Soft Inc
8
 * Copyright (c) 2008-2016 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
/*
25
 * ipsec calls this script to authenticate a user
26
 * based on a username and password. We lookup these
27
 * in our config.xml file and check the credentials.
28
 */
29

    
30
require_once("globals.inc");
31
require_once("config.inc");
32
require_once("radius.inc");
33
require_once("auth.inc");
34
require_once("interfaces.inc");
35

    
36
/**
37
 * Get the NAS-Identifier
38
 *
39
 * We will use our local hostname to make up the nas_id
40
 */
41
if (!function_exists("getNasID")) {
42
function getNasID() {
43
	global $g;
44

    
45
	$nasId = gethostname();
46
	if (empty($nasId)) {
47
		$nasId = $g['product_name'];
48
	}
49
	return $nasId;
50
}
51
}
52

    
53
/**
54
 * Get the NAS-IP-Address based on the current wan address
55
 *
56
 * Use functions in interfaces.inc to find this out
57
 *
58
 */
59
if (!function_exists("getNasIP")) {
60
function getNasIP() {
61
	$nasIp = get_interface_ip();
62
	if (!$nasIp) {
63
		$nasIp = "0.0.0.0";
64
	}
65
	return $nasIp;
66
}
67
}
68
/* setup syslog logging */
69
openlog("charon", LOG_ODELAY, LOG_AUTH);
70

    
71
if (isset($_GET['username'])) {
72
	$authmodes = explode(",", $_GET['authcfg']);
73
	$username = $_GET['username'];
74
	$password = $_GET['password'];
75
	$common_name = $_GET['cn'];
76
} else {
77
	/* read data from environment */
78
	$username = getenv("username");
79
	$password = getenv("password");
80
	$common_name = getenv("common_name");
81
	$authmodes = explode(",", getenv("authcfg"));
82
}
83

    
84
if (!$username || !$password) {
85
	syslog(LOG_ERR, "invalid user authentication environment");
86
	if (isset($_GET['username'])) {
87
		echo "FAILED";
88
		closelog();
89
		return;
90
	} else {
91
		closelog();
92
		exit (-1);
93
	}
94
}
95

    
96
$authenticated = false;
97

    
98
if (($strictusercn === true) && ($common_name != $username)) {
99
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
100
	if (isset($_GET['username'])) {
101
		echo "FAILED";
102
		closelog();
103
		return;
104
	} else {
105
		closelog();
106
		exit (1);
107
	}
108
}
109

    
110
$attributes = array();
111
foreach ($authmodes as $authmode) {
112
	$authcfg = auth_get_authserver($authmode);
113
	if (!$authcfg && $authmode != "Local Database") {
114
		continue;
115
	}
116

    
117
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
118
	if ($authenticated == true) {
119
		if ($authmode == "Local Database") {
120
			$user = getUserEntry($username);
121
			if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
122
				$authenticated = false;
123
				syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.\n");
124
				continue;
125
			}
126
		}
127
		break;
128
	}
129
}
130

    
131
if ($authenticated == false) {
132
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
133
	if (isset($_GET['username'])) {
134
		echo "FAILED";
135
		closelog();
136
		return;
137
	} else {
138
		closelog();
139
		exit (-1);
140
	}
141
}
142

    
143
if (file_exists("/etc/inc/ipsec.attributes.php")) {
144
	include_once("/etc/inc/ipsec.attributes.php");
145
}
146

    
147
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
148
closelog();
149

    
150
if (isset($_GET['username'])) {
151
	echo "OK";
152
} else {
153
	exit (0);
154
}
155

    
156
?>
(22-22/54)