Project

General

Profile

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

    
5
	/* MiniUPnPd */
6

    
7
	define('UPNP_RCFILE', '/usr/local/etc/rc.d/miniupnpd.sh');
8
	define('UPNP_CONFIG','/usr/local/etc/miniupnpd.conf');
9

    
10
	function upnp_notice ($msg) { syslog(LOG_NOTICE, "miniupnpd: {$msg}"); return; }
11
	function upnp_warn ($msg) { syslog(LOG_WARNING, "miniupnpd: {$msg}"); return; }
12

    
13
	function upnp_action ($action) {
14
		if (file_exists(UPNP_RCFILE))
15
			mwexec(UPNP_RCFILE.' '.$action);
16
	}
17

    
18
	function upnp_running () {
19
		if((int)exec("pgrep miniupnpd | wc -l") > 0)
20
			return true;
21
		return false;
22
	}	
23

    
24
	function upnp_config ($name) {
25
		global $config;
26
		if($config['installedpackages']['miniupnpd']['config'][0]["{$name}"])
27
			return $config['installedpackages']['miniupnpd']['config'][0]["{$name}"];
28
		return NULL;
29
	}
30

    
31
	function upnp_write_config($conf_file, $conf_text) {
32
		$conf = fopen($conf_file, "w");
33
		if(!$conf) {
34
			upnp_warn("Could not open {$conf_file} for writing.");
35
			exit;
36
		}
37
		fwrite($conf, $conf_text);
38
		fclose($conf);	
39
	}
40

    
41
	function upnp_uuid() {
42
		/* md5 hash of wan mac */
43
		$arp = explode(' ',exec('arp -an -i '.get_real_wan_interface()));
44
		$uuid = md5($arp[3]);
45
		/* put uuid in correct format 8-4-4-4-12 */
46
		return substr($uuid,0,8)."-".substr($uuid,9,4)."-".substr($uuid,13,4)."-".substr($uuid,17,4)."-".substr($uuid,21,12);
47
	}
48

    
49
	function upnp_validate_ip($ip,$check_cdir) {
50
		/* validate cdir */	
51
		if($check_cdir)	{
52
			$ip_array = explode("/",$ip);
53
			if(count($ip_array) == 2) {
54
				if($ip_array[1] < 1 || $ip_array[1] > 32)
55
					return false;
56
			} else
57
				if(count($ip_array) != 1)
58
					return false;
59
		} else
60
			$ip_array[] = $ip;
61

    
62
		/* validate ip */
63
		if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip_array[0]))
64
			return false;
65
		foreach(explode(".", $ip_array[0]) as $sub)
66
			if($sub < 0 || $sub > 256)
67
				return false;
68
		return true;
69
	}
70

    
71
	function upnp_validate_port($port) {
72
		foreach(explode("-", $port) as $sub)
73
			if($sub < 0 || $sub > 65535)
74
				return false;
75
		return true;	
76
	}
77

    
78
	function before_form_miniupnpd($pkg) {
79
		global $config;
80

    
81
		config_lock();		
82
		
83
		/* if shaper connection speed defined hide fields */
84
		if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
85
			$i=0;
86
			foreach ($pkg['fields']['field'] as $field) {
87
				if ($field['fieldname'] == 'download' || $field['fieldname'] == 'upload')
88
					unset($pkg['fields']['field'][$i]);
89
				$i++;
90
			}
91
		}
92

    
93
		config_unlock();
94
	}
