Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
192

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

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

    
204

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

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

    
223

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

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

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

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

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

    
250

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

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

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

    
271

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

    
286

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

    
299

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

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

    
353

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

    
359

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

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

    
383

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

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

    
398

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

    
403

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

    
410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
490
	$cron_item = array();
491
	$cron_item['minute'] = "*/5";
492
	$cron_item['hour'] = "*";
493
	$cron_item['mday'] = "*";
494
	$cron_item['month'] = "*";
495
	$cron_item['wday'] = "*";
496
	$cron_item['who'] = "root";
497
	$cron_item['command'] = "/usr/local/bin/checkreload.sh";
498

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

    
502

    
503
function upgrade_026_to_027() {
504
	global $config;
505
}
506

    
507

    
508
function upgrade_027_to_028() {
509
	global $config;
510
	$founditem = false;
511
	foreach($config['cron']['item'] as $cronitem) {
512
		if($cronitem['command'] == "/usr/local/bin/checkreload.sh")
513
			$founditem = true;
514
	}
515
	if($founditem == false) {
516
		$cron_item = array();
517
		$cron_item['minute'] = "*/5";
518
		$cron_item['hour'] = "*";
519
		$cron_item['mday'] = "*";
520
		$cron_item['month'] = "*";
521
		$cron_item['wday'] = "*";
522
		$cron_item['who'] = "root";
523
		$cron_item['command'] = "/usr/local/bin/checkreload.sh";
524
		$config['cron']['item'][] = $cron_item;
525
	}
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;
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]['pages'] = "ANY";
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]['fullname'] = "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
		unset ($config['system']['password']);
651
	}
652
}
653

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
715
		$config['sysctl']['item'][14]['tunable'] = "kern.rndtest.verbose";
716
		$config['sysctl']['item'][14]['desc'] =    gettext("Verbosity of the rndtest driver (0: do not display results on console)");
717
		$config['sysctl']['item'][14]['value'] =   "default";
718

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

    
723
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.inflight.enable";
724
		$config['sysctl']['item'][16]['desc'] =    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. ");
725
		$config['sysctl']['item'][16]['value'] =   "default";
726

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

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

    
735
		$config['sysctl']['item'][19]['tunable'] = "hw.bce.tso_enable";
736
		$config['sysctl']['item'][19]['desc'] =    gettext("TCP Offload engine - BCE");
737
		$config['sysctl']['item'][19]['value'] =   "default";
738
		
739
		$config['sysctl']['item'][20]['tunable'] = "net.inet.ip.portrange.first";
740
		$config['sysctl']['item'][20]['desc'] =    gettext("Set the ephemeral port range starting port");
741
		$config['sysctl']['item'][20]['value'] =   "default";
742

    
743
		$config['sysctl']['item'][21]['tunable'] = "hw.syscons.kbd_reboot ";
744
		$config['sysctl']['item'][21]['desc'] =    gettext("Enables ctrl+alt+delete");
745
		$config['sysctl']['item'][21]['value'] =   "default";
746

    
747
	}
748
}
749

    
750

    
751
function upgrade_041_to_042() {
752
	global $config;
753
	if (isset($config['shaper']))
754
		unset($config['shaper']);
755
	if (isset($config['ezshaper']))
756
		unset($config['ezshaper']);
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();
764
	$gateways = array();
765
	$i = 0;
766
	foreach($iflist as $ifname => $interface) {
767
		if(! interface_has_gateway($ifname)) {
768
			continue;
769
		}
770
		if(is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
771
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
772
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Static Gateway"), $ifname);
773
		} else {
774
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
775
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Dynamic Gateway"), $ifname);
776
		}
777
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
778
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
779
		/* add default gateway bit for wan on upgrade */
780
		if($ifname == "wan") {
781
			 $config['gateways']['gateway_item'][$i]['defaultgw'] = true;
782
		}
783
		if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
784
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
785
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
786
		}
787
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
788

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

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

    
811

    
812
function upgrade_043_to_044() {
813
	global $config;
814

    
815
	/* migrate static routes to the new gateways config */
816
	$gateways = return_gateways_array(true);
817
	$i = 0;
818
	if (is_array($config['staticroutes']['route'])) {
819
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
820
			$found = false;
821
			foreach ($gateways as $gwname => $gw) {
822
				if ($gw['gateway'] == $sroute['gateway']) {
823
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
824
					$found = true;
825
					break;
826
				}
827
			}
828
			if ($found == false) {
829
				$gateway = array();
830
				$gateway['name'] = "SROUTE{$i}";	
831
				$gateway['gateway'] = $sroute['gateway'];
832
				$gateway['interface'] = $sroute['interface'];
833
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
834
				if (!is_array($config['gateways']['gateway_item']))
835
					$config['gateways']['gateway_item'] = array();
836
				$config['gateways']['gateway_item'][] = $gateway;
837
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
838
				$i++;
839
			}
840
		}
841
	}
842
}
843

    
844

    
845
function upgrade_044_to_045() {
846
	global $config;
847
	$iflist = get_configured_interface_list(false, true);
848
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
849
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
850
			$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
851
			$config['vlans']['vlan'][$id] = $vlan;
852
			/* Make sure to update the interfaces section with the right name */
853
			foreach($iflist as $ifname) {
854
				if($config['interfaces'][$ifname]['if'] == "vlan{$id}") {
855
					$config['interfaces'][$ifname]['if'] = $vlan['vlanif'];
856
				}
857
			}
858
			
859
		}
860
	}
861
}
862

    
863

    
864
function upgrade_045_to_046() {
865
	global $config;
866
	/* Upgrade load balancer from slb to relayd */
867
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
868
		$vs_a = &$config['load_balancer']['virtual_server'];
869
		$pool_a = &$config['load_balancer']['lbpool'];
870
		$pools = array();
871
		/* Index pools by name */
872
		if(is_array($pool_a)) {
873
			for ($i = 0; isset($pool_a[$i]); $i++) {
874
				if($pool_a[$i]['type'] == "server") {
875
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
876
				}
877
			}
878
		}
879
		/* Convert sitedown entries to pools and re-attach */
880
		for ($i = 0; isset($vs_a[$i]); $i++) {
881
			if (isset($vs_a[$i]['sitedown'])) {
882
				$pool = array();
883
				$pool['type'] = 'server';
884
				$pool['behaviour'] = 'balance';
885
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
886
				$pool['desc'] = sprintf(gettext("Sitedown pool for VS: %s"), $vs_a[$i]['name']);
887
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
888
				$pool['servers'] = array();
889
				$pool['servers'][] = $vs_a[$i]['sitedown'];
890
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
891
				$pool_a[] = $pool;
892
				$vs_a[$i]['sitedown'] = $pool['name'];
893
			}
894
		}
895
	}
896
	if(count($config['load_balancer']) == 0) {
897
		unset($config['load_balancer']);
898
	}
899
}
900

    
901

    
902
function upgrade_046_to_047() {
903
	global $config;
904
	/* Upgrade IPsec from tunnel to phase1/phase2 */
905

    
906
	if(is_array($config['ipsec']['tunnel'])) {
907

    
908
		$a_phase1 = array();
909
		$a_phase2 = array();
910
		$ikeid = 0;
911

    
912
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
913

    
914
			unset($ph1ent);
915
			unset($ph2ent);
916

    
917
			/*
918
				*  attempt to locate an enabled phase1
919
				*  entry that matches the peer gateway
920
				*/
921

    
922
			if (!isset($tunnel['disabled'])) {
923

    
924
				$remote_gateway = $tunnel['remote-gateway'];
925

    
926
				foreach ($a_phase1 as $ph1tmp) {
927
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
928
						$ph1ent = $ph1tmp;
929
						break;
930
					}
931
				}
932
			}
933

    
934
			/* none found, create a new one */
935

    
936
			if (!isset( $ph1ent )) {
937

    
938
				/* build new phase1 entry */
939

    
940
				$ph1ent = array();
941

    
942
				$ph1ent['ikeid'] = ++$ikeid;
943

    
944
				if (isset($tunnel['disabled']))
945
					$ph1ent['disabled'] = $tunnel['disabled'];
946

    
947
				$ph1ent['interface'] = $tunnel['interface'];
948
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
949
				$ph1ent['descr'] = $tunnel['descr'];
950

    
951
				$ph1ent['mode'] = $tunnel['p1']['mode'];
952

    
953
				if (isset($tunnel['p1']['myident']['myaddress']))
954
					$ph1ent['myid_type'] = "myaddress";
955
				if (isset($tunnel['p1']['myident']['address'])) {
956
					$ph1ent['myid_type'] = "address";
957
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
958
				}
959
				if (isset($tunnel['p1']['myident']['fqdn'])) {
960
					$ph1ent['myid_type'] = "fqdn";
961
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
962
				}
963
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
964
					$ph1ent['myid_type'] = "user_fqdn";
965
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
966
				}
967
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
968
					$ph1ent['myid_type'] = "asn1dn";
969
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
970
				}
971
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
972
					$ph1ent['myid_type'] = "dyn_dns";
973
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
974
				}
975

    
976
				$ph1ent['peerid_type'] = "peeraddress";
977

    
978
				switch ($tunnel['p1']['encryption-algorithm']) {
979
					case "des":
980
					$ph1alg = array( 'name' => 'des' );
981
					break;
982
					case "3des":
983
					$ph1alg = array( 'name' => '3des' );
984
					break;
985
					case "blowfish":
986
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
987
					break;
988
					case "cast128":
989
					$ph1alg = array( 'name' => 'cast128' );
990
					break;
991
					case "rijndael":
992
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
993
					break;
994
					case "rijndael 256":
995
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
996
					break;
997
				}
998

    
999
				$ph1ent['encryption-algorithm'] = $ph1alg;
1000
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1001
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1002
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1003
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1004

    
1005
				if (isset($tunnel['p1']['pre-shared-key']))
1006
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1007
				if (isset($tunnel['p1']['cert']))
1008
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1009
				if (isset($tunnel['p1']['peercert']))
1010
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1011
				if (isset($tunnel['p1']['private-key']))
1012
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1013

    
1014
				$ph1ent['nat_traversal'] = "on";
1015
				$ph1ent['dpd_enable'] = 1;
1016
				$ph1ent['dpd_delay'] = 10;
1017
				$ph1ent['dpd_maxfail'] = 5;
1018

    
1019
				$a_phase1[] = $ph1ent;
1020
			}
1021

    
1022
			/* build new phase2 entry */
1023

    
1024
			$ph2ent = array();
1025

    
1026
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1027

    
1028
			if (isset($tunnel['disabled']))
1029
				$ph1ent['disabled'] = $tunnel['disabled'];
1030

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

    
1033
			$type = "lan";
1034
			if ($tunnel['local-subnet']['network'])
1035
				$type = $tunnel['local-subnet']['network'];
1036
			if ($tunnel['local-subnet']['address']) {
1037
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1038
				if (is_null($netbits))
1039
					$type = "address";
1040
				else
1041
					$type = "network";
1042
			}
1043

    
1044
			switch ($type) {
1045
				case "address":
1046
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1047
				break;
1048
				case "network":
1049
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1050
				break;
1051
				default:
1052
				$ph2ent['localid'] = array('type' => $type);
1053
				break;
1054
			}
1055

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

    
1059
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1060

    
1061
			$aes_count = 0;
1062
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1063
				$aes_found = false;
1064
				switch ($tunalg) {
1065
					case "des":
1066
					$ph2alg = array( 'name' => 'des' );
1067
					break;
1068
					case "3des":
1069
					$ph2alg = array( 'name' => '3des' );
1070
					break;
1071
					case "blowfish":
1072
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1073
					break;
1074
					case "cast128":
1075
					$ph2alg = array( 'name' => 'cast128' );
1076
					break;
1077
					case "rijndael":
1078
					case "rijndael 256":
1079
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1080
					$aes_found = true;
1081
					$aes_count++;
1082
					break;
1083
				}
