1
|
<?php
|
2
|
|
3
|
/* $Id$ */
|
4
|
/*
|
5
|
$RCSfile$
|
6
|
|
7
|
Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
|
8
|
All rights reserved.
|
9
|
|
10
|
Copyright (C) 2006 Fernando Lemos
|
11
|
All rights reserved.
|
12
|
|
13
|
This file was rewritten from scratch by Fernando Lemos but
|
14
|
*MIGHT* contain code previously written by:
|
15
|
|
16
|
Copyright (C) 2005 Peter Allgeyer <allgeyer_AT_web.de>
|
17
|
All rights reserved.
|
18
|
|
19
|
Copyright (C) 2004 Peter Curran (peter@closeconsultants.com).
|
20
|
All rights reserved.
|
21
|
|
22
|
Redistribution and use in source and binary forms, with or without
|
23
|
modification, are permitted provided that the following conditions are met:
|
24
|
|
25
|
1. Redistributions of source code must retain the above copyright notices,
|
26
|
this list of conditions and the following disclaimer.
|
27
|
|
28
|
2. Redistributions in binary form must reproduce the above copyright
|
29
|
notices, this list of conditions and the following disclaimer in the
|
30
|
documentation and/or other materials provided with the distribution.
|
31
|
|
32
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
33
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
34
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
35
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
36
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
37
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
38
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
39
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
40
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
41
|
POSSIBILITY OF SUCH DAMAGE.
|
42
|
*/
|
43
|
/* DISABLE_PHP_LINT_CHECKING */
|
44
|
|
45
|
require_once('config.inc');
|
46
|
require_once("certs.inc");
|
47
|
require_once('pfsense-utils.inc');
|
48
|
|
49
|
$openvpn_prots = array("UDP", "TCP");
|
50
|
|
51
|
/*
|
52
|
* The User Auth mode below is disabled because
|
53
|
* OpenVPN erroneously requires that we provide
|
54
|
* a CA configuration parameter. In this mode,
|
55
|
* clients don't send a certificate so there is
|
56
|
* no need for a CA. If we require that admins
|
57
|
* provide one in the pfSense UI due to a bogus
|
58
|
* requirement imposed by OpenVPN, it could be
|
59
|
* considered very confusing ( I know I was ).
|
60
|
*
|
61
|
* -mgrooms
|
62
|
*/
|
63
|
|
64
|
$openvpn_dh_lengths = array(
|
65
|
1024, 2048, 4096 );
|
66
|
|
67
|
$openvpn_server_modes = array(
|
68
|
'p2p_tls' => "Peer to Peer ( SSL/TLS )",
|
69
|
'p2p_shared_key' => "Peer to Peer ( Shared Key )",
|
70
|
'server_tls' => "Remote Access ( SSL/TLS )",
|
71
|
// 'server_user' => "Remote Access ( User Auth )",
|
72
|
'server_tls_user' => "Remote Access ( SSL/TLS + User Auth )");
|
73
|
|
74
|
$openvpn_client_modes = array(
|
75
|
'p2p_tls' => "Peer to Peer ( SSL/TLS )",
|
76
|
'p2p_shared_key' => "Peer to Peer ( Shared Key )" );
|
77
|
|
78
|
function openvpn_create_key() {
|
79
|
|
80
|
$fp = popen("/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null", "r");
|
81
|
if (!$fp)
|
82
|
return false;
|
83
|
|
84
|
$rslt = stream_get_contents($fp);
|
85
|
pclose($fp);
|
86
|
|
87
|
return $rslt;
|
88
|
}
|
89
|
|
90
|
function openvpn_create_dhparams($bits) {
|
91
|
|
92
|
$fp = popen("/usr/bin/openssl dhparam {$bits} 2>/dev/null", "r");
|
93
|
if (!$fp)
|
94
|
return false;
|
95
|
|
96
|
$rslt = stream_get_contents($fp);
|
97
|
pclose($fp);
|
98
|
|
99
|
return $rslt;
|
100
|
}
|
101
|
|
102
|
function openvpn_vpnid_used($vpnid) {
|
103
|
global $config;
|
104
|
|
105
|
if (is_array($config['openvpn']['openvpn-server']))
|
106
|
foreach ($config['openvpn']['openvpn-server'] as & $settings)
|
107
|
if ($vpnid == $settings['vpnid'])
|
108
|
return true;
|
109
|
|
110
|
if (is_array($config['openvpn']['openvpn-client']))
|
111
|
foreach ($config['openvpn']['openvpn-client'] as & $settings)
|
112
|
if ($vpnid == $settings['vpnid'])
|
113
|
return true;
|
114
|
|
115
|
return false;
|
116
|
}
|
117
|
|
118
|
function openvpn_vpnid_next() {
|
119
|
|
120
|
$vpnid = 1;
|
121
|
while(openvpn_vpnid_used($vpnid))
|
122
|
$vpnid++;
|
123
|
|
124
|
return $vpnid;
|
125
|
}
|
126
|
|
127
|
function openvpn_port_used($prot, $port) {
|
128
|
global $config;
|
129
|
|
130
|
if (is_array($config['openvpn']['openvpn-server']))
|
131
|
foreach ($config['openvpn']['openvpn-server'] as & $settings)
|
132
|
if ($port == $settings['local_port'] &&
|
133
|
$prot == $settings['protocol'])
|
134
|
return $settings['vpnid'];
|
135
|
|
136
|
if (is_array($config['openvpn']['openvpn-client']))
|
137
|
foreach ($config['openvpn']['openvpn-client'] as & $settings)
|
138
|
if ($port == $settings['local_port'] &&
|
139
|
$prot == $settings['protocol'])
|
140
|
return $settings['vpnid'];
|
141
|
|
142
|
return 0;
|
143
|
}
|
144
|
|
145
|
function openvpn_port_next($prot) {
|
146
|
|
147
|
$port = 1194;
|
148
|
while(openvpn_port_used($prot, $port))
|
149
|
$port++;
|
150
|
|
151
|
return $port;
|
152
|
}
|
153
|
|
154
|
function openvpn_get_cipherlist() {
|
155
|
|
156
|
$ciphers = array();
|
157
|
$cipher_out = shell_exec('openvpn --show-ciphers | grep "default key" | awk \'{print $1, "(" $2 "-" $3 ")";}\'');
|
158
|
$cipher_lines = explode("\n", trim($cipher_out));
|
159
|
sort($cipher_lines);
|
160
|
foreach ($cipher_lines as $line) {
|
161
|
$words = explode(' ', $line);
|
162
|
$ciphers[$words[0]] = "{$words[0]} {$words[1]}";
|
163
|
}
|
164
|
|
165
|
return $ciphers;
|
166
|
}
|
167
|
|
168
|
function openvpn_validate_host($value, $name) {
|
169
|
$value = trim($value);
|
170
|
if (empty($value) || (!is_domain($value) && !is_ipaddr($value)))
|
171
|
return "The field '$name' must contain a valid IP address or domain name.";
|
172
|
return false;
|
173
|
}
|
174
|
|
175
|
function openvpn_validate_port($value, $name) {
|
176
|
$value = trim($value);
|
177
|
if (empty($value) || !is_numeric($value) || $value < 0 || ($value > 65535))
|
178
|
return "The field '$name' must contain a valid port, ranging from 0 to 65535.";
|
179
|
return false;
|
180
|
}
|
181
|
|
182
|
function openvpn_validate_cidr($value, $name) {
|
183
|
$value = trim($value);
|
184
|
if (!empty($value)) {
|
185
|
list($ip, $mask) = explode('/', $value);
|
186
|
if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
|
187
|
return "The field '$name' must contain a valid CIDR range.";
|
188
|
}
|
189
|
return false;
|
190
|
}
|
191
|
|
192
|
function openvpn_add_dhcpopts(& $settings, & $conf) {
|
193
|
|
194
|
if (!empty($settings['dns_domain']))
|
195
|
$conf .= "push \"dhcp-option DOMAIN {$settings['dns_domain']}\"\n";
|
196
|
|
197
|
if (!empty($settings['dns_server1']))
|
198
|
$conf .= "push \"dhcp-option DNS {$settings['dns_server1']}\"\n";
|
199
|
if (!empty($settings['dns_server2']))
|
200
|
$conf .= "push \"dhcp-option DNS {$settings['dns_server2']}\"\n";
|
201
|
if (!empty($settings['dns_server3']))
|
202
|
$conf .= "push \"dhcp-option DNS {$settings['dns_server3']}\"\n";
|
203
|
if (!empty($settings['dns_server4']))
|
204
|
$conf .= "push \"dhcp-option DNS {$settings['dns_server4']}\"\n";
|
205
|
|
206
|
if (!empty($settings['ntp_server1']))
|
207
|
$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
|
208
|
if (!empty($settings['ntp_server2']))
|
209
|
$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
|
210
|
|
211
|
if ($settings['netbios_enable']) {
|
212
|
|
213
|
if (!empty($settings['dhcp_nbttype']) && ($settings['dhcp_nbttype'] != 0))
|
214
|
$conf .= "push \"dhcp-option NBT {$settings['dhcp_nbttype']}\"\n";
|
215
|
if (!empty($settings['dhcp_nbtscope']))
|
216
|
$conf .= "push \"dhcp-option NBS {$settings['dhcp_nbtscope']}\"\n";
|
217
|
|
218
|
if (!empty($settings['wins_server1']))
|
219
|
$conf .= "push \"dhcp-option WINS {$settings['wins_server1']}\"\n";
|
220
|
if (!empty($settings['wins_server2']))
|
221
|
$conf .= "push \"dhcp-option WINS {$settings['wins_server2']}\"\n";
|
222
|
|
223
|
if (!empty($settings['nbdd_server1']))
|
224
|
$conf .= "push \"dhcp-option NBDD {$settings['nbdd_server1']}\"\n";
|
225
|
}
|
226
|
|
227
|
if ($settings['gwredir'])
|
228
|
$conf .= "push \"redirect-gateway def1\"\n";
|
229
|
}
|
230
|
|
231
|
function openvpn_add_custom(& $settings, & $conf) {
|
232
|
|
233
|
if ($settings['custom_options']) {
|
234
|
|
235
|
$options = explode(';', $settings['custom_options']);
|
236
|
|
237
|
if (is_array($options)) {
|
238
|
foreach ($options as $option)
|
239
|
$conf .= "$option\n";
|
240
|
} else
|
241
|
$conf .= "{$settings['custom_options']}\n";
|
242
|
}
|
243
|
}
|
244
|
|
245
|
function openvpn_add_keyfile(& $data, & $conf, $mode_id, $directive) {
|
246
|
global $g;
|
247
|
|
248
|
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}";
|
249
|
file_put_contents($fpath, base64_decode($data));
|
250
|
chown($fpath, 'nobody');
|
251
|
chgrp($fpath, 'nobody');
|
252
|
|
253
|
$conf .= "{$directive} {$fpath}\n";
|
254
|
}
|
255
|
|
256
|
function openvpn_reconfigure($mode,& $settings) {
|
257
|
global $g, $config;
|
258
|
|
259
|
if (empty($settings))
|
260
|
return;
|
261
|
if ($settings['disable'])
|
262
|
return;
|
263
|
|
264
|
/*
|
265
|
* NOTE: Deleting tap devices causes spontaneous reboots. Instead,
|
266
|
* we use a vpnid number which is allocated for a particular client
|
267
|
* or server configuration. ( see openvpn_vpnid_next() )
|
268
|
*/
|
269
|
|
270
|
$vpnid = $settings['vpnid'];
|
271
|
$mode_id = $mode.$vpnid;
|
272
|
|
273
|
$tunname = "tun{$vpnid}";
|
274
|
if ($mode == "server")
|
275
|
$devname = "ovpns{$vpnid}";
|
276
|
else
|
277
|
$devname = "ovpnc{$vpnid}";
|
278
|
|
279
|
/* is our device already configured */
|
280
|
if (mwexec("/sbin/ifconfig {$devname}")) {
|
281
|
|
282
|
/* create the tap device if required */
|
283
|
if (!file_exists("/dev/{$tunname}"))
|
284
|
exec("/sbin/ifconfig {$tunname} create");
|
285
|
|
286
|
/* rename the device */
|
287
|
mwexec("/sbin/ifconfig {$tunname} name {$devname}");
|
288
|
|
289
|
/* add the device to the openvpn group */
|
290
|
mwexec("/sbin/ifconfig {$devname} group openvpn");
|
291
|
}
|
292
|
|
293
|
$pfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid";
|
294
|
$proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}");
|
295
|
$cipher = $settings['crypto'];
|
296
|
|
297
|
$interface = $settings['interface'];
|
298
|
if (!$interface)
|
299
|
$interface = 'WAN';
|
300
|
|
301
|
$iface = convert_friendly_interface_to_real_interface_name($interface);
|
302
|
$lines = explode(' ', trim(shell_exec("ifconfig {$iface} | grep inet | grep -v inet6")));
|
303
|
$iface_ip = $lines[1];
|
304
|
|
305
|
$conf = "dev {$devname}\n";
|
306
|
$conf .= "dev-type tun\n";
|
307
|
$conf .= "dev-node /dev/{$tunname}\n";
|
308
|
$conf .= "writepid {$pfile}\n";
|
309
|
$conf .= "#user nobody\n";
|
310
|
$conf .= "#group nobody\n";
|
311
|
$conf .= "daemon\n";
|
312
|
$conf .= "keepalive 10 60\n";
|
313
|
$conf .= "ping-timer-rem\n";
|
314
|
$conf .= "persist-tun\n";
|
315
|
$conf .= "persist-key\n";
|
316
|
$conf .= "proto {$proto}\n";
|
317
|
$conf .= "cipher {$cipher}\n";
|
318
|
$conf .= "up /etc/rc.filter_configure\n";
|
319
|
$conf .= "down /etc/rc.filter_configure\n";
|
320
|
$conf .= "local {$iface_ip}\n";
|
321
|
|
322
|
// server specific settings
|
323
|
|
324
|
if ($mode == 'server') {
|
325
|
|
326
|
list($ip, $mask) = explode('/', $settings['tunnel_network']);
|
327
|
$mask = gen_subnet_mask($mask);
|
328
|
|
329
|
// configure tls modes
|
330
|
switch($settings['mode']) {
|
331
|
case 'p2p_tls':
|
332
|
case 'server_tls':
|
333
|
case 'server_tls_user':
|
334
|
$conf .= "tls-server\n";
|
335
|
break;
|
336
|
}
|
337
|
|
338
|
// configure p2p/server modes
|
339
|
switch($settings['mode']) {
|
340
|
case 'p2p_tls':
|
341
|
case 'p2p_shared_key':
|
342
|
$baselong = ip2long($ip) & ip2long($mask);
|
343
|
$ip1 = long2ip($baselong + 1);
|
344
|
$ip2 = long2ip($baselong + 2);
|
345
|
$conf .= "ifconfig $ip1 $ip2\n";
|
346
|
break;
|
347
|
case 'server_tls':
|
348
|
case 'server_user':
|
349
|
case 'server_tls_user':
|
350
|
$conf .= "server {$ip} {$mask}\n";
|
351
|
$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc\n";
|
352
|
break;
|
353
|
}
|
354
|
|
355
|
// configure user auth modes
|
356
|
switch($settings['mode']) {
|
357
|
case 'server_user':
|
358
|
$conf .= "client-cert-not-required\n";
|
359
|
case 'server_tls_user':
|
360
|
$conf .= "username-as-common-name\n";
|
361
|
$conf .= "auth-user-pass-verify /etc/inc/openvpn.auth-user.php via-env\n";
|
362
|
break;
|
363
|
}
|
364
|
|
365
|
// The local port to listen on
|
366
|
$conf .= "lport {$settings['local_port']}\n";
|
367
|
|
368
|
// The management port to listen on
|
369
|
$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
|
370
|
|
371
|
if ($settings['maxclients'])
|
372
|
$conf .= "max-clients {$settings['maxclients']}\n";
|
373
|
|
374
|
// Can we push routes
|
375
|
if ($settings['local_network']) {
|
376
|
list($ip, $mask) = explode('/', $settings['local_network']);
|
377
|
$mask = gen_subnet_mask($mask);
|
378
|
$conf .= "push \"route $ip $mask\"\n";
|
379
|
}
|
380
|
|
381
|
// Configure client dhcp options
|
382
|
switch($settings['mode']) {
|
383
|
case 'server_tls':
|
384
|
case 'server_user':
|
385
|
case 'server_tls_user':
|
386
|
openvpn_add_dhcpopts($settings, $conf);
|
387
|
break;
|
388
|
}
|
389
|
}
|
390
|
|
391
|
// client specific settings
|
392
|
|
393
|
if ($mode == 'client') {
|
394
|
|
395
|
// configure p2p mode
|
396
|
switch($settings['mode']) {
|
397
|
case 'p2p_tls':
|
398
|
$conf .= "tls-client\n";
|
399
|
case 'shared_key':
|
400
|
$conf .= "client\n";
|
401
|
break;
|
402
|
}
|
403
|
|
404
|
// The port we'll listen at
|
405
|
if ($settings['local_port'])
|
406
|
$conf .= "lport {$settings['local_port']}\n";
|
407
|
else
|
408
|
$conf .= "nobind\n";
|
409
|
|
410
|
// The remote server
|
411
|
$conf .= "remote {$settings['server_addr']} {$settings['server_port']}\n";
|
412
|
|
413
|
if (!empty($settings['use_shaper']))
|
414
|
$conf .= "shaper {$settings['use_shaper']}\n";
|
415
|
|
416
|
if (!empty($settings['tunnel_network'])) {
|
417
|
list($ip, $mask) = explode('/', $settings['tunnel_network']);
|
418
|
$mask = gen_subnet_mask($mask);
|
419
|
$baselong = ip2long($ip) & ip2long($mask);
|
420
|
$ip1 = long2ip($baselong + 1);
|
421
|
$ip2 = long2ip($baselong + 2);
|
422
|
$conf .= "ifconfig $ip2 $ip1\n";
|
423
|
}
|
424
|
|
425
|
if ($settings['proxy_addr'])
|
426
|
$conf .= "http-proxy {$settings['proxy_addr']} {$settings['proxy_port']}\n";
|
427
|
}
|
428
|
|
429
|
// Add a remote network route if set
|
430
|
if ($settings['remote_network']) {
|
431
|
list($ip, $mask) = explode('/', $settings['remote_network']);
|
432
|
$mask = gen_subnet_mask($mask);
|
433
|
$conf .= "route $ip $mask\n";
|
434
|
}
|
435
|
|
436
|
// Write the settings for the keys
|
437
|
switch($settings['mode']) {
|
438
|
case 'p2p_shared_key':
|
439
|
openvpn_add_keyfile($settings['shared_key'], $conf, $mode_id, "secret");
|
440
|
break;
|
441
|
case 'p2p_tls':
|
442
|
case 'server_tls':
|
443
|
case 'server_tls_user':
|
444
|
$ca = lookup_ca($settings['caref']);
|
445
|
openvpn_add_keyfile($ca['crt'], $conf, $mode_id, "ca");
|
446
|
case 'server_user':
|
447
|
$cert = lookup_cert($settings['certref']);
|
448
|
openvpn_add_keyfile($cert['crt'], $conf, $mode_id, "cert");
|
449
|
openvpn_add_keyfile($cert['prv'], $conf, $mode_id, "key");
|
450
|
if ($mode == 'server')
|
451
|
$conf .= "dh {$g['etc_path']}/dh-parameters.{$settings['dh_length']}\n";
|
452
|
if ($settings['crl'])
|
453
|
openvpn_add_keyfile($settings['crl'], $conf, $mode_id, "crl-verify");
|
454
|
if ($settings['tls'])
|
455
|
openvpn_add_keyfile($settings['tls'], $conf, $mode_id, "tls-auth");
|
456
|
break;
|
457
|
}
|
458
|
|
459
|
if ($settings['compression'])
|
460
|
$conf .= "comp-lzo\n";
|
461
|
|
462
|
if ($settings['passtos'])
|
463
|
$conf .= "passtos\n";
|
464
|
|
465
|
if ($settings['resolve_retry'])
|
466
|
$conf .= "resolv-retry infinite\n";
|
467
|
|
468
|
if ($settings['dynamic_ip']) {
|
469
|
$conf .= "persist-remote-ip\n";
|
470
|
$conf .= "float\n";
|
471
|
}
|
472
|
|
473
|
openvpn_add_custom($settings, $conf);
|
474
|
|
475
|
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
|
476
|
file_put_contents($fpath, $conf);
|
477
|
chown($fpath, 'nobody');
|
478
|
chgrp($fpath, 'nobody');
|
479
|
}
|
480
|
|
481
|
function openvpn_restart($mode, & $settings) {
|
482
|
global $g, $config;
|
483
|
|
484
|
$vpnid = $settings['vpnid'];
|
485
|
$mode_id = $mode.$vpnid;
|
486
|
|
487
|
/* kill the process if running */
|
488
|
$pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid";
|
489
|
if (file_exists($pfile)) {
|
490
|
|
491
|
/* read the pid file */
|
492
|
$pid = rtrim(file_get_contents($pfile));
|
493
|
unlink($pfile);
|
494
|
|
495
|
/* send a term signal to the process */
|
496
|
posix_kill($pid, SIGTERM);
|
497
|
|
498
|
/* wait until the process exits */
|
499
|
while(posix_kill($pid, 0))
|
500
|
usleep(250000);
|
501
|
}
|
502
|
|
503
|
if ($settings['disable'])
|
504
|
return;
|
505
|
|
506
|
/* start the new process */
|
507
|
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
|
508
|
mwexec_bg("nohup openvpn --config {$fpath}");
|
509
|
touch("{$g['tmp_path']}/filter_dirty");
|
510
|
}
|
511
|
|
512
|
function openvpn_delete($mode, & $settings) {
|
513
|
global $g, $config;
|
514
|
|
515
|
$vpnid = $settings['vpnid'];
|
516
|
$mode_id = $mode.$vpnid;
|
517
|
|
518
|
$tunname = "tun{$vpnid}";
|
519
|
if ($mode == "server")
|
520
|
$devname = "ovpns{$vpnid}";
|
521
|
else
|
522
|
$devname = "ovpnc{$vpnid}";
|
523
|
|
524
|
/* kill the process if running */
|
525
|
$pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid";
|
526
|
if (file_exists($pfile)) {
|
527
|
|
528
|
/* read the pid file */
|
529
|
$pid = trim(file_get_contents($pfile));
|
530
|
unlink($pfile);
|
531
|
|
532
|
/* send a term signal to the process */
|
533
|
posix_kill($pid, SIGTERM);
|
534
|
}
|
535
|
|
536
|
/* remove the device from the openvpn group */
|
537
|
mwexec("/sbin/ifconfig {$devname} -group openvpn");
|
538
|
|
539
|
/* restore the original adapter name */
|
540
|
mwexec("/sbin/ifconfig {$devname} name {$tunname}");
|
541
|
|
542
|
/* remove the configuration files */
|
543
|
mwexec("/bin/rm {$g['varetc_path']}/openvpn/{$mode_id}.*");
|
544
|
}
|
545
|
|
546
|
function openvpn_resync_csc(& $settings) {
|
547
|
global $g, $config;
|
548
|
|
549
|
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
|
550
|
|
551
|
if ($settings['disable']) {
|
552
|
unlink_if_exists($fpath);
|
553
|
return;
|
554
|
}
|
555
|
|
556
|
$conf = '';
|
557
|
if ($settings['block'])
|
558
|
$conf .= "disable\n";
|
559
|
|
560
|
if ($settings['push_reset'])
|
561
|
$conf .= "push-reset\n";
|
562
|
|
563
|
if (!empty($settings['tunnel_network'])) {
|
564
|
list($ip, $mask) = explode('/', $settings['tunnel_network']);
|
565
|
$baselong = ip2long($ip) & gen_subnet_mask_long($mask);
|
566
|
$ip1 = long2ip($baselong + 1);
|
567
|
$ip2 = long2ip($baselong + 2);
|
568
|
$conf .= "ifconfig-push {$ip1} {$ip2}\n";
|
569
|
}
|
570
|
|
571
|
openvpn_add_dhcpopts($settings, $conf);
|
572
|
|
573
|
if ($settings['gwredir'])
|
574
|
$conf .= "push \"redirect-gateway def1\"\n";
|
575
|
|
576
|
openvpn_add_custom($settings, $conf);
|
577
|
|
578
|
file_put_contents($fpath, $conf);
|
579
|
chown($fpath, 'nobody');
|
580
|
chgrp($fpath, 'nobody');
|
581
|
}
|
582
|
|
583
|
function openvpn_delete_csc(& $settings) {
|
584
|
global $g, $config;
|
585
|
|
586
|
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
|
587
|
unlink_if_exists($fpath);
|
588
|
}
|
589
|
|
590
|
// Resync the configuration and restart the VPN
|
591
|
function openvpn_resync($mode, & $settings) {
|
592
|
openvpn_reconfigure($mode, $settings);
|
593
|
openvpn_restart($mode, $settings);
|
594
|
}
|
595
|
|
596
|
// Resync and restart all VPNs
|
597
|
function openvpn_resync_all() {
|
598
|
global $g, $config;
|
599
|
|
600
|
// delay our setup until the system
|
601
|
// has a chance to init our paths
|
602
|
if (!file_exists($g['varetc_path']."/openvpn") ||
|
603
|
!file_exists($g['varetc_path']."/openvpn-csc"))
|
604
|
return;
|
605
|
|
606
|
if (!is_array($config['openvpn']))
|
607
|
$config['openvpn'] = array();
|
608
|
|
609
|
/*
|
610
|
if (!$config['openvpn']['dh-parameters']) {
|
611
|
echo "Configuring OpenVPN Parameters ...\n";
|
612
|
$dh_parameters = openvpn_create_dhparams(1024);
|
613
|
$dh_parameters = base64_encode($dh_parameters);
|
614
|
$config['openvpn']['dh-parameters'] = $dh_parameters;
|
615
|
write_config("OpenVPN DH parameters");
|
616
|
}
|
617
|
|
618
|
$path_ovdh = $g['varetc_path']."/openvpn/dh-parameters";
|
619
|
if (!file_exists($path_ovdh)) {
|
620
|
$dh_parameters = $config['openvpn']['dh-parameters'];
|
621
|
$dh_parameters = base64_decode($dh_parameters);
|
622
|
file_put_contents($path_ovdh, $dh_parameters);
|
623
|
}
|
624
|
*/
|
625
|
|
626
|
if (is_array($config['openvpn']['openvpn-server']))
|
627
|
foreach ($config['openvpn']['openvpn-server'] as & $settings)
|
628
|
openvpn_resync('server', $settings);
|
629
|
|
630
|
if (is_array($config['openvpn']['openvpn-client']))
|
631
|
foreach ($config['openvpn']['openvpn-client'] as & $settings)
|
632
|
openvpn_resync('client', $settings);
|
633
|
|
634
|
if (is_array($config['openvpn']['openvpn-csc']))
|
635
|
foreach ($config['openvpn']['openvpn-csc'] as & $settings)
|
636
|
openvpn_resync_csc($settings);
|
637
|
|
638
|
}
|
639
|
|
640
|
?>
|