Project

General

Profile

Download (3.17 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 52c9f9fa Ermal
<?php
3
/*
4 ac24dc24 Renato Botelho
 * ipsec.auth-user.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 c5d81585 Renato Botelho
 * Copyright (c) 2008 Shrew Soft Inc
8 38809d47 Renato Botelho do Couto
 * Copyright (c) 2008-2013 BSD Perimeter
9
 * Copyright (c) 2013-2016 Electric Sheep Fencing
10 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
11 ac24dc24 Renato Botelho
 * All rights reserved.
12
 *
13 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16 ac24dc24 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24 ac24dc24 Renato Botelho
 */
25 09221bc3 Renato Botelho
26 52c9f9fa Ermal
/*
27 4881e5a9 Ermal
 * ipsec calls this script to authenticate a user
28 52c9f9fa Ermal
 * based on a username and password. We lookup these
29
 * in our config.xml file and check the credentials.
30
 */
31
32
require_once("globals.inc");
33
require_once("config.inc");
34
require_once("auth.inc");
35
require_once("interfaces.inc");
36
37
38
/* setup syslog logging */
39 9eb4257f Ermal
openlog("charon", LOG_ODELAY, LOG_AUTH);
40 52c9f9fa Ermal
41 9e74f980 Ermal
if (isset($_GET['username'])) {
42 85d0e959 Ermal
	$authmodes = explode(",", $_GET['authcfg']);
43
	$username = $_GET['username'];
44
	$password = $_GET['password'];
45
	$common_name = $_GET['cn'];
46
} else {
47
	/* read data from environment */
48
	$username = getenv("username");
49
	$password = getenv("password");
50
	$common_name = getenv("common_name");
51 2a3e3057 Ermal
	$authmodes = explode(",", getenv("authcfg"));
52 85d0e959 Ermal
}
53 52c9f9fa Ermal
54 eeceb2ca Augustin-FL
if (!$username) {
55 52c9f9fa Ermal
	syslog(LOG_ERR, "invalid user authentication environment");
56 fe06990e Ermal
	if (isset($_GET['username'])) {
57 85d0e959 Ermal
		echo "FAILED";
58
		closelog();
59
		return;
60
	} else {
61
		closelog();
62 9a92e2ef Renato Botelho
		exit (-1);
63 85d0e959 Ermal
	}
64 52c9f9fa Ermal
}
65
66
$authenticated = false;
67
68
if (($strictusercn === true) && ($common_name != $username)) {
69 882af7b4 jim-p
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.");
70 eadda967 Ermal
	if (isset($_GET['username'])) {
71 85d0e959 Ermal
		echo "FAILED";
72
		closelog();
73
		return;
74
	} else {
75
		closelog();
76 9a92e2ef Renato Botelho
		exit (1);
77 85d0e959 Ermal
	}
78 52c9f9fa Ermal
}
79
80 f15fdef3 Augustin FL
$attributes = array("nas_identifier" => "xauthIPsec");
81 52c9f9fa Ermal
foreach ($authmodes as $authmode) {
82
	$authcfg = auth_get_authserver($authmode);
83 821a4351 Renato Botelho
	if (!$authcfg && $authmode != "Local Database") {
84 52c9f9fa Ermal
		continue;
85 b37a2e8c Phil Davis
	}
86 52c9f9fa Ermal
87
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
88 936fc874 Ermal
	if ($authenticated == true) {
89 821a4351 Renato Botelho
		if ($authmode == "Local Database") {
90 b37a2e8c Phil Davis
			$user = getUserEntry($username);
91 a9157b6b Ermal
			if (!is_array($user) || !userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
92
				$authenticated = false;
93 882af7b4 jim-p
				syslog(LOG_WARNING, "user '{$username}' cannot authenticate through IPsec since the required privileges are missing.");
94 a9157b6b Ermal
				continue;
95
			}
96 936fc874 Ermal
		}
97 52c9f9fa Ermal
		break;
98 936fc874 Ermal
	}
99 52c9f9fa Ermal
}
100
101
if ($authenticated == false) {
102 882af7b4 jim-p
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.");
103 eadda967 Ermal
	if (isset($_GET['username'])) {
104 85d0e959 Ermal
		echo "FAILED";
105
		closelog();
106
		return;
107
	} else {
108
		closelog();
109 9a92e2ef Renato Botelho
		exit (-1);
110 85d0e959 Ermal
	}
111 52c9f9fa Ermal
}
112
113 b37a2e8c Phil Davis
if (file_exists("/etc/inc/ipsec.attributes.php")) {
114
	include_once("/etc/inc/ipsec.attributes.php");
115
}
116
117 882af7b4 jim-p
syslog(LOG_NOTICE, "user '{$username}' authenticated");
118 85d0e959 Ermal
closelog();
119 52c9f9fa Ermal
120 b37a2e8c Phil Davis
if (isset($_GET['username'])) {
121 85d0e959 Ermal
	echo "OK";
122 b37a2e8c Phil Davis
} else {
123 9a92e2ef Renato Botelho
	exit (0);
124 b37a2e8c Phil Davis
}
125 52c9f9fa Ermal
126
?>