Project

General

Profile

Download (69.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]['descr'] = "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "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]['descr'] =    "TCP Offload engine";
706
		$config['sysctl']['item'][18]['value'] =   "default";
707
		
708
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
709
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
710
		$config['sysctl']['item'][19]['value'] =   "default";
711

    
712
		$config['sysctl']['item'][20]['tunable'] = "hw.syscons.kbd_reboot ";
713
		$config['sysctl']['item'][20]['descr'] =    "Enables ctrl+alt+delete";
714
		$config['sysctl']['item'][20]['value'] =   "default";
715

    
716
	}
717
}
718

    
719

    
720
function upgrade_041_to_042() {
721
	global $config;
722
	if (isset($config['shaper']))
723
		unset($config['shaper']);
724
	if (isset($config['ezshaper']))
725
		unset($config['ezshaper']);
726
}
727

    
728

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

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

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

    
781

    
782
function upgrade_043_to_044() {
783
	global $config;
784

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

    
814

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

    
833

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

    
871

    
872
function upgrade_046_to_047() {
873
	global $config;
874
	/* Upgrade IPsec from tunnel to phase1/phase2 */
875

    
876
	if(is_array($config['ipsec']['tunnel'])) {
877

    
878
		$a_phase1 = array();
879
		$a_phase2 = array();
880
		$ikeid = 0;
881

    
882
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
883

    
884
			unset($ph1ent);
885
			unset($ph2ent);
886

    
887
			/*
888
				*  attempt to locate an enabled phase1
889
				*  entry that matches the peer gateway
890
				*/
891

    
892
			if (!isset($tunnel['disabled'])) {
893

    
894
				$remote_gateway = $tunnel['remote-gateway'];
895

    
896
				foreach ($a_phase1 as $ph1tmp) {
897
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
898
						$ph1ent = $ph1tmp;
899
						break;
900
					}
901
				}
902
			}
903

    
904
			/* none found, create a new one */
905

    
906
			if (!isset( $ph1ent )) {
907

    
908
				/* build new phase1 entry */
909

    
910
				$ph1ent = array();
911

    
912
				$ph1ent['ikeid'] = ++$ikeid;
913

    
914
				if (isset($tunnel['disabled']))
915
					$ph1ent['disabled'] = $tunnel['disabled'];
916

    
917
				$ph1ent['interface'] = $tunnel['interface'];
918
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
919
				$ph1ent['descr'] = $tunnel['descr'];
920

    
921
				$ph1ent['mode'] = $tunnel['p1']['mode'];
922

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

    
946
				$ph1ent['peerid_type'] = "peeraddress";
947

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

    
969
				$ph1ent['encryption-algorithm'] = $ph1alg;
970
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
971
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
972
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
973
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
974

    
975
				if (isset($tunnel['p1']['pre-shared-key']))
976
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
977
				if (isset($tunnel['p1']['cert']))
978
					$ph1ent['cert'] = $tunnel['p1']['cert'];
979
				if (isset($tunnel['p1']['peercert']))
980
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
981
				if (isset($tunnel['p1']['private-key']))
982
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
983

    
984
				$ph1ent['nat_traversal'] = "on";
985
				$ph1ent['dpd_enable'] = 1;
986
				$ph1ent['dpd_delay'] = 10;
987
				$ph1ent['dpd_maxfail'] = 5;
988

    
989
				$a_phase1[] = $ph1ent;
990
			}
991

    
992
			/* build new phase2 entry */
993

    
994
			$ph2ent = array();
995

    
996
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
997

    
998
			if (isset($tunnel['disabled']))
999
				$ph1ent['disabled'] = $tunnel['disabled'];
1000

    
1001
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1002

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

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

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

    
1029
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1030

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

    
1055
				if( !$aes_found || ($aes_count < 2))
1056
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1057
			}
1058

    
1059
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1060
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1061
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1062

    
1063
			if (isset($tunnel['pinghost']['pinghost']))
1064
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1065

    
1066
			$a_phase2[] = $ph2ent;
1067
		}
1068

    
1069
		unset($config['ipsec']['tunnel']);
1070
		$config['ipsec']['phase1'] = $a_phase1;
1071
		$config['ipsec']['phase2'] = $a_phase2;
1072
	}
