1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
|
<?php
/* $Id$ */
/*
$RCSfile$
Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
All rights reserved.
Copyright (C) 2006 Fernando Lemos
All rights reserved.
This file was rewritten from scratch by Fernando Lemos but
*MIGHT* contain code previously written by:
Copyright (C) 2005 Peter Allgeyer <allgeyer_AT_web.de>
All rights reserved.
Copyright (C) 2004 Peter Curran (peter@closeconsultants.com).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notices,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notices, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
DISABLE_PHP_LINT_CHECKING
pfSense_BUILDER_BINARIES: /usr/local/sbin/openvpn /usr/bin/openssl /sbin/ifconfig
pfSense_MODULE: openvpn
*/
require_once('config.inc');
require_once("certs.inc");
require_once('pfsense-utils.inc');
require_once("auth.inc");
$openvpn_prots = array("UDP", "TCP");
$openvpn_dev_mode = array("tun", "tap");
/*
* The User Auth mode below is disabled because
* OpenVPN erroneously requires that we provide
* a CA configuration parameter. In this mode,
* clients don't send a certificate so there is
* no need for a CA. If we require that admins
* provide one in the pfSense UI due to a bogus
* requirement imposed by OpenVPN, it could be
* considered very confusing ( I know I was ).
*
* -mgrooms
*/
$openvpn_dh_lengths = array(
1024, 2048, 4096 );
$openvpn_server_modes = array(
'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"),
'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )"),
'server_tls' => gettext("Remote Access ( SSL/TLS )"),
'server_user' => gettext("Remote Access ( User Auth )"),
'server_tls_user' => gettext("Remote Access ( SSL/TLS + User Auth )"));
$openvpn_client_modes = array(
'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"),
'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )") );
function openvpn_create_key() {
$fp = popen("/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null", "r");
if (!$fp)
return false;
$rslt = stream_get_contents($fp);
pclose($fp);
return $rslt;
}
function openvpn_create_dhparams($bits) {
$fp = popen("/usr/bin/openssl dhparam {$bits} 2>/dev/null", "r");
if (!$fp)
return false;
$rslt = stream_get_contents($fp);
pclose($fp);
return $rslt;
}
function openvpn_vpnid_used($vpnid) {
global $config;
if (is_array($config['openvpn']['openvpn-server']))
foreach ($config['openvpn']['openvpn-server'] as & $settings)
if ($vpnid == $settings['vpnid'])
return true;
if (is_array($config['openvpn']['openvpn-client']))
foreach ($config['openvpn']['openvpn-client'] as & $settings)
if ($vpnid == $settings['vpnid'])
return true;
return false;
}
function openvpn_vpnid_next() {
$vpnid = 1;
while(openvpn_vpnid_used($vpnid))
$vpnid++;
return $vpnid;
}
function openvpn_port_used($prot, $port) {
global $config;
if (is_array($config['openvpn']['openvpn-server']))
foreach ($config['openvpn']['openvpn-server'] as & $settings)
if ($port == $settings['local_port'] &&
$prot == $settings['protocol'])
return $settings['vpnid'];
if (is_array($config['openvpn']['openvpn-client']))
foreach ($config['openvpn']['openvpn-client'] as & $settings)
if ($port == $settings['local_port'] &&
$prot == $settings['protocol'])
return $settings['vpnid'];
return 0;
}
function openvpn_port_next($prot) {
$port = 1194;
while(openvpn_port_used($prot, $port))
$port++;
return $port;
}
function openvpn_get_cipherlist() {
$ciphers = array();
$cipher_out = shell_exec('openvpn --show-ciphers | grep "default key" | awk \'{print $1, "(" $2 "-" $3 ")";}\'');
$cipher_lines = explode("\n", trim($cipher_out));
sort($cipher_lines);
foreach ($cipher_lines as $line) {
$words = explode(' ', $line);
$ciphers[$words[0]] = "{$words[0]} {$words[1]}";
}
$ciphers["none"] = gettext("None (No Encryption)");
return $ciphers;
}
function openvpn_validate_host($value, $name) {
$value = trim($value);
if (empty($value) || (!is_domain($value) && !is_ipaddr($value)))
return sprintf(gettext("The field '%s' must contain a valid IP address or domain name."), $name);
return false;
}
function openvpn_validate_port($value, $name) {
$value = trim($value);
if (empty($value) || !is_numeric($value) || $value < 0 || ($value > 65535))
return sprintf(gettext("The field '%s' must contain a valid port, ranging from 0 to 65535."), $name);
return false;
}
function openvpn_validate_cidr($value, $name) {
$value = trim($value);
if (!empty($value)) {
list($ip, $mask) = explode('/', $value);
if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
return sprintf(gettext("The field '%s' must contain a valid CIDR range."), $name);
}
return false;
}
function openvpn_add_dhcpopts(& $settings, & $conf) {
if (!empty($settings['dns_domain']))
$conf .= "push \"dhcp-option DOMAIN {$settings['dns_domain']}\"\n";
if (!empty($settings['dns_server1']))
$conf .= "push \"dhcp-option DNS {$settings['dns_server1']}\"\n";
if (!empty($settings['dns_server2']))
$conf .= "push \"dhcp-option DNS {$settings['dns_server2']}\"\n";
if (!empty($settings['dns_server3']))
$conf .= "push \"dhcp-option DNS {$settings['dns_server3']}\"\n";
if (!empty($settings['dns_server4']))
$conf .= "push \"dhcp-option DNS {$settings['dns_server4']}\"\n";
if (!empty($settings['ntp_server1']))
$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
if (!empty($settings['ntp_server2']))
$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
if ($settings['netbios_enable']) {
if (!empty($settings['dhcp_nbttype']) && ($settings['dhcp_nbttype'] != 0))
$conf .= "push \"dhcp-option NBT {$settings['dhcp_nbttype']}\"\n";
if (!empty($settings['dhcp_nbtscope']))
$conf .= "push \"dhcp-option NBS {$settings['dhcp_nbtscope']}\"\n";
if (!empty($settings['wins_server1']))
$conf .= "push \"dhcp-option WINS {$settings['wins_server1']}\"\n";
if (!empty($settings['wins_server2']))
$conf .= "push \"dhcp-option WINS {$settings['wins_server2']}\"\n";
if (!empty($settings['nbdd_server1']))
$conf .= "push \"dhcp-option NBDD {$settings['nbdd_server1']}\"\n";
}
if ($settings['gwredir'])
$conf .= "push \"redirect-gateway def1\"\n";
}
function openvpn_add_custom(& $settings, & $conf) {
if ($settings['custom_options']) {
$options = explode(';', $settings['custom_options']);
if (is_array($options)) {
foreach ($options as $option)
$conf .= "$option\n";
} else
$conf .= "{$settings['custom_options']}\n";
}
}
function openvpn_add_keyfile(& $data, & $conf, $mode_id, $directive, $opt = "") {
global $g;
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}";
file_put_contents($fpath, base64_decode($data));
//chown($fpath, 'nobody');
//chgrp($fpath, 'nobody');
@chmod($fpath, 0600);
$conf .= "{$directive} {$fpath} {$opt}\n";
}
function openvpn_reconfigure($mode,& $settings) {
global $g, $config;
if (empty($settings))
return;
if (isset($settings['disable']))
return;
/*
* NOTE: Deleting tap devices causes spontaneous reboots. Instead,
* we use a vpnid number which is allocated for a particular client
* or server configuration. ( see openvpn_vpnid_next() )
*/
$vpnid = $settings['vpnid'];
$mode_id = $mode.$vpnid;
if (isset($settings['dev_mode']))
$tunname = "{$settings['dev_mode']}{$vpnid}";
else { /* defaults to tun */
$tunname = "tun{$vpnid}";
$settings['dev_mode'] = "tun";
}
if ($mode == "server")
$devname = "ovpns{$vpnid}";
else
$devname = "ovpnc{$vpnid}";
/* is our device already configured */
if (mwexec("/sbin/ifconfig {$devname}")) {
/* create the tap device if required */
if (!file_exists("/dev/{$tunname}"))
exec("/sbin/ifconfig {$tunname} create");
/* rename the device */
mwexec("/sbin/ifconfig {$tunname} name {$devname}");
/* add the device to the openvpn group */
mwexec("/sbin/ifconfig {$devname} group openvpn");
}
$pfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid";
$proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}");
$cipher = $settings['crypto'];
$interface = $settings['interface'];
$ipaddr = $settings['ipaddr'];
// If a specific ip address (VIP) is requested, use it.
// Otherwise, if a specific interface is requested, use it
// If "any" interface was selected, local directive will be ommited.
if (!empty($ipaddr)) {
$iface_ip=$ipaddr;
} else {
if ((!empty($interface)) && (strcmp($interface, "any"))) {
$iface_ip=get_interface_ip($interface);
}
}
$conf = "dev {$devname}\n";
$conf .= "dev-type {$settings['dev_mode']}\n";
$conf .= "dev-node /dev/{$tunname}\n";
$conf .= "writepid {$pfile}\n";
$conf .= "#user nobody\n";
$conf .= "#group nobody\n";
$conf .= "script-security 3\n";
$conf .= "daemon\n";
$conf .= "keepalive 10 60\n";
$conf .= "ping-timer-rem\n";
$conf .= "persist-tun\n";
$conf .= "persist-key\n";
$conf .= "proto {$proto}\n";
$conf .= "cipher {$cipher}\n";
$conf .= "up /usr/local/sbin/ovpn-linkup\n";
$conf .= "down /usr/local/sbin/ovpn-linkdown\n";
if (!empty($iface_ip)) {
$conf .= "local {$iface_ip}\n";
}
// server specific settings
if ($mode == 'server') {
list($ip, $mask) = explode('/', $settings['tunnel_network']);
$mask = gen_subnet_mask($mask);
// configure tls modes
switch($settings['mode']) {
case 'p2p_tls':
case 'server_tls':
case 'server_user':
case 'server_tls_user':
$conf .= "tls-server\n";
break;
}
// configure p2p/server modes
switch($settings['mode']) {
case 'p2p_tls':
case 'p2p_shared_key':
$baselong = ip2long32($ip) & ip2long($mask);
$ip1 = long2ip32($baselong + 1);
$ip2 = long2ip32($baselong + 2);
$conf .= "ifconfig $ip1 $ip2\n";
break;
case 'server_tls':
case 'server_user':
case 'server_tls_user':
$conf .= "server {$ip} {$mask}\n";
$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc\n";
break;
}
// configure user auth modes
switch($settings['mode']) {
case 'server_user':
$conf .= "client-cert-not-required\n";
case 'server_tls_user':
$conf .= "username-as-common-name\n";
if (!empty($settings['authmode'])) {
$authcfgs = explode(",", $settings['authmode']);
$sed = "\$authmodes=array(";
$firstsed = 0;
foreach ($authcfgs as $authcfg) {
if ($firstsed > 0)
$sed .= ",";
$firstsed = 1;
$sed .= "\"{$authcfg}\"";
}
$sed .= ");";
mwexec("/bin/cat /etc/inc/openvpn.auth-user.php | /usr/bin/sed 's/\/\/<template>/{$sed}/g' > {$g['varetc_path']}/openvpn/{$mode_id}.php");
mwexec("/bin/chmod a+x {$g['varetc_path']}/openvpn/{$mode_id}.php");
$conf .= "auth-user-pass-verify {$g['varetc_path']}/openvpn/{$mode_id}.php via-env\n";
}
break;
}
// The local port to listen on
$conf .= "lport {$settings['local_port']}\n";
// The management port to listen on
// Use unix socket to overcome the problem on any type of server
$conf .= "management {$g['varetc_path']}/openvpn/{$mode_id}.sock unix\n";
//$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
if ($settings['maxclients'])
$conf .= "max-clients {$settings['maxclients']}\n";
// Can we push routes
if ($settings['local_network']) {
list($ip, $mask) = explode('/', $settings['local_network']);
$mask = gen_subnet_mask($mask);
$conf .= "push \"route $ip $mask\"\n";
}
switch($settings['mode']) {
case 'server_tls':
case 'server_user':
case 'server_tls_user':
// Configure client dhcp options
openvpn_add_dhcpopts($settings, $conf);
if ($settings['client2client'])
$conf .= "client-to-client\n";
break;
}
}
// client specific settings
if ($mode == 'client') {
// configure p2p mode
switch($settings['mode']) {
case 'p2p_tls':
$conf .= "tls-client\n";
case 'shared_key':
$conf .= "client\n";
break;
}
// The port we'll listen at
// If local_port is used, bind the management port
if ($settings['local_port']) {
$conf .= "lport {$settings['local_port']}\n";
// Use unix socket to overcome the problem on any type of server
$conf .= "management {$g['varetc_path']}/openvpn/{$mode_id}.sock unix\n";
//$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
}
// If there is no bind option at all (ip and/or port), add "nobind" directive
if ((empty($iface_ip)) && (!$settings['local_port'])) {
$conf .= "nobind\n";
}
// The remote server
$conf .= "remote {$settings['server_addr']} {$settings['server_port']}\n";
if (!empty($settings['use_shaper']))
$conf .= "shaper {$settings['use_shaper']}\n";
if (!empty($settings['tunnel_network'])) {
list($ip, $mask) = explode('/', $settings['tunnel_network']);
$mask = gen_subnet_mask($mask);
$baselong = ip2long32($ip) & ip2long($mask);
$ip1 = long2ip32($baselong + 1);
$ip2 = long2ip32($baselong + 2);
$conf .= "ifconfig $ip2 $ip1\n";
}
if ($settings['proxy_addr']) {
$conf .= "http-proxy {$settings['proxy_addr']} {$settings['proxy_port']}";
if ($settings['proxy_authtype'] != "none") {
$conf .= " {$g['varetc_path']}/openvpn/{$mode_id}.pas {$settings['proxy_authtype']}";
$proxypas = "{$settings['proxy_user']}\n";
$proxypas .= "{$settings['proxy_passwd']}\n";
file_put_contents("{$g['varetc_path']}/openvpn/{$mode_id}.pas", $proxypas);
}
$conf .= " \n";
}
}
// Add a remote network route if set
if ($settings['remote_network']) {
list($ip, $mask) = explode('/', $settings['remote_network']);
$mask = gen_subnet_mask($mask);
$conf .= "route $ip $mask\n";
}
// Write the settings for the keys
switch($settings['mode']) {
case 'p2p_shared_key':
openvpn_add_keyfile($settings['shared_key'], $conf, $mode_id, "secret");
break;
case 'p2p_tls':
case 'server_tls':
case 'server_tls_user':
case 'server_user':
$ca = lookup_ca($settings['caref']);
openvpn_add_keyfile($ca['crt'], $conf, $mode_id, "ca");
$cert = lookup_cert($settings['certref']);
openvpn_add_keyfile($cert['crt'], $conf, $mode_id, "cert");
openvpn_add_keyfile($cert['prv'], $conf, $mode_id, "key");
if ($mode == 'server')
$conf .= "dh {$g['etc_path']}/dh-parameters.{$settings['dh_length']}\n";
if ($settings['crl'])
openvpn_add_keyfile($settings['crl'], $conf, $mode_id, "crl-verify");
if ($settings['tls']) {
if (stristr($settings['mode'], "server"))
$tlsopt = 0;
else
$tlsopt = 1;
openvpn_add_keyfile($settings['tls'], $conf, $mode_id, "tls-auth", $tlsopt);
}
break;
}
if ($settings['compression'])
$conf .= "comp-lzo\n";
if ($settings['passtos'])
$conf .= "passtos\n";
if ($settings['resolve_retry'])
$conf .= "resolv-retry infinite\n";
if ($settings['dynamic_ip']) {
$conf .= "persist-remote-ip\n";
$conf .= "float\n";
}
openvpn_add_custom($settings, $conf);
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
file_put_contents($fpath, $conf);
//chown($fpath, 'nobody');
//chgrp($fpath, 'nobody');
@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.conf", 0600);
@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.key", 0600);
@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.tls-auth", 0600);
@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.conf", 0600);
}
function openvpn_restart($mode, & $settings) {
global $g, $config;
$vpnid = $settings['vpnid'];
$mode_id = $mode.$vpnid;
/* kill the process if running */
$pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid";
if (file_exists($pfile)) {
/* read the pid file */
$pid = rtrim(file_get_contents($pfile));
unlink($pfile);
/* send a term signal to the process */
posix_kill($pid, SIGTERM);
/* wait until the process exits */
while(posix_kill($pid, 0))
usleep(250000);
}
if (isset($settings['disable']))
return;
/* start the new process */
$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
mwexec_bg("nohup openvpn --config {$fpath}");
touch("{$g['tmp_path']}/filter_dirty");
}
function openvpn_delete($mode, & $settings) {
global $g, $config;
$vpnid = $settings['vpnid'];
$mode_id = $mode.$vpnid;
$tunname = "tun{$vpnid}";
if ($mode == "server")
$devname = "ovpns{$vpnid}";
else
$devname = "ovpnc{$vpnid}";
/* kill the process if running */
$pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid";
if (file_exists($pfile)) {
/* read the pid file */
$pid = trim(file_get_contents($pfile));
unlink($pfile);
/* send a term signal to the process */
posix_kill($pid, SIGTERM);
}
/* remove the device from the openvpn group */
mwexec("/sbin/ifconfig {$devname} -group openvpn");
/* restore the original adapter name */
mwexec("/sbin/ifconfig {$devname} name {$tunname}");
/* remove the configuration files */
mwexec("/bin/rm {$g['varetc_path']}/openvpn/{$mode_id}.*");
}
function openvpn_resync_csc(& $settings) {
global $g, $config;
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
if (isset($settings['disable'])) {
unlink_if_exists($fpath);
return;
}
$conf = '';
if ($settings['block'])
$conf .= "disable\n";
if ($settings['push_reset'])
$conf .= "push-reset\n";
if (!empty($settings['tunnel_network'])) {
list($ip, $mask) = explode('/', $settings['tunnel_network']);
$baselong = ip2long32($ip) & gen_subnet_mask_long($mask);
$ip1 = long2ip32($baselong + 1);
$ip2 = long2ip32($baselong + 2);
$conf .= "ifconfig-push {$ip1} {$ip2}\n";
}
openvpn_add_dhcpopts($settings, $conf);
if ($settings['gwredir'])
$conf .= "push \"redirect-gateway def1\"\n";
openvpn_add_custom($settings, $conf);
file_put_contents($fpath, $conf);
chown($fpath, 'nobody');
chgrp($fpath, 'nobody');
}
function openvpn_delete_csc(& $settings) {
global $g, $config;
$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
unlink_if_exists($fpath);
}
// Resync the configuration and restart the VPN
function openvpn_resync($mode, & $settings) {
openvpn_reconfigure($mode, $settings);
openvpn_restart($mode, $settings);
}
// Resync and restart all VPNs
function openvpn_resync_all($interface = "") {
global $g, $config;
// delay our setup until the system
// has a chance to init our paths
if (!file_exists($g['varetc_path']."/openvpn") ||
!file_exists($g['varetc_path']."/openvpn-csc"))
return;
if (!is_array($config['openvpn']))
$config['openvpn'] = array();
/*
if (!$config['openvpn']['dh-parameters']) {
echo "Configuring OpenVPN Parameters ...\n";
$dh_parameters = openvpn_create_dhparams(1024);
$dh_parameters = base64_encode($dh_parameters);
$config['openvpn']['dh-parameters'] = $dh_parameters;
write_config("OpenVPN DH parameters");
}
$path_ovdh = $g['varetc_path']."/openvpn/dh-parameters";
if (!file_exists($path_ovdh)) {
$dh_parameters = $config['openvpn']['dh-parameters'];
$dh_parameters = base64_decode($dh_parameters);
file_put_contents($path_ovdh, $dh_parameters);
}
*/
if (is_array($config['openvpn']['openvpn-server'])) {
foreach ($config['openvpn']['openvpn-server'] as & $settings) {
if (!empty($interface) && $interface != $settings['interface'])
continue;
openvpn_resync('server', $settings);
}
}
if (is_array($config['openvpn']['openvpn-client'])) {
foreach ($config['openvpn']['openvpn-client'] as & $settings) {
if (!empty($interface) && $interface != $settings['interface'])
continue;
openvpn_resync('client', $settings);
}
}
if (is_array($config['openvpn']['openvpn-csc']))
foreach ($config['openvpn']['openvpn-csc'] as & $settings)
openvpn_resync_csc($settings);
}
function openvpn_get_active_servers() {
global $config, $g;
$servers = array();
if (is_array($config['openvpn']['openvpn-server'])) {
foreach ($config['openvpn']['openvpn-server'] as & $settings) {
$prot = $settings['protocol'];
$port = $settings['local_port'];
$server = array();
$server['port'] = $settings['local_port'];
$server['mode'] = $settings['mode'];
if ($settings['description'])
$server['name'] = "{$settings['description']} {$prot}:{$port}";
else
$server['name'] = "Server {$prot}:{$port}";
$server['conns'] = array();
$vpnid = $settings['vpnid'];
$mode_id = "server{$vpnid}";
$server['mgmt'] = $mode_id;
$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$mode_id}.sock";
$errval;
$errstr;
/* open a tcp connection to the management port of each server */
$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
if ($fp) {
stream_set_timeout($fp, 1);
/* send our status request */
fputs($fp, "status 2\n");
/* recv all response lines */
while (!feof($fp)) {
/* read the next line */
$line = fgets($fp, 1024);
$info = stream_get_meta_data($fp);
if ($info['timed_out'])
break;
/* parse header list line */
if (strstr($line, "HEADER"))
continue;
/* parse end of output line */
if (strstr($line, "END") || strstr($line, "ERROR"))
break;
/* parse client list line */
if (strstr($line, "CLIENT_LIST")) {
$list = explode(",", $line);
$conn = array();
$conn['common_name'] = $list[1];
$conn['remote_host'] = $list[2];
$conn['virtual_addr'] = $list[3];
$conn['bytes_recv'] = $list[4];
$conn['bytes_sent'] = $list[5];
$conn['connect_time'] = $list[6];
$server['conns'][] = $conn;
}
}
/* cleanup */
fclose($fp);
} else {
$conn = array();
$conn['common_name'] = "[error]";
$conn['remote_host'] = "Management Daemon Unreachable";
$conn['virtual_addr'] = "";
$conn['bytes_recv'] = 0;
$conn['bytes_sent'] = 0;
$conn['connect_time'] = 0;
$server['conns'][] = $conn;
}
$servers[] = $server;
}
}
return $servers;
}
function openvpn_get_active_clients() {
global $config, $g;
$clients = array();
if (is_array($config['openvpn']['openvpn-client'])) {
foreach ($config['openvpn']['openvpn-client'] as & $settings) {
$prot = $settings['protocol'];
$port = $settings['local_port'];
$client = array();
$client['port'] = $settings['local_port'];
if ($settings['description'])
$client['name'] = "{$settings['description']} {$prot}:{$port}";
else
$client['name'] = "Client {$prot}:{$port}";
$vpnid = $settings['vpnid'];
$mode_id = "client{$vpnid}";
$client['mgmt'] = $mode_id;
$tcpcli = "unix://{$g['varetc_path']}/openvpn/{$mode_id}.sock";
$errval;
$errstr;
$client['status']="down";
/* open a tcp connection to the management port of each cli */
$fp = @stream_socket_client($tcpcli, $errval, $errstr, 1);
if ($fp) {
stream_set_timeout($fp, 1);
/* send our status request */
fputs($fp, "state 1\n");
/* recv all response lines */
while (!feof($fp)) {
/* read the next line */
$line = fgets($fp, 1024);
$info = stream_get_meta_data($fp);
if ($info['timed_out'])
break;
/* Get the client state */
if (strstr($line,"CONNECTED")) {
$client['status']="up";
$list = explode(",", $line);
$client['connect_time'] = date("D M j G:i:s Y", $list[0]);
$client['virtual_addr'] = $list[3];
$client['remote_host'] = $list[4];
}
/* parse end of output line */
if (strstr($line, "END") || strstr($line, "ERROR"))
break;
}
/* If up, get read/write stats */
if (strcmp($client['status'], "up") == 0) {
fputs($fp, "status 2\n");
/* recv all response lines */
while (!feof($fp)) {
/* read the next line */
$line = fgets($fp, 1024);
$info = stream_get_meta_data($fp);
if ($info['timed_out'])
break;
if (strstr($line,"TCP/UDP read bytes")) {
$list = explode(",", $line);
$client['bytes_recv'] = $list[1];
}
if (strstr($line,"TCP/UDP write bytes")) {
$list = explode(",", $line);
$client['bytes_sent'] = $list[1];
}
/* parse end of output line */
if (strstr($line, "END"))
break;
}
}
fclose($fp);
} else {
$DisplayNote=true;
$client['remote_host'] = "No Management Daemon";
$client['virtual_addr'] = "See Note Below";
$client['bytes_recv'] = 0;
$client['bytes_sent'] = 0;
$client['connect_time'] = 0;
}
$clients[] = $client;
}
}
return $clients;
}
?>
|