Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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