Projet

Général

Profil

Télécharger (8,61 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / pkg / miniupnpd.inc @ 8718669c

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) { log_error("miniupnpd: {$msg}"); }
12
	function upnp_warn ($msg) { log_error("miniupnpd: {$msg}"); }
13

    
14
	function upnp_running () {
15
		if((int)exec('/bin/pgrep -a miniupnpd | /usr/bin/wc -l') > 0)
16
			return true;
17
		return false;
18
	}
19

    
20
	function upnp_write_config($file, $text) {
21
		$handle = fopen($file, 'w');
22
		if(!$handle) {
23
			upnp_warn("Could not open {$file} for writing.");
24
			return;
25
		}
26
		fwrite($handle, $text);
27
		fclose($handle);
28
	}
29

    
30
	function upnp_uuid() {
31
		/* md5 hash of wan mac */
32
		$uuid = md5(get_interface_mac(get_real_interface("wan")));
33
		/* put uuid in correct format 8-4-4-4-12 */
34
		return substr($uuid,0,8).'-'.substr($uuid,9,4).'-'.substr($uuid,13,4).'-'.substr($uuid,17,4).'-'.substr($uuid,21,12);
35
	}
36

    
37
	function upnp_validate_queue($qname) {
38
		read_altq_config();
39
		$qlist = get_altq_name_list();
40
		if (is_array($qlist)) {
41
			return in_array($qname, $qlist);
42
		} else {
43
			return false;
44
		}
45
	}
46

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

    
61
		/* validate ip */
62
		if (!is_ipaddr($ip_array[0]))
63
			return false;
64
		return true;
65
	}
66

    
67
	function upnp_validate_port($port) {
68
		foreach(explode('-', $port) as $sub)
69
			if($sub < 0 || $sub > 65535)
70
				return false;
71
		return true;	
72
	}
73

    
74
	function before_form_miniupnpd(&$pkg) {
75
		global $config;
76

    
77
	}
78

    
79
	function validate_form_miniupnpd($post, &$input_errors) {
80
		if($post['enable'] && (!$post['enable_upnp'] && !$post['enable_natpmp']))
81
			$input_errors[] = 'At least one of \'UPnP\' or \'NAT-PMP\' must be allowed';
82
		if($post['iface_array'])
83
			foreach($post['iface_array'] as $iface) {
84
				if($iface == 'wan')
85
					$input_errors[] = 'It is a security risk to specify WAN in the \'Interface\' field';
86
				elseif ($iface == $post['ext_iface'])
87
					$input_errors[] = 'You cannot select the external interface as an internal interface.';
88
			}
89
		if($post['overridewanip'] && !upnp_validate_ip($post['overridewanip'],false))
90
			$input_errors[] = 'You must specify a valid ip address in the \'Override WAN address\' field';
91
		if(($post['download'] && !$post['upload']) || ($post['upload'] && !$post['download']))
92
			$input_errors[] = 'You must fill in both \'Maximum Download Speed\' and \'Maximum Upload Speed\' fields';
93
		if($post['download'] && $post['download'] <= 0)
94
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Download Speed\' field';
95
		if($post['upload'] && $post['upload'] <= 0)
96
			$input_errors[] = 'You must specify a value greater than 0 in the \'Maximum Upload Speed\' field';
97
		if($post['upnpqueue'] && !upnp_validate_queue($post['upnpqueue']))
98
			$input_errors[] = 'You must specify a valid traffic shaping queue.';
99

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

    
123
	function sync_package_miniupnpd() {
124
		global $config;
125
		global $input_errors;
126

    
127
		$upnp_config = $config['installedpackages']['miniupnpd']['config'][0];
128
		$config_file = '/var/etc/miniupnpd.conf';
129

    
130
		if (!isset($upnp_config['ext_iface']) || empty($upnp_config['ext_iface']))
131
			$ext_ifname = get_real_interface();
132
		else {
133
			$if = convert_friendly_interface_to_real_interface_name($upnp_config['ext_iface']);
134
			if ($if != $upnp_config['ext_iface'])
135
				$ext_ifname = $if;
136
			else {
137
				$ext_ifname = get_real_interface();
138
				upnp_warn("Could not resolve real interface for {$upnp_config['ext_iface']}, defaulting to WAN");
139
			}
140
		}
141

    
142
		$config_text = "ext_ifname={$ext_ifname}\n";
143
		$config_text .= "port=2189\n";
144

    
145
		$ifaces_active = '';
146

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

    
151
			foreach($iface_array as $iface) {
152
				/* Setting the same internal and external interface is not allowed. */
153
				if ($iface == $upnp_config['ext_iface'])
154
					continue;
155
				$if = convert_friendly_interface_to_real_interface_name($iface);
156
				/* above function returns iface if fail */
157
				if($if!=$iface) {
158
					$addr = find_interface_ip($if);
159
					$bits = find_interface_subnet($if);
160
					/* check that the interface has an ip address before adding parameters */
161
					if (is_ipaddr($addr)) {
162
						$config_text .= "listening_ip={$if}\n";
163
						if(!$ifaces_active) {
164
							$webgui_ip = $addr;
165
							$ifaces_active = $iface;
166
						} else
167
							$ifaces_active .= ", {$iface}";
168
					} else
169
						upnp_warn("Interface {$iface} has no ip address, ignoring");
170
				} else
171
					upnp_warn("Could not resolve real interface for {$iface}");
172
			}
173

    
174
			if (!empty($ifaces_active)) {
175
				/* override wan ip address, common for carp, etc */
176
				if($upnp_config['overridewanip'])
177
					$config_text .= "ext_ip={$upnp_config['overridewanip']}\n";
178

    
179
				$download = $upnp_config['download']*1000;
180
				$upload = $upnp_config['upload']*1000;
181

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

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

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

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

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

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

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

    
228
				/* Allow UPnP or NAT-PMP as requested */
229
				$config_text .= "enable_upnp="   . ( $upnp_config['enable_upnp']   ? "yes\n" : "no\n" );
230
				$config_text .= "enable_natpmp=" . ( $upnp_config['enable_natpmp'] ? "yes\n" : "no\n" );
231

    
232
				/* write out the configuration */
233
				upnp_write_config($config_file, $config_text);
234
				
235
				/* if miniupnpd not running start it */
236
				if(!upnp_running()) {
237
					upnp_notice("Starting service on interface: {$ifaces_active}");
238
					upnp_action('start');	
239
				}
240
				/* or restart miniupnpd if settings were changed */
241
				else {
242
					upnp_notice("Restarting service on interface: {$ifaces_active}");
243
					upnp_action('restart');
244
				}
245
			}
246
		} else {
247
			/* user does not want miniupnpd running */
248
			/* lets stop the service and remove the rc file */
249

    
250
			if (file_exists($config_file)) {
251
				if(!$upnp_config['enable'])
252
					upnp_notice('Stopping service: miniupnpd disabled');
253
				else
254
					upnp_notice('Stopping service: no interfaces selected');				
255

    
256
				upnp_action('stop');
257
				@unlink($config_file);
258
			}
259
		}
260
	}
261
?>
(1-1/4)