Project

General

Profile

Download (125 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("rrd.inc");
58
}
59

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

    
69
	/* convert DMZ to optional, if necessary */
70
	if (isset($config['interfaces']['dmz'])) {
71

    
72
		$dmzcfg = &$config['interfaces']['dmz'];
73

    
74
		if ($dmzcfg['if']) {
75
			$config['interfaces']['opt' . $opti] = array();
76
			$optcfg = &$config['interfaces']['opt' . $opti];
77

    
78
			$optcfg['enable'] = $dmzcfg['enable'];
79
			$optcfg['descr'] = "DMZ";
80
			$optcfg['if'] = $dmzcfg['if'];
81
			$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
82
			$optcfg['subnet'] = $dmzcfg['subnet'];
83

    
84
			$ifmap['dmz'] = "opt" . $opti;
85
			$opti++;
86
		}
87

    
88
		unset($config['interfaces']['dmz']);
89
	}
90

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

    
94
		if (!$config['interfaces']['wlan' . $i]['if']) {
95
			unset($config['interfaces']['wlan' . $i]);
96
			continue;
97
		}
98

    
99
		$wlancfg = &$config['interfaces']['wlan' . $i];
100
		$config['interfaces']['opt' . $opti] = array();
101
		$optcfg = &$config['interfaces']['opt' . $opti];
102

    
103
		$optcfg['enable'] = $wlancfg['enable'];
104
		$optcfg['descr'] = "WLAN" . $i;
105
		$optcfg['if'] = $wlancfg['if'];
106
		$optcfg['ipaddr'] = $wlancfg['ipaddr'];
107
		$optcfg['subnet'] = $wlancfg['subnet'];
108
		$optcfg['bridge'] = $wlancfg['bridge'];
109

    
110
		$optcfg['wireless'] = array();
111
		$optcfg['wireless']['mode'] = $wlancfg['mode'];
112
		$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
113
		$optcfg['wireless']['channel'] = $wlancfg['channel'];
114
		$optcfg['wireless']['wep'] = $wlancfg['wep'];
115

    
116
		$ifmap['wlan' . $i] = "opt" . $opti;
117

    
118
		unset($config['interfaces']['wlan' . $i]);
119
		$opti++;
120
	}
121

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

    
126
		$fr = &$config['filter']['rule'][$i];
127

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

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

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

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

    
171
			$fr = &$config['pfqueueing']['rule'][$i];
172

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

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

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

    
213

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

    
221
	/* encrypt password */
222
	$config['system']['password'] = crypt($config['system']['password']);
223
}
224

    
225

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

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

    
244

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

    
251
		for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
252
			$curent = &$config['pfqueueing']['rule'][$i];
253

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

    
262
			$curent['targetpipe'] = $i;
263

    
264
			unset($curent['bandwidth']);
265
			unset($curent['delay']);
266
			unset($curent['mask']);
267
		}
268
	}
269
}
270

    
271

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

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

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

    
297

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

    
315

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

    
340

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

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

    
396

    
397
function upgrade_018_to_019() {
398
	global $config;
399
}
400

    
401

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

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

    
425

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

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

    
440

    
441
function upgrade_023_to_024() {
442
	global $config;
443
}
444

    
445

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

    
454

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

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

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

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

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

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

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

    
499
	$config['cron']['item'][] = $cron_item;
500

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

    
510
	$config['cron']['item'][] = $cron_item;
511

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

    
521
	$config['cron']['item'][] = $cron_item;
522

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

    
532
	$config['cron']['item'][] = $cron_item;
533
}
534

    
535

    
536
function upgrade_026_to_027() {
537
	global $config;
538
}
539

    
540

    
541
function upgrade_027_to_028() {
542
	global $config;
543
}
544

    
545

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

    
559

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

    
566

    
567
function upgrade_030_to_031() {
568
	global $config;
569
	/* Insert upgrade code here */
570
}
571

    
572

    
573
function upgrade_031_to_032() {
574
	global $config;
575
	/* Insert upgrade code here */
576
}
577

    
578

    
579
function upgrade_032_to_033() {
580
	global $config;
581
	/* Insert upgrade code here */
582
}
583

    
584

    
585
function upgrade_033_to_034() {
586
	global $config;
587
	/* Insert upgrade code here */
588
}
589

    
590

    
591
function upgrade_034_to_035() {
592
	global $config;
593
	/* Insert upgrade code here */
594
}
595

    
596

    
597
function upgrade_035_to_036() {
598
	global $config;
599
	/* Insert upgrade code here */
600
}
601

    
602

    
603
function upgrade_036_to_037() {
604
	global $config;
605
	/* Insert upgrade code here */
606
}
607

    
608

    
609
function upgrade_037_to_038() {
610
	global $config;
611
	/* Insert upgrade code here */
612
}
613

    
614

    
615
function upgrade_038_to_039() {
616
	global $config;
617
	/* Insert upgrade code here */
618
}
619

    
620

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

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

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

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

    
662
		$config['system']['nextuid'] = "111";
663
		$config['system']['nextgid'] = "111";
664

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

    
673
function upgrade_040_to_041() {
674
	global $config;
675
	if (!$config['sysctl']) {
676
		$config['sysctl']['item'] = array();
677

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
758
	}
759
}
760

    
761

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

    
772

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

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

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

    
826

    
827
function upgrade_043_to_044() {
828
	global $config;
829

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

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

    
868

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

    
889

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

    
974

    
975
function upgrade_046_to_047() {
976
	global $config;
977
	/* Upgrade IPsec from tunnel to phase1/phase2 */
978

    
979
	if (is_array($config['ipsec']['tunnel'])) {
980

    
981
		$a_phase1 = array();
982
		$a_phase2 = array();
983
		$ikeid = 0;
984

    
985
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
986

    
987
			unset($ph1ent);
988
			unset($ph2ent);
989

    
990
			/*
991
				*  attempt to locate an enabled phase1
992
				*  entry that matches the peer gateway
993
				*/
994

    
995
			if (!isset($tunnel['disabled'])) {
996

    
997
				$remote_gateway = $tunnel['remote-gateway'];
998

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

    
1007
			/* none found, create a new one */
1008

    
1009
			if (!isset($ph1ent)) {
1010

    
1011
				/* build new phase1 entry */
1012

    
1013
				$ph1ent = array();
1014

    
1015
				$ph1ent['ikeid'] = ++$ikeid;
1016

    
1017
				if (isset($tunnel['disabled'])) {
1018
					$ph1ent['disabled'] = $tunnel['disabled'];
1019
				}
1020

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

    
1030
				$ph1ent['mode'] = $tunnel['p1']['mode'];
1031

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

    
1056
				$ph1ent['peerid_type'] = "peeraddress";
1057

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

    
1080
				$ph1ent['encryption-algorithm'] = $ph1alg;
1081
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1082
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1083
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1084
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1085

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

    
1099
				$ph1ent['nat_traversal'] = "on";
1100
				$ph1ent['dpd_enable'] = 1;
1101
				$ph1ent['dpd_delay'] = 10;
1102
				$ph1ent['dpd_maxfail'] = 5;
1103

    
1104
				$a_phase1[] = $ph1ent;
1105
			}
1106

    
1107
			/* build new phase2 entry */
1108

    
1109
			$ph2ent = array();
1110

    
1111
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1112

    
1113
			if (isset($tunnel['disabled'])) {
1114
				$ph1ent['disabled'] = $tunnel['disabled'];
1115
			}
1116

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

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

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

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

    
1147
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1148

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

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

    
1179
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1180
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1181
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1182

    
1183
			if (isset($tunnel['pinghost']['pinghost'])) {
1184
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1185
			}
1186

    
1187
			$a_phase2[] = $ph2ent;
1188
		}
1189

    
1190
		unset($config['ipsec']['tunnel']);
1191
		$config['ipsec']['phase1'] = $a_phase1;
1192
		$config['ipsec']['phase2'] = $a_phase2;
1193
	}
1194

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

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

    
1207
		$mobilecfg = $config['ipsec']['mobileclients'];
1208

    
1209
		$ph1ent = array();
1210
		$ph1ent['ikeid'] = ++$ikeid;
1211

    
1212
		if (!isset($mobilecfg['enable'])) {
1213
			$ph1ent['disabled'] = true;
1214
		}
1215

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

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

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

    
1269
		$ph1ent['encryption-algorithm'] = $ph1alg;
1270
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1271
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1272
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1273
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1274

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

    
1285
		$ph1ent['nat_traversal'] = "on";
1286
		$ph1ent['dpd_enable'] = 1;
1287
		$ph1ent['dpd_delay'] = 10;
1288
		$ph1ent['dpd_maxfail'] = 5;
1289
		$ph1ent['mobile'] = true;
1290

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

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

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

    
1332
		$config['ipsec']['phase1'][] = $ph1ent;
1333
		$config['ipsec']['phase2'][] = $ph2ent;
1334
		unset($config['ipsec']['mobileclients']);
1335
	}
1336
}
1337

    
1338

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

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

    
1402

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

    
1413
	if (!is_array($config['system']['user'])) {
1414
		$config['system']['user'] = array();
1415
	}
1416
	if (!is_array($config['system']['group'])) {
1417
		$config['system']['group'] = array();
1418
	}
1419

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

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

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

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

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

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

    
1470

    
1471
function upgrade_049_to_050() {
1472
	global $config;
1473

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

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

    
1514
	/* sync all local account information */
1515
	local_sync_accounts();
1516
}
1517

    
1518

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

    
1532
	if (isset($config['bridge'])) {
1533
		unset($config['bridge']);
1534
	}
1535

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

    
1561

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

    
1572
	$vpnid = 1;
1573

    
1574
	/* openvpn server configurations */
1575
	if (is_array($config['installedpackages']['openvpnserver'])) {
1576
		$config['openvpn']['openvpn-server'] = array();
1577

    
1578
		$index = 1;
1579
		foreach ($config['installedpackages']['openvpnserver']['config'] as $server) {
1580

    
1581
			if (!is_array($server)) {
1582
				continue;
1583
			}
1584

    
1585
			if ($server['auth_method'] == "pki") {
1586

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

    
1594
				/* create ca reference */
1595
				unset($server['ca_cert']);
1596
				$server['caref'] = $ca['refid'];
1597

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

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

    
1621
				/* create cert reference */
1622
				unset($server['server_cert']);
1623
				unset($server['server_key']);
1624
				$server['certref'] = $cert['refid'];
1625

    
1626
				$index++;
1627
			}
1628

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

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

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

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

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

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

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

    
1705
			if (!empty($server['disable'])) {
1706
				$server['disable'] = true;
1707
			} else {
1708
				unset($server['disable']);
1709
			}
1710

    
1711
			/* allocate vpnid */
1712
			$server['vpnid'] = $vpnid++;
1713

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

    
1742
			$config['openvpn']['openvpn-server'][] = $server;
1743
		}
1744
		unset($config['installedpackages']['openvpnserver']);
1745
	}
1746

    
1747
	/* openvpn client configurations */
1748
	if (is_array($config['installedpackages']['openvpnclient'])) {
1749
		$config['openvpn']['openvpn-client'] = array();
1750

    
1751
		$index = 1;
1752
		foreach ($config['installedpackages']['openvpnclient']['config'] as $client) {
1753

    
1754
			if (!is_array($client)) {
1755
				continue;
1756
			}
1757

    
1758
			if ($client['auth_method'] == "pki") {
1759

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

    
1768
				/* create ca reference */
1769
				unset($client['ca_cert']);
1770
				unset($client['crl']);
1771
				$client['caref'] = $ca['refid'];
1772

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

    
1781
				/* create cert reference */
1782
				unset($client['client_cert']);
1783
				unset($client['client_key']);
1784
				$client['certref'] = $cert['refid'];
1785

    
1786
				$index++;
1787
			}
1788

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

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

    
1816
			/* allocate vpnid */
1817
			$client['vpnid'] = $vpnid++;
1818

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

    
1847
			if (!empty($client['disable'])) {
1848
				$client['disable'] = true;
1849
			} else {
1850
				unset($client['disable']);
1851
			}
1852

    
1853
			$config['openvpn']['openvpn-client'][] = $client;
1854
		}
1855

    
1856
		unset($config['installedpackages']['openvpnclient']);
1857
	}
1858

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

    
1863
		foreach ($config['installedpackages']['openvpncsc']['config'] as $csc) {
1864

    
1865
			if (!is_array($csc)) {
1866
				continue;
1867
			}
1868

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

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

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

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

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

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

    
1920
			if (!empty($csc['disable'])) {
1921
				$csc['disable'] = true;
1922
			} else {
1923
				unset($csc['disable']);
1924
			}
1925

    
1926
			$config['openvpn']['openvpn-csc'][] = $csc;
1927
		}
1928

    
1929
		unset($config['installedpackages']['openvpncsc']);
1930
	}
1931

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

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

    
1957
}
1958

    
1959

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

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

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

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

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

    
1995

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

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

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

    
2085

    
2086
function upgrade_054_to_055() {
2087
	global $config;
2088
	global $g;
2089

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

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

    
2106
	$rrdinterval = 60;
2107
	$valid = $rrdinterval * 2;
2108

    
2109
	/* Asume GigE for now */
2110
	$downstream = 125000000;
2111
	$upstream = 125000000;
2112

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

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

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

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

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

    
2156
		unset($rrdold);
2157
		@unlink("{$g['tmp_path']}/{$xmldump}");
2158
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2159
	}
