Project

General

Profile

Download (8.96 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}"); return; }
8
	function upnp_warn ($msg) { syslog(LOG_WARNING, "miniupnpd: {$msg}"); return; }
9

    
10
	function upnp_action ($action) {
11
		switch($action) {
12
			case "start":
13
				mwexec("killall miniupnpd 2>/dev/null");
14
				upnp_start();
15
				break;
16
			case "stop":
17
				mwexec("killall miniupnpd");
18
				break;
19
			case "restart":
20
				mwexec("killall miniupnpd");
21
				upnp_start();
22
				break;
23
		}
24
	}
25

    
26
	function upnp_running () {
27
		if((int)exec("pgrep miniupnpd | wc -l") > 0)
28
			return true;
29
		return false;
30
	}	
31

    
32
	function upnp_config ($name) {
33
		global $config;
34
		if($config['installedpackages']['miniupnpd']['config'][0]["{$name}"])
35
			return $config['installedpackages']['miniupnpd']['config'][0]["{$name}"];
36
		return NULL;
37
	}
38

    
39
	function upnp_write_config($conf_file, $conf_text) {
40
		$conf_file = "/var/etc/miniupnpd.conf";
41
		$conf = fopen($conf_file, "w");
42
		if(!$conf) {
43
			upnp_warn("Could not open {$conf_file} for writing.");
44
			echo "Could not open {$conf_file} for writing.";
45
			exit;
46
		}
47
		fwrite($conf, $conf_text);
48
		fclose($conf);	
49
	}
50

    
51
	function upnp_uuid() {
52
		/* md5 hash of wan mac */
53
		$arp = explode(' ',exec('arp -an -i '.get_real_wan_interface()));
54
		$uuid = md5($arp[3]);
55
		/* put uuid in correct format 8-4-4-4-12 */
56
		return substr($uuid,0,8)."-".substr($uuid,9,4)."-".substr($uuid,13,4)."-".substr($uuid,17,4)."-".substr($uuid,21,12);
57
	}
58

    
59
	function upnp_validate_ip($ip,$check_cdir) {
60
		/* validate cdir */	
61
		if($check_cdir)	{
62
			$ip_array = explode("/",$ip);
63
			if(count($ip_array) == 2) {
64
				if($ip_array[1] < 1 || $ip_array[1] > 32)
65
					return false;
66
			} else
67
				if(count($ip_array) != 1)
68
					return false;
69
		} else
70
			$ip_array[] = $ip;
71

    
72
		/* validate ip */
73
		if(!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip_array[0]))
74
			return false;
75
		foreach(explode(".", $ip_array[0]) as $sub)
76
			if($sub < 0 || $sub > 256)
77
				return false;
78
		return true;
79
	}
80

    
81
	function upnp_validate_port($port) {
82
		foreach(explode("-", $port) as $sub)
83
			if($sub < 0 || $sub > 65535)
84
				return false;
85
		return true;	
86
	}
87

    
88
	function before_form_miniupnpd($pkg) {
89
		global $config;
90

    
91
		config_lock();		
92
		
93
		/* if shaper connection speed defined hide fields */
94
		if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
95
			$i=0;
96
			foreach ($pkg['fields']['field'] as $field) {
97
				if ($field['fieldname'] == 'download' || $field['fieldname'] == 'upload')
98
					unset($pkg['fields']['field'][$i]);
99
				$i++;
100
			}
101
		}
102

    
103
		config_unlock();
104
	}
