Project

General

Profile

Download (29.9 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
			$pconfig['ldap_rfc2307_userdn'] = isset($a_server[$id]['ldap_rfc2307_userdn']);
167

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

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

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

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

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

    
199
		}
200
	}
201
}
202

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

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

    
216
	/* input validation */
217

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
357

    
358
			if (!$pconfig['ldap_anon']) {
359
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
360
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
361
			} else {
362
				unset($server['ldap_binddn']);
363
				unset($server['ldap_bindpw']);
364
			}
365

    
366
			if ($pconfig['ldap_timeout']) {
367
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
368
			} else {
369
				$server['ldap_timeout'] = 25;
370
			}
371
		}
372

    
373
		if ($server['type'] == "radius") {
374

    
375
			$server['radius_protocol'] = $pconfig['radius_protocol'];
376
			$server['host'] = $pconfig['radius_host'];
377
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
378

    
379
			if ($pconfig['radius_secret']) {
380
				$server['radius_secret'] = $pconfig['radius_secret'];
381
			}
382

    
383
			if ($pconfig['radius_timeout']) {
384
				$server['radius_timeout'] = $pconfig['radius_timeout'];
385
			} else {
386
				$server['radius_timeout'] = 5;
387
			}
388

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

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

    
399
			if ($pconfig['radius_srvcs'] == "acct") {
400
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
401
				unset($server['radius_auth_port']);
402
			}
403
		}
404

    
405
		if (isset($id) && $config['system']['authserver'][$id]) {
406
			$config['system']['authserver'][$id] = $server;
407
		} else {
408
			$config['system']['authserver'][] = $server;
409
		}
410

    
411
		write_config();
412

    
413
		pfSenseHeader("system_authservers.php");
414
	}
415
}
416

    
417
function build_radiusnas_list() {
418
	global $config;
419
	$list = array();
420

    
421
	$iflist = get_configured_interface_with_descr();
422
	foreach ($iflist as $ifdesc => $ifdescr) {
423
		$ipaddr = get_interface_ip($ifdesc);
424
		if (is_ipaddr($ipaddr)) {
425
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
426
		}
427
	}
428

    
429
	if (is_array($config['virtualip']['vip'])) {
430
		foreach ($config['virtualip']['vip'] as $sn) {
431
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
432
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
433
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
434
				$len = $end - $start;
435

    
436
				for ($i = 0; $i <= $len; $i++) {
437
					$snip = long2ip32($start+$i);
438
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
439
				}
440
			} else {
441
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
442
			}
443
		}
444
	}
445

    
446
	return($list);
447
}
448

    
449
// On error, restore the form contents so the user doesn't have to re-enter too much
450
if ($_POST && $input_errors) {
451
	$pconfig = $_POST;
452
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
453
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
454
}
455

    
456
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
457
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
458

    
459
if ($act == "new" || $act == "edit" || $input_errors) {
460
	$pgtitle[] = gettext('Edit');
461
	$pglinks[] = "@self";
462
}
463
$shortcut_section = "authentication";
464
include("head.inc");
465

    
466
if ($input_errors) {
467
	print_input_errors($input_errors);
468
}
469

    
470
if ($savemsg) {
471
	print_info_box($savemsg, 'success');
472
}
473

    
474
$tab_array = array();
475
if (!isAllowedPage("system_usermanager.php")) {
476
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
477
} else {
478
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
479
}
480
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
481
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
482
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
483
display_top_tabs($tab_array);
484

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

    
520
<nav class="action-buttons">
521
	<a href="?act=new" class="btn btn-success btn-sm">
522
		<i class="fa fa-plus icon-embed-btn"></i>
523
		<?=gettext("Add")?>
524
	</a>
525
</nav>
526
<?php
527
	include("foot.inc");
528
	exit;
