Project

General

Profile

Download (27.8 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-2018 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

    
109
if (!is_array($config['ca'])) {
110
	$config['ca'] = array();
111
}
112

    
113
$a_ca =& $config['ca'];
114

    
115
$act = $_REQUEST['act'];
116

    
117
if ($_POST['act'] == "del") {
118

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

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

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

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

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

    
143
		$pconfig['type'] = $a_server[$id]['type'];
144
		$pconfig['name'] = $a_server[$id]['name'];
145

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

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

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

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

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

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

    
199
		}
200
	}
201
}
202

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

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

    
216
	/* input validation */
217

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
342

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

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

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

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

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

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

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

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

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

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

    
396
		write_config();
397

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

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

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

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

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

    
431
	return($list);
432
}
433

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

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

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

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

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

    
459
$tab_array = array();
460
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
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
	$section->addInput(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");
(199-199/234)