Project

General

Profile

Download (24.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	Copyright (C) 2010 Ermal Luçi
4
	All rights reserved.
5

    
6
	Redistribution and use in source and binary forms, with or without
7
	modification, are permitted provided that the following conditions are met:
8

    
9
	1. Redistributions of source code must retain the above copyright notice,
10
	   this list of conditions and the following disclaimer.
11

    
12
	2. Redistributions in binary form must reproduce the above copyright
13
	   notice, this list of conditions and the following disclaimer in the
14
	   documentation and/or other materials provided with the distribution.
15

    
16
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
	POSSIBILITY OF SUCH DAMAGE.
26

    
27
	pfSense_MODULE: openvpn
28
*/
29
require_once("openvpn.inc");
30

    
31
function has_special_chars($text) {
32
	return preg_match('/[^A-Za-z0-9 _-]/', $text);
33
}
34

    
35
function step1_submitphpaction() {
36
	global $stepid, $config;
37
	if ($_POST['authtype'] == "local") {
38
		$stepid = 4;
39
		$config['ovpnserver']['step1']['type'] = "local";
40
	} else if ($_POST['authtype'] == "ldap") {
41
		$stepid = 0;
42
	} else if ($_POST['authtype'] == "radius") {
43
		$stepid = 2;
44
		$config['ovpnserver']['step1']['type'] = "radius";
45
		unset($config['ovpnserver']['step1']['uselist']);
46
	}
47
}
48

    
49
function step2_stepbeforeformdisplay() {
50
	global $pkg, $stepid;
51

    
52
	$fields =& $pkg['step'][1]['fields']['field'];
53

    
54
	$found = false;
55
	$authlist = auth_get_authserver_list();
56
	$fields[1]['options']['option'] = array();
57
	foreach ($authlist as $i => $auth) {
58
		if ($auth['type'] != "ldap")
59
			continue;
60
		$found = true;
61
		$opts = array();
62
		$opts['name'] = $auth['name'];
63
		$opts['value'] = $auth['name'];
64
		$fields[1]['options']['option'][] = $opts;
65
	}
66
	if ($found == false) {
67
		$stepid = 2;
68
	}
69
}
70

    
71
function step2_submitphpaction() {
72
	global $stepid;
73

    
74
	if (isset($_POST['next'])) {
75
		$_POST['uselist'] = "";
76
		$stepid +=3;
77
	}
78
}
79

    
80
function step3_submitphpaction() {
81
	global $stepid, $savemsg, $config;
82

    
83
	/* Default LDAP port is 389 for TCP and 636 for SSL */
84
	if (empty($_POST['port'])) {
85
		if ($_POST['transport'] == "tcp")
86
			$config['ovpnserver']['step2']['port'] = 389;
87
		elseif ($_POST['transport'] == "ssl")
88
			$config['ovpnserver']['step2']['port'] = 636;
89
	} elseif (!is_port($_POST['port'])) {
90
		$stepid--;
91
		$savemsg = "Please enter a valid port number.";
92
	}
93

    
94
	if (empty($_POST['name']) || empty($_POST['ip']) ||empty($_POST['transport']) ||
95
	     empty($_POST['scope']) || empty($_POST['basedn']) || empty($_POST['authscope']) || empty($_POST['nameattr'])) {
96
		$stepid--;
97
		$savemsg = "Please enter all information for authentication server.";
98
	} else if (count(($authcfg = auth_get_authserver($_POST['name']))) > 0) {
99
		$stepid--;
100
		$savemsg = "Please choose a different name because an authentication server with this name already exists.";
101
	} elseif (!is_fqdn($_POST['ip']) && !is_ipaddr($_POST['ip'])) {
102
		$stepid--;
103
		$savemsg = "Please enter a valid IP address or hostname for the authentication server.";
104
	} else {
105
		$config['ovpnserver']['step2']['uselist'] = "on";
106
		$_POST['uselist'] = "on";
107
		$stepid += 2;
108
	}
109
}
110

    
111
function step4_stepbeforeformdisplay() {
112
	global $pkg, $stepid;
113

    
114
	$fields =& $pkg['step'][3]['fields']['field'];
115

    
116
	$found = false;
117
	$authlist = auth_get_authserver_list();
118
	$fields[1]['options']['option'] = array();
119
	foreach ($authlist as $i => $auth) {
120
		if ($auth['type'] != "radius")
121
			continue;
122
		$found = true;
123
		$opts = array();
124
		$opts['name'] = $auth['name'];
125
		$opts['value'] = $auth['name'];
126
		$fields[1]['options']['option'][] = $opts;
127
	}
128
	if ($found == false)
129
		$stepid = 4;
130
}
131

    
132
function step4_submitphpaction() {
133
	global $stepid;
134

    
135
	if (isset($_POST['next'])) {
136
		$_POST['uselist'] = "";
137
		$stepid++;
138
	}
139
}
140

    
141
function step5_submitphpaction() {
142
	global $stepid, $savemsg, $config;
143

    
144
	/* Default RADIUS Auth port = 1812 */
145
	if (empty($_POST['port'])) {
146
		$config['ovpnserver']['step2']['port'] = 1812;
147
	} elseif (!is_port($_POST['port'])) {
148
		$stepid--;
149
		$savemsg = "Please enter a valid port number.";
150
	}
151

    
152
	if (empty($_POST['name']) || empty($_POST['ip']) || empty($_POST['secret'])) {
153
		$stepid--;
154
		$savemsg = "Please enter all information for authentication server.";
155
	} else if (count(($authcfg = auth_get_authserver($_POST['name']))) > 0) {
156
		$stepid--;
157
		$savemsg = "Please choose a different name because an authentication server with this name already exists.";
158
	} elseif (!is_fqdn($_POST['ip']) && !is_ipaddr($_POST['ip'])) {
159
		$stepid--;
160
		$savemsg = "Please enter a valid IP address or hostname for the authentication server.";
161
	} else {
162
		$config['ovpnserver']['step2']['uselist'] = "on";
163
		$_POST['uselist'] = "on";
164
	}
165
}
166

    
167
function step6_stepbeforeformdisplay() {
168
	global $stepid, $config;
169

    
170
	if (count($config['ca']) < 1) {
171
		$stepid++;
172
	}
173
}
174

    
175
function step6_submitphpaction() {
176
	global $stepid, $config;
177

    
178
	if (isset($_POST['next'])) {
179
		$_POST['uselist'] = "";
180
		unset($config['ovpnserver']['step6']['uselist']);
181
		$stepid++;
182
	} else {
183
		$config['ovpnserver']['step6']['uselist'] = "on";
184
		$_POST['uselist'] = "on";
185
	}
186
}
187

    
188
function step7_submitphpaction() {
189
	global $input_errors, $stepid, $savemsg, $_POST, $config;
190

    
191
	$canames = array();
192
	$cacns = array();
193
	if (is_array($config['ca'])) {
194
		foreach($config['ca'] as $ca) {
195
			$canames[] = $ca['descr'];
196
			$cainfo = cert_get_subject_hash($ca['crt']);
197
			$cacns[] = $cainfo["CN"];
198
		}
199
	}
200

    
201
	if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
202
		$input_errors[] = "The field 'Descriptive Name' contains invalid characters.";
203
	}
204

    
205
	if (empty($_POST['descr']) || empty($_POST['keylength']) || empty($_POST['lifetime']) ||
206
	    empty($_POST['country']) || empty($_POST['state']) || empty($_POST['city']) ||
207
	    empty($_POST['organization']) || empty($_POST['email'])) {
208
		$stepid--;
209
		$savemsg = "Please enter all information for the new Certificate Authority.";
210
	} elseif (has_special_chars($_POST['country']) || has_special_chars($_POST['state']) || 
211
	    has_special_chars($_POST['city']) || has_special_chars($_POST['organization'])) {
212
		$stepid--;
213
		$input_errors[] = "Please do not use special characters in Certificate field names.";
214
	} elseif (in_array($_POST['descr'], $canames) || in_array($_POST['descr'], $cacns)) {
215
		$stepid--;
216
		$savemsg = "Please enter a different name for the Certificate Authority. A Certificate Authority with that name already exists.";
217
	} elseif (strlen($_POST['country']) != 2) {
218
		$stepid--;
219
		$savemsg = "Please enter only a two-letter ISO country code";
220
	} else {
221
		$config['ovpnserver']['step6']['uselist'] = "on";
222
		$_POST['uselist'] = "on";
223
	}
224
}
225

    
226
function step8_stepbeforeformdisplay() {
227
	global $stepid, $config;
228

    
229
	if (count($config['cert']) < 1 ||
230
		(count($config['cert']) == 1 && stristr($config['cert'][0]['descr'], "webconf"))) {
231
		$stepid++;
232
	}
233
}
234

    
235
function step8_submitphpaction() {
236
	global $stepid, $config, $_POST;
237

    
238
	if (isset($_POST['next'])) {
239
		$_POST['uselist'] = "";
240
		unset($config['ovpnserver']['step9']['uselist']);
241
		$stepid++;
242
	} else {
243
		$config['ovpnserver']['step6']['uselist'] = "on";
244
		$_POST['uselist'] = "on";
245
	}
246
}
247

    
248
function step9_stepbeforeformdisplay() {
249
	global $config, $pkg, $stepid;
250

    
251
	$pconfig = $config['ovpnserver'];
252

    
253
	if (isset($pconfig['step6']['uselist'])) {
254
		$country = $pconfig['step6']['country'];
255
		$state = $pconfig['step6']['state'];
256
		$city = $pconfig['step6']['city'];
257
		$org = $pconfig['step6']['organization'];
258
		$email = $pconfig['step6']['email'];
259
	} else {
260
		$ca = lookup_ca($pconfig['step6']['authcertca']);
261
		$cavl = cert_get_subject_array($ca['crt']);
262
		$country = $cavl[0]['v'];
263
		$state = $cavl[1]['v'];
264
		$city = $cavl[2]['v'];
265
		$org = $cavl[3]['v'];
266
		$email = $cavl[4]['v'];
267
	}
268
	$fields =& $pkg['step'][$stepid]['fields']['field'];
269

    
270
	foreach ($fields as $idx => $field) {
271
		switch ($field['name']) {
272
		case 'country':
273
			$fields[$idx]['value'] = $country;
274
			break;
275
		case 'state':
276
			$fields[$idx]['value'] = $state;
277
			break;
278
		case 'city':
279
			$fields[$idx]['value'] = $city;
280
			break;
281
		case 'organization':
282
			$fields[$idx]['value'] = $org;
283
			break;
284
		case 'email':
285
			$fields[$idx]['value'] = $email;
286
			break;
287
		}
288
	}
289
}
290

    
291
function step9_submitphpaction() {
292
	global $input_errors, $stepid, $savemsg, $_POST, $config;
293

    
294
	$certnames = array();
295
	$certcns = array();
296
	if (is_array($config['cert'])) {
297
		foreach($config['cert'] as $cert) {
298
			$certnames[] = $cert['descr'];
299
			$certinfo = cert_get_subject_hash($cert['crt']);
300
			$certcns[] = $certinfo["CN"];
301
		}	
302
	}
303

    
304
	if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
305
		$input_errors[] = "The field 'Descriptive Name' contains invalid characters.";
306
	}
