Project

General

Profile

Download (31.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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 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 ($act == 'dup') {
116
	$dup = true;
117
	$act = 'edit';
118
}
119

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

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

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

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

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

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

    
146
		$pconfig['type'] = $a_server[$id]['type'];
147
		if (!$dup) {
148
			$pconfig['name'] = $a_server[$id]['name'];
149
		}
150

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

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

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

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

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

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

    
207
		}
208
	}
209
}
210

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

    
220
if ($dup) {
221
	unset($id);
222
}
223

    
224
if ($_POST['save']) {
225
	unset($input_errors);
226
	$pconfig = $_POST;
227

    
228
	/* input validation */
229

    
230
	if ($pconfig['type'] == "ldap") {
231
		$reqdfields = explode(" ",
232
			"name type ldap_host ldap_port " .
233
			"ldap_urltype ldap_protver ldap_scope " .
234
			"ldap_attr_user ldap_attr_group ldap_attr_member ldapauthcontainers");
235

    
236
		$reqdfieldsn = array(
237
			gettext("Descriptive name"),
238
			gettext("Type"),
239
			gettext("Hostname or IP"),
240
			gettext("Port value"),
241
			gettext("Transport"),
242
			gettext("Protocol version"),
243
			gettext("Search level"),
244
			gettext("User naming Attribute"),
245
			gettext("Group naming Attribute"),
246
			gettext("Group member attribute"),
247
			gettext("Authentication container"));
248

    
249
		if (!$pconfig['ldap_anon']) {
250
			$reqdfields[] = "ldap_binddn";
251
			$reqdfields[] = "ldap_bindpw";
252
			$reqdfieldsn[] = gettext("Bind user DN");
253
			$reqdfieldsn[] = gettext("Bind Password");
254
		}
255
	}
256

    
257
	if ($pconfig['type'] == "radius") {
258
		$reqdfields = explode(" ", "name type radius_protocol radius_host radius_srvcs");
259
		$reqdfieldsn = array(
260
			gettext("Descriptive name"),
261
			gettext("Type"),
262
			gettext("Radius Protocol"),
263
			gettext("Hostname or IP"),
264
			gettext("Services"));
265

    
266
		if ($pconfig['radius_srvcs'] == "both" ||
267
			$pconfig['radius_srvcs'] == "auth") {
268
			$reqdfields[] = "radius_auth_port";
269
			$reqdfieldsn[] = gettext("Authentication port");
270
		}
271

    
272
		if ($pconfig['radius_srvcs'] == "both" ||
273
			$pconfig['radius_srvcs'] == "acct") {
274
			$reqdfields[] = "radius_acct_port";
275
			$reqdfieldsn[] = gettext("Accounting port");
276
		}
277

    
278
		if (!isset($id)) {
279
			$reqdfields[] = "radius_secret";
280
			$reqdfieldsn[] = gettext("Shared Secret");
281
		}
282
	}
283

    
284
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
285

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

    
290
	if (auth_get_authserver($pconfig['name']) && !isset($id)) {
291
		$input_errors[] = gettext("An authentication server with the same name already exists.");
292
	}
293

    
294
	if (isset($id) && $config['system']['authserver'][$id] &&
295
	   ($config['system']['authserver'][$id]['name'] != $pconfig['name'])) {
296
		$input_errors[] = gettext("The name of an authentication server cannot be changed.");
297
	}
298

    
299
	if (($pconfig['type'] == "ldap") || ($pconfig['type'] == "radius")) {
300
		$to_field = "{$pconfig['type']}_timeout";
301
		if (isset($_POST[$to_field]) && !empty($_POST[$to_field]) && (!is_numeric($_POST[$to_field]) || (is_numeric($_POST[$to_field]) && ($_POST[$to_field] <= 0)))) {
302
			$input_errors[] = sprintf(gettext("%s Timeout value must be numeric and positive."), strtoupper($pconfig['type']));
303
		}
304
	}
305

    
306
	if (($pconfig['type'] == 'ldap') && isset($config['system']['webgui']['shellauth']) &&
307
	    ($config['system']['webgui']['authmode'] == $pconfig['name']) && empty($pconfig['ldap_pam_groupdn'])) {
308
		$input_errors[] = gettext("Shell Authentication Group DN must be specified if " . 
309
			"Shell Authentication is enabled for appliance.");
310
	}
311

    
312
	// https://redmine.pfsense.org/issues/4154
313
	if ($pconfig['type'] == "radius") {
314
		if (is_ipaddrv6($_POST['radius_host'])) {
315
			$input_errors[] = gettext("IPv6 does not work for RADIUS authentication, see Bug #4154.");
316
		}
317
	}
318

    
319
	if (!$input_errors) {
320
		$server = array();
321
		$server['refid'] = uniqid();
322
		if (isset($id) && $a_server[$id]) {
323
			$server = $a_server[$id];
324
		}
325

    
326
		$server['type'] = $pconfig['type'];
327
		$server['name'] = $pconfig['name'];
328

    
329
		if ($server['type'] == "ldap") {
330

    
331
			if (!empty($pconfig['ldap_caref'])) {
332
				$server['ldap_caref'] = $pconfig['ldap_caref'];
333
			}
334
			$server['host'] = $pconfig['ldap_host'];
335
			$server['ldap_port'] = $pconfig['ldap_port'];
336
			$server['ldap_urltype'] = $pconfig['ldap_urltype'];
337
			$server['ldap_protver'] = $pconfig['ldap_protver'];
338
			$server['ldap_scope'] = $pconfig['ldap_scope'];
339
			$server['ldap_basedn'] = $pconfig['ldap_basedn'];
340
			$server['ldap_authcn'] = $pconfig['ldapauthcontainers'];
341
			$server['ldap_extended_enabled'] = $pconfig['ldap_extended_enabled'];
342
			$server['ldap_extended_query'] = $pconfig['ldap_extended_query'];
343
			$server['ldap_attr_user'] = $pconfig['ldap_attr_user'];
344
			$server['ldap_attr_group'] = $pconfig['ldap_attr_group'];
345
			$server['ldap_attr_member'] = $pconfig['ldap_attr_member'];
346

    
347
			$server['ldap_attr_groupobj'] = empty($pconfig['ldap_attr_groupobj']) ? "posixGroup" : $pconfig['ldap_attr_groupobj'];
348
			$server['ldap_pam_groupdn'] = $pconfig['ldap_pam_groupdn'];
349

    
350
			if ($pconfig['ldap_utf8'] == "yes") {
351
				$server['ldap_utf8'] = true;
352
			} else {
353
				unset($server['ldap_utf8']);
354
			}
355
			if ($pconfig['ldap_nostrip_at'] == "yes") {
356
				$server['ldap_nostrip_at'] = true;
357
			} else {
358
				unset($server['ldap_nostrip_at']);
359
			}
360
			if ($pconfig['ldap_allow_unauthenticated'] == "yes") {
361
				$server['ldap_allow_unauthenticated'] = true;
362
			} else {
363
				unset($server['ldap_allow_unauthenticated']);
364
			}
365
			if ($pconfig['ldap_rfc2307'] == "yes") {
366
				$server['ldap_rfc2307'] = true;
367
			} else {
368
				unset($server['ldap_rfc2307']);
369
			}
370
			if ($pconfig['ldap_rfc2307_userdn'] == "yes") {
371
				$server['ldap_rfc2307_userdn'] = true;
372
			} else {
373
				unset($server['ldap_rfc2307_userdn']);
374
			}
375

    
376

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

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

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

    
394
			$server['radius_protocol'] = $pconfig['radius_protocol'];
395
			$server['host'] = $pconfig['radius_host'];
396
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
397

    
398
			if ($pconfig['radius_secret']) {
399
				$server['radius_secret'] = $pconfig['radius_secret'];
400
			}
401

    
402
			if ($pconfig['radius_timeout']) {
403
				$server['radius_timeout'] = $pconfig['radius_timeout'];
404
			} else {
405
				$server['radius_timeout'] = 5;
406
			}
407

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

    
413
			if ($pconfig['radius_srvcs'] == "auth") {
414
				$server['radius_auth_port'] = $pconfig['radius_auth_port'];
415
				unset($server['radius_acct_port']);
416
			}
417

    
418
			if ($pconfig['radius_srvcs'] == "acct") {
419
				$server['radius_acct_port'] = $pconfig['radius_acct_port'];
420
				unset($server['radius_auth_port']);
421
			}
422
		}
423

    
424
		if (isset($id) && $config['system']['authserver'][$id]) {
425
			$config['system']['authserver'][$id] = $server;
426
		} else {
427
			$config['system']['authserver'][] = $server;
428
		}
429

    
430
		if (isset($config['system']['webgui']['shellauth']) &&
431
		    ($config['system']['webgui']['authmode'] == $pconfig['name'])) {
432
			set_pam_auth();
433
		}
434

    
435
		write_config("Authentication Servers settings saved");
436

    
437
		pfSenseHeader("system_authservers.php");
438
	}
439
}
440

    
441
function build_radiusnas_list() {
442
	global $config;
443
	$list = array();
444

    
445
	$iflist = get_configured_interface_with_descr();
446
	foreach ($iflist as $ifdesc => $ifdescr) {
447
		$ipaddr = get_interface_ip($ifdesc);
448
		if (is_ipaddr($ipaddr)) {
449
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
450
		}
451
	}
452

    
453
	if (is_array($config['virtualip']['vip'])) {
454
		foreach ($config['virtualip']['vip'] as $sn) {
455
			if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
456
				$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
457
				$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
458
				$len = $end - $start;
459

    
460
				for ($i = 0; $i <= $len; $i++) {
461
					$snip = long2ip32($start+$i);
462
					$list[$snip] = $sn['descr'] . ' - ' . $snip;
463
				}
464
			} else {
465
				$list[$sn['subnet']] = $sn['descr'] . ' - ' . $sn['subnet'];
466
			}
467
		}
468
	}
