Project

General

Profile

Download (34.6 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-2023 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
// start admin user code
51
if (isset($_REQUEST['userid']) && is_numericint($_REQUEST['userid'])) {
52
	$id = $_REQUEST['userid'];
53
}
54

    
55
init_config_arr(array('system', 'user'));
56
$a_user = &$config['system']['user'];
57
$act = $_REQUEST['act'];
58

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

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

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

    
102
if (!empty($_POST) && $read_only) {
103
	$input_errors = array(gettext("Insufficient privileges to make the requested change (read only)."));
104
}
105

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

    
108
	if (!isset($_POST['username']) || !isset($a_user[$id]) || ($_POST['username'] != $a_user[$id]['name'])) {
109
		pfSenseHeader("system_usermanager.php");
110
		exit;
111
	}
112

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

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

    
135
	$nonPrvCas = array();
136
	if (is_array($config['ca']) && count($config['ca']) > 0) {
137
		foreach (config_get_path('ca', []) as $ca) {
138
			if (!$ca['prv']) {
139
				continue;
140
			}
141

    
142
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
143
		}
144
	}
145

    
146
}
147

    
148
if (isset($_POST['dellall']) && !$read_only) {
149

    
150
	$del_users = $_POST['delete_check'];
151
	$deleted_users = array();
152

    
153
	if (!empty($del_users)) {
154
		foreach ($del_users as $userid) {
155
			if (isset($a_user[$userid]) && $a_user[$userid]['scope'] != "system") {
156
				if ($a_user[$userid]['name'] == $_SESSION['Username']) {
157
					$delete_errors[] = sprintf(gettext("Cannot delete user %s because you are currently logged in as that user."), $a_user[$userid]['name']);
158
				} else {
159
					$deleted_users[] = $a_user[$userid]['name'];
160
					local_user_del($a_user[$userid]);
161
					unset($a_user[$userid]);
162
				}
163
			} else {
164
				$delete_errors[] = sprintf(gettext("Cannot delete user %s because it is a system user."), $a_user[$userid]['name']);
165
			}
166
		}
167

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

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

    
180
	if (!$a_user[$id]) {
181
		pfSenseHeader("system_usermanager.php");
182
		exit;
183
	}
184

    
185
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
186
	$certdeleted = $certdeleted['descr'];
187
	unset($a_user[$id]['cert'][$_POST['certid']]);
188
	$savemsg = sprintf(gettext("Removed certificate association \"%s\" from user %s"), $certdeleted, $a_user[$id]['name']);
189
	write_config($savemsg);
190
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
191
	$_POST['act'] = "edit";
192
}
193

    
194
if (($_POST['act'] == "delprivid") && !$read_only) {
195
	$privdeleted = $priv_list[$a_user[$id]['priv'][$_POST['privid']]]['name'];
196
	unset($a_user[$id]['priv'][$_POST['privid']]);
197
	local_user_set($a_user[$id]);
198
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from user %s"), $privdeleted, $a_user[$id]['name']);
199
	write_config($savemsg);
200
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
201
	$_POST['act'] = "edit";
202
}
203

    
204
if ($_POST['save'] && !$read_only) {
205
	unset($input_errors);
206
	$pconfig = $_POST;
207

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

    
230
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
231

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

    
236
	if (strlen($_POST['usernamefld']) > 32) {
237
		$input_errors[] = gettext("The username is longer than 32 characters.");
238
	}
239

    
240
	if (($_POST['passwordfld1']) && ($_POST['passwordfld1'] != $_POST['passwordfld2'])) {
241
		$input_errors[] = gettext("The passwords do not match.");
242
	}
243

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

    
248
	/* Check the POSTed groups to ensure they are valid and exist */
249
	if (is_array($_POST['groups'])) {
250
		foreach ($_POST['groups'] as $newgroup) {
251
			if (empty(getGroupEntry($newgroup))) {
252
				$input_errors[] = gettext("One or more invalid groups was submitted.");
253
			}
254
		}
255
	}
256

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

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

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

    
312
	if (!$input_errors) {
313

    
314
		$userent = array();
315
		if (isset($id) && $a_user[$id]) {
316
			$userent = $a_user[$id];
317
		}
318

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

    
321
		/* the user name was modified */
322
		if (!empty($_POST['oldusername']) && ($_POST['usernamefld'] <> $_POST['oldusername'])) {
323
			$_SERVER['REMOTE_USER'] = $_POST['usernamefld'];
324
			local_user_del($userent);
325
		}
326

    
327
		/* the user password was modified */
328
		if ($_POST['passwordfld1']) {
329
			local_user_set_password($userent, $_POST['passwordfld1']);
330
		}
331

    
332
		/* only change description if sent */
333
		if (isset($_POST['descr'])) {
334
			$userent['descr'] = $_POST['descr'];
335
		}
336

    
337
		$userent['name'] = $_POST['usernamefld'];
338
		$userent['expires'] = $_POST['expires'];
339
		$userent['dashboardcolumns'] = $_POST['dashboardcolumns'];
340
		$userent['authorizedkeys'] = base64_encode($_POST['authorizedkeys']);
341
		$userent['ipsecpsk'] = $_POST['ipsecpsk'];
342

    
343
		if ($_POST['disabled']) {
344
			$userent['disabled'] = true;
345
		} else {
346
			unset($userent['disabled']);
347
		}
348

    
349
		if ($_POST['customsettings']) {
350
			$userent['customsettings'] = true;
351
		} else {
352
			unset($userent['customsettings']);
353
		}
354

    
355
		if ($_POST['webguicss']) {
356
			$userent['webguicss'] = $_POST['webguicss'];
357
		} else {
358
			unset($userent['webguicss']);
359
		}
360

    
361
		if ($_POST['webguifixedmenu']) {
362
			$userent['webguifixedmenu'] = $_POST['webguifixedmenu'];
363
		} else {
364
			unset($userent['webguifixedmenu']);
365
		}
366

    
367
		if ($_POST['webguihostnamemenu']) {
368
			$userent['webguihostnamemenu'] = $_POST['webguihostnamemenu'];
369
		} else {
370
			unset($userent['webguihostnamemenu']);
371
		}
372

    
373
		if ($_POST['interfacessort']) {
374
			$userent['interfacessort'] = true;
375
		} else {
376
			unset($userent['interfacessort']);
377
		}
378

    
379
		if ($_POST['dashboardavailablewidgetspanel']) {
380
			$userent['dashboardavailablewidgetspanel'] = true;
381
		} else {
382
			unset($userent['dashboardavailablewidgetspanel']);
383
		}
384

    
385
		if ($_POST['systemlogsfilterpanel']) {
386
			$userent['systemlogsfilterpanel'] = true;
387
		} else {
388
			unset($userent['systemlogsfilterpanel']);
389
		}
390

    
391
		if ($_POST['systemlogsmanagelogpanel']) {
392
			$userent['systemlogsmanagelogpanel'] = true;
393
		} else {
394
			unset($userent['systemlogsmanagelogpanel']);
395
		}
396

    
397
		if ($_POST['statusmonitoringsettingspanel']) {
398
			$userent['statusmonitoringsettingspanel'] = true;
399
		} else {
400
			unset($userent['statusmonitoringsettingspanel']);
401
		}
402

    
403
		if ($_POST['webguileftcolumnhyper']) {
404
			$userent['webguileftcolumnhyper'] = true;
405
		} else {
406
			unset($userent['webguileftcolumnhyper']);
407
		}
408

    
409
		if ($_POST['disablealiaspopupdetail']) {
410
			$userent['disablealiaspopupdetail'] = true;
411
		} else {
412
			unset($userent['disablealiaspopupdetail']);
413
		}
414

    
415
		if ($_POST['pagenamefirst']) {
416
			$userent['pagenamefirst'] = true;
417
		} else {
418
			unset($userent['pagenamefirst']);
419
		}
420

    
421
		if ($_POST['keephistory']) {
422
			$userent['keephistory'] = true;
423
		} else {
424
			unset($userent['keephistory']);
425
		}
426

    
427
		if (isset($id) && $a_user[$id]) {
428
			$a_user[$id] = $userent;
429
		} else {
430
			if (!empty($_POST['name'])) {
431
				$cert = array();
432
				$cert['refid'] = uniqid();
433
				$userent['cert'] = array();
434

    
435
				$cert['descr'] = $_POST['name'];
436

    
437
				$subject = cert_get_subject_hash($ca['crt']);
438

    
439
				$dn = array();
440
				if (!empty($subject['C'])) {
441
					$dn['countryName'] = $subject['C'];
442
				}
443
				if (!empty($subject['ST'])) {
444
					$dn['stateOrProvinceName'] = $subject['ST'];
445
				}
446
				if (!empty($subject['L'])) {
447
					$dn['localityName'] = $subject['L'];
448
				}
449
				if (!empty($subject['O'])) {
450
					$dn['organizationName'] = $subject['O'];
451
				}
452
				if (!empty($subject['OU'])) {
453
					$dn['organizationalUnitName'] = $subject['OU'];
454
				}
455
				$dn['commonName'] = $userent['name'];
456
				$cn_altname = cert_add_altname_type($userent['name']);
457
				if (!empty($cn_altname)) {
458
					$dn['subjectAltName'] = $cn_altname;
459
				}
460

    
461
				cert_create($cert, $_POST['caref'], $_POST['keylen'],
462
					(int)$_POST['lifetime'], $dn, 'user',
463
					$_POST['digest_alg'], $_POST['keytype'],
464
					$_POST['ecname']);
465

    
466
				if (!is_array($config['cert'])) {
467
					config_set_path('cert', array());
468
				}
469
				$config['cert'][] = $cert;
470
				$userent['cert'][] = $cert['refid'];
471
			}
472
			$userent['uid'] = $config['system']['nextuid']++;
473
			/* Add the user to All Users group. */
474
			foreach (config_get_path('system/group', []) as $gidx => $group) {
475
				if ($group['name'] == "all") {
476
					if (!is_array($config['system']['group'][$gidx]['member'])) {
477
						$config['system']['group'][$gidx]['member'] = array();
478
					}
479
					$config['system']['group'][$gidx]['member'][] = $userent['uid'];
480
					break;
481
				}
482
			}
483

    
484
			$a_user[] = $userent;
485
		}
486

    
487
		/* Sort it alphabetically */
488
		usort($config['system']['user'], function($a, $b) {
489
			return strcmp($a['name'], $b['name']);
490
		});
491

    
492
		local_user_set_groups($userent, $_POST['groups']);
493
		local_user_set($userent);
494

    
495
		/* Update user index to account for new changes */
496
		global $userindex;
497
		$userindex = index_users();
498

    
499
		$savemsg = sprintf(gettext("Successfully %s user %s"), (isset($id)) ? gettext("edited") : gettext("created"), $userent['name']);
500
		write_config($savemsg);
501
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
502
		if (is_dir("/etc/inc/privhooks")) {
503
			run_plugins("/etc/inc/privhooks");
504
		}
505

    
506
		if ($userent['uid'] == 0) {
507
			log_error(gettext("Restarting sshd due to admin account change."));
508
			send_event("service restart sshd");
509
		}
510

    
511
		pfSenseHeader("system_usermanager.php");
512
	}
513
}
514

    
515
function build_priv_table() {
516
	global $a_user, $id, $read_only;
517

    
518
	$privhtml = '<div class="table-responsive">';
519
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
520
	$privhtml .=		'<thead>';
521
	$privhtml .=			'<tr>';
522
	$privhtml .=				'<th>' . gettext('Inherited from') . '</th>';
523
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
524
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
525
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
526
	$privhtml .=			'</tr>';
527
	$privhtml .=		'</thead>';
528
	$privhtml .=		'<tbody>';
529

    
530
	$i = 0;
531
	$user_has_root_priv = false;
532

    
533
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
534
		$group = false;
535
		if ($priv['group']) {
536
			$group = $priv['group'];
537
		}
538

    
539
		$privhtml .=		'<tr>';
540
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
541
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
542
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
543
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
544
			$privhtml .=			' ' . gettext('(admin privilege)');
545
			$user_has_root_priv = true;
546
		}
547
		$privhtml .=			'</td>';
548
		$privhtml .=			'<td>';
549
		if (!$group && !$read_only) {
550
			$privhtml .=			'<a class="fa-solid fa-trash-can no-confirm icon-pointer" title="' . gettext('Delete Privilege') . '" id="delprivid' . $i . '"></a>';
551
		}
552

    
553
		$privhtml .=			'</td>';
554
		$privhtml .=		'</tr>';
555

    
556
		if (!$group) {
557
			$i++;
558
		}
559
	}
