Project

General

Profile

Download (33.7 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-2022 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
}
90

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

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

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

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

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

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

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

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

    
145
}
146

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
311
	if (!$input_errors) {
312

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
420
		if (isset($id) && $a_user[$id]) {
421
			$a_user[$id] = $userent;
422
		} else {
423
			if (!empty($_POST['name'])) {
424
				$cert = array();
425
				$cert['refid'] = uniqid();
426
				$userent['cert'] = array();
427

    
428
				$cert['descr'] = $_POST['name'];
429

    
430
				$subject = cert_get_subject_hash($ca['crt']);
431

    
432
				$dn = array();
433
				if (!empty($subject['C'])) {
434
					$dn['countryName'] = $subject['C'];
435
				}
436
				if (!empty($subject['ST'])) {
437
					$dn['stateOrProvinceName'] = $subject['ST'];
438
				}
439
				if (!empty($subject['L'])) {
440
					$dn['localityName'] = $subject['L'];
441
				}
442
				if (!empty($subject['O'])) {
443
					$dn['organizationName'] = $subject['O'];
444
				}
445
				if (!empty($subject['OU'])) {
446
					$dn['organizationalUnitName'] = $subject['OU'];
447
				}
448
				$dn['commonName'] = $userent['name'];
449
				$cn_altname = cert_add_altname_type($userent['name']);
450
				if (!empty($cn_altname)) {
451
					$dn['subjectAltName'] = $cn_altname;
452
				}
453

    
454
				cert_create($cert, $_POST['caref'], $_POST['keylen'],
455
					(int)$_POST['lifetime'], $dn, 'user',
456
					$_POST['digest_alg'], $_POST['keytype'],
457
					$_POST['ecname']);
458

    
459
				if (!is_array($config['cert'])) {
460
					$config['cert'] = array();
461
				}
462
				$config['cert'][] = $cert;
463
				$userent['cert'][] = $cert['refid'];
464
			}
465
			$userent['uid'] = $config['system']['nextuid']++;
466
			/* Add the user to All Users group. */
467
			foreach ($config['system']['group'] as $gidx => $group) {
468
				if ($group['name'] == "all") {
469
					if (!is_array($config['system']['group'][$gidx]['member'])) {
470
						$config['system']['group'][$gidx]['member'] = array();
471
					}
472
					$config['system']['group'][$gidx]['member'][] = $userent['uid'];
473
					break;
474
				}
475
			}
476

    
477
			$a_user[] = $userent;
478
		}
479

    
480
		/* Sort it alphabetically */
481
		usort($config['system']['user'], function($a, $b) {
482
			return strcmp($a['name'], $b['name']);
483
		});
484

    
485
		local_user_set_groups($userent, $_POST['groups']);
486
		local_user_set($userent);
487

    
488
		/* Update user index to account for new changes */
489
		global $userindex;
490
		$userindex = index_users();
491

    
492
		$savemsg = sprintf(gettext("Successfully %s user %s"), (isset($id)) ? gettext("edited") : gettext("created"), $userent['name']);
493
		write_config($savemsg);
494
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
495
		if (is_dir("/etc/inc/privhooks")) {
496
			run_plugins("/etc/inc/privhooks");
497
		}
498

    
499
		if ($userent['uid'] == 0) {
500
			log_error(gettext("Restarting sshd due to admin account change."));
501
			send_event("service restart sshd");
502
		}
503

    
504
		pfSenseHeader("system_usermanager.php");
505
	}
506
}
507

    
508
function build_priv_table() {
509
	global $a_user, $id, $read_only;
510

    
511
	$privhtml = '<div class="table-responsive">';
512
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
513
	$privhtml .=		'<thead>';
514
	$privhtml .=			'<tr>';
515
	$privhtml .=				'<th>' . gettext('Inherited from') . '</th>';
516
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
517
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
518
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
519
	$privhtml .=			'</tr>';
520
	$privhtml .=		'</thead>';
521
	$privhtml .=		'<tbody>';
522

    
523
	$i = 0;
524
	$user_has_root_priv = false;
525

    
526
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
527
		$group = false;
528
		if ($priv['group']) {
529
			$group = $priv['group'];
530
		}
531

    
532
		$privhtml .=		'<tr>';
533
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
534
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
535
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
536
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
537
			$privhtml .=			' ' . gettext('(admin privilege)');
538
			$user_has_root_priv = true;
539
		}
