Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_groupmanager.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2005 Paul Taylor <paultaylor@winn-dixie.com>
8
 *	Copyright (c)  2008 Shrew Soft Inc
9
 *
10
 *	Some or all of this file is based on the m0n0wall project which is
11
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
12
 *
13
 *	Redistribution and use in source and binary forms, with or without modification,
14
 *	are permitted provided that the following conditions are met:
15
 *
16
 *	1. Redistributions of source code must retain the above copyright notice,
17
 *		this list of conditions and the following disclaimer.
18
 *
19
 *	2. Redistributions in binary form must reproduce the above copyright
20
 *		notice, this list of conditions and the following disclaimer in
21
 *		the documentation and/or other materials provided with the
22
 *		distribution.
23
 *
24
 *	3. All advertising materials mentioning features or use of this software
25
 *		must display the following acknowledgment:
26
 *		"This product includes software developed by the pfSense Project
27
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
28
 *
29
 *	4. The names "pfSense" and "pfSense Project" must not be used to
30
 *		 endorse or promote products derived from this software without
31
 *		 prior written permission. For written permission, please contact
32
 *		 coreteam@pfsense.org.
33
 *
34
 *	5. Products derived from this software may not be called "pfSense"
35
 *		nor may "pfSense" appear in their names without prior written
36
 *		permission of the Electric Sheep Fencing, LLC.
37
 *
38
 *	6. Redistributions of any form whatsoever must retain the following
39
 *		acknowledgment:
40
 *
41
 *	"This product includes software developed by the pfSense Project
42
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
56
 *
57
 *	====================================================================
58
 *
59
 */
60

    
61
##|+PRIV
62
##|*IDENT=page-system-groupmanager
63
##|*NAME=System: Group manager
64
##|*DESCR=Allow access to the 'System: Group manager' page.
65
##|*MATCH=system_groupmanager.php*
66
##|-PRIV
67

    
68
require_once("guiconfig.inc");
69

    
70
if (!is_array($config['system']['group'])) {
71
	$config['system']['group'] = array();
72
}
73

    
74
$a_group = &$config['system']['group'];
75

    
76
unset($id);
77

    
78
if (isset($_POST['groupid']) && is_numericint($_POST['groupid'])) {
79
	$id = $_POST['groupid'];
80
}
81

    
82
if (isset($_GET['groupid']) && is_numericint($_GET['groupid'])) {
83
	$id = $_GET['groupid'];
84
}
85

    
86
$act = (isset($_GET['act']) ? $_GET['act'] : '');
87

    
88
function cpusercmp($a, $b) {
89
	return strcasecmp($a['name'], $b['name']);
90
}
91

    
92
function admin_groups_sort() {
93
	global $a_group;
94

    
95
	if (!is_array($a_group)) {
96
		return;
97
	}
98

    
99
	usort($a_group, "cpusercmp");
100
}
101

    
102
if ($act == "delgroup") {
103

    
104
	if (!isset($id) || !isset($_GET['groupname']) || !isset($a_group[$id]) || ($_GET['groupname'] != $a_group[$id]['name'])) {
105
		pfSenseHeader("system_groupmanager.php");
106
		exit;
107
	}
108

    
109
	conf_mount_rw();
110
	local_group_del($a_group[$id]);
111
	conf_mount_ro();
112
	$groupdeleted = $a_group[$id]['name'];
113
	unset($a_group[$id]);
114
	write_config();
115
	$savemsg = sprintf(gettext("Group %s successfully deleted."), $groupdeleted);
116
}
117

    
118
if ($act == "delpriv") {
119

    
120
	if (!isset($id) || !isset($a_group[$id])) {
121
		pfSenseHeader("system_groupmanager.php");
122
		exit;
123
	}
124

    
125
	$privdeleted = $priv_list[$a_group[$id]['priv'][$_POST['privid']]]['name'];
126
	unset($a_group[$id]['priv'][$_GET['privid']]);
127

    
128
	if (is_array($a_group[$id]['member'])) {
129
		foreach ($a_group[$id]['member'] as $uid) {
130
			$user = getUserEntryByUID($uid);
131
			if ($user) {
132
				local_user_set($user);
133
			}
134
		}
135
	}
136

    
137
	write_config();
138
	$act = "edit";
139
	$savemsg = sprintf(gettext("Privilege %s successfully deleted."), $privdeleted);
140
}
141

    
142
if ($act == "edit") {
143
	if (isset($id) && isset($a_group[$id])) {
144
		$pconfig['name'] = $a_group[$id]['name'];
145
		$pconfig['gid'] = $a_group[$id]['gid'];
146
		$pconfig['gtype'] = empty($a_group[$id]['scope']) ? "local" : $a_group[$id]['scope'];
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($_GET['dellall_x'])) {
154

    
155
	$del_groups = $_GET['delete_check'];
156

    
157
	if (!empty($del_groups)) {
158
		foreach ($del_groups as $groupid) {
159
			if (isset($a_group[$groupid]) && $a_group[$groupid]['scope'] != "system") {
160
				conf_mount_rw();
161
				local_group_del($a_group[$groupid]);
162
				conf_mount_ro();
163
				unset($a_group[$groupid]);
164
			}
165
		}
166
		$savemsg = gettext("Selected groups removed successfully.");
167
		write_config($savemsg);
168
	}
169
}
170

    
171
if (isset($_POST['save'])) {
172
	unset($input_errors);
173
	$pconfig = $_POST;
174

    
175
	/* input validation */
176
	$reqdfields = explode(" ", "groupname");
177
	$reqdfieldsn = array(gettext("Group Name"));
178

    
179
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
180

    
181
	if ($_POST['gtype'] != "remote") {
182
		if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['groupname'])) {
183
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
184
		}
185
	} else {
186
		if (preg_match("/[^a-zA-Z0-9\.\- _]/", $_POST['groupname'])) {
187
			$input_errors[] = sprintf(gettext("The (%s) group name contains invalid characters."), $_POST['gtype']);
188
		}
189
	}
