Project

General

Profile

Download (73.7 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]['priv'] = "page-all";
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
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
717
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
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
		$i = 0;
824
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
825
			/* Make sure to update the interfaces section with the right name */
826
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
827
			foreach($iflist as $ifname) {
828
				if($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
829
					$config['interfaces'][$ifname]['if'] = $vlan_name;
830
					continue;
831
				}
832
			}
833
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
834
			$i++;			
835
		}
836
	}
837
}
838

    
839

    
840
function upgrade_045_to_046() {
841
	global $config;
842
	/* Upgrade load balancer from slb to relayd */
843
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
844
		$vs_a = &$config['load_balancer']['virtual_server'];
845
		$pool_a = &$config['load_balancer']['lbpool'];
846
		$pools = array();
847
		/* Index pools by name */
848
		if(is_array($pool_a)) {
849
			for ($i = 0; isset($pool_a[$i]); $i++) {
850
				if($pool_a[$i]['type'] == "server") {
851
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
852
				}
853
			}
854
		}
855
		/* Convert sitedown entries to pools and re-attach */
856
		for ($i = 0; isset($vs_a[$i]); $i++) {
857
			if (isset($vs_a[$i]['sitedown'])) {
858
				$pool = array();
859
				$pool['type'] = 'server';
860
				$pool['behaviour'] = 'balance';
861
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
862
				$pool['descr'] = "Sitedown pool for VS: {$vs_a[$i]['name']}";
863
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
864
				$pool['servers'] = array();
865
				$pool['servers'][] = $vs_a[$i]['sitedown'];
866
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
867
				$pool_a[] = $pool;
868
				$vs_a[$i]['sitedown'] = $pool['name'];
869
			}
870
		}
871
	}
872
	if(count($config['load_balancer']) == 0) {
873
		unset($config['load_balancer']);
874
	}
875
	mwexec('/usr/sbin/pw groupadd -n _relayd -g 913');
876
	mwexec('/usr/sbin/pw useradd -n _relayd -c "Relay Daemon" -d /var/empty -s /usr/sbin/nologin -u 913 -g 913');
877
}
878

    
879

    
880
function upgrade_046_to_047() {
881
	global $config;
882
	/* Upgrade IPsec from tunnel to phase1/phase2 */
883

    
884
	if(is_array($config['ipsec']['tunnel'])) {
885

    
886
		$a_phase1 = array();
887
		$a_phase2 = array();
888
		$ikeid = 0;
889

    
890
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
891

    
892
			unset($ph1ent);
893
			unset($ph2ent);
894

    
895
			/*
896
				*  attempt to locate an enabled phase1
897
				*  entry that matches the peer gateway
898
				*/
899

    
900
			if (!isset($tunnel['disabled'])) {
901

    
902
				$remote_gateway = $tunnel['remote-gateway'];
903

    
904
				foreach ($a_phase1 as $ph1tmp) {
905
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
906
						$ph1ent = $ph1tmp;
907
						break;
908
					}
909
				}
910
			}
911

    
912
			/* none found, create a new one */
913

    
914
			if (!isset( $ph1ent )) {
915

    
916
				/* build new phase1 entry */
917

    
918
				$ph1ent = array();
919

    
920
				$ph1ent['ikeid'] = ++$ikeid;
921

    
922
				if (isset($tunnel['disabled']))
923
					$ph1ent['disabled'] = $tunnel['disabled'];
924

    
925
				/* convert to the new vip[$vhid] name */
926
				if(preg_match("/^carp/", $tunnel['interface'])) {
927
					$carpid = str_replace("carp", "", $tunnel['interface']);
928
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
929
				}
930
				$ph1ent['interface'] = $tunnel['interface'];
931
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
932
				$ph1ent['descr'] = $tunnel['descr'];
933

    
934
				$ph1ent['mode'] = $tunnel['p1']['mode'];
935

    
936
				if (isset($tunnel['p1']['myident']['myaddress']))
937
					$ph1ent['myid_type'] = "myaddress";
938
				if (isset($tunnel['p1']['myident']['address'])) {
939
					$ph1ent['myid_type'] = "address";
940
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
941
				}
942
				if (isset($tunnel['p1']['myident']['fqdn'])) {
943
					$ph1ent['myid_type'] = "fqdn";
944
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
945
				}
946
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
947
					$ph1ent['myid_type'] = "user_fqdn";
948
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
949
				}
950
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
951
					$ph1ent['myid_type'] = "asn1dn";
952
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
953
				}
954
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
955
					$ph1ent['myid_type'] = "dyn_dns";
956
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
957
				}
958

    
959
				$ph1ent['peerid_type'] = "peeraddress";
960

    
961
				switch ($tunnel['p1']['encryption-algorithm']) {
962
					case "des":
963
					$ph1alg = array( 'name' => 'des' );
964
					break;
965
					case "3des":
966
					$ph1alg = array( 'name' => '3des' );
967
					break;
968
					case "blowfish":
969
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
970
					break;
971
					case "cast128":
972
					$ph1alg = array( 'name' => 'cast128' );
973
					break;
974
					case "rijndael":
975
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
976
					break;
977
					case "rijndael 256":
978
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
979
					break;
980
				}
981

    
982
				$ph1ent['encryption-algorithm'] = $ph1alg;
983
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
984
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
985
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
986
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
987

    
988
				if (isset($tunnel['p1']['pre-shared-key']))
989
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
990
				if (isset($tunnel['p1']['cert']))
991
					$ph1ent['cert'] = $tunnel['p1']['cert'];
992
				if (isset($tunnel['p1']['peercert']))
993
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
994
				if (isset($tunnel['p1']['private-key']))
995
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
996

    
997
				$ph1ent['nat_traversal'] = "on";
998
				$ph1ent['dpd_enable'] = 1;
999
				$ph1ent['dpd_delay'] = 10;
1000
				$ph1ent['dpd_maxfail'] = 5;
1001

    
1002
				$a_phase1[] = $ph1ent;
1003
			}
1004

    
1005
			/* build new phase2 entry */
1006

    
1007
			$ph2ent = array();
1008

    
1009
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1010

    
1011
			if (isset($tunnel['disabled']))
1012
				$ph1ent['disabled'] = $tunnel['disabled'];
1013

    
1014
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1015

    
1016
			$type = "lan";
1017
			if ($tunnel['local-subnet']['network'])
1018
				$type = $tunnel['local-subnet']['network'];
1019
			if ($tunnel['local-subnet']['address']) {
1020
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1021
				if (is_null($netbits))
1022
					$type = "address";
1023
				else
1024
					$type = "network";
1025
			}
1026

    
1027
			switch ($type) {
1028
				case "address":
1029
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1030
				break;
1031
				case "network":
1032
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1033
				break;
1034
				default:
1035
				$ph2ent['localid'] = array('type' => $type);
1036
				break;
1037
			}
1038

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

    
1042
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1043

    
1044
			$aes_count = 0;
1045
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1046
				$aes_found = false;
1047
				switch ($tunalg) {
1048
					case "des":
1049
					$ph2alg = array( 'name' => 'des' );
1050
					break;
1051
					case "3des":
1052
					$ph2alg = array( 'name' => '3des' );
1053
					break;
1054
					case "blowfish":
1055
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1056
					break;
1057
					case "cast128":
1058
					$ph2alg = array( 'name' => 'cast128' );
1059
					break;
1060
					case "rijndael":
1061
					case "rijndael 256":
1062
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1063
					$aes_found = true;
1064
					$aes_count++;
1065
					break;
1066
				}
1067

    
1068
				if( !$aes_found || ($aes_count < 2))
1069
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1070
			}
1071

    
1072
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1073
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1074
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1075

    
1076
			if (isset($tunnel['pinghost']['pinghost']))
1077
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1078

    
1079
			$a_phase2[] = $ph2ent;
1080
		}
1081

    
1082
		unset($config['ipsec']['tunnel']);
1083
		$config['ipsec']['phase1'] = $a_phase1;
1084
		$config['ipsec']['phase2'] = $a_phase2;
1085
	}
1086
}
1087

    
1088

    
1089
function upgrade_047_to_048() {
1090
	global $config;
1091
	if (!empty($config['dyndns'])) {
1092
		$config['dyndnses'] = array();
1093
		$config['dyndnses']['dyndns'] = array();
1094
		if(isset($config['dyndns'][0]['host'])) {
1095
			$tempdyn = array();
1096
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1097
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1098
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1099
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1100
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1101
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1102
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];		
1103
			$tempdyn['interface'] = "wan";
1104
			$tempdyn['descr'] = "Upgraded Dyndns {$tempdyn['type']}";
1105
			$config['dyndnses']['dyndns'][] = $tempdyn;
1106
		}
1107
		unset($config['dyndns']);
1108
	}		
1109
	if (!empty($config['dnsupdate'])) {
1110
		$pconfig = $config['dnsupdate'][0];
1111
		if (!$pconfig['ttl'])
1112
			$pconfig['ttl'] = 60;
1113
		if (!$pconfig['keytype'])
1114
			$pconfig['keytype'] = "zone";
1115
		$pconfig['interface'] = "wan";
1116
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1117
		unset($config['dnsupdate']);
1118
	}
1119

    
1120
	if (is_array($config['pppoe'])) {
1121
		$pconfig = array();
1122
		$pconfig['username'] = $config['pppoe']['username'];
1123
		$pconfig['password'] = $config['pppoe']['password'];
1124
		$pconfig['provider'] = $config['pppoe']['provider'];
1125
		$pconfig['ondemand'] = isset($config['pppoe']['ondemand']);
1126
		$pconfig['timeout'] = $config['pppoe']['timeout'];
1127
		unset($config['pppoe']);
1128
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1129
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1130
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1131
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1132
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1133
	}
1134
	if (is_array($config['pptp'])) {
1135
		$pconfig = array();
1136
		$pconfig['username'] = $config['pptp']['username'];
1137
		$pconfig['password'] = $config['pptp']['password'];
1138
		$pconfig['provider'] = $config['pptp']['provider'];
1139
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1140
		$pconfig['timeout'] = $config['pptp']['timeout'];
1141
		unset($config['pptp']);
1142
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1143
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1144
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1145
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1146
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1147
	}
1148
}
1149

    
1150

    
1151
function upgrade_048_to_049() {
1152
	global $config;
1153
	/* setup new all users group */
1154
	$all = array();
1155
	$all['name'] = "all";
1156
	$all['description'] = "All Users";
1157
	$all['scope'] = "system";
1158
	$all['gid'] = 1998;
1159
	$all['member'] = array();
1160

    
1161
	if (!is_array($config['system']['group']))
1162
		$config['system']['group'] = array();
1163

    
1164
	/* work around broken uid assignments */
1165
	$config['system']['nextuid'] = 2000;
1166
	foreach ($config['system']['user'] as & $user) {
1167
		if (isset($user['uid']) && !$user['uid'])
1168
			continue;
1169
		$user['uid'] = $config['system']['nextuid']++;
1170
	}
1171

    
1172
	/* work around broken gid assignments */
1173
	$config['system']['nextgid'] = 2000;
1174
	foreach ($config['system']['group'] as & $group) {
1175
		if ($group['name'] == $g['admin_group'])
1176
			$group['gid'] = 1999;
1177
		else
1178
			$group['gid'] = $config['system']['nextgid']++;
1179
	}
1180

    
1181
	/* build group membership information */
1182
	foreach ($config['system']['group'] as & $group) {
1183
		$group['member'] = array();
1184
		foreach ($config['system']['user'] as & $user) {
1185
			$groupnames = explode(",", $user['groupname']);
1186
			if (in_array($group['name'],$groupnames))
1187
				$group['member'][] = $user['uid'];
1188
		}
1189
	}
1190

    
1191
	/* reset user group information */
1192
	foreach ($config['system']['user'] as & $user) {
1193
		unset($user['groupname']);
1194
		$all['member'][] = $user['uid'];
1195
	}
1196

    
1197
	/* reset group scope information */
1198
	foreach ($config['system']['group'] as & $group)
1199
		if ($group['name'] != $g['admin_group'])
1200
		$group['scope'] = "user";
1201

    
1202
	/* insert new all group */
1203
	$groups = Array();
1204
	$groups[] = $all;
1205
	$groups = array_merge($config['system']['group'],$groups);
1206
	$config['system']['group'] = $groups;
1207
}
1208

    
1209

    
1210
function upgrade_049_to_050() {
1211
	global $config;
1212
	/* update user privileges */
1213
	foreach ($config['system']['user'] as & $user) {
1214
		$privs = array();
1215
		if (!is_array($user['priv'])) {
1216
			unset($user['priv']);
1217
			continue;
1218
		}
1219
		foreach ($user['priv'] as $priv) {
1220
			switch($priv['id']) {
1221
				case "hasshell":
1222
				$privs[] = "user-shell-access";
1223
				break;
1224
				case "copyfiles":
1225
				$privs[] = "user-copy-files";
1226
				break;
1227
			}
1228
		}
1229
		$user['priv'] = $privs;
1230
	}
1231

    
1232
	/* update group privileges */
1233
	foreach ($config['system']['group'] as & $group) {
1234
		$privs = array();
1235
		if (!is_array($group['pages'])) {
1236
			unset($group['pages']);
1237
			continue;
1238
		}
1239
		foreach ($group['pages'] as $page) {
1240
			$priv = map_page_privname($page);
1241
			if ($priv)
1242
				$privs[] = $priv;
1243
		}
1244
		unset($group['pages']);
1245
		$group['priv'] = $privs;
1246
	}
1247

    
1248
	/* sync all local account information */
1249
	local_sync_accounts();
1250
}
1251

    
1252

    
1253
function upgrade_050_to_051() {
1254
	global $config;
1255
	$pconfig = array();
1256
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1257
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1258
	$pconfig['value'] = "1";
1259
	$config['sysctl']['item'][] = $pconfig;
1260
	$pconfig = array();
1261
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1262
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1263
	$pconfig['value'] = "0";
1264
	$config['sysctl']['item'][] = $pconfig;
1265

    
1266
	unset($config['bridge']);
1267

    
1268
	$convert_bridges = false;
1269
	foreach($config['interfaces'] as $intf) {
1270
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1271
			$config['bridges'] = array();
1272
			$config['bridges']['bridged'] = array();
1273
			$convert_bridges = true;
1274
			break;
1275
		}
1276
	}
