Project

General

Profile

Download (13.3 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-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Paul Taylor <paultaylor@winn-dixie.com>
8
 * Copyright (c) 2008 Shrew Soft Inc
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-system-groupmanager
30
##|*NAME=System: Group Manager
31
##|*DESCR=Allow access to the 'System: Group Manager' page.
32
##|*WARN=standard-warning-root
33
##|*MATCH=system_groupmanager.php*
34
##|-PRIV
35

    
36
require_once("guiconfig.inc");
37
require_once("pfsense-utils.inc");
38

    
39
if (!is_array($config['system']['group'])) {
40
	$config['system']['group'] = array();
41
}
42

    
43
$a_group = &$config['system']['group'];
44

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

    
49
function cpusercmp($a, $b) {
50
	return strcasecmp($a['name'], $b['name']);
51
}
52

    
53
function admin_groups_sort() {
54
	global $a_group;
55

    
56
	if (!is_array($a_group)) {
57
		return;
58
	}
59

    
60
	usort($a_group, "cpusercmp");
61
}
62

    
63
if ($_POST['act'] == "delgroup") {
64

    
65
	if (!isset($id) || !isset($_REQUEST['groupname']) || !isset($a_group[$id]) || ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
66
		pfSenseHeader("system_groupmanager.php");
67
		exit;
68
	}
69

    
70
	local_group_del($a_group[$id]);
71
	$groupdeleted = $a_group[$id]['name'];
72
	unset($a_group[$id]);
73
	/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
74
	$a_group = array_values($a_group);
75
	write_config();
76
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
77
}
78

    
79
if ($_POST['act'] == "delpriv") {
80

    
81
	if (!isset($id) || !isset($a_group[$id])) {
82
		pfSenseHeader("system_groupmanager.php");
83
		exit;
84
	}
85

    
86
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_REQUEST['privid']]]['name'];
87
	unset($a_group[$id]['priv'][$_REQUEST['privid']]);
88

    
89
	if (is_array($a_group[$id]['member'])) {
90
		foreach ($a_group[$id]['member'] as $uid) {
91
			$user = getUserEntryByUID($uid);
92
			if ($user) {
93
				local_user_set($user);
94
			}
95
		}
96
	}
97

    
98
	write_config();
99
	$act = "edit";
100
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
101
}
102

    
103
if ($act == "edit") {
104
	if (isset($id) && isset($a_group[$id])) {
105
		$pconfig['name'] = $a_group[$id]['name'];
106
		$pconfig['gid'] = $a_group[$id]['gid'];
107
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
108
		$pconfig['description'] = $a_group[$id]['description'];
109
		$pconfig['members'] = $a_group[$id]['member'];
110
		$pconfig['priv'] = $a_group[$id]['priv'];
111
	}
112
}
113

    
114
if (isset($_POST['dellall_x'])) {
115

    
116
	$del_groups = $_POST['delete_check'];
117

    
118
	if (!empty($del_groups)) {
119
		foreach ($del_groups as $groupid) {
120
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
121
				local_group_del($a_group[$groupid]);
122
				unset($a_group[$groupid]);
123
			}
124
		}
125
		/* Reindex the array to avoid operating on an incorrect index https://redmine.pfsense.org/issues/7733 */
126
		$a_group = array_values($a_group);
127
		$savemsg = gettext("Selected groups removed successfully.");
128
		write_config($savemsg);
129
	}
130
}
131

    
132
if (isset($_POST['save'])) {
133
	unset($input_errors);
134
	$pconfig = $_POST;
135

    
136
	/* input validation */
137
	$reqdfields = explode(" ", "groupname");
138
	$reqdfieldsn = array(gettext("Group Name"));
139

    
140
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
141

    
142
	if ($_POST['gtype'] != "remote") {
143
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
144
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
145
		}
146
	} else {
147
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
148
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
149
		}
150
	}
151

    
152
	if (strlen($_POST['groupname']) > 16) {
153
		$input_errors[] = gettext("The group name is longer than 16 characters.");
154
	}
155

    
156
	/* Check the POSTed members to ensure they are valid and exist */
157
	if (is_array($_POST['members'])) {
158
		foreach ($_POST['members'] as $newmember) {
159
			if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
160
				$input_errors[] = gettext("One or more invalid group members was submitted.");
161
			}
162
		}
163
	}
164

    
165
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
166
		/* make sure there are no dupes */
