Project

General

Profile

Download (145 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	upgrade_config.inc
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	are permitted provided that the following conditions are met:
10
 *
11
 *	1. Redistributions of source code must retain the above copyright notice,
12
 *		this list of conditions and the following disclaimer.
13
 *
14
 *	2. Redistributions in binary form must reproduce the above copyright
15
 *		notice, this list of conditions and the following disclaimer in
16
 *		the documentation and/or other materials provided with the
17
 *		distribution.
18
 *
19
 *	3. All advertising materials mentioning features or use of this software
20
 *		must display the following acknowledgment:
21
 *		"This product includes software developed by the pfSense Project
22
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *	4. The names "pfSense" and "pfSense Project" must not be used to
25
 *		 endorse or promote products derived from this software without
26
 *		 prior written permission. For written permission, please contact
27
 *		 coreteam@pfsense.org.
28
 *
29
 *	5. Products derived from this software may not be called "pfSense"
30
 *		nor may "pfSense" appear in their names without prior written
31
 *		permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35
 *
36
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *	====================================================================
53
 *
54
 */
55

    
56
if (!function_exists("dump_rrd_to_xml")) {
57
	require_once("rrd.inc");
58
}
59
if (!function_exists("read_altq_config")) {
60
	require_once("shaper.inc");
61
}
62

    
63
/* Upgrade functions must be named:
64
*    upgrade_XXX_to_YYY
65
	* where XXX == previous version, zero padded, and YYY == next version, zero padded
66
	*/
67
function upgrade_010_to_011() {
68
	global $config;
69
	$opti = 1;
70
	$ifmap = array('lan' => 'lan', 'wan' => 'wan', 'pptp' => 'pptp');
71

    
72
	/* convert DMZ to optional, if necessary */
73
	if (isset($config['interfaces']['dmz'])) {
74

    
75
		$dmzcfg = &$config['interfaces']['dmz'];
76

    
77
		if ($dmzcfg['if']) {
78
			$config['interfaces']['opt' . $opti] = array();
79
			$optcfg = &$config['interfaces']['opt' . $opti];
80

    
81
			$optcfg['enable'] = $dmzcfg['enable'];
82
			$optcfg['descr'] = "DMZ";
83
			$optcfg['if'] = $dmzcfg['if'];
84
			$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
85
			$optcfg['subnet'] = $dmzcfg['subnet'];
86

    
87
			$ifmap['dmz'] = "opt" . $opti;
88
			$opti++;
89
		}
90

    
91
		unset($config['interfaces']['dmz']);
92
	}
93

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

    
97
		if (!$config['interfaces']['wlan' . $i]['if']) {
98
			unset($config['interfaces']['wlan' . $i]);
99
			continue;
100
		}
101

    
102
		$wlancfg = &$config['interfaces']['wlan' . $i];
103
		$config['interfaces']['opt' . $opti] = array();
104
		$optcfg = &$config['interfaces']['opt' . $opti];
105

    
106
		$optcfg['enable'] = $wlancfg['enable'];
107
		$optcfg['descr'] = "WLAN" . $i;
108
		$optcfg['if'] = $wlancfg['if'];
109
		$optcfg['ipaddr'] = $wlancfg['ipaddr'];
110
		$optcfg['subnet'] = $wlancfg['subnet'];
111
		$optcfg['bridge'] = $wlancfg['bridge'];
112

    
113
		$optcfg['wireless'] = array();
114
		$optcfg['wireless']['mode'] = $wlancfg['mode'];
115
		$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
116
		$optcfg['wireless']['channel'] = $wlancfg['channel'];
117
		$optcfg['wireless']['wep'] = $wlancfg['wep'];
118

    
119
		$ifmap['wlan' . $i] = "opt" . $opti;
120

    
121
		unset($config['interfaces']['wlan' . $i]);
122
		$opti++;
123
	}
124

    
125
	/* convert filter rules */
126
	$n = count($config['filter']['rule']);
127
	for ($i = 0; $i < $n; $i++) {
128

    
129
		$fr = &$config['filter']['rule'][$i];
130

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

    
142
		/* remap source network */
143
		if (isset($fr['source']['network'])) {
144
			if (array_key_exists($fr['source']['network'], $ifmap)) {
145
				$fr['source']['network'] = $ifmap[$fr['source']['network']];
146
			} else {
147
				/* remove the rule */
148
				printf(gettext("%sWarning: filter rule removed " .
149
					"(source network '%s' does not exist anymore)."), "\n", $fr['source']['network']);
150
				unset($config['filter']['rule'][$i]);
151
				continue;
152
			}
153
		}
154

    
155
		/* remap destination network */
156
		if (isset($fr['destination']['network'])) {
157
			if (array_key_exists($fr['destination']['network'], $ifmap)) {
158
				$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
159
			} else {
160
				/* remove the rule */
161
				printf(gettext("%sWarning: filter rule removed " .
162
					"(destination network '%s' does not exist anymore)."), "\n", $fr['destination']['network']);
163
				unset($config['filter']['rule'][$i]);
164
				continue;
165
			}
166
		}
167
	}
168

    
169
	/* convert shaper rules */
170
	$n = count($config['pfqueueing']['rule']);
171
	if (is_array($config['pfqueueing']['rule'])) {
172
		for ($i = 0; $i < $n; $i++) {
173

    
174
			$fr = &$config['pfqueueing']['rule'][$i];
175

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

    
187
			/* remap source network */
188
			if (isset($fr['source']['network'])) {
189
				if (array_key_exists($fr['source']['network'], $ifmap)) {
190
					$fr['source']['network'] = $ifmap[$fr['source']['network']];
191
				} else {
192
					/* remove the rule */
193
					printf(gettext("%sWarning: traffic shaper rule removed " .
194
						"(source network '%s' does not exist anymore)."), "\n", $fr['source']['network']);
195
					unset($config['pfqueueing']['rule'][$i]);
196
					continue;
197
				}
198
			}
199

    
200
			/* remap destination network */
201
			if (isset($fr['destination']['network'])) {
202
				if (array_key_exists($fr['destination']['network'], $ifmap)) {
203
					$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
204
				} else {
205
					/* remove the rule */
206
					printf(gettext("%sWarning: traffic shaper rule removed " .
207
						"(destination network '%s' does not exist anymore)."), "\n", $fr['destination']['network']);
208
					unset($config['pfqueueing']['rule'][$i]);
209
					continue;
210
				}
211
			}
212
		}
213
	}
214
}
215

    
216

    
217
function upgrade_011_to_012() {
218
	global $config;
219
	/* move LAN DHCP server config */
220
	$tmp = $config['dhcpd'];
221
	$config['dhcpd'] = array();
222
	$config['dhcpd']['lan'] = $tmp;
223

    
224
	/* encrypt password */
225
	$config['system']['password'] = crypt($config['system']['password']);
226
}
227

    
228

    
229
function upgrade_012_to_013() {
230
	global $config;
231
	/* convert advanced outbound NAT config */
232
	for ($i = 0; isset($config['nat']['advancedoutbound']['rule'][$i]); $i++) {
233
		$curent = &$config['nat']['advancedoutbound']['rule'][$i];
234
		$src = $curent['source'];
235
		$curent['source'] = array();
236
		$curent['source']['network'] = $src;
237
		$curent['destination'] = array();
238
		$curent['destination']['any'] = true;
239
	}
240

    
241
	/* add an explicit type="pass" to all filter rules to make things consistent */
242
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
243
		$config['filter']['rule'][$i]['type'] = "pass";
244
	}
245
}
246

    
247

    
248
function upgrade_013_to_014() {
249
	global $config;
250
	/* convert shaper rules (make pipes) */
251
	if (is_array($config['pfqueueing']['rule'])) {
252
		$config['pfqueueing']['pipe'] = array();
253

    
254
		for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
255
			$curent = &$config['pfqueueing']['rule'][$i];
256

    
257
			/* make new pipe and associate with this rule */
258
			$newpipe = array();
259
			$newpipe['descr'] = $curent['descr'];
260
			$newpipe['bandwidth'] = $curent['bandwidth'];
261
			$newpipe['delay'] = $curent['delay'];
262
			$newpipe['mask'] = $curent['mask'];
263
			$config['pfqueueing']['pipe'][$i] = $newpipe;
264

    
265
			$curent['targetpipe'] = $i;
266

    
267
			unset($curent['bandwidth']);
268
			unset($curent['delay']);
269
			unset($curent['mask']);
270
		}
271
	}
272
}
273

    
274

    
275
function upgrade_014_to_015() {
276
	global $config;
277
	/* Default route moved */
278
	if (isset($config['interfaces']['wan']['gateway'])) {
279
		if ($config['interfaces']['wan']['gateway'] <> "") {
280
			$config['system']['gateway'] = $config['interfaces']['wan']['gateway'];
281
		}
282
		unset($config['interfaces']['wan']['gateway']);
283
	}
284

    
285
	/* Queues are no longer interface specific */
286
	if (isset($config['interfaces']['lan']['schedulertype'])) {
287
		unset($config['interfaces']['lan']['schedulertype']);
288
	}
289
	if (isset($config['interfaces']['wan']['schedulertype'])) {
290
		unset($config['interfaces']['wan']['schedulertype']);
291
	}
292

    
293
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
294
		if (isset($config['interfaces']['opt' . $i]['schedulertype'])) {
295
			unset($config['interfaces']['opt' . $i]['schedulertype']);
296
		}
297
	}
298
}
299

    
300

    
301
function upgrade_015_to_016() {
302
	global $config;
303
	/* Alternate firmware URL moved */
304
	if (isset($config['system']['firmwareurl']) && isset($config['system']['firmwarename'])) { // Only convert if *both* are defined.
305
		$config['system']['alt_firmware_url'] = array();
306
		$config['system']['alt_firmware_url']['enabled'] = "";
307
		$config['system']['alt_firmware_url']['firmware_base_url'] = $config['system']['firmwareurl'];
308
		$config['system']['alt_firmware_url']['firmware_filename'] = $config['system']['firmwarename'];
309
	}
310
	if (isset($config['system']['firmwareurl'])) {
311
		unset($config['system']['firmwareurl']);
312
	}
313
	if (isset($config['system']['firmwarename'])) {
314
		unset($config['system']['firmwarename']);
315
	}
316
}
317

    
318

    
319
function upgrade_016_to_017() {
320
	global $config;
321
	/* wipe previous shaper configuration */
322
	if (isset($config['shaper']['queue'])) {
323
		unset($config['shaper']['queue']);
324
	}
325
	if (isset($config['shaper']['rule'])) {
326
		unset($config['shaper']['rule']);
327
	}
328
	if (isset($config['interfaces']['wan']['bandwidth'])) {
329
		unset($config['interfaces']['wan']['bandwidth']);
330
	}
331
	if (isset($config['interfaces']['wan']['bandwidthtype'])) {
332
		unset($config['interfaces']['wan']['bandwidthtype']);
333
	}
334
	if (isset($config['interfaces']['lan']['bandwidth'])) {
335
		unset($config['interfaces']['lan']['bandwidth']);
336
	}
337
	if (isset($config['interfaces']['lan']['bandwidthtype'])) {
338
		unset($config['interfaces']['lan']['bandwidthtype']);
339
	}
340
	$config['shaper']['enable'] = FALSE;
341
}
342

    
343

    
344
function upgrade_017_to_018() {
345
	global $config;
346
	if (isset($config['proxyarp']) && is_array($config['proxyarp']['proxyarpnet'])) {
347
		$proxyarp = &$config['proxyarp']['proxyarpnet'];
348
		foreach ($proxyarp as $arpent) {
349
			$vip = array();
350
			$vip['mode'] = "proxyarp";
351
			$vip['interface'] = $arpent['interface'];
352
			$vip['descr'] = $arpent['descr'];
353
			if (isset($arpent['range'])) {
354
				$vip['range'] = $arpent['range'];
355
				$vip['type'] = "range";
356
			} else {
357
				$subnet = explode('/', $arpent['network']);
358
				$vip['subnet'] = $subnet[0];
359
				if (isset($subnet[1])) {
360
					$vip['subnet_bits'] = $subnet[1];
361
					$vip['type'] = "network";
362
				} else {
363
					$vip['subnet_bits'] = "32";
364
					$vip['type'] = "single";
365
				}
366
			}
367
			$config['virtualip']['vip'][] = $vip;
368
		}
369
		unset($config['proxyarp']);
370
	}
371
	if (isset($config['installedpackages']) && isset($config['installedpackages']['carp']) && is_array($config['installedpackages']['carp']['config'])) {
372
		$carp = &$config['installedpackages']['carp']['config'];
373
		foreach ($carp as $carpent) {
374
			$vip = array();
375
			$vip['mode'] = "carp";
376
			$vip['interface'] = "AUTO";
377
			$vip['descr'] = sprintf(gettext("CARP vhid %s"), $carpent['vhid']);
378
			$vip['type'] = "single";
379
			$vip['vhid'] = $carpent['vhid'];
380
			$vip['advskew'] = $carpent['advskew'];
381
			$vip['password'] = $carpent['password'];
382
			$vip['subnet'] = $carpent['ipaddress'];
383
			$vip['subnet_bits'] = $carpent['netmask'];
384
			$config['virtualip']['vip'][] = $vip;
385
		}
386
		unset($config['installedpackages']['carp']);
387
	}
388
	/* Server NAT is no longer needed */
389
	if (isset($config['nat']['servernat'])) {
390
		unset($config['nat']['servernat']);
391
	}
392

    
393
	/* enable SSH */
394
	if ($config['version'] == "1.8") {
395
		$config['system']['sshenabled'] = true;
396
	}
397
}
398

    
399

    
400
function upgrade_018_to_019() {
401
	global $config;
402
}
403

    
404

    
405
function upgrade_019_to_020() {
406
	global $config;
407
	if (is_array($config['ipsec']['tunnel'])) {
408
		reset($config['ipsec']['tunnel']);
409
		while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
410
			/* Sanity check on required variables */
411
			/* This fixes bogus <tunnel> entries - remnant of bug #393 */
412
			if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
413
				unset($config['ipsec']['tunnel'][$tunnel]);
414
			}
415
		}
416
	}
417
}
418

    
419
function upgrade_020_to_021() {
420
	global $config;
421
	/* shaper scheduler moved */
422
	if (isset($config['system']['schedulertype'])) {
423
		$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
424
		unset($config['system']['schedulertype']);
425
	}
426
}
427

    
428

    
429
function upgrade_021_to_022() {
430
	global $config;
431
	/* move gateway to wan interface */
432
	$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
433
}
434

    
435
function upgrade_022_to_023() {
436
	global $config;
437
	if (isset($config['shaper'])) {
438
		/* wipe previous shaper configuration */
439
		unset($config['shaper']);
440
	}
441
}
442

    
443

    
444
function upgrade_023_to_024() {
445
	global $config;
446
}
447

    
448

    
449
function upgrade_024_to_025() {
450
	global $config;
451
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
452
	if (isset($config['system']['use_rrd_gateway'])) {
453
		unset($config['system']['use_rrd_gateway']);
454
	}
455
}
456

    
457

    
458
function upgrade_025_to_026() {
459
	global $config;
460
	$cron_item = array();
461
	$cron_item['minute'] = "0";
462
	$cron_item['hour'] = "*";
463
	$cron_item['mday'] = "*";
464
	$cron_item['month'] = "*";
465
	$cron_item['wday'] = "*";
466
	$cron_item['who'] = "root";
467
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
468

    
469
	$config['cron']['item'][] = $cron_item;
470

    
471
	$cron_item = array();
472
	$cron_item['minute'] = "1,31";
473
	$cron_item['hour'] = "0-5";
474
	$cron_item['mday'] = "*";
475
	$cron_item['month'] = "*";
476
	$cron_item['wday'] = "*";
477
	$cron_item['who'] = "root";
478
	$cron_item['command'] = "/usr/bin/nice -n20 adjkerntz -a";
479

    
480
	$config['cron']['item'][] = $cron_item;
481

    
482
	$cron_item = array();
483
	$cron_item['minute'] = "1";
484
	$cron_item['hour'] = "*";
485
	$cron_item['mday'] = "1";
486
	$cron_item['month'] = "*";
487
	$cron_item['wday'] = "*";
488
	$cron_item['who'] = "root";
489
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
490

    
491
	$config['cron']['item'][] = $cron_item;
492

    
493
	$cron_item = array();
494
	$cron_item['minute'] = "*/60";
495
	$cron_item['hour'] = "*";
496
	$cron_item['mday'] = "*";
497
	$cron_item['month'] = "*";
498
	$cron_item['wday'] = "*";
499
	$cron_item['who'] = "root";
500
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout";
501

    
502
	$config['cron']['item'][] = $cron_item;
503

    
504
	$cron_item = array();
505
	$cron_item['minute'] = "1";
506
	$cron_item['hour'] = "1";
507
	$cron_item['mday'] = "*";
508
	$cron_item['month'] = "*";
509
	$cron_item['wday'] = "*";
510
	$cron_item['who'] = "root";
511
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.dyndns.update";
512

    
513
	$config['cron']['item'][] = $cron_item;
514

    
515
	$cron_item = array();
516
	$cron_item['minute'] = "*/60";
517
	$cron_item['hour'] = "*";
518
	$cron_item['mday'] = "*";
519
	$cron_item['month'] = "*";
520
	$cron_item['wday'] = "*";
521
	$cron_item['who'] = "root";
522
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 virusprot";
523

    
524
	$config['cron']['item'][] = $cron_item;
525

    
526
	$cron_item = array();
527
	$cron_item['minute'] = "*/60";
528
	$cron_item['hour'] = "*";
529
	$cron_item['mday'] = "*";
530
	$cron_item['month'] = "*";
531
	$cron_item['wday'] = "*";
532
	$cron_item['who'] = "root";
533
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -t 1800 snort2c";
534

    
535
	$config['cron']['item'][] = $cron_item;
536
}
537

    
538

    
539
function upgrade_026_to_027() {
540
	global $config;
541
}
542

    
543

    
544
function upgrade_027_to_028() {
545
	global $config;
546
}
547

    
548

    
549
function upgrade_028_to_029() {
550
	global $config;
551
	$rule_item = array();
552
	$a_filter = &$config['filter']['rule'];
553
	$rule_item['interface'] = "enc0";
554
	$rule_item['type'] = "pass";
555
	$rule_item['source']['any'] = true;
556
	$rule_item['destination']['any'] = true;
557
	$rule_item['descr'] = gettext("Permit IPsec traffic.");
558
	$rule_item['statetype'] = "keep state";
559
	$a_filter[] = $rule_item;
560
}
561

    
562

    
563
function upgrade_029_to_030() {
564
	global $config;
565
	/* enable the rrd config setting by default */
566
	$config['rrd']['enable'] = true;
567
}
568

    
569

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

    
575

    
576
function upgrade_031_to_032() {
577
	global $config;
578
	/* Insert upgrade code here */
579
}
580

    
581

    
582
function upgrade_032_to_033() {
583
	global $config;
584
	/* Insert upgrade code here */
585
}
586

    
587

    
588
function upgrade_033_to_034() {
589
	global $config;
590
	/* Insert upgrade code here */
591
}
592

    
593

    
594
function upgrade_034_to_035() {
595
	global $config;
596
	/* Insert upgrade code here */
597
}
598

    
599

    
600
function upgrade_035_to_036() {
601
	global $config;
602
	/* Insert upgrade code here */
603
}
604

    
605

    
606
function upgrade_036_to_037() {
607
	global $config;
608
	/* Insert upgrade code here */
609
}
610

    
611

    
612
function upgrade_037_to_038() {
613
	global $config;
614
	/* Insert upgrade code here */
615
}
616

    
617

    
618
function upgrade_038_to_039() {
619
	global $config;
620
	/* Insert upgrade code here */
621
}
622

    
623

    
624
function upgrade_039_to_040() {
625
	global $config, $g;
626
	$config['system']['webgui']['auth_method'] = "session";
627
	$config['system']['webgui']['backing_method'] = "htpasswd";
628

    
629
	if (isset($config['system']['username'])) {
630
		$config['system']['group'] = array();
631
		$config['system']['group'][0]['name'] = "admins";
632
		$config['system']['group'][0]['description'] = gettext("System Administrators");
633
		$config['system']['group'][0]['scope'] = "system";
634
		$config['system']['group'][0]['priv'] = "page-all";
635
		$config['system']['group'][0]['home'] = "index.php";
636
		$config['system']['group'][0]['gid'] = "110";
637

    
638
		$config['system']['user'] = array();
639
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
640
		$config['system']['user'][0]['descr'] = "System Administrator";
641
		$config['system']['user'][0]['scope'] = "system";
642
		$config['system']['user'][0]['groupname'] = "admins";
643
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
644
		$config['system']['user'][0]['uid'] = "0";
645
		/* Ensure that we follow what this new "admin" username should be in the session. */
646
		$_SESSION["Username"] = "{$config['system']['username']}";
647

    
648
		$config['system']['user'][0]['priv'] = array();
649
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
650
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
651
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
652
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
653
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
654
		$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).");
655
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
656
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
657
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
658
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
659
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
660
		$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."), $g['product_name']);
661
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
662
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
663
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (this privilege should only be associated with one single user).");
664

    
665
		$config['system']['nextuid'] = "111";
666
		$config['system']['nextgid'] = "111";
667

    
668
		/* wipe previous auth configuration */
669
		unset($config['system']['username']);
670
		if (isset($config['system']['password'])) {
671
			unset($config['system']['password']);
672
		}
673
	}
