Project

General

Profile

Download (35.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_usermanager.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
 * Copyright (c) 2005 Paul Taylor <paultaylor@winn-dixie.com>
11
 * All rights reserved.
12
 *
13
 * originally based on m0n0wall (http://m0n0.ch/wall)
14
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
 * All rights reserved.
16
 *
17
 * Licensed under the Apache License, Version 2.0 (the "License");
18
 * you may not use this file except in compliance with the License.
19
 * You may obtain a copy of the License at
20
 *
21
 * http://www.apache.org/licenses/LICENSE-2.0
22
 *
23
 * Unless required by applicable law or agreed to in writing, software
24
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 * See the License for the specific language governing permissions and
27
 * limitations under the License.
28
 */
29

    
30
##|+PRIV
31
##|*IDENT=page-system-usermanager
32
##|*NAME=System: User Manager
33
##|*DESCR=Allow access to the 'System: User Manager' page.
34
##|*WARN=standard-warning-root
35
##|*MATCH=system_usermanager.php*
36
##|-PRIV
37

    
38
require_once("certs.inc");
39
require_once("guiconfig.inc");
40
require_once("pfsense-utils.inc");
41

    
42
$logging_level = LOG_WARNING;
43
$logging_prefix = gettext("Local User Database");
44
$cert_keylens = array("1024", "2048", "3072", "4096", "6144", "7680", "8192", "15360", "16384");
45
$cert_keytypes = array("RSA", "ECDSA");
46
$openssl_ecnames = cert_build_curve_list();
47

    
48
global $openssl_digest_algs;
49

    
50
$password_extra_help = get_validate_password_hints();
51

    
52
// start admin user code
53
if (isset($_REQUEST['userid']) && is_numericint($_REQUEST['userid'])) {
54
	$id = $_REQUEST['userid'];
55
}
56

    
57
config_init_path('system/user');
58
$act = $_REQUEST['act'];
59

    
60
if (isset($_SERVER['HTTP_REFERER'])) {
61
	$referer = $_SERVER['HTTP_REFERER'];
62
} else {
63
	$referer = '/system_usermanager.php';
64
}
65

    
66
if (isset($id)) {
67
	$this_user = config_get_path("system/user/{$id}");
68
}
69
if ($this_user) {
70
	$pconfig['usernamefld'] = $this_user['name'];
71
	$pconfig['descr'] = $this_user['descr'];
72
	$pconfig['expires'] = $this_user['expires'];
73
	$pconfig['customsettings'] = isset($this_user['customsettings']);
74
	$pconfig['webguicss'] = $this_user['webguicss'];
75
	$pconfig['webguifixedmenu'] = $this_user['webguifixedmenu'];
76
	$pconfig['webguihostnamemenu'] = $this_user['webguihostnamemenu'];
77
	$pconfig['dashboardcolumns'] = $this_user['dashboardcolumns'];
78
	$pconfig['interfacessort'] = isset($this_user['interfacessort']);
79
	$pconfig['dashboardavailablewidgetspanel'] = isset($this_user['dashboardavailablewidgetspanel']);
80
	$pconfig['systemlogsfilterpanel'] = isset($this_user['systemlogsfilterpanel']);
81
	$pconfig['systemlogsmanagelogpanel'] = isset($this_user['systemlogsmanagelogpanel']);
82
	$pconfig['statusmonitoringsettingspanel'] = isset($this_user['statusmonitoringsettingspanel']);
83
	$pconfig['webguileftcolumnhyper'] = isset($this_user['webguileftcolumnhyper']);
84
	$pconfig['disablealiaspopupdetail'] = isset($this_user['disablealiaspopupdetail']);
85
	$pconfig['pagenamefirst'] = isset($this_user['pagenamefirst']);
86
	$pconfig['groups'] = local_user_get_groups($this_user);
87
	$pconfig['utype'] = $this_user['scope'];
88
	$pconfig['uid'] = $this_user['uid'];
89
	$pconfig['authorizedkeys'] = base64_decode($this_user['authorizedkeys']);
90
	$pconfig['priv'] = $this_user['priv'];
91
	$pconfig['ipsecpsk'] = $this_user['ipsecpsk'];
92
	$pconfig['disabled'] = isset($this_user['disabled']);
93
	$pconfig['keephistory'] = isset($this_user['keephistory']);
94
}
95

    
96
/*
97
 * Check user privileges to test if the user is allowed to make changes.
98
 * Otherwise users can end up in an inconsistent state where some changes are
99
 * performed and others denied. See https://redmine.pfsense.org/issues/9259
100
 */
101
phpsession_begin();
102
$guiuser = getUserEntry($_SESSION['Username']);
103
$guiuser = $guiuser['item'];
104
$read_only = (is_array($guiuser) && userHasPrivilege($guiuser, "user-config-readonly"));
105
phpsession_end();
106

    
107
if (!empty($_POST) && $read_only) {
108
	$input_errors = array(gettext("Insufficient privileges to make the requested change (read only)."));
109
}
110

    
111
if (($_POST['act'] == "deluser") && !$read_only) {
112

    
113
	if (!isset($_POST['username']) || (config_get_path("system/user/{$id}") === null) || ($_POST['username'] != config_get_path("system/user/{$id}/name"))) {
114
		pfSenseHeader("system_usermanager.php");
115
		exit;
116
	}
117

    
118
	if ($_POST['username'] == $_SESSION['Username']) {
119
		$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $_POST['username']);
120
	} else {
121
		local_user_del(config_get_path("system/user/{$id}"));
122
		$userdeleted = config_get_path("system/user/{$id}/name");
123
		config_del_path("system/user/{$id}");
124
		/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
125
		config_set_path('system/user', array_values(config_get_path('system/user')));
126
		$savemsg = sprintf(gettext("Successfully deleted user: %s"), $userdeleted);
127
		write_config($savemsg);
128
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
129
	}
130

    
131
} else if ($act == "new") {
132
	/*
133
	 * set this value cause the text field is read only
134
	 * and the user should not be able to mess with this
135
	 * setting.
136
	 */
137
	$pconfig['utype'] = "user";
138
	$pconfig['lifetime'] = 3650;
139

    
140
	$nonPrvCas = array();
141
	foreach (config_get_path('ca', []) as $ca) {
142
		if (!$ca['prv']) {
143
			continue;
144
		}
145

    
146
		$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
147
	}
148

    
149
}
150

    
151
if (isset($_POST['dellall']) && !$read_only) {
152

    
153
	$del_users = $_POST['delete_check'];
154
	$deleted_users = array();
155

    
156
	if (!empty($del_users)) {
157
		foreach ($del_users as $userid) {
158
			$tmp_user = config_get_path("system/user/{$userid}", []);
159
			if ($tmp_user['scope'] != "system") {
160
				if ($tmp_user['name'] == $_SESSION['Username']) {
161
					$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $tmp_user['name']);
162
				} else {
163
					$deleted_users[] = $tmp_user['name'];
164
					local_user_del($tmp_user);
165
					config_del_path("system/user/{$userid}");
166
				}
167
			} else {
168
				$delete_errors[] = sprintf(gettext("Cannot delete user %s because it is a system user."), $tmp_user['name']);
169
			}
170
		}
171

    
172
		if (count($deleted_users) > 0) {
173
			$savemsg = sprintf(gettext("Successfully deleted %s: %s"), (count($deleted_users) == 1) ? gettext("user") : gettext("users"), implode(', ', $deleted_users));
174
			/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
175
			config_set_path('system/user', array_values(config_get_path('system/user')));
176
			write_config($savemsg);
177
			syslog($logging_level, "{$logging_prefix}: {$savemsg}");
178
		}
179
	}
180
}
181

    
182
if (($_POST['act'] == "delcert") && !$read_only) {
183

    
184
	if (!config_get_path("system/user/{$id}")) {
185
		pfSenseHeader("system_usermanager.php");
186
		exit;
187
	}
188

    
189
	$certdeleted = lookup_cert(config_get_path("system/user/{$id}/cert/{$_POST['certid']}"));
190
	$certdeleted = $certdeleted['item']['descr'];
191
	$savemsg = sprintf(gettext("Removed certificate association \"%s\" from user %s"), $certdeleted, config_get_path("system/user/{$id}/name"));
192
	config_del_path("system/user/{$id}/cert/{$_POST['certid']}");
193
	write_config($savemsg);
194
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
195
	$_POST['act'] = "edit";
196
}
197

    
198
if (($_POST['act'] == "delprivid") && !$read_only) {
199
	$privdeleted = array_get_path($priv_list, (config_get_path("system/user/{$id}/priv/{$_POST['privid']}") . '/name'));
200
	config_del_path("system/user/{$id}/priv/{$_POST['privid']}");
201
	local_user_set(config_get_path("system/user/{$id}"));
202
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from user %s"), $privdeleted, config_get_path("system/user/{$id}/name"));
203
	write_config($savemsg);
204
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
205
	$_POST['act'] = "edit";
206
}
207

    
208
if ($_POST['save'] && !$read_only) {
209
	unset($input_errors);
210
	$input_errors = [];
211
	$pconfig = $_POST;
212

    
213
	/* input validation */
214
	if (isset($id) && config_get_path("system/user/{$id}")) {
215
		$reqdfields = explode(" ", "usernamefld");
216
		$reqdfieldsn = array(gettext("Username"));
217
	} else {
218
		if (empty($_POST['name'])) {
219
			$reqdfields = explode(" ", "usernamefld passwordfld1");
220
			$reqdfieldsn = array(
221
				gettext("Username"),
222
				gettext("Password"));
223
		} else {
224
			$reqdfields = explode(" ", "usernamefld passwordfld1 name caref keylen lifetime");
225
			$reqdfieldsn = array(
226
				gettext("Username"),
227
				gettext("Password"),
228
				gettext("Descriptive name"),
229
				gettext("Certificate authority"),
230
				gettext("Key length"),
231
				gettext("Lifetime"));
232
		}
233
	}
234

    
235
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
236

    
237
	if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['usernamefld'])) {
238
		$input_errors[] = gettext("The username contains invalid characters.");
239
	}
240

    
241
	if (strlen($_POST['usernamefld']) > 32) {
242
		$input_errors[] = gettext("The username is longer than 32 characters.");
243
	}
244

    
245
	if (($_POST['passwordfld1']) && ($_POST['passwordfld1'] != $_POST['passwordfld2'])) {
246
		$input_errors[] = gettext("The passwords do not match.");
247
	}
248

    
249
	if (isset($_POST['ipsecpsk']) && !preg_match('/^[[:ascii:]]*$/', $_POST['ipsecpsk'])) {
250
		$input_errors[] = gettext("IPsec Pre-Shared Key contains invalid characters.");
251
	}
252

    
253
	$input_errors = array_merge($input_errors, validate_password($_POST['usernamefld'], $_POST['passwordfld1']));
254

    
255
	/* Check the POSTed groups to ensure they are valid and exist */
256
	if (is_array($_POST['groups'])) {
257
		foreach ($_POST['groups'] as $newgroup) {
258
			if (empty(getGroupEntry($newgroup))) {
259
				$input_errors[] = gettext("One or more invalid groups was submitted.");
260
			}
261
		}
262
	}
263

    
264
	$oldusername = config_get_path("system/user/{$id}/name", '');
265
	/* make sure this user name is unique */
266
	if (!$input_errors) {
267
		foreach (config_get_path('system/user', []) as $userent) {
268
			if ($userent['name'] == $_POST['usernamefld'] && $oldusername != $_POST['usernamefld']) {
269
				$input_errors[] = gettext("Another entry with the same username already exists.");
270
				break;
271
			}
272
		}
273
	}
