Project

General

Profile

Download (9.34 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 7d61beba Phil Davis
	function upnp_notice($msg) {
12
		log_error("miniupnpd: {$msg}");
13
	}
14
15
	function upnp_warn($msg) {
16
		log_error("miniupnpd: {$msg}");
17
	}
18 422e039b Scott Ullrich
19 d86002ec Ryan Wagoner
	function upnp_running () {
20 7d61beba Phil Davis
		if ((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0) {
21 d86002ec Ryan Wagoner
			return true;
22 7d61beba Phil Davis
		}
23 966f579e Ryan Wagoner
		return false;
24 2816f43f Ermal
	}
25 d86002ec Ryan Wagoner
26 d05e1b9f Ryan Wagoner
	function upnp_write_config($file, $text) {
27
		$handle = fopen($file, 'w');
28 7d61beba Phil Davis
		if (!$handle) {
29 d05e1b9f Ryan Wagoner
			upnp_warn("Could not open {$file} for writing.");
30 cf46a14f Ermal
			return;
31 d86002ec Ryan Wagoner
		}
32 d05e1b9f Ryan Wagoner
		fwrite($handle, $text);
33
		fclose($handle);
34 966f579e Ryan Wagoner
	}
35
36
	function upnp_uuid() {
37
		/* md5 hash of wan mac */
38 8df14984 Ermal
		$uuid = md5(get_interface_mac(get_real_interface("wan")));
39 966f579e Ryan Wagoner
		/* put uuid in correct format 8-4-4-4-12 */
40 6c07db48 Phil Davis
		return substr($uuid, 0, 8) . '-' . substr($uuid, 9, 4) . '-' . substr($uuid, 13, 4) . '-' . substr($uuid, 17, 4) . '-' . substr($uuid, 21, 12);
41 966f579e Ryan Wagoner
	}
42 d86002ec Ryan Wagoner
43 1729ace8 jim-p
	function upnp_validate_queue($qname) {
44
		read_altq_config();
45
		$qlist = get_altq_name_list();
46 02afa684 jim-p
		if (is_array($qlist)) {
47
			return in_array($qname, $qlist);
48
		} else {
49
			return false;
50
		}
51 1729ace8 jim-p
	}
52
53 d05e1b9f Ryan Wagoner
	function upnp_validate_ip($ip, $check_cdir) {
54 7d61beba Phil Davis
		/* validate cidr */
55 539d5973 Ermal
		$ip_array = array();
56 7d61beba Phil Davis
		if ($check_cdir) {
57 d05e1b9f Ryan Wagoner
			$ip_array = explode('/', $ip);
58 7d61beba Phil Davis
			if (count($ip_array) == 2) {
59
				if ($ip_array[1] < 1 || $ip_array[1] > 32) {
60 d86002ec Ryan Wagoner
					return false;
61 7d61beba Phil Davis
				}
62
			} else {
63
				if (count($ip_array) != 1) {
64 d86002ec Ryan Wagoner
					return false;
65 7d61beba Phil Davis
				}
66
			}
67
		} else {
68 d86002ec Ryan Wagoner
			$ip_array[] = $ip;
69 7d61beba Phil Davis
		}
70 d86002ec Ryan Wagoner
71
		/* validate ip */
72 7d61beba Phil Davis
		if (!is_ipaddr($ip_array[0])) {
73 d86002ec Ryan Wagoner
			return false;
74 7d61beba Phil Davis
		}
75 d86002ec Ryan Wagoner
		return true;
76
	}
77
78
	function upnp_validate_port($port) {
79 7d61beba Phil Davis
		foreach (explode('-', $port) as $sub) {
80
			if ($sub < 0 || $sub > 65535) {
81 d86002ec Ryan Wagoner
				return false;
82 7d61beba Phil Davis
			}
83
		}
84
		return true;
85 c38fee60 Scott Ullrich
	}
86 02d777de Scott Ullrich
87 cf4e473b jim-p
	function before_form_miniupnpd(&$pkg) {
88 c38fee60 Scott Ullrich
		global $config;
89 02d777de Scott Ullrich
90 c38fee60 Scott Ullrich
	}
91
92 cf4e473b jim-p
	function validate_form_miniupnpd($post, &$input_errors) {
93 7d61beba Phil Davis
		if ($post['enable'] && (!$post['enable_upnp'] && !$post['enable_natpmp'])) {
94 93b2c459 jim-p
			$input_errors[] = 'At least one of \'UPnP\' or \'NAT-PMP\' must be allowed';
95 7d61beba Phil Davis
		}
96
		if ($post['iface_array']) {
97
			foreach ($post['iface_array'] as $iface) {
98
				if ($iface == 'wan') {
99 c38fee60 Scott Ullrich
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
100 7d61beba Phil Davis
				} elseif ($iface == $post['ext_iface']) {
101 f8466c36 jim-p
					$input_errors[] = 'You cannot select the external interface as an internal interface.';
102 7d61beba Phil Davis
				}
103 f8466c36 jim-p
			}
104 7d61beba Phil Davis
		}
105 6c07db48 Phil Davis
		if ($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'], false)) {
106 c38fee60 Scott Ullrich
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
107 7d61beba Phil Davis
		}
108
		if (($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download'])) {
109 c38fee60 Scott Ullrich
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
110 7d61beba Phil Davis
		}
111
		if ($post['download'] && $post['download'] <= 0) {
112 c38fee60 Scott Ullrich
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
113 7d61beba Phil Davis
		}
114
		if ($post['upload'] && $post['upload'] <= 0) {
115 c38fee60 Scott Ullrich
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
116 7d61beba Phil Davis
		}
117
		if ($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue'])) {
118 1729ace8 jim-p
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
119 7d61beba Phil Davis
		}
120 d86002ec Ryan Wagoner
121
		/* user permissions validation */
122 04a893de xbipin
		$j = substr_count(implode(array_keys($post)), "permuser");
123 6c07db48 Phil Davis
		for ($i = 0; $i < $j; $i++) {
124 7d61beba Phil Davis
			if ($post["permuser{$i}"]) {
125 6c07db48 Phil Davis
				$perm = explode(' ', $post["permuser{$i}"]);
126 d86002ec Ryan Wagoner
				/* should explode to 4 args */
127 7d61beba Phil Davis
				if (count($perm) != 4) {
128 d86002ec Ryan Wagoner
					$input_errors[] = "You must follow the specified format in the 'User specified permissions {$i}' field";
129
				} else {
130
					/* must with allow or deny */
131 7d61beba Phil Davis
					if (!($perm[0] == 'allow' || $perm[0] == 'deny')) {
132 d86002ec Ryan Wagoner
						$input_errors[] = "You must begin with allow or deny in the 'User specified permissions {$i}' field";
133 7d61beba Phil Davis
					}
134 d86002ec Ryan Wagoner
					/* verify port or port range */
135 7d61beba Phil Davis
					if (!upnp_validate_port($perm[1]) || !upnp_validate_port($perm[3])) {
136
						$input_errors[] = "You must specify a port or port range between 0 and 65535 in the 'User specified permissions {$i}' field";
137
					}
138 d86002ec Ryan Wagoner
					/* verify ip address */
139 6c07db48 Phil Davis
					if (!upnp_validate_ip($perm[2], true)) {
140 d86002ec Ryan Wagoner
						$input_errors[] = "You must specify a valid ip address in the 'User specified permissions {$i}' field";
141 7d61beba Phil Davis
					}
142 d86002ec Ryan Wagoner
				}
143
			}
144 7d61beba Phil Davis
		}
145 c38fee60 Scott Ullrich
	}
146
147
	function sync_package_miniupnpd() {
148 5779ade6 Renato Botelho
		global $g, $config;
149 c38fee60 Scott Ullrich
		global $input_errors;
150
151 d05e1b9f Ryan Wagoner
		$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
152
		$config_file = '/var/etc/miniupnpd.conf';
153 d86002ec Ryan Wagoner
154 7d61beba Phil Davis
		if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface'])) {
155 f8466c36 jim-p
			$ext_ifname = get_real_interface();
156 7d61beba Phil Davis
		} else {
157 f8466c36 jim-p
			$if = convert_friendly_interface_to_real_interface_name($upnp_config['ext_iface']);
158 7d61beba Phil Davis
			if ($if != $upnp_config['ext_iface']) {
159 f8466c36 jim-p
				$ext_ifname = $if;
160 7d61beba Phil Davis
			} else {
161 f8466c36 jim-p
				$ext_ifname = get_real_interface();
162
				upnp_warn("Could not resolve real interface for {$upnp_config['ext_iface']}, defaulting to WAN");
163
			}
164
		}
165
166
		$config_text = "ext_ifname={$ext_ifname}\n";
167 d05e1b9f Ryan Wagoner
		$config_text .= "port=2189\n";
168 c38fee60 Scott Ullrich
169 d05e1b9f Ryan Wagoner
		$ifaces_active = '';
170 cf97af9a Scott Ullrich
171 7d61beba Phil Davis
		/* since config is written before this file is invoked we don't need to read post data */
172
		if ($upnp_config['enable'] && !empty($upnp_config['iface_array'])) {
173 d05e1b9f Ryan Wagoner
			$iface_array = explode(',', $upnp_config['iface_array']);
174 c38fee60 Scott Ullrich
175 7d61beba Phil Davis
			foreach ($iface_array as $iface) {
176 f8466c36 jim-p
				/* Setting the same internal and external interface is not allowed. */
177 7d61beba Phil Davis
				if ($iface == $upnp_config['ext_iface']) {
178 f8466c36 jim-p
					continue;
179 7d61beba Phil Davis
				}
180 c38fee60 Scott Ullrich
				$if = convert_friendly_interface_to_real_interface_name($iface);
181
				/* above function returns iface if fail */
182 6c07db48 Phil Davis
				if ($if != $iface) {
183 c38fee60 Scott Ullrich
					$addr = find_interface_ip($if);
184 81acbdd7 Daniel Becker
					$bits = find_interface_subnet($if);
185 c38fee60 Scott Ullrich
					/* check that the interface has an ip address before adding parameters */
186 88cbd62a Ermal
					if (is_ipaddr($addr)) {
187 8718669c Renato Botelho
						$config_text .= "listening_ip={$if}\n";
188 7d61beba Phil Davis
						if (!$ifaces_active) {
189 7fae6598 Ryan Wagoner
							$webgui_ip = $addr;
190 422e039b Scott Ullrich
							$ifaces_active = $iface;
191 7d61beba Phil Davis
						} else {
192 422e039b Scott Ullrich
							$ifaces_active .= ", {$iface}";
193 7d61beba Phil Davis
						}
194
					} else {
195 422e039b Scott Ullrich
						upnp_warn("Interface {$iface} has no ip address, ignoring");
196 7d61beba Phil Davis
					}
197
				} else {
198 422e039b Scott Ullrich
					upnp_warn("Could not resolve real interface for {$iface}");
199 7d61beba Phil Davis
				}
200 c38fee60 Scott Ullrich
			}
201
202 88cbd62a Ermal
			if (!empty($ifaces_active)) {
203 d86002ec Ryan Wagoner
				/* override wan ip address, common for carp, etc */
204 7d61beba Phil Davis
				if ($upnp_config['overridewanip']) {
205 d05e1b9f Ryan Wagoner
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
206 7d61beba Phil Davis
				}
207 02d777de Scott Ullrich
208 2816f43f Ermal
				$download = $upnp_config['download']*1000;
209
				$upload = $upnp_config['upload']*1000;
210 d86002ec Ryan Wagoner
211
				/* set upload and download bitrates */
212 7d61beba Phil Davis
				if (!empty($download) && !empty($upload)) {
213 d05e1b9f Ryan Wagoner
					$config_text .= "bitrate_down={$download}\n";
214
					$config_text .= "bitrate_up={$upload}\n";
215 d86002ec Ryan Wagoner
				}
216 7d61beba Phil Davis
217 d86002ec Ryan Wagoner
				/* enable logging of packets handled by miniupnpd rules */
218 7d61beba Phil Davis
				if ($upnp_config['logpackets']) {
219 d05e1b9f Ryan Wagoner
					$config_text .= "packet_log=yes\n";
220 7d61beba Phil Davis
				}
221
222 d86002ec Ryan Wagoner
				/* enable system uptime instead of miniupnpd uptime */
223 7d61beba Phil Davis
				if ($upnp_config['sysuptime']) {
224 d05e1b9f Ryan Wagoner
					$config_text .= "system_uptime=yes\n";
225 7d61beba Phil Davis
				}
226 d86002ec Ryan Wagoner
227 b93bb38c Chris Buechler
				/* set secure_mode */
228
				$config_text .= "secure_mode=yes\n";
229
230 7fae6598 Ryan Wagoner
				/* set webgui url */
231 abbd4150 Josh Galvez (zevlag)
				if (!empty($upnp_config['presentationurl'])){
232
					$config_text .= "presentation_url=" . $upnp_config['presentationurl'] . "\n";
233
				} elseif (!empty($config['system']['webgui']['protocol'])) {
234 d05e1b9f Ryan Wagoner
					$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
235 7d61beba Phil Davis
					if (!empty($config['system']['webgui']['port'])) {
236 d05e1b9f Ryan Wagoner
						$config_text .= ":{$config['system']['webgui']['port']}";
237 7d61beba Phil Davis
					}
238 d05e1b9f Ryan Wagoner
					$config_text .= "/\n";
239 7fae6598 Ryan Wagoner
				}
240 966f579e Ryan Wagoner
241
				/* set uuid and serial */
242 d05e1b9f Ryan Wagoner
				$config_text .= "uuid=".upnp_uuid()."\n";
243 6c07db48 Phil Davis
				$config_text .= "serial=".strtoupper(substr(upnp_uuid(), 0, 8))."\n";
244 966f579e Ryan Wagoner
245
				/* set model number */
246 de8882d6 Josh Galvez (zevlag)
				if (!empty($upnp_config['modelnumber'])){
247
					$config_text .= "model_number=" . $upnp_config['modelnumber'] . "\n";
248 dc2db4dc Josh Galvez (zevlag)
				} else {
249
					$config_text .= "model_number=" . $g['product_version'] . "\n";
250
				}
251 d86002ec Ryan Wagoner
				/* upnp access restrictions */
252 7ca5be34 jim-p
				if (is_array($upnp_config['row'])) {
253
					foreach ($upnp_config['row'] as $row) {
254
						if ($row['permuser']) {
255
							$config_text .= "{$row["permuser"]}\n";
256
						}
257 7d61beba Phil Davis
					}
258 02d777de Scott Ullrich
				}
259
260 7d61beba Phil Davis
				if ($upnp_config['permdefault']) {
261 d05e1b9f Ryan Wagoner
					$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
262 7d61beba Phil Davis
				}
263 02d777de Scott Ullrich
264 1729ace8 jim-p
				/* Recheck if queue is valid */
265 7d61beba Phil Davis
				if (!upnp_validate_queue($upnp_config['upnpqueue'])) {
266 1729ace8 jim-p
					unset($upnp_config['upnpqueue']);
267 7d61beba Phil Davis
				}
268 1729ace8 jim-p
269
				/* Add shaper queue */
270 6c07db48 Phil Davis
				if ($upnp_config['upnpqueue']) {
271 1729ace8 jim-p
					$config_text .= "queue={$upnp_config['upnpqueue']}\n";
272 7d61beba Phil Davis
				}
273 1729ace8 jim-p
274 93b2c459 jim-p
				/* Allow UPnP or NAT-PMP as requested */
275 6c07db48 Phil Davis
				$config_text .= "enable_upnp="   . ($upnp_config['enable_upnp']   ? "yes\n" : "no\n");
276
				$config_text .= "enable_natpmp=" . ($upnp_config['enable_natpmp'] ? "yes\n" : "no\n");
277 93b2c459 jim-p
278 e80df06e Scott Ullrich
				/* write out the configuration */
279 d05e1b9f Ryan Wagoner
				upnp_write_config($config_file, $config_text);
280 7d61beba Phil Davis
281 422e039b Scott Ullrich
				/* if miniupnpd not running start it */
282 7d61beba Phil Davis
				if (!upnp_running()) {
283 422e039b Scott Ullrich
					upnp_notice("Starting service on interface: {$ifaces_active}");
284 7d61beba Phil Davis
					upnp_action('start');
285
				} else {
286
					/* restart miniupnpd if settings were changed */
287 422e039b Scott Ullrich
					upnp_notice("Restarting service on interface: {$ifaces_active}");
288 d86002ec Ryan Wagoner
					upnp_action('restart');
289 02d777de Scott Ullrich
				}
290
			}
291 88cbd62a Ermal
		} else {
292
			/* user does not want miniupnpd running */
293 c38fee60 Scott Ullrich
			/* lets stop the service and remove the rc file */
294 02d777de Scott Ullrich
295 88cbd62a Ermal
			if (file_exists($config_file)) {
296 7d61beba Phil Davis
				if (!$upnp_config['enable']) {
297 d86002ec Ryan Wagoner
					upnp_notice('Stopping service: miniupnpd disabled');
298 7d61beba Phil Davis
				} else {
299
					upnp_notice('Stopping service: no interfaces selected');
300
				}
301 422e039b Scott Ullrich
302 431484c8 Ryan Wagoner
				upnp_action('stop');
303 88cbd62a Ermal
				@unlink($config_file);
304 422e039b Scott Ullrich
			}
305 c38fee60 Scott Ullrich
		}
306
	}
307 422e039b Scott Ullrich
?>