Project

General

Profile

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

    
7
	$shortcut_section = "upnp";
8

    
9
	/* MiniUPnPd */
10

    
11
	function upnp_notice($msg) {
12
		log_error("miniupnpd: {$msg}");
13
	}
14

    
15
	function upnp_warn($msg) {
16
		log_error("miniupnpd: {$msg}");
17
	}
18

    
19
	function upnp_running () {
20
		if ((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0) {
21
			return true;
22
		}
23
		return false;
24
	}
25

    
26
	function upnp_write_config($file, $text) {
27
		$handle = fopen($file, 'w');
28
		if (!$handle) {
29
			upnp_warn("Could not open {$file} for writing.");
30
			return;
31
		}
32
		fwrite($handle, $text);
33
		fclose($handle);
34
	}
35

    
36
	function upnp_uuid() {
37
		/* md5 hash of wan mac */
38
		$uuid = md5(get_interface_mac(get_real_interface("wan")));
39
		/* put uuid in correct format 8-4-4-4-12 */
40
		return substr($uuid, 0, 8) . '-' . substr($uuid, 9, 4) . '-' . substr($uuid, 13, 4) . '-' . substr($uuid, 17, 4) . '-' . substr($uuid, 21, 12);
41
	}
42

    
43
	function upnp_validate_queue($qname) {
44
		read_altq_config();
45
		$qlist = get_altq_name_list();
46
		if (is_array($qlist)) {
47
			return in_array($qname, $qlist);
48
		} else {
49
			return false;
50
		}
51
	}
52

    
53
	function upnp_validate_ip($ip, $check_cdir) {
54
		/* validate cidr */
55
		$ip_array = array();
56
		if ($check_cdir) {
57
			$ip_array = explode('/', $ip);
58
			if (count($ip_array) == 2) {
59
				if ($ip_array[1] < 1 || $ip_array[1] > 32) {
60
					return false;
61
				}
62
			} else {
63
				if (count($ip_array) != 1) {
64
					return false;
65
				}
66
			}
67
		} else {
68
			$ip_array[] = $ip;
69
		}
70

    
71
		/* validate ip */
72
		if (!is_ipaddr($ip_array[0])) {
73
			return false;
74
		}
75
		return true;
76
	}
77

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

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

    
90
	}
