Project

General

Profile

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

    
25
##|+PRIV
26
##|*IDENT=page-system-authservers
27
##|*NAME=System: Authentication Servers
28
##|*DESCR=Allow access to the 'System: Authentication Servers' page.
29
##|*WARN=standard-warning-root
30
##|*MATCH=system_authservers.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34
require_once("auth.inc");
35
require_once("pfsense-utils.inc");
36

    
37
// Have we been called to populate the "Select a container" modal?
38
if ($_REQUEST['ajax']) {
39

    
40
	$ous = array();
41
	$authcfg = array();
42

    
43
	$authcfg['ldap_port'] = $_REQUEST['port'];
44
	$authcfg['ldap_basedn'] = $_REQUEST['basedn'];
45
	$authcfg['host'] = $_REQUEST['host'];
46
	$authcfg['ldap_scope'] = $_REQUEST['scope'];
47
	$authcfg['ldap_binddn'] = $_REQUEST['binddn'];
48
	$authcfg['ldap_bindpw'] = $_REQUEST['bindpw'];
49
	$authcfg['ldap_urltype'] = $_REQUEST['urltype'];
50
	$authcfg['ldap_protver'] = $_REQUEST['proto'];
51
	$authcfg['ldap_authcn'] = explode(";", $_REQUEST['authcn']);
52
	$authcfg['ldap_caref'] = $_REQUEST['cert'];
53

    
54
	$ous = ldap_get_user_ous(true, $authcfg);
55

    
56
	if (empty($ous)) {
57
		print('<span class="text-danger">Could not connect to the LDAP server. Please check the LDAP configuration.</span>');
58
	} else {
59
		$modal = new Modal("Select LDAP containers for authentication", "containers", true);
60
		$group = new Form_MultiCheckboxGroup('Containers');
61

    
62
		if (is_array($ous)) {
63
			$idx = 0;
64

    
65
			foreach ($ous as $ou) {
66
				$group->add(new Form_MultiCheckbox(
67
					'ou' . $idx,
68
					'',
69
					$ou,
70
					in_array($ou, $authcfg['ldap_authcn']),
71
					$ou
72
				));
73

    
74
				$idx++;
75
			}
76
		}
77

    
78
		$modal->add($group);
79

    
80
		// Create a "Save button"
81

    
82
		$btnsv = new Form_Button(
83
			'svcontbtn',
84
			'Save',
85
			null,
86
			'fa-save'
87
		);
88

    
89
		$btnsv->removeClass("btn-default)")->addClass("btn-primary");
90

    
91
		$modal->addInput(new Form_StaticText(
92
			'',
93
			$btnsv
94
		));
95

    
96
		print($modal);
97
	}
98

    
99
	exit;
100
}
101

    
102
$id = $_REQUEST['id'];
103

    
104
if (!is_array($config['system']['authserver'])) {
105
	$config['system']['authserver'] = array();
106
}
107

    
108
$a_server = array_values(auth_get_authserver_list());
109

    
110
init_config_arr(array('ca'));
111
$a_ca = &$config['ca'];
112

    
113
$act = $_REQUEST['act'];
114

    
115
if ($_POST['act'] == "del") {
116

    
117
	if (!$a_server[$_POST['id']]) {
118
		pfSenseHeader("system_authservers.php");
119
		exit;
120
	}
121

    
122
	/* Remove server from main list. */
123
	$serverdeleted = $a_server[$_POST['id']]['name'];
124
	foreach ($config['system']['authserver'] as $k => $as) {
125
		if ($config['system']['authserver'][$k]['name'] == $serverdeleted) {
126
			unset($config['system']['authserver'][$k]);
127
		}
128
	}
129

    
130
	/* Remove server from temp list used later on this page. */
131
	unset($a_server[$_POST['id']]);
132
	$a_server = array_values($a_server);
133

    
134
	$savemsg = sprintf(gettext("Authentication Server %s deleted."), htmlspecialchars($serverdeleted));
135
	write_config($savemsg);
136
}
137

    
138
if ($act == "edit") {
139
	if (isset($id) && $a_server[$id]) {
140

    
141
		$pconfig['type'] = $a_server[$id]['type'];
142
		$pconfig['name'] = $a_server[$id]['name'];
143

    
144
		if ($pconfig['type'] == "ldap") {
145
			$pconfig['ldap_caref'] = $a_server[$id]['ldap_caref'];
146
			$pconfig['ldap_host'] = $a_server[$id]['host'];
147
			$pconfig['ldap_port'] = $a_server[$id]['ldap_port'];
148
			$pconfig['ldap_timeout'] = $a_server[$id]['ldap_timeout'];
149
			$pconfig['ldap_urltype'] = $a_server[$id]['ldap_urltype'];
150
			$pconfig['ldap_protver'] = $a_server[$id]['ldap_protver'];
151
			$pconfig['ldap_scope'] = $a_server[$id]['ldap_scope'];
152
			$pconfig['ldap_basedn'] = $a_server[$id]['ldap_basedn'];
153
			$pconfig['ldap_authcn'] = $a_server[$id]['ldap_authcn'];
154
			$pconfig['ldap_extended_enabled'] = $a_server[$id]['ldap_extended_enabled'];
155
			$pconfig['ldap_extended_query'] = $a_server[$id]['ldap_extended_query'];
156
			$pconfig['ldap_binddn'] = $a_server[$id]['ldap_binddn'];
157
			$pconfig['ldap_bindpw'] = $a_server[$id]['ldap_bindpw'];
158
			$pconfig['ldap_attr_user'] = $a_server[$id]['ldap_attr_user'];
159
			$pconfig['ldap_attr_group'] = $a_server[$id]['ldap_attr_group'];
160
			$pconfig['ldap_attr_member'] = $a_server[$id]['ldap_attr_member'];
161
			$pconfig['ldap_attr_groupobj'] = $a_server[$id]['ldap_attr_groupobj'];
162
			$pconfig['ldap_utf8'] = isset($a_server[$id]['ldap_utf8']);
163
			$pconfig['ldap_nostrip_at'] = isset($a_server[$id]['ldap_nostrip_at']);
164
			$pconfig['ldap_allow_unauthenticated'] = isset($a_server[$id]['ldap_allow_unauthenticated']);
165
			$pconfig['ldap_rfc2307'] = isset($a_server[$id]['ldap_rfc2307']);
166

    
167
			if (!$pconfig['ldap_binddn'] || !$pconfig['ldap_bindpw']) {
168
				$pconfig['ldap_anon'] = true;
169
			}
170
		}
171

    
172
		if ($pconfig['type'] == "radius") {
173
			$pconfig['radius_protocol'] = $a_server[$id]['radius_protocol'];
174
			$pconfig['radius_host'] = $a_server[$id]['host'];
175
			$pconfig['radius_nasip_attribute'] = $a_server[$id]['radius_nasip_attribute'];
176
			$pconfig['radius_auth_port'] = $a_server[$id]['radius_auth_port'];
177
			$pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'];
178
			$pconfig['radius_secret'] = $a_server[$id]['radius_secret'];
179
			$pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'];
180

    
181
			if ($pconfig['radius_auth_port'] &&
182
				$pconfig['radius_acct_port']) {
183
				$pconfig['radius_srvcs'] = "both";
184
			}
185

    
186
			if ($pconfig['radius_auth_port'] &&
187
				!$pconfig['radius_acct_port']) {
188
				$pconfig['radius_srvcs'] = "auth";
189
				$pconfig['radius_acct_port'] = 1813;
190
			}
191

    
192
			if (!$pconfig['radius_auth_port'] &&
193
				$pconfig['radius_acct_port']) {
194
				$pconfig['radius_srvcs'] = "acct";
195
				$pconfig['radius_auth_port'] = 1812;
196
			}
197

    
198
		}
199
	}
200
}
201

    
202
if ($act == "new") {
203
	$pconfig['ldap_protver'] = 3;
204
	$pconfig['ldap_anon'] = true;
205
	$pconfig['radius_protocol'] = "MSCHAPv2";
206
	$pconfig['radius_srvcs'] = "both";
207
	$pconfig['radius_auth_port'] = "1812";
208
	$pconfig['radius_acct_port'] = "1813";
209
}
210

    
211
if ($_POST['save']) {
212
	unset($input_errors);
213
	$pconfig = $_POST;
214

    
215
	/* input validation */
216

    
217
	if ($pconfig['type'] == "ldap") {
218
		$reqdfields = explode(" ",
219
			"name type ldap_host ldap_port " .
220
			"ldap_urltype ldap_protver ldap_scope " .
221
			"ldap_attr_user ldap_attr_group ldap_attr_member ldapauthcontainers");
222

    
223
		$reqdfieldsn = array(
224
			gettext("Descriptive name"),
225
			gettext("Type"),
226
			gettext("Hostname or IP"),
227
			gettext("Port value"),
228
			gettext("Transport"),
229
			gettext("Protocol version"),
230
			gettext("Search level"),
231
			gettext("User naming Attribute"),
232
			gettext("Group naming Attribute"),
233
			gettext("Group member attribute"),
234
			gettext("Authentication container"));
235

    
236
		if (!$pconfig['ldap_anon']) {
237
			$reqdfields[] = "ldap_binddn";
238
			$reqdfields[] = "ldap_bindpw";
239
			$reqdfieldsn[] = gettext("Bind user DN");
240
			$reqdfieldsn[] = gettext("Bind Password");
241
		}
242
	}
243

    
244
	if ($pconfig['type'] == "radius") {
245
		$reqdfields = explode(" ", "name type radius_protocol radius_host radius_srvcs");
246
		$reqdfieldsn = array(
247
			gettext("Descriptive name"),
248
			gettext("Type"),
249
			gettext("Radius Protocol"),
250
			gettext("Hostname or IP"),
251
			gettext("Services"));
252

    
253
		if ($pconfig['radius_srvcs'] == "both" ||
254
			$pconfig['radius_srvcs'] == "auth") {
255
			$reqdfields[] = "radius_auth_port";
256
			$reqdfieldsn[] = gettext("Authentication port");
257
		}
258

    
259
		if ($pconfig['radius_srvcs'] == "both" ||
260
			$pconfig['radius_srvcs'] == "acct") {
261
			$reqdfields[] = "radius_acct_port";
262
			$reqdfieldsn[] = gettext("Accounting port");
263
		}
264

    
265
		if (!isset($id)) {
266
			$reqdfields[] = "radius_secret";
267
			$reqdfieldsn[] = gettext("Shared Secret");
268
		}
269
	}
270

    
271
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
272

    
273
	if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['host'])) {
274
		$input_errors[] = gettext("The host name contains invalid characters.");
275
	}
276

    
277
	if (auth_get_authserver($pconfig['name']) && !isset($id)) {
278
		$input_errors[] = gettext("An authentication server with the same name already exists.");
279
	}
280

    
281
	if (isset($id) && $config['system']['authserver'][$id] &&
282
	   ($config['system']['authserver'][$id]['name'] != $pconfig['name'])) {
283
		$input_errors[] = gettext("The name of an authentication server cannot be changed.");
284
	}
285

    
286
	if (($pconfig['type'] == "ldap") || ($pconfig['type'] == "radius")) {
287
		$to_field = "{$pconfig['type']}_timeout";
288
		if (isset($_POST[$to_field]) && !empty($_POST[$to_field]) && (!is_numeric($_POST[$to_field]) || (is_numeric($_POST[$to_field]) && ($_POST[$to_field] <= 0)))) {
289
			$input_errors[] = sprintf(gettext("%s Timeout value must be numeric and positive."), strtoupper($pconfig['type']));
290
		}
291
	}
292

    
293
	// https://redmine.pfsense.org/issues/4154
294
	if ($pconfig['type'] == "radius") {
295
		if (is_ipaddrv6($_POST['radius_host'])) {
296
			$input_errors[] = gettext("IPv6 does not work for RADIUS authentication, see Bug #4154.");
297
		}
298
	}