674
}
675

    
676
function upgrade_040_to_041() {
677
	global $config;
678
	if (!$config['sysctl']) {
679
		$config['sysctl']['item'] = array();
680

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

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

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

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

    
697
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
698
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
699
		$config['sysctl']['item'][4]['value'] =   "default";
700

    
701
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
702
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
703
		$config['sysctl']['item'][5]['value'] =   "default";
704

    
705
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
706
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
707
		$config['sysctl']['item'][6]['value'] =   "default";
708

    
709
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
710
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
711
		$config['sysctl']['item'][7]['value'] =   "default";
712

    
713
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
714
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
715
		$config['sysctl']['item'][8]['value'] =   "default";
716

    
717
		$config['sysctl']['item'][9]['tunable'] = "net.inet.tcp.delayed_ack";
718
		$config['sysctl']['item'][9]['descr'] =    gettext("Do not delay ACK to try and piggyback it onto a data packet");
719
		$config['sysctl']['item'][9]['value'] =   "default";
720

    
721
		$config['sysctl']['item'][10]['tunable'] = "net.inet.udp.maxdgram";
722
		$config['sysctl']['item'][10]['descr'] =    gettext("Maximum outgoing UDP datagram size");
723
		$config['sysctl']['item'][10]['value'] =   "default";
724

    
725
		$config['sysctl']['item'][11]['tunable'] = "net.link.bridge.pfil_onlyip";
726
		$config['sysctl']['item'][11]['descr'] =    gettext("Handling of non-IP packets which are not passed to pfil (see if_bridge(4))");
727
		$config['sysctl']['item'][11]['value'] =   "default";
728

    
729
		$config['sysctl']['item'][12]['tunable'] = "net.link.tap.user_open";
730
		$config['sysctl']['item'][12]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
731
		$config['sysctl']['item'][12]['value'] =   "default";
732

    
733
		$config['sysctl']['item'][13]['tunable'] = "kern.randompid";
734
		$config['sysctl']['item'][13]['descr'] =    gettext("Randomize PID's (see src/sys/kern/kern_fork.c: sysctl_kern_randompid())");
735
		$config['sysctl']['item'][13]['value'] =   "default";
736

    
737
		$config['sysctl']['item'][14]['tunable'] = "net.inet.tcp.inflight.enable";
738
		$config['sysctl']['item'][14]['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. ");
739
		$config['sysctl']['item'][14]['value'] =   "default";
740

    
741
		$config['sysctl']['item'][15]['tunable'] = "net.inet.icmp.icmplim";
742
		$config['sysctl']['item'][15]['descr'] =    gettext("Set ICMP Limits");
743
		$config['sysctl']['item'][15]['value'] =   "default";
744

    
745
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.tso";
746
		$config['sysctl']['item'][16]['descr'] =    gettext("TCP Offload engine");
747
		$config['sysctl']['item'][16]['value'] =   "default";
748

    
749
		$config['sysctl']['item'][17]['tunable'] = "net.inet.ip.portrange.first";
750
		$config['sysctl']['item'][17]['descr'] =    "Set the ephemeral port range starting port";
751
		$config['sysctl']['item'][17]['value'] =   "default";
752

    
753
		$config['sysctl']['item'][18]['tunable'] = "hw.syscons.kbd_reboot";
754
		$config['sysctl']['item'][18]['descr'] =    "Enables ctrl+alt+delete";
755
		$config['sysctl']['item'][18]['value'] =   "default";
756

    
757
		$config['sysctl']['item'][19]['tunable'] = "kern.ipc.maxsockbuf";
758
		$config['sysctl']['item'][19]['descr'] =    "Maximum socket buffer size";
759
		$config['sysctl']['item'][19]['value'] =   "default";
760

    
761
	}
762
}
763

    
764

    
765
function upgrade_041_to_042() {
766
	global $config;
767
	if (isset($config['shaper'])) {
768
		unset($config['shaper']);
769
	}
770
	if (isset($config['ezshaper'])) {
771
		unset($config['ezshaper']);
772
	}
773
}
774

    
775

    
776
function upgrade_042_to_043() {
777
	global $config;
778
	/* migrate old interface gateway to the new gateways config */
779
	$iflist = get_configured_interface_list(false, true);
780
	$gateways = array();
781
	$i = 0;
782
	foreach ($iflist as $ifname => $interface) {
783
		if (!interface_has_gateway($ifname)) {
784
			continue;
785
		}
786
		$config['gateways']['gateway_item'][$i] = array();
787
		if (is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
788
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
789
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Static Gateway"), $ifname);
790
		} else {
791
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
792
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Dynamic Gateway"), $ifname);
793
		}
794
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
795
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
796
		/* add default gateway bit for wan on upgrade */
797
		if ($ifname == "wan") {
798
			$config['gateways']['gateway_item'][$i]['defaultgw'] = true;
799
		}
800
		if (is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
801
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
802
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
803
		}
804
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
805

    
806
		/* Update all filter rules which might reference this gateway */
807
		$j = 0;
808
		foreach ($config['filter']['rule'] as $rule) {
809
			if (is_ipaddr($rule['gateway'])) {
810
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway']) {
811
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
812
				} else if ($rule['gateway'] == $ifname) {
813
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
814
				}
815
			}
816
			$j++;
817
		}
818

    
819
		/* rename old Quality RRD files in the process */
820
		$rrddbpath = "/var/db/rrd";
821
		$gwname = "GW_" . strtoupper($ifname);
822
		if (is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
823
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
824
		}
825
		$i++;
826
	}
827
}
828

    
829

    
830
function upgrade_043_to_044() {
831
	global $config;
832

    
833
	/* migrate static routes to the new gateways config */
834
	$gateways = return_gateways_array(true);
835
	$i = 0;
836
	if (is_array($config['staticroutes']['route'])) {
837
		$gwmap = array();
838
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
839
			$found = false;
840
			foreach ($gateways as $gwname => $gw) {
841
				if ($gw['gateway'] == $sroute['gateway']) {
842
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
843
					$found = true;
844
					break;
845
				}
846
			}
847
			if ($gwmap[$sroute['gateway']]) {
848
				/* We already added a gateway name for this IP */
849
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
850
				$found = true;
851
			}
852

    
853
			if ($found == false) {
854
				$gateway = array();
855
				$gateway['name'] = "SROUTE{$i}";
856
				$gwmap[$sroute['gateway']] = $gateway['name'];
857
				$gateway['gateway'] = $sroute['gateway'];
858
				$gateway['interface'] = $sroute['interface'];
859
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
860
				if (!is_array($config['gateways']['gateway_item'])) {
861
					$config['gateways']['gateway_item'] = array();
862
				}
863
				$config['gateways']['gateway_item'][] = $gateway;
864
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
865
				$i++;
866
			}
867
		}
868
	}
869
}
870

    
871

    
872
function upgrade_044_to_045() {
873
	global $config;
874
	$iflist = get_configured_interface_list(false, true);
875
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
876
		$i = 0;
877
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
878
			/* Make sure to update the interfaces section with the right name */
879
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
880
			foreach ($iflist as $ifname) {
881
				if ($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
882
					$config['interfaces'][$ifname]['if'] = $vlan_name;
883
					continue;
884
				}
885
			}
886
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
887
			$i++;
888
		}
889
	}
890
}
891

    
892

    
893
function upgrade_045_to_046() {
894
	global $config;
895
	/* Load up monitors that are in the default config for 2.0 but not in 1.2.3
896
		thus wouldn't be in an upgraded config. */
897
	$config['load_balancer']['monitor_type'] = array (
898
		array ('name' => 'ICMP',
899
			'type' => 'icmp',
900
			'descr' => 'ICMP',
901
			'options' => '',
902
		),
903
		array ('name' => 'TCP',
904
			'type' => 'tcp',
905
			'descr' => 'Generic TCP',
906
			'options' => '',
907
		),
908
		array ('name' => 'HTTP',
909
			'type' => 'http',
910
			'descr' => 'Generic HTTP',
911
			'options' =>
912
			array ('path' => '/',
913
				'host' => '',
914
				'code' => '200',
915
			),
916
		),
917
		array ('name' => 'HTTPS',
918
			'type' => 'https',
919
			'descr' => 'Generic HTTPS',
920
			'options' =>
921
			array ('path' => '/',
922
				'host' => '',
923
				'code' => '200',
924
			),
925
		),
926
		array ('name' => 'SMTP',
927
			'type' => 'send',
928
			'descr' => 'Generic SMTP',
929
			'options' =>
930
			array ('send' => '',
931
				'expect' => '220 *',
932
			),
933
		),
934
	);
935
	/* Upgrade load balancer from slb to relayd */
936
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
937
		$vs_a = &$config['load_balancer']['virtual_server'];
938
		$pool_a = &$config['load_balancer']['lbpool'];
939
		$pools = array();
940
		/* Index pools by name */
941
		if (is_array($pool_a)) {
942
			for ($i = 0; isset($pool_a[$i]); $i++) {
943
				if ($pool_a[$i]['type'] == "server") {
944
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
945
				}
946
			}
947
		}
948
		/* Convert sitedown entries to pools and re-attach */
949
		for ($i = 0; isset($vs_a[$i]); $i++) {
950
			/* Set mode while we're here. */
951
			$vs_a[$i]['mode'] = "redirect_mode";
952
			if (isset($vs_a[$i]['sitedown'])) {
953
				$pool = array();
954
				$pool['type'] = 'server';
955
				$pool['behaviour'] = 'balance';
956
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
957
				$pool['descr'] = sprintf(gettext("Sitedown pool for VS: %s"), $vs_a[$i]['name']);
958
				if (is_array($vs_a[$i]['pool'])) {
959
					$vs_a[$i]['pool'] = $vs_a[$i]['pool'][0];
960
				}
961
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
962
				$pool['servers'] = array();
963
				$pool['servers'][] = $vs_a[$i]['sitedown'];
964
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
965
				$pool_a[] = $pool;
966
				$vs_a[$i]['sitedown'] = $pool['name'];
967
			}
968
		}
969
	}
970
	if (count($config['load_balancer']) == 0) {
971
		unset($config['load_balancer']);
972
	}
973
	mwexec('/usr/sbin/pw groupadd -n _relayd -g 913');
974
	mwexec('/usr/sbin/pw useradd -n _relayd -c "Relay Daemon" -d /var/empty -s /usr/sbin/nologin -u 913 -g 913');
975
}
976

    
977

    
978
function upgrade_046_to_047() {
979
	global $config;
980
	/* Upgrade IPsec from tunnel to phase1/phase2 */
981

    
982
	if (is_array($config['ipsec']['tunnel'])) {
983

    
984
		$a_phase1 = array();
985
		$a_phase2 = array();
986
		$ikeid = 0;
987

    
988
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
989

    
990
			unset($ph1ent);
991
			unset($ph2ent);
992

    
993
			/*
994
				*  attempt to locate an enabled phase1
995
				*  entry that matches the peer gateway
996
				*/
997

    
998
			if (!isset($tunnel['disabled'])) {
999

    
1000
				$remote_gateway = $tunnel['remote-gateway'];
1001

    
1002
				foreach ($a_phase1 as $ph1tmp) {
1003
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
1004
						$ph1ent = $ph1tmp;
1005
						break;
1006
					}
1007
				}
1008
			}
1009

    
1010
			/* none found, create a new one */
1011

    
1012
			if (!isset($ph1ent)) {
1013

    
1014
				/* build new phase1 entry */
1015

    
1016
				$ph1ent = array();
1017

    
1018
				$ph1ent['ikeid'] = ++$ikeid;
1019

    
1020
				if (isset($tunnel['disabled'])) {
1021
					$ph1ent['disabled'] = $tunnel['disabled'];
1022
				}
1023

    
1024
				/* convert to the new vip[$vhid] name */
1025
				if (preg_match("/^carp/", $tunnel['interface'])) {
1026
					$carpid = str_replace("carp", "", $tunnel['interface']);
1027
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
1028
				}
1029
				$ph1ent['interface'] = $tunnel['interface'];
1030
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
1031
				$ph1ent['descr'] = $tunnel['descr'];
1032

    
1033
				$ph1ent['mode'] = $tunnel['p1']['mode'];
1034

    
1035
				if (isset($tunnel['p1']['myident']['myaddress'])) {
1036
					$ph1ent['myid_type'] = "myaddress";
1037
				}
1038
				if (isset($tunnel['p1']['myident']['address'])) {
1039
					$ph1ent['myid_type'] = "address";
1040
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
1041
				}
1042
				if (isset($tunnel['p1']['myident']['fqdn'])) {
1043
					$ph1ent['myid_type'] = "fqdn";
1044
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
1045
				}
1046
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
1047
					$ph1ent['myid_type'] = "user_fqdn";
1048
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
1049
				}
1050
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
1051
					$ph1ent['myid_type'] = "asn1dn";
1052
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1053
				}
1054
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1055
					$ph1ent['myid_type'] = "dyn_dns";
1056
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1057
				}
1058

    
1059
				$ph1ent['peerid_type'] = "peeraddress";
1060

    
1061
				switch ($tunnel['p1']['encryption-algorithm']) {
1062
					case "des":
1063
						$ph1alg = array('name' => 'des');
1064
						break;
1065
					case "3des":
1066
						$ph1alg = array('name' => '3des');
1067
						break;
1068
					case "blowfish":
1069
						$ph1alg = array('name' => 'blowfish', 'keylen' => '128');
1070
						break;
1071
					case "cast128":
1072
						$ph1alg = array('name' => 'cast128');
1073
						break;
1074
					case "rijndael":
1075
						$ph1alg = array('name' => 'aes', 'keylen' => '128');
1076
						break;
1077
					case "rijndael 256":
1078
					case "aes 256":
1079
						$ph1alg = array('name' => 'aes', 'keylen' => '256');
1080
						break;
1081
				}
1082

    
1083
				$ph1ent['encryption-algorithm'] = $ph1alg;
1084
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1085
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1086
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1087
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1088

    
1089
				if (isset($tunnel['p1']['pre-shared-key'])) {
1090
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1091
				}
1092
				if (isset($tunnel['p1']['cert'])) {
1093
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1094
				}
1095
				if (isset($tunnel['p1']['peercert'])) {
1096
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1097
				}
1098
				if (isset($tunnel['p1']['private-key'])) {
1099
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1100
				}
1101

    
1102
				$ph1ent['nat_traversal'] = "on";
1103
				$ph1ent['dpd_enable'] = 1;
1104
				$ph1ent['dpd_delay'] = 10;
1105
				$ph1ent['dpd_maxfail'] = 5;
1106

    
1107
				$a_phase1[] = $ph1ent;
1108
			}
1109

    
1110
			/* build new phase2 entry */
1111

    
1112
			$ph2ent = array();
1113

    
1114
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1115

    
1116
			if (isset($tunnel['disabled'])) {
1117
				$ph1ent['disabled'] = $tunnel['disabled'];
1118
			}
1119

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

    
1122
			$type = "lan";
1123
			if ($tunnel['local-subnet']['network']) {
1124
				$type = $tunnel['local-subnet']['network'];
1125
			}
1126
			if ($tunnel['local-subnet']['address']) {
1127
				list($address, $netbits) = explode("/", $tunnel['local-subnet']['address']);
1128
				if (is_null($netbits)) {
1129
					$type = "address";
1130
				} else {
1131
					$type = "network";
1132
				}
1133
			}
1134

    
1135
			switch ($type) {
1136
				case "address":
1137
					$ph2ent['localid'] = array('type' => $type, 'address' => $address);
1138
					break;
1139
				case "network":
1140
					$ph2ent['localid'] = array('type' => $type, 'address' => $address, 'netbits' => $netbits);
1141
					break;
1142
				default:
1143
					$ph2ent['localid'] = array('type' => $type);
1144
					break;
1145
			}
1146

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

    
1150
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1151

    
1152
			$aes_count = 0;
1153
			foreach ($tunnel['p2']['encryption-algorithm-option'] as $tunalg) {
1154
				$aes_found = false;
1155
				switch ($tunalg) {
1156
					case "des":
1157
						$ph2alg = array('name' => 'des');
1158
						break;
1159
					case "3des":
1160
						$ph2alg = array('name' => '3des');
1161
						break;
1162
					case "blowfish":
1163
						$ph2alg = array('name' => 'blowfish', 'keylen' => 'auto');
1164
						break;
1165
					case "cast128":
1166
						$ph2alg = array('name' => 'cast128');
1167
						break;
1168
					case "rijndael":
1169
					case "rijndael 256":
1170
					case "aes 256":
1171
						$ph2alg = array('name' => 'aes', 'keylen' => 'auto');
1172
						$aes_found = true;
1173
						$aes_count++;
1174
						break;
1175
				}
1176

    
1177
				if (!$aes_found || ($aes_count < 2)) {
1178
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1179
				}
1180
			}
1181

    
1182
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1183
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1184
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1185

    
1186
			if (isset($tunnel['pinghost']['pinghost'])) {
1187
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1188
			}
1189

    
1190
			$a_phase2[] = $ph2ent;
1191
		}
1192

    
1193
		unset($config['ipsec']['tunnel']);
1194
		$config['ipsec']['phase1'] = $a_phase1;
1195
		$config['ipsec']['phase2'] = $a_phase2;
1196
	}
1197

    
1198
	/* Upgrade Mobile IPsec */
1199
	if (isset($config['ipsec']['mobileclients']) &&
1200
	    is_array($config['ipsec']['mobileclients']) &&
1201
	    is_array($config['ipsec']['mobileclients']['p1']) &&
1202
	    is_array($config['ipsec']['mobileclients']['p2'])) {
1203

    
1204
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1205
			$config['ipsec']['client']['enable'] = true;
1206
			$config['ipsec']['client']['user_source'] = 'system';
1207
			$config['ipsec']['client']['group_source'] = 'system';
1208
		}
1209

    
1210
		$mobilecfg = $config['ipsec']['mobileclients'];
1211

    
1212
		$ph1ent = array();
1213
		$ph1ent['ikeid'] = ++$ikeid;
1214

    
1215
		if (!isset($mobilecfg['enable'])) {
1216
			$ph1ent['disabled'] = true;
1217
		}
1218

    
1219
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1220
		$ph1ent['interface'] = 'wan';
1221
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1222
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1223

    
1224
		if (isset($mobilecfg['p1']['myident']['myaddress'])) {
1225
			$ph1ent['myid_type'] = "myaddress";
1226
		}
1227
		if (isset($mobilecfg['p1']['myident']['address'])) {
1228
			$ph1ent['myid_type'] = "address";
1229
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1230
		}
1231
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1232
			$ph1ent['myid_type'] = "fqdn";
1233
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1234
		}
1235
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1236
			$ph1ent['myid_type'] = "user_fqdn";
1237
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1238
		}
1239
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1240
			$ph1ent['myid_type'] = "asn1dn";
1241
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1242
		}
1243
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1244
			$ph1ent['myid_type'] = "dyn_dns";
1245
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1246
		}
1247
		$ph1ent['peerid_type'] = "fqdn";
1248
		$ph1ent['peerid_data'] = "";
1249

    
1250
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1251
			case "des":
1252
				$ph1alg = array('name' => 'des');
1253
				break;
1254
			case "3des":
1255
				$ph1alg = array('name' => '3des');
1256
				break;
1257
			case "blowfish":
1258
				$ph1alg = array('name' => 'blowfish', 'keylen' => '128');
1259
				break;
1260
			case "cast128":
1261
				$ph1alg = array('name' => 'cast128');
1262
				break;
1263
			case "rijndael":
1264
				$ph1alg = array('name' => 'aes', 'keylen' => '128');
1265
				break;
1266
			case "rijndael 256":
1267
			case "aes 256":
1268
				$ph1alg = array('name' => 'aes', 'keylen' => '256');
1269
				break;
1270
		}
1271

    
1272
		$ph1ent['encryption-algorithm'] = $ph1alg;
1273
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1274
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1275
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1276
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1277

    
1278
		if (isset($mobilecfg['p1']['cert'])) {
1279
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1280
		}
1281
		if (isset($mobilecfg['p1']['peercert'])) {
1282
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1283
		}
1284
		if (isset($mobilecfg['p1']['private-key'])) {
1285
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1286
		}
1287

    
1288
		$ph1ent['nat_traversal'] = "on";
1289
		$ph1ent['dpd_enable'] = 1;
1290
		$ph1ent['dpd_delay'] = 10;
1291
		$ph1ent['dpd_maxfail'] = 5;
1292
		$ph1ent['mobile'] = true;
1293

    
1294
		$ph2ent = array();
1295
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1296
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1297
		$ph2ent['localid'] = array('type' => 'none');
1298
		$ph2ent['remoteid'] = array('type' => 'mobile');
1299
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1300

    
1301
		$aes_count = 0;
1302
		foreach ($mobilecfg['p2']['encryption-algorithm-option'] as $tunalg) {
1303
			$aes_found = false;
1304
			switch ($tunalg) {
1305
				case "des":
1306
					$ph2alg = array('name' => 'des');
1307
					break;
1308
				case "3des":
1309
					$ph2alg = array('name' => '3des');
1310
					break;
1311
				case "blowfish":
1312
					$ph2alg = array('name' => 'blowfish', 'keylen' => 'auto');
1313
					break;
1314
				case "cast128":
1315
					$ph2alg = array('name' => 'cast128');
1316
					break;
1317
				case "rijndael":
1318
				case "rijndael 256":
1319
				case "aes 256":
1320
					$ph2alg = array('name' => 'aes', 'keylen' => 'auto');
1321
					$aes_found = true;
1322
					$aes_count++;
1323
					break;
1324
			}
1325

    
1326
			if (!$aes_found || ($aes_count < 2)) {
1327
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1328
			}
1329
		}
1330
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1331
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1332
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1333
		$ph2ent['mobile'] = true;
1334

    
1335
		$config['ipsec']['phase1'][] = $ph1ent;
1336
		$config['ipsec']['phase2'][] = $ph2ent;
1337
		unset($config['ipsec']['mobileclients']);
1338
	}
1339
}
1340

    
1341

    
1342
function upgrade_047_to_048() {
1343
	global $config;
1344
	if (!empty($config['dyndns'])) {
1345
		$config['dyndnses'] = array();
1346
		$config['dyndnses']['dyndns'] = array();
1347
		if (isset($config['dyndns'][0]['host'])) {
1348
			$tempdyn = array();
1349
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1350
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1351
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1352
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1353
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1354
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1355
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];
1356
			$tempdyn['interface'] = "wan";
1357
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1358
			$config['dyndnses']['dyndns'][] = $tempdyn;
1359
		}
1360
		unset($config['dyndns']);
1361
	}
1362
	if (!empty($config['dnsupdate'])) {
1363
		$pconfig = $config['dnsupdate'][0];
1364
		if (!$pconfig['ttl']) {
1365
			$pconfig['ttl'] = 60;
1366
		}
1367
		if (!$pconfig['keytype']) {
1368
			$pconfig['keytype'] = "zone";
1369
		}
1370
		$pconfig['interface'] = "wan";
1371
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1372
		unset($config['dnsupdate']);
1373
	}
1374

    
1375
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1376
		$pconfig = array();
1377
		$pconfig['username'] = $config['pppoe'][0]['username'];
1378
		$pconfig['password'] = $config['pppoe'][0]['password'];
1379
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1380
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1381
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1382
		unset($config['pppoe']);
1383
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1384
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1385
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1386
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1387
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1388
	}
1389
	if (is_array($config['pptp'])) {
1390
		$pconfig = array();
1391
		$pconfig['username'] = $config['pptp']['username'];
1392
		$pconfig['password'] = $config['pptp']['password'];
1393
		$pconfig['provider'] = $config['pptp']['provider'];
1394
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1395
		$pconfig['timeout'] = $config['pptp']['timeout'];
1396
		unset($config['pptp']);
1397
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1398
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1399
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1400
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1401
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1402
	}
1403
}
1404

    
1405

    
1406
function upgrade_048_to_049() {
1407
	global $config;
1408
	/* setup new all users group */
1409
	$all = array();
1410
	$all['name'] = "all";
1411
	$all['description'] = gettext("All Users");
1412
	$all['scope'] = "system";
1413
	$all['gid'] = 1998;
1414
	$all['member'] = array();
1415

    
1416
	if (!is_array($config['system']['user'])) {
1417
		$config['system']['user'] = array();
1418
	}
1419
	if (!is_array($config['system']['group'])) {
1420
		$config['system']['group'] = array();
1421
	}
1422

    
1423
	/* work around broken uid assignments */
1424
	$config['system']['nextuid'] = 2000;
1425
	foreach ($config['system']['user'] as & $user) {
1426
		if (isset($user['uid']) && !$user['uid']) {
1427
			continue;
1428
		}
1429
		$user['uid'] = $config['system']['nextuid']++;
1430
	}
1431

    
1432
	/* work around broken gid assignments */
1433
	$config['system']['nextgid'] = 2000;
1434
	foreach ($config['system']['group'] as & $group) {
1435
		if ($group['name'] == $g['admin_group']) {
1436
			$group['gid'] = 1999;
1437
		} else {
1438
			$group['gid'] = $config['system']['nextgid']++;
1439
		}
1440
	}
1441

    
1442
	/* build group membership information */
1443
	foreach ($config['system']['group'] as & $group) {
1444
		$group['member'] = array();
1445
		foreach ($config['system']['user'] as & $user) {
1446
			$groupnames = explode(",", $user['groupname']);
1447
			if (in_array($group['name'], $groupnames)) {
1448
				$group['member'][] = $user['uid'];
1449
			}
1450
		}
1451
	}
1452

    
1453
	/* reset user group information */
1454
	foreach ($config['system']['user'] as & $user) {
1455
		unset($user['groupname']);
1456
		$all['member'][] = $user['uid'];
1457
	}
1458

    
1459
	/* reset group scope information */
1460
	foreach ($config['system']['group'] as & $group) {
1461
		if ($group['name'] != $g['admin_group']) {
1462
			$group['scope'] = "user";
1463
		}
1464
	}
1465

    
1466
	/* insert new all group */
1467
	$groups = Array();
1468
	$groups[] = $all;
1469
	$groups = array_merge($config['system']['group'], $groups);
1470
	$config['system']['group'] = $groups;
1471
}
1472

    
1473

    
1474
function upgrade_049_to_050() {
1475
	global $config;
1476

    
1477
	if (!is_array($config['system']['user'])) {
1478
		$config['system']['user'] = array();
1479
	}
1480
	/* update user privileges */
1481
	foreach ($config['system']['user'] as & $user) {
1482
		$privs = array();
1483
		if (!is_array($user['priv'])) {
1484
			unset($user['priv']);
1485
			continue;
1486
		}
1487
		foreach ($user['priv'] as $priv) {
1488
			switch ($priv['id']) {
1489
				case "hasshell":
1490
					$privs[] = "user-shell-access";
1491
					break;
1492
				case "copyfiles":
1493
					$privs[] = "user-copy-files";
1494
					break;
1495
			}
1496
		}
1497
		$user['priv'] = $privs;
1498
	}
1499

    
1500
	/* update group privileges */
1501
	foreach ($config['system']['group'] as & $group) {
1502
		$privs = array();
1503
		if (!is_array($group['pages'])) {
1504
			unset($group['pages']);
1505
			continue;
1506
		}
1507
		foreach ($group['pages'] as $page) {
1508
			$priv = map_page_privname($page);
1509
			if ($priv) {
1510
				$privs[] = $priv;
1511
			}
1512
		}
1513
		unset($group['pages']);
1514
		$group['priv'] = $privs;
1515
	}
1516

    
1517
	/* sync all local account information */
1518
	local_sync_accounts();
1519
}
1520

    
1521

    
1522
function upgrade_050_to_051() {
1523
	global $config;
1524
	$pconfig = array();
1525
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1526
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1527
	$pconfig['value'] = "1";
1528
	$config['sysctl']['item'][] = $pconfig;
1529
	$pconfig = array();
1530
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1531
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1532
	$pconfig['value'] = "0";
1533
	$config['sysctl']['item'][] = $pconfig;
1534

    
1535
	if (isset($config['bridge'])) {
1536
		unset($config['bridge']);
1537
	}
1538

    
1539
	$convert_bridges = false;
1540
	foreach ($config['interfaces'] as $intf) {
1541
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1542
			$config['bridges'] = array();
1543
			$config['bridges']['bridged'] = array();
1544
			$convert_bridges = true;
1545
			break;
1546
		}
1547
	}
1548
	if ($convert_bridges == true) {
1549
		$i = 0;
1550
		foreach ($config['interfaces'] as $ifr => &$intf) {
1551
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1552
				$nbridge = array();
1553
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1554
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1555
				$nbridge['bridgeif'] = "bridge{$i}";
1556
				$config['bridges']['bridged'][] = $nbridge;
1557
				unset($intf['bridge']);
1558
				$i++;
1559
			}
1560
		}
1561
	}
1562
}
1563

    
1564

    
1565
function upgrade_051_to_052() {
1566
	global $config;
1567
	$config['openvpn'] = array();
1568
	if (!is_array($config['ca'])) {
1569
		$config['ca'] = array();
1570
	}
1571
	if (!is_array($config['cert'])) {
1572
		$config['cert'] = array();
1573
	}
1574

    
1575
	$vpnid = 1;
1576

    
1577
	/* openvpn server configurations */
1578
	if (is_array($config['installedpackages']['openvpnserver'])) {
1579
		$config['openvpn']['openvpn-server'] = array();
1580

    
1581
		$index = 1;
1582
		foreach ($config['installedpackages']['openvpnserver']['config'] as $server) {
1583

    
1584
			if (!is_array($server)) {
1585
				continue;
1586
			}
1587

    
1588
			if ($server['auth_method'] == "pki") {
1589

    
1590
				/* create ca entry */
1591
				$ca = array();
1592
				$ca['refid'] = uniqid();
1593
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1594
				$ca['crt'] = $server['ca_cert'];
1595
				$config['ca'][] = $ca;
1596

    
1597
				/* create ca reference */
1598
				unset($server['ca_cert']);
1599
				$server['caref'] = $ca['refid'];
1600

    
1601
				/* create a crl entry if needed */
1602
				if (!empty($server['crl'][0])) {
1603
					$crl = array();
1604
					$crl['refid'] = uniqid();
1605
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1606
					$crl['caref'] = $ca['refid'];
1607
					$crl['text'] = $server['crl'][0];
1608
					if (!is_array($config['crl'])) {
1609
						$config['crl'] = array();
1610
					}
1611
					$config['crl'][] = $crl;
1612
					$server['crlref'] = $crl['refid'];
1613
				}
1614
				unset($server['crl']);
1615

    
1616
				/* create cert entry */
1617
				$cert = array();
1618
				$cert['refid'] = uniqid();
1619
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1620
				$cert['crt'] = $server['server_cert'];
1621
				$cert['prv'] = $server['server_key'];
1622
				$config['cert'][] = $cert;
1623

    
1624
				/* create cert reference */
1625
				unset($server['server_cert']);
1626
				unset($server['server_key']);
1627
				$server['certref'] = $cert['refid'];
1628

    
1629
				$index++;
1630
			}
1631

    
1632
			/* determine operational mode */
1633
			if ($server['auth_method'] == 'pki') {
1634
				if ($server['nopool']) {
1635
					$server['mode'] = "p2p_tls";
1636
				} else {
1637
					$server['mode'] = "server_tls";
1638
				}
1639
			} else {
1640
				$server['mode'] = "p2p_shared_key";
1641
			}
1642
			unset($server['auth_method']);
1643

    
1644
			/* modify configuration values */
1645
			$server['dh_length'] = 1024;
1646
			unset($server['dh_params']);
1647
			if (!$server['interface']) {
1648
				$server['interface'] = 'any';
1649
			}
1650
			$server['tunnel_network'] = $server['addresspool'];
1651
			unset($server['addresspool']);
1652
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1653
				$server['compression'] = "on";
1654
				unset($server['use_lzo']);
1655
			}
1656
			if ($server['nopool']) {
1657
				$server['pool_enable'] = false;
1658
			} else {
1659
				$server['pool_enable'] = "yes";
1660
			}
1661
			unset($server['nopool']);
1662
			$server['dns_domain'] = $server['dhcp_domainname'];
1663
			unset($server['dhcp_domainname']);
1664

    
1665
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1666
			$d=1;
1667
			foreach ($tmparr as $tmpa) {
1668
				$server["dns_server{$d}"] = $tmpa;
1669
				$d++;
1670
			}
1671
			unset($server['dhcp_dns']);
1672

    
1673
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1674
			$d=1;
1675
			foreach ($tmparr as $tmpa) {
1676
				$server["ntp_server{$d}"] = $tmpa;
1677
				$d++;
1678
			}
1679
			unset($server['dhcp_ntp']);
1680

    
1681
			if ($server['dhcp_nbtdisable']) {
1682
				$server['netbios_enable'] = false;
1683
			} else {
1684
				$server['netbios_enable'] = "yes";
1685
			}
1686
			unset($server['dhcp_nbtdisable']);
1687
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1688
			unset($server['dhcp_nbttype']);
1689
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1690
			unset($server['dhcp_nbtscope']);
1691

    
1692
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1693
			$d=1;
1694
			foreach ($tmparr as $tmpa) {
1695
				$server["nbdd_server{$d}"] = $tmpa;
1696
				$d++;
1697
			}
1698
			unset($server['dhcp_nbdd']);
1699

    
1700
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1701
			$d=1;
1702
			foreach ($tmparr as $tmpa) {
1703
				$server["wins_server{$d}"] = $tmpa;
1704
				$d++;
1705
			}
1706
			unset($server['dhcp_wins']);
1707

    
1708
			if (!empty($server['disable'])) {
1709
				$server['disable'] = true;
1710
			} else {
1711
				unset($server['disable']);
1712
			}
1713

    
1714
			/* allocate vpnid */
1715
			$server['vpnid'] = $vpnid++;
1716

    
1717
			if (!empty($server['custom_options'])) {
1718
				$cstmopts = array();
1719
				$tmpcstmopts = explode(";", $server['custom_options']);
1720
				$assigned_if = "";
1721
				$tmpstr = "";
1722
				foreach ($tmpcstmopts as $tmpcstmopt) {
1723
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1724
					if (substr($tmpstr, 0, 6) == "devtun") {
1725
						$assigned_if = substr($tmpstr, 3);
1726
						continue;
1727
					} else if (substr($tmpstr, 0, 5) == "local") {
1728
						$localip = substr($tmpstr, 5);
1729
						$server['ipaddr'] = str_replace("\n", "", $localip);
1730
					} else {
1731
						$cstmopts[] = $tmpcstmopt;
1732
					}
1733
				}
1734
				$server['custom_options'] = implode(";", $cstmopts);
1735
				if (!empty($assigned_if)) {
1736
					foreach ($config['interfaces'] as $iface => $cfgif) {
1737
						if ($cfgif['if'] == $assigned_if) {
1738
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1739
							break;
1740
						}
1741
					}
1742
				}
1743
			}
1744

    
1745
			$config['openvpn']['openvpn-server'][] = $server;
1746
		}
1747
		unset($config['installedpackages']['openvpnserver']);
1748
	}
1749

    
1750
	/* openvpn client configurations */
1751
	if (is_array($config['installedpackages']['openvpnclient'])) {
1752
		$config['openvpn']['openvpn-client'] = array();
1753

    
1754
		$index = 1;
1755
		foreach ($config['installedpackages']['openvpnclient']['config'] as $client) {
1756

    
1757
			if (!is_array($client)) {
1758
				continue;
1759
			}
1760

    
1761
			if ($client['auth_method'] == "pki") {
1762

    
1763
				/* create ca entry */
1764
				$ca = array();
1765
				$ca['refid'] = uniqid();
1766
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1767
				$ca['crt'] = $client['ca_cert'];
1768
				$ca['crl'] = $client['crl'];
1769
				$config['ca'][] = $ca;
1770

    
1771
				/* create ca reference */
1772
				unset($client['ca_cert']);
1773
				unset($client['crl']);
1774
				$client['caref'] = $ca['refid'];
1775

    
1776
				/* create cert entry */
1777
				$cert = array();
1778
				$cert['refid'] = uniqid();
1779
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1780
				$cert['crt'] = $client['client_cert'];
1781
				$cert['prv'] = $client['client_key'];
1782
				$config['cert'][] = $cert;
1783

    
1784
				/* create cert reference */
1785
				unset($client['client_cert']);
1786
				unset($client['client_key']);
1787
				$client['certref'] = $cert['refid'];
1788

    
1789
				$index++;
1790
			}
1791

    
1792
			/* determine operational mode */
1793
			if ($client['auth_method'] == 'pki') {
1794
				$client['mode'] = "p2p_tls";
1795
			} else {
1796
				$client['mode'] = "p2p_shared_key";
1797
			}
1798
			unset($client['auth_method']);
1799

    
1800
			/* modify configuration values */
1801
			if (!$client['interface']) {
1802
				$client['interface'] = 'wan';
1803
			}
1804
			$client['tunnel_network'] = $client['interface_ip'];
1805
			unset($client['interface_ip']);
1806
			$client['server_addr'] = $client['serveraddr'];
1807
			unset($client['serveraddr']);
1808
			$client['server_port'] = $client['serverport'];
1809
			unset($client['serverport']);
1810
			$client['proxy_addr'] = $client['poxy_hostname'];
1811
			unset($client['proxy_addr']);
1812
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1813
				$client['compression'] = "on";
1814
				unset($client['use_lzo']);
1815
			}
1816
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1817
			unset($client['infiniteresolvretry']);
1818

    
1819
			/* allocate vpnid */
1820
			$client['vpnid'] = $vpnid++;
1821

    
1822
			if (!empty($client['custom_options'])) {
1823
				$cstmopts = array();
1824
				$tmpcstmopts = explode(";", $client['custom_options']);
1825
				$assigned_if = "";
1826
				$tmpstr = "";
1827
				foreach ($tmpcstmopts as $tmpcstmopt) {
1828
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1829
					if (substr($tmpstr, 0, 6) == "devtun") {
1830
						$assigned_if = substr($tmpstr, 3);
1831
						continue;
1832
					} else if (substr($tmpstr, 0, 5) == "local") {
1833
						$localip = substr($tmpstr, 5);
1834
						$client['ipaddr'] = str_replace("\n", "", $localip);
1835
					} else {
1836
						$cstmopts[] = $tmpcstmopt;
1837
					}
1838
				}
1839
				$client['custom_options'] = implode(";", $cstmopts);
1840
				if (!empty($assigned_if)) {
1841
					foreach ($config['interfaces'] as $iface => $cfgif) {
1842
						if ($cfgif['if'] == $assigned_if) {
1843
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1844
							break;
1845
						}
1846
					}
1847
				}
1848
			}
1849

    
1850
			if (!empty($client['disable'])) {
1851
				$client['disable'] = true;
1852
			} else {
1853
				unset($client['disable']);
1854
			}
1855

    
1856
			$config['openvpn']['openvpn-client'][] = $client;
1857
		}
1858

    
1859
		unset($config['installedpackages']['openvpnclient']);
1860
	}
1861

    
1862
	/* openvpn client specific configurations */
1863
	if (is_array($config['installedpackages']['openvpncsc'])) {
1864
		$config['openvpn']['openvpn-csc'] = array();
1865

    
1866
		foreach ($config['installedpackages']['openvpncsc']['config'] as $csc) {
1867

    
1868
			if (!is_array($csc)) {
1869
				continue;
1870
			}
1871

    
1872
			/* modify configuration values */
1873
			$csc['common_name'] = $csc['commonname'];
1874
			unset($csc['commonname']);
1875
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1876
			unset($csc['ifconfig_push']);
1877
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1878
			unset($csc['dhcp_domainname']);
1879

    
1880
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1881
			$d=1;
1882
			foreach ($tmparr as $tmpa) {
1883
				$csc["dns_server{$d}"] = $tmpa;
1884
				$d++;
1885
			}
1886
			unset($csc['dhcp_dns']);
1887

    
1888
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1889
			$d=1;
1890
			foreach ($tmparr as $tmpa) {
1891
				$csc["ntp_server{$d}"] = $tmpa;
1892
				$d++;
1893
			}
1894
			unset($csc['dhcp_ntp']);
1895

    
1896
			if ($csc['dhcp_nbtdisable']) {
1897
				$csc['netbios_enable'] = false;
1898
			} else {
1899
				$csc['netbios_enable'] = "yes";
1900
			}
1901
			unset($csc['dhcp_nbtdisable']);
1902
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1903
			unset($csc['dhcp_nbttype']);
1904
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1905
			unset($csc['dhcp_nbtscope']);
1906

    
1907
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1908
			$d=1;
1909
			foreach ($tmparr as $tmpa) {
1910
				$csc["nbdd_server{$d}"] = $tmpa;
1911
				$d++;
1912
			}
1913
			unset($csc['dhcp_nbdd']);
1914

    
1915
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1916
			$d=1;
1917
			foreach ($tmparr as $tmpa) {
1918
				$csc["wins_server{$d}"] = $tmpa;
1919
				$d++;
1920
			}
1921
			unset($csc['dhcp_wins']);
1922

    
1923
			if (!empty($csc['disable'])) {
1924
				$csc['disable'] = true;
1925
			} else {
1926
				unset($csc['disable']);
1927
			}
1928

    
1929
			$config['openvpn']['openvpn-csc'][] = $csc;
1930
		}
1931

    
1932
		unset($config['installedpackages']['openvpncsc']);
1933
	}
1934

    
1935
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1936
	    count($config['openvpn']['openvpn-client']) > 0) {
1937
		$ovpnrule = array();
1938
		$ovpnrule['type'] = "pass";
1939
		$ovpnrule['interface'] = "openvpn";
1940
		$ovpnrule['statetype'] = "keep state";
1941
		$ovpnrule['source'] = array();
1942
		$ovpnrule['destination'] = array();
1943
		$ovpnrule['source']['any'] = true;
1944
		$ovpnrule['destination']['any'] = true;
1945
		$ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1946
		$config['filter']['rule'][] = $ovpnrule;
1947
	}
1948

    
1949
	/*
1950
		* FIXME: hack to keep things working with no installedpackages
1951
		* or carp array in the configuration data.
1952
		*/
1953
	if (!is_array($config['installedpackages'])) {
1954
		$config['installedpackages'] = array();
1955
	}
1956
	if (!is_array($config['installedpackages']['carp'])) {
1957
		$config['installedpackages']['carp'] = array();
1958
	}
1959

    
1960
}
1961

    
1962

    
1963
function upgrade_052_to_053() {
1964
	global $config;
1965
	if (!is_array($config['ca'])) {
1966
		$config['ca'] = array();
1967
	}
1968
	if (!is_array($config['cert'])) {
1969
		$config['cert'] = array();
1970
	}
1971

    
1972
	/* migrate advanced admin page webui ssl to certificate manager */
1973
	if ($config['system']['webgui']['certificate'] &&
1974
	    $config['system']['webgui']['private-key']) {
1975

    
1976
		/* create cert entry */
1977
		$cert = array();
1978
		$cert['refid'] = uniqid();
1979
		$cert['descr'] = "webConfigurator SSL Certificate";
1980
		$cert['crt'] = $config['system']['webgui']['certificate'];
1981
		$cert['prv'] = $config['system']['webgui']['private-key'];
1982
		$config['cert'][] = $cert;
1983

    
1984
		/* create cert reference */
1985
		unset($config['system']['webgui']['certificate']);
1986
		unset($config['system']['webgui']['private-key']);
1987
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1988
	}
1989

    
1990
	/* migrate advanced admin page ssh keys to user manager */
1991
	if ($config['system']['ssh']['authorizedkeys']) {
1992
		$admin_user =& getUserEntryByUID(0);
1993
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1994
		unset($config['system']['ssh']['authorizedkeys']);
1995
	}
1996
}
1997

    
1998

    
1999
function upgrade_053_to_054() {
2000
	global $config;
2001
	if (is_array($config['load_balancer']['lbpool'])) {
2002
		$lbpool_arr = $config['load_balancer']['lbpool'];
2003
		$lbpool_srv_arr = array();
2004
		$gateway_group_arr = array();
2005
		$gateways = return_gateways_array();
2006
		$group_name_changes = array();
2007
		if (!is_array($config['gateways']['gateway_item'])) {
2008
			$config['gateways']['gateway_item'] = array();
2009
		}
2010

    
2011
		$a_gateways =& $config['gateways']['gateway_item'];
2012
		foreach ($lbpool_arr as $lbpool) {
2013
			if ($lbpool['type'] == "gateway") {
2014
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
2015
				$group_name = preg_replace("/[^A-Za-z0-9]/", "", $lbpool['name']);
2016
				// If we made and changes, check for collisions and note the change.
2017
				if ($group_name != $lbpool['name']) {
2018
					// Make sure the name isn't already in use.
2019
					foreach ($gateway_group_arr as $gwg) {
2020
						// If the name is in use, add some random bits to avoid collision.
2021
						if ($gwg['name'] == $group_name) {
2022
							$group_name .= uniqid();
2023
						}
2024
					}
2025
					$group_name_changes[$lbpool['name']] = $group_name;
2026
				}
2027
				$gateway_group['name'] = $group_name;
2028
				$gateway_group['descr'] = $lbpool['descr'];
2029
				$gateway_group['trigger'] = "down";
2030
				$gateway_group['item'] = array();
2031
				$i = 0;
2032
				foreach ($lbpool['servers'] as $member) {
2033
					$split = explode("|", $member);
2034
					$interface = $split[0];
2035
					$monitor = $split[1];
2036
					/* on static upgraded configuration we automatically prepend GW_ */
2037
					$static_name = "GW_" . strtoupper($interface);
2038
					if (is_ipaddr($monitor)) {
2039
						foreach ($a_gateways as & $gw) {
2040
							if ($gw['name'] == $static_name) {
2041
								$gw['monitor'] = $monitor;
2042
							}
2043
						}
2044
					}
2045

    
2046
					/* on failover increment tier. Else always assign 1 */
2047
					if ($lbpool['behaviour'] == "failover") {
2048
						$i++;
2049
					} else {
2050
						$i = 1;
2051
					}
2052
					$gateway_group['item'][] = "$static_name|$i";
2053
				}
2054
				$gateway_group_arr[] = $gateway_group;
2055
			} else {
2056
				$lbpool_srv_arr[] = $lbpool;
2057
			}
2058
		}
2059
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2060
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2061
	}
2062
	// Unset lbpool if we no longer have any server pools
2063
	if (count($lbpool_srv_arr) == 0) {
2064
		if (empty($config['load_balancer'])) {
2065
			unset($config['load_balancer']);
2066
		} else {
2067
			if (isset($config['load_balancer']['lbpool'])) {
2068
				unset($config['load_balancer']['lbpool']);
2069
			}
2070
		}
2071
	} else {
2072
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2073
	}
2074
	// Only set the gateway group array if we converted any
2075
	if (count($gateway_group_arr) != 0) {
2076
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2077
		// Update any rules that had a gateway change, if any.
2078
		if (count($group_name_changes) > 0) {
2079
			foreach ($config['filter']['rule'] as & $rule) {
2080
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes)) {
2081
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
2082
				}
2083
			}
2084
		}
2085
	}
2086
}
2087

    
2088

    
2089
function upgrade_054_to_055() {
2090
	global $config;
2091
	global $g;
2092

    
2093
	/* RRD files changed for quality, traffic and packets graphs */
2094
	//ini_set("max_execution_time", "1800");
2095
	/* convert traffic RRD file */
2096
	global $parsedcfg, $listtags;
2097
	$listtags = array("ds", "v", "rra", "row");
2098

    
2099
	$rrddbpath = "/var/db/rrd/";
2100
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2101
	if ($g['platform'] != $g['product_name']) {
2102
		/* restore the databases, if we have one */
2103
		if (restore_rrd()) {
2104
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2105
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2106
		}
2107
	}
2108

    
2109
	$rrdinterval = 60;
2110
	$valid = $rrdinterval * 2;
2111

    
2112
	/* Asume GigE for now */
2113
	$downstream = 125000000;
2114
	$upstream = 125000000;
2115

    
2116
	/* build a list of quality databases */
2117
	/* roundtrip has become delay */
2118
	function divide_delay($delayval) {
2119
		$delayval = floatval($delayval);
2120
		$delayval = ($delayval / 1000);
2121
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2122
		return $delayval;
2123
	}
2124
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2125
	$databases = array();
2126
	if (!file_exists($rrddbpath)) {
2127
		@mkdir($rrddbpath);
2128
	}
2129
	chdir($rrddbpath);
2130
	$databases = glob("*-quality.rrd");
2131
	rsort($databases);
2132
	foreach ($databases as $database) {
2133
		$xmldump = "{$database}.old.xml";
2134
		$xmldumpnew = "{$database}.new.xml";
2135

    
2136
		if (platform_booting()) {
2137
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2138
		}
2139
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2140

    
2141
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2142
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2143
		$rrdold = $rrdold['rrd'];
2144

    
2145
		$i = 0;
2146
		foreach ($rrdold['rra'] as $rra) {
2147
			$l = 0;
2148
			foreach ($rra['database']['row'] as $row) {
2149
				$vnew = divide_delay($row['v'][1]);
2150
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2151
				$l++;
2152
			}
2153
			$i++;
2154
		}
2155

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

    
2159
		unset($rrdold);
2160
		@unlink("{$g['tmp_path']}/{$xmldump}");
2161
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2162
	}
2163

    
2164
	/* build a list of traffic and packets databases */
2165
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2166
	rsort($databases);
2167
	foreach ($databases as $database) {
2168
		$databasetmp = "{$database}.tmp";
2169
		$xmldump = "{$database}.old.xml";
2170
		$xmldumptmp = "{$database}.tmp.xml";
2171
		$xmldumpnew = "{$database}.new.xml";
2172

    
2173
		if (platform_booting()) {
2174
			echo "Migrate RRD database {$database} to new format \n";
2175
		}
2176
		/* rename DS source */
2177
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2178
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2179

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

    
2183
		/* create new rrd database file */
2184
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2185
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2186
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2187
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2188
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2189
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2190
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2191
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2192
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2193

    
2194
		create_new_rrd("$rrdcreate");
2195
		/* create temporary xml from new RRD */
2196
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2197

    
2198
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2199
		$rrdold = $rrdold['rrd'];
2200

    
2201
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2202
		$rrdnew = $rrdnew['rrd'];
2203

    
2204
		/* remove any MAX RRA's. Not needed for traffic. */
2205
		$i = 0;
2206
		foreach ($rrdold['rra'] as $rra) {
2207
			if (trim($rra['cf']) == "MAX") {
2208
				unset($rrdold['rra'][$i]);
2209
			}
2210
			$i++;
2211
		}
2212

    
2213
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2214
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2215
		/* we now have the rrd with the new fields, adjust the size now. */
2216
		/* RRA 2 is 60 minutes, RRA 3 is 720 minutes */
2217
		mwexec("/bin/sync");
2218
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 2 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2219
		mwexec("/bin/sync");
2220
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2221
		unset($rrdxmlarray);
2222
		@unlink("{$g['tmp_path']}/{$xmldump}");
2223
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2224
	}
2225
	if (!platform_booting()) {
2226
		enable_rrd_graphing();
2227
	}
2228
	/* Let's save the RRD graphs after we run enable RRD graphing */
2229
	/* The function will restore the rrd.tgz so we will save it after */
2230
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2231
	unlink_if_exists("{$g['vardb_path']}/rrd/*.xml");
2232
	if (platform_booting()) {
2233
		echo "Updating configuration...";
2234
	}
2235
}
2236

    
2237

    
2238
function upgrade_055_to_056() {
2239
	global $config;
2240

    
2241
	if (!is_array($config['ca'])) {
2242
		$config['ca'] = array();
2243
	}
2244
	if (!is_array($config['cert'])) {
2245
		$config['cert'] = array();
2246
	}
2247

    
2248
	/* migrate ipsec ca's to cert manager */
2249
	if (is_array($config['ipsec']['cacert'])) {
2250
		foreach ($config['ipsec']['cacert'] as & $cacert) {
2251
			$ca = array();
2252
			$ca['refid'] = uniqid();
2253
			if (is_array($cacert['cert'])) {
2254
				$ca['crt'] = $cacert['cert'][0];
2255
			} else {
2256
				$ca['crt'] = $cacert['cert'];
2257
			}
2258
			$ca['descr'] = $cacert['ident'];
2259
			$config['ca'][] = $ca;
2260
		}
2261
		unset($config['ipsec']['cacert']);
2262
	}
2263

    
2264
	/* migrate phase1 certificates to cert manager */
2265
	if (is_array($config['ipsec']['phase1'])) {
2266
		foreach ($config['ipsec']['phase1'] as & $ph1ent) {
2267
			$cert = array();
2268
			$cert['refid'] = uniqid();
2269
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2270
			if (is_array($ph1ent['cert'])) {
2271
				$cert['crt'] = $ph1ent['cert'][0];
2272
			} else {
2273
				$cert['crt'] = $ph1ent['cert'];
2274
			}
2275
			$cert['prv'] = $ph1ent['private-key'];
2276
			$config['cert'][] = $cert;
2277
			$ph1ent['certref'] = $cert['refid'];
2278
			if ($ph1ent['cert']) {
2279
				unset($ph1ent['cert']);
2280
			}
2281
			if ($ph1ent['private-key']) {
2282
				unset($ph1ent['private-key']);
2283
			}
2284
			if ($ph1ent['peercert']) {
2285
				unset($ph1ent['peercert']);
2286
			}
2287
		}
2288
	}
2289
}
2290

    
2291

    
2292
function upgrade_056_to_057() {
2293
	global $config;
2294

    
2295
	if (!is_array($config['system']['user'])) {
2296
		$config['system']['user'] = array();
2297
	}
2298
	/* migrate captivate portal to user manager */
2299
	if (is_array($config['captiveportal']['user'])) {
2300
		foreach ($config['captiveportal']['user'] as $user) {
2301
			// avoid user conflicts
2302
			$found = false;
2303
			foreach ($config['system']['user'] as $userent) {
2304
				if ($userent['name'] == $user['name']) {
2305
					$found = true;
2306
					break;
2307
				}
2308
			}
2309
			if ($found) {
2310
				continue;
2311
			}
2312
			$user['scope'] = "user";
2313
			if (isset($user['expirationdate'])) {
2314
				$user['expires'] = $user['expirationdate'];
2315
				unset($user['expirationdate']);
2316
			}
2317
			if (isset($user['password'])) {
2318
				$user['md5-hash'] = $user['password'];
2319
				unset($user['password']);
2320
			}
2321
			$user['uid'] = $config['system']['nextuid']++;
2322
			$config['system']['user'][] = $user;
2323
		}
2324
		unset($config['captiveportal']['user']);
2325
	}
2326
}
2327

    
2328
function upgrade_057_to_058() {
2329
	global $config;
2330
	/* set all phase2 entries to tunnel mode */
2331
	if (is_array($config['ipsec']['phase2'])) {
2332
		foreach ($config['ipsec']['phase2'] as & $ph2ent) {
2333
			$ph2ent['mode'] = 'tunnel';
2334
		}
2335
	}
2336
}
2337

    
2338
function upgrade_058_to_059() {
2339
	global $config;
2340

    
2341
	if (is_array($config['schedules']['schedule'])) {
2342
		foreach ($config['schedules']['schedule'] as & $schedl) {
2343
			$schedl['schedlabel'] = uniqid();
2344
		}
2345
	}
2346
}
2347

    
2348
function upgrade_059_to_060() {
2349
	global $config;
2350
	require_once("/etc/inc/certs.inc");
2351
	if (is_array($config['ca'])) {
2352
		/* Locate issuer for all CAs */
2353
		foreach ($config['ca'] as & $ca) {
2354
			$subject = cert_get_subject($ca['crt']);
2355
			$issuer = cert_get_issuer($ca['crt']);
2356
			if ($issuer <> $subject) {
2357
				$issuer_crt =& lookup_ca_by_subject($issuer);
2358
				if ($issuer_crt) {
2359
					$ca['caref'] = $issuer_crt['refid'];
2360
				}
2361
			}
2362
		}
2363

    
2364
		/* Locate issuer for all certificates */
2365
		if (is_array($config['cert'])) {
2366
			foreach ($config['cert'] as & $cert) {
2367
				$subject = cert_get_subject($cert['crt']);
2368
				$issuer = cert_get_issuer($cert['crt']);
2369
				if ($issuer <> $subject) {
2370
					$issuer_crt =& lookup_ca_by_subject($issuer);
2371
					if ($issuer_crt) {
2372
						$cert['caref'] = $issuer_crt['refid'];
2373
					}
2374
				}
2375
			}
2376
		}
2377
	}
2378
}
2379

    
2380
function upgrade_060_to_061() {
2381
	global $config;
2382

    
2383
	if (is_array($config['interfaces']['wan'])) {
2384
		$config['interfaces']['wan']['enable'] = true;
2385
	}
2386
	if (is_array($config['interfaces']['lan'])) {
2387
		$config['interfaces']['lan']['enable'] = true;
2388
	}
2389

    
2390
	/* On 1.2.3 the "mtu" field adjusted MSS.
2391
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2392
	   See redmine ticket #1886
2393
	*/
2394
	foreach ($config['interfaces'] as $ifr => &$intf) {
2395
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2396
			$intf['mss'] = $intf['mtu'];
2397
			unset($intf['mtu']);
2398
		}
2399
	}
2400
}
2401

    
2402
function upgrade_061_to_062() {
2403
	global $config;
2404

    
2405
	/* Convert NAT port forwarding rules */
2406
	if (is_array($config['nat']['rule'])) {
2407
		$a_nat = &$config['nat']['rule'];
2408

    
2409
		foreach ($a_nat as &$natent) {
2410
			$natent['disabled'] = false;
2411
			$natent['nordr']    = false;
2412

    
2413
			$natent['source'] = array(
2414
				"not"     => false,
2415
				"any"     => true,
2416
				"port"    => ""
2417
			);
2418

    
2419
			$natent['destination'] = array(
2420
				"not"     => false,
2421
				"address" => $natent['external-address'],
2422
				"port"    => $natent['external-port']
2423
			);
2424

    
2425
			if (empty($natent['destination']['address'])) {
2426
				unset($natent['destination']['address']);
2427
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2428
			} else if ($natent['destination']['address'] == 'any') {
2429
				unset($natent['destination']['address']);
2430
				$natent['destination']['any'] = true;
2431
			}
2432

    
2433
			unset($natent['external-address']);
2434
			unset($natent['external-port']);
2435
		}
2436

    
2437
		unset($natent);
2438
	}
2439
}
2440

    
2441
function upgrade_062_to_063() {
2442
	/* Upgrade legacy Themes to the new pfsense_ng */
2443
	// Not supported in 2.3+
2444

    
2445
}
2446

    
2447
function upgrade_063_to_064() {
2448
	global $config;
2449
	$j = 0;
2450
	$ifcfg = &$config['interfaces'];
2451

    
2452
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
2453
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2454
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2455
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2456
			$j++;
2457
			if (isset($ppp['port'])) {
2458
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2459
				unset($config['ppps']['ppp'][$pppid]['port']);
2460
			}
2461
			if (!isset($ppp['type'])) {
2462
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2463
			}
2464
			if (isset($ppp['defaultgw'])) {
2465
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2466
			}
2467
		}
2468
	}
2469

    
2470
	if (!is_array($config['ppps']['ppp'])) {
2471
		$config['ppps']['ppp'] = array();
2472
	}
2473
	$a_ppps = &$config['ppps']['ppp'];
2474

    
2475
	foreach ($ifcfg as $ifname => $ifinfo) {
2476
		$ppp = array();
2477
		// For pppoe conversion
2478
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp") {
2479
			if (isset($ifinfo['ptpid'])) {
2480
				continue;
2481
			}
2482
			$ppp['ptpid'] = $j;
2483
			$ppp['type'] = $ifinfo['ipaddr'];
2484
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2485
			$ppp['ports'] = $ifinfo['if'];
2486
			if ($ifinfo['ipaddr'] == "pppoe") {
2487
				$ppp['username'] = $ifinfo['pppoe_username'];
2488
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2489
			}
2490
			if ($ifinfo['ipaddr'] == "pptp") {
2491
				$ppp['username'] = $ifinfo['pptp_username'];
2492
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2493
			}
2494

    
2495
			if (isset($ifinfo['provider'])) {
2496
				$ppp['provider'] = $ifinfo['provider'];
2497
			}
2498
			if (isset($ifinfo['ondemand'])) {
2499
				$ppp['ondemand'] = true;
2500
			}
2501
			if (isset($ifinfo['timeout'])) {
2502
				$ppp['idletimeout'] = $ifinfo['timeout'];
2503
			}
2504
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])) {
2505
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2506
				if (is_array($config['cron']['item'])) {
2507
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2508
						$item = $config['cron']['item'][$i];
2509
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false) {
2510
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2511
						}
2512
					}
2513
				}
2514
			}
2515
			if (isset($ifinfo['local'])) {
2516
				$ppp['localip'] = $ifinfo['local'];
2517
			}
2518
			if (isset($ifinfo['subnet'])) {
2519
				$ppp['subnet'] = $ifinfo['subnet'];
2520
			}
2521
			if (isset($ifinfo['remote'])) {
2522
				$ppp['gateway'] = $ifinfo['remote'];
2523
			}
2524

    
2525
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2526
			$j++;
2527

    
2528
			unset($ifcfg[$ifname]['pppoe_username']);
2529
			unset($ifcfg[$ifname]['pppoe_password']);
2530
			unset($ifcfg[$ifname]['provider']);
2531
			unset($ifcfg[$ifname]['ondemand']);
2532
			unset($ifcfg[$ifname]['timeout']);
2533
			unset($ifcfg[$ifname]['pppoe_reset']);
2534
			unset($ifcfg[$ifname]['pppoe_preset']);
2535
			unset($ifcfg[$ifname]['pppoe']);
2536
			unset($ifcfg[$ifname]['pptp_username']);
2537
			unset($ifcfg[$ifname]['pptp_password']);
2538
			unset($ifcfg[$ifname]['local']);
2539
			unset($ifcfg[$ifname]['subnet']);
2540
			unset($ifcfg[$ifname]['remote']);
2541

    
2542
			$a_ppps[] = $ppp;
2543

    
2544
		}
2545
	}
2546
}
2547

    
2548
function upgrade_064_to_065() {
2549
	/* Disable TSO and LRO in upgraded configs */
2550
	global $config;
2551
	$config['system']['disablesegmentationoffloading'] = true;
2552
	$config['system']['disablelargereceiveoffloading'] = true;
2553
}
2554

    
2555
function upgrade_065_to_066() {
2556
	global $config;
2557

    
2558
	$dhcrelaycfg =& $config['dhcrelay'];
2559

    
2560
	if (is_array($dhcrelaycfg)) {
2561
		$dhcrelayifs = array();
2562
		$foundifs = false;
2563
		/* DHCPRelay enabled on any interfaces? */
2564
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2565
			if (isset($dhcrelayifconf['enable'])) {
2566
				$dhcrelayifs[] = $dhcrelayif;
2567
				unset($dhcrelaycfg['dhcrelayif']);
2568
				$foundifs = true;
2569
			}
2570
		}
2571
		if ($foundifs == true) {
2572
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2573
		}
2574
	}
2575
}
2576

    
2577
function upgrade_066_to_067() {
2578
	global $config;
2579
	if (isset($config['system']['ca'])) {
2580
		$config['ca'] = $config['system']['ca'];
2581
		unset($config['system']['ca']);
2582
	}
2583
	if (isset($config['system']['cert'])) {
2584
		$config['cert'] = $config['system']['cert'];
2585
		unset($config['system']['cert']);
2586
	}
2587
}
2588

    
2589
function upgrade_067_to_068() {
2590
	global $config;
2591

    
2592
	if (!empty($config['pppoe'])) {
2593
		$config['pppoes'] = array();
2594
		$config['pppoes']['pppoe'] = array();
2595
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2596

    
2597
		if (is_array($config['pppoe']['user'])) {
2598
			$username = array();
2599
			foreach ($config['pppoe']['user'] as $user) {
2600
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2601
				if ($user['ip']) {
2602
					$usr .= ":{$user['ip']}";
2603
				}
2604
				$username[] = $usr;
2605
			}
2606
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2607
		}
2608
		unset($config['pppoe']);
2609
	}
2610
}
2611

    
2612
function upgrade_068_to_069() {
2613
	global $config;
2614
	if (!is_array($config['system']['user'])) {
2615
		return;
2616
	}
2617
	foreach ($config['system']['user'] as & $user) {
2618
		if (!is_array($user['cert'])) {
2619
			continue;
2620
		}
2621
		$rids = array();
2622
		foreach ($user['cert'] as $id => $cert) {
2623
			if (!isset($cert['descr'])) {
2624
				continue;
2625
			}
2626
			$tcert = $cert;
2627
			// Make sure each cert gets a refid
2628
			if (!isset($tcert['refid'])) {
2629
				$tcert['refid'] = uniqid();
2630
			}
2631
			// Keep the cert references for this user
2632
			$rids[] = $tcert['refid'];
2633
			$config['cert'][] = $tcert;
2634
		}
2635
		// Replace user certs with cert references instead.
2636
		if (count($rids) > 0) {
2637
			$user['cert'] = $rids;
2638
		}
2639
	}
2640
}
2641

    
2642
function upgrade_069_to_070() {
2643
	global $config;
2644

    
2645
	/* Convert NAT 1:1 rules */
2646
	if (is_array($config['nat']['onetoone'])) {
2647
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2648
			if ($natent['subnet'] == 32) {
2649
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2650
			} else {
2651
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2652
			}
2653

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

    
2656
			unset($config['nat']['onetoone'][$nidx]['internal']);
2657
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2658
		}
2659

    
2660
		unset($natent);
2661
	}
2662
}
2663

    
2664
function upgrade_070_to_071() {
2665
	global $config;
2666

    
2667
	if (is_array($config['cron']['item'])) {
2668
		foreach ($config['cron']['item'] as $idx => $cronitem) {
2669
			if (stristr($cronitem['command'], "checkreload.sh")) {
2670
				unset($config['cron']['item'][$idx]);
2671
				break;
2672
			}
2673
		}
2674
	}
2675
}
2676

    
2677
function rename_field(& $section, $oldname, $newname) {
2678
	if (is_array($section)) {
2679
		foreach ($section as & $item) {
2680
			if (is_array($item) && !empty($item[$oldname])) {
2681
				$item[$newname] = $item[$oldname];
2682
			}
2683
			if (is_array($item) && isset($item[$oldname])) {
2684
				unset($item[$oldname]);
2685
			}
2686
		}
2687
	}
2688
}
2689

    
2690
function upgrade_071_to_072() {
2691
	global $config;
2692
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item'])) {
2693
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2694
	}
