miniupnpd.inc 5.01 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3
<?php


4
/* MiniUPnPd */
Ad Schellevis's avatar
Ad Schellevis committed
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
function upnp_notice ($msg) { log_error("miniupnpd: {$msg}"); }
function upnp_warn ($msg) { log_error("miniupnpd: {$msg}"); }

function upnp_running () {
	if((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0)
		return true;
	return false;
}

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() {
	/* md5 hash of wan mac */
	$uuid = md5(get_interface_mac(get_real_interface("wan")));
	/* put uuid in correct format 8-4-4-4-12 */
	return substr($uuid,0,8).'-'.substr($uuid,9,4).'-'.substr($uuid,13,4).'-'.substr($uuid,17,4).'-'.substr($uuid,21,12);
}

function sync_package_miniupnpd() {
	global $config;
	global $input_errors;
Ad Schellevis's avatar
Ad Schellevis committed
35

36 37 38 39 40 41 42 43 44 45 46 47
	$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
	$config_file = '/var/etc/miniupnpd.conf';

	if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface']))
		$ext_ifname = get_real_interface();
	else {
		$if = convert_friendly_interface_to_real_interface_name($upnp_config['ext_iface']);
		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");
Ad Schellevis's avatar
Ad Schellevis committed
48 49 50
		}
	}

51 52 53 54 55 56
	$config_text = "ext_ifname={$ext_ifname}\n";
	$config_text .= "port=2189\n";

	$ifaces_active = '';

	/* since config is written before this file invoked we don't need to read post data */
57
	if(!empty($upnp_config['enable']) && !empty($upnp_config['iface_array'])) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		$iface_array = explode(',', $upnp_config['iface_array']);

		foreach($iface_array as $iface) {
			/* Setting the same internal and external interface is not allowed. */
			if ($iface == $upnp_config['ext_iface'])
				continue;
			$if = convert_friendly_interface_to_real_interface_name($iface);
			/* above function returns iface if fail */
			if($if!=$iface) {
				$addr = find_interface_ip($if);
				$bits = find_interface_subnet($if);
				/* check that the interface has an ip address before adding parameters */
				if (is_ipaddr($addr)) {
					$config_text .= "listening_ip={$if}\n";
					if(!$ifaces_active) {
						$webgui_ip = $addr;
						$ifaces_active = $iface;
					} else
						$ifaces_active .= ", {$iface}";
				} else
					upnp_warn("Interface {$iface} has no ip address, ignoring");
Ad Schellevis's avatar
Ad Schellevis committed
79
			} else
80 81
				upnp_warn("Could not resolve real interface for {$iface}");
		}
Ad Schellevis's avatar
Ad Schellevis committed
82

83 84 85 86
		if (!empty($ifaces_active)) {
			/* override wan ip address, common for carp, etc */
			if($upnp_config['overridewanip'])
				$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
Ad Schellevis's avatar
Ad Schellevis committed
87

88 89
			$download = $upnp_config['download']*1000;
			$upload = $upnp_config['upload']*1000;
Ad Schellevis's avatar
Ad Schellevis committed
90

91 92 93 94 95
			/* set upload and download bitrates */
			if(!empty($download) && !empty($upload)) {
				$config_text .= "bitrate_down={$download}\n";
				$config_text .= "bitrate_up={$upload}\n";
			}
Ad Schellevis's avatar
Ad Schellevis committed
96

97 98 99
			/* enable logging of packets handled by miniupnpd rules */
			if($upnp_config['logpackets'])
				$config_text .= "packet_log=yes\n";
Ad Schellevis's avatar
Ad Schellevis committed
100

101 102 103 104 105 106 107 108 109 110
			/* enable system uptime instead of miniupnpd uptime */
			if($upnp_config['sysuptime'])
				$config_text .= "system_uptime=yes\n";

			/* set webgui url */
			if(!empty($config['system']['webgui']['protocol'])) {
				$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
				if(!empty($config['system']['webgui']['port']))
					$config_text .= ":{$config['system']['webgui']['port']}";
				$config_text .= "/\n";
Ad Schellevis's avatar
Ad Schellevis committed
111 112
			}

113 114 115
			/* set uuid and serial */
			$config_text .= "uuid=".upnp_uuid()."\n";
			$config_text .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
Ad Schellevis's avatar
Ad Schellevis committed
116

117 118
			/* set model number */
			$config_text .= "model_number=".file_get_contents("/usr/local/opnsense/version/opnsense")."\n";
Ad Schellevis's avatar
Ad Schellevis committed
119

120 121 122 123
			/* upnp access restrictions */
			for($i=1; $i<=4; $i++) {
				if($upnp_config["permuser{$i}"])
					$config_text .= "{$upnp_config["permuser{$i}"]}\n";
Ad Schellevis's avatar
Ad Schellevis committed
124 125
			}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
			if($upnp_config['permdefault'])
				$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";

			/* Allow UPnP or NAT-PMP as requested */
			$config_text .= "enable_upnp="   . ( $upnp_config['enable_upnp']   ? "yes\n" : "no\n" );
			$config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" );

			/* write out the configuration */
			upnp_write_config($config_file, $config_text);

			/* if miniupnpd not running start it */
			if(!upnp_running()) {
				upnp_notice("Starting service on interface: {$ifaces_active}");
				upnp_action('start');
Ad Schellevis's avatar
Ad Schellevis committed
140
			}
141 142 143 144
			/* or restart miniupnpd if settings were changed */
			else {
				upnp_notice("Restarting service on interface: {$ifaces_active}");
				upnp_action('restart');
Ad Schellevis's avatar
Ad Schellevis committed
145 146
			}
		}
147 148 149 150 151 152 153 154 155 156 157 158 159
	} else {
		/* user does not want miniupnpd running */
		/* lets stop the service and remove the rc file */

		if (file_exists($config_file)) {
			if(!$upnp_config['enable'])
				upnp_notice('Stopping service: miniupnpd disabled');
			else
				upnp_notice('Stopping service: no interfaces selected');

			upnp_action('stop');
			@unlink($config_file);
		}
Ad Schellevis's avatar
Ad Schellevis committed
160
	}
161
}