Project

General

Profile

Download (63.9 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
		$config['dnsupdates'][0]['dnsupdate'] = array();
1126
		$pconfig = array();
1127
		$pconfig['dnsupdate_enable'] = isset($config['dnsupdate'][0]['enable']);
1128
		$pconfig['dnsupdate_host'] = $config['dnsupdate'][0]['host'];
1129
		$pconfig['dnsupdate_ttl'] = $config['dnsupdate'][0]['ttl'];
1130
		if (!$pconfig['dnsupdate_ttl'])
1131
			$pconfig['dnsupdate_ttl'] = 60;
1132
		$pconfig['dnsupdate_keydata'] = $config['dnsupdate'][0]['keydata'];
1133
		$pconfig['dnsupdate_keyname'] = $config['dnsupdate'][0]['keyname'];
1134
		$pconfig['dnsupdate_keytype'] = $config['dnsupdate'][0]['keytype'];
1135
		if (!$pconfig['dnsupdate_keytype'])
1136
			$pconfig['dnsupdate_keytype'] = "zone";
1137
		$pconfig['dnsupdate_server'] = $config['dnsupdate'][0]['server'];
1138
		$pconfig['dnsupdate_usetcp'] = isset($config['dnsupdate'][0]['usetcp']);
1139
		$pconfig['interface'] = "wan";
1140
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1141
		unset($config['dnsupdate']);
1142
	}
1143

    
1144
	if (is_array($config['pppoe'])) {
1145
		$pconfig = array();
1146
		$pconfig['username'] = $config['pppoe']['username'];
1147
		$pconfig['password'] = $config['pppoe']['password'];
1148
		$pconfig['provider'] = $config['pppoe']['provider'];
1149
		$pconfig['ondemand'] = isset($config['pppoe']['ondemand']);
1150
		$pconfig['timeout'] = $config['pppoe']['timeout'];
1151
		unset($config['pppoe']);
1152
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1153
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1154
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1155
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1156
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1157
	}
1158
	if (is_array($config['pptp'])) {
1159
		$pconfig = array();
1160
		$pconfig['username'] = $config['pptp']['username'];
1161
		$pconfig['password'] = $config['pptp']['password'];
1162
		$pconfig['provider'] = $config['pptp']['provider'];
1163
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1164
		$pconfig['timeout'] = $config['pptp']['timeout'];
1165
		unset($config['pptp']);
1166
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1167
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1168
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1169
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1170
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1171
	}
1172
}
1173

    
1174

    
1175
function upgrade_048_to_049() {
1176
	global $config;
1177
	/* setup new all users group */
1178
	$all = array();
1179
	$all['name'] = "all";
1180
	$all['description'] = "All Users";
1181
	$all['scope'] = "system";
1182
	$all['gid'] = 1998;
1183
	$all['member'] = array();
1184

    
1185
	if (!is_array($config['system']['group']))
1186
		$config['system']['group'] = array();
1187

    
1188
	/* work around broken uid assignments */
1189
	$config['system']['nextuid'] = 2000;
1190
	foreach ($config['system']['user'] as & $user) {
1191
		if (isset($user['uid']) && !$user['uid'])
1192
			continue;
1193
		$user['uid'] = $config['system']['nextuid']++;
1194
	}
1195

    
1196
	/* work around broken gid assignments */
1197
	$config['system']['nextgid'] = 2000;
1198
	foreach ($config['system']['group'] as & $group) {
1199
		if ($group['name'] == $g['admin_group'])
1200
			$group['gid'] = 1999;
1201
		else
1202
			$group['gid'] = $config['system']['nextgid']++;
1203
	}
1204

    
1205
	/* build group membership information */
1206
	foreach ($config['system']['group'] as & $group) {
1207
		$group['member'] = array();
1208
		foreach ($config['system']['user'] as & $user) {
1209
			$groupnames = explode(",", $user['groupname']);
1210
			if (in_array($group['name'],$groupnames))
1211
				$group['member'][] = $user['uid'];
1212
		}
1213
	}
1214

    
1215
	/* reset user group information */
1216
	foreach ($config['system']['user'] as & $user) {
1217
		unset($user['groupname']);
1218
		$all['member'][] = $user['uid'];
1219
	}
1220

    
1221
	/* reset group scope information */
1222
	foreach ($config['system']['group'] as & $group)
1223
		if ($group['name'] != $g['admin_group'])
1224
		$group['scope'] = "user";
1225

    
1226
	/* insert new all group */
1227
	$groups = Array();
1228
	$groups[] = $all;
1229
	$groups = array_merge($config['system']['group'],$groups);
1230
	$config['system']['group'] = $groups;
1231
}
1232

    
1233

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

    
1256
	/* update group privileges */
