Project

General

Profile

Download (33.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-2021 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, $_POST['type'],
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
		pfSenseHeader("system_usermanager.php");
500
	}
501
}
502

    
503
function build_priv_table() {
504
	global $a_user, $id, $read_only;
505

    
506
	$privhtml = '<div class="table-responsive">';
507
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
508
	$privhtml .=		'<thead>';
509
	$privhtml .=			'<tr>';
510
	$privhtml .=				'<th>' . gettext('Inherited from') . '</th>';
511
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
512
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
513
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
514
	$privhtml .=			'</tr>';
515
	$privhtml .=		'</thead>';
516
	$privhtml .=		'<tbody>';
517

    
518
	$i = 0;
519
	$user_has_root_priv = false;
520

    
521
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
522
		$group = false;
523
		if ($priv['group']) {
524
			$group = $priv['group'];
525
		}
526

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

    
541
		$privhtml .=			'</td>';
542
		$privhtml .=		'</tr>';
543

    
544
		if (!$group) {
545
			$i++;
546
		}
547
	}
548

    
549
	if ($user_has_root_priv) {
550
		$privhtml .=		'<tr>';
551
		$privhtml .=			'<td colspan="3">';
552
		$privhtml .=				'<b>' . gettext('Security notice: This user effectively has administrator-level access') . '</b>';
553
		$privhtml .=			'</td>';
554
		$privhtml .=			'<td>';
555
		$privhtml .=			'</td>';
556
		$privhtml .=		'</tr>';
557

    
558
	}
559

    
560
	$privhtml .=		'</tbody>';
561
	$privhtml .=	'</table>';
562
	$privhtml .= '</div>';
563

    
564
	$privhtml .= '<nav class="action-buttons">';
565
	if (!$read_only) {
566
		$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>';
567
	}
568
	$privhtml .= '</nav>';
569

    
570
	return($privhtml);
571
}
572

    
573
function build_cert_table() {
574
	global $a_user, $id, $read_only;
575

    
576
	$certhtml = '<div class="table-responsive">';
577
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
578
	$certhtml .=		'<thead>';
579
	$certhtml .=			'<tr>';
580
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
581
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
582
	$certhtml .=				'<th></th>';
583
	$certhtml .=			'</tr>';
584
	$certhtml .=		'</thead>';
585
	$certhtml .=		'<tbody>';
586

    
587
	$a_cert = $a_user[$id]['cert'];
588
	if (is_array($a_cert)) {
589
		$i = 0;
590
		foreach ($a_cert as $certref) {
591
			$cert = lookup_cert($certref);
592
			$ca = lookup_ca($cert['caref']);
593
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
594

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

    
608
	}
609

    
610
	$certhtml .=		'</tbody>';
611
	$certhtml .=	'</table>';
612
	$certhtml .= '</div>';
613

    
614
	$certhtml .= '<nav class="action-buttons">';
615
	if (!$read_only) {
616
		$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>';
617
	}
618
	$certhtml .= '</nav>';
619

    
620
	return($certhtml);
621
}
622

    
623
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
624
$pglinks = array("", "system_usermanager.php", "system_usermanager.php");
625

    
626
if ($act == "new" || $act == "edit" || $input_errors) {
627
	$pgtitle[] = gettext('Edit');
628
	$pglinks[] = "@self";
629
}
630

    
631
include("head.inc");
632

    
633
if ($delete_errors) {
634
	print_input_errors($delete_errors);
635
}
636

    
637
if ($input_errors) {
638
	print_input_errors($input_errors);
639
}
640

    
641
if ($savemsg) {
642
	print_info_box($savemsg, 'success');
643
}
644

    
645
$tab_array = array();
646
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
647
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
648
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
649
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
650
display_top_tabs($tab_array);
651

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

    
708
	<a href="?act=new" class="btn btn-sm btn-success">
709
		<i class="fa fa-plus icon-embed-btn"></i>
710
		<?=gettext("Add")?>
711
	</a>
712

    
713
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
714
		<i class="fa fa-trash icon-embed-btn"></i>
715
		<?=gettext("Delete")?>
716
	</button>
717
	<?php endif; ?>
718

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

    
730
?></div>
731

    
732
<?php
733
	include("foot.inc");
734
	exit;
735
}
736

    
737
$form = new Form;
738

    
739
if ($act == "new" || $act == "edit" || $input_errors):
740

    
741
	$form->addGlobal(new Form_Input(
742
		'act',
743
		null,
744
		'hidden',
745
		''
746
	));
747

    
748
	$form->addGlobal(new Form_Input(
749
		'userid',
750
		null,
751
		'hidden',
752
		isset($id) ? $id:''
753
	));
754

    
755
	$form->addGlobal(new Form_Input(
756
		'privid',
757
		null,
758
		'hidden',
759
		''
760
	));
761

    
762
	$form->addGlobal(new Form_Input(
763
		'certid',
764
		null,
765
		'hidden',
766
		''
767
	));
768

    
769
	$ro = "";
770
	if ($pconfig['utype'] == "system") {
771
		$ro = "readonly";
772
	}
773

    
774
	$section = new Form_Section('User Properties');
775

    
776
	$section->addInput(new Form_StaticText(
777
		'Defined by',
778
		strtoupper($pconfig['utype'])
779
	));
780

    
781
	$form->addGlobal(new Form_Input(
782
		'utype',
783
		null,
784
		'hidden',
785
		$pconfig['utype']
786
	));
787

    
788
	$section->addInput(new Form_Checkbox(
789
		'disabled',
790
		'Disabled',
791
		'This user cannot login',
792
		$pconfig['disabled']
793
	));
794

    
795
	$section->addInput($input = new Form_Input(
796
		'usernamefld',
797
		'*Username',
798
		'text',
799
		$pconfig['usernamefld'],
800
		['autocomplete' => 'new-password']
801
	));
802

    
803
	if ($ro) {
804
		$input->setReadonly();
805
	}
806

    
807
	$form->addGlobal(new Form_Input(
808
		'oldusername',
809
		null,
810
		'hidden',
811
		$pconfig['usernamefld']
812
	));
813

    
814
	if ($act == "edit") {
815
		$pwd_required = "";
816
	} else {
817
		$pwd_required = "*";
818
	}
819

    
820
	$group = new Form_Group($pwd_required . 'Password');
821
	$group->add(new Form_Input(
822
		'passwordfld1',
823
		'Password',
824
		'password',
825
		null,
826
		['autocomplete' => 'new-password']
827
	));
828
	$group->add(new Form_Input(
829
		'passwordfld2',
830
		'Confirm Password',
831
		'password',
832
		null,
833
		['autocomplete' => 'new-password']
834
	));
835

    
836
	$section->add($group);
837

    
838
	$section->addInput($input = new Form_Input(
839
		'descr',
840
		'Full name',
841
		'text',
842
		htmlspecialchars($pconfig['descr'])
843
	))->setHelp('User\'s full name, for administrative information only');
844

    
845
	if ($ro) {
846
		$input->setDisabled();
847
	}
848

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

    
857
	$section->addInput(new Form_Checkbox(
858
		'customsettings',
859
		'Custom Settings',
860
		'Use individual customized GUI options and dashboard layout for this user.',
861
		$pconfig['customsettings']
862
	));
863

    
864
	gen_user_settings_fields($section, $pconfig);
865

    
866
	// ==== Group membership ==================================================
867
	$group = new Form_Group('Group membership');
868

    
869
	// Make a list of all the groups configured on the system, and a list of
870
	// those which this user is a member of
871
	$systemGroups = array();
872
	$usersGroups = array();
873

    
874
	$usergid = [$pconfig['usernamefld']];
875

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

    
886
	$group->add(new Form_Select(
887
		'sysgroups',
888
		null,
889
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
890
		$systemGroups,
891
		true
892
	))->setHelp('Not member of');
893

    
894
	$group->add(new Form_Select(
895
		'groups',
896
		null,
897
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
898
		$usersGroups,
899
		true
900
	))->setHelp('Member of');
901

    
902
	$section->add($group);
903

    
904
	$group = new Form_Group('');
905

    
906
	$group->add(new Form_Button(
907
		'movetoenabled',
908
		'Move to "Member of" list',
909
		null,
910
		'fa-angle-double-right'
911
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
912

    
913
	$group->add(new Form_Button(
914
		'movetodisabled',
915
		'Move to "Not member of" list',
916
		null,
917
		'fa-angle-double-left'
918
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
919

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

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

    
941
	$form->add($section);
942

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

    
948
		$section = new Form_Section('Effective Privileges');
949

    
950
		$section->addInput(new Form_StaticText(
951
			null,
952
			build_priv_table()
953
		));
954

    
955
		$form->add($section);
956

    
957
		// ==== Certificate table section =====================================
958
		$section = new Form_Section('User Certificates');
959

    
960
		$section->addInput(new Form_StaticText(
961
			null,
962
			build_cert_table()
963
		));
964

    
965
		$form->add($section);
966
	}
967

    
968
	// ==== Add user certificate for a new user
969
	if (is_array($config['ca']) && count($config['ca']) > 0) {
970
		$section = new Form_Section('Create Certificate for User');
971
		$section->addClass('cert-options');
972

    
973
		if (!empty($nonPrvCas)) {
974
			$section->addInput(new Form_Input(
975
				'name',
976
				'Descriptive name',
977
				'text',
978
				$pconfig['name']
979
			));
980

    
981
			$section->addInput(new Form_Select(
982
				'caref',
983
				'Certificate authority',
984
				null,
985
				$nonPrvCas
986
			));
987

    
988
			$section->addInput(new Form_Select(
989
				'keytype',
990
				'*Key type',
991
				$pconfig['keytype'],
992
				array_combine($cert_keytypes, $cert_keytypes)
993
			));
994

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

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

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

    
1026
			$section->addInput(new Form_Input(
1027
				'lifetime',
1028
				'Lifetime',
1029
				'number',
1030
				$pconfig['lifetime']
1031
			));
1032
		}
1033

    
1034
		$form->add($section);
1035
	}
1036

    
1037
endif;
1038
// ==== Paste a key for the new user
1039
$section = new Form_Section('Keys');
1040

    
1041
$section->addInput(new Form_Checkbox(
1042
	'showkey',
1043
	'Authorized keys',
1044
	'Click to paste an authorized key',
1045
	false
1046
));
1047

    
1048
$section->addInput(new Form_Textarea(
1049
	'authorizedkeys',
1050
	'Authorized SSH Keys',
1051
	$pconfig['authorizedkeys']
1052
))->setHelp('Enter authorized SSH keys for this user');
1053

    
1054
$section->addInput(new Form_Input(
1055
	'ipsecpsk',
1056
	'IPsec Pre-Shared Key',
1057
	'text',
1058
	$pconfig['ipsecpsk']
1059
));
1060

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

    
1063
print $form;
1064

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

    
1071
	function setcustomoptions() {
1072
		var adv = $('#customsettings').prop('checked');
1073

    
1074
		hideInput('webguicss', !adv);
1075
		hideInput('webguifixedmenu', !adv);
1076
		hideInput('webguihostnamemenu', !adv);
1077
		hideInput('dashboardcolumns', !adv);
1078
		hideCheckbox('interfacessort', !adv);
1079
		hideCheckbox('dashboardavailablewidgetspanel', !adv);
1080
		hideCheckbox('systemlogsfilterpanel', !adv);
1081
		hideCheckbox('systemlogsmanagelogpanel', !adv);
1082
		hideCheckbox('statusmonitoringsettingspanel', !adv);
1083
		hideCheckbox('webguileftcolumnhyper', !adv);
1084
		hideCheckbox('disablealiaspopupdetail', !adv);
1085
		hideCheckbox('pagenamefirst', !adv);
1086
	}
1087

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

    
1097
	function change_keytype() {
1098
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1099
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1100
	}
1101

    
1102
	$('#webguicss').change(function() {
1103
		setThemeWarning();
1104
	});
1105

    
1106
	setThemeWarning();
1107

    
1108
	// On click . .
1109
	$('#customsettings').click(function () {
1110
		setcustomoptions();
1111
	});
1112

    
1113
	$("#movetodisabled").click(function() {
1114
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
1115
	});
1116

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

    
1121
	$("#showcert").click(function() {
1122
		hideClass('cert-options', !this.checked);
1123
	});
1124

    
1125
	$("#showkey").click(function() {
1126
		hideInput('authorizedkeys', false);
1127
		hideCheckbox('showkey', true);
1128
	});
1129

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

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

    
1148
	$('#expires').datepicker();
1149

    
1150
	$('#keytype').change(function () {
1151
		change_keytype();
1152
	});
1153

    
1154
	// ---------- On initial page load ------------------------------------------------------------
1155

    
1156
	hideClass('cert-options', true);
1157
	//hideInput('authorizedkeys', true);
1158
	hideCheckbox('showkey', true);
1159
	setcustomoptions();
1160
	change_keytype();
1161

    
1162
	// On submit mark all the user's groups as "selected"
1163
	$('form').submit(function() {
1164
		AllServers($('[name="groups[]"] option'), true);
1165
	});
1166

    
1167
});
1168
//]]>
1169
</script>
1170
<?php
1171
include('foot.inc');
1172
?>
(207-207/229)