Project

General

Profile

Download (5.5 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
<?php
3
/* $Id$ */
4
/*
5
    openvpn.auth-user.php
6

    
7
    Copyright (C) 2008 Shrew Soft Inc
8
    Copyright (C) 2010 Ermal Lu?i
9
    All rights reserved.
10

    
11
    Redistribution and use in source and binary forms, with or without
12
    modification, are permitted provided that the following conditions are met:
13

    
14
    1. Redistributions of source code must retain the above copyright notice,
15
       this list of conditions and the following disclaimer.
16

    
17
    2. Redistributions in binary form must reproduce the above copyright
18
       notice, this list of conditions and the following disclaimer in the
19
       documentation and/or other materials provided with the distribution.
20

    
21
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
    POSSIBILITY OF SUCH DAMAGE.
31

    
32
	DISABLE_PHP_LINT_CHECKING
33
*/
34
/*
35
	pfSense_BUILDER_BINARIES:	
36
	pfSense_MODULE:	openvpn
37
*/
38
/*
39
 * OpenVPN calls this script to authenticate a user
40
 * based on a username and password. We lookup these
41
 * in our config.xml file and check the credentials.
42
 */
43

    
44
require_once("globals.inc");
45
require_once("config.inc");
46
require_once("radius.inc");
47
require_once("auth.inc");
48
require_once("interfaces.inc");
49

    
50
/**
51
 * Get the NAS-Identifier
52
 *
53
 * We will use our local hostname to make up the nas_id
54
 */
55
if (!function_exists("getNasID")) {
56
function getNasID()
57
{
58
    global $g;
59

    
60
    $nasId = "";
61
    exec("/bin/hostname", $nasId);
62
    if(!$nasId[0])
63
        $nasId[0] = "{$g['product_name']}";
64
    return $nasId[0];
65
}
66
}
67

    
68
/**
69
 * Get the NAS-IP-Address based on the current wan address
70
 *
71
 * Use functions in interfaces.inc to find this out
72
 *
73
 */
74
if (!function_exists("getNasIP")) {
75
function getNasIP()
76
{
77
    $nasIp = get_interface_ip();
78
    if(!$nasIp)
79
        $nasIp = "0.0.0.0";
80
    return $nasIp;
81
}
82
}
83
/* setup syslog logging */
84
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
85

    
86
if (isset($_GET)) {
87
	$authmodes = explode(",", $_GET['authcfg']);
88
	$username = $_GET['username'];
89
	$password = urldecode($_GET['password']);
90
	$common_name = $_GET['cn'];
91
	$modeid = $_GET['modeid'];
92
	$strictusercn = $_GET['strictcn'] == "false" ? false : true;
93
} else {
94
	/* read data from environment */
95
	$username = getenv("username");
96
	$password = getenv("password");
97
	$common_name = getenv("common_name");
98
}
99

    
100
if (!$username || !$password) {
101
	syslog(LOG_ERR, "invalid user authentication environment");
102
	if (isset($_GET)) {
103
		echo "FAILED";
104
		closelog();
105
		return;
106
	} else {
107
		closelog();
108
		exit(-1);
109
	}
110
}
111

    
112
/* Replaced by a sed with propper variables used below(ldap parameters). */
113
//<template>
114

    
115
if (file_exists("{$g['varetc_path']}/openvpn/{$modeid}.ca")) {
116
	putenv("LDAPTLS_CACERT={$g['varetc_path']}/openvpn/{$modeid}.ca");
117
	putenv("LDAPTLS_REQCERT=never");
118
}
119

    
120
$authenticated = false;
121

    
122
if (($strictusercn === true) && ($common_name != $username)) {
123
	syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
124
	if (isset($_GET)) {
125
		echo "FAILED";
126
		closelog();
127
		return;
128
	} else {
129
		closelog();
130
		exit(1);
131
	}
132
}
133

    
134
if (!is_array($authmodes)) {
135
	syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
136
	if (isset($_GET)) {
137
		echo "FAILED";
138
		closelog();
139
		return;
140
	} else {
141
		closelog();
142
		exit(1);
143
	}
144
}
145

    
146
$attributes = array();
147
foreach ($authmodes as $authmode) {
148
	$authcfg = auth_get_authserver($authmode);
149
	if (!$authcfg && $authmode != "local")
150
		continue;
151

    
152
	$authenticated = authenticate_user($username, $password, $authcfg, $attributes);
153
	if ($authenticated == true)
154
		break;
155
}
156

    
157
if ($authenticated == false) {
158
	syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
159
	if (isset($_GET)) {
160
		echo "FAILED";
161
		closelog();
162
		return;
163
	} else {
164
		closelog();
165
		exit(-1);
166
	}
167
}
168

    
169
if (file_exists("/etc/inc/openvpn.attributes.php"))
170
        include_once("/etc/inc/openvpn.attributes.php");
171
        
172
$content = "";
173
if (is_array($attributes['dns-servers'])) {
174
        foreach ($attributes['dns-servers'] as $dnssrv) {
175
                if (is_ipaddr($dnssrv))
176
                        $content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
177
        }
178
}
179
if (is_array($attributes['routes'])) {
180
        foreach ($attributes['routes'] as $route)
181
		$content .= "push \"route {$route} vpn_gateway\"\n";
182
}
183

    
184
if (isset($attributes['framed_ip'])) {
185
/* XXX: only use when TAP windows driver >= 8.2.x */
186
/*      if (isset($attributes['framed_mask'])) {
187
                $content .= "topology subnet\n";
188
                $content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
189
        } else {
190
*/
191
                $content .= "topology net30\n";
192
                $content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) + 1));
193
//      }
194
}
195
    
196
if (!empty($content))
197
        @file_put_contents("{$g['tmp_path']}/{$username}", $content);
198

    
199
syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
200
closelog();
201

    
202
if (isset($_GET))
203
	echo "OK";
204
else
205
	exit(0);
206

    
207
?>
(36-36/66)