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
		$config['gateways']['gateway_item'][$i] = array();
771
		if(is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
772
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
773
			$config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Static Gateway";			
774
		} else {
775
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
776
			$config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Dynamic Gateway";			
777
		}
778
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
779
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
780
		/* add default gateway bit for wan on upgrade */
781
		if($ifname == "wan") {
782
			 $config['gateways']['gateway_item'][$i]['defaultgw'] = true;
783
		}
784
		if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
785
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
786
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
787
		}
788
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
789

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

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

    
812

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

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

    
845

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

    
864

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

    
902

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

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

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

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

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

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

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

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

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

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

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

    
939
				/* build new phase1 entry */
940

    
941
				$ph1ent = array();
942

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

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

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

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

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

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

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

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

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

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

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

    
1023
			/* build new phase2 entry */
1024

    
1025
			$ph2ent = array();
1026

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1106

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

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

    
1168

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

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

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

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

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

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

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

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

    
1227

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

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

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

    
1270

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

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

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

    
1311

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

    
1320
	$vpnid = 1;
1321

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

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

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

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

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

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

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

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

    
1360
				$index++;
1361
			}
1362

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1490
				$index++;
1491
			}
1492

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1628
}
1629

    
1630

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

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

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

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

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

    
1664

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

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

    
1725

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1853

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

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

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

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

    
1900

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2230
?>
(41-41/54)