1073
}
1074

    
1075

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

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

    
1137

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

    
1148
	if (!is_array($config['system']['group']))
1149
		$config['system']['group'] = array();
1150

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

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

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

    
1178
	/* reset user group information */
1179
	foreach ($config['system']['user'] as & $user) {
1180
		unset($user['groupname']);
1181
		$all['member'][] = $user['uid'];
1182
	}
1183

    
1184
	/* reset group scope information */
1185
	foreach ($config['system']['group'] as & $group)
1186
		if ($group['name'] != $g['admin_group'])
1187
		$group['scope'] = "user";
1188

    
1189
	/* insert new all group */
1190
	$groups = Array();
1191
	$groups[] = $all;
1192
	$groups = array_merge($config['system']['group'],$groups);
1193
	$config['system']['group'] = $groups;
1194
}
1195

    
1196

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

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

    
1235
	/* sync all local account information */
1236
	local_sync_accounts();
1237
}
1238

    
1239

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

    
1253
	unset($config['bridge']);
1254

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

    
1280

    
1281
function upgrade_051_to_052() {
1282
	global $config;
1283
	$config['openvpn'] = array();
1284
	if (!is_array($config['ca']))
1285
		$config['ca'] = array();
1286
	if (!is_array($config['cert']))
1287
		$config['cert'] = array();
1288

    
1289
	$vpnid = 1;
1290

    
1291
	/* openvpn server configurations */
1292
	if (is_array($config['installedpackages']['openvpnserver'])) {
1293
		$config['openvpn']['openvpn-server'] = array();
1294

    
1295
		$index = 1;
1296
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1297

    
1298
			if (!is_array($server))
1299
				continue;
1300

    
1301
			if ($server['auth_method'] == "pki") {
1302

    
1303
				/* create ca entry */
1304
				$ca = array();
1305
				$ca['refid'] = uniqid();
1306
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1307
				$ca['crt'] = $server['ca_cert'];
1308
				$config['ca'][] = $ca;
1309

    
1310
				/* create ca reference */
1311
				unset($server['ca_cert']);
1312
				$server['caref'] = $ca['refid'];
1313

    
1314
				/* create a crl entry if needed */
1315
				if (!empty($server['crl'])) {
1316
					$crl = array();
1317
					$crl['refid'] = uniqid();
1318
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1319
					$crl['caref'] = $ca['refid'];
1320
					$crl['text'] = $server['crl'];
1321
					if(!is_array($config['crl']))
1322
						$config['crl'] = array();
1323
					$config['crl'][] = $crl;
1324
					$server['crlref'] = $crl['refid'];
1325
				}
1326
				unset($server['crl']);
1327

    
1328
				/* create cert entry */
1329
				$cert = array();
1330
				$cert['refid'] = uniqid();
1331
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1332
				$cert['crt'] = $server['server_cert'];
1333
				$cert['prv'] = $server['server_key'];
1334
				$config['cert'][] = $cert;
1335

    
1336
				/* create cert reference */
1337
				unset($server['server_cert']);
1338
				unset($server['server_key']);
1339
				$server['certref'] = $cert['refid'];
1340

    
1341
				$index++;
1342
			}
1343

    
1344
			/* determine operational mode */
1345
			if ($server['auth_method'] == 'pki') {
1346
				if($server['nopool']) {
1347
					$server['mode'] = "p2p_tls";
1348
				} else {
1349
					$server['mode'] = "server_tls";
1350
				}
1351
			} else {
1352
				$server['mode'] = "p2p_shared_key";
1353
			}
1354
			unset($server['auth_method']);
1355

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

    
1392
			if (!empty($server['disable']))
1393
				$server['disable'] = true;
1394
			else
1395
				unset($server['disable']);
1396

    
1397
			/* allocate vpnid */
1398
			$server['vpnid'] = $vpnid++;
1399

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

    
1428
			$config['openvpn']['openvpn-server'][] = $server;
1429
		}
1430
		unset($config['installedpackages']['openvpnserver']);
1431
	}
1432

    
1433
	/* openvpn client configurations */
1434
	if (is_array($config['installedpackages']['openvpnclient'])) {
1435
		$config['openvpn']['openvpn-client'] = array();
1436

    
1437
		$index = 1;
1438
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1439

    
1440
			if (!is_array($client))
1441
				continue;
1442

    
1443
			if ($client['auth_method'] == "pki") {
1444

    
1445
				/* create ca entry */
1446
				$ca = array();
1447
				$ca['refid'] = uniqid();
1448
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1449
				$ca['crt'] = $client['ca_cert'];
1450
				$ca['crl'] = $client['crl'];
1451
				$config['ca'][] = $ca;
1452

    
1453
				/* create ca reference */
1454
				unset($client['ca_cert']);
1455
				unset($client['crl']);
1456
				$client['caref'] = $ca['refid'];
1457

    
1458
				/* create cert entry */
1459
				$cert = array();
1460
				$cert['refid'] = uniqid();
1461
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1462
				$cert['crt'] = $client['client_cert'];
1463
				$cert['prv'] = $client['client_key'];
1464
				$config['cert'][] = $cert;
1465

    
1466
				/* create cert reference */
1467
				unset($client['client_cert']);
1468
				unset($client['client_key']);
1469
				$client['certref'] = $cert['refid'];
1470

    
1471
				$index++;
1472
			}
1473

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

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

    
1499
			/* allocate vpnid */
1500
			$client['vpnid'] = $vpnid++;
1501

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

    
1530
			if (!empty($client['disable']))
1531
				$client['disable'] = true;
1532
			else
1533
				unset($client['disable']);
1534

    
1535
			$config['openvpn']['openvpn-client'][] = $client;
1536
		}
1537

    
1538
		unset($config['installedpackages']['openvpnclient']);
1539
	}
1540

    
1541
	/* openvpn client specific configurations */
1542
	if (is_array($config['installedpackages']['openvpncsc'])) {
1543
		$config['openvpn']['openvpn-csc'] = array();
1544

    
1545
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1546

    
1547
			if (!is_array($csc))
1548
				continue;
1549

    
1550
			/* modify configuration values */
1551
			$csc['common_name'] = $csc['commonname'];
1552
			unset($csc['commonname']);
1553
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1554
			unset($csc['ifconfig_push']);
1555
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1556
			unset($csc['dhcp_domainname']);
1557
			$csc['dns_server1'] = $csc['dhcp_dns'];
1558
			unset($csc['dhcp_dns']);
1559
			$csc['ntp_server1'] = $csc['dhcp_ntp'];
1560
			unset($csc['dhcp_ntp']);
1561
			if ($csc['dhcp_nbtdisable'])
1562
				$csc['netbios_enable'] = false;
1563
			else
1564
				$csc['netbios_enable'] = "yes";
1565
			unset($csc['dhcp_nbtdisable']);
1566
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1567
			unset($csc['dhcp_nbttype']);
1568
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1569
			unset($csc['dhcp_nbtscope']);
1570
			$csc['nbdd_server1'] = $csc['dhcp_nbdd'];
1571
			unset($csc['dhcp_nbdd']);
1572
			$csc['wins_server1'] = $csc['dhcp_wins'];
1573
			unset($csc['dhcp_wins']);
1574

    
1575
			if (!empty($csc['disable']))
1576
				$csc['disable'] = true;
1577
			else
1578
				unset($csc['disable']);
1579

    
1580
			$config['openvpn']['openvpn-csc'][] = $csc;
1581
		}
1582

    
1583
		unset($config['installedpackages']['openvpncsc']);
1584
	}
1585

    
1586
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1587
		count($config['openvpn']['openvpn-client']) > 0) {
1588
		$ovpnrule = array();
1589
                $ovpnrule['type'] = "pass";
1590
                $ovpnrule['interface'] = "openvpn";
1591
                $ovpnrule['statetype'] = "keep state";
1592
                $ovpnrule['source'] = array();
1593
                $ovpnrule['destination'] = array();
1594
                $ovpnrule['source']['any'] = true;
1595
                $ovpnrule['destination']['any'] = true;
1596
                $ovpnrule['descr'] = "Auto added OpenVPN rule from config upgrade.";
1597
		$config['filter']['rule'][] = $ovpnrule;
1598
	}
1599

    
1600
	/*
1601
		* FIXME: hack to keep things working with no installedpackages
1602
		* or carp array in the configuration data.
1603
		*/
1604
	if (!is_array($config['installedpackages']))
1605
		$config['installedpackages'] = array();