469

    
470
	return($list);
471
}
472

    
473
// On error, restore the form contents so the user doesn't have to re-enter too much
474
if ($_POST && $input_errors) {
475
	$pconfig = $_POST;
476
	$pconfig['ldap_authcn'] = $_POST['ldapauthcontainers'];
477
	$pconfig['ldap_template'] = $_POST['ldap_tmpltype'];
478
}
479

    
480
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
481
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
482

    
483
if ($act == "new" || $act == "edit" || $input_errors) {
484
	$pgtitle[] = gettext('Edit');
485
	$pglinks[] = "@self";
486
}
487
$shortcut_section = "authentication";
488
include("head.inc");
489

    
490
if ($input_errors) {
491
	print_input_errors($input_errors);
492
}
493

    
494
if ($savemsg) {
495
	print_info_box($savemsg, 'success');
496
}
497

    
498
$tab_array = array();
499
if (!isAllowedPage("system_usermanager.php")) {
500
       $tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
501
} else {
502
       $tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
503
}
504
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
505
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
506
$tab_array[] = array(gettext("Authentication Servers"), true, "system_authservers.php");
507
display_top_tabs($tab_array);
508

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

    
545
<nav class="action-buttons">
546
	<a href="?act=new" class="btn btn-success btn-sm">
547
		<i class="fa fa-plus icon-embed-btn"></i>
548
		<?=gettext("Add")?>
549
	</a>
550
</nav>
551
<?php
552
	include("foot.inc");
553
	exit;
554
}
555

    
556
$form = new Form;
557
$form->setAction('system_authservers.php?act=edit');
558

    
559
$form->addGlobal(new Form_Input(
560
	'userid',
561
	null,
562
	'hidden',
563
	$id
564
));
565

    
566
$section = new Form_Section('Server Settings');
567

    
568
$section->addInput($input = new Form_Input(
569
	'name',
570
	'*Descriptive name',
571
	'text',
572
	$pconfig['name']
573
));
574

    
575
$section->addInput($input = new Form_Select(
576
	'type',
577
	'*Type',
578
	$pconfig['type'],
579
	$auth_server_types
580
))->toggles();
581

    
582
$form->add($section);
583

    
584
// ==== LDAP settings =========================================================
585
$section = new Form_Section('LDAP Server Settings');
586
$section->addClass('toggle-ldap collapse');
587

    
588
if (!isset($pconfig['type']) || $pconfig['type'] == 'ldap')
589
	$section->addClass('in');
590

    
591
$section->addInput(new Form_Input(
592
	'ldap_host',
593
	'*Hostname or IP address',
594
	'text',
595
	$pconfig['ldap_host']
596
))->setHelp('NOTE: When using SSL/TLS or STARTTLS, this hostname MUST match a Subject '.
597
	'Alternative Name (SAN) or the Common Name (CN) of the LDAP server SSL/TLS Certificate.');
598

    
599
$section->addInput(new Form_Input(
600
	'ldap_port',
601
	'*Port value',
602
	'number',
603
	$pconfig['ldap_port']
604
));
605

    
606
$section->addInput(new Form_Select(
607
	'ldap_urltype',
608
	'*Transport',
609
	$pconfig['ldap_urltype'],
610
	array_combine(array_keys($ldap_urltypes), array_keys($ldap_urltypes))
611
));
612

    
613
if (empty($a_ca))
614
{
615
	$section->addInput(new Form_StaticText(
616
		'Peer Certificate Authority',
617
		'No Certificate Authorities defined.<br/>Create one under <a href="system_camanager.php">System &gt; Cert. Manager</a>.'
618
	));
619
}
620
else
621
{
622
	$ldapCaRef = array( 'global' => 'Global Root CA List' );
623
	foreach ($a_ca as $ca)
624
		$ldapCaRef[ $ca['refid'] ] = $ca['descr'];
625

    
626
	$section->addInput(new Form_Select(
627
		'ldap_caref',
628
		'Peer Certificate Authority',
629
		$pconfig['ldap_caref'],
630
		$ldapCaRef
631
	))->setHelp('This CA is used to validate the LDAP server certificate when '.
632
		'\'SSL/TLS Encrypted\' or \'STARTTLS Encrypted\' Transport is active. '.
633
		'This CA must match the CA used by the LDAP server.');
634
}
635

    
636
$section->addInput(new Form_Select(
637
	'ldap_protver',
638
	'*Protocol version',
639
	$pconfig['ldap_protver'],
640
	array_combine($ldap_protvers, $ldap_protvers)
641
));
642

    
643
$section->addInput(new Form_Input(
644
	'ldap_timeout',
645
	'Server Timeout',
646
	'number',
647
	$pconfig['ldap_timeout'],
648
	['placeholder' => 25]
649
))->setHelp('Timeout for LDAP operations (seconds)');
650

    
651
$group = new Form_Group('Search scope');
652

    
653
$SSF = new Form_Select(
654
	'ldap_scope',
655
	'*Level',
656
	$pconfig['ldap_scope'],
657
	$ldap_scopes
658
);
659

    
660
$SSB = new Form_Input(
661
	'ldap_basedn',
662
	'Base DN',
663
	'text',
664
	$pconfig['ldap_basedn']
665
);
666

    
667

    
668
$section->addInput(new Form_StaticText(
669
	'Search scope',
670
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
671
));
672

    
673
$group = new Form_Group('*Authentication containers');
674
$group->add(new Form_Input(
675
	'ldapauthcontainers',
676
	'Containers',
677
	'text',
678
	$pconfig['ldap_authcn']
679
))->setHelp('Note: Semi-Colon separated. This will be prepended to the search '.
680
	'base dn above or the full container path can be specified containing a dc= '.
681
	'component.%1$sExample: CN=Users;DC=example,DC=com or OU=Staff;OU=Freelancers', '<br/>');
682

    
683
$group->add(new Form_Button(
684
	'Select',
685
	'Select a container',
686
	null,
687
	'fa-search'
688
))->setAttribute('type','button')->addClass('btn-info');
689

    
690
$section->add($group);
691

    
692
$section->addInput(new Form_Checkbox(
693
	'ldap_extended_enabled',
694
	'Extended query',
695
	'Enable extended query',
696
	$pconfig['ldap_extended_enabled']
697
));
698

    
699
$group = new Form_Group('Query');
700
$group->addClass('extended');
701

    
702
$group->add(new Form_Input(
703
	'ldap_extended_query',
704
	'Query',
705
	'text',
706
	$pconfig['ldap_extended_query']
707
))->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=*))');
708

    
709
$section->add($group);
710

    
711
$section->addInput(new Form_Checkbox(
712
	'ldap_anon',
713
	'Bind anonymous',
714
	'Use anonymous binds to resolve distinguished names',
715
	$pconfig['ldap_anon']
716
));
717

    
718
$group = new Form_Group('*Bind credentials');
719
$group->addClass('ldapanon');
720

    
721
$group->add(new Form_Input(
722
	'ldap_binddn',
723
	'User DN:',
724
	'text',
725
	$pconfig['ldap_binddn']
726
));
727

    
728
$group->add(new Form_Input(
729
	'ldap_bindpw',
730
	'Password',
731
	'password',
732
	$pconfig['ldap_bindpw']
733
));
734
$section->add($group);
735

    
736
if (!isset($id)) {
737
	$template_list = array();
738

    
739
	foreach ($ldap_templates as $option => $template) {
740
		$template_list[$option] = $template['desc'];
741
	}
742

    
743
	$section->addInput(new Form_Select(
744
		'ldap_tmpltype',
745
		'Initial Template',
746
		$pconfig['ldap_template'],
747
		$template_list
748
	));
749
}
750

    
751
$section->addInput(new Form_Input(
752
	'ldap_attr_user',
753
	'*User naming attribute',
754
	'text',
755
	$pconfig['ldap_attr_user']
756
));
757

    
758
$section->addInput(new Form_Input(
759
	'ldap_attr_group',
760
	'*Group naming attribute',
761
	'text',
762
	$pconfig['ldap_attr_group']
763
));
764

    
765
$section->addInput(new Form_Input(
766
	'ldap_attr_member',
767
	'*Group member attribute',
768
	'text',
769
	$pconfig['ldap_attr_member']
770
));
771

    
772
$section->addInput(new Form_Checkbox(
773
	'ldap_rfc2307',
774
	'RFC 2307 Groups',
775
	'LDAP Server uses RFC 2307 style group membership',
776
	$pconfig['ldap_rfc2307']
777
))->setHelp('RFC 2307 style group membership has members listed on the group '.
778
	'object rather than using groups listed on user object. Leave unchecked '.
779
	'for Active Directory style group membership (RFC 2307bis).');
