Project

General

Profile

Download (28.4 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-2019 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_rfc2307'] = isset($a_server[$id]['ldap_rfc2307']);
165

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

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

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

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

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

    
197
		}
198
	}
199
}
200

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

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

    
214
	/* input validation */
215

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
345

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

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

    
361
		if ($server['type'] == "radius") {
362

    
363
			$server['radius_protocol'] = $pconfig['radius_protocol'];
364
			$server['host'] = $pconfig['radius_host'];
365
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
366

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

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

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

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

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

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

    
399
		write_config();
400

    
401
		pfSenseHeader("system_authservers.php");
402
	}
403
}
404

    
405
function build_radiusnas_list() {
406
	global $config;
407
	$list = array();
408

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

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

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

    
434
	return($list);
435
}
436

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

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

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

    
454
if ($input_errors) {
455
	print_input_errors($input_errors);
456
}
457

    
458
if ($savemsg) {
459
	print_info_box($savemsg, 'success');
460
}
461

    
462
$tab_array = array();
463
if (!isAllowedPage("system_usermanager.php")) {
464
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
465
} else {
466
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
467
}
468
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
469
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
470
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
471
display_top_tabs($tab_array);
472

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

    
508
<nav class="action-buttons">
509
	<a href="?act=new" class="btn btn-success btn-sm">
510
		<i class="fa fa-plus icon-embed-btn"></i>
511
		<?=gettext("Add")?>
512
	</a>
513
</nav>
514
<?php
515
	include("foot.inc");
516
	exit;
517
}
518

    
519
$form = new Form;
520
$form->setAction('system_authservers.php?act=edit');
521

    
522
$form->addGlobal(new Form_Input(
523
	'userid',
524
	null,
525
	'hidden',
526
	$id
527
));
528

    
529
$section = new Form_Section('Server Settings');
530

    
531
$section->addInput($input = new Form_Input(
532
	'name',
533
	'*Descriptive name',
534
	'text',
535
	$pconfig['name']
536
));
537

    
538
$section->addInput($input = new Form_Select(
539
	'type',
540
	'*Type',
541
	$pconfig['type'],
542
	$auth_server_types
543
))->toggles();
544

    
545
$form->add($section);
546

    
547
// ==== LDAP settings =========================================================
548
$section = new Form_Section('LDAP Server Settings');
549
$section->addClass('toggle-ldap collapse');
550

    
551
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
552
	$section->addClass('in');
553

    
554
$section->addInput(new Form_Input(
555
	'ldap_host',
556
	'*Hostname or IP address',
557
	'text',
558
	$pconfig['ldap_host']
559
))->setHelp('NOTE: When using SSL or STARTTLS, this hostname MUST match the Common Name '.
560
	'(CN) of the LDAP server\'s SSL Certificate.');
