Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * vpn_l2tp.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 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
##|+PRIV
25
##|*IDENT=page-vpn-vpnl2tp
26
##|*NAME=VPN: L2TP
27
##|*DESCR=Allow access to the 'VPN: L2TP' page.
28
##|*MATCH=vpn_l2tp.php*
29
##|-PRIV
30

    
31
require_once("guiconfig.inc");
32
require_once("vpn.inc");
33

    
34
config_init_path('l2tp/radius');
35

    
36
$pconfig['remoteip'] = config_get_path('l2tp/radius/remoteip');
37
$pconfig['localip'] = config_get_path('l2tp/radius/localip');
38
$pconfig['l2tp_subnet'] = config_get_path('l2tp/radius/l2tp_subnet');
39
$pconfig['mode'] = config_get_path('l2tp/radius/mode');
40
$pconfig['interface'] = config_get_path('l2tp/radius/interface');
41
$pconfig['l2tp_dns1'] = config_get_path('l2tp/radius/dns1');
42
$pconfig['l2tp_dns2'] = config_get_path('l2tp/radius/dns2');
43
$pconfig['mtu'] = config_get_path('l2tp/radius/mtu');
44
$pconfig['radiusenable'] = config_path_enabled('l2tp/radius');
45
$pconfig['radacct_enable'] = config_path_enabled('l2tp/accounting');
46
$pconfig['radiusserver'] = config_get_path('l2tp/radius/radius')['server'];
47
$pconfig['radiussecret'] = config_get_path('l2tp/radius/radius')['secret'];
48
$pconfig['radiusissueips'] = config_path_enabled('l2tp/radiusissueips');
49
$pconfig['n_l2tp_units'] = config_get_path('l2tp/radius/n_l2tp_units');
50
$pconfig['paporchap'] = config_get_path('l2tp/radius/paporchap');
51
$pconfig['secret'] = config_get_path('l2tp/radius/secret');
52

    
53
if ($_POST['save']) {
54

    
55
	unset($input_errors);
56
	$pconfig = $_POST;
57

    
58
	/* input validation */
59
	if ($_POST['mode'] == "server") {
60
		$reqdfields = explode(" ", "localip");
61
		$reqdfieldsn = array(gettext("Server address"));
62
		if (!$_POST['radiusenable'] || !$_POST['radiusissueips']) {
63
			$reqdfields = array_merge($reqdfields, explode(" ", "remoteip"));
64
			$reqdfieldsn = array_merge($reqdfieldsn, array(gettext("Remote start address")));
65
		} elseif ($_POST['radiusenable']) {
66
			$reqdfields = array_merge($reqdfields, explode(" ", "radiusserver radiussecret"));
67
			$reqdfieldsn = array_merge($reqdfieldsn,
68
				array(gettext("RADIUS server address"), gettext("RADIUS shared secret")));
69
		}
70

    
71
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
72

    
73
		if (($_POST['localip'] && !is_ipaddrv4($_POST['localip']))) {
74
			$input_errors[] = gettext("A valid server address must be specified.");
75
		}
76
		if (is_ipaddr_configured($_POST['localip'])) {
77
			$input_errors[] = gettext("'Server address' parameter should NOT be set to any IP address currently in use on this firewall.");
78
		}
79
		if ($_POST['l2tp_subnet'] && !is_ipaddrv4($_POST['remoteip']) &&
80
		    (!$_POST['radiusenable'] || !$_POST['radiusissueips'])) {
81
			$input_errors[] = gettext("A valid remote start address must be specified.");
82
		}
83
		if (($_POST['radiusserver'] && !is_ipaddrv4($_POST['radiusserver']))) {
84
			$input_errors[] = gettext("A valid RADIUS server address must be specified.");
85
		}
86

    
87
		if ($_POST['secret'] != $_POST['secret_confirm']) {
88
			$input_errors[] = gettext("Secret and confirmation must match");
89
		}
90

    
91
		if ($_POST['radiussecret'] != $_POST['radiussecret_confirm']) {
92
			$input_errors[] = gettext("RADIUS secret and confirmation must match");
93
		}
94

    
95
		if (!is_numericint($_POST['n_l2tp_units']) || $_POST['n_l2tp_units'] > 255) {
96
			$input_errors[] = gettext("Number of L2TP users must be between 1 and 255");
97
		}
98

    
99
		if (!$input_errors) {
100
			$_POST['remoteip'] = $pconfig['remoteip'] = gen_subnet($_POST['remoteip'], $_POST['l2tp_subnet']);
101
			if (is_inrange_v4($_POST['localip'], $_POST['remoteip'], ip_after($_POST['remoteip'], $_POST['n_l2tp_units'] - 1))) {
102
				$input_errors[] = gettext("The specified server address lies in the remote subnet.");
103
			}
104
			if ($_POST['localip'] == get_interface_ip("lan")) {
105
				$input_errors[] = gettext("The specified server address is equal to the LAN interface address.");
106
			}
107
		}
108

    
109
		if (!empty($_POST['l2tp_dns1']) && !is_ipaddrv4(trim($_POST['l2tp_dns1']))) {
110
			$input_errors[] = gettext("The field 'Primary L2TP DNS Server' must contain a valid IPv4 address.");
111
		}
112
		if (!empty($_POST['l2tp_dns2']) && !is_ipaddrv4(trim($_POST['l2tp_dns2']))) {
113
			$input_errors[] = gettext("The field 'Secondary L2TP DNS Server' must contain a valid IPv4 address.");
114
		}
115
		if (!empty($_POST['l2tp_dns2']) && empty($_POST['l2tp_dns1'])) {
116
			$input_errors[] = gettext("The Secondary L2TP DNS Server cannot be set when the Primary L2TP DNS Server is empty.");
117
		}
118
		if ($_POST['mtu']) {
119
			if (!is_numericint($_POST['mtu'])) {
120
				$input_errors[] = "MTU must be an integer.";
121
			}
122
			$min_mtu = 576;
123
			$max_mtu = 9000;
124
			if (($_POST['mtu'] < $min_mtu) || ($_POST['mtu'] > $max_mtu)) {
125
				$input_errors[] = sprintf(gettext("The MTU must be between %d and %d bytes."), $min_mtu, $max_mtu);
126
			}
127
		}
128
	}
129

    
130
	if (!$input_errors) {
131
		$l2tpcfg = config_get_path('l2tp');
132
		$l2tpcfg['remoteip'] = $_POST['remoteip'];
133
		$l2tpcfg['localip'] = $_POST['localip'];
134
		$l2tpcfg['l2tp_subnet'] = $_POST['l2tp_subnet'];
135
		$l2tpcfg['mode'] = $_POST['mode'];
136
		$l2tpcfg['interface'] = $_POST['interface'];
137
		$l2tpcfg['n_l2tp_units'] = $_POST['n_l2tp_units'];
138
		$l2tpcfg['radius']['server'] = $_POST['radiusserver'];
139
		if ($_POST['radiussecret'] != DMYPWD) {
140
			$l2tpcfg['radius']['secret'] = $_POST['radiussecret'];
141
		}
142

    
143
		if ($_POST['secret'] != DMYPWD) {
144
			$l2tpcfg['secret'] = $_POST['secret'];
145
		}
146

    
147
		$l2tpcfg['paporchap'] = $_POST['paporchap'];
148

    
149

    
150
		if ($_POST['l2tp_dns1'] == "") {
151
			if (isset($l2tpcfg['dns1'])) {
152
				unset($l2tpcfg['dns1']);
153
			}
154
		} else {
155
			$l2tpcfg['dns1'] = $_POST['l2tp_dns1'];
156
		}
157

    
158
		if ($_POST['l2tp_dns2'] == "") {
159
			if (isset($l2tpcfg['dns2'])) {
160
				unset($l2tpcfg['dns2']);
161
			}
162
		} else {
163
			$l2tpcfg['dns2'] = $_POST['l2tp_dns2'];
164
		}
165

    
166
		if ($_POST['mtu'] == "") {
167
			if (isset($l2tpcfg['mtu'])) {
168
				unset($l2tpcfg['mtu']);
169
			}
170
		} else {
171
			$l2tpcfg['mtu'] = $_POST['mtu'];
172
		}
173

    
174
		if ($_POST['radiusenable'] == "yes") {
175
			$l2tpcfg['radius']['enable'] = true;
176
		} else {
177
			unset($l2tpcfg['radius']['enable']);
178
		}
179

    
180
		if ($_POST['radacct_enable'] == "yes") {
181
			$l2tpcfg['radius']['accounting'] = true;
182
		} else {
183
			unset($l2tpcfg['radius']['accounting']);
184
		}
185

    
186
		if ($_POST['radiusissueips'] == "yes") {
187
			$l2tpcfg['radius']['radiusissueips'] = true;
188
		} else {
189
			unset($l2tpcfg['radius']['radiusissueips']);
190
		}
191

    
192
		config_set_path('l2tp', $l2tpcfg);
193
		write_config(gettext("L2TP VPN configuration changed."));
194

    
195
		$changes_applied = true;
196
		$retval = 0;
197
		$retval |= vpn_l2tp_configure();
198
	}
199
}
200

    
201
$pgtitle = array(gettext("VPN"), gettext("L2TP"), gettext("Configuration"));
202
$pglinks = array("", "@self", "@self");
203
$shortcut_section = "l2tps";
204
include("head.inc");
205

    
206
if ($input_errors) {
207
	print_input_errors($input_errors);
208
}
209

    
210
if ($changes_applied) {
211
	print_apply_result_box($retval);
212
}
213

    
214
$tab_array = array();
215
$tab_array[] = array(gettext("Configuration"), true, "vpn_l2tp.php");
216
$tab_array[] = array(gettext("Users"), false, "vpn_l2tp_users.php");
217
display_top_tabs($tab_array);
218

    
219
$form = new Form();
220

    
221
$section = new Form_Section("Enable L2TP");
222

    
223
$section->addInput(new Form_Checkbox(
224
	'mode',
225
	'Enable',
226
	'Enable L2TP server',
227
	($pconfig['mode'] == "server"),
228
	'server'
229
));
230

    
231
$form->add($section);
232

    
233
$iflist = array();
234
$interfaces = get_configured_interface_with_descr();
235
foreach ($interfaces as $iface => $ifacename) {
236
	$iflist[$iface] = $ifacename;
237
}
238

    
239
$section = new Form_Section("Configuration");
240
$section->addClass('toggle-l2tp-enable');
241

    
242
$section->addInput(new Form_Select(
243
	'interface',
244
	'*Interface',
245
	$pconfig['interface'],
246
	$iflist
247
));
248

    
249
$section->addInput(new Form_Input(
250
	'localip',
251
	'*Server address',
252
	'text',
253
	$pconfig['localip']
254
))->setHelp('Enter the IP address the L2TP server should give to clients for use as their "gateway". %1$s' .
255
			'Typically this is set to an unused IP just outside of the client range.%1$s%1$s' .
256
			'NOTE: This should NOT be set to any IP address currently in use on this firewall.', '<br />');
