Project

General

Profile

Download (8.34 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(exec('arp -an -i '.get_real_interface().' | /usr/bin/cut -d " " -f4'));
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['iface_array'])
89
			foreach($post['iface_array'] as $iface)
90
				if($iface == 'wan')
91
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
92
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
93
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
94
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
95
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
96
		if($post['download'] && $post['download'] <= 0)
97
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
98
		if($post['upload'] && $post['upload'] <= 0)
99
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
100
		if($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue']))
101
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
102

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

    
126
	function sync_package_miniupnpd() {
127
		global $config;
128
		global $input_errors;
129

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

    
133
		$config_text = "ext_ifname=".get_real_interface()."\n";
134
		$config_text .= "port=2189\n";
135

    
136
		$ifaces_active = '';
137

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

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

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

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

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

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

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

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

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

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

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

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

    
242
		if(!$iface_array || !$ifaces_active) {
243
			/* no parameters user does not want miniupnpd running */
244
			/* lets stop the service and remove the rc file */
245

    
246
			if(file_exists($config_file)) {
247
				if(!$upnp_config['enable'])
248
					upnp_notice('Stopping service: miniupnpd disabled');
249
				else
250
					upnp_notice('Stopping service: no interfaces selected');				
251

    
252
				upnp_action('stop');
253
				unlink($config_file);
254
			}
255
		}
256
	}
257
?>
(3-3/8)