Project

General

Profile

Download (67.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
		/* Ensure that we follow what this new "admin" username should be in the session. */
626
		$_SESSION["Username"] = "{$config['system']['username']}";
627

    
628
		$config['system']['user'][0]['priv'] = array();
629
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
630
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
631
		$config['system']['user'][0]['priv'][0]['descr'] = "Indicates whether this user will lock access to the webConfigurator for other users.";
632
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
633
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
634
		$config['system']['user'][0]['priv'][1]['descr'] = "Indicates whether this user will lock individual HTML pages after having accessed a particular page (the lock will be freed if the user leaves or saves the page form).";
635
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
636
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
637
		$config['system']['user'][0]['priv'][2]['descr'] = "Indicates whether this user is able to login for example via SSH.";
638
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
639
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
640
		$config['system']['user'][0]['priv'][3]['descr'] = "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).";
641
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
642
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
643
		$config['system']['user'][0]['priv'][4]['descr'] = "This user is associated with the UNIX root user (you should associate this privilege only with one single user).";
644

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
747
	}
748
}
749

    
750

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

    
759

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

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

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

    
811

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

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

    
844

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

    
863

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

    
901

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

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

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

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

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

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

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

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

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

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

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

    
938
				/* build new phase1 entry */
939

    
940
				$ph1ent = array();
941

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

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

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

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

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

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

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

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

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

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

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

    
1022
			/* build new phase2 entry */
1023

    
1024
			$ph2ent = array();
1025

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

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

    
1031
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1032

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

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

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

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

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

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

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

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

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

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

    
1105

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

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

    
1167

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

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

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

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

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

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

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

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

    
1226

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

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

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

    
1269

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

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

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

    
1310

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

    
1319
	$vpnid = 1;
1320

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

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

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

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

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

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

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

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

    
1359
				$index++;
1360
			}
1361

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

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

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

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

    
1418
			if (!empty($server['custom_options'])) {
1419
				$cstmopts = array();
1420
				$tmpcstmopts = explode(";", $server['custom_options']);
1421
				$assigned = false;
1422
				$tmpstr = "";
1423
				foreach ($tmpcstmopts as $tmpcstmopt) {
1424
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1425
					if (substr($tmpstr,0 ,6) == "devtun") {
1426
						$assigned = true;
1427
						continue;
1428
					} else if (substr($tmpstr, 0, 5) == "local") {
1429
						$localip = substr($tmpstr, 6);
1430
						$server['ipaddr'] = str_replace("\n", "", $localip);
1431
					} else
1432
						$cstmopts[] = $tmpcstmopt;
1433
				}
1434
				$server['custom_options'] = implode(";", $cstmopts);
1435
				if ($assigned == true) {
1436
					$realif = substr($tmpstr, 3);
1437
					foreach ($config['interfaces'] as $iface => $cfgif) {
1438
						if ($cfgif['if'] == $realif) {
1439
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1440
							break;
1441
						}
1442
					}
1443
				}
1444
			}
1445

    
1446
			$config['openvpn']['openvpn-server'][] = $server;
1447
		}
1448
		unset($config['installedpackages']['openvpnserver']);
1449
	}
1450

    
1451
	/* openvpn client configurations */