190

    
191
	if (strlen($_POST['groupname']) > 16) {
192
		$input_errors[] = gettext("The group name is longer than 16 characters.");
193
	}
194

    
195
	/* Check the POSTed members to ensure they are valid and exist */
196
	if(is_array($_POST['members'])) {
197
		foreach ($_POST['members'] as $newmember) {
198
			if (!is_numeric($newmember) || empty(getUserEntryByUID($newmember))) {
199
				$input_errors[] = gettext("One or more invalid group members was submitted.");
200
			}
201
		}
202
	}
203

    
204
	if (!$input_errors && !(isset($id) && $a_group[$id])) {
205
		/* make sure there are no dupes */
206
		foreach ($a_group as $group) {
207
			if ($group['name'] == $_POST['groupname']) {
208
				$input_errors[] = gettext("Another entry with the same group name already exists.");
209
				break;
210
			}
211
		}
212
	}
213

    
214
	if (!$input_errors) {
215
		$group = array();
216
		if (isset($id) && $a_group[$id]) {
217
			$group = $a_group[$id];
218
		}
219

    
220
		$group['name'] = $_POST['groupname'];
221
		$group['description'] = $_POST['description'];
222
		$group['scope'] = $_POST['gtype'];
223

    
224
		if (empty($_POST['members'])) {
225
			unset($group['member']);
226
		} else if ($group['gid'] != 1998) { // all group
227
			$group['member'] = $_POST['members'];
228
		}
229

    
230
		if (isset($id) && $a_group[$id]) {
231
			$a_group[$id] = $group;
232
		} else {
233
			$group['gid'] = $config['system']['nextgid']++;
234
			$a_group[] = $group;
235
		}
236

    
237
		admin_groups_sort();
238

    
239
		conf_mount_rw();
240
		local_group_set($group);
241
		conf_mount_ro();
242

    
243
		/* Refresh users in this group since their privileges may have changed. */
244
		if (is_array($group['member'])) {
245
			$a_user = &$config['system']['user'];
246
			foreach ($a_user as & $user) {
247
				if (in_array($user['uid'], $group['member'])) {
248
					local_user_set($user);
249
				}
250
			}
251
		}
252

    
253
		write_config();
254

    
255
		header("Location: system_groupmanager.php");
256
		exit;
257
	}
258

    
259
	$pconfig['name'] = $_POST['groupname'];
260
}
261

    
262
function build_priv_table() {
263
	global $a_group, $id;
264

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

    
276
	foreach (get_user_privdesc($a_group[$id]) as $i => $priv) {
277
		$privhtml .=		'<tr>';
278
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
279
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
280
		$privhtml .=			'<td><a class="fa fa-trash" title="' . gettext('Delete Privilege') . '"	href="system_groupmanager.php?act=delpriv&amp;groupid=' . $id . '&amp;privid=' . $i . '"></a></td>';
281
		$privhtml .=		'</tr>';
282

    
283
	}
284

    
285
	$privhtml .=		'</tbody>';
286
	$privhtml .=	'</table>';
287
	$privhtml .= '</div>';
288

    
289
	$privhtml .= '<nav class="action-buttons">';
290
	$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>';
291
	$privhtml .= '</nav>';
292

    
293
	return($privhtml);
294
}
295

    
296
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Groups"));
297

    
298
if ($act == "new" || $act == "edit") {
299
	$pgtitle[] = gettext('Edit');
300
}
301

    
302
include("head.inc");
303

    
304
if ($input_errors) {
305
	print_input_errors($input_errors);
306
}
307

    
308
if ($savemsg) {
309
	print_info_box($savemsg, 'success');
310
}
311

    
312
$tab_array = array();
313
$tab_array[] = array(gettext("Users"), false, "system_usermanager.php");
314
$tab_array[] = array(gettext("Groups"), true, "system_groupmanager.php");
315
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
316
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
317
display_top_tabs($tab_array);
318

    
319
if (!($_GET['act'] == "new" || $_GET['act'] == "edit")) {
320
?>
321
<div class="panel panel-default">
322
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Groups')?></h2></div>
323
	<div class="panel-body">
324
		<div class="table-responsive">
325
			<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap table-rowdblclickedit" data-sortable>
326
				<thead>
327
					<tr>
328
						<th><?=gettext("Group name")?></th>
329
						<th><?=gettext("Description")?></th>
330
						<th><?=gettext("Member Count")?></th>
331
						<th><?=gettext("Actions")?></th>
332
					</tr>
333
				</thead>
334
				<tbody>
335
<?php
336
	foreach ($a_group as $i => $group):
337
		if ($group["name"] == "all") {
338
			$groupcount = count($config['system']['user']);
339
		} else {
340
			$groupcount = count($group['member']);
341
		}
342
?>
343
					<tr>
344
						<td>
345
							<?=htmlspecialchars($group['name'])?>
346
						</td>
347
						<td>
348
							<?=htmlspecialchars($group['description'])?>
349
						</td>
350
						<td>
351
							<?=$groupcount?>
352
						</td>
353
						<td>
354
							<a class="fa fa-pencil" title="<?=gettext("Edit group"); ?>" href="?act=edit&amp;groupid=<?=$i?>"></a>
355
							<?php if ($group['scope'] != "system"): ?>
356
								<a class="fa fa-trash"	title="<?=gettext("Delete group")?>" href="?act=delgroup&amp;groupid=<?=$i?>&amp;groupname=<?=$group['name']?>"></a>
357
							<?php endif;?>
358
						</td>
359
					</tr>
360
<?php
361
	endforeach;
362
?>
363
				</tbody>
364
			</table>
365
		</div>
366
	</div>
367
</div>
368

    
369
<nav class="action-buttons">
370
	<a href="?act=new" class="btn btn-success btn-sm">
371
		<i class="fa fa-plus icon-embed-btn"></i>
372
		<?=gettext("Add")?>
373
	</a>
374
</nav>
375
<?php
376
	include('foot.inc');
377
	exit;
378
}
379

    
380
$form = new Form;
381
$form->setAction('system_groupmanager.php?act=edit');
382
$form->addGlobal(new Form_Input(
383
	'groupid',
384
	null,
385
	'hidden',
386
	$id
387
));
388

    
389
if (isset($id) && $a_group[$id]){
390
	$form->addGlobal(new Form_Input(
391
		'id',
392
		null,
393
		'hidden',
394
		$id
395
	));
396

    
397
	$form->addGlobal(new Form_Input(
398
		'gid',
399
		null,
400
		'hidden',
401
		$pconfig['gid']
402
	));
403
}
404

    
405
$section = new Form_Section('Group Properties');
406

    
407
$section->addInput($input = new Form_Input(
408
	'groupname',
409
	'Group name',
410
	'text',
411
	$pconfig['name']
412
));
413

    
414
if ($pconfig['gtype'] == "system") {
415
	$input->setReadonly();
416

    
417
	$section->addInput(new Form_Input(
418
		'gtype',
419
		'Scope',
420
		'text',
421
		$pconfig['gtype']
422
	))->setReadonly();
423
} else {
424
	$section->addInput(new Form_Select(
425
		'gtype',
426
		'Scope',
427
		$pconfig['gtype'],
428
		["local" => gettext("Local"), "remote" => gettext("Remote")]
429
	));
430
}
431

    
432
$section->addInput(new Form_Input(
433
	'description',
434
	'Description',
435
	'text',
436
	$pconfig['description']
437
))->setHelp('Group description, for administrative information only');
438

    
439

    
440
$form->add($section);
441
if ($pconfig['gid'] != 1998) { // all users group
442

    
443
	// ==== Group membership ==================================================
444
	$group = new Form_Group('Group membership');
445

    
446
	// Make a list of all the groups configured on the system, and a list of
447
	// those which this user is a member of
448
	$systemGroups = array();
449
	$usersGroups = array();
450

    
451
	foreach ($config['system']['user'] as $user) {
452
		if (is_array($pconfig['members']) && in_array($user['uid'], $pconfig['members'])) {
453
			$usersGroups[ $user['uid'] ] = $user['name'];	// Add it to the user's list
454
		} else {
455
			$systemGroups[ $user['uid'] ] = $user['name']; // Add it to the 'not a member of' list
456
		}
457
	}
458

    
459
	$group->add(new Form_Select(
460
		'notmembers',
461
		null,
462
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
463
		$systemGroups,
464
		true
465
	))->setHelp('Not members');
466

    
467
	$group->add(new Form_Select(
468
		'members',
469
		null,
470
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
471
		$usersGroups,
472
		true
473
	))->setHelp('Members');
474

    
475
	$section->add($group);
476

    
477
	$group = new Form_Group('');
478

    
479
	$group->add(new Form_Button(
480
		'movetoenabled',
481
		'Move to "Members"',
482
		null,
483
		'fa-angle-double-right'
484
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
485

    
486
	$group->add(new Form_Button(
487
		'movetodisabled',
488
		'Move to "Not members',
489
		null,
490
		'fa-angle-double-left'
491
	))->setAttribute('type','button')->removeClass('btn-primary')->addClass('btn-info btn-sm');
492

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

    
496
}
497

    
498
if ($_GET['act'] != "new") {
499
	$section = new Form_Section('Assigned Privileges');
500

    
501
	$section->addInput(new Form_StaticText(
502
		null,
503
		build_priv_table()
504
	));
505

    
506

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

    
510
print $form;
511
?>
512
<script type="text/javascript">
513
//<![CDATA[
514
events.push(function() {
515

    
516
	// On click . .
517
	$("#movetodisabled").click(function() {
518
		moveOptions($('[name="members[]"] option'), $('[name="notmembers[]"]'));
519
	});
520

    
521
	$("#movetoenabled").click(function() {
522
		moveOptions($('[name="notmembers[]"] option'), $('[name="members[]"]'));
523
	});
524

    
525
	// On submit mark all the user's groups as "selected"
526
	$('form').submit(function() {
527
		AllServers($('[name="members[]"] option'), true);
528
	});
529
});
530
//]]>
531
</script>
532
<?php
533
include('foot.inc');
(200-200/226)