Project

General

Profile

Download (30.6 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-2024 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-solid 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_set_path('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_get_path('system/authserver', []) as $k => $as) {
130
		if ($as['name'] == $serverdeleted) {
131
			config_del_path("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
	if (!$input_errors) {
313
		$server = array();
314
		$server['refid'] = uniqid();
315
		if (isset($id) && $a_server[$id]) {
316
			$server = $a_server[$id];
317
		}
318

    
319
		$server['type'] = $pconfig['type'];
320
		$server['name'] = $pconfig['name'];
321

    
322
		if ($server['type'] == "ldap") {
323

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

    
340
			$server['ldap_attr_groupobj'] = empty($pconfig['ldap_attr_groupobj']) ? "posixGroup" : $pconfig['ldap_attr_groupobj'];
341
			$server['ldap_pam_groupdn'] = $pconfig['ldap_pam_groupdn'];
342

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

    
369

    
370
			if (!$pconfig['ldap_anon']) {
371
				$server['ldap_binddn'] = $pconfig['ldap_binddn'];
372
				$server['ldap_bindpw'] = $pconfig['ldap_bindpw'];
373
			} else {
374
				unset($server['ldap_binddn']);
375
				unset($server['ldap_bindpw']);
376
			}
377

    
378
			if ($pconfig['ldap_timeout']) {
379
				$server['ldap_timeout'] = $pconfig['ldap_timeout'];
380
			} else {
381
				$server['ldap_timeout'] = 25;
382
			}
383
		}
384

    
385
		if ($server['type'] == "radius") {
386

    
387
			$server['radius_protocol'] = $pconfig['radius_protocol'];
388
			$server['host'] = $pconfig['radius_host'];
389
			$server['radius_nasip_attribute'] = $pconfig['radius_nasip_attribute'];
390

    
391
			if ($pconfig['radius_secret']) {
392
				$server['radius_secret'] = $pconfig['radius_secret'];
393
			}
394

    
395
			if ($pconfig['radius_timeout']) {
396
				$server['radius_timeout'] = $pconfig['radius_timeout'];
397
			} else {
398
				$server['radius_timeout'] = 5;
399
			}
400

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

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

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

    
417
		if (isset($id) && $config['system']['authserver'][$id]) {
418
			$config['system']['authserver'][$id] = $server;
419
		} else {
420
			$config['system']['authserver'][] = $server;
421
		}
422

    
423
		if (isset($config['system']['webgui']['shellauth']) &&
424
		    ($config['system']['webgui']['authmode'] == $pconfig['name'])) {
425
			set_pam_auth();
426
		}
427

    
428
		write_config("Authentication Servers settings saved");
429

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

    
434
function build_radiusnas_list() {
435
	global $config;
436
	$list = array();
437

    
438
	$iflist = get_configured_interface_with_descr();
439
	foreach ($iflist as $ifdesc => $ifdescr) {
440
		$ipaddr = get_interface_ip($ifdesc);
441
		if (is_ipaddr($ipaddr)) {
442
			$list[$ifdesc] = $ifdescr . ' - ' . $ipaddr;
443
		}
444
	}
445

    
446
	foreach (config_get_path('virtualip/vip', []) as $sn) {
447
		if ($sn['mode'] == "proxyarp" && $sn['type'] == "network") {
448
			$start = ip2long32(gen_subnet($sn['subnet'], $sn['subnet_bits']));
449
			$end = ip2long32(gen_subnet_max($sn['subnet'], $sn['subnet_bits']));
450
			$len = $end - $start;
451

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

    
461

    
462
	return($list);
463
}
464

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

    
472
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Authentication Servers"));
473
$pglinks = array("", "system_usermanager.php", "system_authservers.php");
474

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

    
482
if ($input_errors) {
483
	print_input_errors($input_errors);
484
}
485

    
486
if ($savemsg) {
487
	print_info_box($savemsg, 'success');
488
}
489

    
490
$tab_array = array();
491
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
492
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
493
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
494
$tab_array[] = array(gettext("Change Password"), false, "system_usermanager_passwordmg.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-solid fa-pencil" title="<?=gettext("Edit server"); ?>" href="system_authservers.php?act=edit&amp;id=<?=$i?>"></a>
522
							<a class="fa-regular fa-clone" title="<?=gettext("Copy server"); ?>" href="system_authservers.php?act=dup&amp;id=<?=$i?>"></a>
523
							<a class="fa-solid fa-trash-can"  title="<?=gettext("Delete server")?>" href="system_authservers.php?act=del&amp;id=<?=$i?>" usepost></a>
524
						<?php endif?>
525
						</td>
526
					</tr>
527
			<?php endforeach; ?>
528
				</tbody>
529
			</table>
530
		</div>
531
	</div>
532
</div>
533

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

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

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

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

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

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

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

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

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

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

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

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

    
602
$ldapCaRef = array('global' => 'Global Root CA List');
603
foreach ($a_ca as $ca) {
604
	$ldapCaRef[$ca['refid']] = $ca['descr'];
605
}
606

    
607
$section->addInput(new Form_Select(
608
	'ldap_caref',
609
	'Peer Certificate Authority',
610
	$pconfig['ldap_caref'],
611
	$ldapCaRef
612
))->setHelp('This CA is used to validate the LDAP server certificate when '.
613
	'\'SSL/TLS Encrypted\' or \'STARTTLS Encrypted\' Transport is active. '.
614
	'This CA must match the CA used by the LDAP server.');
615

    
616
$section->addInput(new Form_Select(
617
	'ldap_protver',
618
	'*Protocol version',
619
	$pconfig['ldap_protver'],
620
	array_combine($ldap_protvers, $ldap_protvers)
621
));
622

    
623
$section->addInput(new Form_Input(
624
	'ldap_timeout',
625
	'Server Timeout',
626
	'number',
627
	$pconfig['ldap_timeout'],
628
	['placeholder' => 25]
629
))->setHelp('Timeout for LDAP operations (seconds)');
630

    
631
$group = new Form_Group('Search scope');
632

    
633
$SSF = new Form_Select(
634
	'ldap_scope',
635
	'*Level',
636
	$pconfig['ldap_scope'],
637
	$ldap_scopes
638
);
639

    
640
$SSB = new Form_Input(
641
	'ldap_basedn',
642
	'Base DN',
643
	'text',
644
	$pconfig['ldap_basedn']
645
);
646

    
647

    
648
$section->addInput(new Form_StaticText(
649
	'Search scope',
650
	'Level ' . $SSF . '<br />' . 'Base DN' . $SSB
651
));
652

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

    
663
$group->add(new Form_Button(
664
	'Select',
665
	'Select a container',
666
	null,
667
	'fa-solid fa-search'
668
))->setAttribute('type','button')->addClass('btn-info');
669

    
670
$section->add($group);
671

    
672
$section->addInput(new Form_Checkbox(
673
	'ldap_extended_enabled',
674
	'Extended query',
675
	'Enable extended query',
676
	$pconfig['ldap_extended_enabled']
677
));
678

    
679
$group = new Form_Group('Query');
680
$group->addClass('extended');
681

    
682
$group->add(new Form_Input(
683
	'ldap_extended_query',
684
	'Query',
685
	'text',
686
	$pconfig['ldap_extended_query']
687
))->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=*))');
688

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

    
691
$section->addInput(new Form_Checkbox(
692
	'ldap_anon',
693
	'Bind anonymous',
694
	'Use anonymous binds to resolve distinguished names',
695
	$pconfig['ldap_anon']
696
));
697

    
698
$group = new Form_Group('*Bind credentials');
699
$group->addClass('ldapanon');
700

    
701
$group->add(new Form_Input(
702
	'ldap_binddn',
703
	'User DN:',
704
	'text',
705
	$pconfig['ldap_binddn']
706
));
707

    
708
$group->add(new Form_Input(
709
	'ldap_bindpw',
710
	'Password',
711
	'password',
712
	$pconfig['ldap_bindpw']
713
));
714
$section->add($group);
715

    
716
if (!isset($id)) {
717
	$template_list = array();
718

    
719
	foreach ($ldap_templates as $option => $template) {
720
		$template_list[$option] = $template['desc'];
721
	}
722

    
723
	$section->addInput(new Form_Select(
724
		'ldap_tmpltype',
725
		'Initial Template',
726
		$pconfig['ldap_template'],
727
		$template_list
728
	));
729
}
730

    
731
$section->addInput(new Form_Input(
732
	'ldap_attr_user',
733
	'*User naming attribute',
734
	'text',
735
	$pconfig['ldap_attr_user']
736
));
737

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

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

    
752
$section->addInput(new Form_Checkbox(
753
	'ldap_rfc2307',
754
	'RFC 2307 Groups',
755
	'LDAP Server uses RFC 2307 style group membership',
756
	$pconfig['ldap_rfc2307']
757
))->setHelp('RFC 2307 style group membership has members listed on the group '.
758
	'object rather than using groups listed on user object. Leave unchecked '.
759
	'for Active Directory style group membership (RFC 2307bis).');
760

    
761
$group = new Form_Group('RFC 2307 User DN');
762
$group->addClass('ldap_rfc2307_userdn');
763

    
764
$group->add(new Form_Checkbox(
765
	'ldap_rfc2307_userdn',
766
	'RFC 2307 user DN',
767
	'RFC 2307 Use DN for username search.',
768
	$pconfig['ldap_rfc2307_userdn']
769
))->setHelp('Use DN for username search, i.e. "(member=CN=Username,CN=Users,DC=example,DC=com)".');
770

    
771
$section->add($group);
772

    
773
$section->addInput(new Form_Input(
774
	'ldap_attr_groupobj',
775
	'Group Object Class',
776
	'text',
777
	$pconfig['ldap_attr_groupobj'],
778
	['placeholder' => 'posixGroup']
779
))->setHelp('Object class used for groups in RFC2307 mode. '.
780
	'Typically "posixGroup" or "group".');
781

    
782
$section->addInput(new Form_Input(
783
	'ldap_pam_groupdn',
784
	'Shell Authentication Group DN',
785
	'text',
786
	$pconfig['ldap_pam_groupdn']
787
))->setHelp('If LDAP server is used for shell authentication, user must be a member ' .
788
	    'of this group and have a valid posixAccount attributes to be able to login.%s Example: CN=Remoteshellusers,CN=Users,DC=example,DC=com',
789
	    '<br/>');
790

    
791
$section->addInput(new Form_Checkbox(
792
	'ldap_utf8',
793
	'UTF8 Encode',
794
	'UTF8 encode LDAP parameters before sending them to the server.',
795
	$pconfig['ldap_utf8']
796
))->setHelp('Required to support international characters, but may not be '.
797
	'supported by every LDAP server.');
798

    
799
$section->addInput(new Form_Checkbox(
800
	'ldap_nostrip_at',
801
	'Username Alterations',
802
	'Do not strip away parts of the username after the @ symbol',
803
	$pconfig['ldap_nostrip_at']
804
))->setHelp('e.g. user@host becomes user when unchecked.');
805

    
806
$section->addInput(new Form_Checkbox(
807
	'ldap_allow_unauthenticated',
808
	'Allow unauthenticated bind',
809
	'Allow unauthenticated bind',
810
	$pconfig['ldap_allow_unauthenticated']
811
))->setHelp('Unauthenticated binds are bind with an existing login but with an empty password. '.
812
         'Some LDAP servers (Microsoft AD) allow this type of bind without any possibility to disable it.');
813

    
814
$form->add($section);
815

    
816
// ==== RADIUS section ========================================================
817
$section = new Form_Section('RADIUS Server Settings');
818
$section->addClass('toggle-radius collapse');
819

    
820
$section->addInput(new Form_Select(
821
	'radius_protocol',
822
	'*Protocol',
823
	$pconfig['radius_protocol'],
824
	$radius_protocol
825
));
826

    
827
$section->addInput(new Form_Input(
828
	'radius_host',
829
	'*Hostname or IP address',
830
	'text',
831
	$pconfig['radius_host']
832
));
833

    
834
$section->addInput(new Form_Input(
835
	'radius_secret',
836
	'*Shared Secret',
837
	'password',
838
	$pconfig['radius_secret']
839
));
840

    
841
$section->addInput(new Form_Select(
842
	'radius_srvcs',
843
	'*Services offered',
844
	$pconfig['radius_srvcs'],
845
	$radius_srvcs
846
));
847

    
848
$section->addInput(new Form_Input(
849
	'radius_auth_port',
850
	'Authentication port',
851
	'number',
852
	$pconfig['radius_auth_port']
853
));
854

    
855
$section->addInput(new Form_Input(
856
	'radius_acct_port',
857
	'Accounting port',
858
	'number',
859
	$pconfig['radius_acct_port']
860
));
861

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

    
873
$section->addInput(new Form_Select(
874
	'radius_nasip_attribute',
875
	'RADIUS NAS IP Attribute',
876
	$pconfig['radius_nasip_attribute'],
877
	build_radiusnas_list()
878
))->setHelp('Enter the IP to use for the "NAS-IP-Address" attribute during RADIUS Access-Requests.<br />'.
879
			'Please note that this choice won\'t change the interface used for contacting the RADIUS server.');
880

    
881
if (isset($id) && $a_server[$id])
882
{
883
	$form->addGlobal(new Form_Input(
884
		'id',
885
		null,
886
		'hidden',
887
		$id
888
	));
889
}
890

    
891
$form->add($section);
892

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

    
896
$form->add($modal);
897

    
898
print $form;
899
?>
900
<script type="text/javascript">
901
//<![CDATA[
902
events.push(function() {
903

    
904
	// Create an AJAX request (to this page) to get the container list and controls
905
	function select_clicked() {
906
		if (document.getElementById("ldap_port").value == '' ||
907
			document.getElementById("ldap_host").value == '' ||
908
			document.getElementById("ldap_scope").value == '' ||
909
			document.getElementById("ldap_basedn").value == '' ||
910
			document.getElementById("ldapauthcontainers").value == '') {
911
			alert("<?=gettext("Please fill the required values.");?>");
912
			return;
913
		}
914

    
915
		if (!document.getElementById("ldap_anon").checked) {
916
			if (document.getElementById("ldap_binddn").value == '' ||
917
				document.getElementById("ldap_bindpw").value == '') {
918
				alert("<?=gettext("Please fill the bind username/password.");?>");
919
				return;
920
			}
921
		}
922

    
923
		var ajaxRequest;
924
		var authserver = $('#authmode').val();
925
		var cert;
926

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

    
958
		// Deal with the results of the above ajax call
959
		ajaxRequest.done(function (response, textStatus, jqXHR) {
960
			$('#containers').replaceWith(response);
961

    
962
			$('#containers').modal('show');
963

    
964
			// The button handler needs to be here because until the modal has been populated
965
			// the controls we need to attach handlers to do not exist
966
			$('#svcontbtn').prop("type", "button");
967
			$('#svcontbtn').removeAttr("href");
968

    
969
			$('#svcontbtn').click(function () {
970
				var ous = $('[id^=ou]').length;
971
				var i;
972

    
973
				$('#ldapauthcontainers').val("");
974

    
975
				for (i = 0; i < ous; i++) {
976
					if ($('#ou' + i).prop("checked")) {
977
						if ($('#ldapauthcontainers').val() != "") {
978
							$('#ldapauthcontainers').val($('#ldapauthcontainers').val() +";");
979
						}
980

    
981
						$('#ldapauthcontainers').val($('#ldapauthcontainers').val() + $('#ou' + i).val());
982
					}
983
				}
984

    
985
				$('#containers').modal('hide');
986
			});
987
		});
988

    
989
	}
990

    
991
	function set_ldap_port() {
992
		if ($('#ldap_urltype').find(":selected").index() == 2)
993
			$('#ldap_port').val('636');
994
		else
995
			$('#ldap_port').val('389');
996
	}
997

    
998
	function set_required_port_fields() {
999
		if (document.getElementById("radius_srvcs").value == 'auth') {
1000
			setRequired('radius_auth_port', true);
1001
			setRequired('radius_acct_port', false);
1002
		} else if (document.getElementById("radius_srvcs").value == 'acct') {
1003
			setRequired('radius_auth_port', false);
1004
			setRequired('radius_acct_port', true);
1005
		} else { // both
1006
			setRequired('radius_auth_port', true);
1007
			setRequired('radius_acct_port', true);
1008
		}
1009
	}
1010

    
1011
	// Hides all elements of the specified class. This will usually be a section
1012
	function hideClass(s_class, hide) {
1013
		if (hide)
1014
			$('.' + s_class).hide();
1015
		else
1016
			$('.' + s_class).show();
1017
	}
1018

    
1019
	function ldap_tmplchange() {
1020
		switch ($('#ldap_tmpltype').find(":selected").index()) {
1021
<?php
1022
		$index = 0;
1023
		foreach ($ldap_templates as $tmpldata):
1024
?>
1025
			case <?=$index;?>:
1026
				$('#ldap_attr_user').val("<?=$tmpldata['attr_user'];?>");
1027
				$('#ldap_attr_group').val("<?=$tmpldata['attr_group'];?>");
1028
				$('#ldap_attr_member').val("<?=$tmpldata['attr_member'];?>");
1029
				$("#ldap_allow_unauthenticated").attr("checked", <?=$tmpldata['allow_unauthenticated'];?>);
1030
				break;
1031
<?php
1032
			$index++;
1033
		endforeach;
1034
?>
1035
		}
1036
	}
1037

    
1038
	// ---------- On initial page load ------------------------------------------------------------
1039

    
1040
<?php if ($act != 'edit') : ?>
1041
	ldap_tmplchange();
1042
<?php endif; ?>
1043

    
1044
	hideClass('ldapanon', $('#ldap_anon').prop('checked'));
1045
	hideClass('extended', !$('#ldap_extended_enabled').prop('checked'));
1046
	hideClass('ldap_rfc2307_userdn', !$('#ldap_rfc2307').prop('checked'));
1047
	set_required_port_fields();
1048

    
1049
	if ($('#ldap_port').val() == "")
1050
		set_ldap_port();
1051

    
1052
<?php
1053
	if ($act == 'edit') {
1054
?>
1055
		$('#type option:not(:selected)').each(function(){
1056
			$(this).attr('disabled', 'disabled');
1057
		});
1058

    
1059
<?php
1060
		if (!$input_errors && !$dup) {
1061
?>
1062
		$('#name').prop("readonly", true);
1063
<?php
1064
		}
1065
	}
1066
?>
1067
	// ---------- Click checkbox handlers ---------------------------------------------------------
1068

    
1069
	$('#ldap_tmpltype').on('change', function() {
1070
		ldap_tmplchange();
1071
	});
1072

    
1073
	$('#ldap_anon').click(function () {
1074
		hideClass('ldapanon', this.checked);
1075
	});
1076

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

    
1081
	$('#Select').click(function () {
1082
		select_clicked();
1083
	});
1084

    
1085
	$('#ldap_extended_enabled').click(function () {
1086
		hideClass('extended', !this.checked);
1087
	});
1088

    
1089
	$('#ldap_rfc2307').click(function () {
1090
		hideClass('ldap_rfc2307_userdn', !this.checked);
1091
	});
1092

    
1093
	$('#radius_srvcs').on('change', function() {
1094
		set_required_port_fields();
1095
	});
1096

    
1097
});
1098
//]]>
1099
</script>
1100
<?php
1101
include("foot.inc");
(193-193/230)