167
		foreach ($a_group as $group) {
168
			if ($group['name'] == $_POST['groupname']) {
169
				$input_errors[] = gettext("Another entry with the same group name already exists.");
170
				break;
171
			}
172
		}
173
	}
174

    
175
	if (!$input_errors) {
176
		$group = array();
177
		if (isset($id) && $a_group[$id]) {
178
			$group = $a_group[$id];
179
		}
180

    
181
		$group['name'] = $_POST['groupname'];
182
		$group['description'] = $_POST['description'];
183
		$group['scope'] = $_POST['gtype'];
184

    
185
		if (empty($_POST['members'])) {
186
			unset($group['member']);
187
		} else if ($group['gid'] != 1998) { // all group
188
			$group['member'] = $_POST['members'];
189
		}
190

    
191
		if (isset($id) && $a_group[$id]) {
192
			$a_group[$id] = $group;
193
		} else {
194
			$group['gid'] = $config['system']['nextgid']++;
195
			$a_group[] = $group;
196
		}
197

    
198
		admin_groups_sort();
199

    
200
		local_group_set($group);
201

    
202
		/* Refresh users in this group since their privileges may have changed. */
203
		if (is_array($group['member'])) {
204
			$a_user = &$config['system']['user'];
205
			foreach ($a_user as & $user) {
206
				if (in_array($user['uid'], $group['member'])) {
207
					local_user_set($user);
208
				}
209
			}
210
		}
211

    
212
		write_config();
213

    
214
		header("Location: system_groupmanager.php");
215
		exit;
216
	}
217

    
218
	$pconfig['name'] = $_POST['groupname'];
219
}
220

    
221
function build_priv_table() {
222
	global $a_group, $id;
223

    
224
	$privhtml = '<div class="table-responsive">';
225
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
226
	$privhtml .=		'<thead>';
227
	$privhtml .=			'<tr>';
228
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
229
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
230
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
231
	$privhtml .=			'</tr>';
232
	$privhtml .=		'</thead>';
233
	$privhtml .=		'<tbody>';
234

    
235
	$user_has_root_priv = false;
236

    
237
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
238
		$privhtml .=		'<tr>';
239
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
240
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
241
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
242
			$privhtml .=			' ' . gettext('(admin privilege)');
243
			$user_has_root_priv = true;
244
		}
245
		$privhtml .=			'</td>';
246
		$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>';
247
		$privhtml .=		'</tr>';
248

    
249
	}
250

    
251
	if ($user_has_root_priv) {
252
		$privhtml .=		'<tr>';
253
		$privhtml .=			'<td colspan="2">';
254
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
255
		$privhtml .=			'</td>';
256
		$privhtml .=			'<td>';
257
		$privhtml .=			'</td>';
258
		$privhtml .=		'</tr>';
259

    
260
	}
261

    
262
	$privhtml .=		'</tbody>';
263
	$privhtml .=	'</table>';
264
	$privhtml .= '</div>';
265

    
266
	$privhtml .= '<nav class="action-buttons">';
267
	$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>';
268
	$privhtml .= '</nav>';
269

    
270
	return($privhtml);
271
}
272

    
273
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
274
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
275

    
276
if ($act == "new" || $act == "edit") {
277
	$pgtitle[] = gettext('Edit');
278
	$pglinks[] = "@self";
279
}
280

    
281
include("head.inc");
282

    
283
if ($input_errors) {
284
	print_input_errors($input_errors);
285
}
286

    
287
if ($savemsg) {
288
	print_info_box($savemsg, 'success');
289
}
290

    
291
$tab_array = array();
292
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
293
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
294
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
295
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
296
display_top_tabs($tab_array);
297

    
298
if (!($act == "new" || $act == "edit")) {
299
?>
300
<div class="panel panel-default">
301
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
302
	<div class="panel-body">
303
		<div class="table-responsive">
304
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
305
				<thead>
306
					<tr>
307
						<th><?=gettext("Group name")?></th>
308
						<th><?=gettext("Description")?></th>
309
						<th><?=gettext("Member Count")?></th>
310
						<th><?=gettext("Actions")?></th>
311
					</tr>
312
				</thead>
313
				<tbody>
314
<?php
315
	foreach ($a_group as $i => $group):
316
		if ($group["name"] == "all") {
317
			$groupcount = count($config['system']['user']);
318
		} else {
319
			$groupcount = count($group['member']);
320
		}
321
?>
322
					<tr>
323
						<td>
324
							<?=htmlspecialchars($group['name'])?>
325
						</td>
326
						<td>
327
							<?=htmlspecialchars($group['description'])?>
328
						</td>
329
						<td>
330
							<?=$groupcount?>
331
						</td>
332
						<td>
333
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
334
							<?php if ($group['scope'] != "system"): ?>
335
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>" usepost></a>
336
							<?php endif;?>
337
						</td>
338
					</tr>
339
<?php
340
	endforeach;
341
?>
342
				</tbody>
343
			</table>
344
		</div>
345
	</div>
346
</div>
347

    
348
<nav class="action-buttons">
349
	<a href="?act=new" class="btn btn-success btn-sm">
350
		<i class="fa fa-plus icon-embed-btn"></i>
351
		<?=gettext("Add")?>
352
	</a>
353
</nav>
354
<?php
355
	include('foot.inc');
356
	exit;
357
}
358

    
359
$form = new Form;
360
$form->setAction('system_groupmanager.php?act=edit');
361
$form->addGlobal(new Form_Input(
362
	'groupid',
363
	null,
364
	'hidden',
365
	$id
366
));
367

    
368
if (isset($id) && $a_group[$id]) {
369
	$form->addGlobal(new Form_Input(
370
		'id',
371
		null,
372
		'hidden',
373
		$id
374
	));
375

    
376
	$form->addGlobal(new Form_Input(
377
		'gid',
378
		null,
379
		'hidden',
380
		$pconfig['gid']
381
	));
382
}
383

    
384
$section = new Form_Section('Group Properties');
385

    
386
$section->addInput($input = new Form_Input(
387
	'groupname',
388
	'*Group name',
389
	'text',
390
	$pconfig['name']
391
));
392

    
393
if ($pconfig['gtype'] == "system") {
394
	$input->setReadonly();
395

    
396
	$section->addInput(new Form_Input(
397
		'gtype',
398
		'*Scope',
399
		'text',
400
		$pconfig['gtype']
401
	))->setReadonly();
402
} else {
403
	$section->addInput(new Form_Select(
404
		'gtype',
405
		'*Scope',
406
		$pconfig['gtype'],
407
		["local" => gettext("Local"), "remote" => gettext("Remote")]
408
	));
409
}
410

    
411
$section->addInput(new Form_Input(
412
	'description',
413
	'Description',
414
	'text',
415
	$pconfig['description']
416
))->setHelp('Group description, for administrative information only');
417

    
418

    
419
$form->add($section);
420
if ($pconfig['gid'] != 1998) { // all users group
421

    
422
	// ==== Group membership ==================================================
423
	$group = new Form_Group('Group membership');
424

    
425
	// Make a list of all the groups configured on the system, and a list of
426
	// those which this user is a member of
427
	$systemGroups = array();
428
	$usersGroups = array();
429

    
430
	foreach ($config['system']['user'] as $user) {
431
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
432
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
433
		} else {
434
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
435
		}
436
	}
437

    
438
	$group->add(new Form_Select(
439
		'notmembers',
440
		null,
441
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
442
		$systemGroups,
443
		true
444
	))->setHelp('Not members');
445

    
446
	$group->add(new Form_Select(
447
		'members',
448
		null,
449
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
450
		$usersGroups,
451
		true
452
	))->setHelp('Members');
453

    
454
	$section->add($group);
455

    
456
	$group = new Form_Group('');
457

    
458
	$group->add(new Form_Button(
459
		'movetoenabled',
460
		'Move to "Members"',
461
		null,
462
		'fa-angle-double-right'
463
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
464

    
465
	$group->add(new Form_Button(
466
		'movetodisabled',
467
		'Move to "Not members',
468
		null,
469
		'fa-angle-double-left'
470
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
471

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

    
475
}
476

    
477
if (isset($pconfig['gid'])) {
478
	$section = new Form_Section('Assigned Privileges');
479

    
480
	$section->addInput(new Form_StaticText(
481
		null,
482
		build_priv_table()
483
	));
484

    
485

    
486
	$form->add($section);
487
}
488

    
489
print $form;
490
?>
491
<script type="text/javascript">
492
//<![CDATA[
493
events.push(function() {
494

    
495
	// On click . .
496
	$("#movetodisabled").click(function() {
497
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
498
	});
499

    
500
	$("#movetoenabled").click(function() {
501
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
502
	});
503

    
504
	// On submit mark all the user's groups as "selected"
505
	$('form').submit(function() {
506
		AllServers($('[name="members[]"] option'), true);
507
	});
508
});
509
//]]>
510
</script>
511
<?php
512
include('foot.inc');
(205-205/232)