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