Project

General

Profile

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