274
	/* also make sure it is not reserved */
275
	if (!$input_errors) {
276
		$system_users = explode("\n", file_get_contents("/etc/passwd"));
277
		foreach ($system_users as $s_user) {
278
			$ent = explode(":", $s_user);
279
			if ($ent[0] == $_POST['usernamefld'] && $oldusername != $_POST['usernamefld']) {
280
				$input_errors[] = gettext("That username is reserved by the system.");
281
				break;
282
			}
283
		}
284
	}
285

    
286
	/*
287
	 * Check for a valid expiration date if one is set at all (valid means,
288
	 * DateTime puts out a time stamp so any DateTime compatible time
289
	 * format may be used. to keep it simple for the enduser, we only
290
	 * claim to accept MM/DD/YYYY as inputs. Advanced users may use inputs
291
	 * like "+1 day", which will be converted to MM/DD/YYYY based on "now".
292
	 * Otherwise such an entry would lead to an invalid expiration data.
293
	 */
294
	if ($_POST['expires']) {
295
		try {
296
			$expdate = new DateTime($_POST['expires']);
297
			//convert from any DateTime compatible date to MM/DD/YYYY
298
			$_POST['expires'] = $expdate->format("m/d/Y");
299
		} catch (Exception $ex) {
300
			$input_errors[] = gettext("Invalid expiration date format; use MM/DD/YYYY instead.");
301
		}
302
	}
303

    
304
	if (!empty($_POST['name'])) {
305
		$ca = lookup_ca($_POST['caref']);
306
		$ca = $ca['item'];
307
		if (!$ca) {
308
			$input_errors[] = gettext("Invalid internal Certificate Authority") . "\n";
309
		}
310
	}
311
	validate_webguicss_field($input_errors, $_POST['webguicss']);
312
	validate_webguifixedmenu_field($input_errors, $_POST['webguifixedmenu']);
313
	validate_webguihostnamemenu_field($input_errors, $_POST['webguihostnamemenu']);
314
	validate_dashboardcolumns_field($input_errors, $_POST['dashboardcolumns']);