540
		$privhtml .=			'</td>';
541
		$privhtml .=			'<td>';
542
		if (!$group && !$read_only) {
543
			$privhtml .=			'<a class="fa fa-trash no-confirm icon-pointer" title="' . gettext('Delete Privilege') . '" id="delprivid' . $i . '"></a>';
544
		}
545

    
546
		$privhtml .=			'</td>';
547
		$privhtml .=		'</tr>';
548

    
549
		if (!$group) {
550
			$i++;
551
		}
552
	}
553

    
554
	if ($user_has_root_priv) {
555
		$privhtml .=		'<tr>';
556
		$privhtml .=			'<td colspan="3">';
557
		$privhtml .=				'<b>' . gettext('Security notice: This user effectively has administrator-level access') . '</b>';
558
		$privhtml .=			'</td>';
559
		$privhtml .=			'<td>';
560
		$privhtml .=			'</td>';
561
		$privhtml .=		'</tr>';
562

    
563
	}
564

    
565
	$privhtml .=		'</tbody>';
566
	$privhtml .=	'</table>';
567
	$privhtml .= '</div>';
568

    
569
	$privhtml .= '<nav class="action-buttons">';
570
	if (!$read_only) {
571
		$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success"><i class="fa fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
572
	}
573
	$privhtml .= '</nav>';
574

    
575
	return($privhtml);
576
}
577

    
578
function build_cert_table() {
579
	global $a_user, $id, $read_only;
580

    
581
	$certhtml = '<div class="table-responsive">';
582
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
583
	$certhtml .=		'<thead>';
584
	$certhtml .=			'<tr>';
585
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
586
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
587
	$certhtml .=				'<th></th>';
588
	$certhtml .=			'</tr>';
589
	$certhtml .=		'</thead>';
590
	$certhtml .=		'<tbody>';
591

    
592
	$a_cert = $a_user[$id]['cert'];
593
	if (is_array($a_cert)) {
594
		$i = 0;
595
		foreach ($a_cert as $certref) {
596
			$cert = lookup_cert($certref);
597
			$ca = lookup_ca($cert['caref']);
598
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
599

    
600
			$certhtml .=	'<tr>';
601
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
602
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
603
			$certhtml .=		'<td>';
604
			if (!$read_only) {
605
				$certhtml .=			'<a id="delcert' . $i .'" class="fa fa-trash no-confirm icon-pointer" title="';
606
				$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
607
			}
608
			$certhtml .=		'</td>';
609
			$certhtml .=	'</tr>';
610
			$i++;
611
		}
612

    
613
	}
614

    
615
	$certhtml .=		'</tbody>';
616
	$certhtml .=	'</table>';
617
	$certhtml .= '</div>';
618

    
619
	$certhtml .= '<nav class="action-buttons">';
620
	if (!$read_only) {
621
		$certhtml .=	'<a href="system_certmanager.php?act=new&amp;userid=' . $id . '" class="btn btn-success"><i class="fa fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
622
	}
623
	$certhtml .= '</nav>';
624

    
625
	return($certhtml);
626
}
627

    
628
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
629
$pglinks = array("", "system_usermanager.php", "system_usermanager.php");
630

    
631
if ($act == "new" || $act == "edit" || $input_errors) {
632
	$pgtitle[] = gettext('Edit');
633
	$pglinks[] = "@self";
634
}
635

    
636
include("head.inc");
637

    
638
if ($delete_errors) {
639
	print_input_errors($delete_errors);
640
}
641

    
642
if ($input_errors) {
643
	print_input_errors($input_errors);
644
}
645

    
646
if ($savemsg) {
647
	print_info_box($savemsg, 'success');
648
}
649

    
650
$tab_array = array();
651
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
652
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
653
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
654
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
655
display_top_tabs($tab_array);
656

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

    
713
	<a href="?act=new" class="btn btn-sm btn-success">
