Project

General

Profile

Download (91.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
4
	All rights reserved.
5

    
6
	originally part of m0n0wall (http://m0n0.ch/wall)
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
1. Redistributions of source code must retain the above copyright notice,
14
	this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	notice, this list of conditions and the following disclaimer in the
18
	documentation and/or other materials provided with the distribution.
19

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

    
32
/*
33
	pfSense_BUILDER_BINARIES:	/usr/bin/find	/bin/cd	/usr/local/bin/rrdtool	/usr/bin/nice
34
	pfSense_MODULE:	config
35
*/
36

    
37
if(!function_exists("dump_rrd_to_xml")) 
38
	require("rrd.inc");
39

    
40
/* Upgrade functions must be named:
41
*    upgrade_XXX_to_YYY
42
	* where XXX == previous version, zero padded, and YYY == next version, zero padded
43
	*/
44
function upgrade_010_to_011() {
45
	global $config;
46
	$opti = 1;
47
	$ifmap = array('lan' => 'lan', 'wan' => 'wan', 'pptp' => 'pptp');
48

    
49
	/* convert DMZ to optional, if necessary */
50
	if (isset($config['interfaces']['dmz'])) {
51

    
52
		$dmzcfg = &$config['interfaces']['dmz'];
53

    
54
		if ($dmzcfg['if']) {
55
			$config['interfaces']['opt' . $opti] = array();
56
			$optcfg = &$config['interfaces']['opt' . $opti];
57

    
58
			$optcfg['enable'] = $dmzcfg['enable'];
59
			$optcfg['descr'] = "DMZ";
60
			$optcfg['if'] = $dmzcfg['if'];
61
			$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
62
			$optcfg['subnet'] = $dmzcfg['subnet'];
63

    
64
			$ifmap['dmz'] = "opt" . $opti;
65
			$opti++;
66
		}
67

    
68
		unset($config['interfaces']['dmz']);
69
	}
70

    
71
	/* convert WLAN1/2 to optional, if necessary */
72
	for ($i = 1; isset($config['interfaces']['wlan' . $i]); $i++) {
73

    
74
		if (!$config['interfaces']['wlan' . $i]['if']) {
75
			unset($config['interfaces']['wlan' . $i]);
76
			continue;
77
		}
78

    
79
		$wlancfg = &$config['interfaces']['wlan' . $i];
80
		$config['interfaces']['opt' . $opti] = array();
81
		$optcfg = &$config['interfaces']['opt' . $opti];
82

    
83
		$optcfg['enable'] = $wlancfg['enable'];
84
		$optcfg['descr'] = "WLAN" . $i;
85
		$optcfg['if'] = $wlancfg['if'];
86
		$optcfg['ipaddr'] = $wlancfg['ipaddr'];
87
		$optcfg['subnet'] = $wlancfg['subnet'];
88
		$optcfg['bridge'] = $wlancfg['bridge'];
89

    
90
		$optcfg['wireless'] = array();
91
		$optcfg['wireless']['mode'] = $wlancfg['mode'];
92
		$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
93
		$optcfg['wireless']['channel'] = $wlancfg['channel'];
94
		$optcfg['wireless']['wep'] = $wlancfg['wep'];
95

    
96
		$ifmap['wlan' . $i] = "opt" . $opti;
97

    
98
		unset($config['interfaces']['wlan' . $i]);
99
		$opti++;
100
	}
101

    
102
	/* convert filter rules */
103
	$n = count($config['filter']['rule']);
104
	for ($i = 0; $i < $n; $i++) {
105

    
106
		$fr = &$config['filter']['rule'][$i];
107

    
108
		/* remap interface */
109
		if (array_key_exists($fr['interface'], $ifmap))
110
			$fr['interface'] = $ifmap[$fr['interface']];
111
		else {
112
			/* remove the rule */
113
			printf(gettext("%sWarning: filter rule removed " .
114
				"(interface '%s' does not exist anymore)."), "\n", $fr['interface']);
115
			unset($config['filter']['rule'][$i]);
116
			continue;
117
		}
118

    
119
		/* remap source network */
120
		if (isset($fr['source']['network'])) {
121
			if (array_key_exists($fr['source']['network'], $ifmap))
122
				$fr['source']['network'] = $ifmap[$fr['source']['network']];
123
			else {
124
				/* remove the rule */
125
				printf(gettext("%sWarning: filter rule removed " .
126
					"(source network '%s' does not exist anymore)."), "\n", $fr['source']['network']);
127
				unset($config['filter']['rule'][$i]);
128
				continue;
129
			}
130
		}
131

    
132
		/* remap destination network */
133
		if (isset($fr['destination']['network'])) {
134
			if (array_key_exists($fr['destination']['network'], $ifmap))
135
				$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
136
			else {
137
				/* remove the rule */
138
				printf(gettext("%sWarning: filter rule removed " .
139
					"(destination network '%s' does not exist anymore)."), "\n", $fr['destination']['network']);
140
				unset($config['filter']['rule'][$i]);
141
				continue;
142
			}
143
		}
144
	}
145

    
146
	/* convert shaper rules */
147
	$n = count($config['pfqueueing']['rule']);
148
	if (is_array($config['pfqueueing']['rule']))
149
	for ($i = 0; $i < $n; $i++) {
150

    
151
		$fr = &$config['pfqueueing']['rule'][$i];
152

    
153
		/* remap interface */
154
		if (array_key_exists($fr['interface'], $ifmap))
155
			$fr['interface'] = $ifmap[$fr['interface']];
156
		else {
157
			/* remove the rule */
158
			printf(gettext("%sWarning: traffic shaper rule removed " .
159
				"(interface '%s' does not exist anymore)."), "\n", $fr['interface']);
160
			unset($config['pfqueueing']['rule'][$i]);
161
			continue;
162
		}
163

    
164
		/* remap source network */
165
		if (isset($fr['source']['network'])) {
166
			if (array_key_exists($fr['source']['network'], $ifmap))
167
				$fr['source']['network'] = $ifmap[$fr['source']['network']];
168
			else {
169
				/* remove the rule */
170
				printf(gettext("%sWarning: traffic shaper rule removed " .
171
					"(source network '%s' does not exist anymore)."), "\n", $fr['source']['network']);
172
				unset($config['pfqueueing']['rule'][$i]);
173
				continue;
174
			}
175
		}
176

    
177
		/* remap destination network */
178
		if (isset($fr['destination']['network'])) {
179
			if (array_key_exists($fr['destination']['network'], $ifmap))
180
				$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
181
			else {
182
				/* remove the rule */
183
				printf(gettext("%sWarning: traffic shaper rule removed " .
184
					"(destination network '%s' does not exist anymore)."), "\n", $fr['destination']['network']);
185
				unset($config['pfqueueing']['rule'][$i]);
186
				continue;
187
			}
188
		}
189
	}
190
}
191

    
192

    
193
function upgrade_011_to_012() {
194
	global $config;
195
	/* move LAN DHCP server config */
196
	$tmp = $config['dhcpd'];
197
	$config['dhcpd'] = array();
198
	$config['dhcpd']['lan'] = $tmp;
199

    
200
	/* encrypt password */
201
	$config['system']['password'] = crypt($config['system']['password']);
202
}
203

    
204

    
205
function upgrade_012_to_013() {
206
	global $config;
207
	/* convert advanced outbound NAT config */
208
	for ($i = 0; isset($config['nat']['advancedoutbound']['rule'][$i]); $i++) {
209
		$curent = &$config['nat']['advancedoutbound']['rule'][$i];
210
		$src = $curent['source'];
211
		$curent['source'] = array();
212
		$curent['source']['network'] = $src;
213
		$curent['destination'] = array();
214
		$curent['destination']['any'] = true;
215
	}
216

    
217
	/* add an explicit type="pass" to all filter rules to make things consistent */
218
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
219
		$config['filter']['rule'][$i]['type'] = "pass";
220
	}
221
}
222

    
223

    
224
function upgrade_013_to_014() {
225
	global $config;
226
	/* convert shaper rules (make pipes) */
227
	if (is_array($config['pfqueueing']['rule'])) {
228
		$config['pfqueueing']['pipe'] = array();
229

    
230
		for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
231
			$curent = &$config['pfqueueing']['rule'][$i];
232

    
233
			/* make new pipe and associate with this rule */
234
			$newpipe = array();
235
			$newpipe['descr'] = $curent['descr'];
236
			$newpipe['bandwidth'] = $curent['bandwidth'];
237
			$newpipe['delay'] = $curent['delay'];
238
			$newpipe['mask'] = $curent['mask'];
239
			$config['pfqueueing']['pipe'][$i] = $newpipe;
240

    
241
			$curent['targetpipe'] = $i;
242

    
243
			unset($curent['bandwidth']);
244
			unset($curent['delay']);
245
			unset($curent['mask']);
246
		}
247
	}
248
}
249

    
250

    
251
function upgrade_014_to_015() {
252
	global $config;
253
	/* Default route moved */
254
	if (isset($config['interfaces']['wan']['gateway']))
255
		if ($config['interfaces']['wan']['gateway'] <> "")
256
		$config['interfaces']['wan']['gateway'] = $config['interfaces']['wan']['gateway'];
257
	unset($config['interfaces']['wan']['gateway']);
258

    
259
	/* Queues are no longer interface specific */
260
	if (isset($config['interfaces']['lan']['schedulertype']))
261
		unset($config['interfaces']['lan']['schedulertype']);
262
	if (isset($config['interfaces']['wan']['schedulertype']))
263
		unset($config['interfaces']['wan']['schedulertype']);
264

    
265
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
266
		if(isset($config['interfaces']['opt' . $i]['schedulertype']))
267
			unset($config['interfaces']['opt' . $i]['schedulertype']);
268
	}
269
}
270

    
271

    
272
function upgrade_015_to_016() {
273
	global $config;
274
	/* Alternate firmware URL moved */
275
	if (isset($config['system']['firmwareurl']) && isset($config['system']['firmwarename'])) { // Only convert if *both* are defined.
276
		$config['system']['alt_firmware_url'] = array();
277
		$config['system']['alt_firmware_url']['enabled'] = "";
278
		$config['system']['alt_firmware_url']['firmware_base_url'] = $config['system']['firmwareurl'];
279
		$config['system']['alt_firmware_url']['firmware_filename'] = $config['system']['firmwarename'];
280
		unset($config['system']['firmwareurl'], $config['system']['firmwarename']);
281
	} else {
282
		unset($config['system']['firmwareurl'], $config['system']['firmwarename']);
283
	}
284
}
285

    
286

    
287
function upgrade_016_to_017() {
288
	global $config;
289
	/* wipe previous shaper configuration */
290
	unset($config['shaper']['queue']);
291
	unset($config['shaper']['rule']);
292
	unset($config['interfaces']['wan']['bandwidth']);
293
	unset($config['interfaces']['wan']['bandwidthtype']);
294
	unset($config['interfaces']['lan']['bandwidth']);
295
	unset($config['interfaces']['lan']['bandwidthtype']);
296
	$config['shaper']['enable'] = FALSE;
297
}
298

    
299

    
300
function upgrade_017_to_018() {
301
	global $config;
302
	if(isset($config['proxyarp']) && is_array($config['proxyarp']['proxyarpnet'])) {
303
		$proxyarp = &$config['proxyarp']['proxyarpnet'];
304
		foreach($proxyarp as $arpent){
305
			$vip = array();
306
			$vip['mode'] = "proxyarp";
307
			$vip['interface'] = $arpent['interface'];
308
			$vip['descr'] = $arpent['descr'];
309
			if (isset($arpent['range'])) {
310
				$vip['range'] = $arpent['range'];
311
				$vip['type'] = "range";
312
			} else {
313
				$subnet = explode('/', $arpent['network']);
314
				$vip['subnet'] = $subnet[0];
315
				if (isset($subnet[1])) {
316
					$vip['subnet_bits'] = $subnet[1];
317
					$vip['type'] = "network";
318
				} else {
319
					$vip['subnet_bits'] = "32";
320
					$vip['type'] = "single";
321
				}
322
			}
323
			$config['virtualip']['vip'][] = $vip;
324
		}
325
		unset($config['proxyarp']);
326
	}
327
	if(isset($config['installedpackages']) && isset($config['installedpackages']['carp']) && is_array($config['installedpackages']['carp']['config'])) {
328
		$carp = &$config['installedpackages']['carp']['config'];
329
		foreach($carp as $carpent){
330
			$vip = array();
331
			$vip['mode'] = "carp";
332
			$vip['interface'] = "AUTO";
333
			$vip['descr'] = sprintf(gettext("CARP vhid %s"), $carpent['vhid']);
334
			$vip['type'] = "single";
335
			$vip['vhid'] = $carpent['vhid'];
336
			$vip['advskew'] = $carpent['advskew'];
337
			$vip['password'] = $carpent['password'];
338
			$vip['subnet'] = $carpent['ipaddress'];
339
			$vip['subnet_bits'] = $carpent['netmask'];
340
			$config['virtualip']['vip'][] = $vip;
341
		}
342
		unset($config['installedpackages']['carp']);
343
	}
344
	/* Server NAT is no longer needed */
345
	unset($config['nat']['servernat']);
346

    
347
	/* enable SSH */
348
	if ($config['version'] == "1.8") {
349
		$config['system']['sshenabled'] = true;
350
	}
351
}
352

    
353

    
354
function upgrade_018_to_019() {
355
	global $config;
356
	$config['theme']="metallic";
357
}
358

    
359

    
360
function upgrade_019_to_020() {
361
	global $config;
362
	if(is_array($config['ipsec']['tunnel'])) {
363
		reset($config['ipsec']['tunnel']);
364
		while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
365
			/* Sanity check on required variables */
366
			/* This fixes bogus <tunnel> entries - remnant of bug #393 */
367
			if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
368
				unset($config['ipsec']['tunnel'][$tunnel]);
369
			}
370
		}
