Project

General

Profile

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

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

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

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

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

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

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

    
38
if (!function_exists("dump_rrd_to_xml")) {
39
	require("rrd.inc");
40
}
41

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

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

    
54
		$dmzcfg = &$config['interfaces']['dmz'];
55

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

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

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

    
70
		unset($config['interfaces']['dmz']);
71
	}
72

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

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

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

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

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

    
98
		$ifmap['wlan' . $i] = "opt" . $opti;
99

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

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

    
108
		$fr = &$config['filter']['rule'][$i];
109

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

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

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

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

    
153
			$fr = &$config['pfqueueing']['rule'][$i];
154

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

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

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

    
195

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

    
203
	/* encrypt password */
204
	$config['system']['password'] = crypt($config['system']['password']);
205
}
206

    
207

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

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

    
226

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

    
233
		for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
234
			$curent = &$config['pfqueueing']['rule'][$i];
235

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

    
244
			$curent['targetpipe'] = $i;
245

    
246
			unset($curent['bandwidth']);
247
			unset($curent['delay']);
248
			unset($curent['mask']);
249
		}
250
	}
251
}
252

    
253

    
254
function upgrade_014_to_015() {
255
	global $config;
256
	/* Default route moved */
257
	if (isset($config['interfaces']['wan']['gateway'])) {
258
		if ($config['interfaces']['wan']['gateway'] <> "") {
259
			$config['system']['gateway'] = $config['interfaces']['wan']['gateway'];
260
		}
261
		unset($config['interfaces']['wan']['gateway']);
262
	}
263

    
264
	/* Queues are no longer interface specific */
265
	if (isset($config['interfaces']['lan']['schedulertype'])) {
266
		unset($config['interfaces']['lan']['schedulertype']);
267
	}
268
	if (isset($config['interfaces']['wan']['schedulertype'])) {
269
		unset($config['interfaces']['wan']['schedulertype']);
270
	}
271

    
272
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
273
		if (isset($config['interfaces']['opt' . $i]['schedulertype'])) {
274
			unset($config['interfaces']['opt' . $i]['schedulertype']);
275
		}
276
	}
277
}
278

    
279

    
280
function upgrade_015_to_016() {
281
	global $config;
282
	/* Alternate firmware URL moved */
283
	if (isset($config['system']['firmwareurl']) && isset($config['system']['firmwarename'])) { // Only convert if *both* are defined.
284
		$config['system']['alt_firmware_url'] = array();
285
		$config['system']['alt_firmware_url']['enabled'] = "";
286
		$config['system']['alt_firmware_url']['firmware_base_url'] = $config['system']['firmwareurl'];
287
		$config['system']['alt_firmware_url']['firmware_filename'] = $config['system']['firmwarename'];
288
	}
289
	if (isset($config['system']['firmwareurl'])) {
290
		unset($config['system']['firmwareurl']);
291
	}
292
	if (isset($config['system']['firmwarename'])) {
293
		unset($config['system']['firmwarename']);
294
	}
295
}
296

    
297

    
298
function upgrade_016_to_017() {
299
	global $config;
300
	/* wipe previous shaper configuration */
301
	if (isset($config['shaper']['queue'])) {
302
		unset($config['shaper']['queue']);
303
	}
304
	if (isset($config['shaper']['rule'])) {
305
		unset($config['shaper']['rule']);
306
	}
307
	if (isset($config['interfaces']['wan']['bandwidth'])) {
308
		unset($config['interfaces']['wan']['bandwidth']);
309
	}
310
	if (isset($config['interfaces']['wan']['bandwidthtype'])) {
311
		unset($config['interfaces']['wan']['bandwidthtype']);
312
	}
313
	if (isset($config['interfaces']['lan']['bandwidth'])) {
314
		unset($config['interfaces']['lan']['bandwidth']);
315
	}
316
	if (isset($config['interfaces']['lan']['bandwidthtype'])) {
317
		unset($config['interfaces']['lan']['bandwidthtype']);
318
	}
319
	$config['shaper']['enable'] = FALSE;
320
}
321

    
322

    
323
function upgrade_017_to_018() {
324
	global $config;
325
	if (isset($config['proxyarp']) && is_array($config['proxyarp']['proxyarpnet'])) {
326
		$proxyarp = &$config['proxyarp']['proxyarpnet'];
327
		foreach ($proxyarp as $arpent) {
328
			$vip = array();
329
			$vip['mode'] = "proxyarp";
330
			$vip['interface'] = $arpent['interface'];
331
			$vip['descr'] = $arpent['descr'];
332
			if (isset($arpent['range'])) {
333
				$vip['range'] = $arpent['range'];
334
				$vip['type'] = "range";
335
			} else {
336
				$subnet = explode('/', $arpent['network']);
337
				$vip['subnet'] = $subnet[0];
338
				if (isset($subnet[1])) {
339
					$vip['subnet_bits'] = $subnet[1];
340
					$vip['type'] = "network";
341
				} else {
342
					$vip['subnet_bits'] = "32";
343
					$vip['type'] = "single";
344
				}
345
			}
346
			$config['virtualip']['vip'][] = $vip;
347
		}
348
		unset($config['proxyarp']);
349
	}
350
	if (isset($config['installedpackages']) && isset($config['installedpackages']['carp']) && is_array($config['installedpackages']['carp']['config'])) {
351
		$carp = &$config['installedpackages']['carp']['config'];
352
		foreach ($carp as $carpent) {
353
			$vip = array();
354
			$vip['mode'] = "carp";
355
			$vip['interface'] = "AUTO";
356
			$vip['descr'] = sprintf(gettext("CARP vhid %s"), $carpent['vhid']);
357
			$vip['type'] = "single";
358
			$vip['vhid'] = $carpent['vhid'];
359
			$vip['advskew'] = $carpent['advskew'];
360
			$vip['password'] = $carpent['password'];
361
			$vip['subnet'] = $carpent['ipaddress'];
362
			$vip['subnet_bits'] = $carpent['netmask'];
363
			$config['virtualip']['vip'][] = $vip;
364
		}
365
		unset($config['installedpackages']['carp']);
366
	}
367
	/* Server NAT is no longer needed */
368
	if (isset($config['nat']['servernat'])) {
369
		unset($config['nat']['servernat']);
370
	}
371

    
372
	/* enable SSH */
373
	if ($config['version'] == "1.8") {
374
		$config['system']['sshenabled'] = true;
375
	}
376
}
377

    
378

    
379
function upgrade_018_to_019() {
380
	global $config;
381
	$config['theme'] = "metallic";
382
}
383

    
384

    
385
function upgrade_019_to_020() {
386
	global $config;
387
	if (is_array($config['ipsec']['tunnel'])) {
388
		reset($config['ipsec']['tunnel']);
389
		while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
390
			/* Sanity check on required variables */
391
			/* This fixes bogus <tunnel> entries - remnant of bug #393 */
392
			if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
393
				unset($config['ipsec']['tunnel'][$tunnel]);
394
			}
395
		}
396
	}
397
}
398

    
399
function upgrade_020_to_021() {
400
	global $config;
401
	/* shaper scheduler moved */
402
	if (isset($config['system']['schedulertype'])) {
403
		$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
404
		unset($config['system']['schedulertype']);
405
	}
406
}
407

    
408

    
409
function upgrade_021_to_022() {
410
	global $config;
411
	/* move gateway to wan interface */
412
	$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
413
}
414

    
415
function upgrade_022_to_023() {
416
	global $config;
417
	if (isset($config['shaper'])) {
418
		/* wipe previous shaper configuration */
419
		unset($config['shaper']);
420
	}
421
}
422

    
423

    
424
function upgrade_023_to_024() {
425
	global $config;
426
}
427

    
428

    
429
function upgrade_024_to_025() {
430
	global $config;
431
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
432
	if (isset($config['system']['use_rrd_gateway'])) {
433
		unset($config['system']['use_rrd_gateway']);
434
	}
435
}
436

    
437

    
438
function upgrade_025_to_026() {
439
	global $config;
440
	$cron_item = array();
441
	$cron_item['minute'] = "0";
442
	$cron_item['hour'] = "*";
443
	$cron_item['mday'] = "*";
444
	$cron_item['month'] = "*";
445
	$cron_item['wday'] = "*";
446
	$cron_item['who'] = "root";
447
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
448

    
449
	$config['cron']['item'][] = $cron_item;
450

    
451
	$cron_item = array();
452
	$cron_item['minute'] = "1,31";
453
	$cron_item['hour'] = "0-5";
454
	$cron_item['mday'] = "*";
455
	$cron_item['month'] = "*";
456
	$cron_item['wday'] = "*";
457
	$cron_item['who'] = "root";
458
	$cron_item['command'] = "/usr/bin/nice -n20 adjkerntz -a";
459

    
460
	$config['cron']['item'][] = $cron_item;
461

    
462
	$cron_item = array();
463
	$cron_item['minute'] = "1";
464
	$cron_item['hour'] = "*";
465
	$cron_item['mday'] = "1";
466
	$cron_item['month'] = "*";
467
	$cron_item['wday'] = "*";
468
	$cron_item['who'] = "root";
469
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
470

    
471
	$config['cron']['item'][] = $cron_item;
472

    
473
	$cron_item = array();
474
	$cron_item['minute'] = "*/60";
475
	$cron_item['hour'] = "*";
476
	$cron_item['mday'] = "*";
477
	$cron_item['month'] = "*";
478
	$cron_item['wday'] = "*";
479
	$cron_item['who'] = "root";
480
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout";
481

    
482
	$config['cron']['item'][] = $cron_item;
483

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

    
493
	$config['cron']['item'][] = $cron_item;
494

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

    
504
	$config['cron']['item'][] = $cron_item;
505

    
506
	$cron_item = array();
507
	$cron_item['minute'] = "*/60";
508
	$cron_item['hour'] = "*";
509
	$cron_item['mday'] = "*";
510
	$cron_item['month'] = "*";
511
	$cron_item['wday'] = "*";
512
	$cron_item['who'] = "root";
513
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -t 1800 snort2c";
514

    
515
	$config['cron']['item'][] = $cron_item;
516
}
517

    
518

    
519
function upgrade_026_to_027() {
520
	global $config;
521
}
522

    
523

    
524
function upgrade_027_to_028() {
525
	global $config;
526
}
527

    
528

    
529
function upgrade_028_to_029() {
530
	global $config;
531
	$rule_item = array();
532
	$a_filter = &$config['filter']['rule'];
533
	$rule_item['interface'] = "enc0";
534
	$rule_item['type'] = "pass";
535
	$rule_item['source']['any'] = true;
536
	$rule_item['destination']['any'] = true;
537
	$rule_item['descr'] = gettext("Permit IPsec traffic.");
538
	$rule_item['statetype'] = "keep state";
539
	$a_filter[] = $rule_item;
540
}
541

    
542

    
543
function upgrade_029_to_030() {
544
	global $config;
545
	/* enable the rrd config setting by default */
546
	$config['rrd']['enable'] = true;
547
}
548

    
549

    
550
function upgrade_030_to_031() {
551
	global $config;
552
	/* Insert upgrade code here */
553
}
554

    
555

    
556
function upgrade_031_to_032() {
557
	global $config;
558
	/* Insert upgrade code here */
559
}
560

    
561

    
562
function upgrade_032_to_033() {
563
	global $config;
564
	/* Insert upgrade code here */
565
}
566

    
567

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

    
573

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

    
579

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

    
585

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

    
591

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

    
597

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

    
603

    
604
function upgrade_039_to_040() {
605
	global $config, $g;
606
	$config['system']['webgui']['auth_method'] = "session";
607
	$config['system']['webgui']['backing_method'] = "htpasswd";
608

    
609
	if (isset($config['system']['username'])) {
610
		$config['system']['group'] = array();
611
		$config['system']['group'][0]['name'] = "admins";
612
		$config['system']['group'][0]['description'] = gettext("System Administrators");
613
		$config['system']['group'][0]['scope'] = "system";
614
		$config['system']['group'][0]['priv'] = "page-all";
615
		$config['system']['group'][0]['home'] = "index.php";
616
		$config['system']['group'][0]['gid'] = "110";
617

    
618
		$config['system']['user'] = array();
619
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
620
		$config['system']['user'][0]['descr'] = "System Administrator";
621
		$config['system']['user'][0]['scope'] = "system";
622
		$config['system']['user'][0]['groupname'] = "admins";
623
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
624
		$config['system']['user'][0]['uid'] = "0";
625
		/* Ensure that we follow what this new "admin" username should be in the session. */
626
		$_SESSION["Username"] = "{$config['system']['username']}";
627

    
628
		$config['system']['user'][0]['priv'] = array();
629
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
630
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
631
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
632
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
633
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
634
		$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).");
635
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
636
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
637
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
638
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
639
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
640
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly)."), $g['product_name']);
641
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
642
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
643
		$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).");
644

    
645
		$config['system']['nextuid'] = "111";
646
		$config['system']['nextgid'] = "111";
647

    
648
		/* wipe previous auth configuration */
649
		unset($config['system']['username']);
650
		if (isset($config['system']['password'])) {
651
			unset($config['system']['password']);
652
		}
653
	}
654
}
655

    
656
function upgrade_040_to_041() {
657
	global $config;
658
	if (!$config['sysctl']) {
659
		$config['sysctl']['item'] = array();
660

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

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

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

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

    
677
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
678
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
679
		$config['sysctl']['item'][4]['value'] =   "default";
680

    
681
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
682
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
683
		$config['sysctl']['item'][5]['value'] =   "default";
684

    
685
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
686
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
687
		$config['sysctl']['item'][6]['value'] =   "default";
688

    
689
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
690
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
691
		$config['sysctl']['item'][7]['value'] =   "default";
692

    
693
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
694
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
695
		$config['sysctl']['item'][8]['value'] =   "default";
696

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

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

    
705
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
706
		$config['sysctl']['item'][11]['descr'] =    gettext("Maximum outgoing UDP datagram size");
707
		$config['sysctl']['item'][11]['value'] =   "default";
708

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

    
713
		$config['sysctl']['item'][13]['tunable'] = "net.link.tap.user_open";
714
		$config['sysctl']['item'][13]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
715
		$config['sysctl']['item'][13]['value'] =   "default";
716

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

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

    
725
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
726
		$config['sysctl']['item'][17]['descr'] =    gettext("Set ICMP Limits");
727
		$config['sysctl']['item'][17]['value'] =   "default";
728

    
729
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
730
		$config['sysctl']['item'][18]['descr'] =    gettext("TCP Offload engine");
731
		$config['sysctl']['item'][18]['value'] =   "default";
732

    
733
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
734
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
735
		$config['sysctl']['item'][19]['value'] =   "default";
736

    
737
		$config['sysctl']['item'][20]['tunable'] = "hw.syscons.kbd_reboot";
738
		$config['sysctl']['item'][20]['descr'] =    "Enables ctrl+alt+delete";
739
		$config['sysctl']['item'][20]['value'] =   "default";
740

    
741
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
742
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
743
		$config['sysctl']['item'][21]['value'] =   "default";
744

    
745
	}
746
}
747

    
748

    
749
function upgrade_041_to_042() {
750
	global $config;
751
	if (isset($config['shaper'])) {
752
		unset($config['shaper']);
753
	}
754
	if (isset($config['ezshaper'])) {
755
		unset($config['ezshaper']);
756
	}
757
}
758

    
759

    
760
function upgrade_042_to_043() {
761
	global $config;
762
	/* migrate old interface gateway to the new gateways config */
763
	$iflist = get_configured_interface_list(false, true);
764
	$gateways = array();
765
	$i = 0;
766
	foreach ($iflist as $ifname => $interface) {
767
		if (!interface_has_gateway($ifname)) {
768
			continue;
769
		}
770
		$config['gateways']['gateway_item'][$i] = array();
771
		if (is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
772
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
773
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Static Gateway"), $ifname);
774
		} else {
775
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
776
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Dynamic Gateway"), $ifname);
777
		}
778
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
779
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
780
		/* add default gateway bit for wan on upgrade */
781
		if ($ifname == "wan") {
782
			$config['gateways']['gateway_item'][$i]['defaultgw'] = true;
783
		}
784
		if (is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
785
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
786
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
787
		}
788
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
789

    
790
		/* Update all filter rules which might reference this gateway */
791
		$j = 0;
792
		foreach ($config['filter']['rule'] as $rule) {
793
			if (is_ipaddr($rule['gateway'])) {
794
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway']) {
795
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
796
				} else if ($rule['gateway'] == $ifname) {
797
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
798
				}
799
			}
800
			$j++;
801
		}
802

    
803
		/* rename old Quality RRD files in the process */
804
		$rrddbpath = "/var/db/rrd";
805
		$gwname = "GW_" . strtoupper($ifname);
806
		if (is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
807
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
808
		}
809
		$i++;
810
	}
811
}
812

    
813

    
814
function upgrade_043_to_044() {
815
	global $config;
816

    
817
	/* migrate static routes to the new gateways config */
818
	$gateways = return_gateways_array(true);
819
	$i = 0;
820
	if (is_array($config['staticroutes']['route'])) {
821
		$gwmap = array();
822
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
823
			$found = false;
824
			foreach ($gateways as $gwname => $gw) {
825
				if ($gw['gateway'] == $sroute['gateway']) {
826
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
827
					$found = true;
828
					break;
829
				}
830
			}
831
			if ($gwmap[$sroute['gateway']]) {
832
				/* We already added a gateway name for this IP */
833
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
834
				$found = true;
835
			}
836

    
837
			if ($found == false) {
838
				$gateway = array();
839
				$gateway['name'] = "SROUTE{$i}";
840
				$gwmap[$sroute['gateway']] = $gateway['name'];
841
				$gateway['gateway'] = $sroute['gateway'];
842
				$gateway['interface'] = $sroute['interface'];
843
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
844
				if (!is_array($config['gateways']['gateway_item'])) {
845
					$config['gateways']['gateway_item'] = array();
846
				}
847
				$config['gateways']['gateway_item'][] = $gateway;
848
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
849
				$i++;
850
			}
851
		}
852
	}
853
}
854

    
855

    
856
function upgrade_044_to_045() {
857
	global $config;
858
	$iflist = get_configured_interface_list(false, true);
859
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
860
		$i = 0;
861
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
862
			/* Make sure to update the interfaces section with the right name */
863
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
864
			foreach ($iflist as $ifname) {
865
				if ($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
866
					$config['interfaces'][$ifname]['if'] = $vlan_name;
867
					continue;
868
				}
869
			}
870
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
871
			$i++;
872
		}
873
	}
874
}
875

    
876

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

    
961

    
962
function upgrade_046_to_047() {
963
	global $config;
964
	/* Upgrade IPsec from tunnel to phase1/phase2 */
965

    
966
	if (is_array($config['ipsec']['tunnel'])) {
967

    
968
		$a_phase1 = array();
969
		$a_phase2 = array();
970
		$ikeid = 0;
971

    
972
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
973

    
974
			unset($ph1ent);
975
			unset($ph2ent);
976

    
977
			/*
978
				*  attempt to locate an enabled phase1
979
				*  entry that matches the peer gateway
980
				*/
981

    
982
			if (!isset($tunnel['disabled'])) {
983

    
984
				$remote_gateway = $tunnel['remote-gateway'];
985

    
986
				foreach ($a_phase1 as $ph1tmp) {
987
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
988
						$ph1ent = $ph1tmp;
989
						break;
990
					}
991
				}
992
			}
993

    
994
			/* none found, create a new one */
995

    
996
			if (!isset($ph1ent)) {
997

    
998
				/* build new phase1 entry */
999

    
1000
				$ph1ent = array();
1001

    
1002
				$ph1ent['ikeid'] = ++$ikeid;
1003

    
1004
				if (isset($tunnel['disabled'])) {
1005
					$ph1ent['disabled'] = $tunnel['disabled'];
1006
				}
1007

    
1008
				/* convert to the new vip[$vhid] name */
1009
				if (preg_match("/^carp/", $tunnel['interface'])) {
1010
					$carpid = str_replace("carp", "", $tunnel['interface']);
1011
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
1012
				}
1013
				$ph1ent['interface'] = $tunnel['interface'];
1014
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
1015
				$ph1ent['descr'] = $tunnel['descr'];
1016

    
1017
				$ph1ent['mode'] = $tunnel['p1']['mode'];
1018

    
1019
				if (isset($tunnel['p1']['myident']['myaddress'])) {
1020
					$ph1ent['myid_type'] = "myaddress";
1021
				}
1022
				if (isset($tunnel['p1']['myident']['address'])) {
1023
					$ph1ent['myid_type'] = "address";
1024
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
1025
				}
1026
				if (isset($tunnel['p1']['myident']['fqdn'])) {
1027
					$ph1ent['myid_type'] = "fqdn";
1028
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
1029
				}
1030
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
1031
					$ph1ent['myid_type'] = "user_fqdn";
1032
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
1033
				}
1034
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
1035
					$ph1ent['myid_type'] = "asn1dn";
1036
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1037
				}
1038
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1039
					$ph1ent['myid_type'] = "dyn_dns";
1040
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1041
				}
1042

    
1043
				$ph1ent['peerid_type'] = "peeraddress";
1044

    
1045
				switch ($tunnel['p1']['encryption-algorithm']) {
1046
					case "des":
1047
						$ph1alg = array('name' => 'des');
1048
						break;
1049
					case "3des":
1050
						$ph1alg = array('name' => '3des');
1051
						break;
1052
					case "blowfish":
1053
						$ph1alg = array('name' => 'blowfish', 'keylen' => '128');
1054
						break;
1055
					case "cast128":
1056
						$ph1alg = array('name' => 'cast128');
1057
						break;
1058
					case "rijndael":
1059
						$ph1alg = array('name' => 'aes', 'keylen' => '128');
1060
						break;
1061
					case "rijndael 256":
1062
					case "aes 256":
1063
						$ph1alg = array('name' => 'aes', 'keylen' => '256');
1064
						break;
1065
				}
1066

    
1067
				$ph1ent['encryption-algorithm'] = $ph1alg;
1068
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1069
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1070
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1071
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1072

    
1073
				if (isset($tunnel['p1']['pre-shared-key'])) {
1074
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1075
				}
1076
				if (isset($tunnel['p1']['cert'])) {
1077
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1078
				}
1079
				if (isset($tunnel['p1']['peercert'])) {
1080
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1081
				}
1082
				if (isset($tunnel['p1']['private-key'])) {
1083
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1084
				}
1085

    
1086
				$ph1ent['nat_traversal'] = "on";
1087
				$ph1ent['dpd_enable'] = 1;
1088
				$ph1ent['dpd_delay'] = 10;
1089
				$ph1ent['dpd_maxfail'] = 5;
1090

    
1091
				$a_phase1[] = $ph1ent;
1092
			}
1093

    
1094
			/* build new phase2 entry */
1095

    
1096
			$ph2ent = array();
1097

    
1098
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1099

    
1100
			if (isset($tunnel['disabled'])) {
1101
				$ph1ent['disabled'] = $tunnel['disabled'];
1102
			}
1103

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

    
1106
			$type = "lan";
1107
			if ($tunnel['local-subnet']['network']) {
1108
				$type = $tunnel['local-subnet']['network'];
1109
			}
1110
			if ($tunnel['local-subnet']['address']) {
1111
				list($address, $netbits) = explode("/", $tunnel['local-subnet']['address']);
1112
				if (is_null($netbits)) {
1113
					$type = "address";
1114
				} else {
1115
					$type = "network";
1116
				}
1117
			}
1118

    
1119
			switch ($type) {
1120
				case "address":
1121
					$ph2ent['localid'] = array('type' => $type, 'address' => $address);
1122
					break;
1123
				case "network":
1124
					$ph2ent['localid'] = array('type' => $type, 'address' => $address, 'netbits' => $netbits);
1125
					break;
1126
				default:
1127
					$ph2ent['localid'] = array('type' => $type);
1128
					break;
1129
			}
1130

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

    
1134
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1135

    
1136
			$aes_count = 0;
1137
			foreach ($tunnel['p2']['encryption-algorithm-option'] as $tunalg) {
1138
				$aes_found = false;
1139
				switch ($tunalg) {
1140
					case "des":
1141
						$ph2alg = array('name' => 'des');
1142
						break;
1143
					case "3des":
1144
						$ph2alg = array('name' => '3des');
1145
						break;
1146
					case "blowfish":
1147
						$ph2alg = array('name' => 'blowfish', 'keylen' => 'auto');
1148
						break;
1149
					case "cast128":
1150
						$ph2alg = array('name' => 'cast128');
1151
						break;
1152
					case "rijndael":
1153
					case "rijndael 256":
1154
					case "aes 256":
1155
						$ph2alg = array('name' => 'aes', 'keylen' => 'auto');
1156
						$aes_found = true;
1157
						$aes_count++;
1158
						break;
1159
				}
1160

    
1161
				if (!$aes_found || ($aes_count < 2)) {
1162
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1163
				}
1164
			}
1165

    
1166
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1167
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1168
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1169

    
1170
			if (isset($tunnel['pinghost']['pinghost'])) {
1171
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1172
			}
1173

    
1174
			$a_phase2[] = $ph2ent;
1175
		}
1176

    
1177
		unset($config['ipsec']['tunnel']);
1178
		$config['ipsec']['phase1'] = $a_phase1;
1179
		$config['ipsec']['phase2'] = $a_phase2;
1180
	}
1181

    
1182
	/* Upgrade Mobile IPsec */
1183
	if (isset($config['ipsec']['mobileclients']) &&
1184
	    is_array($config['ipsec']['mobileclients']) &&
1185
	    is_array($config['ipsec']['mobileclients']['p1']) &&
1186
	    is_array($config['ipsec']['mobileclients']['p2'])) {
1187

    
1188
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1189
			$config['ipsec']['client']['enable'] = true;
1190
			$config['ipsec']['client']['user_source'] = 'system';
1191
			$config['ipsec']['client']['group_source'] = 'system';
1192
		}
1193

    
1194
		$mobilecfg = $config['ipsec']['mobileclients'];
1195

    
1196
		$ph1ent = array();
1197
		$ph1ent['ikeid'] = ++$ikeid;
1198

    
1199
		if (!isset($mobilecfg['enable'])) {
1200
			$ph1ent['disabled'] = true;
1201
		}
1202

    
1203
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1204
		$ph1ent['interface'] = 'wan';
1205
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1206
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1207

    
1208
		if (isset($mobilecfg['p1']['myident']['myaddress'])) {
1209
			$ph1ent['myid_type'] = "myaddress";
1210
		}
1211
		if (isset($mobilecfg['p1']['myident']['address'])) {
1212
			$ph1ent['myid_type'] = "address";
1213
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1214
		}
1215
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1216
			$ph1ent['myid_type'] = "fqdn";
1217
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1218
		}
1219
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1220
			$ph1ent['myid_type'] = "user_fqdn";
1221
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1222
		}
1223
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1224
			$ph1ent['myid_type'] = "asn1dn";
1225
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1226
		}
1227
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1228
			$ph1ent['myid_type'] = "dyn_dns";
1229
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1230
		}
1231
		$ph1ent['peerid_type'] = "fqdn";
1232
		$ph1ent['peerid_data'] = "";
1233

    
1234
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1235
			case "des":
1236
				$ph1alg = array('name' => 'des');
1237
				break;
1238
			case "3des":
1239
				$ph1alg = array('name' => '3des');
1240
				break;
1241
			case "blowfish":
1242
				$ph1alg = array('name' => 'blowfish', 'keylen' => '128');
1243
				break;
1244
			case "cast128":
1245
				$ph1alg = array('name' => 'cast128');
1246
				break;
1247
			case "rijndael":
1248
				$ph1alg = array('name' => 'aes', 'keylen' => '128');
1249
				break;
1250
			case "rijndael 256":
1251
			case "aes 256":
1252
				$ph1alg = array('name' => 'aes', 'keylen' => '256');
1253
				break;
1254
		}
1255

    
1256
		$ph1ent['encryption-algorithm'] = $ph1alg;
1257
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1258
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1259
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1260
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1261

    
1262
		if (isset($mobilecfg['p1']['cert'])) {
1263
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1264
		}
1265
		if (isset($mobilecfg['p1']['peercert'])) {
1266
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1267
		}
1268
		if (isset($mobilecfg['p1']['private-key'])) {
1269
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1270
		}
1271

    
1272
		$ph1ent['nat_traversal'] = "on";
1273
		$ph1ent['dpd_enable'] = 1;
1274
		$ph1ent['dpd_delay'] = 10;
1275
		$ph1ent['dpd_maxfail'] = 5;
1276
		$ph1ent['mobile'] = true;
1277

    
1278
		$ph2ent = array();
1279
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1280
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1281
		$ph2ent['localid'] = array('type' => 'none');
1282
		$ph2ent['remoteid'] = array('type' => 'mobile');
1283
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1284

    
1285
		$aes_count = 0;
1286
		foreach ($mobilecfg['p2']['encryption-algorithm-option'] as $tunalg) {
1287
			$aes_found = false;
1288
			switch ($tunalg) {
1289
				case "des":
1290
					$ph2alg = array('name' => 'des');
1291
					break;
1292
				case "3des":
1293
					$ph2alg = array('name' => '3des');
1294
					break;
1295
				case "blowfish":
1296
					$ph2alg = array('name' => 'blowfish', 'keylen' => 'auto');
1297
					break;
1298
				case "cast128":
1299
					$ph2alg = array('name' => 'cast128');
1300
					break;
1301
				case "rijndael":
1302
				case "rijndael 256":
1303
				case "aes 256":
1304
					$ph2alg = array('name' => 'aes', 'keylen' => 'auto');
1305
					$aes_found = true;
1306
					$aes_count++;
1307
					break;
1308
			}
1309

    
1310
			if (!$aes_found || ($aes_count < 2)) {
1311
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1312
			}
1313
		}
1314
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1315
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1316
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1317
		$ph2ent['mobile'] = true;
1318

    
1319
		$config['ipsec']['phase1'][] = $ph1ent;
1320
		$config['ipsec']['phase2'][] = $ph2ent;
1321
		unset($config['ipsec']['mobileclients']);
1322
	}
1323
}
1324

    
1325

    
1326
function upgrade_047_to_048() {
1327
	global $config;
1328
	if (!empty($config['dyndns'])) {
1329
		$config['dyndnses'] = array();
1330
		$config['dyndnses']['dyndns'] = array();
1331
		if (isset($config['dyndns'][0]['host'])) {
1332
			$tempdyn = array();
1333
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1334
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1335
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1336
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1337
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1338
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1339
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];
1340
			$tempdyn['interface'] = "wan";
1341
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1342
			$config['dyndnses']['dyndns'][] = $tempdyn;
1343
		}
1344
		unset($config['dyndns']);
1345
	}
1346
	if (!empty($config['dnsupdate'])) {
1347
		$pconfig = $config['dnsupdate'][0];
1348
		if (!$pconfig['ttl']) {
1349
			$pconfig['ttl'] = 60;
1350
		}
1351
		if (!$pconfig['keytype']) {
1352
			$pconfig['keytype'] = "zone";
1353
		}
1354
		$pconfig['interface'] = "wan";
1355
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1356
		unset($config['dnsupdate']);
1357
	}
1358

    
1359
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1360
		$pconfig = array();
1361
		$pconfig['username'] = $config['pppoe'][0]['username'];
1362
		$pconfig['password'] = $config['pppoe'][0]['password'];
1363
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1364
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1365
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1366
		unset($config['pppoe']);
1367
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1368
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1369
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1370
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1371
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1372
	}
1373
	if (is_array($config['pptp'])) {
1374
		$pconfig = array();
1375
		$pconfig['username'] = $config['pptp']['username'];
1376
		$pconfig['password'] = $config['pptp']['password'];
1377
		$pconfig['provider'] = $config['pptp']['provider'];
1378
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1379
		$pconfig['timeout'] = $config['pptp']['timeout'];
1380
		unset($config['pptp']);
1381
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1382
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1383
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1384
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1385
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1386
	}
1387
}
1388

    
1389

    
1390
function upgrade_048_to_049() {
1391
	global $config;
1392
	/* setup new all users group */
1393
	$all = array();
1394
	$all['name'] = "all";
1395
	$all['description'] = gettext("All Users");
1396
	$all['scope'] = "system";
1397
	$all['gid'] = 1998;
1398
	$all['member'] = array();
1399

    
1400
	if (!is_array($config['system']['user'])) {
1401
		$config['system']['user'] = array();
1402
	}
1403
	if (!is_array($config['system']['group'])) {
1404
		$config['system']['group'] = array();
1405
	}
1406

    
1407
	/* work around broken uid assignments */
1408
	$config['system']['nextuid'] = 2000;
1409
	foreach ($config['system']['user'] as & $user) {
1410
		if (isset($user['uid']) && !$user['uid']) {
1411
			continue;
1412
		}
1413
		$user['uid'] = $config['system']['nextuid']++;
1414
	}
1415

    
1416
	/* work around broken gid assignments */
1417
	$config['system']['nextgid'] = 2000;
1418
	foreach ($config['system']['group'] as & $group) {
1419
		if ($group['name'] == $g['admin_group']) {
1420
			$group['gid'] = 1999;
1421
		} else {
1422
			$group['gid'] = $config['system']['nextgid']++;
1423
		}
1424
	}
1425

    
1426
	/* build group membership information */
1427
	foreach ($config['system']['group'] as & $group) {
1428
		$group['member'] = array();
1429
		foreach ($config['system']['user'] as & $user) {
1430
			$groupnames = explode(",", $user['groupname']);
1431
			if (in_array($group['name'], $groupnames)) {
1432
				$group['member'][] = $user['uid'];
1433
			}
1434
		}
1435
	}
1436

    
1437
	/* reset user group information */
1438
	foreach ($config['system']['user'] as & $user) {
1439
		unset($user['groupname']);
1440
		$all['member'][] = $user['uid'];
1441
	}
1442

    
1443
	/* reset group scope information */
1444
	foreach ($config['system']['group'] as & $group) {
1445
		if ($group['name'] != $g['admin_group']) {
1446
			$group['scope'] = "user";
1447
		}
1448
	}
1449

    
1450
	/* insert new all group */
1451
	$groups = Array();
1452
	$groups[] = $all;
1453
	$groups = array_merge($config['system']['group'], $groups);
1454
	$config['system']['group'] = $groups;
1455
}
1456

    
1457

    
1458
function upgrade_049_to_050() {
1459
	global $config;
1460

    
1461
	if (!is_array($config['system']['user'])) {
1462
		$config['system']['user'] = array();
1463
	}
1464
	/* update user privileges */
1465
	foreach ($config['system']['user'] as & $user) {
1466
		$privs = array();
1467
		if (!is_array($user['priv'])) {
1468
			unset($user['priv']);
1469
			continue;
1470
		}
1471
		foreach ($user['priv'] as $priv) {
1472
			switch ($priv['id']) {
1473
				case "hasshell":
1474
					$privs[] = "user-shell-access";
1475
					break;
1476
				case "copyfiles":
1477
					$privs[] = "user-copy-files";
1478
					break;
1479
			}
1480
		}
1481
		$user['priv'] = $privs;
1482
	}
1483

    
1484
	/* update group privileges */
1485
	foreach ($config['system']['group'] as & $group) {
1486
		$privs = array();
1487
		if (!is_array($group['pages'])) {
1488
			unset($group['pages']);
1489
			continue;
1490
		}
1491
		foreach ($group['pages'] as $page) {
1492
			$priv = map_page_privname($page);
1493
			if ($priv) {
1494
				$privs[] = $priv;
1495
			}
1496
		}
1497
		unset($group['pages']);
1498
		$group['priv'] = $privs;
1499
	}
1500

    
1501
	/* sync all local account information */
1502
	local_sync_accounts();
1503
}
1504

    
1505

    
1506
function upgrade_050_to_051() {
1507
	global $config;
1508
	$pconfig = array();
1509
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1510
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1511
	$pconfig['value'] = "1";
1512
	$config['sysctl']['item'][] = $pconfig;
1513
	$pconfig = array();
1514
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1515
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1516
	$pconfig['value'] = "0";
1517
	$config['sysctl']['item'][] = $pconfig;
1518

    
1519
	if (isset($config['bridge'])) {
1520
		unset($config['bridge']);
1521
	}
1522

    
1523
	$convert_bridges = false;
1524
	foreach ($config['interfaces'] as $intf) {
1525
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1526
			$config['bridges'] = array();
1527
			$config['bridges']['bridged'] = array();
1528
			$convert_bridges = true;
1529
			break;
1530
		}
1531
	}