1084

    
1085
				if( !$aes_found || ($aes_count < 2))
1086
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1087
			}
1088

    
1089
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1090
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1091
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1092

    
1093
			if (isset($tunnel['pinghost']['pinghost']))
1094
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1095

    
1096
			$a_phase2[] = $ph2ent;
1097
		}
1098

    
1099
		unset($config['ipsec']['tunnel']);
1100
		$config['ipsec']['phase1'] = $a_phase1;
1101
		$config['ipsec']['phase2'] = $a_phase2;
1102
	}
1103
}
1104

    
1105

    
1106
function upgrade_047_to_048() {
1107
	global $config;
1108
	if (!empty($config['dyndns'])) {
1109
		$config['dyndnses'] = array();
1110
		$config['dyndnses']['dyndns'] = array();
1111
		if(isset($config['dyndns'][0]['enable'])) {
1112
			$tempdyn = array();
1113
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1114
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1115
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1116
			$tempdyn['usernamefld'] = $config['dyndns'][0]['username'];
1117
			$tempdyn['passwordfld'] = $config['dyndns'][0]['password'];
1118
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1119
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];		
1120
			$tempdyn['interface'] = "wan";
1121
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1122
			$config['dyndnses']['dyndns'][] = $tempdyn;
1123
		}
1124
		unset($config['dyndns']);
1125
	}		
1126
	if (!empty($config['dnsupdate'])) {
1127
		$pconfig = $config['dnsupdate'][0];
1128
		if (!$pconfig['ttl'])
1129
			$pconfig['ttl'] = 60;
1130
		if (!$pconfig['keytype'])
1131
			$pconfig['keytype'] = "zone";
1132
		$pconfig['interface'] = "wan";
1133
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1134
		unset($config['dnsupdate']);
1135
	}
1136

    
1137
	if (is_array($config['pppoe'])) {
1138
		$pconfig = array();
1139
		$pconfig['username'] = $config['pppoe']['username'];
1140
		$pconfig['password'] = $config['pppoe']['password'];
1141
		$pconfig['provider'] = $config['pppoe']['provider'];
1142
		$pconfig['ondemand'] = isset($config['pppoe']['ondemand']);
1143
		$pconfig['timeout'] = $config['pppoe']['timeout'];
1144
		unset($config['pppoe']);
1145
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1146
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1147
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1148
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1149
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1150
	}
1151
	if (is_array($config['pptp'])) {
1152
		$pconfig = array();
1153
		$pconfig['username'] = $config['pptp']['username'];
1154
		$pconfig['password'] = $config['pptp']['password'];
1155
		$pconfig['provider'] = $config['pptp']['provider'];
1156
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1157
		$pconfig['timeout'] = $config['pptp']['timeout'];
1158
		unset($config['pptp']);
1159
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1160
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1161
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1162
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1163
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1164
	}
1165
}
1166

    
1167

    
1168
function upgrade_048_to_049() {
1169
	global $config;
1170
	/* setup new all users group */
1171
	$all = array();
1172
	$all['name'] = "all";
1173
	$all['description'] = gettext("All Users");
1174
	$all['scope'] = "system";
1175
	$all['gid'] = 1998;
1176
	$all['member'] = array();
1177

    
1178
	if (!is_array($config['system']['group']))
1179
		$config['system']['group'] = array();
1180

    
1181
	/* work around broken uid assignments */
1182
	$config['system']['nextuid'] = 2000;
1183
	foreach ($config['system']['user'] as & $user) {
1184
		if (isset($user['uid']) && !$user['uid'])
1185
			continue;
1186
		$user['uid'] = $config['system']['nextuid']++;
1187
	}
1188

    
1189
	/* work around broken gid assignments */
1190
	$config['system']['nextgid'] = 2000;
1191
	foreach ($config['system']['group'] as & $group) {
1192
		if ($group['name'] == $g['admin_group'])
1193
			$group['gid'] = 1999;
1194
		else
1195
			$group['gid'] = $config['system']['nextgid']++;
1196
	}
1197

    
1198
	/* build group membership information */
1199
	foreach ($config['system']['group'] as & $group) {
1200
		$group['member'] = array();
1201
		foreach ($config['system']['user'] as & $user) {
1202
			$groupnames = explode(",", $user['groupname']);
1203
			if (in_array($group['name'],$groupnames))
1204
				$group['member'][] = $user['uid'];
1205
		}
1206
	}
1207

    
1208
	/* reset user group information */
1209
	foreach ($config['system']['user'] as & $user) {
1210
		unset($user['groupname']);
1211
		$all['member'][] = $user['uid'];
1212
	}
1213

    
1214
	/* reset group scope information */
1215
	foreach ($config['system']['group'] as & $group)
1216
		if ($group['name'] != $g['admin_group'])
1217
		$group['scope'] = "user";
1218

    
1219
	/* insert new all group */
1220
	$groups = Array();
1221
	$groups[] = $all;
1222
	$groups = array_merge($config['system']['group'],$groups);
1223
	$config['system']['group'] = $groups;
1224
}
1225

    
1226

    
1227
function upgrade_049_to_050() {
1228
	global $config;
1229
	/* update user privileges */
1230
	foreach ($config['system']['user'] as & $user) {
1231
		$privs = array();
1232
		if (!is_array($user['priv'])) {
1233
			unset($user['priv']);
1234
			continue;
1235
		}
1236
		foreach ($user['priv'] as $priv) {
1237
			switch($priv['id']) {
1238
				case "hasshell":
1239
				$privs[] = "user-shell-access";
1240
				break;
1241
				case "copyfiles":
1242
				$privs[] = "user-copy-files";
1243
				break;
1244
			}
1245
		}
1246
		$user['priv'] = $privs;
1247
	}
1248

    
1249
	/* update group privileges */
1250
	foreach ($config['system']['group'] as & $group) {
1251
		$privs = array();
1252
		if (!is_array($group['pages'])) {
1253
			unset($group['pages']);
1254
			continue;
1255
		}
1256
		foreach ($group['pages'] as $page) {
1257
			$priv = map_page_privname($page);
1258
			if ($priv)
1259
				$privs[] = $priv;
1260
		}
1261
		unset($group['pages']);
1262
		$group['priv'] = $privs;
1263
	}
1264

    
1265
	/* sync all local account information */
1266
	local_sync_accounts();
1267
}
1268

    
1269

    
1270
function upgrade_050_to_051() {
1271
	global $config;
1272
	$pconfig = array();
1273
	$pconfig['desc'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1274
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1275
	$pconfig['value'] = "1";
1276
	$config['sysctl']['item'][] = $pconfig;
1277
	$pconfig = array();
1278
	$pconfig['desc'] = "Set to 1 to enable filtering on the bridge interface";
1279
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1280
	$pconfig['value'] = "0";
1281
	$config['sysctl']['item'][] = $pconfig;
1282

    
1283
	unset($config['bridge']);
1284

    
1285
	$convert_bridges = false;
1286
	foreach($config['interfaces'] as $intf) {
1287
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1288
			$config['bridges'] = array();
1289
			$config['bridges']['bridged'] = array();
1290
			$convert_bridges = true;
1291
			break;
1292
		}
1293
	}
1294
	if ($convert_bridges == true) {
1295
		$i = 0;
1296
		foreach ($config['interfaces'] as $ifr => &$intf) {
1297
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1298
				$nbridge = array();
1299
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1300
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1301
				$nbridge['bridgeif'] = "bridge{$i}";
1302
				$config['bridges']['bridged'][] = $nbridge;
1303
				unset($intf['bridge']);
1304
				$i++;
1305
			}
1306
		}
1307
	}
