Project

General

Profile

Download (31 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2008 Shrew Soft Inc
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
##|+PRIV
26
##|*IDENT=page-system-authservers
27
##|*NAME=System: Authentication Servers
28
##|*DESCR=Allow access to the 'System: Authentication Servers' page.
29
##|*WARN=standard-warning-root
30
##|*MATCH=system_authservers.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34
require_once("auth.inc");
35
require_once("pfsense-utils.inc");
36

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

    
40
	$ous = array();
41
	$authcfg = array();
42

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

    
54
	$ous = ldap_get_user_ous(true, $authcfg);
55

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

    
62
		if (is_array($ous)) {
63
			$idx = 0;
64

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

    
74
				$idx++;
75
			}
76
		}
77

    
78
		$modal->add($group);
79

    
80
		// Create a "Save button"
81

    
82
		$btnsv = new Form_Button(
83
			'svcontbtn',
84
			'Save',
85
			null,
86
			'fa-save'
87
		);
88

    
89
		$btnsv->removeClass("btn-default)")->addClass("btn-primary");
90

    
91
		$modal->addInput(new Form_StaticText(
92
			'',
93
			$btnsv
94
		));
95

    
96
		print($modal);
97
	}
98

    
99
	exit;
100
}
101

    
102
$id = $_REQUEST['id'];
103

    
104
if (!is_array($config['system']['authserver'])) {
105
	$config['system']['authserver'] = array();
106
}
107

    
108
$a_server = array_values(auth_get_authserver_list());
109

    
110
init_config_arr(array('ca'));
111
$a_ca = &$config['ca'];
112

    
113
$act = $_REQUEST['act'];
114

    
115
if ($_POST['act'] == "del") {
116

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

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

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

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

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

    
141
		$pconfig['type'] = $a_server[$id]['type'];
142
		$pconfig['name'] = $a_server[$id]['name'];
143

    
144
		if ($pconfig['type'] == "ldap") {
145
			$pconfig['ldap_caref'] = $a_server[$id]['ldap_caref'];
146
			$pconfig['ldap_host'] = $a_server[$id]['host'];
147
			$pconfig['ldap_port'] = $a_server[$id]['ldap_port'];
148
			$pconfig['ldap_timeout'] = $a_server[$id]['ldap_timeout'];
149
			$pconfig['ldap_urltype'] = $a_server[$id]['ldap_urltype'];
150
			$pconfig['ldap_protver'] = $a_server[$id]['ldap_protver'];
151
			$pconfig['ldap_scope'] = $a_server[$id]['ldap_scope'];
152
			$pconfig['ldap_basedn'] = $a_server[$id]['ldap_basedn'];
153
			$pconfig['ldap_authcn'] = $a_server[$id]['ldap_authcn'];
154
			$pconfig['ldap_extended_enabled'] = $a_server[$id]['ldap_extended_enabled'];
155
			$pconfig['ldap_extended_query'] = $a_server[$id]['ldap_extended_query'];
156
			$pconfig['ldap_binddn'] = $a_server[$id]['ldap_binddn'];
157
			$pconfig['ldap_bindpw'] = $a_server[$id]['ldap_bindpw'];
158
			$pconfig['ldap_attr_user'] = $a_server[$id]['ldap_attr_user'];
159
			$pconfig['ldap_attr_group'] = $a_server[$id]['ldap_attr_group'];
160
			$pconfig['ldap_attr_member'] = $a_server[$id]['ldap_attr_member'];
161
			$pconfig['ldap_attr_groupobj'] = $a_server[$id]['ldap_attr_groupobj'];
162
			$pconfig['ldap_pam_groupdn'] = $a_server[$id]['ldap_pam_groupdn'];
163
			$pconfig['ldap_utf8'] = isset($a_server[$id]['ldap_utf8']);
164
			$pconfig['ldap_nostrip_at'] = isset($a_server[$id]['ldap_nostrip_at']);
165
			$pconfig['ldap_allow_unauthenticated'] = isset($a_server[$id]['ldap_allow_unauthenticated']);
166
			$pconfig['ldap_rfc2307'] = isset($a_server[$id]['ldap_rfc2307']);
167
			$pconfig['ldap_rfc2307_userdn'] = isset($a_server[$id]['ldap_rfc2307_userdn']);
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_nasip_attribute'] = $a_server[$id]['radius_nasip_attribute'];
178
			$pconfig['radius_auth_port'] = $a_server[$id]['radius_auth_port'];
179
			$pconfig['radius_acct_port'] = $a_server[$id]['radius_acct_port'];
180
			$pconfig['radius_secret'] = $a_server[$id]['radius_secret'];
181
			$pconfig['radius_timeout'] = $a_server[$id]['radius_timeout'];
182

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

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

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

    
200
		}
201
	}
202
}
203

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

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

    
217
	/* input validation */
218

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

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

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

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

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

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

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

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

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

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

    
283
	if (isset($id) && $config['system']['authserver'][$id] &&
284
	   ($config['system']['authserver'][$id]['name'] != $pconfig['name'])) {
285
		$input_errors[] = gettext("The name of an authentication server cannot be changed.");
286
	}
287

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

    
295
	if (($pconfig['type'] == 'ldap') && isset($config['system']['webgui']['shellauth']) &&
296
	    ($config['system']['webgui']['authmode'] == $pconfig['name']) && empty($pconfig['ldap_pam_groupdn'])) {
297
		$input_errors[] = gettext("Shell Authentication Group DN must be specified if " . 
298
			"Shell Authentication is enabled for appliance.");
299
	}
300

    
301
	// https://redmine.pfsense.org/issues/4154
302
	if ($pconfig['type'] == "radius") {
303
		if (is_ipaddrv6($_POST['radius_host'])) {
304
			$input_errors[] = gettext("IPv6 does not work for RADIUS authentication, see Bug #4154.");
305
		}
306
	}
307

    
308
	if (!$input_errors) {
309
		$server = array();
310
		$server['refid'] = uniqid();
311
		if (isset($id) && $a_server[$id]) {
312
			$server = $a_server[$id];
313
		}
314

    
315
		$server['type'] = $pconfig['type'];
316
		$server['name'] = $pconfig['name'];
317

    
318
		if ($server['type'] == "ldap") {
319

    
320
			if (!empty($pconfig['ldap_caref'])) {
321
				$server['ldap_caref'] = $pconfig['ldap_caref'];
322
			}
323
			$server['host'] = $pconfig['ldap_host'];
324
			$server['ldap_port'] = $pconfig['ldap_port'];
325
			$server['ldap_urltype'] = $pconfig['ldap_urltype'];
326
			$server['ldap_protver'] = $pconfig['ldap_protver'];
327
			$server['ldap_scope'] = $pconfig['ldap_scope'];
328
			$server['ldap_basedn'] = $pconfig['ldap_basedn'];
329
			$server['ldap_authcn'] = $pconfig['ldapauthcontainers'];
330
			$server['ldap_extended_enabled'] = $pconfig['ldap_extended_enabled'];
331
			$server['ldap_extended_query'] = $pconfig['ldap_extended_query'];
332
			$server['ldap_attr_user'] = $pconfig['ldap_attr_user'];
333
			$server['ldap_attr_group'] = $pconfig['ldap_attr_group'];
334
			$server['ldap_attr_member'] = $pconfig['ldap_attr_member'];
335

    
336
			$server['ldap_attr_groupobj'] = empty($pconfig['ldap_attr_groupobj']) ? "posixGroup" : $pconfig['ldap_attr_groupobj'];
337
			$server['ldap_pam_groupdn'] = $pconfig['ldap_pam_groupdn'];
338

    
339
			if ($pconfig['ldap_utf8'] == "yes") {
340
				$server['ldap_utf8'] = true;
341
			} else {
342
				unset($server['ldap_utf8']);
343
			}
344
			if ($pconfig['ldap_nostrip_at'] == "yes") {
345
				$server['ldap_nostrip_at'] = true;
346
			} else {
347
				unset($server['ldap_nostrip_at']);
348
			}
349
			if ($pconfig['ldap_allow_unauthenticated'] == "yes") {
350
				$server['ldap_allow_unauthenticated'] = true;
351
			} else {
352
				unset($server['ldap_allow_unauthenticated']);
353
			}
354
			if ($pconfig['ldap_rfc2307'] == "yes") {
355
				$server['ldap_rfc2307'] = true;
356
			} else {
357
				unset($server['ldap_rfc2307']);
358
			}
359
			if ($pconfig['ldap_rfc2307_userdn'] == "yes") {
360
				$server['ldap_rfc2307_userdn'] = true;
361
			} else {
362
				unset($server['ldap_rfc2307_userdn']);
363
			}
364

    
365

    
366
			if (!$pconfig['ldap_anon']) {
367
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
368
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
369
			} else {
370
				unset($server['ldap_binddn']);
371
				unset($server['ldap_bindpw']);
372
			}
373

    
374
			if ($pconfig['ldap_timeout']) {
375
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
376
			} else {
377
				$server['ldap_timeout'] = 25;
378
			}
379
		}
380

    
381
		if ($server['type'] == "radius") {
382

    
383
			$server['radius_protocol'] = $pconfig['radius_protocol'];
384
			$server['host'] = $pconfig['radius_host'];
385
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
386

    
387
			if ($pconfig['radius_secret']) {
388
				$server['radius_secret'] = $pconfig['radius_secret'];
389
			}
390

    
391
			if ($pconfig['radius_timeout']) {
392
				$server['radius_timeout'] = $pconfig['radius_timeout'];
393
			} else {
394
				$server['radius_timeout'] = 5;
395
			}
396

    
397
			if ($pconfig['radius_srvcs'] == "both") {
398
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
399
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
400
			}
401

    
402
			if ($pconfig['radius_srvcs'] == "auth") {
403
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
404
				unset($server['radius_acct_port']);
405
			}
406

    
407
			if ($pconfig['radius_srvcs'] == "acct") {
408
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
409
				unset($server['radius_auth_port']);
410
			}
411
		}
412

    
413
		if (isset($id) && $config['system']['authserver'][$id]) {
414
			$config['system']['authserver'][$id] = $server;
415
		} else {
416
			$config['system']['authserver'][] = $server;
417
		}
418

    
419
		if (isset($config['system']['webgui']['shellauth']) &&
420
		    ($config['system']['webgui']['authmode'] == $pconfig['name'])) {
421
			set_pam_auth();
422
		}
423

    
424
		write_config("Authentication Servers settings saved");
425

    
426
		pfSenseHeader("system_authservers.php");
427
	}