780

    
781
$group = new Form_Group('RFC 2307 User DN');
782
$group->addClass('ldap_rfc2307_userdn');
783

    
784
$group->add(new Form_Checkbox(
785
	'ldap_rfc2307_userdn',
786
	'RFC 2307 user DN',
787
	'RFC 2307 Use DN for username search.',
788
	$pconfig['ldap_rfc2307_userdn']
789
))->setHelp('Use DN for username search, i.e. "(member=CN=Username,CN=Users,DC=example,DC=com)".');
790

    
791
$section->add($group);
792

    
793
$section->addInput(new Form_Input(
794
	'ldap_attr_groupobj',
795
	'Group Object Class',
796
	'text',
797
	$pconfig['ldap_attr_groupobj'],
798
	['placeholder' => 'posixGroup']
799
))->setHelp('Object class used for groups in RFC2307 mode. '.
800
	'Typically "posixGroup" or "group".');
801

    
802
$section->addInput(new Form_Input(
803
	'ldap_pam_groupdn',
804
	'Shell Authentication Group DN',
805
	'text',
806
	$pconfig['ldap_pam_groupdn']
807
))->setHelp('If LDAP server is used for shell authentication, user must be a member ' .
808
	    'of this group and have a valid posixAccount attributes to be able to login.%s Example: CN=Remoteshellusers,CN=Users,DC=example,DC=com',
809
	    '<br/>');
810

    
811
$section->addInput(new Form_Checkbox(
812
	'ldap_utf8',
813
	'UTF8 Encode',
814
	'UTF8 encode LDAP parameters before sending them to the server.',
815
	$pconfig['ldap_utf8']
816
))->setHelp('Required to support international characters, but may not be '.
817
	'supported by every LDAP server.');
818

    
819
$section->addInput(new Form_Checkbox(
820
	'ldap_nostrip_at',
821
	'Username Alterations',
822
	'Do not strip away parts of the username after the @ symbol',
823
	$pconfig['ldap_nostrip_at']
824
))->setHelp('e.g. user@host becomes user when unchecked.');
825

    
826
$section->addInput(new Form_Checkbox(
827
	'ldap_allow_unauthenticated',
828
	'Allow unauthenticated bind',
829
	'Allow unauthenticated bind',
830
	$pconfig['ldap_allow_unauthenticated']
831
))->setHelp('Unauthenticated binds are bind with an existing login but with an empty password. '.
832
         'Some LDAP servers (Microsoft AD) allow this type of bind without any possiblity to disable it.');
