1
|
<?php
|
2
|
require('config.inc');
|
3
|
|
4
|
|
5
|
// Return the list of ciphers OpenVPN supports
|
6
|
function openvpn_get_ciphers($pkg) {
|
7
|
foreach ($pkg['fields']['field'] as $i => $field) {
|
8
|
if ($field['fieldname'] == 'crypto') break;
|
9
|
}
|
10
|
$option_array = &$pkg['fields']['field'][$i]['options']['option'];
|
11
|
$ciphers_out = shell_exec('openvpn --show-ciphers | grep "default key" | awk \'{print $1, "(" $2 "-" $3 ")";}\'');
|
12
|
$ciphers = explode("\n", trim($ciphers_out));
|
13
|
sort($ciphers);
|
14
|
foreach ($ciphers as $cipher) {
|
15
|
$value = explode(' ', $cipher);
|
16
|
$value = $value[0];
|
17
|
$option_array[] = array('value' => $value, 'name' => $cipher);
|
18
|
}
|
19
|
}
|
20
|
|
21
|
|
22
|
// Do the input validation
|
23
|
function openvpn_validate_input($mode, $post, $input_errors) {
|
24
|
$Mode = ucfirst($mode);
|
25
|
|
26
|
$port = trim($post['port']);
|
27
|
if ($port && (!is_numeric($port) || ($port < 0) || ($port > 65535)))
|
28
|
$input_errors[] = 'The field \'Port\' should contain a valid port number, between 1 and 65536.';
|
29
|
if ($mode == 'client') {
|
30
|
$server_port = trim($post['serverport']);
|
31
|
if ($server_port && (!is_numeric($server_port) || ($server_port < 0) || ($port > 65535)))
|
32
|
$input_errors[] = 'The field \'Server port\' should contain a valid port number, between 1 and 65536.';
|
33
|
}
|
34
|
|
35
|
$reqfields = array('local_ip', 'remote_ip');
|
36
|
$reqfieldsn = array('Local IP', 'Remote IP');
|
37
|
foreach($reqfields as $i => $field) {
|
38
|
$value = trim($post[$field]);
|
39
|
if ($value and (!is_ipaddr($value)))
|
40
|
$input_errors[] = "The field '{$reqfieldsn[$i]}' must contain a valid IP address";
|
41
|
}
|
42
|
|
43
|
if ($mode == 'client') {
|
44
|
$server_addr = trim($post['serveraddr']);
|
45
|
if ($value && !(is_domain($server_addr) || is_ipaddr($server_addr)))
|
46
|
$input_errors[] = 'The field \'Server address\' must contain a valid IP address or domain name.';
|
47
|
}
|
48
|
|
49
|
$value = trim($post['ipblock']);
|
50
|
if ($value) {
|
51
|
list($ip, $mask) = explode('/', $value);
|
52
|
if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
|
53
|
$input_errors[] = "The field 'IP block' must contain a valid CIDR range.";
|
54
|
}
|
55
|
|
56
|
if ($_POST['auth_method'] == 'shared_key') {
|
57
|
$reqfields[] = 'shared_key';
|
58
|
$reqfieldsn[] = 'Shared key';
|
59
|
}
|
60
|
else {
|
61
|
$req = explode(' ', "ca_cert {$mode}_cert {$mode}_key");
|
62
|
$reqn = array( 'CA certificate',
|
63
|
ucfirst($mode) . ' certificate',
|
64
|
ucfirst($mode) . ' key');
|
65
|
$reqfields = array_merge($reqfields, $req);
|
66
|
$reqfieldsn = array_merge($reqfieldsn, $reqn);
|
67
|
if ($mode == 'server') {
|
68
|
$reqfields[] = 'dh_params';
|
69
|
$reqfieldsn[] = 'DH parameters';
|
70
|
}
|
71
|
}
|
72
|
do_input_validation($post, $reqfields, $reqfieldsn, &$input_errors);
|
73
|
|
74
|
$value = trim($post['shared_key']);
|
75
|
$items = array();
|
76
|
if ($_POST['auth_method'] == 'shared_key') {
|
77
|
$items[] = array( 'field' => 'shared_key',
|
78
|
'string' => 'OpenVPN Static key V1',
|
79
|
'name' => 'Shared key');
|
80
|
}
|
81
|
else {
|
82
|
$items[] = array( 'field' => 'ca_cert',
|
83
|
'string' => 'CERTIFICATE',
|
84
|
'name' => 'CA certificate');
|
85
|
$items[] = array( 'field' => "{$mode}_cert",
|
86
|
'string' => 'CERTIFICATE',
|
87
|
'name' => "$Mode certificate");
|
88
|
$items[] = array( 'field' => "{$mode}_key",
|
89
|
'string' => 'RSA PRIVATE KEY',
|
90
|
'name' => "$Mode key");
|
91
|
if ($mode == 'server') {
|
92
|
$items[] = array( 'field' => 'dh_params',
|
93
|
'string' => 'DH PARAMETERS',
|
94
|
'name' => 'DH parameters');
|
95
|
$items[] = array( 'field' => 'crl',
|
96
|
'string' => 'X509 CRL',
|
97
|
'name' => 'CRL');
|
98
|
}
|
99
|
}
|
100
|
foreach ($items as $item) {
|
101
|
$value = trim($_POST[$item['field']]);
|
102
|
$string = $item['string'];
|
103
|
if ($value && (!strstr($value, "-----BEGIN {$string}-----") || !strstr($value, "-----END {$string}-----")))
|
104
|
$input_errors[] = "The field '{$item['name']}' does not appear to be valid";
|
105
|
}
|
106
|
}
|
107
|
|
108
|
|
109
|
// Rewrite the settings
|
110
|
function openvpn_reconfigure($mode, $id) {
|
111
|
global $g, $config;
|
112
|
|
113
|
$settings = $config['installedpackages']["openvpn$mode"]['config'][$id];
|
114
|
if ($settings['disable']) return;
|
115
|
|
116
|
// Set up the keys
|
117
|
// Note that the keys' extension is the directive that goes to the config file
|
118
|
$base_file = $g['varetc_path'] . "/openvpn_{$mode}{$id}.";
|
119
|
$keys = array();
|
120
|
if ($settings['auth_method'] == 'shared_key')
|
121
|
$keys[] = array('field' => 'shared_key', 'ext' => 'secret', 'directive' => 'secret');
|
122
|
else {
|
123
|
$keys[] = array('field' => 'ca_cert', 'ext' => 'ca', 'directive' => 'ca');
|
124
|
$keys[] = array('field' => "{$mode}_cert", 'ext' => 'cert', 'directive' => 'cert');
|
125
|
$keys[] = array('field' => "{$mode}_key", 'ext' => 'key', 'directive' => 'key');
|
126
|
if ($mode == 'server')
|
127
|
$keys[] = array('field' => 'dh_params', 'ext' => 'dh', 'directive' => 'dh');
|
128
|
if ($settings['crl'])
|
129
|
$keys[] = array('field' => 'crl', 'ext' => 'crl', 'directive' => 'crl-verify');
|
130
|
}
|
131
|
foreach($keys as $key) {
|
132
|
$filename = $base_file . $key['ext'];
|
133
|
file_put_contents($filename, base64_decode($settings[$key['field']]));
|
134
|
chown($filename, 'nobody');
|
135
|
chgrp($filename, 'nobody');
|
136
|
}
|
137
|
|
138
|
$proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}");
|
139
|
$port = $settings['port'];
|
140
|
$ifconfig = $settings['local_ip'] . ' ' . $settings['remote_ip'];
|
141
|
list($route_ip, $route_mask) = explode('/', $settings['ipblock']);
|
142
|
$route_mask = gen_subnet_mask($route_mask);
|
143
|
$cipher = $settings['crypto'];
|
144
|
$openvpn_conf = <<<EOD
|
145
|
user nobody
|
146
|
group nobody
|
147
|
daemon
|
148
|
keepalive 10 60
|
149
|
ping-timer-rem
|
150
|
persist-tun
|
151
|
persist-key
|
152
|
dev tun
|
153
|
proto $proto
|
154
|
port $port
|
155
|
ifconfig $ifconfig
|
156
|
route $route_ip $route_mask
|
157
|
cipher $cipher
|
158
|
|
159
|
EOD;
|
160
|
if ($settings['auth_method'] == 'pki')
|
161
|
$openvpn_conf .= "tls-$mode\n";
|
162
|
|
163
|
// Write the settings for the keys
|
164
|
foreach ($keys as $key)
|
165
|
$openvpn_conf .= $key['directive'] . ' ' . $base_file . $key['ext'] . "\n";
|
166
|
|
167
|
if ($mode == 'client') $openvpn_conf .= 'remote ' . $settings['serveraddr'] . ' ' .$settings['serverport'] . "\n";
|
168
|
if ($settings['use_lzo']) $openvpn_conf .= "comp-lzo\n";
|
169
|
if ($settings['dynamic_ip']) $openvpn_conf .= "persist-remote-ip\n";
|
170
|
|
171
|
file_put_contents($g['varetc_path'] . "/openvpn_{$mode}{$id}.conf", $openvpn_conf);
|
172
|
}
|
173
|
|
174
|
|
175
|
function openvpn_restart($mode, $id) {
|
176
|
global $g, $config;
|
177
|
|
178
|
$pidfile = $g['varrun_path'] . "/openvpn_{$mode}{$id}.pid";
|
179
|
killbypid($pidfile);
|
180
|
sleep(2);
|
181
|
|
182
|
$settings = $config['installedpackages']["openvpn$mode"]['config'][$id];
|
183
|
if ($settings['disable']) return;
|
184
|
|
185
|
$configfile = $g['varetc_path'] . "/openvpn_{$mode}{$id}.conf";
|
186
|
mwexec("openvpn --config $configfile --writepid $pidfile");
|
187
|
}
|
188
|
|
189
|
|
190
|
// Resync the configuration and restart the VPN
|
191
|
function openvpn_resync($mode, $id) {
|
192
|
openvpn_reconfigure($mode, $id);
|
193
|
openvpn_restart($mode, $id);
|
194
|
}
|
195
|
|
196
|
|
197
|
// Resync and restart all VPNs
|
198
|
function openvpn_resync_all() {
|
199
|
global $config;
|
200
|
|
201
|
foreach (array('server', 'client') as $mode) {
|
202
|
if (is_array($config['installedpackages']["openvpn$mode"]['config'])) {
|
203
|
foreach ($config['installedpackages']["openvpn$mode"]['config'] as $id => $settings)
|
204
|
openvpn_resync($mode, $id);
|
205
|
}
|
206
|
}
|
207
|
}
|
208
|
|
209
|
|
210
|
function openvpn_print_javascript($mode) {
|
211
|
$javascript = <<<EOD
|
212
|
<script language="JavaScript">
|
213
|
<!--
|
214
|
function onAuthMethodChanged() {
|
215
|
var endis = (document.iform.auth_method.options.value == 'shared_key');
|
216
|
document.iform.shared_key.disabled = !endis;
|
217
|
document.iform.ca_cert.disabled = endis;
|
218
|
document.iform.{$mode}_cert.disabled = endis;
|
219
|
document.iform.{$mode}_key.disabled = endis;
|
220
|
|
221
|
EOD;
|
222
|
if ($mode == 'server') {
|
223
|
$javascript .= "\tdocument.iform.dh_params.disabled = endis;\n";
|
224
|
$javascript .= "\tdocument.iform.crl.disabled = endis;\n";
|
225
|
}
|
226
|
$javascript .= <<<EOD
|
227
|
}
|
228
|
//-->
|
229
|
</script>
|
230
|
|
231
|
EOD;
|
232
|
print($javascript);
|
233
|
}
|
234
|
|
235
|
|
236
|
function openvpn_print_javascript2() {
|
237
|
$javascript = <<<EOD
|
238
|
<script language="JavaScript">
|
239
|
<!--
|
240
|
onAuthMethodChanged();
|
241
|
//-->
|
242
|
</script>
|
243
|
|
244
|
EOD;
|
245
|
print($javascript);
|
246
|
}
|
247
|
?>
|