428
}
429

    
430
function build_radiusnas_list() {
431
	global $config;
432
	$list = array();
433

    
434
	$iflist = get_configured_interface_with_descr();
435
	foreach ($iflist as $ifdesc => $ifdescr) {
436
		$ipaddr = get_interface_ip($ifdesc);
437
		if (is_ipaddr($ipaddr)) {
438
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
439
		}
440
	}
441

    
442
	if (is_array($config['virtualip']['vip'])) {
443
		foreach ($config['virtualip']['vip'] as $sn) {
444
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
445
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
446
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
447
				$len = $end - $start;
448

    
449
				for ($i = 0; $i <= $len; $i++) {
450
					$snip = long2ip32($start+$i);
451
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
452
				}
453
			} else {
454
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
455
			}
456
		}
457
	}
458

    
459
	return($list);
460
}
461

    
462
// On error, restore the form contents so the user doesn't have to re-enter too much
463
if ($_POST && $input_errors) {
464
	$pconfig = $_POST;
465
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
466
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
467
}
468

    
469
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
470
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
471

    
472
if ($act == "new" || $act == "edit" || $input_errors) {
473
	$pgtitle[] = gettext('Edit');
474
	$pglinks[] = "@self";
475
}
476
$shortcut_section = "authentication";
477
include("head.inc");
478

    
479
if ($input_errors) {
480
	print_input_errors($input_errors);
481
}
482

    
483
if ($savemsg) {
484
	print_info_box($savemsg, 'success');
485
}
486

    
487
$tab_array = array();
488
if (!isAllowedPage("system_usermanager.php")) {
489
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
490
} else {
491
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
492
}
493
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
494
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
495
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
496
display_top_tabs($tab_array);
497

    
498
if (!($act == "new" || $act == "edit" || $input_errors)) {
499
?>
500
<div class="panel panel-default">
501
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Authentication Servers')?></h2></div>
502
	<div class="panel-body">
503
		<div class="table-responsive">
504
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
505
				<thead>
506
					<tr>
507
						<th><?=gettext("Server Name")?></th>
508
						<th><?=gettext("Type")?></th>
509
						<th><?=gettext("Host Name")?></th>
510
						<th><?=gettext("Actions")?></th>
511
					</tr>
512
				</thead>
513
				<tbody>
514
			<?php foreach ($a_server as $i => $server): ?>
515
					<tr>
516
						<td><?=htmlspecialchars($server['name'])?></td>
517
						<td><?=htmlspecialchars($auth_server_types[$server['type']])?></td>
518
						<td><?=htmlspecialchars($server['host'])?></td>
519
						<td>
520
						<?php if ($i < (count($a_server) - 1)): ?>
521
							<a class="fa fa-pencil" title="<?=gettext("Edit server"); ?>" href="system_authservers.php?act=edit&amp;id=<?=$i?>"></a>
522
							<a class="fa fa-trash"  title="<?=gettext("Delete server")?>" href="system_authservers.php?act=del&amp;id=<?=$i?>" usepost></a>
523
						<?php endif?>
524
						</td>
525
					</tr>
526
			<?php endforeach; ?>
527
				</tbody>
528
			</table>
529
		</div>
530
	</div>
531
</div>
532

    
533
<nav class="action-buttons">
534
	<a href="?act=new" class="btn btn-success btn-sm">
535
		<i class="fa fa-plus icon-embed-btn"></i>
536
		<?=gettext("Add")?>
537
	</a>
538
</nav>
539
<?php
540
	include("foot.inc");
541
	exit;
542
}
543

    
544
$form = new Form;
545
$form->setAction('system_authservers.php?act=edit');
546

    
547
$form->addGlobal(new Form_Input(
548
	'userid',
549
	null,
550
	'hidden',
551
	$id
552
));
553

    
554
$section = new Form_Section('Server Settings');
555

    
556
$section->addInput($input = new Form_Input(
557
	'name',
558
	'*Descriptive name',
559
	'text',
560
	$pconfig['name']
561
));
562

    
563
$section->addInput($input = new Form_Select(
564
	'type',
565
	'*Type',
566
	$pconfig['type'],
567
	$auth_server_types
568
))->toggles();
569

    
570
$form->add($section);
571

    
572
// ==== LDAP settings =========================================================
573
$section = new Form_Section('LDAP Server Settings');
574
$section->addClass('toggle-ldap collapse');
575

    
576
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
577
	$section->addClass('in');
578

    
579
$section->addInput(new Form_Input(
580
	'ldap_host',
581
	'*Hostname or IP address',
582
	'text',
583
	$pconfig['ldap_host']
584
))->setHelp('NOTE: When using SSL/TLS or STARTTLS, this hostname MUST match a Subject '.
585
	'Alternative Name (SAN) or the Common Name (CN) of the LDAP server SSL/TLS Certificate.');
586

    
587
$section->addInput(new Form_Input(
588
	'ldap_port',
589
	'*Port value',
590
	'number',
591
	$pconfig['ldap_port']
592
));
593

    
594
$section->addInput(new Form_Select(
595
	'ldap_urltype',
596
	'*Transport',
597
	$pconfig['ldap_urltype'],
598
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
599
));
600

    
601
if (empty($a_ca))
602
{
603
	$section->addInput(new Form_StaticText(
604
		'Peer Certificate Authority',
605
		'No Certificate Authorities defined.<br/>Create one under <a href="system_camanager.php">System &gt; Cert. Manager</a>.'
606
	));
607
}
608
else
609
{
610
	$ldapCaRef = array( 'global' => 'Global Root CA List' );
611
	foreach ($a_ca as $ca)
612
		$ldapCaRef[ $ca['refid'] ] = $ca['descr'];
613

    
614
	$section->addInput(new Form_Select(
615
		'ldap_caref',
616
		'Peer Certificate Authority',
617
		$pconfig['ldap_caref'],
618
		$ldapCaRef
619
	))->setHelp('This CA is used to validate the LDAP server certificate when '.
620
		'\'SSL/TLS Encrypted\' or \'STARTTLS Encrypted\' Transport is active. '.
621
		'This CA must match the CA used by the LDAP server.');
622
}
623

    
624
$section->addInput(new Form_Select(
625
	'ldap_protver',
626
	'*Protocol version',
627
	$pconfig['ldap_protver'],
628
	array_combine($ldap_protvers, $ldap_protvers)
629
));
630

    
631
$section->addInput(new Form_Input(
632
	'ldap_timeout',
633
	'Server Timeout',
634
	'number',
635
	$pconfig['ldap_timeout'],
636
	['placeholder' => 25]
637
))->setHelp('Timeout for LDAP operations (seconds)');
638

    
639
$group = new Form_Group('Search scope');
640

    
641
$SSF = new Form_Select(
642
	'ldap_scope',
643
	'*Level',
644
	$pconfig['ldap_scope'],
645
	$ldap_scopes
646
);
647

    
648
$SSB = new Form_Input(
649
	'ldap_basedn',
650
	'Base DN',
651
	'text',
652
	$pconfig['ldap_basedn']
653
);
654

    
655

    
656
$section->addInput(new Form_StaticText(
657
	'Search scope',
658
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
659
));
660

    
661
$group = new Form_Group('*Authentication containers');
662
$group->add(new Form_Input(
663
	'ldapauthcontainers',
664
	'Containers',
665
	'text',
666
	$pconfig['ldap_authcn']
667
))->setHelp('Note: Semi-Colon separated. This will be prepended to the search '.
668
	'base dn above or the full container path can be specified containing a dc= '.
669
	'component.%1$sExample: CN=Users;DC=example,DC=com or OU=Staff;OU=Freelancers', '<br/>');
670

    
671
$group->add(new Form_Button(
672
	'Select',
673
	'Select a container',
674
	null,
675
	'fa-search'
676
))->setAttribute('type','button')->addClass('btn-info');
677

    
678
$section->add($group);
679

    
680
$section->addInput(new Form_Checkbox(
681
	'ldap_extended_enabled',
682
	'Extended query',
683
	'Enable extended query',
684
	$pconfig['ldap_extended_enabled']
685
));
686

    
687
$group = new Form_Group('Query');
688
$group->addClass('extended');
689

    
690
$group->add(new Form_Input(
691
	'ldap_extended_query',
692
	'Query',
693
	'text',
694
	$pconfig['ldap_extended_query']
695
))->setHelp('Example (MSAD): memberOf=CN=Groupname,OU=MyGroups,DC=example,DC=com<br>Example (2307): |(&(objectClass=posixGroup)(cn=Groupname)(memberUid=*))(&(objectClass=posixGroup)(cn=anotherGroup)(memberUid=*))');
696

    
697
$section->add($group);
698

    
699
$section->addInput(new Form_Checkbox(
700
	'ldap_anon',
701
	'Bind anonymous',
702
	'Use anonymous binds to resolve distinguished names',
703
	$pconfig['ldap_anon']
704
));
705

    
706
$group = new Form_Group('*Bind credentials');
707
$group->addClass('ldapanon');
708

    
709
$group->add(new Form_Input(
710
	'ldap_binddn',
711
	'User DN:',
712
	'text',
713
	$pconfig['ldap_binddn']
714
));
715

    
716
$group->add(new Form_Input(
717
	'ldap_bindpw',
718
	'Password',
719
	'password',
720
	$pconfig['ldap_bindpw']
721
));
722
$section->add($group);
723

    
724
if (!isset($id)) {
725
	$template_list = array();
726

    
727
	foreach ($ldap_templates as $option => $template) {
728
		$template_list[$option] = $template['desc'];
729
	}
730

    
731
	$section->addInput(new Form_Select(
732
		'ldap_tmpltype',
733
		'Initial Template',
734
		$pconfig['ldap_template'],
735
		$template_list
736
	));
737
}
738

    
739
$section->addInput(new Form_Input(
740
	'ldap_attr_user',
741
	'*User naming attribute',
742
	'text',
743
	$pconfig['ldap_attr_user']
744
));
745

    
746
$section->addInput(new Form_Input(
747
	'ldap_attr_group',
748
	'*Group naming attribute',
749
	'text',
750
	$pconfig['ldap_attr_group']
751
));
752

    
753
$section->addInput(new Form_Input(
754
	'ldap_attr_member',
755
	'*Group member attribute',
756
	'text',
757
	$pconfig['ldap_attr_member']
758
));
759

    
760
$section->addInput(new Form_Checkbox(
761
	'ldap_rfc2307',
762
	'RFC 2307 Groups',
763
	'LDAP Server uses RFC 2307 style group membership',
764
	$pconfig['ldap_rfc2307']
765
))->setHelp('RFC 2307 style group membership has members listed on the group '.
766
	'object rather than using groups listed on user object. Leave unchecked '.
767
	'for Active Directory style group membership (RFC 2307bis).');
768

    
769
$group = new Form_Group('RFC 2307 User DN');
770
$group->addClass('ldap_rfc2307_userdn');
771

    
772
$group->add(new Form_Checkbox(
773
	'ldap_rfc2307_userdn',
774
	'RFC 2307 user DN',
775
	'RFC 2307 Use DN for username search.',
776
	$pconfig['ldap_rfc2307_userdn']
777
))->setHelp('Use DN for username search, i.e. "(member=CN=Username,CN=Users,DC=example,DC=com)".');
778

    
779
$section->add($group);
780

    
781
$section->addInput(new Form_Input(
782
	'ldap_attr_groupobj',
783
	'Group Object Class',
784
	'text',
785
	$pconfig['ldap_attr_groupobj'],
786
	['placeholder' => 'posixGroup']
787
))->setHelp('Object class used for groups in RFC2307 mode. '.
788
	'Typically "posixGroup" or "group".');
789

    
790
$section->addInput(new Form_Input(
791
	'ldap_pam_groupdn',
792
	'Shell Authentication Group DN',
793
	'text',
794
	$pconfig['ldap_pam_groupdn']
795
))->setHelp('If LDAP server is used for shell authentication, user must be a member ' .
796
	    'of this group and have a valid posixAccount attributes to be able to login.%s Example: CN=Remoteshellusers,CN=Users,DC=example,DC=com',
797
	    '<br/>');
798

    
799
$section->addInput(new Form_Checkbox(
800
	'ldap_utf8',
801
	'UTF8 Encode',
802
	'UTF8 encode LDAP parameters before sending them to the server.',
803
	$pconfig['ldap_utf8']
804
))->setHelp('Required to support international characters, but may not be '.
805
	'supported by every LDAP server.');
806

    
807
$section->addInput(new Form_Checkbox(
808
	'ldap_nostrip_at',
809
	'Username Alterations',
810
	'Do not strip away parts of the username after the @ symbol',
811
	$pconfig['ldap_nostrip_at']
812
))->setHelp('e.g. user@host becomes user when unchecked.');
813

    
814
$section->addInput(new Form_Checkbox(
815
	'ldap_allow_unauthenticated',
816
	'Allow unauthenticated bind',
817
	'Allow unauthenticated bind',
818
	$pconfig['ldap_allow_unauthenticated']
819
))->setHelp('Unauthenticated binds are bind with an existing login but with an empty password. '.
820
         'Some LDAP servers (Microsoft AD) allow this type of bind without any possiblity to disable it.');