833

    
834
$form->add($section);
835

    
836
// ==== RADIUS section ========================================================
837
$section = new Form_Section('RADIUS Server Settings');
838
$section->addClass('toggle-radius collapse');
839

    
840
$section->addInput(new Form_Select(
841
	'radius_protocol',
842
	'*Protocol',
843
	$pconfig['radius_protocol'],
844
	$radius_protocol
845
));
846

    
847
$section->addInput(new Form_Input(
848
	'radius_host',
849
	'*Hostname or IP address',
850
	'text',
851
	$pconfig['radius_host']
852
));
853

    
854
$section->addInput(new Form_Input(
855
	'radius_secret',
856
	'*Shared Secret',
857
	'password',
858
	$pconfig['radius_secret']
859
));
860

    
861
$section->addInput(new Form_Select(
862
	'radius_srvcs',
863
	'*Services offered',
864
	$pconfig['radius_srvcs'],
865
	$radius_srvcs
866
));
867

    
868
$section->addInput(new Form_Input(
869
	'radius_auth_port',
870
	'Authentication port',
871
	'number',
872
	$pconfig['radius_auth_port']
873
));
874

    
875
$section->addInput(new Form_Input(
876
	'radius_acct_port',
877
	'Accounting port',
878
	'number',
879
	$pconfig['radius_acct_port']
880
));
881

    
882
$section->addInput(new Form_Input(
883
	'radius_timeout',
884
	'Authentication Timeout',
885
	'number',
886
	$pconfig['radius_timeout']
887
))->setHelp('This value controls how long, in seconds, that the RADIUS '.
888
	'server may take to respond to an authentication request. If left blank, the '.
889
	'default value is 5 seconds. NOTE: If using an interactive two-factor '.
890
	'authentication system, increase this timeout to account for how long it will '.
891
	'take the user to receive and enter a token.');
