Commit ea0beafb authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) restructure / cleanup miniupnpd.inc

(cherry picked from commit 7637bd11)
parent 4aaaefc2
<?php <?php
function upnp_notice ($msg) { log_error("miniupnpd: {$msg}"); }
function upnp_warn ($msg) { log_error("miniupnpd: {$msg}"); }
function upnp_running () { function upnp_running () {
if((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0) if ((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0) {
return true; return true;
}
return false; return false;
} }
function upnp_start() function upnp_start() {
{
if (file_exists('/var/etc/miniupnpd.conf')) { if (file_exists('/var/etc/miniupnpd.conf')) {
@unlink('/var/run/miniupnpd.pid'); @unlink('/var/run/miniupnpd.pid');
mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P /var/run/miniupnpd.pid'); mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P /var/run/miniupnpd.pid');
} }
} }
function upnp_stop() function upnp_stop() {
{
killbypid('/var/run/miniupnpd.pid'); killbypid('/var/run/miniupnpd.pid');
while((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0) { while ((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0) {
mwexec('killall miniupnpd 2>/dev/null', true); mwexec('killall miniupnpd 2>/dev/null', true);
} }
mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null'); mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null'); mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
} }
function upnp_configure() function upnp_configure() {
{
global $config; global $config;
if (!empty($config['installedpackages']['miniupnpd']['config'][0]['enable'])) { if (!empty($config['installedpackages']['miniupnpd']['config'][0]['enable'])) {
...@@ -38,17 +33,6 @@ function upnp_configure() ...@@ -38,17 +33,6 @@ function upnp_configure()
} }
} }
function upnp_write_config($file, $text) {
$handle = fopen($file, 'w');
if(!$handle) {
upnp_warn("Could not open {$file} for writing.");
return;
}
fwrite($handle, $text);
fclose($handle);
}
function upnp_uuid() { function upnp_uuid() {
/* md5 hash of wan mac */ /* md5 hash of wan mac */
$uuid = md5(get_interface_mac(get_real_interface("wan"))); $uuid = md5(get_interface_mac(get_real_interface("wan")));
...@@ -63,61 +47,56 @@ function sync_package_miniupnpd() { ...@@ -63,61 +47,56 @@ function sync_package_miniupnpd() {
$upnp_config = $config['installedpackages']['miniupnpd']['config'][0]; $upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
$config_file = '/var/etc/miniupnpd.conf'; $config_file = '/var/etc/miniupnpd.conf';
if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface'])) { $ext_ifname = get_real_interface($upnp_config['ext_iface']);
$ext_ifname = get_real_interface(); if ($ext_ifname == $upnp_config['ext_iface']) {
} else { log_error("miniupnpd: Could not resolve real interface for {$upnp_config['ext_iface']}, exit");
$if = get_real_interface($upnp_config['ext_iface']); return;
if ($if != $upnp_config['ext_iface']) {
$ext_ifname = $if;
} else {
$ext_ifname = get_real_interface();
upnp_warn("Could not resolve real interface for {$upnp_config['ext_iface']}, defaulting to WAN");
}
} }
$config_text = "ext_ifname={$ext_ifname}\n"; $config_text = "ext_ifname={$ext_ifname}\n";
$config_text .= "port=2189\n"; $config_text .= "port=2189\n";
$ifaces_active = ''; $ifaces_active = '';
/* since config is written before this file invoked we don't need to read post data */ /* since config is written before this file invoked we don't need to read post data */
if(!empty($upnp_config['enable']) && !empty($upnp_config['iface_array'])) { if (!empty($upnp_config['enable']) && !empty($upnp_config['iface_array'])) {
$iface_array = explode(',', $upnp_config['iface_array']); foreach(explode(',', $upnp_config['iface_array']) as $iface) {
foreach($iface_array as $iface) {
/* Setting the same internal and external interface is not allowed. */ /* Setting the same internal and external interface is not allowed. */
if ($iface == $upnp_config['ext_iface']) { if ($iface == $upnp_config['ext_iface']) {
continue; continue;
} }
$if = get_real_interface($iface); $if = get_real_interface($iface);
/* above function returns iface if fail */ /* above function returns iface if fail */
if($if!=$iface) { if ($if!=$iface) {
$addr = find_interface_ip($if); $addr = find_interface_ip($if);
$bits = find_interface_subnet($if); $bits = find_interface_subnet($if);
/* check that the interface has an ip address before adding parameters */ /* check that the interface has an ip address before adding parameters */
if (is_ipaddr($addr)) { if (is_ipaddr($addr)) {
$config_text .= "listening_ip={$if}\n"; $config_text .= "listening_ip={$if}\n";
if(!$ifaces_active) { if (!$ifaces_active) {
$webgui_ip = $addr; $webgui_ip = $addr;
$ifaces_active = $iface; $ifaces_active = $iface;
} else } else {
$ifaces_active .= ", {$iface}"; $ifaces_active .= ", {$iface}";
} else }
upnp_warn("Interface {$iface} has no ip address, ignoring"); } else {
} else log_error("miniupnpd: Interface {$iface} has no ip address, ignoring");
upnp_warn("Could not resolve real interface for {$iface}"); }
} else {
log_error("miniupnpd: Could not resolve real interface for {$iface}");
}
} }
if (!empty($ifaces_active)) { if (!empty($ifaces_active)) {
/* override wan ip address, common for carp, etc */ /* override wan ip address, common for carp, etc */
if($upnp_config['overridewanip']) if (!empty($upnp_config['overridewanip'])) {
$config_text .= "ext_ip={$upnp_config['overridewanip']}\n"; $config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
}
/* set upload and download bitrates */
if (!empty($upnp_config['download']) && !empty($upnp_config['upload'])) {
$download = $upnp_config['download']*1000; $download = $upnp_config['download']*1000;
$upload = $upnp_config['upload']*1000; $upload = $upnp_config['upload']*1000;
/* set upload and download bitrates */
if(!empty($download) && !empty($upload)) {
$config_text .= "bitrate_down={$download}\n"; $config_text .= "bitrate_down={$download}\n";
$config_text .= "bitrate_up={$upload}\n"; $config_text .= "bitrate_up={$upload}\n";
} }
...@@ -125,18 +104,21 @@ function sync_package_miniupnpd() { ...@@ -125,18 +104,21 @@ function sync_package_miniupnpd() {
$config_text .= "secure_mode=yes\n"; $config_text .= "secure_mode=yes\n";
/* enable logging of packets handled by miniupnpd rules */ /* enable logging of packets handled by miniupnpd rules */
if($upnp_config['logpackets']) if (!empty($upnp_config['logpackets'])) {
$config_text .= "packet_log=yes\n"; $config_text .= "packet_log=yes\n";
}
/* enable system uptime instead of miniupnpd uptime */ /* enable system uptime instead of miniupnpd uptime */
if($upnp_config['sysuptime']) if (!empty($upnp_config['sysuptime'])) {
$config_text .= "system_uptime=yes\n"; $config_text .= "system_uptime=yes\n";
}
/* set webgui url */ /* set webgui url */
if(!empty($config['system']['webgui']['protocol'])) { if (!empty($config['system']['webgui']['protocol'])) {
$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}"; $config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
if(!empty($config['system']['webgui']['port'])) if (!empty($config['system']['webgui']['port'])) {
$config_text .= ":{$config['system']['webgui']['port']}"; $config_text .= ":{$config['system']['webgui']['port']}";
}
$config_text .= "/\n"; $config_text .= "/\n";
} }
...@@ -148,43 +130,43 @@ function sync_package_miniupnpd() { ...@@ -148,43 +130,43 @@ function sync_package_miniupnpd() {
$config_text .= "model_number=".file_get_contents("/usr/local/opnsense/version/opnsense")."\n"; $config_text .= "model_number=".file_get_contents("/usr/local/opnsense/version/opnsense")."\n";
/* upnp access restrictions */ /* upnp access restrictions */
for($i=1; $i<=4; $i++) { for ($i=1; $i<=4; $i++) {
if($upnp_config["permuser{$i}"]) if ($upnp_config["permuser{$i}"]) {
$config_text .= "{$upnp_config["permuser{$i}"]}\n"; $config_text .= "{$upnp_config["permuser{$i}"]}\n";
} }
}
if($upnp_config['permdefault']) if (!empty($upnp_config['permdefault'])) {
$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n"; $config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
}
/* Allow UPnP or NAT-PMP as requested */ /* Allow UPnP or NAT-PMP as requested */
$config_text .= "enable_upnp=" . ( $upnp_config['enable_upnp'] ? "yes\n" : "no\n" ); $config_text .= "enable_upnp=" . ( $upnp_config['enable_upnp'] ? "yes\n" : "no\n" );
$config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" ); $config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" );
/* write out the configuration */ /* write out the configuration */
upnp_write_config($config_file, $config_text); file_put_contents($config_file, $config_text);
if (!upnp_running()) {
/* if miniupnpd not running start it */ /* if miniupnpd not running start it */
if(!upnp_running()) { log_error("miniupnpd: Starting service on interface: {$ifaces_active}");
upnp_notice("Starting service on interface: {$ifaces_active}");
upnp_start(); upnp_start();
} } else {
/* or restart miniupnpd if settings were changed */ /* or restart miniupnpd if settings were changed */
else { log_error("miniupnpd: Restarting service on interface: {$ifaces_active}");
upnp_notice("Restarting service on interface: {$ifaces_active}");
upnp_stop(); upnp_stop();
upnp_start(); upnp_start();
} }
} }
} else { } else {
/* user does not want miniupnpd running */ /* user does not want miniupnpd running */
/* lets stop the service and remove the rc file */ /* lets stop the service and remove the config file */
if (file_exists($config_file)) { if (file_exists($config_file)) {
if(!$upnp_config['enable']) if (empty($upnp_config['enable'])) {
upnp_notice('Stopping service: miniupnpd disabled'); log_error("miniupnpd: Stopping service: miniupnpd disabled");
else } else {
upnp_notice('Stopping service: no interfaces selected'); log_error("miniupnpd: Stopping service: no interfaces selected");
}
upnp_stop(); upnp_stop();
@unlink($config_file); @unlink($config_file);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment