Project

General

Profile

Download (4.9 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-2020 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

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

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

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

    
50
$metainc = $prefix."etc/inc/meta.inc";
51

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

    
57
require_once($metainc);
58

    
59
echo "--Locating www php files--\n";
60

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

    
64
echo "--Gathering privilege metadata--\n";
65

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

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

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

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

    
104
foreach ($data as $fname => $tags) {
105

    
106
	foreach ($tags as $tname => $vals) {
107

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

    
114
		foreach ($vals as $vname => $vlist) {
115

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

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

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

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

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

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

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

    
158
		$pdata .= "\n";
159
	}
160
}
161

    
162
$pdata .= "\n";
163
$pdata .= "\$priv_rmvd = array();\n";
164
$pdata .= "\n";
165

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

    
169
fclose($fp);
170

    
171
/*
172
 * TODO : Build additional functionality
173
 *
174

    
175
echo "--Checking for pages without privilege definitions--\n";
176

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

    
189
echo "--Checking for stale privilege definitions--\n";
190

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

    
203
 */
204

    
205
?>
(1-1/2)