2160

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

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

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

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

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

    
2195
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2196
		$rrdold = $rrdold['rrd'];
2197

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

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

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

    
2234

    
2235
function upgrade_055_to_056() {
2236
	global $config;
2237

    
2238
	if (!is_array($config['ca'])) {
2239
		$config['ca'] = array();
2240
	}
2241
	if (!is_array($config['cert'])) {
2242
		$config['cert'] = array();
2243
	}
2244

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

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

    
2288

    
2289
function upgrade_056_to_057() {
2290
	global $config;
2291

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

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

    
2335
function upgrade_058_to_059() {
2336
	global $config;
2337

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

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

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

    
2377
function upgrade_060_to_061() {
2378
	global $config;
2379

    
2380
	if (is_array($config['interfaces']['wan'])) {
2381
		$config['interfaces']['wan']['enable'] = true;
2382
	}
2383
	if (is_array($config['interfaces']['lan'])) {
2384
		$config['interfaces']['lan']['enable'] = true;
2385
	}
2386

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

    
2399
function upgrade_061_to_062() {
2400
	global $config;
2401

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

    
2406
		foreach ($a_nat as &$natent) {
2407
			$natent['disabled'] = false;
2408
			$natent['nordr']    = false;
2409

    
2410
			$natent['source'] = array(
2411
				"not"     => false,
2412
				"any"     => true,
2413
				"port"    => ""
2414
			);
2415

    
2416
			$natent['destination'] = array(
2417
				"not"     => false,
2418
				"address" => $natent['external-address'],
2419
				"port"    => $natent['external-port']
2420
			);
2421

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

    
2430
			unset($natent['external-address']);
2431
			unset($natent['external-port']);
2432
		}
2433

    
2434
		unset($natent);
2435
	}
2436
}
2437

    
2438
function upgrade_062_to_063() {
2439
	/* Upgrade legacy Themes to the new pfsense_ng */
2440
	// Not supported in 2.3+
2441

    
2442
}
2443

    
2444
function upgrade_063_to_064() {
2445
	global $config;
2446
	$j = 0;
2447
	$ifcfg = &$config['interfaces'];
2448

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

    
2467
	if (!is_array($config['ppps']['ppp'])) {
2468
		$config['ppps']['ppp'] = array();
2469
	}
2470
	$a_ppps = &$config['ppps']['ppp'];
2471

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

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

    
2522
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2523
			$j++;
2524

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

    
2539
			$a_ppps[] = $ppp;
2540

    
2541
		}
2542
	}
2543
}
2544

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

    
2552
function upgrade_065_to_066() {
2553
	global $config;
2554

    
2555
	$dhcrelaycfg =& $config['dhcrelay'];
2556

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

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

    
2586
function upgrade_067_to_068() {
2587
	global $config;
2588

    
2589
	if (!empty($config['pppoe'])) {
2590
		$config['pppoes'] = array();
2591
		$config['pppoes']['pppoe'] = array();
2592
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2593

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

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

    
2639
function upgrade_069_to_070() {
2640
	global $config;
2641

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

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

    
2653
			unset($config['nat']['onetoone'][$nidx]['internal']);
2654
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2655
		}
2656

    
2657
		unset($natent);
2658
	}
2659
}
2660

    
2661
function upgrade_070_to_071() {
2662
	global $config;
2663

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

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

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

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

    
2716
function upgrade_073_to_074() {
2717
	global $config;
2718
	rename_field($config['system']['user'], 'fullname', 'descr');
2719
}
2720

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

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

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

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

    
2784
function upgrade_079_to_080() {
2785
	global $config;
2786

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

    
2795
function upgrade_080_to_081() {
2796
	global $config;
2797
	global $g;
2798
	/* Welcome to the 2.1 migration path */
2799

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

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

    
2814
	$rrddbpath = "/var/db/rrd/";
2815
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2816

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

    
2825
	$rrdinterval = 60;
2826
	$valid = $rrdinterval * 2;
2827

    
2828
	/* Asume GigE for now */
2829
	$downstream = 125000000;
2830
	$upstream = 125000000;
2831

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

    
2839
		if (platform_booting()) {
2840
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2841
		}
2842

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

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

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

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

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

    
2895
				<!-- PDP Status -->
2896
				<last_ds> 0 </last_ds>
2897
				<value> NaN </value>
2898
				<unknown_sec> 3 </unknown_sec>
2899
			</ds>
2900
			";
2901

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

    
2934
		$value_search = "<\/row>";
2935
		$value_replace = "</row>";
2936
		$value = "<v> NaN </v>";
2937

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

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

    
2973
function upgrade_081_to_082() {
2974
	/* don't enable the allow IPv6 toggle */
2975
}
2976

    
2977
function upgrade_082_to_083() {
2978
	global $config;
2979

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

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

    
3018
function upgrade_084_to_085() {
3019
	global $config;
3020

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

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

    
3064
function upgrade_085_to_086() {
3065
	global $config, $g;
3066

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

    
3083
function upgrade_086_to_087() {
3084
	global $config, $dummynet_pipe_list;
3085

    
3086
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue'])) {
3087
		return;
3088
	}
3089

    
3090
	$dnqueue_number = 1;
3091
	$dnpipe_number = 1;
3092

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

    
3104
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
3105

    
3106
	if (!is_array($config['filter']) || !is_array($config['filter']['rule'])) {
3107
		return;
3108
	}
3109

    
3110
	require_once("shaper.inc");
3111
	read_dummynet_config();
3112

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

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

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

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

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

    
3173
					/* add ca reference to certificate */
3174
					$cert['caref'] = $ca['refid'];
3175
				}
3176

    
3177
				$config['cert'][] = $cert;
3178

    
3179
				/* create cert reference */
3180
				$setting['certref'] = $cert['refid'];
3181

    
3182
				unset($setting['certificate']);
3183
				unset($setting['private-key']);
3184
				unset($setting['cacertificate']);
3185

    
3186
			}
3187
		}
3188
	}
3189
}
3190

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

    
3207
function upgrade_090_to_091() {
3208
	global $config;
3209

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

    
3225
function upgrade_091_to_092() {
3226
	global $config;
3227

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

    
3238
function upgrade_092_to_093() {
3239
	global $g;
3240

    
3241
	$suffixes = array("concurrent", "loggedin");
3242

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

    
3250
	if (!platform_booting()) {
3251
		enable_rrd_graphing();
3252
	}
3253
}
3254

    
3255
function upgrade_093_to_094() {
3256
	global $config;
3257

    
3258
	if (isset($config['system']['powerd_mode'])) {
3259
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3260
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3261
		unset($config['system']['powerd_mode']);
3262
	}
3263
}
3264

    
3265
function upgrade_094_to_095() {
3266
	global $config;
3267

    
3268
	if (!isset($config['interfaces']) || !is_array($config['interfaces'])) {
3269
		return;
3270
	}
3271

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

    
3281
function upgrade_095_to_096() {
3282
	global $config, $g;
3283

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

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

    
3297
	/* Assume 2*10GigE for now */
3298
	$stream = 2500000000;
3299

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

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

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

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

    
3332
function upgrade_097_to_098() {
3333
	// no longer used (used to set kill_states)
3334
	return;
3335
}
3336

    
3337
function upgrade_098_to_099() {
3338
	global $config;
3339

    
3340
	if (empty($config['dhcpd']) || !is_array($config['dhcpd'])) {
3341
		return;
3342
	}
3343

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

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

    
3357
function upgrade_100_to_101() {
3358
	global $config, $g;
3359

    
3360
	if (!is_array($config['voucher'])) {
3361
		return;
3362
	}
3363

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

    
3376
function upgrade_101_to_102() {
3377
	global $config, $g;
3378

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

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

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

    
3411
function upgrade_102_to_103() {
3412
	global $config;
3413

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

    
3421
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3422

    
3423
	if (isset($config['nat']['ipsecpassthru'])) {
3424
		unset($config['nat']['ipsecpassthru']);
3425
	}
3426
	if (isset($config['nat']['advancedoutbound'])) {
3427
		unset($config['nat']['advancedoutbound']);
3428
	}
3429
}
3430

    
3431
function upgrade_103_to_104() {
3432
	global $config;
3433

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

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

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

    
3465
	/* sync all local account information */
3466
	local_sync_accounts();
3467
}
3468

    
3469
function upgrade_104_to_105() {
3470
	global $config;
3471

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

    
3486
function upgrade_105_to_106() {
3487

    
3488
	/* NOTE: This entry can be reused for something else since the upgrade code was reverted */
3489
}
3490

    
3491
function upgrade_106_to_107() {
3492
	global $config;
3493

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

    
3516
function upgrade_107_to_108() {
3517
	global $config;
3518

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

    
3526
function upgrade_108_to_109() {
3527
	global $config;
3528

    
3529
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule'])) {
3530
		return;
3531
	}
3532

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

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

    
3546
function upgrade_109_to_110() {
3547
	global $config;
3548

    
3549
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3550
		return;
3551
	}
3552

    
3553
	foreach ($config['ipsec']['phase2'] as &$rule) {
3554
		if (!empty($rule['uniqid'])) {
3555
			continue;
3556
		}
3557

    
3558
		$rule['uniqid'] = uniqid();
3559
	}
3560
}
3561

    
3562
function upgrade_110_to_111() {
3563
	global $config;
3564

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

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

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

    
3585
			unset($config['installedpackages']['menu'][$idx]);
3586
			break;
3587
		}
3588
	}
3589

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

    
3600
	if (!isset($config['installedpackages']['unbound']['config'][0])) {
3601
		return;
3602
	}
3603

    
3604
	$pkg = $config['installedpackages']['unbound']['config'][0];
3605

    
3606
	if (isset($config['installedpackages']['unboundadvanced']['config'][0])) {
3607
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3608
	}
3609

    
3610
	$new = array();
3611

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

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

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

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

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

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

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

    
3687
	$config['unbound'] = $new;
3688

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

    
3699
	unset($pkg, $new);
3700
}
3701

    
3702
function upgrade_111_to_112() {
3703
	global $config;
3704

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

    
3716
function upgrade_112_to_113() {
3717
	global $config;
3718

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

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

    
3736
function upgrade_113_to_114() {
3737
	global $config;
3738

    
3739
	if (!isset($config['ipsec']['phase1']) ||
3740
	    !is_array($config['ipsec']['phase1'])) {
3741
		return;
3742
	}
3743

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

    
3751
function upgrade_114_to_115() {
3752
	global $config;
3753

    
3754
	if (isset($config['unbound']['custom_options'])) {
3755
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3756
	}
3757
}
3758

    
3759
function upgrade_115_to_116() {
3760
	global $config;
3761

    
3762
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3763
		return;
3764
	}
3765

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

    
3773
function upgrade_116_to_117() {
3774
	global $config;
3775

    
3776
	if (!isset($config['ipsec']['client']) ||
3777
	    !isset($config['ipsec']['client']['dns_split']) ||
3778
	    empty($config['ipsec']['client']['dns_split'])) {
3779
		return;
3780
	}
3781

    
3782
	$config['ipsec']['client']['dns_split'] =
3783
		preg_replace('/\s*,\s*/', ' ', trim($config['ipsec']['client']['dns_split']));
3784

    
3785
}
3786

    
3787
function upgrade_117_to_118() {
3788
	global $config;
3789

    
3790
	// 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.
3791
	if (isset($config['system']['ca'])) {
3792
		unset($config['system']['ca']);
3793
	}
3794
	if (isset($config['system']['cert'])) {
3795
		unset($config['system']['cert']);
3796
	}
3797

    
3798
	if (!isset($config['ipsec']['phase1'])) {
3799
		return;
3800
	}
3801

    
3802
	$a_phase1 =& $config['ipsec']['phase1'];
3803

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

    
3817
function upgrade_118_to_119() {
3818
	global $config;
3819

    
3820
	if (!isset($config['ipsec']['phase1'])) {
3821
		return;
3822
	}
3823

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

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

    
3834
function upgrade_119_to_120() {
3835
	require_once("ipsec.inc");
3836
	global $config, $ipsec_log_cats;
3837

    
3838
	if (!is_array($config['ipsec'])) {
3839
		return;
3840
	}
3841

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

    
3849
}
3850

    
3851

    
3852
function upgrade_120_to_121() {
3853
	global $config;
3854

    
3855
	if (!isset($config['installedpackages']['miniupnpd']['config'][0])) {
3856
		return;
3857
	}
3858

    
3859
	$miniupnpd =& $config['installedpackages']['miniupnpd']['config'][0];
3860

    
3861
	$miniupnpd['row'] = array();
3862

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

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

    
3880
function upgrade_122_to_123() {
3881
	global $config;
3882

    
3883
	// PPTP server was removed
3884
	if (isset($config['pptpd'])) {
3885
		unset($config['pptpd']);
3886
	}
3887

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

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

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

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

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

    
3978
function upgrade_123_to_124() {
3979
	if (isset($config['system']['altpkgrepo'])) {
3980
		unset($config['system']['altpkgrepo']);
3981
	}
3982

    
3983
	if (isset($config['theme'])) {
3984
		unset($config['theme']);
3985
	}
3986
}
3987

    
3988
function upgrade_124_to_125() {
3989
	global $config;
3990

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

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

    
4011
function upgrade_125_to_126() {
4012
	require_once("ipsec.inc");
4013
	global $config, $ipsec_log_cats, $ipsec_log_sevs;
4014

    
4015
	$def_loglevel = 1;
4016
	if (!is_array($config['ipsec'])) {
4017
		return;
4018
	}
4019

    
4020
	if (!isset($config['ipsec']['logging']) || !is_array($config['ipsec']['logging'])) {
4021
		$config['ipsec']['logging'] = array();
4022
	}
4023

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

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

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

    
4051
	if (!isset($config['widgets']['sequence'])) {
4052
		return;
4053
	}
4054

    
4055
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4056
	$new_widgets = array();
4057

    
4058
	foreach ($cur_widgets as $widget) {
4059
		list($file, $col, $display) = explode(':', $widget);
4060

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

    
4074
		/* Remove '-container' from widget name */
4075
		$file = preg_replace('/-container$/', '', $file);
4076

    
4077
		$new_widgets[] = "{$file}:{$col}:{$display}";
4078
	}
4079

    
4080
	$config['widgets']['sequence'] = implode(',', $new_widgets);
4081

    
4082
}
4083

    
4084
function upgrade_127_to_128() {
4085
	global $config;
4086

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

    
4096
function upgrade_128_to_129() {
4097
	global $config;
4098

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

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

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

    
4128
function upgrade_129_to_130() {
4129
	global $config;
4130

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

    
4144
function upgrade_130_to_131() {
4145
	global $config;
4146

    
4147
	if (isset($config['syslog']['apinger'])) {
4148
		$config['syslog']['dpinger'] = true;
4149
		unset($config['syslog']['apinger']);
4150
	}
4151

    
4152
	if (isset($config['system']['apinger_debug'])) {
4153
		unset($config['system']['apinger_debug']);
4154
	}
4155

    
4156
	if (!isset($config['gateways']['gateway_item']) ||
4157
	    !is_array($config['gateways']['gateway_item'])) {
4158
		return;
4159
	}
4160

    
4161
	foreach ($config['gateways']['gateway_item'] as &$gw) {
4162
		// dpinger uses milliseconds
4163
		if (isset($gw['interval']) &&
4164
		    is_numeric($gw['interval'])) {
4165
			$gw['interval'] = $gw['interval'] * 1000;
4166
		}
4167
		if (isset($gw['down']) &&
4168
		    is_numeric($gw['down'])) {
4169
			$gw['loss_interval'] = $gw['down'] * 1000;
4170
			unset($gw['down']);
4171
		}
4172

    
4173
		if (isset($gw['avg_delay_samples'])) {
4174
			unset($gw['avg_delay_samples']);
4175
		}
4176
		if (isset($gw['avg_delay_samples_calculated'])) {
4177
			unset($gw['avg_delay_samples_calculated']);
4178
		}
4179
		if (isset($gw['avg_loss_samples'])) {
4180
			unset($gw['avg_loss_samples']);
4181
		}
4182
		if (isset($gw['avg_loss_samples_calculated'])) {
4183
			unset($gw['avg_loss_samples_calculated']);
4184
		}
4185
		if (isset($gw['avg_loss_delay_samples'])) {
4186
			unset($gw['avg_loss_delay_samples']);
4187
		}
4188
		if (isset($gw['avg_loss_delay_samples_calculated'])) {
4189
			unset($gw['avg_loss_delay_samples_calculated']);
4190
		}
4191
	}
4192
}
4193

    
4194
function upgrade_131_to_132() {
4195
	global $config;
4196
	if (isset($config['system']['usefifolog'])) {
4197
		unset($config['system']['usefifolog']);
4198
		clear_all_log_files(false);
4199
	}
4200
}
4201

    
4202
function upgrade_132_to_133() {
4203
	global $config;
4204

    
4205
	if (isset($config['ipsec']['phase1']) &&
4206
	    is_array($config['ipsec']['phase1'])) {
4207
		foreach ($config['ipsec']['phase1'] as &$p1) {
4208
			if (isset($p1['encryption-algorithm']['name']) &&
4209
			    $p1['encryption-algorithm']['name'] == 'des') {
4210
				$p1['disabled'] = true;
4211
				file_notice("IPsec",
4212
				    "DES is no longer supported, IPsec phase 1 " .
4213
				    "item '{$p1['descr']}' is being disabled.");
4214
			}
4215
		}
4216
	}
4217

    
4218
	if (isset($config['ipsec']['phase2']) &&
4219
	    is_array($config['ipsec']['phase2'])) {
4220
		foreach ($config['ipsec']['phase2'] as &$p2) {
4221
			if (!isset($p2['encryption-algorithm-option']) ||
4222
			    !is_array($p2['encryption-algorithm-option'])) {
4223
				continue;
4224
			}
4225

    
4226
			foreach ($p2['encryption-algorithm-option'] as $ealgo) {
4227
				if ($ealgo['name'] == 'des') {
4228
					$p2['disabled'] = true;
4229
					file_notice("IPsec",
4230
					    "DES is no longer supported, IPsec phase 2 " .
4231
					    "item '{$p2['descr']}' is being disabled.");
4232
				}
4233
			}
4234
		}
4235
	}
4236
}
4237

    
4238
// Determine the highest column number in use and set dashboardcolumns accordingly
4239
function upgrade_133_to_134() {
4240
	global $config;
4241

    
4242
	if (!isset($config['widgets']['sequence']) || isset($config['system']['webgui']['dashboardcolumns'])) {
4243
		return;
4244
	}
4245

    
4246
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4247
	$maxcols = 2;
4248

    
4249
	foreach ($cur_widgets as $widget) {
4250
		list($file, $col, $display) = explode(':', $widget);
4251

    
4252
		if (($display != 'none') && ($display != 'hide')) {
4253
			preg_match('#[0-9]+$#', $col, $column);
4254
			if ($column[0] > $maxcols) {
4255
				$maxcols = $column[0];
4256
			}
4257
		}
4258
	}
4259

    
4260
	$config['system']['webgui']['dashboardcolumns'] = $maxcols % 10;
4261
}
4262

    
4263
function upgrade_134_to_135() {
4264
	global $config;
4265

    
4266
	if (isset($config['syslog']['nologlighttpd'])) {
4267
		unset($config['syslog']['nologlighttpd']);
4268
		$config['syslog']['nolognginx'] = true;
4269
	}
4270
}
4271

    
4272
function upgrade_135_to_136() {
4273
	global $config;
4274

    
4275
	if (isset($config['l7shaper'])) {
4276
		file_notice("L7shaper", "Layer 7 shaping is no longer supported. Its configuration has been removed.");
4277
		unset($config['l7shaper']);
4278
		if (is_array($config['filter']['rule'])) {
4279
			foreach ($config['filter']['rule'] as $idx => $rule) {
4280
				if (isset($rule['l7container'])) {
4281
					unset($config['filter']['rule'][$idx]['l7container']);
4282
				}
4283
			}
4284
		}
4285
	}
4286
}
4287

    
4288
function upgrade_136_to_137() {
4289
	global $config;
4290

    
4291
	if (is_array($config['dhcpd'])) {
4292
		foreach ($config['dhcpd'] as &$dhcpd) {
4293
			if (!is_array($dhcpd['numberoptions']['item'])) {
4294
				continue;
4295
			}
4296

    
4297
			foreach ($dhcpd['numberoptions']['item'] as &$item) {
4298
				$item['value'] = base64_encode($item['value']);
4299
			}
4300
		}
4301
	}
4302

    
4303
	if (is_array($config['dhcpdv6'])) {
4304
		foreach ($config['dhcpdv6'] as &$dhcpdv6) {
4305
			if (!is_array($dhcpdv6['numberoptions']['item'])) {
4306
				continue;
4307
			}
4308

    
4309
			foreach ($dhcpdv6['numberoptions']['item'] as &$item) {
4310
				$item['value'] = base64_encode($item['value']);
4311
			}
4312
		}
4313
	}
4314
}
4315

    
4316
function upgrade_137_to_138() {
4317
	global $config;
4318

    
4319
	// the presence of unityplugin tag used to disable loading of unity plugin
4320
	// it's now disabled by default, and config tag is to enable. Unset accordingly. 
4321
	if (is_array($config['ipsec'])) {
4322
		if (isset($config['ipsec']['unityplugin'])) {
4323
			unset($config['ipsec']['unityplugin']);
4324
		}
4325
	}
4326
}
4327

    
4328
function upgrade_138_to_139() {
4329
	global $config;
4330

    
4331
	// clean up state killing on gateway failure. having kill_states set used to mean it was disabled
4332
	// now set gw_down_kill_states if enabled. 
4333
	if (!isset($config['system']['kill_states'])) {
4334
		$config['system']['gw_down_kill_states'] = true;
4335
	} else {
4336
		unset($config['system']['kill_states']);
4337
	}
4338
}
4339

    
4340
?>
(54-54/65)