Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
193

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

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

    
205

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

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

    
224

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

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

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

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

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

    
251

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

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

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

    
274

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

    
292

    
293
function upgrade_016_to_017() {
294
	global $config;
295
	/* wipe previous shaper configuration */
296
	if (isset($config['shaper']['queue'])) {
297
		unset($config['shaper']['queue']);
298
	}
299
	if (isset($config['shaper']['rule'])) {
300
		unset($config['shaper']['rule']);
301
	}
302
	if (isset($config['interfaces']['wan']['bandwidth'])) {
303
		unset($config['interfaces']['wan']['bandwidth']);
304
	}
305
	if (isset($config['interfaces']['wan']['bandwidthtype'])) {
306
		unset($config['interfaces']['wan']['bandwidthtype']);
307
	}
308
	if (isset($config['interfaces']['lan']['bandwidth'])) {
309
		unset($config['interfaces']['lan']['bandwidth']);
310
	}
311
	if (isset($config['interfaces']['lan']['bandwidthtype'])) {
312
		unset($config['interfaces']['lan']['bandwidthtype']);
313
	}
314
	$config['shaper']['enable'] = FALSE;
315
}
316

    
317

    
318
function upgrade_017_to_018() {
319
	global $config;
320
	if(isset($config['proxyarp']) && is_array($config['proxyarp']['proxyarpnet'])) {
321
		$proxyarp = &$config['proxyarp']['proxyarpnet'];
322
		foreach($proxyarp as $arpent){
323
			$vip = array();
324
			$vip['mode'] = "proxyarp";
325
			$vip['interface'] = $arpent['interface'];
326
			$vip['descr'] = $arpent['descr'];
327
			if (isset($arpent['range'])) {
328
				$vip['range'] = $arpent['range'];
329
				$vip['type'] = "range";
330
			} else {
331
				$subnet = explode('/', $arpent['network']);
332
				$vip['subnet'] = $subnet[0];
333
				if (isset($subnet[1])) {
334
					$vip['subnet_bits'] = $subnet[1];
335
					$vip['type'] = "network";
336
				} else {
337
					$vip['subnet_bits'] = "32";
338
					$vip['type'] = "single";
339
				}
340
			}
341
			$config['virtualip']['vip'][] = $vip;
342
		}
343
		unset($config['proxyarp']);
344
	}
345
	if(isset($config['installedpackages']) && isset($config['installedpackages']['carp']) && is_array($config['installedpackages']['carp']['config'])) {
346
		$carp = &$config['installedpackages']['carp']['config'];
347
		foreach($carp as $carpent){
348
			$vip = array();
349
			$vip['mode'] = "carp";
350
			$vip['interface'] = "AUTO";
351
			$vip['descr'] = sprintf(gettext("CARP vhid %s"), $carpent['vhid']);
352
			$vip['type'] = "single";
353
			$vip['vhid'] = $carpent['vhid'];
354
			$vip['advskew'] = $carpent['advskew'];
355
			$vip['password'] = $carpent['password'];
356
			$vip['subnet'] = $carpent['ipaddress'];
357
			$vip['subnet_bits'] = $carpent['netmask'];
358
			$config['virtualip']['vip'][] = $vip;
359
		}
360
		unset($config['installedpackages']['carp']);
361
	}
362
	/* Server NAT is no longer needed */
363
	if (isset($config['nat']['servernat'])) {
364
		unset($config['nat']['servernat']);
365
	}
366

    
367
	/* enable SSH */
368
	if ($config['version'] == "1.8") {
369
		$config['system']['sshenabled'] = true;
370
	}
371
}
372

    
373

    
374
function upgrade_018_to_019() {
375
	global $config;
376
	$config['theme'] = "metallic";
377
}
378

    
379

    
380
function upgrade_019_to_020() {
381
	global $config;
382
	if(is_array($config['ipsec']['tunnel'])) {
383
		reset($config['ipsec']['tunnel']);
384
		while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
385
			/* Sanity check on required variables */
386
			/* This fixes bogus <tunnel> entries - remnant of bug #393 */
387
			if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
388
				unset($config['ipsec']['tunnel'][$tunnel]);
389
			}
390
		}
391
	}
392
}
393

    
394
function upgrade_020_to_021() {
395
	global $config;
396
	/* shaper scheduler moved */
397
	if(isset($config['system']['schedulertype'])) {
398
		$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
399
		unset($config['system']['schedulertype']);
400
	}
401
}
402

    
403

    
404
function upgrade_021_to_022() {
405
	global $config;
406
	/* move gateway to wan interface */
407
	$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
408
}
409

    
410
function upgrade_022_to_023() {
411
	global $config;
412
	if(isset($config['shaper'])) {
413
		/* wipe previous shaper configuration */
414
		unset($config['shaper']);
415
	}
416
}
417

    
418

    
419
function upgrade_023_to_024() {
420
	global $config;
421
}
422

    
423

    
424
function upgrade_024_to_025() {
425
	global $config;
426
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
427
	if (isset($config['system']['use_rrd_gateway'])) {
428
		unset($config['system']['use_rrd_gateway']);
429
	}
430
}
431

    
432

    
433
function upgrade_025_to_026() {
434
	global $config;
435
	$cron_item = array();
436
	$cron_item['minute'] = "0";
437
	$cron_item['hour'] = "*";
438
	$cron_item['mday'] = "*";
439
	$cron_item['month'] = "*";
440
	$cron_item['wday'] = "*";
441
	$cron_item['who'] = "root";
442
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
443

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

    
446
	$cron_item = array();
447
	$cron_item['minute'] = "1,31";
448
	$cron_item['hour'] = "0-5";
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 adjkerntz -a";
454

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

    
457
	$cron_item = array();
458
	$cron_item['minute'] = "1";
459
	$cron_item['hour'] = "*";
460
	$cron_item['mday'] = "1";
461
	$cron_item['month'] = "*";
462
	$cron_item['wday'] = "*";
463
	$cron_item['who'] = "root";
464
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
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 sshlockout";
476

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

    
479
	$cron_item = array();
480
	$cron_item['minute'] = "1";
481
	$cron_item['hour'] = "1";
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 /etc/rc.dyndns.update";
487

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

    
490
	$cron_item = array();
491
	$cron_item['minute'] = "*/60";
492
	$cron_item['hour'] = "*";
493
	$cron_item['mday'] = "*";
494
	$cron_item['month'] = "*";
495
	$cron_item['wday'] = "*";
496
	$cron_item['who'] = "root";
497
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 virusprot";
498

    
499
	$config['cron']['item'][] = $cron_item;
500

    
501
	$cron_item = array();
502
	$cron_item['minute'] = "*/60";
503
	$cron_item['hour'] = "*";
504
	$cron_item['mday'] = "*";
505
	$cron_item['month'] = "*";
506
	$cron_item['wday'] = "*";
507
	$cron_item['who'] = "root";
508
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -t 1800 snort2c";
509

    
510
	$config['cron']['item'][] = $cron_item;
511
}
512

    
513

    
514
function upgrade_026_to_027() {
515
	global $config;
516
}
517

    
518

    
519
function upgrade_027_to_028() {
520
	global $config;
521
}
522

    
523

    
524
function upgrade_028_to_029() {
525
	global $config;
526
	$rule_item = array();
527
	$a_filter = &$config['filter']['rule'];
528
	$rule_item['interface'] = "enc0";
529
	$rule_item['type'] = "pass";
530
	$rule_item['source']['any'] = true;
531
	$rule_item['destination']['any'] = true;
532
	$rule_item['descr'] = gettext("Permit IPsec traffic.");
533
	$rule_item['statetype'] = "keep state";
534
	$a_filter[] = $rule_item;
535
}
536

    
537

    
538
function upgrade_029_to_030() {
539
	global $config;
540
	/* enable the rrd config setting by default */
541
	$config['rrd']['enable'] = true;
542
}
543

    
544

    
545
function upgrade_030_to_031() {
546
	global $config;
547
	/* Insert upgrade code here */
548
}
549

    
550

    
551
function upgrade_031_to_032() {
552
	global $config;
553
	/* Insert upgrade code here */
554
}
555

    
556

    
557
function upgrade_032_to_033() {
558
	global $config;
559
	/* Insert upgrade code here */
560
}
561

    
562

    
563
function upgrade_033_to_034() {
564
	global $config;
565
	/* Insert upgrade code here */
566
}
567

    
568

    
569
function upgrade_034_to_035() {
570
	global $config;
571
	/* Insert upgrade code here */
572
}
573

    
574

    
575
function upgrade_035_to_036() {
576
	global $config;
577
	/* Insert upgrade code here */
578
}
579

    
580

    
581
function upgrade_036_to_037() {
582
	global $config;
583
	/* Insert upgrade code here */
584
}
585

    
586

    
587
function upgrade_037_to_038() {
588
	global $config;
589
	/* Insert upgrade code here */
590
}
591

    
592

    
593
function upgrade_038_to_039() {
594
	global $config;
595
	/* Insert upgrade code here */
596
}
597

    
598

    
599
function upgrade_039_to_040() {
600
	global $config, $g;
601
	$config['system']['webgui']['auth_method'] = "session";
602
	$config['system']['webgui']['backing_method'] = "htpasswd";
603

    
604
	if (isset($config['system']['username'])) {
605
		$config['system']['group'] = array();
606
		$config['system']['group'][0]['name'] = "admins";
607
		$config['system']['group'][0]['description'] = gettext("System Administrators");
608
		$config['system']['group'][0]['scope'] = "system";
609
		$config['system']['group'][0]['priv'] = "page-all";
610
		$config['system']['group'][0]['home'] = "index.php";
611
		$config['system']['group'][0]['gid'] = "110";
612

    
613
		$config['system']['user'] = array();
614
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
615
		$config['system']['user'][0]['descr'] = "System Administrator";
616
		$config['system']['user'][0]['scope'] = "system";
617
		$config['system']['user'][0]['groupname'] = "admins";
618
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
619
		$config['system']['user'][0]['uid'] = "0";
620
		/* Ensure that we follow what this new "admin" username should be in the session. */
621
		$_SESSION["Username"] = "{$config['system']['username']}";
622

    
623
		$config['system']['user'][0]['priv'] = array();
624
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
625
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
626
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
627
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
628
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
629
		$config['system']['user'][0]['priv'][1]['descr'] = gettext("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).");
630
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
631
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
632
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
633
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
634
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
635
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly)."), $g['product_name']);
636
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
637
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
638
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (you should associate this privilege only with one single user).");
639

    
640
		$config['system']['nextuid'] = "111";
641
		$config['system']['nextgid'] = "111";
642

    
643
		/* wipe previous auth configuration */
644
		unset($config['system']['username']);
645
		if (isset($config['system']['password'])) {
646
			unset($config['system']['password']);
647
		}
648
	}
649
}
650

    
651
function upgrade_040_to_041() {
652
	global $config;
653
	if(!$config['sysctl']) {
654
		$config['sysctl']['item'] = array();
655

    
656
		$config['sysctl']['item'][0]['tunable'] = "net.inet.tcp.blackhole";
657
		$config['sysctl']['item'][0]['descr'] =    gettext("Drop packets to closed TCP ports without returning a RST");
658
		$config['sysctl']['item'][0]['value'] =   "default";
659

    
660
		$config['sysctl']['item'][1]['tunable'] = "net.inet.udp.blackhole";
661
		$config['sysctl']['item'][1]['descr'] =    gettext("Do not send ICMP port unreachable messages for closed UDP ports");
662
		$config['sysctl']['item'][1]['value'] =   "default";
663

    
664
		$config['sysctl']['item'][2]['tunable'] = "net.inet.ip.random_id";
665
		$config['sysctl']['item'][2]['descr'] =    gettext("Randomize the ID field in IP packets (default is 0: sequential IP IDs)");
666
		$config['sysctl']['item'][2]['value'] =   "default";
667

    
668
		$config['sysctl']['item'][3]['tunable'] = "net.inet.tcp.drop_synfin";
669
		$config['sysctl']['item'][3]['descr'] =    gettext("Drop SYN-FIN packets (breaks RFC1379, but nobody uses it anyway)");
670
		$config['sysctl']['item'][3]['value'] =   "default";
671

    
672
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
673
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
674
		$config['sysctl']['item'][4]['value'] =   "default";
675

    
676
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
677
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
678
		$config['sysctl']['item'][5]['value'] =   "default";
679

    
680
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
681
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
682
		$config['sysctl']['item'][6]['value'] =   "default";
683

    
684
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
685
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
686
		$config['sysctl']['item'][7]['value'] =   "default";
687

    
688
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
689
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
690
		$config['sysctl']['item'][8]['value'] =   "default";
691

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

    
696
		$config['sysctl']['item'][10]['tunable'] = "net.inet.tcp.delayed_ack";
697
		$config['sysctl']['item'][10]['descr'] =    gettext("Do not delay ACK to try and piggyback it onto a data packet");
698
		$config['sysctl']['item'][10]['value'] =   "default";
699

    
700
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
701
		$config['sysctl']['item'][11]['descr'] =    gettext("Maximum outgoing UDP datagram size");
702
		$config['sysctl']['item'][11]['value'] =   "default";
703

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

    
708
		$config['sysctl']['item'][13]['tunable'] = "net.link.tap.user_open";
709
		$config['sysctl']['item'][13]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
710
		$config['sysctl']['item'][13]['value'] =   "default";
711

    
712
		$config['sysctl']['item'][15]['tunable'] = "kern.randompid";
713
		$config['sysctl']['item'][15]['descr'] =    gettext("Randomize PID's (see src/sys/kern/kern_fork.c: sysctl_kern_randompid())");
714
		$config['sysctl']['item'][15]['value'] =   "default";
715

    
716
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.inflight.enable";
717
		$config['sysctl']['item'][16]['descr'] =    gettext("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. ");
718
		$config['sysctl']['item'][16]['value'] =   "default";
719

    
720
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
721
		$config['sysctl']['item'][17]['descr'] =    gettext("Set ICMP Limits");
722
		$config['sysctl']['item'][17]['value'] =   "default";
723

    
724
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
725
		$config['sysctl']['item'][18]['descr'] =    gettext("TCP Offload engine");
726
		$config['sysctl']['item'][18]['value'] =   "default";
727

    
728
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
729
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
730
		$config['sysctl']['item'][19]['value'] =   "default";
731

    
732
		$config['sysctl']['item'][20]['tunable'] = "hw.syscons.kbd_reboot";
733
		$config['sysctl']['item'][20]['descr'] =    "Enables ctrl+alt+delete";
734
		$config['sysctl']['item'][20]['value'] =   "default";
735

    
736
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
737
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
738
		$config['sysctl']['item'][21]['value'] =   "default";
739

    
740
	}
741
}
742

    
743

    
744
function upgrade_041_to_042() {
745
	global $config;
746
	if (isset($config['shaper']))
747
		unset($config['shaper']);
748
	if (isset($config['ezshaper']))
749
		unset($config['ezshaper']);
750
}
751

    
752

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

    
783
		/* Update all filter rules which might reference this gateway */
784
		$j = 0;
785
		foreach($config['filter']['rule'] as $rule) {
786
			if(is_ipaddr($rule['gateway'])) {
787
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway'])
788
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
789
				else if ($rule['gateway'] == $ifname)
790
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
791
			}
792
			$j++;
793
		}
794

    
795
		/* rename old Quality RRD files in the process */
796
		$rrddbpath = "/var/db/rrd";
797
		$gwname = "GW_" . strtoupper($ifname);
798
		if(is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
799
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
800
		}
801
		$i++;
802
	}
803
}
804

    
805

    
806
function upgrade_043_to_044() {
807
	global $config;
808

    
809
	/* migrate static routes to the new gateways config */
810
	$gateways = return_gateways_array(true);
811
	$i = 0;
812
	if (is_array($config['staticroutes']['route'])) {
813
		$gwmap = array();
814
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
815
			$found = false;
816
			foreach ($gateways as $gwname => $gw) {
817
				if ($gw['gateway'] == $sroute['gateway']) {
818
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
819
					$found = true;
820
					break;
821
				}
822
			}
823
			if($gwmap[$sroute['gateway']]) {
824
				/* We already added a gateway name for this IP */
825
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
826
				$found = true;
827
			}
828

    
829
			if ($found == false) {
830
				$gateway = array();
831
				$gateway['name'] = "SROUTE{$i}";
832
				$gwmap[$sroute['gateway']] = $gateway['name'];
833
				$gateway['gateway'] = $sroute['gateway'];
834
				$gateway['interface'] = $sroute['interface'];
835
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
836
				if (!is_array($config['gateways']['gateway_item']))
837
					$config['gateways']['gateway_item'] = array();
838
				$config['gateways']['gateway_item'][] = $gateway;
839
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
840
				$i++;
841
			}
842
		}
843
	}
844
}
845

    
846

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

    
867

    
868
function upgrade_045_to_046() {
869
	global $config;
870
	/* Load up monitors that are in the default config for 2.0 but not in 1.2.3
871
		thus wouldn't be in an upgraded config. */
872
	$config['load_balancer']['monitor_type'] = array (
873
		array ( 'name' => 'ICMP',
874
			'type' => 'icmp',
875
			'descr' => 'ICMP',
876
			'options' => '',
877
		),
878
		array ( 'name' => 'TCP',
879
			'type' => 'tcp',
880
			'descr' => 'Generic TCP',
881
			'options' => '',
882
		),
883
		array ( 'name' => 'HTTP',
884
			'type' => 'http',
885
			'descr' => 'Generic HTTP',
886
			'options' =>
887
			array ( 'path' => '/',
888
				'host' => '',
889
				'code' => '200',
890
			),
891
		),
892
		array ( 'name' => 'HTTPS',
893
			'type' => 'https',
894
			'descr' => 'Generic HTTPS',
895
			'options' =>
896
			array ( 'path' => '/',
897
				'host' => '',
898
				'code' => '200',
899
			),
900
		),
901
		array ( 'name' => 'SMTP',
902
			'type' => 'send',
903
			'descr' => 'Generic SMTP',
904
			'options' =>
905
			array ( 'send' => '',
906
				'expect' => '220 *',
907
			),
908
		),
909
	);
910
	/* Upgrade load balancer from slb to relayd */
911
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
912
		$vs_a = &$config['load_balancer']['virtual_server'];
913
		$pool_a = &$config['load_balancer']['lbpool'];
914
		$pools = array();
915
		/* Index pools by name */
916
		if(is_array($pool_a)) {
917
			for ($i = 0; isset($pool_a[$i]); $i++) {
918
				if($pool_a[$i]['type'] == "server") {
919
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
920
				}
921
			}
922
		}
923
		/* Convert sitedown entries to pools and re-attach */
924
		for ($i = 0; isset($vs_a[$i]); $i++) {
925
			/* Set mode while we're here. */
926
			$vs_a[$i]['mode'] = "redirect_mode";
927
			if (isset($vs_a[$i]['sitedown'])) {
928
				$pool = array();
929
				$pool['type'] = 'server';
930
				$pool['behaviour'] = 'balance';
931
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
932
				$pool['descr'] = sprintf(gettext("Sitedown pool for VS: %s"), $vs_a[$i]['name']);
933
				if (is_array($vs_a[$i]['pool']))
934
					$vs_a[$i]['pool'] = $vs_a[$i]['pool'][0];
935
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
936
				$pool['servers'] = array();
937
				$pool['servers'][] = $vs_a[$i]['sitedown'];
938
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
939
				$pool_a[] = $pool;
940
				$vs_a[$i]['sitedown'] = $pool['name'];
941
			}
942
		}
943
	}
944
	if(count($config['load_balancer']) == 0) {
945
		unset($config['load_balancer']);
946
	}
947
	mwexec('/usr/sbin/pw groupadd -n _relayd -g 913');
948
	mwexec('/usr/sbin/pw useradd -n _relayd -c "Relay Daemon" -d /var/empty -s /usr/sbin/nologin -u 913 -g 913');
949
}
950

    
951

    
952
function upgrade_046_to_047() {
953
	global $config;
954
	/* Upgrade IPsec from tunnel to phase1/phase2 */
955

    
956
	if(is_array($config['ipsec']['tunnel'])) {
957

    
958
		$a_phase1 = array();
959
		$a_phase2 = array();
960
		$ikeid = 0;
961

    
962
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
963

    
964
			unset($ph1ent);
965
			unset($ph2ent);
966

    
967
			/*
968
				*  attempt to locate an enabled phase1
969
				*  entry that matches the peer gateway
970
				*/
971

    
972
			if (!isset($tunnel['disabled'])) {
973

    
974
				$remote_gateway = $tunnel['remote-gateway'];
975

    
976
				foreach ($a_phase1 as $ph1tmp) {
977
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
978
						$ph1ent = $ph1tmp;
979
						break;
980
					}
981
				}
982
			}
983

    
984
			/* none found, create a new one */
985

    
986
			if (!isset( $ph1ent )) {
987

    
988
				/* build new phase1 entry */
989

    
990
				$ph1ent = array();
991

    
992
				$ph1ent['ikeid'] = ++$ikeid;
993

    
994
				if (isset($tunnel['disabled']))
995
					$ph1ent['disabled'] = $tunnel['disabled'];
996

    
997
				/* convert to the new vip[$vhid] name */
998
				if(preg_match("/^carp/", $tunnel['interface'])) {
999
					$carpid = str_replace("carp", "", $tunnel['interface']);
1000
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
1001
				}
1002
				$ph1ent['interface'] = $tunnel['interface'];
1003
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
1004
				$ph1ent['descr'] = $tunnel['descr'];
1005

    
1006
				$ph1ent['mode'] = $tunnel['p1']['mode'];
1007

    
1008
				if (isset($tunnel['p1']['myident']['myaddress']))
1009
					$ph1ent['myid_type'] = "myaddress";
1010
				if (isset($tunnel['p1']['myident']['address'])) {
1011
					$ph1ent['myid_type'] = "address";
1012
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
1013
				}
1014
				if (isset($tunnel['p1']['myident']['fqdn'])) {
1015
					$ph1ent['myid_type'] = "fqdn";
1016
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
1017
				}
1018
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
1019
					$ph1ent['myid_type'] = "user_fqdn";
1020
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
1021
				}
1022
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
1023
					$ph1ent['myid_type'] = "asn1dn";
1024
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1025
				}
1026
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1027
					$ph1ent['myid_type'] = "dyn_dns";
1028
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1029
				}
1030

    
1031
				$ph1ent['peerid_type'] = "peeraddress";
1032

    
1033
				switch ($tunnel['p1']['encryption-algorithm']) {
1034
					case "des":
1035
					$ph1alg = array( 'name' => 'des' );
1036
					break;
1037
					case "3des":
1038
					$ph1alg = array( 'name' => '3des' );
1039
					break;
1040
					case "blowfish":
1041
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1042
					break;
1043
					case "cast128":
1044
					$ph1alg = array( 'name' => 'cast128' );
1045
					break;
1046
					case "rijndael":
1047
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1048
					break;
1049
					case "rijndael 256":
1050
					case "aes 256":
1051
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1052
					break;
1053
				}
1054

    
1055
				$ph1ent['encryption-algorithm'] = $ph1alg;
1056
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1057
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1058
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1059
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1060

    
1061
				if (isset($tunnel['p1']['pre-shared-key']))
1062
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1063
				if (isset($tunnel['p1']['cert']))
1064
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1065
				if (isset($tunnel['p1']['peercert']))
1066
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1067
				if (isset($tunnel['p1']['private-key']))
1068
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1069

    
1070
				$ph1ent['nat_traversal'] = "on";
1071
				$ph1ent['dpd_enable'] = 1;
1072
				$ph1ent['dpd_delay'] = 10;
1073
				$ph1ent['dpd_maxfail'] = 5;
1074

    
1075
				$a_phase1[] = $ph1ent;
1076
			}
1077

    
1078
			/* build new phase2 entry */
1079

    
1080
			$ph2ent = array();
1081

    
1082
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1083

    
1084
			if (isset($tunnel['disabled']))
1085
				$ph1ent['disabled'] = $tunnel['disabled'];
1086

    
1087
			$ph2ent['descr'] = sprintf(gettext("phase2 for %s"), $tunnel['descr']);
1088

    
1089
			$type = "lan";
1090
			if ($tunnel['local-subnet']['network'])
1091
				$type = $tunnel['local-subnet']['network'];
1092
			if ($tunnel['local-subnet']['address']) {
1093
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1094
				if (is_null($netbits))
1095
					$type = "address";
1096
				else
1097
					$type = "network";
1098
			}
1099

    
1100
			switch ($type) {
1101
				case "address":
1102
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1103
				break;
1104
				case "network":
1105
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1106
				break;
1107
				default:
1108
				$ph2ent['localid'] = array('type' => $type);
1109
				break;
1110
			}
1111

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

    
1115
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1116

    
1117
			$aes_count = 0;
1118
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1119
				$aes_found = false;
1120
				switch ($tunalg) {
1121
					case "des":
1122
					$ph2alg = array( 'name' => 'des' );
1123
					break;
1124
					case "3des":
1125
					$ph2alg = array( 'name' => '3des' );
1126
					break;
1127
					case "blowfish":
1128
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1129
					break;
1130
					case "cast128":
1131
					$ph2alg = array( 'name' => 'cast128' );
1132
					break;
1133
					case "rijndael":
1134
					case "rijndael 256":
1135
					case "aes 256":
1136
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1137
					$aes_found = true;
1138
					$aes_count++;
1139
					break;
1140
				}
1141

    
1142
				if( !$aes_found || ($aes_count < 2))
1143
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1144
			}
1145

    
1146
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1147
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1148
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1149

    
1150
			if (isset($tunnel['pinghost']['pinghost']))
1151
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1152

    
1153
			$a_phase2[] = $ph2ent;
1154
		}
1155

    
1156
		unset($config['ipsec']['tunnel']);
1157
		$config['ipsec']['phase1'] = $a_phase1;
1158
		$config['ipsec']['phase2'] = $a_phase2;
1159
	}
1160

    
1161
	/* Upgrade Mobile IPsec */
1162
	if (isset($config['ipsec']['mobileclients'])
1163
		&& is_array($config['ipsec']['mobileclients'])
1164
		&& is_array($config['ipsec']['mobileclients']['p1'])
1165
		&& is_array($config['ipsec']['mobileclients']['p2'])) {
1166

    
1167
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1168
			$config['ipsec']['client']['enable'] = true;
1169
			$config['ipsec']['client']['user_source'] = 'system';
1170
			$config['ipsec']['client']['group_source'] = 'system';
1171
		}
1172

    
1173
		$mobilecfg = $config['ipsec']['mobileclients'];
1174

    
1175
		$ph1ent = array();
1176
		$ph1ent['ikeid'] = ++$ikeid;
1177

    
1178
		if (!isset($mobilecfg['enable']))
1179
			$ph1ent['disabled'] = true;
1180

    
1181
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1182
		$ph1ent['interface'] = 'wan';
1183
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1184
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1185

    
1186
		if (isset($mobilecfg['p1']['myident']['myaddress']))
1187
			$ph1ent['myid_type'] = "myaddress";
1188
		if (isset($mobilecfg['p1']['myident']['address'])) {
1189
			$ph1ent['myid_type'] = "address";
1190
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1191
		}
1192
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1193
			$ph1ent['myid_type'] = "fqdn";
1194
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1195
		}
1196
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1197
			$ph1ent['myid_type'] = "user_fqdn";
1198
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1199
		}
1200
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1201
			$ph1ent['myid_type'] = "asn1dn";
1202
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1203
		}
1204
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1205
			$ph1ent['myid_type'] = "dyn_dns";
1206
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1207
		}
1208
		$ph1ent['peerid_type'] = "fqdn";
1209
		$ph1ent['peerid_data'] = "";
1210

    
1211
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1212
			case "des":
1213
			$ph1alg = array( 'name' => 'des' );
1214
			break;
1215
			case "3des":
1216
			$ph1alg = array( 'name' => '3des' );
1217
			break;
1218
			case "blowfish":
1219
			$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1220
			break;
1221
			case "cast128":
1222
			$ph1alg = array( 'name' => 'cast128' );
1223
			break;
1224
			case "rijndael":
1225
			$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1226
			break;
1227
			case "rijndael 256":
1228
			case "aes 256":
1229
			$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1230
			break;
1231
		}
1232

    
1233
		$ph1ent['encryption-algorithm'] = $ph1alg;