105

    
106
	function validate_form_miniupnpd($post, $input_errors) {
107
		if($post['iface_array'])
108
			foreach($post['iface_array'] as $iface)
109
				if($iface == "wan")
110
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
111
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
112
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
113
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
114
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
115
		if($post['download'] && $post['download'] <= 0)
116
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
117
		if($post['upload'] && $post['upload'] <= 0)
118
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
119

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

    
143
	function sync_package_miniupnpd() {
144
		global $config;
145
		global $input_errors;
146

    
147
		config_lock();	
148

    
149
		$configtext = "ext_ifname=".get_real_wan_interface()."\n";
150
		$configtext .= "port=2189\n";
151

    
152
		$ifaces_active = "";
153

    
154
		/* since config is written before this file invoked we don't need to read post data */
155
		if(upnp_config('enable') && upnp_config('iface_array'))
156
			$iface_array = explode(',',upnp_config('iface_array'));
157

    
158
		if($iface_array) {
159
			foreach($iface_array as $iface) {
160
				$if = convert_friendly_interface_to_real_interface_name($iface);
161
				/* above function returns iface if fail */
162
				if($if!=$iface) {
163
					$addr = find_interface_ip($if);
164
					/* non enabled interfaces are displayed in list on miniupnpd settings page */
165
					/* check that the interface has an ip address before adding parameters */
166
					if($addr) {
167
						$configtext .= "listening_ip={$addr}\n";
168
						if(!$ifaces_active) {
169
							$webgui_ip = $addr;
170
							$ifaces_active = $iface;
171
						} else {
172
							$ifaces_active .= ", {$iface}";
173
						}
174
					} else {
175
						upnp_warn("Interface {$iface} has no ip address, ignoring");
176
					}
177
				} else {
178
					upnp_warn("Could not resolve real interface for {$iface}");
179
				}
180
			}
181

    
182
			if($ifaces_active) {
183
				/* override wan ip address, common for carp, etc */
184
				if(upnp_config('overridewanip'))
185
					$configtext .= "ext_ip=".upnp_config('overridewanip')."\n";
186

    
187
				/* if shaper connection speed defined use those values */
188
				if($config['ezshaper']['step2']['download'] && $config['ezshaper']['step2']['upload']) {
189
					$download = $config['ezshaper']['step2']['download']*1000;
190
					$upload = $config['ezshaper']['step2']['upload']*1000;
191
				} else {
192
					$download = upnp_config('download')*1000;
193
					$upload = upnp_config('upload')*1000;
194
				}
195

    
196
				/* set upload and download bitrates */
197
				if($download && $upload) {
198
					$configtext .= "bitrate_down={$download}\n";
199
					$configtext .= "bitrate_up={$upload}\n";
200
				}
201
				
202
				/* enable logging of packets handled by miniupnpd rules */
203
				if(upnp_config('logpackets'))
204
					$configtext .= "packet_log=yes\n";
205
				
206
				/* enable system uptime instead of miniupnpd uptime */
207
				if(upnp_config('sysuptime'))
208
					$configtext .= "system_uptime=yes\n";
209

    
210
				/* set webgui url */
211
				if($config['system']['webgui']['protocol']) {
212
					$configtext .= "presentation_url=".$config['system']['webgui']['protocol']."://{$webgui_ip}";
213
					if($config['system']['webgui']['port'])
214
						$configtext .= ":".$config['system']['webgui']['port'];
215
					$configtext .= "/\n";
216
				}
217

    
218
				/* set uuid and serial */
219
				$configtext .= "uuid=".upnp_uuid()."\n";
220
				$configtext .= "serial=".strtoupper(substr(upnp_uuid(),0,8))."\n";
221

    
222
				/* set model number */
223
				$configtext .= "model_number=".exec("cat /etc/version")."\n";
224
	
225
				/* upnp access restrictions */
226
				for($i=1; $i<=4; $i++) {
227
					if(upnp_config("permuser{$i}"))
228
						$configtext .= upnp_config("permuser{$i}")."\n";
229
				}
230

    
231
				if(upnp_config('permdefault'))
232
					$configtext .= "deny 0-65535 0.0.0.0/0 0-65535\n";
233

    
234
				/* generate rc file start and stop */
235
				$stop = <<<EOD
236
if [ `pgrep miniupnpd | wc -l` != 0  ]; then
237
		/usr/bin/killall miniupnpd
238
		while [ `pgrep miniupnpd | wc -l` != 0 ]; do
239
			sleep 1
240
		done
241
	fi
242
	# Clear existing rules and rdr entries
243
	if [ `pfctl -aminiupnpd -sr | wc -l` != 0  ]; then
244
		/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null
245
	fi
246
	if [ `pfctl -aminiupnpd -sn | wc -l` != 0  ]; then
247
		/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null
248
	fi
249
EOD;
250
				$start = $stop."\n\t/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf";
251

    
252
				/* write out the configuration */
253
				conf_mount_rw();
254
				upnp_write_config("/var/etc/miniupnpd.conf",$configtext);
255
				conf_mount_ro();
256
				
257
				/* if miniupnpd not running start it */
258
				if(!upnp_running()) {
259
					upnp_notice("Starting service on interface: {$ifaces_active}");
260
					upnp_action('start');	
261
				}
262
				/* or restart miniupnpd if settings were changed */
263
				elseif($_POST['iface_array']) {
264
					upnp_notice("Restarting service on interface: {$ifaces_active}");
265
					upnp_action('restart');
266
				}
267
			}
268
		}
269

    
270
		if(!$iface_array || !$ifaces_active) {
271
			/* no parameters user does not want miniupnpd running */
272
			/* lets stop the service and remove the rc file */
273

    
274
			if(file_exists(UPNP_RCFILE)) {
275
				if(!upnp_config('enable'))
276
					upnp_notice('Stopping service: miniupnpd disabled');
277
				else
278
					upnp_notice('Stopping service: no interfaces selected');
279
					
280
				upnp_action('stop');
281

    
282
				conf_mount_rw();
283
				unlink(UPNP_RCFILE);
284
				unlink("/var/etc/miniupnpd.conf");
285
				conf_mount_ro();
286
			}
287
		}
288

    
289
		config_unlock();
290
	}
291
?>
(3-3/11)