1452
	if (is_array($config['installedpackages']['openvpnclient'])) {
1453
		$config['openvpn']['openvpn-client'] = array();
1454

    
1455
		$index = 1;
1456
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1457

    
1458
			if (!is_array($client))
1459
				continue;
1460

    
1461
			if ($client['auth_method'] == "pki") {
1462

    
1463
				/* create ca entry */
1464
				$ca = array();
1465
				$ca['refid'] = uniqid();
1466
				$ca['name'] = "OpenVPN Client CA #{$index}";
1467
				$ca['crt'] = $client['ca_cert'];
1468
				$ca['crl'] = $client['crl'];
1469
				$config['ca'][] = $ca;
1470

    
1471
				/* create ca reference */
1472
				unset($client['ca_cert']);
1473
				unset($client['crl']);
1474
				$client['caref'] = $ca['refid'];
1475

    
1476
				/* create cert entry */
1477
				$cert = array();
1478
				$cert['refid'] = uniqid();
1479
				$cert['name'] = "OpenVPN Client Certificate #{$index}";
1480
				$cert['crt'] = $client['client_cert'];
1481
				$cert['prv'] = $client['client_key'];
1482
				$config['cert'][] = $cert;
1483

    
1484
				/* create cert reference */
1485
				unset($client['client_cert']);
1486
				unset($client['client_key']);
1487
				$client['certref'] = $cert['refid'];
1488

    
1489
				$index++;
1490
			}
1491

    
1492
			/* determine operational mode */
1493
			if ($client['auth_method'] == 'pki')
1494
				$client['mode'] = "p2p_tls";
1495
			else
1496
				$client['mode'] = "p2p_shared_key";
1497
			unset($client['auth_method']);
1498

    
1499
			/* modify configuration values */
1500
			if (!$client['interface'])
1501
				$client['interface'] = 'wan';
1502
			$client['tunnel_network'] = $client['interface_ip'];
1503
			unset($client['interface_ip']);
1504
			$client['server_addr'] = $client['serveraddr'];
1505
			unset($client['serveraddr']);
1506
			$client['server_port'] = $client['serverport'];
1507
			unset($client['serverport']);
1508
			$client['proxy_addr'] = $client['poxy_hostname'];
1509
			unset($client['proxy_addr']);
1510
			if (isset($client['use_lzo'])) {
1511
				$client['compression'] = "on";
1512
				unset($client['use_lzo']);
1513
			}
1514
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1515
			unset($client['infiniteresolvretry']);
1516

    
1517
			/* allocate vpnid */
1518
			$client['vpnid'] = $vpnid++;
1519

    
1520
			if (!empty($client['custom_options'])) {
1521
				$cstmopts = array();
1522
				$tmpcstmopts = explode(";", $client['custom_options']);
1523
				$assigned = false;
1524
				$tmpstr = "";
1525
				foreach ($tmpcstmopts as $tmpcstmopt) {
1526
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1527
					if (substr($tmpstr,0 ,6) == "devtun") {
1528
						$assigned = true;
1529
						continue;
1530
					} else if (substr($tmpstr, 0, 5) == "local") {
1531
                                                $localip = substr($tmpstr, 6);
1532
                                                $client['ipaddr'] = str_replace("\n", "", $localip);
1533
					} else
1534
						$cstmopts[] = $tmpcstmopt;
1535
				}
1536
				$client['custom_options'] = implode(";", $cstmopts);
1537
				if ($assigned == true) {
1538
					$realif = substr($tmpstr, 3);
1539
					foreach ($config['interfaces'] as $iface => $cfgif) {
1540
						if ($cfgif['if'] == $realif) {
1541
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1542
							break;
1543
						}
1544
					}
1545
				}
1546
			}
1547

    
1548
			if (!empty($client['disable']))
1549
				$client['disable'] = true;
1550
			else
1551
				unset($client['disable']);
1552

    
1553
			$config['openvpn']['openvpn-client'][] = $client;
1554
		}
1555

    
1556
		unset($config['installedpackages']['openvpnclient']);
1557
	}
1558

    
1559
	/* openvpn client specific configurations */