1234
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1235
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1236
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1237
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1238

    
1239
		if (isset($mobilecfg['p1']['cert']))
1240
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1241
		if (isset($mobilecfg['p1']['peercert']))
1242
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1243
		if (isset($mobilecfg['p1']['private-key']))
1244
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1245

    
1246
		$ph1ent['nat_traversal'] = "on";
1247
		$ph1ent['dpd_enable'] = 1;
1248
		$ph1ent['dpd_delay'] = 10;
1249
		$ph1ent['dpd_maxfail'] = 5;
1250
		$ph1ent['mobile'] = true;
1251

    
1252
		$ph2ent = array();
1253
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1254
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1255
		$ph2ent['localid'] = array('type' => 'none');
1256
		$ph2ent['remoteid'] = array('type' => 'mobile');
1257
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1258

    
1259
		$aes_count = 0;
1260
		foreach( $mobilecfg['p2']['encryption-algorithm-option'] as $tunalg ) {
1261
			$aes_found = false;
1262
			switch ($tunalg) {
1263
				case "des":
1264
				$ph2alg = array( 'name' => 'des' );
1265
				break;
1266
				case "3des":
1267
				$ph2alg = array( 'name' => '3des' );
1268
				break;
1269
				case "blowfish":
1270
				$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1271
				break;
1272
				case "cast128":
1273
				$ph2alg = array( 'name' => 'cast128' );
1274
				break;
1275
				case "rijndael":
1276
				case "rijndael 256":
1277
				case "aes 256":
1278
				$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1279
				$aes_found = true;
1280
				$aes_count++;
1281
				break;
1282
			}
1283

    
1284
			if( !$aes_found || ($aes_count < 2))
1285
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1286
		}
1287
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1288
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1289
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1290
		$ph2ent['mobile'] = true;
1291

    
1292
		$config['ipsec']['phase1'][] = $ph1ent;
1293
		$config['ipsec']['phase2'][] = $ph2ent;
1294
		unset($config['ipsec']['mobileclients']);
1295
	}
1296
}
1297

    
1298

    
1299
function upgrade_047_to_048() {
1300
	global $config;
1301
	if (!empty($config['dyndns'])) {
1302
		$config['dyndnses'] = array();
1303
		$config['dyndnses']['dyndns'] = array();
1304
		if(isset($config['dyndns'][0]['host'])) {
1305
			$tempdyn = array();
1306
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1307
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1308
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1309
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1310
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1311
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1312
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];
1313
			$tempdyn['interface'] = "wan";
1314
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1315
			$config['dyndnses']['dyndns'][] = $tempdyn;
1316
		}
1317
		unset($config['dyndns']);
1318
	}
1319
	if (!empty($config['dnsupdate'])) {
1320
		$pconfig = $config['dnsupdate'][0];
1321
		if (!$pconfig['ttl'])
1322
			$pconfig['ttl'] = 60;
1323
		if (!$pconfig['keytype'])
1324
			$pconfig['keytype'] = "zone";
1325
		$pconfig['interface'] = "wan";
1326
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1327
		unset($config['dnsupdate']);
1328
	}
1329

    
1330
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1331
		$pconfig = array();
1332
		$pconfig['username'] = $config['pppoe'][0]['username'];
1333
		$pconfig['password'] = $config['pppoe'][0]['password'];
1334
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1335
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1336
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1337
		unset($config['pppoe']);
1338
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1339
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1340
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1341
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1342
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1343
	}
1344
	if (is_array($config['pptp'])) {
1345
		$pconfig = array();
1346
		$pconfig['username'] = $config['pptp']['username'];
1347
		$pconfig['password'] = $config['pptp']['password'];
1348
		$pconfig['provider'] = $config['pptp']['provider'];
1349
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1350
		$pconfig['timeout'] = $config['pptp']['timeout'];
1351
		unset($config['pptp']);
1352
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1353
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1354
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1355
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1356
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1357
	}
1358
}
1359

    
1360

    
1361
function upgrade_048_to_049() {
1362
	global $config;
1363
	/* setup new all users group */
1364
	$all = array();
1365
	$all['name'] = "all";
1366
	$all['description'] = gettext("All Users");
1367
	$all['scope'] = "system";
1368
	$all['gid'] = 1998;
1369
	$all['member'] = array();
1370

    
1371
	if (!is_array($config['system']['user']))
1372
		$config['system']['user'] = array();
1373
	if (!is_array($config['system']['group']))
1374
		$config['system']['group'] = array();
1375

    
1376
	/* work around broken uid assignments */
1377
	$config['system']['nextuid'] = 2000;
1378
	foreach ($config['system']['user'] as & $user) {
1379
		if (isset($user['uid']) && !$user['uid'])
1380
			continue;
1381
		$user['uid'] = $config['system']['nextuid']++;
1382
	}
1383

    
1384
	/* work around broken gid assignments */
1385
	$config['system']['nextgid'] = 2000;
1386
	foreach ($config['system']['group'] as & $group) {
1387
		if ($group['name'] == $g['admin_group'])
1388
			$group['gid'] = 1999;
1389
		else
1390
			$group['gid'] = $config['system']['nextgid']++;
1391
	}
1392

    
1393
	/* build group membership information */
1394
	foreach ($config['system']['group'] as & $group) {
1395
		$group['member'] = array();
1396
		foreach ($config['system']['user'] as & $user) {
1397
			$groupnames = explode(",", $user['groupname']);
1398
			if (in_array($group['name'],$groupnames))
1399
				$group['member'][] = $user['uid'];
1400
		}
1401
	}
1402

    
1403
	/* reset user group information */
1404
	foreach ($config['system']['user'] as & $user) {
1405
		unset($user['groupname']);
1406
		$all['member'][] = $user['uid'];
1407
	}
1408

    
1409
	/* reset group scope information */
1410
	foreach ($config['system']['group'] as & $group)
1411
		if ($group['name'] != $g['admin_group'])
1412
		$group['scope'] = "user";
1413

    
1414
	/* insert new all group */
1415
	$groups = Array();
1416
	$groups[] = $all;
1417
	$groups = array_merge($config['system']['group'],$groups);
1418
	$config['system']['group'] = $groups;
1419
}
1420

    
1421

    
1422
function upgrade_049_to_050() {
1423
	global $config;
1424

    
1425
	if (!is_array($config['system']['user']))
1426
		$config['system']['user'] = array();
1427
	/* update user privileges */
1428
	foreach ($config['system']['user'] as & $user) {
1429
		$privs = array();
1430
		if (!is_array($user['priv'])) {
1431
			unset($user['priv']);
1432
			continue;
1433
		}
1434
		foreach ($user['priv'] as $priv) {
1435
			switch($priv['id']) {
1436
				case "hasshell":
1437
				$privs[] = "user-shell-access";
1438
				break;
1439
				case "copyfiles":
1440
				$privs[] = "user-copy-files";
1441
				break;
1442
			}
1443
		}
1444
		$user['priv'] = $privs;
1445
	}
1446

    
1447
	/* update group privileges */
1448
	foreach ($config['system']['group'] as & $group) {
1449
		$privs = array();
1450
		if (!is_array($group['pages'])) {
1451
			unset($group['pages']);
1452
			continue;
1453
		}
1454
		foreach ($group['pages'] as $page) {
1455
			$priv = map_page_privname($page);
1456
			if ($priv)
1457
				$privs[] = $priv;
1458
		}
1459
		unset($group['pages']);
1460
		$group['priv'] = $privs;
1461
	}
1462

    
1463
	/* sync all local account information */
1464
	local_sync_accounts();
1465
}
1466

    
1467

    
1468
function upgrade_050_to_051() {
1469
	global $config;
1470
	$pconfig = array();
1471
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1472
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1473
	$pconfig['value'] = "1";
1474
	$config['sysctl']['item'][] = $pconfig;
1475
	$pconfig = array();
1476
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1477
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1478
	$pconfig['value'] = "0";
1479
	$config['sysctl']['item'][] = $pconfig;
1480

    
1481
	if (isset($config['bridge'])) {
1482
		unset($config['bridge']);
1483
	}
1484

    
1485
	$convert_bridges = false;
1486
	foreach($config['interfaces'] as $intf) {
1487
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1488
			$config['bridges'] = array();
1489
			$config['bridges']['bridged'] = array();
1490
			$convert_bridges = true;
1491
			break;
1492
		}
1493
	}
1494
	if ($convert_bridges == true) {
1495
		$i = 0;
1496
		foreach ($config['interfaces'] as $ifr => &$intf) {
1497
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1498
				$nbridge = array();
1499
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1500
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1501
				$nbridge['bridgeif'] = "bridge{$i}";
1502
				$config['bridges']['bridged'][] = $nbridge;
1503
				unset($intf['bridge']);
1504
				$i++;
1505
			}
1506
		}
1507
	}
1508
}
1509

    
1510

    
1511
function upgrade_051_to_052() {
1512
	global $config;
1513
	$config['openvpn'] = array();
1514
	if (!is_array($config['ca']))
1515
		$config['ca'] = array();
1516
	if (!is_array($config['cert']))
1517
		$config['cert'] = array();
1518

    
1519
	$vpnid = 1;
1520

    
1521
	/* openvpn server configurations */
1522
	if (is_array($config['installedpackages']['openvpnserver'])) {
1523
		$config['openvpn']['openvpn-server'] = array();
1524

    
1525
		$index = 1;
1526
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1527

    
1528
			if (!is_array($server))
1529
				continue;
1530

    
1531
			if ($server['auth_method'] == "pki") {
1532

    
1533
				/* create ca entry */
1534
				$ca = array();
1535
				$ca['refid'] = uniqid();
1536
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1537
				$ca['crt'] = $server['ca_cert'];
1538
				$config['ca'][] = $ca;
1539

    
1540
				/* create ca reference */
1541
				unset($server['ca_cert']);
1542
				$server['caref'] = $ca['refid'];
1543

    
1544
				/* create a crl entry if needed */
1545
				if (!empty($server['crl'][0])) {
1546
					$crl = array();
1547
					$crl['refid'] = uniqid();
1548
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1549
					$crl['caref'] = $ca['refid'];
1550
					$crl['text'] = $server['crl'][0];
1551
					if(!is_array($config['crl']))
1552
						$config['crl'] = array();
1553
					$config['crl'][] = $crl;
1554
					$server['crlref'] = $crl['refid'];
1555
				}
1556
				unset($server['crl']);
1557

    
1558
				/* create cert entry */
1559
				$cert = array();
1560
				$cert['refid'] = uniqid();
1561
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1562
				$cert['crt'] = $server['server_cert'];
1563
				$cert['prv'] = $server['server_key'];
1564
				$config['cert'][] = $cert;
1565

    
1566
				/* create cert reference */
1567
				unset($server['server_cert']);
1568
				unset($server['server_key']);
1569
				$server['certref'] = $cert['refid'];
1570

    
1571
				$index++;
1572
			}
1573

    
1574
			/* determine operational mode */
1575
			if ($server['auth_method'] == 'pki') {
1576
				if($server['nopool']) {
1577
					$server['mode'] = "p2p_tls";
1578
				} else {
1579
					$server['mode'] = "server_tls";
1580
				}
1581
			} else {
1582
				$server['mode'] = "p2p_shared_key";
1583
			}
1584
			unset($server['auth_method']);
1585

    
1586
			/* modify configuration values */
1587
			$server['dh_length'] = 1024;
1588
			unset($server['dh_params']);
1589
			if (!$server['interface'])
1590
				$server['interface'] = 'any';
1591
			$server['tunnel_network'] = $server['addresspool'];
1592
			unset($server['addresspool']);
1593
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1594
				$server['compression'] = "on";
1595
				unset($server['use_lzo']);
1596
			}
1597
			if ($server['nopool'])
1598
				$server['pool_enable'] = false;
1599
			else
1600
				$server['pool_enable'] = "yes";
1601
			unset($server['nopool']);
1602
			$server['dns_domain'] = $server['dhcp_domainname'];
1603
			unset($server['dhcp_domainname']);
1604

    
1605
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1606
			$d=1;
1607
			foreach ($tmparr as $tmpa) {
1608
				$server["dns_server{$d}"] = $tmpa;
1609
				$d++;
1610
			}
1611
			unset($server['dhcp_dns']);
1612

    
1613
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1614
			$d=1;
1615
			foreach ($tmparr as $tmpa) {
1616
				$server["ntp_server{$d}"] = $tmpa;
1617
				$d++;
1618
			}
1619
			unset($server['dhcp_ntp']);
1620

    
1621
			if ($server['dhcp_nbtdisable'])
1622
				$server['netbios_enable'] = false;
1623
			else
1624
				$server['netbios_enable'] = "yes";
1625
			unset($server['dhcp_nbtdisable']);
1626
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1627
			unset($server['dhcp_nbttype']);
1628
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1629
			unset($server['dhcp_nbtscope']);
1630

    
1631
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1632
			$d=1;
1633
			foreach ($tmparr as $tmpa) {
1634
				$server["nbdd_server{$d}"] = $tmpa;
1635
				$d++;
1636
			}
1637
			unset($server['dhcp_nbdd']);
1638

    
1639
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1640
			$d=1;
1641
			foreach ($tmparr as $tmpa) {
1642
				$server["wins_server{$d}"] = $tmpa;
1643
				$d++;
1644
			}
1645
			unset($server['dhcp_wins']);
1646

    
1647
			if (!empty($server['disable']))
1648
				$server['disable'] = true;
1649
			else
1650
				unset($server['disable']);
1651

    
1652
			/* allocate vpnid */
1653
			$server['vpnid'] = $vpnid++;
1654

    
1655
			if (!empty($server['custom_options'])) {
1656
				$cstmopts = array();
1657
				$tmpcstmopts = explode(";", $server['custom_options']);
1658
				$assigned_if = "";
1659
				$tmpstr = "";
1660
				foreach ($tmpcstmopts as $tmpcstmopt) {
1661
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1662
					if (substr($tmpstr,0 ,6) == "devtun") {
1663
						$assigned_if = substr($tmpstr, 3);
1664
						continue;
1665
					} else if (substr($tmpstr, 0, 5) == "local") {
1666
						$localip = substr($tmpstr, 5);
1667
						$server['ipaddr'] = str_replace("\n", "", $localip);
1668
					} else
1669
						$cstmopts[] = $tmpcstmopt;
1670
				}
1671
				$server['custom_options'] = implode(";", $cstmopts);
1672
				if (!empty($assigned_if)) {
1673
					foreach ($config['interfaces'] as $iface => $cfgif) {
1674
						if ($cfgif['if'] == $assigned_if) {
1675
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1676
							break;
1677
						}
1678
					}
1679
				}
1680
			}
1681

    
1682
			$config['openvpn']['openvpn-server'][] = $server;
1683
		}
1684
		unset($config['installedpackages']['openvpnserver']);
1685
	}
1686

    
1687
	/* openvpn client configurations */
1688
	if (is_array($config['installedpackages']['openvpnclient'])) {
1689
		$config['openvpn']['openvpn-client'] = array();
1690

    
1691
		$index = 1;
1692
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1693

    
1694
			if (!is_array($client))
1695
				continue;
1696

    
1697
			if ($client['auth_method'] == "pki") {
1698

    
1699
				/* create ca entry */
1700
				$ca = array();
1701
				$ca['refid'] = uniqid();
1702
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1703
				$ca['crt'] = $client['ca_cert'];
1704
				$ca['crl'] = $client['crl'];
1705
				$config['ca'][] = $ca;
1706

    
1707
				/* create ca reference */
1708
				unset($client['ca_cert']);
1709
				unset($client['crl']);
1710
				$client['caref'] = $ca['refid'];
1711

    
1712
				/* create cert entry */
1713
				$cert = array();
1714
				$cert['refid'] = uniqid();
1715
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1716
				$cert['crt'] = $client['client_cert'];
1717
				$cert['prv'] = $client['client_key'];
1718
				$config['cert'][] = $cert;
1719

    
1720
				/* create cert reference */
1721
				unset($client['client_cert']);
1722
				unset($client['client_key']);
1723
				$client['certref'] = $cert['refid'];
1724

    
1725
				$index++;
1726
			}
1727

    
1728
			/* determine operational mode */
1729
			if ($client['auth_method'] == 'pki')
1730
				$client['mode'] = "p2p_tls";
1731
			else
1732
				$client['mode'] = "p2p_shared_key";
1733
			unset($client['auth_method']);
1734

    
1735
			/* modify configuration values */
1736
			if (!$client['interface'])
1737
				$client['interface'] = 'wan';
1738
			$client['tunnel_network'] = $client['interface_ip'];
1739
			unset($client['interface_ip']);
1740
			$client['server_addr'] = $client['serveraddr'];
1741
			unset($client['serveraddr']);
1742
			$client['server_port'] = $client['serverport'];
1743
			unset($client['serverport']);
1744
			$client['proxy_addr'] = $client['poxy_hostname'];
1745
			unset($client['proxy_addr']);
1746
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1747
				$client['compression'] = "on";
1748
				unset($client['use_lzo']);
1749
			}
1750
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1751
			unset($client['infiniteresolvretry']);
1752

    
1753
			/* allocate vpnid */
1754
			$client['vpnid'] = $vpnid++;
1755

    
1756
			if (!empty($client['custom_options'])) {
1757
				$cstmopts = array();
1758
				$tmpcstmopts = explode(";", $client['custom_options']);
1759
				$assigned_if = "";
1760
				$tmpstr = "";
1761
				foreach ($tmpcstmopts as $tmpcstmopt) {
1762
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1763
					if (substr($tmpstr,0 ,6) == "devtun") {
1764
						$assigned_if = substr($tmpstr, 3);
1765
						continue;
1766
					} else if (substr($tmpstr, 0, 5) == "local") {
1767
						$localip = substr($tmpstr, 5);
1768
						$client['ipaddr'] = str_replace("\n", "", $localip);
1769
					} else
1770
						$cstmopts[] = $tmpcstmopt;
1771
				}
1772
				$client['custom_options'] = implode(";", $cstmopts);
1773
				if (!empty($assigned_if)) {
1774
					foreach ($config['interfaces'] as $iface => $cfgif) {
1775
						if ($cfgif['if'] == $assigned_if) {
1776
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1777
							break;
1778
						}
1779
					}
1780
				}
1781
			}
1782

    
1783
			if (!empty($client['disable']))
1784
				$client['disable'] = true;
1785
			else
1786
				unset($client['disable']);
1787

    
1788
			$config['openvpn']['openvpn-client'][] = $client;
1789
		}
1790

    
1791
		unset($config['installedpackages']['openvpnclient']);
1792
	}
1793

    
1794
	/* openvpn client specific configurations */
1795
	if (is_array($config['installedpackages']['openvpncsc'])) {
1796
		$config['openvpn']['openvpn-csc'] = array();
1797

    
1798
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1799

    
1800
			if (!is_array($csc))
1801
				continue;
1802

    
1803
			/* modify configuration values */
1804
			$csc['common_name'] = $csc['commonname'];
1805
			unset($csc['commonname']);
1806
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1807
			unset($csc['ifconfig_push']);
1808
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1809
			unset($csc['dhcp_domainname']);
1810

    
1811
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1812
			$d=1;
1813
			foreach ($tmparr as $tmpa) {
1814
				$csc["dns_server{$d}"] = $tmpa;
1815
				$d++;
1816
			}
1817
			unset($csc['dhcp_dns']);
1818

    
1819
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1820
			$d=1;
1821
			foreach ($tmparr as $tmpa) {
1822
				$csc["ntp_server{$d}"] = $tmpa;
1823
				$d++;
1824
			}
1825
			unset($csc['dhcp_ntp']);
1826

    
1827
			if ($csc['dhcp_nbtdisable'])
1828
				$csc['netbios_enable'] = false;
1829
			else
1830
				$csc['netbios_enable'] = "yes";
1831
			unset($csc['dhcp_nbtdisable']);
1832
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1833
			unset($csc['dhcp_nbttype']);
1834
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1835
			unset($csc['dhcp_nbtscope']);
1836

    
1837
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1838
			$d=1;
1839
			foreach ($tmparr as $tmpa) {
1840
				$csc["nbdd_server{$d}"] = $tmpa;
1841
				$d++;
1842
			}
1843
			unset($csc['dhcp_nbdd']);
1844

    
1845
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1846
			$d=1;
1847
			foreach ($tmparr as $tmpa) {
1848
				$csc["wins_server{$d}"] = $tmpa;
1849
				$d++;
1850
			}
1851
			unset($csc['dhcp_wins']);
1852

    
1853
			if (!empty($csc['disable']))
1854
				$csc['disable'] = true;
1855
			else
1856
				unset($csc['disable']);
1857

    
1858
			$config['openvpn']['openvpn-csc'][] = $csc;
1859
		}
1860

    
1861
		unset($config['installedpackages']['openvpncsc']);
1862
	}
1863

    
1864
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1865
		count($config['openvpn']['openvpn-client']) > 0) {
1866
		$ovpnrule = array();
1867
		$ovpnrule['type'] = "pass";
1868
		$ovpnrule['interface'] = "openvpn";
1869
		$ovpnrule['statetype'] = "keep state";
1870
		$ovpnrule['source'] = array();
1871
		$ovpnrule['destination'] = array();
1872
		$ovpnrule['source']['any'] = true;
1873
		$ovpnrule['destination']['any'] = true;
1874
		$ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1875
		$config['filter']['rule'][] = $ovpnrule;
1876
	}
1877

    
1878
	/*
1879
		* FIXME: hack to keep things working with no installedpackages
1880
		* or carp array in the configuration data.
1881
		*/
1882
	if (!is_array($config['installedpackages']))
1883
		$config['installedpackages'] = array();
1884
	if (!is_array($config['installedpackages']['carp']))
1885
		$config['installedpackages']['carp'] = array();
1886

    
1887
}
1888

    
1889

    
1890
function upgrade_052_to_053() {
1891
	global $config;
1892
	if (!is_array($config['ca']))
1893
		$config['ca'] = array();
1894
	if (!is_array($config['cert']))
1895
		$config['cert'] = array();
1896

    
1897
	/* migrate advanced admin page webui ssl to certificate manager */
1898
	if ($config['system']['webgui']['certificate'] &&
1899
	$config['system']['webgui']['private-key']) {
1900

    
1901
		/* create cert entry */
1902
		$cert = array();
1903
		$cert['refid'] = uniqid();
1904
		$cert['descr'] = "webConfigurator SSL Certificate";
1905
		$cert['crt'] = $config['system']['webgui']['certificate'];
1906
		$cert['prv'] = $config['system']['webgui']['private-key'];
1907
		$config['cert'][] = $cert;
1908

    
1909
		/* create cert reference */
1910
		unset($config['system']['webgui']['certificate']);
1911
		unset($config['system']['webgui']['private-key']);
1912
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1913
	}
1914

    
1915
	/* migrate advanced admin page ssh keys to user manager */
1916
	if ($config['system']['ssh']['authorizedkeys']) {
1917
		$admin_user =& getUserEntryByUID(0);
1918
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1919
		unset($config['system']['ssh']['authorizedkeys']);
1920
	}
1921
}
1922

    
1923

    
1924
function upgrade_053_to_054() {
1925
	global $config;
1926
	if(is_array($config['load_balancer']['lbpool'])) {
1927
		$lbpool_arr = $config['load_balancer']['lbpool'];
1928
		$lbpool_srv_arr = array();
1929
		$gateway_group_arr = array();
1930
		$gateways = return_gateways_array();
1931
		$group_name_changes = array();
1932
		if (! is_array($config['gateways']['gateway_item']))
1933
			$config['gateways']['gateway_item'] = array();
1934

    
1935
		$a_gateways =& $config['gateways']['gateway_item'];
1936
		foreach($lbpool_arr as $lbpool) {
1937
			if($lbpool['type'] == "gateway") {
1938
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
1939
				$group_name = preg_replace("/[^A-Za-z0-9]/", "", $lbpool['name'] );
1940
				// If we made and changes, check for collisions and note the change.
1941
				if ($group_name != $lbpool['name']) {
1942
					// Make sure the name isn't already in use.
1943
					foreach ($gateway_group_arr as $gwg) {
1944
						// If the name is in use, add some random bits to avoid collision.
1945
						if ($gwg['name'] == $group_name)
1946
							$group_name .= uniqid();
1947
					}
1948
					$group_name_changes[$lbpool['name']] = $group_name;
1949
				}
1950
				$gateway_group['name'] = $group_name;
1951
				$gateway_group['descr'] = $lbpool['descr'];
1952
				$gateway_group['trigger'] = "down";
1953
				$gateway_group['item'] = array();
1954
				$i = 0;
1955
				foreach($lbpool['servers'] as $member) {
1956
					$split = explode("|", $member);
1957
					$interface = $split[0];
1958
					$monitor = $split[1];
1959
					/* on static upgraded configuration we automatically prepend GW_ */
1960
					$static_name = "GW_" . strtoupper($interface);
1961
					if(is_ipaddr($monitor))
1962
						foreach ($a_gateways as & $gw)
1963
							if ($gw['name'] == $static_name)
1964
								$gw['monitor'] = $monitor;
1965

    
1966
					/* on failover increment tier. Else always assign 1 */
1967
					if($lbpool['behaviour'] == "failover") {
1968
						$i++;
1969
					} else {
1970
						$i = 1;
1971
					}
1972
					$gateway_group['item'][] = "$static_name|$i";
1973
				}
1974
				$gateway_group_arr[] = $gateway_group;
1975
			} else {
1976
				$lbpool_srv_arr[] = $lbpool;
1977
			}
1978
		}
1979
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1980
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1981
	}
1982
	// Unset lbpool if we no longer have any server pools
1983
	if (count($lbpool_srv_arr) == 0) {
1984
		if(empty($config['load_balancer'])) {
1985
			unset($config['load_balancer']);
1986
		} else {
1987
			if (isset($config['load_balancer']['lbpool'])) {
1988
				unset($config['load_balancer']['lbpool']);
1989
			}
1990
		}
1991
	} else {
1992
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1993
	}
1994
	// Only set the gateway group array if we converted any
1995
	if (count($gateway_group_arr) != 0) {
1996
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1997
		// Update any rules that had a gateway change, if any.
1998
		if (count($group_name_changes) > 0)
1999
			foreach ($config['filter']['rule'] as & $rule)
2000
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes))
2001
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
2002
	}
2003
}
2004

    
2005

    
2006
function upgrade_054_to_055() {
2007
	global $config;
2008
	global $g;
2009

    
2010
	/* RRD files changed for quality, traffic and packets graphs */
2011
	//ini_set("max_execution_time", "1800");
2012
	/* convert traffic RRD file */
2013
	global $parsedcfg, $listtags;
2014
	$listtags = array("ds", "v", "rra", "row");
2015

    
2016
	$rrddbpath = "/var/db/rrd/";
2017
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2018
	if ($g['platform'] != "pfSense") {
2019
		/* restore the databases, if we have one */
2020
		if (restore_rrd()) {
2021
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2022
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2023
		}
2024
	}
2025

    
2026
	$rrdinterval = 60;
2027
	$valid = $rrdinterval * 2;
2028

    
2029
	/* Asume GigE for now */
2030
	$downstream = 125000000;
2031
	$upstream = 125000000;
2032

    
2033
	/* build a list of quality databases */
2034
	/* roundtrip has become delay */
2035
	function divide_delay($delayval) {
2036
		$delayval = floatval($delayval);
2037
		$delayval = ($delayval / 1000);
2038
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2039
		return $delayval;
2040
	}
2041
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2042
	$databases = array();
2043
	if (!file_exists($rrddbpath))
2044
		@mkdir($rrddbpath);
2045
	chdir($rrddbpath);
2046
	$databases = glob("*-quality.rrd");
2047
	rsort($databases);
2048
	foreach($databases as $database) {
2049
		$xmldump = "{$database}.old.xml";
2050
		$xmldumpnew = "{$database}.new.xml";
2051

    
2052
		if (platform_booting())
2053
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2054
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2055

    
2056
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2057
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2058
		$rrdold = $rrdold['rrd'];
2059

    
2060
		$i = 0;
2061
		foreach($rrdold['rra'] as $rra) {
2062
			$l = 0;
2063
			foreach($rra['database']['row'] as $row) {
2064
				$vnew = divide_delay($row['v'][1]);
2065
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2066
				$l++;
2067
			}
2068
			$i++;
2069
		}
2070

    
2071
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw($rrdold, "rrd"));
2072
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2073

    
2074
		unset($rrdold);
2075
		@unlink("{$g['tmp_path']}/{$xmldump}");
2076
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2077
	}