2695
}
2696

    
2697
function upgrade_072_to_073() {
2698
	global $config;
2699
	if (!is_array($config['load_balancer'])) {
2700
		return;
2701
	}
2702
	if (is_array($config['load_balancer']['monitor_type'])) {
2703
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2704
	}
2705
	if (is_array($config['load_balancer']['lbpool'])) {
2706
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2707
	}
2708
	if (is_array($config['load_balancer']['lbaction'])) {
2709
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2710
	}
2711
	if (is_array($config['load_balancer']['lbprotocol'])) {
2712
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2713
	}
2714
	if (is_array($config['load_balancer']['virtual_server'])) {
2715
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2716
	}
2717
}
2718

    
2719
function upgrade_073_to_074() {
2720
	global $config;
2721
	rename_field($config['system']['user'], 'fullname', 'descr');
2722
}
2723

    
2724
function upgrade_074_to_075() {
2725
	global $config;
2726
	if (is_array($config['ca'])) {
2727
		rename_field($config['ca'], 'name', 'descr');
2728
	}
2729
	if (is_array($config['cert'])) {
2730
		rename_field($config['cert'], 'name', 'descr');
2731
	}
2732
	if (is_array($config['crl'])) {
2733
		rename_field($config['crl'], 'name', 'descr');
2734
	}
2735
}
2736

    
2737
function upgrade_075_to_076() {
2738
	global $config;
2739
	$cron_item = array();
2740
	$cron_item['minute'] = "30";
2741
	$cron_item['hour'] = "12";
2742
	$cron_item['mday'] = "*";
2743
	$cron_item['month'] = "*";
2744
	$cron_item['wday'] = "*";
2745
	$cron_item['who'] = "root";
2746
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2747
	$config['cron']['item'][] = $cron_item;
2748
}
2749

    
2750
function upgrade_076_to_077() {
2751
	global $config;
2752
	foreach ($config['filter']['rule'] as & $rule) {
2753
		if (isset($rule['protocol']) && !empty($rule['protocol'])) {
2754
			$rule['protocol'] = strtolower($rule['protocol']);
2755
		}
2756
	}
2757
}
2758

    
2759
function upgrade_077_to_078() {
2760
	global $config;
2761
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius']) &&
2762
	    !is_array($config['pptpd']['radius']['server'])) {
2763
		$radarr = array();
2764
		$radsvr = array();
2765
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2766
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2767
		$radsvr['port'] = 1812;
2768
		$radsvr['acctport'] = 1813;
2769
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2770
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2771
		if ($radarr['accounting']) {
2772
			$radarr['acct_update'] = $radsvr['ip'];
2773
		}
2774
		$radarr['server'] = $radsvr;
2775
		$config['pptpd']['radius'] = $radarr;
2776
	}
2777
	if (is_array($config['pptpd'])) {
2778
		$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
2779
	}
2780
}
2781
function upgrade_078_to_079() {
2782
	global $g;
2783
	/* Delete old and unused RRD file */
2784
	unlink_if_exists("{$g['vardb_path']}/rrd/captiveportal-totalusers.rrd");
2785
}
2786

    
2787
function upgrade_079_to_080() {
2788
	global $config;
2789

    
2790
	/* Upgrade config in 1.2.3 specifying a username other than admin for syncing. */
2791
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2792
	    is_array($config['installedpackages']['carpsettings']['config'])) {
2793
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2794
		unset($config['system']['username']);
2795
	}
2796
}
2797

    
2798
function upgrade_080_to_081() {
2799
	global $config;
2800
	global $g;
2801
	/* Welcome to the 2.1 migration path */
2802

    
2803
	/* tag all the existing gateways as being IPv4 */
2804
	$i = 0;
2805
	if (is_array($config['gateways']['gateway_item'])) {
2806
		foreach ($config['gateways']['gateway_item'] as $gw) {
2807
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2808
			$i++;
2809
		}
2810
	}
2811

    
2812
	/* RRD files changed for quality, traffic and packets graphs */
2813
	/* convert traffic RRD file */
2814
	global $parsedcfg, $listtags;
2815
	$listtags = array("ds", "v", "rra", "row");
2816

    
2817
	$rrddbpath = "/var/db/rrd/";
2818
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2819

    
2820
	if ($g['platform'] != $g['product_name']) {
2821
		/* restore the databases, if we have one */
2822
		if (restore_rrd()) {
2823
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2824
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2825
		}
2826
	}
2827

    
2828
	$rrdinterval = 60;
2829
	$valid = $rrdinterval * 2;
2830

    
2831
	/* Asume GigE for now */
2832
	$downstream = 125000000;
2833
	$upstream = 125000000;
2834

    
2835
	/* build a list of traffic and packets databases */
2836
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2837
	rsort($databases);
2838
	foreach ($databases as $database) {
2839
		$xmldump = "{$database}.old.xml";
2840
		$xmldumpnew = "{$database}.new.xml";
2841

    
2842
		if (platform_booting()) {
2843
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2844
		}
2845

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

    
2849
		/* search and replace tags to add data sources */
2850
		$ds_search = "<!-- Round Robin Archives -->";
2851
		$ds_arr = array();
2852
		$ds_arr[] = "	<ds>
2853
				<name> inpass6 </name>
2854
				<type> COUNTER </type>
2855
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2856
				<min> 0.0000000000e+00 </min>
2857
				<max> 1.2500000000e+08 </max>
2858

    
2859
				<!-- PDP Status -->
2860
				<last_ds> 0 </last_ds>
2861
				<value> NaN </value>
2862
				<unknown_sec> 3 </unknown_sec>
2863
			</ds>
2864
			";
2865
		$ds_arr[] = "	<ds>
2866
				<name> outpass6 </name>
2867
				<type> COUNTER </type>
2868
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2869
				<min> 0.0000000000e+00 </min>
2870
				<max> 1.2500000000e+08 </max>
2871

    
2872
				<!-- PDP Status -->
2873
				<last_ds> 0 </last_ds>
2874
				<value> NaN </value>
2875
				<unknown_sec> 3 </unknown_sec>
2876
			</ds>
2877
			";
2878
		$ds_arr[] = "	<ds>
2879
				<name> inblock6 </name>
2880
				<type> COUNTER </type>
2881
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2882
				<min> 0.0000000000e+00 </min>
2883
				<max> 1.2500000000e+08 </max>
2884

    
2885
				<!-- PDP Status -->
2886
				<last_ds> 0 </last_ds>
2887
				<value> NaN </value>
2888
				<unknown_sec> 3 </unknown_sec>
2889
			</ds>
2890
			";
2891
		$ds_arr[] = "	<ds>
2892
				<name> outblock6 </name>
2893
				<type> COUNTER </type>
2894
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2895
				<min> 0.0000000000e+00 </min>
2896
				<max> 1.2500000000e+08 </max>
2897

    
2898
				<!-- PDP Status -->
2899
				<last_ds> 0 </last_ds>
2900
				<value> NaN </value>
2901
				<unknown_sec> 3 </unknown_sec>
2902
			</ds>
2903
			";
2904

    
2905
		$cdp_search = "<\/cdp_prep>";
2906
		$cdp_replace = "</cdp_prep>";
2907
		$cdp_arr = array();
2908
		$cdp_arr[] = "			<ds>
2909
					<primary_value> NaN </primary_value>
2910
					<secondary_value> 0.0000000000e+00 </secondary_value>
2911
					<value> NaN </value>
2912
					<unknown_datapoints> 0 </unknown_datapoints>
2913
					</ds>
2914
		";
2915
		$cdp_arr[] = "			<ds>
2916
					<primary_value> NaN </primary_value>
2917
					<secondary_value> 0.0000000000e+00 </secondary_value>
2918
					<value> NaN </value>
2919
					<unknown_datapoints> 0 </unknown_datapoints>
2920
					</ds>
2921
		";
2922
		$cdp_arr[] = "			<ds>
2923
					<primary_value> NaN </primary_value>
2924
					<secondary_value> 0.0000000000e+00 </secondary_value>
2925
					<value> NaN </value>
2926
					<unknown_datapoints> 0 </unknown_datapoints>
2927
					</ds>
2928
		";
2929
		$cdp_arr[] = "			<ds>
2930
					<primary_value> NaN </primary_value>
2931
					<secondary_value> 0.0000000000e+00 </secondary_value>
2932
					<value> NaN </value>
2933
					<unknown_datapoints> 0 </unknown_datapoints>
2934
					</ds>
2935
		";
2936

    
2937
		$value_search = "<\/row>";
2938
		$value_replace = "</row>";
2939
		$value = "<v> NaN </v>";
2940

    
2941
		$xml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2942
		foreach ($ds_arr as $ds) {
2943
			$xml = preg_replace("/$ds_search/s", "$ds{$ds_search}", $xml);
2944
		}
2945
		foreach ($cdp_arr as $cdp) {
2946
			$xml = preg_replace("/$cdp_search/s", "$cdp{$cdp_replace}", $xml);
2947
		}
2948
		foreach ($ds_arr as $ds) {
2949
			$xml = preg_replace("/$value_search/s", "$value{$value_replace}", $xml);
2950
		}
2951

    
2952
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $xml);
2953
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2954
		unset($xml);
2955
		# Default /tmp tmpfs is ~40mb, do not leave temp files around
2956
		unlink_if_exists("{$g['tmp_path']}/{$xmldump}");
2957
		unlink_if_exists("{$g['tmp_path']}/{$xmldumpnew}");
2958
	}
2959
	if (!platform_booting()) {
2960
		enable_rrd_graphing();
2961
	}
2962
	/* Let's save the RRD graphs after we run enable RRD graphing */
2963
	/* The function will restore the rrd.tgz so we will save it after */
2964
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2965
	if (platform_booting()) {
2966
		echo "Updating configuration...";
2967
	}
2968
	foreach ($config['filter']['rule'] as & $rule) {
2969
		if (isset($rule['protocol']) && !empty($rule['protocol'])) {
2970
			$rule['protocol'] = strtolower($rule['protocol']);
2971
		}
2972
	}
2973
	unset($rule);
2974
}
2975

    
2976
function upgrade_081_to_082() {
2977
	/* don't enable the allow IPv6 toggle */
2978
}
2979

    
2980
function upgrade_082_to_083() {
2981
	global $config;
2982

    
2983
	/* Upgrade captiveportal config */
2984
	if (!empty($config['captiveportal'])) {
2985
		$tmpcp = $config['captiveportal'];
2986
		$config['captiveportal'] = array();
2987
		$config['captiveportal']['cpzone'] = array();
2988
		$config['captiveportal']['cpzone'] = $tmpcp;
2989
		$config['captiveportal']['cpzone']['zoneid'] = 8000;
2990
		$config['captiveportal']['cpzone']['zone'] = "cpzone";
2991
		if ($config['captiveportal']['cpzone']['auth_method'] == "radius") {
2992
			$config['captiveportal']['cpzone']['radius_protocol'] = "PAP";
2993
		}
2994
	}
2995
	if (!empty($config['voucher'])) {
2996
		$tmpcp = $config['voucher'];
2997
		$config['voucher'] = array();
2998
		$config['voucher']['cpzone'] = array();
2999
		$config['voucher']['cpzone'] = $tmpcp;
3000
	}
3001
}
3002

    
3003
function upgrade_083_to_084() {
3004
	global $config;
3005
	if (!isset($config['hasync'])) {
3006
		if (!empty($config['installedpackages']) &&
3007
		    !empty($config['installedpackages']['carpsettings']) &&
3008
		    !empty($config['installedpackages']['carpsettings']['config'])) {
3009
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
3010
			unset($config['installedpackages']['carpsettings']);
3011
		}
3012
		if (empty($config['installedpackages']['carpsettings']) && isset($config['installedpackages']['carpsettings'])) {
3013
			unset($config['installedpackages']['carpsettings']);
3014
		}
3015
		if (empty($config['installedpackages']) && isset($config['installedpackages'])) {
3016
			unset($config['installedpackages']);
3017
		}
3018
	}
3019
}
3020

    
3021
function upgrade_084_to_085() {
3022
	global $config;
3023

    
3024
	$gateway_group_arr = array();
3025
	$gateways = return_gateways_array();
3026
	$oldnames = array();
3027
	/* setup translation array */
3028
	foreach ($gateways as $name => $gw) {
3029
		if (isset($gw['dynamic'])) {
3030
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
3031
			$oldnames[$oldname] = $name;
3032
		} else {
3033
			$oldnames[$name] = $name;
3034
		}
3035
	}
3036

    
3037
	/* process the old array */
3038
	if (is_array($config['gateways']['gateway_group'])) {
3039
		$group_array_new = array();
3040
		foreach ($config['gateways']['gateway_group'] as $name => $group) {
3041
			if (is_array($group['item'])) {
3042
				$newlist = array();
3043
				foreach ($group['item'] as $entry) {
3044
					$elements = explode("|", $entry);
3045
					if ($oldnames[$elements[0]] <> "") {
3046
						$newlist[] = "{$oldnames[$elements[0]]}|{$elements[1]}";
3047
					} else {
3048
						$newlist[] = "{$elements[0]}|{$elements[1]}";
3049
					}
3050
				}
3051
				$group['item'] = $newlist;
3052
				$group_array_new[$name] = $group;
3053
			}
3054
		}
3055
		$config['gateways']['gateway_group'] = $group_array_new;
3056
	}
3057
	/* rename old Quality RRD files in the process */
3058
	$rrddbpath = "/var/db/rrd";
3059
	foreach ($oldnames as $old => $new) {
3060
		if (is_readable("{$rrddbpath}/{$old}-quality.rrd")) {
3061
			@rename("{$rrddbpath}/{$old}-quality.rrd", "{$rrddbpath}/{$new}-quality.rrd");
3062
		}
3063
	}
3064
	unset($gateways, $oldnames, $gateway_group_arr);
3065
}
3066

    
3067
function upgrade_085_to_086() {
3068
	global $config, $g;
3069

    
3070
	/* XXX: Gross hacks in sight */
3071
	if (is_array($config['virtualip']['vip'])) {
3072
		$vipchg = array();
3073
		foreach ($config['virtualip']['vip'] as $vip) {
3074
			if ($vip['mode'] != "carp") {
3075
				continue;
3076
			}
3077
			$config = array_replace_values_recursive(
3078
				$config,
3079
				'^vip' . $vip['vhid'] . '$',
3080
				"{$vip['interface']}_vip{$vip['vhid']}"
3081
			);
3082
		}
3083
	}
3084
}
3085

    
3086
function upgrade_086_to_087() {
3087
	global $config, $dummynet_pipe_list;
3088

    
3089
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue'])) {
3090
		return;
3091
	}
3092

    
3093
	$dnqueue_number = 1;
3094
	$dnpipe_number = 1;
3095

    
3096
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
3097
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
3098
		$dnpipe_number++;
3099
		if (is_array($dnpipe['queue'])) {
3100
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
3101
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
3102
				$dnqueue_number++;
3103
			}
3104
		}
3105
	}
3106

    
3107
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
3108

    
3109
	if (!is_array($config['filter']) || !is_array($config['filter']['rule'])) {
3110
		return;
3111
	}
3112

    
3113
	require_once("shaper.inc");
3114
	read_dummynet_config();
3115

    
3116
	$dn_list = array();
3117
	if (is_array($dummynet_pipe_list)) {
3118
		foreach ($dummynet_pipe_list as $dn) {
3119
			$tmplist =& $dn->get_queue_list();
3120
			foreach ($tmplist as $qname => $link) {
3121
				$dn_list[$link] = $qname;
3122
			}
3123
		}
3124
		unset($dummynet_pipe_list);
3125
	}
3126

    
3127
	foreach ($config['filter']['rule'] as $idx => $rule) {
3128
		if (!empty($rule['dnpipe'])) {
3129
			if (!empty($dn_list[$rule['dnpipe']])) {
3130
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
3131
			}
3132
		}
3133
		if (!empty($rule['pdnpipe'])) {
3134
			if (!empty($dn_list[$rule['pdnpipe']])) {
3135
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
3136
			}
3137
		}
3138
	}
3139
}
3140
function upgrade_087_to_088() {
3141
	global $config;
3142
	if (isset($config['system']['glxsb_enable'])) {
3143
		unset($config['system']['glxsb_enable']);
3144
		$config['system']['crypto_hardware'] = "glxsb";
3145
	}
3146
}
3147

    
3148
function upgrade_088_to_089() {
3149
	global $config;
3150
	if (!is_array($config['ca'])) {
3151
		$config['ca'] = array();
3152
	}
3153
	if (!is_array($config['cert'])) {
3154
		$config['cert'] = array();
3155
	}
3156

    
3157
	/* migrate captive portal ssl to certificate manager */
3158
	if (is_array($config['captiveportal'])) {
3159
		foreach ($config['captiveportal'] as $id => &$setting) {
3160
			if (isset($setting['httpslogin'])) {
3161
				/* create cert entry */
3162
				$cert = array();
3163
				$cert['refid'] = uniqid();
3164
				$cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
3165
				$cert['crt'] = $setting['certificate'];
3166
				$cert['prv'] = $setting['private-key'];
3167

    
3168
				if (!empty($setting['cacertificate'])) {
3169
					/* create ca entry */
3170
					$ca = array();
3171
					$ca['refid'] = uniqid();
3172
					$ca['descr'] = "Captive Portal CA - {$setting['zone']}";
3173
					$ca['crt'] = $setting['cacertificate'];
3174
					$config['ca'][] = $ca;
3175

    
3176
					/* add ca reference to certificate */
3177
					$cert['caref'] = $ca['refid'];
3178
				}
3179

    
3180
				$config['cert'][] = $cert;
3181

    
3182
				/* create cert reference */
3183
				$setting['certref'] = $cert['refid'];
3184

    
3185
				unset($setting['certificate']);
3186
				unset($setting['private-key']);
3187
				unset($setting['cacertificate']);
3188

    
3189
			}
3190
		}
3191
	}
3192
}
3193

    
3194
function upgrade_089_to_090() {
3195
	global $config;
3196
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
3197
		$vs_a = &$config['load_balancer']['virtual_server'];
3198
		for ($i = 0; isset($vs_a[$i]); $i++) {
3199
			if (is_array($vs_a[$i]['pool'])) {
3200
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
3201
				unset($vs_a[$i]['pool']);
3202
			} elseif (!empty($vs_a[$i]['pool'])) {
3203
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
3204
				unset($vs_a[$i]['pool']);
3205
			}
3206
		}
3207
	}
3208
}
3209

    
3210
function upgrade_090_to_091() {
3211
	global $config;
3212

    
3213
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
3214
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
3215
			if (!empty($dnqueue['bandwidth'])) {
3216
				$bw = array();
3217
				$bw['bw'] = $dnqueue['bandwidth'];
3218
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
3219
				$bw['bwsched'] = "none";
3220
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
3221
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
3222
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
3223
			}
3224
		}
3225
	}