1532
	if ($convert_bridges == true) {
1533
		$i = 0;
1534
		foreach ($config['interfaces'] as $ifr => &$intf) {
1535
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1536
				$nbridge = array();
1537
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1538
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1539
				$nbridge['bridgeif'] = "bridge{$i}";
1540
				$config['bridges']['bridged'][] = $nbridge;
1541
				unset($intf['bridge']);
1542
				$i++;
1543
			}
1544
		}
1545
	}
1546
}
1547

    
1548

    
1549
function upgrade_051_to_052() {
1550
	global $config;
1551
	$config['openvpn'] = array();
1552
	if (!is_array($config['ca'])) {
1553
		$config['ca'] = array();
1554
	}
1555
	if (!is_array($config['cert'])) {
1556
		$config['cert'] = array();
1557
	}
1558

    
1559
	$vpnid = 1;
1560

    
1561
	/* openvpn server configurations */
1562
	if (is_array($config['installedpackages']['openvpnserver'])) {
1563
		$config['openvpn']['openvpn-server'] = array();
1564

    
1565
		$index = 1;
1566
		foreach ($config['installedpackages']['openvpnserver']['config'] as $server) {
1567

    
1568
			if (!is_array($server)) {
1569
				continue;
1570
			}
1571

    
1572
			if ($server['auth_method'] == "pki") {
1573

    
1574
				/* create ca entry */
1575
				$ca = array();
1576
				$ca['refid'] = uniqid();
1577
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1578
				$ca['crt'] = $server['ca_cert'];
1579
				$config['ca'][] = $ca;
1580

    
1581
				/* create ca reference */
1582
				unset($server['ca_cert']);
1583
				$server['caref'] = $ca['refid'];
1584

    
1585
				/* create a crl entry if needed */
1586
				if (!empty($server['crl'][0])) {
1587
					$crl = array();
1588
					$crl['refid'] = uniqid();
1589
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1590
					$crl['caref'] = $ca['refid'];
1591
					$crl['text'] = $server['crl'][0];
1592
					if (!is_array($config['crl'])) {
1593
						$config['crl'] = array();
1594
					}
1595
					$config['crl'][] = $crl;
1596
					$server['crlref'] = $crl['refid'];
1597
				}
1598
				unset($server['crl']);
1599

    
1600
				/* create cert entry */
1601
				$cert = array();
1602
				$cert['refid'] = uniqid();
1603
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1604
				$cert['crt'] = $server['server_cert'];
1605
				$cert['prv'] = $server['server_key'];
1606
				$config['cert'][] = $cert;
1607

    
1608
				/* create cert reference */
1609
				unset($server['server_cert']);
1610
				unset($server['server_key']);
1611
				$server['certref'] = $cert['refid'];
1612

    
1613
				$index++;
1614
			}
1615

    
1616
			/* determine operational mode */
1617
			if ($server['auth_method'] == 'pki') {
1618
				if ($server['nopool']) {
1619
					$server['mode'] = "p2p_tls";
1620
				} else {
1621
					$server['mode'] = "server_tls";
1622
				}
1623
			} else {
1624
				$server['mode'] = "p2p_shared_key";
1625
			}
1626
			unset($server['auth_method']);
1627

    
1628
			/* modify configuration values */
1629
			$server['dh_length'] = 1024;
1630
			unset($server['dh_params']);
1631
			if (!$server['interface']) {
1632
				$server['interface'] = 'any';
1633
			}
1634
			$server['tunnel_network'] = $server['addresspool'];
1635
			unset($server['addresspool']);
1636
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1637
				$server['compression'] = "on";
1638
				unset($server['use_lzo']);
1639
			}
1640
			if ($server['nopool']) {
1641
				$server['pool_enable'] = false;
1642
			} else {
1643
				$server['pool_enable'] = "yes";
1644
			}
1645
			unset($server['nopool']);
1646
			$server['dns_domain'] = $server['dhcp_domainname'];
1647
			unset($server['dhcp_domainname']);
1648

    
1649
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1650
			$d=1;
1651
			foreach ($tmparr as $tmpa) {
1652
				$server["dns_server{$d}"] = $tmpa;
1653
				$d++;
1654
			}
1655
			unset($server['dhcp_dns']);
1656

    
1657
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1658
			$d=1;
1659
			foreach ($tmparr as $tmpa) {
1660
				$server["ntp_server{$d}"] = $tmpa;
1661
				$d++;
1662
			}
1663
			unset($server['dhcp_ntp']);
1664

    
1665
			if ($server['dhcp_nbtdisable']) {
1666
				$server['netbios_enable'] = false;
1667
			} else {
1668
				$server['netbios_enable'] = "yes";
1669
			}
1670
			unset($server['dhcp_nbtdisable']);
1671
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1672
			unset($server['dhcp_nbttype']);
1673
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1674
			unset($server['dhcp_nbtscope']);
1675

    
1676
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1677
			$d=1;
1678
			foreach ($tmparr as $tmpa) {
1679
				$server["nbdd_server{$d}"] = $tmpa;
1680
				$d++;
1681
			}
1682
			unset($server['dhcp_nbdd']);
1683

    
1684
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1685
			$d=1;
1686
			foreach ($tmparr as $tmpa) {
1687
				$server["wins_server{$d}"] = $tmpa;
1688
				$d++;
1689
			}
1690
			unset($server['dhcp_wins']);
1691

    
1692
			if (!empty($server['disable'])) {
1693
				$server['disable'] = true;
1694
			} else {
1695
				unset($server['disable']);
1696
			}
1697

    
1698
			/* allocate vpnid */
1699
			$server['vpnid'] = $vpnid++;
1700

    
1701
			if (!empty($server['custom_options'])) {
1702
				$cstmopts = array();
1703
				$tmpcstmopts = explode(";", $server['custom_options']);
1704
				$assigned_if = "";
1705
				$tmpstr = "";
1706
				foreach ($tmpcstmopts as $tmpcstmopt) {
1707
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1708
					if (substr($tmpstr, 0, 6) == "devtun") {
1709
						$assigned_if = substr($tmpstr, 3);
1710
						continue;
1711
					} else if (substr($tmpstr, 0, 5) == "local") {
1712
						$localip = substr($tmpstr, 5);
1713
						$server['ipaddr'] = str_replace("\n", "", $localip);
1714
					} else {
1715
						$cstmopts[] = $tmpcstmopt;
1716
					}
1717
				}
1718
				$server['custom_options'] = implode(";", $cstmopts);
1719
				if (!empty($assigned_if)) {
1720
					foreach ($config['interfaces'] as $iface => $cfgif) {
1721
						if ($cfgif['if'] == $assigned_if) {
1722
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1723
							break;
1724
						}
1725
					}
1726
				}
1727
			}
1728

    
1729
			$config['openvpn']['openvpn-server'][] = $server;
1730
		}
1731
		unset($config['installedpackages']['openvpnserver']);
1732
	}
1733

    
1734
	/* openvpn client configurations */
1735
	if (is_array($config['installedpackages']['openvpnclient'])) {
1736
		$config['openvpn']['openvpn-client'] = array();
1737

    
1738
		$index = 1;
1739
		foreach ($config['installedpackages']['openvpnclient']['config'] as $client) {
1740

    
1741
			if (!is_array($client)) {
1742
				continue;
1743
			}
1744

    
1745
			if ($client['auth_method'] == "pki") {
1746

    
1747
				/* create ca entry */
1748
				$ca = array();
1749
				$ca['refid'] = uniqid();
1750
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1751
				$ca['crt'] = $client['ca_cert'];
1752
				$ca['crl'] = $client['crl'];
1753
				$config['ca'][] = $ca;
1754

    
1755
				/* create ca reference */
1756
				unset($client['ca_cert']);
1757
				unset($client['crl']);
1758
				$client['caref'] = $ca['refid'];
1759

    
1760
				/* create cert entry */
1761
				$cert = array();
1762
				$cert['refid'] = uniqid();
1763
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1764
				$cert['crt'] = $client['client_cert'];
1765
				$cert['prv'] = $client['client_key'];
1766
				$config['cert'][] = $cert;
1767

    
1768
				/* create cert reference */
1769
				unset($client['client_cert']);
1770
				unset($client['client_key']);
1771
				$client['certref'] = $cert['refid'];
1772

    
1773
				$index++;
1774
			}
1775

    
1776
			/* determine operational mode */
1777
			if ($client['auth_method'] == 'pki') {
1778
				$client['mode'] = "p2p_tls";
1779
			} else {
1780
				$client['mode'] = "p2p_shared_key";
1781
			}
1782
			unset($client['auth_method']);
1783

    
1784
			/* modify configuration values */
1785
			if (!$client['interface']) {
1786
				$client['interface'] = 'wan';
1787
			}
1788
			$client['tunnel_network'] = $client['interface_ip'];
1789
			unset($client['interface_ip']);
1790
			$client['server_addr'] = $client['serveraddr'];
1791
			unset($client['serveraddr']);
1792
			$client['server_port'] = $client['serverport'];
1793
			unset($client['serverport']);
1794
			$client['proxy_addr'] = $client['poxy_hostname'];
1795
			unset($client['proxy_addr']);
1796
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1797
				$client['compression'] = "on";
1798
				unset($client['use_lzo']);
1799
			}
1800
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1801
			unset($client['infiniteresolvretry']);
1802

    
1803
			/* allocate vpnid */
1804
			$client['vpnid'] = $vpnid++;
1805

    
1806
			if (!empty($client['custom_options'])) {
1807
				$cstmopts = array();
1808
				$tmpcstmopts = explode(";", $client['custom_options']);
1809
				$assigned_if = "";
1810
				$tmpstr = "";
1811
				foreach ($tmpcstmopts as $tmpcstmopt) {
1812
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1813
					if (substr($tmpstr, 0, 6) == "devtun") {
1814
						$assigned_if = substr($tmpstr, 3);
1815
						continue;
1816
					} else if (substr($tmpstr, 0, 5) == "local") {
1817
						$localip = substr($tmpstr, 5);
1818
						$client['ipaddr'] = str_replace("\n", "", $localip);
1819
					} else {
1820
						$cstmopts[] = $tmpcstmopt;
1821
					}
1822
				}
1823
				$client['custom_options'] = implode(";", $cstmopts);
1824
				if (!empty($assigned_if)) {
1825
					foreach ($config['interfaces'] as $iface => $cfgif) {
1826
						if ($cfgif['if'] == $assigned_if) {
1827
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1828
							break;
1829
						}
1830
					}
1831
				}
1832
			}
1833

    
1834
			if (!empty($client['disable'])) {
1835
				$client['disable'] = true;
1836
			} else {
1837
				unset($client['disable']);
1838
			}
1839

    
1840
			$config['openvpn']['openvpn-client'][] = $client;
1841
		}
1842

    
1843
		unset($config['installedpackages']['openvpnclient']);
1844
	}
1845

    
1846
	/* openvpn client specific configurations */
1847
	if (is_array($config['installedpackages']['openvpncsc'])) {
1848
		$config['openvpn']['openvpn-csc'] = array();
1849

    
1850
		foreach ($config['installedpackages']['openvpncsc']['config'] as $csc) {
1851

    
1852
			if (!is_array($csc)) {
1853
				continue;
1854
			}
1855

    
1856
			/* modify configuration values */
1857
			$csc['common_name'] = $csc['commonname'];
1858
			unset($csc['commonname']);
1859
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1860
			unset($csc['ifconfig_push']);
1861
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1862
			unset($csc['dhcp_domainname']);
1863

    
1864
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1865
			$d=1;
1866
			foreach ($tmparr as $tmpa) {
1867
				$csc["dns_server{$d}"] = $tmpa;
1868
				$d++;
1869
			}
1870
			unset($csc['dhcp_dns']);
1871

    
1872
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1873
			$d=1;
1874
			foreach ($tmparr as $tmpa) {
1875
				$csc["ntp_server{$d}"] = $tmpa;
1876
				$d++;
1877
			}
1878
			unset($csc['dhcp_ntp']);
1879

    
1880
			if ($csc['dhcp_nbtdisable']) {
1881
				$csc['netbios_enable'] = false;
1882
			} else {
1883
				$csc['netbios_enable'] = "yes";
1884
			}
1885
			unset($csc['dhcp_nbtdisable']);
1886
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1887
			unset($csc['dhcp_nbttype']);
1888
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1889
			unset($csc['dhcp_nbtscope']);
1890

    
1891
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1892
			$d=1;
1893
			foreach ($tmparr as $tmpa) {
1894
				$csc["nbdd_server{$d}"] = $tmpa;
1895
				$d++;
1896
			}
1897
			unset($csc['dhcp_nbdd']);
1898

    
1899
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1900
			$d=1;
1901
			foreach ($tmparr as $tmpa) {
1902
				$csc["wins_server{$d}"] = $tmpa;
1903
				$d++;
1904
			}
1905
			unset($csc['dhcp_wins']);
1906

    
1907
			if (!empty($csc['disable'])) {
1908
				$csc['disable'] = true;
1909
			} else {
1910
				unset($csc['disable']);
1911
			}
1912

    
1913
			$config['openvpn']['openvpn-csc'][] = $csc;
1914
		}
1915

    
1916
		unset($config['installedpackages']['openvpncsc']);
1917
	}
1918

    
1919
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1920
	    count($config['openvpn']['openvpn-client']) > 0) {
1921
		$ovpnrule = array();
1922
		$ovpnrule['type'] = "pass";
1923
		$ovpnrule['interface'] = "openvpn";
1924
		$ovpnrule['statetype'] = "keep state";
1925
		$ovpnrule['source'] = array();
1926
		$ovpnrule['destination'] = array();
1927
		$ovpnrule['source']['any'] = true;
1928
		$ovpnrule['destination']['any'] = true;
1929
		$ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1930
		$config['filter']['rule'][] = $ovpnrule;
1931
	}
1932

    
1933
	/*
1934
		* FIXME: hack to keep things working with no installedpackages
1935
		* or carp array in the configuration data.
1936
		*/
1937
	if (!is_array($config['installedpackages'])) {
1938
		$config['installedpackages'] = array();
1939
	}
1940
	if (!is_array($config['installedpackages']['carp'])) {
1941
		$config['installedpackages']['carp'] = array();
1942
	}
1943

    
1944
}
1945

    
1946

    
1947
function upgrade_052_to_053() {
1948
	global $config;
1949
	if (!is_array($config['ca'])) {
1950
		$config['ca'] = array();
1951
	}
1952
	if (!is_array($config['cert'])) {
1953
		$config['cert'] = array();
1954
	}
1955

    
1956
	/* migrate advanced admin page webui ssl to certificate manager */
1957
	if ($config['system']['webgui']['certificate'] &&
1958
	    $config['system']['webgui']['private-key']) {
1959

    
1960
		/* create cert entry */
1961
		$cert = array();
1962
		$cert['refid'] = uniqid();
1963
		$cert['descr'] = "webConfigurator SSL Certificate";
1964
		$cert['crt'] = $config['system']['webgui']['certificate'];
1965
		$cert['prv'] = $config['system']['webgui']['private-key'];
1966
		$config['cert'][] = $cert;
1967

    
1968
		/* create cert reference */
1969
		unset($config['system']['webgui']['certificate']);
1970
		unset($config['system']['webgui']['private-key']);
1971
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1972
	}
1973

    
1974
	/* migrate advanced admin page ssh keys to user manager */
1975
	if ($config['system']['ssh']['authorizedkeys']) {
1976
		$admin_user =& getUserEntryByUID(0);
1977
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1978
		unset($config['system']['ssh']['authorizedkeys']);
1979
	}
1980
}
1981

    
1982

    
1983
function upgrade_053_to_054() {
1984
	global $config;
1985
	if (is_array($config['load_balancer']['lbpool'])) {
1986
		$lbpool_arr = $config['load_balancer']['lbpool'];
1987
		$lbpool_srv_arr = array();
1988
		$gateway_group_arr = array();
1989
		$gateways = return_gateways_array();
1990
		$group_name_changes = array();
1991
		if (!is_array($config['gateways']['gateway_item'])) {
1992
			$config['gateways']['gateway_item'] = array();
1993
		}
1994

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

    
2030
					/* on failover increment tier. Else always assign 1 */
2031
					if ($lbpool['behaviour'] == "failover") {
2032
						$i++;
2033
					} else {
2034
						$i = 1;
2035
					}
2036
					$gateway_group['item'][] = "$static_name|$i";
2037
				}
2038
				$gateway_group_arr[] = $gateway_group;
2039
			} else {
2040
				$lbpool_srv_arr[] = $lbpool;
2041
			}
2042
		}
2043
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2044
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2045
	}
2046
	// Unset lbpool if we no longer have any server pools
2047
	if (count($lbpool_srv_arr) == 0) {
2048
		if (empty($config['load_balancer'])) {
2049
			unset($config['load_balancer']);
2050
		} else {
2051
			if (isset($config['load_balancer']['lbpool'])) {
2052
				unset($config['load_balancer']['lbpool']);
2053
			}
2054
		}
2055
	} else {
2056
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2057
	}
2058
	// Only set the gateway group array if we converted any
2059
	if (count($gateway_group_arr) != 0) {
2060
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2061
		// Update any rules that had a gateway change, if any.
2062
		if (count($group_name_changes) > 0) {
2063
			foreach ($config['filter']['rule'] as & $rule) {
2064
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes)) {
2065
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
2066
				}
2067
			}
2068
		}
2069
	}
2070
}
2071

    
2072

    
2073
function upgrade_054_to_055() {
2074
	global $config;
2075
	global $g;
2076

    
2077
	/* RRD files changed for quality, traffic and packets graphs */
2078
	//ini_set("max_execution_time", "1800");
2079
	/* convert traffic RRD file */
2080
	global $parsedcfg, $listtags;
2081
	$listtags = array("ds", "v", "rra", "row");
2082

    
2083
	$rrddbpath = "/var/db/rrd/";
2084
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2085
	if ($g['platform'] != "pfSense") {
2086
		/* restore the databases, if we have one */
2087
		if (restore_rrd()) {
2088
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2089
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2090
		}
2091
	}
2092

    
2093
	$rrdinterval = 60;
2094
	$valid = $rrdinterval * 2;
2095

    
2096
	/* Asume GigE for now */
2097
	$downstream = 125000000;
2098
	$upstream = 125000000;
2099

    
2100
	/* build a list of quality databases */
2101
	/* roundtrip has become delay */
2102
	function divide_delay($delayval) {
2103
		$delayval = floatval($delayval);
2104
		$delayval = ($delayval / 1000);
2105
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2106
		return $delayval;
2107
	}
2108
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2109
	$databases = array();
2110
	if (!file_exists($rrddbpath)) {
2111
		@mkdir($rrddbpath);
2112
	}
2113
	chdir($rrddbpath);
2114
	$databases = glob("*-quality.rrd");
2115
	rsort($databases);
2116
	foreach ($databases as $database) {
2117
		$xmldump = "{$database}.old.xml";
2118
		$xmldumpnew = "{$database}.new.xml";
2119

    
2120
		if (platform_booting()) {
2121
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2122
		}
2123
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2124

    
2125
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2126
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2127
		$rrdold = $rrdold['rrd'];
2128

    
2129
		$i = 0;
2130
		foreach ($rrdold['rra'] as $rra) {
2131
			$l = 0;
2132
			foreach ($rra['database']['row'] as $row) {
2133
				$vnew = divide_delay($row['v'][1]);
2134
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2135
				$l++;
2136
			}
2137
			$i++;
2138
		}
2139

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

    
2143
		unset($rrdold);
2144
		@unlink("{$g['tmp_path']}/{$xmldump}");
2145
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2146
	}
2147
	/* let apinger recreate required files */
2148
	if (!platform_booting()) {
2149
		setup_gateways_monitor();
2150
	}
2151

    
2152
	/* build a list of traffic and packets databases */
2153
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2154
	rsort($databases);
2155
	foreach ($databases as $database) {
2156
		$databasetmp = "{$database}.tmp";
2157
		$xmldump = "{$database}.old.xml";
2158
		$xmldumptmp = "{$database}.tmp.xml";
2159
		$xmldumpnew = "{$database}.new.xml";
2160

    
2161
		if (platform_booting()) {
2162
			echo "Migrate RRD database {$database} to new format \n";
2163
		}
2164
		/* rename DS source */
2165
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2166
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2167

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

    
2171
		/* create new rrd database file */
2172
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2173
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2174
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2175
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2176
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2177
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2178
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2179
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2180
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2181

    
2182
		create_new_rrd("$rrdcreate");
2183
		/* create temporary xml from new RRD */
2184
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2185

    
2186
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2187
		$rrdold = $rrdold['rrd'];
2188

    
2189
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2190
		$rrdnew = $rrdnew['rrd'];
2191

    
2192
		/* remove any MAX RRA's. Not needed for traffic. */
2193
		$i = 0;
2194
		foreach ($rrdold['rra'] as $rra) {
2195
			if (trim($rra['cf']) == "MAX") {
2196
				unset($rrdold['rra'][$i]);
2197
			}
2198
			$i++;
2199
		}
2200

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

    
2225

    
2226
function upgrade_055_to_056() {
2227
	global $config;
2228

    
2229
	if (!is_array($config['ca'])) {
2230
		$config['ca'] = array();
2231
	}
2232
	if (!is_array($config['cert'])) {
2233
		$config['cert'] = array();
2234
	}
2235

    
2236
	/* migrate ipsec ca's to cert manager */
2237
	if (is_array($config['ipsec']['cacert'])) {
2238
		foreach ($config['ipsec']['cacert'] as & $cacert) {
2239
			$ca = array();
2240
			$ca['refid'] = uniqid();
2241
			if (is_array($cacert['cert'])) {
2242
				$ca['crt'] = $cacert['cert'][0];
2243
			} else {
2244
				$ca['crt'] = $cacert['cert'];
2245
			}
2246
			$ca['descr'] = $cacert['ident'];
2247
			$config['ca'][] = $ca;
2248
		}
2249
		unset($config['ipsec']['cacert']);
2250
	}
2251

    
2252
	/* migrate phase1 certificates to cert manager */
2253
	if (is_array($config['ipsec']['phase1'])) {
2254
		foreach ($config['ipsec']['phase1'] as & $ph1ent) {
2255
			$cert = array();
2256
			$cert['refid'] = uniqid();
2257
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2258
			if (is_array($ph1ent['cert'])) {
2259
				$cert['crt'] = $ph1ent['cert'][0];
2260
			} else {
2261
				$cert['crt'] = $ph1ent['cert'];
2262
			}
2263
			$cert['prv'] = $ph1ent['private-key'];
2264
			$config['cert'][] = $cert;
2265
			$ph1ent['certref'] = $cert['refid'];
2266
			if ($ph1ent['cert']) {
2267
				unset($ph1ent['cert']);
2268
			}
2269
			if ($ph1ent['private-key']) {
2270
				unset($ph1ent['private-key']);
2271
			}
2272
			if ($ph1ent['peercert']) {
2273
				unset($ph1ent['peercert']);
2274
			}
2275
		}
2276
	}
2277
}
2278

    
2279

    
2280
function upgrade_056_to_057() {
2281
	global $config;
2282

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

    
2316
function upgrade_057_to_058() {
2317
	global $config;
2318
	/* set all phase2 entries to tunnel mode */
2319
	if (is_array($config['ipsec']['phase2'])) {
2320
		foreach ($config['ipsec']['phase2'] as & $ph2ent) {
2321
			$ph2ent['mode'] = 'tunnel';
2322
		}
2323
	}
2324
}
2325

    
2326
function upgrade_058_to_059() {
2327
	global $config;
2328

    
2329
	if (is_array($config['schedules']['schedule'])) {
2330
		foreach ($config['schedules']['schedule'] as & $schedl) {
2331
			$schedl['schedlabel'] = uniqid();
2332
		}
2333
	}
2334
}
2335

    
2336
function upgrade_059_to_060() {
2337
	global $config;
2338
	require_once("/etc/inc/certs.inc");
2339
	if (is_array($config['ca'])) {
2340
		/* Locate issuer for all CAs */
2341
		foreach ($config['ca'] as & $ca) {
2342
			$subject = cert_get_subject($ca['crt']);
2343
			$issuer = cert_get_issuer($ca['crt']);
2344
			if ($issuer <> $subject) {
2345
				$issuer_crt =& lookup_ca_by_subject($issuer);
2346
				if ($issuer_crt) {
2347
					$ca['caref'] = $issuer_crt['refid'];
2348
				}
2349
			}
2350
		}
2351

    
2352
		/* Locate issuer for all certificates */
2353
		if (is_array($config['cert'])) {
2354
			foreach ($config['cert'] as & $cert) {
2355
				$subject = cert_get_subject($cert['crt']);
2356
				$issuer = cert_get_issuer($cert['crt']);
2357
				if ($issuer <> $subject) {
2358
					$issuer_crt =& lookup_ca_by_subject($issuer);
2359
					if ($issuer_crt) {
2360
						$cert['caref'] = $issuer_crt['refid'];
2361
					}
2362
				}
2363
			}
2364
		}
2365
	}
2366
}
2367

    
2368
function upgrade_060_to_061() {
2369
	global $config;
2370

    
2371
	if (is_array($config['interfaces']['wan'])) {
2372
		$config['interfaces']['wan']['enable'] = true;
2373
	}
2374
	if (is_array($config['interfaces']['lan'])) {
2375
		$config['interfaces']['lan']['enable'] = true;
2376
	}
2377

    
2378
	/* On 1.2.3 the "mtu" field adjusted MSS.
2379
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2380
	   See redmine ticket #1886
2381
	*/
2382
	foreach ($config['interfaces'] as $ifr => &$intf) {
2383
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2384
			$intf['mss'] = $intf['mtu'];
2385
			unset($intf['mtu']);
2386
		}
2387
	}
2388
}
2389

    
2390
function upgrade_061_to_062() {
2391
	global $config;
2392

    
2393
	/* Convert NAT port forwarding rules */
2394
	if (is_array($config['nat']['rule'])) {
2395
		$a_nat = &$config['nat']['rule'];
2396

    
2397
		foreach ($a_nat as &$natent) {
2398
			$natent['disabled'] = false;
2399
			$natent['nordr']    = false;
2400

    
2401
			$natent['source'] = array(
2402
				"not"     => false,
2403
				"any"     => true,
2404
				"port"    => ""
2405
			);
2406

    
2407
			$natent['destination'] = array(
2408
				"not"     => false,
2409
				"address" => $natent['external-address'],
2410
				"port"    => $natent['external-port']
2411
			);
2412

    
2413
			if (empty($natent['destination']['address'])) {
2414
				unset($natent['destination']['address']);
2415
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2416
			} else if ($natent['destination']['address'] == 'any') {
2417
				unset($natent['destination']['address']);
2418
				$natent['destination']['any'] = true;
2419
			}
2420

    
2421
			unset($natent['external-address']);
2422
			unset($natent['external-port']);
2423
		}
2424

    
2425
		unset($natent);
2426
	}
2427
}
2428

    
2429
function upgrade_062_to_063() {
2430
	/* Upgrade legacy Themes to the new pfsense_ng */
2431
	global $config;
2432

    
2433
	switch ($config['theme']) {
2434
		case "nervecenter":
2435
			$config['theme'] = "pfsense_ng";
2436
			break;
2437
	}
2438

    
2439
}
2440

    
2441
function upgrade_063_to_064() {
2442
	global $config;
2443
	$j = 0;
2444
	$ifcfg = &$config['interfaces'];
2445

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

    
2464
	if (!is_array($config['ppps']['ppp'])) {
2465
		$config['ppps']['ppp'] = array();
2466
	}
2467
	$a_ppps = &$config['ppps']['ppp'];
2468

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

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

    
2519
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2520
			$j++;
2521

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

    
2536
			$a_ppps[] = $ppp;
2537

    
2538
		}
2539
	}
2540
}
2541

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

    
2549
function upgrade_065_to_066() {
2550
	global $config;
2551

    
2552
	$dhcrelaycfg =& $config['dhcrelay'];
2553

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

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

    
2583
function upgrade_067_to_068() {
2584
	global $config;
2585

    
2586
	if (!empty($config['pppoe'])) {
2587
		$config['pppoes'] = array();
2588
		$config['pppoes']['pppoe'] = array();
2589
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2590

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

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

    
2636
function upgrade_069_to_070() {
2637
	global $config;
2638

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

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

    
2650
			unset($config['nat']['onetoone'][$nidx]['internal']);
2651
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2652
		}
2653

    
2654
		unset($natent);
2655
	}
2656
}
2657

    
2658
function upgrade_070_to_071() {
2659
	global $config;
2660

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

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

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

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

    
2713
function upgrade_073_to_074() {
2714
	global $config;
2715
	rename_field($config['system']['user'], 'fullname', 'descr');
2716
}
2717

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

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

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

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

    
2781
function upgrade_079_to_080() {
2782
	global $config;
2783

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

    
2792
function upgrade_080_to_081() {
2793
	global $config;
2794
	global $g;
2795
	/* Welcome to the 2.1 migration path */
2796

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

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

    
2811
	$rrddbpath = "/var/db/rrd/";
2812
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2813

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

    
2822
	$rrdinterval = 60;
2823
	$valid = $rrdinterval * 2;
2824

    
2825
	/* Asume GigE for now */
2826
	$downstream = 125000000;
2827
	$upstream = 125000000;
2828

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

    
2836
		if (platform_booting()) {
2837
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2838
		}
2839

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

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

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

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

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

    
2892
				<!-- PDP Status -->
2893
				<last_ds> 0 </last_ds>
2894
				<value> NaN </value>
2895
				<unknown_sec> 3 </unknown_sec>
2896
			</ds>
2897
			";
2898

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

    
2931
		$value_search = "<\/row>";
2932
		$value_replace = "</row>";
2933
		$value = "<v> NaN </v>";
2934

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

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

    
2970
function upgrade_081_to_082() {
2971
	/* don't enable the allow IPv6 toggle */
2972
}
2973

    
2974
function upgrade_082_to_083() {
2975
	global $config;
2976

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

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

    
3015
function upgrade_084_to_085() {
3016
	global $config;
3017

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

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

    
3061
function upgrade_085_to_086() {
3062
	global $config, $g;
3063

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

    
3080
function upgrade_086_to_087() {
3081
	global $config, $dummynet_pipe_list;
3082

    
3083
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue'])) {
3084
		return;
3085
	}
3086

    
3087
	$dnqueue_number = 1;
3088
	$dnpipe_number = 1;
3089

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

    
3101
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
3102

    
3103
	if (!is_array($config['filter']) || !is_array($config['filter']['rule'])) {
3104
		return;
3105
	}
3106

    
3107
	require_once("shaper.inc");
3108
	read_dummynet_config();
3109

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

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

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

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

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

    
3170
					/* add ca reference to certificate */
3171
					$cert['caref'] = $ca['refid'];
3172
				}
3173

    
3174
				$config['cert'][] = $cert;
3175

    
3176
				/* create cert reference */
3177
				$setting['certref'] = $cert['refid'];
3178

    
3179
				unset($setting['certificate']);
3180
				unset($setting['private-key']);
3181
				unset($setting['cacertificate']);
3182

    
3183
			}
3184
		}
3185
	}
3186
}
3187

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

    
3204
function upgrade_090_to_091() {
3205
	global $config;
3206

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

    
3222
function upgrade_091_to_092() {
3223
	global $config;
3224

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

    
3235
function upgrade_092_to_093() {
3236
	global $g;
3237

    
3238
	$suffixes = array("concurrent", "loggedin");
3239

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

    
3247
	if (!platform_booting()) {
3248
		enable_rrd_graphing();
3249
	}
3250
}
3251

    
3252
function upgrade_093_to_094() {
3253
	global $config;
3254

    
3255
	if (isset($config['system']['powerd_mode'])) {
3256
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3257
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3258
		unset($config['system']['powerd_mode']);
3259
	}
3260
}
3261

    
3262
function upgrade_094_to_095() {
3263
	global $config;
3264

    
3265
	if (!isset($config['interfaces']) || !is_array($config['interfaces'])) {
3266
		return;
3267
	}
3268

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

    
3278
function upgrade_095_to_096() {
3279
	global $config, $g;
3280

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

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

    
3294
	/* Assume 2*10GigE for now */
3295
	$stream = 2500000000;
3296

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

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

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

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

    
3329
function upgrade_097_to_098() {
3330
	global $config, $g;
3331
	/* Disable kill_states by default */
3332
	$config['system']['kill_states'] = true;
3333
}
3334

    
3335
function upgrade_098_to_099() {
3336
	global $config;
3337

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

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

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

    
3355
function upgrade_100_to_101() {
3356
	global $config, $g;
3357

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

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

    
3374
function upgrade_101_to_102() {
3375
	global $config, $g;
3376

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

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

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

    
3409
function upgrade_102_to_103() {
3410
	global $config;
3411

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

    
3419
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3420

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

    
3429
function upgrade_103_to_104() {
3430
	global $config;
3431

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

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

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

    
3463
	/* sync all local account information */
3464
	local_sync_accounts();
3465
}
3466

    
3467
function upgrade_104_to_105() {
3468
	global $config;
3469

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

    
3484
function upgrade_105_to_106() {
3485

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

    
3489
function upgrade_106_to_107() {
3490
	global $config;
3491

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

    
3514
function upgrade_107_to_108() {
3515
	global $config;
3516

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

    
3524
function upgrade_108_to_109() {
3525
	global $config;
3526

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

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

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

    
3544
function upgrade_109_to_110() {
3545
	global $config;
3546

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

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

    
3556
		$rule['uniqid'] = uniqid();
3557
	}
3558
}
3559

    
3560
function upgrade_110_to_111() {
3561
	global $config;
3562

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

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

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

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

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

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

    
3602
	$pkg = $config['installedpackages']['unbound']['config'][0];
3603

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

    
3608
	$new = array();
3609

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

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

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

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

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

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

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

    
3685
	$config['unbound'] = $new;
3686

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

    
3697
	unset($pkg, $new);
3698
}
3699

    
3700
function upgrade_111_to_112() {
3701
	global $config;
3702

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

    
3714
function upgrade_112_to_113() {
3715
	global $config;
3716

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

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

    
3734
function upgrade_113_to_114() {
3735
	global $config;
3736

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

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

    
3749
function upgrade_114_to_115() {
3750
	global $config;
3751

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

    
3757
function upgrade_115_to_116() {
3758
	global $config;
3759

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

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

    
3771
function upgrade_116_to_117() {
3772
	global $config;
3773

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

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

    
3783
}
3784

    
3785
function upgrade_117_to_118() {
3786
	global $config;
3787

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

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

    
3800
	$a_phase1 =& $config['ipsec']['phase1'];
3801

    
3802
	foreach ($a_phase1 as &$ph1_entry) {
3803
		// update asn1dn strings from racoon's format to strongswan's 
3804
		if (isset($ph1_entry['myid_type']) && $ph1_entry['myid_type'] == 'asn1dn') {
3805
			$ph1_entry['myid_data'] =
3806
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['myid_data']);
3807
		}
3808
		if (isset($ph1_entry['peerid_type']) && $ph1_entry['peerid_type'] == 'asn1dn') {
3809
			$ph1_entry['peerid_data'] =
3810
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['peerid_data']);
3811
		}
3812
		// iketype 'auto' was removed and is really v2, update accordingly
3813
		if ($ph1_entry['iketype'] == "auto") {
3814
			$ph1_entry['iketype'] = "ikev2";
3815
		}
3816
	}
3817
}
3818

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

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

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

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

    
3836
function upgrade_119_to_120() {
3837
	global $config;
3838

    
3839
	if (!isset($config['installedpackages']['miniupnpd']['config'][0])) {
3840
		return;
3841
	}
3842

    
3843
	$miniupnpd =& $config['installedpackages']['miniupnpd']['config'][0];
3844

    
3845
	$miniupnpd['row'] = array();
3846

    
3847
	for ($i = 1; $i <= 4; $i++) {
3848
		if (isset($miniupnpd["permuser{$i}"]) && !empty($miniupnpd["permuser{$i}"])) {
3849
			$miniupnpd['row'][] = array('permuser' => $miniupnpd["permuser{$i}"]);
3850
		}
3851
		unset($miniupnpd["permuser{$i}"]);
3852
	}
3853
}
3854

    
3855
function upgrade_120_to_121() {
3856
	global $config;
3857
	foreach ($config['system']['user'] as &$user) {
3858
		if (isset($user['nt-hash'])) {
3859
			unset($user['nt-hash']);
3860
		}
3861
	}
3862
}
3863

    
3864
?>
(55-55/68)