Project

General

Profile

Download (26.2 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
$id = $_REQUEST['id'];
100

    
101
if (!is_array($config['system']['authserver'])) {
102
	$config['system']['authserver'] = array();
103
}
104

    
105
$a_servers = auth_get_authserver_list();
106

    
107
foreach ($a_servers as $servers) {
108
	$a_server[] = $servers;
109
}
110

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

    
115
$a_ca =& $config['ca'];
116

    
117
$act = $_REQUEST['act'];
118

    
119
if ($_POST['act'] == "del") {
120

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

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

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

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

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

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

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

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

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

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

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

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

    
199
		}
200
	}
201
}
202

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

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

    
216
	/* input validation */
217

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

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

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

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

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

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

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

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

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

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

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

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

    
296
		$server['type'] = $pconfig['type'];
297
		$server['name'] = $pconfig['name'];
298

    
299
		if ($server['type'] == "ldap") {
300

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

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

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

    
335

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

    
344
			if ($pconfig['ldap_timeout']) {
345
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
346
			} else {
347
				$server['ldap_timeout'] = 25;
348
			}
349
		}
350

    
351
		if ($server['type'] == "radius") {
352

    
353
			$server['radius_protocol'] = $pconfig['radius_protocol'];
354
			$server['host'] = $pconfig['radius_host'];
355

    
356
			if ($pconfig['radius_secret']) {
357
				$server['radius_secret'] = $pconfig['radius_secret'];
358
			}
359

    
360
			if ($pconfig['radius_timeout']) {
361
				$server['radius_timeout'] = $pconfig['radius_timeout'];
362
			} else {
363
				$server['radius_timeout'] = 5;
364
			}
365

    
366
			if ($pconfig['radius_srvcs'] == "both") {
367
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
368
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
369
			}
370

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

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

    
382
		if (isset($id) && $config['system']['authserver'][$id]) {
383
			$config['system']['authserver'][$id] = $server;
384
		} else {
385
			$config['system']['authserver'][] = $server;
386
		}
387

    
388
		write_config();
389

    
390
		pfSenseHeader("system_authservers.php");
391
	}
392
}
393

    
394
// On error, restore the form contents so the user doesn't have to re-enter too much
395
if ($_POST && $input_errors) {
396
	$pconfig = $_POST;
397
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
398
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
399
}
400

    
401
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
402
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
403

    
404
if ($act == "new" || $act == "edit" || $input_errors) {
405
	$pgtitle[] = gettext('Edit');
406
	$pglinks[] = "@self";
407
}
408
$shortcut_section = "authentication";
409
include("head.inc");
410

    
411
if ($input_errors) {
412
	print_input_errors($input_errors);
413
}
414

    
415
if ($savemsg) {
416
	print_info_box($savemsg, 'success');
417
}
418

    
419
$tab_array = array();
420
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
421
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
422
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
423
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
424
display_top_tabs($tab_array);
425

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

    
461
<nav class="action-buttons">
462
	<a href="?act=new" class="btn btn-success btn-sm">
463
		<i class="fa fa-plus icon-embed-btn"></i>
464
		<?=gettext("Add")?>
465
	</a>
466
</nav>
467
<?php
468
	include("foot.inc");
469
	exit;
470
}
471

    
472
$form = new Form;
473
$form->setAction('system_authservers.php?act=edit');
474

    
475
$form->addGlobal(new Form_Input(
476
	'userid',
477
	null,
478
	'hidden',
479
	$id
480
));
481

    
482
$section = new Form_Section('Server Settings');
483

    
484
$section->addInput($input = new Form_Input(
485
	'name',
486
	'*Descriptive name',
487
	'text',
488
	$pconfig['name']
489
));
490

    
491
$section->addInput($input = new Form_Select(
492
	'type',
493
	'*Type',
494
	$pconfig['type'],
495
	$auth_server_types
496
))->toggles();
497

    
498
$form->add($section);
499

    
500
// ==== LDAP settings =========================================================
501
$section = new Form_Section('LDAP Server Settings');
502
$section->addClass('toggle-ldap collapse');
503

    
504
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
505
	$section->addClass('in');
506

    
507
$section->addInput(new Form_Input(
508
	'ldap_host',
509
	'*Hostname or IP address',
510
	'text',
511
	$pconfig['ldap_host']
512
))->setHelp('NOTE: When using SSL or STARTTLS, this hostname MUST match the Common Name '.
513
	'(CN) of the LDAP server\'s SSL Certificate.');
