Project

General

Profile

Download (16.2 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-2023 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
init_config_arr(array('system', 'group'));
45
$a_group = &$config['system']['group'];
46

    
47
unset($id);
48
$id = $_REQUEST['groupid'];
49
$act = (isset($_REQUEST['act']) ? $_REQUEST['act'] : '');
50

    
51
$dup = null;
52

    
53
if ($act == 'dup') {
54
	$dup = $id;
55
	$act = 'edit';
56
}
57

    
58
function cpusercmp($a, $b) {
59
	return strcasecmp($a['name'], $b['name']);
60
}
61

    
62
function admin_groups_sort() {
63
	global $a_group;
64

    
65
	if (!is_array($a_group)) {
66
		return;
67
	}
68

    
69
	usort($a_group, "cpusercmp");
70
}
71

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

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

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

    
88
	if (!isset($id) || !isset($_REQUEST['groupname']) ||
89
	    !isset($a_group[$id]) ||
90
	    ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
91
		pfSenseHeader("system_groupmanager.php");
92
		exit;
93
	}
94

    
95
	local_group_del($a_group[$id]);
96
	$groupdeleted = $a_group[$id]['name'];
97
	unset($a_group[$id]);
98
	/*
99
	 * Reindex the array to avoid operating on an incorrect index
100
	 * https://redmine.pfsense.org/issues/7733
101
	 */
102
	$a_group = array_values($a_group);
103

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

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

    
112
	if (!isset($id) || !isset($a_group[$id])) {
113
		pfSenseHeader("system_groupmanager.php");
114
		exit;
115
	}
116

    
117
	$privdeleted =
118
	    $priv_list[$a_group[$id]['priv'][$_REQUEST['privid']]]['name'];
119
	unset($a_group[$id]['priv'][$_REQUEST['privid']]);
120

    
121
	if (is_array($a_group[$id]['member'])) {
122
		foreach ($a_group[$id]['member'] as $uid) {
123
			$user = getUserEntryByUID($uid);
124
			if ($user) {
125
				local_user_set($user);
126
			}
127
		}
128
	}
129

    
130
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from group %s"),
131
	    $privdeleted, $a_group[$id]['name']);
132
	write_config($savemsg);
133
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
134

    
135
	$act = "edit";
136
}
137

    
138
if ($act == "edit") {
139
	if (isset($id) && isset($a_group[$id])) {
140
		if ($dup === null) {
141
			$pconfig['name'] = $a_group[$id]['name'];
142
			$pconfig['gid'] = $a_group[$id]['gid'];
143
			$pconfig['gtype'] = empty($a_group[$id]['scope'])
144
			    ? "local" : $a_group[$id]['scope'];
145
		} else {
146
			$pconfig['gtype'] = ($a_group[$id]['scope'] == 'system')
147
			    ? "local" : $a_group[$id]['scope'];
148
		}
149
		$pconfig['priv'] = $a_group[$id]['priv'];
150
		$pconfig['description'] = $a_group[$id]['description'];
151
		$pconfig['members'] = $a_group[$id]['member'];
152
	}
153
}
154

    
155
if (isset($_POST['dellall_x']) && !$read_only) {
156

    
157
	$del_groups = $_POST['delete_check'];
158
	$deleted_groups = array();
159

    
160
	if (!empty($del_groups)) {
161
		foreach ($del_groups as $groupid) {
162
			if (isset($a_group[$groupid]) &&
163
			    $a_group[$groupid]['scope'] != "system") {
164
				$deleted_groups[] = $a_group[$groupid]['name'];
165
				local_group_del($a_group[$groupid]);
166
				unset($a_group[$groupid]);
167
			}
168
		}
169

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

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

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

    
192
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
193

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

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

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

    
234
	if (!$input_errors) {
235
		$group = array();
236
		if (isset($id) && $a_group[$id]) {
237
			$group = $a_group[$id];
238
		}
239

    
240
		$group['name'] = $_POST['groupname'];
241
		$group['description'] = $_POST['description'];
242
		$group['scope'] = $_POST['gtype'];
243

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

    
250
		if (isset($id) && $a_group[$id]) {
251
			$a_group[$id] = $group;
252
		} else {
253
			$group['gid'] = $config['system']['nextgid']++;
254
			if ($_POST['dup']) {
255
				$group['priv'] = $a_group[$_POST['dup']]['priv'];
256
			}
257
			$a_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
			init_config_arr(array('system', 'user'));
270
			$a_user = &$config['system']['user'];
271
			foreach ($a_user as & $user) {
272
				if (in_array($user['uid'], $group['member'])) {
273
					local_user_set($user);
274
				}
275
			}
276
		}
277

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

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

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

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

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

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

    
310
	$user_has_root_priv = false;
311

    
312
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
313
		$privhtml .=		'<tr>';
314
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
315
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
316
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
317
			$privhtml .=			' ' . gettext('(admin privilege)');
318
			$user_has_root_priv = true;
319
		}
320
		$privhtml .=			'</td>';
321
		if (!$read_only && ($dup === null)) {
322
			$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>';
323
		}
324
		$privhtml .=		'</tr>';
325

    
326
	}
327

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

    
337
	}
338

    
339
	$privhtml .=		'</tbody>';
340
	$privhtml .=	'</table>';
341
	$privhtml .= '</div>';
342

    
343
	$privhtml .= '<nav class="action-buttons">';
344
	if (!$read_only && ($dup === null)) {
345
		$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>';
346
	}
347
	$privhtml .= '</nav>';
348

    
349
	return($privhtml);
350
}
351

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

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

    
360
include("head.inc");
361

    
362
if ($input_errors) {
363
	print_input_errors($input_errors);
364
}
365

    
366
if ($savemsg) {
367
	print_info_box($savemsg, 'success');
368
}
369

    
370
$tab_array = array();
371
if (!isAllowedPage("system_usermanager.php")) {
372
	$tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
373
} else {
374
	$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
375
}
376
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
377
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
378
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
379
display_top_tabs($tab_array);
380

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

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

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

    
465
if (isset($id) && $a_group[$id]) {
466
	$form->addGlobal(new Form_Input(
467
		'id',
468
		null,
469
		'hidden',
470
		$id
471
	));
472

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
584
}
585

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

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

    
594

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

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

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

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

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