560

    
561
	if ($user_has_root_priv) {
562
		$privhtml .=		'<tr>';
563
		$privhtml .=			'<td colspan="3">';
564
		$privhtml .=				'<b>' . gettext('Security notice: This user effectively has administrator-level access') . '</b>';
565
		$privhtml .=			'</td>';
566
		$privhtml .=			'<td>';
567
		$privhtml .=			'</td>';
568
		$privhtml .=		'</tr>';
569

    
570
	}
571

    
572
	$privhtml .=		'</tbody>';
573
	$privhtml .=	'</table>';
574
	$privhtml .= '</div>';
575

    
576
	$privhtml .= '<nav class="action-buttons">';
577
	if (!$read_only) {
578
		$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>';
579
	}
580
	$privhtml .= '</nav>';
581

    
582
	return($privhtml);
583
}
584

    
585
function build_cert_table() {
586
	global $a_user, $id, $read_only;
587

    
588
	$certhtml = '<div class="table-responsive">';
589
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
590
	$certhtml .=		'<thead>';
591
	$certhtml .=			'<tr>';
592
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
593
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
594
	$certhtml .=				'<th></th>';
595
	$certhtml .=			'</tr>';
596
	$certhtml .=		'</thead>';
597
	$certhtml .=		'<tbody>';
598

    
599
	$a_cert = $a_user[$id]['cert'];
600
	if (is_array($a_cert)) {
601
		$i = 0;
602
		foreach ($a_cert as $certref) {
603
			$cert = lookup_cert($certref);
604
			$ca = lookup_ca($cert['caref']);
605
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
606

    
607
			$certhtml .=	'<tr>';
608
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
609
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
610
			$certhtml .=		'<td>';
611
			if (!$read_only) {
612
				$certhtml .=			'<a id="delcert' . $i .'" class="fa-solid fa-trash-can no-confirm icon-pointer" title="';
613
				$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
614
			}
615
			$certhtml .=		'</td>';
616
			$certhtml .=	'</tr>';
617
			$i++;
618
		}
619

    
620
	}
621

    
622
	$certhtml .=		'</tbody>';
623
	$certhtml .=	'</table>';
624
	$certhtml .= '</div>';
625

    
626
	$certhtml .= '<nav class="action-buttons">';
627
	if (!$read_only) {
628
		$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>';
629
	}
630
	$certhtml .= '</nav>';
631

    
632
	return($certhtml);
633
}
634

    
635
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
636
$pglinks = array("", "system_usermanager.php", "system_usermanager.php");
637

    
638
if ($act == "new" || $act == "edit" || $input_errors) {
639
	$pgtitle[] = gettext('Edit');
640
	$pglinks[] = "@self";
641
}
642

    
643
include("head.inc");
644

    
645
if ($delete_errors) {
646
	print_input_errors($delete_errors);
647
}
648

    
649
if ($input_errors) {
650
	print_input_errors($input_errors);
651
}
652

    
653
if ($savemsg) {
654
	print_info_box($savemsg, 'success');
655
}
656

    
657
$tab_array = array();
658
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
659
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
660
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
661
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
662
display_top_tabs($tab_array);
663

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

    
720
	<a href="?act=new" class="btn btn-sm btn-success">