892

    
893
$section->addInput(new Form_Select(
894
	'radius_nasip_attribute',
895
	'RADIUS NAS IP Attribute',
896
	$pconfig['radius_nasip_attribute'],
897
	build_radiusnas_list()
898
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Acccess-Requests.<br />'.
899
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
900

    
901
if (isset($id) && $a_server[$id])
902
{
903
	$form->addGlobal(new Form_Input(
904
		'id',
905
		null,
906
		'hidden',
907
		$id
908
	));
909
}
910

    
911
$form->add($section);
912

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

    
916
$form->add($modal);
917

    
918
print $form;
919
?>
920
<script type="text/javascript">
921
//<![CDATA[
922
events.push(function() {
923

    
924
	// Create an AJAX request (to this page) to get the container list and controls
925
	function select_clicked() {
926
		if (document.getElementById("ldap_port").value == '' ||
927
			document.getElementById("ldap_host").value == '' ||
928
			document.getElementById("ldap_scope").value == '' ||
929
			document.getElementById("ldap_basedn").value == '' ||
930
			document.getElementById("ldapauthcontainers").value == '') {
931
			alert("<?=gettext("Please fill the required values.");?>");
932
			return;
933
		}
934

    
935
		if (!document.getElementById("ldap_anon").checked) {
936
			if (document.getElementById("ldap_binddn").value == '' ||
937
				document.getElementById("ldap_bindpw").value == '') {
938
				alert("<?=gettext("Please fill the bind username/password.");?>");
939
				return;
940
			}
941
		}
942

    
943
		var ajaxRequest;
944
		var authserver = $('#authmode').val();
945
		var cert;
946

    
947
<?php if (count($a_ca) > 0): ?>
948
			cert = $('#ldap_caref').val();
949
<?php else: ?>
950
			cert = '';
951
<?php endif; ?>
952
/*
953
		$('#containers').modal('show');
954
		$('#serverlist').parent('div').prev('label').remove();
955
		$('#serverlist').parent('div').removeClass("col-sm-10");
956
		$('#serverlist').parent('div').addClass("col-sm-12");
957
*/
958
		ajaxRequest = $.ajax(
959
			{
960
				url: "/system_authservers.php",
961
				type: "post",
962
				data: {
963
					ajax: 	"ajax",
964
					port: 	$('#ldap_port').val(),
965
					host: 	$('#ldap_host').val(),
966
					scope: 	$('#ldap_scope').val(),
967
					basedn: $('#ldap_basedn').val(),
968
					binddn: $('#ldap_binddn').val(),
969
					bindpw: $('#ldap_bindpw').val(),
970
					urltype:$('#ldap_urltype').val(),
971
					proto:  $('#ldap_protver').val(),
972
					authcn: $('#ldapauthcontainers').val(),
973
					cert:   cert
974
				}
975
			}
976
		);
977

    
978
		// Deal with the results of the above ajax call
979
		ajaxRequest.done(function (response, textStatus, jqXHR) {
980
			$('#containers').replaceWith(response);
981

    
982
			$('#containers').modal('show');
983

    
984
			// The button handler needs to be here because until the modal has been populated
985
			// the controls we need to attach handlers to do not exist
986
			$('#svcontbtn').prop("type", "button");
987
			$('#svcontbtn').removeAttr("href");
988

    
989
			$('#svcontbtn').click(function () {
990
				var ous = $('[id^=ou]').length;
991
				var i;
992

    
993
				$('#ldapauthcontainers').val("");
994

    
995
				for (i = 0; i < ous; i++) {
996
					if ($('#ou' + i).prop("checked")) {
997
						if ($('#ldapauthcontainers').val() != "") {
998
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
999
						}
1000

    
1001
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
1002
					}
1003
				}
1004

    
1005
				$('#containers').modal('hide');
1006
			});
1007
		});
1008

    
1009
	}
1010

    
1011
	function set_ldap_port() {
1012
		if ($('#ldap_urltype').find(":selected").index() == 2)
1013
			$('#ldap_port').val('636');
1014
		else
1015
			$('#ldap_port').val('389');
1016
	}
1017

    
1018
	function set_required_port_fields() {
1019
		if (document.getElementById("radius_srvcs").value == 'auth') {
1020
			setRequired('radius_auth_port', true);
1021
			setRequired('radius_acct_port', false);
1022
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
1023
			setRequired('radius_auth_port', false);
1024
			setRequired('radius_acct_port', true);
1025
		} else { // both
1026
			setRequired('radius_auth_port', true);
1027
			setRequired('radius_acct_port', true);
1028
		}
1029
	}
1030

    
1031
	// Hides all elements of the specified class. This will usually be a section
1032
	function hideClass(s_class, hide) {
1033
		if (hide)
1034
			$('.' + s_class).hide();
1035
		else
1036
			$('.' + s_class).show();
1037
	}
1038

    
1039
	function ldap_tmplchange() {
1040
		switch ($('#ldap_tmpltype').find(":selected").index()) {
1041
<?php
1042
		$index = 0;
1043
		foreach ($ldap_templates as $tmpldata):
1044
?>
1045
			case <?=$index;?>:
1046
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
1047
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
1048
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
1049
				$("#ldap_allow_unauthenticated").attr("checked", <?=$tmpldata['allow_unauthenticated'];?>);
1050
				break;
1051
<?php
1052
			$index++;
1053
		endforeach;
1054
?>
1055
		}
1056
	}
1057

    
1058
	// ---------- On initial page load ------------------------------------------------------------
1059

    
1060
<?php if ($act != 'edit') : ?>
1061
	ldap_tmplchange();
1062
<?php endif; ?>
1063

    
1064
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
1065
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
1066
	hideClass('ldap_rfc2307_userdn', !$('#ldap_rfc2307').prop('checked'));
1067
	set_required_port_fields();
1068

    
1069
	if ($('#ldap_port').val() == "")
1070
		set_ldap_port();
1071

    
1072
<?php
1073
	if ($act == 'edit') {
1074
?>
1075
		$('#type option:not(:selected)').each(function(){
1076
			$(this).attr('disabled', 'disabled');
1077
		});
1078

    
1079
<?php
1080
		if (!$input_errors && !$dup) {
1081
?>
1082
		$('#name').prop("readonly", true);
1083
<?php
1084
		}
1085
	}
1086
?>
1087
	// ---------- Click checkbox handlers ---------------------------------------------------------
1088

    
1089
	$('#ldap_tmpltype').on('change', function() {
1090
		ldap_tmplchange();
1091
	});
1092

    
1093
	$('#ldap_anon').click(function () {
1094
		hideClass('ldapanon', this.checked);
1095
	});
1096

    
1097
	$('#ldap_urltype').on('change', function() {
1098
		set_ldap_port();
1099
	});
1100

    
1101
	$('#Select').click(function () {
1102
		select_clicked();
1103
	});
1104

    
1105
	$('#ldap_extended_enabled').click(function () {
1106
		hideClass('extended', !this.checked);
1107
	});
1108

    
1109
	$('#ldap_rfc2307').click(function () {
1110
		hideClass('ldap_rfc2307_userdn', !this.checked);
1111
	});
1112

    
1113
	$('#radius_srvcs').on('change', function() {
1114
		set_required_port_fields();
1115
	});
1116

    
1117
});
1118
//]]>
1119
</script>
1120
<?php
1121
include("foot.inc");
(191-191/227)