299

    
300
	if (!$input_errors) {
301
		$server = array();
302
		$server['refid'] = uniqid();
303
		if (isset($id) && $a_server[$id]) {
304
			$server = $a_server[$id];
305
		}
306

    
307
		$server['type'] = $pconfig['type'];
308
		$server['name'] = $pconfig['name'];
309

    
310
		if ($server['type'] == "ldap") {
311

    
312
			if (!empty($pconfig['ldap_caref'])) {
313
				$server['ldap_caref'] = $pconfig['ldap_caref'];
314
			}
315
			$server['host'] = $pconfig['ldap_host'];
316
			$server['ldap_port'] = $pconfig['ldap_port'];
317
			$server['ldap_urltype'] = $pconfig['ldap_urltype'];
318
			$server['ldap_protver'] = $pconfig['ldap_protver'];
319
			$server['ldap_scope'] = $pconfig['ldap_scope'];
320
			$server['ldap_basedn'] = $pconfig['ldap_basedn'];
321
			$server['ldap_authcn'] = $pconfig['ldapauthcontainers'];
322
			$server['ldap_extended_enabled'] = $pconfig['ldap_extended_enabled'];
323
			$server['ldap_extended_query'] = $pconfig['ldap_extended_query'];
324
			$server['ldap_attr_user'] = $pconfig['ldap_attr_user'];
325
			$server['ldap_attr_group'] = $pconfig['ldap_attr_group'];
326
			$server['ldap_attr_member'] = $pconfig['ldap_attr_member'];
327

    
328
			$server['ldap_attr_groupobj'] = empty($pconfig['ldap_attr_groupobj']) ? "posixGroup" : $pconfig['ldap_attr_groupobj'];
329

    
330
			if ($pconfig['ldap_utf8'] == "yes") {
331
				$server['ldap_utf8'] = true;
332
			} else {
333
				unset($server['ldap_utf8']);
334
			}
335
			if ($pconfig['ldap_nostrip_at'] == "yes") {
336
				$server['ldap_nostrip_at'] = true;
337
			} else {
338
				unset($server['ldap_nostrip_at']);
339
			}
340
			if ($pconfig['ldap_allow_unauthenticated'] == "yes") {
341
				$server['ldap_allow_unauthenticated'] = true;
342
			} else {
343
				unset($server['ldap_allow_unauthenticated']);
344
			}
345
			if ($pconfig['ldap_rfc2307'] == "yes") {
346
				$server['ldap_rfc2307'] = true;
347
			} else {
348
				unset($server['ldap_rfc2307']);
349
			}
350

    
351

    
352
			if (!$pconfig['ldap_anon']) {
353
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
354
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
355
			} else {
356
				unset($server['ldap_binddn']);
357
				unset($server['ldap_bindpw']);
358
			}
359

    
360
			if ($pconfig['ldap_timeout']) {
361
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
362
			} else {
363
				$server['ldap_timeout'] = 25;
364
			}
365
		}
366

    
367
		if ($server['type'] == "radius") {
368

    
369
			$server['radius_protocol'] = $pconfig['radius_protocol'];
370
			$server['host'] = $pconfig['radius_host'];
371
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
372

    
373
			if ($pconfig['radius_secret']) {
374
				$server['radius_secret'] = $pconfig['radius_secret'];
375
			}
376

    
377
			if ($pconfig['radius_timeout']) {
378
				$server['radius_timeout'] = $pconfig['radius_timeout'];
379
			} else {
380
				$server['radius_timeout'] = 5;
381
			}
382

    
383
			if ($pconfig['radius_srvcs'] == "both") {
384
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
385
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
386
			}
387

    
388
			if ($pconfig['radius_srvcs'] == "auth") {
389
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
390
				unset($server['radius_acct_port']);
391
			}
392

    
393
			if ($pconfig['radius_srvcs'] == "acct") {
394
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
395
				unset($server['radius_auth_port']);
396
			}
397
		}
398

    
399
		if (isset($id) && $config['system']['authserver'][$id]) {
400
			$config['system']['authserver'][$id] = $server;
401
		} else {
402
			$config['system']['authserver'][] = $server;
403
		}
404

    
405
		write_config();
406

    
407
		pfSenseHeader("system_authservers.php");
408
	}
