Project

General

Profile

Download (14.6 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
$logging_level = LOG_WARNING;
40
$logging_prefix = gettext("Local User Database");
41

    
42
init_config_arr(array('system', 'group'));
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']) ||
66
	    !isset($a_group[$id]) ||
67
	    ($_REQUEST['groupname'] != $a_group[$id]['name'])) {
68
		pfSenseHeader("system_groupmanager.php");
69
		exit;
70
	}
71

    
72
	local_group_del($a_group[$id]);
73
	$groupdeleted = $a_group[$id]['name'];
74
	unset($a_group[$id]);
75
	/*
76
	 * Reindex the array to avoid operating on an incorrect index
77
	 * https://redmine.pfsense.org/issues/7733
78
	 */
79
	$a_group = array_values($a_group);
80

    
81
	$savemsg = sprintf(gettext("Successfully deleted group: %s"),
82
	    $groupdeleted);
83
	write_config($savemsg);
84
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
85
}
86

    
87
if ($_POST['act'] == "delpriv") {
88

    
89
	if (!isset($id) || !isset($a_group[$id])) {
90
		pfSenseHeader("system_groupmanager.php");
91
		exit;
92
	}
93

    
94
	$privdeleted =
95
	    $priv_list[$a_group[$id]['priv'][$_REQUEST['privid']]]['name'];
96
	unset($a_group[$id]['priv'][$_REQUEST['privid']]);
97

    
98
	if (is_array($a_group[$id]['member'])) {
99
		foreach ($a_group[$id]['member'] as $uid) {
100
			$user = getUserEntryByUID($uid);
101
			if ($user) {
102
				local_user_set($user);
103
			}
104
		}
105
	}
106

    
107
	$savemsg = sprintf(gettext("Removed Privilege \"%s\" from group %s"),
108
	    $privdeleted, $a_group[$id]['name']);
109
	write_config($savemsg);
110
	syslog($logging_level, "{$logging_prefix}: {$savemsg}");
111

    
112
	$act = "edit";
113
}
114

    
115
if ($act == "edit") {
116
	if (isset($id) && isset($a_group[$id])) {
117
		$pconfig['name'] = $a_group[$id]['name'];
118
		$pconfig['gid'] = $a_group[$id]['gid'];
119
		$pconfig['gtype'] = empty($a_group[$id]['scope'])
120
		    ? "local" : $a_group[$id]['scope'];
121
		$pconfig['description'] = $a_group[$id]['description'];
122
		$pconfig['members'] = $a_group[$id]['member'];
123
		$pconfig['priv'] = $a_group[$id]['priv'];
124
	}
125
}
126

    
127
if (isset($_POST['dellall_x'])) {
128

    
129
	$del_groups = $_POST['delete_check'];
130
	$deleted_groups = array();
131

    
132
	if (!empty($del_groups)) {
133
		foreach ($del_groups as $groupid) {
134
			if (isset($a_group[$groupid]) &&
135
			    $a_group[$groupid]['scope'] != "system") {
136
				$deleted_groups[] = $a_group[$groupid]['name'];
137
				local_group_del($a_group[$groupid]);
138
				unset($a_group[$groupid]);
139
			}
140
		}
141

    
142
		$savemsg = sprintf(gettext("Successfully deleted %s: %s"),
143
		    (count($deleted_groups) == 1)
144
		    ? gettext("group") : gettext("groups"),
145
		    implode(', ', $deleted_groups));
146
		/*
147
		 * Reindex the array to avoid operating on an incorrect index
148
		 * https://redmine.pfsense.org/issues/7733
149
		 */
150
		$a_group = array_values($a_group);
151
		write_config($savemsg);
152
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
153
	}
154
}
155

    
156
if (isset($_POST['save'])) {
157
	unset($input_errors);
158
	$pconfig = $_POST;
159

    
160
	/* input validation */
161
	$reqdfields = explode(" ", "groupname");
162
	$reqdfieldsn = array(gettext("Group Name"));
163

    
164
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
165

    
166
	if ($_POST['gtype'] != "remote") {
167
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
168
			$input_errors[] = sprintf(gettext(
169
			    "The (%s) group name contains invalid characters."),
170
			    $_POST['gtype']);
171
		}
172
	} else {
173
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
174
			$input_errors[] = sprintf(gettext(
175
			    "The (%s) group name contains invalid characters."),
176
			    $_POST['gtype']);
177
		}
