Project

General

Profile

Download (10.1 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($interface) {
37
		$mac = get_interface_mac($interface);
38
		if (empty($mac)) {
39
			$mac = generate_random_mac_address();
40
		}
41
		/* md5 hash of external inteface mac */
42
		$uuid = md5($mac);
43
		/* put uuid in correct format 8-4-4-4-12 */
44
		return substr($uuid, 0, 8) . '-' . substr($uuid, 9, 4) . '-' . substr($uuid, 13, 4) . '-' . substr($uuid, 17, 4) . '-' . substr($uuid, 21, 12);
45
	}
46

    
47
	function upnp_validate_queue($qname) {
48
		read_altq_config();
49
		$qlist = get_altq_name_list();
50
		if (is_array($qlist)) {
51
			return in_array($qname, $qlist);
52
		} else {
53
			return false;
54
		}
55
	}
56

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

    
75
		/* validate ip */
76
		if (!is_ipaddr($ip_array[0])) {
77
			return false;
78
		}
79
		return true;
80
	}
81

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

    
91
	function before_form_miniupnpd(&$pkg) {
92

    
93
	}
94

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

    
124
		if ($post['enable_stun']) {
125
			if (!$post['stun_host'] || (!is_ipaddrv4($post['stun_host']) && !is_hostname($post['stun_host']))) {
126
				$input_errors[] = 'A valid IP address or hostname for \'STUN Server\' must be specified';
127
			}
128
			if ($post['stun_port'] && !is_port($post['stun_port'])) {
129
				$input_errors[] = 'A valid port number for \'STUN Port\' must be specified';
130
			}
131
		}
132

    
133
		/* ACL Entries validation */
134
		$j = substr_count(implode(array_keys($post)), "permuser");
135
		for ($i = 0; $i < $j; $i++) {
136
			if ($post["permuser{$i}"]) {
137
				$perm = explode(' ', $post["permuser{$i}"]);
138
				/* should explode to 4 args */
139
				if (count($perm) != 4) {
140
					$input_errors[] = "You must follow the specified format in the 'ACL Entries {$i}' field";
141
				} else {
142
					/* must with allow or deny */
143
					if (!($perm[0] == 'allow' || $perm[0] == 'deny')) {
144
						$input_errors[] = "You must begin with allow or deny in the 'ACL Entries {$i}' field";
145
					}
146
					/* verify port or port range */
147
					if (!upnp_validate_port($perm[1]) || !upnp_validate_port($perm[3])) {
148
						$input_errors[] = "You must specify a port or port range between 1 and 65535 in the 'ACL Entries {$i}' field";
149
					}
150
					/* verify ip address */
151
					if (!upnp_validate_ip($perm[2], true)) {
152
						$input_errors[] = "You must specify a valid IP address in the 'ACL Entries {$i}' field";
153
					}
154
				}
155
			}
156
		}
157
	}
158

    
159
	function sync_package_miniupnpd() {
160
		global $g;
161
		global $input_errors;
162

    
163
		$upnp_config = config_get_path('installedpackages/miniupnpd/config/0');
164
		$config_file = '/var/etc/miniupnpd.conf';
165

    
166
		if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface'])) {
167
			$ext_ifname = get_real_interface();
168
		} else {
169
			$if = convert_friendly_interface_to_real_interface_name($upnp_config['ext_iface']);
170
			if ($if != $upnp_config['ext_iface']) {
171
				$ext_ifname = $if;
172
			} else {
173
				$ext_ifname = get_real_interface();
174
				upnp_warn("Could not resolve real interface for {$upnp_config['ext_iface']}, defaulting to WAN");
175
			}
176
		}
177

    
178
		$config_text = "ext_ifname={$ext_ifname}\n";
179
		$config_text .= "port=2189\n";
180

    
181
		$ifaces_active = '';
182

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

    
187
			foreach ($iface_array as $iface) {
188
				/* Setting the same internal and external interface is not allowed. */
189
				if ($iface == $upnp_config['ext_iface']) {
190
					continue;
191
				}
192
				$if = convert_friendly_interface_to_real_interface_name($iface);
193
				/* above function returns iface if fail */
194
				if ($if != $iface) {
195
					$addr = find_interface_ip($if);
196
					$bits = find_interface_subnet($if);
197
					/* check that the interface has an ip address before adding parameters */
198
					if (is_ipaddr($addr)) {
199
						$config_text .= "listening_ip={$if}\n";
200
						if (!$ifaces_active) {
201
							$webgui_ip = $addr;
202
							$ifaces_active = $iface;
203
						} else {
204
							$ifaces_active .= ", {$iface}";
205
						}
206
					} else {
207
						upnp_warn("Interface {$iface} has no IP address, ignoring");
208
					}
209
				} else {
210
					upnp_warn("Could not resolve real interface for {$iface}");
211
				}
212
			}