721
		<i class="fa-solid fa-plus icon-embed-btn"></i>
722
		<?=gettext("Add")?>
723
	</a>
724

    
725
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
726
		<i class="fa-solid fa-trash-can icon-embed-btn"></i>
727
		<?=gettext("Delete")?>
728
	</button>
729
	<?php endif; ?>
730

    
731
</nav>
732
</form>
733
<div class="infoblock">
734
<?php
735
	print_callout('<p>' . gettext("Additional users can be added here. User permissions for accessing " .
736
		"the webConfigurator can be assigned directly or inherited from group memberships. " .
737
		"Some system object properties can be modified but they cannot be deleted.") . '</p>' .
738
		'<p>' . gettext("Accounts added here are also used for other parts of the system " .
739
		"such as OpenVPN, IPsec, and Captive Portal.") . '</p>'
740
	);
741

    
742
?></div>
743

    
744
<?php
745
	include("foot.inc");
746
	exit;
747
}
748

    
749
$form = new Form;
750

    
751
if ($act == "new" || $act == "edit" || $input_errors):
752

    
753
	$form->addGlobal(new Form_Input(
754
		'act',
755
		null,
756
		'hidden',
757
		''
758
	));
759

    
760
	$form->addGlobal(new Form_Input(
761
		'userid',
762
		null,
763
		'hidden',
764
		isset($id) ? $id:''
765
	));
766

    
767
	$form->addGlobal(new Form_Input(
768
		'privid',
769
		null,
770
		'hidden',
771
		''
772
	));
773

    
774
	$form->addGlobal(new Form_Input(
775
		'certid',
776
		null,
777
		'hidden',
778
		''
779
	));
780

    
781
	$ro = "";
782
	if ($pconfig['utype'] == "system") {
783
		$ro = "readonly";
784
	}
785

    
786
	$section = new Form_Section('User Properties');
787

    
788
	$section->addInput(new Form_StaticText(
789
		'Defined by',
790
		strtoupper($pconfig['utype'])
791
	));
792

    
793
	$form->addGlobal(new Form_Input(
794
		'utype',
795
		null,
796
		'hidden',
797
		$pconfig['utype']
798
	));
799

    
800
	$section->addInput(new Form_Checkbox(
801
		'disabled',
802
		'Disabled',
803
		'This user cannot login',
804
		$pconfig['disabled']
805
	));
806

    
807
	$section->addInput($input = new Form_Input(
808
		'usernamefld',
809
		'*Username',
810
		'text',
811
		$pconfig['usernamefld'],
812
		['autocomplete' => 'new-password']
813
	));
814

    
815
	if ($ro) {
816
		$input->setReadonly();
817
	}
818

    
819
	$form->addGlobal(new Form_Input(
820
		'oldusername',
821
		null,
822
		'hidden',
823
		$pconfig['usernamefld']
824
	));
825

    
826
	if ($act == "edit") {
827
		$pwd_required = "";
828
	} else {
829
		$pwd_required = "*";
830
	}
831

    
832
	$group = new Form_Group($pwd_required . 'Password');
833
	$group->add(new Form_Input(
834
		'passwordfld1',
835
		'Password',
836
		'password',
837
		null,
838
		['autocomplete' => 'new-password']
839
	));
840
	$group->add(new Form_Input(
841
		'passwordfld2',
842
		'Confirm Password',
843
		'password',
844
		null,
845
		['autocomplete' => 'new-password']
846
	));
847

    
848
	$section->add($group);
849

    
850
	$section->addInput($input = new Form_Input(
851
		'descr',
852
		'Full name',
853
		'text',
854
		htmlspecialchars($pconfig['descr'])
855
	))->setHelp('User\'s full name, for administrative information only');
856

    
857
	if ($ro) {
858
		$input->setDisabled();
859
	}
860

    
861
	$section->addInput(new Form_Input(
862
		'expires',
863
		'Expiration date',
864
		'text',
865
		$pconfig['expires']
866
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
867
		'the expiration date as MM/DD/YYYY');
868

    
869
	$section->addInput(new Form_Checkbox(
870
		'customsettings',
871
		'Custom Settings',
872
		'Use individual customized GUI options and dashboard layout for this user.',
873
		$pconfig['customsettings']
874
	));
875

    
876
	gen_user_settings_fields($section, $pconfig);
877

    
878
	// ==== Group membership ==================================================
879
	$group = new Form_Group('Group membership');
880

    
881
	// Make a list of all the groups configured on the system, and a list of
882
	// those which this user is a member of
883
	$systemGroups = array();
884
	$usersGroups = array();
885

    
886
	$usergid = [$pconfig['usernamefld']];
887

    
888
	foreach (config_get_path('system/group', []) as $Ggroup) {
889
		if ($Ggroup['name'] != "all") {
890
			if (($act == 'edit' || $input_errors) && $Ggroup['member'] && in_array($a_user[$id]['uid'], $Ggroup['member'])) {
891
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
892
			} else {
893
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
894
			}
895
		}
896
	}
897

    
898
	$group->add(new Form_Select(
899
		'sysgroups',
900
		null,
901
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
902
		$systemGroups,
903
		true
904
	))->setHelp('Not member of');
905

    
906
	$group->add(new Form_Select(
907
		'groups',
908
		null,
909
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
910
		$usersGroups,
911
		true
912
	))->setHelp('Member of');
913

    
914
	$section->add($group);
915

    
916
	$group = new Form_Group('');
917

    
918
	$group->add(new Form_Button(
919
		'movetoenabled',
920
		'Move to "Member of" list',
921
		null,
922
		'fa-angle-double-right'
923
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
924

    
925
	$group->add(new Form_Button(
926
		'movetodisabled',
927
		'Move to "Not member of" list',
928
		null,
929
		'fa-angle-double-left'
930
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
931

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

    
935
	// ==== Button for adding user certificate ================================
936
	if ($act == 'new') {
937
		if (count($nonPrvCas) > 0) {
938
			$section->addInput(new Form_Checkbox(
939
				'showcert',
940
				'Certificate',
941
				'Click to create a user certificate',
942
				false
943
			));
944
		} else {
945
			$section->addInput(new Form_StaticText(
946
				'Certificate',
947
				gettext('No private CAs found. A private CA is required to create a new user certificate. ' .
948
					'Save the user first to import an external certificate.')
949
			));
950
		}
951
	}
952

    
953
	$form->add($section);
954

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

    
960
		$section = new Form_Section('Effective Privileges');
961

    
962
		$section->addInput(new Form_StaticText(
963
			null,
964
			build_priv_table()
965
		));
966

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

    
969
		// ==== Certificate table section =====================================
970
		$section = new Form_Section('User Certificates');
971

    
972
		$section->addInput(new Form_StaticText(
973
			null,
974
			build_cert_table()
975
		));
976

    
977
		$form->add($section);
978
	}
979

    
980
	// ==== Add user certificate for a new user
981
	if (is_array($config['ca']) && count($config['ca']) > 0) {
982
		$section = new Form_Section('Create Certificate for User');
983
		$section->addClass('cert-options');
984

    
985
		if (!empty($nonPrvCas)) {
986
			$section->addInput(new Form_Input(
987
				'name',
988
				'Descriptive name',
989
				'text',
990
				$pconfig['name']
991
			));
992

    
993
			$section->addInput(new Form_Select(
994
				'caref',
995
				'Certificate authority',
996
				null,
997
				$nonPrvCas
998
			));
999

    
1000
			$section->addInput(new Form_Select(
1001
				'keytype',
1002
				'*Key type',
1003
				$pconfig['keytype'],
1004
				array_combine($cert_keytypes, $cert_keytypes)
1005
			));
1006

    
1007
			$group = new Form_Group($i == 0 ? '*Key length':'');
1008
			$group->addClass('rsakeys');
1009
			$group->add(new Form_Select(
1010
				'keylen',
1011
				null,
1012
				$pconfig['keylen'] ? $pconfig['keylen'] : '2048',
1013
				array_combine($cert_keylens, $cert_keylens)
1014
			))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
1015
				'The Key Length should not be lower than 2048 or some platforms ' .
1016
				'may consider the certificate invalid.', '<br/>');
1017
			$section->add($group);
1018

    
1019
			$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
1020
			$group->addClass('ecnames');
1021
			$group->add(new Form_Select(
1022
				'ecname',
1023
				null,
1024
				$pconfig['ecname'] ? $pconfig['ecname'] : 'prime256v1',
1025
				$openssl_ecnames
1026
			))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
1027
			$section->add($group);
1028

    
1029
			$section->addInput(new Form_Select(
1030
				'digest_alg',
1031
				'*Digest Algorithm',
1032
				$pconfig['digest_alg'] ? $pconfig['digest_alg'] : 'sha256',
1033
				array_combine($openssl_digest_algs, $openssl_digest_algs)
1034
			))->setHelp('The digest method used when the certificate is signed. %1$s' .
1035
				'The best practice is to use an algorithm stronger than SHA1. '.
1036
				'Some platforms may consider weaker digest algorithms invalid', '<br/>');
1037

    
1038
			$section->addInput(new Form_Input(
1039
				'lifetime',
1040
				'Lifetime',
1041
				'number',
1042
				$pconfig['lifetime']
1043
			));
1044
		}
1045

    
1046
		$form->add($section);
1047
	}
1048

    
1049
endif;
1050
// ==== Paste a key for the new user
1051
$section = new Form_Section('Keys');
1052

    
1053
$section->addInput(new Form_Checkbox(
1054
	'showkey',
1055
	'Authorized keys',
1056
	'Click to paste an authorized key',
1057
	false
1058
));
1059

    
1060
$section->addInput(new Form_Textarea(
1061
	'authorizedkeys',
1062
	'Authorized SSH Keys',
1063
	$pconfig['authorizedkeys']
1064
))->setHelp('Enter authorized SSH keys for this user');
1065

    
1066
$section->addInput(new Form_Input(
1067
	'ipsecpsk',
1068
	'IPsec Pre-Shared Key',
1069
	'text',
1070
	$pconfig['ipsecpsk']
1071
));
1072

    
1073
$form->add($section);
1074

    
1075
$section = new Form_Section('Shell Behavior');
1076

    
1077
$section->addInput(new Form_Checkbox(
1078
	'keephistory',
1079
	'Keep Command History',
1080
	'Keep shell command history between login sessions',
1081
	$pconfig['keephistory']
1082
))->setHelp('If this user has shell access, this option preserves the last 1000 unique commands entered at a shell prompt between login sessions. ' .
1083
		'The user can access history using the up and down arrows at an SSH or console shell prompt ' .
1084
		'and search the history by typing a partial command and then using the up or down arrows.');
1085

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

    
1088
print $form;
1089

    
1090
$csswarning = sprintf(gettext("%sUser-created themes are unsupported, use at your own risk."), "<br />");
1091
?>
1092
<script type="text/javascript">
1093
//<![CDATA[
1094
events.push(function() {
1095

    
1096
	function setcustomoptions() {
1097
		var adv = $('#customsettings').prop('checked');
1098

    
1099
		hideInput('webguicss', !adv);
1100
		hideInput('webguifixedmenu', !adv);
1101
		hideInput('webguihostnamemenu', !adv);
1102
		hideInput('dashboardcolumns', !adv);
1103
		hideCheckbox('interfacessort', !adv);
1104
		hideCheckbox('dashboardavailablewidgetspanel', !adv);
1105
		hideCheckbox('systemlogsfilterpanel', !adv);
1106
		hideCheckbox('systemlogsmanagelogpanel', !adv);
1107
		hideCheckbox('statusmonitoringsettingspanel', !adv);
1108
		hideCheckbox('webguileftcolumnhyper', !adv);
1109
		hideCheckbox('disablealiaspopupdetail', !adv);
1110
		hideCheckbox('pagenamefirst', !adv);
1111
	}
1112

    
1113
	// Handle displaying a warning message if a user-created theme is selected.
1114
	function setThemeWarning() {
1115
		if ($('#webguicss').val().startsWith("pfSense")) {
1116
			$('#csstxt').html("").addClass("text-default");
1117
		} else {
1118
			$('#csstxt').html("<?=$csswarning?>").addClass("text-danger");
1119
		}
1120
	}
1121

    
1122
	function change_keytype() {
1123
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1124
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1125
	}
1126

    
1127
	$('#webguicss').change(function() {
1128
		setThemeWarning();
1129
	});
1130

    
1131
	setThemeWarning();
1132

    
1133
	// On click . .
1134
	$('#customsettings').click(function () {
1135
		setcustomoptions();
1136
	});
1137

    
1138
	$("#movetodisabled").click(function() {
1139
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
1140
	});
1141

    
1142
	$("#movetoenabled").click(function() {
1143
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
1144
	});
1145

    
1146
	$("#showcert").click(function() {
1147
		hideClass('cert-options', !this.checked);
1148
	});
1149

    
1150
	$("#showkey").click(function() {
1151
		hideInput('authorizedkeys', false);
1152
		hideCheckbox('showkey', true);
1153
	});
1154

    
1155
	$('[id^=delcert]').click(function(event) {
1156
		if (confirm(event.target.title)) {
1157
			$('#certid').val(event.target.id.match(/\d+$/)[0]);
1158
			$('#userid').val('<?=$id;?>');
1159
			$('#act').val('delcert');
1160
			$('form').submit();
1161
		}
1162
	});
1163

    
1164
	$('[id^=delprivid]').click(function(event) {
1165
		if (confirm(event.target.title)) {
1166
			$('#privid').val(event.target.id.match(/\d+$/)[0]);
1167
			$('#userid').val('<?=$id;?>');
1168
			$('#act').val('delprivid');
1169
			$('form').submit();
1170
		}
1171
	});
1172

    
1173
	$('#expires').datepicker();
1174

    
1175
	$('#keytype').change(function () {
1176
		change_keytype();
1177
	});
1178

    
1179
	// ---------- On initial page load ------------------------------------------------------------
1180

    
1181
	hideClass('cert-options', true);
1182
	//hideInput('authorizedkeys', true);
1183
	hideCheckbox('showkey', true);
1184
	setcustomoptions();
1185
	change_keytype();
1186

    
1187
	// On submit mark all the user's groups as "selected"
1188
	$('form').submit(function() {
1189
		AllServers($('[name="groups[]"] option'), true);
1190
	});
1191

    
1192
});
1193
//]]>
1194
</script>
1195
<?php
1196
include('foot.inc');
1197
?>
(208-208/228)