315

    
316
	if (!$input_errors) {
317
		if (isset($id) && config_get_path("system/user/{$id}")) {
318
			$user_item_config = [
319
				'idx' => $id,
320
				'item' => config_get_path("system/user/{$id}")
321
			];
322
		} else {
323
			$user_item_config = ['idx' => null, 'item' => null];
324
		}
325
		$userent = &$user_item_config['item'];
326

    
327
		isset($_POST['utype']) ? $userent['scope'] = $_POST['utype'] : $userent['scope'] = "system";
328

    
329
		/* the user name was modified */
330
		if (!empty($_POST['oldusername']) && ($_POST['usernamefld'] <> $_POST['oldusername'])) {
331
			$_SERVER['REMOTE_USER'] = $_POST['usernamefld'];
332
			local_user_del($userent);
333
		}
334

    
335
		/* the user password was modified */
336
		if ($_POST['passwordfld1']) {
337
			local_user_set_password($user_item_config, $_POST['passwordfld1']);
338
		}
339

    
340
		/* only change description if sent */
341
		if (isset($_POST['descr'])) {
342
			$userent['descr'] = $_POST['descr'];
343
		}
344

    
345
		$userent['name'] = $_POST['usernamefld'];
346
		$userent['expires'] = $_POST['expires'];
347
		$userent['dashboardcolumns'] = $_POST['dashboardcolumns'];
348
		$userent['authorizedkeys'] = base64_encode($_POST['authorizedkeys']);
349
		$userent['ipsecpsk'] = $_POST['ipsecpsk'];
350

    
351
		if ($_POST['disabled']) {
352
			$userent['disabled'] = true;
353
		} else {
354
			unset($userent['disabled']);
355
		}
356

    
357
		if ($_POST['customsettings']) {
358
			$userent['customsettings'] = true;
359
		} else {
360
			unset($userent['customsettings']);
361
		}
362

    
363
		if ($_POST['webguicss']) {
364
			$userent['webguicss'] = $_POST['webguicss'];
365
		} else {
366
			unset($userent['webguicss']);
367
		}
368

    
369
		if ($_POST['webguifixedmenu']) {
370
			$userent['webguifixedmenu'] = $_POST['webguifixedmenu'];
371
		} else {
372
			unset($userent['webguifixedmenu']);
373
		}
374

    
375
		if ($_POST['webguihostnamemenu']) {
376
			$userent['webguihostnamemenu'] = $_POST['webguihostnamemenu'];
377
		} else {
378
			unset($userent['webguihostnamemenu']);
379
		}
380

    
381
		if ($_POST['interfacessort']) {
382
			$userent['interfacessort'] = true;
383
		} else {
384
			unset($userent['interfacessort']);
385
		}
386

    
387
		if ($_POST['dashboardavailablewidgetspanel']) {
388
			$userent['dashboardavailablewidgetspanel'] = true;
389
		} else {
390
			unset($userent['dashboardavailablewidgetspanel']);
391
		}
392

    
393
		if ($_POST['systemlogsfilterpanel']) {
394
			$userent['systemlogsfilterpanel'] = true;
395
		} else {
396
			unset($userent['systemlogsfilterpanel']);
397
		}
398

    
399
		if ($_POST['systemlogsmanagelogpanel']) {
400
			$userent['systemlogsmanagelogpanel'] = true;
401
		} else {
402
			unset($userent['systemlogsmanagelogpanel']);
403
		}
404

    
405
		if ($_POST['statusmonitoringsettingspanel']) {
406
			$userent['statusmonitoringsettingspanel'] = true;
407
		} else {
408
			unset($userent['statusmonitoringsettingspanel']);
409
		}
410

    
411
		if ($_POST['webguileftcolumnhyper']) {
412
			$userent['webguileftcolumnhyper'] = true;
413
		} else {
414
			unset($userent['webguileftcolumnhyper']);
415
		}
416

    
417
		if ($_POST['disablealiaspopupdetail']) {
418
			$userent['disablealiaspopupdetail'] = true;
419
		} else {
420
			unset($userent['disablealiaspopupdetail']);
421
		}
422

    
423
		if ($_POST['pagenamefirst']) {
424
			$userent['pagenamefirst'] = true;
425
		} else {
426
			unset($userent['pagenamefirst']);
427
		}
428

    
429
		if ($_POST['keephistory']) {
430
			$userent['keephistory'] = true;
431
		} else {
432
			unset($userent['keephistory']);
433
		}
434

    
435
		if (isset($id) && config_get_path("system/user/{$id}")) {
436
			config_set_path("system/user/{$id}", $userent);
437
		} else {
438
			if (!empty($_POST['name'])) {
439
				$cert = array();
440
				$cert['refid'] = uniqid();
441
				$userent['cert'] = array();
442

    
443
				$cert['descr'] = $_POST['name'];
444

    
445
				$subject = cert_get_subject_hash($ca['crt']);
446

    
447
				$dn = array();
448
				if (!empty($subject['C'])) {
449
					$dn['countryName'] = $subject['C'];
450
				}
451
				if (!empty($subject['ST'])) {
452
					$dn['stateOrProvinceName'] = $subject['ST'];
453
				}
454
				if (!empty($subject['L'])) {
455
					$dn['localityName'] = $subject['L'];
456
				}
457
				if (!empty($subject['O'])) {
458
					$dn['organizationName'] = $subject['O'];
459
				}
460
				if (!empty($subject['OU'])) {
461
					$dn['organizationalUnitName'] = $subject['OU'];
462
				}
463
				$dn['commonName'] = $userent['name'];
464
				$cn_altname = cert_add_altname_type($userent['name']);
465
				if (!empty($cn_altname)) {
466
					$dn['subjectAltName'] = $cn_altname;
467
				}
468

    
469
				cert_create($cert, $_POST['caref'], $_POST['keylen'],
470
					(int)$_POST['lifetime'], $dn, 'user',
471
					$_POST['digest_alg'], $_POST['keytype'],
472
					$_POST['ecname']);
473

    
474
				config_set_path('cert/', $cert);
475
				$userent['cert'][] = $cert['refid'];
476
			}
477
			$nextuid_config = config_get_path('system/nextuid');
478
			$userent['uid'] = $nextuid_config++;
479
			config_set_path('system/nextuid', $nextuid_config);
480
			/* Add the user to All Users group. */
481
			$group_config = config_get_path('system/group', []);
482
			foreach ($group_config as $gidx => &$group) {
483
				if ($group['name'] == "all") {
484
					if (!is_array($group['member'])) {
485
						$group['member'] = [];
486
					}
487
					$group['member'][] = $userent['uid'];
488
					break;
489
				}
490
			}
491
			unset($group);
492
			config_set_path('system/group', $group_config);
493

    
494
			config_set_path('system/user/', $userent);
495
		}
496

    
497
		/* Sort it alphabetically */
498
		$user_config = config_get_path('system/user');
499
		usort($user_config, function($a, $b) {
500
			return strcmp($a['name'], $b['name']);
501
		});
502
		config_set_path('system/user', $user_config);
503

    
504
		local_user_set_groups($userent, $_POST['groups']);
505
		local_user_set($userent);
506

    
507
		/* Update user index to account for new changes */
508
		global $userindex;
509
		$userindex = index_users();
510

    
511
		$savemsg = sprintf(gettext("Successfully %s user %s"), (isset($id)) ? gettext("edited") : gettext("created"), $userent['name']);
512
		write_config($savemsg);
513
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
514
		if (is_dir("/etc/inc/privhooks")) {
515
			run_plugins("/etc/inc/privhooks");
516
		}
517

    
518
		if ($userent['uid'] == 0) {
519
			log_error(gettext("Restarting sshd due to admin account change."));
520
			send_event("service restart sshd");
521
		}
522

    
523
		pfSenseHeader("system_usermanager.php");
524
	}