1560
	if (is_array($config['installedpackages']['openvpncsc'])) {
1561
		$config['openvpn']['openvpn-csc'] = array();
1562

    
1563
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1564

    
1565
			if (!is_array($csc))
1566
				continue;
1567

    
1568
			/* modify configuration values */
1569
			$csc['common_name'] = $csc['commonname'];
1570
			unset($csc['commonname']);
1571
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1572
			unset($csc['ifconfig_push']);
1573
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1574
			unset($csc['dhcp_domainname']);
1575
			$csc['dns_server1'] = $csc['dhcp_dns'];
1576
			unset($csc['dhcp_dns']);
1577
			$csc['ntp_server1'] = $csc['dhcp_ntp'];
1578
			unset($csc['dhcp_ntp']);
1579
			if ($csc['dhcp_nbtdisable'])
1580
				$csc['netbios_enable'] = false;
1581
			else
1582
				$csc['netbios_enable'] = "yes";
1583
			unset($csc['dhcp_nbtdisable']);
1584
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1585
			unset($csc['dhcp_nbttype']);
1586
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1587
			unset($csc['dhcp_nbtscope']);
1588
			$csc['nbdd_server1'] = $csc['dhcp_nbdd'];
1589
			unset($csc['dhcp_nbdd']);
1590
			$csc['wins_server1'] = $csc['dhcp_wins'];
1591
			unset($csc['dhcp_wins']);
1592

    
1593
			if (!empty($csc['disable']))
1594
				$csc['disable'] = true;
1595
			else
1596
				unset($csc['disable']);
1597

    
1598
			$config['openvpn']['openvpn-csc'][] = $csc;
1599
		}
1600

    
1601
		unset($config['installedpackages']['openvpncsc']);
1602
	}
1603

    
1604
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1605
		count($config['openvpn']['openvpn-client']) > 0) {
1606
		$ovpnrule = array();
1607
                $ovpnrule['type'] = "pass";
1608
                $ovpnrule['interface'] = "openvpn";
1609
                $ovpnrule['statetype'] = "keep state";
1610
                $ovpnrule['source'] = array();
1611
                $ovpnrule['destination'] = array();
1612
                $ovpnrule['source']['any'] = true;
1613
                $ovpnrule['destination']['any'] = true;
1614
                $ovpnrule['descr'] = "Auto added OpenVPN rule from config upgrade.";
1615
		$config['filter']['rule'][] = $ovpnrule;
1616
	}
1617

    
1618
	/*
1619
		* FIXME: hack to keep things working with no installedpackages
1620
		* or carp array in the configuration data.
1621
		*/
1622
	if (!is_array($config['installedpackages']))
1623
		$config['installedpackages'] = array();
1624
	if (!is_array($config['installedpackages']['carp']))
1625
		$config['installedpackages']['carp'] = array();
1626

    
1627
}
1628

    
1629

    
1630
function upgrade_052_to_053() {
1631
	global $config;
1632
	if (!is_array($config['ca']))
1633
		$config['ca'] = array();
1634
	if (!is_array($config['cert']))
1635
		$config['cert'] = array();
1636

    
1637
	/* migrate advanced admin page webui ssl to certifcate mngr */
1638
	if ($config['system']['webgui']['certificate'] &&
1639
	$config['system']['webgui']['private-key']) {
1640

    
1641
		/* create cert entry */
1642
		$cert = array();
1643
		$cert['refid'] = uniqid();
1644
		$cert['name'] = "webConfigurator SSL Certificate";
1645
		$cert['crt'] = $config['system']['webgui']['certificate'];
1646
		$cert['prv'] = $config['system']['webgui']['private-key'];
1647
		$config['cert'][] = $cert;
1648

    
1649
		/* create cert reference */
1650
		unset($config['system']['webgui']['certificate']);
1651
		unset($config['system']['webgui']['private-key']);
1652
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1653
	}
1654

    
1655
	/* migrate advanced admin page ssh keys to user manager */
1656
	if ($config['system']['ssh']['authorizedkeys']) {
1657
		$admin_user =& getUserEntryByUID(0);
1658
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1659
		unset($config['system']['ssh']['authorizedkeys']);
1660
	}
1661
}
1662

    
1663

    
1664
function upgrade_053_to_054() {
1665
	global $config;
1666
	if(is_array($config['load_balancer']['lbpool'])) {
1667
		$lbpool_arr = $config['load_balancer']['lbpool'];
1668
		$lbpool_srv_arr = array();
1669
		$gateway_group_arr = array();
1670
		$gateways = return_gateways_array();
1671
		if (! is_array($config['gateways']['gateway_item']))
1672
			$config['gateways']['gateway_item'] = array();
1673

    
1674
		$a_gateways =& $config['gateways']['gateway_item'];
1675
		foreach($lbpool_arr as $lbpool) {
1676
			if($lbpool['type'] == "gateway") {
1677
				$gateway_group['name'] = $lbpool['name'];
1678
				$gateway_group['descr'] = $lbpool['desc'];
1679
				$gateway_group['trigger'] = "down";
1680
				$gateway_group['item'] = array();
1681
				$i = 0;
1682
				foreach($lbpool['servers'] as $member) {
1683
					$split = split("\|", $member);
1684
					$interface = $split[0];
1685
					$monitor = $split[1];
1686
					/* on static upgraded configuration we automatically prepend GW_ */
1687
					$static_name = "GW_" . strtoupper($interface);
1688
					if(is_ipaddr($monitor)) {
1689
						$interface = $static_name;
1690
						$config['interfaces'][$interface]['monitorip'] = $monitor;
1691
					}
1692
					/* on failover increment tier. Else always assign 1 */
1693
					if($lbpool['behaviour'] == "failover") {
1694
						$i++;
1695
					} else {
1696
						$i = 1;
1697
					}
1698
					$gateway_group['item'][] = "$interface|$i";
1699
				}
1700
				$gateway_group_arr[] = $gateway_group;
1701
			} else {
1702
				$lbpool_srv_arr[] = $lbpool;
1703
			}
1704
		}
1705
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1706
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1707
	}
1708
	// Unset lbpool if we no longer have any server pools
1709
	if (count($lbpool_srv_arr) == 0) {
1710
		if(empty($config['load_balancer'])) {
1711
			unset($config['load_balancer']);
1712
		} else {
1713
			unset($config['load_balancer']['lbpool']);
1714
		}
1715
	} else {
1716
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1717
	}
1718
	// Only set the gateway group array if we converted any
1719
	if (count($gateway_group_arr) != 0) {
1720
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1721
	}
1722
}
1723

    
1724

    
1725
function upgrade_054_to_055() {
1726
	global $config;
1727
	global $g;
1728

    
1729
	/* RRD files changed for quality, traffic and packets graphs */
1730
	//ini_set("max_execution_time", "1800");
1731
	/* convert traffic RRD file */
1732
	global $parsedcfg, $listtags;
1733
	$listtags = array("ds", "v", "rra", "row");
1734

    
1735
	$rrddbpath = "/var/db/rrd/";
1736
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1737

    
1738
	$rrdinterval = 60;
1739
	$valid = $rrdinterval * 2;
1740

    
1741
	/* Asume GigE for now */
1742
	$downstream = 125000000;
1743
	$upstream = 125000000;
1744

    
1745
	/* build a list of quality databases */
1746
	/* roundtrip has become delay */
1747
	function divide_delay($delayval) {
1748
		$delayval = floatval($delayval);
1749
		$delayval = ($delayval / 1000);
1750
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1751
		return $delayval;
1752
	}
1753
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1754
	$databases = array();
1755
	chdir($rrddbpath);
1756
	$databases = glob("*-quality.rrd");
1757
	rsort($databases);
1758
	foreach($databases as $database) {
1759
		$xmldump = "{$database}.old.xml";
1760
		$xmldumpnew = "{$database}.new.xml";
1761

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

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

    
1771
		$i = 0;
1772
		foreach($rrdold['rra'] as $rra) {
1773
			$l = 0;
1774
			foreach($rra['database']['row'] as $row) {
1775
				$vnew = divide_delay($row['v'][1]);
1776
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1777
				$l++;
1778
			}
1779
			$i++;
1780
		}
1781

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

    
1786
	}
1787
	/* let apinger recreate required files */
1788
	setup_gateways_monitor();
1789

    
1790
	/* build a list of traffic and packets databases */
1791
	$databases = array();
1792
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
1793
	rsort($databases);
1794
	foreach($databases as $database) {
1795
		$databasetmp = "{$database}.tmp";
1796
		$xmldump = "{$database}.old.xml";
1797
		$xmldumptmp = "{$database}.tmp.xml";
1798
		$xmldumpnew = "{$database}.new.xml";
1799

    
1800
		if ($g['booting'])
1801
			echo "Migrate RRD database {$database} to new format \n";
1802
		/* rename DS source */
1803
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1804
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1805

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

    
1809
		/* create new rrd database file */
1810
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
1811
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
1812
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
1813
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
1814
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
1815
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
1816
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
1817
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
1818
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
1819

    
1820
		create_new_rrd("$rrdcreate");
1821
		/* create temporary xml from new RRD */
1822
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
1823

    
1824
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1825
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1826
		$rrdold = $rrdold['rrd'];
1827

    
1828
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
1829
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
1830
		$rrdnew = $rrdnew['rrd'];
1831

    
1832
		/* remove any MAX RRA's. Not needed for traffic. */
1833
		$i = 0;
1834
		foreach ($rrdold['rra'] as $rra) {
1835
			if(trim($rra['cf']) == "MAX") {
1836
				unset($rrdold['rra'][$i]);
1837
			}
1838
			$i++;
1839
		}
1840

    
1841
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
1842
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
1843
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1844
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1845

    
1846
	}
1847
	enable_rrd_graphing();
1848
	if ($g['booting'])
1849
		echo "Updating configuration...";
1850
}
1851

    
1852

    
1853
function upgrade_055_to_056() {
1854
	global $config;
1855

    
1856
	if (!is_array($config['ca']))
1857
		$config['ca'] = array();
1858
	if (!is_array($config['cert']))
1859
		$config['cert'] = array();
1860

    
1861
	/* migrate ipsec ca's to cert manager */
1862
	if (is_array($config['ipsec']['cacert'])) {
1863
		foreach($config['ipsec']['cacert'] as & $cacert) {
1864
			$ca = array();
1865
			$ca['refid'] = uniqid();
1866
			if (is_array($cacert['cert']))
1867
				$ca['crt'] = $cacert['cert'][0];
1868
			else
1869
				$ca['crt'] = $cacert['cert'];
1870
			$ca['name'] = $cacert['ident'];
1871
			$config['ca'][] = $ca;
1872
		}
1873
		unset($config['ipsec']['cacert']);
1874
	}
1875

    
1876
	/* migrate phase1 certificates to cert manager */
1877
	if (is_array($config['ipsec']['phase1'])) {
1878
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
1879
			$cert = array();
1880
			$cert['refid'] = uniqid();
1881
			$cert['name'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
1882
			if (is_array($ph1ent['cert']))
1883
				$cert['crt'] = $ph1ent['cert'][0];
1884
			else
1885
				$cert['crt'] = $ph1ent['cert'];
1886
			$cert['prv'] = $ph1ent['private-key'];
1887
			$config['cert'][] = $cert;
1888
			$ph1ent['certref'] = $cert['refid'];
1889
			if ($ph1ent['cert'])
1890
				unset($ph1ent['cert']);
1891
			if ($ph1ent['private-key'])
1892
				unset($ph1ent['private-key']);
1893
			if ($ph1ent['peercert'])
1894
				unset($ph1ent['peercert']);
1895
		}
1896
	}
1897
}
1898

    
1899

    
1900
function upgrade_056_to_057() {
1901
	global $config;
1902
	/* migrate captivate portal to user manager */
1903
	if (is_array($config['captiveportal']['user'])) {
1904
		foreach($config['captiveportal']['user'] as $user) {
1905
			// avoid user conflicts
1906
			if ($config['system']['user'][$user['name']])
1907
				continue;
1908
			$user['scope'] = "user";
1909
			if (isset($user['expirationdate'])) {
1910
				$user['expires'] = $user['expirationdate'];
1911
				unset($user['expirationdate']);
1912
			}
1913
			if (isset($user['password'])) {
1914
				$user['md5-hash'] = $user['password'];
1915
				unset($user['password']);
1916
			}
1917
			$config['system']['user'][] = $user;
1918
		}
1919
		unset($config['captiveportal']['user']);
1920
	}
1921
}
1922

    
1923
function upgrade_057_to_058() {
1924
	global $config;
1925
	/* set all phase2 entries to tunnel mode */
1926
	if (is_array($config['ipsec']['phase2']))
1927
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1928
			$ph2ent['mode'] = 'tunnel';
1929
}
1930

    
1931
function upgrade_058_to_059() {
1932
	global $config;
1933

    
1934
	if (is_array($config['schedules']['schedule'])) {
1935
		foreach ($config['schedules']['schedule'] as & $schedl)
1936
			$schedl['schedlabel'] = uniqid();
1937
	}
1938
}
1939

    
1940
function upgrade_059_to_060() {
1941
	global $config;
1942
	require_once("/etc/inc/certs.inc");
1943
	if (is_array($config['ca'])) {
1944
		/* Locate issuer for all CAs */
1945
		foreach ($config['ca'] as & $ca) {
1946
			$subject = cert_get_subject($ca['crt']);
1947
			$issuer = cert_get_issuer($ca['crt']);
1948
			if($issuer <> $subject) {
1949
				$issuer_crt =& lookup_ca_by_subject($issuer);
1950
				if($issuer_crt)
1951
					$ca['caref'] = $issuer_crt['refid'];
1952
			}
1953
		}
1954
		
1955
		/* Locate issuer for all certificates */
1956
		if (is_array($config['cert'])) {
1957
			foreach ($config['cert'] as & $cert) {
1958
				$subject = cert_get_subject($cert['crt']);
1959
				$issuer = cert_get_issuer($cert['crt']);
1960
				if($issuer <> $subject) {
1961
					$issuer_crt =& lookup_ca_by_subject($issuer);
1962
					if($issuer_crt)
1963
						$cert['caref'] = $issuer_crt['refid'];
1964
				}
1965
			}
1966
		}
1967
	}
1968
}
1969

    
1970
function upgrade_060_to_061() {
1971
	global $config;
1972

    
1973
	if (is_array($config['interfaces']['wan']))
1974
		$config['interfaces']['wan']['enable'] = true;
1975
	if (is_array($config['interfaces']['lan']))
1976
		$config['interfaces']['lan']['enable'] = true;
1977
}
1978

    
1979
function upgrade_061_to_062() {
1980
	global $config;
1981

    
1982
	/* Convert NAT port forwarding rules */
1983
	if (is_array($config['nat']['rule'])) {
1984
		$a_nat = &$config['nat']['rule'];
1985

    
1986
		foreach ($a_nat as &$natent) {
1987
			$natent['disabled'] = false;
1988
			$natent['nordr']    = false;
1989

    
1990
			$natent['source'] = array(
1991
				"not"     => false,
1992
				"any"     => true,
1993
				"port"    => ""
1994
			);
1995

    
1996
			$natent['destination'] = array(
1997
				"not"     => false,
1998
				"address" => $natent['external-address'],
1999
				"port"    => $natent['external-port']
2000
			);
2001

    
2002
			if (empty($natent['destination']['address'])) {
2003
				unset($natent['destination']['address']);
2004
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2005
			} else if ($natent['destination']['address'] == 'any') {
2006
				unset($natent['destination']['address']);
2007
				$natent['destination']['any'] = true;
2008
			}
2009

    
2010
			unset($natent['external-address']);
2011
			unset($natent['external-port']);
2012
		}
2013

    
2014
		unset($natent);
2015
	}
2016
}
2017

    
2018
function upgrade_062_to_063() {
2019
	/* Upgrade legacy Themes to the new pfsense_ng */
2020
	global $config;
2021

    
2022
	switch($config['theme']) {
2023
		case "nervecenter":
2024
			$config['theme'] = "pfsense_ng";
2025
			break;
2026
	}
2027
	
2028
}
2029

    
2030
function upgrade_063_to_064() {
2031
	global $config;
2032
	$j=0;
2033
	$ifcfg = &$config['interfaces'];
2034
	
2035
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {	
2036
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2037
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2038
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2039
			$j++;
2040
			if (isset($ppp['port'])){
2041
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2042
				unset($config['ppps']['ppp'][$pppid]['port']);
2043
			}
2044
			if (!isset($ppp['type'])){
2045
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2046
			}
2047
			if (isset($ppp['defaultgw']))
2048
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2049
		}
2050
	}