2078
	/* let apinger recreate required files */
2079
	if (!platform_booting())
2080
		setup_gateways_monitor();
2081

    
2082
	/* build a list of traffic and packets databases */
2083
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2084
	rsort($databases);
2085
	foreach($databases as $database) {
2086
		$databasetmp = "{$database}.tmp";
2087
		$xmldump = "{$database}.old.xml";
2088
		$xmldumptmp = "{$database}.tmp.xml";
2089
		$xmldumpnew = "{$database}.new.xml";
2090

    
2091
		if (platform_booting())
2092
			echo "Migrate RRD database {$database} to new format \n";
2093
		/* rename DS source */
2094
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2095
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2096

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

    
2100
		/* create new rrd database file */
2101
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2102
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2103
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2104
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2105
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2106
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2107
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2108
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2109
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2110

    
2111
		create_new_rrd("$rrdcreate");
2112
		/* create temporary xml from new RRD */
2113
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2114

    
2115
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2116
		$rrdold = $rrdold['rrd'];
2117

    
2118
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2119
		$rrdnew = $rrdnew['rrd'];
2120

    
2121
		/* remove any MAX RRA's. Not needed for traffic. */
2122
		$i = 0;
2123
		foreach ($rrdold['rra'] as $rra) {
2124
			if(trim($rra['cf']) == "MAX") {
2125
				unset($rrdold['rra'][$i]);
2126
			}
2127
			$i++;
2128
		}
2129

    
2130
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2131
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2132
		/* we now have the rrd with the new fields, adjust the size now. */
2133
		/* RRA 2 is 60 minutes, RRA 3 is 720 minutes */
2134
		mwexec("/bin/sync");
2135
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 2 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2136
		mwexec("/bin/sync");
2137
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2138
		unset($rrdxmlarray);
2139
		@unlink("{$g['tmp_path']}/{$xmldump}");
2140
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2141
	}
2142
	if (!platform_booting())
2143
		enable_rrd_graphing();
2144
	/* Let's save the RRD graphs after we run enable RRD graphing */
2145
	/* The function will restore the rrd.tgz so we will save it after */
2146
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2147
	unlink_if_exists("{$g['vardb_path']}/rrd/*.xml");
2148
	if (platform_booting())
2149
		echo "Updating configuration...";
2150
}
2151

    
2152

    
2153
function upgrade_055_to_056() {
2154
	global $config;
2155

    
2156
	if (!is_array($config['ca']))
2157
		$config['ca'] = array();
2158
	if (!is_array($config['cert']))
2159
		$config['cert'] = array();
2160

    
2161
	/* migrate ipsec ca's to cert manager */
2162
	if (is_array($config['ipsec']['cacert'])) {
2163
		foreach($config['ipsec']['cacert'] as & $cacert) {
2164
			$ca = array();
2165
			$ca['refid'] = uniqid();
2166
			if (is_array($cacert['cert']))
2167
				$ca['crt'] = $cacert['cert'][0];
2168
			else
2169
				$ca['crt'] = $cacert['cert'];
2170
			$ca['descr'] = $cacert['ident'];
2171
			$config['ca'][] = $ca;
2172
		}
2173
		unset($config['ipsec']['cacert']);
2174
	}
2175

    
2176
	/* migrate phase1 certificates to cert manager */
2177
	if (is_array($config['ipsec']['phase1'])) {
2178
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
2179
			$cert = array();
2180
			$cert['refid'] = uniqid();
2181
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2182
			if (is_array($ph1ent['cert']))
2183
				$cert['crt'] = $ph1ent['cert'][0];
2184
			else
2185
				$cert['crt'] = $ph1ent['cert'];
2186
			$cert['prv'] = $ph1ent['private-key'];
2187
			$config['cert'][] = $cert;
2188
			$ph1ent['certref'] = $cert['refid'];
2189
			if ($ph1ent['cert'])
2190
				unset($ph1ent['cert']);
2191
			if ($ph1ent['private-key'])
2192
				unset($ph1ent['private-key']);
2193
			if ($ph1ent['peercert'])
2194
				unset($ph1ent['peercert']);
2195
		}
2196
	}
2197
}
2198

    
2199

    
2200
function upgrade_056_to_057() {
2201
	global $config;
2202

    
2203
	if (!is_array($config['system']['user']))
2204
		$config['system']['user'] = array();
2205
	/* migrate captivate portal to user manager */
2206
	if (is_array($config['captiveportal']['user'])) {
2207
		foreach($config['captiveportal']['user'] as $user) {
2208
			// avoid user conflicts
2209
			$found = false;
2210
			foreach ($config['system']['user'] as $userent) {
2211
				if ($userent['name'] == $user['name']) {
2212
					$found = true;
2213
					break;
2214
				}
2215
			}
2216
			if ($found)
2217
				continue;
2218
			$user['scope'] = "user";
2219
			if (isset($user['expirationdate'])) {
2220
				$user['expires'] = $user['expirationdate'];
2221
				unset($user['expirationdate']);
2222
			}
2223
			if (isset($user['password'])) {
2224
				$user['md5-hash'] = $user['password'];
2225
				unset($user['password']);
2226
			}
2227
			$user['uid'] = $config['system']['nextuid']++;
2228
			$config['system']['user'][] = $user;
2229
		}
2230
		unset($config['captiveportal']['user']);
2231
	}
2232
}
2233

    
2234
function upgrade_057_to_058() {
2235
	global $config;
2236
	/* set all phase2 entries to tunnel mode */
2237
	if (is_array($config['ipsec']['phase2']))
2238
		foreach($config['ipsec']['phase2'] as & $ph2ent)
2239
			$ph2ent['mode'] = 'tunnel';
2240
}
2241

    
2242
function upgrade_058_to_059() {
2243
	global $config;
2244

    
2245
	if (is_array($config['schedules']['schedule'])) {
2246
		foreach ($config['schedules']['schedule'] as & $schedl)
2247
			$schedl['schedlabel'] = uniqid();
2248
	}
2249
}
2250

    
2251
function upgrade_059_to_060() {
2252
	global $config;
2253
	require_once("/etc/inc/certs.inc");
2254
	if (is_array($config['ca'])) {
2255
		/* Locate issuer for all CAs */
2256
		foreach ($config['ca'] as & $ca) {
2257
			$subject = cert_get_subject($ca['crt']);
2258
			$issuer = cert_get_issuer($ca['crt']);
2259
			if($issuer <> $subject) {
2260
				$issuer_crt =& lookup_ca_by_subject($issuer);
2261
				if($issuer_crt)
2262
					$ca['caref'] = $issuer_crt['refid'];
2263
			}
2264
		}
2265

    
2266
		/* Locate issuer for all certificates */
2267
		if (is_array($config['cert'])) {
2268
			foreach ($config['cert'] as & $cert) {
2269
				$subject = cert_get_subject($cert['crt']);
2270
				$issuer = cert_get_issuer($cert['crt']);
2271
				if($issuer <> $subject) {
2272
					$issuer_crt =& lookup_ca_by_subject($issuer);
2273
					if($issuer_crt)
2274
						$cert['caref'] = $issuer_crt['refid'];
2275
				}
2276
			}
2277
		}
2278
	}
2279
}
2280

    
2281
function upgrade_060_to_061() {
2282
	global $config;
2283

    
2284
	if (is_array($config['interfaces']['wan']))
2285
		$config['interfaces']['wan']['enable'] = true;
2286
	if (is_array($config['interfaces']['lan']))
2287
		$config['interfaces']['lan']['enable'] = true;
2288

    
2289
	/* On 1.2.3 the "mtu" field adjusted MSS.
2290
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2291
	   See redmine ticket #1886
2292
	*/
2293
	foreach ($config['interfaces'] as $ifr => &$intf) {
2294
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2295
			$intf['mss'] = $intf['mtu'];
2296
			unset($intf['mtu']);
2297
		}
2298
	}
2299
}
2300

    
2301
function upgrade_061_to_062() {
2302
	global $config;
2303

    
2304
	/* Convert NAT port forwarding rules */
2305
	if (is_array($config['nat']['rule'])) {
2306
		$a_nat = &$config['nat']['rule'];
2307

    
2308
		foreach ($a_nat as &$natent) {
2309
			$natent['disabled'] = false;
2310
			$natent['nordr']    = false;
2311

    
2312
			$natent['source'] = array(
2313
				"not"     => false,
2314
				"any"     => true,
2315
				"port"    => ""
2316
			);
2317

    
2318
			$natent['destination'] = array(
2319
				"not"     => false,
2320
				"address" => $natent['external-address'],
2321
				"port"    => $natent['external-port']
2322
			);
2323

    
2324
			if (empty($natent['destination']['address'])) {
2325
				unset($natent['destination']['address']);
2326
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2327
			} else if ($natent['destination']['address'] == 'any') {
2328
				unset($natent['destination']['address']);
2329
				$natent['destination']['any'] = true;
2330
			}
2331

    
2332
			unset($natent['external-address']);
2333
			unset($natent['external-port']);
2334
		}
2335

    
2336
		unset($natent);
2337
	}
2338
}
2339

    
2340
function upgrade_062_to_063() {
2341
	/* Upgrade legacy Themes to the new pfsense_ng */
2342
	global $config;
2343

    
2344
	switch($config['theme']) {
2345
		case "nervecenter":
2346
			$config['theme'] = "pfsense_ng";
2347
			break;
2348
	}
2349

    
2350
}
2351

    
2352
function upgrade_063_to_064() {
2353
	global $config;
2354
	$j=0;
2355
	$ifcfg = &$config['interfaces'];
2356

    
2357
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
2358
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2359
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2360
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2361
			$j++;
2362
			if (isset($ppp['port'])){
2363
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2364
				unset($config['ppps']['ppp'][$pppid]['port']);
2365
			}
2366
			if (!isset($ppp['type'])){
2367
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2368
			}
2369
			if (isset($ppp['defaultgw']))
2370
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2371
		}
2372
	}
2373

    
2374
	if (!is_array($config['ppps']['ppp']))
2375
		$config['ppps']['ppp'] = array();
2376
	$a_ppps = &$config['ppps']['ppp'];
2377

    
2378
	foreach ($ifcfg as $ifname => $ifinfo) {
2379
		$ppp = array();
2380
		// For pppoe conversion
2381
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2382
			if (isset($ifinfo['ptpid']))
2383
				continue;
2384
			$ppp['ptpid'] =  $j;
2385
			$ppp['type'] = $ifinfo['ipaddr'];
2386
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2387
			$ppp['ports'] = $ifinfo['if'];
2388
			if ($ifinfo['ipaddr'] == "pppoe"){
2389
				$ppp['username'] = $ifinfo['pppoe_username'];
2390
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2391
			}
2392
			if ($ifinfo['ipaddr'] == "pptp"){
2393
				$ppp['username'] = $ifinfo['pptp_username'];
2394
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2395
			}
2396

    
2397
			if (isset($ifinfo['provider']))
2398
				$ppp['provider'] = $ifinfo['provider'];
2399
			if (isset($ifinfo['ondemand']))
2400
				$ppp['ondemand'] = true;
2401
			if (isset($ifinfo['timeout']))
2402
				$ppp['idletimeout'] = $ifinfo['timeout'];
2403
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2404
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2405
				if (is_array($config['cron']['item'])) {
2406
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2407
						$item = $config['cron']['item'][$i];
2408
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2409
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2410
					}
2411
				}
2412
			}
2413
			if (isset($ifinfo['local']))
2414
				$ppp['localip'] = $ifinfo['local'];
2415
			if (isset($ifinfo['subnet']))
2416
				$ppp['subnet'] = $ifinfo['subnet'];
2417
			if (isset($ifinfo['remote']))
2418
				$ppp['gateway'] = $ifinfo['remote'];
2419

    
2420
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2421
			$j++;
2422

    
2423
			unset($ifcfg[$ifname]['pppoe_username']);
2424
			unset($ifcfg[$ifname]['pppoe_password']);
2425
			unset($ifcfg[$ifname]['provider']);
2426
			unset($ifcfg[$ifname]['ondemand']);
2427
			unset($ifcfg[$ifname]['timeout']);
2428
			unset($ifcfg[$ifname]['pppoe_reset']);
2429
			unset($ifcfg[$ifname]['pppoe_preset']);
2430
			unset($ifcfg[$ifname]['pppoe']);
2431
			unset($ifcfg[$ifname]['pptp_username']);
2432
			unset($ifcfg[$ifname]['pptp_password']);
2433
			unset($ifcfg[$ifname]['local']);
2434
			unset($ifcfg[$ifname]['subnet']);
2435
			unset($ifcfg[$ifname]['remote']);
2436

    
2437
			$a_ppps[] = $ppp;
2438

    
2439
		}
2440
	}
2441
}
2442

    
2443
function upgrade_064_to_065() {
2444
	/* Disable TSO and LRO in upgraded configs */
2445
	global $config;
2446
	$config['system']['disablesegmentationoffloading'] = true;
2447
	$config['system']['disablelargereceiveoffloading'] = true;
2448
}
2449

    
2450
function upgrade_065_to_066() {
2451
	global $config;
2452

    
2453
	$dhcrelaycfg =& $config['dhcrelay'];
2454

    
2455
	if (is_array($dhcrelaycfg)) {
2456
		$dhcrelayifs = array();
2457
		$foundifs = false;
2458
		/* DHCPRelay enabled on any interfaces? */
2459
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2460
			if (isset($dhcrelayifconf['enable'])) {
2461
				$dhcrelayifs[] = $dhcrelayif;
2462
				unset($dhcrelaycfg['dhcrelayif']);
2463
				$foundifs = true;
2464
			}
2465
		}
2466
		if ($foundifs == true)
2467
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2468
	}