371
	}
372
}
373

    
374
function upgrade_020_to_021() {
375
	global $config;
376
	/* shaper scheduler moved */
377
	if(isset($config['system']['schedulertype'])) {
378
		$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
379
		unset($config['system']['schedulertype']);
380
	}
381
}
382

    
383

    
384
function upgrade_021_to_022() {
385
	global $config;
386
	/* move gateway to wan interface */
387
	$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
388
}
389

    
390
function upgrade_022_to_023() {
391
	global $config;
392
	if(isset($config['shaper'])) {
393
		/* wipe previous shaper configuration */
394
		unset($config['shaper']);
395
	}
396
}
397

    
398

    
399
function upgrade_023_to_024() {
400
	global $config;
401
}
402

    
403

    
404
function upgrade_024_to_025() {
405
	global $config;
406
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
407
	unset($config['system']['use_rrd_gateway']);
408
}
409

    
410

    
411
function upgrade_025_to_026() {
412
	global $config;
413
	$cron_item = array();
414
	$cron_item['minute'] = "0";
415
	$cron_item['hour'] = "*";
416
	$cron_item['mday'] = "*";
417
	$cron_item['month'] = "*";
418
	$cron_item['wday'] = "*";
419
	$cron_item['who'] = "root";
420
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
421

    
422
	$config['cron']['item'][] = $cron_item;
423

    
424
	$cron_item = array();
425
	$cron_item['minute'] = "1,31";
426
	$cron_item['hour'] = "0-5";
427
	$cron_item['mday'] = "*";
428
	$cron_item['month'] = "*";
429
	$cron_item['wday'] = "*";
430
	$cron_item['who'] = "root";
431
	$cron_item['command'] = "/usr/bin/nice -n20 adjkerntz -a";
432

    
433
	$config['cron']['item'][] = $cron_item;
434

    
435
	$cron_item = array();
436
	$cron_item['minute'] = "1";
437
	$cron_item['hour'] = "*";
438
	$cron_item['mday'] = "1";
439
	$cron_item['month'] = "*";
440
	$cron_item['wday'] = "*";
441
	$cron_item['who'] = "root";
442
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
443

    
444
	$config['cron']['item'][] = $cron_item;
445

    
446
	$cron_item = array();
447
	$cron_item['minute'] = "*/60";
448
	$cron_item['hour'] = "*";
449
	$cron_item['mday'] = "*";
450
	$cron_item['month'] = "*";
451
	$cron_item['wday'] = "*";
452
	$cron_item['who'] = "root";
453
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout";
454

    
455
	$config['cron']['item'][] = $cron_item;
456

    
457
	$cron_item = array();
458
	$cron_item['minute'] = "1";
459
	$cron_item['hour'] = "1";
460
	$cron_item['mday'] = "*";
461
	$cron_item['month'] = "*";
462
	$cron_item['wday'] = "*";
463
	$cron_item['who'] = "root";
464
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.dyndns.update";
465

    
466
	$config['cron']['item'][] = $cron_item;
467

    
468
	$cron_item = array();
469
	$cron_item['minute'] = "*/60";
470
	$cron_item['hour'] = "*";
471
	$cron_item['mday'] = "*";
472
	$cron_item['month'] = "*";
473
	$cron_item['wday'] = "*";
474
	$cron_item['who'] = "root";
475
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 virusprot";
476

    
477
	$config['cron']['item'][] = $cron_item;
478

    
479
	$cron_item = array();
480
	$cron_item['minute'] = "*/60";
481
	$cron_item['hour'] = "*";
482
	$cron_item['mday'] = "*";
483
	$cron_item['month'] = "*";
484
	$cron_item['wday'] = "*";
485
	$cron_item['who'] = "root";
486
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -t 1800 snort2c";
487

    
488
	$config['cron']['item'][] = $cron_item;
489
}
490

    
491

    
492
function upgrade_026_to_027() {
493
	global $config;
494
}
495

    
496

    
497
function upgrade_027_to_028() {
498
	global $config;
499
}
500

    
501

    
502
function upgrade_028_to_029() {
503
	global $config;
504
	$rule_item = array();
505
	$a_filter = &$config['filter']['rule'];
506
	$rule_item['interface'] = "enc0";
507
	$rule_item['type'] = "pass";
508
	$rule_item['source']['any'] = true;
509
	$rule_item['destination']['any'] = true;
510
	$rule_item['descr'] = gettext("Permit IPsec traffic.");
511
	$rule_item['statetype'] = "keep state";
512
	$a_filter[] = $rule_item;
513
}
514

    
515

    
516
function upgrade_029_to_030() {
517
	global $config;
518
	/* enable the rrd config setting by default */
519
	$config['rrd']['enable'] = true;
520
}
521

    
522

    
523
function upgrade_030_to_031() {
524
	global $config;
525
	/* Insert upgrade code here */
526
}
527

    
528

    
529
function upgrade_031_to_032() {
530
	global $config;
531
	/* Insert upgrade code here */
532
}
533

    
534

    
535
function upgrade_032_to_033() {
536
	global $config;
537
	/* Insert upgrade code here */
538
}
539

    
540

    
541
function upgrade_033_to_034() {
542
	global $config;
543
	/* Insert upgrade code here */
544
}
545

    
546

    
547
function upgrade_034_to_035() {
548
	global $config;
549
	/* Insert upgrade code here */
550
}
551

    
552

    
553
function upgrade_035_to_036() {
554
	global $config;
555
	/* Insert upgrade code here */
556
}
557

    
558

    
559
function upgrade_036_to_037() {
560
	global $config;
561
	/* Insert upgrade code here */
562
}
563

    
564

    
565
function upgrade_037_to_038() {
566
	global $config;
567
	/* Insert upgrade code here */
568
}
569

    
570

    
571
function upgrade_038_to_039() {
572
	global $config;
573
	/* Insert upgrade code here */
574
}
575

    
576

    
577
function upgrade_039_to_040() {
578
	global $config, $g;
579
	$config['system']['webgui']['auth_method'] = "session";
580
	$config['system']['webgui']['backing_method'] = "htpasswd";
581

    
582
	if (isset ($config['system']['username'])) {
583
		$config['system']['group'] = array();
584
		$config['system']['group'][0]['name'] = "admins";
585
		$config['system']['group'][0]['description'] = gettext("System Administrators");
586
		$config['system']['group'][0]['scope'] = "system";
587
		$config['system']['group'][0]['priv'] = "page-all";
588
		$config['system']['group'][0]['home'] = "index.php";
589
		$config['system']['group'][0]['gid'] = "110";
590

    
591
		$config['system']['user'] = array();
592
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
593
		$config['system']['user'][0]['descr'] = "System Administrator";
594
		$config['system']['user'][0]['scope'] = "system";
595
		$config['system']['user'][0]['groupname'] = "admins";
596
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
597
		$config['system']['user'][0]['uid'] = "0";
598
		/* Ensure that we follow what this new "admin" username should be in the session. */
599
		$_SESSION["Username"] = "{$config['system']['username']}";
600

    
601
		$config['system']['user'][0]['priv'] = array();
602
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
603
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
604
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
605
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
606
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
607
		$config['system']['user'][0]['priv'][1]['descr'] = gettext("Indicates whether this user will lock individual HTML pages after having accessed a particular page (the lock will be freed if the user leaves or saves the page form).");
608
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
609
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
610
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
611
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
612
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
613
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly)."), $g['product_name']);
614
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
615
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
616
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (you should associate this privilege only with one single user).");
617

    
618
		$config['system']['nextuid'] = "111";
619
		$config['system']['nextgid'] = "111";
620

    
621
		/* wipe previous auth configuration */
622
		unset ($config['system']['username']);
623
		unset ($config['system']['password']);
624
	}
625
}
626

    
627
function upgrade_040_to_041() {
628
	global $config;
629
	if(!$config['sysctl']) {
630
		$config['sysctl']['item'] = array();
631

    
632
		$config['sysctl']['item'][0]['tunable'] = "net.inet.tcp.blackhole";
633
		$config['sysctl']['item'][0]['descr'] =    gettext("Drop packets to closed TCP ports without returning a RST");
634
		$config['sysctl']['item'][0]['value'] =   "default";
635

    
636
		$config['sysctl']['item'][1]['tunable'] = "net.inet.udp.blackhole";
637
		$config['sysctl']['item'][1]['descr'] =    gettext("Do not send ICMP port unreachable messages for closed UDP ports");
638
		$config['sysctl']['item'][1]['value'] =   "default";
639

    
640
		$config['sysctl']['item'][2]['tunable'] = "net.inet.ip.random_id";
641
		$config['sysctl']['item'][2]['descr'] =    gettext("Randomize the ID field in IP packets (default is 0: sequential IP IDs)");
642
		$config['sysctl']['item'][2]['value'] =   "default";
643

    
644
		$config['sysctl']['item'][3]['tunable'] = "net.inet.tcp.drop_synfin";
645
		$config['sysctl']['item'][3]['descr'] =    gettext("Drop SYN-FIN packets (breaks RFC1379, but nobody uses it anyway)");
646
		$config['sysctl']['item'][3]['value'] =   "default";
647

    
648
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
649
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
650
		$config['sysctl']['item'][4]['value'] =   "default";
651

    
652
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
653
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
654
		$config['sysctl']['item'][5]['value'] =   "default";
655

    
656
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
657
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
658
		$config['sysctl']['item'][6]['value'] =   "default";
659

    
660
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
661
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
662
		$config['sysctl']['item'][7]['value'] =   "default";
663

    
664
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
665
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
666
		$config['sysctl']['item'][8]['value'] =   "default";
667

    
668
		$config['sysctl']['item'][9]['tunable'] = "net.inet.ip.fastforwarding";
669
		$config['sysctl']['item'][9]['descr'] =    gettext("Fastforwarding (see http://lists.freebsd.org/pipermail/freebsd-net/2004-January/002534.html)");
670
		$config['sysctl']['item'][9]['value'] =   "default";
671

    
672
		$config['sysctl']['item'][10]['tunable'] = "net.inet.tcp.delayed_ack";
673
		$config['sysctl']['item'][10]['descr'] =    gettext("Do not delay ACK to try and piggyback it onto a data packet");
674
		$config['sysctl']['item'][10]['value'] =   "default";
675

    
676
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
677
		$config['sysctl']['item'][11]['descr'] =    gettext("Maximum outgoing UDP datagram size");
678
		$config['sysctl']['item'][11]['value'] =   "default";
679

    
680
		$config['sysctl']['item'][12]['tunable'] = "net.link.bridge.pfil_onlyip";
681
		$config['sysctl']['item'][12]['descr'] =    gettext("Handling of non-IP packets which are not passed to pfil (see if_bridge(4))");
682
		$config['sysctl']['item'][12]['value'] =   "default";
683

    
684
		$config['sysctl']['item'][13]['tunable'] = "net.link.tap.user_open";
685
		$config['sysctl']['item'][13]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
686
		$config['sysctl']['item'][13]['value'] =   "default";
687

    
688
		$config['sysctl']['item'][15]['tunable'] = "kern.randompid";
689
		$config['sysctl']['item'][15]['descr'] =    gettext("Randomize PID's (see src/sys/kern/kern_fork.c: sysctl_kern_randompid())");
690
		$config['sysctl']['item'][15]['value'] =   "default";
691

    
692
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.inflight.enable";
693
		$config['sysctl']['item'][16]['descr'] =    gettext("The system will attempt to calculate the bandwidth delay product for each connection and limit the amount of data queued to the network to just the amount required to maintain optimum throughput. ");
694
		$config['sysctl']['item'][16]['value'] =   "default";
695

    
696
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
697
		$config['sysctl']['item'][17]['descr'] =    gettext("Set ICMP Limits");
698
		$config['sysctl']['item'][17]['value'] =   "default";
699

    
700
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
701
		$config['sysctl']['item'][18]['descr'] =    gettext("TCP Offload engine");
702
		$config['sysctl']['item'][18]['value'] =   "default";
703
		
704
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
705
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
706
		$config['sysctl']['item'][19]['value'] =   "default";
707

    
708
		$config['sysctl']['item'][20]['tunable'] = "hw.syscons.kbd_reboot";
709
		$config['sysctl']['item'][20]['descr'] =    "Enables ctrl+alt+delete";
710
		$config['sysctl']['item'][20]['value'] =   "default";
711

    
712
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
713
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
714
		$config['sysctl']['item'][21]['value'] =   "default";
715

    
716
	}
717
}
718

    
719

    
720
function upgrade_041_to_042() {
721
	global $config;
722
	if (isset($config['shaper']))
723
		unset($config['shaper']);
724
	if (isset($config['ezshaper']))
725
		unset($config['ezshaper']);
726
}
727

    
728

    
729
function upgrade_042_to_043() {
730
	global $config;
731
	/* migrate old interface gateway to the new gateways config */
732
	$iflist = get_configured_interface_list(false, true);
733
	$gateways = array();
734
	$i = 0;
735
	foreach($iflist as $ifname => $interface) {
736
		if(! interface_has_gateway($ifname)) {
737
			continue;
738
		}
739
		$config['gateways']['gateway_item'][$i] = array();
740
		if(is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
741
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
742
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Static Gateway"), $ifname);
743
		} else {
744
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
745
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Dynamic Gateway"), $ifname);
746
		}
747
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
748
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
749
		/* add default gateway bit for wan on upgrade */
750
		if($ifname == "wan") {
751
			 $config['gateways']['gateway_item'][$i]['defaultgw'] = true;
752
		}
753
		if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
754
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
755
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
756
		}
757
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
758

    
759
		/* Update all filter rules which might reference this gateway */
760
		$j = 0;
761
		foreach($config['filter']['rule'] as $rule) {
762
			if(is_ipaddr($rule['gateway'])) {
763
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway'])
764
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
765
				else if ($rule['gateway'] == $ifname)
766
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
767
			}
768
			$j++;
769
		}
770

    
771
		/* rename old Quality RRD files in the process */
772
		$rrddbpath = "/var/db/rrd";
773
		$gwname = "GW_" . strtoupper($ifname);
774
		if(is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
775
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
776
		}
777
		$i++;
778
	}
