Project

General

Profile

Download (4.99 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
<?php
3
/*
4
 * generate-privdefs.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
/*
26
 * This utility processes the <prefix>/usr/local/www
27
 * directory and builds a privilege definition file
28
 * based on the embedded metadata tags. For more info
29
 * please see <prefix>/etc/inc/meta.inc
30
 */
31

    
32
if (count($argv) < 2) {
33
	echo "usage: generate-privdefs <prefix>\n";
34
	echo "\n";
35
	echo "This utility generates privilege definitions and writes them to\n";
36
	echo "'<prefix>/etc/inc/priv.defs.inc'. The <prefix> parameter should\n";
37
	echo "be specified as your base pfSense working directory.\n";
38
	echo "\n";
39
	echo "Examples:\n";
40
	echo "#generate-privdefs /\n";
41
	echo "#generate-privdefs /home/pfsense/src/\n";
42
	echo "\n";
43
	exit -1;
44
}
45

    
46
$prefix = $argv[1];
47
if (!file_exists($prefix)) {
48
	echo "prefix {$prefix} is invalid";
49
	exit -1;
50
}
51

    
52
$metainc = $prefix."etc/inc/meta.inc";
53

    
54
if (!file_exists($metainc)) {
55
	echo "unable to locate {$metainc} file\n";
56
	exit -1;
57
}
58

    
59
require_once($metainc);
60

    
61
echo "--Locating www php files--\n";
62

    
63
$path = $prefix."/usr/local/www";
64
list_phpfiles($path, $found);
65

    
66
echo "--Gathering privilege metadata--\n";
67

    
68
$data;
69
sort($found);
70
foreach ($found as $fname)
71
	read_file_metadata($path."/".$fname, $data, "PRIV");
72

    
73
echo "--Generating privilege definitions--\n";
74
$privdef = $prefix."etc/inc/priv.defs.inc";
75

    
76
$fp = fopen($privdef, "w");
77
if (!$fp) {
78
	echo "unable to open {$privdef}\n";
79
	exit -2;
80
}
81

    
82
$pdata;
83
$pdata  = "<?php\n";
84
$pdata .= "/*\n";
85
$pdata .= " * priv.defs.inc - Default Privilege Definitions\n";
86
$pdata .= " * Generated by pfSense/tools/scripts/generate-privdefs.php\n";
87
$pdata .= " *\n";
88
$pdata .= " * ***************************************************\n";
89
$pdata .= " * DO NOT EDIT THIS FILE. IT IS GENERATED BY A SCRIPT.\n";
90
$pdata .= " * ***************************************************\n";
91
$pdata .= " *\n";
92
$pdata .= " * Text is pulled from metadata headers in the referenced files.\n";
93
$pdata .= " *\n";
94
$pdata .= " */\n";
95
$pdata .= "\n";
96
$pdata .= "\$priv_list = array();\n";
97
$pdata .= "\n";
98
$pdata .= "\$priv_list['page-all'] = array();\n";
99
$pdata .= "\$priv_list['page-all']['name'] = gettext(\"WebCfg - All pages\");\n";
100
$pdata .= "\$priv_list['page-all']['descr'] = gettext(\"Allow access to all pages\");\n";
101
$pdata .= "\$priv_list['page-all']['warn'] = \"standard-warning-root\";\n";
102
$pdata .= "\$priv_list['page-all']['match'] = array();\n";
103
$pdata .= "\$priv_list['page-all']['match'][] = \"*\";\n";
104
$pdata .= "\n";
105

    
106
foreach ($data as $fname => $tags) {
107

    
108
	foreach ($tags as $tname => $vals) {
109

    
110
		$ident = "";
111
		$name = "";
112
		$descr = "";
113
		$warn = "";
114
		$match = array();
115

    
116
		foreach ($vals as $vname => $vlist) {
117

    
118
			switch ($vname) {
119
				case "IDENT":
120
					$ident = $vlist[0];
121
					break;
122
				case "NAME":
123
					$name = $vlist[0];
124
					break;
125
				case "DESCR":
126
					$descr = $vlist[0];
127
					break;
128
				case "WARN":
129
					$warn = $vlist[0];
130
					break;
131
				case "MATCH":
132
					$match = $vlist;
133
					break;
134
			}
135
		}
136

    
137
		if (!$ident) {
138
			echo "invalid IDENT in {$fname} privilege\n";
139
			continue;
140
		}
141

    
142
		if (!count($match)) {
143
			echo "invalid MATCH in {$fname} privilege\n";
144
			continue;
145
		}
146

    
147
		$pdata .= "\$priv_list['{$ident}'] = array();\n";
148
		$pdata .= "\$priv_list['{$ident}']['name'] = gettext(\"WebCfg - {$name}\");\n";
149
		$pdata .= "\$priv_list['{$ident}']['descr'] = gettext(\"{$descr}\");\n";
150

    
151
		if (strlen($warn) > 0) {
152
			$pdata .= "\$priv_list['{$ident}']['warn'] = \"{$warn}\";\n";
153
		}
154

    
155
		$pdata .= "\$priv_list['{$ident}']['match'] = array();\n";
156

    
157
		foreach ($match as $url)
158
			$pdata .= "\$priv_list['{$ident}']['match'][] = \"{$url}\";\n";
159

    
160
		$pdata .= "\n";
161
	}
162
}
163

    
164
$pdata .= "\n";
165
$pdata .= "\$priv_rmvd = array();\n";
166
$pdata .= "\n";
167

    
168
$pdata .= "?>\n";
169
fwrite($fp, $pdata);
170

    
171
fclose($fp);
172

    
173
/*
174
 * TODO : Build additional functionality
175
 *
176

    
177
echo "--Checking for pages without privilege definitions--\n";
178

    
179
foreach ($found as $fname) {
180
	$match = false;
181
	foreach ($pages_current as $pname => $pdesc) {
182
		if (!strcmp($pname, $fname)) {
183
			$match = true;
184
			break;
185
		}
186
	}
187
	if (!$match)
188
		echo "missing: $fname\n";
189
}
190

    
191
echo "--Checking for stale privilege definitions--\n";
192

    
193
foreach ($pages_current as $pname => $pdesc) {
194
	$match = false;
195
	foreach ($found as $fname) {
196
		if (!strncmp($fname, $pname, strlen($fname))) {
197
			$match = true;
198
			break;
199
		}
200
	}
201
	if (!$match)
202
		echo "stale: $pname\n";
203
}
204

    
205
 */
206

    
207
?>
(1-1/2)