2469
}
2470

    
2471
function upgrade_066_to_067() {
2472
	global $config;
2473
	if (isset($config['system']['ca'])) {
2474
		$config['ca'] = $config['system']['ca'];
2475
	}
2476
	if (isset($config['system']['cert'])) {
2477
		$config['cert'] = $config['system']['cert'];
2478
	}
2479
}
2480

    
2481
function upgrade_067_to_068() {
2482
	global $config;
2483

    
2484
	if (!empty($config['pppoe'])) {
2485
		$config['pppoes'] = array();
2486
		$config['pppoes']['pppoe'] = array();
2487
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2488

    
2489
		if (is_array($config['pppoe']['user'])) {
2490
			$username = array();
2491
			foreach ($config['pppoe']['user'] as $user) {
2492
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2493
				if ($user['ip'])
2494
					$usr .= ":{$user['ip']}";
2495
				$username[] = $usr;
2496
			}
2497
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2498
		}
2499
		unset($config['pppoe']);
2500
	}
2501
}
2502

    
2503
function upgrade_068_to_069() {
2504
	global $config;
2505
	if (!is_array($config['system']['user']))
2506
		return;
2507
	foreach ($config['system']['user'] as & $user) {
2508
		if (!is_array($user['cert']))
2509
			continue;
2510
		$rids = array();
2511
		foreach ($user['cert'] as $id => $cert) {
2512
			if (!isset($cert['descr']))
2513
				continue;
2514
			$tcert = $cert;
2515
			// Make sure each cert gets a refid
2516
			if (!isset($tcert['refid']))
2517
				$tcert['refid'] = uniqid();
2518
			// Keep the cert references for this user
2519
			$rids[] = $tcert['refid'];
2520
			$config['cert'][] = $tcert;
2521
		}
2522
		// Replace user certs with cert references instead.
2523
		if (count($rids) > 0)
2524
			$user['cert'] = $rids;
2525
	}
2526
}
2527

    
2528
function upgrade_069_to_070() {
2529
	global $config;
2530

    
2531
	/* Convert NAT 1:1 rules */
2532
	if (is_array($config['nat']['onetoone'])) {
2533
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2534
			if ($natent['subnet'] == 32)
2535
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2536
			else
2537
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2538

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

    
2541
			unset($config['nat']['onetoone'][$nidx]['internal']);
2542
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2543
		}
2544

    
2545
		unset($natent);
2546
	}
2547
}
2548

    
2549
function upgrade_070_to_071() {
2550
	global $config;
2551

    
2552
	if (is_array($config['cron']['item'])) {
2553
		foreach($config['cron']['item'] as $idx => $cronitem) {
2554
			if(stristr($cronitem['command'], "checkreload.sh")) {
2555
				unset($config['cron']['item'][$idx]);
2556
				break;
2557
			}
2558
		}
2559
	}
2560
}
2561

    
2562
function rename_field(& $section, $oldname, $newname) {
2563
	if (is_array($section)) {
2564
		foreach($section as & $item) {
2565
			if (is_array($item) && !empty($item[$oldname]))
2566
				$item[$newname] = $item[$oldname];
2567
			if (is_array($item) && isset($item[$oldname]))
2568
				unset($item[$oldname]);
2569
		}
2570
	}
2571
}
2572

    
2573
function upgrade_071_to_072() {
2574
	global $config;
2575
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2576
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2577
}
2578

    
2579
function upgrade_072_to_073() {
2580
	global $config;
2581
	if (!is_array($config['load_balancer']))
2582
		return;
2583
	if (is_array($config['load_balancer']['monitor_type']))
2584
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2585
	if (is_array($config['load_balancer']['lbpool']))
2586
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2587
	if (is_array($config['load_balancer']['lbaction']))
2588
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2589
	if (is_array($config['load_balancer']['lbprotocol']))
2590
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2591
	if (is_array($config['load_balancer']['virtual_server']))
2592
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2593
}
2594

    
2595
function upgrade_073_to_074() {
2596
	global $config;
2597
	rename_field($config['system']['user'], 'fullname', 'descr');
2598
}
2599

    
2600
function upgrade_074_to_075() {
2601
	global $config;
2602
	if (is_array($config['ca']))
2603
		rename_field($config['ca'], 'name', 'descr');
2604
	if (is_array($config['cert']))
2605
		rename_field($config['cert'], 'name', 'descr');
2606
	if (is_array($config['crl']))
2607
		rename_field($config['crl'], 'name', 'descr');
2608
}
2609

    
2610
function upgrade_075_to_076() {
2611
	global $config;
2612
	$cron_item = array();
2613
	$cron_item['minute'] = "30";
2614
	$cron_item['hour'] = "12";
2615
	$cron_item['mday'] = "*";
2616
	$cron_item['month'] = "*";
2617
	$cron_item['wday'] = "*";
2618
	$cron_item['who'] = "root";
2619
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2620
	$config['cron']['item'][] = $cron_item;
2621
}
2622

    
2623
function upgrade_076_to_077() {
2624
	global $config;
2625
	foreach($config['filter']['rule'] as & $rule) {
2626
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2627
		$rule['protocol'] = strtolower($rule['protocol']);
2628
	}
2629
}
2630

    
2631
function upgrade_077_to_078() {
2632
	global $config;
2633
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius'])
2634
		&& !is_array($config['pptpd']['radius']['server'])) {
2635
		$radarr = array();
2636
		$radsvr = array();
2637
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2638
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2639
		$radsvr['port'] = 1812;
2640
		$radsvr['acctport'] = 1813;
2641
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2642
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2643
		if ($radarr['accounting'])
2644
			$radarr['acct_update'] = $radsvr['ip'];
2645
		$radarr['server'] = $radsvr;
2646
		$config['pptpd']['radius'] = $radarr;
2647
	}
2648
	if (is_array($config['pptpd'])) {
2649
		$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
2650
	}
2651
}
2652
function upgrade_078_to_079() {
2653
	global $g;
2654
	/* Delete old and unused RRD file */
2655
	unlink_if_exists("{$g['vardb_path']}/rrd/captiveportal-totalusers.rrd");
2656
}
2657

    
2658
function upgrade_079_to_080() {
2659
	global $config;
2660

    
2661
	/* Upgrade config in 1.2.3 specifying a username other than admin for syncing. */
2662
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2663
		is_array($config['installedpackages']['carpsettings']['config'])) {
2664
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2665
		unset($config['system']['username']);
2666
	}
2667
}
2668

    
2669
function upgrade_080_to_081() {
2670
	global $config;
2671
	global $g;
2672
	/* Welcome to the 2.1 migration path */
2673

    
2674
	/* tag all the existing gateways as being IPv4 */
2675
	$i = 0;
2676
	if(is_array($config['gateways']['gateway_item'])) {
2677
		foreach($config['gateways']['gateway_item'] as $gw) {
2678
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2679
			$i++;
2680
		}
2681
	}
2682

    
2683
	/* RRD files changed for quality, traffic and packets graphs */
2684
	/* convert traffic RRD file */
2685
	global $parsedcfg, $listtags;
2686
	$listtags = array("ds", "v", "rra", "row");
2687

    
2688
	$rrddbpath = "/var/db/rrd/";
2689
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2690

    
2691
	if ($g['platform'] != "pfSense") {
2692
		/* restore the databases, if we have one */
2693
		if (restore_rrd()) {
2694
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2695
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2696
		}
2697
	}
2698

    
2699
	$rrdinterval = 60;
2700
	$valid = $rrdinterval * 2;
2701

    
2702
	/* Asume GigE for now */
2703
	$downstream = 125000000;
2704
	$upstream = 125000000;
2705

    
2706
	/* build a list of traffic and packets databases */
2707
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2708
	rsort($databases);
2709
	foreach($databases as $database) {
2710
		$xmldump = "{$database}.old.xml";
2711
		$xmldumpnew = "{$database}.new.xml";
2712

    
2713
		if (platform_booting())
2714
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2715

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

    
2719
		/* search and replace tags to add data sources */
2720
		$ds_search = "<!-- Round Robin Archives -->";
2721
		$ds_arr = array();
2722
		$ds_arr[] = "	<ds>
2723
				<name> inpass6 </name>
2724
				<type> COUNTER </type>
2725
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2726
				<min> 0.0000000000e+00 </min>
2727
				<max> 1.2500000000e+08 </max>
2728

    
2729
				<!-- PDP Status -->
2730
				<last_ds> 0 </last_ds>
2731
				<value> NaN </value>
2732
				<unknown_sec> 3 </unknown_sec>
2733
			</ds>
2734
			";
2735
		$ds_arr[] = "	<ds>
2736
				<name> outpass6 </name>
2737
				<type> COUNTER </type>
2738
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2739
				<min> 0.0000000000e+00 </min>
2740
				<max> 1.2500000000e+08 </max>
2741

    
2742
				<!-- PDP Status -->
2743
				<last_ds> 0 </last_ds>
2744
				<value> NaN </value>
2745
				<unknown_sec> 3 </unknown_sec>
2746
			</ds>
2747
			";
2748
		$ds_arr[] = "	<ds>
2749
				<name> inblock6 </name>
2750
				<type> COUNTER </type>
2751
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2752
				<min> 0.0000000000e+00 </min>
2753
				<max> 1.2500000000e+08 </max>
2754

    
2755
				<!-- PDP Status -->
2756
				<last_ds> 0 </last_ds>
2757
				<value> NaN </value>
2758
				<unknown_sec> 3 </unknown_sec>
2759
			</ds>
2760
			";
2761
		$ds_arr[] = "	<ds>
2762
				<name> outblock6 </name>
2763
				<type> COUNTER </type>
2764
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2765
				<min> 0.0000000000e+00 </min>
2766
				<max> 1.2500000000e+08 </max>
2767

    
2768
				<!-- PDP Status -->
2769
				<last_ds> 0 </last_ds>
2770
				<value> NaN </value>
2771
				<unknown_sec> 3 </unknown_sec>
2772
			</ds>
2773
			";
2774

    
2775
		$cdp_search = "<\/cdp_prep>";
2776
		$cdp_replace = "</cdp_prep>";
2777
		$cdp_arr = array();
2778
		$cdp_arr[] = "			<ds>
2779
					<primary_value> NaN </primary_value>
2780
					<secondary_value> 0.0000000000e+00 </secondary_value>
2781
					<value> NaN </value>
2782
					<unknown_datapoints> 0 </unknown_datapoints>
2783
					</ds>
2784
		";
2785
		$cdp_arr[] = "			<ds>
2786
					<primary_value> NaN </primary_value>
2787
					<secondary_value> 0.0000000000e+00 </secondary_value>
2788
					<value> NaN </value>
2789
					<unknown_datapoints> 0 </unknown_datapoints>
2790
					</ds>
2791
		";
2792
		$cdp_arr[] = "			<ds>
2793
					<primary_value> NaN </primary_value>
2794
					<secondary_value> 0.0000000000e+00 </secondary_value>
2795
					<value> NaN </value>
2796
					<unknown_datapoints> 0 </unknown_datapoints>
2797
					</ds>
2798
		";
2799
		$cdp_arr[] = "			<ds>
2800
					<primary_value> NaN </primary_value>
2801
					<secondary_value> 0.0000000000e+00 </secondary_value>
2802
					<value> NaN </value>
2803
					<unknown_datapoints> 0 </unknown_datapoints>
2804
					</ds>
2805
		";
2806

    
2807
		$value_search = "<\/row>";
2808
		$value_replace = "</row>";
2809
		$value = "<v> NaN </v>";
2810

    
2811
		$xml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2812
		foreach($ds_arr as $ds) {
2813
			$xml = preg_replace("/$ds_search/s", "$ds{$ds_search}", $xml);
2814
		}
2815
		foreach($cdp_arr as $cdp) {
2816
			$xml = preg_replace("/$cdp_search/s", "$cdp{$cdp_replace}", $xml);
2817
		}
2818
		foreach($ds_arr as $ds) {
2819
			$xml = preg_replace("/$value_search/s", "$value{$value_replace}", $xml);
2820
		}
2821
		
2822
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $xml);
2823
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2824
		unset($xml);
2825
		# Default /tmp tmpfs is ~40mb, do not leave temp files around
2826
		unlink_if_exists("{$g['tmp_path']}/{$xmldump}");
2827
		unlink_if_exists("{$g['tmp_path']}/{$xmldumpnew}");
2828
	}
2829
	if (!platform_booting())
2830
		enable_rrd_graphing();
2831
	/* Let's save the RRD graphs after we run enable RRD graphing */
2832
	/* The function will restore the rrd.tgz so we will save it after */
2833
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2834
	if (platform_booting())
2835
		echo "Updating configuration...";
2836
	foreach($config['filter']['rule'] as & $rule) {
2837
		if (isset($rule['protocol']) && !empty($rule['protocol']))
2838
			$rule['protocol'] = strtolower($rule['protocol']);
2839
	}
2840
	unset($rule);
