Project

General

Profile

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

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

    
31
require_once("guiconfig.inc");
32
require_once("auth.inc");
33
require_once("pfsense-utils.inc");
34

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

    
38
	$ous = array();
39
	$authcfg = array();
40

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

    
52
	$ous = ldap_get_user_ous(true, $authcfg);
53

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

    
60
		if (is_array($ous)) {
61
			$idx = 0;
62

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

    
72
				$idx++;
73
			}
74
		}
75

    
76
		$modal->add($group);
77

    
78
		// Create a "Save button"
79

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

    
87
		$btnsv->removeClass("btn-default)")->addClass("btn-primary");
88

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

    
94
		print($modal);
95
	}
96

    
97
	exit;
98
}
99

    
100
$id = $_REQUEST['id'];
101

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

    
106
$a_server = array_values(auth_get_authserver_list());
107

    
108
init_config_arr(array('ca'));
109
$a_ca = &$config['ca'];
110

    
111
$act = $_REQUEST['act'];
112

    
113
if ($_POST['act'] == "del") {
114

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

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

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

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

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

    
139
		$pconfig['type'] = $a_server[$id]['type'];
140
		$pconfig['name'] = $a_server[$id]['name'];
141

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

    
164
			if (!$pconfig['ldap_binddn'] || !$pconfig['ldap_bindpw']) {
165
				$pconfig['ldap_anon'] = true;
166
			}
167
		}
168

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

    
178
			if ($pconfig['radius_auth_port'] &&
179
				$pconfig['radius_acct_port']) {
180
				$pconfig['radius_srvcs'] = "both";
181
			}
182

    
183
			if ($pconfig['radius_auth_port'] &&
184
				!$pconfig['radius_acct_port']) {
185
				$pconfig['radius_srvcs'] = "auth";
186
				$pconfig['radius_acct_port'] = 1813;
187
			}
188

    
189
			if (!$pconfig['radius_auth_port'] &&
190
				$pconfig['radius_acct_port']) {
191
				$pconfig['radius_srvcs'] = "acct";
192
				$pconfig['radius_auth_port'] = 1812;
193
			}
194

    
195
		}
196
	}
197
}
198

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

    
208
if ($_POST['save']) {
209
	unset($input_errors);
210
	$pconfig = $_POST;
211

    
212
	/* input validation */
213

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

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

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

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

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

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

    
262
		if (!isset($id)) {
263
			$reqdfields[] = "radius_secret";
264
			$reqdfieldsn[] = gettext("Shared Secret");
265
		}
266
	}
267

    
268
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
269

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

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

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

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

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

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

    
304
		$server['type'] = $pconfig['type'];
305
		$server['name'] = $pconfig['name'];
306

    
307
		if ($server['type'] == "ldap") {
308

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

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

    
327
			if ($pconfig['ldap_utf8'] == "yes") {
328
				$server['ldap_utf8'] = true;
329
			} else {
330
				unset($server['ldap_utf8']);
331
			}
332
			if ($pconfig['ldap_nostrip_at'] == "yes") {
333
				$server['ldap_nostrip_at'] = true;
334
			} else {
335
				unset($server['ldap_nostrip_at']);
336
			}
337
			if ($pconfig['ldap_rfc2307'] == "yes") {
338
				$server['ldap_rfc2307'] = true;
339
			} else {
340
				unset($server['ldap_rfc2307']);
341
			}
342

    
343

    
344
			if (!$pconfig['ldap_anon']) {
345
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
346
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
347
			} else {
348
				unset($server['ldap_binddn']);
349
				unset($server['ldap_bindpw']);
350
			}
351

    
352
			if ($pconfig['ldap_timeout']) {
353
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
354
			} else {
355
				$server['ldap_timeout'] = 25;
356
			}
357
		}
358

    
359
		if ($server['type'] == "radius") {
360

    
361
			$server['radius_protocol'] = $pconfig['radius_protocol'];
362
			$server['host'] = $pconfig['radius_host'];
363
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
364

    
365
			if ($pconfig['radius_secret']) {
366
				$server['radius_secret'] = $pconfig['radius_secret'];
367
			}
368

    
369
			if ($pconfig['radius_timeout']) {
370
				$server['radius_timeout'] = $pconfig['radius_timeout'];
371
			} else {
372
				$server['radius_timeout'] = 5;
373
			}
374

    
375
			if ($pconfig['radius_srvcs'] == "both") {
376
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
377
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
378
			}
379

    
380
			if ($pconfig['radius_srvcs'] == "auth") {
381
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
382
				unset($server['radius_acct_port']);
383
			}
384

    
385
			if ($pconfig['radius_srvcs'] == "acct") {
386
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
387
				unset($server['radius_auth_port']);
388
			}
389
		}
390

    
391
		if (isset($id) && $config['system']['authserver'][$id]) {
392
			$config['system']['authserver'][$id] = $server;
393
		} else {
394
			$config['system']['authserver'][] = $server;
395
		}
396

    
397
		write_config();
398

    
399
		pfSenseHeader("system_authservers.php");
400
	}
401
}
402

    
403
function build_radiusnas_list() {
404
	global $config;
405
	$list = array();
406

    
407
	$iflist = get_configured_interface_with_descr();
408
	foreach ($iflist as $ifdesc => $ifdescr) {
409
		$ipaddr = get_interface_ip($ifdesc);
410
		if (is_ipaddr($ipaddr)) {
411
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
412
		}
413
	}
414

    
415
	if (is_array($config['virtualip']['vip'])) {
416
		foreach ($config['virtualip']['vip'] as $sn) {
417
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
418
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
419
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
420
				$len = $end - $start;
421

    
422
				for ($i = 0; $i <= $len; $i++) {
423
					$snip = long2ip32($start+$i);
424
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
425
				}
426
			} else {
427
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
428
			}
429
		}
430
	}
431

    
432
	return($list);
433
}
434

    
435
// On error, restore the form contents so the user doesn't have to re-enter too much
436
if ($_POST && $input_errors) {
437
	$pconfig = $_POST;
438
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
439
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
440
}
441

    
442
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
443
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
444

    
445
if ($act == "new" || $act == "edit" || $input_errors) {
446
	$pgtitle[] = gettext('Edit');
447
	$pglinks[] = "@self";
448
}
449
$shortcut_section = "authentication";
450
include("head.inc");
451

    
452
if ($input_errors) {
453
	print_input_errors($input_errors);
454
}
455

    
456
if ($savemsg) {
457
	print_info_box($savemsg, 'success');
458
}
459

    
460
$tab_array = array();
461
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
462
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
463
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
464
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
465
display_top_tabs($tab_array);
466

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

    
502
<nav class="action-buttons">
503
	<a href="?act=new" class="btn btn-success btn-sm">
504
		<i class="fa fa-plus icon-embed-btn"></i>
505
		<?=gettext("Add")?>
506
	</a>
507
</nav>
508
<?php
509
	include("foot.inc");
510
	exit;
511
}
512

    
513
$form = new Form;
514
$form->setAction('system_authservers.php?act=edit');
515

    
516
$form->addGlobal(new Form_Input(
517
	'userid',
518
	null,
519
	'hidden',
520
	$id
521
));
522

    
523
$section = new Form_Section('Server Settings');
524

    
525
$section->addInput($input = new Form_Input(
526
	'name',
527
	'*Descriptive name',
528
	'text',
529
	$pconfig['name']
530
));
531

    
532
$section->addInput($input = new Form_Select(
533
	'type',
534
	'*Type',
535
	$pconfig['type'],
536
	$auth_server_types
537
))->toggles();
538

    
539
$form->add($section);
540

    
541
// ==== LDAP settings =========================================================
542
$section = new Form_Section('LDAP Server Settings');
543
$section->addClass('toggle-ldap collapse');
544

    
545
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
546
	$section->addClass('in');
