Project

General

Profile

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

    
6
	/* MiniUPnPd */
7

    
8
	function upnp_notice ($msg) { syslog(LOG_NOTICE, "miniupnpd: {$msg}"); }
9
	function upnp_warn ($msg) { syslog(LOG_WARNING, "miniupnpd: {$msg}"); }
10

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

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

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

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

    
44
	function upnp_validate_ip($ip, $check_cdir) {
45
		/* validate cdir */	
46
		if($check_cdir)	{
47
			$ip_array = explode('/', $ip);
48
			if(count($ip_array) == 2) {
49
				if($ip_array[1] < 1 || $ip_array[1] > 32)
50
					return false;
51
			} else
52
				if(count($ip_array) != 1)
53
					return false;
54
		} else
55
			$ip_array[] = $ip;
56

    
57
		/* validate ip */
58
		if(!eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', $ip_array[0]))
59
			return false;
60
		foreach(explode('.', $ip_array[0]) as $sub)
61
			if($sub < 0 || $sub > 256)
62
				return false;
63
		return true;
64
	}
65

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

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

    
76
		/* if shaper connection speed defined hide fields */
77
		if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
78
			$i=0;
79
			foreach ($pkg['fields']['field'] as $field) {
80
				if ($field['fieldname'] == 'download' || $field['fieldname'] == 'upload')
81
					unset($pkg['fields']['field'][$i]);
82
				$i++;
83
			}
84
		}
85
	}