561

    
562
$section->addInput(new Form_Input(
563
	'ldap_port',
564
	'*Port value',
565
	'number',
566
	$pconfig['ldap_port']
567
));
568

    
569
$section->addInput(new Form_Select(
570
	'ldap_urltype',
571
	'*Transport',
572
	$pconfig['ldap_urltype'],
573
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
574
));
575

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

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

    
599
$section->addInput(new Form_Select(
600
	'ldap_protver',
601
	'*Protocol version',
602
	$pconfig['ldap_protver'],
603
	array_combine($ldap_protvers, $ldap_protvers)
604
));
605

    
606
$section->addInput(new Form_Input(
607
	'ldap_timeout',
608
	'Server Timeout',
609
	'number',
610
	$pconfig['ldap_timeout'],
611
	['placeholder' => 25]
612
))->setHelp('Timeout for LDAP operations (seconds)');
613

    
614
$group = new Form_Group('Search scope');
615

    
616
$SSF = new Form_Select(
617
	'ldap_scope',
618
	'*Level',
619
	$pconfig['ldap_scope'],
620
	$ldap_scopes
621
);
622

    
623
$SSB = new Form_Input(
624
	'ldap_basedn',
625
	'Base DN',
626
	'text',
627
	$pconfig['ldap_basedn']
628
);
629

    
630

    
631
$section->addInput(new Form_StaticText(
632
	'Search scope',
633
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
634
));
635

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

    
646
$group->add(new Form_Button(
647
	'Select',
648
	'Select a container',
649
	null,
650
	'fa-search'
651
))->setAttribute('type','button')->addClass('btn-info');
652

    
653
$section->add($group);
654

    
655
$section->addInput(new Form_Checkbox(
656
	'ldap_extended_enabled',
657
	'Extended query',
658
	'Enable extended query',
659
	$pconfig['ldap_extended_enabled']
660
));
661

    
662
$group = new Form_Group('Query');
663
$group->addClass('extended');
664

    
665
$group->add(new Form_Input(
666
	'ldap_extended_query',
667
	'Query',
668
	'text',
669
	$pconfig['ldap_extended_query']
670
))->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=*))');
671

    
672
$section->add($group);
673

    
674
$section->addInput(new Form_Checkbox(
675
	'ldap_anon',
676
	'Bind anonymous',
677
	'Use anonymous binds to resolve distinguished names',
678
	$pconfig['ldap_anon']
679
));
680

    
681
$group = new Form_Group('*Bind credentials');
682
$group->addClass('ldapanon');
683

    
684
$group->add(new Form_Input(
685
	'ldap_binddn',
686
	'User DN:',
687
	'text',
688
	$pconfig['ldap_binddn']
689
));
690

    
691
$group->add(new Form_Input(
692
	'ldap_bindpw',
693
	'Password',
694
	'password',
695
	$pconfig['ldap_bindpw']
696
));
697
$section->add($group);
698

    
699
if (!isset($id)) {
700
	$template_list = array();
701

    
702
	foreach ($ldap_templates as $option => $template) {
703
		$template_list[$option] = $template['desc'];
704
	}
705

    
706
	$section->addInput(new Form_Select(
707
		'ldap_tmpltype',
708
		'Initial Template',
709
		$pconfig['ldap_template'],
710
		$template_list
711
	));
712
}
713

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

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

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

    
735
$section->addInput(new Form_Checkbox(
736
	'ldap_rfc2307',
737
	'RFC 2307 Groups',
738
	'LDAP Server uses RFC 2307 style group membership',
739
	$pconfig['ldap_rfc2307']
740
))->setHelp('RFC 2307 style group membership has members listed on the group '.
741
	'object rather than using groups listed on user object. Leave unchecked '.
742
	'for Active Directory style group membership (RFC 2307bis).');
743

    
744
$section->addInput(new Form_Input(
745
	'ldap_attr_groupobj',
746
	'Group Object Class',
747
	'text',
748
	$pconfig['ldap_attr_groupobj'],
749
	['placeholder' => 'posixGroup']
750
))->setHelp('Object class used for groups in RFC2307 mode. '.
751
	'Typically "posixGroup" or "group".');
752

    
753
$section->addInput(new Form_Checkbox(
754
	'ldap_utf8',
755
	'UTF8 Encode',
756
	'UTF8 encode LDAP parameters before sending them to the server.',
757
	$pconfig['ldap_utf8']
758
))->setHelp('Required to support international characters, but may not be '.
759
	'supported by every LDAP server.');