525
}
526

    
527
function build_priv_table() {
528
	global $id, $read_only;
529

    
530
	$privhtml = '<div class="table-responsive">';
531
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
532
	$privhtml .=		'<thead>';
533
	$privhtml .=			'<tr>';
534
	$privhtml .=				'<th>' . gettext('Inherited from') . '</th>';
535
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
536
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
537
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
538
	$privhtml .=			'</tr>';
539
	$privhtml .=		'</thead>';
540
	$privhtml .=		'<tbody>';
541

    
542
	$i = 0;
543
	$user_has_root_priv = false;
544

    
545
	foreach (get_user_privdesc(config_get_path("system/user/{$id}")) as $priv) {
546
		$group = false;
547
		if ($priv['group']) {
548
			$group = $priv['group'];
549
		}
550

    
551
		$privhtml .=		'<tr>';
552
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
553
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
554
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
555
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
556
			$privhtml .=			' ' . gettext('(admin privilege)');
557
			$user_has_root_priv = true;
558
		}
559
		$privhtml .=			'</td>';
560
		$privhtml .=			'<td>';
561
		if (!$group && !$read_only) {
562
			$privhtml .=			'<a class="fa-solid fa-trash-can no-confirm icon-pointer" title="' . gettext('Delete Privilege') . '" id="delprivid' . $i . '"></a>';
563
		}
564

    
565
		$privhtml .=			'</td>';
566
		$privhtml .=		'</tr>';
567

    
568
		if (!$group) {
569
			$i++;
570
		}
571
	}
572

    
573
	if ($user_has_root_priv) {
574
		$privhtml .=		'<tr>';
575
		$privhtml .=			'<td colspan="3">';
576
		$privhtml .=				'<b>' . gettext('Security notice: This user effectively has administrator-level access') . '</b>';
577
		$privhtml .=			'</td>';
578
		$privhtml .=			'<td>';
579
		$privhtml .=			'</td>';
580
		$privhtml .=		'</tr>';
581

    
582
	}
583

    
584
	$privhtml .=		'</tbody>';
585
	$privhtml .=	'</table>';
586
	$privhtml .= '</div>';
587

    
588
	$privhtml .= '<nav class="action-buttons">';
589
	if (!$read_only) {
590
		$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
591
	}
592
	$privhtml .= '</nav>';
593

    
594
	return($privhtml);
595
}
596

    
597
function build_cert_table() {
598
	global $id, $read_only;
599

    
600
	$certhtml = '<div class="table-responsive">';
601
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
602
	$certhtml .=		'<thead>';
603
	$certhtml .=			'<tr>';
604
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
605
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
606
	$certhtml .=				'<th></th>';
607
	$certhtml .=			'</tr>';
608
	$certhtml .=		'</thead>';
609
	$certhtml .=		'<tbody>';
610

    
611
	$i = 0;
612
	foreach (config_get_path("system/user/{$id}/cert", []) as $certref) {
613
		$cert = lookup_cert($certref);
614
		$cert = $cert['item'];
615
		$ca = lookup_ca($cert['caref']);
616
		$ca = $ca['item'];
617
		$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
618

    
619
		$certhtml .=	'<tr>';
620
		$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
621
		$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
622
		$certhtml .=		'<td>';
623
		if (!$read_only) {
624
			$certhtml .=			'<a id="delcert' . $i .'" class="fa-solid fa-trash-can no-confirm icon-pointer" title="';
625
			$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
626
		}
627
		$certhtml .=		'</td>';
628
		$certhtml .=	'</tr>';
629
		$i++;
630
	}
631

    
632
	$certhtml .=		'</tbody>';
633
	$certhtml .=	'</table>';
634
	$certhtml .= '</div>';
635

    
636
	$certhtml .= '<nav class="action-buttons">';
637
	if (!$read_only) {
638
		$certhtml .=	'<a href="system_certmanager.php?act=new&amp;userid=' . $id . '" class="btn btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
639
	}
640
	$certhtml .= '</nav>';
641

    
642
	return($certhtml);
643
}
644

    
645
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
646
$pglinks = array("", "system_usermanager.php", "system_usermanager.php");
647

    
648
if ($act == "new" || $act == "edit" || $input_errors) {
649
	$pgtitle[] = gettext('Edit');
650
	$pglinks[] = "@self";
651
}
652

    
653
include("head.inc");
654

    
655
if ($delete_errors) {
656
	print_input_errors($delete_errors);
657
}
658

    
659
if ($input_errors) {
660
	print_input_errors($input_errors);
661
}
662

    
663
if ($savemsg) {
664
	print_info_box($savemsg, 'success');
665
}
666

    
667
$tab_array = array();
668
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
669
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
670
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
671
$tab_array[] = array(gettext("Change Password"), false, "system_usermanager_passwordmg.php");
672
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
673
display_top_tabs($tab_array);
674

    
675
if (!($act == "new" || $act == "edit" || $input_errors)) {
676
?>
677
<form method="post">
678
<div class="panel panel-default">
679
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Users')?></h2></div>
680
	<div class="panel-body">
681
		<div class="table-responsive">
682
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
683
				<thead>
684
					<tr>
685
						<th>&nbsp;</th>
686
						<th><?=gettext("Username")?></th>
687
						<th><?=gettext("Full name")?></th>
688
						<th><?=gettext("Status")?></th>
689
						<th><?=gettext("Groups")?></th>
690
						<th><?=gettext("Actions")?></th>
691
					</tr>
692
				</thead>
693
				<tbody>
694
<?php
695
foreach (config_get_path('system/user', []) as $i => $userent):
696
	?>
697
					<tr>
698
						<td>
699
							<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=((($userent['scope'] == "system") || ($userent['name'] == $_SESSION['Username'])) ? 'disabled' : '')?>/>
700
						</td>
701
						<td>
702
<?php
703
	if ($userent['scope'] != "user") {
704
		$usrimg = 'fa-regular fa-eye';
705
	} else {
706
		$usrimg = 'fa-solid fa-user';
707
	}
708
?>
709
							<i class="<?=$usrimg?>" title="<?= gettext("Scope") . ": {$userent['scope']}" ?>"></i>
710
							<?=htmlspecialchars($userent['name'])?>
711
						</td>
712
						<td><?=htmlspecialchars($userent['descr'])?></td>
713
						<td><i class="<?= (isset($userent['disabled'])) ? 'fa-solid fa-ban" title="' . gettext("Disabled") . '"' : 'fa-solid fa-check" title="' . gettext("Enabled") . '"' ; ?>"><span style='display: none'><?= (isset($userent['disabled'])) ? gettext("Disabled") : gettext("Enabled") ; ?></span></i></td>
714
						<td><?=implode(",", local_user_get_groups($userent))?></td>
715
						<td>
716
							<a class="fa-solid fa-pencil" title="<?=gettext("Edit user"); ?>" href="?act=edit&amp;userid=<?=$i?>"></a>
717
<?php if (($userent['scope'] != "system") && ($userent['name'] != $_SESSION['Username']) && !$read_only): ?>
718
							<a class="fa-solid fa-trash-can"	title="<?=gettext("Delete user")?>" href="?act=deluser&amp;userid=<?=$i?>&amp;username=<?=$userent['name']?>" usepost></a>
719
<?php endif; ?>
720
						</td>
721
					</tr>
722
<?php endforeach; ?>
723
				</tbody>
724
			</table>
725
		</div>
726
	</div>
727
</div>
728
<nav class="action-buttons">
729
	<?php if (!$read_only): ?>
730

    
731
	<a href="?act=new" class="btn btn-sm btn-success">
732
		<i class="fa-solid fa-plus icon-embed-btn"></i>
733
		<?=gettext("Add")?>
734
	</a>
735

    
736
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
737
		<i class="fa-solid fa-trash-can icon-embed-btn"></i>
738
		<?=gettext("Delete")?>
739
	</button>
740
	<?php endif; ?>
741

    
742
</nav>
743
</form>
744
<div class="infoblock">
745
<?php
746
	print_callout('<p>' . gettext("Additional users can be added here. User permissions for accessing " .
747
		"the webConfigurator can be assigned directly or inherited from group memberships. " .
748
		"Some system object properties can be modified but they cannot be deleted.") . '</p>' .
749
		'<p>' . gettext("Accounts added here are also used for other parts of the system " .
750
		"such as OpenVPN, IPsec, and Captive Portal.") . '</p>'
751
	);
752

    
753
?></div>
754

    
755
<?php
756
	include("foot.inc");
757
	exit;
758
}
759

    
760
$form = new Form;
761

    
762
if ($act == "new" || $act == "edit" || $input_errors):
763

    
764
	$form->addGlobal(new Form_Input(
765
		'act',
766
		null,
767
		'hidden',
768
		''
769
	));