3226
}
3227

    
3228
function upgrade_091_to_092() {
3229
	global $config;
3230

    
3231
	if (is_array($config['nat']['advancedoutbound']) && is_array($config['nat']['advancedoutbound']['rule'])) {
3232
		$nat_rules = &$config['nat']['advancedoutbound']['rule'];
3233
		for ($i = 0; isset($nat_rules[$i]); $i++) {
3234
			if (empty($nat_rules[$i]['interface'])) {
3235
				$nat_rules[$i]['interface'] = 'wan';
3236
			}
3237
		}
3238
	}
3239
}
3240

    
3241
function upgrade_092_to_093() {
3242
	global $g;
3243

    
3244
	$suffixes = array("concurrent", "loggedin");
3245

    
3246
	foreach ($suffixes as $suffix) {
3247
		if (file_exists("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd")) {
3248
			rename("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd",
3249
				"{$g['vardb_path']}/rrd/captiveportal-cpZone-{$suffix}.rrd");
3250
		}
3251
	}
3252

    
3253
	if (!platform_booting()) {
3254
		enable_rrd_graphing();
3255
	}
3256
}
3257

    
3258
function upgrade_093_to_094() {
3259
	global $config;
3260

    
3261
	if (isset($config['system']['powerd_mode'])) {
3262
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3263
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3264
		unset($config['system']['powerd_mode']);
3265
	}
3266
}
3267

    
3268
function upgrade_094_to_095() {
3269
	global $config;
3270

    
3271
	if (!isset($config['interfaces']) || !is_array($config['interfaces'])) {
3272
		return;
3273
	}
3274

    
3275
	foreach ($config['interfaces'] as $iface => $cfg) {
3276
		if (isset($cfg['ipaddrv6']) && ($cfg['ipaddrv6'] == "track6")) {
3277
			if (!isset($cfg['track6-prefix-id']) || ($cfg['track6-prefix-id'] == "")) {
3278
				$config['interfaces'][$iface]['track6-prefix-id'] = 0;
3279
			}
3280
		}
3281
	}
3282
}
3283

    
3284
function upgrade_095_to_096() {
3285
	global $config, $g;
3286

    
3287
	$names = array("inpass", "outpass", "inblock", "outblock",
3288
		"inpass6", "outpass6", "inblock6", "outblock6");
3289
	$rrddbpath = "/var/db/rrd";
3290
	$rrdtool = "/usr/local/bin/rrdtool";
3291

    
3292
	if ($g['platform'] != $g['product_name']) {
3293
		/* restore the databases, if we have one */
3294
		if (restore_rrd()) {
3295
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
3296
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
3297
		}
3298
	}
3299

    
3300
	/* Assume 2*10GigE for now */
3301
	$stream = 2500000000;
3302

    
3303
	/* build a list of traffic and packets databases */
3304
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
3305
	rsort($databases);
3306
	foreach ($databases as $database) {
3307
		if (platform_booting()) {
3308
			echo "Update RRD database {$database}.\n";
3309
		}
3310

    
3311
		$cmd = "{$rrdtool} tune {$rrddbpath}/{$database}";
3312
		foreach ($names as $name) {
3313
			$cmd .= " -a {$name}:{$stream}";
3314
		}
3315
		mwexec("{$cmd} 2>&1");
3316

    
3317
	}
3318
	if (!platform_booting()) {
3319
		enable_rrd_graphing();
3320
	}
3321
	/* Let's save the RRD graphs after we run enable RRD graphing */
3322
	/* The function will restore the rrd.tgz so we will save it after */
3323
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
3324
}
3325

    
3326
function upgrade_096_to_097() {
3327
	global $config, $g;
3328
	/* If the user had disabled default block rule logging before, then bogon/private network logging was already off, so respect their choice. */
3329
	if (isset($config['syslog']['nologdefaultblock'])) {
3330
		$config['syslog']['nologbogons'] = true;
3331
		$config['syslog']['nologprivatenets'] = true;
3332
	}
3333
}
3334

    
3335
function upgrade_097_to_098() {
3336
	// no longer used (used to set kill_states)
3337
	return;
3338
}
3339

    
3340
function upgrade_098_to_099() {
3341
	global $config;
3342

    
3343
	if (empty($config['dhcpd']) || !is_array($config['dhcpd'])) {
3344
		return;
3345
	}
3346

    
3347
	foreach ($config['dhcpd'] as & $dhcpifconf) {
3348
		if (isset($dhcpifconf['next-server'])) {
3349
			$dhcpifconf['nextserver'] = $dhcpifconf['next-server'];
3350
			unset($dhcpifconf['next-server']);
3351
		}
3352
	}
3353
}
3354

    
3355
function upgrade_099_to_100() {
3356
	require_once("/etc/inc/services.inc");
3357
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3358
}
3359

    
3360
function upgrade_100_to_101() {
3361
	global $config, $g;
3362

    
3363
	if (!is_array($config['voucher'])) {
3364
		return;
3365
	}
3366

    
3367
	foreach ($config['voucher'] as $cpzone => $cp) {
3368
		if (!is_array($cp['roll'])) {
3369
			continue;
3370
		}
3371
		foreach ($cp['roll'] as $ridx => $rcfg) {
3372
			if (!empty($rcfg['comment'])) {
3373
				$config['voucher'][$cpzone]['roll'][$ridx]['descr'] = $rcfg['comment'];
3374
			}
3375
		}
3376
	}
3377
}
3378

    
3379
function upgrade_101_to_102() {
3380
	global $config, $g;
3381

    
3382
	if (is_array($config['captiveportal'])) {
3383
		foreach ($config['captiveportal'] as $cpzone => $cp) {
3384
			if (!is_array($cp['passthrumac'])) {
3385
				continue;
3386
			}
3387

    
3388
			foreach ($cp['passthrumac'] as $idx => $passthrumac) {
3389
				$config['captiveportal'][$cpzone]['passthrumac'][$idx]['action'] = 'pass';
3390
			}
3391
		}
3392
	}
3393

    
3394
	/* Convert OpenVPN Compression option to the new style */
3395
	// Nothing to do if there is no OpenVPN tag
3396
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
3397
		if (is_array($config['openvpn']['openvpn-server'])) {
3398
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
3399
				if (!empty($vpn['compression'])) {
3400
					$vpn['compression'] = "adaptive";
3401
				}
3402
			}
3403
		}
3404
		if (is_array($config['openvpn']['openvpn-client'])) {
3405
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
3406
				if (!empty($vpn['compression'])) {
3407
					$vpn['compression'] = "adaptive";
3408
				}
3409
			}
3410
		}
3411
	}
3412
}
3413

    
3414
function upgrade_102_to_103() {
3415
	global $config;
3416

    
3417
	if (isset($config['nat']['advancedoutbound']['enable'])) {
3418
		$config['nat']['advancedoutbound']['mode'] = "advanced";
3419
		unset($config['nat']['advancedoutbound']['enable']);
3420
	} else {
3421
		$config['nat']['advancedoutbound']['mode'] = "automatic";
3422
	}
3423

    
3424
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3425

    
3426
	if (isset($config['nat']['ipsecpassthru'])) {
3427
		unset($config['nat']['ipsecpassthru']);
3428
	}
3429
	if (isset($config['nat']['advancedoutbound'])) {
3430
		unset($config['nat']['advancedoutbound']);
3431
	}
3432
}
3433

    
3434
function upgrade_103_to_104() {
3435
	global $config;
3436

    
3437
	$changed_privs = array(
3438
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3439
		"page-interfacess-groups" => "page-interfaces-groups",
3440
		"page-interfacess-lagg" => "page-interfaces-lagg",
3441
		"page-interfacess-qinq" => "page-interfaces-qinq"
3442
	);
3443

    
3444
	/* update user privileges */
3445
	foreach ($config['system']['user'] as & $user) {
3446
		if (!is_array($user['priv'])) {
3447
			continue;
3448
		}
3449
		foreach ($user['priv'] as & $priv) {
3450
			if (array_key_exists($priv, $changed_privs)) {
3451
				$priv = $changed_privs[$priv];
3452
			}
3453
		}
3454
	}
3455

    
3456
	/* update group privileges */
3457
	foreach ($config['system']['group'] as & $group) {
3458
		if (!is_array($group['priv'])) {
3459
			continue;
3460
		}
3461
		foreach ($group['priv'] as & $priv) {
3462
			if (array_key_exists($priv, $changed_privs)) {
3463
				$priv = $changed_privs[$priv];
3464
			}
3465
		}
3466
	}
3467

    
3468
	/* sync all local account information */
3469
	local_sync_accounts();
3470
}
3471

    
3472
function upgrade_104_to_105() {
3473
	global $config;
3474

    
3475
	if (is_array($config['captiveportal'])) {
3476
		$zoneid = 2;
3477
		foreach ($config['captiveportal'] as $cpzone => $cpcfg) {
3478
			if (empty($cpcfg['zoneid'])) {
3479
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3480
				$zoneid += 2;
3481
			} else if ($cpcfg['zoneid'] > 4000) {
3482
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3483
				$zoneid += 2;
3484
			}
3485
		}
3486
	}
3487
}
3488

    
3489
function upgrade_105_to_106() {
3490
	/* NOTE: This upgrade code was reverted. See redmine ticket #3967 and
3491
	   https://github.com/pfsense/pfsense/commit/6f55af1c25f5232ffe905a90f5f97aad4c87bdfa */
3492
}
3493

    
3494
function upgrade_106_to_107() {
3495
	global $config;
3496

    
3497
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
3498
		$tracker = (int)microtime(true);
3499
		foreach ($config['filter']['rule'] as $ridx => $rule) {
3500
			if (empty($rule['tracker'])) {
3501
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
3502
				$tracker++;
3503
			}
3504
		}
3505
		unset($tracker, $ridx);
3506
	}
3507
	if (is_array($config['nat']) && is_array($config['nat']['rule'])) {
3508
		$tracker = (int)microtime(true);
3509
		foreach ($config['nat']['rule'] as $ridx => $rule) {
3510
			if (empty($rule['tracker'])) {
3511
				$config['nat']['rule'][$ridx]['tracker'] = $tracker;
3512
				$tracker++;
3513
			}
3514
		}
3515
		unset($tracker, $ridx);
3516
	}
3517
}
3518

    
3519
function upgrade_107_to_108() {
3520
	global $config;
3521

    
3522
	if (isset($config['system']['webgui']['noautocomplete'])) {
3523
		unset($config['system']['webgui']['noautocomplete']);
3524
	} else {
3525
		$config['system']['webgui']['loginautocomplete'] = true;
3526
	}
3527
}
3528

    
3529
function upgrade_108_to_109() {
3530
	global $config;
3531

    
3532
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule'])) {
3533
		return;
3534
	}
3535

    
3536
	foreach ($config['filter']['rule'] as &$rule) {
3537
		if (!isset($rule['dscp']) || empty($rule['dscp'])) {
3538
			continue;
3539
		}
3540

    
3541
		$pos = strpos($rule['dscp'], ' ');
3542
		if ($pos !== false) {
3543
			$rule['dscp'] = substr($rule['dscp'], 0, $pos);
3544
		}
3545
		unset($pos);
3546
	}
3547
}
3548

    
3549
function upgrade_109_to_110() {
3550
	global $config;
3551

    
3552
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3553
		return;
3554
	}
3555

    
3556
	foreach ($config['ipsec']['phase2'] as &$rule) {
3557
		if (!empty($rule['uniqid'])) {
3558
			continue;
3559
		}
3560

    
3561
		$rule['uniqid'] = uniqid();
3562
	}
3563
}
3564

    
3565
function upgrade_110_to_111() {
3566
	global $config;
3567

    
3568
	/* Make sure unbound user exist */
3569
	mwexec('/usr/sbin/pw groupadd -n unbound -g 59', true);
3570
	mwexec('/usr/sbin/pw useradd -n unbound -c "Unbound DNS Resolver" -d /var/unbound -s /usr/sbin/nologin -u 59 -g 59', true);
3571

    
3572
	/* cleanup old unbound package stuffs */
3573
	unlink_if_exists("/usr/local/pkg/unbound.xml");
3574
	unlink_if_exists("/usr/local/pkg/unbound.inc");
3575
	unlink_if_exists("/usr/local/pkg/unbound_advanced.xml");
3576
	unlink_if_exists("/usr/local/www/unbound_status.php");
3577
	unlink_if_exists("/usr/local/www/unbound_acls.php");
3578
	unlink_if_exists("/usr/local/bin/unbound_monitor.sh");
3579
	unlink_if_exists("/usr/local/etc/rc.d/unbound.sh");
3580

    
3581
	/* Remove old menu and service entries */
3582
	if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
3583
		foreach ($config['installedpackages']['menu'] as $idx => $menu) {
3584
			if ($menu['name'] != 'Unbound DNS') {
3585
				continue;
3586
			}
3587

    
3588
			unset($config['installedpackages']['menu'][$idx]);
3589
			break;
3590
		}
3591
	}
3592

    
3593
	if (isset($config['installedpackages']['service']) && is_array($config['installedpackages']['service'])) {
3594
		foreach ($config['installedpackages']['service'] as $idx => $service) {
3595
			if ($service['name'] != 'unbound') {
3596
				continue;
3597
			}
3598
			unset($config['installedpackages']['service'][$idx]);
3599
			break;
3600
		}
3601
	}
3602

    
3603
	if (!isset($config['installedpackages']['unbound']['config'][0])) {
3604
		return;
3605
	}
3606

    
3607
	$pkg = $config['installedpackages']['unbound']['config'][0];
3608

    
3609
	if (isset($config['installedpackages']['unboundadvanced']['config'][0])) {
3610
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3611
	}
3612

    
3613
	$new = array();
3614

    
3615
	/* deal first with boolean fields */
3616
	$fields = array(
3617
		"enable" => "enable",
3618
		"dnssec_status" => "dnssec",
3619
		"forwarding_mode" => "forwarding",
3620
		"regdhcp" => "regdhcp",
3621
		"regdhcpstatic" => "regdhcpstatic",
3622
		"txtsupport" => "txtsupport",
3623
		"hide_id" => "hideidentity",
3624
		"hide_version" => "hideversion",
3625
		"prefetch" => "prefetch",
3626
		"prefetch_key" => "prefetchkey",
3627
		"harden_glue" => "hardenglue",
3628
		"harden_dnssec_stripped" => "dnssec_stripped");
3629

    
3630
	foreach ($fields as $oldk => $newk) {
3631
		if (isset($pkg[$oldk])) {
3632
			if ($pkg[$oldk] == 'on') {
3633
				$new[$newk] = true;
3634
			}
3635
			unset($pkg[$oldk]);
3636
		}
3637
	}
3638

    
3639
	$fields = array(
3640
		"active_interface" => "network_interface",
3641
		"query_interface" => "outgoing_interface",
3642
		"unbound_verbosity" => "log_verbosity",
3643
		"msg_cache_size" => "msgcachesize",
3644
		"outgoing_num_tcp" => "outgoing_num_tcp",
3645
		"incoming_num_tcp" => "incoming_num_tcp",
3646
		"edns_buffer_size" => "edns_buffer_size",
3647
		"num_queries_per_thread" => "num_queries_per_thread",
3648
		"jostle_timeout" => "jostle_timeout",
3649
		"cache_max_ttl" => "cache_max_ttl",
3650
		"cache_min_ttl" => "cache_min_ttl",
3651
		"infra_host_ttl" => "infra_host_ttl",
3652
		"infra_cache_numhosts" => "infra_cache_numhosts",
3653
		"unwanted_reply_threshold" => "unwanted_reply_threshold",
3654
		"custom_options" => "custom_options");
3655

    
3656
	foreach ($fields as $oldk => $newk) {
3657
		if (isset($pkg[$oldk])) {
3658
			$new[$newk] = $pkg[$oldk];
3659
			unset($pkg[$oldk]);
3660
		}
3661
	}
3662

    
3663
	if (isset($new['custom_options']) && !empty($new['custom_options'])) {
3664
		$new['custom_options'] = str_replace("\r\n", "\n", $new['custom_options']);
3665
	}
3666

    
3667
	/* Following options were removed, bring them as custom_options */
3668
	if (isset($pkg['stats']) && $pkg['stats'] == "on") {
3669
		if (isset($pkg['stats_interval'])) {
3670
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-interval: {$pkg['stats_interval']}";
3671
		}
3672
		if (isset($pkg['cumulative_stats'])) {
3673
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-cumulative: {$pkg['cumulative_stats']}";
3674
		}
3675
		if (isset($pkg['extended_stats']) && $pkg['extended_stats'] == "on") {
3676
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: yes";
3677
		} else {
3678
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: no";
3679
		}
3680
	}
3681

    
3682
	$new['acls'] = array();
3683
	if (isset($config['installedpackages']['unboundacls']['config']) &&
3684
	    is_array($config['installedpackages']['unboundacls']['config'])) {
3685
		foreach ($config['installedpackages']['unboundacls']['config'] as $acl) {
3686
			$new['acls'][] = $acl;
3687
		}
3688
	}
3689

    
3690
	$config['unbound'] = $new;
3691

    
3692
	if (isset($config['installedpackages']['unbound'])) {
3693
		unset($config['installedpackages']['unbound']);
3694
	}
3695
	if (isset($config['installedpackages']['unboundadvanced'])) {
3696
		unset($config['installedpackages']['unboundadvanced']);
3697
	}
3698
	if (isset($config['installedpackages']['unboundacls'])) {
3699
		unset($config['installedpackages']['unboundacls']);
3700
	}
3701

    
3702
	unset($pkg, $new);
3703
}
3704

    
3705
function upgrade_111_to_112() {
3706
	global $config;
3707

    
3708
	$config['cron']['item'][] = array(
3709
		'minute' => '*/60',
3710
		'hour' => '*',
3711
		'mday' => '*',
3712
		'month' => '*',
3713
		'wday' => '*',
3714
		'who' => 'root',
3715
		'command' => '/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 webConfiguratorlockout'
3716
	);
3717
}
3718

    
3719
function upgrade_112_to_113() {
3720
	global $config;
3721

    
3722
	if (isset($config['notifications']['smtp']['ssl'])) {
3723
		if ($config['notifications']['smtp']['ssl'] == "checked") {
3724
			$config['notifications']['smtp']['ssl'] = true;
3725
		} else {
3726
			unset($config['notifications']['smtp']['ssl']);
3727
		}
3728
	}
3729

    
3730
	if (isset($config['notifications']['smtp']['tls'])) {
3731
		if ($config['notifications']['smtp']['tls'] == "checked") {
3732
			$config['notifications']['smtp']['tls'] = true;
3733
		} else {
3734
			unset($config['notifications']['smtp']['tls']);
3735
		}
3736
	}
3737
}
3738

    
3739
function upgrade_113_to_114() {
3740
	global $config;
3741

    
3742
	if (!isset($config['ipsec']['phase1']) ||
3743
	    !is_array($config['ipsec']['phase1'])) {
3744
		return;
3745
	}
3746

    
3747
	foreach ($config['ipsec']['phase1'] as &$ph1ent) {
3748
		if (!isset($ph1ent['iketype'])) {
3749
			$ph1ent['iketype'] = 'ikev1';
3750
		}
3751
	}
3752
}
3753

    
3754
function upgrade_114_to_115() {
3755
	global $config;
3756

    
3757
	if (isset($config['unbound']['custom_options'])) {
3758
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3759
	}
3760
}
3761

    
3762
function upgrade_115_to_116() {
3763
	global $config;
3764

    
3765
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3766
		return;
3767
	}
3768

    
3769
	$keyid = 1;
3770
	foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
3771
		$config['ipsec']['phase2'][$idx]['reqid'] = $keyid;
3772
		$keyid++;
3773
	}
3774
}
3775

    
3776
function upgrade_116_to_117() {
3777
	global $config;
3778

    
3779
	if (!isset($config['ipsec']['client']) ||
3780
	    !isset($config['ipsec']['client']['dns_split']) ||
3781
	    empty($config['ipsec']['client']['dns_split'])) {
3782
		return;
3783
	}
3784

    
3785
	$config['ipsec']['client']['dns_split'] =
3786
		preg_replace('/\s*,\s*/', ' ', trim($config['ipsec']['client']['dns_split']));
3787

    
3788
}
3789

    
3790
function upgrade_117_to_118() {
3791
	global $config;
3792

    
3793
	// Unset any old CA and Cert in the system section that might still be there from when upgrade_066_to_067 did not unset them.
3794
	if (isset($config['system']['ca'])) {
3795
		unset($config['system']['ca']);
3796
	}
3797
	if (isset($config['system']['cert'])) {
3798
		unset($config['system']['cert']);
3799
	}
3800

    
3801
	if (!isset($config['ipsec']['phase1'])) {
3802
		return;
3803
	}
3804

    
3805
	$a_phase1 =& $config['ipsec']['phase1'];
3806

    
3807
	foreach ($a_phase1 as &$ph1_entry) {
3808
		// update asn1dn strings from racoon's format to strongswan's
3809
		if (isset($ph1_entry['myid_type']) && $ph1_entry['myid_type'] == 'asn1dn') {
3810
			$ph1_entry['myid_data'] =
3811
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['myid_data']);
3812
		}
3813
		if (isset($ph1_entry['peerid_type']) && $ph1_entry['peerid_type'] == 'asn1dn') {
3814
			$ph1_entry['peerid_data'] =
3815
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['peerid_data']);
3816
		}
3817
	}