409
}
410

    
411
function build_radiusnas_list() {
412
	global $config;
413
	$list = array();
414

    
415
	$iflist = get_configured_interface_with_descr();
416
	foreach ($iflist as $ifdesc => $ifdescr) {
417
		$ipaddr = get_interface_ip($ifdesc);
418
		if (is_ipaddr($ipaddr)) {
419
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
420
		}
421
	}
422

    
423
	if (is_array($config['virtualip']['vip'])) {
424
		foreach ($config['virtualip']['vip'] as $sn) {
425
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
426
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
427
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
428
				$len = $end - $start;
429

    
430
				for ($i = 0; $i <= $len; $i++) {
431
					$snip = long2ip32($start+$i);
432
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
433
				}
434
			} else {
435
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
436
			}
437
		}
438
	}
439

    
440
	return($list);
441
}
442

    
443
// On error, restore the form contents so the user doesn't have to re-enter too much
444
if ($_POST && $input_errors) {
445
	$pconfig = $_POST;
446
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
447
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
448
}
449

    
450
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
451
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
452

    
453
if ($act == "new" || $act == "edit" || $input_errors) {
454
	$pgtitle[] = gettext('Edit');
455
	$pglinks[] = "@self";
456
}
457
$shortcut_section = "authentication";
458
include("head.inc");
459

    
460
if ($input_errors) {
461
	print_input_errors($input_errors);
462
}
463

    
464
if ($savemsg) {
465
	print_info_box($savemsg, 'success');
466
}
467

    
468
$tab_array = array();
469
if (!isAllowedPage("system_usermanager.php")) {
470
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
471
} else {
472
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
473
}
474
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
475
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
476
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
477
display_top_tabs($tab_array);
478

    
479
if (!($act == "new" || $act == "edit" || $input_errors)) {
480
?>
481
<div class="panel panel-default">
482
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Authentication Servers')?></h2></div>
483
	<div class="panel-body">
484
		<div class="table-responsive">
485
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
486
				<thead>
487
					<tr>
488
						<th><?=gettext("Server Name")?></th>
489
						<th><?=gettext("Type")?></th>
490
						<th><?=gettext("Host Name")?></th>
491
						<th><?=gettext("Actions")?></th>
492
					</tr>
493
				</thead>
494
				<tbody>
495
			<?php foreach ($a_server as $i => $server): ?>
496
					<tr>
497
						<td><?=htmlspecialchars($server['name'])?></td>
498
						<td><?=htmlspecialchars($auth_server_types[$server['type']])?></td>
499
						<td><?=htmlspecialchars($server['host'])?></td>
500
						<td>
501
						<?php if ($i < (count($a_server) - 1)): ?>
502
							<a class="fa fa-pencil" title="<?=gettext("Edit server"); ?>" href="system_authservers.php?act=edit&amp;id=<?=$i?>"></a>
503
							<a class="fa fa-trash"  title="<?=gettext("Delete server")?>" href="system_authservers.php?act=del&amp;id=<?=$i?>" usepost></a>
504
						<?php endif?>
505
						</td>
506
					</tr>
507
			<?php endforeach; ?>
508
				</tbody>
509
			</table>
510
		</div>
511
	</div>
512
</div>
513

    
514
<nav class="action-buttons">
515
	<a href="?act=new" class="btn btn-success btn-sm">
516
		<i class="fa fa-plus icon-embed-btn"></i>
517
		<?=gettext("Add")?>
518
	</a>
519
</nav>
520
<?php
521
	include("foot.inc");
522
	exit;
523
}
524

    
525
$form = new Form;
526
$form->setAction('system_authservers.php?act=edit');
527

    
528
$form->addGlobal(new Form_Input(
529
	'userid',
530
	null,
531
	'hidden',
532
	$id
533
));
534

    
535
$section = new Form_Section('Server Settings');
536

    
537
$section->addInput($input = new Form_Input(
538
	'name',
539
	'*Descriptive name',
540
	'text',
541
	$pconfig['name']
542
));
543

    
544
$section->addInput($input = new Form_Select(
545
	'type',
546
	'*Type',
547
	$pconfig['type'],
548
	$auth_server_types
549
))->toggles();
550

    
551
$form->add($section);
552

    
553
// ==== LDAP settings =========================================================
554
$section = new Form_Section('LDAP Server Settings');
555
$section->addClass('toggle-ldap collapse');
556

    
557
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
558
	$section->addClass('in');
