Project

General

Profile

Download (63.5 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
			echo "\nWarning: filter rule removed " .
114
				"(interface '{$fr['interface']}' does not exist anymore).";
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
				echo "\nWarning: filter rule removed " .
126
					"(source network '{$fr['source']['network']}' does not exist anymore).";
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
				echo "\nWarning: filter rule removed " .
139
					"(destination network '{$fr['destination']['network']}' does not exist anymore).";
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
			echo "\nWarning: traffic shaper rule removed " .
159
				"(interface '{$fr['interface']}' does not exist anymore).";
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
				echo "\nWarning: traffic shaper rule removed " .
171
					"(source network '{$fr['source']['network']}' does not exist anymore).";
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
				echo "\nWarning: traffic shaper rule removed " .
184
					"(destination network '{$fr['destination']['network']}' does not exist anymore).";
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'] = "CARP vhid {$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'] = "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'] = "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

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

    
643
		$config['system']['nextuid'] = "111";
644
		$config['system']['nextgid'] = "111";
645

    
646
		/* wipe previous auth configuration */
647
		unset ($config['system']['username']);
648
		unset ($config['system']['password']);
649
	}
650
}
651

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

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

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

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

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

    
673
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
674
		$config['sysctl']['item'][4]['desc'] =    "Sending of IPv4 ICMP redirects";
675
		$config['sysctl']['item'][4]['value'] =   "default";
676

    
677
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
678
		$config['sysctl']['item'][5]['desc'] =    "Sending of IPv6 ICMP redirects";
679
		$config['sysctl']['item'][5]['value'] =   "default";
680

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

    
685
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
686
		$config['sysctl']['item'][7]['desc'] =    "Maximum incoming TCP datagram size";
687
		$config['sysctl']['item'][7]['value'] =   "default";
688

    
689
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
690
		$config['sysctl']['item'][8]['desc'] =    "Maximum outgoing TCP datagram size";
691
		$config['sysctl']['item'][8]['value'] =   "default";
692

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

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

    
701
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
702
		$config['sysctl']['item'][11]['desc'] =    "Maximum outgoing UDP datagram size";
703
		$config['sysctl']['item'][11]['value'] =   "default";
704

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

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

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

    
717
		$config['sysctl']['item'][15]['tunable'] = "kern.randompid";
718
		$config['sysctl']['item'][15]['desc'] =    "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]['desc'] =    "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]['desc'] =    "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]['desc'] =    "TCP Offload engine";
731
		$config['sysctl']['item'][18]['value'] =   "default";
732

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

    
741
		$config['sysctl']['item'][21]['tunable'] = "hw.syscons.kbd_reboot ";
742
		$config['sysctl']['item'][21]['desc'] =    "Enables ctrl+alt+delete";
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
	if (isset($config['ezshaper']))
754
		unset($config['ezshaper']);
755
}
756

    
757

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

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

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

    
809

    
810
function upgrade_043_to_044() {
811
	global $config;
812

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

    
842

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

    
861

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

    
899

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

    
904
	if(is_array($config['ipsec']['tunnel'])) {
905

    
906
		$a_phase1 = array();
907
		$a_phase2 = array();
908
		$ikeid = 0;
909

    
910
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
911

    
912
			unset($ph1ent);
913
			unset($ph2ent);
914

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

    
920
			if (!isset($tunnel['disabled'])) {
921

    
922
				$remote_gateway = $tunnel['remote-gateway'];
923

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

    
932
			/* none found, create a new one */
933

    
934
			if (!isset( $ph1ent )) {
935

    
936
				/* build new phase1 entry */
937

    
938
				$ph1ent = array();
939

    
940
				$ph1ent['ikeid'] = ++$ikeid;
941

    
942
				if (isset($tunnel['disabled']))
943
					$ph1ent['disabled'] = $tunnel['disabled'];
944

    
945
				$ph1ent['interface'] = $tunnel['interface'];
946
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
947
				$ph1ent['descr'] = $tunnel['descr'];
948

    
949
				$ph1ent['mode'] = $tunnel['p1']['mode'];
950

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

    
974
				$ph1ent['peerid_type'] = "peeraddress";
975

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

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

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

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

    
1017
				$a_phase1[] = $ph1ent;
1018
			}
1019

    
1020
			/* build new phase2 entry */
1021

    
1022
			$ph2ent = array();
1023

    
1024
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1025

    
1026
			if (isset($tunnel['disabled']))
1027
				$ph1ent['disabled'] = $tunnel['disabled'];
1028

    
1029
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1030

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

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

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

    
1057
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1058

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

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

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

    
1091
			if (isset($tunnel['pinghost']['pinghost']))
1092
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1093

    
1094
			$a_phase2[] = $ph2ent;
1095
		}
1096

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

    
1103

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

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

    
1165

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

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

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

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

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

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

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

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

    
1224

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

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

    
1263
	/* sync all local account information */
1264
	local_sync_accounts();
1265
}
1266

    
1267

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

    
1281
	unset($config['bridge']);
1282

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

    
1308

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

    
1317
	$vpnid = 1;
1318

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

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

    
1326
			if (!is_array($server))
1327
				continue;
1328

    
1329
			if ($server['auth_method'] == "pki") {
1330

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

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

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

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

    
1357
				$index++;
1358
			}
1359

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

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

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

    
1413
			/* allocate vpnid */
1414
			$server['vpnid'] = $vpnid++;
1415

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

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

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

    
1428
			if (!is_array($client))
1429
				continue;
1430

    
1431
			if ($client['auth_method'] == "pki") {
1432

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

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

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

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

    
1459
				$index++;
1460
			}
1461

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

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

    
1487
			/* allocate vpnid */
1488
			$client['vpnid'] = $vpnid++;
1489

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

    
1495
			$config['openvpn']['openvpn-client'][] = $client;
1496
		}
1497

    
1498
		unset($config['installedpackages']['openvpnclient']);
1499
	}
1500

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

    
1505
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1506

    
1507
			if (!is_array($csc))
1508
				continue;
1509

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

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

    
1540
			$config['openvpn']['openvpn-csc'][] = $csc;
1541
		}
1542

    
1543
		unset($config['installedpackages']['openvpncsc']);
1544
	}
1545

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

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

    
1569
}
1570

    
1571

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

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

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

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

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

    
1605

    
1606
function upgrade_053_to_054() {
1607
	global $config;
1608
	if(is_array($config['load_balancer']['lbpool'])) {
1609
		$lbpool_arr = $config['load_balancer']['lbpool'];
1610
		$lbpool_srv_arr = array();
1611
		$gateway_group_arr = array();
1612
		$gateways = return_gateways_array();
1613
		if (is_array($config['gateways']['gateway_item']))
1614
			$config['gateways']['gateway_item'] = array();
1615
		$a_gateways =& $config['gateways']['gateway_item'];
1616
		foreach($lbpool_arr as $lbpool) {
1617
			if($lbpool['type'] == "gateway") {
1618
				$gateway_group['name'] = $lbpool['name'];
1619
				$gateway_group['descr'] = $lbpool['desc'];
1620
				$gateway_group['trigger'] = "down";
1621
				$gateway_group['item'] = array();
1622
				$i = 0;
1623
				foreach($lbpool['servers'] as $member) {
1624
					$split = split("\|", $member);
1625
					$interface = $split[0];
1626
					$monitor = $split[1];
1627
					/* on static upgraded configuration we automatically prepend GW_ */
1628
					$static_name = "GW_" . strtoupper($interface);
1629
					if(is_ipaddr($monitor)) {
1630
						$interface = $static_name;
1631
						if(is_array($gateways[$static_name]) && isset($gateways[$static_name]['attribute']))
1632
							$a_gateways[$gateways[$static_name]['attribute']]['monitor'] = $monitor;
1633
						else
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
		echo "Migrate RRD database {$database} to new format \n";
1707
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
1708

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1788
	}
1789
	enable_rrd_graphing();
1790
	echo "Updating configuration...";
1791
}
1792

    
1793

    
1794
function upgrade_055_to_056() {
1795
	global $config;
1796

    
1797
	if (!is_array($config['system']['ca']))
1798
		$config['system']['ca'] = array();
1799
	if (!is_array($config['system']['cert']))
1800
		$config['system']['cert'] = array();
1801

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

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

    
1840

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

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

    
1872
function upgrade_058_to_059() {
1873
	global $config;
1874

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

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

    
1911
function upgrade_060_to_061() {
1912
	global $config;
1913

    
1914
	if (is_array($config['interfaces']['wan']))
1915
		$config['interfaces']['wan']['enable'] = true;
1916
	if (is_array($config['interfaces']['lan']))
1917
		$config['interfaces']['lan']['enable'] = true;
1918
}
1919

    
1920
function upgrade_061_to_062() {
1921
	global $config;
1922

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

    
1927
		foreach ($a_nat as &$natent) {
1928
			$natent['disabled'] = false;
1929
			$natent['nordr']    = false;
1930

    
1931
			$natent['source'] = array(
1932
				"not"     => false,
1933
				"any"     => true,
1934
				"port"    => ""
1935
			);
1936

    
1937
			$natent['destination'] = array(
1938
				"not"     => false,
1939
				"address" => $natent['external-address'],
1940
				"port"    => $natent['external-port']
1941
			);
1942

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

    
1951
			unset($natent['external-address']);
1952
			unset($natent['external-port']);
1953
		}
1954

    
1955
		unset($natent);
1956
	}
1957
}
1958

    
1959
function upgrade_062_to_063() {
1960
	/* Upgrade legacy Themes to the new pfsense_ng */
1961
	global $config;
1962

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

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

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

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

    
2062
?>
(40-40/51)