Project

General

Profile

Download (26.5 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
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_auth_port'] = $a_server[$id]['radius_auth_port'];
177
			$pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'];
178
			$pconfig['radius_secret'] = $a_server[$id]['radius_secret'];
179
			$pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'];
180

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

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

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

    
198
		}
199
	}
200
}
201

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

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

    
215
	/* input validation */
216

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
341

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

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

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

    
359
			$server['radius_protocol'] = $pconfig['radius_protocol'];
360
			$server['host'] = $pconfig['radius_host'];
361

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

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

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

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

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

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

    
394
		write_config();
395

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
589

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

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

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

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

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

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

    
624
$group->add(new Form_Input(
625
	'ldap_extended_query',
626
	'Query',
627
	'text',
628
	$pconfig['ldap_extended_query']
629
))->setHelp('Example: memberOf=CN=Groupname,OU=MyGroups,DC=example,DC=com;OU=OtherUsers,DC=example,DC=com');
630

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
894
	}
895

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

    
903
	function set_required_port_fields() {
904
		if (document.getElementById("radius_srvcs").value == 'auth') {
905
			setRequired('radius_auth_port', true);
906
			setRequired('radius_acct_port', false);
907
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
908
			setRequired('radius_auth_port', false);
909
			setRequired('radius_acct_port', true);
910
		} else { // both
911
			setRequired('radius_auth_port', true);
912
			setRequired('radius_acct_port', true);
913
		}
914
	}
915

    
916
	// Hides all elements of the specified class. This will usually be a section
917
	function hideClass(s_class, hide) {
918
		if (hide)
919
			$('.' + s_class).hide();
920
		else
921
			$('.' + s_class).show();
922
	}
923

    
924
	function ldap_tmplchange() {
925
		switch ($('#ldap_tmpltype').find(":selected").index()) {
926
<?php
927
		$index = 0;
928
		foreach ($ldap_templates as $tmpldata):
929
?>
930
			case <?=$index;?>:
931
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
932
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
933
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
934
				break;
935
<?php
936
			$index++;
937
		endforeach;
938
?>
939
		}
940
	}
941

    
942
	// ---------- On initial page load ------------------------------------------------------------
943

    
944
<?php if ($act != 'edit') : ?>
945
	ldap_tmplchange();
946
<?php endif; ?>
947

    
948
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
949
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
950
	set_required_port_fields();
951

    
952
	if ($('#ldap_port').val() == "")
953
		set_ldap_port();
954

    
955
<?php
956
	if ($act == 'edit') {
957
?>
958
		$('#type option:not(:selected)').each(function(){
959
			$(this).attr('disabled', 'disabled');
960
		});
961

    
962
<?php
963
		if (!$input_errors) {
964
?>
965
		$('#name').prop("readonly", true);
966
<?php
967
		}
968
	}
969
?>
970
	// ---------- Click checkbox handlers ---------------------------------------------------------
971

    
972
	$('#ldap_tmpltype').on('change', function() {
973
		ldap_tmplchange();
974
	});
975

    
976
	$('#ldap_anon').click(function () {
977
		hideClass('ldapanon', this.checked);
978
	});
979

    
980
	$('#ldap_urltype').on('change', function() {
981
		set_ldap_port();
982
	});
983

    
984
	$('#Select').click(function () {
985
		select_clicked();
986
	});
987

    
988
	$('#ldap_extended_enabled').click(function () {
989
		hideClass('extended', !this.checked);
990
	});
991

    
992
	$('#radius_srvcs').on('change', function() {
993
		set_required_port_fields();
994
	});
995

    
996
});
997
//]]>
998
</script>
999
<?php
1000
include("foot.inc");
(194-194/228)