821

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

    
824
// ==== RADIUS section ========================================================
825
$section = new Form_Section('RADIUS Server Settings');
826
$section->addClass('toggle-radius collapse');
827

    
828
$section->addInput(new Form_Select(
829
	'radius_protocol',
830
	'*Protocol',
831
	$pconfig['radius_protocol'],
832
	$radius_protocol
833
));
834

    
835
$section->addInput(new Form_Input(
836
	'radius_host',
837
	'*Hostname or IP address',
838
	'text',
839
	$pconfig['radius_host']
840
));
841

    
842
$section->addInput(new Form_Input(
843
	'radius_secret',
844
	'*Shared Secret',
845
	'password',
846
	$pconfig['radius_secret']
847
));
848

    
849
$section->addInput(new Form_Select(
850
	'radius_srvcs',
851
	'*Services offered',
852
	$pconfig['radius_srvcs'],
853
	$radius_srvcs
854
));
855

    
856
$section->addInput(new Form_Input(
857
	'radius_auth_port',
858
	'Authentication port',
859
	'number',
860
	$pconfig['radius_auth_port']
861
));
862

    
863
$section->addInput(new Form_Input(
864
	'radius_acct_port',
865
	'Accounting port',
866
	'number',
867
	$pconfig['radius_acct_port']
868
));
869

    
870
$section->addInput(new Form_Input(
871
	'radius_timeout',
872
	'Authentication Timeout',
873
	'number',
874
	$pconfig['radius_timeout']
875
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
876
	'server may take to respond to an authentication request. If left blank, the '.
877
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
878
	'authentication system, increase this timeout to account for how long it will '.
879
	'take the user to receive and enter a token.');
880

    
881
$section->addInput(new Form_Select(
882
	'radius_nasip_attribute',
883
	'RADIUS NAS IP Attribute',
884
	$pconfig['radius_nasip_attribute'],
885
	build_radiusnas_list()
886
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Acccess-Requests.<br />'.
887
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
888

    
889
if (isset($id) && $a_server[$id])
890
{
891
	$form->addGlobal(new Form_Input(
892
		'id',
893
		null,
894
		'hidden',
895
		$id
896
	));
897
}
898

    
899
$form->add($section);
900

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

    
904
$form->add($modal);
905

    
906
print $form;
907
?>
908
<script type="text/javascript">
909
//<![CDATA[
910
events.push(function() {
911

    
912
	// Create an AJAX request (to this page) to get the container list and controls
913
	function select_clicked() {
914
		if (document.getElementById("ldap_port").value == '' ||
915
			document.getElementById("ldap_host").value == '' ||
916
			document.getElementById("ldap_scope").value == '' ||
917
			document.getElementById("ldap_basedn").value == '' ||
918
			document.getElementById("ldapauthcontainers").value == '') {
919
			alert("<?=gettext("Please fill the required values.");?>");
920
			return;
921
		}
922

    
923
		if (!document.getElementById("ldap_anon").checked) {
924
			if (document.getElementById("ldap_binddn").value == '' ||
925
				document.getElementById("ldap_bindpw").value == '') {
926
				alert("<?=gettext("Please fill the bind username/password.");?>");
927
				return;
928
			}
929
		}
930

    
931
		var ajaxRequest;
932
		var authserver = $('#authmode').val();
933
		var cert;
934

    
935
<?php if (count($a_ca) > 0): ?>
936
			cert = $('#ldap_caref').val();
937
<?php else: ?>
938
			cert = '';
939
<?php endif; ?>
940
/*
941
		$('#containers').modal('show');
942
		$('#serverlist').parent('div').prev('label').remove();
943
		$('#serverlist').parent('div').removeClass("col-sm-10");
944
		$('#serverlist').parent('div').addClass("col-sm-12");
945
*/
946
		ajaxRequest = $.ajax(
947
			{
948
				url: "/system_authservers.php",
949
				type: "post",
950
				data: {
951
					ajax: 	"ajax",
952
					port: 	$('#ldap_port').val(),
953
					host: 	$('#ldap_host').val(),
954
					scope: 	$('#ldap_scope').val(),
955
					basedn: $('#ldap_basedn').val(),
956
					binddn: $('#ldap_binddn').val(),
957
					bindpw: $('#ldap_bindpw').val(),
958
					urltype:$('#ldap_urltype').val(),
959
					proto:  $('#ldap_protver').val(),
960
					authcn: $('#ldapauthcontainers').val(),
961
					cert:   cert
962
				}
963
			}
964
		);
965

    
966
		// Deal with the results of the above ajax call
967
		ajaxRequest.done(function (response, textStatus, jqXHR) {
968
			$('#containers').replaceWith(response);
969

    
970
			$('#containers').modal('show');
971

    
972
			// The button handler needs to be here because until the modal has been populated
973
			// the controls we need to attach handlers to do not exist
974
			$('#svcontbtn').prop("type", "button");
975
			$('#svcontbtn').removeAttr("href");
976

    
977
			$('#svcontbtn').click(function () {
978
				var ous = $('[id^=ou]').length;
979
				var i;
980

    
981
				$('#ldapauthcontainers').val("");
982

    
983
				for (i = 0; i < ous; i++) {
984
					if ($('#ou' + i).prop("checked")) {
985
						if ($('#ldapauthcontainers').val() != "") {
986
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
987
						}
988

    
989
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
990
					}
991
				}
992

    
993
				$('#containers').modal('hide');
994
			});
995
		});
996

    
997
	}
998

    
999
	function set_ldap_port() {
1000
		if ($('#ldap_urltype').find(":selected").index() == 2)
1001
			$('#ldap_port').val('636');
1002
		else
1003
			$('#ldap_port').val('389');
1004
	}
1005

    
1006
	function set_required_port_fields() {
1007
		if (document.getElementById("radius_srvcs").value == 'auth') {
1008
			setRequired('radius_auth_port', true);
1009
			setRequired('radius_acct_port', false);
1010
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
1011
			setRequired('radius_auth_port', false);
1012
			setRequired('radius_acct_port', true);
1013
		} else { // both
1014
			setRequired('radius_auth_port', true);
1015
			setRequired('radius_acct_port', true);
1016
		}
1017
	}
1018

    
1019
	// Hides all elements of the specified class. This will usually be a section
1020
	function hideClass(s_class, hide) {
1021
		if (hide)
1022
			$('.' + s_class).hide();
1023
		else
1024
			$('.' + s_class).show();
1025
	}
1026

    
1027
	function ldap_tmplchange() {
1028
		switch ($('#ldap_tmpltype').find(":selected").index()) {
1029
<?php
1030
		$index = 0;
1031
		foreach ($ldap_templates as $tmpldata):
1032
?>
1033
			case <?=$index;?>:
1034
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
1035
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
1036
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
1037
				$("#ldap_allow_unauthenticated").attr("checked", <?=$tmpldata['allow_unauthenticated'];?>);
1038
				break;
1039
<?php
1040
			$index++;
1041
		endforeach;
1042
?>
1043
		}
1044
	}
1045

    
1046
	// ---------- On initial page load ------------------------------------------------------------
1047

    
1048
<?php if ($act != 'edit') : ?>
1049
	ldap_tmplchange();
1050
<?php endif; ?>
1051

    
1052
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
1053
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
1054
	hideClass('ldap_rfc2307_userdn', !$('#ldap_rfc2307').prop('checked'));
1055
	set_required_port_fields();
1056

    
1057
	if ($('#ldap_port').val() == "")
1058
		set_ldap_port();
1059

    
1060
<?php
1061
	if ($act == 'edit') {
1062
?>
1063
		$('#type option:not(:selected)').each(function(){
1064
			$(this).attr('disabled', 'disabled');
1065
		});
1066

    
1067
<?php
1068
		if (!$input_errors) {
1069
?>
1070
		$('#name').prop("readonly", true);
1071
<?php
1072
		}
1073
	}
1074
?>
1075
	// ---------- Click checkbox handlers ---------------------------------------------------------
1076

    
1077
	$('#ldap_tmpltype').on('change', function() {
1078
		ldap_tmplchange();
1079
	});
1080

    
1081
	$('#ldap_anon').click(function () {
1082
		hideClass('ldapanon', this.checked);
1083
	});
1084

    
1085
	$('#ldap_urltype').on('change', function() {
1086
		set_ldap_port();
1087
	});
1088

    
1089
	$('#Select').click(function () {
1090
		select_clicked();
1091
	});
1092

    
1093
	$('#ldap_extended_enabled').click(function () {
1094
		hideClass('extended', !this.checked);
1095
	});
1096

    
1097
	$('#ldap_rfc2307').click(function () {
1098
		hideClass('ldap_rfc2307_userdn', !this.checked);
1099
	});
1100

    
1101
	$('#radius_srvcs').on('change', function() {
1102
		set_required_port_fields();
1103
	});
1104

    
1105
});
1106
//]]>
1107
</script>
1108
<?php
1109
include("foot.inc");
(193-193/229)