770

    
771
	$form->addGlobal(new Form_Input(
772
		'userid',
773
		null,
774
		'hidden',
775
		isset($id) ? $id:''
776
	));
777

    
778
	$form->addGlobal(new Form_Input(
779
		'privid',
780
		null,
781
		'hidden',
782
		''
783
	));
784

    
785
	$form->addGlobal(new Form_Input(
786
		'certid',
787
		null,
788
		'hidden',
789
		''
790
	));
791

    
792
	$ro = "";
793
	if ($pconfig['utype'] == "system") {
794
		$ro = "readonly";
795
	}
796

    
797
	$section = new Form_Section('User Properties');
798

    
799
	$section->addInput(new Form_StaticText(
800
		'Defined by',
801
		strtoupper($pconfig['utype'])
802
	));
803

    
804
	$form->addGlobal(new Form_Input(
805
		'utype',
806
		null,
807
		'hidden',
808
		$pconfig['utype']
809
	));
810

    
811
	$section->addInput(new Form_Checkbox(
812
		'disabled',
813
		'Disabled',
814
		'This user cannot login',
815
		$pconfig['disabled']
816
	));
817

    
818
	$section->addInput($input = new Form_Input(
819
		'usernamefld',
820
		'*Username',
821
		'text',
822
		$pconfig['usernamefld'],
823
		['autocomplete' => 'new-password']
824
	));
825

    
826
	if ($ro) {
827
		$input->setReadonly();
828
	}
829

    
830
	$form->addGlobal(new Form_Input(
831
		'oldusername',
832
		null,
833
		'hidden',
834
		$pconfig['usernamefld']
835
	));
836

    
837
	if ($act == "edit") {
838
		$pwd_required = "";
839
	} else {
840
		$pwd_required = "*";
841
	}
842

    
843
	$group = new Form_Group($pwd_required . 'Password');
844
	$group->add(new Form_Input(
845
		'passwordfld1',
846
		'Password',
847
		'password',
848
		null,
849
		['autocomplete' => 'new-password']
850
	))->setHelp('Enter a new password.' .
851
			'%1$s%1$s' .
852
			'Hints:%1$s' .
853
			' %2$s', '<br/>', $password_extra_help);
854
	$group->add(new Form_Input(
855
		'passwordfld2',
856
		'Confirm Password',
857
		'password',
858
		null,
859
		['autocomplete' => 'new-password']
860
	))->setHelp('Type the new password again for confirmation.');
861

    
862
	$section->add($group);
863

    
864
	$section->addInput($input = new Form_Input(
865
		'descr',
866
		'Full name',
867
		'text',
868
		htmlspecialchars($pconfig['descr'])
869
	))->setHelp('User\'s full name, for administrative information only');
870

    
871
	if ($ro) {
872
		$input->setDisabled();
873
	}
