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-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2008 Shrew Soft Inc
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25
 *
26
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30
 *
31
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *
38
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53
 */
54

    
55
##|+PRIV
56
##|*IDENT=page-system-authservers
57
##|*NAME=System: Authentication Servers
58
##|*DESCR=Allow access to the 'System: Authentication Servers' page.
59
##|*MATCH=system_authservers.php*
60
##|-PRIV
61

    
62
require_once("guiconfig.inc");
63
require_once("auth.inc");
64

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

    
68
	$ous = array();
69
	$authcfg = array();
70

    
71
	$authcfg['ldap_port'] = $_REQUEST['port'];
72
	$authcfg['ldap_basedn'] = $_REQUEST['basedn'];
73
	$authcfg['host'] = $_REQUEST['host'];
74
	$authcfg['ldap_scope'] = $_REQUEST['scope'];
75
	$authcfg['ldap_binddn'] = $_REQUEST['binddn'];
76
	$authcfg['ldap_bindpw'] = $_REQUEST['bindpw'];
77
	$authcfg['ldap_urltype'] = $_REQUEST['urltype'];
78
	$authcfg['ldap_protver'] = $_REQUEST['proto'];
79
	$authcfg['ldap_authcn'] = explode(";", $_REQUEST['authcn']);
80
	$authcfg['ldap_caref'] = $_REQUEST['cert'];
81

    
82
	$ous = ldap_get_user_ous(true, $authcfg);
83

    
84
	if (empty($ous)) {
85
		print('<span class="text-danger">Could not connect to the LDAP server. Please check the LDAP configuration.</span>');
86
	} else {
87
		$modal = new Modal("Select LDAP containers for authentication", "containers", true);
88
		$group = new Form_MultiCheckboxGroup('Containers');
89

    
90
		if (is_array($ous)) {
91
			$idx = 0;
92

    
93
			foreach ($ous as $ou) {
94
				$group->add(new Form_MultiCheckbox(
95
					'ou' . $idx,
96
					'',
97
					$ou,
98
					in_array($ou, $authcfg['ldap_authcn']),
99
					$ou
100
				));
101

    
102
				$idx++;
103
			}
104
		}
105

    
106
		$modal->add($group);
107

    
108
		// Create a "Save button"
109

    
110
		$btnsv = new Form_Button(
111
			'svcontbtn',
112
			'Save',
113
			null,
114
			'fa-save'
115
		);
116

    
117
		$btnsv->removeClass("btn-default)")->addClass("btn-primary");
118

    
119
		$modal->addInput(new Form_StaticText(
120
			'',
121
			$btnsv
122
		));
123

    
124
		print($modal);
125
	}
126

    
127
	exit;