547

    
548
$section->addInput(new Form_Input(
549
	'ldap_host',
550
	'*Hostname or IP address',
551
	'text',
552
	$pconfig['ldap_host']
553
))->setHelp('NOTE: When using SSL or STARTTLS, this hostname MUST match the Common Name '.
554
	'(CN) of the LDAP server\'s SSL Certificate.');
555

    
556
$section->addInput(new Form_Input(
557
	'ldap_port',
558
	'*Port value',
559
	'number',
560
	$pconfig['ldap_port']
561
));
562

    
563
$section->addInput(new Form_Select(
564
	'ldap_urltype',
565
	'*Transport',
566
	$pconfig['ldap_urltype'],
567
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
568
));
569

    
570
if (empty($a_ca))
571
{
572
	$section->addInput(new Form_StaticText(
573
		'Peer Certificate Authority',
574
		'No Certificate Authorities defined.<br/>Create one under <a href="system_camanager.php">System &gt; Cert. Manager</a>.'
575
	));
576
}
577
else
578
{
579
	$ldapCaRef = array( 'global' => 'Global Root CA List' );
580
	foreach ($a_ca as $ca)
581
		$ldapCaRef[ $ca['refid'] ] = $ca['descr'];
582

    
583
	$section->addInput(new Form_Select(
584
		'ldap_caref',
585
		'Peer Certificate Authority',
586
		$pconfig['ldap_caref'],
587
		$ldapCaRef
588
	))->setHelp('This option is used if \'SSL Encrypted\' '.
589
		'or \'TCP - STARTTLS\' options are chosen. '.
590
		'It must match with the CA in the AD otherwise problems will arise.');
591
}
592

    
593
$section->addInput(new Form_Select(
594
	'ldap_protver',
595
	'*Protocol version',
596
	$pconfig['ldap_protver'],
597
	array_combine($ldap_protvers, $ldap_protvers)
598
));
599

    
600
$section->addInput(new Form_Input(
601
	'ldap_timeout',
602
	'Server Timeout',
603
	'number',
604
	$pconfig['ldap_timeout'],
605
	['placeholder' => 25]
606
))->setHelp('Timeout for LDAP operations (seconds)');
607

    
608
$group = new Form_Group('Search scope');
609

    
610
$SSF = new Form_Select(
611
	'ldap_scope',
612
	'*Level',
613
	$pconfig['ldap_scope'],
614
	$ldap_scopes
615
);
616

    
617
$SSB = new Form_Input(
618
	'ldap_basedn',
619
	'Base DN',
620
	'text',
621
	$pconfig['ldap_basedn']
622
);
623

    
624

    
625
$section->addInput(new Form_StaticText(
626
	'Search scope',
627
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
628
));
629

    
630
$group = new Form_Group('*Authentication containers');
631
$group->add(new Form_Input(
632
	'ldapauthcontainers',
633
	'Containers',
634
	'text',
635
	$pconfig['ldap_authcn']
636
))->setHelp('Note: Semi-Colon separated. This will be prepended to the search '.
637
	'base dn above or the full container path can be specified containing a dc= '.
638
	'component.%1$sExample: CN=Users;DC=example,DC=com or OU=Staff;OU=Freelancers', '<br/>');
639

    
640
$group->add(new Form_Button(
641
	'Select',
642
	'Select a container',
643
	null,
644
	'fa-search'
645
))->setAttribute('type','button')->addClass('btn-info');
646

    
647
$section->add($group);
648

    
649
$section->addInput(new Form_Checkbox(
650
	'ldap_extended_enabled',
651
	'Extended query',
652
	'Enable extended query',
653
	$pconfig['ldap_extended_enabled']
654
));
655

    
656
$group = new Form_Group('Query');
657
$group->addClass('extended');
658

    
659
$group->add(new Form_Input(
660
	'ldap_extended_query',
661
	'Query',
662
	'text',
663
	$pconfig['ldap_extended_query']
664
))->setHelp('Example: memberOf=CN=Groupname,OU=MyGroups,DC=example,DC=com');
665

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

    
668
$section->addInput(new Form_Checkbox(
669
	'ldap_anon',
670
	'Bind anonymous',
671
	'Use anonymous binds to resolve distinguished names',
672
	$pconfig['ldap_anon']
673
));
674

    
675
$group = new Form_Group('*Bind credentials');
676
$group->addClass('ldapanon');
677

    
678
$group->add(new Form_Input(
679
	'ldap_binddn',
680
	'User DN:',
681
	'text',
682
	$pconfig['ldap_binddn']
683
));
684

    
685
$group->add(new Form_Input(
686
	'ldap_bindpw',
687
	'Password',
688
	'password',
689
	$pconfig['ldap_bindpw']
690
));
691
$section->add($group);
692

    
693
if (!isset($id)) {
694
	$template_list = array();
695

    
696
	foreach ($ldap_templates as $option => $template) {
697
		$template_list[$option] = $template['desc'];
698
	}
699

    
700
	$section->addInput(new Form_Select(
701
		'ldap_tmpltype',
702
		'Initial Template',
703
		$pconfig['ldap_template'],
704
		$template_list
705
	));
706
}
707

    
708
$section->addInput(new Form_Input(
709
	'ldap_attr_user',
710
	'*User naming attribute',
711
	'text',
712
	$pconfig['ldap_attr_user']
713
));
714

    
715
$section->addInput(new Form_Input(
716
	'ldap_attr_group',
717
	'*Group naming attribute',
718
	'text',
719
	$pconfig['ldap_attr_group']
720
));
721

    
722
$section->addInput(new Form_Input(
723
	'ldap_attr_member',
724
	'*Group member attribute',
725
	'text',
726
	$pconfig['ldap_attr_member']
727
));
728

    
729
$section->addInput(new Form_Checkbox(
730
	'ldap_rfc2307',
731
	'RFC 2307 Groups',
732
	'LDAP Server uses RFC 2307 style group membership',
733
	$pconfig['ldap_rfc2307']
734
))->setHelp('RFC 2307 style group membership has members listed on the group '.
735
	'object rather than using groups listed on user object. Leave unchecked '.
736
	'for Active Directory style group membership (RFC 2307bis).');
737

    
738
$section->addInput(new Form_Input(
739
	'ldap_attr_groupobj',
740
	'Group Object Class',
741
	'text',
742
	$pconfig['ldap_attr_groupobj'],
743
	['placeholder' => 'posixGroup']
744
))->setHelp('Object class used for groups in RFC2307 mode. '.
745
	'Typically "posixGroup" or "group".');
746

    
747
$section->addInput(new Form_Checkbox(
748
	'ldap_utf8',
749
	'UTF8 Encode',
750
	'UTF8 encode LDAP parameters before sending them to the server.',
751
	$pconfig['ldap_utf8']
752
))->setHelp('Required to support international characters, but may not be '.
753
	'supported by every LDAP server.');
754

    
755
$section->addInput(new Form_Checkbox(
756
	'ldap_nostrip_at',
757
	'Username Alterations',
758
	'Do not strip away parts of the username after the @ symbol',
759
	$pconfig['ldap_nostrip_at']
760
))->setHelp('e.g. user@host becomes user when unchecked.');
761

    
762
$form->add($section);
763

    
764
// ==== RADIUS section ========================================================
765
$section = new Form_Section('RADIUS Server Settings');
766
$section->addClass('toggle-radius collapse');
767

    
768
$section->addInput(new Form_Select(
769
	'radius_protocol',
770
	'*Protocol',
771
	$pconfig['radius_protocol'],
772
	$radius_protocol
773
));
774

    
775
$section->addInput(new Form_Input(
776
	'radius_host',
777
	'*Hostname or IP address',
778
	'text',
779
	$pconfig['radius_host']
780
));
781

    
782
$section->addInput(new Form_Input(
783
	'radius_secret',
784
	'*Shared Secret',
785
	'password',
786
	$pconfig['radius_secret']
787
));
788

    
789
$section->addInput(new Form_Select(
790
	'radius_srvcs',
791
	'*Services offered',
792
	$pconfig['radius_srvcs'],
793
	$radius_srvcs
794
));
795

    
796
$section->addInput(new Form_Input(
797
	'radius_auth_port',
798
	'Authentication port',
799
	'number',
800
	$pconfig['radius_auth_port']
801
));
802

    
803
$section->addInput(new Form_Input(
804
	'radius_acct_port',
805
	'Accounting port',
806
	'number',
807
	$pconfig['radius_acct_port']
808
));
809

    
810
$section->addInput(new Form_Input(
811
	'radius_timeout',
812
	'Authentication Timeout',
813
	'number',
814
	$pconfig['radius_timeout']
815
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
816
	'server may take to respond to an authentication request. If left blank, the '.
817
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
818
	'authentication system, increase this timeout to account for how long it will '.
819
	'take the user to receive and enter a token.');
820

    
821
$section->addInput(new Form_Select(
822
	'radius_nasip_attribute',
823
	'RADIUS NAS IP Attribute',
824
	$pconfig['radius_nasip_attribute'],
825
	build_radiusnas_list()
826
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Acccess-Requests.<br />'.
827
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
828

    
829
if (isset($id) && $a_server[$id])
830
{
831
	$section->addInput(new Form_Input(
832
		'id',
833
		null,
834
		'hidden',
835
		$id
836
	));
837
}
838

    
839
$form->add($section);
840

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

    
844
$form->add($modal);
845

    
846
print $form;
847
?>
848
<script type="text/javascript">
849
//<![CDATA[
850
events.push(function() {
851

    
852
	// Create an AJAX request (to this page) to get the container list and controls
853
	function select_clicked() {
854
		if (document.getElementById("ldap_port").value == '' ||
855
			document.getElementById("ldap_host").value == '' ||
856
			document.getElementById("ldap_scope").value == '' ||
857
			document.getElementById("ldap_basedn").value == '' ||
858
			document.getElementById("ldapauthcontainers").value == '') {
859
			alert("<?=gettext("Please fill the required values.");?>");
860
			return;
861
		}
862

    
863
		if (!document.getElementById("ldap_anon").checked) {
864
			if (document.getElementById("ldap_binddn").value == '' ||
865
				document.getElementById("ldap_bindpw").value == '') {
866
				alert("<?=gettext("Please fill the bind username/password.");?>");
867
				return;
868
			}
869
		}
870

    
871
		var ajaxRequest;
872
		var authserver = $('#authmode').val();
873
		var cert;
874

    
875
<?php if (count($a_ca) > 0): ?>
876
			cert = $('#ldap_caref').val();
877
<?php else: ?>
878
			cert = '';
879
<?php endif; ?>
880
/*
881
		$('#containers').modal('show');
882
		$('#serverlist').parent('div').prev('label').remove();
883
		$('#serverlist').parent('div').removeClass("col-sm-10");
884
		$('#serverlist').parent('div').addClass("col-sm-12");
885
*/
886
		ajaxRequest = $.ajax(
887
			{
888
				url: "/system_authservers.php",
889
				type: "post",
890
				data: {
891
					ajax: 	"ajax",
892
					port: 	$('#ldap_port').val(),
893
					host: 	$('#ldap_host').val(),
894
					scope: 	$('#ldap_scope').val(),
895
					basedn: $('#ldap_basedn').val(),
896
					binddn: $('#ldap_binddn').val(),
897
					bindpw: $('#ldap_bindpw').val(),
898
					urltype:$('#ldap_urltype').val(),
899
					proto:  $('#ldap_protver').val(),
900
					authcn: $('#ldapauthcontainers').val(),
901
					cert:   cert
902
				}
903
			}
904
		);
905

    
906
		// Deal with the results of the above ajax call
907
		ajaxRequest.done(function (response, textStatus, jqXHR) {
908
			$('#containers').replaceWith(response);
909

    
910
			$('#containers').modal('show');
911

    
912
			// The button handler needs to be here because until the modal has been populated
913
			// the controls we need to attach handlers to do not exist
914
			$('#svcontbtn').prop("type", "button");
915
			$('#svcontbtn').removeAttr("href");
916

    
917
			$('#svcontbtn').click(function () {
918
				var ous = $('[id^=ou]').length;
919
				var i;
920

    
921
				$('#ldapauthcontainers').val("");
922

    
923
				for (i = 0; i < ous; i++) {
924
					if ($('#ou' + i).prop("checked")) {
925
						if ($('#ldapauthcontainers').val() != "") {
926
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
927
						}
928

    
929
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
930
					}
931
				}
932

    
933
				$('#containers').modal('hide');
934
			});
935
		});
936

    
937
	}
938

    
939
	function set_ldap_port() {
940
		if ($('#ldap_urltype').find(":selected").index() == 2)
941
			$('#ldap_port').val('636');
942
		else
943
			$('#ldap_port').val('389');
944
	}
945

    
946
	function set_required_port_fields() {
947
		if (document.getElementById("radius_srvcs").value == 'auth') {
948
			setRequired('radius_auth_port', true);
949
			setRequired('radius_acct_port', false);
950
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
951
			setRequired('radius_auth_port', false);
952
			setRequired('radius_acct_port', true);
953
		} else { // both
954
			setRequired('radius_auth_port', true);
955
			setRequired('radius_acct_port', true);
956
		}
957
	}
958

    
959
	// Hides all elements of the specified class. This will usually be a section
960
	function hideClass(s_class, hide) {
961
		if (hide)
962
			$('.' + s_class).hide();
963
		else
964
			$('.' + s_class).show();
965
	}
966

    
967
	function ldap_tmplchange() {
968
		switch ($('#ldap_tmpltype').find(":selected").index()) {
969
<?php
970
		$index = 0;
971
		foreach ($ldap_templates as $tmpldata):
972
?>
973
			case <?=$index;?>:
974
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
975
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
976
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
977
				break;
978
<?php
979
			$index++;
980
		endforeach;
981
?>
982
		}
983
	}
984

    
985
	// ---------- On initial page load ------------------------------------------------------------
986

    
987
<?php if ($act != 'edit') : ?>
988
	ldap_tmplchange();
989
<?php endif; ?>
990

    
991
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
992
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
993
	set_required_port_fields();
994

    
995
	if ($('#ldap_port').val() == "")
996
		set_ldap_port();
997

    
998
<?php
999
	if ($act == 'edit') {
1000
?>
1001
		$('#type option:not(:selected)').each(function(){
1002
			$(this).attr('disabled', 'disabled');
1003
		});
1004

    
1005
<?php
1006
		if (!$input_errors) {
1007
?>
1008
		$('#name').prop("readonly", true);
1009
<?php
1010
		}
1011
	}
1012
?>
1013
	// ---------- Click checkbox handlers ---------------------------------------------------------
1014

    
1015
	$('#ldap_tmpltype').on('change', function() {
1016
		ldap_tmplchange();
1017
	});
1018

    
1019
	$('#ldap_anon').click(function () {
1020
		hideClass('ldapanon', this.checked);
1021
	});
1022

    
1023
	$('#ldap_urltype').on('change', function() {
1024
		set_ldap_port();
1025
	});
1026

    
1027
	$('#Select').click(function () {
1028
		select_clicked();
1029
	});
1030

    
1031
	$('#ldap_extended_enabled').click(function () {
1032
		hideClass('extended', !this.checked);
1033
	});
1034

    
1035
	$('#radius_srvcs').on('change', function() {
1036
		set_required_port_fields();
1037
	});
1038

    
1039
});
1040
//]]>
1041
</script>
1042
<?php
1043
include("foot.inc");
(200-200/235)