1 |
c38fee60
|
Scott Ullrich
|
<?php
|
2 |
|
|
require_once("config.inc");
|
3 |
|
|
require_once("functions.inc");
|
4 |
|
|
|
5 |
422e039b
|
Scott Ullrich
|
/* MiniUPnPd */
|
6 |
c38fee60
|
Scott Ullrich
|
|
7 |
422e039b
|
Scott Ullrich
|
define('UPNP_RCFILE', '/usr/local/etc/rc.d/miniupnpd.sh');
|
8 |
d86002ec
|
Ryan Wagoner
|
define('UPNP_CONFIG','/usr/local/etc/miniupnpd.conf');
|
9 |
422e039b
|
Scott Ullrich
|
|
10 |
|
|
function upnp_notice ($msg) { syslog(LOG_NOTICE, "miniupnpd: {$msg}"); return; }
|
11 |
|
|
function upnp_warn ($msg) { syslog(LOG_WARNING, "miniupnpd: {$msg}"); return; }
|
12 |
|
|
|
13 |
|
|
function upnp_action ($action) {
|
14 |
|
|
if (file_exists(UPNP_RCFILE))
|
15 |
|
|
mwexec(UPNP_RCFILE.' '.$action);
|
16 |
|
|
}
|
17 |
02d777de
|
Scott Ullrich
|
|
18 |
d86002ec
|
Ryan Wagoner
|
function upnp_running () {
|
19 |
|
|
if((int)exec("pgrep miniupnpd | wc -l") > 0)
|
20 |
|
|
return true;
|
21 |
966f579e
|
Ryan Wagoner
|
return false;
|
22 |
d86002ec
|
Ryan Wagoner
|
}
|
23 |
|
|
|
24 |
c38fee60
|
Scott Ullrich
|
function upnp_config ($name) {
|
25 |
|
|
global $config;
|
26 |
|
|
if($config['installedpackages']['miniupnpd']['config'][0]["{$name}"])
|
27 |
|
|
return $config['installedpackages']['miniupnpd']['config'][0]["{$name}"];
|
28 |
966f579e
|
Ryan Wagoner
|
return NULL;
|
29 |
c38fee60
|
Scott Ullrich
|
}
|
30 |
|
|
|
31 |
d86002ec
|
Ryan Wagoner
|
function upnp_write_config($conf_file, $conf_text) {
|
32 |
|
|
$conf = fopen($conf_file, "w");
|
33 |
|
|
if(!$conf) {
|
34 |
|
|
upnp_warn("Could not open {$conf_file} for writing.");
|
35 |
|
|
exit;
|
36 |
|
|
}
|
37 |
|
|
fwrite($conf, $conf_text);
|
38 |
|
|
fclose($conf);
|
39 |
966f579e
|
Ryan Wagoner
|
}
|
40 |
|
|
|
41 |
|
|
function upnp_uuid() {
|
42 |
|
|
/* md5 hash of wan mac */
|
43 |
|
|
$arp = explode(' ',exec('arp -an -i '.get_real_wan_interface()));
|
44 |
|
|
$uuid = md5($arp[3]);
|
45 |
|
|
/* put uuid in correct format 8-4-4-4-12 */
|
46 |
|
|
return substr($uuid,0,8)."-".substr($uuid,9,4)."-".substr($uuid,13,4)."-".substr($uuid,17,4)."-".substr($uuid,21,12);
|
47 |
|
|
}
|
48 |
d86002ec
|
Ryan Wagoner
|
|
49 |
|
|
function upnp_validate_ip($ip,$check_cdir) {
|
50 |
|
|
/* validate cdir */
|
51 |
|
|
if($check_cdir) {
|
52 |
|
|
$ip_array = explode("/",$ip);
|
53 |
|
|
if(count($ip_array) == 2) {
|
54 |
|
|
if($ip_array[1] < 1 || $ip_array[1] > 32)
|
55 |
|
|
return false;
|
56 |
|
|
} else
|
57 |
|
|
if(count($ip_array) != 1)
|
58 |
|
|
return false;
|
59 |
|
|
} else
|
60 |
|
|
$ip_array[] = $ip;
|
61 |
|
|
|
62 |
|
|
/* validate ip */
|
63 |
|
|
if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip_array[0]))
|
64 |
|
|
return false;
|
65 |
|
|
foreach(explode(".", $ip_array[0]) as $sub)
|
66 |
|
|
if($sub < 0 || $sub > 256)
|
67 |
|
|
return false;
|
68 |
|
|
return true;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
function upnp_validate_port($port) {
|
72 |
|
|
foreach(explode("-", $port) as $sub)
|
73 |
|
|
if($sub < 0 || $sub > 65535)
|
74 |
|
|
return false;
|
75 |
|
|
return true;
|
76 |
c38fee60
|
Scott Ullrich
|
}
|
77 |
02d777de
|
Scott Ullrich
|
|
78 |
c38fee60
|
Scott Ullrich
|
function before_form_miniupnpd($pkg) {
|
79 |
|
|
global $config;
|
80 |
02d777de
|
Scott Ullrich
|
|
81 |
422e039b
|
Scott Ullrich
|
config_lock();
|
82 |
|
|
|
83 |
c38fee60
|
Scott Ullrich
|
/* if shaper connection speed defined hide fields */
|
84 |
|
|
if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
|
85 |
|
|
$i=0;
|
86 |
|
|
foreach ($pkg['fields']['field'] as $field) {
|
87 |
|
|
if ($field['fieldname'] == 'download' || $field['fieldname'] == 'upload')
|
88 |
|
|
unset($pkg['fields']['field'][$i]);
|
89 |
|
|
$i++;
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
422e039b
|
Scott Ullrich
|
|
93 |
|
|
config_unlock();
|
94 |
c38fee60
|
Scott Ullrich
|
}
|
95 |
|
|
|
96 |
|
|
function validate_form_miniupnpd($post, $input_errors) {
|
97 |
|
|
if($post['iface_array'])
|
98 |
|
|
foreach($post['iface_array'] as $iface)
|
99 |
|
|
if($iface == "wan")
|
100 |
|
|
$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
|
101 |
d86002ec
|
Ryan Wagoner
|
if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
|
102 |
c38fee60
|
Scott Ullrich
|
$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
|
103 |
|
|
if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
|
104 |
|
|
$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
|
105 |
d86002ec
|
Ryan Wagoner
|
if($post['download'] && $post['download'] <= 0)
|
106 |
c38fee60
|
Scott Ullrich
|
$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
|
107 |
d86002ec
|
Ryan Wagoner
|
if($post['upload'] && $post['upload'] <= 0)
|
108 |
c38fee60
|
Scott Ullrich
|
$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
|
109 |
d86002ec
|
Ryan Wagoner
|
|
110 |
|
|
/* user permissions validation */
|
111 |
|
|
for($i=1; $i<=4; $i++) {
|
112 |
|
|
if($post["permuser{$i}"]) {
|
113 |
|
|
$perm = explode(' ',$post["permuser{$i}"]);
|
114 |
|
|
/* should explode to 4 args */
|
115 |
|
|
if(count($perm) != 4) {
|
116 |
|
|
$input_errors[] = "You must follow the specified format in the 'User specified permissions {$i}' field";
|
117 |
|
|
} else {
|
118 |
|
|
/* must with allow or deny */
|
119 |
|
|
if(!($perm[0] == 'allow' || $perm[0] == 'deny'))
|
120 |
|
|
$input_errors[] = "You must begin with allow or deny in the 'User specified permissions {$i}' field";
|
121 |
|
|
/* verify port or port range */
|
122 |
|
|
if(!upnp_validate_port($perm[1]) || !upnp_validate_port($perm[3]))
|
123 |
|
|
$input_errors[] = "You must specify a port or port range between 0 and 65535 in the 'User specified
|
124 |
|
|
permissions {$i}' field";
|
125 |
|
|
/* verify ip address */
|
126 |
|
|
if(!upnp_validate_ip($perm[2],true))
|
127 |
|
|
$input_errors[] = "You must specify a valid ip address in the 'User specified permissions {$i}' field";
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
c38fee60
|
Scott Ullrich
|
}
|
132 |
|
|
|
133 |
|
|
function sync_package_miniupnpd() {
|
134 |
|
|
global $config;
|
135 |
|
|
global $input_errors;
|
136 |
|
|
|
137 |
966f579e
|
Ryan Wagoner
|
config_lock();
|
138 |
d86002ec
|
Ryan Wagoner
|
|
139 |
|
|
$configtext = "ext_ifname=".get_real_wan_interface()."\n";
|
140 |
|
|
$configtext .= "port=2189\n";
|
141 |
c38fee60
|
Scott Ullrich
|
|
142 |
02d777de
|
Scott Ullrich
|
/* since config is written before this file invoked we don't need to read post data */
|
143 |
d86002ec
|
Ryan Wagoner
|
if(upnp_config('enable') && upnp_config('iface_array'))
|
144 |
|
|
$iface_array = explode(',',upnp_config('iface_array'));
|
145 |
c38fee60
|
Scott Ullrich
|
|
146 |
02d777de
|
Scott Ullrich
|
if($iface_array) {
|
147 |
c38fee60
|
Scott Ullrich
|
foreach($iface_array as $iface) {
|
148 |
|
|
$if = convert_friendly_interface_to_real_interface_name($iface);
|
149 |
|
|
/* above function returns iface if fail */
|
150 |
|
|
if($if!=$iface) {
|
151 |
|
|
$addr = find_interface_ip($if);
|
152 |
|
|
/* non enabled interfaces are displayed in list on miniupnpd settings page */
|
153 |
|
|
/* check that the interface has an ip address before adding parameters */
|
154 |
|
|
if($addr) {
|
155 |
d86002ec
|
Ryan Wagoner
|
$configtext .= "listening_ip={$addr}\n";
|
156 |
7fae6598
|
Ryan Wagoner
|
if(!$ifaces_active) {
|
157 |
|
|
$webgui_ip = $addr;
|
158 |
422e039b
|
Scott Ullrich
|
$ifaces_active = $iface;
|
159 |
7fae6598
|
Ryan Wagoner
|
} else {
|
160 |
422e039b
|
Scott Ullrich
|
$ifaces_active .= ", {$iface}";
|
161 |
7fae6598
|
Ryan Wagoner
|
}
|
162 |
c38fee60
|
Scott Ullrich
|
} else {
|
163 |
422e039b
|
Scott Ullrich
|
upnp_warn("Interface {$iface} has no ip address, ignoring");
|
164 |
c38fee60
|
Scott Ullrich
|
}
|
165 |
|
|
} else {
|
166 |
422e039b
|
Scott Ullrich
|
upnp_warn("Could not resolve real interface for {$iface}");
|
167 |
c38fee60
|
Scott Ullrich
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
d86002ec
|
Ryan Wagoner
|
if($ifaces_active) {
|
171 |
|
|
/* override wan ip address, common for carp, etc */
|
172 |
|
|
if(upnp_config('overridewanip'))
|
173 |
|
|
$configtext .= "ext_ip=".upnp_config('overridewanip')."\n";
|
174 |
02d777de
|
Scott Ullrich
|
|
175 |
c38fee60
|
Scott Ullrich
|
/* if shaper connection speed defined use those values */
|
176 |
|
|
if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
|
177 |
|
|
$download = $config['ezshaper']['step2']['download']*1000;
|
178 |
|
|
$upload = $config['ezshaper']['step2']['upload']*1000;
|
179 |
|
|
} else {
|
180 |
d86002ec
|
Ryan Wagoner
|
$download = upnp_config('download')*1000;
|
181 |
|
|
$upload = upnp_config('upload')*1000;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/* set upload and download bitrates */
|
185 |
|
|
if($download && $upload) {
|
186 |
|
|
$configtext .= "bitrate_down={$download}\n";
|
187 |
|
|
$configtext .= "bitrate_up={$upload}\n";
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/* enable logging of packets handled by miniupnpd rules */
|
191 |
|
|
if(upnp_config('logpackets'))
|
192 |
|
|
$configtext .= "packet_log=yes\n";
|
193 |
|
|
|
194 |
|
|
/* enable system uptime instead of miniupnpd uptime */
|
195 |
|
|
if(upnp_config('sysuptime'))
|
196 |
|
|
$configtext .= "system_uptime=yes\n";
|
197 |
|
|
|
198 |
7fae6598
|
Ryan Wagoner
|
/* set webgui url */
|
199 |
|
|
if($config['system']['webgui']['protocol']) {
|
200 |
966f579e
|
Ryan Wagoner
|
$configtext .= "presentation_url=".$config['system']['webgui']['protocol']."://{$webgui_ip}";
|
201 |
7fae6598
|
Ryan Wagoner
|
if($config['system']['webgui']['port'])
|
202 |
|
|
$configtext .= ":".$config['system']['webgui']['port'];
|
203 |
|
|
$configtext .= "/\n";
|
204 |
|
|
}
|
205 |
966f579e
|
Ryan Wagoner
|
|
206 |
|
|
/* set uuid and serial */
|
207 |
|
|
$configtext .= "uuid=".upnp_uuid()."\n";
|
208 |
|
|
$configtext .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
|
209 |
|
|
|
210 |
|
|
/* set model number */
|
211 |
|
|
$configtext .= "model_number=".exec("cat /etc/version")."\n";
|
212 |
7fae6598
|
Ryan Wagoner
|
|
213 |
d86002ec
|
Ryan Wagoner
|
/* upnp access restrictions */
|
214 |
|
|
for($i=1; $i<=4; $i++) {
|
215 |
|
|
if(upnp_config("permuser{$i}"))
|
216 |
|
|
$configtext .= upnp_config("permuser{$i}")."\n";
|
217 |
02d777de
|
Scott Ullrich
|
}
|
218 |
|
|
|
219 |
d86002ec
|
Ryan Wagoner
|
if(upnp_config('permdefault'))
|
220 |
|
|
$configtext .= "deny 0-65535 0.0.0.0/0 0-65535\n";
|
221 |
02d777de
|
Scott Ullrich
|
|
222 |
d86002ec
|
Ryan Wagoner
|
/* generate rc file start and stop */
|
223 |
c38fee60
|
Scott Ullrich
|
$stop = <<<EOD
|
224 |
|
|
if [ `pgrep miniupnpd | wc -l` != 0 ]; then
|
225 |
|
|
/usr/bin/killall miniupnpd
|
226 |
|
|
while [ `pgrep miniupnpd | wc -l` != 0 ]; do
|
227 |
|
|
sleep 1
|
228 |
02d777de
|
Scott Ullrich
|
done
|
229 |
c38fee60
|
Scott Ullrich
|
fi
|
230 |
|
|
# Clear existing rules and rdr entries
|
231 |
|
|
if [ `pfctl -aminiupnpd -sr | wc -l` != 0 ]; then
|
232 |
|
|
/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null
|
233 |
|
|
fi
|
234 |
|
|
if [ `pfctl -aminiupnpd -sn | wc -l` != 0 ]; then
|
235 |
|
|
/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null
|
236 |
|
|
fi
|
237 |
|
|
EOD;
|
238 |
d86002ec
|
Ryan Wagoner
|
$start = $stop."\n\t/usr/local/sbin/miniupnpd -f ".UPNP_CONFIG;
|
239 |
02d777de
|
Scott Ullrich
|
|
240 |
d86002ec
|
Ryan Wagoner
|
/* write out the configuration */
|
241 |
422e039b
|
Scott Ullrich
|
conf_mount_rw();
|
242 |
d86002ec
|
Ryan Wagoner
|
upnp_write_config(UPNP_CONFIG,$configtext);
|
243 |
c38fee60
|
Scott Ullrich
|
write_rcfile(array(
|
244 |
d86002ec
|
Ryan Wagoner
|
'file' => 'miniupnpd.sh',
|
245 |
|
|
'start' => $start,
|
246 |
|
|
'stop' => $stop
|
247 |
c38fee60
|
Scott Ullrich
|
)
|
248 |
|
|
);
|
249 |
422e039b
|
Scott Ullrich
|
conf_mount_ro();
|
250 |
|
|
|
251 |
|
|
/* if miniupnpd not running start it */
|
252 |
37f54a71
|
Ryan Wagoner
|
if(!upnp_running()) {
|
253 |
422e039b
|
Scott Ullrich
|
upnp_notice("Starting service on interface: {$ifaces_active}");
|
254 |
d86002ec
|
Ryan Wagoner
|
upnp_action('start');
|
255 |
422e039b
|
Scott Ullrich
|
}
|
256 |
|
|
/* or restart miniupnpd if settings were changed */
|
257 |
|
|
elseif($_POST['iface_array']) {
|
258 |
|
|
upnp_notice("Restarting service on interface: {$ifaces_active}");
|
259 |
d86002ec
|
Ryan Wagoner
|
upnp_action('restart');
|
260 |
02d777de
|
Scott Ullrich
|
}
|
261 |
|
|
}
|
262 |
c38fee60
|
Scott Ullrich
|
}
|
263 |
|
|
|
264 |
d86002ec
|
Ryan Wagoner
|
if(!$iface_array || !$ifaces_active) {
|
265 |
c38fee60
|
Scott Ullrich
|
/* no parameters user does not want miniupnpd running */
|
266 |
|
|
/* lets stop the service and remove the rc file */
|
267 |
02d777de
|
Scott Ullrich
|
|
268 |
422e039b
|
Scott Ullrich
|
if(file_exists(UPNP_RCFILE)) {
|
269 |
d86002ec
|
Ryan Wagoner
|
if(!upnp_config('enable'))
|
270 |
|
|
upnp_notice('Stopping service: miniupnpd disabled');
|
271 |
422e039b
|
Scott Ullrich
|
else
|
272 |
d86002ec
|
Ryan Wagoner
|
upnp_notice('Stopping service: no interfaces selected');
|
273 |
422e039b
|
Scott Ullrich
|
|
274 |
d86002ec
|
Ryan Wagoner
|
upnp_action('stop');
|
275 |
422e039b
|
Scott Ullrich
|
|
276 |
|
|
conf_mount_rw();
|
277 |
|
|
unlink(UPNP_RCFILE);
|
278 |
d86002ec
|
Ryan Wagoner
|
unlink(UPNP_CONFIG);
|
279 |
422e039b
|
Scott Ullrich
|
conf_mount_ro();
|
280 |
|
|
}
|
281 |
c38fee60
|
Scott Ullrich
|
}
|
282 |
02d777de
|
Scott Ullrich
|
|
283 |
c38fee60
|
Scott Ullrich
|
config_unlock();
|
284 |
|
|
}
|
285 |
422e039b
|
Scott Ullrich
|
?>
|