128
}
129

    
130
if (is_numericint($_GET['id'])) {
131
	$id = $_GET['id'];
132
}
133

    
134
if (isset($_POST['id']) && is_numericint($_POST['id'])) {
135
	$id = $_POST['id'];
136
}
137

    
138
if (!is_array($config['system']['authserver'])) {
139
	$config['system']['authserver'] = array();
140
}
141

    
142
$a_server = array_values(auth_get_authserver_list());
143

    
144
if (!is_array($config['ca'])) {
145
	$config['ca'] = array();
146
}
147
$a_ca =& $config['ca'];
148

    
149
$act = $_GET['act'];
150
if ($_POST['act']) {
151
	$act = $_POST['act'];
152
}
153

    
154
if ($act == "del") {
155

    
156
	if (!$a_server[$_GET['id']]) {
157
		pfSenseHeader("system_authservers.php");
158
		exit;
159
	}
160

    
161
	/* Remove server from main list. */
162
	$serverdeleted = $a_server[$_GET['id']]['name'];
163
	foreach ($config['system']['authserver'] as $k => $as) {
164
		if ($config['system']['authserver'][$k]['name'] == $serverdeleted) {
165
			unset($config['system']['authserver'][$k]);
166
		}
167
	}
168

    
169
	/* Remove server from temp list used later on this page. */
170
	unset($a_server[$_GET['id']]);
171
	$a_server = array_values($a_server);
172

    
173
	$savemsg = sprintf(gettext("Authentication Server %s deleted."), htmlspecialchars($serverdeleted));
174
	write_config($savemsg);
175
}
176

    
177
if ($act == "edit") {
178
	if (isset($id) && $a_server[$id]) {
179

    
180
		$pconfig['type'] = $a_server[$id]['type'];
181
		$pconfig['name'] = $a_server[$id]['name'];
182

    
183
		if ($pconfig['type'] == "ldap") {
184
			$pconfig['ldap_caref'] = $a_server[$id]['ldap_caref'];
185
			$pconfig['ldap_host'] = $a_server[$id]['host'];
186
			$pconfig['ldap_port'] = $a_server[$id]['ldap_port'];
187
			$pconfig['ldap_timeout'] = $a_server[$id]['ldap_timeout'];
188
			$pconfig['ldap_urltype'] = $a_server[$id]['ldap_urltype'];
189
			$pconfig['ldap_protver'] = $a_server[$id]['ldap_protver'];
190
			$pconfig['ldap_scope'] = $a_server[$id]['ldap_scope'];
191
			$pconfig['ldap_basedn'] = $a_server[$id]['ldap_basedn'];
192
			$pconfig['ldap_authcn'] = $a_server[$id]['ldap_authcn'];
193
			$pconfig['ldap_extended_enabled'] = $a_server[$id]['ldap_extended_enabled'];
194
			$pconfig['ldap_extended_query'] = $a_server[$id]['ldap_extended_query'];
195
			$pconfig['ldap_binddn'] = $a_server[$id]['ldap_binddn'];
196
			$pconfig['ldap_bindpw'] = $a_server[$id]['ldap_bindpw'];
197
			$pconfig['ldap_attr_user'] = $a_server[$id]['ldap_attr_user'];
198
			$pconfig['ldap_attr_group'] = $a_server[$id]['ldap_attr_group'];
199
			$pconfig['ldap_attr_member'] = $a_server[$id]['ldap_attr_member'];
200
			$pconfig['ldap_attr_groupobj'] = $a_server[$id]['ldap_attr_groupobj'];
201
			$pconfig['ldap_utf8'] = isset($a_server[$id]['ldap_utf8']);
202
			$pconfig['ldap_nostrip_at'] = isset($a_server[$id]['ldap_nostrip_at']);
203
			$pconfig['ldap_rfc2307'] = isset($a_server[$id]['ldap_rfc2307']);
204

    
205
			if (!$pconfig['ldap_binddn'] || !$pconfig['ldap_bindpw']) {
206
				$pconfig['ldap_anon'] = true;
207
			}
208
		}
209

    
210
		if ($pconfig['type'] == "radius") {
211
			$pconfig['radius_host'] = $a_server[$id]['host'];
212
			$pconfig['radius_auth_port'] = $a_server[$id]['radius_auth_port'];
213
			$pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'];
214
			$pconfig['radius_secret'] = $a_server[$id]['radius_secret'];
215
			$pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'];
216

    
217
			if ($pconfig['radius_auth_port'] &&
218
				$pconfig['radius_acct_port']) {
219
				$pconfig['radius_srvcs'] = "both";
220
			}
221

    
222
			if ($pconfig['radius_auth_port'] &&
223
				!$pconfig['radius_acct_port']) {
224
				$pconfig['radius_srvcs'] = "auth";
225
				$pconfig['radius_acct_port'] = 1813;
226
			}
227

    
228
			if (!$pconfig['radius_auth_port'] &&
229
				$pconfig['radius_acct_port']) {
230
				$pconfig['radius_srvcs'] = "acct";
231
				$pconfig['radius_auth_port'] = 1812;
232
			}
233

    
234
		}
235
	}
236
}
237

    
238
if ($act == "new") {
239
	$pconfig['ldap_protver'] = 3;
240
	$pconfig['ldap_anon'] = true;
241
	$pconfig['radius_srvcs'] = "both";
242
	$pconfig['radius_auth_port'] = "1812";
243
	$pconfig['radius_acct_port'] = "1813";
244
}
245

    
246
if ($_POST) {
247
	unset($input_errors);
248
	$pconfig = $_POST;
249

    
250
	/* input validation */
251

    
252
	if ($pconfig['type'] == "ldap") {
253
		$reqdfields = explode(" ",
254
			"name type ldap_host ldap_port " .
255
			"ldap_urltype ldap_protver ldap_scope " .
256
			"ldap_attr_user ldap_attr_group ldap_attr_member ldapauthcontainers");
257

    
258
		$reqdfieldsn = array(
259
			gettext("Descriptive name"),
260
			gettext("Type"),
261
			gettext("Hostname or IP"),
262
			gettext("Port value"),
263
			gettext("Transport"),
264
			gettext("Protocol version"),
265
			gettext("Search level"),
266
			gettext("User naming Attribute"),
267
			gettext("Group naming Attribute"),
268
			gettext("Group member attribute"),
269
			gettext("Authentication container"));
270

    
271
		if (!$pconfig['ldap_anon']) {
272
			$reqdfields[] = "ldap_binddn";
273
			$reqdfields[] = "ldap_bindpw";
274
			$reqdfieldsn[] = gettext("Bind user DN");
275
			$reqdfieldsn[] = gettext("Bind Password");
276
		}
277
	}
278

    
279
	if ($pconfig['type'] == "radius") {
280
		$reqdfields = explode(" ", "name type radius_host radius_srvcs");
281
		$reqdfieldsn = array(
282
			gettext("Descriptive name"),
283
			gettext("Type"),
284
			gettext("Hostname or IP"),
285
			gettext("Services"));
286

    
287
		if ($pconfig['radius_srvcs'] == "both" ||
288
			$pconfig['radius_srvcs'] == "auth") {
289
			$reqdfields[] = "radius_auth_port";
290
			$reqdfieldsn[] = gettext("Authentication port");
291
		}
292

    
293
		if ($pconfig['radius_srvcs'] == "both" ||
294
			$pconfig['radius_srvcs'] == "acct") {
295
			$reqdfields[] = "radius_acct_port";
296
			$reqdfieldsn[] = gettext("Accounting port");
297
		}
298

    
299
		if (!isset($id)) {
300
			$reqdfields[] = "radius_secret";
301
			$reqdfieldsn[] = gettext("Shared Secret");
302
		}
303
	}
304

    
305
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
306

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

    
311
	if (auth_get_authserver($pconfig['name']) && !isset($id)) {
312
		$input_errors[] = gettext("An authentication server with the same name already exists.");
313
	}
314

    
315
	if (($pconfig['type'] == "ldap") || ($pconfig['type'] == "radius")) {
316
		$to_field = "{$pconfig['type']}_timeout";
317
		if (isset($_POST[$to_field]) && !empty($_POST[$to_field]) && (!is_numeric($_POST[$to_field]) || (is_numeric($_POST[$to_field]) && ($_POST[$to_field] <= 0)))) {
318
			$input_errors[] = sprintf(gettext("%s Timeout value must be numeric and positive."), strtoupper($pconfig['type']));
319
		}
320
	}
321

    
322
	// https://redmine.pfsense.org/issues/4154
323
	if ($pconfig['type'] == "radius") {
324
		if (is_ipaddrv6($_POST['radius_host'])) {
325
			$input_errors[] = gettext("IPv6 does not work for RADIUS authentication, see Bug #4154.");
326
		}
327
	}
328

    
329
	if (!$input_errors) {
330
		$server = array();
331
		$server['refid'] = uniqid();
332
		if (isset($id) && $a_server[$id]) {
333
			$server = $a_server[$id];
334
		}
335

    
336
		$server['type'] = $pconfig['type'];
337
		$server['name'] = $pconfig['name'];
338

    
339
		if ($server['type'] == "ldap") {
340

    
341
			if (!empty($pconfig['ldap_caref'])) {
342
				$server['ldap_caref'] = $pconfig['ldap_caref'];
343
			}
344
			$server['host'] = $pconfig['ldap_host'];
345
			$server['ldap_port'] = $pconfig['ldap_port'];
346
			$server['ldap_urltype'] = $pconfig['ldap_urltype'];
347
			$server['ldap_protver'] = $pconfig['ldap_protver'];
348
			$server['ldap_scope'] = $pconfig['ldap_scope'];
349
			$server['ldap_basedn'] = $pconfig['ldap_basedn'];
350
			$server['ldap_authcn'] = $pconfig['ldapauthcontainers'];
351
			$server['ldap_extended_enabled'] = $pconfig['ldap_extended_enabled'];
352
			$server['ldap_extended_query'] = $pconfig['ldap_extended_query'];
353
			$server['ldap_attr_user'] = $pconfig['ldap_attr_user'];
354
			$server['ldap_attr_group'] = $pconfig['ldap_attr_group'];
355
			$server['ldap_attr_member'] = $pconfig['ldap_attr_member'];
356

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

    
359
			if ($pconfig['ldap_utf8'] == "yes") {
360
				$server['ldap_utf8'] = true;
361
			} else {
362
				unset($server['ldap_utf8']);
363
			}
364
			if ($pconfig['ldap_nostrip_at'] == "yes") {
365
				$server['ldap_nostrip_at'] = true;
366
			} else {
367
				unset($server['ldap_nostrip_at']);
368
			}
369
			if ($pconfig['ldap_rfc2307'] == "yes") {
370
				$server['ldap_rfc2307'] = true;
371
			} else {
372
				unset($server['ldap_rfc2307']);
373
			}
374

    
375

    
376
			if (!$pconfig['ldap_anon']) {
377
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
378
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
379
			} else {
380
				unset($server['ldap_binddn']);
381
				unset($server['ldap_bindpw']);
382
			}
383

    
384
			if ($pconfig['ldap_timeout']) {
385
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
386
			} else {
387
				$server['ldap_timeout'] = 25;
388
			}
389
		}
390

    
391
		if ($server['type'] == "radius") {
392

    
393
			$server['host'] = $pconfig['radius_host'];
394

    
395
			if ($pconfig['radius_secret']) {
396
				$server['radius_secret'] = $pconfig['radius_secret'];
397
			}
398

    
399
			if ($pconfig['radius_timeout']) {
400
				$server['radius_timeout'] = $pconfig['radius_timeout'];
401
			} else {
402
				$server['radius_timeout'] = 5;
403
			}
404

    
405
			if ($pconfig['radius_srvcs'] == "both") {
406
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
407
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
408
			}
409

    
410
			if ($pconfig['radius_srvcs'] == "auth") {
411
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
412
				unset($server['radius_acct_port']);
413
			}
414

    
415
			if ($pconfig['radius_srvcs'] == "acct") {
416
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
417
				unset($server['radius_auth_port']);
418
			}
419
		}
420

    
421
		if (isset($id) && $config['system']['authserver'][$id]) {
422
			$config['system']['authserver'][$id] = $server;
423
		} else {
424
			$config['system']['authserver'][] = $server;
425
		}
426

    
427
		write_config();
428

    
429
		pfSenseHeader("system_authservers.php");
430
	}
431
}
432

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
622

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

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

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

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

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

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

    
657
$group->add(new Form_Input(
658
	'ldap_extended_query',
659
	'Query',
660
	'text',
661
	$pconfig['ldap_extended_query']
662
))->setHelp('Example: &amp;(objectClass=inetOrgPerson)(mail=*@example.com)');
663

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
766
$section->addInput(new Form_Input(
767
	'radius_host',
768
	'*Hostname or IP address',
769
	'text',
770
	$pconfig['radius_host']
771
));
772

    
773
$section->addInput(new Form_Input(
774
	'radius_secret',
775
	'*Shared Secret',
776
	'password',
777
	$pconfig['radius_secret']
778
));
779

    
780
$section->addInput(new Form_Select(
781
	'radius_srvcs',
782
	'*Services offered',
783
	$pconfig['radius_srvcs'],
784
	$radius_srvcs
785
));
786

    
787
$section->addInput(new Form_Input(
788
	'radius_auth_port',
789
	'Authentication port',
790
	'number',
791
	$pconfig['radius_auth_port']
792
));
793

    
794
$section->addInput(new Form_Input(
795
	'radius_acct_port',
796
	'Accounting port',
797
	'number',
798
	$pconfig['radius_acct_port']
799
));
800

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

    
812
if (isset($id) && $a_server[$id])
813
{
814
	$section->addInput(new Form_Input(
815
		'id',
816
		null,
817
		'hidden',
818
		$id
819
	));
820
}
821

    
822
$form->add($section);
823

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

    
827
$form->add($modal);
828

    
829
print $form;
830
?>
831
<script type="text/javascript">
832
//<![CDATA[
833
events.push(function() {
834

    
835
	// Create an AJAX request (to this page) to get the container list and controls
836
	function select_clicked() {
837
		if (document.getElementById("ldap_port").value == '' ||
838
			document.getElementById("ldap_host").value == '' ||
839
			document.getElementById("ldap_scope").value == '' ||
840
			document.getElementById("ldap_basedn").value == '' ||
841
			document.getElementById("ldapauthcontainers").value == '') {
842
			alert("<?=gettext("Please fill the required values.");?>");
843
			return;
844
		}
845

    
846
		if (!document.getElementById("ldap_anon").checked) {
847
			if (document.getElementById("ldap_binddn").value == '' ||
848
				document.getElementById("ldap_bindpw").value == '') {
849
				alert("<?=gettext("Please fill the bind username/password.");?>");
850
				return;
851
			}
852
		}
853

    
854
		var ajaxRequest;
855
		var authserver = $('#authmode').val();
856
		var cert;
857

    
858
<?php if (count($a_ca) > 0): ?>
859
			cert = $('#ldap_caref').val();
860
<?php else: ?>
861
			cert = '';
862
<?php endif; ?>
863
/*
864
		$('#containers').modal('show');
865
		$('#serverlist').parent('div').prev('label').remove();
866
		$('#serverlist').parent('div').removeClass("col-sm-10");
867
		$('#serverlist').parent('div').addClass("col-sm-12");
868
*/
869
		ajaxRequest = $.ajax(
870
			{
871
				url: "/system_authservers.php",
872
				type: "post",
873
				data: {
874
					ajax: 	"ajax",
875
					port: 	$('#ldap_port').val(),
876
					host: 	$('#ldap_host').val(),
877
					scope: 	$('#ldap_scope').val(),
878
					basedn: $('#ldap_basedn').val(),
879
					binddn: $('#ldap_binddn').val(),
880
					bindpw: $('#ldap_bindpw').val(),
881
					urltype:$('#ldap_urltype').val(),
882
					proto:  $('#ldap_protver').val(),
883
					authcn: $('#ldapauthcontainers').val(),
884
					cert:   cert
885
				}
886
			}
887
		);
888

    
889
		// Deal with the results of the above ajax call
890
		ajaxRequest.done(function (response, textStatus, jqXHR) {
891
			$('#containers').replaceWith(response);
892

    
893
			$('#containers').modal('show');
894

    
895
			// The button handler needs to be here because until the modal has been populated
896
			// the controls we need to attach handlers to do not exist
897
			$('#svcontbtn').prop("type", "button");
898
			$('#svcontbtn').removeAttr("href");
899

    
900
			$('#svcontbtn').click(function () {
901
				var ous = $('[id^=ou]').length;
902
				var i;
903

    
904
				$('#ldapauthcontainers').val("");
905

    
906
				for (i = 0; i < ous; i++) {
907
					if ($('#ou' + i).prop("checked")) {
908
						if ($('#ldapauthcontainers').val() != "") {
909
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
910
						}
911

    
912
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
913
					}
914
				}
915

    
916
				$('#containers').modal('hide');
917
			});
918
		});
919

    
920
	}
921

    
922
	function set_ldap_port() {
923
		if ($('#ldap_urltype').find(":selected").index() == 2)
924
			$('#ldap_port').val('636');
925
		else
926
			$('#ldap_port').val('389');
927
	}
928

    
929
	function set_required_port_fields() {
930
		if (document.getElementById("radius_srvcs").value == 'auth') {
931
			setRequired('radius_auth_port', true);
932
			setRequired('radius_acct_port', false);
933
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
934
			setRequired('radius_auth_port', false);
935
			setRequired('radius_acct_port', true);
936
		} else { // both
937
			setRequired('radius_auth_port', true);
938
			setRequired('radius_acct_port', true);
939
		}
940
	}
941

    
942
	// Hides all elements of the specified class. This will usually be a section
943
	function hideClass(s_class, hide) {
944
		if (hide)
945
			$('.' + s_class).hide();
946
		else
947
			$('.' + s_class).show();
948
	}
949

    
950
	function ldap_tmplchange() {
951
		switch ($('#ldap_tmpltype').find(":selected").index()) {
952
<?php
953
		$index = 0;
954
		foreach ($ldap_templates as $tmpldata):
955
?>
956
			case <?=$index;?>:
957
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
958
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
959
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
960
				break;
961
<?php
962
			$index++;
963
		endforeach;
964
?>
965
		}
966
	}
967

    
968
	// ---------- On initial page load ------------------------------------------------------------
969

    
970
<?php if ($act != 'edit') : ?>
971
	ldap_tmplchange();
972
<?php endif; ?>
973

    
974
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
975
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
976
	set_required_port_fields();
977

    
978
	if ($('#ldap_port').val() == "")
979
		set_ldap_port();
980

    
981
<?php
982
	if ($act == 'edit') {
983
?>
984
		$('#type option:not(:selected)').each(function(){
985
			$(this).attr('disabled', 'disabled');
986
		});
987

    
988
<?php
989
		if (!$input_errors) {
990
?>
991
		$('#name').prop("readonly", true);
992
<?php
993
		}
994
	}
995
?>
996
	// ---------- Click checkbox handlers ---------------------------------------------------------
997

    
998
	$('#ldap_tmpltype').on('change', function() {
999
		ldap_tmplchange();
1000
	});
1001

    
1002
	$('#ldap_anon').click(function () {
1003
		hideClass('ldapanon', this.checked);
1004
	});
1005

    
1006
	$('#ldap_urltype').on('change', function() {
1007
		set_ldap_port();
1008
	});
1009

    
1010
	$('#Select').click(function () {
1011
		select_clicked();
1012
	});
1013

    
1014
	$('#ldap_extended_enabled').click(function () {
1015
		hideClass('extended', !this.checked);
1016
	});
1017

    
1018
	$('#radius_srvcs').on('change', function() {
1019
		set_required_port_fields();
1020
	});
1021

    
1022
});
1023
//]]>
1024
</script>
1025
<?php
1026
include("foot.inc");
(191-191/225)