Project

General

Profile

Download (17 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_groupmanager.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2005 Paul Taylor <paultaylor@winn-dixie.com>
10
 * Copyright (c) 2008 Shrew Soft Inc
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-groupmanager
32
##|*NAME=System: Group Manager
33
##|*DESCR=Allow access to the 'System: Group Manager' page.
34
##|*WARN=standard-warning-root
35
##|*MATCH=system_groupmanager.php*
36
##|-PRIV
37

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

    
41
$logging_level = LOG_WARNING;
42
$logging_prefix = gettext("Local User Database");
43

    
44
$id = is_numericint($_REQUEST['groupid']) ? $_REQUEST['groupid'] : null;
45
$act = (isset($_REQUEST['act']) ? $_REQUEST['act'] : '');
46

    
47
$dup = null;
48

    
49
if ($act == 'dup') {
50
	$dup = $id;
51
	$act = 'edit';
52
}
53

    
54
function cpusercmp($a, $b) {
55
	return strcasecmp($a['name'], $b['name']);
56
}
57

    
58
function admin_groups_sort() {
59
	$group_config = config_get_path('system/group');
60

    
61
	if (!is_array($group_config)) {
62
		return;
63
	}
64

    
65
	usort($group_config, "cpusercmp");
66
	config_set_path("system/group", $group_config);
67
}
68

    
69
/*
70
 * Check user privileges to test if the user is allowed to make changes.
71
 * Otherwise users can end up in an inconsistent state where some changes are
72
 * performed and others denied. See https://redmine.pfsense.org/issues/9259
73
 */
74
phpsession_begin();
75
$guiuser = getUserEntry($_SESSION['Username']);
76
$guiuser = $guiuser['item'];
77
$read_only = (is_array($guiuser) && userHasPrivilege($guiuser, "user-config-readonly"));
78
phpsession_end();
79

    
80
if (!empty($_POST) && $read_only) {
81
	$input_errors = array(gettext("Insufficient privileges to make the requested change (read only)."));
82
}
83

    
84
if (($_POST['act'] == "delgroup") && !$read_only) {
85

    
86
	if (!isset($id) || !isset($_REQUEST['groupname']) ||
87
	    (config_get_path("system/group/{$id}") === null) ||
88
	    ($_REQUEST['groupname'] != config_get_path("system/group/{$id}/name"))) {
89
		pfSenseHeader("system_groupmanager.php");
90
		exit;
91
	}
92

    
93
	local_group_del(config_get_path("system/group/{$id}"));
94
	$groupdeleted = config_get_path("system/group/{$id}/name");
95
	config_del_path("system/group/{$id}");
96
	/*
97
	 * Reindex the array to avoid operating on an incorrect index
98
	 * https://redmine.pfsense.org/issues/7733
99
	 */
100
	config_set_path("system/group", array_values(config_get_path('system/group', [])));
101

    
102
	$savemsg = sprintf(gettext("Successfully deleted group: %s"),
103
	    $groupdeleted);
104
	write_config($savemsg);
105
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
106
}
107

    
108
if (($_POST['act'] == "delpriv") && !$read_only && ($dup === null)) {
109

    
110
	if (!isset($id) || (config_get_path("system/group/{$id}") === null)) {
111
		pfSenseHeader("system_groupmanager.php");
112
		exit;
113
	}
114

    
115
	$privdeleted = array_get_path($priv_list, (config_get_path("system/group/{$id}/priv/{$_REQUEST['privid']}") . "/name"));
116
	config_del_path("system/group/{$id}/priv/{$_REQUEST['privid']}");
117

    
118
	foreach (config_get_path("system/group/{$id}/member", []) as $uid) {
119
		$user = getUserEntryByUID($uid);
120
		$user = $user['item'];
121
		if ($user) {
122
			local_user_set($user);
123
		}
124
	}
125

    
126
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from group %s"),
127
	    $privdeleted, config_get_path("system/group/{$id}/name"));
128
	write_config($savemsg);
129
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
130

    
131
	$act = "edit";
132
}
133

    
134
if ($act == "edit") {
135
	if (isset($id)) {
136
		$this_group = config_get_path("system/group/{$id}");
137
		if ($dup === null) {
138
			$pconfig['name'] = $this_group['name'];
139
			$pconfig['gid'] = $this_group['gid'];
140
			$pconfig['gtype'] = empty($this_group['scope'])
141
			    ? "local" : $this_group['scope'];
142
		} else {
143
			$pconfig['gtype'] = ($this_group['scope'] == 'system')
144
			    ? "local" : $this_group['scope'];
145
		}
146
		$pconfig['priv'] = $this_group['priv'];
147
		$pconfig['description'] = $this_group['description'];
148
		$pconfig['members'] = $this_group['member'];
149
	}
150
}
151

    
152
if (isset($_POST['dellall_x']) && !$read_only) {
153

    
154
	$del_groups = $_POST['delete_check'];
155
	$deleted_groups = array();
156

    
157
	if (!empty($del_groups)) {
158
		foreach ($del_groups as $groupid) {
159
			$this_group = config_get_path("system/group/{$groupid}");
160
			if (isset($this_group) &&
161
			    $this_group['scope'] != "system") {
162
				$deleted_groups[] = $this_group['name'];
163
				local_group_del($this_group);
164
				config_del_path("system/group/{$groupid}");
165
			}
166
		}
167

    
168
		$savemsg = sprintf(gettext("Successfully deleted %s: %s"),
169
		    (count($deleted_groups) == 1)
170
		    ? gettext("group") : gettext("groups"),
171
		    implode(', ', $deleted_groups));
172
		/*
173
		 * Reindex the array to avoid operating on an incorrect index
174
		 * https://redmine.pfsense.org/issues/7733
175
		 */
176
		config_set_path("system/group", array_values(config_get_path('system/group', [])));
177
		write_config($savemsg);
178
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
179
	}
180
}
181

    
182
if (isset($_POST['save']) && !$read_only) {
183
	unset($input_errors);
184
	$pconfig = $_POST;
185

    
186
	/* input validation */
187
	$reqdfields = explode(" ", "groupname");
188
	$reqdfieldsn = array(gettext("Group Name"));
189

    
190
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
191

    
192
	if ($_POST['gtype'] != "remote") {
193
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
194
			$input_errors[] = sprintf(gettext(
195
			    "The (%s) group name contains invalid characters."),
196
			    $_POST['gtype']);
197
		}
198
		if (strlen($_POST['groupname']) > 16) {
199
			$input_errors[] = gettext(
200
			    "The group name is longer than 16 characters.");
201
		}
202
	} else {
203
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
204
			$input_errors[] = sprintf(gettext(
205
			    "The (%s) group name contains invalid characters."),
206
			    $_POST['gtype']);
207
		}
208
	}
209

    
210
	/* Check the POSTed members to ensure they are valid and exist */
211
	if (is_array($_POST['members'])) {
212
		foreach ($_POST['members'] as $newmember) {
213
			if (!is_numeric($newmember) ||
214
			    empty(getUserEntryByUID($newmember))) {
215
				$input_errors[] = gettext("One or more " .
216
				    "invalid group members was submitted.");
217
			}
218
		}
219
	}
220

    
221
	if (!$input_errors && !(isset($id) && config_get_path("system/group/{$id}"))) {
222
		/* make sure there are no dupes */
223
		foreach (config_get_path('system/group', []) as $group) {
224
			if ($group['name'] == $_POST['groupname']) {
225
				$input_errors[] = gettext("Another entry " .
226
				    "with the same group name already exists.");
227
				break;
228
			}
229
		}
230
	}
231

    
232
	if (!$input_errors) {
233
		$group = array();
234
		if (isset($id) && config_get_path("system/group/{$id}")) {
235
			$group = config_get_path("system/group/{$id}");
236
		}
237

    
238
		$group['name'] = $_POST['groupname'];
239
		$group['description'] = $_POST['description'];
240
		$group['scope'] = $_POST['gtype'];
241

    
242
		if (empty($_POST['members'])) {
243
			unset($group['member']);
244
		} else if ($group['gid'] != 1998) { // all group
245
			$group['member'] = $_POST['members'];
246
		}
247

    
248
		if (isset($id) && config_get_path("system/group/{$id}")) {
249
			config_set_path("system/group/{$id}", $group);
250
		} else {
251
			$nextgid = config_get_path('system/nextgid');
252
			$group['gid'] = $nextgid++;
253
			config_set_path('system/nextgid', $nextgid);
254
			if ($_POST['dup']) {
255
				$group['priv'] = config_get_path("system/group/{$_POST['dup']}/priv");
256
			}
257
			config_set_path('system/group/', $group);
258
		}
259

    
260
		admin_groups_sort();
261

    
262
		local_group_set($group);
263

    
264
		/*
265
		 * Refresh users in this group since their privileges may have
266
		 * changed.
267
		 */
268
		if (is_array($group['member'])) {
269
			foreach (config_get_path('system/user', []) as $idx => $user) {
270
				if (in_array($user['uid'], $group['member'])) {
271
					local_user_set($user);
272
					config_set_path("system/user/{$idx}", $user);
273
				}
274
			}
275
		}
276

    
277
		/* Sort it alphabetically */
278
		$group_config = config_get_path('system/group', []);
279
		usort($group_config, function($a, $b) {
280
			return strcmp($a['name'], $b['name']);
281
		});
282
		config_set_path('system/group', $group_config);
283

    
284
		$savemsg = sprintf(gettext("Successfully %s group %s"),
285
		    (strlen($id) > 0) ? gettext("edited") : gettext("created"),
286
		    $group['name']);
287
		write_config($savemsg);
288
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
289

    
290
		header("Location: system_groupmanager.php");
291
		exit;
292
	}
293

    
294
	$pconfig['name'] = $_POST['groupname'];
295
}
296

    
297
function build_priv_table() {
298
	global $id, $read_only, $dup;
299

    
300
	$privhtml = '<div class="table-responsive">';
301
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
302
	$privhtml .=		'<thead>';
303
	$privhtml .=			'<tr>';
304
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
305
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
306
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
307
	$privhtml .=			'</tr>';
308
	$privhtml .=		'</thead>';
309
	$privhtml .=		'<tbody>';
310

    
311
	$user_has_root_priv = false;
312

    
313
	if (isset($id)) {
314
		foreach (get_user_privdesc(config_get_path("system/group/{$id}")) as $i => $priv) {
315
			$privhtml .=		'<tr>';
316
			$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
317
			$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
318
			if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
319
				$privhtml .=			' ' . gettext('(admin privilege)');
320
				$user_has_root_priv = true;
321
			}
322
			$privhtml .=			'</td>';
323
			if (!$read_only && ($dup === null)) {
324
				$privhtml .=			'<td><a class="fa-solid fa-trash-can" title="' . gettext('Delete Privilege') . '"	href="system_groupmanager.php?act=delpriv&amp;groupid=' . $id . '&amp;privid=' . $i . '" usepost></a></td>';
325
			}
326
			$privhtml .=		'</tr>';
327
		}
328
	}
329

    
330
	if ($user_has_root_priv) {
331
		$privhtml .=		'<tr>';
332
		$privhtml .=			'<td colspan="2">';
333
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
334
		$privhtml .=			'</td>';
335
		$privhtml .=			'<td>';
336
		$privhtml .=			'</td>';
337
		$privhtml .=		'</tr>';
338

    
339
	}
340

    
341
	$privhtml .=		'</tbody>';
342
	$privhtml .=	'</table>';
343
	$privhtml .= '</div>';
344

    
345
	$privhtml .= '<nav class="action-buttons">';
346
	if (!$read_only && ($dup === null)) {
347
		$privhtml .=	'<a href="system_groupmanager_addprivs.php?groupid=' . $id . '" class="btn btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>' . gettext("Add") . '</a>';
348
	}