2051
	
2052
	if (!is_array($config['ppps']['ppp']))
2053
		$config['ppps']['ppp'] = array();
2054
	$a_ppps = &$config['ppps']['ppp'];
2055

    
2056
	foreach ($ifcfg as $ifname => $ifinfo) {
2057
		$ppp = array();
2058
		// For pppoe conversion
2059
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2060
			if (isset($ifinfo['ptpid']))
2061
				continue;
2062
			$ppp['ptpid'] =  $j;
2063
			$ppp['type'] = $ifinfo['ipaddr'];
2064
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2065
			$ppp['ports'] = $ifinfo['if'];
2066
			if ($ifinfo['ipaddr'] == "pppoe"){
2067
				$ppp['username'] = $ifinfo['pppoe_username'];
2068
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2069
			}
2070
			if ($ifinfo['ipaddr'] == "pptp"){
2071
				$ppp['username'] = $ifinfo['pptp_username'];
2072
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2073
			}
2074
			
2075
			if (isset($ifinfo['provider']))
2076
				$ppp['provider'] = $ifinfo['provider'];
2077
			if (isset($ifinfo['ondemand']))
2078
				$ppp['ondemand'] = true;
2079
			if (isset($ifinfo['timeout']))
2080
				$ppp['idletimeout'] = $ifinfo['timeout'];
2081
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2082
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2083
				if (is_array($config['cron']['item'])) {
2084
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2085
						$item = $config['cron']['item'][$i];
2086
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2087
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2088
					}
2089
				}