257

    
258
$section->addInput(new Form_IpAddress(
259
        'remoteip',
260
        '*Remote address range',
261
        $pconfig['remoteip']
262
))->addMask('l2tp_subnet', $pconfig['l2tp_subnet'], 32)->setWidth(5)
263
  ->setHelp('Specify the starting address for the client IP address subnet.');
264

    
265
$section->addInput(new Form_Select(
266
	'n_l2tp_units',
267
	'*Number of L2TP users',
268
	$pconfig['n_l2tp_units'],
269
	array_combine(range(1, 255, 1), range(1, 255, 1))
270
));
271

    
272
$section->addPassword(new Form_Input(
273
	'secret',
274
	'Secret',
275
	'password',
276
	$pconfig['secret']
277
))->setHelp('Specify optional secret shared between peers. Required on some devices/setups.');
278

    
279
$section->addInput(new Form_Select(
280
	'paporchap',
281
	'*Authentication type',
282
	$pconfig['paporchap'],
283
	array(
284
		'chap' => 'CHAP',
285
		'chap-msv2' => 'MS-CHAPv2',
286
		'pap' => 'PAP'
287
		)
288
))->setHelp('Specifies the protocol to use for authentication.');
289

    
290
$section->addInput(new Form_Input(
291
	'l2tp_dns1',
292
	'Primary L2TP DNS server',
293
	'text',
294
	$pconfig['l2tp_dns1']
295
));
296

    
297
$section->addInput(new Form_Input(
298
	'l2tp_dns2',
299
	'Secondary L2TP DNS server',
300
	'text',
301
	$pconfig['l2tp_dns2']
302
));
303

    
304
$section->addInput(new Form_Input(
305
	'mtu',
306
	'VPN MTU',
307
	'number',
308
	$pconfig['mtu']
309
))->setHelp('If this field is blank, the adapter\'s default MTU will be used. ' .
310
			'This is typically 1500 bytes but can vary in some circumstances.');