559

    
560
$section->addInput(new Form_Input(
561
	'ldap_host',
562
	'*Hostname or IP address',
563
	'text',
564
	$pconfig['ldap_host']
565
))->setHelp('NOTE: When using SSL/TLS or STARTTLS, this hostname MUST match a Subject '.
566
	'Alternative Name (SAN) or the Common Name (CN) of the LDAP server SSL/TLS Certificate.');
567

    
568
$section->addInput(new Form_Input(
569
	'ldap_port',
570
	'*Port value',
571
	'number',
572
	$pconfig['ldap_port']
573
));
574

    
575
$section->addInput(new Form_Select(
576
	'ldap_urltype',
577
	'*Transport',
578
	$pconfig['ldap_urltype'],
579
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
580
));
581

    
582
if (empty($a_ca))
583
{
584
	$section->addInput(new Form_StaticText(
585
		'Peer Certificate Authority',
586
		'No Certificate Authorities defined.<br/>Create one under <a href="system_camanager.php">System &gt; Cert. Manager</a>.'
587
	));
588
}
589
else
590
{
591
	$ldapCaRef = array( 'global' => 'Global Root CA List' );
592
	foreach ($a_ca as $ca)
593
		$ldapCaRef[ $ca['refid'] ] = $ca['descr'];
594

    
595
	$section->addInput(new Form_Select(
596
		'ldap_caref',
597
		'Peer Certificate Authority',
598
		$pconfig['ldap_caref'],
599
		$ldapCaRef
600
	))->setHelp('This CA is used to validate the LDAP server certificate when '.
601
		'\'SSL/TLS Encrypted\' or \'STARTTLS Encrypted\' Transport is active. '.
602
		'This CA must match the CA used by the LDAP server.');
603
}
604

    
605
$section->addInput(new Form_Select(
606
	'ldap_protver',
607
	'*Protocol version',
608
	$pconfig['ldap_protver'],
609
	array_combine($ldap_protvers, $ldap_protvers)
610
));
611

    
612
$section->addInput(new Form_Input(
613
	'ldap_timeout',
614
	'Server Timeout',
615
	'number',
616
	$pconfig['ldap_timeout'],
617
	['placeholder' => 25]
618
))->setHelp('Timeout for LDAP operations (seconds)');
619

    
620
$group = new Form_Group('Search scope');
621

    
622
$SSF = new Form_Select(
623
	'ldap_scope',
624
	'*Level',
625
	$pconfig['ldap_scope'],
626
	$ldap_scopes
627
);
628

    
629
$SSB = new Form_Input(
630
	'ldap_basedn',
631
	'Base DN',
632
	'text',
633
	$pconfig['ldap_basedn']
634
);
635

    
636

    
637
$section->addInput(new Form_StaticText(
638
	'Search scope',
639
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
640
));
641

    
642
$group = new Form_Group('*Authentication containers');
643
$group->add(new Form_Input(
644
	'ldapauthcontainers',
645
	'Containers',
646
	'text',
647
	$pconfig['ldap_authcn']
648
))->setHelp('Note: Semi-Colon separated. This will be prepended to the search '.
649
	'base dn above or the full container path can be specified containing a dc= '.
650
	'component.%1$sExample: CN=Users;DC=example,DC=com or OU=Staff;OU=Freelancers', '<br/>');
651

    
652
$group->add(new Form_Button(
653
	'Select',
654
	'Select a container',
655
	null,
656
	'fa-search'
657
))->setAttribute('type','button')->addClass('btn-info');
658

    
659
$section->add($group);
660

    
661
$section->addInput(new Form_Checkbox(
662
	'ldap_extended_enabled',
663
	'Extended query',
664
	'Enable extended query',
665
	$pconfig['ldap_extended_enabled']
666
));
667

    
668
$group = new Form_Group('Query');
669
$group->addClass('extended');
670

    
671
$group->add(new Form_Input(
672
	'ldap_extended_query',
673
	'Query',
674
	'text',
675
	$pconfig['ldap_extended_query']
676
))->setHelp('Example (MSAD): memberOf=CN=Groupname,OU=MyGroups,DC=example,DC=com<br>Example (2307): |(&(objectClass=posixGroup)(cn=Groupname)(memberUid=*))(&(objectClass=posixGroup)(cn=anotherGroup)(memberUid=*))');
677

    
678
$section->add($group);
679

    
680
$section->addInput(new Form_Checkbox(
681
	'ldap_anon',
682
	'Bind anonymous',
683
	'Use anonymous binds to resolve distinguished names',
684
	$pconfig['ldap_anon']
685
));
686

    
687
$group = new Form_Group('*Bind credentials');
688
$group->addClass('ldapanon');
689

    
690
$group->add(new Form_Input(
691
	'ldap_binddn',
692
	'User DN:',
693
	'text',
694
	$pconfig['ldap_binddn']
695
));
696

    
697
$group->add(new Form_Input(
698
	'ldap_bindpw',
699
	'Password',
700
	'password',
701
	$pconfig['ldap_bindpw']
702
));
703
$section->add($group);
704

    
705
if (!isset($id)) {
706
	$template_list = array();
707

    
708
	foreach ($ldap_templates as $option => $template) {
709
		$template_list[$option] = $template['desc'];
710
	}
711

    
712
	$section->addInput(new Form_Select(
713
		'ldap_tmpltype',
714
		'Initial Template',
715
		$pconfig['ldap_template'],
716
		$template_list
717
	));
718
}
719

    
720
$section->addInput(new Form_Input(
721
	'ldap_attr_user',
722
	'*User naming attribute',
723
	'text',
724
	$pconfig['ldap_attr_user']
725
));
726

    
727
$section->addInput(new Form_Input(
728
	'ldap_attr_group',
729
	'*Group naming attribute',
730
	'text',
731
	$pconfig['ldap_attr_group']
732
));
733

    
734
$section->addInput(new Form_Input(
735
	'ldap_attr_member',
736
	'*Group member attribute',
737
	'text',
738
	$pconfig['ldap_attr_member']
739
));
740

    
741
$section->addInput(new Form_Checkbox(
742
	'ldap_rfc2307',
743
	'RFC 2307 Groups',
744
	'LDAP Server uses RFC 2307 style group membership',
745
	$pconfig['ldap_rfc2307']
746
))->setHelp('RFC 2307 style group membership has members listed on the group '.
747
	'object rather than using groups listed on user object. Leave unchecked '.
748
	'for Active Directory style group membership (RFC 2307bis).');
749

    
750
$section->addInput(new Form_Input(
751
	'ldap_attr_groupobj',
752
	'Group Object Class',
753
	'text',
754
	$pconfig['ldap_attr_groupobj'],
755
	['placeholder' => 'posixGroup']
756
))->setHelp('Object class used for groups in RFC2307 mode. '.
757
	'Typically "posixGroup" or "group".');
758

    
759
$section->addInput(new Form_Checkbox(
760
	'ldap_utf8',
761
	'UTF8 Encode',
762
	'UTF8 encode LDAP parameters before sending them to the server.',
763
	$pconfig['ldap_utf8']
764
))->setHelp('Required to support international characters, but may not be '.
765
	'supported by every LDAP server.');