529
}
530

    
531
$form = new Form;
532
$form->setAction('system_authservers.php?act=edit');
533

    
534
$form->addGlobal(new Form_Input(
535
	'userid',
536
	null,
537
	'hidden',
538
	$id
539
));
540

    
541
$section = new Form_Section('Server Settings');
542

    
543
$section->addInput($input = new Form_Input(
544
	'name',
545
	'*Descriptive name',
546
	'text',
547
	$pconfig['name']
548
));
549

    
550
$section->addInput($input = new Form_Select(
551
	'type',
552
	'*Type',
553
	$pconfig['type'],
554
	$auth_server_types
555
))->toggles();
556

    
557
$form->add($section);
558

    
559
// ==== LDAP settings =========================================================
560
$section = new Form_Section('LDAP Server Settings');
561
$section->addClass('toggle-ldap collapse');
562

    
563
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
564
	$section->addClass('in');
565

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

    
574
$section->addInput(new Form_Input(
575
	'ldap_port',
576
	'*Port value',
577
	'number',
578
	$pconfig['ldap_port']
579
));
580

    
581
$section->addInput(new Form_Select(
582
	'ldap_urltype',
583
	'*Transport',
584
	$pconfig['ldap_urltype'],
585
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
586
));
587

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

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

    
611
$section->addInput(new Form_Select(
612
	'ldap_protver',
613
	'*Protocol version',
614
	$pconfig['ldap_protver'],
615
	array_combine($ldap_protvers, $ldap_protvers)
616
));
617

    
618
$section->addInput(new Form_Input(
619
	'ldap_timeout',
620
	'Server Timeout',
621
	'number',
622
	$pconfig['ldap_timeout'],
623
	['placeholder' => 25]
624
))->setHelp('Timeout for LDAP operations (seconds)');
625

    
626
$group = new Form_Group('Search scope');
627

    
628
$SSF = new Form_Select(
629
	'ldap_scope',
630
	'*Level',
631
	$pconfig['ldap_scope'],
632
	$ldap_scopes
633
);
634

    
635
$SSB = new Form_Input(
636
	'ldap_basedn',
637
	'Base DN',
638
	'text',
639
	$pconfig['ldap_basedn']
640
);
641

    
642

    
643
$section->addInput(new Form_StaticText(
644
	'Search scope',
645
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
646
));
647

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

    
658
$group->add(new Form_Button(
659
	'Select',
660
	'Select a container',
661
	null,
662
	'fa-search'
663
))->setAttribute('type','button')->addClass('btn-info');
664

    
665
$section->add($group);
666

    
667
$section->addInput(new Form_Checkbox(
668
	'ldap_extended_enabled',
669
	'Extended query',
670
	'Enable extended query',
671
	$pconfig['ldap_extended_enabled']
672
));
673

    
674
$group = new Form_Group('Query');
675
$group->addClass('extended');
676

    
677
$group->add(new Form_Input(
678
	'ldap_extended_query',
679
	'Query',
680
	'text',
681
	$pconfig['ldap_extended_query']
682
))->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=*))');
683

    
684
$section->add($group);
685

    
686
$section->addInput(new Form_Checkbox(
687
	'ldap_anon',
688
	'Bind anonymous',
689
	'Use anonymous binds to resolve distinguished names',
690
	$pconfig['ldap_anon']
691
));
692

    
693
$group = new Form_Group('*Bind credentials');
694
$group->addClass('ldapanon');
695

    
696
$group->add(new Form_Input(
697
	'ldap_binddn',
698
	'User DN:',
699
	'text',
700
	$pconfig['ldap_binddn']
701
));
702

    
703
$group->add(new Form_Input(
704
	'ldap_bindpw',
705
	'Password',
706
	'password',
707
	$pconfig['ldap_bindpw']
708
));
709
$section->add($group);
710

    
711
if (!isset($id)) {
712
	$template_list = array();
713

    
714
	foreach ($ldap_templates as $option => $template) {
715
		$template_list[$option] = $template['desc'];
716
	}
717

    
718
	$section->addInput(new Form_Select(
719
		'ldap_tmpltype',
720
		'Initial Template',
721
		$pconfig['ldap_template'],
722
		$template_list
723
	));
724
}
725

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

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

    
740
$section->addInput(new Form_Input(
741
	'ldap_attr_member',
742
	'*Group member attribute',
743
	'text',
744
	$pconfig['ldap_attr_member']
745
));
746

    
747
$section->addInput(new Form_Checkbox(
748
	'ldap_rfc2307',
749
	'RFC 2307 Groups',
750
	'LDAP Server uses RFC 2307 style group membership',
751
	$pconfig['ldap_rfc2307']
752
))->setHelp('RFC 2307 style group membership has members listed on the group '.
753
	'object rather than using groups listed on user object. Leave unchecked '.
754
	'for Active Directory style group membership (RFC 2307bis).');
755

    
756
$group = new Form_Group('RFC 2307 User DN');
757
$group->addClass('ldap_rfc2307_userdn');
758

    
759
$group->add(new Form_Checkbox(
760
	'ldap_rfc2307_userdn',
761
	'RFC 2307 user DN',
762
	'RFC 2307 Use DN for username search.',
763
	$pconfig['ldap_rfc2307_userdn']
764
))->setHelp('Use DN for username search, i.e. "(member=CN=Username,CN=Users,DC=example,DC=com)".');
765

    
766
$section->add($group);
767

    
768
$section->addInput(new Form_Input(
769
	'ldap_attr_groupobj',
770
	'Group Object Class',
771
	'text',
772
	$pconfig['ldap_attr_groupobj'],
773
	['placeholder' => 'posixGroup']
774
))->setHelp('Object class used for groups in RFC2307 mode. '.
775
	'Typically "posixGroup" or "group".');
776

    
777
$section->addInput(new Form_Checkbox(
778
	'ldap_utf8',
779
	'UTF8 Encode',
780
	'UTF8 encode LDAP parameters before sending them to the server.',
781
	$pconfig['ldap_utf8']
782
))->setHelp('Required to support international characters, but may not be '.
783
	'supported by every LDAP server.');
784

    
785
$section->addInput(new Form_Checkbox(
786
	'ldap_nostrip_at',
787
	'Username Alterations',
788
	'Do not strip away parts of the username after the @ symbol',
789
	$pconfig['ldap_nostrip_at']
790
))->setHelp('e.g. user@host becomes user when unchecked.');
791

    
792
$section->addInput(new Form_Checkbox(
793
	'ldap_allow_unauthenticated',
794
	'Allow unauthenticated bind',
795
	'Allow unauthenticated bind',
796
	$pconfig['ldap_allow_unauthenticated']
797
))->setHelp('Unauthenticated binds are bind with an existing login but with an empty password. '.
798
         'Some LDAP servers (Microsoft AD) allow this type of bind without any possiblity to disable it.');