213

    
214
			if (!empty($ifaces_active)) {
215
				/* override wan ip address, common for carp, etc */
216
				if ($upnp_config['overridewanip']) {
217
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
218
				}
219

    
220
				$download = (int)$upnp_config['download'] * 1000;
221
				$upload = (int)$upnp_config['upload'] * 1000;
222

    
223
				/* set upload and download bitrates */
224
				if (!empty($download) && !empty($upload)) {
225
					$config_text .= "bitrate_down={$download}\n";
226
					$config_text .= "bitrate_up={$upload}\n";
227
				}
228

    
229
				/* enable logging of packets handled by miniupnpd rules */
230
				if ($upnp_config['logpackets']) {
231
					$config_text .= "packet_log=yes\n";
232
				}
233

    
234
				/* enable system uptime instead of miniupnpd uptime */
235
				if ($upnp_config['sysuptime']) {
236
					$config_text .= "system_uptime=yes\n";
237
				}
238

    
239
				/* set secure_mode */
240
				$config_text .= "secure_mode=yes\n";
241

    
242
				/* set webgui url */
243
				$webgui_config = config_get_path('system/webgui');
244
				if (!empty($upnp_config['presentationurl'])){
245
					$config_text .= "presentation_url=" . $upnp_config['presentationurl'] . "\n";
246
				} elseif (!empty($webgui_config['protocol'])) {
247
					$config_text .= "presentation_url={$webgui_config['protocol']}://{$webgui_ip}";
248
					if (!empty($webgui_config['port'])) {
249
						$config_text .= ":{$webgui_config['port']}";
250
					}
251
					$config_text .= "/\n";
252
				}
253

    
254
				/* set uuid and serial */
255
				$uuid = upnp_uuid($ext_ifname);
256
				$config_text .= "uuid={$uuid}\n";
257
				$config_text .= "serial=".strtoupper(substr($uuid, 0, 8))."\n";
258

    
259
				/* set model number */
260
				if (!empty($upnp_config['modelnumber'])){
261
					$config_text .= "model_number=" . $upnp_config['modelnumber'] . "\n";
262
				} else {
263
					$config_text .= "model_number=" . g_get('product_version') . "\n";
264
				}
265
				/* upnp access restrictions */
266
				if (is_array($upnp_config['row'])) {
267
					foreach ($upnp_config['row'] as $row) {
268
						if ($row['permuser']) {
269
							$config_text .= "{$row["permuser"]}\n";
270
						}
271
					}
272
				}
273

    
274
				if ($upnp_config['permdefault']) {
275
					$config_text .= "deny 1-65535 0.0.0.0/0 1-65535\n";
276
				}
277

    
278
				/* Recheck if queue is valid */
279
				if (!upnp_validate_queue($upnp_config['upnpqueue'])) {
280
					unset($upnp_config['upnpqueue']);
281
				}
282

    
283
				/* Add shaper queue */
284
				if ($upnp_config['upnpqueue']) {
285
					$config_text .= "queue={$upnp_config['upnpqueue']}\n";
286
				}
287

    
288
				/* Allow UPnP IGD or PCP/NAT-PMP as requested */
289
				$config_text .= "enable_upnp="   . ($upnp_config['enable_upnp']   ? "yes\n" : "no\n");
290
				$config_text .= "enable_pcp_pmp=" . ($upnp_config['enable_natpmp'] ? "yes\n" : "no\n");
291

    
292
				/* STUN configuration */
293
				if ($upnp_config['enable_stun']) {
294
					$config_text .= "ext_perform_stun=yes\n";
295
					$config_text .= "ext_stun_host={$upnp_config['stun_host']}\n";
296
					if ($upnp_config['stun_port']){
297
						$config_text .= "ext_stun_port={$upnp_config['stun_port']}\n";
298
					}
299
				}
300

    
301
				/* write out the configuration */
302
				upnp_write_config($config_file, $config_text);
303

    
304
				/* if miniupnpd not running start it */
305
				if (!upnp_running()) {
306
					upnp_notice("Starting service on interface: {$ifaces_active}");
307
					upnp_action('start');
308
				} else {
309
					/* restart miniupnpd if settings were changed */
310
					upnp_notice("Restarting service on interface: {$ifaces_active}");
311
					upnp_action('restart');
312
				}
313
			}
314
		} else {
315
			/* user does not want miniupnpd running */
316
			/* lets stop the service and remove the rc file */
317

    
318
			if (file_exists($config_file)) {
319
				if (!$upnp_config['enable']) {
320
					upnp_notice('Stopping service: miniupnpd disabled');
321
				} else {
322
					upnp_notice('Stopping service: no interfaces selected');
323
				}
324

    
325
				upnp_action('stop');
326
				@unlink($config_file);
327
			}
328
		}
329
		/* Reload filter to trigger UPnP rule changes */
330
		send_event("filter reload");
331
	}
332
?>
(1-1/2)