349
	$privhtml .= '</nav>';
350

    
351
	return($privhtml);
352
}
353

    
354
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
355
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
356

    
357
if ($act == "new" || $act == "edit") {
358
	$pgtitle[] = gettext('Edit');
359
	$pglinks[] = "@self";
360
}
361

    
362
include("head.inc");
363

    
364
if ($input_errors) {
365
	print_input_errors($input_errors);
366
}
367

    
368
if ($savemsg) {
369
	print_info_box($savemsg, 'success');
370
}
371

    
372
$tab_array = array();
373
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
374
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
375
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
376
$tab_array[] = array(gettext("Change Password"), false, "system_usermanager_passwordmg.php");
377
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
378
display_top_tabs($tab_array);
379

    
380
if (!($act == "new" || $act == "edit")) {
381
?>
382
<div class="panel panel-default">
383
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
384
	<div class="panel-body">
385
		<div class="table-responsive">
386
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
387
				<thead>
388
					<tr>
389
						<th><?=gettext("Group name")?></th>
390
						<th><?=gettext("Description")?></th>
391
						<th><?=gettext("Member Count")?></th>
392
						<th><?=gettext("Actions")?></th>
393
					</tr>
394
				</thead>
395
				<tbody>
396
<?php
397
	foreach (config_get_path('system/group', []) as $i => $group):
398
		if ($group["name"] == "all") {
399
			$groupcount = count(config_get_path('system/user', []));
400
		} elseif (is_array($group['member'])) {
401
			$groupcount = count($group['member']);
402
		} else {
403
			$groupcount = 0;
404
		}
405
?>
406
					<tr>
407
						<td>
408
							<?=htmlspecialchars($group['name'])?>
409
						</td>
410
						<td>
411
							<?=htmlspecialchars($group['description'])?>
412
						</td>
413
						<td>
414
							<?=$groupcount?>
415
						</td>
416
						<td>
417
							<a class="fa-solid fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
418
							<a class="fa-regular fa-clone" title="<?=gettext("Copy group"); ?>" href="?act=dup&amp;groupid=<?=$i?>"></a>
419
							<?php if (($group['scope'] != "system") && !$read_only): ?>
420
								<a class="fa-solid fa-trash-can"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>" usepost></a>
421
							<?php endif;?>
422
						</td>
423
					</tr>
424
<?php
425
	endforeach;
426
?>
427
				</tbody>
428
			</table>
429
		</div>
430
	</div>
431
</div>
432

    
433
<nav class="action-buttons">
434
	<?php if (!$read_only): ?>
435
	<a href="?act=new" class="btn btn-success btn-sm">
436
		<i class="fa-solid fa-plus icon-embed-btn"></i>
437
		<?=gettext("Add")?>
438
	</a>
439
	<?php endif; ?>
440
</nav>
441
<?php
442
	include('foot.inc');
443
	exit;
444
}
445

    
446
$form = new Form;
447
$form->setAction('system_groupmanager.php?act=edit');
448
if ($dup === null) {
449
	$form->addGlobal(new Form_Input(
450
		'groupid',
451
		null,
452
		'hidden',
453
		$id
454
	));
455
} else {
456
	$form->addGlobal(new Form_Input(
457
		'dup',
458
		null,
459
		'hidden',
460
		$dup
461
	));
462
}
463

    
464
if (isset($id) && config_get_path("system/group/{$id}")) {
465
	$form->addGlobal(new Form_Input(
466
		'id',
467
		null,
468
		'hidden',
469
		$id
470
	));
471

    
472
	$form->addGlobal(new Form_Input(
473
		'gid',
474
		null,
475
		'hidden',
476
		$pconfig['gid']
477
	));
478
}
479

    
480
$section = new Form_Section('Group Properties');
481

    
482
$section->addInput($input = new Form_Input(
483
	'groupname',
484
	'*Group name',
485
	'text',
486
	$pconfig['name']
487
));
488

    
489
if ($pconfig['gtype'] == "system") {
490
	$input->setReadonly();
491

    
492
	$section->addInput(new Form_Input(
493
		'gtype',
494
		'*Scope',
495
		'text',
496
		$pconfig['gtype']
497
	))->setReadonly();
498
} else {
499
	$section->addInput(new Form_Select(
500
		'gtype',
501
		'*Scope',
502
		$pconfig['gtype'],
503
		["local" => gettext("Local"), "remote" => gettext("Remote")]
504
	))->setHelp("<span class=\"text-danger\">Warning: Changing this " .
505
	    "setting may affect the local groups file, in which case a " .
506
	    "reboot may be required for the changes to take effect.</span>");
507
}
508

    
509
$section->addInput(new Form_Input(
510
	'description',
511
	'Description',
512
	'text',
513
	$pconfig['description']
514
))->setHelp('Group description, for administrative information only');
515

    
516
$form->add($section);
517

    
518
/* all users group */
519
if ($pconfig['gid'] != 1998) {
520
	/* Group membership */
521
	$group = new Form_Group('Group membership');
522

    
523
	/*
524
	 * Make a list of all the groups configured on the system, and a list of
525
	 * those which this user is a member of
526
	 */
527
	$systemGroups = array();
528
	$usersGroups = array();
529

    
530
	foreach (config_get_path('system/user', []) as $user) {
531
		if (is_array($pconfig['members']) && in_array($user['uid'],
532
		    $pconfig['members'])) {
533
			/* Add it to the user's list */
534
			$usersGroups[ $user['uid'] ] = $user['name'];
535
		} else {
536
			/* Add it to the 'not a member of' list */
537
			$systemGroups[ $user['uid'] ] = $user['name'];
538
		}
539
	}
540

    
541
	$group->add(new Form_Select(
542
		'notmembers',
543
		null,
544
		array_combine((array)$pconfig['groups'],
545
		    (array)$pconfig['groups']),
546
		$systemGroups,
547
		true
548
	))->setHelp('Not members');
549

    
550
	$group->add(new Form_Select(
551
		'members',
552
		null,
553
		array_combine((array)$pconfig['groups'],
554
		    (array)$pconfig['groups']),
555
		$usersGroups,
556
		true
557
	))->setHelp('Members');
558

    
559
	$section->add($group);
560

    
561
	$group = new Form_Group('');
562

    
563
	$group->add(new Form_Button(
564
		'movetoenabled',
565
		'Move to "Members"',
566
		null,
567
		'fa-solid fa-angle-double-right'
568
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
569
	    'btn-info btn-sm');
570

    
571
	$group->add(new Form_Button(
572
		'movetodisabled',
573
		'Move to "Not members',
574
		null,
575
		'fa-solid fa-angle-double-left'
576
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
577
	    'btn-info btn-sm');
578

    
579
	$group->setHelp(
580
	    'Hold down CTRL (PC)/COMMAND (Mac) key to select multiple items.');
581
	$section->add($group);
582

    
583
}
584

    
585
if (isset($pconfig['gid']) || ($dup !== null)) {
586
	$section = new Form_Section('Assigned Privileges');
587

    
588
	$section->addInput(new Form_StaticText(
589
		null,
590
		build_priv_table()
591
	));
592

    
593

    
594
	$form->add($section);
595
}
596

    
597
print $form;
598
?>
599
<script type="text/javascript">
600
//<![CDATA[
601
events.push(function() {
602

    
603
	// On click . .
604
	$("#movetodisabled").click(function() {
605
		moveOptions($('[name="members[]"] option'),
606
		    $('[name="notmembers[]"]'));
607
	});
608

    
609
	$("#movetoenabled").click(function() {
610
		moveOptions($('[name="notmembers[]"] option'),
611
		    $('[name="members[]"]'));
612
	});
613

    
614
	// On submit mark all the user's groups as "selected"
615
	$('form').submit(function() {
616
		AllServers($('[name="members[]"] option'), true);
617
	});
618
});
619
//]]>
620
</script>
621
<?php
622
include('foot.inc');
(204-204/232)