1277
	if ($convert_bridges == true) {
1278
		$i = 0;
1279
		foreach ($config['interfaces'] as $ifr => &$intf) {
1280
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1281
				$nbridge = array();
1282
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1283
				$nbridge['descr'] = "Converted bridged {$ifr}";
1284
				$nbridge['bridgeif'] = "bridge{$i}";
1285
				$config['bridges']['bridged'][] = $nbridge;
1286
				unset($intf['bridge']);
1287
				$i++;
1288
			}
1289
		}
1290
	}
1291
}
1292

    
1293

    
1294
function upgrade_051_to_052() {
1295
	global $config;
1296
	$config['openvpn'] = array();
1297
	if (!is_array($config['ca']))
1298
		$config['ca'] = array();
1299
	if (!is_array($config['cert']))
1300
		$config['cert'] = array();
1301

    
1302
	$vpnid = 1;
1303

    
1304
	/* openvpn server configurations */
1305
	if (is_array($config['installedpackages']['openvpnserver'])) {
1306
		$config['openvpn']['openvpn-server'] = array();
1307

    
1308
		$index = 1;
1309
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1310

    
1311
			if (!is_array($server))
1312
				continue;
1313

    
1314
			if ($server['auth_method'] == "pki") {
1315

    
1316
				/* create ca entry */
1317
				$ca = array();
1318
				$ca['refid'] = uniqid();
1319
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1320
				$ca['crt'] = $server['ca_cert'];
1321
				$config['ca'][] = $ca;
1322

    
1323
				/* create ca reference */
1324
				unset($server['ca_cert']);
1325
				$server['caref'] = $ca['refid'];
1326

    
1327
				/* create a crl entry if needed */
1328
				if (!empty($server['crl'])) {
1329
					$crl = array();
1330
					$crl['refid'] = uniqid();
1331
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1332
					$crl['caref'] = $ca['refid'];
1333
					$crl['text'] = $server['crl'];
1334
					if(!is_array($config['crl']))
1335
						$config['crl'] = array();
1336
					$config['crl'][] = $crl;
1337
					$server['crlref'] = $crl['refid'];
1338
				}
1339
				unset($server['crl']);
1340

    
1341
				/* create cert entry */
1342
				$cert = array();
1343
				$cert['refid'] = uniqid();
1344
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1345
				$cert['crt'] = $server['server_cert'];
1346
				$cert['prv'] = $server['server_key'];
1347
				$config['cert'][] = $cert;
1348

    
1349
				/* create cert reference */
1350
				unset($server['server_cert']);
1351
				unset($server['server_key']);
1352
				$server['certref'] = $cert['refid'];
1353

    
1354
				$index++;
1355
			}
1356

    
1357
			/* determine operational mode */
1358
			if ($server['auth_method'] == 'pki') {
1359
				if($server['nopool']) {
1360
					$server['mode'] = "p2p_tls";
1361
				} else {
1362
					$server['mode'] = "server_tls";
1363
				}
1364
			} else {
1365
				$server['mode'] = "p2p_shared_key";
1366
			}
1367
			unset($server['auth_method']);
1368

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

    
1405
			if (!empty($server['disable']))
1406
				$server['disable'] = true;
1407
			else
1408
				unset($server['disable']);
1409

    
1410
			/* allocate vpnid */
1411
			$server['vpnid'] = $vpnid++;
1412

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

    
1441
			$config['openvpn']['openvpn-server'][] = $server;
1442
		}
1443
		unset($config['installedpackages']['openvpnserver']);
1444
	}
1445

    
1446
	/* openvpn client configurations */
1447
	if (is_array($config['installedpackages']['openvpnclient'])) {
1448
		$config['openvpn']['openvpn-client'] = array();
1449

    
1450
		$index = 1;
1451
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1452

    
1453
			if (!is_array($client))
1454
				continue;
1455

    
1456
			if ($client['auth_method'] == "pki") {
1457

    
1458
				/* create ca entry */
1459
				$ca = array();
1460
				$ca['refid'] = uniqid();
1461
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1462
				$ca['crt'] = $client['ca_cert'];
1463
				$ca['crl'] = $client['crl'];
1464
				$config['ca'][] = $ca;
1465

    
1466
				/* create ca reference */
1467
				unset($client['ca_cert']);
1468
				unset($client['crl']);
1469
				$client['caref'] = $ca['refid'];
1470

    
1471
				/* create cert entry */
1472
				$cert = array();
1473
				$cert['refid'] = uniqid();
1474
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1475
				$cert['crt'] = $client['client_cert'];
1476
				$cert['prv'] = $client['client_key'];
1477
				$config['cert'][] = $cert;
1478

    
1479
				/* create cert reference */
1480
				unset($client['client_cert']);
1481
				unset($client['client_key']);
1482
				$client['certref'] = $cert['refid'];
1483

    
1484
				$index++;
1485
			}
1486

    
1487
			/* determine operational mode */
1488
			if ($client['auth_method'] == 'pki')
1489
				$client['mode'] = "p2p_tls";
1490
			else
1491
				$client['mode'] = "p2p_shared_key";
1492
			unset($client['auth_method']);
1493

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

    
1512
			/* allocate vpnid */
1513
			$client['vpnid'] = $vpnid++;
1514

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

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

    
1548
			$config['openvpn']['openvpn-client'][] = $client;
1549
		}
1550

    
1551
		unset($config['installedpackages']['openvpnclient']);
1552
	}
1553

    
1554
	/* openvpn client specific configurations */
1555
	if (is_array($config['installedpackages']['openvpncsc'])) {
1556
		$config['openvpn']['openvpn-csc'] = array();
1557

    
1558
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1559

    
1560
			if (!is_array($csc))
1561
				continue;
1562

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

    
1588
			if (!empty($csc['disable']))
1589
				$csc['disable'] = true;
1590
			else
1591
				unset($csc['disable']);
1592

    
1593
			$config['openvpn']['openvpn-csc'][] = $csc;
1594
		}
1595

    
1596
		unset($config['installedpackages']['openvpncsc']);
1597
	}
1598

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

    
1613
	/*
1614
		* FIXME: hack to keep things working with no installedpackages
1615
		* or carp array in the configuration data.
1616
		*/
1617
	if (!is_array($config['installedpackages']))
1618
		$config['installedpackages'] = array();
1619
	if (!is_array($config['installedpackages']['carp']))
1620
		$config['installedpackages']['carp'] = array();
1621

    
1622
}
1623

    
1624

    
1625
function upgrade_052_to_053() {
1626
	global $config;
1627
	if (!is_array($config['ca']))
1628
		$config['ca'] = array();
1629
	if (!is_array($config['cert']))
1630
		$config['cert'] = array();
1631

    
1632
	/* migrate advanced admin page webui ssl to certifcate mngr */
1633
	if ($config['system']['webgui']['certificate'] &&
1634
	$config['system']['webgui']['private-key']) {
1635

    
1636
		/* create cert entry */
1637
		$cert = array();
1638
		$cert['refid'] = uniqid();
1639
		$cert['descr'] = "webConfigurator SSL Certificate";
1640
		$cert['crt'] = $config['system']['webgui']['certificate'];
1641
		$cert['prv'] = $config['system']['webgui']['private-key'];
1642
		$config['cert'][] = $cert;
1643

    
1644
		/* create cert reference */
1645
		unset($config['system']['webgui']['certificate']);
1646
		unset($config['system']['webgui']['private-key']);
1647
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1648
	}
1649

    
1650
	/* migrate advanced admin page ssh keys to user manager */
1651
	if ($config['system']['ssh']['authorizedkeys']) {
1652
		$admin_user =& getUserEntryByUID(0);
1653
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1654
		unset($config['system']['ssh']['authorizedkeys']);
1655
	}
1656
}
1657

    
1658

    
1659
function upgrade_053_to_054() {
1660
	global $config;
1661
	if(is_array($config['load_balancer']['lbpool'])) {
1662
		$lbpool_arr = $config['load_balancer']['lbpool'];
1663
		$lbpool_srv_arr = array();
1664
		$gateway_group_arr = array();
1665
		$gateways = return_gateways_array();
1666
		if (! is_array($config['gateways']['gateway_item']))
1667
			$config['gateways']['gateway_item'] = array();
1668

    
1669
		$a_gateways =& $config['gateways']['gateway_item'];
1670
		foreach($lbpool_arr as $lbpool) {
1671
			if($lbpool['type'] == "gateway") {
1672
				$gateway_group['name'] = $lbpool['name'];
1673
				$gateway_group['descr'] = $lbpool['descr'];
1674
				$gateway_group['trigger'] = "down";
1675
				$gateway_group['item'] = array();
1676
				$i = 0;
1677
				foreach($lbpool['servers'] as $member) {
1678
					$split = split("\|", $member);
1679
					$interface = $split[0];
1680
					$monitor = $split[1];
1681
					/* on static upgraded configuration we automatically prepend GW_ */
1682
					$static_name = "GW_" . strtoupper($interface);
1683
					if(is_ipaddr($monitor))
1684
						foreach ($a_gateways as & $gw)
1685
							if ($gw['name'] == $static_name)
1686
								$gw['monitor'] = $monitor;
1687

    
1688
					/* on failover increment tier. Else always assign 1 */
1689
					if($lbpool['behaviour'] == "failover") {
1690
						$i++;
1691
					} else {
1692
						$i = 1;
1693
					}
1694
					$gateway_group['item'][] = "$static_name|$i";
1695
				}
1696
				$gateway_group_arr[] = $gateway_group;
1697
			} else {
1698
				$lbpool_srv_arr[] = $lbpool;
1699
			}
1700
		}
1701
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1702
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1703
	}
1704
	// Unset lbpool if we no longer have any server pools
1705
	if (count($lbpool_srv_arr) == 0) {
1706
		if(empty($config['load_balancer'])) {
1707
			unset($config['load_balancer']);
1708
		} else {
1709
			unset($config['load_balancer']['lbpool']);
1710
		}
1711
	} else {
1712
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1713
	}
1714
	// Only set the gateway group array if we converted any
1715
	if (count($gateway_group_arr) != 0) {
1716
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1717
	}
1718
}
1719

    
1720

    
1721
function upgrade_054_to_055() {
1722
	global $config;
1723
	global $g;
1724

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

    
1731
	$rrddbpath = "/var/db/rrd/";
1732
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1733

    
1734
	$rrdinterval = 60;
1735
	$valid = $rrdinterval * 2;
1736

    
1737
	/* Asume GigE for now */
1738
	$downstream = 125000000;
1739
	$upstream = 125000000;
1740

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

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

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

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

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

    
1784
	}
1785
	/* let apinger recreate required files */
1786
	setup_gateways_monitor();
1787

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

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

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

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

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

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

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

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

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

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

    
1850

    
1851
function upgrade_055_to_056() {
1852
	global $config;
1853

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

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

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

    
1897

    
1898
function upgrade_056_to_057() {
1899
	global $config;
1900
	if (!is_array($config['system']['user']))
1901
		$config['system']['user'] = array();
1902
	/* migrate captivate portal to user manager */
1903
	if (is_array($config['captiveportal']['user'])) {
1904
		foreach($config['captiveportal']['user'] as $user) {
1905
			// avoid user conflicts
1906
			$found = false;
1907
			foreach ($config['system']['user'] as $userent) {
1908
				if ($userent['name'] == $user['name']) {
1909
					$found = true;
1910
					break;
1911
				}
1912
			}
1913
			if ($found)
1914
				continue;
1915
			$user['scope'] = "user";
1916
			if (isset($user['expirationdate'])) {
1917
				$user['expires'] = $user['expirationdate'];
1918
				unset($user['expirationdate']);
1919
			}
1920
			if (isset($user['password'])) {
1921
				$user['md5-hash'] = $user['password'];
1922
				unset($user['password']);
1923
			}
1924
			$user['uid'] = $config['system']['nextuid']++;
1925
			$config['system']['user'][] = $user;
1926
		}
1927
		unset($config['captiveportal']['user']);
1928
	}
1929
}
1930

    
1931
function upgrade_057_to_058() {
1932
	global $config;
1933
	/* set all phase2 entries to tunnel mode */
1934
	if (is_array($config['ipsec']['phase2']))
1935
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1936
			$ph2ent['mode'] = 'tunnel';
1937
}
1938

    
1939
function upgrade_058_to_059() {
1940
	global $config;
1941

    
1942
	if (is_array($config['schedules']['schedule'])) {
1943
		foreach ($config['schedules']['schedule'] as & $schedl)
1944
			$schedl['schedlabel'] = uniqid();
1945
	}
1946
}
1947

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

    
1978
function upgrade_060_to_061() {
1979
	global $config;
1980

    
1981
	if (is_array($config['interfaces']['wan']))
1982
		$config['interfaces']['wan']['enable'] = true;
1983
	if (is_array($config['interfaces']['lan']))
1984
		$config['interfaces']['lan']['enable'] = true;
1985
}
1986

    
1987
function upgrade_061_to_062() {
1988
	global $config;
1989

    
1990
	/* Convert NAT port forwarding rules */
1991
	if (is_array($config['nat']['rule'])) {
1992
		$a_nat = &$config['nat']['rule'];
1993

    
1994
		foreach ($a_nat as &$natent) {
1995
			$natent['disabled'] = false;
1996
			$natent['nordr']    = false;
1997

    
1998
			$natent['source'] = array(
1999
				"not"     => false,
2000
				"any"     => true,
2001
				"port"    => ""
2002
			);
2003

    
2004
			$natent['destination'] = array(
2005
				"not"     => false,
2006
				"address" => $natent['external-address'],
2007
				"port"    => $natent['external-port']
2008
			);
2009

    
2010
			if (empty($natent['destination']['address'])) {
2011
				unset($natent['destination']['address']);
2012
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2013
			} else if ($natent['destination']['address'] == 'any') {
2014
				unset($natent['destination']['address']);
2015
				$natent['destination']['any'] = true;
2016
			}
2017

    
2018
			unset($natent['external-address']);
2019
			unset($natent['external-port']);
2020
		}
2021

    
2022
		unset($natent);
2023
	}
2024
}
2025

    
2026
function upgrade_062_to_063() {
2027
	/* Upgrade legacy Themes to the new pfsense_ng */
2028
	global $config;
2029

    
2030
	switch($config['theme']) {
2031
		case "nervecenter":
2032
			$config['theme'] = "pfsense_ng";
2033
			break;
2034
	}
2035
	
2036
}
2037

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

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

    
2106
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2107
			$j++;
2108
			
2109
			unset($ifcfg[$ifname]['pppoe_username']);
2110
			unset($ifcfg[$ifname]['pppoe_password']);
2111
			unset($ifcfg[$ifname]['provider']);
2112
			unset($ifcfg[$ifname]['ondemand']);
2113
			unset($ifcfg[$ifname]['timeout']);
2114
			unset($ifcfg[$ifname]['pppoe_reset']);
2115
			unset($ifcfg[$ifname]['pppoe_preset']);
2116
			unset($ifcfg[$ifname]['pppoe']);
2117
			unset($ifcfg[$ifname]['pptp_username']);
2118
			unset($ifcfg[$ifname]['pptp_password']);
2119
			unset($ifcfg[$ifname]['local']);
2120
			unset($ifcfg[$ifname]['subnet']);
2121
			unset($ifcfg[$ifname]['remote']);
2122
			
2123
			$a_ppps[] = $ppp;
2124
			
2125
		}
