Project

General

Profile

Download (15.9 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-2021 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
if ($act == 'dup') {
52
	$dup = true;
53
	$act = 'edit';
54
}
55

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

    
60
function admin_groups_sort() {
61
	global $a_group;
62

    
63
	if (!is_array($a_group)) {
64
		return;
65
	}
66

    
67
	usort($a_group, "cpusercmp");
68
}
69

    
70
/*
71
 * Check user privileges to test if the user is allowed to make changes.
72
 * Otherwise users can end up in an inconsistent state where some changes are
73
 * performed and others denied. See https://redmine.pfsense.org/issues/9259
74
 */
75
phpsession_begin();
76
$guiuser = getUserEntry($_SESSION['Username']);
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
	    !isset($a_group[$id]) ||
88
	    ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
89
		pfSenseHeader("system_groupmanager.php");
90
		exit;
91
	}
92

    
93
	local_group_del($a_group[$id]);
94
	$groupdeleted = $a_group[$id]['name'];
95
	unset($a_group[$id]);
96
	/*
97
	 * Reindex the array to avoid operating on an incorrect index
98
	 * https://redmine.pfsense.org/issues/7733
99
	 */
100
	$a_group = array_values($a_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) {
109

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

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

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

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

    
133
	$act = "edit";
134
}
135

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

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

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

    
158
	if (!empty($del_groups)) {
159
		foreach ($del_groups as $groupid) {
160
			if (isset($a_group[$groupid]) &&
161
			    $a_group[$groupid]['scope'] != "system") {
162
				$deleted_groups[] = $a_group[$groupid]['name'];
163
				local_group_del($a_group[$groupid]);
164
				unset($a_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
		$a_group = array_values($a_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
	if ($dup) {
187
		unset($id);
188
	}
189

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

    
194
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
195

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

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

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

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

    
242
		$group['name'] = $_POST['groupname'];
243
		$group['description'] = $_POST['description'];
244
		$group['scope'] = $_POST['gtype'];
245

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

    
252
		if (isset($id) && $a_group[$id]) {
253
			$a_group[$id] = $group;
254
		} else {
255
			$group['gid'] = $config['system']['nextgid']++;
256
			$a_group[] = $group;
257
		}
258

    
259
		admin_groups_sort();
260

    
261
		local_group_set($group);
262

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

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

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

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

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

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

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

    
309
	$user_has_root_priv = false;
310

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

    
325
	}
326

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

    
336
	}
337

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

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

    
348
	return($privhtml);
349
}
350

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

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

    
359
include("head.inc");
360

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

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

    
369
$tab_array = array();
370
if (!isAllowedPage("system_usermanager.php")) {
371
	$tab_array[] = array(gettext("User Password"), false, "system_usermanager_passwordmg.php");
372
} else {
373
	$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
374
}
375
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
376
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.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 ($a_group as $i => $group):
398
		if ($group["name"] == "all") {
399
			$groupcount = count($config['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 fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
418
							<a class="fa 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 fa-trash"	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 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
$form->addGlobal(new Form_Input(
449
	'groupid',
450
	null,
451
	'hidden',
452
	$id
453
));
454

    
455
if (isset($id) && $a_group[$id]) {
456
	$form->addGlobal(new Form_Input(
457
		'id',
458
		null,
459
		'hidden',
460
		$id
461
	));
462

    
463
	$form->addGlobal(new Form_Input(
464
		'gid',
465
		null,
466
		'hidden',
467
		$pconfig['gid']
468
	));
469
}
470

    
471
$section = new Form_Section('Group Properties');
472

    
473
$section->addInput($input = new Form_Input(
474
	'groupname',
475
	'*Group name',
476
	'text',
477
	$pconfig['name']
478
));
479

    
480
if ($pconfig['gtype'] == "system") {
481
	$input->setReadonly();
482

    
483
	$section->addInput(new Form_Input(
484
		'gtype',
485
		'*Scope',
486
		'text',
487
		$pconfig['gtype']
488
	))->setReadonly();
489
} else {
490
	$section->addInput(new Form_Select(
491
		'gtype',
492
		'*Scope',
493
		$pconfig['gtype'],
494
		["local" => gettext("Local"), "remote" => gettext("Remote")]
495
	))->setHelp("<span class=\"text-danger\">Warning: Changing this " .
496
	    "setting may affect the local groups file, in which case a " .
497
	    "reboot may be required for the changes to take effect.</span>");
498
}
499

    
500
$section->addInput(new Form_Input(
501
	'description',
502
	'Description',
503
	'text',
504
	$pconfig['description']
505
))->setHelp('Group description, for administrative information only');
506

    
507
$form->add($section);
508

    
509
/* all users group */
510
if ($pconfig['gid'] != 1998) {
511
	/* Group membership */
512
	$group = new Form_Group('Group membership');
513

    
514
	/*
515
	 * Make a list of all the groups configured on the system, and a list of
516
	 * those which this user is a member of
517
	 */
518
	$systemGroups = array();
519
	$usersGroups = array();
520

    
521
	foreach ($config['system']['user'] as $user) {
522
		if (is_array($pconfig['members']) && in_array($user['uid'],
523
		    $pconfig['members'])) {
524
			/* Add it to the user's list */
525
			$usersGroups[ $user['uid'] ] = $user['name'];
526
		} else {
527
			/* Add it to the 'not a member of' list */
528
			$systemGroups[ $user['uid'] ] = $user['name'];
529
		}
530
	}
531

    
532
	$group->add(new Form_Select(
533
		'notmembers',
534
		null,
535
		array_combine((array)$pconfig['groups'],
536
		    (array)$pconfig['groups']),
537
		$systemGroups,
538
		true
539
	))->setHelp('Not members');
540

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

    
550
	$section->add($group);
551

    
552
	$group = new Form_Group('');
553

    
554
	$group->add(new Form_Button(
555
		'movetoenabled',
556
		'Move to "Members"',
557
		null,
558
		'fa-angle-double-right'
559
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
560
	    'btn-info btn-sm');
561

    
562
	$group->add(new Form_Button(
563
		'movetodisabled',
564
		'Move to "Not members',
565
		null,
566
		'fa-angle-double-left'
567
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
568
	    'btn-info btn-sm');
569

    
570
	$group->setHelp(
571
	    'Hold down CTRL (PC)/COMMAND (Mac) key to select multiple items.');
572
	$section->add($group);
573

    
574
}
575

    
576
if (isset($pconfig['gid']) || $dup) {
577
	$section = new Form_Section('Assigned Privileges');
578

    
579
	$section->addInput(new Form_StaticText(
580
		null,
581
		build_priv_table()
582
	));
583

    
584

    
585
	$form->add($section);
586
}
587

    
588
print $form;
589
?>
590
<script type="text/javascript">
591
//<![CDATA[
592
events.push(function() {
593

    
594
	// On click . .
595
	$("#movetodisabled").click(function() {
596
		moveOptions($('[name="members[]"] option'),
597
		    $('[name="notmembers[]"]'));
598
	});
599

    
600
	$("#movetoenabled").click(function() {
601
		moveOptions($('[name="notmembers[]"] option'),
602
		    $('[name="members[]"]'));
603
	});
604

    
605
	// On submit mark all the user's groups as "selected"
606
	$('form').submit(function() {
607
		AllServers($('[name="members[]"] option'), true);
608
	});
609
});
610
//]]>
611
</script>
612
<?php
613
include('foot.inc');
(200-200/227)