1257
	foreach ($config['system']['group'] as & $group) {
1258
		$privs = array();
1259
		if (!is_array($group['pages'])) {
1260
			unset($group['pages']);
1261
			continue;
1262
		}
1263
		foreach ($group['pages'] as $page) {
1264
			$priv = map_page_privname($page);
1265
			if ($priv)
1266
				$privs[] = $priv;
1267
		}
1268
		unset($group['pages']);
1269
		$group['priv'] = $privs;
1270
	}
1271

    
1272
	/* sync all local account information */
1273
	local_sync_accounts();
1274
}
1275

    
1276

    
1277
function upgrade_050_to_051() {
1278
	global $config;
1279
	$pconfig = array();
1280
	$pconfig['desc'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1281
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1282
	$pconfig['value'] = "1";
1283
	$config['sysctl']['item'][] = $pconfig;
1284
	$pconfig = array();
1285
	$pconfig['desc'] = "Set to 1 to enable filtering on the bridge interface";
1286
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1287
	$pconfig['value'] = "0";
1288
	$config['sysctl']['item'][] = $pconfig;
1289

    
1290
	unset($config['bridge']);
1291

    
1292
	$convert_bridges = false;
1293
	foreach($config['interfaces'] as $intf) {
1294
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1295
			$config['bridges'] = array();
1296
			$config['bridges']['bridged'] = array();
1297
			$convert_bridges = true;
1298
			break;
1299
		}
1300
	}
1301
	if ($convert_bridges == true) {
1302
		$i = 0;
1303
		foreach ($config['interfaces'] as $ifr => &$intf) {
1304
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1305
				$nbridge = array();
1306
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1307
				$nbridge['descr'] = "Converted bridged {$ifr}";
1308
				$nbridge['bridgeif'] = "bridge{$i}";
1309
				$config['bridges']['bridged'][] = $nbridge;
1310
				unset($intf['bridge']);
1311
				$i++;
1312
			}
1313
		}
1314
	}
1315
}
1316

    
1317

    
1318
function upgrade_051_to_052() {
1319
	global $config;
1320
	$config['openvpn'] = array();
1321
	if (!is_array($config['system']['ca']))
1322
		$config['system']['ca'] = array();
1323
	if (!is_array($config['system']['cert']))
1324
		$config['system']['cert'] = array();
1325

    
1326
	$vpnid = 1;
1327

    
1328
	/* openvpn server configurations */
1329
	if (is_array($config['installedpackages']['openvpnserver'])) {
1330
		$config['openvpn']['openvpn-server'] = array();
1331

    
1332
		$index = 1;
1333
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1334

    
1335
			if (!is_array($server))
1336
				continue;
1337

    
1338
			if ($server['auth_method'] == "pki") {
1339

    
1340
				/* create ca entry */
1341
				$ca = array();
1342
				$ca['refid'] = uniqid();
1343
				$ca['name'] = "OpenVPN Server CA #{$index}";
1344
				$ca['crt'] = $server['ca_cert'];
1345
				$ca['crl'] = $server['crl'];
1346
				$config['system']['ca'][] = $ca;
1347

    
1348
				/* create ca reference */
1349
				unset($server['ca_cert']);
1350
				unset($server['crl']);
1351
				$server['caref'] = $ca['refid'];
1352

    
1353
				/* create cert entry */
1354
				$cert = array();
1355
				$cert['refid'] = uniqid();
1356
				$cert['name'] = "OpenVPN Server Certificate #{$index}";
1357
				$cert['crt'] = $server['server_cert'];
1358
				$cert['prv'] = $server['server_key'];
1359
				$config['system']['cert'][] = $cert;
1360

    
1361
				/* create cert reference */
1362
				unset($server['server_cert']);
1363
				unset($server['server_key']);
1364
				$server['certref'] = $cert['refid'];
1365

    
1366
				$index++;
1367
			}
1368

    
1369
			/* determine operational mode */
1370
			if ($server['auth_method'] == 'pki') {
1371
				if($server['nopool']) {
1372
					$server['mode'] = "p2p_tls";
1373
				} else {
1374
					$server['mode'] = "server_tls";
1375
				}
1376
			} else {
1377
				$server['mode'] = "p2p_shared_key";
1378
			}
1379
			unset($server['auth_method']);
1380

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

    
1417
			if (!empty($server['disable']))
1418
				$server['disable'] = true;
1419
			else
1420
				unset($server['disable']);
1421

    
1422
			/* allocate vpnid */
1423
			$server['vpnid'] = $vpnid++;
1424

    
1425
			$config['openvpn']['openvpn-server'][] = $server;
1426
		}
1427
		unset($config['installedpackages']['openvpnserver']);
1428
	}