766

    
767
$section->addInput(new Form_Checkbox(
768
	'ldap_nostrip_at',
769
	'Username Alterations',
770
	'Do not strip away parts of the username after the @ symbol',
771
	$pconfig['ldap_nostrip_at']
772
))->setHelp('e.g. user@host becomes user when unchecked.');
773

    
774
$section->addInput(new Form_Checkbox(
775
	'ldap_allow_unauthenticated',
776
	'Allow unauthenticated bind',
777
	'Allow unauthenticated bind',
778
	$pconfig['ldap_allow_unauthenticated']
779
))->setHelp('Unauthenticated binds are bind with an existing login but with an empty password. '.
780
         'Some LDAP servers (Microsoft AD) allow this type of bind without any possiblity to disable it.');
781

    
782
$form->add($section);
783

    
784
// ==== RADIUS section ========================================================
785
$section = new Form_Section('RADIUS Server Settings');
786
$section->addClass('toggle-radius collapse');
787

    
788
$section->addInput(new Form_Select(
789
	'radius_protocol',
790
	'*Protocol',
791
	$pconfig['radius_protocol'],
792
	$radius_protocol
793
));
794

    
795
$section->addInput(new Form_Input(
796
	'radius_host',
797
	'*Hostname or IP address',
798
	'text',
799
	$pconfig['radius_host']
800
));
801

    
802
$section->addInput(new Form_Input(
803
	'radius_secret',
804
	'*Shared Secret',
805
	'password',
806
	$pconfig['radius_secret']
807
));
808

    
809
$section->addInput(new Form_Select(
810
	'radius_srvcs',
811
	'*Services offered',
812
	$pconfig['radius_srvcs'],
813
	$radius_srvcs
814
));
815

    
816
$section->addInput(new Form_Input(
817
	'radius_auth_port',
818
	'Authentication port',
819
	'number',
820
	$pconfig['radius_auth_port']
821
));
822

    
823
$section->addInput(new Form_Input(
824
	'radius_acct_port',
825
	'Accounting port',
826
	'number',
827
	$pconfig['radius_acct_port']
828
));
829

    
830
$section->addInput(new Form_Input(
831
	'radius_timeout',
832
	'Authentication Timeout',
833
	'number',
834
	$pconfig['radius_timeout']
835
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
836
	'server may take to respond to an authentication request. If left blank, the '.
837
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
838
	'authentication system, increase this timeout to account for how long it will '.
839
	'take the user to receive and enter a token.');
840

    
841
$section->addInput(new Form_Select(
842
	'radius_nasip_attribute',
843
	'RADIUS NAS IP Attribute',
844
	$pconfig['radius_nasip_attribute'],
845
	build_radiusnas_list()
846
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Acccess-Requests.<br />'.
847
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
848

    
849
if (isset($id) && $a_server[$id])
850
{
851
	$form->addGlobal(new Form_Input(
852
		'id',
853
		null,
854
		'hidden',
855
		$id
856
	));
857
}
858

    
859
$form->add($section);
860

    
861
// Create a largely empty modal to show the available containers. We will populate it via AJAX later
862
$modal = new Modal("LDAP containers", "containers", true);
863

    
864
$form->add($modal);
865

    
866
print $form;
867
?>
868
<script type="text/javascript">
869
//<![CDATA[
870
events.push(function() {
871

    
872
	// Create an AJAX request (to this page) to get the container list and controls
873
	function select_clicked() {
874
		if (document.getElementById("ldap_port").value == '' ||
875
			document.getElementById("ldap_host").value == '' ||
876
			document.getElementById("ldap_scope").value == '' ||
877
			document.getElementById("ldap_basedn").value == '' ||
878
			document.getElementById("ldapauthcontainers").value == '') {
879
			alert("<?=gettext("Please fill the required values.");?>");
880
			return;
881
		}
882

    
883
		if (!document.getElementById("ldap_anon").checked) {
884
			if (document.getElementById("ldap_binddn").value == '' ||
885
				document.getElementById("ldap_bindpw").value == '') {
886
				alert("<?=gettext("Please fill the bind username/password.");?>");
887
				return;
888
			}
889
		}
890

    
891
		var ajaxRequest;
892
		var authserver = $('#authmode').val();
893
		var cert;
894

    
895
<?php if (count($a_ca) > 0): ?>
896
			cert = $('#ldap_caref').val();
897
<?php else: ?>
898
			cert = '';
899
<?php endif; ?>
900
/*
901
		$('#containers').modal('show');
902
		$('#serverlist').parent('div').prev('label').remove();
903
		$('#serverlist').parent('div').removeClass("col-sm-10");
904
		$('#serverlist').parent('div').addClass("col-sm-12");
905
*/
906
		ajaxRequest = $.ajax(
907
			{
908
				url: "/system_authservers.php",
909
				type: "post",
910
				data: {
911
					ajax: 	"ajax",
912
					port: 	$('#ldap_port').val(),
913
					host: 	$('#ldap_host').val(),
914
					scope: 	$('#ldap_scope').val(),
915
					basedn: $('#ldap_basedn').val(),
916
					binddn: $('#ldap_binddn').val(),
917
					bindpw: $('#ldap_bindpw').val(),
918
					urltype:$('#ldap_urltype').val(),
919
					proto:  $('#ldap_protver').val(),
920
					authcn: $('#ldapauthcontainers').val(),
921
					cert:   cert
922
				}
923
			}
924
		);
925

    
926
		// Deal with the results of the above ajax call
927
		ajaxRequest.done(function (response, textStatus, jqXHR) {
928
			$('#containers').replaceWith(response);
929

    
930
			$('#containers').modal('show');
931

    
932
			// The button handler needs to be here because until the modal has been populated
933
			// the controls we need to attach handlers to do not exist
934
			$('#svcontbtn').prop("type", "button");
935
			$('#svcontbtn').removeAttr("href");
936

    
937
			$('#svcontbtn').click(function () {
938
				var ous = $('[id^=ou]').length;
939
				var i;
940

    
941
				$('#ldapauthcontainers').val("");
942

    
943
				for (i = 0; i < ous; i++) {
944
					if ($('#ou' + i).prop("checked")) {
945
						if ($('#ldapauthcontainers').val() != "") {
946
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
947
						}
948

    
949
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
950
					}
951
				}
952

    
953
				$('#containers').modal('hide');
954
			});
955
		});
956

    
957
	}
958

    
959
	function set_ldap_port() {
960
		if ($('#ldap_urltype').find(":selected").index() == 2)
961
			$('#ldap_port').val('636');
962
		else
963
			$('#ldap_port').val('389');
964
	}
965

    
966
	function set_required_port_fields() {
967
		if (document.getElementById("radius_srvcs").value == 'auth') {
968
			setRequired('radius_auth_port', true);
969
			setRequired('radius_acct_port', false);
970
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
971
			setRequired('radius_auth_port', false);
972
			setRequired('radius_acct_port', true);
973
		} else { // both
974
			setRequired('radius_auth_port', true);
975
			setRequired('radius_acct_port', true);
976
		}
977
	}
978

    
979
	// Hides all elements of the specified class. This will usually be a section
980
	function hideClass(s_class, hide) {
981
		if (hide)
982
			$('.' + s_class).hide();
983
		else
984
			$('.' + s_class).show();
985
	}
986

    
987
	function ldap_tmplchange() {
988
		switch ($('#ldap_tmpltype').find(":selected").index()) {
989
<?php
990
		$index = 0;
991
		foreach ($ldap_templates as $tmpldata):
992
?>
993
			case <?=$index;?>:
994
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
995
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
996
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
997
				$("#ldap_allow_unauthenticated").attr("checked", <?=$tmpldata['allow_unauthenticated'];?>);
998
				break;
999
<?php
1000
			$index++;
1001
		endforeach;
1002
?>
1003
		}
1004
	}
1005

    
1006
	// ---------- On initial page load ------------------------------------------------------------
1007

    
1008
<?php if ($act != 'edit') : ?>
1009
	ldap_tmplchange();
1010
<?php endif; ?>
1011

    
1012
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
1013
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
1014
	set_required_port_fields();
1015

    
1016
	if ($('#ldap_port').val() == "")
1017
		set_ldap_port();
1018

    
1019
<?php
1020
	if ($act == 'edit') {
1021
?>
1022
		$('#type option:not(:selected)').each(function(){
1023
			$(this).attr('disabled', 'disabled');
1024
		});
1025

    
1026
<?php
1027
		if (!$input_errors) {
1028
?>
1029
		$('#name').prop("readonly", true);
1030
<?php
1031
		}
1032
	}
1033
?>
1034
	// ---------- Click checkbox handlers ---------------------------------------------------------
1035

    
1036
	$('#ldap_tmpltype').on('change', function() {
1037
		ldap_tmplchange();
1038
	});
1039

    
1040
	$('#ldap_anon').click(function () {
1041
		hideClass('ldapanon', this.checked);
1042
	});
1043

    
1044
	$('#ldap_urltype').on('change', function() {
1045
		set_ldap_port();
1046
	});
1047

    
1048
	$('#Select').click(function () {
1049
		select_clicked();
1050
	});
1051

    
1052
	$('#ldap_extended_enabled').click(function () {
1053
		hideClass('extended', !this.checked);
1054
	});
1055

    
1056
	$('#radius_srvcs').on('change', function() {
1057
		set_required_port_fields();
1058
	});
1059

    
1060
});
1061
//]]>
1062
</script>
1063
<?php
1064
include("foot.inc");
(191-191/227)