1606
	if (!is_array($config['installedpackages']['carp']))
1607
		$config['installedpackages']['carp'] = array();
1608

    
1609
}
1610

    
1611

    
1612
function upgrade_052_to_053() {
1613
	global $config;
1614
	if (!is_array($config['ca']))
1615
		$config['ca'] = array();
1616
	if (!is_array($config['cert']))
1617
		$config['cert'] = array();
1618

    
1619
	/* migrate advanced admin page webui ssl to certifcate mngr */
1620
	if ($config['system']['webgui']['certificate'] &&
1621
	$config['system']['webgui']['private-key']) {
1622

    
1623
		/* create cert entry */
1624
		$cert = array();
1625
		$cert['refid'] = uniqid();
1626
		$cert['descr'] = "webConfigurator SSL Certificate";
1627
		$cert['crt'] = $config['system']['webgui']['certificate'];
1628
		$cert['prv'] = $config['system']['webgui']['private-key'];
1629
		$config['cert'][] = $cert;
1630

    
1631
		/* create cert reference */
1632
		unset($config['system']['webgui']['certificate']);
1633
		unset($config['system']['webgui']['private-key']);
1634
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1635
	}
1636

    
1637
	/* migrate advanced admin page ssh keys to user manager */
1638
	if ($config['system']['ssh']['authorizedkeys']) {
1639
		$admin_user =& getUserEntryByUID(0);
1640
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1641
		unset($config['system']['ssh']['authorizedkeys']);
1642
	}
1643
}
1644

    
1645

    
1646
function upgrade_053_to_054() {
1647
	global $config;
1648
	if(is_array($config['load_balancer']['lbpool'])) {
1649
		$lbpool_arr = $config['load_balancer']['lbpool'];
1650
		$lbpool_srv_arr = array();
1651
		$gateway_group_arr = array();
1652
		$gateways = return_gateways_array();
1653
		if (! is_array($config['gateways']['gateway_item']))
1654
			$config['gateways']['gateway_item'] = array();
1655

    
1656
		$a_gateways =& $config['gateways']['gateway_item'];
1657
		foreach($lbpool_arr as $lbpool) {
1658
			if($lbpool['type'] == "gateway") {
1659
				$gateway_group['name'] = $lbpool['name'];
1660
				$gateway_group['descr'] = $lbpool['descr'];
1661
				$gateway_group['trigger'] = "down";
1662
				$gateway_group['item'] = array();
1663
				$i = 0;
1664
				foreach($lbpool['servers'] as $member) {
1665
					$split = split("\|", $member);
1666
					$interface = $split[0];
1667
					$monitor = $split[1];
1668
					/* on static upgraded configuration we automatically prepend GW_ */
1669
					$static_name = "GW_" . strtoupper($interface);
1670
					if(is_ipaddr($monitor))
1671
						foreach ($a_gateways as & $gw)
1672
							if ($gw['name'] == $static_name)
1673
								$gw['monitor'] = $monitor;
1674

    
1675
					/* on failover increment tier. Else always assign 1 */
1676
					if($lbpool['behaviour'] == "failover") {
1677
						$i++;
1678
					} else {
1679
						$i = 1;
1680
					}
1681
					$gateway_group['item'][] = "$interface|$i";
1682
				}
1683
				$gateway_group_arr[] = $gateway_group;
1684
			} else {
1685
				$lbpool_srv_arr[] = $lbpool;
1686
			}
1687
		}
1688
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1689
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1690
	}
1691
	// Unset lbpool if we no longer have any server pools
1692
	if (count($lbpool_srv_arr) == 0) {
1693
		if(empty($config['load_balancer'])) {
1694
			unset($config['load_balancer']);
1695
		} else {
1696
			unset($config['load_balancer']['lbpool']);
1697
		}
1698
	} else {
1699
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1700
	}
1701
	// Only set the gateway group array if we converted any
1702
	if (count($gateway_group_arr) != 0) {
1703
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1704
	}
1705
}
1706

    
1707

    
1708
function upgrade_054_to_055() {
1709
	global $config;
1710
	global $g;
1711

    
1712
	/* RRD files changed for quality, traffic and packets graphs */
1713
	//ini_set("max_execution_time", "1800");
1714
	/* convert traffic RRD file */
1715
	global $parsedcfg, $listtags;
1716
	$listtags = array("ds", "v", "rra", "row");
1717

    
1718
	$rrddbpath = "/var/db/rrd/";
1719
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1720

    
1721
	$rrdinterval = 60;
1722
	$valid = $rrdinterval * 2;
1723

    
1724
	/* Asume GigE for now */
1725
	$downstream = 125000000;
1726
	$upstream = 125000000;
1727

    
1728
	/* build a list of quality databases */
1729
	/* roundtrip has become delay */
1730
	function divide_delay($delayval) {
1731
		$delayval = floatval($delayval);
1732
		$delayval = ($delayval / 1000);
1733
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1734
		return $delayval;
1735
	}
1736
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1737
	$databases = array();
1738
	if (!file_exists($rrddbpath))
1739
		@mkdir($rrddbpath);
1740
	chdir($rrddbpath);
1741
	$databases = glob("*-quality.rrd");
1742
	rsort($databases);
1743
	foreach($databases as $database) {
1744
		$xmldump = "{$database}.old.xml";
1745
		$xmldumpnew = "{$database}.new.xml";
1746

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

    
1751
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
1752
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1753
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1754
		$rrdold = $rrdold['rrd'];
1755

    
1756
		$i = 0;
1757
		foreach($rrdold['rra'] as $rra) {
1758
			$l = 0;
1759
			foreach($rra['database']['row'] as $row) {
1760
				$vnew = divide_delay($row['v'][1]);
1761
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1762
				$l++;
1763
			}
1764
			$i++;
1765
		}
1766

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

    
1771
	}
1772
	/* let apinger recreate required files */
1773
	setup_gateways_monitor();
1774

    
1775
	/* build a list of traffic and packets databases */
1776
	$databases = array();
1777
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
1778
	rsort($databases);
1779
	foreach($databases as $database) {
1780
		$databasetmp = "{$database}.tmp";
1781
		$xmldump = "{$database}.old.xml";
1782
		$xmldumptmp = "{$database}.tmp.xml";
1783
		$xmldumpnew = "{$database}.new.xml";
1784

    
1785
		if ($g['booting'])
1786
			echo "Migrate RRD database {$database} to new format \n";
1787
		/* rename DS source */
1788
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1789
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1790

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

    
1794
		/* create new rrd database file */
1795
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
1796
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
1797
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
1798
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
1799
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
1800
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
1801
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
1802
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
1803
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
1804

    
1805
		create_new_rrd("$rrdcreate");
1806
		/* create temporary xml from new RRD */
1807
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
1808

    
1809
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1810
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1811
		$rrdold = $rrdold['rrd'];
1812

    
1813
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
1814
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
1815
		$rrdnew = $rrdnew['rrd'];
1816

    
1817
		/* remove any MAX RRA's. Not needed for traffic. */
1818
		$i = 0;
1819
		foreach ($rrdold['rra'] as $rra) {
1820
			if(trim($rra['cf']) == "MAX") {
1821
				unset($rrdold['rra'][$i]);
1822
			}
1823
			$i++;
1824
		}
1825

    
1826
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
1827
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
1828
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1829
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1830

    
1831
	}
1832
	enable_rrd_graphing();
1833
	if ($g['booting'])
1834
		echo "Updating configuration...";
1835
}
1836

    
1837

    
1838
function upgrade_055_to_056() {
1839
	global $config;
1840

    
1841
	if (!is_array($config['ca']))
1842
		$config['ca'] = array();
1843
	if (!is_array($config['cert']))
1844
		$config['cert'] = array();
1845

    
1846
	/* migrate ipsec ca's to cert manager */
1847
	if (is_array($config['ipsec']['cacert'])) {
1848
		foreach($config['ipsec']['cacert'] as & $cacert) {
1849
			$ca = array();
1850
			$ca['refid'] = uniqid();
1851
			if (is_array($cacert['cert']))
1852
				$ca['crt'] = $cacert['cert'][0];
1853
			else
1854
				$ca['crt'] = $cacert['cert'];
1855
			$ca['descr'] = $cacert['ident'];
1856
			$config['ca'][] = $ca;
1857
		}
1858
		unset($config['ipsec']['cacert']);
1859
	}
1860

    
1861
	/* migrate phase1 certificates to cert manager */
1862
	if (is_array($config['ipsec']['phase1'])) {
1863
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
1864
			$cert = array();
1865
			$cert['refid'] = uniqid();
1866
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
1867
			if (is_array($ph1ent['cert']))
1868
				$cert['crt'] = $ph1ent['cert'][0];
1869
			else
1870
				$cert['crt'] = $ph1ent['cert'];
1871
			$cert['prv'] = $ph1ent['private-key'];
1872
			$config['cert'][] = $cert;
1873
			$ph1ent['certref'] = $cert['refid'];
1874
			if ($ph1ent['cert'])
1875
				unset($ph1ent['cert']);
1876
			if ($ph1ent['private-key'])
1877
				unset($ph1ent['private-key']);
1878
			if ($ph1ent['peercert'])
1879
				unset($ph1ent['peercert']);
1880
		}
1881
	}