178
	}
179

    
180
	if (strlen($_POST['groupname']) > 16) {
181
		$input_errors[] = gettext(
182
		    "The group name is longer than 16 characters.");
183
	}
184

    
185
	/* Check the POSTed members to ensure they are valid and exist */
186
	if (is_array($_POST['members'])) {
187
		foreach ($_POST['members'] as $newmember) {
188
			if (!is_numeric($newmember) ||
189
			    empty(getUserEntryByUID($newmember))) {
190
				$input_errors[] = gettext("One or more " .
191
				    "invalid group members was submitted.");
192
			}
193
		}
194
	}
195

    
196
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
197
		/* make sure there are no dupes */
198
		foreach ($a_group as $group) {
199
			if ($group['name'] == $_POST['groupname']) {
200
				$input_errors[] = gettext("Another entry " .
201
				    "with the same group name already exists.");
202
				break;
203
			}
204
		}
205
	}
206

    
207
	if (!$input_errors) {
208
		$group = array();
209
		if (isset($id) && $a_group[$id]) {
210
			$group = $a_group[$id];
211
		}
212

    
213
		$group['name'] = $_POST['groupname'];
214
		$group['description'] = $_POST['description'];
215
		$group['scope'] = $_POST['gtype'];
216

    
217
		if (empty($_POST['members'])) {
218
			unset($group['member']);
219
		} else if ($group['gid'] != 1998) { // all group
220
			$group['member'] = $_POST['members'];
221
		}
222

    
223
		if (isset($id) && $a_group[$id]) {
224
			$a_group[$id] = $group;
225
		} else {
226
			$group['gid'] = $config['system']['nextgid']++;
227
			$a_group[] = $group;
228
		}
229

    
230
		admin_groups_sort();
231

    
232
		local_group_set($group);
233

    
234
		/*
235
		 * Refresh users in this group since their privileges may have
236
		 * changed.
237
		 */
238
		if (is_array($group['member'])) {
239
			init_config_arr(array('system', 'user'));
240
			$a_user = &$config['system']['user'];
241
			foreach ($a_user as & $user) {
242
				if (in_array($user['uid'], $group['member'])) {
243
					local_user_set($user);
244
				}
245
			}
246
		}
247

    
248
		/* Sort it alphabetically */
249
		usort($config['system']['group'], function($a, $b) {
250
			return strcmp($a['name'], $b['name']);
251
		});
252

    
253
		$savemsg = sprintf(gettext("Successfully %s group %s"),
254
		    (strlen($id) > 0) ? gettext("edited") : gettext("created"),
255
		    $group['name']);
256
		write_config($savemsg);
257
		syslog($logging_level, "{$logging_prefix}: {$savemsg}");
258

    
259
		header("Location: system_groupmanager.php");
260
		exit;
261
	}
262

    
263
	$pconfig['name'] = $_POST['groupname'];
264
}
265

    
266
function build_priv_table() {
267
	global $a_group, $id;
268

    
269
	$privhtml = '<div class="table-responsive">';
270
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
271
	$privhtml .=		'<thead>';
272
	$privhtml .=			'<tr>';
273
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
274
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
275
	$privhtml .=				'<th>' . gettext('Action') . '</th>';
276
	$privhtml .=			'</tr>';
277
	$privhtml .=		'</thead>';
278
	$privhtml .=		'<tbody>';
279

    
280
	$user_has_root_priv = false;
281

    
282
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
283
		$privhtml .=		'<tr>';
284
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
285
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']);
286
		if (isset($priv['warn']) && ($priv['warn'] == 'standard-warning-root')) {
287
			$privhtml .=			' ' . gettext('(admin privilege)');
288
			$user_has_root_priv = true;
289
		}
290
		$privhtml .=			'</td>';
291
		$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>';
292
		$privhtml .=		'</tr>';
293

    
294
	}
295

    
296
	if ($user_has_root_priv) {
297
		$privhtml .=		'<tr>';
298
		$privhtml .=			'<td colspan="2">';
299
		$privhtml .=				'<b>' . gettext('Security notice: Users in this group effectively have administrator-level access') . '</b>';
300
		$privhtml .=			'</td>';
301
		$privhtml .=			'<td>';
302
		$privhtml .=			'</td>';
303
		$privhtml .=		'</tr>';
304

    
305
	}
306

    
307
	$privhtml .=		'</tbody>';
308
	$privhtml .=	'</table>';
309
	$privhtml .= '</div>';
310

    
311
	$privhtml .= '<nav class="action-buttons">';
312
	$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>';
313
	$privhtml .= '</nav>';
314

    
315
	return($privhtml);
316
}
317

    
318
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
319
$pglinks = array("", "system_usermanager.php", "system_groupmanager.php");
320

    
321
if ($act == "new" || $act == "edit") {
322
	$pgtitle[] = gettext('Edit');
323
	$pglinks[] = "@self";
324
}
325

    
326
include("head.inc");
327

    
328
if ($input_errors) {
329
	print_input_errors($input_errors);
330
}
331

    
332
if ($savemsg) {
333
	print_info_box($savemsg, 'success');
334
}
335

    
336
$tab_array = array();
337
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
338
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
339
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
340
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
341
display_top_tabs($tab_array);
342

    
343
if (!($act == "new" || $act == "edit")) {
344
?>
345
<div class="panel panel-default">
346
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
347
	<div class="panel-body">
348
		<div class="table-responsive">
349
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
350
				<thead>
351
					<tr>
352
						<th><?=gettext("Group name")?></th>
353
						<th><?=gettext("Description")?></th>
354
						<th><?=gettext("Member Count")?></th>
355
						<th><?=gettext("Actions")?></th>
356
					</tr>
357
				</thead>
358
				<tbody>
359
<?php
360
	foreach ($a_group as $i => $group):
361
		if ($group["name"] == "all") {
362
			$groupcount = count($config['system']['user']);
363
		} elseif (is_array($group['member'])) {
364
			$groupcount = count($group['member']);
365
		} else {
366
			$groupcount = 0;
367
		}
368
?>
369
					<tr>
370
						<td>
371
							<?=htmlspecialchars($group['name'])?>
372
						</td>
373
						<td>
374
							<?=htmlspecialchars($group['description'])?>
375
						</td>
376
						<td>
377
							<?=$groupcount?>
378
						</td>
379
						<td>
380
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
381
							<?php if ($group['scope'] != "system"): ?>
382
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>" usepost></a>
383
							<?php endif;?>
384
						</td>
385
					</tr>
386
<?php
387
	endforeach;
388
?>
389
				</tbody>
390
			</table>
391
		</div>
392
	</div>
393
</div>
394

    
395
<nav class="action-buttons">
396
	<a href="?act=new" class="btn btn-success btn-sm">
397
		<i class="fa fa-plus icon-embed-btn"></i>
398
		<?=gettext("Add")?>
399
	</a>
400
</nav>
401
<?php
402
	include('foot.inc');
403
	exit;
404
}
405

    
406
$form = new Form;
407
$form->setAction('system_groupmanager.php?act=edit');
408
$form->addGlobal(new Form_Input(
409
	'groupid',
410
	null,
411
	'hidden',
412
	$id
413
));
414

    
415
if (isset($id) && $a_group[$id]) {
416
	$form->addGlobal(new Form_Input(
417
		'id',
418
		null,
419
		'hidden',
420
		$id
421
	));
422

    
423
	$form->addGlobal(new Form_Input(
424
		'gid',
425
		null,
426
		'hidden',
427
		$pconfig['gid']
428
	));
429
}
430

    
431
$section = new Form_Section('Group Properties');
432

    
433
$section->addInput($input = new Form_Input(
434
	'groupname',
435
	'*Group name',
436
	'text',
437
	$pconfig['name']
438
));
439

    
440
if ($pconfig['gtype'] == "system") {
441
	$input->setReadonly();
442

    
443
	$section->addInput(new Form_Input(
444
		'gtype',
445
		'*Scope',
446
		'text',
447
		$pconfig['gtype']
448
	))->setReadonly();
449
} else {
450
	$section->addInput(new Form_Select(
451
		'gtype',
452
		'*Scope',
453
		$pconfig['gtype'],
454
		["local" => gettext("Local"), "remote" => gettext("Remote")]
455
	))->setHelp("<span class=\"text-danger\">Warning: Changing this " .
456
	    "setting may affect the local groups file, in which case a " .
457
	    "reboot may be required for the changes to take effect.</span>");
458
}
459

    
460
$section->addInput(new Form_Input(
461
	'description',
462
	'Description',
463
	'text',
464
	$pconfig['description']
465
))->setHelp('Group description, for administrative information only');
466

    
467
$form->add($section);
468

    
469
/* all users group */
470
if ($pconfig['gid'] != 1998) {
471
	/* Group membership */
472
	$group = new Form_Group('Group membership');
473

    
474
	/*
475
	 * Make a list of all the groups configured on the system, and a list of
476
	 * those which this user is a member of
477
	 */
478
	$systemGroups = array();
479
	$usersGroups = array();
480

    
481
	foreach ($config['system']['user'] as $user) {
482
		if (is_array($pconfig['members']) && in_array($user['uid'],
483
		    $pconfig['members'])) {
484
			/* Add it to the user's list */
485
			$usersGroups[ $user['uid'] ] = $user['name'];
486
		} else {
487
			/* Add it to the 'not a member of' list */
488
			$systemGroups[ $user['uid'] ] = $user['name'];
489
		}
490
	}
491

    
492
	$group->add(new Form_Select(
493
		'notmembers',
494
		null,
495
		array_combine((array)$pconfig['groups'],
496
		    (array)$pconfig['groups']),
497
		$systemGroups,
498
		true
499
	))->setHelp('Not members');
500

    
501
	$group->add(new Form_Select(
502
		'members',
503
		null,
504
		array_combine((array)$pconfig['groups'],
505
		    (array)$pconfig['groups']),
506
		$usersGroups,
507
		true
508
	))->setHelp('Members');
509

    
510
	$section->add($group);
511

    
512
	$group = new Form_Group('');
513

    
514
	$group->add(new Form_Button(
515
		'movetoenabled',
516
		'Move to "Members"',
517
		null,
518
		'fa-angle-double-right'
519
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
520
	    'btn-info btn-sm');
521

    
522
	$group->add(new Form_Button(
523
		'movetodisabled',
524
		'Move to "Not members',
525
		null,
526
		'fa-angle-double-left'
527
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass(
528
	    'btn-info btn-sm');
529

    
530
	$group->setHelp(
531
	    'Hold down CTRL (PC)/COMMAND (Mac) key to select multiple items.');
532
	$section->add($group);
533

    
534
}
535

    
536
if (isset($pconfig['gid'])) {
537
	$section = new Form_Section('Assigned Privileges');
538

    
539
	$section->addInput(new Form_StaticText(
540
		null,
541
		build_priv_table()
542
	));
543

    
544

    
545
	$form->add($section);
546
}
547

    
548
print $form;
549
?>
550
<script type="text/javascript">
551
//<![CDATA[
552
events.push(function() {
553

    
554
	// On click . .
555
	$("#movetodisabled").click(function() {
556
		moveOptions($('[name="members[]"] option'),
557
		    $('[name="notmembers[]"]'));
558
	});
559

    
560
	$("#movetoenabled").click(function() {
561
		moveOptions($('[name="notmembers[]"] option'),
562
		    $('[name="members[]"]'));
563
	});
564

    
565
	// On submit mark all the user's groups as "selected"
566
	$('form').submit(function() {
567
		AllServers($('[name="members[]"] option'), true);
568
	});
569
});
570
//]]>
571
</script>
572
<?php
573
include('foot.inc');
(207-207/234)