2841
}
2842

    
2843
function upgrade_081_to_082() {
2844
	/* don't enable the allow IPv6 toggle */
2845
}
2846

    
2847
function upgrade_082_to_083() {
2848
	global $config;
2849

    
2850
	/* Upgrade captiveportal config */
2851
	if (!empty($config['captiveportal'])) {
2852
		$tmpcp = $config['captiveportal'];
2853
		$config['captiveportal'] = array();
2854
		$config['captiveportal']['cpzone'] = array();
2855
		$config['captiveportal']['cpzone'] = $tmpcp;
2856
		$config['captiveportal']['cpzone']['zoneid'] = 8000;
2857
		$config['captiveportal']['cpzone']['zone'] = "cpzone";
2858
		if ($config['captiveportal']['cpzone']['auth_method'] == "radius")
2859
			$config['captiveportal']['cpzone']['radius_protocol'] = "PAP";
2860
	}
2861
	if (!empty($config['voucher'])) {
2862
		$tmpcp = $config['voucher'];
2863
		$config['voucher'] = array();
2864
		$config['voucher']['cpzone'] = array();
2865
		$config['voucher']['cpzone'] = $tmpcp;
2866
	}
2867
}
2868

    
2869
function upgrade_083_to_084() {
2870
	global $config;
2871
	if (!isset($config['hasync'])) {
2872
		if (!empty($config['installedpackages']) &&
2873
		    !empty($config['installedpackages']['carpsettings']) &&
2874
		    !empty($config['installedpackages']['carpsettings']['config'])) {
2875
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
2876
			unset($config['installedpackages']['carpsettings']);
2877
		}
2878
		if (empty($config['installedpackages']['carpsettings']) && isset($config['installedpackages']['carpsettings'])) {
2879
			unset($config['installedpackages']['carpsettings']);
2880
		}
2881
		if (empty($config['installedpackages']) && isset($config['installedpackages'])) {
2882
			unset($config['installedpackages']);
2883
		}
2884
	}
2885
}
2886

    
2887
function upgrade_084_to_085() {
2888
	global $config;
2889

    
2890
	$gateway_group_arr = array();
2891
	$gateways = return_gateways_array();
2892
	$oldnames = array();
2893
	/* setup translation array */
2894
	foreach($gateways as $name => $gw) {
2895
		if(isset($gw['dynamic'])){
2896
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
2897
			$oldnames[$oldname] = $name;
2898
		} else {
2899
			$oldnames[$name] = $name;
2900
		}
2901
	}
2902

    
2903
	/* process the old array */
2904
	if(is_array($config['gateways']['gateway_group'])) {
2905
		$group_array_new = array();
2906
		foreach($config['gateways']['gateway_group'] as $name => $group) {
2907
			if(is_array($group['item'])) {
2908
				$newlist = array();
2909
				foreach($group['item'] as $entry) {
2910
					$elements = explode("|", $entry);
2911
					if($oldnames[$elements[0]] <> "") {
2912
						$newlist[] = "{$oldnames[$elements[0]]}|{$elements[1]}";
2913
					} else {
2914
						$newlist[] = "{$elements[0]}|{$elements[1]}";
2915
					}
2916
				}
2917
				$group['item'] = $newlist;
2918
				$group_array_new[$name] = $group;
2919
			}
2920
		}
2921
		$config['gateways']['gateway_group'] = $group_array_new;
2922
	}
2923
	/* rename old Quality RRD files in the process */
2924
	$rrddbpath = "/var/db/rrd";
2925
	foreach($oldnames as $old => $new) {
2926
		if(is_readable("{$rrddbpath}/{$old}-quality.rrd")) {
2927
			@rename("{$rrddbpath}/{$old}-quality.rrd", "{$rrddbpath}/{$new}-quality.rrd");
2928
		}
2929
	}
2930
	unset($gateways, $oldnames, $gateway_group_arr);
2931
}
2932

    
2933
function upgrade_085_to_086() {
2934
	global $config, $g;
2935

    
2936
	/* XXX: Gross hacks in sight */
2937
	if (is_array($config['virtualip']['vip'])) {
2938
		$vipchg = array();
2939
		foreach ($config['virtualip']['vip'] as $vip) {
2940
			if ($vip['mode'] != "carp")
2941
				continue;
2942
			$config = array_replace_values_recursive(
2943
				$config,
2944
				'^vip' . $vip['vhid'] . '$',
2945
				"{$vip['interface']}_vip{$vip['vhid']}"
2946
			);
2947
		}
2948
	}
2949
}
2950

    
2951
function upgrade_086_to_087() {
2952
	global $config, $dummynet_pipe_list;
2953

    
2954
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue']))
2955
		return;
2956

    
2957
	$dnqueue_number = 1;
2958
	$dnpipe_number = 1;
2959

    
2960
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
2961
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
2962
		$dnpipe_number++;
2963
		if (is_array($dnpipe['queue'])) {
2964
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
2965
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
2966
				$dnqueue_number++;
2967
			}
2968
		}
2969
	}
2970

    
2971
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
2972

    
2973
	if (!is_array($config['filter']) || !is_array($config['filter']['rule']))
2974
		return;
2975

    
2976
	require_once("shaper.inc");
2977
	read_dummynet_config();
2978

    
2979
	$dn_list = array();
2980
	if (is_array($dummynet_pipe_list)) {
2981
		foreach ($dummynet_pipe_list as $dn) {
2982
			$tmplist =& $dn->get_queue_list();
2983
			foreach ($tmplist as $qname => $link) {
2984
				$dn_list[$link] = $qname;
2985
			}
2986
		}
2987
		unset($dummynet_pipe_list);
2988
	}
2989

    
2990
	foreach ($config['filter']['rule'] as $idx => $rule) {
2991
		if (!empty($rule['dnpipe'])) {
2992
			if (!empty($dn_list[$rule['dnpipe']]))
2993
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
2994
		}
2995
		if (!empty($rule['pdnpipe'])) {
2996
			if (!empty($dn_list[$rule['pdnpipe']]))
2997
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
2998
		}
2999
	}
3000
}
3001
function upgrade_087_to_088() {
3002
	global $config;
3003
	if (isset($config['system']['glxsb_enable'])) {
3004
		unset($config['system']['glxsb_enable']);
3005
		$config['system']['crypto_hardware'] = "glxsb";
3006
	}
3007
}
3008

    
3009
function upgrade_088_to_089() {
3010
	global $config;
3011
	if (!is_array($config['ca']))
3012
		$config['ca'] = array();
3013
	if (!is_array($config['cert']))
3014
		$config['cert'] = array();
3015

    
3016
	/* migrate captive portal ssl to certificate manager */
3017
	if (is_array($config['captiveportal'])) {
3018
		foreach ($config['captiveportal'] as $id => &$setting) {
3019
			if (isset($setting['httpslogin'])) {
3020
				/* create cert entry */
3021
				$cert = array();
3022
				$cert['refid'] = uniqid();
3023
				$cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
3024
				$cert['crt'] = $setting['certificate'];
3025
				$cert['prv'] = $setting['private-key'];
3026

    
3027
				if (!empty($setting['cacertificate'])) {
3028
					/* create ca entry */
3029
					$ca = array();
3030
					$ca['refid'] = uniqid();
3031
					$ca['descr'] = "Captive Portal CA - {$setting['zone']}";
3032
					$ca['crt'] = $setting['cacertificate'];
3033
					$config['ca'][] = $ca;
3034

    
3035
					/* add ca reference to certificate */
3036
					$cert['caref'] = $ca['refid'];
3037
				}
3038

    
3039
				$config['cert'][] = $cert;
3040

    
3041
				/* create cert reference */
3042
				$setting['certref'] = $cert['refid'];
3043

    
3044
				unset($setting['certificate']);
3045
				unset($setting['private-key']);
3046
				unset($setting['cacertificate']);
3047

    
3048
			}
3049
		}
3050
	}
3051
}
3052

    
3053
function upgrade_089_to_090() {
3054
	global $config;
3055
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
3056
		$vs_a = &$config['load_balancer']['virtual_server'];
3057
		for ($i = 0; isset($vs_a[$i]); $i++) {
3058
			if (is_array($vs_a[$i]['pool'])) {
3059
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
3060
				unset($vs_a[$i]['pool']);
3061
			} elseif (!empty($vs_a[$i]['pool'])) {
3062
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
3063
				unset($vs_a[$i]['pool']);
3064
			}
3065
		}
3066
	}
3067
}
3068

    
3069
function upgrade_090_to_091() {
3070
	global $config;
3071

    
3072
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
3073
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
3074
			if (!empty($dnqueue['bandwidth'])) {
3075
				$bw = array();
3076
				$bw['bw'] = $dnqueue['bandwidth'];
3077
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
3078
				$bw['bwsched'] = "none";
3079
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
3080
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
3081
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
3082
			}
3083
		}
3084
	}
3085
}
3086

    
3087
function upgrade_091_to_092() {
3088
	global $config;
3089

    
3090
	if (is_array($config['nat']['advancedoutbound']) && is_array($config['nat']['advancedoutbound']['rule'])) {
3091
		$nat_rules = &$config['nat']['advancedoutbound']['rule'];
3092
		for ($i = 0; isset($nat_rules[$i]); $i++) {
3093
			if (empty($nat_rules[$i]['interface'])) {
3094
				$nat_rules[$i]['interface'] = 'wan';
3095
			}
3096
		}
3097
	}
3098
}
3099

    
3100
function upgrade_092_to_093() {
3101
	global $g;
3102

    
3103
	$suffixes = array("concurrent", "loggedin");
3104

    
3105
	foreach ($suffixes as $suffix)
3106
		if (file_exists("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd"))
3107
			rename("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd",
3108
				"{$g['vardb_path']}/rrd/captiveportal-cpZone-{$suffix}.rrd");
3109

    
3110
	if (!platform_booting())
3111
		enable_rrd_graphing();
3112
}
3113

    
3114
function upgrade_093_to_094() {
3115
	global $config;
3116

    
3117
	if (isset($config['system']['powerd_mode'])) {
3118
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3119
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3120
		unset($config['system']['powerd_mode']);
3121
	}
3122
}
3123

    
3124
function upgrade_094_to_095() {
3125
	global $config;
3126

    
3127
	if (!isset($config['interfaces']) || !is_array($config['interfaces']))
3128
		return;
3129

    
3130
	foreach ($config['interfaces'] as $iface => $cfg)
3131
		if (isset($cfg['ipaddrv6']) && ($cfg['ipaddrv6'] == "track6"))
3132
			if (!isset($cfg['track6-prefix-id']) || ($cfg['track6-prefix-id'] == ""))
3133
				$config['interfaces'][$iface]['track6-prefix-id'] = 0;
3134
}
3135

    
3136
function upgrade_095_to_096() {
3137
	global $config, $g;
3138

    
3139
	$names = array("inpass", "outpass", "inblock", "outblock",
3140
		"inpass6", "outpass6", "inblock6", "outblock6");
3141
	$rrddbpath = "/var/db/rrd";
3142
	$rrdtool = "/usr/local/bin/rrdtool";
3143

    
3144
	if ($g['platform'] != "pfSense") {
3145
		/* restore the databases, if we have one */
3146
		if (restore_rrd()) {
3147
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
3148
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
3149
		}
3150
	}
3151

    
3152
	/* Assume 2*10GigE for now */
3153
	$stream = 2500000000;
3154

    
3155
	/* build a list of traffic and packets databases */
3156
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
3157
	rsort($databases);
3158
	foreach($databases as $database) {
3159
		if (platform_booting())
3160
			echo "Update RRD database {$database}.\n";
3161

    
3162
		$cmd = "{$rrdtool} tune {$rrddbpath}/{$database}";
3163
		foreach ($names as $name)
3164
			$cmd .= " -a {$name}:{$stream}";
3165
		mwexec("{$cmd} 2>&1");
3166

    
3167
	}
3168
	if (!platform_booting())
3169
		enable_rrd_graphing();
3170
	/* Let's save the RRD graphs after we run enable RRD graphing */
3171
	/* The function will restore the rrd.tgz so we will save it after */
3172
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
3173
}
3174

    
3175
function upgrade_096_to_097() {
3176
	global $config, $g;
3177
	/* If the user had disabled default block rule logging before, then bogon/private network logging was already off, so respect their choice. */
3178
	if (isset($config['syslog']['nologdefaultblock'])) {
3179
		$config['syslog']['nologbogons'] = true;
3180
		$config['syslog']['nologprivatenets'] = true;
3181
	}
3182
}
3183

    
3184
function upgrade_097_to_098() {
3185
	global $config, $g;
3186
	/* Disable kill_states by default */
3187
	$config['system']['kill_states'] = true;
3188
}
3189

    
3190
function upgrade_098_to_099() {
3191
	global $config;
3192

    
3193
	if (empty($config['dhcpd']) || !is_array($config['dhcpd']))
3194
		return;
3195

    
3196
	foreach ($config['dhcpd'] as & $dhcpifconf) {
3197
		if (isset($dhcpifconf['next-server'])) {
3198
			$dhcpifconf['nextserver'] = $dhcpifconf['next-server'];
3199
			unset($dhcpifconf['next-server']);
3200
		}
3201
	}
3202
}
3203

    
3204
function upgrade_099_to_100() {
3205
	require_once("/etc/inc/services.inc");
3206
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3207
}
3208

    
3209
function upgrade_100_to_101() {
3210
	global $config, $g;
3211

    
3212
	if (!is_array($config['voucher']))
3213
		return;
3214

    
3215
	foreach ($config['voucher'] as $cpzone => $cp) {
3216
		if (!is_array($cp['roll']))
3217
			continue;
3218
		foreach ($cp['roll'] as $ridx => $rcfg) {
3219
			if (!empty($rcfg['comment']))
3220
				$config['voucher'][$cpzone]['roll'][$ridx]['descr'] = $rcfg['comment'];
3221
		}
3222
	}
3223
}
3224

    
3225
function upgrade_101_to_102() {
3226
	global $config, $g;
3227

    
3228
	if (is_array($config['captiveportal'])) {
3229
		foreach ($config['captiveportal'] as $cpzone => $cp) {
3230
			if (!is_array($cp['passthrumac']))
3231
				continue;
3232

    
3233
			foreach ($cp['passthrumac'] as $idx => $passthrumac)
3234
				$config['captiveportal'][$cpzone]['passthrumac'][$idx]['action'] = 'pass';
3235
		}
3236
	}
3237

    
3238
	/* Convert OpenVPN Compression option to the new style */
3239
	// Nothing to do if there is no OpenVPN tag
3240
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
3241
		if (is_array($config['openvpn']['openvpn-server'])) {
3242
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
3243
				if (!empty($vpn['compression']))
3244
					$vpn['compression'] = "adaptive";
3245
			}
3246
		}
3247
		if (is_array($config['openvpn']['openvpn-client'])) {
3248
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
3249
				if (!empty($vpn['compression']))
3250
					$vpn['compression'] = "adaptive";
3251
			}
3252
		}
3253
	}