714
		<i class="fa fa-plus icon-embed-btn"></i>
715
		<?=gettext("Add")?>
716
	</a>
717

    
718
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
719
		<i class="fa fa-trash icon-embed-btn"></i>
720
		<?=gettext("Delete")?>
721
	</button>
722
	<?php endif; ?>
723

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

    
735
?></div>
736

    
737
<?php
738
	include("foot.inc");
739
	exit;
740
}
741

    
742
$form = new Form;
743

    
744
if ($act == "new" || $act == "edit" || $input_errors):
745

    
746
	$form->addGlobal(new Form_Input(
747
		'act',
748
		null,
749
		'hidden',
750
		''
751
	));
752

    
753
	$form->addGlobal(new Form_Input(
754
		'userid',
755
		null,
756
		'hidden',
757
		isset($id) ? $id:''
758
	));
759

    
760
	$form->addGlobal(new Form_Input(
761
		'privid',
762
		null,
763
		'hidden',
764
		''
765
	));
766

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

    
774
	$ro = "";
775
	if ($pconfig['utype'] == "system") {
776
		$ro = "readonly";
777
	}
778

    
779
	$section = new Form_Section('User Properties');
780

    
781
	$section->addInput(new Form_StaticText(
782
		'Defined by',
783
		strtoupper($pconfig['utype'])
784
	));
785

    
786
	$form->addGlobal(new Form_Input(
787
		'utype',
788
		null,
789
		'hidden',
790
		$pconfig['utype']
791
	));
792

    
793
	$section->addInput(new Form_Checkbox(
794
		'disabled',
795
		'Disabled',
796
		'This user cannot login',
797
		$pconfig['disabled']
798
	));
799

    
800
	$section->addInput($input = new Form_Input(
801
		'usernamefld',
802
		'*Username',
803
		'text',
804
		$pconfig['usernamefld'],
805
		['autocomplete' => 'new-password']
806
	));
807

    
808
	if ($ro) {
809
		$input->setReadonly();
810
	}
811

    
812
	$form->addGlobal(new Form_Input(
813
		'oldusername',
814
		null,
815
		'hidden',
816
		$pconfig['usernamefld']
817
	));
818

    
819
	if ($act == "edit") {
820
		$pwd_required = "";
821
	} else {
822
		$pwd_required = "*";
823
	}
824

    
825
	$group = new Form_Group($pwd_required . 'Password');
826
	$group->add(new Form_Input(
827
		'passwordfld1',
828
		'Password',
829
		'password',
830
		null,
831
		['autocomplete' => 'new-password']
832
	));
833
	$group->add(new Form_Input(
834
		'passwordfld2',
835
		'Confirm Password',
836
		'password',
837
		null,
838
		['autocomplete' => 'new-password']
839
	));
840

    
841
	$section->add($group);
842

    
843
	$section->addInput($input = new Form_Input(
844
		'descr',
845
		'Full name',
846
		'text',
847
		htmlspecialchars($pconfig['descr'])
848
	))->setHelp('User\'s full name, for administrative information only');
849

    
850
	if ($ro) {
851
		$input->setDisabled();
852
	}