3818
}
3819

    
3820
function upgrade_118_to_119() {
3821
	global $config;
3822

    
3823
	if (!isset($config['ipsec']['phase1'])) {
3824
		return;
3825
	}
3826

    
3827
	// change peerid_type to 'any' for EAP types to retain previous behavior of omitting rightid
3828
	$a_phase1 =& $config['ipsec']['phase1'];
3829

    
3830
	foreach ($a_phase1 as &$ph1_entry) {
3831
		if (strstr($ph1_entry['authentication_method'], 'eap')) {
3832
			$ph1_entry['peerid_type'] = "any";
3833
		}
3834
	}
3835
}
3836

    
3837
function upgrade_119_to_120() {
3838
	require_once("ipsec.inc");
3839
	global $config, $ipsec_log_cats;
3840

    
3841
	if (!is_array($config['ipsec'])) {
3842
		return;
3843
	}
3844

    
3845
	// add 1 to configured log levels as part of redmine #5340
3846
	foreach ($ipsec_log_cats as $lkey => $ldescr) {
3847
		if (isset($config['ipsec']["ipsec_{$lkey}"])) {
3848
			$config['ipsec']["ipsec_{$lkey}"] = $config['ipsec']["ipsec_{$lkey}"] + 1;
3849
		}
3850
	}
3851

    
3852
}
3853

    
3854

    
3855
function upgrade_120_to_121() {
3856
	global $config;
3857

    
3858
	if (!isset($config['installedpackages']['miniupnpd']['config'][0])) {
3859
		return;
3860
	}
3861

    
3862
	$miniupnpd =& $config['installedpackages']['miniupnpd']['config'][0];
3863

    
3864
	$miniupnpd['row'] = array();
3865

    
3866
	for ($i = 1; $i <= 4; $i++) {
3867
		if (isset($miniupnpd["permuser{$i}"]) && !empty($miniupnpd["permuser{$i}"])) {
3868
			$miniupnpd['row'][] = array('permuser' => $miniupnpd["permuser{$i}"]);
3869
		}
3870
		unset($miniupnpd["permuser{$i}"]);
3871
	}
3872
}
3873

    
3874
function upgrade_121_to_122() {
3875
	global $config;
3876
	foreach ($config['system']['user'] as &$user) {
3877
		if (isset($user['nt-hash'])) {
3878
			unset($user['nt-hash']);
3879
		}
3880
	}
3881
}
3882

    
3883
function upgrade_122_to_123() {
3884
	global $config;
3885

    
3886
	// PPTP server was removed
3887
	if (isset($config['pptpd'])) {
3888
		unset($config['pptpd']);
3889
	}
3890

    
3891
	// Cleanup firewall rules
3892
	if (isset($config['filter']['rule']) && is_array($config['filter']['rule'])) {
3893
		$rules =& $config['filter']['rule'];
3894
		$last_rule = count($rules) - 1;
3895
		// Process in reverse order to be able to unset items
3896
		for ($i = $last_rule; $i >= 0; $i--) {
3897
			if (isset($rules[$i]['interface']) && $rules[$i]['interface'] == 'pptp') {
3898
				unset($config['filter']['rule'][$i]);
3899
				continue;
3900
			}
3901
			if (isset($rules[$i]['source']['network']) && $rules[$i]['source']['network'] == 'pptp') {
3902
				unset($config['filter']['rule'][$i]);
3903
				continue;
3904
			}
3905
			if (isset($rules[$i]['destination']['network']) && $rules[$i]['destination']['network'] == 'pptp') {
3906
				unset($config['filter']['rule'][$i]);
3907
				continue;
3908
			}
3909
		}
3910
	}
3911

    
3912
	// Cleanup 1:1 NAT rules
3913
	if (isset($config['nat']['onetoone']) && is_array($config['nat']['onetoone'])) {
3914
		$onetoone =& $config['nat']['onetoone'];
3915
		$last_rule = count($onetoone) - 1;
3916
		// Process in reverse order to be able to unset items
3917
		for ($i = $last_rule; $i >= 0; $i--) {
3918
			if (isset($onetoone[$i]['interface']) && $onetoone[$i]['interface'] == 'pptp') {
3919
				unset($config['nat']['onetoone'][$i]);
3920
				continue;
3921
			}
3922
			if (isset($onetoone[$i]['source']['network']) && $onetoone[$i]['source']['network'] == 'pptp') {
3923
				unset($config['nat']['onetoone'][$i]);
3924
				continue;
3925
			}
3926
			if (isset($onetoone[$i]['destination']['network']) && $onetoone[$i]['destination']['network'] == 'pptp') {
3927
				unset($config['nat']['onetoone'][$i]);
3928
				continue;
3929
			}
3930
		}
3931
	}
3932

    
3933
	// Cleanup npt NAT rules
3934
	if (isset($config['nat']['npt']) && is_array($config['nat']['npt'])) {
3935
		$npt =& $config['nat']['npt'];
3936
		$last_rule = count($npt) - 1;
3937
		// Process in reverse order to be able to unset items
3938
		for ($i = $last_rule; $i >= 0; $i--) {
3939
			if (isset($npt[$i]['interface']) && $npt[$i]['interface'] == 'pptp') {
3940
				unset($config['nat']['npt'][$i]);
3941
				continue;
3942
			}
3943
		}
3944
	}
3945

    
3946
	// Cleanup Port-forward NAT rules
3947
	if (isset($config['nat']['rule']) && is_array($config['nat']['rule'])) {
3948
		$nat_rules =& $config['nat']['rule'];
3949
		$last_rule = count($nat_rules) - 1;
3950
		// Process in reverse order to be able to unset items
3951
		for ($i = $last_rule; $i >= 0; $i--) {
3952
			if (isset($nat_rules[$i]['interface']) && $nat_rules[$i]['interface'] == 'pptp') {
3953
				unset($config['nat']['rule'][$i]);
3954
				continue;
3955
			}
3956
			if (isset($nat_rules[$i]['source']['network']) && $nat_rules[$i]['source']['network'] == 'pptp') {
3957
				unset($config['nat']['rule'][$i]);
3958
				continue;
3959
			}
3960
			if (isset($nat_rules[$i]['destination']['network']) && $nat_rules[$i]['destination']['network'] == 'pptp') {
3961
				unset($config['nat']['rule'][$i]);
3962
				continue;
3963
			}
3964
		}
3965
	}
3966

    
3967
	// Cleanup Port-forward NAT rules
3968
	if (isset($config['nat']['outbound']['rule']) && is_array($config['nat']['outbound']['rule'])) {
3969
		$out_rules =& $config['nat']['outbound']['rule'];
3970
		$last_rule = count($out_rules) - 1;
3971
		// Process in reverse order to be able to unset items
3972
		for ($i = $last_rule; $i >= 0; $i--) {
3973
			if (isset($out_rules[$i]['interface']) && $out_rules[$i]['interface'] == 'pptp') {
3974
				unset($config['nat']['outbound']['rule'][$i]);
3975
				continue;
3976
			}
3977
		}
3978
	}
3979
}
3980

    
3981
function upgrade_123_to_124() {
3982
	if (isset($config['system']['altpkgrepo'])) {
3983
		unset($config['system']['altpkgrepo']);
3984
	}
3985

    
3986
	if (isset($config['theme'])) {
3987
		unset($config['theme']);
3988
	}
3989
}
3990

    
3991
function upgrade_124_to_125() {
3992
	global $config;
3993

    
3994
	/* Find interfaces with WEP configured. */
3995
	foreach ($config['interfaces'] as $ifname => $intf) {
3996
		if (!is_array($intf['wireless'])) {
3997
			continue;
3998
		}
3999

    
4000
		/* Generate a notice, disable interface, remove WEP settings */
4001
		if (isset($intf['wireless']['wep']['enable'])) {
4002
			if (!function_exists("file_notice")) {
4003
				require_once("notices.inc");
4004
			}
4005
			file_notice("WirelessSettings", sprintf(gettext("WEP is no longer supported. It will be disabled on the %s interface and the interface will be disabled. Please reconfigure the interface."), $ifname));
4006
			unset($config['interfaces'][$ifname]['wireless']['wep']);
4007
			if (isset($intf['enable'])) {
4008
				unset($config['interfaces'][$ifname]['enable']);
4009
			}
4010
		}
4011
	}
4012
}
4013

    
4014
function upgrade_125_to_126() {
4015
	require_once("ipsec.inc");
4016
	global $config, $ipsec_log_cats, $ipsec_log_sevs;
4017

    
4018
	$def_loglevel = 1;
4019
	if (!is_array($config['ipsec'])) {
4020
		return;
4021
	}
4022

    
4023
	if (!isset($config['ipsec']['logging']) || !is_array($config['ipsec']['logging'])) {
4024
		$config['ipsec']['logging'] = array();
4025
	}
4026

    
4027
	/* subtract 2 from ipsec log levels. the value stored in the config.xml
4028
	 * will now match the strongswan level exactly.
4029
	 */
4030
	foreach (array_keys($ipsec_log_cats) as $cat) {
4031
		if (!isset($config['ipsec']["ipsec_{$cat}"])) {
4032
			$new_level = $def_loglevel;
4033
		} else {
4034
			$new_level = intval($config['ipsec']["ipsec_{$cat}"]) - 2;
4035
		}
4036

    
4037
		if (in_array($new_level, array_keys($ipsec_log_sevs))) {
4038
			$config['ipsec']['logging'][$cat] = $new_level;
4039
		} else {
4040
			$config['ipsec']['logging'][$cat] = $def_loglevel;
4041
		}
4042
		unset($config['ipsec']["ipsec_{$cat}"]);
4043
	}
4044
}
4045

    
4046
// prior to v2.3 <widgets><sequence> contains a list of widgets with display types:
4047
//		none, close, hide, & show
4048
// v2.3 & later uses:
4049
//		close & open
4050
// widgets not in use are simply not in the list
4051
function upgrade_126_to_127() {
4052
	global $config;
4053

    
4054
	if (!isset($config['widgets']['sequence'])) {
4055
		return;
4056
	}
4057

    
4058
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4059
	$new_widgets = array();
4060

    
4061
	foreach ($cur_widgets as $widget) {
4062
		list($file, $col, $display) = explode(':', $widget);
4063

    
4064
		switch ($display) {
4065
			case 'hide':
4066
				$display = 'close';
4067
				break;
4068
			case 'show':
4069
				$display = 'open';
4070
				break;
4071
			case 'open':
4072
				break;
4073
			default:
4074
				continue 2;
4075
		}
4076

    
4077
		/* Remove '-container' from widget name */
4078
		$file = preg_replace('/-container$/', '', $file);
4079

    
4080
		$new_widgets[] = "{$file}:{$col}:{$display}";
4081
	}
4082

    
4083
	$config['widgets']['sequence'] = implode(',', $new_widgets);
4084

    
4085
}
4086

    
4087
function upgrade_127_to_128() {
4088
	global $config;
4089

    
4090
	// If bindip is not already specified then migrate the old SNMP bindlan flag to a bindip setting
4091
	if (isset($config['snmpd']['bindlan'])) {
4092
		if (!isset($config['snmpd']['bindip'])) {
4093
			$config['snmpd']['bindip'] = 'lan';
4094
		}
4095
		unset($config['snmpd']['bindlan']);
4096
	}
4097
}
4098

    
4099
function upgrade_128_to_129() {
4100
	global $config;
4101

    
4102
	/* net.inet.ip.fastforwarding does not exist in 2.3. */
4103
	if (!isset($config['sysctl']['item']) ||
4104
	    !is_array($config['sysctl']['item'])) {
4105
		return;
4106
	}
4107

    
4108
	foreach ($config['sysctl']['item'] as $idx => $sysctl) {
4109
		if ($sysctl['tunable'] == "net.inet.ip.fastforwarding") {
4110
			unset($config['sysctl']['item'][$idx]);
4111
		}
4112
		if ($sysctl['tunable'] == "net.inet.ipsec.debug") {
4113
			$config['sysctl']['item'][$idx]['value'] = "0";
4114
		}
4115
	}
4116

    
4117
	/* IPSEC is always on in 2.3. */
4118
	if (isset($config['ipsec']['enable'])) {
4119
		unset($config['ipsec']['enable']);
4120
	} else if (is_array($config['ipsec']['phase1'])) {
4121
		/*
4122
		 * If IPsec was globally disabled, disable all
4123
		 * phase1 entries
4124
		 */
4125
		foreach ($config['ipsec']['phase1'] as $idx => $p1) {
4126
			$config['ipsec']['phase1'][$idx]['disabled'] = true;
4127
		}
4128
	}
4129
}
4130

    
4131
function upgrade_129_to_130() {
4132
	global $config;
4133

    
4134
	/* Change OpenVPN topology_subnet checkbox into topology multi-select #5526 */
4135
	if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
4136
		foreach ($config['openvpn']['openvpn-server'] as & $serversettings) {
4137
			if (strtolower($serversettings['topology_subnet']) == "yes") {
4138
				unset($serversettings['topology_subnet']);
4139
				$serversettings['topology'] = "subnet";
4140
			} else {
4141
				$serversettings['topology'] = "net30";
4142
			}
4143
		}
4144
	}
4145
}
4146

    
4147
function upgrade_130_to_131() {
4148
	global $config;
4149

    
4150
	// Default dpinger parameters at time of this upgrade (2.3)
4151
	$default_interval = 500;
4152
	$default_alert_interval = 1000;
4153
	$default_loss_interval = 2000;
4154
	$default_time_period = 60000;
4155

    
4156
	if (isset($config['syslog']['apinger'])) {
4157
		$config['syslog']['dpinger'] = true;
4158
		unset($config['syslog']['apinger']);
4159
	}
4160

    
4161
	if (isset($config['system']['apinger_debug'])) {
4162
		unset($config['system']['apinger_debug']);
4163
	}
4164

    
4165
	if (!isset($config['gateways']['gateway_item']) ||
4166
	    !is_array($config['gateways']['gateway_item'])) {
4167
		return;
4168
	}
4169

    
4170
	if (is_array($config['gateways']['gateway_item'])) {
4171
		foreach ($config['gateways']['gateway_item'] as &$gw) {
4172
			// dpinger uses milliseconds
4173
			if (isset($gw['interval']) &&
4174
				is_numeric($gw['interval'])) {
4175
				$gw['interval'] = $gw['interval'] * 1000;
4176
			}
4177

    
4178
			if (isset($gw['interval'])) {
4179
				$effective_interval = $gw['interval'];
4180
			} else {
4181
				$effective_interval = $default_interval;
4182
			}
4183

    
4184
			if (isset($gw['down']) &&
4185
				is_numeric($gw['down'])) {
4186
				$gw['time_period'] = $gw['down'] * 1000;
4187
				unset($gw['down']);
4188
			}
4189

    
4190
			if (isset($gw['time_period'])) {
4191
				$effective_time_period = $gw['time_period'];
4192
			} else {
4193
				$effective_time_period = $default_time_period;
4194
			}
4195

    
4196
			if (isset($gw['latencyhigh'])) {
4197
				// Default loss_interval is 2000, but must be set
4198
				// higher if latencyhigh is higher.
4199
				if ($gw['latencyhigh'] > $default_loss_interval) {
4200
					$gw['loss_interval'] = $gw['latencyhigh'];
4201
				}
4202
			}
4203

    
4204
			if (isset($gw['loss_interval'])) {
4205
				$effective_loss_interval = $gw['loss_interval'];
4206
			} else {
4207
				$effective_loss_interval = $default_loss_interval;
4208
			}
4209

    
4210
			if (isset($gw['interval'])) {
4211
				// Default alert_interval is 1000, but must be set
4212
				// higher if interval is higher.
4213
				if ($gw['interval'] > $default_alert_interval) {
4214
					$gw['alert_interval'] = $gw['interval'];
4215
				}
4216
			}
4217

    
4218
			if ((($effective_interval * 2) + $effective_loss_interval) >= $effective_time_period) {
4219
				$gw['time_period'] = ($effective_interval * 2) + $effective_loss_interval + 1;
4220
			}
4221

    
4222
			if (isset($gw['avg_delay_samples'])) {
4223
				unset($gw['avg_delay_samples']);
4224
			}
4225
			if (isset($gw['avg_delay_samples_calculated'])) {
4226
				unset($gw['avg_delay_samples_calculated']);
4227
			}
4228
			if (isset($gw['avg_loss_samples'])) {
4229
				unset($gw['avg_loss_samples']);
4230
			}
4231
			if (isset($gw['avg_loss_samples_calculated'])) {
4232
				unset($gw['avg_loss_samples_calculated']);
4233
			}
4234
			if (isset($gw['avg_loss_delay_samples'])) {
4235
				unset($gw['avg_loss_delay_samples']);
4236
			}
4237
			if (isset($gw['avg_loss_delay_samples_calculated'])) {
4238
				unset($gw['avg_loss_delay_samples_calculated']);
4239
			}
4240
		}
4241
	}
4242
}
4243

    
4244
function upgrade_131_to_132() {
4245
	global $config;
4246
	if (isset($config['system']['usefifolog'])) {
4247
		unset($config['system']['usefifolog']);
4248
		clear_all_log_files(false);
4249
	}
4250
}
4251

    
4252
function upgrade_132_to_133() {
4253
	global $config;
4254

    
4255
	if (isset($config['ipsec']['phase1']) &&
4256
	    is_array($config['ipsec']['phase1'])) {
4257
		foreach ($config['ipsec']['phase1'] as &$p1) {
4258
			if (isset($p1['encryption-algorithm']['name']) &&
4259
			    $p1['encryption-algorithm']['name'] == 'des') {
4260
				$p1['disabled'] = true;
4261
				file_notice("IPsec",
4262
				    sprintf(gettext("DES is no longer supported, IPsec phase 1 item '%s' is being disabled."), $p1['descr']));
4263
			}
4264
		}
4265
	}
4266

    
4267
	if (isset($config['ipsec']['phase2']) &&
4268
	    is_array($config['ipsec']['phase2'])) {
4269
		foreach ($config['ipsec']['phase2'] as &$p2) {
4270
			if (!isset($p2['encryption-algorithm-option']) ||
4271
			    !is_array($p2['encryption-algorithm-option'])) {
4272
				continue;
4273
			}
4274

    
4275
			foreach ($p2['encryption-algorithm-option'] as $ealgo) {
4276
				if ($ealgo['name'] == 'des') {
4277
					$p2['disabled'] = true;
4278
					file_notice("IPsec",
4279
					    sprintf(gettext("DES is no longer supported, IPsec phase 2 item '%s' is being disabled."), $p2['descr']));
4280
				}
4281
			}
4282
		}
4283
	}
4284
}
4285

    
4286
// Determine the highest column number in use and set dashboardcolumns accordingly
4287
function upgrade_133_to_134() {
4288
	global $config;
4289

    
4290
	if (!isset($config['widgets']['sequence']) || isset($config['system']['webgui']['dashboardcolumns'])) {
4291
		return;
4292
	}
4293

    
4294
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4295
	$maxcols = 2;
4296

    
4297
	foreach ($cur_widgets as $widget) {
4298
		list($file, $col, $display) = explode(':', $widget);
4299

    
4300
		if (($display != 'none') && ($display != 'hide')) {
4301
			preg_match('#[0-9]+$#', $col, $column);
4302
			if ($column[0] > $maxcols) {
4303
				$maxcols = $column[0];
4304
			}
4305
		}
4306
	}
4307

    
4308
	$config['system']['webgui']['dashboardcolumns'] = $maxcols % 10;
4309
}
4310

    
4311
function upgrade_134_to_135() {
4312
	global $config;
4313

    
4314
	if (isset($config['syslog']['nologlighttpd'])) {
4315
		unset($config['syslog']['nologlighttpd']);
4316
		$config['syslog']['nolognginx'] = true;
4317
	}
4318
}
4319

    
4320
function upgrade_135_to_136() {
4321
	global $config;
4322

    
4323
	$l7_active = false;
4324
	if (isset($config['l7shaper'])) {
4325
		unset($config['l7shaper']);
4326
		if (is_array($config['filter']['rule'])) {
4327
			foreach ($config['filter']['rule'] as $idx => $rule) {
4328
				if (isset($rule['l7container'])) {
4329
					unset($config['filter']['rule'][$idx]['l7container']);
4330
					$l7_active = true;
4331
				}
4332
			}
4333
		}
4334
		if ($l7_active) {
4335
			file_notice("L7shaper", gettext("Layer 7 shaping is no longer supported. Its configuration has been removed."));
4336
		}
4337
	}
4338
}
4339

    
4340
function upgrade_136_to_137() {
4341
	global $config;
4342

    
4343
	if (is_array($config['dhcpd'])) {
4344
		foreach ($config['dhcpd'] as &$dhcpd) {
4345
			if (!is_array($dhcpd['numberoptions']['item'])) {
4346
				continue;
4347
			}
4348

    
4349
			foreach ($dhcpd['numberoptions']['item'] as &$item) {
4350
				$item['value'] = base64_encode($item['value']);
4351
			}
4352
		}
4353
	}
4354

    
4355
	if (is_array($config['dhcpdv6'])) {
4356
		foreach ($config['dhcpdv6'] as &$dhcpdv6) {
4357
			if (!is_array($dhcpdv6['numberoptions']['item'])) {
4358
				continue;
4359
			}
4360

    
4361
			foreach ($dhcpdv6['numberoptions']['item'] as &$item) {
4362
				$item['value'] = base64_encode($item['value']);
4363
			}
4364
		}
4365
	}
4366
}
4367

    
4368
function upgrade_137_to_138() {
4369
	global $config;
4370

    
4371
	// the presence of unityplugin tag used to disable loading of unity plugin
4372
	// it's now disabled by default, and config tag is to enable. Unset accordingly.
4373
	if (is_array($config['ipsec'])) {
4374
		if (isset($config['ipsec']['unityplugin'])) {
4375
			unset($config['ipsec']['unityplugin']);
4376
		}
4377
	}
4378
}
4379

    
4380
function upgrade_138_to_139() {
4381
	global $config;
4382

    
4383
	// clean up state killing on gateway failure. having kill_states set used to mean it was disabled
4384
	// now set gw_down_kill_states if enabled.
4385
	if (!isset($config['system']['kill_states'])) {
4386
		$config['system']['gw_down_kill_states'] = true;
4387
	} else {
4388
		unset($config['system']['kill_states']);
4389
	}
4390
}
4391

    
4392
function upgrade_139_to_140() {
4393
	global $config;
4394

    
4395
	if (is_array($config['virtualip']['vip'])) {
4396
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4397
			if ($vip['mode'] == "carp") {
4398
				if (!isset($vip['uniqid'])) {
4399
					$config['virtualip']['vip'][$idx]['uniqid'] = uniqid();
4400
				}
4401
			}
4402
		}
4403
	}
4404
}
4405

    
4406
function upgrade_140_to_141() {
4407
	global $config;
4408

    
4409
	// retain OpenVPN's net30 default topology for upgraded client configs so they still work
4410
	// This is for 2.3 ALPHA to a later 2.3, not 2.2.x upgrades, which had no topology setting on clients
4411
	if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
4412
		foreach ($config['openvpn']['openvpn-client'] as $idx => $ovpnclient) {
4413
			if (!isset($ovpnclient['topology'])) {
4414
				$config['openvpn']['openvpn-client'][$idx]['topology'] = "net30";
4415
			}
4416
		}
4417
	}
4418

    
4419
	// repeat addition of filter tracker IDs from 106_to_107 where missing since associated filter rules were missing them
4420
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
4421
		$tracker = (int)microtime(true);
4422
		foreach ($config['filter']['rule'] as $ridx => $rule) {
4423
			if (empty($rule['tracker'])) {
4424
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
4425
				$tracker++;
4426
			}
4427
		}
4428
		unset($tracker, $ridx);
4429
	}