2090
			}
2091
			if (isset($ifinfo['local']))
2092
				$ppp['localip'] = $ifinfo['local'];
2093
			if (isset($ifinfo['subnet']))
2094
				$ppp['subnet'] = $ifinfo['subnet'];
2095
			if (isset($ifinfo['remote']))
2096
				$ppp['gateway'] = $ifinfo['remote'];
2097

    
2098
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2099
			$j++;
2100
			
2101
			unset($ifcfg[$ifname]['pppoe_username']);
2102
			unset($ifcfg[$ifname]['pppoe_password']);
2103
			unset($ifcfg[$ifname]['provider']);
2104
			unset($ifcfg[$ifname]['ondemand']);
2105
			unset($ifcfg[$ifname]['timeout']);
2106
			unset($ifcfg[$ifname]['pppoe_reset']);
2107
			unset($ifcfg[$ifname]['pppoe_preset']);
2108
			unset($ifcfg[$ifname]['pppoe']);
2109
			unset($ifcfg[$ifname]['pptp_username']);
2110
			unset($ifcfg[$ifname]['pptp_password']);
2111
			unset($ifcfg[$ifname]['local']);
2112
			unset($ifcfg[$ifname]['subnet']);
2113
			unset($ifcfg[$ifname]['remote']);
2114
			