311

    
312
$form->add($section);
313

    
314
$section = new Form_Section("RADIUS");
315
$section->addClass('toggle-l2tp-enable');
316

    
317
$section->addInput(new Form_Checkbox(
318
	'radiusenable',
319
	'Enable',
320
	'Use a RADIUS server for authentication',
321
	$pconfig['radiusenable']
322
))->setHelp('When set, all users will be authenticated using the RADIUS server specified below. The local user database will not be used.');
323

    
324
$section->addInput(new Form_Checkbox(
325
	'radacct_enable',
326
	'Accounting',
327
	'Enable RADIUS accounting',
328
	$pconfig['radacct_enable']
329
))->setHelp('Sends accounting packets to the RADIUS server.');
330

    
331
$section->addInput(new Form_IpAddress(
332
	'radiusserver',
333
	'*Server',
334
	$pconfig['radiusserver']
335
))->setHelp('Enter the IP address of the RADIUS server.');
336

    
337
$section->addPassword(new Form_Input(
338
	'radiussecret',
339
	'*Secret',
340
	'password',
341
	$pconfig['radiussecret']
342
))->setHelp('Enter the shared secret that will be used to authenticate to the RADIUS server.');
343

    
344
$section->addInput(new Form_Checkbox(
345
	'radiusissueips',
346
	'RADIUS issued IPs',
347
	'Issue IP Addresses via RADIUS server.',
348
	$pconfig['radiusissueips']
349
));
350

    
351
$form->add($section);
352

    
353
print($form);
354
?>
355
<div class="infoblock blockopen">
356
<?php
357
	print_info_box(gettext("Don't forget to add a firewall rule to permit traffic from L2TP clients."), 'info', false);
358
?>
359
</div>
360

    
361
<script type="text/javascript">
362
//<![CDATA[
363
events.push(function() {
364

    
365
	function setL2TP () {
366
		hide = ! $('#mode').prop('checked');
367

    
368
		hideClass('toggle-l2tp-enable', hide);
369
	}
370

    
371
	function setRADIUS () {
372
		hide = ! $('#radiusenable').prop('checked');
373

    
374
		hideCheckbox('radacct_enable', hide);
375
		hideInput('radiusserver', hide);
376
		hideInput('radiussecret', hide);
377
		hideCheckbox('radiusissueips', hide);
378
	}
379

    
380
	// on-click
381
	$('#mode').click(function () {
382
		setL2TP();
383
	});
384

    
385
	$('#radiusenable').click(function () {
386
		setRADIUS();
387
	});
388

    
389
	// on-page-load
390
	setRADIUS();
391
	setL2TP();
392

    
393
});
394
//]]>
395
</script>
396

    
397
<?php include("foot.inc")?>
(225-225/232)