95

    
96
	function validate_form_miniupnpd($post, $input_errors) {
97
		if($post['iface_array'])
98
			foreach($post['iface_array'] as $iface)
99
				if($iface == "wan")
100
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
101
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
102
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
103
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
104
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
105
		if($post['download'] && $post['download'] <= 0)
106
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
107
		if($post['upload'] && $post['upload'] <= 0)
108
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
109

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

    
133
	function sync_package_miniupnpd() {
134
		global $config;
135
		global $input_errors;
136

    
137
		config_lock();	
138

    
139
		$configtext = "ext_ifname=".get_real_wan_interface()."\n";
140
		$configtext .= "port=2189\n";
141

    
142
		$ifaces_active = "";
143

    
144
		/* since config is written before this file invoked we don't need to read post data */
145
		if(upnp_config('enable') && upnp_config('iface_array'))
146
			$iface_array = explode(',',upnp_config('iface_array'));
147

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

    
172
			if($ifaces_active) {
173
				/* override wan ip address, common for carp, etc */
174
				if(upnp_config('overridewanip'))
175
					$configtext .= "ext_ip=".upnp_config('overridewanip')."\n";
176

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

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

    
200
				/* set webgui url */
201
				if($config['system']['webgui']['protocol']) {
202
					$configtext .= "presentation_url=".$config['system']['webgui']['protocol']."://{$webgui_ip}";
203
					if($config['system']['webgui']['port'])
204
						$configtext .= ":".$config['system']['webgui']['port'];
205
					$configtext .= "/\n";
206
				}
207

    
208
				/* set uuid and serial */
209
				$configtext .= "uuid=".upnp_uuid()."\n";
210
				$configtext .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
211

    
212
				/* set model number */
213
				$configtext .= "model_number=".exec("cat /etc/version")."\n";
214
	
215
				/* upnp access restrictions */
216
				for($i=1; $i<=4; $i++) {
217
					if(upnp_config("permuser{$i}"))
218
						$configtext .= upnp_config("permuser{$i}")."\n";
219
				}
220

    
221
				if(upnp_config('permdefault'))
222
					$configtext .= "deny 0-65535 0.0.0.0/0 0-65535\n";
223

    
224
				/* generate rc file start and stop */
225
				$stop = <<<EOD
226
if [ `pgrep miniupnpd | wc -l` != 0  ]; then
227
		/usr/bin/killall miniupnpd
228
		while [ `pgrep miniupnpd | wc -l` != 0 ]; do
229
			sleep 1
230
		done
231
	fi
232
	# Clear existing rules and rdr entries
233
	if [ `pfctl -aminiupnpd -sr | wc -l` != 0  ]; then
234
		/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null
235
	fi
236
	if [ `pfctl -aminiupnpd -sn | wc -l` != 0  ]; then
237
		/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null
238
	fi
239
EOD;
240
				$start = $stop."\n\t/usr/local/sbin/miniupnpd -f ".UPNP_CONFIG;
241

    
242
				/* write out the configuration */
243
				conf_mount_rw();
244
				upnp_write_config(UPNP_CONFIG,$configtext);
245
				write_rcfile(array(
246
					    'file' => 'miniupnpd.sh',
247
					    'start' => $start,
248
					    'stop' => $stop
249
				    )
250
				);
251
				conf_mount_ro();
252

    
253
				/* if miniupnpd not running start it */
254
				if(!upnp_running()) {
255
					upnp_notice("Starting service on interface: {$ifaces_active}");
256
					upnp_action('start');	
257
				}
258
				/* or restart miniupnpd if settings were changed */
259
				elseif($_POST['iface_array']) {
260
					upnp_notice("Restarting service on interface: {$ifaces_active}");
261
					upnp_action('restart');
262
				}
263
			}
264
		}
265

    
266
		if(!$iface_array || !$ifaces_active) {
267
			/* no parameters user does not want miniupnpd running */
268
			/* lets stop the service and remove the rc file */
269

    
270
			if(file_exists(UPNP_RCFILE)) {
271
				if(!upnp_config('enable'))
272
					upnp_notice('Stopping service: miniupnpd disabled');
273
				else
274
					upnp_notice('Stopping service: no interfaces selected');
275
					
276
				upnp_action('stop');
277

    
278
				conf_mount_rw();
279
				unlink(UPNP_RCFILE);
280
				unlink(UPNP_CONFIG);
281
				conf_mount_ro();
282
			}
283
		}
284

    
285
		config_unlock();
286
	}
287
?>
(3-3/11)