2115
			$a_ppps[] = $ppp;
2116
			
2117
		}
2118
	}
2119
}
2120

    
2121
function upgrade_064_to_065() {
2122
	/* Disable TSO and LRO in upgraded configs */
2123
	global $config;
2124
	$config['system']['disablesegmentationoffloading'] = true;
2125
	$config['system']['disablelargereceiveoffloading'] = true;
2126
}
2127

    
2128
function upgrade_065_to_066() {
2129
	global $config;
2130

    
2131
	$dhcrelaycfg =& $config['dhcrelay'];
2132

    
2133
        if (is_array($dhcrelaycfg)) {
2134
        	$dhcrelayifs = array();
2135
		$foundifs = false;
2136
        	/* DHCPRelay enabled on any interfaces? */
2137
                foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2138
                        if (isset($dhcrelayifconf['enable'])) {
2139
				$dhcrelayifs[] = $dhcrelayif;
2140
				unset($dhcrelaycfg['dhcrelayif']);
2141
				$foundifs = true;
2142
			}
2143
                }
2144
		if ($foundifs == true)
2145
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2146
        }
2147
}
2148

    
2149
function upgrade_066_to_067() {
2150
	global $config;
2151
	if (isset($config['system']['ca'])) {
2152
		$config['ca'] = $config['system']['ca'];
2153
	}
2154
	if (isset($config['system']['cert'])) {
2155
		$config['cert'] = $config['system']['cert'];
2156
	}
2157
}
2158

    
2159
function upgrade_067_to_068() {
2160
	global $config;
2161

    
2162
	if (!empty($config['pppoe'])) {
2163
		$config['pppoes'] = array();
2164
		$config['pppoes']['pppoe'] = array();
2165
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2166

    
2167
		if (is_array($config['pppoe']['user'])) {
2168
			$username = array(); 
2169
			foreach ($config['pppoe']['user'] as $user) {
2170
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2171
				if ($user['ip'])
2172
					$usr .= ":{$user['ip']}";
2173
				$username[] = $usr;
2174
			}
2175
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2176
		}
2177
		unset($config['pppoe']);
2178
	}
2179
}
2180

    
2181
function upgrade_068_to_069() {
2182
	global $config;
2183
	if (!is_array($config['system']['user']))
2184
		return;
2185
	foreach ($config['system']['user'] as & $user) {
2186
		if (!is_array($user['cert']))
2187
			continue;
2188
		$rids = array();
2189
		foreach ($user['cert'] as $id => $cert) {
2190
			if (!isset($cert['name']))
2191
				continue;
2192
			$tcert = $cert;
2193
			// Make sure each cert gets a refid
2194
			if (!isset($tcert['refid']))
2195
				$tcert['refid'] = uniqid();
2196
			// Keep the cert references for this user
2197
			$rids[] = $tcert['refid'];
2198
			$config['cert'][] = $tcert;
2199
		}
2200
		// Replace user certs with cert references instead.
2201
		if (count($rids) > 0)
2202
			$user['cert'] = $rids;
2203
	}
2204
}
2205

    
2206
function upgrade_069_to_070() {
2207
	global $config;
2208

    
2209
	/* Convert NAT 1:1 rules */
2210
	if (is_array($config['nat']['onetoone'])) {
2211
		$a_nat = &$config['nat']['onetoone'];
2212

    
2213
		foreach ($a_nat as &$natent) {
2214
			if ($natent['subnet'] == 32)
2215
				$natent['source'] = array("address" => $natent['internal']);
2216
			else
2217
				$natent['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2218

    
2219
			$natent['destination'] = array("any" => true);
2220

    
2221
			unset($natent['internal']);
2222
			unset($natent['subnet']);
2223
		}
2224

    
2225
		unset($natent);
2226
	}
2227
}
2228

    
2229
?>
(41-41/54)