779
}
780

    
781

    
782
function upgrade_043_to_044() {
783
	global $config;
784

    
785
	/* migrate static routes to the new gateways config */
786
	$gateways = return_gateways_array(true);
787
	$i = 0;
788
	if (is_array($config['staticroutes']['route'])) {
789
		$gwmap = array();
790
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
791
			$found = false;
792
			foreach ($gateways as $gwname => $gw) {
793
				if ($gw['gateway'] == $sroute['gateway']) {
794
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
795
					$found = true;
796
					break;
797
				}
798
			}
799
			if($gwmap[$sroute['gateway']]) {
800
				/* We already added a gateway name for this IP */
801
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
802
				$found = true;
803
			}			
804
			if ($found == false) {
805
				$gateway = array();
806
				$gateway['name'] = "SROUTE{$i}";
807
				$gwmap[$sroute['gateway']] = $gateway['name'];
808
				$gateway['gateway'] = $sroute['gateway'];
809
				$gateway['interface'] = $sroute['interface'];
810
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
811
				if (!is_array($config['gateways']['gateway_item']))
812
					$config['gateways']['gateway_item'] = array();
813
				$config['gateways']['gateway_item'][] = $gateway;
814
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
815
				$i++;
816
			}
817
		}
818
	}
819
}
820

    
821

    
822
function upgrade_044_to_045() {
823
	global $config;
824
	$iflist = get_configured_interface_list(false, true);
825
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
826
		$i = 0;
827
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
828
			/* Make sure to update the interfaces section with the right name */
829
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
830
			foreach($iflist as $ifname) {
831
				if($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
832
					$config['interfaces'][$ifname]['if'] = $vlan_name;
833
					continue;
834
				}
835
			}
836
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
837
			$i++;			
838
		}
839
	}
840
}
841

    
842

    
843
function upgrade_045_to_046() {
844
	global $config;
845
	/* Load up monitors that are in the default config for 2.0 but not in 1.2.3 
846
		thus wouldn't be in an upgraded config. */
847
	$config['load_balancer']['monitor_type'] = array (
848
		array ( 'name' => 'ICMP',
849
			'type' => 'icmp',
850
			'descr' => 'ICMP',
851
			'options' => '',
852
		),
853
		array ( 'name' => 'TCP',
854
			'type' => 'tcp',
855
			'descr' => 'Generic TCP',
856
			'options' => '',
857
		),
858
		array ( 'name' => 'HTTP',
859
			'type' => 'http',
860
			'descr' => 'Generic HTTP',
861
			'options' =>
862
			array ( 'path' => '/',
863
				'host' => '',
864
				'code' => '200',
865
			),
866
		),
867
		array ( 'name' => 'HTTPS',
868
			'type' => 'https',
869
			'descr' => 'Generic HTTPS',
870
			'options' =>
871
			array ( 'path' => '/',
872
				'host' => '',
873
				'code' => '200',
874
			),
875
		),
876
		array ( 'name' => 'SMTP',
877
			'type' => 'send',
878
			'descr' => 'Generic SMTP',
879
			'options' =>
880
			array ( 'send' => '',
881
				'expect' => '220 *',
882
			),
883
		),
884
	);
885
	/* Upgrade load balancer from slb to relayd */
886
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
887
		$vs_a = &$config['load_balancer']['virtual_server'];
888
		$pool_a = &$config['load_balancer']['lbpool'];
889
		$pools = array();
890
		/* Index pools by name */
891
		if(is_array($pool_a)) {
892
			for ($i = 0; isset($pool_a[$i]); $i++) {
893
				if($pool_a[$i]['type'] == "server") {
894
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
895
				}
896
			}
897
		}
898
		/* Convert sitedown entries to pools and re-attach */
899
		for ($i = 0; isset($vs_a[$i]); $i++) {
900
			/* Set mode while we're here. */
901
			$vs_a[$i]['mode'] = "redirect_mode";
902
			if (isset($vs_a[$i]['sitedown'])) {
903
				$pool = array();
904
				$pool['type'] = 'server';
905
				$pool['behaviour'] = 'balance';
906
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
907
				$pool['descr'] = sprintf(gettext("Sitedown pool for VS: %s"), $vs_a[$i]['name']);
908
				if (is_array($vs_a[$i]['pool']))
909
					$vs_a[$i]['pool'] = $vs_a[$i]['pool'][0];
910
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
911
				$pool['servers'] = array();
912
				$pool['servers'][] = $vs_a[$i]['sitedown'];
913
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
914
				$pool_a[] = $pool;
915
				$vs_a[$i]['sitedown'] = $pool['name'];
916
			}
917
		}
918
	}
919
	if(count($config['load_balancer']) == 0) {
920
		unset($config['load_balancer']);
921
	}
922
	mwexec('/usr/sbin/pw groupadd -n _relayd -g 913');
923
	mwexec('/usr/sbin/pw useradd -n _relayd -c "Relay Daemon" -d /var/empty -s /usr/sbin/nologin -u 913 -g 913');
924
}
925

    
926

    
927
function upgrade_046_to_047() {
928
	global $config;
929
	/* Upgrade IPsec from tunnel to phase1/phase2 */
930

    
931
	if(is_array($config['ipsec']['tunnel'])) {
932

    
933
		$a_phase1 = array();
934
		$a_phase2 = array();
935
		$ikeid = 0;
936

    
937
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
938

    
939
			unset($ph1ent);
940
			unset($ph2ent);
941

    
942
			/*
943
				*  attempt to locate an enabled phase1
944
				*  entry that matches the peer gateway
945
				*/
946

    
947
			if (!isset($tunnel['disabled'])) {
948

    
949
				$remote_gateway = $tunnel['remote-gateway'];
950

    
951
				foreach ($a_phase1 as $ph1tmp) {
952
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
953
						$ph1ent = $ph1tmp;
954
						break;
955
					}
956
				}
957
			}
958

    
959
			/* none found, create a new one */
960

    
961
			if (!isset( $ph1ent )) {
962

    
963
				/* build new phase1 entry */
964

    
965
				$ph1ent = array();
966

    
967
				$ph1ent['ikeid'] = ++$ikeid;
968

    
969
				if (isset($tunnel['disabled']))
970
					$ph1ent['disabled'] = $tunnel['disabled'];
971

    
972
				/* convert to the new vip[$vhid] name */
973
				if(preg_match("/^carp/", $tunnel['interface'])) {
974
					$carpid = str_replace("carp", "", $tunnel['interface']);
975
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
976
				}
977
				$ph1ent['interface'] = $tunnel['interface'];
978
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
979
				$ph1ent['descr'] = $tunnel['descr'];
980

    
981
				$ph1ent['mode'] = $tunnel['p1']['mode'];
982

    
983
				if (isset($tunnel['p1']['myident']['myaddress']))
984
					$ph1ent['myid_type'] = "myaddress";
985
				if (isset($tunnel['p1']['myident']['address'])) {
986
					$ph1ent['myid_type'] = "address";
987
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
988
				}
989
				if (isset($tunnel['p1']['myident']['fqdn'])) {
990
					$ph1ent['myid_type'] = "fqdn";
991
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
992
				}
993
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
994
					$ph1ent['myid_type'] = "user_fqdn";
995
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
996
				}
997
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
998
					$ph1ent['myid_type'] = "asn1dn";
999
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1000
				}
1001
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1002
					$ph1ent['myid_type'] = "dyn_dns";
1003
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1004
				}
1005

    
1006
				$ph1ent['peerid_type'] = "peeraddress";
1007

    
1008
				switch ($tunnel['p1']['encryption-algorithm']) {
1009
					case "des":
1010
					$ph1alg = array( 'name' => 'des' );
1011
					break;
1012
					case "3des":
1013
					$ph1alg = array( 'name' => '3des' );
1014
					break;
1015
					case "blowfish":
1016
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1017
					break;
1018
					case "cast128":
1019
					$ph1alg = array( 'name' => 'cast128' );
1020
					break;
1021
					case "rijndael":
1022
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1023
					break;
1024
					case "rijndael 256":
1025
					case "aes 256":
1026
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1027
					break;
1028
				}
1029

    
1030
				$ph1ent['encryption-algorithm'] = $ph1alg;
1031
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1032
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1033
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1034
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1035

    
1036
				if (isset($tunnel['p1']['pre-shared-key']))
1037
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1038
				if (isset($tunnel['p1']['cert']))
1039
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1040
				if (isset($tunnel['p1']['peercert']))
1041
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1042
				if (isset($tunnel['p1']['private-key']))
1043
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1044

    
1045
				$ph1ent['nat_traversal'] = "on";
1046
				$ph1ent['dpd_enable'] = 1;
1047
				$ph1ent['dpd_delay'] = 10;
1048
				$ph1ent['dpd_maxfail'] = 5;
1049

    
1050
				$a_phase1[] = $ph1ent;
1051
			}
1052

    
1053
			/* build new phase2 entry */
1054

    
1055
			$ph2ent = array();
1056

    
1057
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1058

    
1059
			if (isset($tunnel['disabled']))
1060
				$ph1ent['disabled'] = $tunnel['disabled'];
1061

    
1062
			$ph2ent['descr'] = sprintf(gettext("phase2 for %s"), $tunnel['descr']);
1063

    
1064
			$type = "lan";
1065
			if ($tunnel['local-subnet']['network'])
1066
				$type = $tunnel['local-subnet']['network'];
1067
			if ($tunnel['local-subnet']['address']) {
1068
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1069
				if (is_null($netbits))
1070
					$type = "address";
1071
				else
1072
					$type = "network";
1073
			}
1074

    
1075
			switch ($type) {
1076
				case "address":
1077
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1078
				break;
1079
				case "network":
1080
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1081
				break;
1082
				default:
1083
				$ph2ent['localid'] = array('type' => $type);
1084
				break;
1085
			}
1086

    
1087
			list($address,$netbits) = explode("/",$tunnel['remote-subnet']);
1088
			$ph2ent['remoteid'] = array('type' => 'network','address' => $address,'netbits' => $netbits);
1089

    
1090
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1091

    
1092
			$aes_count = 0;
1093
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1094
				$aes_found = false;
1095
				switch ($tunalg) {
1096
					case "des":
1097
					$ph2alg = array( 'name' => 'des' );
1098
					break;
1099
					case "3des":
1100
					$ph2alg = array( 'name' => '3des' );
1101
					break;
1102
					case "blowfish":
1103
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1104
					break;
1105
					case "cast128":
1106
					$ph2alg = array( 'name' => 'cast128' );
1107
					break;
1108
					case "rijndael":
1109
					case "rijndael 256":
1110
					case "aes 256":
1111
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1112
					$aes_found = true;
1113
					$aes_count++;
1114
					break;
1115
				}