760

    
761
$section->addInput(new Form_Checkbox(
762
	'ldap_nostrip_at',
763
	'Username Alterations',
764
	'Do not strip away parts of the username after the @ symbol',
765
	$pconfig['ldap_nostrip_at']
766
))->setHelp('e.g. user@host becomes user when unchecked.');
767

    
768
$form->add($section);
769

    
770
// ==== RADIUS section ========================================================
771
$section = new Form_Section('RADIUS Server Settings');
772
$section->addClass('toggle-radius collapse');
773

    
774
$section->addInput(new Form_Select(
775
	'radius_protocol',
776
	'*Protocol',
777
	$pconfig['radius_protocol'],
778
	$radius_protocol
779
));
780

    
781
$section->addInput(new Form_Input(
782
	'radius_host',
783
	'*Hostname or IP address',
784
	'text',
785
	$pconfig['radius_host']
786
));
787

    
788
$section->addInput(new Form_Input(
789
	'radius_secret',
790
	'*Shared Secret',
791
	'password',
792
	$pconfig['radius_secret']
793
));
794

    
795
$section->addInput(new Form_Select(
796
	'radius_srvcs',
797
	'*Services offered',
798
	$pconfig['radius_srvcs'],
799
	$radius_srvcs
800
));
801

    
802
$section->addInput(new Form_Input(
803
	'radius_auth_port',
804
	'Authentication port',
805
	'number',
806
	$pconfig['radius_auth_port']
807
));
808

    
809
$section->addInput(new Form_Input(
810
	'radius_acct_port',
811
	'Accounting port',
812
	'number',
813
	$pconfig['radius_acct_port']
814
));
815

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

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

    
835
if (isset($id) && $a_server[$id])
836
{
837
	$form->addGlobal(new Form_Input(
838
		'id',
839
		null,
840
		'hidden',
841
		$id
842
	));
843
}
844

    
845
$form->add($section);
846

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

    
850
$form->add($modal);
851

    
852
print $form;
853
?>
854
<script type="text/javascript">
855
//<![CDATA[
856
events.push(function() {
857

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

    
869
		if (!document.getElementById("ldap_anon").checked) {
870
			if (document.getElementById("ldap_binddn").value == '' ||
871
				document.getElementById("ldap_bindpw").value == '') {
872
				alert("<?=gettext("Please fill the bind username/password.");?>");
873
				return;
874
			}
875
		}
876

    
877
		var ajaxRequest;
878
		var authserver = $('#authmode').val();
879
		var cert;
880

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

    
912
		// Deal with the results of the above ajax call
913
		ajaxRequest.done(function (response, textStatus, jqXHR) {
914
			$('#containers').replaceWith(response);
915

    
916
			$('#containers').modal('show');
917

    
918
			// The button handler needs to be here because until the modal has been populated
919
			// the controls we need to attach handlers to do not exist
920
			$('#svcontbtn').prop("type", "button");
921
			$('#svcontbtn').removeAttr("href");
922

    
923
			$('#svcontbtn').click(function () {
924
				var ous = $('[id^=ou]').length;
925
				var i;
926

    
927
				$('#ldapauthcontainers').val("");
928

    
929
				for (i = 0; i < ous; i++) {
930
					if ($('#ou' + i).prop("checked")) {
931
						if ($('#ldapauthcontainers').val() != "") {
932
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
933
						}
934

    
935
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
936
					}
937
				}
938

    
939
				$('#containers').modal('hide');
940
			});
941
		});
942

    
943
	}
944

    
945
	function set_ldap_port() {
946
		if ($('#ldap_urltype').find(":selected").index() == 2)
947
			$('#ldap_port').val('636');
948
		else
949
			$('#ldap_port').val('389');
950
	}
951

    
952
	function set_required_port_fields() {
953
		if (document.getElementById("radius_srvcs").value == 'auth') {
954
			setRequired('radius_auth_port', true);
955
			setRequired('radius_acct_port', false);
956
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
957
			setRequired('radius_auth_port', false);
958
			setRequired('radius_acct_port', true);
959
		} else { // both
960
			setRequired('radius_auth_port', true);
961
			setRequired('radius_acct_port', true);
962
		}
963
	}
964

    
965
	// Hides all elements of the specified class. This will usually be a section
966
	function hideClass(s_class, hide) {
967
		if (hide)
968
			$('.' + s_class).hide();
969
		else
970
			$('.' + s_class).show();
971
	}
972

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

    
991
	// ---------- On initial page load ------------------------------------------------------------
992

    
993
<?php if ($act != 'edit') : ?>
994
	ldap_tmplchange();
995
<?php endif; ?>
996

    
997
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
998
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
999
	set_required_port_fields();
1000

    
1001
	if ($('#ldap_port').val() == "")
1002
		set_ldap_port();
1003

    
1004
<?php
1005
	if ($act == 'edit') {
1006
?>
1007
		$('#type option:not(:selected)').each(function(){
1008
			$(this).attr('disabled', 'disabled');
1009
		});
1010

    
1011
<?php
1012
		if (!$input_errors) {
1013
?>
1014
		$('#name').prop("readonly", true);
1015
<?php
1016
		}
1017
	}
1018
?>
1019
	// ---------- Click checkbox handlers ---------------------------------------------------------
1020

    
1021
	$('#ldap_tmpltype').on('change', function() {
1022
		ldap_tmplchange();
1023
	});
1024

    
1025
	$('#ldap_anon').click(function () {
1026
		hideClass('ldapanon', this.checked);
1027
	});
1028

    
1029
	$('#ldap_urltype').on('change', function() {
1030
		set_ldap_port();
1031
	});
1032

    
1033
	$('#Select').click(function () {
1034
		select_clicked();
1035
	});
1036

    
1037
	$('#ldap_extended_enabled').click(function () {
1038
		hideClass('extended', !this.checked);
1039
	});
1040

    
1041
	$('#radius_srvcs').on('change', function() {
1042
		set_required_port_fields();
1043
	});
1044

    
1045
});
1046
//]]>
1047
</script>
1048
<?php
1049
include("foot.inc");
(191-191/227)