874

    
875
	$section->addInput(new Form_Input(
876
		'expires',
877
		'Expiration date',
878
		'text',
879
		$pconfig['expires']
880
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
881
		'the expiration date as MM/DD/YYYY');
882

    
883
	$section->addInput(new Form_Checkbox(
884
		'customsettings',
885
		'Custom Settings',
886
		'Use individual customized GUI options and dashboard layout for this user.',
887
		$pconfig['customsettings']
888
	));
889

    
890
	gen_user_settings_fields($section, $pconfig);
891

    
892
	// ==== Group membership ==================================================
893
	$group = new Form_Group('Group membership');
894

    
895
	// Make a list of all the groups configured on the system, and a list of
896
	// those which this user is a member of
897
	$systemGroups = array();
898
	$usersGroups = array();
899

    
900
	$usergid = [$pconfig['usernamefld']];
901

    
902
	foreach (config_get_path('system/group', []) as $Ggroup) {
903
		if ($Ggroup['name'] != "all") {
904
			if (($act == 'edit' || $input_errors) && $Ggroup['member'] && in_array(config_get_path("system/user/{$id}/uid", []), $Ggroup['member'])) {
905
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
906
			} else {
907
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
908
			}
909
		}
910
	}
911

    
912
	$group->add(new Form_Select(
913
		'sysgroups',
914
		null,
915
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
916
		$systemGroups,
917
		true
918
	))->setHelp('Not member of');
919

    
920
	$group->add(new Form_Select(
921
		'groups',
922
		null,
923
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
924
		$usersGroups,
925
		true
926
	))->setHelp('Member of');
927

    
928
	$section->add($group);
929

    
930
	$group = new Form_Group('');
931

    
932
	$group->add(new Form_Button(
933
		'movetoenabled',
934
		'Move to "Member of" list',
935
		null,
936
		'fa-solid fa-angle-double-right'
937
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
938

    
939
	$group->add(new Form_Button(
940
		'movetodisabled',
941
		'Move to "Not member of" list',
942
		null,
943
		'fa-solid fa-angle-double-left'
944
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
945

    
946
	$group->setHelp('Hold down CTRL (PC)/COMMAND (Mac) key to select multiple items.');
947
	$section->add($group);
948

    
949
	// ==== Button for adding user certificate ================================
950
	if ($act == 'new') {
951
		if (count($nonPrvCas) > 0) {
952
			$section->addInput(new Form_Checkbox(
953
				'showcert',
954
				'Certificate',
955
				'Click to create a user certificate',
956
				false
957
			));
958
		} else {
959
			$section->addInput(new Form_StaticText(
960
				'Certificate',
961
				gettext('No private CAs found. A private CA is required to create a new user certificate. ' .
962
					'Save the user first to import an external certificate.')
963
			));
964
		}
965
	}
966

    
967
	$form->add($section);
968

    
969
	// ==== Effective privileges section ======================================
970
	if (isset($pconfig['uid'])) {
971
		// We are going to build an HTML table and add it to an Input_StaticText. It may be ugly, but it
972
		// is the best way to make the display we need.
973

    
974
		$section = new Form_Section('Effective Privileges');
975

    
976
		$section->addInput(new Form_StaticText(
977
			null,
978
			build_priv_table()
979
		));
980

    
981
		$form->add($section);
982

    
983
		// ==== Certificate table section =====================================
984
		$section = new Form_Section('User Certificates');
985

    
986
		$section->addInput(new Form_StaticText(
987
			null,
988
			build_cert_table()
989
		));
990

    
991
		$form->add($section);
992
	}
993

    
994
	// ==== Add user certificate for a new user
995
	if (count(config_get_path('ca', [])) > 0) {
996
		$section = new Form_Section('Create Certificate for User');
997
		$section->addClass('cert-options');
998

    
999
		if (!empty($nonPrvCas)) {
1000
			$section->addInput(new Form_Input(
1001
				'name',
1002
				'Descriptive name',
1003
				'text',
1004
				$pconfig['name']
1005
			));
1006

    
1007
			$section->addInput(new Form_Select(
1008
				'caref',
1009
				'Certificate authority',
1010
				null,
1011
				$nonPrvCas
1012
			));
1013

    
1014
			$section->addInput(new Form_Select(
1015
				'keytype',
1016
				'*Key type',
1017
				$pconfig['keytype'],
1018
				array_combine($cert_keytypes, $cert_keytypes)
1019
			));
1020

    
1021
			$group = new Form_Group($i == 0 ? '*Key length':'');
1022
			$group->addClass('rsakeys');
1023
			$group->add(new Form_Select(
1024
				'keylen',
1025
				null,
1026
				$pconfig['keylen'] ? $pconfig['keylen'] : '2048',
1027
				array_combine($cert_keylens, $cert_keylens)
1028
			))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
1029
				'The Key Length should not be lower than 2048 or some platforms ' .
1030
				'may consider the certificate invalid.', '<br/>');
1031
			$section->add($group);
1032

    
1033
			$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
1034
			$group->addClass('ecnames');
1035
			$group->add(new Form_Select(
1036
				'ecname',
1037
				null,
1038
				$pconfig['ecname'] ? $pconfig['ecname'] : 'prime256v1',
1039
				$openssl_ecnames
1040
			))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
1041
			$section->add($group);
1042

    
1043
			$section->addInput(new Form_Select(
1044
				'digest_alg',
1045
				'*Digest Algorithm',
1046
				$pconfig['digest_alg'] ? $pconfig['digest_alg'] : 'sha256',
1047
				array_combine($openssl_digest_algs, $openssl_digest_algs)
1048
			))->setHelp('The digest method used when the certificate is signed. %1$s' .
1049
				'The best practice is to use an algorithm stronger than SHA1. '.
1050
				'Some platforms may consider weaker digest algorithms invalid', '<br/>');
1051

    
1052
			$section->addInput(new Form_Input(
1053
				'lifetime',
1054
				'Lifetime',
1055
				'number',
1056
				$pconfig['lifetime']
1057
			));
1058
		}
1059

    
1060
		$form->add($section);
1061
	}
1062

    
1063
endif;
1064
// ==== Paste a key for the new user
1065
$section = new Form_Section('Keys');
1066

    
1067
$section->addInput(new Form_Checkbox(
1068
	'showkey',
1069
	'Authorized keys',
1070
	'Click to paste an authorized key',
1071
	false
1072
));
1073

    
1074
$section->addInput(new Form_Textarea(
1075
	'authorizedkeys',
1076
	'Authorized SSH Keys',
1077
	$pconfig['authorizedkeys']
1078
))->setHelp('Enter authorized SSH keys for this user');
1079

    
1080
$section->addInput(new Form_Input(
1081
	'ipsecpsk',
1082
	'IPsec Pre-Shared Key',
1083
	'text',
1084
	$pconfig['ipsecpsk']
1085
));
1086

    
1087
$form->add($section);
1088

    
1089
$section = new Form_Section('Shell Behavior');
1090

    
1091
$section->addInput(new Form_Checkbox(
1092
	'keephistory',
1093
	'Keep Command History',
1094
	'Keep shell command history between login sessions',
1095
	$pconfig['keephistory']
1096
))->setHelp('If this user has shell access, this option preserves the last 1000 unique commands entered at a shell prompt between login sessions. ' .
1097
		'The user can access history using the up and down arrows at an SSH or console shell prompt ' .
1098
		'and search the history by typing a partial command and then using the up or down arrows.');
1099

    
1100
$form->add($section);
1101

    
1102
print $form;
1103

    
1104
$csswarning = sprintf(gettext("%sUser-created themes are unsupported, use at your own risk."), "<br />");
1105
?>
1106
<script type="text/javascript">
1107
//<![CDATA[
1108
events.push(function() {
1109

    
1110
	function setcustomoptions() {
1111
		var adv = $('#customsettings').prop('checked');
1112

    
1113
		hideInput('webguicss', !adv);
1114
		hideInput('webguifixedmenu', !adv);
1115
		hideInput('webguihostnamemenu', !adv);
1116
		hideInput('dashboardcolumns', !adv);
1117
		hideCheckbox('interfacessort', !adv);
1118
		hideCheckbox('dashboardavailablewidgetspanel', !adv);
1119
		hideCheckbox('systemlogsfilterpanel', !adv);
1120
		hideCheckbox('systemlogsmanagelogpanel', !adv);
1121
		hideCheckbox('statusmonitoringsettingspanel', !adv);
1122
		hideCheckbox('webguileftcolumnhyper', !adv);
1123
		hideCheckbox('disablealiaspopupdetail', !adv);
1124
		hideCheckbox('pagenamefirst', !adv);
1125
	}
1126

    
1127
	// Handle displaying a warning message if a user-created theme is selected.
1128
	function setThemeWarning() {
1129
		if ($('#webguicss').val().startsWith("pfSense")) {
1130
			$('#csstxt').html("").addClass("text-default");
1131
		} else {
1132
			$('#csstxt').html("<?=$csswarning?>").addClass("text-danger");
1133
		}
1134
	}
1135

    
1136
	function change_keytype() {
1137
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1138
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1139
	}
1140

    
1141
	$('#webguicss').change(function() {
1142
		setThemeWarning();
1143
	});
1144

    
1145
	setThemeWarning();
1146

    
1147
	// On click . .
1148
	$('#customsettings').click(function () {
1149
		setcustomoptions();
1150
	});
1151

    
1152
	$("#movetodisabled").click(function() {
1153
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
1154
	});
1155

    
1156
	$("#movetoenabled").click(function() {
1157
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
1158
	});
1159

    
1160
	$("#showcert").click(function() {
1161
		hideClass('cert-options', !this.checked);
1162
	});
1163

    
1164
	$("#showkey").click(function() {
1165
		hideInput('authorizedkeys', false);
1166
		hideCheckbox('showkey', true);
1167
	});
1168

    
1169
	$('[id^=delcert]').click(function(event) {
1170
		if (confirm(event.target.title)) {
1171
			$('#certid').val(event.target.id.match(/\d+$/)[0]);
1172
			$('#userid').val('<?=$id;?>');
1173
			$('#act').val('delcert');
1174
			$('form').submit();
1175
		}
1176
	});
1177

    
1178
	$('[id^=delprivid]').click(function(event) {
1179
		if (confirm(event.target.title)) {
1180
			$('#privid').val(event.target.id.match(/\d+$/)[0]);
1181
			$('#userid').val('<?=$id;?>');
1182
			$('#act').val('delprivid');
1183
			$('form').submit();
1184
		}
1185
	});
1186

    
1187
	$('#expires').datepicker();
1188

    
1189
	$('#keytype').change(function () {
1190
		change_keytype();
1191
	});
1192

    
1193
	// ---------- On initial page load ------------------------------------------------------------
1194

    
1195
	hideClass('cert-options', true);
1196
	//hideInput('authorizedkeys', true);
1197
	hideCheckbox('showkey', true);
1198
	setcustomoptions();
1199
	change_keytype();
1200

    
1201
	// On submit mark all the user's groups as "selected"
1202
	$('form').submit(function() {
1203
		AllServers($('[name="groups[]"] option'), true);
1204
	});
1205

    
1206
});
1207
//]]>
1208
</script>
1209
<?php
1210
include('foot.inc');
1211
?>
(212-212/232)