1116

    
1117
				if( !$aes_found || ($aes_count < 2))
1118
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1119
			}
1120

    
1121
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1122
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1123
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1124

    
1125
			if (isset($tunnel['pinghost']['pinghost']))
1126
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1127

    
1128
			$a_phase2[] = $ph2ent;
1129
		}
1130

    
1131
		unset($config['ipsec']['tunnel']);
1132
		$config['ipsec']['phase1'] = $a_phase1;
1133
		$config['ipsec']['phase2'] = $a_phase2;
1134
	}
1135

    
1136
	/* Upgrade Mobile IPsec */
1137
	if (isset($config['ipsec']['mobileclients'])
1138
		&& is_array($config['ipsec']['mobileclients'])
1139
		&& is_array($config['ipsec']['mobileclients']['p1'])
1140
		&& is_array($config['ipsec']['mobileclients']['p2'])) {
1141

    
1142
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1143
			$config['ipsec']['client']['enable'] = true;
1144
			$config['ipsec']['client']['user_source'] = 'system';
1145
			$config['ipsec']['client']['group_source'] = 'system';
1146
		}
1147

    
1148
		$mobilecfg = $config['ipsec']['mobileclients'];
1149

    
1150
		$ph1ent = array();
1151
		$ph1ent['ikeid'] = ++$ikeid;
1152

    
1153
		if (!isset($mobilecfg['enable']))
1154
			$ph1ent['disabled'] = true;
1155

    
1156
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1157
		$ph1ent['interface'] = 'wan';
1158
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1159
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1160

    
1161
		if (isset($mobilecfg['p1']['myident']['myaddress']))
1162
			$ph1ent['myid_type'] = "myaddress";
1163
		if (isset($mobilecfg['p1']['myident']['address'])) {
1164
			$ph1ent['myid_type'] = "address";
1165
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1166
		}
1167
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1168
			$ph1ent['myid_type'] = "fqdn";
1169
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1170
		}
1171
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1172
			$ph1ent['myid_type'] = "user_fqdn";
1173
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1174
		}
1175
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1176
			$ph1ent['myid_type'] = "asn1dn";
1177
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1178
		}
1179
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1180
			$ph1ent['myid_type'] = "dyn_dns";
1181
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1182
		}
1183
		$ph1ent['peerid_type'] = "fqdn";
1184
		$ph1ent['peerid_data'] = "";
1185

    
1186
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1187
			case "des":
1188
			$ph1alg = array( 'name' => 'des' );
1189
			break;
1190
			case "3des":
1191
			$ph1alg = array( 'name' => '3des' );
1192
			break;
1193
			case "blowfish":
1194
			$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1195
			break;
1196
			case "cast128":
1197
			$ph1alg = array( 'name' => 'cast128' );
1198
			break;
1199
			case "rijndael":
1200
			$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1201
			break;
1202
			case "rijndael 256":
1203
			case "aes 256":
1204
			$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1205
			break;
1206
		}
1207

    
1208
		$ph1ent['encryption-algorithm'] = $ph1alg;
1209
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1210
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1211
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1212
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1213

    
1214
		if (isset($mobilecfg['p1']['cert']))
1215
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1216
		if (isset($mobilecfg['p1']['peercert']))
1217
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1218
		if (isset($mobilecfg['p1']['private-key']))
1219
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1220

    
1221
		$ph1ent['nat_traversal'] = "on";
1222
		$ph1ent['dpd_enable'] = 1;
1223
		$ph1ent['dpd_delay'] = 10;
1224
		$ph1ent['dpd_maxfail'] = 5;
1225
		$ph1ent['mobile'] = true;
1226

    
1227
		$ph2ent = array();
1228
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1229
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1230
		$ph2ent['localid'] = array('type' => 'none');
1231
		$ph2ent['remoteid'] = array('type' => 'mobile');
1232
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1233

    
1234
		$aes_count = 0;
1235
		foreach( $mobilecfg['p2']['encryption-algorithm-option'] as $tunalg ) {
1236
			$aes_found = false;
1237
			switch ($tunalg) {
1238
				case "des":
1239
				$ph2alg = array( 'name' => 'des' );
1240
				break;
1241
				case "3des":
1242
				$ph2alg = array( 'name' => '3des' );
1243
				break;
1244
				case "blowfish":
1245
				$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1246
				break;
1247
				case "cast128":
1248
				$ph2alg = array( 'name' => 'cast128' );
1249
				break;
1250
				case "rijndael":
1251
				case "rijndael 256":
1252
				case "aes 256":
1253
				$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1254
				$aes_found = true;
1255
				$aes_count++;
1256
				break;
1257
			}
1258

    
1259
			if( !$aes_found || ($aes_count < 2))
1260
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1261
		}
1262
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1263
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1264
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1265
		$ph2ent['mobile'] = true;
1266

    
1267
		$config['ipsec']['phase1'][] = $ph1ent;
1268
		$config['ipsec']['phase2'][] = $ph2ent;
1269
		unset($config['ipsec']['mobileclients']);
1270
	}
1271
}
1272

    
1273

    
1274
function upgrade_047_to_048() {
1275
	global $config;
1276
	if (!empty($config['dyndns'])) {
1277
		$config['dyndnses'] = array();
1278
		$config['dyndnses']['dyndns'] = array();
1279
		if(isset($config['dyndns'][0]['host'])) {
1280
			$tempdyn = array();
1281
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1282
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1283
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1284
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1285
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1286
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1287
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];		
1288
			$tempdyn['interface'] = "wan";
1289
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1290
			$config['dyndnses']['dyndns'][] = $tempdyn;
1291
		}
1292
		unset($config['dyndns']);
1293
	}		
1294
	if (!empty($config['dnsupdate'])) {
1295
		$pconfig = $config['dnsupdate'][0];
1296
		if (!$pconfig['ttl'])
1297
			$pconfig['ttl'] = 60;
1298
		if (!$pconfig['keytype'])
1299
			$pconfig['keytype'] = "zone";
1300
		$pconfig['interface'] = "wan";
1301
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1302
		unset($config['dnsupdate']);
1303
	}
1304

    
1305
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1306
		$pconfig = array();
1307
		$pconfig['username'] = $config['pppoe'][0]['username'];
1308
		$pconfig['password'] = $config['pppoe'][0]['password'];
1309
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1310
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1311
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1312
		unset($config['pppoe']);
1313
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1314
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1315
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1316
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1317
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1318
	}
1319
	if (is_array($config['pptp'])) {
1320
		$pconfig = array();
1321
		$pconfig['username'] = $config['pptp']['username'];
1322
		$pconfig['password'] = $config['pptp']['password'];
1323
		$pconfig['provider'] = $config['pptp']['provider'];
1324
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1325
		$pconfig['timeout'] = $config['pptp']['timeout'];
1326
		unset($config['pptp']);
1327
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1328
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1329
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1330
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1331
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1332
	}
1333
}
1334

    
1335

    
1336
function upgrade_048_to_049() {
1337
	global $config;
1338
	/* setup new all users group */
1339
	$all = array();
1340
	$all['name'] = "all";
1341
	$all['description'] = gettext("All Users");
1342
	$all['scope'] = "system";
1343
	$all['gid'] = 1998;
1344
	$all['member'] = array();
1345

    
1346
	if (!is_array($config['system']['user']))
1347
		$config['system']['user'] = array();
1348
	if (!is_array($config['system']['group']))
1349
		$config['system']['group'] = array();
1350

    
1351
	/* work around broken uid assignments */
1352
	$config['system']['nextuid'] = 2000;
1353
	foreach ($config['system']['user'] as & $user) {
1354
		if (isset($user['uid']) && !$user['uid'])
1355
			continue;
1356
		$user['uid'] = $config['system']['nextuid']++;
1357
	}
1358

    
1359
	/* work around broken gid assignments */
1360
	$config['system']['nextgid'] = 2000;
1361
	foreach ($config['system']['group'] as & $group) {
1362
		if ($group['name'] == $g['admin_group'])
1363
			$group['gid'] = 1999;
1364
		else
1365
			$group['gid'] = $config['system']['nextgid']++;
1366
	}
1367

    
1368
	/* build group membership information */
1369
	foreach ($config['system']['group'] as & $group) {
1370
		$group['member'] = array();
1371
		foreach ($config['system']['user'] as & $user) {
1372
			$groupnames = explode(",", $user['groupname']);
1373
			if (in_array($group['name'],$groupnames))
1374
				$group['member'][] = $user['uid'];
1375
		}
1376
	}
1377

    
1378
	/* reset user group information */
1379
	foreach ($config['system']['user'] as & $user) {
1380
		unset($user['groupname']);
1381
		$all['member'][] = $user['uid'];
1382
	}
1383

    
1384
	/* reset group scope information */
1385
	foreach ($config['system']['group'] as & $group)
1386
		if ($group['name'] != $g['admin_group'])
1387
		$group['scope'] = "user";
1388

    
1389
	/* insert new all group */
1390
	$groups = Array();
1391
	$groups[] = $all;
1392
	$groups = array_merge($config['system']['group'],$groups);
1393
	$config['system']['group'] = $groups;
1394
}
1395

    
1396

    
1397
function upgrade_049_to_050() {
1398
	global $config;
1399

    
1400
	if (!is_array($config['system']['user']))
1401
		$config['system']['user'] = array();
1402
	/* update user privileges */
1403
	foreach ($config['system']['user'] as & $user) {
1404
		$privs = array();
1405
		if (!is_array($user['priv'])) {
1406
			unset($user['priv']);
1407
			continue;
1408
		}
1409
		foreach ($user['priv'] as $priv) {
1410
			switch($priv['id']) {
1411
				case "hasshell":
1412
				$privs[] = "user-shell-access";
1413
				break;
1414
				case "copyfiles":
1415
				$privs[] = "user-copy-files";
1416
				break;
1417
			}
1418
		}
1419
		$user['priv'] = $privs;
1420
	}
1421

    
1422
	/* update group privileges */
1423
	foreach ($config['system']['group'] as & $group) {
1424
		$privs = array();
1425
		if (!is_array($group['pages'])) {
1426
			unset($group['pages']);
1427
			continue;
1428
		}
1429
		foreach ($group['pages'] as $page) {
1430
			$priv = map_page_privname($page);
1431
			if ($priv)
1432
				$privs[] = $priv;
1433
		}
1434
		unset($group['pages']);
1435
		$group['priv'] = $privs;
1436
	}
1437

    
1438
	/* sync all local account information */
1439
	local_sync_accounts();
1440
}
1441

    
1442

    
1443
function upgrade_050_to_051() {
1444
	global $config;
1445
	$pconfig = array();
1446
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1447
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1448
	$pconfig['value'] = "1";
1449
	$config['sysctl']['item'][] = $pconfig;
1450
	$pconfig = array();
1451
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1452
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1453
	$pconfig['value'] = "0";
1454
	$config['sysctl']['item'][] = $pconfig;
1455

    
1456
	unset($config['bridge']);
1457

    
1458
	$convert_bridges = false;
1459
	foreach($config['interfaces'] as $intf) {
1460
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1461
			$config['bridges'] = array();
1462
			$config['bridges']['bridged'] = array();
1463
			$convert_bridges = true;
1464
			break;
1465
		}
1466
	}
1467
	if ($convert_bridges == true) {
1468
		$i = 0;
1469
		foreach ($config['interfaces'] as $ifr => &$intf) {
1470
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1471
				$nbridge = array();
1472
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1473
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1474
				$nbridge['bridgeif'] = "bridge{$i}";
1475
				$config['bridges']['bridged'][] = $nbridge;
1476
				unset($intf['bridge']);
1477
				$i++;
1478
			}
1479
		}
1480
	}
