Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
192

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

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

    
204

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

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

    
223

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

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

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

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

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

    
250

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

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

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

    
271

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

    
286

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

    
299

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

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

    
353

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

    
359

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

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

    
383

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

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

    
398

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

    
403

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

    
410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
491

    
492
function upgrade_026_to_027() {
493
	global $config;
494
}
495

    
496

    
497
function upgrade_027_to_028() {
498
	global $config;
499
}
500

    
501

    
502
function upgrade_028_to_029() {
503
	global $config;
504
	$rule_item = array();
505
	$a_filter = &$config['filter']['rule'];
506
	$rule_item['interface'] = "enc0";
507
	$rule_item['type'] = "pass";
508
	$rule_item['source']['any'] = true;
509
	$rule_item['destination']['any'] = true;
510
	$rule_item['descr'] = "Permit IPsec traffic.";
511
	$rule_item['statetype'] = "keep state";
512
	$a_filter[] = $rule_item;
513
}
514

    
515

    
516
function upgrade_029_to_030() {
517
	global $config;
518
	/* enable the rrd config setting by default */
519
	$config['rrd']['enable'] = true;
520
}
521

    
522

    
523
function upgrade_030_to_031() {
524
	global $config;
525
	/* Insert upgrade code here */
526
}
527

    
528

    
529
function upgrade_031_to_032() {
530
	global $config;
531
	/* Insert upgrade code here */
532
}
533

    
534

    
535
function upgrade_032_to_033() {
536
	global $config;
537
	/* Insert upgrade code here */
538
}
539

    
540

    
541
function upgrade_033_to_034() {
542
	global $config;
543
	/* Insert upgrade code here */
544
}
545

    
546

    
547
function upgrade_034_to_035() {
548
	global $config;
549
	/* Insert upgrade code here */
550
}
551

    
552

    
553
function upgrade_035_to_036() {
554
	global $config;
555
	/* Insert upgrade code here */
556
}
557

    
558

    
559
function upgrade_036_to_037() {
560
	global $config;
561
	/* Insert upgrade code here */
562
}
563

    
564

    
565
function upgrade_037_to_038() {
566
	global $config;
567
	/* Insert upgrade code here */
568
}
569

    
570

    
571
function upgrade_038_to_039() {
572
	global $config;
573
	/* Insert upgrade code here */
574
}
575

    
576

    
577
function upgrade_039_to_040() {
578
	global $config;
579
	$config['system']['webgui']['auth_method'] = "session";
580
	$config['system']['webgui']['backing_method'] = "htpasswd";
581

    
582
	if (isset ($config['system']['username'])) {
583
		$config['system']['group'] = array();
584
		$config['system']['group'][0]['name'] = "admins";
585
		$config['system']['group'][0]['description'] = "System Administrators";
586
		$config['system']['group'][0]['scope'] = "system";
587
		$config['system']['group'][0]['pages'] = "ANY";
588
		$config['system']['group'][0]['home'] = "index.php";
589
		$config['system']['group'][0]['gid'] = "110";
590

    
591
		$config['system']['user'] = array();
592
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
593
		$config['system']['user'][0]['fullname'] = "System Administrator";
594
		$config['system']['user'][0]['scope'] = "system";
595
		$config['system']['user'][0]['groupname'] = "admins";
596
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
597
		$config['system']['user'][0]['uid'] = "0";
598
		/* Ensure that we follow what this new "admin" username should be in the session. */
599
		$_SESSION["Username"] = "{$config['system']['username']}";
600

    
601
		$config['system']['user'][0]['priv'] = array();
602
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
603
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
604
		$config['system']['user'][0]['priv'][0]['descr'] = "Indicates whether this user will lock access to the webConfigurator for other users.";
605
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
606
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
607
		$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).";
608
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
609
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
610
		$config['system']['user'][0]['priv'][2]['descr'] = "Indicates whether this user is able to login for example via SSH.";
611
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
612
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
613
		$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).";
614
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
615
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
616
		$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).";
617

    
618
		$config['system']['nextuid'] = "111";
619
		$config['system']['nextgid'] = "111";
620

    
621
		/* wipe previous auth configuration */
622
		unset ($config['system']['username']);
623
		unset ($config['system']['password']);
624
	}