1308
}
1309

    
1310

    
1311
function upgrade_051_to_052() {
1312
	global $config;
1313
	$config['openvpn'] = array();
1314
	if (!is_array($config['system']['ca']))
1315
		$config['system']['ca'] = array();
1316
	if (!is_array($config['system']['cert']))
1317
		$config['system']['cert'] = array();
1318

    
1319
	$vpnid = 1;
1320

    
1321
	/* openvpn server configurations */
1322
	if (is_array($config['installedpackages']['openvpnserver'])) {
1323
		$config['openvpn']['openvpn-server'] = array();
1324

    
1325
		$index = 1;
1326
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1327

    
1328
			if (!is_array($server))
1329
				continue;
1330

    
1331
			if ($server['auth_method'] == "pki") {
1332

    
1333
				/* create ca entry */
1334
				$ca = array();
1335
				$ca['refid'] = uniqid();
1336
				$ca['name'] = "OpenVPN Server CA #{$index}";
1337
				$ca['crt'] = $server['ca_cert'];
1338
				$ca['crl'] = $server['crl'];
1339
				$config['system']['ca'][] = $ca;
1340

    
1341
				/* create ca reference */
1342
				unset($server['ca_cert']);
1343
				unset($server['crl']);
1344
				$server['caref'] = $ca['refid'];
1345

    
1346
				/* create cert entry */
1347
				$cert = array();
1348
				$cert['refid'] = uniqid();
1349
				$cert['name'] = "OpenVPN Server Certificate #{$index}";
1350
				$cert['crt'] = $server['server_cert'];
1351
				$cert['prv'] = $server['server_key'];
1352
				$config['system']['cert'][] = $cert;
1353

    
1354
				/* create cert reference */
1355
				unset($server['server_cert']);
1356
				unset($server['server_key']);
1357
				$server['certref'] = $cert['refid'];
1358

    
1359
				$index++;
1360
			}
1361

    
1362
			/* determine operational mode */
1363
			if ($server['auth_method'] == 'pki') {
1364
				if($server['nopool']) {
1365
					$server['mode'] = "p2p_tls";
1366
				} else {
1367
					$server['mode'] = "server_tls";
1368
				}
1369
			} else {
1370
				$server['mode'] = "p2p_shared_key";
1371
			}
1372
			unset($server['auth_method']);
1373

    
1374
			/* modify configuration values */
1375
			$server['dh_length'] = 1024;
1376
			unset($server['dh_params']);
1377
			if (!$server['interface'])
1378
				$server['interface'] = 'wan';
1379
			$server['tunnel_network'] = $server['addresspool'];
1380
			unset($server['addresspool']);
1381
			if (isset($server['use_lzo'])) {
1382
				$server['compression'] = "on";
1383
				unset($server['use_lzo']);
1384
			}
1385
			if ($server['nopool'])
1386
				$server['pool_enable'] = false;
1387
			else
1388
				$server['pool_enable'] = "yes";
1389
			unset($server['nopool']);
1390
			$server['dns_domain'] = $server['dhcp_domainname'];
1391
			unset($server['dhcp_domainname']);
1392
			$server['dns_server1'] = $server['dhcp_dns'];
1393
			unset($server['dhcp_dns']);
1394
			$server['ntp_server1'] = $server['dhcp_ntp'];
1395
			unset($server['dhcp_ntp']);
1396
			if ($server['dhcp_nbtdisable'])
1397
				$server['netbios_enable'] = false;
1398
			else
1399
				$server['netbios_enable'] = "yes";
1400
			unset($server['dhcp_nbtdisable']);
1401
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1402
			unset($server['dhcp_nbttype']);
1403
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1404
			unset($server['dhcp_nbtscope']);
1405
			$server['nbdd_server1'] = $server['dhcp_nbdd'];
1406
			unset($server['dhcp_nbdd']);
1407
			$server['wins_server1'] = $server['dhcp_wins'];
1408
			unset($server['dhcp_wins']);
1409

    
1410
			if (!empty($server['disable']))
1411
				$server['disable'] = true;
1412
			else
1413
				unset($server['disable']);
1414

    
1415
			/* allocate vpnid */
1416
			$server['vpnid'] = $vpnid++;
1417

    
1418
			$config['openvpn']['openvpn-server'][] = $server;
1419
		}
1420
		unset($config['installedpackages']['openvpnserver']);
1421
	}
1422

    
1423
	/* openvpn client configurations */
1424
	if (is_array($config['installedpackages']['openvpnclient'])) {
1425
		$config['openvpn']['openvpn-client'] = array();
1426

    
1427
		$index = 1;
1428
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1429

    
1430
			if (!is_array($client))
1431
				continue;
1432

    
1433
			if ($client['auth_method'] == "pki") {
1434

    
1435
				/* create ca entry */
1436
				$ca = array();
1437
				$ca['refid'] = uniqid();
1438
				$ca['name'] = "OpenVPN Client CA #{$index}";
1439
				$ca['crt'] = $client['ca_cert'];
1440
				$ca['crl'] = $client['crl'];
1441
				$config['system']['ca'][] = $ca;
1442

    
1443
				/* create ca reference */
1444
				unset($client['ca_cert']);
1445
				unset($client['crl']);
1446
				$client['caref'] = $ca['refid'];
1447

    
1448
				/* create cert entry */
1449
				$cert = array();
1450
				$cert['refid'] = uniqid();
1451
				$cert['name'] = "OpenVPN Client Certificate #{$index}";
1452
				$cert['crt'] = $client['client_cert'];
1453
				$cert['prv'] = $client['client_key'];
1454
				$config['system']['cert'][] = $cert;
1455

    
1456
				/* create cert reference */
1457
				unset($client['client_cert']);
1458
				unset($client['client_key']);
1459
				$client['certref'] = $cert['refid'];
1460

    
1461
				$index++;
1462
			}
1463

    
1464
			/* determine operational mode */
1465
			if ($client['auth_method'] == 'pki')
1466
				$client['mode'] = "p2p_tls";
1467
			else
1468
				$client['mode'] = "p2p_shared_key";
1469
			unset($client['auth_method']);
1470

    
1471
			/* modify configuration values */
1472
			if (!$client['interface'])
1473
				$client['interface'] = 'wan';
1474
			$client['tunnel_network'] = $client['interface_ip'];
1475
			unset($client['interface_ip']);
1476
			$client['server_addr'] = $client['serveraddr'];
1477
			unset($client['serveraddr']);
1478
			$client['server_port'] = $client['serverport'];
1479
			unset($client['serverport']);
1480
			$client['proxy_addr'] = $client['poxy_hostname'];
1481
			unset($client['proxy_addr']);
1482
			if (isset($client['use_lzo'])) {
1483
				$client['compression'] = "on";
1484
				unset($client['use_lzo']);
1485
			}
1486
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1487
			unset($client['infiniteresolvretry']);
1488

    
1489
			/* allocate vpnid */
1490
			$client['vpnid'] = $vpnid++;
1491

    
1492
			if (!empty($client['disable']))
1493
				$client['disable'] = true;
1494
			else
1495
				unset($client['disable']);
1496

    
1497
			$config['openvpn']['openvpn-client'][] = $client;
1498
		}
1499

    
1500
		unset($config['installedpackages']['openvpnclient']);
1501
	}
1502

    
1503
	/* openvpn client specific configurations */
1504
	if (is_array($config['installedpackages']['openvpncsc'])) {
1505
		$config['openvpn']['openvpn-csc'] = array();
1506

    
1507
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1508

    
1509
			if (!is_array($csc))
1510
				continue;
1511

    
1512
			/* modify configuration values */
1513
			$csc['common_name'] = $csc['commonname'];
1514
			unset($csc['commonname']);
1515
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1516
			unset($csc['ifconfig_push']);
1517
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1518
			unset($csc['dhcp_domainname']);
1519
			$csc['dns_server1'] = $csc['dhcp_dns'];
1520
			unset($csc['dhcp_dns']);
1521
			$csc['ntp_server1'] = $csc['dhcp_ntp'];
1522
			unset($csc['dhcp_ntp']);
1523
			if ($csc['dhcp_nbtdisable'])
1524
				$csc['netbios_enable'] = false;
1525
			else
1526
				$csc['netbios_enable'] = "yes";
1527
			unset($csc['dhcp_nbtdisable']);
1528
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1529
			unset($csc['dhcp_nbttype']);
1530
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1531
			unset($csc['dhcp_nbtscope']);
1532
			$csc['nbdd_server1'] = $csc['dhcp_nbdd'];
1533
			unset($csc['dhcp_nbdd']);
1534
			$csc['wins_server1'] = $csc['dhcp_wins'];
1535
			unset($csc['dhcp_wins']);
1536

    
1537
			if (!empty($csc['disable']))
1538
				$csc['disable'] = true;
1539
			else
1540
				unset($csc['disable']);
1541

    
1542
			$config['openvpn']['openvpn-csc'][] = $csc;
1543
		}
1544

    
1545
		unset($config['installedpackages']['openvpncsc']);
1546
	}
1547

    
1548
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1549
		count($config['openvpn']['openvpn-client']) > 0) {
1550
		$ovpnrule = array();
1551
                $ovpnrule['type'] = "pass";
1552
                $ovpnrule['interface'] = "openvpn";
1553
                $ovpnrule['statetype'] = "keep state";
1554
                $ovpnrule['source'] = array();
1555
                $ovpnrule['destination'] = array();
1556
                $ovpnrule['source']['any'] = true;
1557
                $ovpnrule['destination']['any'] = true;
1558
                $ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1559
		$config['filter']['rule'][] = $ovpnrule;
1560
	}
1561

    
1562
	/*
1563
		* FIXME: hack to keep things working with no installedpackages
1564
		* or carp array in the configuration data.
1565
		*/
1566
	if (!is_array($config['installedpackages']))
1567
		$config['installedpackages'] = array();
1568
	if (!is_array($config['installedpackages']['carp']))
1569
		$config['installedpackages']['carp'] = array();
1570

    
1571
}
1572

    
1573

    
1574
function upgrade_052_to_053() {
1575
	global $config;
1576
	if (!is_array($config['system']['ca']))
1577
		$config['system']['ca'] = array();
1578
	if (!is_array($config['system']['cert']))
1579
		$config['system']['cert'] = array();
1580

    
1581
	/* migrate advanced admin page webui ssl to certifcate mngr */
1582
	if ($config['system']['webgui']['certificate'] &&
1583
	$config['system']['webgui']['private-key']) {
1584

    
1585
		/* create cert entry */
1586
		$cert = array();
1587
		$cert['refid'] = uniqid();
1588
		$cert['name'] = "webConfigurator SSL Certificate";
1589
		$cert['crt'] = $config['system']['webgui']['certificate'];
1590
		$cert['prv'] = $config['system']['webgui']['private-key'];
1591
		$config['system']['cert'][] = $cert;
1592

    
1593
		/* create cert reference */
1594
		unset($config['system']['webgui']['certificate']);
1595
		unset($config['system']['webgui']['private-key']);
1596
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1597
	}
1598

    
1599
	/* migrate advanced admin page ssh keys to user manager */
1600
	if ($config['system']['ssh']['authorizedkeys']) {
1601
		$admin_user =& getUserEntryByUID(0);
1602
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1603
		unset($config['system']['ssh']['authorizedkeys']);
1604
	}
1605
}
1606

    
1607

    
1608
function upgrade_053_to_054() {
1609
	global $config;
1610
	if(is_array($config['load_balancer']['lbpool'])) {
1611
		$lbpool_arr = $config['load_balancer']['lbpool'];
1612
		$lbpool_srv_arr = array();
1613
		$gateway_group_arr = array();
1614
		$gateways = return_gateways_array();
1615
		if (! is_array($config['gateways']['gateway_item']))
1616
			$config['gateways']['gateway_item'] = array();
1617

    
1618
		$a_gateways =& $config['gateways']['gateway_item'];
1619
		foreach($lbpool_arr as $lbpool) {
1620
			if($lbpool['type'] == "gateway") {
1621
				$gateway_group['name'] = $lbpool['name'];
1622
				$gateway_group['descr'] = $lbpool['desc'];
1623
				$gateway_group['trigger'] = "down";
1624
				$gateway_group['item'] = array();
1625
				$i = 0;
1626
				foreach($lbpool['servers'] as $member) {
1627
					$split = split("\|", $member);
1628
					$interface = $split[0];
1629
					$monitor = $split[1];
1630
					/* on static upgraded configuration we automatically prepend GW_ */
1631
					$static_name = "GW_" . strtoupper($interface);
1632
					if(is_ipaddr($monitor)) {
1633
						$interface = $static_name;
1634
						$config['interfaces'][$interface]['monitorip'] = $monitor;
1635
					}
1636
					/* on failover increment tier. Else always assign 1 */
1637
					if($lbpool['behaviour'] == "failover") {
1638
						$i++;
1639
					} else {
1640
						$i = 1;
1641
					}
1642
					$gateway_group['item'][] = "$interface|$i";
1643
				}
1644
				$gateway_group_arr[] = $gateway_group;
1645
			} else {
1646
				$lbpool_srv_arr[] = $lbpool;
1647
			}
1648
		}
1649
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1650
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1651
	}
1652
	// Unset lbpool if we no longer have any server pools
1653
	if (count($lbpool_srv_arr) == 0) {
1654
		if(empty($config['load_balancer'])) {
1655
			unset($config['load_balancer']);
1656
		} else {
1657
			unset($config['load_balancer']['lbpool']);
1658
		}
1659
	} else {
1660
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1661
	}
1662
	// Only set the gateway group array if we converted any
1663
	if (count($gateway_group_arr) != 0) {
1664
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1665
	}
1666
}
1667

    
1668

    
1669
function upgrade_054_to_055() {
1670
	global $config;
1671
	global $g;
1672

    
1673
	/* RRD files changed for quality, traffic and packets graphs */
1674
	//ini_set("max_execution_time", "1800");
1675
	/* convert traffic RRD file */
1676
	global $parsedcfg, $listtags;
1677
	$listtags = array("ds", "v", "rra", "row");
1678

    
1679
	$rrddbpath = "/var/db/rrd/";
1680
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1681

    
1682
	$rrdinterval = 60;
1683
	$valid = $rrdinterval * 2;
1684

    
1685
	/* Asume GigE for now */
1686
	$downstream = 125000000;
1687
	$upstream = 125000000;
1688

    
1689
	/* build a list of quality databases */
1690
	/* roundtrip has become delay */
1691
	function divide_delay($delayval) {
1692
		$delayval = floatval($delayval);
1693
		$delayval = ($delayval / 1000);
1694
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1695
		return $delayval;
1696
	}
1697
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1698
	$databases = array();
1699
	chdir($rrddbpath);
1700
	$databases = glob("*-quality.rrd");
1701
	rsort($databases);
1702
	foreach($databases as $database) {
1703
		$xmldump = "{$database}.old.xml";
1704
		$xmldumpnew = "{$database}.new.xml";
1705

    
1706
		if ($g['booting'])
1707
			echo "Migrate RRD database {$database} to new format \n";
1708
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
1709

    
1710
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
1711
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1712
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1713
		$rrdold = $rrdold['rrd'];
1714

    
1715
		$i = 0;
1716
		foreach($rrdold['rra'] as $rra) {
1717
			$l = 0;
1718
			foreach($rra['database']['row'] as $row) {
1719
				$vnew = divide_delay($row['v'][1]);
1720
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1721
				$l++;
1722
			}
1723
			$i++;
1724
		}
1725

    
1726
		$rrdxml = dump_xml_config_raw($rrdold, "rrd");
1727
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1728
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1729

    
1730
	}
1731
	/* let apinger recreate required files */
1732
	setup_gateways_monitor();
1733

    
1734
	/* build a list of traffic and packets databases */
1735
	$databases = array();
1736
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
1737
	rsort($databases);
1738
	foreach($databases as $database) {
1739
		$databasetmp = "{$database}.tmp";
1740
		$xmldump = "{$database}.old.xml";
1741
		$xmldumptmp = "{$database}.tmp.xml";
1742
		$xmldumpnew = "{$database}.new.xml";
1743

    
1744
		if ($g['booting'])
1745
			echo "Migrate RRD database {$database} to new format \n";
1746
		/* rename DS source */
1747
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1748
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1749

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

    
1753
		/* create new rrd database file */
1754
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
1755
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
1756
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
1757
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
1758
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
1759
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
1760
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
1761
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
1762
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
1763

    
1764
		create_new_rrd("$rrdcreate");
1765
		/* create temporary xml from new RRD */
1766
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
1767

    
1768
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1769
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1770
		$rrdold = $rrdold['rrd'];
1771

    
1772
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
1773
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
1774
		$rrdnew = $rrdnew['rrd'];
1775

    
1776
		/* remove any MAX RRA's. Not needed for traffic. */
1777
		$i = 0;
1778
		foreach ($rrdold['rra'] as $rra) {
1779
			if(trim($rra['cf']) == "MAX") {
1780
				unset($rrdold['rra'][$i]);
1781
			}
1782
			$i++;
1783
		}
1784

    
1785
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
1786
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
1787
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1788
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1789

    
1790
	}
1791
	enable_rrd_graphing();
1792
	if ($g['booting'])
1793
		echo "Updating configuration...";
1794
}
1795

    
1796

    
1797
function upgrade_055_to_056() {
1798
	global $config;
1799

    
1800
	if (!is_array($config['system']['ca']))
1801
		$config['system']['ca'] = array();
1802
	if (!is_array($config['system']['cert']))
1803
		$config['system']['cert'] = array();
1804

    
1805
	/* migrate ipsec ca's to cert manager */
1806
	if (is_array($config['ipsec']['cacert'])) {
1807
		foreach($config['ipsec']['cacert'] as & $cacert) {
1808
			$ca = array();
1809
			$ca['refid'] = uniqid();
1810
			if (is_array($cacert['cert']))
1811
				$ca['crt'] = $cacert['cert'][0];
1812
			else
1813
				$ca['crt'] = $cacert['cert'];
1814
			$ca['name'] = $cacert['ident'];
1815
			$config['system']['ca'][] = $ca;
1816
		}
1817
		unset($config['ipsec']['cacert']);
1818
	}
1819

    
1820
	/* migrate phase1 certificates to cert manager */
1821
	if (is_array($config['ipsec']['phase1'])) {
1822
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
1823
			$cert = array();
1824
			$cert['refid'] = uniqid();
1825
			$cert['name'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
1826
			if (is_array($ph1ent['cert']))
1827
				$cert['crt'] = $ph1ent['cert'][0];
1828
			else
1829
				$cert['crt'] = $ph1ent['cert'];
1830
			$cert['prv'] = $ph1ent['private-key'];
1831
			$config['system']['cert'][] = $cert;
1832
			$ph1ent['certref'] = $cert['refid'];
1833
			if ($ph1ent['cert'])
1834
				unset($ph1ent['cert']);
1835
			if ($ph1ent['private-key'])
1836
				unset($ph1ent['private-key']);
1837
			if ($ph1ent['peercert'])
1838
				unset($ph1ent['peercert']);
1839
		}
1840
	}
1841
}
1842

    
1843

    
1844
function upgrade_056_to_057() {
1845
	global $config;
1846
	/* migrate captivate portal to user manager */
1847
	if (is_array($config['captiveportal']['user'])) {
1848
		foreach($config['captiveportal']['user'] as $user) {
1849
			// avoid user conflicts
1850
			if ($config['system']['user'][$user['name']])
1851
				continue;
1852
			$user['scope'] = "user";
1853
			if (isset($user['expirationdate'])) {
1854
				$user['expires'] = $user['expirationdate'];
1855
				unset($user['expirationdate']);
1856
			}
1857
			if (isset($user['password'])) {
1858
				$user['md5-hash'] = $user['password'];
1859
				unset($user['password']);
1860
			}
1861
			$config['system']['user'][] = $user;
1862
		}
1863
		unset($config['captiveportal']['user']);
1864
	}
1865
}
1866

    
1867
function upgrade_057_to_058() {
1868
	global $config;
1869
	/* set all phase2 entries to tunnel mode */
1870
	if (is_array($config['ipsec']['phase2']))
1871
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1872
			$ph2ent['mode'] = 'tunnel';
1873
}
1874

    
1875
function upgrade_058_to_059() {
1876
	global $config;
1877

    
1878
	if (is_array($config['schedules']['schedule'])) {
1879
		foreach ($config['schedules']['schedule'] as & $schedl)
1880
			$schedl['schedlabel'] = uniqid();
1881
	}
1882
}
1883

    
1884
function upgrade_059_to_060() {
1885
	global $config;
1886
	require_once("/etc/inc/certs.inc");
1887
	if (is_array($config['system']['ca'])) {
1888
		/* Locate issuer for all CAs */
1889
		foreach ($config['system']['ca'] as & $ca) {
1890
			$subject = cert_get_subject($ca['crt']);
1891
			$issuer = cert_get_issuer($ca['crt']);
1892
			if($issuer <> $subject) {
1893
				$issuer_crt =& lookup_ca_by_subject($issuer);
1894
				if($issuer_crt)
1895
					$ca['caref'] = $issuer_crt['refid'];
1896
			}
1897
		}
1898
		
1899
		/* Locate issuer for all certificates */
1900
		if (is_array($config['system']['cert'])) {
1901
			foreach ($config['system']['cert'] as & $cert) {
1902
				$subject = cert_get_subject($cert['crt']);
1903
				$issuer = cert_get_issuer($cert['crt']);
1904
				if($issuer <> $subject) {
1905
					$issuer_crt =& lookup_ca_by_subject($issuer);
1906
					if($issuer_crt)
1907
						$cert['caref'] = $issuer_crt['refid'];
1908
				}
1909
			}
1910
		}
1911
	}
1912
}
1913

    
1914
function upgrade_060_to_061() {
1915
	global $config;
1916

    
1917
	if (is_array($config['interfaces']['wan']))
1918
		$config['interfaces']['wan']['enable'] = true;
1919
	if (is_array($config['interfaces']['lan']))
1920
		$config['interfaces']['lan']['enable'] = true;
1921
}
1922

    
1923
function upgrade_061_to_062() {
1924
	global $config;
1925

    
1926
	/* Convert NAT port forwarding rules */
1927
	if (is_array($config['nat']['rule'])) {
1928
		$a_nat = &$config['nat']['rule'];
1929

    
1930
		foreach ($a_nat as &$natent) {
1931
			$natent['disabled'] = false;
1932
			$natent['nordr']    = false;
1933

    
1934
			$natent['source'] = array(
1935
				"not"     => false,
1936
				"any"     => true,
1937
				"port"    => ""
1938
			);
1939

    
1940
			$natent['destination'] = array(
1941
				"not"     => false,
1942
				"address" => $natent['external-address'],
1943
				"port"    => $natent['external-port']
1944
			);
1945

    
1946
			if (empty($natent['destination']['address'])) {
1947
				unset($natent['destination']['address']);
1948
				$natent['destination']['network'] = $natent['interface'] . 'ip';
1949
			} else if ($natent['destination']['address'] == 'any') {
1950
				unset($natent['destination']['address']);
1951
				$natent['destination']['any'] = true;
1952
			}
1953

    
1954
			unset($natent['external-address']);
1955
			unset($natent['external-port']);
1956
		}
1957

    
1958
		unset($natent);
1959
	}
1960
}
1961

    
1962
function upgrade_062_to_063() {
1963
	/* Upgrade legacy Themes to the new pfsense_ng */
1964
	global $config;
1965

    
1966
	switch($config['theme']) {
1967
		case "nervecenter":
1968
			$config['theme'] = "pfsense_ng";
1969
			break;
1970
	}
1971
	
1972
}
1973

    
1974
function upgrade_063_to_064() {
1975
	global $config;
1976
	$j=0;
1977
	$ifcfg = &$config['interfaces'];
1978
	
1979
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {	
1980
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1981
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
1982
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
1983
			$j++;
1984
			if (isset($ppp['port'])){
1985
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
1986
				unset($config['ppps']['ppp'][$pppid]['port']);
1987
			}
1988
			if (!isset($ppp['type'])){
1989
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
1990
			}
1991
			if (isset($ppp['defaultgw']))
1992
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
1993
		}
1994
	}
1995
	
1996
	if (!is_array($config['ppps']['ppp']))
1997
		$config['ppps']['ppp'] = array();
1998
	$a_ppps = &$config['ppps']['ppp'];
1999

    
2000
	foreach ($ifcfg as $ifname => $ifinfo) {
2001
		$ppp = array();
2002
		// For pppoe conversion
2003
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2004
			if (isset($ifinfo['ptpid']))
2005
				continue;
2006
			$ppp['ptpid'] =  $j;
2007
			$ppp['type'] = $ifinfo['ipaddr'];
2008
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2009
			$ppp['ports'] = $ifinfo['if'];
2010
			if ($ifinfo['ipaddr'] == "pppoe"){
2011
				$ppp['username'] = $ifinfo['pppoe_username'];
2012
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2013
			}
2014
			if ($ifinfo['ipaddr'] == "pptp"){
2015
				$ppp['username'] = $ifinfo['pptp_username'];
2016
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2017
			}
2018
			
2019
			if (isset($ifinfo['provider']))
2020
				$ppp['provider'] = $ifinfo['provider'];
2021
			if (isset($ifinfo['ondemand']))
2022
				$ppp['ondemand'] = true;
2023
			if (isset($ifinfo['timeout']))
2024
				$ppp['idletimeout'] = $ifinfo['timeout'];
2025
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2026
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2027
				if (is_array($config['cron']['item'])) {
2028
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2029
						$item = $config['cron']['item'][$i];
2030
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2031
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2032
					}
2033
				}
2034
			}
2035
			if (isset($ifinfo['local']))
2036
				$ppp['localip'] = $ifinfo['local'];
2037
			if (isset($ifinfo['subnet']))
2038
				$ppp['subnet'] = $ifinfo['subnet'];
2039
			if (isset($ifinfo['remote']))
2040
				$ppp['gateway'] = $ifinfo['remote'];
2041

    
2042
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2043
			$j++;
2044
			
2045
			unset($ifcfg[$ifname]['pppoe_username']);
2046
			unset($ifcfg[$ifname]['pppoe_password']);
2047
			unset($ifcfg[$ifname]['provider']);
2048
			unset($ifcfg[$ifname]['ondemand']);
2049
			unset($ifcfg[$ifname]['timeout']);
2050
			unset($ifcfg[$ifname]['pppoe_reset']);
2051
			unset($ifcfg[$ifname]['pppoe_preset']);
2052
			unset($ifcfg[$ifname]['pppoe']);
2053
			unset($ifcfg[$ifname]['pptp_username']);
2054
			unset($ifcfg[$ifname]['pptp_password']);
2055
			unset($ifcfg[$ifname]['local']);
2056
			unset($ifcfg[$ifname]['subnet']);
2057
			unset($ifcfg[$ifname]['remote']);
2058
			
2059
			$a_ppps[] = $ppp;
2060
			
2061
		}
2062
	}
2063
}
2064

    
2065
function upgrade_064_to_065() {
2066
	/* Disable TSO and LRO in upgraded configs */
2067
	global $config;
2068
	$config['system']['disablesegmentationoffloading'] = true;
2069
	$config['system']['disablelargereceiveoffloading'] = true;
2070
}
2071

    
2072
?>
(41-41/53)