1481
}
1482

    
1483

    
1484
function upgrade_051_to_052() {
1485
	global $config;
1486
	$config['openvpn'] = array();
1487
	if (!is_array($config['ca']))
1488
		$config['ca'] = array();
1489
	if (!is_array($config['cert']))
1490
		$config['cert'] = array();
1491

    
1492
	$vpnid = 1;
1493

    
1494
	/* openvpn server configurations */
1495
	if (is_array($config['installedpackages']['openvpnserver'])) {
1496
		$config['openvpn']['openvpn-server'] = array();
1497

    
1498
		$index = 1;
1499
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1500

    
1501
			if (!is_array($server))
1502
				continue;
1503

    
1504
			if ($server['auth_method'] == "pki") {
1505

    
1506
				/* create ca entry */
1507
				$ca = array();
1508
				$ca['refid'] = uniqid();
1509
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1510
				$ca['crt'] = $server['ca_cert'];
1511
				$config['ca'][] = $ca;
1512

    
1513
				/* create ca reference */
1514
				unset($server['ca_cert']);
1515
				$server['caref'] = $ca['refid'];
1516

    
1517
				/* create a crl entry if needed */
1518
				if (!empty($server['crl'][0])) {
1519
					$crl = array();
1520
					$crl['refid'] = uniqid();
1521
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1522
					$crl['caref'] = $ca['refid'];
1523
					$crl['text'] = $server['crl'][0];
1524
					if(!is_array($config['crl']))
1525
						$config['crl'] = array();
1526
					$config['crl'][] = $crl;
1527
					$server['crlref'] = $crl['refid'];
1528
				}
1529
				unset($server['crl']);
1530

    
1531
				/* create cert entry */
1532
				$cert = array();
1533
				$cert['refid'] = uniqid();
1534
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1535
				$cert['crt'] = $server['server_cert'];
1536
				$cert['prv'] = $server['server_key'];
1537
				$config['cert'][] = $cert;
1538

    
1539
				/* create cert reference */
1540
				unset($server['server_cert']);
1541
				unset($server['server_key']);
1542
				$server['certref'] = $cert['refid'];
1543

    
1544
				$index++;
1545
			}
1546

    
1547
			/* determine operational mode */
1548
			if ($server['auth_method'] == 'pki') {
1549
				if($server['nopool']) {
1550
					$server['mode'] = "p2p_tls";
1551
				} else {
1552
					$server['mode'] = "server_tls";
1553
				}
1554
			} else {
1555
				$server['mode'] = "p2p_shared_key";
1556
			}
1557
			unset($server['auth_method']);
1558

    
1559
			/* modify configuration values */
1560
			$server['dh_length'] = 1024;
1561
			unset($server['dh_params']);
1562
			if (!$server['interface'])
1563
				$server['interface'] = 'any';
1564
			$server['tunnel_network'] = $server['addresspool'];
1565
			unset($server['addresspool']);
1566
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1567
				$server['compression'] = "on";
1568
				unset($server['use_lzo']);
1569
			}
1570
			if ($server['nopool'])
1571
				$server['pool_enable'] = false;
1572
			else
1573
				$server['pool_enable'] = "yes";
1574
			unset($server['nopool']);
1575
			$server['dns_domain'] = $server['dhcp_domainname'];
1576
			unset($server['dhcp_domainname']);
1577

    
1578
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1579
			$d=1;
1580
			foreach ($tmparr as $tmpa) {
1581
				$server["dns_server{$d}"] = $tmpa;
1582
				$d++;
1583
			}
1584
			unset($server['dhcp_dns']);
1585

    
1586
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1587
			$d=1;
1588
			foreach ($tmparr as $tmpa) {
1589
				$server["ntp_server{$d}"] = $tmpa;
1590
				$d++;
1591
			}
1592
			unset($server['dhcp_ntp']);
1593

    
1594
			if ($server['dhcp_nbtdisable'])
1595
				$server['netbios_enable'] = false;
1596
			else
1597
				$server['netbios_enable'] = "yes";
1598
			unset($server['dhcp_nbtdisable']);
1599
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1600
			unset($server['dhcp_nbttype']);
1601
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1602
			unset($server['dhcp_nbtscope']);
1603

    
1604
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1605
			$d=1;
1606
			foreach ($tmparr as $tmpa) {
1607
				$server["nbdd_server{$d}"] = $tmpa;
1608
				$d++;
1609
			}
1610
			unset($server['dhcp_nbdd']);
1611

    
1612
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1613
			$d=1;
1614
			foreach ($tmparr as $tmpa) {
1615
				$server["wins_server{$d}"] = $tmpa;
1616
				$d++;
1617
			}
1618
			unset($server['dhcp_wins']);
1619

    
1620
			if (!empty($server['disable']))
1621
				$server['disable'] = true;
1622
			else
1623
				unset($server['disable']);
1624

    
1625
			/* allocate vpnid */
1626
			$server['vpnid'] = $vpnid++;
1627

    
1628
			if (!empty($server['custom_options'])) {
1629
				$cstmopts = array();
1630
				$tmpcstmopts = explode(";", $server['custom_options']);
1631
				$assigned_if = "";
1632
				$tmpstr = "";
1633
				foreach ($tmpcstmopts as $tmpcstmopt) {
1634
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1635
					if (substr($tmpstr,0 ,6) == "devtun") {
1636
						$assigned_if = substr($tmpstr, 3);
1637
						continue;
1638
					} else if (substr($tmpstr, 0, 5) == "local") {
1639
						$localip = substr($tmpstr, 5);
1640
						$server['ipaddr'] = str_replace("\n", "", $localip);
1641
					} else
1642
						$cstmopts[] = $tmpcstmopt;
1643
				}
1644
				$server['custom_options'] = implode(";", $cstmopts);
1645
				if (!empty($assigned_if)) {
1646
					foreach ($config['interfaces'] as $iface => $cfgif) {
1647
						if ($cfgif['if'] == $assigned_if) {
1648
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1649
							break;
1650
						}
1651
					}
1652
				}
1653
			}
1654

    
1655
			$config['openvpn']['openvpn-server'][] = $server;
1656
		}
1657
		unset($config['installedpackages']['openvpnserver']);
1658
	}
1659

    
1660
	/* openvpn client configurations */
1661
	if (is_array($config['installedpackages']['openvpnclient'])) {
1662
		$config['openvpn']['openvpn-client'] = array();
1663

    
1664
		$index = 1;
1665
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1666

    
1667
			if (!is_array($client))
1668
				continue;
1669

    
1670
			if ($client['auth_method'] == "pki") {
1671

    
1672
				/* create ca entry */
1673
				$ca = array();
1674
				$ca['refid'] = uniqid();
1675
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1676
				$ca['crt'] = $client['ca_cert'];
1677
				$ca['crl'] = $client['crl'];
1678
				$config['ca'][] = $ca;
1679

    
1680
				/* create ca reference */
1681
				unset($client['ca_cert']);
1682
				unset($client['crl']);
1683
				$client['caref'] = $ca['refid'];
1684

    
1685
				/* create cert entry */
1686
				$cert = array();
1687
				$cert['refid'] = uniqid();
1688
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1689
				$cert['crt'] = $client['client_cert'];
1690
				$cert['prv'] = $client['client_key'];
1691
				$config['cert'][] = $cert;
1692

    
1693
				/* create cert reference */
1694
				unset($client['client_cert']);
1695
				unset($client['client_key']);
1696
				$client['certref'] = $cert['refid'];
1697

    
1698
				$index++;
1699
			}
1700

    
1701
			/* determine operational mode */
1702
			if ($client['auth_method'] == 'pki')
1703
				$client['mode'] = "p2p_tls";
1704
			else
1705
				$client['mode'] = "p2p_shared_key";
1706
			unset($client['auth_method']);
1707

    
1708
			/* modify configuration values */
1709
			if (!$client['interface'])
1710
				$client['interface'] = 'wan';
1711
			$client['tunnel_network'] = $client['interface_ip'];
1712
			unset($client['interface_ip']);
1713
			$client['server_addr'] = $client['serveraddr'];
1714
			unset($client['serveraddr']);
1715
			$client['server_port'] = $client['serverport'];
1716
			unset($client['serverport']);
1717
			$client['proxy_addr'] = $client['poxy_hostname'];
1718
			unset($client['proxy_addr']);
1719
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1720
				$client['compression'] = "on";
1721
				unset($client['use_lzo']);
1722
			}
1723
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1724
			unset($client['infiniteresolvretry']);
1725

    
1726
			/* allocate vpnid */
1727
			$client['vpnid'] = $vpnid++;
1728

    
1729
			if (!empty($client['custom_options'])) {
1730
				$cstmopts = array();
1731
				$tmpcstmopts = explode(";", $client['custom_options']);
1732
				$assigned_if = "";
1733
				$tmpstr = "";
1734
				foreach ($tmpcstmopts as $tmpcstmopt) {
1735
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1736
					if (substr($tmpstr,0 ,6) == "devtun") {
1737
						$assigned_if = substr($tmpstr, 3);
1738
						continue;
1739
					} else if (substr($tmpstr, 0, 5) == "local") {
1740
                                                $localip = substr($tmpstr, 5);
1741
                                                $client['ipaddr'] = str_replace("\n", "", $localip);
1742
					} else
1743
						$cstmopts[] = $tmpcstmopt;
1744
				}
1745
				$client['custom_options'] = implode(";", $cstmopts);
1746
				if (!empty($assigned_if)) {
1747
					foreach ($config['interfaces'] as $iface => $cfgif) {
1748
						if ($cfgif['if'] == $assigned_if) {
1749
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1750
							break;
1751
						}
1752
					}
1753
				}
1754
			}
1755

    
1756
			if (!empty($client['disable']))
1757
				$client['disable'] = true;
1758
			else
1759
				unset($client['disable']);
1760

    
1761
			$config['openvpn']['openvpn-client'][] = $client;
1762
		}
1763

    
1764
		unset($config['installedpackages']['openvpnclient']);
1765
	}
1766

    
1767
	/* openvpn client specific configurations */
1768
	if (is_array($config['installedpackages']['openvpncsc'])) {
1769
		$config['openvpn']['openvpn-csc'] = array();
1770

    
1771
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1772

    
1773
			if (!is_array($csc))
1774
				continue;
1775

    
1776
			/* modify configuration values */
1777
			$csc['common_name'] = $csc['commonname'];
1778
			unset($csc['commonname']);
1779
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1780
			unset($csc['ifconfig_push']);
1781
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1782
			unset($csc['dhcp_domainname']);
1783

    
1784
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1785
			$d=1;
1786
			foreach ($tmparr as $tmpa) {
1787
				$csc["dns_server{$d}"] = $tmpa;
1788
				$d++;
1789
			}
1790
			unset($csc['dhcp_dns']);
1791

    
1792
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1793
			$d=1;
1794
			foreach ($tmparr as $tmpa) {
1795
				$csc["ntp_server{$d}"] = $tmpa;
1796
				$d++;
1797
			}
1798
			unset($csc['dhcp_ntp']);
1799

    
1800
			if ($csc['dhcp_nbtdisable'])
1801
				$csc['netbios_enable'] = false;
1802
			else
1803
				$csc['netbios_enable'] = "yes";
1804
			unset($csc['dhcp_nbtdisable']);
1805
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1806
			unset($csc['dhcp_nbttype']);
1807
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1808
			unset($csc['dhcp_nbtscope']);
1809

    
1810
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1811
			$d=1;
1812
			foreach ($tmparr as $tmpa) {
1813
				$csc["nbdd_server{$d}"] = $tmpa;
1814
				$d++;
1815
			}
1816
			unset($csc['dhcp_nbdd']);
1817

    
1818
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1819
			$d=1;
1820
			foreach ($tmparr as $tmpa) {
1821
				$csc["wins_server{$d}"] = $tmpa;
1822
				$d++;
1823
			}
1824
			unset($csc['dhcp_wins']);
1825

    
1826
			if (!empty($csc['disable']))
1827
				$csc['disable'] = true;
1828
			else
1829
				unset($csc['disable']);
1830

    
1831
			$config['openvpn']['openvpn-csc'][] = $csc;
1832
		}
1833

    
1834
		unset($config['installedpackages']['openvpncsc']);
1835
	}
1836

    
1837
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1838
		count($config['openvpn']['openvpn-client']) > 0) {
1839
		$ovpnrule = array();
1840
                $ovpnrule['type'] = "pass";
1841
                $ovpnrule['interface'] = "openvpn";
1842
                $ovpnrule['statetype'] = "keep state";
1843
                $ovpnrule['source'] = array();
1844
                $ovpnrule['destination'] = array();
1845
                $ovpnrule['source']['any'] = true;
1846
                $ovpnrule['destination']['any'] = true;
1847
                $ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1848
		$config['filter']['rule'][] = $ovpnrule;
1849
	}
1850

    
1851
	/*
1852
		* FIXME: hack to keep things working with no installedpackages
1853
		* or carp array in the configuration data.
1854
		*/
1855
	if (!is_array($config['installedpackages']))
1856
		$config['installedpackages'] = array();
1857
	if (!is_array($config['installedpackages']['carp']))
1858
		$config['installedpackages']['carp'] = array();
1859

    
1860
}
1861

    
1862

    
1863
function upgrade_052_to_053() {
1864
	global $config;
1865
	if (!is_array($config['ca']))
1866
		$config['ca'] = array();
1867
	if (!is_array($config['cert']))
1868
		$config['cert'] = array();
1869

    
1870
	/* migrate advanced admin page webui ssl to certifcate mngr */
1871
	if ($config['system']['webgui']['certificate'] &&
1872
	$config['system']['webgui']['private-key']) {
1873

    
1874
		/* create cert entry */
1875
		$cert = array();
1876
		$cert['refid'] = uniqid();
1877
		$cert['descr'] = "webConfigurator SSL Certificate";
1878
		$cert['crt'] = $config['system']['webgui']['certificate'];
1879
		$cert['prv'] = $config['system']['webgui']['private-key'];
1880
		$config['cert'][] = $cert;
1881

    
1882
		/* create cert reference */
1883
		unset($config['system']['webgui']['certificate']);
1884
		unset($config['system']['webgui']['private-key']);
1885
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1886
	}
1887

    
1888
	/* migrate advanced admin page ssh keys to user manager */
1889
	if ($config['system']['ssh']['authorizedkeys']) {
1890
		$admin_user =& getUserEntryByUID(0);
1891
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1892
		unset($config['system']['ssh']['authorizedkeys']);
1893
	}
1894
}
1895

    
1896

    
1897
function upgrade_053_to_054() {
1898
	global $config;
1899
	if(is_array($config['load_balancer']['lbpool'])) {
1900
		$lbpool_arr = $config['load_balancer']['lbpool'];
1901
		$lbpool_srv_arr = array();
1902
		$gateway_group_arr = array();
1903
		$gateways = return_gateways_array();
1904
		$group_name_changes = array();
1905
		if (! is_array($config['gateways']['gateway_item']))
1906
			$config['gateways']['gateway_item'] = array();
1907

    
1908
		$a_gateways =& $config['gateways']['gateway_item'];
1909
		foreach($lbpool_arr as $lbpool) {
1910
			if($lbpool['type'] == "gateway") {
1911
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
1912
				$group_name = preg_replace("/[^A-Za-z0-9]/", "", $lbpool['name'] );
1913
				// If we made and changes, check for collisions and note the change.
1914
				if ($group_name != $lbpool['name']) {
1915
					// Make sure the name isn't already in use.
1916
					foreach ($gateway_group_arr as $gwg) {
1917
						// If the name is in use, add some random bits to avoid collision.
1918
						if ($gwg['name'] == $group_name)
1919
							$group_name .= uniqid();
1920
					}
1921
					$group_name_changes[$lbpool['name']] = $group_name;
1922
				}
1923
				$gateway_group['name'] = $group_name;
1924
				$gateway_group['descr'] = $lbpool['descr'];
1925
				$gateway_group['trigger'] = "down";
1926
				$gateway_group['item'] = array();
1927
				$i = 0;
1928
				foreach($lbpool['servers'] as $member) {
1929
					$split = explode("|", $member);
1930
					$interface = $split[0];
1931
					$monitor = $split[1];
1932
					/* on static upgraded configuration we automatically prepend GW_ */
1933
					$static_name = "GW_" . strtoupper($interface);
1934
					if(is_ipaddr($monitor))
1935
						foreach ($a_gateways as & $gw)
1936
							if ($gw['name'] == $static_name)
1937
								$gw['monitor'] = $monitor;
1938

    
1939
					/* on failover increment tier. Else always assign 1 */
1940
					if($lbpool['behaviour'] == "failover") {
1941
						$i++;
1942
					} else {
1943
						$i = 1;
1944
					}
1945
					$gateway_group['item'][] = "$static_name|$i";
1946
				}
1947
				$gateway_group_arr[] = $gateway_group;
1948
			} else {
1949
				$lbpool_srv_arr[] = $lbpool;
1950
			}
1951
		}
1952
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1953
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1954
	}
1955
	// Unset lbpool if we no longer have any server pools
1956
	if (count($lbpool_srv_arr) == 0) {
1957
		if(empty($config['load_balancer'])) {
1958
			unset($config['load_balancer']);
1959
		} else {
1960
			unset($config['load_balancer']['lbpool']);
1961
		}
1962
	} else {
1963
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1964
	}
1965
	// Only set the gateway group array if we converted any
1966
	if (count($gateway_group_arr) != 0) {
1967
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1968
		// Update any rules that had a gateway change, if any.
1969
		if (count($group_name_changes) > 0)
1970
			foreach ($config['filter']['rule'] as & $rule)
1971
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes))
1972
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
1973
	}
1974
}
1975

    
1976

    
1977
function upgrade_054_to_055() {
1978
	global $config;
1979
	global $g;
1980

    
1981
	/* RRD files changed for quality, traffic and packets graphs */
1982
	//ini_set("max_execution_time", "1800");
1983
	/* convert traffic RRD file */
1984
	global $parsedcfg, $listtags;
1985
	$listtags = array("ds", "v", "rra", "row");
1986

    
1987
	$rrddbpath = "/var/db/rrd/";
1988
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1989
	if ($g['platform'] != "pfSense") {
1990
		/* restore the databases, if we have one */
1991
		if (restore_rrd()) {
1992
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
1993
			exec("/bin/mv {$g['cf_conf_path']}/rrd.tgz {$g['cf_conf_path']}/backup");
1994
		}
1995
	}
1996

    
1997
	$rrdinterval = 60;
1998
	$valid = $rrdinterval * 2;
1999

    
2000
	/* Asume GigE for now */
2001
	$downstream = 125000000;
2002
	$upstream = 125000000;
2003

    
2004
	/* build a list of quality databases */
2005
	/* roundtrip has become delay */
2006
	function divide_delay($delayval) {
2007
		$delayval = floatval($delayval);
2008
		$delayval = ($delayval / 1000);
2009
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2010
		return $delayval;
2011
	}
2012
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2013
	$databases = array();
2014
	if (!file_exists($rrddbpath))
2015
		@mkdir($rrddbpath);
2016
	chdir($rrddbpath);
2017
	$databases = glob("*-quality.rrd");
2018
	rsort($databases);
2019
	foreach($databases as $database) {
2020
		$xmldump = "{$database}.old.xml";
2021
		$xmldumpnew = "{$database}.new.xml";
2022

    
2023
		if ($g['booting'])
2024
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2025
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2026

    
2027
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2028
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2029
		$rrdold = $rrdold['rrd'];
2030

    
2031
		$i = 0;
2032
		foreach($rrdold['rra'] as $rra) {
2033
			$l = 0;
2034
			foreach($rra['database']['row'] as $row) {
2035
				$vnew = divide_delay($row['v'][1]);
2036
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2037
				$l++;
2038
			}
2039
			$i++;
2040
		}
2041

    
2042
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw($rrdold, "rrd"));
2043
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2044

    
2045
		unset($rrdold);
2046
	}
2047
	/* let apinger recreate required files */
2048
	setup_gateways_monitor();
2049

    
2050
	/* build a list of traffic and packets databases */
2051
	$databases = array();
2052
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
2053
	rsort($databases);
2054
	foreach($databases as $database) {
2055
		$databasetmp = "{$database}.tmp";
2056
		$xmldump = "{$database}.old.xml";
2057
		$xmldumptmp = "{$database}.tmp.xml";
2058
		$xmldumpnew = "{$database}.new.xml";
2059

    
2060
		if ($g['booting'])
2061
			echo "Migrate RRD database {$database} to new format \n";
2062
		/* rename DS source */
2063
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2064
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2065

    
2066
		/* dump contents to xml and move database out of the way */
2067
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2068

    
2069
		/* create new rrd database file */
2070
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2071
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2072
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2073
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2074
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2075
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2076
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2077
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2078
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2079

    
2080
		create_new_rrd("$rrdcreate");
2081
		/* create temporary xml from new RRD */
2082
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2083

    
2084
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2085
		$rrdold = $rrdold['rrd'];
2086

    
2087
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2088
		$rrdnew = $rrdnew['rrd'];
2089

    
2090
		/* remove any MAX RRA's. Not needed for traffic. */
2091
		$i = 0;
2092
		foreach ($rrdold['rra'] as $rra) {
2093
			if(trim($rra['cf']) == "MAX") {
2094
				unset($rrdold['rra'][$i]);
2095
			}
2096
			$i++;
2097
		}
2098

    
2099
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2100
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2101
		/* we now have the rrd with the new fields, adjust the size now. */
2102
		/* RRA 2 is 60 minutes, RRA 3 is 720 minutes */
2103
		mwexec("/bin/sync");
2104
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 2 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2105
		mwexec("/bin/sync");
2106
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2107
		unset($rrdxmlarray);
2108
	}
2109
	enable_rrd_graphing();
2110
	/* Let's save the RRD graphs after we run enable RRD graphing */
2111
	/* The function will restore the rrd.tgz so we will save it after */
2112
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2113
	if ($g['booting'])
2114
		echo "Updating configuration...";
2115
}
2116

    
2117

    
2118
function upgrade_055_to_056() {
2119
	global $config;
2120

    
2121
	if (!is_array($config['ca']))
2122
		$config['ca'] = array();
2123
	if (!is_array($config['cert']))
2124
		$config['cert'] = array();
2125

    
2126
	/* migrate ipsec ca's to cert manager */
2127
	if (is_array($config['ipsec']['cacert'])) {
2128
		foreach($config['ipsec']['cacert'] as & $cacert) {
2129
			$ca = array();
2130
			$ca['refid'] = uniqid();
2131
			if (is_array($cacert['cert']))
2132
				$ca['crt'] = $cacert['cert'][0];
2133
			else
2134
				$ca['crt'] = $cacert['cert'];
2135
			$ca['descr'] = $cacert['ident'];
2136
			$config['ca'][] = $ca;
2137
		}
2138
		unset($config['ipsec']['cacert']);
2139
	}
2140

    
2141
	/* migrate phase1 certificates to cert manager */
2142
	if (is_array($config['ipsec']['phase1'])) {
2143
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
2144
			$cert = array();
2145
			$cert['refid'] = uniqid();
2146
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2147
			if (is_array($ph1ent['cert']))
2148
				$cert['crt'] = $ph1ent['cert'][0];
2149
			else
2150
				$cert['crt'] = $ph1ent['cert'];
2151
			$cert['prv'] = $ph1ent['private-key'];
2152
			$config['cert'][] = $cert;
2153
			$ph1ent['certref'] = $cert['refid'];
2154
			if ($ph1ent['cert'])
2155
				unset($ph1ent['cert']);
2156
			if ($ph1ent['private-key'])
2157
				unset($ph1ent['private-key']);
2158
			if ($ph1ent['peercert'])
2159
				unset($ph1ent['peercert']);
2160
		}
2161
	}
2162
}
2163

    
2164

    
2165
function upgrade_056_to_057() {
2166
	global $config;
2167

    
2168
	if (!is_array($config['system']['user']))
2169
		$config['system']['user'] = array();
2170
	/* migrate captivate portal to user manager */
2171
	if (is_array($config['captiveportal']['user'])) {
2172
		foreach($config['captiveportal']['user'] as $user) {
2173
			// avoid user conflicts
2174
			$found = false;
2175
			foreach ($config['system']['user'] as $userent) {
2176
				if ($userent['name'] == $user['name']) {
2177
					$found = true;
2178
					break;
2179
				}
2180
			}
2181
			if ($found)
2182
				continue;
2183
			$user['scope'] = "user";
2184
			if (isset($user['expirationdate'])) {
2185
				$user['expires'] = $user['expirationdate'];
2186
				unset($user['expirationdate']);
2187
			}
2188
			if (isset($user['password'])) {
2189
				$user['md5-hash'] = $user['password'];
2190
				unset($user['password']);
2191
			}
2192
			$user['uid'] = $config['system']['nextuid']++;
2193
			$config['system']['user'][] = $user;
2194
		}
2195
		unset($config['captiveportal']['user']);
2196
	}
2197
}
2198

    
2199
function upgrade_057_to_058() {
2200
	global $config;
2201
	/* set all phase2 entries to tunnel mode */
2202
	if (is_array($config['ipsec']['phase2']))
2203
		foreach($config['ipsec']['phase2'] as & $ph2ent)
2204
			$ph2ent['mode'] = 'tunnel';
2205
}
2206

    
2207
function upgrade_058_to_059() {
2208
	global $config;
2209

    
2210
	if (is_array($config['schedules']['schedule'])) {
2211
		foreach ($config['schedules']['schedule'] as & $schedl)
2212
			$schedl['schedlabel'] = uniqid();
2213
	}
2214
}
2215

    
2216
function upgrade_059_to_060() {
2217
	global $config;
2218
	require_once("/etc/inc/certs.inc");
2219
	if (is_array($config['ca'])) {
2220
		/* Locate issuer for all CAs */
2221
		foreach ($config['ca'] as & $ca) {
2222
			$subject = cert_get_subject($ca['crt']);
2223
			$issuer = cert_get_issuer($ca['crt']);
2224
			if($issuer <> $subject) {
2225
				$issuer_crt =& lookup_ca_by_subject($issuer);
2226
				if($issuer_crt)
2227
					$ca['caref'] = $issuer_crt['refid'];
2228
			}
2229
		}
2230
		
2231
		/* Locate issuer for all certificates */
2232
		if (is_array($config['cert'])) {
2233
			foreach ($config['cert'] as & $cert) {
2234
				$subject = cert_get_subject($cert['crt']);
2235
				$issuer = cert_get_issuer($cert['crt']);
2236
				if($issuer <> $subject) {
2237
					$issuer_crt =& lookup_ca_by_subject($issuer);
2238
					if($issuer_crt)
2239
						$cert['caref'] = $issuer_crt['refid'];
2240
				}
2241
			}
2242
		}
2243
	}
2244
}
2245

    
2246
function upgrade_060_to_061() {
2247
	global $config;
2248

    
2249
	if (is_array($config['interfaces']['wan']))
2250
		$config['interfaces']['wan']['enable'] = true;
2251
	if (is_array($config['interfaces']['lan']))
2252
		$config['interfaces']['lan']['enable'] = true;
2253

    
2254
	/* On 1.2.3 the "mtu" field adjusted MSS.
2255
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2256
	   See redmine ticket #1886
2257
	*/
2258
	foreach ($config['interfaces'] as $ifr => &$intf) {
2259
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2260
			$intf['mss'] = $intf['mtu'];
2261
			unset($intf['mtu']);
2262
		}
2263
	}