799

    
800
$form->add($section);
801

    
802
// ==== RADIUS section ========================================================
803
$section = new Form_Section('RADIUS Server Settings');
804
$section->addClass('toggle-radius collapse');
805

    
806
$section->addInput(new Form_Select(
807
	'radius_protocol',
808
	'*Protocol',
809
	$pconfig['radius_protocol'],
810
	$radius_protocol
811
));
812

    
813
$section->addInput(new Form_Input(
814
	'radius_host',
815
	'*Hostname or IP address',
816
	'text',
817
	$pconfig['radius_host']
818
));
819

    
820
$section->addInput(new Form_Input(
821
	'radius_secret',
822
	'*Shared Secret',
823
	'password',
824
	$pconfig['radius_secret']
825
));
826

    
827
$section->addInput(new Form_Select(
828
	'radius_srvcs',
829
	'*Services offered',
830
	$pconfig['radius_srvcs'],
831
	$radius_srvcs
832
));
833

    
834
$section->addInput(new Form_Input(
835
	'radius_auth_port',
836
	'Authentication port',
837
	'number',
838
	$pconfig['radius_auth_port']
839
));
840

    
841
$section->addInput(new Form_Input(
842
	'radius_acct_port',
843
	'Accounting port',
844
	'number',
845
	$pconfig['radius_acct_port']
846
));
847

    
848
$section->addInput(new Form_Input(
849
	'radius_timeout',
850
	'Authentication Timeout',
851
	'number',
852
	$pconfig['radius_timeout']
853
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
854
	'server may take to respond to an authentication request. If left blank, the '.
855
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
856
	'authentication system, increase this timeout to account for how long it will '.
857
	'take the user to receive and enter a token.');
858

    
859
$section->addInput(new Form_Select(
860
	'radius_nasip_attribute',
861
	'RADIUS NAS IP Attribute',
862
	$pconfig['radius_nasip_attribute'],
863
	build_radiusnas_list()
864
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Acccess-Requests.<br />'.
865
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
866

    
867
if (isset($id) && $a_server[$id])
868
{
869
	$form->addGlobal(new Form_Input(
870
		'id',
871
		null,
872
		'hidden',
873
		$id
874
	));
875
}
876

    
877
$form->add($section);
878

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

    
882
$form->add($modal);
883

    
884
print $form;
885
?>
886
<script type="text/javascript">
887
//<![CDATA[
888
events.push(function() {
889

    
890
	// Create an AJAX request (to this page) to get the container list and controls
891
	function select_clicked() {
892
		if (document.getElementById("ldap_port").value == '' ||
893
			document.getElementById("ldap_host").value == '' ||
894
			document.getElementById("ldap_scope").value == '' ||
895
			document.getElementById("ldap_basedn").value == '' ||
896
			document.getElementById("ldapauthcontainers").value == '') {
897
			alert("<?=gettext("Please fill the required values.");?>");
898
			return;
899
		}
900

    
901
		if (!document.getElementById("ldap_anon").checked) {
902
			if (document.getElementById("ldap_binddn").value == '' ||
903
				document.getElementById("ldap_bindpw").value == '') {
904
				alert("<?=gettext("Please fill the bind username/password.");?>");
905
				return;
906
			}
907
		}
908

    
909
		var ajaxRequest;
910
		var authserver = $('#authmode').val();
911
		var cert;
912

    
913
<?php if (count($a_ca) > 0): ?>
914
			cert = $('#ldap_caref').val();
915
<?php else: ?>
916
			cert = '';
917
<?php endif; ?>
918
/*
919
		$('#containers').modal('show');
920
		$('#serverlist').parent('div').prev('label').remove();
921
		$('#serverlist').parent('div').removeClass("col-sm-10");
922
		$('#serverlist').parent('div').addClass("col-sm-12");
923
*/
924
		ajaxRequest = $.ajax(
925
			{
926
				url: "/system_authservers.php",
927
				type: "post",
928
				data: {
929
					ajax: 	"ajax",
930
					port: 	$('#ldap_port').val(),
931
					host: 	$('#ldap_host').val(),
932
					scope: 	$('#ldap_scope').val(),
933
					basedn: $('#ldap_basedn').val(),
934
					binddn: $('#ldap_binddn').val(),
935
					bindpw: $('#ldap_bindpw').val(),
936
					urltype:$('#ldap_urltype').val(),
937
					proto:  $('#ldap_protver').val(),
938
					authcn: $('#ldapauthcontainers').val(),
939
					cert:   cert
940
				}
941
			}
942
		);
943

    
944
		// Deal with the results of the above ajax call
945
		ajaxRequest.done(function (response, textStatus, jqXHR) {
946
			$('#containers').replaceWith(response);
947

    
948
			$('#containers').modal('show');
949

    
950
			// The button handler needs to be here because until the modal has been populated
951
			// the controls we need to attach handlers to do not exist
952
			$('#svcontbtn').prop("type", "button");
953
			$('#svcontbtn').removeAttr("href");
954

    
955
			$('#svcontbtn').click(function () {
956
				var ous = $('[id^=ou]').length;
957
				var i;
958

    
959
				$('#ldapauthcontainers').val("");
960

    
961
				for (i = 0; i < ous; i++) {
962
					if ($('#ou' + i).prop("checked")) {
963
						if ($('#ldapauthcontainers').val() != "") {
964
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
965
						}
966

    
967
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
968
					}
969
				}
970

    
971
				$('#containers').modal('hide');
972
			});
973
		});
974

    
975
	}
976

    
977
	function set_ldap_port() {
978
		if ($('#ldap_urltype').find(":selected").index() == 2)
979
			$('#ldap_port').val('636');
980
		else
981
			$('#ldap_port').val('389');
982
	}
983

    
984
	function set_required_port_fields() {
985
		if (document.getElementById("radius_srvcs").value == 'auth') {
986
			setRequired('radius_auth_port', true);
987
			setRequired('radius_acct_port', false);
988
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
989
			setRequired('radius_auth_port', false);
990
			setRequired('radius_acct_port', true);
991
		} else { // both
992
			setRequired('radius_auth_port', true);
993
			setRequired('radius_acct_port', true);
994
		}
995
	}
996

    
997
	// Hides all elements of the specified class. This will usually be a section
998
	function hideClass(s_class, hide) {
999
		if (hide)
1000
			$('.' + s_class).hide();
1001
		else
1002
			$('.' + s_class).show();
1003
	}
1004

    
1005
	function ldap_tmplchange() {
1006
		switch ($('#ldap_tmpltype').find(":selected").index()) {
1007
<?php
1008
		$index = 0;
1009
		foreach ($ldap_templates as $tmpldata):
1010
?>
1011
			case <?=$index;?>:
1012
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
1013
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
1014
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
1015
				$("#ldap_allow_unauthenticated").attr("checked", <?=$tmpldata['allow_unauthenticated'];?>);
1016
				break;
1017
<?php
1018
			$index++;
1019
		endforeach;
1020
?>
1021
		}
1022
	}
1023

    
1024
	// ---------- On initial page load ------------------------------------------------------------
1025

    
1026
<?php if ($act != 'edit') : ?>
1027
	ldap_tmplchange();
1028
<?php endif; ?>
1029

    
1030
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
1031
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
1032
	hideClass('ldap_rfc2307_userdn', !$('#ldap_rfc2307').prop('checked'));
1033
	set_required_port_fields();
1034

    
1035
	if ($('#ldap_port').val() == "")
1036
		set_ldap_port();
1037

    
1038
<?php
1039
	if ($act == 'edit') {
1040
?>
1041
		$('#type option:not(:selected)').each(function(){
1042
			$(this).attr('disabled', 'disabled');
1043
		});
1044

    
1045
<?php
1046
		if (!$input_errors) {
1047
?>
1048
		$('#name').prop("readonly", true);
1049
<?php
1050
		}
1051
	}
1052
?>
1053
	// ---------- Click checkbox handlers ---------------------------------------------------------
1054

    
1055
	$('#ldap_tmpltype').on('change', function() {
1056
		ldap_tmplchange();
1057
	});
1058

    
1059
	$('#ldap_anon').click(function () {
1060
		hideClass('ldapanon', this.checked);
1061
	});
1062

    
1063
	$('#ldap_urltype').on('change', function() {
1064
		set_ldap_port();
1065
	});
1066

    
1067
	$('#Select').click(function () {
1068
		select_clicked();
1069
	});
1070

    
1071
	$('#ldap_extended_enabled').click(function () {
1072
		hideClass('extended', !this.checked);
1073
	});
1074

    
1075
	$('#ldap_rfc2307').click(function () {
1076
		hideClass('ldap_rfc2307_userdn', !this.checked);
1077
	});
1078

    
1079
	$('#radius_srvcs').on('change', function() {
1080
		set_required_port_fields();
1081
	});
1082

    
1083
});
1084
//]]>
1085
</script>
1086
<?php
1087
include("foot.inc");
(192-192/228)