1
|
#!/usr/local/bin/php -f
|
2
|
<?php
|
3
|
/* ====================================================================
|
4
|
* Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
|
5
|
*
|
6
|
* Redistribution and use in source and binary forms, with or without
|
7
|
* modification, are permitted provided that the following conditions are met:
|
8
|
*
|
9
|
* 1. Redistributions of source code must retain the above copyright notice,
|
10
|
* this list of conditions and the following disclaimer.
|
11
|
*
|
12
|
* 2. Redistributions in binary form must reproduce the above copyright
|
13
|
* notice, this list of conditions and the following disclaimer in
|
14
|
* the documentation and/or other materials provided with the
|
15
|
* distribution.
|
16
|
*
|
17
|
* 3. All advertising materials mentioning features or use of this software
|
18
|
* must display the following acknowledgment:
|
19
|
* "This product includes software developed by the pfSense Project
|
20
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
21
|
*
|
22
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
23
|
* endorse or promote products derived from this software without
|
24
|
* prior written permission. For written permission, please contact
|
25
|
* coreteam@pfsense.org.
|
26
|
*
|
27
|
* 5. Products derived from this software may not be called "pfSense"
|
28
|
* nor may "pfSense" appear in their names without prior written
|
29
|
* permission of the Electric Sheep Fencing, LLC.
|
30
|
*
|
31
|
* 6. Redistributions of any form whatsoever must retain the following
|
32
|
* acknowledgment:
|
33
|
*
|
34
|
* "This product includes software developed by the pfSense Project
|
35
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
36
|
*
|
37
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
38
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
39
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
40
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
41
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
42
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
43
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
44
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
45
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
46
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
47
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
48
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
49
|
* ====================================================================
|
50
|
*/
|
51
|
|
52
|
/*
|
53
|
* This utility processes the <prefix>/usr/local/www
|
54
|
* directory and builds a privilege definition file
|
55
|
* based on the embedded metadata tags. For more info
|
56
|
* please see <prefix>/etc/inc/meta.inc
|
57
|
*/
|
58
|
|
59
|
if (count($argv) < 2) {
|
60
|
echo "usage: generate-privdefs <prefix>\n";
|
61
|
echo "\n";
|
62
|
echo "This utility generates privilege definitions and writes them to\n";
|
63
|
echo "'<prefix>/etc/inc/priv.defs.inc'. The <prefix> parameter should\n";
|
64
|
echo "be specified as your base pfSense working directory.\n";
|
65
|
echo "\n";
|
66
|
echo "Examples:\n";
|
67
|
echo "#generate-privdefs /\n";
|
68
|
echo "#generate-privdefs /home/pfsense/RELENG_1/pfSense/\n";
|
69
|
echo "\n";
|
70
|
exit -1;
|
71
|
}
|
72
|
|
73
|
$prefix = $argv[1];
|
74
|
if (!file_exists($prefix)) {
|
75
|
echo "prefix {$prefix} is invalid";
|
76
|
exit -1;
|
77
|
}
|
78
|
|
79
|
$metainc = $prefix."etc/inc/meta.inc";
|
80
|
|
81
|
if (!file_exists($metainc)) {
|
82
|
echo "unable to locate {$metainc} file\n";
|
83
|
exit -1;
|
84
|
}
|
85
|
|
86
|
require_once($metainc);
|
87
|
|
88
|
echo "--Locating www php files--\n";
|
89
|
|
90
|
$path = $prefix."/usr/local/www";
|
91
|
list_phpfiles($path, $found);
|
92
|
|
93
|
echo "--Gathering privilege metadata--\n";
|
94
|
|
95
|
$data;
|
96
|
foreach ($found as $fname)
|
97
|
read_file_metadata($path."/".$fname, $data, "PRIV");
|
98
|
|
99
|
echo "--Generating privilege definitions--\n";
|
100
|
$privdef = $prefix."etc/inc/priv.defs.inc";
|
101
|
|
102
|
$fp = fopen($privdef,"w");
|
103
|
if (!$fp) {
|
104
|
echo "unable to open {$privdef}\n";
|
105
|
exit -2;
|
106
|
}
|
107
|
|
108
|
$pdata;
|
109
|
$pdata = "<?php\n";
|
110
|
$pdata .= "/*\n";
|
111
|
$pdata .= " * priv.defs.inc - Generated privilege definitions\n";
|
112
|
$pdata .= " *\n";
|
113
|
$pdata .= " */\n";
|
114
|
$pdata .= "\n";
|
115
|
$pdata .= "\$priv_list = array();\n";
|
116
|
$pdata .= "\n";
|
117
|
$pdata .= "\$priv_list['page-all'] = array();\n";
|
118
|
$pdata .= "\$priv_list['page-all']['name'] = \"WebCfg - All pages\";\n";
|
119
|
$pdata .= "\$priv_list['page-all']['descr'] = \"Allow access to all pages\";\n";
|
120
|
$pdata .= "\$priv_list['page-all']['match'] = array();\n";
|
121
|
$pdata .= "\$priv_list['page-all']['match'][] = \"*\";\n";
|
122
|
$pdata .= "\n";
|
123
|
|
124
|
foreach ($data as $fname => $tags) {
|
125
|
|
126
|
foreach ($tags as $tname => $vals) {
|
127
|
|
128
|
$ident = "";
|
129
|
$name = "";
|
130
|
$descr = "";
|
131
|
$match = array();
|
132
|
|
133
|
foreach ($vals as $vname => $vlist) {
|
134
|
|
135
|
switch ($vname) {
|
136
|
case "IDENT":
|
137
|
$ident = $vlist[0];
|
138
|
break;
|
139
|
case "NAME":
|
140
|
$name = $vlist[0];
|
141
|
break;
|
142
|
case "DESCR":
|
143
|
$descr = $vlist[0];
|
144
|
break;
|
145
|
case "MATCH":
|
146
|
$match = $vlist;
|
147
|
break;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
if (!$ident) {
|
152
|
echo "invalid IDENT in {$fname} privilege\n";
|
153
|
continue;
|
154
|
}
|
155
|
|
156
|
if (!count($match)) {
|
157
|
echo "invalid MATCH in {$fname} privilege\n";
|
158
|
continue;
|
159
|
}
|
160
|
|
161
|
$pdata .= "\$priv_list['{$ident}'] = array();\n";
|
162
|
$pdata .= "\$priv_list['{$ident}']['name'] = \"WebCfg - {$name}\";\n";
|
163
|
$pdata .= "\$priv_list['{$ident}']['descr'] = \"{$descr}\";\n";
|
164
|
$pdata .= "\$priv_list['{$ident}']['match'] = array();\n";
|
165
|
|
166
|
foreach ($match as $url)
|
167
|
$pdata .= "\$priv_list['{$ident}']['match'][] = \"{$url}\";\n";
|
168
|
|
169
|
$pdata .= "\n";
|
170
|
}
|
171
|
}
|
172
|
|
173
|
$pdata .= "\n";
|
174
|
$pdata .= "\$priv_rmvd = array();\n";
|
175
|
$pdata .= "\n";
|
176
|
|
177
|
$pdata .= "?>\n";
|
178
|
fwrite($fp, $pdata);
|
179
|
|
180
|
fclose($fp);
|
181
|
|
182
|
/*
|
183
|
* TODO : Build additional functionality
|
184
|
*
|
185
|
|
186
|
echo "--Checking for pages without privilege definitions--\n";
|
187
|
|
188
|
foreach ($found as $fname) {
|
189
|
$match = false;
|
190
|
foreach ($pages_current as $pname => $pdesc) {
|
191
|
if (!strcmp($pname,$fname)) {
|
192
|
$match = true;
|
193
|
break;
|
194
|
}
|
195
|
}
|
196
|
if (!$match)
|
197
|
echo "missing: $fname\n";
|
198
|
}
|
199
|
|
200
|
echo "--Checking for stale privilege definitions--\n";
|
201
|
|
202
|
foreach ($pages_current as $pname => $pdesc) {
|
203
|
$match = false;
|
204
|
foreach ($found as $fname) {
|
205
|
if (!strncmp($fname,$pname,strlen($fname))) {
|
206
|
$match = true;
|
207
|
break;
|
208
|
}
|
209
|
}
|
210
|
if (!$match)
|
211
|
echo "stale: $pname\n";
|
212
|
}
|
213
|
|
214
|
*/
|
215
|
|
216
|
?>
|