91

    
92
	function validate_form_miniupnpd($post, &$input_errors) {
93
		if ($post['enable'] && (!$post['enable_upnp'] && !$post['enable_natpmp'])) {
94
			$input_errors[] = 'At least one of \'UPnP\' or \'NAT-PMP\' must be allowed';
95
		}
96
		if ($post['iface_array']) {
97
			foreach ($post['iface_array'] as $iface) {
98
				if ($iface == 'wan') {
99
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
100
				} elseif ($iface == $post['ext_iface']) {
101
					$input_errors[] = 'You cannot select the external interface as an internal interface.';
102
				}
103
			}
104
		}
105
		if ($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'], false)) {
106
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
107
		}
108
		if (($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download'])) {
109
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
110
		}
111
		if ($post['download'] && $post['download'] <= 0) {
112
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
113
		}
114
		if ($post['upload'] && $post['upload'] <= 0) {
115
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
116
		}
117
		if ($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue'])) {
118
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
119
		}
120

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

    
147
	function sync_package_miniupnpd() {
148
		global $g, $config;
149
		global $input_errors;
150

    
151
		$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
152
		$config_file = '/var/etc/miniupnpd.conf';
153

    
154
		if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface'])) {
155
			$ext_ifname = get_real_interface();
156
		} else {
157
			$if = convert_friendly_interface_to_real_interface_name($upnp_config['ext_iface']);
158
			if ($if != $upnp_config['ext_iface']) {
159
				$ext_ifname = $if;
160
			} else {
161
				$ext_ifname = get_real_interface();
162
				upnp_warn("Could not resolve real interface for {$upnp_config['ext_iface']}, defaulting to WAN");
163
			}
164
		}
165

    
166
		$config_text = "ext_ifname={$ext_ifname}\n";
167
		$config_text .= "port=2189\n";
168

    
169
		$ifaces_active = '';
170

    
171
		/* since config is written before this file is invoked we don't need to read post data */
172
		if ($upnp_config['enable'] && !empty($upnp_config['iface_array'])) {
173
			$iface_array = explode(',', $upnp_config['iface_array']);
174

    
175
			foreach ($iface_array as $iface) {
176
				/* Setting the same internal and external interface is not allowed. */
177
				if ($iface == $upnp_config['ext_iface']) {
178
					continue;
179
				}
180
				$if = convert_friendly_interface_to_real_interface_name($iface);
181
				/* above function returns iface if fail */
182
				if ($if != $iface) {
183
					$addr = find_interface_ip($if);
184
					$bits = find_interface_subnet($if);
185
					/* check that the interface has an ip address before adding parameters */
186
					if (is_ipaddr($addr)) {
187
						$config_text .= "listening_ip={$if}\n";
188
						if (!$ifaces_active) {
189
							$webgui_ip = $addr;
190
							$ifaces_active = $iface;
191
						} else {
192
							$ifaces_active .= ", {$iface}";
193
						}
194
					} else {
195
						upnp_warn("Interface {$iface} has no ip address, ignoring");
196
					}
197
				} else {
198
					upnp_warn("Could not resolve real interface for {$iface}");
199
				}
200
			}
201

    
202
			if (!empty($ifaces_active)) {
203
				/* override wan ip address, common for carp, etc */
204
				if ($upnp_config['overridewanip']) {
205
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
206
				}
207

    
208
				$download = $upnp_config['download']*1000;
209
				$upload = $upnp_config['upload']*1000;
210

    
211
				/* set upload and download bitrates */
212
				if (!empty($download) && !empty($upload)) {
213
					$config_text .= "bitrate_down={$download}\n";
214
					$config_text .= "bitrate_up={$upload}\n";
215
				}
216

    
217
				/* enable logging of packets handled by miniupnpd rules */
218
				if ($upnp_config['logpackets']) {
219
					$config_text .= "packet_log=yes\n";
220
				}
221

    
222
				/* enable system uptime instead of miniupnpd uptime */
223
				if ($upnp_config['sysuptime']) {
224
					$config_text .= "system_uptime=yes\n";
225
				}
226

    
227
				/* set secure_mode */
228
				$config_text .= "secure_mode=yes\n";
229

    
230
				/* set webgui url */
231
				if (!empty($upnp_config['presentationurl'])){
232
					$config_text .= "presentation_url=" . $upnp_config['presentationurl'] . "\n";
233
				} elseif (!empty($config['system']['webgui']['protocol'])) {
234
					$config_text .= "presentation_url={$config['system']['webgui']['protocol']}://{$webgui_ip}";
235
					if (!empty($config['system']['webgui']['port'])) {
236
						$config_text .= ":{$config['system']['webgui']['port']}";
237
					}
238
					$config_text .= "/\n";
239
				}
240

    
241
				/* set uuid and serial */
242
				$config_text .= "uuid=".upnp_uuid()."\n";
243
				$config_text .= "serial=".strtoupper(substr(upnp_uuid(), 0, 8))."\n";
244

    
245
				/* set model number */
246
				if (!empty($upnp_config['modelnumber'])){
247
					$config_text .= "model_number=" . $upnp_config['modelnumber'] . "\n";
248
				} else {
249
					$config_text .= "model_number=" . $g['product_version'] . "\n";
250
				}
251
				/* upnp access restrictions */
252
				if (is_array($upnp_config['row'])) {
253
					foreach ($upnp_config['row'] as $row) {
254
						if ($row['permuser']) {
255
							$config_text .= "{$row["permuser"]}\n";
256
						}
257
					}
258
				}
259

    
260
				if ($upnp_config['permdefault']) {
261
					$config_text .= "deny 0-65535 0.0.0.0/0 0-65535\n";
262
				}
263

    
264
				/* Recheck if queue is valid */
265
				if (!upnp_validate_queue($upnp_config['upnpqueue'])) {
266
					unset($upnp_config['upnpqueue']);
267
				}
268

    
269
				/* Add shaper queue */
270
				if ($upnp_config['upnpqueue']) {
271
					$config_text .= "queue={$upnp_config['upnpqueue']}\n";
272
				}
273

    
274
				/* Allow UPnP or NAT-PMP as requested */
275
				$config_text .= "enable_upnp="   . ($upnp_config['enable_upnp']   ? "yes\n" : "no\n");
276
				$config_text .= "enable_natpmp=" . ($upnp_config['enable_natpmp'] ? "yes\n" : "no\n");
277

    
278
				/* write out the configuration */
279
				upnp_write_config($config_file, $config_text);
280

    
281
				/* if miniupnpd not running start it */
282
				if (!upnp_running()) {
283
					upnp_notice("Starting service on interface: {$ifaces_active}");
284
					upnp_action('start');
285
				} else {
286
					/* restart miniupnpd if settings were changed */
287
					upnp_notice("Restarting service on interface: {$ifaces_active}");
288
					upnp_action('restart');
289
				}
290
			}
291
		} else {
292
			/* user does not want miniupnpd running */
293
			/* lets stop the service and remove the rc file */
294

    
295
			if (file_exists($config_file)) {
296
				if (!$upnp_config['enable']) {
297
					upnp_notice('Stopping service: miniupnpd disabled');
298
				} else {
299
					upnp_notice('Stopping service: no interfaces selected');
300
				}
301

    
302
				upnp_action('stop');
303
				@unlink($config_file);
304
			}
305
		}
306
	}
307
?>
(1-1/2)