Project

General

Profile

Download (7.09 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
			$result = strncmp($page, $match, $wcpos);
127

    
128
		if (!$result)
129
			return true;
130
	}
131

    
132
	return false;
133
}
134

    
135
function map_page_privname($page) {
136
	global $priv_list;
137

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

    
148
	return false;
149
}
150

    
151
function get_user_privdesc(& $user) {
152
	global $priv_list;
153

    
154
	$privs = array();
155

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

    
160
	$names = local_user_get_groups($user, true);
161

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

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

    
182
	return $privs;
183
}
184

    
185
function isAllowed($username, $page) {
186
	global $_SESSION;
187

    
188
	if (!isset($username))
189
		return false;
190

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

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

    
202
	return false;
203
}
204

    
205

    
206
function isAllowedPage($page) {
207
	global $_SESSION;
208

    
209

    
210
	$username = $_SESSION['Username'];
211

    
212
	if (!isset($username))
213
		return false;
214

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

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

    
226
	return false;
227
}
228

    
229
function getPrivPages(& $entry, & $allowed_pages) {
230
	global $priv_list;
231

    
232
	if (!is_array($entry['priv']))
233
		return;
234

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

    
249
function getAllowedPages($username) {
250
	global $config, $_SESSION;
251

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

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

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

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

    
283
	$_SESSION['page-match'] = $allowed_pages;
284

    
285
	return $allowed_pages;
286
}
287

    
288
?>
(39-39/61)