4430

    
4431
}
4432

    
4433
function upgrade_141_to_142() {
4434
	global $config;
4435
	/* Convert Namecheap type DynDNS entries to the new split hostname and domain format */
4436

    
4437
	if (!is_array($config['dyndnses'])) {
4438
		$config['dyndnses'] = array();
4439
	}
4440
	if (!is_array($config['dyndnses']['dyndns'])) {
4441
		$config['dyndnses']['dyndns'] = array();
4442
	}
4443
	$a_dyndns = &$config['dyndnses']['dyndns'];
4444

    
4445
	foreach ($a_dyndns as &$dyndns) {
4446
		if ($dyndns['type'] == "namecheap") {
4447
			/* Use the old style logic to split the host and domain one last time. */
4448
			$dparts = explode(".", trim($dyndns['host']));
4449
			$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
4450
			$domain_offset = count($dparts) - $domain_part_count;
4451
			$dyndns['host'] = implode(".", array_slice($dparts, 0, $domain_offset));
4452
			$dyndns['domainname'] = implode(".", array_slice($dparts, $domain_offset));
4453
		}
4454
	}
4455

    
4456
	/* unset old pppoerestart cron job if it exists. redmine 1905 */
4457
	if (is_array($config['cron']['item'])) {
4458
		foreach ($config['cron']['item'] as $idx => $cronitem) {
4459
			if ($cronitem['command'] == "/etc/pppoerestart") {
4460
				unset($config['cron']['item'][$idx]);
4461
			}
4462
		}
4463
	}
4464
}
4465

    
4466
// Updated to check for empty separator definitions via is_array()
4467
function upgrade_142_to_143() {
4468
	global $config;
4469

    
4470
	/* Re-index firewall rule separators per interface */
4471
	if (is_array($config['filter']['separator'])) {
4472
		foreach ($config['filter']['separator'] as $interface => $separators) {
4473

    
4474
			if(is_array($separators)) {
4475
				foreach ($separators as $sepn => $separator) {
4476

    
4477
					$seprow = substr($separator['row']['0'], 2);
4478
					$sepif  = $separator['if'];
4479

    
4480
					// Determine position of separator within the interface rules.
4481
					$i = -1; $j = 0;
4482
					foreach ($config['filter']['rule'] as $rulen => $filterent) {
4483

    
4484
						if ($i == $seprow) {
4485
							// Set separator row to it's position within the interface rules.
4486
							$config['filter']['separator'][$sepif][$sepn]['row'] = 'fr' . $j;
4487
							continue 2;	// Advance to next separator
4488
						}
4489

    
4490
						// Position within the interface rules.
4491
						if (($filterent['interface'] == $sepif && !isset($filterent['floating'])) || (isset($filterent['floating']) && "floatingrules" == $sepif)) {
4492
							$j++;
4493
						}
4494
						$i++;
4495
					}
4496
				}
4497
			}
4498
		}
4499
	}
4500

    
4501
	/* Re-index nat rule separators */
4502
	if (is_array($config['nat']['separator'])) {
4503
		foreach ($config['nat']['separator'] as $sepn => $separator) {
4504
			if (is_array($separator)) {
4505
				$seprow = substr($separator['row']['0'], 2);
4506
				$config['nat']['separator'][$sepn]['row'] = 'fr' . ($seprow + 1);
4507
			}
4508
		}
4509
	}
4510
}
4511

    
4512
function get_vip_from_ip_alias($ipalias) {
4513
	global $config;
4514

    
4515
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4516
		if ($vip['mode'] != "ipalias") {
4517
			continue;
4518
		}
4519
		if ($ipalias == $vip['subnet']) {
4520
			return ("_vip{$vip['uniqid']}");
4521
		}
4522
	}
4523

    
4524
	return ($ipalias);
4525
}
4526

    
4527
function get_vip_from_oldcarp($carp) {
4528
	global $config;
4529

    
4530
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4531
		if ($vip['mode'] != "carp") {
4532
			continue;
4533
		}
4534
		if ($carp == "{$vip['interface']}_vip{$vip['vhid']}") {
4535
			return ("_vip{$vip['uniqid']}");
4536
		}
4537
	}
4538

    
4539
	return ($carp);
4540
}
4541

    
4542
function upgrade_143_to_144() {
4543
	global $config;
4544

    
4545
	if (is_array($config['virtualip']['vip'])) {
4546
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4547
			if ($vip['mode'] == "ipalias") {
4548
				if (!isset($vip['uniqid'])) {
4549
					$config['virtualip']['vip'][$idx]['uniqid'] = uniqid();
4550
				}
4551
			}
4552
		}
4553
	}
4554

    
4555
	/* Convert IPsec phase 1 entries. */
4556
	if (is_array($config['ipsec']['phase1'])) {
4557
		foreach ($config['ipsec']['phase1'] as $idx => $ph1ent) {
4558
			if (is_ipaddr($ph1ent['interface']) || is_ipaddrv6($ph1ent['interface'])) {
4559
				$config['ipsec']['phase1'][$idx]['interface'] = get_vip_from_ip_alias($ph1ent['interface']);
4560
			} else if (strpos($ph1ent['interface'], "_vip")) {
4561
				$config['ipsec']['phase1'][$idx]['interface'] = get_vip_from_oldcarp($ph1ent['interface']);
4562
			}
4563
		}
4564
	}
4565

    
4566
	/* Convert openvpn. */
4567
	if (is_array($config['openvpn']['openvpn-server'])) {
4568
		foreach ($config['openvpn']['openvpn-server'] as $idx => $ovpn) {
4569
			if (empty($ovpn['interface'])) {
4570
				continue;
4571
			}
4572
			if (is_ipaddr($ovpn['interface']) || is_ipaddrv6($ovpn['interface'])) {
4573
				$config['openvpn']['openvpn-server'][$idx]['interface'] = get_vip_from_ip_alias($ovpn['interface']);
4574
			} else if (strpos($ovpn['interface'], "_vip")) {
4575
				$config['openvpn']['openvpn-server'][$idx]['interface'] = get_vip_from_oldcarp($ovpn['interface']);
4576
			}
4577
		}
4578
	}
4579
	if (is_array($config['openvpn']['openvpn-client'])) {
4580
		foreach ($config['openvpn']['openvpn-client'] as $idx => $ovpn) {
4581
			if (empty($ovpn['interface'])) {
4582
				continue;
4583
			}
4584
			if (is_ipaddr($ovpn['interface']) || is_ipaddrv6($ovpn['interface'])) {
4585
				$config['openvpn']['openvpn-client'][$idx]['interface'] = get_vip_from_ip_alias($ovpn['interface']);
4586
			} else if (strpos($ovpn['interface'], "_vip")) {
4587
				$config['openvpn']['openvpn-client'][$idx]['interface'] = get_vip_from_oldcarp($ovpn['interface']);
4588
			}
4589
		}
4590
	}
4591

    
4592
	/* Convert unbound. */
4593
	if (is_array($config['unbound']) && !empty($config['unbound']['active_interface'])) {
4594
		$active_ifs = explode(",", $config['unbound']['active_interface']);
4595
		$ifs = array();
4596
		foreach ($active_ifs as $if) {
4597
			if (is_ipaddr($if) || is_ipaddrv6($if)) {
4598
				$ifs[] = get_vip_from_ip_alias($if);
4599
			} else if (strpos($if, "_vip")) {
4600
				$ifs[] = get_vip_from_oldcarp($if);
4601
			} else {
4602
				$ifs[] = $if;
4603
			}
4604
		}
4605
		$config['unbound']['active_interface'] = implode(",", $ifs);
4606
	}
4607

    
4608
	/* Convert dnsmasq. */
4609
	if (is_array($config['dnsmasq']) && !empty($config['dnsmasq']['interface'])) {
4610
		$active_ifs = explode(",", $config['dnsmasq']['interface']);
4611
		$ifs = array();
4612
		foreach ($active_ifs as $if) {
4613
			if (is_ipaddr($if) || is_ipaddrv6($if)) {
4614
				$ifs[] = get_vip_from_ip_alias($if);
4615
			} else if (strpos($if, "_vip")) {
4616
				$ifs[] = get_vip_from_oldcarp($if);
4617
			} else {
4618
				$ifs[] = $if;
4619
			}
4620
		}
4621
		$config['dnsmasq']['interface'] = implode(",", $ifs);
4622
	}
4623
}
4624

    
4625
function upgrade_144_to_145() {
4626
	global $config;
4627

    
4628
	// Enable DHCPv6 server and radvd config for track6 interfaces,
4629
	// matching what used to be automatically enabled with no user
4630
	// configurability.
4631
	if (is_array($config['interfaces'])) {
4632
		foreach ($config['interfaces'] as $ifname => $ifcfg) {
4633
			if (isset($ifcfg['enable'])) {
4634
				if ($ifcfg['ipaddrv6'] == "track6") {
4635
					$config['dhcpdv6'][$ifname]['enable'] = true;
4636
					$config['dhcpdv6'][$ifname]['range']['from'] = "::1000";
4637
					$config['dhcpdv6'][$ifname]['range']['to'] = "::2000";
4638
					$config['dhcpdv6'][$ifname]['ramode'] = "assist";
4639
					$config['dhcpdv6'][$ifname]['rapriority'] = "medium";
4640
				}
4641
			}
4642
		}
4643
	}
4644
}
4645

    
4646
function upgrade_145_to_146() {
4647
	// Add standard deviation to the quality rrds
4648
	global $config, $g;
4649

    
4650
	$rrddbpath = "/var/db/rrd";
4651
	$rrdtool = "/usr/local/bin/rrdtool";
4652

    
4653
	$awkcmd = "/usr/bin/awk '";
4654
	$awkcmd .= "{\n";
4655
	$awkcmd .= "    if (sub(/<\\/v><\\/row>/, \"</v><v>NaN</v></row>\") == 0)\n";
4656
	$awkcmd .= "    {\n";
4657
	$awkcmd .= "        if (/<\\/cdp_prep>/)\n";
4658
	$awkcmd .= "        {\n";
4659
	$awkcmd .= "            print \"			<ds>\"\n";
4660
	$awkcmd .= "            print \"			<primary_value> 0.0000000000e+00 </primary_value>\"\n";
4661
	$awkcmd .= "            print \"			<secondary_value> 0.0000000000e+00 </secondary_value>\"\n";
4662
	$awkcmd .= "            print \"			<value> NaN </value>\"\n";
4663
	$awkcmd .= "            print \"			<unknown_datapoints> 0 </unknown_datapoints>\"\n";
4664
	$awkcmd .= "            print \"			</ds>\"\n";
4665
	$awkcmd .= "        }\n";
4666
	$awkcmd .= "        else if (/<!-- Round Robin Archives -->/)\n";
4667
	$awkcmd .= "        {\n";
4668
	$awkcmd .= "            print \"	<ds>\"\n";
4669
	$awkcmd .= "            print \"		<name> stddev </name>\"\n";
4670
	$awkcmd .= "            print \"		<type> GAUGE </type>\"\n";
4671
	$awkcmd .= "            print \"		<minimal_heartbeat> 120 </minimal_heartbeat>\"\n";
4672
	$awkcmd .= "            print \"		<min> 0.0000000000e+00 </min>\"\n";
4673
	$awkcmd .= "            print \"		<max> 1.0000000000e+05 </max>\\n\"\n";
4674
	$awkcmd .= "            print \"		<!-- PDP Status -->\"\n";
4675
	$awkcmd .= "            print \"		<last_ds> 0 </last_ds>\"\n";
4676
	$awkcmd .= "            print \"		<value> 0.0000000000e+00 </value>\"\n";
4677
	$awkcmd .= "            print \"		<unknown_sec> 0 </unknown_sec>\"\n";
4678
	$awkcmd .= "            print \"	</ds>\\n\"\n";
4679
	$awkcmd .= "        }\n";
4680
	$awkcmd .= "    }\n";
4681
	$awkcmd .= "    print;\n";
4682
	$awkcmd .= "}'";
4683

    
4684
	if ($g['platform'] != $g['product_name']) {
4685
		/* restore the databases, if we have one */
4686
		if (restore_rrd()) {
4687
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
4688
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
4689
		}
4690
	}
4691

    
4692
	$databases = return_dir_as_array($rrddbpath, '/-quality\.rrd$/');
4693
	foreach ($databases as $database) {
4694
		$xmldump = "{$g['tmp_path']}/{$database}.xml";
4695

    
4696
		if (platform_booting()) {
4697
			echo "Update RRD database {$database}.\n";
4698
		}
4699

    
4700
		exec("$rrdtool dump {$rrddbpath}/{$database} | {$awkcmd} > {$xmldump}");
4701
		exec("$rrdtool restore -f {$xmldump} {$rrddbpath}/{$database}");
4702
		@unlink("{$xmldump}");
4703
	}
4704

    
4705
	if (!platform_booting()) {
4706
		enable_rrd_graphing();
4707
	}
4708
	/* Let's save the RRD graphs after we run enable RRD graphing */
4709
	/* The function will restore the rrd.tgz so we will save it after */
4710
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
4711
}
4712

    
4713
function upgrade_bgpd_146_to_147() {
4714
	global $config;
4715

    
4716
	if (!isset($config['installedpackages']['openbgpd']['config']) ||
4717
	    !is_array($config['installedpackages']['openbgpd']['config'])) {
4718
		return;
4719
	}
4720
	$openbgpd_conf = &$config['installedpackages']['openbgpd']['config'][0];
4721
	if (!isset($openbgpd_conf['carpstatusip']) &&
4722
	    !is_ipaddr($openbgpd_conf['carpstatusip'])) {
4723
		return;
4724
	}
4725

    
4726
	if (!is_array($config['virtualip']['vip']))
4727
		return;
4728
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4729
		if ($vip['subnet'] == $openbgpd_conf['carpstatusip']) {
4730
			$openbgpd_conf['carpstatusvid'] = "_vip{$vip['uniqid']}";
4731
			unset($openbgpd_conf['carpstatusip']);
4732
			return;
4733
		}
4734
	}
4735
}
4736

    
4737
function upgrade_quagga_146_to_147() {
4738
	global $config;
4739

    
4740
	if (!isset($config['installedpackages']['quaggaospfd']['config']) ||
4741
	    !is_array($config['installedpackages']['quaggaospfd']['config'])) {
4742
		return;
4743
	}
4744
	$ospfd_conf = &$config['installedpackages']['quaggaospfd']['config'][0];
4745
	if (!isset($ospfd_conf['carpstatusip']) &&
4746
	    !is_ipaddr($ospfd_conf['carpstatusip'])) {
4747
		return;
4748
	}
4749

    
4750
	if (!is_array($config['virtualip']['vip']))
4751
		return;
4752
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4753
		if ($vip['subnet'] == $ospfd_conf['carpstatusip']) {
4754
			$ospfd_conf['carpstatusvid'] = "_vip{$vip['uniqid']}";
4755
			unset($ospfd_conf['carpstatusip']);
4756
			return;
4757
		}
4758
	}
4759
}
4760

    
4761
function upgrade_146_to_147() {
4762

    
4763
	upgrade_bgpd_146_to_147();
4764
	upgrade_quagga_146_to_147();
4765
}
4766

    
4767
function upgrade_147_to_148() {
4768
	global $config;
4769

    
4770
	// Ensure there are no spaces in group names by
4771
	// replacing spaces with underscores
4772
	if (is_array($config['system']['group'])) {
4773
		$cleargroups = false;
4774
		foreach ($config['system']['group'] as $idx => $grp) {
4775
			if (strstr($grp['name'], " ")) {
4776
				$cleargroups = true;
4777
				$config['system']['group'][$idx]['scope'] = "remote";
4778
			}
4779
		}
4780

    
4781
		// if there was a space in a group name, there may be multiple
4782
		// groups with the same name in the group file. To prevent pw 
4783
		// from getting into a neverending loop, delete all user-defined
4784
		// groups here. local_sync_accounts will run shortly after this
4785
		// and add them back. redmine #6012
4786
		if ($cleargroups) {
4787
			foreach ($config['system']['group'] as $grp) {
4788
				mwexec("/usr/sbin/pw groupdel -g {$grp['gid']}");
4789
			}
4790
		}
4791
	}
4792
}
4793

    
4794
function upgrade_148_to_149() {
4795
	global $config;
4796
	global $altq_list_queues;
4797

    
4798
        if (!isset($config['shaper']['queue']) || !is_array($config['shaper']['queue']))
4799
                return;
4800

    
4801
	read_altq_config();
4802

    
4803
	/* Set root queue bandwidth. */
4804
	foreach ($altq_list_queues as $altq) {
4805
		$sum = $altq->GetTotalBw();
4806
		while ($sum > get_queue_bandwidth($altq)) {
4807
			if (intval(($sum / 1000) * 1.2) < (1024 * 1024)) {
4808
				/* 1Gb where possible. */
4809
				$bw = 1024 * 1024;
4810
			} else {
4811
				/* Increase by 20% until it fits. */
4812
				$bw = intval(($sum / 1000) * 1.2);
4813
			}
4814
			$altq->SetBandwidth($bw);
4815
			$altq->SetBwscale("Kb");
4816
			$altq->wconfig();
4817
			$sum = $altq->GetTotalBw();
4818
		}
4819
	}
4820
}
4821

    
4822
function upgrade_149_to_150() {
4823
	global $config;
4824

    
4825
	if (is_array($config['dhcpdv6'])) {
4826
                foreach ($config['dhcpdv6'] as &$dhcpdv6) {
4827
			if (isset($dhcpdv6['rainterface'])) {
4828
				if (strstr($dhcpdv6['rainterface'], "_vip")) {
4829
					$dhcpdv6['rainterface'] = get_vip_from_oldcarp($dhcpdv6['rainterface']);
4830
				}
4831
			}
4832
		}
4833
	}
4834
}
4835

    
4836
function upgrade_150_to_151() {
4837
	global $config;
4838

    
4839
	// Default dpinger parameters at time of this upgrade (2.3.1)
4840
	$default_interval = 500;
4841
	$default_alert_interval = 1000;
4842
	$default_loss_interval = 2000;
4843
	$default_time_period = 60000;
4844
	$default_latencyhigh = 500;
4845

    
4846
	// Check advanced gateway parameter relationships in case they are incorrect
4847
	if (is_array($config['gateways']['gateway_item'])) {
4848
		foreach ($config['gateways']['gateway_item'] as &$gw) {
4849
			if (isset($gw['interval'])) {
4850
				$effective_interval = $gw['interval'];
4851
			} else {
4852
				$effective_interval = $default_interval;
4853
			}
4854

    
4855
			if (isset($gw['alert_interval'])) {
4856
				$effective_alert_interval = $gw['alert_interval'];
4857
			} else {
4858
				$effective_alert_interval = $default_alert_interval;
4859
			}
4860

    
4861
			if (isset($gw['loss_interval'])) {
4862
				$effective_loss_interval = $gw['loss_interval'];
4863
			} else {
4864
				$effective_loss_interval = $default_loss_interval;
4865
			}
4866

    
4867
			if (isset($gw['time_period'])) {
4868
				$effective_time_period = $gw['time_period'];
4869
			} else {
4870
				$effective_time_period = $default_time_period;
4871
			}
4872

    
4873
			if (isset($gw['latencyhigh'])) {
4874
				$effective_latencyhigh = $gw['latencyhigh'];
4875
			} else {
4876
				$effective_latencyhigh = $default_latencyhigh;
4877
			}
4878

    
4879
			// Loss interval has to be at least as big as high latency.
4880
			if ($effective_latencyhigh > $effective_loss_interval) {
4881
				$effective_loss_interval = $gw['loss_interval'] = $effective_latencyhigh;
4882
			}
4883

    
4884
			// Alert interval has to be at least as big as probe interval.
4885
			if ($effective_interval > $effective_alert_interval) {
4886
				$gw['alert_interval'] = $effective_interval;
4887
			}
4888

    
4889
			// The time period for averaging has to be more than 2 probes plus the loss interval.
4890
			if ((($effective_interval * 2) + $effective_loss_interval) >= $effective_time_period) {
4891
				$gw['time_period'] = ($effective_interval * 2) + $effective_loss_interval + 1;
4892
			}
4893
		}
4894
	}
4895
}
4896

    
4897
function upgrade_151_to_152() {
4898
	global $g, $config;
4899

    
4900
	require_once("/etc/inc/services.inc");
4901

    
4902
	// Remove these cron jobs on full install if not using ramdisk.
4903
	if (($g['platform'] == $g['product_name']) && !isset($config['system']['use_mfs_tmpvar'])) {
4904
		install_cron_job("/etc/rc.backup_rrd.sh", false);
4905
		install_cron_job("/etc/rc.backup_dhcpleases.sh", false);
4906
	}
4907
}
4908

    
4909
function upgrade_152_to_153() {
4910
	global $config;
4911

    
4912
	if (is_array($config['virtualip']['vip'])) {
4913
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4914
			if (substr($vip['interface'], 0, 4) == "_vip") {
4915
				// using new VIP format
4916
				continue;
4917
			} else if (strstr($vip['interface'], "_vip")) {
4918
				// using old VIP format, update
4919
				$config['virtualip']['vip'][$idx]['interface'] = get_vip_from_oldcarp($vip['interface']);
4920
			}
4921
		}
4922
	}
4923

    
4924
	// upgrade GIFs using VIP to new format
4925
	if (is_array($config['gifs']['gif'])) {
4926
		foreach ($config['gifs']['gif'] as $idx => $gif) {
4927
			if (substr($gif['if'], 0, 4) == "_vip") {
4928
				// using new VIP format
4929
				continue;
4930
			} else if (strstr($gif['if'], "_vip")) {
4931
				// using old VIP format, update
4932
				$config['gifs']['gif'][$idx]['if'] = get_vip_from_oldcarp($gif['if']);
4933
			}
4934
		}
4935
	}
4936

    
4937
	// upgrade GREs using VIP to new format
4938
	if (is_array($config['gres']['gre'])) {
4939
		foreach ($config['gres']['gre'] as $idx => $gre) {
4940
			if (substr($gre['if'], 0, 4) == "_vip") {
4941
				// using new VIP format
4942
				continue;
4943
			} else if (strstr($gre['if'], "_vip")) {
4944
				// using old VIP format, update
4945
				$config['gres']['gre'][$idx]['if'] = get_vip_from_oldcarp($gre['if']);
4946
			}
4947
		}
4948
	}
4949

    
4950
	// upgrade gateway groups using VIPs
4951
	if (is_array($config['gateways']['gateway_group'])) {
4952
		foreach ($config['gateways']['gateway_group'] as $idx => $gw) {
4953
			if (is_array($gw['item'])) {
4954
				$newitems = array();
4955
				$gwvipchange = false;
4956
				foreach ($gw['item'] as $item) {
4957
					if (strstr($item, "|_vip")) {
4958
						// using new VIP format
4959
						$newitems[] = $item;
4960
						continue;
4961
					} else if (strstr($item, "_vip")) {
4962
						// using old VIP format, update
4963
						$gwitemarr = explode("|", $item);
4964
						$gwitemarr[2] = get_vip_from_oldcarp($gwitemarr[2]);
4965
						$newitems[] = implode("|", $gwitemarr);
4966
						$gwvipchange = true;
4967
					} else {
4968
						$newitems[] = $item;
4969
					}
4970
				}
4971
				if ($gwvipchange) {
4972
					$config['gateways']['gateway_group'][$idx]['item'] = $newitems;
4973
				}
4974
			}
4975
		}
4976
	}
4977
}
4978

    
4979
function upgrade_153_to_154() {
4980
	/* NOTE: This upgrade code was reverted. See redmine ticket #6118 and
4981
	   https://github.com/pfsense/pfsense/commit/538a3c04a6b6671151e913b06b2f340b6f8ee222 */
4982
}
4983
?>
(54-54/65)