853

    
854
	$section->addInput(new Form_Input(
855
		'expires',
856
		'Expiration date',
857
		'text',
858
		$pconfig['expires']
859
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
860
		'the expiration date as MM/DD/YYYY');
861

    
862
	$section->addInput(new Form_Checkbox(
863
		'customsettings',
864
		'Custom Settings',
865
		'Use individual customized GUI options and dashboard layout for this user.',
866
		$pconfig['customsettings']
867
	));
868

    
869
	gen_user_settings_fields($section, $pconfig);
870

    
871
	// ==== Group membership ==================================================
872
	$group = new Form_Group('Group membership');
873

    
874
	// Make a list of all the groups configured on the system, and a list of
875
	// those which this user is a member of
876
	$systemGroups = array();
877
	$usersGroups = array();
878

    
879
	$usergid = [$pconfig['usernamefld']];
880

    
881
	foreach ($config['system']['group'] as $Ggroup) {
882
		if ($Ggroup['name'] != "all") {
883
			if (($act == 'edit' || $input_errors) && $Ggroup['member'] && in_array($a_user[$id]['uid'], $Ggroup['member'])) {
884
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
885
			} else {
886
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
887
			}
888
		}
889
	}
890

    
891
	$group->add(new Form_Select(
892
		'sysgroups',
893
		null,
894
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
895
		$systemGroups,
896
		true
897
	))->setHelp('Not member of');
898

    
899
	$group->add(new Form_Select(
900
		'groups',
901
		null,
902
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
903
		$usersGroups,
904
		true
905
	))->setHelp('Member of');
906

    
907
	$section->add($group);
908

    
909
	$group = new Form_Group('');
910

    
911
	$group->add(new Form_Button(
912
		'movetoenabled',
913
		'Move to "Member of" list',
914
		null,
915
		'fa-angle-double-right'
916
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
917

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

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

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

    
946
	$form->add($section);
947

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

    
953
		$section = new Form_Section('Effective Privileges');
954

    
955
		$section->addInput(new Form_StaticText(
956
			null,
957
			build_priv_table()
958
		));
959

    
960
		$form->add($section);
961

    
962
		// ==== Certificate table section =====================================
963
		$section = new Form_Section('User Certificates');
964

    
965
		$section->addInput(new Form_StaticText(
966
			null,
967
			build_cert_table()
968
		));
969

    
970
		$form->add($section);
971
	}
972

    
973
	// ==== Add user certificate for a new user
974
	if (is_array($config['ca']) && count($config['ca']) > 0) {
975
		$section = new Form_Section('Create Certificate for User');
976
		$section->addClass('cert-options');
977

    
978
		if (!empty($nonPrvCas)) {
979
			$section->addInput(new Form_Input(
980
				'name',
981
				'Descriptive name',
982
				'text',
983
				$pconfig['name']
984
			));
985

    
986
			$section->addInput(new Form_Select(
987
				'caref',
988
				'Certificate authority',
989
				null,
990
				$nonPrvCas
991
			));
992

    
993
			$section->addInput(new Form_Select(
994
				'keytype',
995
				'*Key type',
996
				$pconfig['keytype'],
997
				array_combine($cert_keytypes, $cert_keytypes)
998
			));
999

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

    
1012
			$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
1013
			$group->addClass('ecnames');
1014
			$group->add(new Form_Select(
1015
				'ecname',
1016
				null,
1017
				$pconfig['ecname'] ? $pconfig['ecname'] : 'prime256v1',
1018
				$openssl_ecnames
1019
			))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
1020
			$section->add($group);
1021

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

    
1031
			$section->addInput(new Form_Input(
1032
				'lifetime',
1033
				'Lifetime',
1034
				'number',
1035
				$pconfig['lifetime']
1036
			));
1037
		}
1038

    
1039
		$form->add($section);
1040
	}
1041

    
1042
endif;
1043
// ==== Paste a key for the new user
1044
$section = new Form_Section('Keys');
1045

    
1046
$section->addInput(new Form_Checkbox(
1047
	'showkey',
1048
	'Authorized keys',
1049
	'Click to paste an authorized key',
1050
	false
1051
));
1052

    
1053
$section->addInput(new Form_Textarea(
1054
	'authorizedkeys',
1055
	'Authorized SSH Keys',
1056
	$pconfig['authorizedkeys']
1057
))->setHelp('Enter authorized SSH keys for this user');
1058

    
1059
$section->addInput(new Form_Input(
1060
	'ipsecpsk',
1061
	'IPsec Pre-Shared Key',
1062
	'text',
1063
	$pconfig['ipsecpsk']
1064
));
1065

    
1066
$form->add($section);
1067

    
1068
print $form;
1069

    
1070
$csswarning = sprintf(gettext("%sUser-created themes are unsupported, use at your own risk."), "<br />");
1071
?>
1072
<script type="text/javascript">
1073
//<![CDATA[
1074
events.push(function() {
1075

    
1076
	function setcustomoptions() {
1077
		var adv = $('#customsettings').prop('checked');
1078

    
1079
		hideInput('webguicss', !adv);
1080
		hideInput('webguifixedmenu', !adv);
1081
		hideInput('webguihostnamemenu', !adv);
1082
		hideInput('dashboardcolumns', !adv);
1083
		hideCheckbox('interfacessort', !adv);
1084
		hideCheckbox('dashboardavailablewidgetspanel', !adv);
1085
		hideCheckbox('systemlogsfilterpanel', !adv);
1086
		hideCheckbox('systemlogsmanagelogpanel', !adv);
1087
		hideCheckbox('statusmonitoringsettingspanel', !adv);
1088
		hideCheckbox('webguileftcolumnhyper', !adv);
1089
		hideCheckbox('disablealiaspopupdetail', !adv);
1090
		hideCheckbox('pagenamefirst', !adv);
1091
	}
1092

    
1093
	// Handle displaying a warning message if a user-created theme is selected.
1094
	function setThemeWarning() {
1095
		if ($('#webguicss').val().startsWith("pfSense")) {
1096
			$('#csstxt').html("").addClass("text-default");
1097
		} else {
1098
			$('#csstxt').html("<?=$csswarning?>").addClass("text-danger");
1099
		}
1100
	}
1101

    
1102
	function change_keytype() {
1103
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1104
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1105
	}
1106

    
1107
	$('#webguicss').change(function() {
1108
		setThemeWarning();
1109
	});
1110

    
1111
	setThemeWarning();
1112

    
1113
	// On click . .
1114
	$('#customsettings').click(function () {
1115
		setcustomoptions();
1116
	});
1117

    
1118
	$("#movetodisabled").click(function() {
1119
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
1120
	});
1121

    
1122
	$("#movetoenabled").click(function() {
1123
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
1124
	});
1125

    
1126
	$("#showcert").click(function() {
1127
		hideClass('cert-options', !this.checked);
1128
	});
1129

    
1130
	$("#showkey").click(function() {
1131
		hideInput('authorizedkeys', false);
1132
		hideCheckbox('showkey', true);
1133
	});
1134

    
1135
	$('[id^=delcert]').click(function(event) {
1136
		if (confirm(event.target.title)) {
1137
			$('#certid').val(event.target.id.match(/\d+$/)[0]);
1138
			$('#userid').val('<?=$id;?>');
1139
			$('#act').val('delcert');
1140
			$('form').submit();
1141
		}
1142
	});
1143

    
1144
	$('[id^=delprivid]').click(function(event) {
1145
		if (confirm(event.target.title)) {
1146
			$('#privid').val(event.target.id.match(/\d+$/)[0]);
1147
			$('#userid').val('<?=$id;?>');
1148
			$('#act').val('delprivid');
1149
			$('form').submit();
1150
		}
1151
	});
1152

    
1153
	$('#expires').datepicker();
1154

    
1155
	$('#keytype').change(function () {
1156
		change_keytype();
1157
	});
1158

    
1159
	// ---------- On initial page load ------------------------------------------------------------
1160

    
1161
	hideClass('cert-options', true);
1162
	//hideInput('authorizedkeys', true);
1163
	hideCheckbox('showkey', true);
1164
	setcustomoptions();
1165
	change_keytype();
1166

    
1167
	// On submit mark all the user's groups as "selected"
1168
	$('form').submit(function() {
1169
		AllServers($('[name="groups[]"] option'), true);
1170
	});
1171

    
1172
});
1173
//]]>
1174
</script>
1175
<?php
1176
include('foot.inc');
1177
?>
(208-208/228)