1
|
<?php
|
2
|
require_once("config.inc");
|
3
|
require_once("functions.inc");
|
4
|
|
5
|
/* MiniUPnPd */
|
6
|
|
7
|
define('UPNP_RCFILE', '/usr/local/etc/rc.d/miniupnpd.sh');
|
8
|
define('UPNP_CONFIG','/usr/local/etc/miniupnpd.conf');
|
9
|
|
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
|
|
18
|
function upnp_running () {
|
19
|
if((int)exec("pgrep miniupnpd | wc -l") > 0)
|
20
|
return true;
|
21
|
return false;
|
22
|
}
|
23
|
|
24
|
function upnp_config ($name) {
|
25
|
global $config;
|
26
|
if($config['installedpackages']['miniupnpd']['config'][0]["{$name}"])
|
27
|
return $config['installedpackages']['miniupnpd']['config'][0]["{$name}"];
|
28
|
return NULL;
|
29
|
}
|
30
|
|
31
|
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
|
}
|
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
|
|
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
|
}
|
77
|
|
78
|
function before_form_miniupnpd($pkg) {
|
79
|
global $config;
|
80
|
|
81
|
config_lock();
|
82
|
|
83
|
/* 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
|
|
93
|
config_unlock();
|
94
|
}
|
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
|
if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
|
102
|
$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
|
if($post['download'] && $post['download'] <= 0)
|
106
|
$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
|
107
|
if($post['upload'] && $post['upload'] <= 0)
|
108
|
$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
|
109
|
|
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
|
}
|
132
|
|
133
|
function sync_package_miniupnpd() {
|
134
|
global $config;
|
135
|
global $input_errors;
|
136
|
|
137
|
config_lock();
|
138
|
|
139
|
$configtext = "ext_ifname=".get_real_wan_interface()."\n";
|
140
|
$configtext .= "port=2189\n";
|
141
|
|
142
|
/* since config is written before this file invoked we don't need to read post data */
|
143
|
if(upnp_config('enable') && upnp_config('iface_array'))
|
144
|
$iface_array = explode(',',upnp_config('iface_array'));
|
145
|
|
146
|
if($iface_array) {
|
147
|
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
|
$configtext .= "listening_ip={$addr}\n";
|
156
|
if(!$ifaces_active) {
|
157
|
$webgui_ip = $addr;
|
158
|
$ifaces_active = $iface;
|
159
|
} else {
|
160
|
$ifaces_active .= ", {$iface}";
|
161
|
}
|
162
|
} else {
|
163
|
upnp_warn("Interface {$iface} has no ip address, ignoring");
|
164
|
}
|
165
|
} else {
|
166
|
upnp_warn("Could not resolve real interface for {$iface}");
|
167
|
}
|
168
|
}
|
169
|
|
170
|
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
|
|
175
|
/* 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
|
$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
|
/* set webgui url */
|
199
|
if($config['system']['webgui']['protocol']) {
|
200
|
$configtext .= "presentation_url=".$config['system']['webgui']['protocol']."://{$webgui_ip}";
|
201
|
if($config['system']['webgui']['port'])
|
202
|
$configtext .= ":".$config['system']['webgui']['port'];
|
203
|
$configtext .= "/\n";
|
204
|
}
|
205
|
|
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
|
|
213
|
/* upnp access restrictions */
|
214
|
for($i=1; $i<=4; $i++) {
|
215
|
if(upnp_config("permuser{$i}"))
|
216
|
$configtext .= upnp_config("permuser{$i}")."\n";
|
217
|
}
|
218
|
|
219
|
if(upnp_config('permdefault'))
|
220
|
$configtext .= "deny 0-65535 0.0.0.0/0 0-65535\n";
|
221
|
|
222
|
/* generate rc file start and stop */
|
223
|
$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
|
done
|
229
|
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
|
$start = $stop."\n\t/usr/local/sbin/miniupnpd -f ".UPNP_CONFIG;
|
239
|
|
240
|
/* write out the configuration */
|
241
|
conf_mount_rw();
|
242
|
upnp_write_config(UPNP_CONFIG,$configtext);
|
243
|
write_rcfile(array(
|
244
|
'file' => 'miniupnpd.sh',
|
245
|
'start' => $start,
|
246
|
'stop' => $stop
|
247
|
)
|
248
|
);
|
249
|
conf_mount_ro();
|
250
|
|
251
|
/* if miniupnpd not running start it */
|
252
|
if(!upnp_running()) {
|
253
|
upnp_notice("Starting service on interface: {$ifaces_active}");
|
254
|
upnp_action('start');
|
255
|
}
|
256
|
/* or restart miniupnpd if settings were changed */
|
257
|
elseif($_POST['iface_array']) {
|
258
|
upnp_notice("Restarting service on interface: {$ifaces_active}");
|
259
|
upnp_action('restart');
|
260
|
}
|
261
|
}
|
262
|
}
|
263
|
|
264
|
if(!$iface_array || !$ifaces_active) {
|
265
|
/* no parameters user does not want miniupnpd running */
|
266
|
/* lets stop the service and remove the rc file */
|
267
|
|
268
|
if(file_exists(UPNP_RCFILE)) {
|
269
|
if(!upnp_config('enable'))
|
270
|
upnp_notice('Stopping service: miniupnpd disabled');
|
271
|
else
|
272
|
upnp_notice('Stopping service: no interfaces selected');
|
273
|
|
274
|
upnp_action('stop');
|
275
|
|
276
|
conf_mount_rw();
|
277
|
unlink(UPNP_RCFILE);
|
278
|
unlink(UPNP_CONFIG);
|
279
|
conf_mount_ro();
|
280
|
}
|
281
|
}
|
282
|
|
283
|
config_unlock();
|
284
|
}
|
285
|
?>
|