Project

General

Profile

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