625
}
626

    
627
function upgrade_040_to_041() {
628
	global $config;
629
	if(!$config['sysctl']) {
630
		$config['sysctl']['item'] = array();
631

    
632
		$config['sysctl']['item'][0]['tunable'] = "net.inet.tcp.blackhole";
633
		$config['sysctl']['item'][0]['desc'] =    "Drop packets to closed TCP ports without returning a RST";
634
		$config['sysctl']['item'][0]['value'] =   "default";
635

    
636
		$config['sysctl']['item'][1]['tunable'] = "net.inet.udp.blackhole";
637
		$config['sysctl']['item'][1]['desc'] =    "Do not send ICMP port unreachable messages for closed UDP ports";
638
		$config['sysctl']['item'][1]['value'] =   "default";
639

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

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

    
648
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
649
		$config['sysctl']['item'][4]['desc'] =    "Sending of IPv4 ICMP redirects";
650
		$config['sysctl']['item'][4]['value'] =   "default";
651

    
652
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
653
		$config['sysctl']['item'][5]['desc'] =    "Sending of IPv6 ICMP redirects";
654
		$config['sysctl']['item'][5]['value'] =   "default";
655

    
656
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
657
		$config['sysctl']['item'][6]['desc'] =    "Generate SYN cookies for outbound SYN-ACK packets";
658
		$config['sysctl']['item'][6]['value'] =   "default";
659

    
660
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
661
		$config['sysctl']['item'][7]['desc'] =    "Maximum incoming TCP datagram size";
662
		$config['sysctl']['item'][7]['value'] =   "default";
663

    
664
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
665
		$config['sysctl']['item'][8]['desc'] =    "Maximum outgoing TCP datagram size";
666
		$config['sysctl']['item'][8]['value'] =   "default";
667

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

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

    
676
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
677
		$config['sysctl']['item'][11]['desc'] =    "Maximum outgoing UDP datagram size";
678
		$config['sysctl']['item'][11]['value'] =   "default";
679

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

    
684
		$config['sysctl']['item'][13]['tunable'] = "net.link.tap.user_open";
685
		$config['sysctl']['item'][13]['desc'] =    "Allow unprivileged access to tap(4) device nodes";
686
		$config['sysctl']['item'][13]['value'] =   "default";
687

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

    
692
		$config['sysctl']['item'][15]['tunable'] = "kern.randompid";
693
		$config['sysctl']['item'][15]['desc'] =    "Randomize PID's (see src/sys/kern/kern_fork.c: sysctl_kern_randompid())";
694
		$config['sysctl']['item'][15]['value'] =   "default";
695

    
696
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.inflight.enable";
697
		$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. ";
698
		$config['sysctl']['item'][16]['value'] =   "default";
699

    
700
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
701
		$config['sysctl']['item'][17]['desc'] =    "Set ICMP Limits";
702
		$config['sysctl']['item'][17]['value'] =   "default";
703

    
704
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
705
		$config['sysctl']['item'][18]['desc'] =    "TCP Offload engine";
706
		$config['sysctl']['item'][18]['value'] =   "default";
707

    
708
		$config['sysctl']['item'][19]['tunable'] = "hw.bce.tso_enable";
709
		$config['sysctl']['item'][19]['desc'] =    "TCP Offload engine - BCE";
710
		$config['sysctl']['item'][19]['value'] =   "default";
711
		
712
		$config['sysctl']['item'][20]['tunable'] = "net.inet.ip.portrange.first";
713
		$config['sysctl']['item'][20]['desc'] =    "Set the ephemeral port range starting port";
714
		$config['sysctl']['item'][20]['value'] =   "default";
715

    
716
		$config['sysctl']['item'][21]['tunable'] = "hw.syscons.kbd_reboot ";
717
		$config['sysctl']['item'][21]['desc'] =    "Enables ctrl+alt+delete";
718
		$config['sysctl']['item'][21]['value'] =   "default";
719

    
720
	}
721
}
722

    
723

    
724
function upgrade_041_to_042() {
725
	global $config;
726
	if (isset($config['shaper']))
727
		unset($config['shaper']);
728
	if (isset($config['ezshaper']))
729
		unset($config['ezshaper']);
730
}
731

    
732

    
733
function upgrade_042_to_043() {
734
	global $config;
735
	/* migrate old interface gateway to the new gateways config */
736
	$iflist = get_configured_interface_list(false, true);
737
	$gateways = array();
738
	$i = 0;
739
	foreach($iflist as $ifname => $interface) {
740
		if(! interface_has_gateway($ifname)) {
741
			continue;
742
		}
743
		$config['gateways']['gateway_item'][$i] = array();
744
		if(is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
745
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
746
			$config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Static Gateway";			
747
		} else {
748
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
749
			$config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Dynamic Gateway";			
750
		}
751
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
752
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
753
		/* add default gateway bit for wan on upgrade */
754
		if($ifname == "wan") {
755
			 $config['gateways']['gateway_item'][$i]['defaultgw'] = true;
756
		}
757
		if(is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
758
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
759
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
760
		}
761
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
762

    
763
		/* Update all filter rules which might reference this gateway */
764
		$j = 0;
765
		foreach($config['filter']['rule'] as $rule) {
766
			if(is_ipaddr($rule['gateway'])) {
767
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway'])
768
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
769
				else if ($rule['gateway'] == $ifname)
770
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
771
			}
772
			$j++;
773
		}
774

    
775
		/* rename old Quality RRD files in the process */
776
		$rrddbpath = "/var/db/rrd";
777
		$gwname = "GW_" . strtoupper($ifname);
778
		if(is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
779
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
780
		}
781
		$i++;
782
	}
783
}
784

    
785

    
786
function upgrade_043_to_044() {
787
	global $config;
788

    
789
	/* migrate static routes to the new gateways config */
790
	$gateways = return_gateways_array(true);
791
	$i = 0;
792
	if (is_array($config['staticroutes']['route'])) {
793
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
794
			$found = false;
795
			foreach ($gateways as $gwname => $gw) {
796
				if ($gw['gateway'] == $sroute['gateway']) {
797
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
798
					$found = true;
799
					break;
800
				}
801
			}
802
			if ($found == false) {
803
				$gateway = array();
804
				$gateway['name'] = "SROUTE{$i}";	
805
				$gateway['gateway'] = $sroute['gateway'];
806
				$gateway['interface'] = $sroute['interface'];
807
				$gateway['descr'] = "Upgraded static route for {$sroute['network']}";
808
				if (!is_array($config['gateways']['gateway_item']))
809
					$config['gateways']['gateway_item'] = array();
810
				$config['gateways']['gateway_item'][] = $gateway;
811
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
812
				$i++;
813
			}
814
		}
815
	}
816
}
817

    
818

    
819
function upgrade_044_to_045() {
820
	global $config;
821
	$iflist = get_configured_interface_list(false, true);
822
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
823
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
824
			$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
825
			$config['vlans']['vlan'][$id] = $vlan;
826
			/* Make sure to update the interfaces section with the right name */
827
			foreach($iflist as $ifname) {
828
				if($config['interfaces'][$ifname]['if'] == "vlan{$id}") {
829
					$config['interfaces'][$ifname]['if'] = $vlan['vlanif'];
830
				}
831
			}
832
			
833
		}
834
	}
835
}
836

    
837

    
838
function upgrade_045_to_046() {
839
	global $config;
840
	/* Upgrade load balancer from slb to relayd */
841
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
842
		$vs_a = &$config['load_balancer']['virtual_server'];
843
		$pool_a = &$config['load_balancer']['lbpool'];
844
		$pools = array();
845
		/* Index pools by name */
846
		if(is_array($pool_a)) {
847
			for ($i = 0; isset($pool_a[$i]); $i++) {
848
				if($pool_a[$i]['type'] == "server") {
849
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
850
				}
851
			}
852
		}
853
		/* Convert sitedown entries to pools and re-attach */
854
		for ($i = 0; isset($vs_a[$i]); $i++) {
855
			if (isset($vs_a[$i]['sitedown'])) {
856
				$pool = array();
857
				$pool['type'] = 'server';
858
				$pool['behaviour'] = 'balance';
859
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
860
				$pool['desc'] = "Sitedown pool for VS: {$vs_a[$i]['name']}";
861
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
862
				$pool['servers'] = array();
863
				$pool['servers'][] = $vs_a[$i]['sitedown'];
864
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
865
				$pool_a[] = $pool;
866
				$vs_a[$i]['sitedown'] = $pool['name'];
867
			}
868
		}
869
	}
870
	if(count($config['load_balancer']) == 0) {
871
		unset($config['load_balancer']);
872
	}
873
}
874

    
875

    
876
function upgrade_046_to_047() {
877
	global $config;
878
	/* Upgrade IPsec from tunnel to phase1/phase2 */
879

    
880
	if(is_array($config['ipsec']['tunnel'])) {
881

    
882
		$a_phase1 = array();
883
		$a_phase2 = array();
884
		$ikeid = 0;
885

    
886
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
887

    
888
			unset($ph1ent);
889
			unset($ph2ent);
890

    
891
			/*
892
				*  attempt to locate an enabled phase1
893
				*  entry that matches the peer gateway
894
				*/
895

    
896
			if (!isset($tunnel['disabled'])) {
897

    
898
				$remote_gateway = $tunnel['remote-gateway'];
899

    
900
				foreach ($a_phase1 as $ph1tmp) {
901
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
902
						$ph1ent = $ph1tmp;
903
						break;
904
					}
905
				}
906
			}
907

    
908
			/* none found, create a new one */
909

    
910
			if (!isset( $ph1ent )) {
911

    
912
				/* build new phase1 entry */
913

    
914
				$ph1ent = array();
915

    
916
				$ph1ent['ikeid'] = ++$ikeid;
917

    
918
				if (isset($tunnel['disabled']))
919
					$ph1ent['disabled'] = $tunnel['disabled'];
920

    
921
				$ph1ent['interface'] = $tunnel['interface'];
922
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
923
				$ph1ent['descr'] = $tunnel['descr'];
924

    
925
				$ph1ent['mode'] = $tunnel['p1']['mode'];
926

    
927
				if (isset($tunnel['p1']['myident']['myaddress']))
928
					$ph1ent['myid_type'] = "myaddress";
929
				if (isset($tunnel['p1']['myident']['address'])) {
930
					$ph1ent['myid_type'] = "address";
931
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
932
				}
933
				if (isset($tunnel['p1']['myident']['fqdn'])) {
934
					$ph1ent['myid_type'] = "fqdn";
935
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
936
				}
937
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
938
					$ph1ent['myid_type'] = "user_fqdn";
939
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
940
				}
941
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
942
					$ph1ent['myid_type'] = "asn1dn";
943
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
944
				}
945
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
946
					$ph1ent['myid_type'] = "dyn_dns";
947
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
948
				}
949

    
950
				$ph1ent['peerid_type'] = "peeraddress";
951

    
952
				switch ($tunnel['p1']['encryption-algorithm']) {
953
					case "des":
954
					$ph1alg = array( 'name' => 'des' );
955
					break;
956
					case "3des":
957
					$ph1alg = array( 'name' => '3des' );
958
					break;
959
					case "blowfish":
960
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
961
					break;
962
					case "cast128":
963
					$ph1alg = array( 'name' => 'cast128' );
964
					break;
965
					case "rijndael":
966
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
967
					break;
968
					case "rijndael 256":
969
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
970
					break;
971
				}
972

    
973
				$ph1ent['encryption-algorithm'] = $ph1alg;
974
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
975
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
976
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
977
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
978

    
979
				if (isset($tunnel['p1']['pre-shared-key']))
980
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
981
				if (isset($tunnel['p1']['cert']))
982
					$ph1ent['cert'] = $tunnel['p1']['cert'];
983
				if (isset($tunnel['p1']['peercert']))
984
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
985
				if (isset($tunnel['p1']['private-key']))
986
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
987

    
988
				$ph1ent['nat_traversal'] = "on";
989
				$ph1ent['dpd_enable'] = 1;
990
				$ph1ent['dpd_delay'] = 10;
991
				$ph1ent['dpd_maxfail'] = 5;
992

    
993
				$a_phase1[] = $ph1ent;
994
			}
995

    
996
			/* build new phase2 entry */
997

    
998
			$ph2ent = array();
999

    
1000
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1001

    
1002
			if (isset($tunnel['disabled']))
1003
				$ph1ent['disabled'] = $tunnel['disabled'];
1004

    
1005
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1006

    
1007
			$type = "lan";
1008
			if ($tunnel['local-subnet']['network'])
1009
				$type = $tunnel['local-subnet']['network'];
1010
			if ($tunnel['local-subnet']['address']) {
1011
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1012
				if (is_null($netbits))
1013
					$type = "address";
1014
				else
1015
					$type = "network";
1016
			}
1017

    
1018
			switch ($type) {
1019
				case "address":
1020
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1021
				break;
1022
				case "network":
1023
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1024
				break;
1025
				default:
1026
				$ph2ent['localid'] = array('type' => $type);
1027
				break;
1028
			}
1029

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

    
1033
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1034

    
1035
			$aes_count = 0;
1036
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1037
				$aes_found = false;
1038
				switch ($tunalg) {
1039
					case "des":
1040
					$ph2alg = array( 'name' => 'des' );
1041
					break;
1042
					case "3des":
1043
					$ph2alg = array( 'name' => '3des' );
1044
					break;
1045
					case "blowfish":
1046
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1047
					break;
1048
					case "cast128":
1049
					$ph2alg = array( 'name' => 'cast128' );
1050
					break;
1051
					case "rijndael":
1052
					case "rijndael 256":
1053
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1054
					$aes_found = true;
1055
					$aes_count++;
1056
					break;
1057
				}
1058

    
1059
				if( !$aes_found || ($aes_count < 2))
1060
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1061
			}
1062

    
1063
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1064
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1065
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1066

    
1067
			if (isset($tunnel['pinghost']['pinghost']))
1068
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1069

    
1070
			$a_phase2[] = $ph2ent;
1071
		}
1072

    
1073
		unset($config['ipsec']['tunnel']);
1074
		$config['ipsec']['phase1'] = $a_phase1;
1075
		$config['ipsec']['phase2'] = $a_phase2;
1076
	}
1077
}
1078

    
1079

    
1080
function upgrade_047_to_048() {
1081
	global $config;
1082
	if (!empty($config['dyndns'])) {
1083
		$config['dyndnses'] = array();
1084
		$config['dyndnses']['dyndns'] = array();
1085
		if(isset($config['dyndns'][0]['enable'])) {
1086
			$tempdyn = array();
1087
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1088
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1089
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1090
			$tempdyn['usernamefld'] = $config['dyndns'][0]['username'];
1091
			$tempdyn['passwordfld'] = $config['dyndns'][0]['password'];
1092
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1093
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];		
1094
			$tempdyn['interface'] = "wan";
1095
			$tempdyn['descr'] = "Upgraded Dyndns {$tempdyn['type']}";
1096
			$config['dyndnses']['dyndns'][] = $tempdyn;
1097
		}
1098
		unset($config['dyndns']);
1099
	}		
1100
	if (!empty($config['dnsupdate'])) {
1101
		$pconfig = $config['dnsupdate'][0];
1102
		if (!$pconfig['ttl'])
1103
			$pconfig['ttl'] = 60;
1104
		if (!$pconfig['keytype'])
1105
			$pconfig['keytype'] = "zone";
1106
		$pconfig['interface'] = "wan";
1107
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1108
		unset($config['dnsupdate']);
1109
	}
1110

    
1111
	if (is_array($config['pppoe'])) {
1112
		$pconfig = array();
1113
		$pconfig['username'] = $config['pppoe']['username'];
1114
		$pconfig['password'] = $config['pppoe']['password'];
1115
		$pconfig['provider'] = $config['pppoe']['provider'];
1116
		$pconfig['ondemand'] = isset($config['pppoe']['ondemand']);
1117
		$pconfig['timeout'] = $config['pppoe']['timeout'];
1118
		unset($config['pppoe']);
1119
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1120
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1121
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1122
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1123
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1124
	}
1125
	if (is_array($config['pptp'])) {
1126
		$pconfig = array();
1127
		$pconfig['username'] = $config['pptp']['username'];
1128
		$pconfig['password'] = $config['pptp']['password'];
1129
		$pconfig['provider'] = $config['pptp']['provider'];
1130
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1131
		$pconfig['timeout'] = $config['pptp']['timeout'];
1132
		unset($config['pptp']);
1133
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1134
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1135
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1136
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1137
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1138
	}
1139
}
1140

    
1141

    
1142
function upgrade_048_to_049() {
1143
	global $config;
1144
	/* setup new all users group */
1145
	$all = array();
1146
	$all['name'] = "all";
1147
	$all['description'] = "All Users";
1148
	$all['scope'] = "system";
1149
	$all['gid'] = 1998;
1150
	$all['member'] = array();
1151

    
1152
	if (!is_array($config['system']['group']))
1153
		$config['system']['group'] = array();
1154

    
1155
	/* work around broken uid assignments */
1156
	$config['system']['nextuid'] = 2000;
1157
	foreach ($config['system']['user'] as & $user) {
1158
		if (isset($user['uid']) && !$user['uid'])
1159
			continue;
1160
		$user['uid'] = $config['system']['nextuid']++;
1161
	}
1162

    
1163
	/* work around broken gid assignments */
1164
	$config['system']['nextgid'] = 2000;
1165
	foreach ($config['system']['group'] as & $group) {
1166
		if ($group['name'] == $g['admin_group'])
1167
			$group['gid'] = 1999;
1168
		else
1169
			$group['gid'] = $config['system']['nextgid']++;
1170
	}
1171

    
1172
	/* build group membership information */
1173
	foreach ($config['system']['group'] as & $group) {
1174
		$group['member'] = array();
1175
		foreach ($config['system']['user'] as & $user) {
1176
			$groupnames = explode(",", $user['groupname']);
1177
			if (in_array($group['name'],$groupnames))
1178
				$group['member'][] = $user['uid'];
1179
		}
1180
	}
1181

    
1182
	/* reset user group information */
1183
	foreach ($config['system']['user'] as & $user) {
1184
		unset($user['groupname']);
1185
		$all['member'][] = $user['uid'];
1186
	}
1187

    
1188
	/* reset group scope information */
1189
	foreach ($config['system']['group'] as & $group)
1190
		if ($group['name'] != $g['admin_group'])
1191
		$group['scope'] = "user";
1192

    
1193
	/* insert new all group */
1194
	$groups = Array();
1195
	$groups[] = $all;
1196
	$groups = array_merge($config['system']['group'],$groups);
1197
	$config['system']['group'] = $groups;
1198
}
1199

    
1200

    
1201
function upgrade_049_to_050() {
1202
	global $config;
1203
	/* update user privileges */
1204
	foreach ($config['system']['user'] as & $user) {
1205
		$privs = array();
1206
		if (!is_array($user['priv'])) {
1207
			unset($user['priv']);
1208
			continue;
1209
		}
1210
		foreach ($user['priv'] as $priv) {
1211
			switch($priv['id']) {
1212
				case "hasshell":
1213
				$privs[] = "user-shell-access";
1214
				break;
1215
				case "copyfiles":
1216
				$privs[] = "user-copy-files";
1217
				break;
1218
			}
1219
		}
1220
		$user['priv'] = $privs;
1221
	}
1222

    
1223
	/* update group privileges */
1224
	foreach ($config['system']['group'] as & $group) {
1225
		$privs = array();
1226
		if (!is_array($group['pages'])) {
1227
			unset($group['pages']);
1228
			continue;
1229
		}
1230
		foreach ($group['pages'] as $page) {
1231
			$priv = map_page_privname($page);
1232
			if ($priv)
1233
				$privs[] = $priv;
1234
		}
1235
		unset($group['pages']);
1236
		$group['priv'] = $privs;
1237
	}
1238

    
1239
	/* sync all local account information */
1240
	local_sync_accounts();
1241
}
1242

    
1243

    
1244
function upgrade_050_to_051() {
1245
	global $config;
1246
	$pconfig = array();
1247
	$pconfig['desc'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1248
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1249
	$pconfig['value'] = "1";
1250
	$config['sysctl']['item'][] = $pconfig;
1251
	$pconfig = array();
1252
	$pconfig['desc'] = "Set to 1 to enable filtering on the bridge interface";
1253
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1254
	$pconfig['value'] = "0";
1255
	$config['sysctl']['item'][] = $pconfig;
1256

    
1257
	unset($config['bridge']);
1258

    
1259
	$convert_bridges = false;
1260
	foreach($config['interfaces'] as $intf) {
1261
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1262
			$config['bridges'] = array();
1263
			$config['bridges']['bridged'] = array();
1264
			$convert_bridges = true;
1265
			break;
1266
		}
1267
	}
1268
	if ($convert_bridges == true) {
1269
		$i = 0;
1270
		foreach ($config['interfaces'] as $ifr => &$intf) {
1271
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1272
				$nbridge = array();
1273
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1274
				$nbridge['descr'] = "Converted bridged {$ifr}";
1275
				$nbridge['bridgeif'] = "bridge{$i}";
1276
				$config['bridges']['bridged'][] = $nbridge;
1277
				unset($intf['bridge']);
1278
				$i++;
1279
			}
1280
		}
1281
	}
1282
}
1283

    
1284

    
1285
function upgrade_051_to_052() {
1286
	global $config;
1287
	$config['openvpn'] = array();
1288
	if (!is_array($config['ca']))
1289
		$config['ca'] = array();
1290
	if (!is_array($config['cert']))
1291
		$config['cert'] = array();
1292

    
1293
	$vpnid = 1;
1294

    
1295
	/* openvpn server configurations */
1296
	if (is_array($config['installedpackages']['openvpnserver'])) {
1297
		$config['openvpn']['openvpn-server'] = array();
1298

    
1299
		$index = 1;
1300
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1301

    
1302
			if (!is_array($server))
1303
				continue;
1304

    
1305
			if ($server['auth_method'] == "pki") {
1306

    
1307
				/* create ca entry */
1308
				$ca = array();
1309
				$ca['refid'] = uniqid();
1310
				$ca['name'] = "OpenVPN Server CA #{$index}";
1311
				$ca['crt'] = $server['ca_cert'];
1312
				$ca['crl'] = $server['crl'];
1313
				$config['ca'][] = $ca;
1314

    
1315
				/* create ca reference */
1316
				unset($server['ca_cert']);
1317
				unset($server['crl']);
1318
				$server['caref'] = $ca['refid'];
1319

    
1320
				/* create cert entry */
1321
				$cert = array();
1322
				$cert['refid'] = uniqid();
1323
				$cert['name'] = "OpenVPN Server Certificate #{$index}";
1324
				$cert['crt'] = $server['server_cert'];
1325
				$cert['prv'] = $server['server_key'];
1326
				$config['cert'][] = $cert;
1327

    
1328
				/* create cert reference */
1329
				unset($server['server_cert']);
1330
				unset($server['server_key']);
1331
				$server['certref'] = $cert['refid'];
1332

    
1333
				$index++;
1334
			}
1335

    
1336
			/* determine operational mode */
1337
			if ($server['auth_method'] == 'pki') {
1338
				if($server['nopool']) {
1339
					$server['mode'] = "p2p_tls";
1340
				} else {
1341
					$server['mode'] = "server_tls";
1342
				}
1343
			} else {
1344
				$server['mode'] = "p2p_shared_key";
1345
			}
1346
			unset($server['auth_method']);
1347

    
1348
			/* modify configuration values */
1349
			$server['dh_length'] = 1024;
1350
			unset($server['dh_params']);
1351
			if (!$server['interface'])
1352
				$server['interface'] = 'wan';
1353
			$server['tunnel_network'] = $server['addresspool'];
1354
			unset($server['addresspool']);
1355
			if (isset($server['use_lzo'])) {
1356
				$server['compression'] = "on";
1357
				unset($server['use_lzo']);
1358
			}
1359
			if ($server['nopool'])
1360
				$server['pool_enable'] = false;
1361
			else
1362
				$server['pool_enable'] = "yes";
1363
			unset($server['nopool']);
1364
			$server['dns_domain'] = $server['dhcp_domainname'];
1365
			unset($server['dhcp_domainname']);
1366
			$server['dns_server1'] = $server['dhcp_dns'];
1367
			unset($server['dhcp_dns']);
1368
			$server['ntp_server1'] = $server['dhcp_ntp'];
1369
			unset($server['dhcp_ntp']);
1370
			if ($server['dhcp_nbtdisable'])
1371
				$server['netbios_enable'] = false;
1372
			else
1373
				$server['netbios_enable'] = "yes";
1374
			unset($server['dhcp_nbtdisable']);
1375
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1376
			unset($server['dhcp_nbttype']);
1377
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1378
			unset($server['dhcp_nbtscope']);
1379
			$server['nbdd_server1'] = $server['dhcp_nbdd'];
1380
			unset($server['dhcp_nbdd']);
1381
			$server['wins_server1'] = $server['dhcp_wins'];
1382
			unset($server['dhcp_wins']);
1383

    
1384
			if (!empty($server['disable']))
1385
				$server['disable'] = true;
1386
			else
1387
				unset($server['disable']);
1388

    
1389
			/* allocate vpnid */
1390
			$server['vpnid'] = $vpnid++;
1391

    
1392
			if (!empty($server['custom_options'])) {
1393
				$cstmopts = array();
1394
				$tmpcstmopts = explode(";", $server['custom_options']);
1395
				$assigned = false;
1396
				$tmpstr = "";
1397
				foreach ($tmpcstmopts as $tmpcstmopt) {
1398
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1399
					if (substr($tmpstr,0 ,6) == "devtun") {
1400
						$assigned = true;
1401
						continue;
1402
					} else if (substr($tmpstr, 0, 5) == "local") {
1403
						$localip = substr($tmpstr, 6);
1404
						$server['ipaddr'] = str_replace("\n", "", $localip);
1405
					} else
1406
						$cstmopts[] = $tmpcstmopt;
1407
				}
1408
				$server['custom_options'] = implode(";", $cstmopts);
1409
				if ($assigned == true) {
1410
					$realif = substr($tmpstr, 3);
1411
					foreach ($config['interfaces'] as $iface => $cfgif) {
1412
						if ($cfgif['if'] == $realif) {
1413
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1414
							break;
1415
						}
1416
					}
1417
				}
1418
			}
1419

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

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

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

    
1432
			if (!is_array($client))
1433
				continue;
1434

    
1435
			if ($client['auth_method'] == "pki") {
1436

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

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

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

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

    
1463
				$index++;
1464
			}
1465

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

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

    
1491
			/* allocate vpnid */
1492
			$client['vpnid'] = $vpnid++;
1493

    
1494
			if (!empty($client['custom_options'])) {
1495
				$cstmopts = array();
1496
				$tmpcstmopts = explode(";", $client['custom_options']);
1497
				$assigned = false;
1498
				$tmpstr = "";
1499
				foreach ($tmpcstmopts as $tmpcstmopt) {
1500
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1501
					if (substr($tmpstr,0 ,6) == "devtun") {
1502
						$assigned = true;
1503
						continue;
1504
					} else if (substr($tmpstr, 0, 5) == "local") {
1505
                                                $localip = substr($tmpstr, 6);
1506
                                                $client['ipaddr'] = str_replace("\n", "", $localip);
1507
					} else
1508
						$cstmopts[] = $tmpcstmopt;
1509
				}
1510
				$client['custom_options'] = implode(";", $cstmopts);
1511
				if ($assigned == true) {
1512
					$realif = substr($tmpstr, 3);
1513
					foreach ($config['interfaces'] as $iface => $cfgif) {
1514
						if ($cfgif['if'] == $realif) {
1515
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1516
							break;
1517
						}
1518
					}
1519
				}
1520
			}
1521

    
1522
			if (!empty($client['disable']))
1523
				$client['disable'] = true;
1524
			else
1525
				unset($client['disable']);
1526

    
1527
			$config['openvpn']['openvpn-client'][] = $client;
1528
		}
1529

    
1530
		unset($config['installedpackages']['openvpnclient']);
1531
	}
1532

    
1533
	/* openvpn client specific configurations */
1534
	if (is_array($config['installedpackages']['openvpncsc'])) {
1535
		$config['openvpn']['openvpn-csc'] = array();
1536

    
1537
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1538

    
1539
			if (!is_array($csc))
1540
				continue;
1541

    
1542
			/* modify configuration values */
1543
			$csc['common_name'] = $csc['commonname'];
1544
			unset($csc['commonname']);
1545
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1546
			unset($csc['ifconfig_push']);
1547
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1548
			unset($csc['dhcp_domainname']);
1549
			$csc['dns_server1'] = $csc['dhcp_dns'];
1550
			unset($csc['dhcp_dns']);
1551
			$csc['ntp_server1'] = $csc['dhcp_ntp'];
1552
			unset($csc['dhcp_ntp']);
1553
			if ($csc['dhcp_nbtdisable'])
1554
				$csc['netbios_enable'] = false;
1555
			else
1556
				$csc['netbios_enable'] = "yes";
1557
			unset($csc['dhcp_nbtdisable']);
1558
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1559
			unset($csc['dhcp_nbttype']);
1560
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1561
			unset($csc['dhcp_nbtscope']);
1562
			$csc['nbdd_server1'] = $csc['dhcp_nbdd'];
1563
			unset($csc['dhcp_nbdd']);
1564
			$csc['wins_server1'] = $csc['dhcp_wins'];
1565
			unset($csc['dhcp_wins']);
1566

    
1567
			if (!empty($csc['disable']))
1568
				$csc['disable'] = true;
1569
			else
1570
				unset($csc['disable']);
1571

    
1572
			$config['openvpn']['openvpn-csc'][] = $csc;
1573
		}
1574

    
1575
		unset($config['installedpackages']['openvpncsc']);
1576
	}
1577

    
1578
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1579
		count($config['openvpn']['openvpn-client']) > 0) {
1580
		$ovpnrule = array();
1581
                $ovpnrule['type'] = "pass";
1582
                $ovpnrule['interface'] = "openvpn";
1583
                $ovpnrule['statetype'] = "keep state";
1584
                $ovpnrule['source'] = array();
1585
                $ovpnrule['destination'] = array();
1586
                $ovpnrule['source']['any'] = true;
1587
                $ovpnrule['destination']['any'] = true;
1588
                $ovpnrule['descr'] = "Auto added OpenVPN rule from config upgrade.";
1589
		$config['filter']['rule'][] = $ovpnrule;
1590
	}
1591

    
1592
	/*
1593
		* FIXME: hack to keep things working with no installedpackages
1594
		* or carp array in the configuration data.
1595
		*/
1596
	if (!is_array($config['installedpackages']))
1597
		$config['installedpackages'] = array();
1598
	if (!is_array($config['installedpackages']['carp']))
1599
		$config['installedpackages']['carp'] = array();
1600

    
1601
}
1602

    
1603

    
1604
function upgrade_052_to_053() {
1605
	global $config;
1606
	if (!is_array($config['ca']))
1607
		$config['ca'] = array();
1608
	if (!is_array($config['cert']))
1609
		$config['cert'] = array();
1610

    
1611
	/* migrate advanced admin page webui ssl to certifcate mngr */
1612
	if ($config['system']['webgui']['certificate'] &&
1613
	$config['system']['webgui']['private-key']) {
1614

    
1615
		/* create cert entry */
1616
		$cert = array();
1617
		$cert['refid'] = uniqid();
1618
		$cert['name'] = "webConfigurator SSL Certificate";
1619
		$cert['crt'] = $config['system']['webgui']['certificate'];
1620
		$cert['prv'] = $config['system']['webgui']['private-key'];
1621
		$config['cert'][] = $cert;
1622

    
1623
		/* create cert reference */
1624
		unset($config['system']['webgui']['certificate']);
1625
		unset($config['system']['webgui']['private-key']);
1626
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1627
	}
1628

    
1629
	/* migrate advanced admin page ssh keys to user manager */
1630
	if ($config['system']['ssh']['authorizedkeys']) {
1631
		$admin_user =& getUserEntryByUID(0);
1632
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1633
		unset($config['system']['ssh']['authorizedkeys']);
1634
	}
1635
}
1636

    
1637

    
1638
function upgrade_053_to_054() {
1639
	global $config;
1640
	if(is_array($config['load_balancer']['lbpool'])) {
1641
		$lbpool_arr = $config['load_balancer']['lbpool'];
1642
		$lbpool_srv_arr = array();
1643
		$gateway_group_arr = array();
1644
		$gateways = return_gateways_array();
1645
		if (! is_array($config['gateways']['gateway_item']))
1646
			$config['gateways']['gateway_item'] = array();
1647

    
1648
		$a_gateways =& $config['gateways']['gateway_item'];
1649
		foreach($lbpool_arr as $lbpool) {
1650
			if($lbpool['type'] == "gateway") {
1651
				$gateway_group['name'] = $lbpool['name'];
1652
				$gateway_group['descr'] = $lbpool['desc'];
1653
				$gateway_group['trigger'] = "down";
1654
				$gateway_group['item'] = array();
1655
				$i = 0;
1656
				foreach($lbpool['servers'] as $member) {
1657
					$split = split("\|", $member);
1658
					$interface = $split[0];
1659
					$monitor = $split[1];
1660
					/* on static upgraded configuration we automatically prepend GW_ */
1661
					$static_name = "GW_" . strtoupper($interface);
1662
					if(is_ipaddr($monitor)) {
1663
						$interface = $static_name;
1664
						$config['interfaces'][$interface]['monitorip'] = $monitor;
1665
					}
1666
					/* on failover increment tier. Else always assign 1 */
1667
					if($lbpool['behaviour'] == "failover") {
1668
						$i++;
1669
					} else {
1670
						$i = 1;
1671
					}
1672
					$gateway_group['item'][] = "$interface|$i";
1673
				}
1674
				$gateway_group_arr[] = $gateway_group;
1675
			} else {
1676
				$lbpool_srv_arr[] = $lbpool;
1677
			}
1678
		}
1679
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1680
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1681
	}
1682
	// Unset lbpool if we no longer have any server pools
1683
	if (count($lbpool_srv_arr) == 0) {
1684
		if(empty($config['load_balancer'])) {
1685
			unset($config['load_balancer']);
1686
		} else {
1687
			unset($config['load_balancer']['lbpool']);
1688
		}
1689
	} else {
1690
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1691
	}
1692
	// Only set the gateway group array if we converted any
1693
	if (count($gateway_group_arr) != 0) {
1694
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1695
	}
1696
}
1697

    
1698

    
1699
function upgrade_054_to_055() {
1700
	global $config;
1701
	global $g;
1702

    
1703
	/* RRD files changed for quality, traffic and packets graphs */
1704
	//ini_set("max_execution_time", "1800");
1705
	/* convert traffic RRD file */
1706
	global $parsedcfg, $listtags;
1707
	$listtags = array("ds", "v", "rra", "row");
1708

    
1709
	$rrddbpath = "/var/db/rrd/";
1710
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1711

    
1712
	$rrdinterval = 60;
1713
	$valid = $rrdinterval * 2;
1714

    
1715
	/* Asume GigE for now */
1716
	$downstream = 125000000;
1717
	$upstream = 125000000;
1718

    
1719
	/* build a list of quality databases */
1720
	/* roundtrip has become delay */
1721
	function divide_delay($delayval) {
1722
		$delayval = floatval($delayval);
1723
		$delayval = ($delayval / 1000);
1724
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1725
		return $delayval;
1726
	}
1727
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1728
	$databases = array();
1729
	chdir($rrddbpath);
1730
	$databases = glob("*-quality.rrd");
1731
	rsort($databases);
1732
	foreach($databases as $database) {
1733
		$xmldump = "{$database}.old.xml";
1734
		$xmldumpnew = "{$database}.new.xml";
1735

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

    
1740
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
1741
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1742
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1743
		$rrdold = $rrdold['rrd'];
1744

    
1745
		$i = 0;
1746
		foreach($rrdold['rra'] as $rra) {
1747
			$l = 0;
1748
			foreach($rra['database']['row'] as $row) {
1749
				$vnew = divide_delay($row['v'][1]);
1750
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1751
				$l++;
1752
			}
1753
			$i++;
1754
		}
1755

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

    
1760
	}
1761
	/* let apinger recreate required files */
1762
	setup_gateways_monitor();
1763

    
1764
	/* build a list of traffic and packets databases */
1765
	$databases = array();
1766
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
1767
	rsort($databases);
1768
	foreach($databases as $database) {
1769
		$databasetmp = "{$database}.tmp";
1770
		$xmldump = "{$database}.old.xml";
1771
		$xmldumptmp = "{$database}.tmp.xml";
1772
		$xmldumpnew = "{$database}.new.xml";
1773

    
1774
		if ($g['booting'])
1775
			echo "Migrate RRD database {$database} to new format \n";
1776
		/* rename DS source */
1777
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1778
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1779

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

    
1783
		/* create new rrd database file */
1784
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
1785
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
1786
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
1787
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
1788
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
1789
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
1790
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
1791
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
1792
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
1793

    
1794
		create_new_rrd("$rrdcreate");
1795
		/* create temporary xml from new RRD */
1796
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
1797

    
1798
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1799
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1800
		$rrdold = $rrdold['rrd'];
1801

    
1802
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
1803
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
1804
		$rrdnew = $rrdnew['rrd'];
1805

    
1806
		/* remove any MAX RRA's. Not needed for traffic. */
1807
		$i = 0;
1808
		foreach ($rrdold['rra'] as $rra) {
1809
			if(trim($rra['cf']) == "MAX") {
1810
				unset($rrdold['rra'][$i]);
1811
			}
1812
			$i++;
1813
		}
1814

    
1815
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
1816
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
1817
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1818
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1819

    
1820
	}
1821
	enable_rrd_graphing();
1822
	if ($g['booting'])
1823
		echo "Updating configuration...";
1824
}
1825

    
1826

    
1827
function upgrade_055_to_056() {
1828
	global $config;
1829

    
1830
	if (!is_array($config['ca']))
1831
		$config['ca'] = array();
1832
	if (!is_array($config['cert']))
1833
		$config['cert'] = array();
1834

    
1835
	/* migrate ipsec ca's to cert manager */
1836
	if (is_array($config['ipsec']['cacert'])) {
1837
		foreach($config['ipsec']['cacert'] as & $cacert) {
1838
			$ca = array();
1839
			$ca['refid'] = uniqid();
1840
			if (is_array($cacert['cert']))
1841
				$ca['crt'] = $cacert['cert'][0];
1842
			else
1843
				$ca['crt'] = $cacert['cert'];
1844
			$ca['name'] = $cacert['ident'];
1845
			$config['ca'][] = $ca;
1846
		}
1847
		unset($config['ipsec']['cacert']);
1848
	}
1849

    
1850
	/* migrate phase1 certificates to cert manager */
1851
	if (is_array($config['ipsec']['phase1'])) {
1852
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
1853
			$cert = array();
1854
			$cert['refid'] = uniqid();
1855
			$cert['name'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
1856
			if (is_array($ph1ent['cert']))
1857
				$cert['crt'] = $ph1ent['cert'][0];
1858
			else
1859
				$cert['crt'] = $ph1ent['cert'];
1860
			$cert['prv'] = $ph1ent['private-key'];
1861
			$config['cert'][] = $cert;
1862
			$ph1ent['certref'] = $cert['refid'];
1863
			if ($ph1ent['cert'])
1864
				unset($ph1ent['cert']);
1865
			if ($ph1ent['private-key'])
1866
				unset($ph1ent['private-key']);
1867
			if ($ph1ent['peercert'])
1868
				unset($ph1ent['peercert']);
1869
		}
1870
	}
1871
}
1872

    
1873

    
1874
function upgrade_056_to_057() {
1875
	global $config;
1876
	/* migrate captivate portal to user manager */
1877
	if (is_array($config['captiveportal']['user'])) {
1878
		foreach($config['captiveportal']['user'] as $user) {
1879
			// avoid user conflicts
1880
			if ($config['system']['user'][$user['name']])
1881
				continue;
1882
			$user['scope'] = "user";
1883
			if (isset($user['expirationdate'])) {
1884
				$user['expires'] = $user['expirationdate'];
1885
				unset($user['expirationdate']);
1886
			}
1887
			if (isset($user['password'])) {
1888
				$user['md5-hash'] = $user['password'];
1889
				unset($user['password']);
1890
			}
1891
			$config['system']['user'][] = $user;
1892
		}
1893
		unset($config['captiveportal']['user']);
1894
	}
1895
}
1896

    
1897
function upgrade_057_to_058() {
1898
	global $config;
1899
	/* set all phase2 entries to tunnel mode */
1900
	if (is_array($config['ipsec']['phase2']))
1901
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1902
			$ph2ent['mode'] = 'tunnel';
1903
}
1904

    
1905
function upgrade_058_to_059() {
1906
	global $config;
1907

    
1908
	if (is_array($config['schedules']['schedule'])) {
1909
		foreach ($config['schedules']['schedule'] as & $schedl)
1910
			$schedl['schedlabel'] = uniqid();
1911
	}
1912
}
1913

    
1914
function upgrade_059_to_060() {
1915
	global $config;
1916
	require_once("/etc/inc/certs.inc");
1917
	if (is_array($config['ca'])) {
1918
		/* Locate issuer for all CAs */
1919
		foreach ($config['ca'] as & $ca) {
1920
			$subject = cert_get_subject($ca['crt']);
1921
			$issuer = cert_get_issuer($ca['crt']);
1922
			if($issuer <> $subject) {
1923
				$issuer_crt =& lookup_ca_by_subject($issuer);
1924
				if($issuer_crt)
1925
					$ca['caref'] = $issuer_crt['refid'];
1926
			}
1927
		}
1928
		
1929
		/* Locate issuer for all certificates */
1930
		if (is_array($config['cert'])) {
1931
			foreach ($config['cert'] as & $cert) {
1932
				$subject = cert_get_subject($cert['crt']);
1933
				$issuer = cert_get_issuer($cert['crt']);
1934
				if($issuer <> $subject) {
1935
					$issuer_crt =& lookup_ca_by_subject($issuer);
1936
					if($issuer_crt)
1937
						$cert['caref'] = $issuer_crt['refid'];
1938
				}
1939
			}
1940
		}
1941
	}
1942
}
1943

    
1944
function upgrade_060_to_061() {
1945
	global $config;
1946

    
1947
	if (is_array($config['interfaces']['wan']))
1948
		$config['interfaces']['wan']['enable'] = true;
1949
	if (is_array($config['interfaces']['lan']))
1950
		$config['interfaces']['lan']['enable'] = true;
1951
}
1952

    
1953
function upgrade_061_to_062() {
1954
	global $config;
1955

    
1956
	/* Convert NAT port forwarding rules */
1957
	if (is_array($config['nat']['rule'])) {
1958
		$a_nat = &$config['nat']['rule'];
1959

    
1960
		foreach ($a_nat as &$natent) {
1961
			$natent['disabled'] = false;
1962
			$natent['nordr']    = false;
1963

    
1964
			$natent['source'] = array(
1965
				"not"     => false,
1966
				"any"     => true,
1967
				"port"    => ""
1968
			);
1969

    
1970
			$natent['destination'] = array(
1971
				"not"     => false,
1972
				"address" => $natent['external-address'],
1973
				"port"    => $natent['external-port']
1974
			);
1975

    
1976
			if (empty($natent['destination']['address'])) {
1977
				unset($natent['destination']['address']);
1978
				$natent['destination']['network'] = $natent['interface'] . 'ip';
1979
			} else if ($natent['destination']['address'] == 'any') {
1980
				unset($natent['destination']['address']);
1981
				$natent['destination']['any'] = true;
1982
			}
1983

    
1984
			unset($natent['external-address']);
1985
			unset($natent['external-port']);
1986
		}
1987

    
1988
		unset($natent);
1989
	}
1990
}
1991

    
1992
function upgrade_062_to_063() {
1993
	/* Upgrade legacy Themes to the new pfsense_ng */
1994
	global $config;
1995

    
1996
	switch($config['theme']) {
1997
		case "nervecenter":
1998
			$config['theme'] = "pfsense_ng";
1999
			break;
2000
	}
2001
	
2002
}
2003

    
2004
function upgrade_063_to_064() {
2005
	global $config;
2006
	$j=0;
2007
	$ifcfg = &$config['interfaces'];
2008
	
2009
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {	
2010
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2011
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2012
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2013
			$j++;
2014
			if (isset($ppp['port'])){
2015
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2016
				unset($config['ppps']['ppp'][$pppid]['port']);
2017
			}
2018
			if (!isset($ppp['type'])){
2019
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2020
			}
2021
			if (isset($ppp['defaultgw']))
2022
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2023
		}
2024
	}
2025
	
2026
	if (!is_array($config['ppps']['ppp']))
2027
		$config['ppps']['ppp'] = array();
2028
	$a_ppps = &$config['ppps']['ppp'];
2029

    
2030
	foreach ($ifcfg as $ifname => $ifinfo) {
2031
		$ppp = array();
2032
		// For pppoe conversion
2033
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2034
			if (isset($ifinfo['ptpid']))
2035
				continue;
2036
			$ppp['ptpid'] =  $j;
2037
			$ppp['type'] = $ifinfo['ipaddr'];
2038
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2039
			$ppp['ports'] = $ifinfo['if'];
2040
			if ($ifinfo['ipaddr'] == "pppoe"){
2041
				$ppp['username'] = $ifinfo['pppoe_username'];
2042
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2043
			}
2044
			if ($ifinfo['ipaddr'] == "pptp"){
2045
				$ppp['username'] = $ifinfo['pptp_username'];
2046
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2047
			}
2048
			
2049
			if (isset($ifinfo['provider']))
2050
				$ppp['provider'] = $ifinfo['provider'];
2051
			if (isset($ifinfo['ondemand']))
2052
				$ppp['ondemand'] = true;
2053
			if (isset($ifinfo['timeout']))
2054
				$ppp['idletimeout'] = $ifinfo['timeout'];
2055
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2056
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2057
				if (is_array($config['cron']['item'])) {
2058
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2059
						$item = $config['cron']['item'][$i];
2060
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2061
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2062
					}
2063
				}
2064
			}
2065
			if (isset($ifinfo['local']))
2066
				$ppp['localip'] = $ifinfo['local'];
2067
			if (isset($ifinfo['subnet']))
2068
				$ppp['subnet'] = $ifinfo['subnet'];
2069
			if (isset($ifinfo['remote']))
2070
				$ppp['gateway'] = $ifinfo['remote'];
2071

    
2072
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2073
			$j++;
2074
			
2075
			unset($ifcfg[$ifname]['pppoe_username']);
2076
			unset($ifcfg[$ifname]['pppoe_password']);
2077
			unset($ifcfg[$ifname]['provider']);
2078
			unset($ifcfg[$ifname]['ondemand']);
2079
			unset($ifcfg[$ifname]['timeout']);
2080
			unset($ifcfg[$ifname]['pppoe_reset']);
2081
			unset($ifcfg[$ifname]['pppoe_preset']);
2082
			unset($ifcfg[$ifname]['pppoe']);
2083
			unset($ifcfg[$ifname]['pptp_username']);
2084
			unset($ifcfg[$ifname]['pptp_password']);
2085
			unset($ifcfg[$ifname]['local']);
2086
			unset($ifcfg[$ifname]['subnet']);
2087
			unset($ifcfg[$ifname]['remote']);
2088
			
2089
			$a_ppps[] = $ppp;
2090
			
2091
		}
2092
	}
2093
}
2094

    
2095
function upgrade_064_to_065() {
2096
	/* Disable TSO and LRO in upgraded configs */
2097
	global $config;
2098
	$config['system']['disablesegmentationoffloading'] = true;
2099
	$config['system']['disablelargereceiveoffloading'] = true;
2100
}
2101

    
2102
function upgrade_065_to_066() {
2103
	global $config;
2104

    
2105
	$dhcrelaycfg =& $config['dhcrelay'];
2106

    
2107
        if (is_array($dhcrelaycfg)) {
2108
        	$dhcrelayifs = array();
2109
		$foundifs = false;
2110
        	/* DHCPRelay enabled on any interfaces? */
2111
                foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2112
                        if (isset($dhcrelayifconf['enable'])) {
2113
				$dhcrelayifs[] = $dhcrelayif;
2114
				unset($dhcrelaycfg['dhcrelayif']);
2115
				$foundifs = true;
2116
			}
2117
                }
2118
		if ($foundifs == true)
2119
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2120
        }
2121
}
2122

    
2123
function upgrade_066_to_067() {
2124
	global $config;
2125
	if (isset($config['system']['ca'])) {
2126
		$config['ca'] = $config['system']['ca'];
2127
	}
2128
	if (isset($config['system']['cert'])) {
2129
		$config['cert'] = $config['system']['cert'];
2130
	}
2131
}
2132

    
2133
function upgrade_067_to_068() {
2134
	global $config;
2135

    
2136
	if (!empty($config['pppoe'])) {
2137
		$config['pppoes'] = array();
2138
		$config['pppoes']['pppoe'] = array();
2139
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2140

    
2141
		if (is_array($config['pppoe']['user'])) {
2142
			$username = array(); 
2143
			foreach ($config['pppoe']['user'] as $user) {
2144
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2145
				if ($user['ip'])
2146
					$usr .= ":{$user['ip']}";
2147
				$username[] = $usr;
2148
			}
2149
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2150
		}
2151
		unset($config['pppoe']);
2152
	}
2153
}
2154

    
2155
function upgrade_068_to_069() {
2156
	global $config;
2157
	if (!is_array($config['system']['user']))
2158
		return;
2159
	foreach ($config['system']['user'] as & $user) {
2160
		if (!is_array($user['cert']))
2161
			continue;
2162
		$rids = array();
2163
		foreach ($user['cert'] as $id => $cert) {
2164
			if (!isset($cert['name']))
2165
				continue;
2166
			$tcert = $cert;
2167
			// Make sure each cert gets a refid
2168
			if (!isset($tcert['refid']))
2169
				$tcert['refid'] = uniqid();
2170
			// Keep the cert references for this user
2171
			$rids[] = $tcert['refid'];
2172
			$config['cert'][] = $tcert;
2173
		}
2174
		// Replace user certs with cert references instead.
2175
		if (count($rids) > 0)
2176
			$user['cert'] = $rids;
2177
	}
2178
}
2179

    
2180
function upgrade_069_to_070() {
2181
	global $config;
2182

    
2183
	/* Convert NAT 1:1 rules */
2184
	if (is_array($config['nat']['onetoone'])) {
2185
		$a_nat = &$config['nat']['onetoone'];
2186

    
2187
		foreach ($a_nat as &$natent) {
2188
			if ($natent['subnet'] == 32)
2189
				$natent['source'] = array("address" => $natent['internal']);
2190
			else
2191
				$natent['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2192

    
2193
			$natent['destination'] = array("any" => true);
2194

    
2195
			unset($natent['internal']);
2196
			unset($natent['subnet']);
2197
		}
2198

    
2199
		unset($natent);
2200
	}
2201
}
2202

    
2203
function upgrade_070_to_071() {
2204
	global $config;
2205

    
2206
	if (is_array($config['cron']['item'])) {
2207
		foreach($config['cron']['item'] as $idx => $cronitem) {
2208
			if($cronitem['command'] == "/usr/local/bin/checkreload.sh") {
2209
				unset($config['cron']['item'][$idx]);
2210
				break;
2211
			}
2212
		}
2213
	}
2214
}
2215
?>
(41-41/54)