86

    
87
	function validate_form_miniupnpd($post, $input_errors) {
88
		if($post['enable'] && (!$post['enable_upnp'] && !$post['enable_natpmp']))
89
			$input_errors[] = 'At least one of \'UPnP\' or \'NAT-PMP\' must be allowed';
90
		if($post['iface_array'])
91
			foreach($post['iface_array'] as $iface)
92
				if($iface == 'wan')
93
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
94
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
95
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
96
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
97
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
98
		if($post['download'] && $post['download'] <= 0)
99
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
100
		if($post['upload'] && $post['upload'] <= 0)
101
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
102
		if($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue']))
103
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
104

    
105
		/* user permissions validation */
106
		for($i=1; $i<=4; $i++) {
107
			if($post["permuser{$i}"]) {
108
				$perm = explode(' ',$post["permuser{$i}"]);
109
				/* should explode to 4 args */
110
				if(count($perm) != 4) {
111
					$input_errors[] = "You must follow the specified format in the 'User specified permissions {$i}' field";
112
				} else {
113
					/* must with allow or deny */
114
					if(!($perm[0] == 'allow' || $perm[0] == 'deny'))
115
						$input_errors[] = "You must begin with allow or deny in the 'User specified permissions {$i}' field";
116
					/* verify port or port range */
117
					if(!upnp_validate_port($perm[1]) || !upnp_validate_port($perm[3]))
118
						$input_errors[] = "You must specify a port or port range between 0 and 65535 in the 'User specified
119
							permissions {$i}' field";
120
					/* verify ip address */
121
					if(!upnp_validate_ip($perm[2],true))
122
						$input_errors[] = "You must specify a valid ip address in the 'User specified permissions {$i}' field";
123
				}
124
			}
125
		}		
126
	}
127

    
128
	function sync_package_miniupnpd() {
129
		global $config;
130
		global $input_errors;
131

    
132
		$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
133
		$config_file = '/var/etc/miniupnpd.conf';
134

    
135
		$config_text = "ext_ifname=".get_real_interface()."\n";
136
		$config_text .= "port=2189\n";
137

    
138
		$ifaces_active = '';
139

    
140
		/* since config is written before this file invoked we don't need to read post data */
141
		if($upnp_config['enable'] && $upnp_config['iface_array'])
142
			$iface_array = explode(',', $upnp_config['iface_array']);
143

    
144
		if($iface_array) {
145
			foreach($iface_array as $iface) {
146
				$if = convert_friendly_interface_to_real_interface_name($iface);
147
				/* above function returns iface if fail */
148
				if($if!=$iface) {
149
					$addr = find_interface_ip($if);
150
					/* non enabled interfaces are displayed in list on miniupnpd settings page */
151
					/* check that the interface has an ip address before adding parameters */
152
					if($addr) {
153
						$config_text .= "listening_ip={$addr}\n";
154
						if(!$ifaces_active) {
155
							$webgui_ip = $addr;
156
							$ifaces_active = $iface;
157
						} else {
158
							$ifaces_active .= ", {$iface}";
159
						}
160
					} else {
161
						upnp_warn("Interface {$iface} has no ip address, ignoring");
162
					}
163
				} else {
164
					upnp_warn("Could not resolve real interface for {$iface}");
165
				}
166
			}
167

    
168
			if($ifaces_active) {
169
				/* override wan ip address, common for carp, etc */
170
				if($upnp_config['overridewanip'])
171
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
172

    
173
				/* if shaper connection speed defined use those values */
174
				if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
175
					$download = $config['ezshaper']['step2']['download']*1000;
176
					$upload = $config['ezshaper']['step2']['upload']*1000;
177
				} else {
178
					$download = $upnp_config['download']*1000;
179
					$upload = $upnp_config['upload']*1000;
180
				}
181

    
182
				/* set upload and download bitrates */
183
				if($download && $upload) {
184
					$config_text .= "bitrate_down={$download}\n";
185
					$config_text .= "bitrate_up={$upload}\n";
186
				}
187
				
188
				/* enable logging of packets handled by miniupnpd rules */
189
				if($upnp_config['logpackets'])
190
					$config_text .= "packet_log=yes\n";
191
				
192
				/* enable system uptime instead of miniupnpd uptime */
193
				if($upnp_config['sysuptime'])
194
					$config_text .= "system_uptime=yes\n";
195

    
196
				/* set webgui url */
197
				if($config['system']['webgui']['protocol']) {
198
					$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
199
					if($config['system']['webgui']['port'])
200
						$config_text .= ":{$config['system']['webgui']['port']}";
201
					$config_text .= "/\n";
202
				}
203

    
204
				/* set uuid and serial */
205
				$config_text .= "uuid=".upnp_uuid()."\n";
206
				$config_text .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
207

    
208
				/* set model number */
209
				$config_text .= "model_number=".exec("/bin/cat /etc/version")."\n";
210
	
211
				/* upnp access restrictions */
212
				for($i=1; $i<=4; $i++) {
213
					if($upnp_config["permuser{$i}"])
214
						$config_text .= "{$upnp_config["permuser{$i}"]}\n";
215
				}
216

    
217
				if($upnp_config['permdefault'])
218
					$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
219

    
220
				/* Recheck if queue is valid */
221
				if (!upnp_validate_queue($upnp_config['upnpqueue']))
222
					unset($upnp_config['upnpqueue']);
223

    
224
				/* Add shaper queue */
225
				if($upnp_config['upnpqueue'])
226
					$config_text .= "queue={$upnp_config['upnpqueue']}\n";
227

    
228
				/* Allow UPnP or NAT-PMP as requested */
229
				$config_text .= "enable_upnp="   . ( $upnp_config['enable_upnp']   ? "yes\n" : "no\n" );
230
				$config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" );
231

    
232
				/* write out the configuration */
233
				upnp_write_config($config_file, $config_text);
234
				
235
				/* if miniupnpd not running start it */
236
				if(!upnp_running()) {
237
					upnp_notice("Starting service on interface: {$ifaces_active}");
238
					upnp_action('start');	
239
				}
240
				/* or restart miniupnpd if settings were changed */
241
				elseif($_POST['iface_array']) {
242
					upnp_notice("Restarting service on interface: {$ifaces_active}");
243
					upnp_action('restart');
244
				}
245
			}
246
		}
247

    
248
		if(!$iface_array || !$ifaces_active) {
249
			/* no parameters user does not want miniupnpd running */
250
			/* lets stop the service and remove the rc file */
251

    
252
			if(file_exists($config_file)) {
253
				if(!$upnp_config['enable'])
254
					upnp_notice('Stopping service: miniupnpd disabled');
255
				else
256
					upnp_notice('Stopping service: no interfaces selected');				
257

    
258
				upnp_action('stop');
259
				unlink($config_file);
260
			}
261
		}
262
	}
263
?>
(3-3/10)