Project

General

Profile

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

    
5
	/* MiniUPnPd */
6

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

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

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

    
26
	function upnp_uuid() {
27
		/* md5 hash of wan mac */
28
		$uuid = md5(exec('arp -an -i '.get_real_wan_interface().' | /usr/bin/cut -d " " -f4'));
29
		/* put uuid in correct format 8-4-4-4-12 */
30
		return substr($uuid,0,8).'-'.substr($uuid,9,4).'-'.substr($uuid,13,4).'-'.substr($uuid,17,4).'-'.substr($uuid,21,12);
31
	}
32

    
33
	function upnp_validate_ip($ip, $check_cdir) {
34
		/* validate cdir */	
35
		if($check_cdir)	{
36
			$ip_array = explode('/', $ip);
37
			if(count($ip_array) == 2) {
38
				if($ip_array[1] < 1 || $ip_array[1] > 32)
39
					return false;
40
			} else
41
				if(count($ip_array) != 1)
42
					return false;
43
		} else
44
			$ip_array[] = $ip;
45

    
46
		/* validate ip */
47
		if(!eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', $ip_array[0]))
48
			return false;
49
		foreach(explode('.', $ip_array[0]) as $sub)
50
			if($sub < 0 || $sub > 256)
51
				return false;
52
		return true;
53
	}
54

    
55
	function upnp_validate_port($port) {
56
		foreach(explode('-', $port) as $sub)
57
			if($sub < 0 || $sub > 65535)
58
				return false;
59
		return true;	
60
	}
61

    
62
	function before_form_miniupnpd($pkg) {
63
		global $config;
64

    
65
		config_lock();		
66
		
67
		/* if shaper connection speed defined hide fields */
68
		if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
69
			$i=0;
70
			foreach ($pkg['fields']['field'] as $field) {
71
				if ($field['fieldname'] == 'download' || $field['fieldname'] == 'upload')
72
					unset($pkg['fields']['field'][$i]);
73
				$i++;
74
			}
75
		}
76

    
77
		config_unlock();
78
	}
79

    
80
	function validate_form_miniupnpd($post, $input_errors) {
81
		if($post['iface_array'])
82
			foreach($post['iface_array'] as $iface)
83
				if($iface == 'wan')
84
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
85
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
86
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
87
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
88
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
89
		if($post['download'] && $post['download'] <= 0)
90
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
91
		if($post['upload'] && $post['upload'] <= 0)
92
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
93

    
94
		/* user permissions validation */
95
		for($i=1; $i<=4; $i++) {
96
			if($post["permuser{$i}"]) {
97
				$perm = explode(' ',$post["permuser{$i}"]);
98
				/* should explode to 4 args */
99
				if(count($perm) != 4) {
100
					$input_errors[] = "You must follow the specified format in the 'User specified permissions {$i}' field";
101
				} else {
102
					/* must with allow or deny */
103
					if(!($perm[0] == 'allow' || $perm[0] == 'deny'))
104
						$input_errors[] = "You must begin with allow or deny in the 'User specified permissions {$i}' field";
105
					/* verify port or port range */
106
					if(!upnp_validate_port($perm[1]) || !upnp_validate_port($perm[3]))
107
						$input_errors[] = "You must specify a port or port range between 0 and 65535 in the 'User specified
108
							permissions {$i}' field";
109
					/* verify ip address */
110
					if(!upnp_validate_ip($perm[2],true))
111
						$input_errors[] = "You must specify a valid ip address in the 'User specified permissions {$i}' field";
112
				}
113
			}
114
		}		
115
	}
116

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

    
121
		config_lock();
122

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

    
126
		$config_text = "ext_ifname=".get_real_wan_interface()."\n";
127
		$config_text .= "port=2189\n";
128

    
129
		$ifaces_active = '';
130

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

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

    
159
			if($ifaces_active) {
160
				/* override wan ip address, common for carp, etc */
161
				if($upnp_config['overridewanip'])
162
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
163

    
164
				/* if shaper connection speed defined use those values */
165
				if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
166
					$download = $config['ezshaper']['step2']['download']*1000;
167
					$upload = $config['ezshaper']['step2']['upload']*1000;
168
				} else {
169
					$download = $upnp_config['download']*1000;
170
					$upload = $upnp_config['upload']*1000;
171
				}
172

    
173
				/* set upload and download bitrates */
174
				if($download && $upload) {
175
					$config_text .= "bitrate_down={$download}\n";
176
					$config_text .= "bitrate_up={$upload}\n";
177
				}
178
				
179
				/* enable logging of packets handled by miniupnpd rules */
180
				if($upnp_config['logpackets'])
181
					$config_text .= "packet_log=yes\n";
182
				
183
				/* enable system uptime instead of miniupnpd uptime */
184
				if($upnp_config['sysuptime'])
185
					$config_text .= "system_uptime=yes\n";
186

    
187
				/* set webgui url */
188
				if($config['system']['webgui']['protocol']) {
189
					$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
190
					if($config['system']['webgui']['port'])
191
						$config_text .= ":{$config['system']['webgui']['port']}";
192
					$config_text .= "/\n";
193
				}
194

    
195
				/* set uuid and serial */
196
				$config_text .= "uuid=".upnp_uuid()."\n";
197
				$config_text .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
198

    
199
				/* set model number */
200
				$config_text .= "model_number=".exec("/bin/cat /etc/version")."\n";
201
	
202
				/* upnp access restrictions */
203
				for($i=1; $i<=4; $i++) {
204
					if($upnp_config["permuser{$i}"])
205
						$config_text .= "{$upnp_config["permuser{$i}"]}\n";
206
				}
207

    
208
				if($upnp_config['permdefault'])
209
					$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\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
				elseif($_POST['iface_array']) {
221
					upnp_notice("Restarting service on interface: {$ifaces_active}");
222
					upnp_action('restart');
223
				}
224
			}
225
		}
226

    
227
		if(!$iface_array || !$ifaces_active) {
228
			/* no parameters user does not want miniupnpd running */
229
			/* lets stop the service and remove the rc file */
230

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

    
237
				upnp_action('stop');
238
				unlink($config_file);
239
			}
240
		}
241

    
242
		config_unlock();
243
	}
244
?>
(3-3/11)