Project

General

Profile

Download (7.51 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
		Copyright (C) 2008 Shrew Soft Inc
5
		All rights reserved.
6

    
7
		Copyright (C) 2007, 2008 Scott Ullrich <sullrich@gmail.com>
8
		All rights reserved.
9

    
10
        Copyright (C) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
11
        All rights reserved.
12

    
13
        Copyright (C) 2006 Paul Taylor <paultaylor@winn-dixie.com>.
14
        All rights reserved.
15

    
16
        Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
17
        All rights reserved.
18

    
19
        Redistribution and use in source and binary forms, with or without
20
        modification, are permitted provided that the following conditions are met:
21

    
22
        1. Redistributions of source code must retain the above copyright notice,
23
           this list of conditions and the following disclaimer.
24

    
25
        2. Redistributions in binary form must reproduce the above copyright
26
           notice, this list of conditions and the following disclaimer in the
27
           documentation and/or other materials provided with the distribution.
28

    
29
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
30
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
33
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
        POSSIBILITY OF SUCH DAMAGE.
39

    
40
		DISABLE_PHP_LINT_CHECKING
41
*/
42

    
43
require_once("functions.inc");
44
require_once("priv.defs.inc");
45

    
46
/*
47
 * USER PRIVILEGE DEFINITIONS
48
 */
49

    
50
$priv_list['user-shell-access'] = array();
51
$priv_list['user-shell-access']['name']  = "User - Shell account access";
52
$priv_list['user-shell-access']['descr'] = "Indicates whether the user is able to login for ".
53
										   "example via SSH.";
54

    
55
$priv_list['user-copy-files'] = array();
56
$priv_list['user-copy-files']['name']  = "User - Copy files";
57
$priv_list['user-copy-files']['descr'] = "Indicates whether the user is allowed to copy files ".
58
										 "onto the {$g['product_name']} appliance via SCP/SFTP. ".
59
										 "If you are going to use this privilege, you must install ".
60
										 "scponly on the appliance (Hint: pkg_add -r scponly).";
61

    
62
sort_privs($priv_list);
63

    
64
function cmp_privkeys($a, $b) {
65
	/* user privs at the top */
66
	$auser = strncmp("user-", $a, 5);
67
	$buser = strncmp("user-", $b, 5);
68
	if($auser != $buser)
69
		return $auser - buser;
70

    
71
	/* name compare others */
72
	return strcasecmp($a, $b);
73
}
74

    
75
function sort_privs(& $privs) {
76

    
77
	uksort($privs, "cmp_privkeys");
78
}
79

    
80
function cmp_page_matches($page, & $matches, $fullwc = true) {
81

    
82
//	$dbg_matches = implode(",", $matches);
83
//	log_error("debug: checking page {$page} match with {$dbg_matches}");
84

    
85
	if (!is_array($matches))
86
		return false;
87

    
88
	/* skip any leading fwdslash */
89
	$test = strpos($page, "/");
90
	if ($test !== false && $test == 0)
91
		$page = substr($page, 1);
92

    
93
	/* look for a match */
94
	foreach ($matches as $match) {
95

    
96
		/* possibly ignore full wildcard match */
97
		if (!$fullwc && !strcmp($match ,"*"))
98
			continue;
99

    
100
		/* compare exact or wildcard match */
101
		$wcpos = strpos($match, "*");
102
		if ($wcpos === false)
103
			$result = strcmp($page, $match);
104
		else
105
			$result = strncmp($page, $match, $wcpos);
106

    
107
		if (!$result)
108
			return true;
109
	}
110

    
111
	return false;
112
}
113

    
114
function map_page_privname($page) {
115
	global $priv_list;
116

    
117
	foreach ($priv_list as $pname => $pdata) {
118
		if (strncmp($pname, "page-", 5))
119
			continue;
120
		$fullwc = false;
121
		if (!strcasecmp($page,"any")||!strcmp($page,"*"))
122
			$fullwc = true;
123
		if (cmp_page_matches($page, $pdata['match'], $fullwc))
124
			return $pname;
125
	}
126

    
127
	return false;
128
}
129

    
130
function get_user_privileges(& $user) {
131

    
132
	$privs = $user['priv'];
133
	if (!is_array($privs))
134
		$privs = array();
135

    
136
	$names = local_user_get_groups($user, true);
137

    
138
	foreach ($names as $name) {
139
		$group = getGroupEntry($name);
140
		if (is_array($group['priv']))
141
			$privs = array_merge( $privs, $group['priv']);
142
	}
143

    
144
	return $privs;
145
}
146

    
147
function get_user_privdesc(& $user) {
148
	global $priv_list;
149

    
150
	$privs = array();
151

    
152
	$user_privs = $user['priv'];
153
	if (!is_array($user_privs))
154
		$user_privs = array();
155

    
156
	$names = local_user_get_groups($user, true);
157

    
158
	foreach ($names as $name) {
159
		$group = getGroupEntry($name);
160
		$group_privs = $group['priv'];
161
		if (!is_array($group_privs))
162
			continue;
163
		foreach ($group_privs as $pname) {
164
			if (in_array($pname,$user_privs))
165
				continue;
166
			if (!$priv_list[$pname])
167
				continue;
168
			$priv = $priv_list[$pname];
169
			$priv['group'] = $group['name'];
170
			$privs[] = $priv;
171
		}
172
	}
173

    
174
	foreach ($user_privs as $pname)
175
		if($priv_list[$pname])
176
			$privs[] = $priv_list[$pname];
177

    
178
	return $privs;
179
}
180

    
181
function isAllowedPage($page) {
182
	global $_SESSION;
183

    
184
	$username = $_SESSION['Username'];
185
	if (!isset($username))
186
		return false;
187

    
188
	/* admin/root access check */
189
	$user = getUserEntry($username);
190
	if (isset($user))
191
		if (isset($user['uid']))
192
			if ($user['uid']==0)
193
				return true;
194

    
195
	/* user privelege access check */
196
	if (cmp_page_matches($page, $_SESSION['page-match']))
197
		return true;
198

    
199
	return false;
200
}
201

    
202
function getPrivPages(& $entry, & $allowed_pages) {
203
	global $priv_list;
204

    
205
	if (!is_array($entry['priv']))
206
		return;
207

    
208
	foreach ($entry['priv'] as $pname) {
209
		if (strncmp($pname, "page-", 5))
210
			continue;
211
		$priv = &$priv_list[$pname];
212
		if (!is_array($priv))
213
			continue;
214
		$matches = &$priv['match'];
215
		if (!is_array($matches))
216
			continue;
217
		foreach ($matches as $match)
218
			$allowed_pages[] = $match;
219
	}
220
}
221

    
222
function getAllowedPages($username) {
223
	global $config, $_SESSION;
224

    
225
	if (!function_exists("ldap_connect"))
226
		return;
227
	
228
	$allowed_pages = array();
229
	$allowed_groups = array();
230
	
231
	$ldapon = $_SESSION['ldapon'];
232

    
233
	// search for a local user by name
234
	$local_user = getUserEntry($username);
235

    
236
	// obtain local groups if we have a local user
237
	if ($local_user) {
238
		$allowed_groups = local_user_get_groups($local_user);
239
		getPrivPages($local_user, $allowed_pages);
240
	}
241

    
242
	// obtain ldap groups if we are in ldap mode
243
	if ($config['system']['webgui']['backend'] == "ldap" && !$local_user)
244
		$allowed_groups = ldap_get_groups($username);
245

    
246
	// obtain ldapother groups if we are in ldap mode
247
	if ($config['system']['webgui']['backend'] == "ldapother" && !$local_user)
248
		$allowed_groups = ldap_get_groups($username);
249

    
250
	// build a list of allowed pages
251
	if (is_array($config['system']['group']) && is_array($allowed_groups))
252
		foreach ($config['system']['group'] as $group)
253
			if (in_array($group['name'], $allowed_groups))
254
				getPrivPages($group, $allowed_pages);
255

    
256
//	$dbg_pages = implode(",", $allowed_pages);
257
//	$dbg_groups = implode(",", $allowed_groups);
258
//	log_error("debug: user {$username} groups = {$dbg_groups}");
259
//	log_error("debug: user {$username} pages = {$dbg_pages}");
260

    
261
	$_SESSION['page-match'] = $allowed_pages;
262

    
263
	return $allowed_pages;
264
}
265

    
266
function userHasPrivilege($userent, $privid = false) {
267

    
268
	if (!$privid || !is_array($userent))
269
		return false;
270

    
271
	$privs = get_user_privileges($userent);
272

    
273
	if (!is_array($privs))
274
		return false;
275

    
276
	if (!in_array($privid, $privs))
277
		return false;
278

    
279
	return true;
280
}
281

    
282
?>
(25-25/39)