3254
}
3255

    
3256
function upgrade_102_to_103() {
3257
	global $config;
3258

    
3259
	if (isset($config['nat']['advancedoutbound']['enable'])) {
3260
		$config['nat']['advancedoutbound']['mode'] = "advanced";
3261
		unset($config['nat']['advancedoutbound']['enable']);
3262
	} else
3263
		$config['nat']['advancedoutbound']['mode'] = "automatic";
3264

    
3265
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3266

    
3267
	if (isset($config['nat']['ipsecpassthru'])) {
3268
		unset($config['nat']['ipsecpassthru']);
3269
	}
3270
	if (isset($config['nat']['advancedoutbound'])) {
3271
		unset($config['nat']['advancedoutbound']);
3272
	}
3273
}
3274

    
3275
function upgrade_103_to_104() {
3276
	global $config;
3277

    
3278
	$changed_privs = array(
3279
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3280
		"page-interfacess-groups" => "page-interfaces-groups",
3281
		"page-interfacess-lagg" => "page-interfaces-lagg",
3282
		"page-interfacess-qinq" => "page-interfaces-qinq"
3283
	);
3284

    
3285
	/* update user privileges */
3286
	foreach ($config['system']['user'] as & $user) {
3287
		if (!is_array($user['priv']))
3288
			continue;
3289
		foreach ($user['priv'] as & $priv) {
3290
			if (array_key_exists($priv, $changed_privs))
3291
				$priv = $changed_privs[$priv];
3292
		}
3293
	}
3294

    
3295
	/* update group privileges */
3296
	foreach ($config['system']['group'] as & $group) {
3297
		if (!is_array($group['priv']))
3298
			continue;
3299
		foreach ($group['priv'] as & $priv) {
3300
			if (array_key_exists($priv, $changed_privs))
3301
				$priv = $changed_privs[$priv];
3302
		}
3303
	}
3304

    
3305
	/* sync all local account information */
3306
	local_sync_accounts();
3307
}
3308

    
3309
function upgrade_104_to_105() {
3310
	global $config;
3311

    
3312
	if (is_array($config['captiveportal'])) {
3313
		$zoneid = 2;
3314
		foreach ($config['captiveportal'] as $cpzone => $cpcfg) {
3315
			if (empty($cpcfg['zoneid'])) {
3316
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3317
				$zoneid += 2;
3318
			} else if ($cpcfg['zoneid'] > 4000) {
3319
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3320
				$zoneid += 2;
3321
			}
3322
		}
3323
	}
3324
}
3325

    
3326
function upgrade_105_to_106() {
3327

    
3328
	/* NOTE: This entry can be reused for something else since the upgrade code was reverted */
3329
}
3330

    
3331
function upgrade_106_to_107() {
3332
	global $config;
3333

    
3334
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
3335
		$tracker = (int)microtime(true);
3336
		foreach ($config['filter']['rule'] as $ridx => $rule) {
3337
			if (empty($rule['tracker'])) {
3338
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
3339
				$tracker++;
3340
			}
3341
		}
3342
		unset($tracker, $ridx);
3343
	}
3344
	if (is_array($config['nat']) && is_array($config['nat']['rule'])) {
3345
		$tracker = (int)microtime(true);
3346
		foreach ($config['nat']['rule'] as $ridx => $rule) {
3347
			if (empty($rule['tracker'])) {
3348
				$config['nat']['rule'][$ridx]['tracker'] = $tracker;
3349
				$tracker++;
3350
			}
3351

    
3352
		}
3353
		unset($tracker, $ridx);
3354
	}
3355
}
3356

    
3357
function upgrade_107_to_108() {
3358
	global $config;
3359

    
3360
	if (isset($config['system']['webgui']['noautocomplete']))
3361
		unset($config['system']['webgui']['noautocomplete']);
3362
	else
3363
		$config['system']['webgui']['loginautocomplete'] = true;
3364
}
3365

    
3366
function upgrade_108_to_109() {
3367
	global $config;
3368

    
3369
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule']))
3370
		return;
3371

    
3372
	foreach ($config['filter']['rule'] as &$rule) {
3373
		if (!isset($rule['dscp']) || empty($rule['dscp']))
3374
			continue;
3375

    
3376
		$pos = strpos($rule['dscp'], ' ');
3377
		if ($pos !== false)
3378
			$rule['dscp'] = substr($rule['dscp'], 0, $pos);
3379
		unset($pos);
3380
	}
3381
}
3382

    
3383
function upgrade_109_to_110() {
3384
	global $config;
3385

    
3386
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2']))
3387
		return;
3388

    
3389
	foreach ($config['ipsec']['phase2'] as &$rule) {
3390
		if (!empty($rule['uniqid']))
3391
			continue;
3392

    
3393
		$rule['uniqid'] = uniqid();
3394
	}
3395
}
3396

    
3397
function upgrade_110_to_111() {
3398
	global $config;
3399

    
3400
	/* Make sure unbound user exist */
3401
	mwexec('/usr/sbin/pw groupadd -n unbound -g 59', true);
3402
	mwexec('/usr/sbin/pw useradd -n unbound -c "Unbound DNS Resolver" -d /var/unbound -s /usr/sbin/nologin -u 59 -g 59', true);
3403

    
3404
	/* cleanup old unbound package stuffs */
3405
	unlink_if_exists("/usr/local/pkg/unbound.xml");
3406
	unlink_if_exists("/usr/local/pkg/unbound.inc");
3407
	unlink_if_exists("/usr/local/pkg/unbound_advanced.xml");
3408
	unlink_if_exists("/usr/local/www/unbound_status.php");
3409
	unlink_if_exists("/usr/local/www/unbound_acls.php");
3410
	unlink_if_exists("/usr/local/bin/unbound_monitor.sh");
3411
	unlink_if_exists("/usr/local/etc/rc.d/unbound.sh");
3412

    
3413
	/* Remove old menu and service entries */
3414
	if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
3415
		foreach ($config['installedpackages']['menu'] as $idx => $menu) {
3416
			if ($menu['name'] != 'Unbound DNS')
3417
				continue;
3418

    
3419
			unset($config['installedpackages']['menu'][$idx]);
3420
			break;
3421
		}
3422
	}
3423

    
3424
	if (isset($config['installedpackages']['service']) && is_array($config['installedpackages']['service'])) {
3425
		foreach ($config['installedpackages']['service'] as $idx => $service) {
3426
			if ($service['name'] != 'unbound')
3427
				continue;
3428
			unset($config['installedpackages']['service'][$idx]);
3429
			break;
3430
		}
3431
	}
3432

    
3433
	if (!isset($config['installedpackages']['unbound']['config'][0]))
3434
		return;
3435

    
3436
	$pkg = $config['installedpackages']['unbound']['config'][0];
3437

    
3438
	if (isset($config['installedpackages']['unboundadvanced']['config'][0]))
3439
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3440

    
3441
	$new = array();
3442

    
3443
	/* deal first with boolean fields */
3444
	$fields = array(
3445
		"enable" => "enable",
3446
		"dnssec_status" => "dnssec",
3447
		"forwarding_mode" => "forwarding",
3448
		"regdhcp" => "regdhcp",
3449
		"regdhcpstatic" => "regdhcpstatic",
3450
		"txtsupport" => "txtsupport",
3451
		"hide_id" => "hideidentity",
3452
		"hide_version" => "hideversion",
3453
		"prefetch" => "prefetch",
3454
		"prefetch_key" => "prefetchkey",
3455
		"harden_glue" => "hardenglue",
3456
		"harden_dnssec_stripped" => "dnssec_stripped");
3457

    
3458
	foreach ($fields as $oldk => $newk) {
3459
		if (isset($pkg[$oldk])) {
3460
			if ($pkg[$oldk] == 'on')
3461
				$new[$newk] = true;
3462
			unset($pkg[$oldk]);
3463
		}
3464
	}
3465

    
3466
	$fields = array(
3467
		"active_interface" => "network_interface",
3468
		"query_interface" => "outgoing_interface",
3469
		"unbound_verbosity" => "log_verbosity",
3470
		"unbound_verbosity" => "log_verbosity",
3471
		"msg_cache_size" => "msgcachesize",
3472
		"outgoing_num_tcp" => "outgoing_num_tcp",
3473
		"incoming_num_tcp" => "incoming_num_tcp",
3474
		"edns_buffer_size" => "edns_buffer_size",
3475
		"num_queries_per_thread" => "num_queries_per_thread",
3476
		"jostle_timeout" => "jostle_timeout",
3477
		"cache_max_ttl" => "cache_max_ttl",
3478
		"cache_min_ttl" => "cache_min_ttl",
3479
		"infra_host_ttl" => "infra_host_ttl",
3480
		"infra_cache_numhosts" => "infra_cache_numhosts",
3481
		"unwanted_reply_threshold" => "unwanted_reply_threshold",
3482
		"custom_options" => "custom_options");
3483

    
3484
	foreach ($fields as $oldk => $newk) {
3485
		if (isset($pkg[$oldk])) {
3486
			$new[$newk] = $pkg[$oldk];
3487
			unset($pkg[$oldk]);
3488
		}
3489
	}
3490

    
3491
	if (isset($new['custom_options']) && !empty($new['custom_options']))
3492
		$new['custom_options'] = str_replace("\r\n", "\n", $new['custom_options']);
3493

    
3494
	/* Following options were removed, bring them as custom_options */
3495
	if (isset($pkg['stats']) && $pkg['stats'] == "on") {
3496
		if (isset($pkg['stats_interval']))
3497
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-interval: {$pkg['stats_interval']}";
3498
		if (isset($pkg['cumulative_stats']))
3499
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-cumulative: {$pkg['cumulative_stats']}";
3500
		if (isset($pkg['extended_stats']) && $pkg['extended_stats'] == "on")
3501
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: yes";
3502
		else
3503
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: no";
3504
	}
3505

    
3506
	$new['acls'] = array();
3507
	if (isset($config['installedpackages']['unboundacls']['config']) &&
3508
	    is_array($config['installedpackages']['unboundacls']['config'])) {
3509
		foreach ($config['installedpackages']['unboundacls']['config'] as $acl)
3510
			$new['acls'][] = $acl;
3511
	}
3512

    
3513
	$config['unbound'] = $new;
3514

    
3515
	if(isset($config['installedpackages']['unbound']))
3516
		unset($config['installedpackages']['unbound']);
3517
	if(isset($config['installedpackages']['unboundadvanced']))
3518
		unset($config['installedpackages']['unboundadvanced']);
3519
	if(isset($config['installedpackages']['unboundacls']))
3520
		unset($config['installedpackages']['unboundacls']);
3521

    
3522
	unset($pkg, $new);
3523
}
3524

    
3525
function upgrade_111_to_112() {
3526
	global $config;
3527

    
3528
	$config['cron']['item'][] = array(
3529
		'minute' => '*/60',
3530
		'hour' => '*',
3531
		'mday' => '*',
3532
		'month' => '*',
3533
		'wday' => '*',
3534
		'who' => 'root',
3535
		'command' => '/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 webConfiguratorlockout'
3536
	);
3537
}
3538

    
3539
function upgrade_112_to_113() {
3540
	global $config;
3541

    
3542
	if (isset($config['notifications']['smtp']['ssl'])) {
3543
		if ($config['notifications']['smtp']['ssl'] == "checked") {
3544
			$config['notifications']['smtp']['ssl'] = true;
3545
		} else {
3546
			unset($config['notifications']['smtp']['ssl']);
3547
		}
3548
	}
3549

    
3550
	if (isset($config['notifications']['smtp']['tls'])) {
3551
		if ($config['notifications']['smtp']['tls'] == "checked") {
3552
			$config['notifications']['smtp']['tls'] = true;
3553
		} else {
3554
			unset($config['notifications']['smtp']['tls']);
3555
		}
3556
	}
3557
}
3558

    
3559
function upgrade_113_to_114() {
3560
	global $config;
3561

    
3562
	if (!isset($config['ipsec']['phase1']) ||
3563
	    !is_array($config['ipsec']['phase1']))
3564
		return;
3565

    
3566
	foreach($config['ipsec']['phase1'] as &$ph1ent)
3567
		if (!isset($ph1ent['iketype']))
3568
			$ph1ent['iketype'] = 'ikev1';
3569
}
3570

    
3571
function upgrade_114_to_115() {
3572
	global $config;
3573

    
3574
	if (isset($config['unbound']['custom_options']))
3575
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3576
}
3577

    
3578
function upgrade_115_to_116() {
3579
	global $config;
3580

    
3581
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2']))
3582
                return;
3583

    
3584
        $keyid = 1;
3585
        foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
3586
                $config['ipsec']['phase2'][$idx]['reqid'] = $keyid;
3587
		$keyid++;
3588
	}
3589
}
3590

    
3591
function upgrade_116_to_117() {
3592
	global $config;
3593

    
3594
	if (!isset($config['ipsec']['client']) ||
3595
	    !isset($config['ipsec']['client']['dns_split']) ||
3596
	    empty($config['ipsec']['client']['dns_split'])) {
3597
		return;
3598
	}
3599

    
3600
	$config['ipsec']['client']['dns_split'] =
3601
		preg_replace('/\s*,\s*/', ' ', trim($config['ipsec']['client']['dns_split']));
3602

    
3603
}
3604

    
3605
function upgrade_117_to_118() {
3606
	global $config;
3607

    
3608
	// Unset any old CA and Cert in the system section that might still be there from when upgrade_066_to_067 did not unset them.
3609
	if (isset($config['system']['ca'])) {
3610
		unset($config['system']['ca']);
3611
	}
3612
	if (isset($config['system']['cert'])) {
3613
		unset($config['system']['cert']);
3614
	}
3615

    
3616
	if (!isset($config['ipsec']['phase1'])) {
3617
		return;
3618
	}
3619

    
3620
	$a_phase1 =& $config['ipsec']['phase1'];
3621

    
3622
	foreach ($a_phase1 as &$ph1_entry) {
3623
		// update asn1dn strings from racoon's format to strongswan's 
3624
		if (isset($ph1_entry['myid_type']) && $ph1_entry['myid_type'] == 'asn1dn') {
3625
			$ph1_entry['myid_data'] =
3626
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['myid_data']);
3627
		}
3628
		if (isset($ph1_entry['peerid_type']) && $ph1_entry['peerid_type'] == 'asn1dn') {
3629
			$ph1_entry['peerid_data'] =
3630
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['peerid_data']);
3631
		}
3632
		// iketype 'auto' was removed and is really v2, update accordingly
3633
		if ($ph1_entry['iketype'] == "auto") {
3634
			$ph1_entry['iketype'] = "ikev2";
3635
		}
3636
	}
3637
}
3638

    
3639
function upgrade_118_to_119() {
3640
	global $config;
3641

    
3642
        if (!isset($config['ipsec']['phase1'])) {
3643
                return;
3644
        }
3645

    
3646
	// change peerid_type to 'any' for EAP types to retain previous behavior of omitting rightid
3647
	$a_phase1 =& $config['ipsec']['phase1'];
3648

    
3649
	foreach ($a_phase1 as &$ph1_entry) {
3650
		if (strstr($ph1_entry['authentication_method'], 'eap')) {
3651
			$ph1_entry['peerid_type'] = "any";	
3652
		}
3653
	}
3654
}
3655

    
3656
?>
(55-55/68)