2264
}
2265

    
2266
function upgrade_061_to_062() {
2267
	global $config;
2268

    
2269
	/* Convert NAT port forwarding rules */
2270
	if (is_array($config['nat']['rule'])) {
2271
		$a_nat = &$config['nat']['rule'];
2272

    
2273
		foreach ($a_nat as &$natent) {
2274
			$natent['disabled'] = false;
2275
			$natent['nordr']    = false;
2276

    
2277
			$natent['source'] = array(
2278
				"not"     => false,
2279
				"any"     => true,
2280
				"port"    => ""
2281
			);
2282

    
2283
			$natent['destination'] = array(
2284
				"not"     => false,
2285
				"address" => $natent['external-address'],
2286
				"port"    => $natent['external-port']
2287
			);
2288

    
2289
			if (empty($natent['destination']['address'])) {
2290
				unset($natent['destination']['address']);
2291
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2292
			} else if ($natent['destination']['address'] == 'any') {
2293
				unset($natent['destination']['address']);
2294
				$natent['destination']['any'] = true;
2295
			}
2296

    
2297
			unset($natent['external-address']);
2298
			unset($natent['external-port']);
2299
		}
2300

    
2301
		unset($natent);
2302
	}
2303
}
2304

    
2305
function upgrade_062_to_063() {
2306
	/* Upgrade legacy Themes to the new pfsense_ng */
2307
	global $config;
2308

    
2309
	switch($config['theme']) {
2310
		case "nervecenter":
2311
			$config['theme'] = "pfsense_ng";
2312
			break;
2313
	}
2314
	
2315
}
2316

    
2317
function upgrade_063_to_064() {
2318
	global $config;
2319
	$j=0;
2320
	$ifcfg = &$config['interfaces'];
2321
	
2322
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {	
2323
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2324
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2325
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2326
			$j++;
2327
			if (isset($ppp['port'])){
2328
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2329
				unset($config['ppps']['ppp'][$pppid]['port']);
2330
			}
2331
			if (!isset($ppp['type'])){
2332
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2333
			}
2334
			if (isset($ppp['defaultgw']))
2335
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2336
		}
2337
	}
2338
	
2339
	if (!is_array($config['ppps']['ppp']))
2340
		$config['ppps']['ppp'] = array();
2341
	$a_ppps = &$config['ppps']['ppp'];
2342

    
2343
	foreach ($ifcfg as $ifname => $ifinfo) {
2344
		$ppp = array();
2345
		// For pppoe conversion
2346
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2347
			if (isset($ifinfo['ptpid']))
2348
				continue;
2349
			$ppp['ptpid'] =  $j;
2350
			$ppp['type'] = $ifinfo['ipaddr'];
2351
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2352
			$ppp['ports'] = $ifinfo['if'];
2353
			if ($ifinfo['ipaddr'] == "pppoe"){
2354
				$ppp['username'] = $ifinfo['pppoe_username'];
2355
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2356
			}
2357
			if ($ifinfo['ipaddr'] == "pptp"){
2358
				$ppp['username'] = $ifinfo['pptp_username'];
2359
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2360
			}
2361
			
2362
			if (isset($ifinfo['provider']))
2363
				$ppp['provider'] = $ifinfo['provider'];
2364
			if (isset($ifinfo['ondemand']))
2365
				$ppp['ondemand'] = true;
2366
			if (isset($ifinfo['timeout']))
2367
				$ppp['idletimeout'] = $ifinfo['timeout'];
2368
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2369
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2370
				if (is_array($config['cron']['item'])) {
2371
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2372
						$item = $config['cron']['item'][$i];
2373
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2374
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2375
					}
2376
				}
2377
			}
2378
			if (isset($ifinfo['local']))
2379
				$ppp['localip'] = $ifinfo['local'];
2380
			if (isset($ifinfo['subnet']))
2381
				$ppp['subnet'] = $ifinfo['subnet'];
2382
			if (isset($ifinfo['remote']))
2383
				$ppp['gateway'] = $ifinfo['remote'];
2384

    
2385
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2386
			$j++;
2387
			
2388
			unset($ifcfg[$ifname]['pppoe_username']);
2389
			unset($ifcfg[$ifname]['pppoe_password']);
2390
			unset($ifcfg[$ifname]['provider']);
2391
			unset($ifcfg[$ifname]['ondemand']);
2392
			unset($ifcfg[$ifname]['timeout']);
2393
			unset($ifcfg[$ifname]['pppoe_reset']);
2394
			unset($ifcfg[$ifname]['pppoe_preset']);
2395
			unset($ifcfg[$ifname]['pppoe']);
2396
			unset($ifcfg[$ifname]['pptp_username']);
2397
			unset($ifcfg[$ifname]['pptp_password']);
2398
			unset($ifcfg[$ifname]['local']);
2399
			unset($ifcfg[$ifname]['subnet']);
2400
			unset($ifcfg[$ifname]['remote']);
2401
			
2402
			$a_ppps[] = $ppp;
2403
			
2404
		}
2405
	}
2406
}
2407

    
2408
function upgrade_064_to_065() {
2409
	/* Disable TSO and LRO in upgraded configs */
2410
	global $config;
2411
	$config['system']['disablesegmentationoffloading'] = true;
2412
	$config['system']['disablelargereceiveoffloading'] = true;
2413
}
2414

    
2415
function upgrade_065_to_066() {
2416
	global $config;
2417

    
2418
	$dhcrelaycfg =& $config['dhcrelay'];
2419

    
2420
        if (is_array($dhcrelaycfg)) {
2421
        	$dhcrelayifs = array();
2422
		$foundifs = false;
2423
        	/* DHCPRelay enabled on any interfaces? */
2424
                foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2425
                        if (isset($dhcrelayifconf['enable'])) {
2426
				$dhcrelayifs[] = $dhcrelayif;
2427
				unset($dhcrelaycfg['dhcrelayif']);
2428
				$foundifs = true;
2429
			}
2430
                }
2431
		if ($foundifs == true)
2432
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2433
        }
2434
}
2435

    
2436
function upgrade_066_to_067() {
2437
	global $config;
2438
	if (isset($config['system']['ca'])) {
2439
		$config['ca'] = $config['system']['ca'];
2440
	}
2441
	if (isset($config['system']['cert'])) {
2442
		$config['cert'] = $config['system']['cert'];
2443
	}
2444
}
2445

    
2446
function upgrade_067_to_068() {
2447
	global $config;
2448

    
2449
	if (!empty($config['pppoe'])) {
2450
		$config['pppoes'] = array();
2451
		$config['pppoes']['pppoe'] = array();
2452
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2453

    
2454
		if (is_array($config['pppoe']['user'])) {
2455
			$username = array(); 
2456
			foreach ($config['pppoe']['user'] as $user) {
2457
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2458
				if ($user['ip'])
2459
					$usr .= ":{$user['ip']}";
2460
				$username[] = $usr;
2461
			}
2462
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2463
		}
2464
		unset($config['pppoe']);
2465
	}
2466
}
2467

    
2468
function upgrade_068_to_069() {
2469
	global $config;
2470
	if (!is_array($config['system']['user']))
2471
		return;
2472
	foreach ($config['system']['user'] as & $user) {
2473
		if (!is_array($user['cert']))
2474
			continue;
2475
		$rids = array();
2476
		foreach ($user['cert'] as $id => $cert) {
2477
			if (!isset($cert['descr']))
2478
				continue;
2479
			$tcert = $cert;
2480
			// Make sure each cert gets a refid
2481
			if (!isset($tcert['refid']))
2482
				$tcert['refid'] = uniqid();
2483
			// Keep the cert references for this user
2484
			$rids[] = $tcert['refid'];
2485
			$config['cert'][] = $tcert;
2486
		}
2487
		// Replace user certs with cert references instead.
2488
		if (count($rids) > 0)
2489
			$user['cert'] = $rids;
2490
	}
2491
}
2492

    
2493
function upgrade_069_to_070() {
2494
	global $config;
2495

    
2496
	/* Convert NAT 1:1 rules */
2497
	if (is_array($config['nat']['onetoone'])) {
2498
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2499
			if ($natent['subnet'] == 32)
2500
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2501
			else
2502
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2503

    
2504
			$config['nat']['onetoone'][$nidx]['destination'] = array("any" => true);
2505

    
2506
			unset($config['nat']['onetoone'][$nidx]['internal']);
2507
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2508
		}
2509

    
2510
		unset($natent);
2511
	}
2512
}
2513

    
2514
function upgrade_070_to_071() {
2515
	global $config;
2516

    
2517
	if (is_array($config['cron']['item'])) {
2518
		foreach($config['cron']['item'] as $idx => $cronitem) {
2519
			if(stristr($cronitem['command'], "checkreload.sh")) {
2520
				unset($config['cron']['item'][$idx]);
2521
				break;
2522
			}
2523
		}
2524
	}
2525
}
2526

    
2527
function rename_field(& $section, $oldname, $newname) {
2528
	if (is_array($section)) {
2529
		foreach($section as & $item) {
2530
			if (is_array($item) && !empty($item[$oldname]))
2531
				$item[$newname] = $item[$oldname];
2532
			if (is_array($item) && isset($item[$oldname]))
2533
				unset($item[$oldname]);
2534
		}
2535
	}
2536
}
2537

    
2538
function upgrade_071_to_072() {
2539
	global $config;
2540
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2541
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2542
}
2543

    
2544
function upgrade_072_to_073() {
2545
	global $config;
2546
	if (!is_array($config['load_balancer']))
2547
		return;
2548
	if (is_array($config['load_balancer']['monitor_type']))
2549
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2550
	if (is_array($config['load_balancer']['lbpool']))
2551
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2552
	if (is_array($config['load_balancer']['lbaction']))
2553
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2554
	if (is_array($config['load_balancer']['lbprotocol']))
2555
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2556
	if (is_array($config['load_balancer']['virtual_server']))
2557
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2558
}
2559

    
2560
function upgrade_073_to_074() {
2561
	global $config;
2562
	rename_field($config['system']['user'], 'fullname', 'descr');
2563
}
2564

    
2565
function upgrade_074_to_075() {
2566
	global $config;
2567
	if (is_array($config['ca']))
2568
		rename_field($config['ca'], 'name', 'descr');
2569
	if (is_array($config['cert']))
2570
		rename_field($config['cert'], 'name', 'descr');
2571
	if (is_array($config['crl']))
2572
		rename_field($config['crl'], 'name', 'descr');
2573
}
2574

    
2575
function upgrade_075_to_076() {
2576
	global $config;
2577
	$cron_item = array();
2578
	$cron_item['minute'] = "30";
2579
	$cron_item['hour'] = "12";
2580
	$cron_item['mday'] = "*";
2581
	$cron_item['month'] = "*";
2582
	$cron_item['wday'] = "*";
2583
	$cron_item['who'] = "root";
2584
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2585
	$config['cron']['item'][] = $cron_item;
2586
}
2587

    
2588
function upgrade_076_to_077() {
2589
	global $config;
2590
	foreach($config['filter']['rule'] as & $rule) {
2591
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2592
		$rule['protocol'] = strtolower($rule['protocol']);
2593
	}
2594
}
2595

    
2596
function upgrade_077_to_078() {
2597
	global $config;
2598
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius'])
2599
		&& !is_array($config['pptpd']['radius']['server'])) {
2600
		$radarr = array();
2601
		$radsvr = array();
2602
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2603
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2604
		$radsvr['port'] = 1812;
2605
		$radsvr['acctport'] = 1813;
2606
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2607
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2608
		if ($radarr['accounting'])
2609
			$radarr['acct_update'] = $radsvr['ip'];
2610
		$radarr['server'] = $radsvr;
2611
		$config['pptpd']['radius'] = $radarr;
2612
	}
