Project

General

Profile

Download (7.47 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
/*
44
	pfSense_MODULE:	auth
45
*/
46

    
47
require_once("priv.defs.inc");
48

    
49
/* Load and process custom privs. */
50
function get_priv_files($directory) {
51
	$dir_array = array();
52
	if(!is_dir($directory)) 
53
		return;
54
	if ($dh = opendir($directory)) {
55
		while (($file = readdir($dh)) !== false) {
56
			$canadd = 0;
57
			if($file == ".") 
58
				$canadd = 1;
59
			if($file == "..") 
60
				$canadd = 1;
61
			if($canadd == 0)
62
				array_push($dir_array, $file);
63
		}
64
		closedir($dh);
65
	}
66
	if(!is_array($dir_array))
67
		return;
68
	return $dir_array;
69
}
70

    
71
// Load and sort privs
72
$dir_array = get_priv_files("/etc/inc/priv");
73
foreach ($dir_array as $file) 
74
	if (!is_dir("/etc/inc/priv/{$file}") && stristr($file,".inc")) 
75
		include("/etc/inc/priv/{$file}");
76
if(is_dir("/usr/local/pkg/priv")) {
77
	$dir_array = get_priv_files("/usr/local/pkg/priv");
78
	foreach ($dir_array as $file) 
79
		if (!is_dir("/usr/local/pkg/priv/{$file}") && stristr($file,".inc")) 
80
			include("/usr/local/pkg/priv/{$file}");
81
}
82

    
83
if(is_array($priv_list))
84
	sort_privs($priv_list);
85

    
86
function cmp_privkeys($a, $b) {
87
	/* user privs at the top */
88
	$auser = strncmp("user-", $a, 5);
89
	$buser = strncmp("user-", $b, 5);
90
	if($auser != $buser)
91
		return $auser - $buser;
92

    
93
	/* name compare others */
94
	return strcasecmp($a, $b);
95
}
96

    
97
function sort_privs(& $privs) {
98
	uksort($privs, "cmp_privkeys");
99
}
100

    
101
function cmp_page_matches($page, & $matches, $fullwc = true) {
102

    
103
//	$dbg_matches = implode(",", $matches);
104
//	log_error("debug: checking page {$page} match with {$dbg_matches}");
105

    
106
	if (!is_array($matches))
107
		return false;
108

    
109
	/* skip any leading fwdslash */
110
	$test = strpos($page, "/");
111
	if ($test !== false && $test == 0)
112
		$page = substr($page, 1);
113

    
114
	/* look for a match */
115
	foreach ($matches as $match) {
116

    
117
		/* possibly ignore full wildcard match */
118
		if (!$fullwc && !strcmp($match ,"*"))
119
			continue;
120

    
121
		/* compare exact or wildcard match */
122
		$wcpos = strpos($match, "*");
123
		if ($wcpos === false)
124
			$result = strcmp($page, $match);
125
		else {
126
			$match = "/" . str_replace(array(".", "*"), array("\.", ".*"), $match) . "/";
127
			$result = !preg_match($match, $page);
128
		}
129

    
130
		if (!$result)
131
			return true;
132
	}
133

    
134
	return false;
135
}
136

    
137
function map_page_privname($page) {
138
	global $priv_list;
139

    
140
	foreach ($priv_list as $pname => $pdata) {
141
		if (strncmp($pname, "page-", 5))
142
			continue;
143
		$fullwc = false;
144
		if (!strcasecmp($page,"any")||!strcmp($page,"*"))
145
			$fullwc = true;
146
		if (cmp_page_matches($page, $pdata['match'], $fullwc))
147
			return $pname;
148
	}
149

    
150
	return false;
151
}
152

    
153
function get_user_privdesc(& $user) {
154
	global $priv_list;
155

    
156
	$privs = array();
157

    
158
	$user_privs = $user['priv'];
159
	if (!is_array($user_privs))
160
		$user_privs = array();
161

    
162
	$names = local_user_get_groups($user, true);
163

    
164
	foreach ($names as $name) {
165
		$group = getGroupEntry($name);
166
		$group_privs = $group['priv'];
167
		if (!is_array($group_privs))
168
			continue;
169
		foreach ($group_privs as $pname) {
170
			if (in_array($pname,$user_privs))
171
				continue;
172
			if (!$priv_list[$pname])
173
				continue;
174
			$priv = $priv_list[$pname];
175
			$priv['group'] = $group['name'];
176
			$privs[] = $priv;
177
		}
178
	}
179

    
180
	foreach ($user_privs as $pname)
181
		if($priv_list[$pname])
182
			$privs[] = $priv_list[$pname];
183

    
184
	return $privs;
185
}
186

    
187
function isAllowed($username, $page) {
188
	global $_SESSION;
189

    
190
	if (!isset($username))
191
		return false;
192

    
193
	/* admin/root access check */
194
	$user = getUserEntry($username);
195
	if (isset($user))
196
		if (isset($user['uid']))
197
			if ($user['uid']==0)
198
				return true;
199

    
200
	/* user privelege access check */
201
	if (cmp_page_matches($page, $_SESSION['page-match']))
202
		return true;
203

    
204
	return false;
205
}
206

    
207

    
208
function isAllowedPage($page) {
209
	global $_SESSION;
210

    
211

    
212
	$username = $_SESSION['Username'];
213

    
214
	if (!isset($username))
215
		return false;
216

    
217
	/* admin/root access check */
218
	$user = getUserEntry($username);
219
	if (isset($user))
220
		if (isset($user['uid']))
221
			if ($user['uid']==0)
222
				return true;
223

    
224
	/* user privelege access check */
225
	if (cmp_page_matches($page, $_SESSION['page-match']))
226
		return true;
227

    
228
	return false;
229
}
230

    
231
function getPrivPages(& $entry, & $allowed_pages) {
232
	global $priv_list;
233

    
234
	if (!is_array($entry['priv']))
235
		return;
236

    
237
	foreach ($entry['priv'] as $pname) {
238
		if (strncmp($pname, "page-", 5))
239
			continue;
240
		$priv = &$priv_list[$pname];
241
		if (!is_array($priv))
242
			continue;
243
		$matches = &$priv['match'];
244
		if (!is_array($matches))
245
			continue;
246
		foreach ($matches as $match)
247
			$allowed_pages[] = $match;
248
	}
249
}
250

    
251
function getAllowedPages($username) {
252
	global $config, $_SESSION;
253

    
254
	if (!function_exists("ldap_connect"))
255
		return;
256
	
257
	$allowed_pages = array();
258
	$allowed_groups = array();
259
	
260
	$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
261
	// obtain ldap groups if we are in ldap mode
262
	if ($authcfg['type'] == "ldap")
263
		$allowed_groups = @ldap_get_groups($username, $authcfg);
264
	else {
265
		// search for a local user by name
266
		$local_user = getUserEntry($username);
267
		getPrivPages($local_user, $allowed_pages);
268

    
269
		// obtain local groups if we have a local user
270
		if ($local_user)
271
			$allowed_groups = local_user_get_groups($local_user);
272
	}
273

    
274
	// build a list of allowed pages
275
	if (is_array($config['system']['group']) && is_array($allowed_groups))
276
		foreach ($config['system']['group'] as $group)
277
			if (in_array($group['name'], $allowed_groups))
278
				getPrivPages($group, $allowed_pages);
279

    
280
//	$dbg_pages = implode(",", $allowed_pages);
281
//	$dbg_groups = implode(",", $allowed_groups);
282
//	log_error("debug: user {$username} groups = {$dbg_groups}");
283
//	log_error("debug: user {$username} pages = {$dbg_pages}");
284

    
285
	$_SESSION['page-match'] = $allowed_pages;
286

    
287
	return $allowed_pages;
288
}
289

    
290
function sort_user_privs($privs) {
291
	// Privileges to place first, to redirect properly.
292
	$priority_privs = array("page-dashboard-all", "page-system-login/logout");
293

    
294
	$fprivs = array_intersect($privs, $priority_privs);
295
	$sprivs  = array_diff($privs, $priority_privs);
296

    
297
	return array_merge($fprivs, $sprivs);
298
}
299
?>
(39-39/61)