Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
bffce791
Commit
bffce791
authored
Feb 11, 2016
by
Ad Schellevis
Committed by
Franco Fichtner
Feb 14, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(legacy) spaces and curly braces in openvpn.inc
(cherry picked from commit
f862e857
)
parent
85e1993d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1218 additions
and
1139 deletions
+1218
-1139
openvpn.inc
src/etc/inc/openvpn.inc
+1218
-1139
No files found.
src/etc/inc/openvpn.inc
View file @
bffce791
<?php
<?php
/*
/*
Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
Copyright (C) 2016 Deciso B.V.
Copyright (C) 2006 Fernando Lemos
Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
Copyright (C) 2005 Peter Allgeyer <allgeyer_AT_web.de>
Copyright (C) 2006 Fernando Lemos
Copyright (C) 2004 Peter Curran <peter@closeconsultants.com>
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:
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.
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
2. Redistributions in binary form must reproduce the above copyright
documentation and/or other materials provided with the distribution.
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
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
POSSIBILITY OF SUCH DAMAGE.
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
*/
global
$openvpn_verbosity_level
;
global
$openvpn_verbosity_level
;
$openvpn_verbosity_level
=
array
(
$openvpn_verbosity_level
=
array
(
0
=>
gettext
(
'0 (none)'
),
0
=>
gettext
(
'0 (none)'
),
1
=>
gettext
(
'1 (default)'
),
1
=>
gettext
(
'1 (default)'
),
2
=>
gettext
(
'2'
),
2
=>
gettext
(
'2'
),
3
=>
gettext
(
'3 (recommended)'
),
3
=>
gettext
(
'3 (recommended)'
),
4
=>
gettext
(
'4'
),
4
=>
gettext
(
'4'
),
5
=>
gettext
(
'5'
),
5
=>
gettext
(
'5'
),
6
=>
gettext
(
'6'
),
6
=>
gettext
(
'6'
),
7
=>
gettext
(
'7'
),
7
=>
gettext
(
'7'
),
8
=>
gettext
(
'8'
),
8
=>
gettext
(
'8'
),
9
=>
gettext
(
'9'
),
9
=>
gettext
(
'9'
),
10
=>
gettext
(
'10'
),
10
=>
gettext
(
'10'
),
11
=>
gettext
(
'11'
),
11
=>
gettext
(
'11'
),
);
);
/*
/*
...
@@ -60,1208 +61,1286 @@ $openvpn_verbosity_level = array(
...
@@ -60,1208 +61,1286 @@ $openvpn_verbosity_level = array(
global
$openvpn_compression_modes
;
global
$openvpn_compression_modes
;
$openvpn_compression_modes
=
array
(
$openvpn_compression_modes
=
array
(
''
=>
gettext
(
"No Preference"
),
''
=>
gettext
(
"No Preference"
),
'no'
=>
gettext
(
"Disabled - No Compression"
),
'no'
=>
gettext
(
"Disabled - No Compression"
),
'adaptive'
=>
gettext
(
"Enabled with Adaptive Compression"
),
'adaptive'
=>
gettext
(
"Enabled with Adaptive Compression"
),
'yes'
=>
gettext
(
"Enabled without Adaptive Compression"
));
'yes'
=>
gettext
(
"Enabled without Adaptive Compression"
)
);
function
openvpn_create_key
()
function
openvpn_create_key
()
{
{
$fp
=
popen
(
"/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null"
,
"r"
);
$fp
=
popen
(
"/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null"
,
"r"
);
if
(
!
$fp
)
{
if
(
!
$fp
)
{
return
false
;
return
false
;
}
}
$rslt
=
stream_get_contents
(
$fp
);
$rslt
=
stream_get_contents
(
$fp
);
pclose
(
$fp
);
pclose
(
$fp
);
return
$rslt
;
return
$rslt
;
}
}
function
openvpn_vpnid_used
(
$vpnid
)
function
openvpn_vpnid_used
(
$vpnid
)
{
{
global
$config
;
global
$config
;
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
if
(
$vpnid
==
$settings
[
'vpnid'
])
{
if
(
$vpnid
==
$settings
[
'vpnid'
])
{
return
true
;
return
true
;
}
}
}
}
}
}
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
if
(
$vpnid
==
$settings
[
'vpnid'
])
{
if
(
$vpnid
==
$settings
[
'vpnid'
])
{
return
true
;
return
true
;
}
}
}
}
}
}
return
false
;
return
false
;
}
}
function
openvpn_vpnid_next
()
function
openvpn_vpnid_next
()
{
{
$vpnid
=
1
;
$vpnid
=
1
;
while
(
openvpn_vpnid_used
(
$vpnid
))
{
while
(
openvpn_vpnid_used
(
$vpnid
))
$vpnid
++
;
$vpnid
++
;
}
return
$vpnid
;
return
$vpnid
;
}
}
function
openvpn_port_used
(
$prot
,
$interface
,
$port
,
$curvpnid
=
0
)
{
function
openvpn_port_used
(
$prot
,
$interface
,
$port
,
$curvpnid
=
0
)
{
global
$config
;
global
$config
;
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
if
(
isset
(
$settings
[
'disable'
]))
if
(
isset
(
$settings
[
'disable'
]))
{
continue
;
continue
;
}
if
(
$curvpnid
!=
0
&&
$curvpnid
==
$settings
[
'vpnid'
])
continue
;
if
(
$curvpnid
!=
0
&&
$curvpnid
==
$settings
[
'vpnid'
])
{
continue
;
if
(
$port
==
$settings
[
'local_port'
]
&&
$prot
==
$settings
[
'protocol'
]
&&
}
(
$interface
==
$settings
[
'interface'
]
||
$interface
==
"any"
||
$settings
[
'interface'
]
==
"any"
))
return
$settings
[
'vpnid'
];
if
(
$port
==
$settings
[
'local_port'
]
&&
$prot
==
$settings
[
'protocol'
]
&&
}
(
$interface
==
$settings
[
'interface'
]
||
$interface
==
"any"
||
$settings
[
'interface'
]
==
"any"
))
{
}
return
$settings
[
'vpnid'
];
}
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
}
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
}
if
(
isset
(
$settings
[
'disable'
]))
continue
;
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
if
(
$curvpnid
!=
0
&&
$curvpnid
==
$settings
[
'vpnid'
])
if
(
isset
(
$settings
[
'disable'
]))
{
continue
;
continue
;
}
if
(
$port
==
$settings
[
'local_port'
]
&&
$prot
==
$settings
[
'protocol'
]
&&
(
$interface
==
$settings
[
'interface'
]
||
$interface
==
"any"
||
$settings
[
'interface'
]
==
"any"
))
if
(
$curvpnid
!=
0
&&
$curvpnid
==
$settings
[
'vpnid'
])
{
return
$settings
[
'vpnid'
];
continue
;
}
}
}
if
(
$port
==
$settings
[
'local_port'
]
&&
$prot
==
$settings
[
'protocol'
]
&&
return
0
;
(
$interface
==
$settings
[
'interface'
]
||
$interface
==
"any"
||
$settings
[
'interface'
]
==
"any"
))
{
return
$settings
[
'vpnid'
];
}
}
}
return
0
;
}
}
function
openvpn_port_next
(
$prot
,
$interface
=
"wan"
)
{
function
openvpn_port_next
(
$prot
,
$interface
=
"wan"
)
{
$port
=
1194
;
$port
=
1194
;
while
(
openvpn_port_used
(
$prot
,
$interface
,
$port
))
while
(
openvpn_port_used
(
$prot
,
$interface
,
$port
))
{
$port
++
;
$port
++
;
while
(
openvpn_port_used
(
$prot
,
"any"
,
$port
))
}
$port
++
;
while
(
openvpn_port_used
(
$prot
,
"any"
,
$port
))
{
$port
++
;
return
$port
;
}
return
$port
;
}
}
function
openvpn_get_cipherlist
()
{
function
openvpn_get_cipherlist
()
{
$ciphers
=
array
();
$ciphers
=
array
();
$cipher_out
=
shell_exec
(
'/usr/local/sbin/openvpn --show-ciphers | /usr/bin/grep "default key" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\''
);
$cipher_out
=
shell_exec
(
'/usr/local/sbin/openvpn --show-ciphers | /usr/bin/grep "default key" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\''
);
$cipher_lines
=
explode
(
"
\n
"
,
trim
(
$cipher_out
));
$cipher_lines
=
explode
(
"
\n
"
,
trim
(
$cipher_out
));
sort
(
$cipher_lines
);
sort
(
$cipher_lines
);
foreach
(
$cipher_lines
as
$line
)
{
foreach
(
$cipher_lines
as
$line
)
{
$words
=
explode
(
' '
,
$line
);
$words
=
explode
(
' '
,
$line
);
$ciphers
[
$words
[
0
]]
=
"
{
$words
[
0
]
}
{
$words
[
1
]
}
"
;
$ciphers
[
$words
[
0
]]
=
"
{
$words
[
0
]
}
{
$words
[
1
]
}
"
;
}
}
$ciphers
[
"none"
]
=
gettext
(
"None (No Encryption)"
);
$ciphers
[
"none"
]
=
gettext
(
"None (No Encryption)"
);
return
$ciphers
;
return
$ciphers
;
}
}
function
openvpn_get_digestlist
()
{
function
openvpn_get_digestlist
()
{
$digests
=
array
();
$digests
=
array
();
$digest_out
=
shell_exec
(
'/usr/local/sbin/openvpn --show-digests | /usr/bin/grep "digest size" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\''
);
$digest_out
=
shell_exec
(
'/usr/local/sbin/openvpn --show-digests | /usr/bin/grep "digest size" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\''
);
$digest_lines
=
explode
(
"
\n
"
,
trim
(
$digest_out
));
$digest_lines
=
explode
(
"
\n
"
,
trim
(
$digest_out
));
sort
(
$digest_lines
);
sort
(
$digest_lines
);
foreach
(
$digest_lines
as
$line
)
{
foreach
(
$digest_lines
as
$line
)
{
$words
=
explode
(
' '
,
$line
);
$words
=
explode
(
' '
,
$line
);
$digests
[
$words
[
0
]]
=
"
{
$words
[
0
]
}
{
$words
[
1
]
}
"
;
$digests
[
$words
[
0
]]
=
"
{
$words
[
0
]
}
{
$words
[
1
]
}
"
;
}
}
$digests
[
"none"
]
=
gettext
(
"None (No Authentication)"
);
$digests
[
"none"
]
=
gettext
(
"None (No Authentication)"
);
return
$digests
;
return
$digests
;
}
}
function
openvpn_get_engines
()
function
openvpn_get_engines
()
{
{
$openssl_engines
=
array
(
'none'
=>
'No Hardware Crypto Acceleration'
);
$openssl_engines
=
array
(
'none'
=>
'No Hardware Crypto Acceleration'
);
exec
(
'/usr/local/bin/openssl engine -t -c'
,
$openssl_engine_output
);
exec
(
'/usr/local/bin/openssl engine -t -c'
,
$openssl_engine_output
);
if
(
!
count
(
$openssl_engine_output
))
{
if
(
!
count
(
$openssl_engine_output
))
{
/* LibreSSL doesn't offer anything of value */
/* LibreSSL doesn't offer anything of value */
return
$openssl_engines
;
return
$openssl_engines
;
}
}
$openssl_engine_output
=
implode
(
"
\n
"
,
$openssl_engine_output
);
$openssl_engine_output
=
implode
(
"
\n
"
,
$openssl_engine_output
);
$openssl_engine_output
=
preg_replace
(
"/
\\
n
\\
s+/"
,
"|"
,
$openssl_engine_output
);
$openssl_engine_output
=
preg_replace
(
"/
\\
n
\\
s+/"
,
"|"
,
$openssl_engine_output
);
$openssl_engine_output
=
explode
(
"
\n
"
,
$openssl_engine_output
);
$openssl_engine_output
=
explode
(
"
\n
"
,
$openssl_engine_output
);
foreach
(
$openssl_engine_output
as
$oeo
)
{
foreach
(
$openssl_engine_output
as
$oeo
)
{
$keep
=
true
;
$keep
=
true
;
$details
=
explode
(
"|"
,
$oeo
);
$details
=
explode
(
"|"
,
$oeo
);
$engine
=
array_shift
(
$details
);
$engine
=
array_shift
(
$details
);
$linematch
=
array
();
$linematch
=
array
();
preg_match
(
"/\((.*)\)\s(.*)/"
,
$engine
,
$linematch
);
preg_match
(
"/\((.*)\)\s(.*)/"
,
$engine
,
$linematch
);
foreach
(
$details
as
$dt
)
{
foreach
(
$details
as
$dt
)
{
if
(
strpos
(
$dt
,
"unavailable"
)
!==
FALSE
)
if
(
strpos
(
$dt
,
"unavailable"
)
!==
FALSE
)
{
$keep
=
false
;
$keep
=
false
;
if
(
strpos
(
$dt
,
"available"
)
!==
FALSE
)
}
continue
;
if
(
strpos
(
$dt
,
"available"
)
!==
FALSE
)
{
if
(
strpos
(
$dt
,
"["
)
!==
FALSE
)
continue
;
$ciphers
=
trim
(
$dt
,
"[]"
);
}
}
if
(
strpos
(
$dt
,
"["
)
!==
FALSE
)
{
if
(
!
empty
(
$ciphers
))
$ciphers
=
trim
(
$dt
,
"[]"
);
$ciphers
=
" - "
.
$ciphers
;
}
if
(
strlen
(
$ciphers
)
>
60
)
}
$ciphers
=
substr
(
$ciphers
,
0
,
60
)
.
" ... "
;
if
(
!
empty
(
$ciphers
))
{
if
(
$keep
)
$ciphers
=
" - "
.
$ciphers
;
$openssl_engines
[
$linematch
[
1
]]
=
$linematch
[
2
]
.
$ciphers
;
}
}
if
(
strlen
(
$ciphers
)
>
60
)
{
return
$openssl_engines
;
$ciphers
=
substr
(
$ciphers
,
0
,
60
)
.
" ... "
;
}
if
(
$keep
)
{
$openssl_engines
[
$linematch
[
1
]]
=
$linematch
[
2
]
.
$ciphers
;
}
}
return
$openssl_engines
;
}
}
function
openvpn_validate_engine
(
$engine
)
{
function
openvpn_validate_engine
(
$engine
)
$engines
=
openvpn_get_engines
();
{
return
array_key_exists
(
$engine
,
$engines
);
$engines
=
openvpn_get_engines
();
return
array_key_exists
(
$engine
,
$engines
);
}
}
function
openvpn_validate_port
(
$value
,
$name
)
{
function
openvpn_validate_port
(
$value
,
$name
)
$value
=
trim
(
$value
);
{
if
(
empty
(
$value
)
||
!
is_numeric
(
$value
)
||
$value
<
0
||
(
$value
>
65535
))
$value
=
trim
(
$value
);
return
sprintf
(
gettext
(
"The field '%s' must contain a valid port, ranging from 0 to 65535."
),
$name
);
if
(
empty
(
$value
)
||
!
is_numeric
(
$value
)
||
$value
<
0
||
(
$value
>
65535
))
{
return
false
;
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
,
$multiple
=
false
,
$ipproto
=
"ipv4"
)
{
function
openvpn_validate_cidr
(
$value
,
$name
,
$multiple
=
false
,
$ipproto
=
"ipv4"
)
$value
=
trim
(
$value
);
{
$error
=
false
;
$value
=
trim
(
$value
);
if
(
empty
(
$value
))
$error
=
false
;
return
false
;
if
(
empty
(
$value
))
{
$networks
=
explode
(
','
,
$value
);
return
false
;
}
if
(
!
$multiple
&&
(
count
(
$networks
)
>
1
))
$networks
=
explode
(
','
,
$value
);
return
sprintf
(
gettext
(
"The field '%s' must contain a single valid %s CIDR range."
),
$name
,
$ipproto
);
if
(
!
$multiple
&&
(
count
(
$networks
)
>
1
))
{
foreach
(
$networks
as
$network
)
{
return
sprintf
(
gettext
(
"The field '%s' must contain a single valid %s CIDR range."
),
$name
,
$ipproto
);
if
(
$ipproto
==
"ipv4"
)
}
$error
=
!
openvpn_validate_cidr_ipv4
(
$network
);
else
foreach
(
$networks
as
$network
)
{
$error
=
!
openvpn_validate_cidr_ipv6
(
$network
);
if
(
$ipproto
==
"ipv4"
)
{
if
(
$error
)
$error
=
!
openvpn_validate_cidr_ipv4
(
$network
);
break
;
}
else
{
}
$error
=
!
openvpn_validate_cidr_ipv6
(
$network
);
}
if
(
$error
)
if
(
$error
)
{
return
sprintf
(
gettext
(
"The field '%s' must contain only valid %s CIDR range(s) separated by commas."
),
$name
,
$ipproto
);
break
;
else
}
return
false
;
}
if
(
$error
)
{
return
sprintf
(
gettext
(
"The field '%s' must contain only valid %s CIDR range(s) separated by commas."
),
$name
,
$ipproto
);
}
else
{
return
false
;
}
}
}
function
openvpn_validate_cidr_ipv4
(
$value
)
{
function
openvpn_validate_cidr_ipv4
(
$value
)
{
$value
=
trim
(
$value
);
$value
=
trim
(
$value
);
if
(
!
empty
(
$value
))
{
if
(
!
empty
(
$value
))
{
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$value
);
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$value
);
if
(
!
is_ipaddrv4
(
$ip
)
or
!
is_numeric
(
$mask
)
or
(
$mask
>
32
)
or
(
$mask
<
0
))
if
(
!
is_ipaddrv4
(
$ip
)
or
!
is_numeric
(
$mask
)
or
(
$mask
>
32
)
or
(
$mask
<
0
))
{
return
false
;
return
false
;
}
}
return
true
;
}
return
true
;
}
}
function
openvpn_validate_cidr_ipv6
(
$value
)
{
function
openvpn_validate_cidr_ipv6
(
$value
)
$value
=
trim
(
$value
);
{
if
(
!
empty
(
$value
))
{
$value
=
trim
(
$value
);
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$value
);
if
(
!
empty
(
$value
))
{
if
(
empty
(
$prefix
))
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$value
);
$prefix
=
"128"
;
if
(
empty
(
$prefix
))
{
if
(
!
is_ipaddrv6
(
$ipv6
)
or
!
is_numeric
(
$prefix
)
or
(
$prefix
>
128
)
or
(
$prefix
<
0
))
$prefix
=
"128"
;
return
false
;
}
}
if
(
!
is_ipaddrv6
(
$ipv6
)
or
!
is_numeric
(
$prefix
)
or
(
$prefix
>
128
)
or
(
$prefix
<
0
))
{
return
true
;
return
false
;
}
}
return
true
;
}
}
function
openvpn_add_dhcpopts
(
&
$settings
,
&
$conf
)
{
function
openvpn_add_dhcpopts
(
&
$settings
,
&
$conf
)
{
if
(
!
empty
(
$settings
[
'dns_domain'
]))
if
(
!
empty
(
$settings
[
'dns_domain'
]))
{
$conf
.=
"push
\"
dhcp-option DOMAIN
{
$settings
[
'dns_domain'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option DOMAIN
{
$settings
[
'dns_domain'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'dns_server1'
]))
if
(
!
empty
(
$settings
[
'dns_server1'
]))
{
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server1'
]
}
\"\n
"
;
$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_server2'
]))
{
if
(
!
empty
(
$settings
[
'dns_server3'
]))
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server2'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server3'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'dns_server4'
]))
if
(
!
empty
(
$settings
[
'dns_server3'
]))
{
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server4'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server3'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'push_register_dns'
]))
if
(
!
empty
(
$settings
[
'dns_server4'
]))
{
$conf
.=
"push
\"
register-dns
\"\n
"
;
$conf
.=
"push
\"
dhcp-option DNS
{
$settings
[
'dns_server4'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'ntp_server1'
]))
$conf
.=
"push
\"
dhcp-option NTP
{
$settings
[
'ntp_server1'
]
}
\"\n
"
;
if
(
!
empty
(
$settings
[
'push_register_dns'
]))
{
if
(
!
empty
(
$settings
[
'ntp_server2'
]))
$conf
.=
"push
\"
register-dns
\"\n
"
;
$conf
.=
"push
\"
dhcp-option NTP
{
$settings
[
'ntp_server2'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'netbios_enable'
]))
{
if
(
!
empty
(
$settings
[
'ntp_server1'
]))
{
if
(
!
empty
(
$settings
[
'dhcp_nbttype'
])
&&
(
$settings
[
'dhcp_nbttype'
]
!=
0
))
$conf
.=
"push
\"
dhcp-option NTP
{
$settings
[
'ntp_server1'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option NBT
{
$settings
[
'dhcp_nbttype'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'dhcp_nbtscope'
]))
if
(
!
empty
(
$settings
[
'ntp_server2'
]))
{
$conf
.=
"push
\"
dhcp-option NBS
{
$settings
[
'dhcp_nbtscope'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option NTP
{
$settings
[
'ntp_server2'
]
}
\"\n
"
;
}
if
(
!
empty
(
$settings
[
'wins_server1'
]))
$conf
.=
"push
\"
dhcp-option WINS
{
$settings
[
'wins_server1'
]
}
\"\n
"
;
if
(
!
empty
(
$settings
[
'netbios_enable'
]))
{
if
(
!
empty
(
$settings
[
'wins_server2'
]))
if
(
!
empty
(
$settings
[
'dhcp_nbttype'
])
&&
(
$settings
[
'dhcp_nbttype'
]
!=
0
))
{
$conf
.=
"push
\"
dhcp-option WINS
{
$settings
[
'wins_server2'
]
}
\"\n
"
;
$conf
.=
"push
\"
dhcp-option NBT
{
$settings
[
'dhcp_nbttype'
]
}
\"\n
"
;
}
}
if
(
!
empty
(
$settings
[
'dhcp_nbtscope'
]))
{
if
(
!
empty
(
$settings
[
'gwredir'
]))
$conf
.=
"push
\"
dhcp-option NBS
{
$settings
[
'dhcp_nbtscope'
]
}
\"\n
"
;
$conf
.=
"push
\"
redirect-gateway def1
\"\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
[
'gwredir'
]))
{
$conf
.=
"push
\"
redirect-gateway def1
\"\n
"
;
}
}
}
function
openvpn_add_custom
(
&
$settings
,
&
$conf
)
{
function
openvpn_add_custom
(
&
$settings
,
&
$conf
)
{
if
(
$settings
[
'custom_options'
])
{
if
(
$settings
[
'custom_options'
])
{
$options
=
explode
(
';'
,
$settings
[
'custom_options'
]);
$options
=
explode
(
';'
,
$settings
[
'custom_options'
]);
if
(
is_array
(
$options
))
{
foreach
(
$options
as
$option
)
{
if
(
is_array
(
$options
))
{
$conf
.=
"
$option
\n
"
;
foreach
(
$options
as
$option
)
}
$conf
.=
"
$option
\n
"
;
}
else
{
}
else
$conf
.=
"
{
$settings
[
'custom_options'
]
}
\n
"
;
$conf
.=
"
{
$settings
[
'custom_options'
]
}
\n
"
;
}
}
}
}
}
function
openvpn_add_keyfile
(
&
$data
,
&
$conf
,
$mode_id
,
$directive
,
$opt
=
''
)
function
openvpn_add_keyfile
(
&
$data
,
&
$conf
,
$mode_id
,
$directive
,
$opt
=
''
)
{
{
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.
{
$directive
}
"
;
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.
{
$directive
}
"
;
openvpn_create_dirs
();
openvpn_create_dirs
();
file_put_contents
(
$fpath
,
base64_decode
(
$data
));
file_put_contents
(
$fpath
,
base64_decode
(
$data
));
//chown($fpath, 'nobody');
//chown($fpath, 'nobody');
//chgrp($fpath, 'nobody');
//chgrp($fpath, 'nobody');
@
chmod
(
$fpath
,
0600
);
@
chmod
(
$fpath
,
0600
);
$conf
.=
"
{
$directive
}
{
$fpath
}
{
$opt
}
\n
"
;
$conf
.=
"
{
$directive
}
{
$fpath
}
{
$opt
}
\n
"
;
}
}
function
openvpn_reconfigure
(
$mode
,
$settings
)
function
openvpn_reconfigure
(
$mode
,
$settings
)
{
{
global
$config
;
global
$config
;
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
{
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
{
return
;
return
;
}
}
openvpn_create_dirs
();
openvpn_create_dirs
();
/*
/*
* NOTE: Deleting tap devices causes spontaneous reboots. Instead,
* NOTE: Deleting tap devices causes spontaneous reboots. Instead,
* we use a vpnid number which is allocated for a particular client
* we use a vpnid number which is allocated for a particular client
* or server configuration. ( see openvpn_vpnid_next() )
* or server configuration. ( see openvpn_vpnid_next() )
*/
*/
$vpnid
=
$settings
[
'vpnid'
];
$vpnid
=
$settings
[
'vpnid'
];
$mode_id
=
$mode
.
$vpnid
;
$mode_id
=
$mode
.
$vpnid
;
if
(
isset
(
$settings
[
'dev_mode'
]))
{
if
(
isset
(
$settings
[
'dev_mode'
]))
{
$tunname
=
"
{
$settings
[
'dev_mode'
]
}{
$vpnid
}
"
;
$tunname
=
"
{
$settings
[
'dev_mode'
]
}{
$vpnid
}
"
;
}
else
{
/* defaults to tun */
}
else
{
/* defaults to tun */
$tunname
=
"tun
{
$vpnid
}
"
;
$tunname
=
"tun
{
$vpnid
}
"
;
$settings
[
'dev_mode'
]
=
"tun"
;
$settings
[
'dev_mode'
]
=
"tun"
;
}
}
if
(
$mode
==
"server"
)
{
if
(
$mode
==
"server"
)
{
$devname
=
"ovpns
{
$vpnid
}
"
;
$devname
=
"ovpns
{
$vpnid
}
"
;
}
else
{
}
else
{
$devname
=
"ovpnc
{
$vpnid
}
"
;
$devname
=
"ovpnc
{
$vpnid
}
"
;
}
}
/* is our device already configured */
/* is our device already configured */
if
(
!
does_interface_exist
(
$devname
))
{
if
(
!
does_interface_exist
(
$devname
))
{
/* create the tap device if required */
if
(
!
file_exists
(
"/dev/
{
$tunname
}
"
))
{
/* create the tap device if required */
exec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$tunname
)
.
" create"
);
if
(
!
file_exists
(
"/dev/
{
$tunname
}
"
))
}
exec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$tunname
)
.
" create"
);
/* rename the device */
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$tunname
)
.
" name "
.
escapeshellarg
(
$devname
));
/* rename the device */
/* add the device to the openvpn group */
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$tunname
)
.
" name "
.
escapeshellarg
(
$devname
));
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" group openvpn"
);
}
/* add the device to the openvpn group */
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" group openvpn"
);
$proto
=
strtolower
(
$settings
[
'protocol'
]);
}
if
(
substr
(
$settings
[
'protocol'
],
0
,
3
)
==
"TCP"
)
{
$proto
=
"
{
$proto
}
-
{
$mode
}
"
;
$proto
=
strtolower
(
$settings
[
'protocol'
]);
}
if
(
substr
(
$settings
[
'protocol'
],
0
,
3
)
==
"TCP"
)
$dev_mode
=
$settings
[
'dev_mode'
];
$proto
=
"
{
$proto
}
-
{
$mode
}
"
;
$cipher
=
$settings
[
'crypto'
];
$dev_mode
=
$settings
[
'dev_mode'
];
// OpenVPN defaults to SHA1, so use it when unset to maintain compatibility.
$cipher
=
$settings
[
'crypto'
];
$digest
=
!
empty
(
$settings
[
'digest'
])
?
$settings
[
'digest'
]
:
"SHA1"
;
// OpenVPN defaults to SHA1, so use it when unset to maintain compatibility.
$digest
=
!
empty
(
$settings
[
'digest'
])
?
$settings
[
'digest'
]
:
"SHA1"
;
$interface
=
get_failover_interface
(
$settings
[
'interface'
]);
$ipaddr
=
$settings
[
'ipaddr'
];
$interface
=
get_failover_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 a specific ip address (VIP) is requested, use it.
// If "any" interface was selected, local directive will be ommited.
// Otherwise, if a specific interface is requested, use it
if
(
is_ipaddr
(
$ipaddr
))
{
// If "any" interface was selected, local directive will be ommited.
$iface_ip
=
$ipaddr
;
if
(
is_ipaddr
(
$ipaddr
))
{
}
elseif
(
!
empty
(
$interface
)
&&
$interface
!=
"any"
)
{
$iface_ip
=
$ipaddr
;
$iface_ip
=
get_interface_ip
(
$interface
);
}
elseif
(
!
empty
(
$interface
)
&&
$interface
!=
"any"
)
{
if
(
empty
(
$iface_ip
))
{
$iface_ip
=
get_interface_ip
(
$interface
);
$iface_ip
=
get_interface_ipv6
(
$interface
);
if
(
empty
(
$iface_ip
))
{
}
$iface_ip
=
get_interface_ipv6
(
$interface
);
}
}
}
$conf
=
"dev
{
$devname
}
\n
"
;
if
(
isset
(
$settings
[
'verbosity_level'
]))
{
$conf
=
"dev
{
$devname
}
\n
"
;
$conf
.=
"verb
{
$settings
[
'verbosity_level'
]
}
\n
"
;
if
(
isset
(
$settings
[
'verbosity_level'
]))
{
}
$conf
.=
"verb
{
$settings
[
'verbosity_level'
]
}
\n
"
;
}
$conf
.=
"dev-type
{
$settings
[
'dev_mode'
]
}
\n
"
;
switch
(
$settings
[
'dev_mode'
])
{
$conf
.=
"dev-type
{
$settings
[
'dev_mode'
]
}
\n
"
;
case
"tun"
:
switch
(
$settings
[
'dev_mode'
])
{
if
(
!
$settings
[
'no_tun_ipv6'
])
{
case
"tun"
:
$conf
.=
"tun-ipv6
\n
"
;
if
(
!
$settings
[
'no_tun_ipv6'
])
{
}
$conf
.=
"tun-ipv6
\n
"
;
break
;
}
}
break
;
$conf
.=
"dev-node /dev/
{
$tunname
}
\n
"
;
}
$conf
.=
"writepid /var/run/openvpn_
{
$mode_id
}
.pid
\n
"
;
$conf
.=
"dev-node /dev/
{
$tunname
}
\n
"
;
$conf
.=
"#user nobody
\n
"
;
$conf
.=
"writepid /var/run/openvpn_
{
$mode_id
}
.pid
\n
"
;
$conf
.=
"#group nobody
\n
"
;
$conf
.=
"#user nobody
\n
"
;
$conf
.=
"script-security 3
\n
"
;
$conf
.=
"#group nobody
\n
"
;
$conf
.=
"daemon
\n
"
;
$conf
.=
"script-security 3
\n
"
;
$conf
.=
"keepalive 10 60
\n
"
;
$conf
.=
"daemon
\n
"
;
$conf
.=
"ping-timer-rem
\n
"
;
$conf
.=
"keepalive 10 60
\n
"
;
$conf
.=
"persist-tun
\n
"
;
$conf
.=
"ping-timer-rem
\n
"
;
$conf
.=
"persist-key
\n
"
;
$conf
.=
"persist-tun
\n
"
;
$conf
.=
"proto
{
$proto
}
\n
"
;
$conf
.=
"persist-key
\n
"
;
$conf
.=
"cipher
{
$cipher
}
\n
"
;
$conf
.=
"proto
{
$proto
}
\n
"
;
$conf
.=
"auth
{
$digest
}
\n
"
;
$conf
.=
"cipher
{
$cipher
}
\n
"
;
$conf
.=
"up /usr/local/sbin/ovpn-linkup
\n
"
;
$conf
.=
"auth
{
$digest
}
\n
"
;
$conf
.=
"down /usr/local/sbin/ovpn-linkdown
\n
"
;
$conf
.=
"up /usr/local/sbin/ovpn-linkup
\n
"
;
if
(
file_exists
(
"/usr/local/sbin/openvpn.attributes.sh"
))
{
$conf
.=
"down /usr/local/sbin/ovpn-linkdown
\n
"
;
switch
(
$settings
[
'mode'
])
{
if
(
file_exists
(
"/usr/local/sbin/openvpn.attributes.sh"
))
{
case
'server_user'
:
switch
(
$settings
[
'mode'
])
{
case
'server_tls_user'
:
case
'server_user'
:
$conf
.=
"client-connect /usr/local/sbin/openvpn.attributes.sh
\n
"
;
case
'server_tls_user'
:
$conf
.=
"client-disconnect /usr/local/sbin/openvpn.attributes.sh
\n
"
;
$conf
.=
"client-connect /usr/local/sbin/openvpn.attributes.sh
\n
"
;
break
;
$conf
.=
"client-disconnect /usr/local/sbin/openvpn.attributes.sh
\n
"
;
}
break
;
}
}
}
if
(
!
empty
(
$iface_ip
))
{
$conf
.=
"local
{
$iface_ip
}
\n
"
;
if
(
!
empty
(
$iface_ip
))
{
}
$conf
.=
"local
{
$iface_ip
}
\n
"
;
}
if
(
openvpn_validate_engine
(
$settings
[
'engine'
])
&&
(
$settings
[
'engine'
]
!=
"none"
))
{
$conf
.=
"engine
{
$settings
[
'engine'
]
}
\n
"
;
if
(
openvpn_validate_engine
(
$settings
[
'engine'
])
&&
(
$settings
[
'engine'
]
!=
"none"
))
}
$conf
.=
"engine
{
$settings
[
'engine'
]
}
\n
"
;
// server specific settings
// server specific settings
if
(
$mode
==
'server'
)
{
if
(
$mode
==
'server'
)
{
list
(
$ip
,
$cidr
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$settings
[
'tunnel_networkv6'
]);
list
(
$ip
,
$cidr
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
$mask
=
gen_subnet_mask
(
$cidr
);
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$settings
[
'tunnel_networkv6'
]);
$mask
=
gen_subnet_mask
(
$cidr
);
// configure tls modes
switch
(
$settings
[
'mode'
])
{
// configure tls modes
case
'p2p_tls'
:
switch
(
$settings
[
'mode'
])
{
case
'server_tls'
:
case
'p2p_tls'
:
case
'server_user'
:
case
'server_tls'
:
case
'server_tls_user'
:
case
'server_user'
:
$conf
.=
"tls-server
\n
"
;
case
'server_tls_user'
:
break
;
$conf
.=
"tls-server
\n
"
;
}
break
;
}
// configure p2p/server modes
switch
(
$settings
[
'mode'
])
{
// configure p2p/server modes
case
'p2p_tls'
:
switch
(
$settings
[
'mode'
])
{
// If the CIDR is less than a /30, OpenVPN will complain if you try to
case
'p2p_tls'
:
// use the server directive. It works for a single client without it.
// If the CIDR is less than a /30, OpenVPN will complain if you try to
// See ticket #1417
// use the server directive. It works for a single client without it.
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
)
&&
(
$cidr
<
30
))
{
// See ticket #1417
$conf
.=
"server
{
$ip
}
{
$mask
}
\n
"
;
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
)
&&
(
$cidr
<
30
))
{
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
$conf
.=
"server
{
$ip
}
{
$mask
}
\n
"
;
if
(
is_ipaddr
(
$ipv6
))
{
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
$conf
.=
"server-ipv6
{
$ipv6
}
/
{
$prefix
}
\n
"
;
if
(
is_ipaddr
(
$ipv6
))
}
$conf
.=
"server-ipv6
{
$ipv6
}
/
{
$prefix
}
\n
"
;
}
}
case
'p2p_shared_key'
:
case
'p2p_shared_key'
:
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
{
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
$conf
.=
"ifconfig
{
$ip1
}
{
$ip2
}
\n
"
;
$conf
.=
"ifconfig
{
$ip1
}
{
$ip2
}
\n
"
;
}
else
{
else
$conf
.=
"ifconfig
{
$ip1
}
{
$mask
}
\n
"
;
$conf
.=
"ifconfig
{
$ip1
}
{
$mask
}
\n
"
;
}
}
}
if
(
!
empty
(
$ipv6
)
&&
!
empty
(
$prefix
))
{
if
(
!
empty
(
$ipv6
)
&&
!
empty
(
$prefix
))
{
list
(
$ipv6_1
,
$ipv6_2
)
=
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
);
list
(
$ipv6_1
,
$ipv6_2
)
=
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
);
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
{
$conf
.=
"ifconfig-ipv6
{
$ipv6_1
}
{
$ipv6_2
}
\n
"
;
$conf
.=
"ifconfig-ipv6
{
$ipv6_1
}
{
$ipv6_2
}
\n
"
;
else
}
else
{
$conf
.=
"ifconfig-ipv6
{
$ipv6_1
}
{
$prefix
}
\n
"
;
$conf
.=
"ifconfig-ipv6
{
$ipv6_1
}
{
$prefix
}
\n
"
;
}
}
break
;
}
case
'server_tls'
:
break
;
case
'server_user'
:
case
'server_tls'
:
case
'server_tls_user'
:
case
'server_user'
:
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
case
'server_tls_user'
:
$conf
.=
"server
{
$ip
}
{
$mask
}
\n
"
;
if
(
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
if
(
is_ipaddr
(
$ipv6
))
$conf
.=
"server
{
$ip
}
{
$mask
}
\n
"
;
$conf
.=
"server-ipv6
{
$ipv6
}
/
{
$prefix
}
\n
"
;
if
(
is_ipaddr
(
$ipv6
))
{
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
$conf
.=
"server-ipv6
{
$ipv6
}
/
{
$prefix
}
\n
"
;
}
else
{
}
if
(
$settings
[
'serverbridge_dhcp'
])
{
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
if
((
!
empty
(
$settings
[
'serverbridge_interface'
]))
&&
(
strcmp
(
$settings
[
'serverbridge_interface'
],
"none"
)))
{
}
else
{
$biface_ip
=
get_interface_ip
(
$settings
[
'serverbridge_interface'
]);
if
(
$settings
[
'serverbridge_dhcp'
])
{
$biface_sm
=
gen_subnet_mask
(
get_interface_subnet
(
$settings
[
'serverbridge_interface'
]));
if
((
!
empty
(
$settings
[
'serverbridge_interface'
]))
&&
(
strcmp
(
$settings
[
'serverbridge_interface'
],
"none"
)))
{
if
(
is_ipaddrv4
(
$biface_ip
)
&&
is_ipaddrv4
(
$settings
[
'serverbridge_dhcp_start'
])
&&
is_ipaddrv4
(
$settings
[
'serverbridge_dhcp_end'
]))
{
$biface_ip
=
get_interface_ip
(
$settings
[
'serverbridge_interface'
]);
$conf
.=
"server-bridge
{
$biface_ip
}
{
$biface_sm
}
{
$settings
[
'serverbridge_dhcp_start'
]
}
{
$settings
[
'serverbridge_dhcp_end'
]
}
\n
"
;
$biface_sm
=
gen_subnet_mask
(
get_interface_subnet
(
$settings
[
'serverbridge_interface'
]));
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
if
(
is_ipaddrv4
(
$biface_ip
)
&&
is_ipaddrv4
(
$settings
[
'serverbridge_dhcp_start'
])
&&
is_ipaddrv4
(
$settings
[
'serverbridge_dhcp_end'
]))
{
}
else
{
$conf
.=
"server-bridge
{
$biface_ip
}
{
$biface_sm
}
{
$settings
[
'serverbridge_dhcp_start'
]
}
{
$settings
[
'serverbridge_dhcp_end'
]
}
\n
"
;
$conf
.=
"mode server
\n
"
;
$conf
.=
"client-config-dir /var/etc/openvpn-csc
\n
"
;
}
}
else
{
}
else
{
$conf
.=
"mode server
\n
"
;
$conf
.=
"mode server
\n
"
;
}
}
}
else
{
}
$conf
.=
"mode server
\n
"
;
}
}
break
;
}
}
}
break
;
// configure user auth modes
}
switch
(
$settings
[
'mode'
])
{
case
'server_user'
:
// configure user auth modes
$conf
.=
"client-cert-not-required
\n
"
;
switch
(
$settings
[
'mode'
])
{
case
'server_tls_user'
:
case
'server_user'
:
/* username-as-common-name is not compatible with server-bridge */
$conf
.=
"client-cert-not-required
\n
"
;
if
(
stristr
(
$conf
,
"server-bridge"
)
===
false
)
case
'server_tls_user'
:
$conf
.=
"username-as-common-name
\n
"
;
/* username-as-common-name is not compatible with server-bridge */
if
(
!
empty
(
$settings
[
'authmode'
]))
{
if
(
stristr
(
$conf
,
"server-bridge"
)
===
false
)
{
$strictusercn
=
"false"
;
$conf
.=
"username-as-common-name
\n
"
;
if
(
$settings
[
'strictusercn'
])
}
$strictusercn
=
"true"
;
if
(
!
empty
(
$settings
[
'authmode'
]))
{
$conf
.=
"auth-user-pass-verify
\"
/usr/local/sbin/ovpn_auth_verify user '
{
$settings
[
'authmode'
]
}
'
{
$strictusercn
}
{
$mode_id
}
\"
via-env
\n
"
;
$strictusercn
=
"false"
;
}
if
(
$settings
[
'strictusercn'
])
{
break
;
$strictusercn
=
"true"
;
}
}
if
(
!
isset
(
$settings
[
'cert_depth'
])
&&
(
strstr
(
$settings
[
'mode'
],
'tls'
)))
$conf
.=
"auth-user-pass-verify
\"
/usr/local/sbin/ovpn_auth_verify user '
{
$settings
[
'authmode'
]
}
'
{
$strictusercn
}
{
$mode_id
}
\"
via-env
\n
"
;
$settings
[
'cert_depth'
]
=
1
;
}
if
(
is_numeric
(
$settings
[
'cert_depth'
]))
{
break
;
if
((
$mode
==
'client'
)
&&
empty
(
$settings
[
'certref'
]))
}
$cert
=
""
;
if
(
!
isset
(
$settings
[
'cert_depth'
])
&&
(
strstr
(
$settings
[
'mode'
],
'tls'
)))
{
else
{
$settings
[
'cert_depth'
]
=
1
;
$cert
=
lookup_cert
(
$settings
[
'certref'
]);
}
/* XXX: Seems not used at all! */
if
(
is_numeric
(
$settings
[
'cert_depth'
]))
{
$servercn
=
urlencode
(
cert_get_cn
(
$cert
[
'crt'
]));
if
((
$mode
==
'client'
)
&&
empty
(
$settings
[
'certref'
]))
{
$conf
.=
"tls-verify
\"
/usr/local/sbin/ovpn_auth_verify tls '
{
$servercn
}
'
{
$settings
[
'cert_depth'
]
}
\"\n
"
;
$cert
=
""
;
}
}
else
{
}
$cert
=
lookup_cert
(
$settings
[
'certref'
]);
/* XXX: Seems not used at all! */
// The local port to listen on
$servercn
=
urlencode
(
cert_get_cn
(
$cert
[
'crt'
]));
$conf
.=
"lport
{
$settings
[
'local_port'
]
}
\n
"
;
$conf
.=
"tls-verify
\"
/usr/local/sbin/ovpn_auth_verify tls '
{
$servercn
}
'
{
$settings
[
'cert_depth'
]
}
\"\n
"
;
}
// The management port to listen on
}
// Use unix socket to overcome the problem on any type of server
$conf
.=
"management /var/etc/openvpn/
{
$mode_id
}
.sock unix
\n
"
;
// The local port to listen on
//$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
$conf
.=
"lport
{
$settings
[
'local_port'
]
}
\n
"
;
if
(
$settings
[
'maxclients'
])
// The management port to listen on
$conf
.=
"max-clients
{
$settings
[
'maxclients'
]
}
\n
"
;
// Use unix socket to overcome the problem on any type of server
$conf
.=
"management /var/etc/openvpn/
{
$mode_id
}
.sock unix
\n
"
;
// Can we push routes
//$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
if
(
$settings
[
'local_network'
])
{
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_network'
],
"ipv4"
,
true
);
if
(
$settings
[
'maxclients'
])
{
}
$conf
.=
"max-clients
{
$settings
[
'maxclients'
]
}
\n
"
;
if
(
$settings
[
'local_networkv6'
])
{
}
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_networkv6'
],
"ipv6"
,
true
);
}
// Can we push routes
if
(
$settings
[
'local_network'
])
{
switch
(
$settings
[
'mode'
])
{
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_network'
],
"ipv4"
,
true
);
case
'server_tls'
:
}
case
'server_user'
:
if
(
$settings
[
'local_networkv6'
])
{
case
'server_tls_user'
:
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_networkv6'
],
"ipv6"
,
true
);
// Configure client dhcp options
}
openvpn_add_dhcpopts
(
$settings
,
$conf
);
if
(
$settings
[
'client2client'
])
switch
(
$settings
[
'mode'
])
{
$conf
.=
"client-to-client
\n
"
;
case
'server_tls'
:
break
;
case
'server_user'
:
}
case
'server_tls_user'
:
if
(
isset
(
$settings
[
'duplicate_cn'
]))
// Configure client dhcp options
$conf
.=
"duplicate-cn
\n
"
;
openvpn_add_dhcpopts
(
$settings
,
$conf
);
}
if
(
$settings
[
'client2client'
])
{
$conf
.=
"client-to-client
\n
"
;
// client specific settings
}
break
;
if
(
$mode
==
'client'
)
{
}
if
(
isset
(
$settings
[
'duplicate_cn'
]))
{
// configure p2p mode
$conf
.=
"duplicate-cn
\n
"
;
switch
(
$settings
[
'mode'
])
{
}
case
'p2p_tls'
:
}
$conf
.=
"tls-client
\n
"
;
case
'shared_key'
:
// client specific settings
$conf
.=
"client
\n
"
;
if
(
$mode
==
'client'
)
{
break
;
// configure p2p mode
}
switch
(
$settings
[
'mode'
])
{
case
'p2p_tls'
:
// If there is no bind option at all (ip and/or port), add "nobind" directive
$conf
.=
"tls-client
\n
"
;
// Otherwise, use the local port if defined, failing that, use lport 0 to
case
'shared_key'
:
// ensure a random source port.
$conf
.=
"client
\n
"
;
if
((
empty
(
$iface_ip
))
&&
(
!
$settings
[
'local_port'
]))
break
;
$conf
.=
"nobind
\n
"
;
}
elseif
(
$settings
[
'local_port'
])
$conf
.=
"lport
{
$settings
[
'local_port'
]
}
\n
"
;
// If there is no bind option at all (ip and/or port), add "nobind" directive
else
// Otherwise, use the local port if defined, failing that, use lport 0 to
$conf
.=
"lport 0
\n
"
;
// ensure a random source port.
if
((
empty
(
$iface_ip
))
&&
(
!
$settings
[
'local_port'
]))
{
// Use unix socket to overcome the problem on any type of server
$conf
.=
"nobind
\n
"
;
$conf
.=
"management /var/etc/openvpn/
{
$mode_id
}
.sock unix
\n
"
;
}
elseif
(
$settings
[
'local_port'
])
{
$conf
.=
"lport
{
$settings
[
'local_port'
]
}
\n
"
;
// The remote server
}
else
{
$conf
.=
"remote
{
$settings
[
'server_addr'
]
}
{
$settings
[
'server_port'
]
}
\n
"
;
$conf
.=
"lport 0
\n
"
;
}
if
(
!
empty
(
$settings
[
'use_shaper'
]))
$conf
.=
"shaper
{
$settings
[
'use_shaper'
]
}
\n
"
;
// Use unix socket to overcome the problem on any type of server
$conf
.=
"management /var/etc/openvpn/
{
$mode_id
}
.sock unix
\n
"
;
if
(
!
empty
(
$settings
[
'tunnel_network'
]))
{
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
// The remote server
$mask
=
gen_subnet_mask
(
$mask
);
$conf
.=
"remote
{
$settings
[
'server_addr'
]
}
{
$settings
[
'server_port'
]
}
\n
"
;
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
if
(
!
empty
(
$settings
[
'use_shaper'
]))
{
$conf
.=
"ifconfig
{
$ip2
}
{
$ip1
}
\n
"
;
$conf
.=
"shaper
{
$settings
[
'use_shaper'
]
}
\n
"
;
else
}
$conf
.=
"ifconfig
{
$ip2
}
{
$mask
}
\n
"
;
}
if
(
!
empty
(
$settings
[
'tunnel_network'
]))
{
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
if
(
!
empty
(
$settings
[
'tunnel_networkv6'
]))
{
$mask
=
gen_subnet_mask
(
$mask
);
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$settings
[
'tunnel_networkv6'
]);
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
list
(
$ipv6_1
,
$ipv6_2
)
=
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
);
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
{
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
$conf
.=
"ifconfig
{
$ip2
}
{
$ip1
}
\n
"
;
$conf
.=
"ifconfig-ipv6
{
$ipv6_2
}
{
$ipv6_1
}
\n
"
;
}
else
{
else
$conf
.=
"ifconfig
{
$ip2
}
{
$mask
}
\n
"
;
$conf
.=
"ifconfig-ipv6
{
$ipv6_2
}
{
$prefix
}
\n
"
;
}
}
}
if
(
$settings
[
'auth_user'
]
&&
$settings
[
'auth_pass'
])
{
if
(
!
empty
(
$settings
[
'tunnel_networkv6'
]))
{
$up_file
=
"/var/etc/openvpn/
{
$mode_id
}
.up"
;
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
$settings
[
'tunnel_networkv6'
]);
$conf
.=
"auth-user-pass
{
$up_file
}
\n
"
;
list
(
$ipv6_1
,
$ipv6_2
)
=
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
);
$userpass
=
"
{
$settings
[
'auth_user'
]
}
\n
"
;
if
(
$settings
[
'dev_mode'
]
==
'tun'
)
{
$userpass
.=
"
{
$settings
[
'auth_pass'
]
}
\n
"
;
$conf
.=
"ifconfig-ipv6
{
$ipv6_2
}
{
$ipv6_1
}
\n
"
;
file_put_contents
(
$up_file
,
$userpass
);
}
else
{
}
$conf
.=
"ifconfig-ipv6
{
$ipv6_2
}
{
$prefix
}
\n
"
;
}
if
(
$settings
[
'proxy_addr'
])
{
}
$conf
.=
"http-proxy
{
$settings
[
'proxy_addr'
]
}
{
$settings
[
'proxy_port'
]
}
"
;
if
(
$settings
[
'proxy_authtype'
]
!=
"none"
)
{
if
(
$settings
[
'auth_user'
]
&&
$settings
[
'auth_pass'
])
{
$conf
.=
" /var/etc/openvpn/
{
$mode_id
}
.pas
{
$settings
[
'proxy_authtype'
]
}
"
;
$up_file
=
"/var/etc/openvpn/
{
$mode_id
}
.up"
;
$proxypas
=
"
{
$settings
[
'proxy_user'
]
}
\n
"
;
$conf
.=
"auth-user-pass
{
$up_file
}
\n
"
;
$proxypas
.=
"
{
$settings
[
'proxy_passwd'
]
}
\n
"
;
$userpass
=
"
{
$settings
[
'auth_user'
]
}
\n
"
;
file_put_contents
(
"/var/etc/openvpn/
{
$mode_id
}
.pas"
,
$proxypas
);
$userpass
.=
"
{
$settings
[
'auth_pass'
]
}
\n
"
;
}
file_put_contents
(
$up_file
,
$userpass
);
$conf
.=
"
\n
"
;
}
}
}
if
(
$settings
[
'proxy_addr'
])
{
$conf
.=
"http-proxy
{
$settings
[
'proxy_addr'
]
}
{
$settings
[
'proxy_port'
]
}
"
;
// Add a remote network route if set, and only for p2p modes.
if
(
$settings
[
'proxy_authtype'
]
!=
"none"
)
{
if
((
substr
(
$settings
[
'mode'
],
0
,
3
)
==
"p2p"
)
&&
(
openvpn_validate_cidr
(
$settings
[
'remote_network'
],
""
,
true
,
"ipv4"
)
===
FALSE
))
{
$conf
.=
" /var/etc/openvpn/
{
$mode_id
}
.pas
{
$settings
[
'proxy_authtype'
]
}
"
;
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_network'
],
"ipv4"
,
false
);
$proxypas
=
"
{
$settings
[
'proxy_user'
]
}
\n
"
;
}
$proxypas
.=
"
{
$settings
[
'proxy_passwd'
]
}
\n
"
;
// Add a remote network route if set, and only for p2p modes.
file_put_contents
(
"/var/etc/openvpn/
{
$mode_id
}
.pas"
,
$proxypas
);
if
((
substr
(
$settings
[
'mode'
],
0
,
3
)
==
"p2p"
)
&&
(
openvpn_validate_cidr
(
$settings
[
'remote_networkv6'
],
""
,
true
,
"ipv6"
)
===
FALSE
))
{
}
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_networkv6'
],
"ipv6"
,
false
);
$conf
.=
"
\n
"
;
}
}
}
// Write the settings for the keys
switch
(
$settings
[
'mode'
])
{
// Add a remote network route if set, and only for p2p modes.
case
'p2p_shared_key'
:
if
((
substr
(
$settings
[
'mode'
],
0
,
3
)
==
"p2p"
)
&&
(
openvpn_validate_cidr
(
$settings
[
'remote_network'
],
""
,
true
,
"ipv4"
)
===
FALSE
))
{
openvpn_add_keyfile
(
$settings
[
'shared_key'
],
$conf
,
$mode_id
,
"secret"
);
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_network'
],
"ipv4"
,
false
);
break
;
}
case
'p2p_tls'
:
// Add a remote network route if set, and only for p2p modes.
case
'server_tls'
:
if
((
substr
(
$settings
[
'mode'
],
0
,
3
)
==
"p2p"
)
&&
(
openvpn_validate_cidr
(
$settings
[
'remote_networkv6'
],
""
,
true
,
"ipv6"
)
===
FALSE
))
{
case
'server_tls_user'
:
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_networkv6'
],
"ipv6"
,
false
);
case
'server_user'
:
}
$ca
=
lookup_ca
(
$settings
[
'caref'
]);
openvpn_add_keyfile
(
$ca
[
'crt'
],
$conf
,
$mode_id
,
"ca"
);
// Write the settings for the keys
switch
(
$settings
[
'mode'
])
{
if
(
!
empty
(
$settings
[
'certref'
]))
{
case
'p2p_shared_key'
:
$cert
=
lookup_cert
(
$settings
[
'certref'
]);
openvpn_add_keyfile
(
$settings
[
'shared_key'
],
$conf
,
$mode_id
,
"secret"
);
openvpn_add_keyfile
(
$cert
[
'crt'
],
$conf
,
$mode_id
,
"cert"
);
break
;
openvpn_add_keyfile
(
$cert
[
'prv'
],
$conf
,
$mode_id
,
"key"
);
case
'p2p_tls'
:
}
case
'server_tls'
:
if
(
$mode
==
'server'
)
case
'server_tls_user'
:
$conf
.=
"dh /usr/local/etc/dh-parameters.
{
$settings
[
'dh_length'
]
}
\n
"
;
case
'server_user'
:
if
(
!
empty
(
$settings
[
'crlref'
]))
{
$ca
=
lookup_ca
(
$settings
[
'caref'
]);
$crl
=
lookup_crl
(
$settings
[
'crlref'
]);
openvpn_add_keyfile
(
$ca
[
'crt'
],
$conf
,
$mode_id
,
"ca"
);
crl_update
(
$crl
);
openvpn_add_keyfile
(
$crl
[
'text'
],
$conf
,
$mode_id
,
"crl-verify"
);
if
(
!
empty
(
$settings
[
'certref'
]))
{
}
$cert
=
lookup_cert
(
$settings
[
'certref'
]);
if
(
$settings
[
'tls'
])
{
openvpn_add_keyfile
(
$cert
[
'crt'
],
$conf
,
$mode_id
,
"cert"
);
if
(
$mode
==
"server"
)
openvpn_add_keyfile
(
$cert
[
'prv'
],
$conf
,
$mode_id
,
"key"
);
$tlsopt
=
0
;
}
else
if
(
$mode
==
'server'
)
{
$tlsopt
=
1
;
$conf
.=
"dh /usr/local/etc/dh-parameters.
{
$settings
[
'dh_length'
]
}
\n
"
;
openvpn_add_keyfile
(
$settings
[
'tls'
],
$conf
,
$mode_id
,
"tls-auth"
,
$tlsopt
);
}
}
if
(
!
empty
(
$settings
[
'crlref'
]))
{
break
;
$crl
=
lookup_crl
(
$settings
[
'crlref'
]);
}
crl_update
(
$crl
);
openvpn_add_keyfile
(
$crl
[
'text'
],
$conf
,
$mode_id
,
"crl-verify"
);
if
(
!
empty
(
$settings
[
'compression'
]))
}
$conf
.=
"comp-lzo
{
$settings
[
'compression'
]
}
\n
"
;
if
(
$settings
[
'tls'
])
{
if
(
$mode
==
"server"
)
{
if
(
$settings
[
'passtos'
])
$tlsopt
=
0
;
$conf
.=
"passtos
\n
"
;
}
else
{
$tlsopt
=
1
;
if
(
$settings
[
'resolve_retry'
])
}
$conf
.=
"resolv-retry infinite
\n
"
;
openvpn_add_keyfile
(
$settings
[
'tls'
],
$conf
,
$mode_id
,
"tls-auth"
,
$tlsopt
);
}
if
(
$settings
[
'dynamic_ip'
])
{
break
;
$conf
.=
"persist-remote-ip
\n
"
;
}
$conf
.=
"float
\n
"
;
}
if
(
!
empty
(
$settings
[
'compression'
]))
{
$conf
.=
"comp-lzo
{
$settings
[
'compression'
]
}
\n
"
;
if
(
$settings
[
'topology_subnet'
])
{
}
$conf
.=
"topology subnet
\n
"
;
}
if
(
$settings
[
'passtos'
])
{
$conf
.=
"passtos
\n
"
;
// New client features
}
if
(
$mode
==
"client"
)
{
// Dont pull routes checkbox
if
(
$settings
[
'resolve_retry'
])
{
if
(
$settings
[
'route_no_pull'
])
{
$conf
.=
"resolv-retry infinite
\n
"
;
$conf
.=
"route-nopull
\n
"
;
}
}
if
(
$settings
[
'dynamic_ip'
])
{
// Dont add/remove routes checkbox
$conf
.=
"persist-remote-ip
\n
"
;
if
(
$settings
[
'route_no_exec'
])
{
$conf
.=
"float
\n
"
;
$conf
.=
"route-noexec
\n
"
;
}
}
}
if
(
$settings
[
'topology_subnet'
])
{
$conf
.=
"topology subnet
\n
"
;
openvpn_add_custom
(
$settings
,
$conf
);
}
openvpn_create_dirs
();
// New client features
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.conf"
;
if
(
$mode
==
"client"
)
{
file_put_contents
(
$fpath
,
$conf
);
// Dont pull routes checkbox
unset
(
$conf
);
if
(
$settings
[
'route_no_pull'
])
{
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.interface"
;
$conf
.=
"route-nopull
\n
"
;
file_put_contents
(
$fpath
,
$interface
);
}
//chown($fpath, 'nobody');
//chgrp($fpath, 'nobody');
// Dont add/remove routes checkbox
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.conf"
,
0600
);
if
(
$settings
[
'route_no_exec'
])
{
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.interface"
,
0600
);
$conf
.=
"route-noexec
\n
"
;
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.key"
,
0600
);
}
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.tls-auth"
,
0600
);
}
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.conf"
,
0600
);
openvpn_add_custom
(
$settings
,
$conf
);
openvpn_create_dirs
();
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.conf"
;
file_put_contents
(
$fpath
,
$conf
);
unset
(
$conf
);
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.interface"
;
file_put_contents
(
$fpath
,
$interface
);
//chown($fpath, 'nobody');
//chgrp($fpath, 'nobody');
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.conf"
,
0600
);
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.interface"
,
0600
);
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.key"
,
0600
);
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.tls-auth"
,
0600
);
@
chmod
(
"/var/etc/openvpn/
{
$mode_id
}
.conf"
,
0600
);
}
}
function
openvpn_restart
(
$mode
,
$settings
)
function
openvpn_restart
(
$mode
,
$settings
)
{
{
global
$config
;
global
$config
;
$vpnid
=
$settings
[
'vpnid'
];
$vpnid
=
$settings
[
'vpnid'
];
$mode_id
=
$mode
.
$vpnid
;
$mode_id
=
$mode
.
$vpnid
;
/* kill the process if running */
/* kill the process if running */
$pfile
=
"/var/run/openvpn_
{
$mode_id
}
.pid"
;
$pfile
=
"/var/run/openvpn_
{
$mode_id
}
.pid"
;
killbypid
(
$pfile
);
killbypid
(
$pfile
);
while
(
isvalidpid
(
$pfile
))
{
while
(
isvalidpid
(
$pfile
))
{
usleep
(
250000
);
usleep
(
250000
);
}
}
if
(
isset
(
$settings
[
'disable'
]))
{
if
(
isset
(
$settings
[
'disable'
]))
{
return
;
return
;
}
}
/* Do not start a client if we are a CARP backup on this vip! */
/* Do not start a client if we are a CARP backup on this vip! */
if
((
$mode
==
"client"
)
&&
(
strstr
(
$settings
[
'interface'
],
"_vip"
)
&&
get_carp_interface_status
(
$settings
[
'interface'
])
==
"BACKUP"
))
{
if
((
$mode
==
"client"
)
&&
(
strstr
(
$settings
[
'interface'
],
"_vip"
)
&&
get_carp_interface_status
(
$settings
[
'interface'
])
==
"BACKUP"
))
{
return
;
return
;
}
}
/* Check if client is bound to a gateway group */
/* Check if client is bound to a gateway group */
$a_groups
=
return_gateway_groups_array
();
$a_groups
=
return_gateway_groups_array
();
if
(
is_array
(
$a_groups
[
$settings
[
'interface'
]]))
{
if
(
is_array
(
$a_groups
[
$settings
[
'interface'
]]))
{
/* the interface is a gateway group. If a vip is defined and its a CARP backup then do not start */
/* the interface is a gateway group. If a vip is defined and its a CARP backup then do not start */
if
((
$a_groups
[
$settings
[
'interface'
]][
0
][
'vip'
]
<>
""
)
&&
(
get_carp_interface_status
(
$a_groups
[
$settings
[
'interface'
]][
0
][
'vip'
])
==
"BACKUP"
))
if
((
$a_groups
[
$settings
[
'interface'
]][
0
][
'vip'
]
<>
""
)
&&
(
get_carp_interface_status
(
$a_groups
[
$settings
[
'interface'
]][
0
][
'vip'
])
==
"BACKUP"
))
{
return
;
return
;
}
}
}
/* start the new process */
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.conf"
;
/* start the new process */
openvpn_clear_route
(
$mode
,
$settings
);
$fpath
=
"/var/etc/openvpn/
{
$mode_id
}
.conf"
;
mwexec_bg
(
"/usr/local/sbin/openvpn --config "
.
escapeshellarg
(
$fpath
));
openvpn_clear_route
(
$mode
,
$settings
);
mwexec_bg
(
"/usr/local/sbin/openvpn --config "
.
escapeshellarg
(
$fpath
));
if
(
!
file_exists
(
"/var/run/booting"
))
configd_run
(
"filter reload"
);
if
(
!
file_exists
(
"/var/run/booting"
))
{
configd_run
(
"filter reload"
);
}
}
}
function
openvpn_delete
(
$mode
,
&
$settings
)
function
openvpn_delete
(
$mode
,
&
$settings
)
{
{
global
$config
;
global
$config
;
$vpnid
=
$settings
[
'vpnid'
];
$vpnid
=
$settings
[
'vpnid'
];
$mode_id
=
$mode
.
$vpnid
;
$mode_id
=
$mode
.
$vpnid
;
if
(
isset
(
$settings
[
'dev_mode'
]))
{
if
(
isset
(
$settings
[
'dev_mode'
]))
{
$tunname
=
"
{
$settings
[
'dev_mode'
]
}{
$vpnid
}
"
;
$tunname
=
"
{
$settings
[
'dev_mode'
]
}{
$vpnid
}
"
;
}
else
{
/* defaults to tun */
}
else
{
/* defaults to tun */
$tunname
=
"tun
{
$vpnid
}
"
;
$tunname
=
"tun
{
$vpnid
}
"
;
}
}
if
(
$mode
==
"server"
)
{
if
(
$mode
==
"server"
)
{
$devname
=
"ovpns
{
$vpnid
}
"
;
$devname
=
"ovpns
{
$vpnid
}
"
;
}
else
{
}
else
{
$devname
=
"ovpnc
{
$vpnid
}
"
;
$devname
=
"ovpnc
{
$vpnid
}
"
;
}
}
/* kill the process if running */
/* kill the process if running */
killbypid
(
"/var/run/openvpn_
{
$mode_id
}
.pid"
);
killbypid
(
"/var/run/openvpn_
{
$mode_id
}
.pid"
);
/* remove the device from the openvpn group */
/* remove the device from the openvpn group */
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" -group openvpn"
);
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" -group openvpn"
);
/* restore the original adapter name */
/* restore the original adapter name */
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" name "
.
escapeshellarg
(
$tunname
));
mwexec
(
"/sbin/ifconfig "
.
escapeshellarg
(
$devname
)
.
" name "
.
escapeshellarg
(
$tunname
));
/* remove the configuration files */
/* remove the configuration files */
@
array_map
(
'unlink'
,
glob
(
"/var/etc/openvpn/
{
$mode_id
}
.*"
));
@
array_map
(
'unlink'
,
glob
(
"/var/etc/openvpn/
{
$mode_id
}
.*"
));
}
}
function
openvpn_resync_csc
(
&
$settings
)
function
openvpn_resync_csc
(
&
$settings
)
{
{
global
$config
;
global
$config
;
$fpath
=
"/var/etc/openvpn-csc/
{
$settings
[
'common_name'
]
}
"
;
$fpath
=
"/var/etc/openvpn-csc/
{
$settings
[
'common_name'
]
}
"
;
if
(
isset
(
$settings
[
'disable'
]))
{
if
(
isset
(
$settings
[
'disable'
]))
{
@
unlink
(
$fpath
);
@
unlink
(
$fpath
);
return
;
return
;
}
}
openvpn_create_dirs
();
openvpn_create_dirs
();
$conf
=
''
;
$conf
=
''
;
if
(
!
empty
(
$settings
[
'block'
]))
if
(
!
empty
(
$settings
[
'block'
]))
{
$conf
.=
"disable
\n
"
;
$conf
.=
"disable
\n
"
;
}
if
(
!
empty
(
$settings
[
'push_reset'
]))
$conf
.=
"push-reset
\n
"
;
if
(
!
empty
(
$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
);
if
(
!
empty
(
$settings
[
'tunnel_network'
]))
{
$serverip
=
long2ip32
(
$baselong
+
1
);
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
$clientip
=
long2ip32
(
$baselong
+
2
);
$baselong
=
ip2long32
(
$ip
)
&
gen_subnet_mask_long
(
$mask
);
/* Because this is being pushed, the order from the client's point of view. */
$serverip
=
long2ip32
(
$baselong
+
1
);
if
(
$settings
[
'dev_mode'
]
!=
'tap'
)
$clientip
=
long2ip32
(
$baselong
+
2
);
$conf
.=
"ifconfig-push
{
$clientip
}
{
$serverip
}
\n
"
;
/* Because this is being pushed, the order from the client's point of view. */
else
if
(
$settings
[
'dev_mode'
]
!=
'tap'
)
{
$conf
.=
"ifconfig-push
{
$clientip
}
{
$mask
}
\n
"
;
$conf
.=
"ifconfig-push
{
$clientip
}
{
$serverip
}
\n
"
;
}
}
else
{
$conf
.=
"ifconfig-push
{
$clientip
}
{
$mask
}
\n
"
;
if
(
$settings
[
'local_network'
])
{
}
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_network'
],
"ipv4"
,
true
);
}
}
if
(
$settings
[
'local_networkv6'
])
{
if
(
$settings
[
'local_network'
])
{
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_networkv6'
],
"ipv6"
,
true
);
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_network'
],
"ipv4"
,
true
);
}
}
if
(
$settings
[
'local_networkv6'
])
{
// Add a remote network iroute if set
$conf
.=
openvpn_gen_routes
(
$settings
[
'local_networkv6'
],
"ipv6"
,
true
);
if
(
openvpn_validate_cidr
(
$settings
[
'remote_network'
],
""
,
true
,
"ipv4"
)
===
FALSE
)
{
}
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_network'
],
"ipv4"
,
false
,
true
);
}
// Add a remote network iroute if set
// Add a remote network iroute if set
if
(
openvpn_validate_cidr
(
$settings
[
'remote_network'
],
""
,
true
,
"ipv4"
)
===
FALSE
)
{
if
(
openvpn_validate_cidr
(
$settings
[
'remote_networkv6'
],
""
,
true
,
"ipv6"
)
===
FALSE
)
{
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_network'
],
"ipv4"
,
false
,
true
);
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_networkv6'
],
"ipv6"
,
false
,
true
);
}
}
// Add a remote network iroute if set
if
(
openvpn_validate_cidr
(
$settings
[
'remote_networkv6'
],
""
,
true
,
"ipv6"
)
===
FALSE
)
{
openvpn_add_dhcpopts
(
$settings
,
$conf
);
$conf
.=
openvpn_gen_routes
(
$settings
[
'remote_networkv6'
],
"ipv6"
,
false
,
true
);
}
if
(
!
empty
(
$settings
[
'gwredir'
]))
$conf
.=
"push
\"
redirect-gateway def1
\"\n
"
;
openvpn_add_dhcpopts
(
$settings
,
$conf
);
openvpn_add_custom
(
$settings
,
$conf
);
if
(
!
empty
(
$settings
[
'gwredir'
]))
{
$conf
.=
"push
\"
redirect-gateway def1
\"\n
"
;
file_put_contents
(
$fpath
,
$conf
);
}
chown
(
$fpath
,
'nobody'
);
chgrp
(
$fpath
,
'nobody'
);
openvpn_add_custom
(
$settings
,
$conf
);
file_put_contents
(
$fpath
,
$conf
);
chown
(
$fpath
,
'nobody'
);
chgrp
(
$fpath
,
'nobody'
);
}
}
function
openvpn_resync
(
$mode
,
$settings
)
function
openvpn_resync
(
$mode
,
$settings
)
{
{
openvpn_reconfigure
(
$mode
,
$settings
);
openvpn_reconfigure
(
$mode
,
$settings
);
openvpn_restart
(
$mode
,
$settings
);
openvpn_restart
(
$mode
,
$settings
);
}
}
function
openvpn_resync_all
(
$interface
=
''
)
function
openvpn_resync_all
(
$interface
=
''
)
{
{
global
$config
;
global
$config
;
openvpn_create_dirs
();
openvpn_create_dirs
();
if
(
!
isset
(
$config
[
'openvpn'
])
||
!
is_array
(
$config
[
'openvpn'
]))
{
if
(
!
isset
(
$config
[
'openvpn'
])
||
!
is_array
(
$config
[
'openvpn'
]))
{
return
;
return
;
}
}
if
(
$interface
<>
""
)
if
(
$interface
<>
""
)
{
log_error
(
"Resyncing OpenVPN instances for interface "
.
convert_friendly_interface_to_friendly_descr
(
$interface
)
.
"."
);
log_error
(
"Resyncing OpenVPN instances for interface "
.
convert_friendly_interface_to_friendly_descr
(
$interface
)
.
"."
);
else
}
else
{
log_error
(
"Resyncing OpenVPN instances."
);
log_error
(
"Resyncing OpenVPN instances."
);
}
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
if
(
$interface
<>
""
&&
$interface
!=
$settings
[
'interface'
])
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
continue
;
if
(
$interface
<>
""
&&
$interface
!=
$settings
[
'interface'
])
{
openvpn_resync
(
'server'
,
$settings
);
continue
;
}
}
}
openvpn_resync
(
'server'
,
$settings
);
}
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
}
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
if
(
$interface
<>
""
&&
$interface
!=
$settings
[
'interface'
])
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
continue
;
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
openvpn_resync
(
'client'
,
$settings
);
if
(
$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
);
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-csc'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-csc'
]
as
&
$settings
)
{
openvpn_resync_csc
(
$settings
);
}
}
}
}
function
openvpn_get_active_servers
(
$type
=
'multipoint'
)
function
openvpn_get_active_servers
(
$type
=
'multipoint'
)
{
{
global
$config
;
global
$config
;
$servers
=
array
();
$servers
=
array
();
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
])
&&
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-server'
])
&&
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
&
$settings
)
{
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
{
continue
;
continue
;
}
$prot
=
$settings
[
'protocol'
];
$port
=
$settings
[
'local_port'
];
$prot
=
$settings
[
'protocol'
];
$port
=
$settings
[
'local_port'
];
$server
=
array
();
$server
[
'port'
]
=
(
$settings
[
'local_port'
])
?
$settings
[
'local_port'
]
:
1194
;
$server
=
array
();
$server
[
'mode'
]
=
$settings
[
'mode'
];
$server
[
'port'
]
=
(
$settings
[
'local_port'
])
?
$settings
[
'local_port'
]
:
1194
;
if
(
$settings
[
'description'
])
$server
[
'mode'
]
=
$settings
[
'mode'
];
$server
[
'name'
]
=
"
{
$settings
[
'description'
]
}
{
$prot
}
:
{
$port
}
"
;
if
(
$settings
[
'description'
])
{
else
$server
[
'name'
]
=
"
{
$settings
[
'description'
]
}
{
$prot
}
:
{
$port
}
"
;
$server
[
'name'
]
=
"Server
{
$prot
}
:
{
$port
}
"
;
}
else
{
$server
[
'conns'
]
=
array
();
$server
[
'name'
]
=
"Server
{
$prot
}
:
{
$port
}
"
;
$server
[
'vpnid'
]
=
$settings
[
'vpnid'
];
}
$server
[
'mgmt'
]
=
"server
{
$server
[
'vpnid'
]
}
"
;
$server
[
'conns'
]
=
array
();
$socket
=
"unix:///var/etc/openvpn/
{
$server
[
'mgmt'
]
}
.sock"
;
$server
[
'vpnid'
]
=
$settings
[
'vpnid'
];
list
(
$tn
,
$sm
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
$server
[
'mgmt'
]
=
"server
{
$server
[
'vpnid'
]
}
"
;
$socket
=
"unix:///var/etc/openvpn/
{
$server
[
'mgmt'
]
}
.sock"
;
if
(((
$server
[
'mode'
]
==
"p2p_shared_key"
)
||
(
$sm
>=
30
)
)
&&
(
$type
==
"p2p"
))
list
(
$tn
,
$sm
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
$servers
[]
=
openvpn_get_client_status
(
$server
,
$socket
);
elseif
((
$server
[
'mode'
]
!=
"p2p_shared_key"
)
&&
(
$type
==
"multipoint"
)
&&
(
$sm
<
30
))
if
(((
$server
[
'mode'
]
==
"p2p_shared_key"
)
||
(
$sm
>=
30
)
)
&&
(
$type
==
"p2p"
))
{
$servers
[]
=
openvpn_get_server_status
(
$server
,
$socket
);
$servers
[]
=
openvpn_get_client_status
(
$server
,
$socket
);
}
elseif
((
$server
[
'mode'
]
!=
"p2p_shared_key"
)
&&
(
$type
==
"multipoint"
)
&&
(
$sm
<
30
))
{
}
$servers
[]
=
openvpn_get_server_status
(
$server
,
$socket
);
}
}
return
$servers
;
}
}
return
$servers
;
}
}
function
openvpn_get_server_status
(
$server
,
$socket
)
{
function
openvpn_get_server_status
(
$server
,
$socket
)
$errval
;
{
$errstr
;
$errval
;
$fp
=
@
stream_socket_client
(
$socket
,
$errval
,
$errstr
,
1
);
$errstr
;
if
(
$fp
)
{
$fp
=
@
stream_socket_client
(
$socket
,
$errval
,
$errstr
,
1
);
stream_set_timeout
(
$fp
,
1
);
if
(
$fp
)
{
stream_set_timeout
(
$fp
,
1
);
/* send our status request */
fputs
(
$fp
,
"status 2
\n
"
);
/* send our status request */
fputs
(
$fp
,
"status 2
\n
"
);
/* recv all response lines */
while
(
!
feof
(
$fp
))
{
/* recv all response lines */
while
(
!
feof
(
$fp
))
{
/* read the next line */
/* read the next line */
$line
=
fgets
(
$fp
,
1024
);
$line
=
fgets
(
$fp
,
1024
);
$info
=
stream_get_meta_data
(
$fp
);
$info
=
stream_get_meta_data
(
$fp
);
if
(
$info
[
'timed_out'
])
{
if
(
$info
[
'timed_out'
])
break
;
break
;
}
/* parse header list line */
/* parse header list line */
if
(
strstr
(
$line
,
"HEADER"
))
{
if
(
strstr
(
$line
,
"HEADER"
))
continue
;
continue
;
}
/* parse end of output line */
/* parse end of output line */
if
(
strstr
(
$line
,
"END"
)
||
strstr
(
$line
,
"ERROR"
))
{
if
(
strstr
(
$line
,
"END"
)
||
strstr
(
$line
,
"ERROR"
))
break
;
break
;
}
/* parse client list line */
/* parse client list line */
if
(
strstr
(
$line
,
"CLIENT_LIST"
))
{
if
(
strstr
(
$line
,
"CLIENT_LIST"
))
{
$list
=
explode
(
","
,
$line
);
$list
=
explode
(
","
,
$line
);
$conn
=
array
();
$conn
=
array
();
$conn
[
'common_name'
]
=
$list
[
1
];
$conn
[
'common_name'
]
=
$list
[
1
];
$conn
[
'remote_host'
]
=
$list
[
2
];
$conn
[
'remote_host'
]
=
$list
[
2
];
$conn
[
'virtual_addr'
]
=
$list
[
3
];
$conn
[
'virtual_addr'
]
=
$list
[
3
];
$conn
[
'bytes_recv'
]
=
$list
[
4
];
$conn
[
'bytes_recv'
]
=
$list
[
4
];
$conn
[
'bytes_sent'
]
=
$list
[
5
];
$conn
[
'bytes_sent'
]
=
$list
[
5
];
$conn
[
'connect_time'
]
=
$list
[
6
];
$conn
[
'connect_time'
]
=
$list
[
6
];
$server
[
'conns'
][]
=
$conn
;
$server
[
'conns'
][]
=
$conn
;
}
}
/* parse routing table lines */
/* parse routing table lines */
if
(
strstr
(
$line
,
"ROUTING_TABLE"
))
{
if
(
strstr
(
$line
,
"ROUTING_TABLE"
))
{
$list
=
explode
(
","
,
$line
);
$list
=
explode
(
","
,
$line
);
$conn
=
array
();
$conn
=
array
();
$conn
[
'virtual_addr'
]
=
$list
[
1
];
$conn
[
'virtual_addr'
]
=
$list
[
1
];
$conn
[
'common_name'
]
=
$list
[
2
];
$conn
[
'common_name'
]
=
$list
[
2
];
$conn
[
'remote_host'
]
=
$list
[
3
];
$conn
[
'remote_host'
]
=
$list
[
3
];
$conn
[
'last_time'
]
=
$list
[
4
];
$conn
[
'last_time'
]
=
$list
[
4
];
$server
[
'routes'
][]
=
$conn
;
$server
[
'routes'
][]
=
$conn
;
}
}
}
}
/* cleanup */
fclose
(
$fp
);
/* cleanup */
}
else
{
fclose
(
$fp
);
$conn
=
array
();
}
else
{
$conn
[
'common_name'
]
=
"[error]"
;
$conn
=
array
();
$conn
[
'remote_host'
]
=
"Unable to contact daemon"
;
$conn
[
'common_name'
]
=
"[error]"
;
$conn
[
'virtual_addr'
]
=
"Service not running?"
;
$conn
[
'remote_host'
]
=
"Unable to contact daemon"
;
$conn
[
'bytes_recv'
]
=
0
;
$conn
[
'virtual_addr'
]
=
"Service not running?"
;
$conn
[
'bytes_sent'
]
=
0
;
$conn
[
'bytes_recv'
]
=
0
;
$conn
[
'connect_time'
]
=
0
;
$conn
[
'bytes_sent'
]
=
0
;
$server
[
'conns'
][]
=
$conn
;
$conn
[
'connect_time'
]
=
0
;
}
$server
[
'conns'
][]
=
$conn
;
return
$server
;
}
return
$server
;
}
}
function
openvpn_get_active_clients
()
function
openvpn_get_active_clients
()
{
{
global
$config
;
global
$config
;
$clients
=
array
();
$clients
=
array
();
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
])
&&
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
if
(
isset
(
$config
[
'openvpn'
][
'openvpn-client'
])
&&
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
&
$settings
)
{
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
{
if
(
empty
(
$settings
)
||
isset
(
$settings
[
'disable'
]))
continue
;
continue
;
}
$prot
=
$settings
[
'protocol'
];
$prot
=
$settings
[
'protocol'
];
$port
=
(
$settings
[
'local_port'
])
?
":
{
$settings
[
'local_port'
]
}
"
:
""
;
$port
=
(
$settings
[
'local_port'
])
?
":
{
$settings
[
'local_port'
]
}
"
:
""
;
$client
=
array
();
$client
=
array
();
$client
[
'port'
]
=
$settings
[
'local_port'
];
$client
[
'port'
]
=
$settings
[
'local_port'
];
if
(
$settings
[
'description'
])
if
(
$settings
[
'description'
])
{
$client
[
'name'
]
=
"
{
$settings
[
'description'
]
}
{
$prot
}{
$port
}
"
;
$client
[
'name'
]
=
"
{
$settings
[
'description'
]
}
{
$prot
}{
$port
}
"
;
else
}
else
{
$client
[
'name'
]
=
"Client
{
$prot
}{
$port
}
"
;
$client
[
'name'
]
=
"Client
{
$prot
}{
$port
}
"
;
}
$client
[
'vpnid'
]
=
$settings
[
'vpnid'
];
$client
[
'mgmt'
]
=
"client
{
$client
[
'vpnid'
]
}
"
;
$client
[
'vpnid'
]
=
$settings
[
'vpnid'
]
;
$socket
=
"unix:///var/etc/openvpn/
{
$client
[
'mgmt'
]
}
.sock
"
;
$client
[
'mgmt'
]
=
"client
{
$client
[
'vpnid'
]
}
"
;
$client
[
'status'
]
=
"down
"
;
$socket
=
"unix:///var/etc/openvpn/
{
$client
[
'mgmt'
]
}
.sock
"
;
$client
[
'status'
]
=
"down"
;
$clients
[]
=
openvpn_get_client_status
(
$client
,
$socket
);
$clients
[]
=
openvpn_get_client_status
(
$client
,
$socket
);
}
}
}
}
return
$clients
;
return
$clients
;
}
}
function
openvpn_get_client_status
(
$client
,
$socket
)
{
function
openvpn_get_client_status
(
$client
,
$socket
)
$errval
;
{
$errstr
;
$errval
;
$fp
=
@
stream_socket_client
(
$socket
,
$errval
,
$errstr
,
1
);
$errstr
;
if
(
$fp
)
{
$fp
=
@
stream_socket_client
(
$socket
,
$errval
,
$errstr
,
1
);
stream_set_timeout
(
$fp
,
1
);
if
(
$fp
)
{
/* send our status request */
stream_set_timeout
(
$fp
,
1
);
fputs
(
$fp
,
"state 1
\n
"
);
/* send our status request */
fputs
(
$fp
,
"state 1
\n
"
);
/* recv all response lines */
while
(
!
feof
(
$fp
))
{
/* recv all response lines */
/* read the next line */
while
(
!
feof
(
$fp
))
{
$line
=
fgets
(
$fp
,
1024
);
/* read the next line */
$line
=
fgets
(
$fp
,
1024
);
$info
=
stream_get_meta_data
(
$fp
);
if
(
$info
[
'timed_out'
])
$info
=
stream_get_meta_data
(
$fp
);
break
;
if
(
$info
[
'timed_out'
])
{
break
;
/* Get the client state */
}
if
(
strstr
(
$line
,
"CONNECTED"
))
{
$client
[
'status'
]
=
"up"
;
/* Get the client state */
$list
=
explode
(
","
,
$line
);
if
(
strstr
(
$line
,
"CONNECTED"
))
{
$client
[
'status'
]
=
"up"
;
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
$list
=
explode
(
","
,
$line
);
$client
[
'virtual_addr'
]
=
$list
[
3
];
$client
[
'remote_host'
]
=
$list
[
4
];
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
}
$client
[
'virtual_addr'
]
=
$list
[
3
];
if
(
strstr
(
$line
,
"CONNECTING"
))
{
$client
[
'remote_host'
]
=
$list
[
4
];
$client
[
'status'
]
=
"connecting"
;
}
}
if
(
strstr
(
$line
,
"CONNECTING"
))
{
if
(
strstr
(
$line
,
"ASSIGN_IP"
))
{
$client
[
'status'
]
=
"connecting"
;
$client
[
'status'
]
=
"waiting"
;
}
$list
=
explode
(
","
,
$line
);
if
(
strstr
(
$line
,
"ASSIGN_IP"
))
{
$client
[
'status'
]
=
"waiting"
;
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
$list
=
explode
(
","
,
$line
);
$client
[
'virtual_addr'
]
=
$list
[
3
];
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
}
$client
[
'virtual_addr'
]
=
$list
[
3
];
if
(
strstr
(
$line
,
"RECONNECTING"
))
{
}
$client
[
'status'
]
=
"reconnecting"
;
if
(
strstr
(
$line
,
"RECONNECTING"
))
{
$list
=
explode
(
","
,
$line
);
$client
[
'status'
]
=
"reconnecting"
;
$list
=
explode
(
","
,
$line
);
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
$client
[
'connect_time'
]
=
date
(
"D M j G:i:s Y"
,
$list
[
0
]);
$client
[
'status'
]
.=
"; "
.
$list
[
2
];
$client
[
'status'
]
.=
"; "
.
$list
[
2
];
}
}
/* parse end of output line */
/* parse end of output line */
if
(
strstr
(
$line
,
"END"
)
||
strstr
(
$line
,
"ERROR"
))
if
(
strstr
(
$line
,
"END"
)
||
strstr
(
$line
,
"ERROR"
))
{
break
;
break
;
}
}
}
/* If up, get read/write stats */
if
(
strcmp
(
$client
[
'status'
],
"up"
)
==
0
)
{
/* If up, get read/write stats */
fputs
(
$fp
,
"status 2
\n
"
);
if
(
strcmp
(
$client
[
'status'
],
"up"
)
==
0
)
{
/* recv all response lines */
fputs
(
$fp
,
"status 2
\n
"
);
while
(
!
feof
(
$fp
))
{
/* recv all response lines */
/* read the next line */
while
(
!
feof
(
$fp
))
{
$line
=
fgets
(
$fp
,
1024
);
/* read the next line */
$line
=
fgets
(
$fp
,
1024
);
$info
=
stream_get_meta_data
(
$fp
);
if
(
$info
[
'timed_out'
])
$info
=
stream_get_meta_data
(
$fp
);
break
;
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 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
];
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
;
/* parse end of output line */
}
if
(
strstr
(
$line
,
"END"
))
{
}
break
;
}
fclose
(
$fp
);
}
}
}
else
{
fclose
(
$fp
);
$DisplayNote
=
true
;
}
else
{
$client
[
'remote_host'
]
=
"Unable to contact daemon"
;
$DisplayNote
=
true
;
$client
[
'virtual_addr'
]
=
"Service not running?"
;
$client
[
'remote_host'
]
=
"Unable to contact daemon"
;
$client
[
'bytes_recv'
]
=
0
;
$client
[
'virtual_addr'
]
=
"Service not running?"
;
$client
[
'bytes_sent'
]
=
0
;
$client
[
'bytes_recv'
]
=
0
;
$client
[
'connect_time'
]
=
0
;
$client
[
'bytes_sent'
]
=
0
;
}
$client
[
'connect_time'
]
=
0
;
return
$client
;
}
return
$client
;
}
}
function
openvpn_create_dirs
()
function
openvpn_create_dirs
()
{
{
@
mkdir
(
'/var/etc/openvpn-csc'
,
0750
);
@
mkdir
(
'/var/etc/openvpn-csc'
,
0750
);
@
mkdir
(
'/var/etc/openvpn'
,
0750
);
@
mkdir
(
'/var/etc/openvpn'
,
0750
);
}
}
function
openvpn_get_interface_ip
(
$ip
,
$mask
)
{
function
openvpn_get_interface_ip
(
$ip
,
$mask
)
$baselong
=
ip2long32
(
$ip
)
&
ip2long
(
$mask
);
{
$ip1
=
long2ip32
(
$baselong
+
1
);
$baselong
=
ip2long32
(
$ip
)
&
ip2long
(
$mask
);
$ip2
=
long2ip32
(
$baselong
+
2
);
$ip1
=
long2ip32
(
$baselong
+
1
);
return
array
(
$ip1
,
$ip2
);
$ip2
=
long2ip32
(
$baselong
+
2
);
return
array
(
$ip1
,
$ip2
);
}
}
function
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
)
{
function
openvpn_get_interface_ipv6
(
$ipv6
,
$prefix
)
$basev6
=
gen_subnetv6
(
$ipv6
,
$prefix
);
{
// Is there a better way to do this math?
$basev6
=
gen_subnetv6
(
$ipv6
,
$prefix
);
$ipv6_arr
=
explode
(
':'
,
$basev6
);
// Is there a better way to do this math?
$last
=
hexdec
(
array_pop
(
$ipv6_arr
));
$ipv6_arr
=
explode
(
':'
,
$basev6
);
$ipv6_1
=
Net_IPv6
::
compress
(
Net_IPv6
::
uncompress
(
implode
(
':'
,
$ipv6_arr
)
.
':'
.
dechex
(
$last
+
1
)));
$last
=
hexdec
(
array_pop
(
$ipv6_arr
));
$ipv6_2
=
Net_IPv6
::
compress
(
Net_IPv6
::
uncompress
(
implode
(
':'
,
$ipv6_arr
)
.
':'
.
dechex
(
$last
+
2
)));
$ipv6_1
=
Net_IPv6
::
compress
(
Net_IPv6
::
uncompress
(
implode
(
':'
,
$ipv6_arr
)
.
':'
.
dechex
(
$last
+
1
)));
return
array
(
$ipv6_1
,
$ipv6_2
);
$ipv6_2
=
Net_IPv6
::
compress
(
Net_IPv6
::
uncompress
(
implode
(
':'
,
$ipv6_arr
)
.
':'
.
dechex
(
$last
+
2
)));
return
array
(
$ipv6_1
,
$ipv6_2
);
}
}
function
openvpn_clear_route
(
$mode
,
$settings
)
{
function
openvpn_clear_route
(
$mode
,
$settings
)
if
(
empty
(
$settings
[
'tunnel_network'
]))
{
return
;
if
(
empty
(
$settings
[
'tunnel_network'
]))
{
list
(
$ip
,
$cidr
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
return
;
$mask
=
gen_subnet_mask
(
$cidr
);
}
$clear_route
=
false
;
list
(
$ip
,
$cidr
)
=
explode
(
'/'
,
$settings
[
'tunnel_network'
]);
$mask
=
gen_subnet_mask
(
$cidr
);
switch
(
$settings
[
'mode'
])
{
$clear_route
=
false
;
case
'shared_key'
:
$clear_route
=
true
;
switch
(
$settings
[
'mode'
])
{
break
;
case
'shared_key'
:
case
'p2p_tls'
:
$clear_route
=
true
;
case
'p2p_shared_key'
:
break
;
if
(
$cidr
==
30
)
case
'p2p_tls'
:
$clear_route
=
true
;
case
'p2p_shared_key'
:
break
;
if
(
$cidr
==
30
)
{
}
$clear_route
=
true
;
}
if
(
$clear_route
&&
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
break
;
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
}
$ip_to_clear
=
(
$mode
==
"server"
)
?
$ip1
:
$ip2
;
/* XXX: Family for route? */
if
(
$clear_route
&&
!
empty
(
$ip
)
&&
!
empty
(
$mask
))
{
mwexec
(
"/sbin/route -q delete
{
$ip_to_clear
}
"
);
list
(
$ip1
,
$ip2
)
=
openvpn_get_interface_ip
(
$ip
,
$mask
);
}
$ip_to_clear
=
(
$mode
==
"server"
)
?
$ip1
:
$ip2
;
/* XXX: Family for route? */
mwexec
(
"/sbin/route -q delete
{
$ip_to_clear
}
"
);
}
}
}
function
openvpn_gen_routes
(
$value
,
$ipproto
=
"ipv4"
,
$push
=
false
,
$iroute
=
false
)
{
function
openvpn_gen_routes
(
$value
,
$ipproto
=
"ipv4"
,
$push
=
false
,
$iroute
=
false
)
$routes
=
""
;
{
if
(
empty
(
$value
))
$routes
=
""
;
return
""
;
if
(
empty
(
$value
))
{
$networks
=
explode
(
','
,
$value
);
return
""
;
}
foreach
(
$networks
as
$network
)
{
$networks
=
explode
(
','
,
$value
);
if
(
$ipproto
==
"ipv4"
)
$route
=
openvpn_gen_route_ipv4
(
$network
,
$iroute
);
foreach
(
$networks
as
$network
)
{
else
if
(
$ipproto
==
"ipv4"
)
{
$route
=
openvpn_gen_route_ipv6
(
$network
,
$iroute
);
$route
=
openvpn_gen_route_ipv4
(
$network
,
$iroute
);
}
else
{
if
(
$push
)
$route
=
openvpn_gen_route_ipv6
(
$network
,
$iroute
);
$routes
.=
"push
\"
{
$route
}
\"\n
"
;
}
else
if
(
$push
)
{
$routes
.=
"
{
$route
}
\n
"
;
$routes
.=
"push
\"
{
$route
}
\"\n
"
;
}
}
else
{
return
$routes
;
$routes
.=
"
{
$route
}
\n
"
;
}
}
return
$routes
;
}
}
function
openvpn_gen_route_ipv4
(
$network
,
$iroute
=
false
)
{
function
openvpn_gen_route_ipv4
(
$network
,
$iroute
=
false
)
$i
=
(
$iroute
)
?
"i"
:
""
;
{
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
trim
(
$network
));
$i
=
(
$iroute
)
?
"i"
:
""
;
$mask
=
gen_subnet_mask
(
$mask
);
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
trim
(
$network
));
return
"
{
$i
}
route
$ip
$mask
"
;
$mask
=
gen_subnet_mask
(
$mask
);
return
"
{
$i
}
route
$ip
$mask
"
;
}
}
function
openvpn_gen_route_ipv6
(
$network
,
$iroute
=
false
)
{
function
openvpn_gen_route_ipv6
(
$network
,
$iroute
=
false
)
$i
=
(
$iroute
)
?
"i"
:
""
;
{
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
trim
(
$network
));
$i
=
(
$iroute
)
?
"i"
:
""
;
if
(
empty
(
$prefix
))
list
(
$ipv6
,
$prefix
)
=
explode
(
'/'
,
trim
(
$network
));
$prefix
=
"128"
;
if
(
empty
(
$prefix
))
{
return
"
{
$i
}
route-ipv6 ${ipv6}/${prefix}"
;
$prefix
=
"128"
;
}
return
"
{
$i
}
route-ipv6 ${ipv6}/${prefix}"
;
}
}
function
openvpn_get_settings
(
$mode
,
$vpnid
)
{
function
openvpn_get_settings
(
$mode
,
$vpnid
)
global
$config
;
{
global
$config
;
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
$settings
)
{
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-server'
]))
{
if
(
isset
(
$settings
[
'disable'
]))
foreach
(
$config
[
'openvpn'
][
'openvpn-server'
]
as
$settings
)
{
continue
;
if
(
isset
(
$settings
[
'disable'
]))
{
continue
;
if
(
$vpnid
!=
0
&&
$vpnid
==
$settings
[
'vpnid'
])
}
return
$settings
;
if
(
$vpnid
!=
0
&&
$vpnid
==
$settings
[
'vpnid'
])
{
}
return
$settings
;
}
}
}
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
}
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
$settings
)
{
if
(
isset
(
$settings
[
'disable'
]))
if
(
is_array
(
$config
[
'openvpn'
][
'openvpn-client'
]))
{
continue
;
foreach
(
$config
[
'openvpn'
][
'openvpn-client'
]
as
$settings
)
{
if
(
isset
(
$settings
[
'disable'
]))
{
if
(
$vpnid
!=
0
&&
$vpnid
==
$settings
[
'vpnid'
])
continue
;
return
$settings
;
}
}
}
if
(
$vpnid
!=
0
&&
$vpnid
==
$settings
[
'vpnid'
])
{
return
$settings
;
return
array
();
}
}
}
return
array
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment