Project

General

Profile

Download (25.7 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-2016 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

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

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

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

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

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

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

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

    
71
				$idx++;
72
			}
73
		}
74

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

    
77
		// Create a "Save button"
78

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

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

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

    
93
		print($modal);
94
	}
95

    
96
	exit;
97
}
98

    
99
if (is_numericint($_GET['id'])) {
100
	$id = $_GET['id'];
101
}
102

    
103
if (isset($_POST['id']) && is_numericint($_POST['id'])) {
104
	$id = $_POST['id'];
105
}
106

    
107
if (!is_array($config['system']['authserver'])) {
108
	$config['system']['authserver'] = array();
109
}
110

    
111
$a_servers = auth_get_authserver_list();
112
foreach ($a_servers as $servers) {
113
	$a_server[] = $servers;
114
}
115

    
116
if (!is_array($config['ca'])) {
117
	$config['ca'] = array();
118
}
119
$a_ca =& $config['ca'];
120

    
121
$act = $_GET['act'];
122
if ($_POST['act']) {
123
	$act = $_POST['act'];
124
}
125

    
126
if ($act == "del") {
127

    
128
	if (!$a_server[$_GET['id']]) {
129
		pfSenseHeader("system_authservers.php");
130
		exit;
131
	}
132

    
133
	/* Remove server from main list. */
134
	$serverdeleted = $a_server[$_GET['id']]['name'];
135
	foreach ($config['system']['authserver'] as $k => $as) {
136
		if ($config['system']['authserver'][$k]['name'] == $serverdeleted) {
137
			unset($config['system']['authserver'][$k]);
138
		}
139
	}
140

    
141
	/* Remove server from temp list used later on this page. */
142
	unset($a_server[$_GET['id']]);
143

    
144
	$savemsg = sprintf(gettext("Authentication Server %s deleted."), htmlspecialchars($serverdeleted));
145
	write_config($savemsg);
146
}
147

    
148
if ($act == "edit") {
149
	if (isset($id) && $a_server[$id]) {
150

    
151
		$pconfig['type'] = $a_server[$id]['type'];
152
		$pconfig['name'] = $a_server[$id]['name'];
153

    
154
		if ($pconfig['type'] == "ldap") {
155
			$pconfig['ldap_caref'] = $a_server[$id]['ldap_caref'];
156
			$pconfig['ldap_host'] = $a_server[$id]['host'];
157
			$pconfig['ldap_port'] = $a_server[$id]['ldap_port'];
158
			$pconfig['ldap_timeout'] = $a_server[$id]['ldap_timeout'];
159
			$pconfig['ldap_urltype'] = $a_server[$id]['ldap_urltype'];
160
			$pconfig['ldap_protver'] = $a_server[$id]['ldap_protver'];
161
			$pconfig['ldap_scope'] = $a_server[$id]['ldap_scope'];
162
			$pconfig['ldap_basedn'] = $a_server[$id]['ldap_basedn'];
163
			$pconfig['ldap_authcn'] = $a_server[$id]['ldap_authcn'];
164
			$pconfig['ldap_extended_enabled'] = $a_server[$id]['ldap_extended_enabled'];
165
			$pconfig['ldap_extended_query'] = $a_server[$id]['ldap_extended_query'];
166
			$pconfig['ldap_binddn'] = $a_server[$id]['ldap_binddn'];
167
			$pconfig['ldap_bindpw'] = $a_server[$id]['ldap_bindpw'];
168
			$pconfig['ldap_attr_user'] = $a_server[$id]['ldap_attr_user'];
169
			$pconfig['ldap_attr_group'] = $a_server[$id]['ldap_attr_group'];
170
			$pconfig['ldap_attr_member'] = $a_server[$id]['ldap_attr_member'];
171
			$pconfig['ldap_attr_groupobj'] = $a_server[$id]['ldap_attr_groupobj'];
172
			$pconfig['ldap_utf8'] = isset($a_server[$id]['ldap_utf8']);
173
			$pconfig['ldap_nostrip_at'] = isset($a_server[$id]['ldap_nostrip_at']);
174
			$pconfig['ldap_rfc2307'] = isset($a_server[$id]['ldap_rfc2307']);
175

    
176
			if (!$pconfig['ldap_binddn'] || !$pconfig['ldap_bindpw']) {
177
				$pconfig['ldap_anon'] = true;
178
			}
179
		}
180

    
181
		if ($pconfig['type'] == "radius") {
182
			$pconfig['radius_protocol'] = $a_server[$id]['radius_protocol'];
183
			$pconfig['radius_host'] = $a_server[$id]['host'];
184
			$pconfig['radius_auth_port'] = $a_server[$id]['radius_auth_port'];
185
			$pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'];
186
			$pconfig['radius_secret'] = $a_server[$id]['radius_secret'];
187
			$pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'];
188

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

    
194
			if ($pconfig['radius_auth_port'] &&
195
				!$pconfig['radius_acct_port']) {
196
				$pconfig['radius_srvcs'] = "auth";
197
				$pconfig['radius_acct_port'] = 1813;
198
			}
199

    
200
			if (!$pconfig['radius_auth_port'] &&
201
				$pconfig['radius_acct_port']) {
202
				$pconfig['radius_srvcs'] = "acct";
203
				$pconfig['radius_auth_port'] = 1812;
204
			}
205

    
206
		}
207
	}
208
}
209

    
210
if ($act == "new") {
211
	$pconfig['ldap_protver'] = 3;
212
	$pconfig['ldap_anon'] = true;
213
	$pconfig['radius_protocol'] = "MSCHAPv2";
214
	$pconfig['radius_srvcs'] = "both";
215
	$pconfig['radius_auth_port'] = "1812";
216
	$pconfig['radius_acct_port'] = "1813";
217
}
218

    
219
if ($_POST) {
220
	unset($input_errors);
221
	$pconfig = $_POST;
222

    
223
	/* input validation */
224

    
225
	if ($pconfig['type'] == "ldap") {
226
		$reqdfields = explode(" ",
227
			"name type ldap_host ldap_port " .
228
			"ldap_urltype ldap_protver ldap_scope " .
229
			"ldap_attr_user ldap_attr_group ldap_attr_member ldapauthcontainers");
230

    
231
		$reqdfieldsn = array(
232
			gettext("Descriptive name"),
233
			gettext("Type"),
234
			gettext("Hostname or IP"),
235
			gettext("Port value"),
236
			gettext("Transport"),
237
			gettext("Protocol version"),
238
			gettext("Search level"),
239
			gettext("User naming Attribute"),
240
			gettext("Group naming Attribute"),
241
			gettext("Group member attribute"),
242
			gettext("Authentication container"));
243

    
244
		if (!$pconfig['ldap_anon']) {
245
			$reqdfields[] = "ldap_binddn";
246
			$reqdfields[] = "ldap_bindpw";
247
			$reqdfieldsn[] = gettext("Bind user DN");
248
			$reqdfieldsn[] = gettext("Bind Password");
249
		}
250
	}
251

    
252
	if ($pconfig['type'] == "radius") {
253
		$reqdfields = explode(" ", "name type radius_protocol radius_host radius_srvcs");
254
		$reqdfieldsn = array(
255
			gettext("Descriptive name"),
256
			gettext("Type"),
257
			gettext("Radius Protocol"),
258
			gettext("Hostname or IP"),
259
			gettext("Services"));
260

    
261
		if ($pconfig['radius_srvcs'] == "both" ||
262
			$pconfig['radius_srvcs'] == "auth") {
263
			$reqdfields[] = "radius_auth_port";
264
			$reqdfieldsn[] = gettext("Authentication port");
265
		}
266

    
267
		if ($pconfig['radius_srvcs'] == "both" ||
268
			$pconfig['radius_srvcs'] == "acct") {
269
			$reqdfields[] = "radius_acct_port";
270
			$reqdfieldsn[] = gettext("Accounting port");
271
		}
272

    
273
		if (!isset($id)) {
274
			$reqdfields[] = "radius_secret";
275
			$reqdfieldsn[] = gettext("Shared Secret");
276
		}
277
	}
278

    
279
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
280

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

    
285
	if (auth_get_authserver($pconfig['name']) && !isset($id)) {
286
		$input_errors[] = gettext("An authentication server with the same name already exists.");
287
	}
288

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

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

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

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

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

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

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

    
395
		write_config();
396

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

    
401
// On error, restore the form contents so the user doesn't have to re-enter too much
402
if ($_POST && $input_errors) {
403
	$pconfig = $_POST;
404
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
405
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
406
}
407

    
408
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
409
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
410

    
411
if ($act == "new" || $act == "edit" || $input_errors) {
412
	$pgtitle[] = gettext('Edit');
413
	$pglinks[] = "@self";
414
}
415
$shortcut_section = "authentication";
416
include("head.inc");
417

    
418
if ($input_errors) {
419
	print_input_errors($input_errors);
420
}
421

    
422
if ($savemsg) {
423
	print_info_box($savemsg, 'success');
424
}
425

    
426
$tab_array = array();
427
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
428
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
429
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
430
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
431
display_top_tabs($tab_array);
432

    
433
if (!($act == "new" || $act == "edit" || $input_errors)) {
434
?>
435
<div class="panel panel-default">
436
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Authentication Servers')?></h2></div>
437
	<div class="panel-body">
438
		<div class="table-responsive">
439
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
440
				<thead>
441
					<tr>
442
						<th><?=gettext("Server Name")?></th>
443
						<th><?=gettext("Type")?></th>
444
						<th><?=gettext("Host Name")?></th>
445
						<th><?=gettext("Actions")?></th>
446
					</tr>
447
				</thead>
448
				<tbody>
449
			<?php foreach ($a_server as $i => $server): ?>
450
					<tr>
451
						<td><?=htmlspecialchars($server['name'])?></td>
452
						<td><?=htmlspecialchars($auth_server_types[$server['type']])?></td>
453
						<td><?=htmlspecialchars($server['host'])?></td>
454
						<td>
455
						<?php if ($i < (count($a_server) - 1)): ?>
456
							<a class="fa fa-pencil" title="<?=gettext("Edit server"); ?>" href="system_authservers.php?act=edit&amp;id=<?=$i?>"></a>
457
							<a class="fa fa-trash"  title="<?=gettext("Delete server")?>" href="system_authservers.php?act=del&amp;id=<?=$i?>"></a>
458
						<?php endif?>
459
						</td>
460
					</tr>
461
			<?php endforeach; ?>
462
				</tbody>
463
			</table>
464
		</div>
465
	</div>
466
</div>
467

    
468
<nav class="action-buttons">
469
	<a href="?act=new" class="btn btn-success btn-sm">
470
		<i class="fa fa-plus icon-embed-btn"></i>
471
		<?=gettext("Add")?>
472
	</a>
473
</nav>
474
<?php
475
	include("foot.inc");
476
	exit;
477
}
478

    
479
$form = new Form;
480
$form->setAction('system_authservers.php?act=edit');
481

    
482
$form->addGlobal(new Form_Input(
483
	'userid',
484
	null,
485
	'hidden',
486
	$id
487
));
488

    
489
$section = new Form_Section('Server Settings');
490

    
491
$section->addInput($input = new Form_Input(
492
	'name',
493
	'Descriptive name',
494
	'text',
495
	$pconfig['name']
496
));
497

    
498
$section->addInput($input = new Form_Select(
499
	'type',
500
	'Type',
501
	$pconfig['type'],
502
	$auth_server_types
503
))->toggles();
504

    
505
$form->add($section);
506

    
507
// ==== LDAP settings =========================================================
508
$section = new Form_Section('LDAP Server Settings');
509
$section->addClass('toggle-ldap collapse');
510

    
511
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
512
	$section->addClass('in');
513

    
514
$section->addInput(new Form_Input(
515
	'ldap_host',
516
	'Hostname or IP address',
517
	'text',
518
	$pconfig['ldap_host']
519
))->setHelp('NOTE: When using SSL or STARTTLS, this hostname MUST match the Common Name '.
520
	'(CN) of the LDAP server\'s SSL Certificate.');
521

    
522
$section->addInput(new Form_Input(
523
	'ldap_port',
524
	'Port value',
525
	'number',
526
	$pconfig['ldap_port']
527
));
528

    
529
$section->addInput(new Form_Select(
530
	'ldap_urltype',
531
	'Transport',
532
	$pconfig['ldap_urltype'],
533
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
534
));
535

    
536
if (empty($a_ca))
537
{
538
	$section->addInput(new Form_StaticText(
539
		'Peer Certificate Authority',
540
		'No Certificate Authorities defined.<br/>Create one under <a href="system_camanager.php">System &gt; Cert. Manager</a>.'
541
	));
542
}
543
else
544
{
545
	$ldapCaRef = [];
546
	foreach ($a_ca as $ca)
547
		$ldapCaRef[ $ca['refid'] ] = $ca['descr'];
548

    
549
	$section->addInput(new Form_Select(
550
		'ldap_caref',
551
		'Peer Certificate Authority',
552
		$pconfig['ldap_caref'],
553
		$ldapCaRef
554
	))->setHelp('This option is used if \'SSL Encrypted\' '.
555
		'or \'TCP - STARTTLS\' options are chosen. '.
556
		'It must match with the CA in the AD otherwise problems will arise.');
557
}
558

    
559
$section->addInput(new Form_Select(
560
	'ldap_protver',
561
	'Protocol version',
562
	$pconfig['ldap_protver'],
563
	array_combine($ldap_protvers, $ldap_protvers)
564
));
565

    
566
$section->addInput(new Form_Input(
567
	'ldap_timeout',
568
	'Server Timeout',
569
	'number',
570
	$pconfig['ldap_timeout'],
571
	['placeholder' => 25]
572
))->setHelp('Timeout for LDAP operations (seconds)');
573

    
574
$group = new Form_Group('Search scope');
575

    
576
$SSF = new Form_Select(
577
	'ldap_scope',
578
	'Level',
579
	$pconfig['ldap_scope'],
580
	$ldap_scopes
581
);
582

    
583
$SSB = new Form_Input(
584
	'ldap_basedn',
585
	'Base DN',
586
	'text',
587
	$pconfig['ldap_basedn']
588
);
589

    
590

    
591
$section->addInput(new Form_StaticText(
592
	'Search scope',
593
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
594
));
595

    
596
$group = new Form_Group('Authentication containers');
597
$group->add(new Form_Input(
598
	'ldapauthcontainers',
599
	'Containers',
600
	'text',
601
	$pconfig['ldap_authcn']
602
))->setHelp('Note: Semi-Colon separated. This will be prepended to the search '.
603
	'base dn above or the full container path can be specified containing a dc= '.
604
	'component.<br/>Example: CN=Users;DC=example,DC=com or OU=Staff;OU=Freelancers');
605

    
606
$group->add(new Form_Button(
607
	'Select',
608
	'Select a container',
609
	null,
610
	'fa-search'
611
))->setAttribute('type','button')->addClass('btn-info');
612

    
613
$section->add($group);
614

    
615
$section->addInput(new Form_Checkbox(
616
	'ldap_extended_enabled',
617
	'Extended query',
618
	'Enable extended query',
619
	$pconfig['ldap_extended_enabled']
620
));
621

    
622
$group = new Form_Group('Query');
623
$group->addClass('extended');
624

    
625
$group->add(new Form_Input(
626
	'ldap_extended_query',
627
	'Query',
628
	'text',
629
	$pconfig['ldap_extended_query']
630
))->setHelp('Example: &amp;(objectClass=inetOrgPerson)(mail=*@example.com)');
631

    
632
$section->add($group);
633

    
634
$section->addInput(new Form_Checkbox(
635
	'ldap_anon',
636
	'Bind anonymous',
637
	'Use anonymous binds to resolve distinguished names',
638
	$pconfig['ldap_anon']
639
));
640

    
641
$group = new Form_Group('Bind credentials');
642
$group->addClass('ldapanon');
643

    
644
$group->add(new Form_Input(
645
	'ldap_binddn',
646
	'User DN:',
647
	'text',
648
	$pconfig['ldap_binddn']
649
));
650

    
651
$group->add(new Form_Input(
652
	'ldap_bindpw',
653
	'Password',
654
	'password',
655
	$pconfig['ldap_bindpw']
656
));
657
$section->add($group);
658

    
659
if (!isset($id)) {
660
	$template_list = array();
661

    
662
	foreach ($ldap_templates as $option => $template) {
663
		$template_list[$option] = $template['desc'];
664
	}
665

    
666
	$section->addInput(new Form_Select(
667
		'ldap_tmpltype',
668
		'Initial Template',
669
		$pconfig['ldap_template'],
670
		$template_list
671
	));
672
}
673

    
674
$section->addInput(new Form_Input(
675
	'ldap_attr_user',
676
	'User naming attribute',
677
	'text',
678
	$pconfig['ldap_attr_user']
679
));
680

    
681
$section->addInput(new Form_Input(
682
	'ldap_attr_group',
683
	'Group naming attribute',
684
	'text',
685
	$pconfig['ldap_attr_group']
686
));
687

    
688
$section->addInput(new Form_Input(
689
	'ldap_attr_member',
690
	'Group member attribute',
691
	'text',
692
	$pconfig['ldap_attr_member']
693
));
694

    
695
$section->addInput(new Form_Checkbox(
696
	'ldap_rfc2307',
697
	'RFC 2307 Groups',
698
	'LDAP Server uses RFC 2307 style group membership',
699
	$pconfig['ldap_rfc2307']
700
))->setHelp('RFC 2307 style group membership has members listed on the group '.
701
	'object rather than using groups listed on user object. Leave unchecked '.
702
	'for Active Directory style group membership (RFC 2307bis).');
703

    
704
$section->addInput(new Form_Input(
705
	'ldap_attr_groupobj',
706
	'Group Object Class',
707
	'text',
708
	$pconfig['ldap_attr_groupobj'],
709
	['placeholder' => 'posixGroup']
710
))->setHelp('Object class used for groups in RFC2307 mode. '.
711
	'Typically "posixGroup" or "group".');
712

    
713
$section->addInput(new Form_Checkbox(
714
	'ldap_utf8',
715
	'UTF8 Encode',
716
	'UTF8 encode LDAP parameters before sending them to the server.',
717
	$pconfig['ldap_utf8']
718
))->setHelp('Required to support international characters, but may not be '.
719
	'supported by every LDAP server.');
720

    
721
$section->addInput(new Form_Checkbox(
722
	'ldap_nostrip_at',
723
	'Username Alterations',
724
	'Do not strip away parts of the username after the @ symbol',
725
	$pconfig['ldap_nostrip_at']
726
))->setHelp('e.g. user@host becomes user when unchecked.');
727

    
728
$form->add($section);
729

    
730
// ==== RADIUS section ========================================================
731
$section = new Form_Section('RADIUS Server Settings');
732
$section->addClass('toggle-radius collapse');
733

    
734
$section->addInput(new Form_Select(
735
	'radius_protocol',
736
	'Protocol',
737
	$pconfig['radius_protocol'],
738
	$radius_protocol
739
));
740

    
741
$section->addInput(new Form_Input(
742
	'radius_host',
743
	'Hostname or IP address',
744
	'text',
745
	$pconfig['radius_host']
746
));
747

    
748
$section->addInput(new Form_Input(
749
	'radius_secret',
750
	'Shared Secret',
751
	'password',
752
	$pconfig['radius_secret']
753
));
754

    
755
$section->addInput(new Form_Select(
756
	'radius_srvcs',
757
	'Services offered',
758
	$pconfig['radius_srvcs'],
759
	$radius_srvcs
760
));
761

    
762
$section->addInput(new Form_Input(
763
	'radius_auth_port',
764
	'Authentication port',
765
	'number',
766
	$pconfig['radius_auth_port']
767
));
768

    
769
$section->addInput(new Form_Input(
770
	'radius_acct_port',
771
	'Accounting port',
772
	'number',
773
	$pconfig['radius_acct_port']
774
));
775

    
776
$section->addInput(new Form_Input(
777
	'radius_timeout',
778
	'Authentication Timeout',
779
	'number',
780
	$pconfig['radius_timeout']
781
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
782
	'server may take to respond to an authentication request. If left blank, the '.
783
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
784
	'authentication system, increase this timeout to account for how long it will '.
785
	'take the user to receive and enter a token.');
786

    
787
if (isset($id) && $a_server[$id])
788
{
789
	$section->addInput(new Form_Input(
790
		'id',
791
		null,
792
		'hidden',
793
		$id
794
	));
795
}
796

    
797
$form->add($section);
798

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

    
802
$form->add($modal);
803

    
804
print $form;
805
?>
806
<script type="text/javascript">
807
//<![CDATA[
808
events.push(function() {
809

    
810
	// Create an AJAX request (to this page) to get the container list and controls
811
	function select_clicked() {
812
		if (document.getElementById("ldap_port").value == '' ||
813
			document.getElementById("ldap_host").value == '' ||
814
			document.getElementById("ldap_scope").value == '' ||
815
			document.getElementById("ldap_basedn").value == '' ||
816
			document.getElementById("ldapauthcontainers").value == '') {
817
			alert("<?=gettext("Please fill the required values.");?>");
818
			return;
819
		}
820

    
821
		if (!document.getElementById("ldap_anon").checked) {
822
			if (document.getElementById("ldap_binddn").value == '' ||
823
				document.getElementById("ldap_bindpw").value == '') {
824
				alert("<?=gettext("Please fill the bind username/password.");?>");
825
				return;
826
			}
827
		}
828

    
829
		var ajaxRequest;
830
		var authserver = $('#authmode').val();
831
		var cert;
832

    
833
<?php if (count($a_ca) > 0): ?>
834
			cert = $('#ldap_caref').val();
835
<?php else: ?>
836
			cert = '';
837
<?php endif; ?>
838
/*
839
		$('#containers').modal('show');
840
		$('#serverlist').parent('div').prev('label').remove();
841
		$('#serverlist').parent('div').removeClass("col-sm-10");
842
		$('#serverlist').parent('div').addClass("col-sm-12");
843
*/
844
		ajaxRequest = $.ajax(
845
			{
846
				url: "/system_authservers.php",
847
				type: "post",
848
				data: {
849
					ajax: 	"ajax",
850
					port: 	$('#ldap_port').val(),
851
					host: 	$('#ldap_host').val(),
852
					scope: 	$('#ldap_scope').val(),
853
					basedn: $('#ldap_basedn').val(),
854
					binddn: $('#ldap_binddn').val(),
855
					bindpw: $('#ldap_bindpw').val(),
856
					urltype:$('#ldap_urltype').val(),
857
					proto:  $('#ldap_protver').val(),
858
					authcn: $('#ldapauthcontainers').val(),
859
					cert:   cert
860
				}
861
			}
862
		);
863

    
864
		// Deal with the results of the above ajax call
865
		ajaxRequest.done(function (response, textStatus, jqXHR) {
866
			$('#containers').replaceWith(response);
867

    
868
			$('#containers').modal('show');
869

    
870
			// The button handler needs to be here because until the modal has been populated
871
			// the controls we need to attach handlers to do not exist
872
			$('#svcontbtn').prop("type", "button");
873
			$('#svcontbtn').removeAttr("href");
874

    
875
			$('#svcontbtn').click(function () {
876
				var ous = $('[id^=ou]').length;
877
				var i;
878

    
879
				$('#ldapauthcontainers').val("");
880

    
881
				for (i = 0; i < ous; i++) {
882
					if ($('#ou' + i).prop("checked")) {
883
						if ($('#ldapauthcontainers').val() != "") {
884
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
885
						}
886

    
887
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
888
					}
889
				}
890

    
891
				$('#containers').modal('hide');
892
			});
893
		});
894

    
895
	}
896

    
897
	function set_ldap_port() {
898
		if ($('#ldap_urltype').find(":selected").index() == 2)
899
			$('#ldap_port').val('636');
900
		else
901
			$('#ldap_port').val('389');
902
	}
903

    
904
	// Hides all elements of the specified class. This will usually be a section
905
	function hideClass(s_class, hide) {
906
		if (hide)
907
			$('.' + s_class).hide();
908
		else
909
			$('.' + s_class).show();
910
	}
911

    
912
	function ldap_tmplchange() {
913
		switch ($('#ldap_tmpltype').find(":selected").index()) {
914
<?php
915
		$index = 0;
916
		foreach ($ldap_templates as $tmpldata):
917
?>
918
			case <?=$index;?>:
919
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
920
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
921
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
922
				break;
923
<?php
924
			$index++;
925
		endforeach;
926
?>
927
		}
928
	}
929

    
930
	// ---------- On initial page load ------------------------------------------------------------
931

    
932
<?php if ($act != 'edit') : ?>
933
	ldap_tmplchange();
934
<?php endif; ?>
935

    
936
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
937
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
938

    
939
	if ($('#ldap_port').val() == "")
940
		set_ldap_port();
941

    
942
<?php
943
	if ($act == 'edit') {
944
?>
945
		$('#type option:not(:selected)').each(function(){
946
			$(this).attr('disabled', 'disabled');
947
		});
948

    
949
<?php
950
		if (!$input_errors) {
951
?>
952
		$('#name').prop("readonly", true);
953
<?php
954
		}
955
	}
956
?>
957
	// ---------- Click checkbox handlers ---------------------------------------------------------
958

    
959
	$('#ldap_tmpltype').on('change', function() {
960
		ldap_tmplchange();
961
	});
962

    
963
	$('#ldap_anon').click(function () {
964
		hideClass('ldapanon', this.checked);
965
	});
966

    
967
	$('#ldap_urltype').on('change', function() {
968
		set_ldap_port();
969
	});
970

    
971
	$('#Select').click(function () {
972
		select_clicked();
973
	});
974

    
975
	$('#ldap_extended_enabled').click(function () {
976
		hideClass('extended', !this.checked);
977
	});
978

    
979
});
980
//]]>
981
</script>
982
<?php
983
include("foot.inc");
(191-191/225)