514

    
515
$section->addInput(new Form_Input(
516
	'ldap_port',
517
	'*Port value',
518
	'number',
519
	$pconfig['ldap_port']
520
));
521

    
522
$section->addInput(new Form_Select(
523
	'ldap_urltype',
524
	'*Transport',
525
	$pconfig['ldap_urltype'],
526
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
527
));
528

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

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

    
552
$section->addInput(new Form_Select(
553
	'ldap_protver',
554
	'*Protocol version',
555
	$pconfig['ldap_protver'],
556
	array_combine($ldap_protvers, $ldap_protvers)
557
));
558

    
559
$section->addInput(new Form_Input(
560
	'ldap_timeout',
561
	'Server Timeout',
562
	'number',
563
	$pconfig['ldap_timeout'],
564
	['placeholder' => 25]
565
))->setHelp('Timeout for LDAP operations (seconds)');
566

    
567
$group = new Form_Group('Search scope');
568

    
569
$SSF = new Form_Select(
570
	'ldap_scope',
571
	'*Level',
572
	$pconfig['ldap_scope'],
573
	$ldap_scopes
574
);
575

    
576
$SSB = new Form_Input(
577
	'ldap_basedn',
578
	'Base DN',
579
	'text',
580
	$pconfig['ldap_basedn']
581
);
582

    
583

    
584
$section->addInput(new Form_StaticText(
585
	'Search scope',
586
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
587
));
588

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

    
599
$group->add(new Form_Button(
600
	'Select',
601
	'Select a container',
602
	null,
603
	'fa-search'
604
))->setAttribute('type','button')->addClass('btn-info');
605

    
606
$section->add($group);
607

    
608
$section->addInput(new Form_Checkbox(
609
	'ldap_extended_enabled',
610
	'Extended query',
611
	'Enable extended query',
612
	$pconfig['ldap_extended_enabled']
613
));
614

    
615
$group = new Form_Group('Query');
616
$group->addClass('extended');
617

    
618
$group->add(new Form_Input(
619
	'ldap_extended_query',
620
	'Query',
621
	'text',
622
	$pconfig['ldap_extended_query']
623
))->setHelp('Example: &amp;(objectClass=inetOrgPerson)(mail=*@example.com)');
624

    
625
$section->add($group);
626

    
627
$section->addInput(new Form_Checkbox(
628
	'ldap_anon',
629
	'Bind anonymous',
630
	'Use anonymous binds to resolve distinguished names',
631
	$pconfig['ldap_anon']
632
));
633

    
634
$group = new Form_Group('*Bind credentials');
635
$group->addClass('ldapanon');
636

    
637
$group->add(new Form_Input(
638
	'ldap_binddn',
639
	'User DN:',
640
	'text',
641
	$pconfig['ldap_binddn']
642
));
643

    
644
$group->add(new Form_Input(
645
	'ldap_bindpw',
646
	'Password',
647
	'password',
648
	$pconfig['ldap_bindpw']
649
));
650
$section->add($group);
651

    
652
if (!isset($id)) {
653
	$template_list = array();
654

    
655
	foreach ($ldap_templates as $option => $template) {
656
		$template_list[$option] = $template['desc'];
657
	}
658

    
659
	$section->addInput(new Form_Select(
660
		'ldap_tmpltype',
661
		'Initial Template',
662
		$pconfig['ldap_template'],
663
		$template_list
664
	));
665
}
666

    
667
$section->addInput(new Form_Input(
668
	'ldap_attr_user',
669
	'*User naming attribute',
670
	'text',
671
	$pconfig['ldap_attr_user']
672
));
673

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

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

    
688
$section->addInput(new Form_Checkbox(
689
	'ldap_rfc2307',
690
	'RFC 2307 Groups',
691
	'LDAP Server uses RFC 2307 style group membership',
692
	$pconfig['ldap_rfc2307']
693
))->setHelp('RFC 2307 style group membership has members listed on the group '.
694
	'object rather than using groups listed on user object. Leave unchecked '.
695
	'for Active Directory style group membership (RFC 2307bis).');
696

    
697
$section->addInput(new Form_Input(
698
	'ldap_attr_groupobj',
699
	'Group Object Class',
700
	'text',
701
	$pconfig['ldap_attr_groupobj'],
702
	['placeholder' => 'posixGroup']
703
))->setHelp('Object class used for groups in RFC2307 mode. '.
704
	'Typically "posixGroup" or "group".');
705

    
706
$section->addInput(new Form_Checkbox(
707
	'ldap_utf8',
708
	'UTF8 Encode',
709
	'UTF8 encode LDAP parameters before sending them to the server.',
710
	$pconfig['ldap_utf8']
711
))->setHelp('Required to support international characters, but may not be '.
712
	'supported by every LDAP server.');