2126
	}
2127
}
2128

    
2129
function upgrade_064_to_065() {
2130
	/* Disable TSO and LRO in upgraded configs */
2131
	global $config;
2132
	$config['system']['disablesegmentationoffloading'] = true;
2133
	$config['system']['disablelargereceiveoffloading'] = true;
2134
}
2135

    
2136
function upgrade_065_to_066() {
2137
	global $config;
2138

    
2139
	$dhcrelaycfg =& $config['dhcrelay'];
2140

    
2141
        if (is_array($dhcrelaycfg)) {
2142
        	$dhcrelayifs = array();
2143
		$foundifs = false;
2144
        	/* DHCPRelay enabled on any interfaces? */
2145
                foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2146
                        if (isset($dhcrelayifconf['enable'])) {
2147
				$dhcrelayifs[] = $dhcrelayif;
2148
				unset($dhcrelaycfg['dhcrelayif']);
2149
				$foundifs = true;
2150
			}
2151
                }
2152
		if ($foundifs == true)
2153
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2154
        }
2155
}
2156

    
2157
function upgrade_066_to_067() {
2158
	global $config;
2159
	if (isset($config['system']['ca'])) {
2160
		$config['ca'] = $config['system']['ca'];
2161
	}
2162
	if (isset($config['system']['cert'])) {
2163
		$config['cert'] = $config['system']['cert'];
2164
	}
2165
}
2166

    
2167
function upgrade_067_to_068() {
2168
	global $config;
2169

    
2170
	if (!empty($config['pppoe'])) {
2171
		$config['pppoes'] = array();
2172
		$config['pppoes']['pppoe'] = array();
2173
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2174

    
2175
		if (is_array($config['pppoe']['user'])) {
2176
			$username = array(); 
2177
			foreach ($config['pppoe']['user'] as $user) {
2178
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2179
				if ($user['ip'])
2180
					$usr .= ":{$user['ip']}";
2181
				$username[] = $usr;
2182
			}
2183
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2184
		}
2185
		unset($config['pppoe']);
2186
	}
2187
}
2188

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

    
2214
function upgrade_069_to_070() {
2215
	global $config;
2216

    
2217
	/* Convert NAT 1:1 rules */
2218
	if (is_array($config['nat']['onetoone'])) {
2219
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2220
			if ($natent['subnet'] == 32)
2221
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2222
			else
2223
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2224

    
2225
			$config['nat']['onetoone'][$nidx]['destination'] = array("any" => true);
2226

    
2227
			unset($config['nat']['onetoone'][$nidx]['internal']);
2228
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2229
		}
2230

    
2231
		unset($natent);
2232
	}
2233
}
2234

    
2235
function upgrade_070_to_071() {
2236
	global $config;
2237

    
2238
	if (is_array($config['cron']['item'])) {
2239
		foreach($config['cron']['item'] as $idx => $cronitem) {
2240
			if(stristr($cronitem['command'], "checkreload.sh")) {
2241
				unset($config['cron']['item'][$idx]);
2242
				break;
2243
			}
2244
		}
2245
	}
2246
}
2247

    
2248
function rename_field(& $section, $oldname, $newname) {
2249
	if (is_array($section)) {
2250
		foreach($section as & $item) {
2251
			if (!empty($item[$oldname]))
2252
				$item[$newname] = $item[$oldname];
2253
			if (isset($item[$oldname]))
2254
				unset($item[$oldname]);
2255
		}
2256
	}
2257
}
2258

    
2259
function upgrade_071_to_072() {
2260
	global $config;
2261
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2262
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2263
}
2264

    
2265
function upgrade_072_to_073() {
2266
	global $config;
2267
	if (!is_array($config['load_balancer']))
2268
		return;
2269
	if (is_array($config['load_balancer']['monitor_type']))
2270
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2271
	if (is_array($config['load_balancer']['lbpool']))
2272
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2273
	if (is_array($config['load_balancer']['lbaction']))
2274
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2275
	if (is_array($config['load_balancer']['lbprotocol']))
2276
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2277
	if (is_array($config['load_balancer']['virtual_server']))
2278
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2279
}
2280

    
2281
function upgrade_073_to_074() {
2282
	global $config;
2283
	rename_field($config['system']['user'], 'fullname', 'descr');
2284
}
2285

    
2286
function upgrade_074_to_075() {
2287
	global $config;
2288
	if (is_array($config['ca']))
2289
		rename_field($config['ca'], 'name', 'descr');
2290
	if (is_array($config['cert']))
2291
		rename_field($config['cert'], 'name', 'descr');
2292
	if (is_array($config['crl']))
2293
		rename_field($config['crl'], 'name', 'descr');
2294
}
2295

    
2296
function upgrade_075_to_076() {
2297
	global $config;
2298
	$cron_item = array();
2299
	$cron_item['minute'] = "30";
2300
	$cron_item['hour'] = "12";
2301
	$cron_item['mday'] = "*";
2302
	$cron_item['month'] = "*";
2303
	$cron_item['wday'] = "*";
2304
	$cron_item['who'] = "root";
2305
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2306
	$config['cron']['item'][] = $cron_item;
2307
}
2308

    
2309
function upgrade_076_to_077() {
2310
	global $config;
2311
	foreach($config['filter']['rule'] as & $rule) {
2312
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2313
		$rule['protocol'] = strtolower($rule['protocol']);
2314
	}
2315
}
2316

    
2317
function upgrade_077_to_078() {
2318
	global $config;
2319
	global $g;
2320

    
2321
	/* RRD files changed for quality, traffic and packets graphs */
2322
	/* convert traffic RRD file */
2323
	global $parsedcfg, $listtags;
2324
	$listtags = array("ds", "v", "rra", "row");
2325

    
2326
	$rrddbpath = "/var/db/rrd/";
2327
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2328

    
2329
	$rrdinterval = 60;
2330
	$valid = $rrdinterval * 2;
2331

    
2332
	/* Asume GigE for now */
2333
	$downstream = 125000000;
2334
	$upstream = 125000000;
2335

    
2336
	/* build a list of traffic and packets databases */
2337
	$databases = array();
2338
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
2339
	rsort($databases);
2340
	foreach($databases as $database) {
2341
		$databasetmp = "{$database}.tmp";
2342
		$xmldump = "{$database}.old.xml";
2343
		$xmldumptmp = "{$database}.tmp.xml";
2344
		$xmldumpnew = "{$database}.new.xml";
2345

    
2346
		if ($g['booting'])
2347
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2348

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

    
2352
		/* create new rrd database file */
2353
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2354
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2355
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2356
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2357
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2358
		$rrdcreate .= "DS:inpass6:COUNTER:$valid:0:$downstream ";
2359
		$rrdcreate .= "DS:outpass6:COUNTER:$valid:0:$upstream ";
2360
		$rrdcreate .= "DS:inblock6:COUNTER:$valid:0:$downstream ";
2361
		$rrdcreate .= "DS:outblock6:COUNTER:$valid:0:$upstream ";
2362
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2363
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2364
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2365
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
2366

    
2367
		create_new_rrd("$rrdcreate");
2368
		/* create temporary xml from new RRD */
2369
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2370

    
2371
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2372
		$rrdold = xml2array($rrdoldxml, 1, "tag");
2373
		$rrdold = $rrdold['rrd'];
2374

    
2375
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
2376
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
2377
		$rrdnew = $rrdnew['rrd'];
2378

    
2379
		/* remove any MAX RRA's. Not needed for traffic. */
2380
		$i = 0;
2381
		foreach ($rrdold['rra'] as $rra) {
2382
			if(trim($rra['cf']) == "MAX") {
2383
				unset($rrdold['rra'][$i]);
2384
			}
2385
			$i++;
2386
		}
2387

    
2388
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
2389
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
2390
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
2391
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2392

    
2393
	}
2394
	enable_rrd_graphing();
2395
	if ($g['booting'])
2396
		echo "Updating configuration...";
2397
	foreach($config['filter']['rule'] as & $rule) {
2398
		if (isset($rule['protocol']) && !empty($rule['protocol']))
2399
			$rule['protocol'] = strtolower($rule['protocol']);
2400
	}
2401
}
2402

    
2403
?>
(48-48/61)