Project

General

Profile

Download (7.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
	require_once("util.inc");
3
	require_once("config.inc");
4
	require_once("functions.inc");
5
	require_once("shaper.inc");
6

    
7
	/* MiniUPnPd */
8

    
9
	function upnp_notice ($msg) { log_error("miniupnpd: {$msg}"); }
10
	function upnp_warn ($msg) { log_error("miniupnpd: {$msg}"); }
11

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

    
18
	function upnp_write_config($file, $text) {
19
		$handle = fopen($file, 'w');
20
		if(!$handle) {
21
			upnp_warn("Could not open {$file} for writing.");
22
			return;
23
		}
24
		fwrite($handle, $text);
25
		fclose($handle);
26
	}
27

    
28
	function upnp_uuid() {
29
		/* md5 hash of wan mac */
30
		$uuid = md5(get_interface_mac(get_real_interface("wan")));
31
		/* put uuid in correct format 8-4-4-4-12 */
32
		return substr($uuid,0,8).'-'.substr($uuid,9,4).'-'.substr($uuid,13,4).'-'.substr($uuid,17,4).'-'.substr($uuid,21,12);
33
	}
34

    
35
	function upnp_validate_queue($qname) {
36
		read_altq_config();
37
		$qlist = get_altq_name_list();
38
		if (is_array($qlist)) {
39
			return in_array($qname, $qlist);
40
		} else {
41
			return false;
42
		}
43
	}
44

    
45
	function upnp_validate_ip($ip, $check_cdir) {
46
		/* validate cidr */	
47
		$ip_array = array();
48
		if($check_cdir)	{
49
			$ip_array = explode('/', $ip);
50
			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
		if (!is_ipaddr($ip_array[0]))
61
			return false;
62
		return true;
63
	}
64

    
65
	function upnp_validate_port($port) {
66
		foreach(explode('-', $port) as $sub)
67
			if($sub < 0 || $sub > 65535)
68
				return false;
69
		return true;	
70
	}
71

    
72
	function before_form_miniupnpd($pkg) {
73
		global $config;
74

    
75
	}
76

    
77
	function validate_form_miniupnpd($post, $input_errors) {
78
		if($post['enable'] && (!$post['enable_upnp'] && !$post['enable_natpmp']))
79
			$input_errors[] = 'At least one of \'UPnP\' or \'NAT-PMP\' must be allowed';
80
		if($post['iface_array'])
81
			foreach($post['iface_array'] as $iface)
82
				if($iface == 'wan')
83
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
84
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
85
			$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
		if($post['download'] && $post['download'] <= 0)
89
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
90
		if($post['upload'] && $post['upload'] <= 0)
91
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
92
		if($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue']))
93
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
94

    
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
	}
117

    
118
	function sync_package_miniupnpd() {
119
		global $config;
120
		global $input_errors;
121

    
122
		$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
123
		$config_file = '/var/etc/miniupnpd.conf';
124

    
125
		$config_text = "ext_ifname=".get_real_interface()."\n";
126
		$config_text .= "port=2189\n";
127

    
128
		$ifaces_active = '';
129

    
130
		/* since config is written before this file invoked we don't need to read post data */
131
		if($upnp_config['enable'] && !empty($upnp_config['iface_array'])) {
132
			$iface_array = explode(',', $upnp_config['iface_array']);
133

    
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
					$bits = find_interface_subnet($if);
140
					/* check that the interface has an ip address before adding parameters */
141
					if (is_ipaddr($addr)) {
142
						$config_text .= "listening_ip={$addr}/{$bits}\n";
143
						if(!$ifaces_active) {
144
							$webgui_ip = $addr;
145
							$ifaces_active = $iface;
146
						} else
147
							$ifaces_active .= ", {$iface}";
148
					} else
149
						upnp_warn("Interface {$iface} has no ip address, ignoring");
150
				} else
151
					upnp_warn("Could not resolve real interface for {$iface}");
152
			}
153

    
154
			if (!empty($ifaces_active)) {
155
				/* override wan ip address, common for carp, etc */
156
				if($upnp_config['overridewanip'])
157
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
158

    
159
				$download = $upnp_config['download']*1000;
160
				$upload = $upnp_config['upload']*1000;
161

    
162
				/* set upload and download bitrates */
163
				if(!empty($download) && !empty($upload)) {
164
					$config_text .= "bitrate_down={$download}\n";
165
					$config_text .= "bitrate_up={$upload}\n";
166
				}
167
				
168
				/* enable logging of packets handled by miniupnpd rules */
169
				if($upnp_config['logpackets'])
170
					$config_text .= "packet_log=yes\n";
171
				
172
				/* enable system uptime instead of miniupnpd uptime */
173
				if($upnp_config['sysuptime'])
174
					$config_text .= "system_uptime=yes\n";
175

    
176
				/* set webgui url */
177
				if(!empty($config['system']['webgui']['protocol'])) {
178
					$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
179
					if(!empty($config['system']['webgui']['port']))
180
						$config_text .= ":{$config['system']['webgui']['port']}";
181
					$config_text .= "/\n";
182
				}
183

    
184
				/* set uuid and serial */
185
				$config_text .= "uuid=".upnp_uuid()."\n";
186
				$config_text .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
187

    
188
				/* set model number */
189
				$config_text .= "model_number=".file_get_contents("/etc/version")."\n";
190
	
191
				/* upnp access restrictions */
192
				for($i=1; $i<=4; $i++) {
193
					if($upnp_config["permuser{$i}"])
194
						$config_text .= "{$upnp_config["permuser{$i}"]}\n";
195
				}
196

    
197
				if($upnp_config['permdefault'])
198
					$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
199

    
200
				/* Recheck if queue is valid */
201
				if (!upnp_validate_queue($upnp_config['upnpqueue']))
202
					unset($upnp_config['upnpqueue']);
203

    
204
				/* Add shaper queue */
205
				if($upnp_config['upnpqueue'])
206
					$config_text .= "queue={$upnp_config['upnpqueue']}\n";
207

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

    
212
				/* write out the configuration */
213
				upnp_write_config($config_file, $config_text);
214
				
215
				/* if miniupnpd not running start it */
216
				if(!upnp_running()) {
217
					upnp_notice("Starting service on interface: {$ifaces_active}");
218
					upnp_action('start');	
219
				}
220
				/* or restart miniupnpd if settings were changed */
221
				else {
222
					upnp_notice("Restarting service on interface: {$ifaces_active}");
223
					upnp_action('restart');
224
				}
225
			}
226
		} else {
227
			/* user does not want miniupnpd running */
228
			/* lets stop the service and remove the rc file */
229

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

    
236
				upnp_action('stop');
237
				@unlink($config_file);
238
			}
239
		}
240
	}
241
?>
(2-2/9)