713

    
714
$section->addInput(new Form_Checkbox(
715
	'ldap_nostrip_at',
716
	'Username Alterations',
717
	'Do not strip away parts of the username after the @ symbol',
718
	$pconfig['ldap_nostrip_at']
719
))->setHelp('e.g. user@host becomes user when unchecked.');
720

    
721
$form->add($section);
722

    
723
// ==== RADIUS section ========================================================
724
$section = new Form_Section('RADIUS Server Settings');
725
$section->addClass('toggle-radius collapse');
726

    
727
$section->addInput(new Form_Select(
728
	'radius_protocol',
729
	'*Protocol',
730
	$pconfig['radius_protocol'],
731
	$radius_protocol
732
));
733

    
734
$section->addInput(new Form_Input(
735
	'radius_host',
736
	'*Hostname or IP address',
737
	'text',
738
	$pconfig['radius_host']
739
));
740

    
741
$section->addInput(new Form_Input(
742
	'radius_secret',
743
	'*Shared Secret',
744
	'password',
745
	$pconfig['radius_secret']
746
));
747

    
748
$section->addInput(new Form_Select(
749
	'radius_srvcs',
750
	'*Services offered',
751
	$pconfig['radius_srvcs'],
752
	$radius_srvcs
753
));
754

    
755
$section->addInput(new Form_Input(
756
	'radius_auth_port',
757
	'Authentication port',
758
	'number',
759
	$pconfig['radius_auth_port']
760
));
761

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

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

    
780
if (isset($id) && $a_server[$id])
781
{
782
	$section->addInput(new Form_Input(
783
		'id',
784
		null,
785
		'hidden',
786
		$id
787
	));
788
}
789

    
790
$form->add($section);
791

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

    
795
$form->add($modal);
796

    
797
print $form;
798
?>
799
<script type="text/javascript">
800
//<![CDATA[
801
events.push(function() {
802

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

    
814
		if (!document.getElementById("ldap_anon").checked) {
815
			if (document.getElementById("ldap_binddn").value == '' ||
816
				document.getElementById("ldap_bindpw").value == '') {
817
				alert("<?=gettext("Please fill the bind username/password.");?>");
818
				return;
819
			}
820
		}
821

    
822
		var ajaxRequest;
823
		var authserver = $('#authmode').val();
824
		var cert;
825

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

    
857
		// Deal with the results of the above ajax call
858
		ajaxRequest.done(function (response, textStatus, jqXHR) {
859
			$('#containers').replaceWith(response);
860

    
861
			$('#containers').modal('show');
862

    
863
			// The button handler needs to be here because until the modal has been populated
864
			// the controls we need to attach handlers to do not exist
865
			$('#svcontbtn').prop("type", "button");
866
			$('#svcontbtn').removeAttr("href");
867

    
868
			$('#svcontbtn').click(function () {
869
				var ous = $('[id^=ou]').length;
870
				var i;
871

    
872
				$('#ldapauthcontainers').val("");
873

    
874
				for (i = 0; i < ous; i++) {
875
					if ($('#ou' + i).prop("checked")) {
876
						if ($('#ldapauthcontainers').val() != "") {
877
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
878
						}
879

    
880
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
881
					}
882
				}
883

    
884
				$('#containers').modal('hide');
885
			});
886
		});
887

    
888
	}
889

    
890
	function set_ldap_port() {
891
		if ($('#ldap_urltype').find(":selected").index() == 2)
892
			$('#ldap_port').val('636');
893
		else
894
			$('#ldap_port').val('389');
895
	}
896

    
897
	function set_required_port_fields() {
898
		if (document.getElementById("radius_srvcs").value == 'auth') {
899
			setRequired('radius_auth_port', true);
900
			setRequired('radius_acct_port', false);
901
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
902
			setRequired('radius_auth_port', false);
903
			setRequired('radius_acct_port', true);
904
		} else { // both
905
			setRequired('radius_auth_port', true);
906
			setRequired('radius_acct_port', true);
907
		}
908
	}
909

    
910
	// Hides all elements of the specified class. This will usually be a section
911
	function hideClass(s_class, hide) {
912
		if (hide)
913
			$('.' + s_class).hide();
914
		else
915
			$('.' + s_class).show();
916
	}
917

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

    
936
	// ---------- On initial page load ------------------------------------------------------------
937

    
938
<?php if ($act != 'edit') : ?>
939
	ldap_tmplchange();
940
<?php endif; ?>
941

    
942
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
943
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
944
	set_required_port_fields();
945

    
946
	if ($('#ldap_port').val() == "")
947
		set_ldap_port();
948

    
949
<?php
950
	if ($act == 'edit') {
951
?>
952
		$('#type option:not(:selected)').each(function(){
953
			$(this).attr('disabled', 'disabled');
954
		});
955

    
956
<?php
957
		if (!$input_errors) {
958
?>
959
		$('#name').prop("readonly", true);
960
<?php
961
		}
962
	}
963
?>
964
	// ---------- Click checkbox handlers ---------------------------------------------------------
965

    
966
	$('#ldap_tmpltype').on('change', function() {
967
		ldap_tmplchange();
968
	});
969

    
970
	$('#ldap_anon').click(function () {
971
		hideClass('ldapanon', this.checked);
972
	});
973

    
974
	$('#ldap_urltype').on('change', function() {
975
		set_ldap_port();
976
	});
977

    
978
	$('#Select').click(function () {
979
		select_clicked();
980
	});
981

    
982
	$('#ldap_extended_enabled').click(function () {
983
		hideClass('extended', !this.checked);
984
	});
985

    
986
	$('#radius_srvcs').on('change', function() {
987
		set_required_port_fields();
988
	});
989

    
990
});
991
//]]>
992
</script>
993
<?php
994
include("foot.inc");
(189-189/223)