307

    
308
	if (empty($_POST['descr']) || empty($_POST['keylength']) || empty($_POST['lifetime']) ||
309
	    empty($_POST['country']) || empty($_POST['state']) || empty($_POST['city']) ||
310
	    empty($_POST['organization']) || empty($_POST['email'])) {
311
		$stepid--;
312
		$savemsg = "Please enter all information for the new certificate.";
313
	} elseif (has_special_chars($_POST['country']) || has_special_chars($_POST['state']) || 
314
	    has_special_chars($_POST['city']) || has_special_chars($_POST['organization'])) {
315
		$stepid--;
316
		$input_errors[] = "Please do not use special characters in Certificate field names.";
317
	} elseif (in_array($_POST['descr'], $certnames) || in_array($_POST['descr'], $certcns)) {
318
		$stepid--;
319
		$savemsg = "Please enter a different name for the Certificate. A Certificate with that name/common name already exists.";	
320
	} elseif (strlen($_POST['country']) != 2) {
321
		$stepid--;
322
		$savemsg = "Please enter only a two-letter ISO country code";
323
	} else {
324
		$config['ovpnserver']['step9']['uselist'] = "on";
325
		$_POST['uselist'] = "on";
326
	}
327
}
328

    
329
function step10_stepbeforeformdisplay() {
330
	global $pkg, $stepid, $netbios_nodetypes;
331

    
332
	foreach ($pkg['step'][$stepid]['fields']['field'] as $idx => $field) {
333
		if ($field['name'] == "crypto") {
334
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'] = array();
335
			$cipherlist = openvpn_get_cipherlist();
336
			foreach ($cipherlist as $name => $desc) {
337
				$opt = array();
338
				$opt['name'] = $desc;
339
				$opt['value'] = $name;
340
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'][] = $opt;
341
			}
342
		} else if ($field['name'] == "digest") {
343
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'] = array();
344
			$digestlist = openvpn_get_digestlist();
345
			foreach ($digestlist as $name => $desc) {
346
				$opt = array();
347
				$opt['name'] = $desc;
348
				$opt['value'] = $name;
349
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'][] = $opt;
350
			}
351
		} else if ($field['name'] == "compression") {
352
			global $openvpn_compression_modes;
353
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'] = array();
354
			foreach ($openvpn_compression_modes as $name => $desc) {
355
				$opt = array();
356
				$opt['name'] = $desc;
357
				$opt['value'] = $name;
358
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'][] = $opt;
359
			}
360
		} else if ($field['name'] == "engine") {
361
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'] = array();
362
			$engines = openvpn_get_engines();
363
			foreach ($engines as $name => $desc) {
364
				$opt = array();
365
				$opt['name'] = $desc;
366
				$opt['value'] = $name;
367
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'][] = $opt;
368
			}
369
		} else if ($field['name'] == "nbttype") {
370
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'] = array();
371
			foreach ($netbios_nodetypes as $type => $name) {
372
				$opt = array();
373
				$opt['name'] = $name;
374
				$opt['value'] = $type;
375
			$pkg['step'][$stepid]['fields']['field'][$idx]['options']['option'][] = $opt;
376
			}
377
		} else if ($field['name'] == "localport") {
378
			$pkg['step'][$stepid]['fields']['field'][$idx]['value'] = openvpn_port_next('UDP');
379
		}
380
	}
381
}
382

    
383
function step10_submitphpaction() {
384
	global $savemsg, $stepid;
385

    
386
	/* Default OpenVPN port to next available port if left empty. */
387
	if (empty($_POST['localport']))
388
		$pconfig["step10"]["localport"] = openvpn_port_next('UDP');
389

    
390
	/* input validation */
391
	if ($result = openvpn_validate_port($_POST['localport'], 'Local port'))
392
		$input_errors[] = $result;
393

    
394
	if ($result = openvpn_validate_cidr($_POST['tunnelnet'], 'Tunnel Network', false, "ipv4"))
395
		$input_errors[] = $result;
396

    
397
	if ($result = openvpn_validate_cidr($_POST['localnet'], 'Local Network', true, "ipv4"))
398
		$input_errors[] = $result;
399

    
400
	$portused = openvpn_port_used($_POST['protocol'], $_POST['interface'], $_POST['localport']);
401
	if ($portused != 0)
402
		$input_errors[] = "The specified 'Local port' is in use. Please enter a port not already in use.";
403

    
404
	if (!isset($_POST['generatetlskey']) && isset($_POST['tlsauthentication']))
405
		if (!strstr($_POST['tlssharedkey'], "-----BEGIN OpenVPN Static key V1-----") ||
406
			!strstr($_POST['tlssharedkey'], "-----END OpenVPN Static key V1-----"))
407
			$input_errors[] = "The field 'TLS Authentication Key' does not appear to be valid";
408

    
409
	if (!empty($_POST['dnsserver1']) && !is_ipaddr(trim($_POST['dnsserver1'])))
410
		$input_errors[] = "The field 'DNS Server #1' must contain a valid IP address";
411
	if (!empty($_POST['dnsserver2']) && !is_ipaddr(trim($_POST['dnsserver2'])))
412
		$input_errors[] = "The field 'DNS Server #2' must contain a valid IP address";
413
	if (!empty($_POST['dnsserver3']) && !is_ipaddr(trim($_POST['dnsserver3'])))
414
		$input_errors[] = "The field 'DNS Server #3' must contain a valid IP address";
415
	if (!empty($_POST['dnsserver4']) && !is_ipaddr(trim($_POST['dnsserver4'])))
416
		$input_errors[] = "The field 'DNS Server #4' must contain a valid IP address";
417

    
418
	if (!empty($_POST['ntpserver1']) && !is_ipaddr(trim($_POST['ntpserver1'])))
419
		$input_errors[] = "The field 'NTP Server #1' must contain a valid IP address";
420
	if (!empty($_POST['ntpserver2']) && !is_ipaddr(trim($_POST['ntpserver2'])))
421
		$input_errors[] = "The field 'NTP Server #2' must contain a valid IP address";
422

    
423
	if (!empty($_POST['winsserver1']) && !is_ipaddr(trim($_POST['winsserver1'])))
424
		$input_errors[] = "The field 'WINS Server #1' must contain a valid IP address";
425
	if (!empty($_POST['winsserver2']) && !is_ipaddr(trim($_POST['winsserver2'])))
426
		$input_errors[] = "The field 'WINS Server #2' must contain a valid IP address";
427

    
428
	if ($_POST['concurrentcon'] && !is_numeric($_POST['concurrentcon']))
429
		$input_errors[] = "The field 'Concurrent connections' must be numeric.";
430

    
431
	if (empty($_POST['tunnelnet']))
432
		$input_errors[] = "You must specify a 'Tunnel network'.";
433

    
434
	if (count($input_errors) > 0) {
435
		$savemsg = $input_errors[0];
436
		$stepid = $stepid - 1;
437
	}
438
}
439

    
440
function step12_submitphpaction() {
441
	global $config;
442

    
443
	$pconfig = $config['ovpnserver'];
444

    
445
	if (!is_array($config['ovpnserver'])) {
446
		$message = "No configuration found, please try again.";
447
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=1&message={$message}");
448
		exit;
449
	}
450

    
451
	if ($pconfig['step1']['type'] == "local") {
452
		$auth = array();
453
		$auth['name'] = "Local Database";
454
		$auth['type'] = "local";
455
	} else if (isset($pconfig['step2']['uselist'])) {
456
		$auth = array();
457
		$auth['type'] = $pconfig['step1']['type'];
458
		$auth['refid'] = uniqid();
459
		$auth['name'] = $pconfig['step2']['authtype'];
460

    
461
		if ($auth['type'] == "ldap") {
462
			$auth['host'] = $pconfig['step2']['ip'];
463
			$auth['ldap_port'] = $pconfig['step2']['port'];
464
			if ($pconfig['step1']['transport'] == "tcp")
465
				$auth['ldap_urltype'] = 'TCP - Standard';
466
			else
467
				$auth['ldap_urltype'] = 'SSL - Encrypted';
468
			$auth['ldap_protver'] = 3;
469
			$auth['ldap_scope'] = $pconfig['step2']['scope'];
470
			$auth['ldap_basedn'] = $pconfig['step2']['basedn'];
471
			$auth['ldap_authcn'] = $pconfig['step2']['authscope'];
472
			$auth['ldap_binddn'] = $pconfig['step2']['userdn'];
473
			$auth['ldap_bindpw'] = $pconfig['step2']['passdn'];
474
			$auth['ldap_attr_user'] = $pconfig['step1']['nameattr'];
475
			$auth['ldap_attr_member'] = $pconfig['step1']['memberattr'];
476
			$auth['ldap_attr_group'] = $pconfig['step1']['groupattr'];
477
		} else if ($auth['type'] == "radius") {
478
			$auth['host'] = $pconfig['step2']['ip'];
479
			$auth['radius_auth_port'] = $pconfig['step2']['port'];
480
			$auth['radius_secret'] = $pconfig['step2']['password'];
481
			$auth['radius_srvcs'] = "auth";
482
		}
483
		if (!is_array($config['system']['authserver']))
484
			$config['system']['authserver'] = array();
485

    
486
		$config['system']['authserver'][] = $auth;
487
	} else if (!isset($pconfig['step2']['uselist']) && empty($pconfig['step2']['authserv'])) {
488
		$message = "Please choose an authentication server .";
489
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=1&message={$message}");
490
		exit;
491
	} else if (!($auth = auth_get_authserver($pconfig['step2']['authserv']))) {
492
		$message = "An invalid authentication server has been specified.";
493
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=1&message={$message}");
494
		exit;
495
	}
496

    
497
	if (isset($pconfig['step6']['uselist']) && !empty($pconfig['step6']['certca'])) {
498
		$ca = array();
499
		$ca['refid'] = uniqid();
500
		$ca['descr'] = $pconfig['step6']['certca'];
501
		$dn = array(
502
			'countryName' => $pconfig['step6']['country'],
503
			'stateOrProvinceName' => $pconfig['step6']['state'],
504
			'localityName' => $pconfig['step6']['city'],
505
			'organizationName' => $pconfig['step6']['organization'],
506
			'emailAddress' => $pconfig['step6']['email'],
507
			'commonName' => $pconfig['step6']['certca']);
508

    
509
		ca_create($ca, $pconfig['step6']['keylength'], $pconfig['step6']['lifetime'], $dn, "sha256");
510
		if (!is_array($config['ca']))
511
			$config['ca'] = array();
512

    
513
		$config['ca'][] = $ca;
514
	} else if (!isset($pconfig['step6']['uselist']) && empty($pconfig['step6']['authcertca'])) {
515
		$message = "Please choose a Certificate Authority.";
516
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=5&message={$message}");
517
		exit;
518
	} else if (!($ca = lookup_ca($pconfig['step6']['authcertca']))) {
519
		$message = "An invalid Certificate Authority has been specified.";
520
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=5&message={$message}");
521
		exit;
522
	}
523

    
524
	if (isset($pconfig['step9']['uselist'])) {
525
		$cert = array();
526
		$cert['refid'] = uniqid();
527
		$cert['descr'] = $pconfig['step9']['certname'];
528
		$dn = array(
529
			'countryName' => $pconfig['step9']['country'],
530
			'stateOrProvinceName' => $pconfig['step9']['state'],
531
			'localityName' => $pconfig['step9']['city'],
532
			'organizationName' => $pconfig['step9']['organization'],
533
			'emailAddress' => $pconfig['step9']['email'],
534
			'commonName' => $pconfig['step9']['certname']);
535

    
536
		cert_create($cert, $ca['refid'], $pconfig['step9']['keylength'], $pconfig['step9']['lifetime'], $dn, 'server', "sha256");
537
		if (!is_array($config['cert']))
538
			$config['cert'] = array();
539

    
540
		$config['cert'][] = $cert;
541
	} else if (!isset($pconfig['step9']['uselist']) && empty($pconfig['step9']['authcertname'])) {
542
		$message = "Please choose a Certificate.";
543
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=7&message={$message}");
544
		exit;
545
	} else if (!($cert = lookup_cert($pconfig['step9']['authcertname']))) {
546
		$message = "An invalid Certificate has been specified.";
547
		header("Location:wizard.php?xml=openvpn_wizard.xml&stepid=7&message={$message}");
548
		exit;
549
	}
550
	$server = array();
551
	$server['vpnid'] = openvpn_vpnid_next();
552
	switch ($auth['type']) {
553
		case "ldap":
554
			$server['authmode'] = $auth['name'];
555
			$server['mode'] = "server_user";
556
			break;
557
		case "radius":
558
			$server['authmode'] = $auth['name'];
559
			$server['mode'] = "server_user";
560
			break;
561
		default:
562
			$server['authmode'] = "Local Database";
563
			$server['mode'] = "server_tls_user";
564
			break;
565
	}
566
	$server['caref'] = $ca['refid'];
567
	$server['certref'] = $cert['refid'];
568
	$server['protocol'] = $pconfig['step10']['protocol'];
569
	$server['interface'] = $pconfig['step10']['interface'];
570
	if (isset($pconfig['step10']['localport']))
571
		$server['local_port'] = $pconfig['step10']['localport'];
572

    
573
	if (strlen($pconfig['step10']['descr']) > 30)
574
		$pconfig['step10']['descr'] = substr($pconfig['step10']['descr'], 0, 30);
575
	$server['description'] = $pconfig['step10']['descr'];
576
	$server['custom_options'] = $pconfig['step10']['advanced'];
577
	if (isset($pconfig['step10']['tlsauth'])) {
578
		if (isset($pconfig['step10']['gentlskey']))
579
			$tlskey = openvpn_create_key();
580
		else
581
			$tlskey = $pconfig['step10']['tlskey'];
582
		$server['tls'] = base64_encode($tlskey);
583
	}
584
	$server['dh_length'] = $pconfig['step10']['dhkey'];
585
	$server['tunnel_network'] = $pconfig['step10']['tunnelnet'];
586
	if (isset($pconfig['step10']['rdrgw']))
587
		$server['gwredir'] = $pconfig['step10']['rdrgw'];
588
	if (isset($pconfig['step10']['localnet']))
589
		$server['local_network'] = $pconfig['step10']['localnet'];
590
	if (isset($pconfig['step10']['concurrentcon']))
591
		$server['maxclients'] = $pconfig['step10']['concurrentcon'];
592
	if (isset($pconfig['step10']['compression']))
593
		$server['compression'] = $pconfig['step10']['compression'];
594
	if (isset($pconfig['step10']['tos']))
595
		$server['passtos'] = $pconfig['step10']['tos'];
596
	if (isset($pconfig['step10']['interclient']))
597
		$server['client2client'] = $pconfig['step10']['interclient'];
598
	if (isset($pconfig['step10']['duplicate_cn']))
599
		$server['duplicate_cn'] = $pconfig['step10']['duplicate_cn'];
600
	if (isset($pconfig['step10']['dynip']))
601
		$server['dynamic_ip'] = $pconfig['step10']['dynip'];
602
	if (isset($pconfig['step10']['addrpool']))
603
		$server['pool_enable'] = $pconfig['step10']['addrpool'];
604
	if (isset($pconfig['step10']['defaultdomain']))
605
		$server['dns_domain'] = $pconfig['step10']['defaultdomain'];
606
	if (isset($pconfig['step10']['dns1']))
607
		$server['dns_server1'] = $pconfig['step10']['dns1'];
608
	if (isset($pconfig['step10']['dns2']))
609
		$server['dns_server2'] = $pconfig['step10']['dns2'];
610
	if (isset($pconfig['step10']['dns3']))
611
		$server['dns_server3'] = $pconfig['step10']['dns3'];
612
	if (isset($pconfig['step10']['dns4']))
613
		$server['dns_server4'] = $pconfig['step10']['dns4'];
614
	if (isset($pconfig['step10']['ntp1']))
615
		$server['ntp_server1'] = $pconfig['step10']['ntp1'];
616
	if (isset($pconfig['step10']['ntp2']))
617
		$server['ntp_server2'] = $pconfig['step10']['ntp2'];
618
	if (isset($pconfig['step10']['wins1']))
619
		$server['wins_server1'] = $pconfig['step10']['wins1'];
620
	if (isset($pconfig['step10']['wins2']))
621
		$server['wins_server2'] = $pconfig['step10']['wins2'];
622
	if (isset($pconfig['step10']['nbtenable'])) {
623
		$server['netbios_ntype'] = $pconfig['step10']['nbttype'];
624
		if (isset($pconfig['step10']['nbtscope']))
625
			$server['netbios_scope'] = $pconfig['step10']['nbtscope'];
626
		$server['netbios_enable'] = $pconfig['step10']['nbtenable'];
627
	}
628
	$server['crypto'] = $pconfig['step10']['crypto'];
629
	$server['digest'] = $pconfig['step10']['digest'];
630
	$server['engine'] = $pconfig['step10']['engine'];
631

    
632
	if (isset($pconfig['step11']['ovpnrule'])) {
633
		$rule = array();
634
		$rule['descr'] = sprintf(gettext("OpenVPN %s wizard"),$server['description']);
635
		/* Ensure the rule descr is not too long for pf to handle */
636
		if (strlen($rule['descr']) > 52)
637
			$rule['descr'] = substr($rule['descr'], 0, 52);
638
		$rule['direction'] = "in";
639
		$rule['source']['any'] = TRUE;
640
		$rule['destination']['network'] = $server['interface'] . "ip";
641
		$rule['destination']['port'] = $server['local_port'];
642
		$rule['interface'] = $server['interface'];
643
		$rule['protocol'] = strtolower($server['protocol']);
644
		$rule['type'] = "pass";
645
		$rule['enabled'] = "on";
646
		$rule['created'] = make_config_revision_entry(null, gettext("OpenVPN Wizard"));
647
		$config['filter']['rule'][] = $rule;
648
	}
649
	if (isset($pconfig['step11']['ovpnallow'])) {
650
		$rule = array();
651
		$rule['descr'] = sprintf(gettext("OpenVPN %s wizard"),$server['description']);
652
		/* Ensure the rule descr is not too long for pf to handle */
653
		if (strlen($rule['descr']) > 52)
654
			$rule['descr'] = substr($rule['descr'], 0, 52);
655
		$rule['source']['any'] = TRUE;
656
		$rule['destination']['any'] = TRUE;
657
		$rule['interface'] = "openvpn";
658
		//$rule['protocol'] = $server['protocol'];
659
		$rule['type'] = "pass";
660
		$rule['enabled'] = "on";
661
		$rule['created'] = make_config_revision_entry(null, gettext("OpenVPN Wizard"));
662
		$config['filter']['rule'][] = $rule;
663
	}
664

    
665
	if (!is_array($config['openvpn']['openvpn-server']))
666
		$config['openvpn']['openvpn-server'] = array();
667

    
668
	$config['openvpn']['openvpn-server'][] = $server;
669

    
670
	openvpn_resync('server', $server);
671
	write_config();
672
	header("Location: vpn_openvpn_server.php");
673
	exit;
674
}
675

    
676
?>
(1-1/7)