2613
	if (is_array($config['pptpd'])) {
2614
		$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
2615
	}
2616
}
2617
function upgrade_078_to_079() {
2618
	global $g;
2619
	/* Delete old and unused RRD file */
2620
	unlink_if_exists("{$g['vardb_path']}/rrd/captiveportal-totalusers.rrd");
2621
}
2622

    
2623
function upgrade_079_to_080() {
2624
	global $config;
2625

    
2626
	/* Upgrade config in 1.2.3 specifying a username other than admin for synching. */
2627
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2628
		is_array($config['installedpackages']['carpsettings']['config'])) {
2629
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2630
		unset($config['system']['username']);
2631
	}
2632
}
2633

    
2634
function upgrade_080_to_081() {
2635
	global $config;
2636
	global $g;
2637
	/* Welcome to the 2.1 migration path */
2638

    
2639
	/* tag all the existing gateways as being IPv4 */
2640
	$i = 0;
2641
	if(is_array($config['gateways']['gateway_item'])) {
2642
		foreach($config['gateways']['gateway_item'] as $gw) {
2643
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2644
			$i++;
2645
		}
2646
	}
2647

    
2648
	/* RRD files changed for quality, traffic and packets graphs */
2649
	/* convert traffic RRD file */
2650
	global $parsedcfg, $listtags;
2651
	$listtags = array("ds", "v", "rra", "row");
2652

    
2653
	$rrddbpath = "/var/db/rrd/";
2654
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2655

    
2656
	$rrdinterval = 60;
2657
	$valid = $rrdinterval * 2;
2658

    
2659
	/* Asume GigE for now */
2660
	$downstream = 125000000;
2661
	$upstream = 125000000;
2662

    
2663
	/* build a list of traffic and packets databases */
2664
	$databases = array();
2665
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
2666
	rsort($databases);
2667
	foreach($databases as $database) {
2668
		$databasetmp = "{$database}.tmp";
2669
		$xmldump = "{$database}.old.xml";
2670
		$xmldumptmp = "{$database}.tmp.xml";
2671
		$xmldumpnew = "{$database}.new.xml";
2672

    
2673
		if ($g['booting'])
2674
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2675

    
2676
		/* dump contents to xml and move database out of the way */
2677
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2678

    
2679
		/* create new rrd database file */
2680
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2681
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2682
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2683
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2684
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2685
		$rrdcreate .= "DS:inpass6:COUNTER:$valid:0:$downstream ";
2686
		$rrdcreate .= "DS:outpass6:COUNTER:$valid:0:$upstream ";
2687
		$rrdcreate .= "DS:inblock6:COUNTER:$valid:0:$downstream ";
2688
		$rrdcreate .= "DS:outblock6:COUNTER:$valid:0:$upstream ";
2689
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2690
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2691
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2692
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
2693

    
2694
		create_new_rrd("$rrdcreate");
2695
		/* create temporary xml from new RRD */
2696
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2697

    
2698
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2699
		$rrdold = $rrdold['rrd'];
2700

    
2701
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2702
		$rrdnew = $rrdnew['rrd'];
2703

    
2704
		/* remove any MAX RRA's. Not needed for traffic. */
2705
		$i = 0;
2706
		foreach ($rrdold['rra'] as $rra) {
2707
			if(trim($rra['cf']) == "MAX") {
2708
				unset($rrdold['rra'][$i]);
2709
			}
2710
			$i++;
2711
		}
2712

    
2713
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2714
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2715

    
2716
	}
2717
	enable_rrd_graphing();
2718
	if ($g['booting'])
2719
		echo "Updating configuration...";
2720
	foreach($config['filter']['rule'] as & $rule) {
2721
		if (isset($rule['protocol']) && !empty($rule['protocol']))
2722
			$rule['protocol'] = strtolower($rule['protocol']);
2723
	}
2724
}
2725

    
2726
function upgrade_081_to_082() {
2727
	global $config, $g;
2728
	/* enable the allow IPv6 toggle */
2729
	$config['system']['ipv6allow'] = true;
2730
}
2731

    
2732
function upgrade_082_to_083() {
2733
	global $config;
2734

    
2735
	/* Upgrade captiveportal config */
2736
	if (!empty($config['captiveportal'])) {
2737
		$tmpcp = $config['captiveportal'];
2738
		$config['captiveportal'] = array();
2739
		$config['captiveportal']['cpZone'] = array();
2740
		$config['captiveportal']['cpZone'] = $tmpcp;
2741
		$config['captiveportal']['cpZone']['zoneid'] = 8000;
2742
	}
2743
	if (!empty($config['voucher'])) {
2744
		$tmpcp = $config['voucher'];
2745
		$config['voucher'] = array();
2746
		$config['voucher']['cpZone'] = array();
2747
		$config['voucher']['cpZone'] = $tmpcp;
2748
	}
2749
}
2750

    
2751
function upgrade_083_to_084() {
2752
	global $config;
2753
	if (!isset($config['hasync'])) {
2754
		if (!empty($config['installedpackages']) &&
2755
		    !empty($config['installedpackages']['carpsettings']) &&
2756
		    !empty($config['installedpackages']['carpsettings']['config'])) {
2757
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
2758
			unset($config['installedpackages']['carpsettings']);
2759
		}
2760
		if (empty($config['installedpackages']['carpsettings'])) {
2761
			unset($config['installedpackages']['carpsettings']);
2762
		}
2763
		if (empty($config['installedpackages'])) {
2764
			unset($config['installedpackages']);
2765
		}
2766
	}
2767
}
2768

    
2769
function upgrade_084_to_085() {
2770
	global $config;
2771

    
2772
	$gateway_group_arr = array();
2773
	$gateways = return_gateways_array();
2774
	$oldnames = array();
2775
	/* setup translation array */
2776
	foreach($gateways as $name => $gw) {
2777
		if(isset($gw['dynamic'])){
2778
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
2779
			$oldnames[$oldname] = $name; 
2780
		} else {
2781
			$oldnames[$name] = $name;
2782
		}
2783
	}
2784

    
2785
	/* process the old array */
2786
	if(is_array($config['gateways']['gateway_group'])) {
2787
		$group_array_new = array();
2788
		foreach($config['gateways']['gateway_group'] as $name => $group) {
2789
			if(is_array($group['item'])) {
2790
				$newlist = array();
2791
				foreach($group['item'] as $entry) {
2792
					$elements = explode("|", $entry);
2793
					if($oldnames[$elements[0]] <> "") {
2794
						$newlist[] = "{$oldnames[$elements[0]]}|{$elements[1]}";
2795
					} else {
2796
						$newlist[] = "{$elements[0]}|{$elements[1]}";
2797
					}
2798
				}
2799
				$group['item'] = $newlist;
2800
				$group_array_new[$name] = $group;
2801
			}
2802
		}
2803
		$config['gateways']['gateway_group'] = $group_array_new;
2804
	}
2805
	/* rename old Quality RRD files in the process */
2806
	$rrddbpath = "/var/db/rrd";
2807
	foreach($oldnames as $old => $new) {
2808
		if(is_readable("{$rrddbpath}/{$old}-quality.rrd")) {
2809
			rename("{$rrddbpath}/{$old}-quality.rrd", "{$rrddbpath}/{$new}-quality.rrd");
2810
		}
2811
	}
2812
}
2813

    
2814
function upgrade_085_to_086() {
2815
	global $config, $g;
2816

    
2817
	/* XXX: Gross hacks in sight */
2818
	if (is_array($config['virtualip']['vip'])) {
2819
		$vipchg = array();
2820
		foreach ($config['virtualip']['vip'] as $vip) {
2821
			if ($vip['mode'] != "carp")
2822
				continue;
2823
			$vipchg[] = "s/\\([^_]\\)vip{$vip['vhid']}\\([^0-9]\\)/\\1{$vip['interface']}_vip{$vip['vhid']}\\2/g\n";
2824
		}
2825
		if (!empty($vipchg)) {
2826
			file_put_contents("{$g['tmp_path']}/vipreplace", $vipchg);
2827
			write_config();
2828
			mwexec("/usr/bin/sed -I \"\" -f {$g['tmp_path']}/vipreplace /conf/config.xml");
2829
			require_once("config.lib.inc");
2830
			$config = parse_config(true);
2831
			@unlink("{$g['tmp_path']}/vipreplace");
2832
		}
2833
	}
2834
}
2835

    
2836
function upgrade_086_to_087() {
2837
	global $config, $dummynet_pipe_list;
2838

    
2839
	if (!is_array($config['filter']) || !is_array($config['filter']['rule']))
2840
		return;
2841
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue']))
2842
		return;
2843

    
2844
	$dnqueue_number = 1;
2845
	$dnpipe_number = 1;
2846

    
2847
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
2848
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
2849
		$dnpipe_number++;
2850
		if (is_array($dnpipe['queue'])) {
2851
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
2852
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
2853
				$dnqueue_number++;
2854
			}
2855
		}
2856
	}
2857

    
2858
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
2859

    
2860
	require_once("shaper.inc");
2861
	read_dummynet_config();
2862

    
2863
	$dn_list = array();
2864
        if (is_array($dummynet_pipe_list)) {
2865
                foreach ($dummynet_pipe_list as $dn) {
2866
                        $tmplist =& $dn->get_queue_list();
2867
                        foreach ($tmplist as $qname => $link) {
2868
                                $dn_list[$link] = $qname;
2869
                        }
2870
                }
2871
        }
2872

    
2873
	foreach ($config['filter']['rule'] as $idx => $rule) {
2874
		if (!empty($rule['dnpipe'])) {
2875
			if (!empty($dn_list[$rule['dnpipe']]))
2876
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
2877
		}
2878
		if (!empty($rule['pdnpipe'])) {
2879
			if (!empty($dn_list[$rule['pdnpipe']]))
2880
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
2881
		}
2882
	}
2883
}
2884
function upgrade_087_to_088() {
2885
	global $config;
2886
	if (isset($config['system']['glxsb_enable'])) {
2887
		unset($config['system']['glxsb_enable']);
2888
		$config['system']['crypto_hardware'] = "glxsb";
2889
	}
2890
}
2891

    
2892
function upgrade_088_to_089() {
2893
  global $config;
2894
  if (!is_array($config['ca']))
2895
    $config['ca'] = array();
2896
  if (!is_array($config['cert']))
2897
    $config['cert'] = array();
2898
    
2899
  /* migrate captive portal ssl to certifcate mngr */
2900
  if (is_array($config['captiveportal'])) {
2901
    foreach ($config['captiveportal'] as $id => &$setting) {
2902
      if (isset($setting['httpslogin'])) {
2903
        /* create cert entry */
2904
        $cert = array();
2905
        $cert['refid'] = uniqid();
2906
        $cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
2907
        $cert['crt'] = $setting['certificate'];
2908
        $cert['prv'] = $setting['private-key'];
2909
        
2910
        if (!empty($setting['cacertificate'])) {
2911
          /* create ca entry */
2912
          $ca = array();
2913
          $ca['refid'] = uniqid();
2914
          $ca['descr'] = "Captive Portal CA - {$setting['zone']}";
2915
          $ca['crt'] = $setting['cacertificate'];
2916
          $config['ca'][] = $ca;
2917
          
2918
          /* add ca reference to certificate */
2919
          $cert['caref'] = $ca['refid'];
2920
        }
2921
        
2922
        $config['cert'][] = $cert;
2923
        
2924
        /* create cert reference */
2925
        $setting['certref'] = $cert['refid'];
2926
        
2927
        unset($setting['certificate']);
2928
        unset($setting['private-key']);
2929
        unset($setting['cacertificate']);
2930
        
2931
      }
2932
    }
2933
  }
2934
}
2935
function upgrade_089_to_090() {
2936
	global $config;
2937
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
2938
		$vs_a = &$config['load_balancer']['virtual_server'];
2939
		for ($i = 0; isset($vs_a[$i]); $i++) {
2940
			if (is_array($vs_a[$i]['pool'])) {
2941
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
2942
				unset($vs_a[$i]['pool']);
2943
			} elseif (!empty($vs_a[$i]['pool'])) {
2944
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
2945
				unset($vs_a[$i]['pool']);
2946
			}
2947
		}
2948
	}
2949
}
2950

    
2951
function upgrade_090_to_091() {
2952
	global $config;
2953

    
2954
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
2955
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
2956
			if (!empty($dnqueue['bandwidth'])) {
2957
				$bw = array();
2958
				$bw['bw'] = $dnqueue['bandwidth'];
2959
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
2960
				$bw['bwsched'] = "none";
2961
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
2962
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
2963
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
2964
			}
2965
		}
2966
	}
2967
}
2968
?>
(53-53/66)