1429

    
1430
	/* openvpn client configurations */
1431
	if (is_array($config['installedpackages']['openvpnclient'])) {
1432
		$config['openvpn']['openvpn-client'] = array();
1433

    
1434
		$index = 1;
1435
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1436

    
1437
			if (!is_array($client))
1438
				continue;
1439

    
1440
			if ($client['auth_method'] == "pki") {
1441

    
1442
				/* create ca entry */
1443
				$ca = array();
1444
				$ca['refid'] = uniqid();
1445
				$ca['name'] = "OpenVPN Client CA #{$index}";
1446
				$ca['crt'] = $client['ca_cert'];
1447
				$ca['crl'] = $client['crl'];
1448
				$config['system']['ca'][] = $ca;
1449

    
1450
				/* create ca reference */
1451
				unset($client['ca_cert']);
1452
				unset($client['crl']);
1453
				$client['caref'] = $ca['refid'];
1454

    
1455
				/* create cert entry */
1456
				$cert = array();
1457
				$cert['refid'] = uniqid();
1458
				$cert['name'] = "OpenVPN Client Certificate #{$index}";
1459
				$cert['crt'] = $client['client_cert'];
1460
				$cert['prv'] = $client['client_key'];
1461
				$config['system']['cert'][] = $cert;
1462

    
1463
				/* create cert reference */
1464
				unset($client['client_cert']);
1465
				unset($client['client_key']);
1466
				$client['certref'] = $cert['refid'];
1467

    
1468
				$index++;
1469
			}
1470

    
1471
			/* determine operational mode */
1472
			if ($client['auth_method'] == 'pki')
1473
				$client['mode'] = "p2p_tls";
1474
			else
1475
				$client['mode'] = "p2p_shared_key";
1476
			unset($client['auth_method']);
1477

    
1478
			/* modify configuration values */
1479
			if (!$client['interface'])
1480
				$client['interface'] = 'wan';
1481
			$client['tunnel_network'] = $client['interface_ip'];
1482
			unset($client['interface_ip']);
1483
			$client['server_addr'] = $client['serveraddr'];
1484
			unset($client['serveraddr']);
1485
			$client['server_port'] = $client['serverport'];
1486
			unset($client['serverport']);
1487
			$client['proxy_addr'] = $client['poxy_hostname'];
1488
			unset($client['proxy_addr']);
1489
			if (isset($client['use_lzo'])) {
1490
				$client['compression'] = "on";
1491
				unset($client['use_lzo']);
1492
			}
1493
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1494
			unset($client['infiniteresolvretry']);
1495

    
1496
			/* allocate vpnid */
1497
			$client['vpnid'] = $vpnid++;
1498

    
1499
			if (!empty($client['disable']))
1500
				$client['disable'] = true;
1501
			else
1502
				unset($client['disable']);
1503

    
1504
			$config['openvpn']['openvpn-client'][] = $client;
1505
		}
1506

    
1507
		unset($config['installedpackages']['openvpnclient']);
1508
	}
1509

    
1510
	/* openvpn client specific configurations */
1511
	if (is_array($config['installedpackages']['openvpncsc'])) {
1512
		$config['openvpn']['openvpn-csc'] = array();
1513

    
1514
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1515

    
1516
			if (!is_array($csc))
1517
				continue;
1518

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

    
1544
			if (!empty($csc['disable']))
1545
				$csc['disable'] = true;
1546
			else
1547
				unset($csc['disable']);
1548

    
1549
			$config['openvpn']['openvpn-csc'][] = $csc;
1550
		}
1551

    
1552
		unset($config['installedpackages']['openvpncsc']);
1553
	}
1554

    
1555
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1556
		count($config['openvpn']['openvpn-client']) > 0) {
1557
		$ovpnrule = array();
1558
                $ovpnrule['type'] = "pass";
1559
                $ovpnrule['interface'] = "openvpn";
1560
                $ovpnrule['statetype'] = "keep state";
1561
                $ovpnrule['source'] = array();
1562
                $ovpnrule['destination'] = array();
1563
                $ovpnrule['source']['any'] = true;
1564
                $ovpnrule['destination']['any'] = true;
1565
                $ovpnrule['descr'] = "Auto added OpenVPN rule from config upgrade.";
1566
		$config['filter']['rule'][] = $ovpnrule;
1567
	}
