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 (($pconfig['type'] == "ldap") || ($pconfig['type'] == "radius")) {
279
		$to_field = "{$pconfig['type']}_timeout";
280
		if (isset($_POST[$to_field]) && !empty($_POST[$to_field]) && (!is_numeric($_POST[$to_field]) || (is_numeric($_POST[$to_field]) && ($_POST[$to_field] <= 0)))) {
281
			$input_errors[] = sprintf(gettext("%s Timeout value must be numeric and positive."), strtoupper($pconfig['type']));
282
		}
283
	}
284

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

    
292
	if (!$input_errors) {
293
		$server = array();
294
		$server['refid'] = uniqid();
295
		if (isset($id) && $a_server[$id]) {
296
			$server = $a_server[$id];
297
		}
298

    
299
		$server['type'] = $pconfig['type'];
300
		$server['name'] = $pconfig['name'];
301

    
302
		if ($server['type'] == "ldap") {
303

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

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

    
322
			if ($pconfig['ldap_utf8'] == "yes") {
323
				$server['ldap_utf8'] = true;
324
			} else {
325
				unset($server['ldap_utf8']);
326
			}
327
			if ($pconfig['ldap_nostrip_at'] == "yes") {
328
				$server['ldap_nostrip_at'] = true;
329
			} else {
330
				unset($server['ldap_nostrip_at']);
331
			}
332
			if ($pconfig['ldap_rfc2307'] == "yes") {
333
				$server['ldap_rfc2307'] = true;
334
			} else {
335
				unset($server['ldap_rfc2307']);
336
			}
337

    
338

    
339
			if (!$pconfig['ldap_anon']) {
340
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
341
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
342
			} else {
343
				unset($server['ldap_binddn']);
344
				unset($server['ldap_bindpw']);
345
			}
346

    
347
			if ($pconfig['ldap_timeout']) {
348
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
349
			} else {
350
				$server['ldap_timeout'] = 25;
351
			}
352
		}
353

    
354
		if ($server['type'] == "radius") {
355

    
356
			$server['radius_protocol'] = $pconfig['radius_protocol'];
357
			$server['host'] = $pconfig['radius_host'];
358
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
359

    
360
			if ($pconfig['radius_secret']) {
361
				$server['radius_secret'] = $pconfig['radius_secret'];
362
			}
363

    
364
			if ($pconfig['radius_timeout']) {
365
				$server['radius_timeout'] = $pconfig['radius_timeout'];
366
			} else {
367
				$server['radius_timeout'] = 5;
368
			}
369

    
370
			if ($pconfig['radius_srvcs'] == "both") {
371
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
372
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
373
			}
374

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

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

    
386
		if (isset($id) && $config['system']['authserver'][$id]) {
387
			$config['system']['authserver'][$id] = $server;
388
		} else {
389
			$config['system']['authserver'][] = $server;
390
		}
391

    
392
		write_config();
393

    
394
		pfSenseHeader("system_authservers.php");
395
	}
396
}
397

    
398
function build_radiusnas_list() {
399
	global $config;
400
	$list = array();
401

    
402
	$iflist = get_configured_interface_with_descr();
403
	foreach ($iflist as $ifdesc => $ifdescr) {
404
		$ipaddr = get_interface_ip($ifdesc);
405
		if (is_ipaddr($ipaddr)) {
406
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
407
		}
408
	}
409

    
410
	if (is_array($config['virtualip']['vip'])) {
411
		foreach ($config['virtualip']['vip'] as $sn) {
412
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
413
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
414
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
415
				$len = $end - $start;
416

    
417
				for ($i = 0; $i <= $len; $i++) {
418
					$snip = long2ip32($start+$i);
419
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
420
				}
421
			} else {
422
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
423
			}
424
		}
425
	}
426

    
427
	return($list);
428
}
429

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

    
437
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
438
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
439

    
440
if ($act == "new" || $act == "edit" || $input_errors) {
441
	$pgtitle[] = gettext('Edit');
442
	$pglinks[] = "@self";
443
}
444
$shortcut_section = "authentication";
445
include("head.inc");
446

    
447
if ($input_errors) {
448
	print_input_errors($input_errors);
449
}
450

    
451
if ($savemsg) {
452
	print_info_box($savemsg, 'success');
453
}
454

    
455
$tab_array = array();
456
if (!isAllowedPage("system_usermanager.php")) {
457
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
458
} else {
459
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
460
}
461
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
462
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
463
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
464
display_top_tabs($tab_array);
465

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
623

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
936
	}
937

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

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

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

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

    
984
	// ---------- On initial page load ------------------------------------------------------------
985

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

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

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

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

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

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

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

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

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

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

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

    
1038
});
1039
//]]>
1040
</script>
1041
<?php
1042
include("foot.inc");
(190-190/225)