1882
}
1883

    
1884

    
1885
function upgrade_056_to_057() {
1886
	global $config;
1887
	if (!is_array($config['system']['user']))
1888
		$config['system']['user'] = array();
1889
	/* migrate captivate portal to user manager */
1890
	if (is_array($config['captiveportal']['user'])) {
1891
		foreach($config['captiveportal']['user'] as $user) {
1892
			// avoid user conflicts
1893
			$found = false;
1894
			foreach ($config['system']['user'] as $userent) {
1895
				if ($userent['name'] == $user['name']) {
1896
					$found = true;
1897
					break;
1898
				}
1899
			}
1900
			if ($found)
1901
				continue;
1902
			$user['scope'] = "user";
1903
			if (isset($user['expirationdate'])) {
1904
				$user['expires'] = $user['expirationdate'];
1905
				unset($user['expirationdate']);
1906
			}
1907
			if (isset($user['password'])) {
1908
				$user['md5-hash'] = $user['password'];
1909
				unset($user['password']);
1910
			}
1911
			$user['uid'] = $config['system']['nextuid']++;
1912
			$config['system']['user'][] = $user;
1913
		}
1914
		unset($config['captiveportal']['user']);
1915
	}
1916
}
1917

    
1918
function upgrade_057_to_058() {
1919
	global $config;
1920
	/* set all phase2 entries to tunnel mode */
1921
	if (is_array($config['ipsec']['phase2']))
1922
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1923
			$ph2ent['mode'] = 'tunnel';
1924
}
1925

    
1926
function upgrade_058_to_059() {
1927
	global $config;
1928

    
1929
	if (is_array($config['schedules']['schedule'])) {
1930
		foreach ($config['schedules']['schedule'] as & $schedl)
1931
			$schedl['schedlabel'] = uniqid();
1932
	}
1933
}
1934

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

    
1965
function upgrade_060_to_061() {
1966
	global $config;
1967

    
1968
	if (is_array($config['interfaces']['wan']))
1969
		$config['interfaces']['wan']['enable'] = true;
1970
	if (is_array($config['interfaces']['lan']))
1971
		$config['interfaces']['lan']['enable'] = true;
1972
}
1973

    
1974
function upgrade_061_to_062() {
1975
	global $config;
1976

    
1977
	/* Convert NAT port forwarding rules */
1978
	if (is_array($config['nat']['rule'])) {
1979
		$a_nat = &$config['nat']['rule'];
1980

    
1981
		foreach ($a_nat as &$natent) {
1982
			$natent['disabled'] = false;
1983
			$natent['nordr']    = false;
1984

    
1985
			$natent['source'] = array(
1986
				"not"     => false,
1987
				"any"     => true,
1988
				"port"    => ""
1989
			);
1990

    
1991
			$natent['destination'] = array(
1992
				"not"     => false,
1993
				"address" => $natent['external-address'],
1994
				"port"    => $natent['external-port']
1995
			);
1996

    
1997
			if (empty($natent['destination']['address'])) {
1998
				unset($natent['destination']['address']);
1999
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2000
			} else if ($natent['destination']['address'] == 'any') {
2001
				unset($natent['destination']['address']);
2002
				$natent['destination']['any'] = true;
2003
			}
2004

    
2005
			unset($natent['external-address']);
2006
			unset($natent['external-port']);
2007
		}
2008

    
2009
		unset($natent);
2010
	}
2011
}
2012

    
2013
function upgrade_062_to_063() {
2014
	/* Upgrade legacy Themes to the new pfsense_ng */
2015
	global $config;
2016

    
2017
	switch($config['theme']) {
2018
		case "nervecenter":
2019
			$config['theme'] = "pfsense_ng";
2020
			break;
2021
	}
2022
	
2023
}
2024

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

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

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

    
2116
function upgrade_064_to_065() {
2117
	/* Disable TSO and LRO in upgraded configs */
2118
	global $config;
2119
	$config['system']['disablesegmentationoffloading'] = true;
2120
	$config['system']['disablelargereceiveoffloading'] = true;
2121
}
2122

    
2123
function upgrade_065_to_066() {
2124
	global $config;
2125

    
2126
	$dhcrelaycfg =& $config['dhcrelay'];
2127

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

    
2144
function upgrade_066_to_067() {
2145
	global $config;
2146
	if (isset($config['system']['ca'])) {
2147
		$config['ca'] = $config['system']['ca'];
2148
	}
2149
	if (isset($config['system']['cert'])) {
2150
		$config['cert'] = $config['system']['cert'];
2151
	}
2152
}
2153

    
2154
function upgrade_067_to_068() {
2155
	global $config;
2156

    
2157
	if (!empty($config['pppoe'])) {
2158
		$config['pppoes'] = array();
2159
		$config['pppoes']['pppoe'] = array();
2160
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2161

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

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

    
2201
function upgrade_069_to_070() {
2202
	global $config;
2203

    
2204
	/* Convert NAT 1:1 rules */
2205
	if (is_array($config['nat']['onetoone'])) {
2206
		$a_nat = &$config['nat']['onetoone'];
2207

    
2208
		foreach ($a_nat as &$natent) {
2209
			if ($natent['subnet'] == 32)
2210
				$natent['source'] = array("address" => $natent['internal']);
2211
			else
2212
				$natent['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2213

    
2214
			$natent['destination'] = array("any" => true);
2215

    
2216
			unset($natent['internal']);
2217
			unset($natent['subnet']);
2218
		}
2219

    
2220
		unset($natent);
2221
	}
2222
}
2223

    
2224
function upgrade_070_to_071() {
2225
	global $config;
2226

    
2227
	if (is_array($config['cron']['item'])) {
2228
		foreach($config['cron']['item'] as $idx => $cronitem) {
2229
			if(stristr($cronitem['command'], "checkreload.sh")) {
2230
				unset($config['cron']['item'][$idx]);
2231
				break;
2232
			}
2233
		}
2234
	}
2235
}
2236

    
2237
function rename_field(& $section, $oldname, $newname) {
2238
	if (is_array($section)) {
2239
		foreach($section as & $item) {
2240
			if (!empty($item[$oldname]))
2241
				$item[$newname] = $item[$oldname];
2242
			if (isset($item[$oldname]))
2243
				unset($item[$oldname]);
2244
		}
2245
	}
2246
}
2247

    
2248
function upgrade_071_to_072() {
2249
	global $config;
2250
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2251
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2252
}
2253

    
2254
function upgrade_072_to_073() {
2255
	global $config;
2256
	if (!is_array($config['load_balancer']))
2257
		return;
2258
	if (is_array($config['load_balancer']['monitor_type']))
2259
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2260
	if (is_array($config['load_balancer']['lbpool']))
2261
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2262
	if (is_array($config['load_balancer']['lbaction']))
2263
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2264
	if (is_array($config['load_balancer']['lbprotocol']))
2265
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2266
	if (is_array($config['load_balancer']['virtual_server']))
2267
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2268
}
2269

    
2270
function upgrade_073_to_074() {
2271
	global $config;
2272
	rename_field($config['system']['user'], 'fullname', 'descr');
2273
}
2274

    
2275
function upgrade_074_to_075() {
2276
	global $config;
2277
	if (is_array($config['ca']))
2278
		rename_field($config['ca'], 'name', 'descr');
2279
	if (is_array($config['cert']))
2280
		rename_field($config['cert'], 'name', 'descr');
2281
	if (is_array($config['crl']))
2282
		rename_field($config['crl'], 'name', 'descr');
2283
}
2284

    
2285
?>
(41-41/54)