1568

    
1569
	/*
1570
		* FIXME: hack to keep things working with no installedpackages
1571
		* or carp array in the configuration data.
1572
		*/
1573
	if (!is_array($config['installedpackages']))
1574
		$config['installedpackages'] = array();
1575
	if (!is_array($config['installedpackages']['carp']))
1576
		$config['installedpackages']['carp'] = array();
1577

    
1578
}
1579

    
1580

    
1581
function upgrade_052_to_053() {
1582
	global $config;
1583
	if (!is_array($config['system']['ca']))
1584
		$config['system']['ca'] = array();
1585
	if (!is_array($config['system']['cert']))
1586
		$config['system']['cert'] = array();
1587

    
1588
	/* migrate advanced admin page webui ssl to certifcate mngr */
1589
	if ($config['system']['webgui']['certificate'] &&
1590
	$config['system']['webgui']['private-key']) {
1591

    
1592
		/* create cert entry */
1593
		$cert = array();
1594
		$cert['refid'] = uniqid();
1595
		$cert['name'] = "webConfigurator SSL Certificate";
1596
		$cert['crt'] = $config['system']['webgui']['certificate'];
1597
		$cert['prv'] = $config['system']['webgui']['private-key'];
1598
		$config['system']['cert'][] = $cert;
1599

    
1600
		/* create cert reference */
1601
		unset($config['system']['webgui']['certificate']);
1602
		unset($config['system']['webgui']['private-key']);
1603
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1604
	}
1605

    
1606
	/* migrate advanced admin page ssh keys to user manager */
1607
	if ($config['system']['ssh']['authorizedkeys']) {
1608
		$admin_user =& getUserEntryByUID(0);
1609
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1610
		unset($config['system']['ssh']['authorizedkeys']);
1611
	}
1612
}
1613

    
1614

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

    
1673

    
1674
function upgrade_054_to_055() {
1675
	global $config;
1676
	global $g;
1677

    
1678
	/* RRD files changed for quality, traffic and packets graphs */
1679
	//ini_set("max_execution_time", "1800");
1680
	/* convert traffic RRD file */
1681
	global $parsedcfg, $listtags;
1682
	$listtags = array("ds", "v", "rra", "row");
1683

    
1684
	$rrddbpath = "/var/db/rrd/";
1685
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1686

    
1687
	$rrdinterval = 60;
1688
	$valid = $rrdinterval * 2;
1689

    
1690
	/* Asume GigE for now */
1691
	$downstream = 125000000;
1692
	$upstream = 125000000;
1693

    
1694
	/* build a list of quality databases */
1695
	/* roundtrip has become delay */
1696
	function divide_delay($delayval) {
1697
		$delayval = floatval($delayval);
1698
		$delayval = ($delayval / 1000);
1699
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1700
		return $delayval;
1701
	}
1702
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1703
	$databases = array();
1704
	exec("cd $rrddbpath;/usr/bin/find *-quality.rrd", $databases);
1705
	echo "done.\n";
1706
	rsort($databases);
1707
	foreach($databases as $database) {
1708
		$xmldump = "{$database}.old.xml";
1709
		$xmldumpnew = "{$database}.new.xml";
1710

    
1711
		echo "Migrate RRD database {$database} to new format \n";
1712
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
1713

    
1714
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
1715
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1716
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1717
		$rrdold = $rrdold['rrd'];
1718

    
1719
		$i = 0;
1720
		foreach($rrdold['rra'] as $rra) {
1721
			$l = 0;
1722
			foreach($rra['database']['row'] as $row) {
1723
				$vnew = divide_delay($row['v'][1]);
1724
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1725
				$l++;
1726
			}
1727
			$i++;
1728
		}
1729

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

    
1734
	}
1735
	/* let apinger recreate required files */
1736
	setup_gateways_monitor();
1737

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

    
1748
		echo "Migrate RRD database {$database} to new format \n";
1749
		/* rename DS source */
1750
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1751
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1752

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

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

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

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

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

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

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

    
1793
	}
1794
	enable_rrd_graphing();
1795
	echo "Updating configuration...";
1796
}
1797

    
1798

    
1799
function upgrade_055_to_056() {
1800
	global $config;
1801

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

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

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

    
1845

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

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

    
1877
function upgrade_058_to_059() {
1878
	global $config;
1879

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

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

    
1916
function upgrade_060_to_061() {
1917
	global $config;
1918

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

    
1925
function upgrade_061_to_062() {
1926
	global $config;
1927

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

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

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

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

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

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

    
1960
		unset($natent);
1961
	}
1962
}
1963

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

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

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

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

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

    
2067
?>
(40-40/50)