Project

General

Profile

Download (107 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['interfaces']['wan']['gateway'] = $config['interfaces']['wan']['gateway'];
258
	unset($config['interfaces']['wan']['gateway']);
259

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

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

    
272

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

    
287

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

    
300

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

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

    
354

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

    
360

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

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

    
384

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

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

    
399

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

    
404

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

    
411

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
492

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

    
497

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

    
502

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

    
516

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

    
523

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

    
529

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

    
535

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

    
541

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

    
547

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

    
553

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

    
559

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

    
565

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

    
571

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

    
577

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

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

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

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

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

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

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

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

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

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

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

    
649
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
650
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
651
		$config['sysctl']['item'][4]['value'] =   "default";
652

    
653
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
654
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
655
		$config['sysctl']['item'][5]['value'] =   "default";
656

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

    
661
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
662
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
663
		$config['sysctl']['item'][7]['value'] =   "default";
664

    
665
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
666
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
667
		$config['sysctl']['item'][8]['value'] =   "default";
668

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

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

    
677
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
678
		$config['sysctl']['item'][11]['descr'] =    gettext("Maximum outgoing UDP datagram size");
679
		$config['sysctl']['item'][11]['value'] =   "default";
680

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

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

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

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

    
697
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
698
		$config['sysctl']['item'][17]['descr'] =    gettext("Set ICMP Limits");
699
		$config['sysctl']['item'][17]['value'] =   "default";
700

    
701
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
702
		$config['sysctl']['item'][18]['descr'] =    gettext("TCP Offload engine");
703
		$config['sysctl']['item'][18]['value'] =   "default";
704

    
705
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
706
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
707
		$config['sysctl']['item'][19]['value'] =   "default";
708

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

    
713
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
714
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
715
		$config['sysctl']['item'][21]['value'] =   "default";
716

    
717
	}
718
}
719

    
720

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

    
729

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

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

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

    
782

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

    
786
	/* migrate static routes to the new gateways config */
787
	$gateways = return_gateways_array(true);
788
	$i = 0;
789
	if (is_array($config['staticroutes']['route'])) {
790
		$gwmap = array();
791
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
792
			$found = false;
793
			foreach ($gateways as $gwname => $gw) {
794
				if ($gw['gateway'] == $sroute['gateway']) {
795
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
796
					$found = true;
797
					break;
798
				}
799
			}
800
			if($gwmap[$sroute['gateway']]) {
801
				/* We already added a gateway name for this IP */
802
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
803
				$found = true;
804
			}
805

    
806
			if ($found == false) {
807
				$gateway = array();
808
				$gateway['name'] = "SROUTE{$i}";
809
				$gwmap[$sroute['gateway']] = $gateway['name'];
810
				$gateway['gateway'] = $sroute['gateway'];
811
				$gateway['interface'] = $sroute['interface'];
812
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
813
				if (!is_array($config['gateways']['gateway_item']))
814
					$config['gateways']['gateway_item'] = array();
815
				$config['gateways']['gateway_item'][] = $gateway;
816
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
817
				$i++;
818
			}
819
		}
820
	}
821
}
822

    
823

    
824
function upgrade_044_to_045() {
825
	global $config;
826
	$iflist = get_configured_interface_list(false, true);
827
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
828
		$i = 0;
829
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
830
			/* Make sure to update the interfaces section with the right name */
831
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
832
			foreach($iflist as $ifname) {
833
				if($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
834
					$config['interfaces'][$ifname]['if'] = $vlan_name;
835
					continue;
836
				}
837
			}
838
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
839
			$i++;
840
		}
841
	}
842
}
843

    
844

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

    
928

    
929
function upgrade_046_to_047() {
930
	global $config;
931
	/* Upgrade IPsec from tunnel to phase1/phase2 */
932

    
933
	if(is_array($config['ipsec']['tunnel'])) {
934

    
935
		$a_phase1 = array();
936
		$a_phase2 = array();
937
		$ikeid = 0;
938

    
939
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
940

    
941
			unset($ph1ent);
942
			unset($ph2ent);
943

    
944
			/*
945
				*  attempt to locate an enabled phase1
946
				*  entry that matches the peer gateway
947
				*/
948

    
949
			if (!isset($tunnel['disabled'])) {
950

    
951
				$remote_gateway = $tunnel['remote-gateway'];
952

    
953
				foreach ($a_phase1 as $ph1tmp) {
954
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
955
						$ph1ent = $ph1tmp;
956
						break;
957
					}
958
				}
959
			}
960

    
961
			/* none found, create a new one */
962

    
963
			if (!isset( $ph1ent )) {
964

    
965
				/* build new phase1 entry */
966

    
967
				$ph1ent = array();
968

    
969
				$ph1ent['ikeid'] = ++$ikeid;
970

    
971
				if (isset($tunnel['disabled']))
972
					$ph1ent['disabled'] = $tunnel['disabled'];
973

    
974
				/* convert to the new vip[$vhid] name */
975
				if(preg_match("/^carp/", $tunnel['interface'])) {
976
					$carpid = str_replace("carp", "", $tunnel['interface']);
977
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
978
				}
979
				$ph1ent['interface'] = $tunnel['interface'];
980
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
981
				$ph1ent['descr'] = $tunnel['descr'];
982

    
983
				$ph1ent['mode'] = $tunnel['p1']['mode'];
984

    
985
				if (isset($tunnel['p1']['myident']['myaddress']))
986
					$ph1ent['myid_type'] = "myaddress";
987
				if (isset($tunnel['p1']['myident']['address'])) {
988
					$ph1ent['myid_type'] = "address";
989
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
990
				}
991
				if (isset($tunnel['p1']['myident']['fqdn'])) {
992
					$ph1ent['myid_type'] = "fqdn";
993
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
994
				}
995
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
996
					$ph1ent['myid_type'] = "user_fqdn";
997
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
998
				}
999
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
1000
					$ph1ent['myid_type'] = "asn1dn";
1001
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1002
				}
1003
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1004
					$ph1ent['myid_type'] = "dyn_dns";
1005
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1006
				}
1007

    
1008
				$ph1ent['peerid_type'] = "peeraddress";
1009

    
1010
				switch ($tunnel['p1']['encryption-algorithm']) {
1011
					case "des":
1012
					$ph1alg = array( 'name' => 'des' );
1013
					break;
1014
					case "3des":
1015
					$ph1alg = array( 'name' => '3des' );
1016
					break;
1017
					case "blowfish":
1018
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1019
					break;
1020
					case "cast128":
1021
					$ph1alg = array( 'name' => 'cast128' );
1022
					break;
1023
					case "rijndael":
1024
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1025
					break;
1026
					case "rijndael 256":
1027
					case "aes 256":
1028
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1029
					break;
1030
				}
1031

    
1032
				$ph1ent['encryption-algorithm'] = $ph1alg;
1033
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1034
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1035
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1036
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1037

    
1038
				if (isset($tunnel['p1']['pre-shared-key']))
1039
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1040
				if (isset($tunnel['p1']['cert']))
1041
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1042
				if (isset($tunnel['p1']['peercert']))
1043
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1044
				if (isset($tunnel['p1']['private-key']))
1045
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1046

    
1047
				$ph1ent['nat_traversal'] = "on";
1048
				$ph1ent['dpd_enable'] = 1;
1049
				$ph1ent['dpd_delay'] = 10;
1050
				$ph1ent['dpd_maxfail'] = 5;
1051

    
1052
				$a_phase1[] = $ph1ent;
1053
			}
1054

    
1055
			/* build new phase2 entry */
1056

    
1057
			$ph2ent = array();
1058

    
1059
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1060

    
1061
			if (isset($tunnel['disabled']))
1062
				$ph1ent['disabled'] = $tunnel['disabled'];
1063

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

    
1066
			$type = "lan";
1067
			if ($tunnel['local-subnet']['network'])
1068
				$type = $tunnel['local-subnet']['network'];
1069
			if ($tunnel['local-subnet']['address']) {
1070
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1071
				if (is_null($netbits))
1072
					$type = "address";
1073
				else
1074
					$type = "network";
1075
			}
1076

    
1077
			switch ($type) {
1078
				case "address":
1079
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1080
				break;
1081
				case "network":
1082
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1083
				break;
1084
				default:
1085
				$ph2ent['localid'] = array('type' => $type);
1086
				break;
1087
			}
1088

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

    
1092
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1093

    
1094
			$aes_count = 0;
1095
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1096
				$aes_found = false;
1097
				switch ($tunalg) {
1098
					case "des":
1099
					$ph2alg = array( 'name' => 'des' );
1100
					break;
1101
					case "3des":
1102
					$ph2alg = array( 'name' => '3des' );
1103
					break;
1104
					case "blowfish":
1105
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1106
					break;
1107
					case "cast128":
1108
					$ph2alg = array( 'name' => 'cast128' );
1109
					break;
1110
					case "rijndael":
1111
					case "rijndael 256":
1112
					case "aes 256":
1113
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1114
					$aes_found = true;
1115
					$aes_count++;
1116
					break;
1117
				}
1118

    
1119
				if( !$aes_found || ($aes_count < 2))
1120
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1121
			}
1122

    
1123
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1124
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1125
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1126

    
1127
			if (isset($tunnel['pinghost']['pinghost']))
1128
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1129

    
1130
			$a_phase2[] = $ph2ent;
1131
		}
1132

    
1133
		unset($config['ipsec']['tunnel']);
1134
		$config['ipsec']['phase1'] = $a_phase1;
1135
		$config['ipsec']['phase2'] = $a_phase2;
1136
	}
1137

    
1138
	/* Upgrade Mobile IPsec */
1139
	if (isset($config['ipsec']['mobileclients'])
1140
		&& is_array($config['ipsec']['mobileclients'])
1141
		&& is_array($config['ipsec']['mobileclients']['p1'])
1142
		&& is_array($config['ipsec']['mobileclients']['p2'])) {
1143

    
1144
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1145
			$config['ipsec']['client']['enable'] = true;
1146
			$config['ipsec']['client']['user_source'] = 'system';
1147
			$config['ipsec']['client']['group_source'] = 'system';
1148
		}
1149

    
1150
		$mobilecfg = $config['ipsec']['mobileclients'];
1151

    
1152
		$ph1ent = array();
1153
		$ph1ent['ikeid'] = ++$ikeid;
1154

    
1155
		if (!isset($mobilecfg['enable']))
1156
			$ph1ent['disabled'] = true;
1157

    
1158
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1159
		$ph1ent['interface'] = 'wan';
1160
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1161
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1162

    
1163
		if (isset($mobilecfg['p1']['myident']['myaddress']))
1164
			$ph1ent['myid_type'] = "myaddress";
1165
		if (isset($mobilecfg['p1']['myident']['address'])) {
1166
			$ph1ent['myid_type'] = "address";
1167
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1168
		}
1169
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1170
			$ph1ent['myid_type'] = "fqdn";
1171
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1172
		}
1173
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1174
			$ph1ent['myid_type'] = "user_fqdn";
1175
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1176
		}
1177
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1178
			$ph1ent['myid_type'] = "asn1dn";
1179
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1180
		}
1181
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1182
			$ph1ent['myid_type'] = "dyn_dns";
1183
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1184
		}
1185
		$ph1ent['peerid_type'] = "fqdn";
1186
		$ph1ent['peerid_data'] = "";
1187

    
1188
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1189
			case "des":
1190
			$ph1alg = array( 'name' => 'des' );
1191
			break;
1192
			case "3des":
1193
			$ph1alg = array( 'name' => '3des' );
1194
			break;
1195
			case "blowfish":
1196
			$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1197
			break;
1198
			case "cast128":
1199
			$ph1alg = array( 'name' => 'cast128' );
1200
			break;
1201
			case "rijndael":
1202
			$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1203
			break;
1204
			case "rijndael 256":
1205
			case "aes 256":
1206
			$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1207
			break;
1208
		}
1209

    
1210
		$ph1ent['encryption-algorithm'] = $ph1alg;
1211
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1212
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1213
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1214
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1215

    
1216
		if (isset($mobilecfg['p1']['cert']))
1217
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1218
		if (isset($mobilecfg['p1']['peercert']))
1219
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1220
		if (isset($mobilecfg['p1']['private-key']))
1221
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1222

    
1223
		$ph1ent['nat_traversal'] = "on";
1224
		$ph1ent['dpd_enable'] = 1;
1225
		$ph1ent['dpd_delay'] = 10;
1226
		$ph1ent['dpd_maxfail'] = 5;
1227
		$ph1ent['mobile'] = true;
1228

    
1229
		$ph2ent = array();
1230
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1231
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1232
		$ph2ent['localid'] = array('type' => 'none');
1233
		$ph2ent['remoteid'] = array('type' => 'mobile');
1234
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1235

    
1236
		$aes_count = 0;
1237
		foreach( $mobilecfg['p2']['encryption-algorithm-option'] as $tunalg ) {
1238
			$aes_found = false;
1239
			switch ($tunalg) {
1240
				case "des":
1241
				$ph2alg = array( 'name' => 'des' );
1242
				break;
1243
				case "3des":
1244
				$ph2alg = array( 'name' => '3des' );
1245
				break;
1246
				case "blowfish":
1247
				$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1248
				break;
1249
				case "cast128":
1250
				$ph2alg = array( 'name' => 'cast128' );
1251
				break;
1252
				case "rijndael":
1253
				case "rijndael 256":
1254
				case "aes 256":
1255
				$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1256
				$aes_found = true;
1257
				$aes_count++;
1258
				break;
1259
			}
1260

    
1261
			if( !$aes_found || ($aes_count < 2))
1262
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1263
		}
1264
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1265
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1266
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1267
		$ph2ent['mobile'] = true;
1268

    
1269
		$config['ipsec']['phase1'][] = $ph1ent;
1270
		$config['ipsec']['phase2'][] = $ph2ent;
1271
		unset($config['ipsec']['mobileclients']);
1272
	}
1273
}
1274

    
1275

    
1276
function upgrade_047_to_048() {
1277
	global $config;
1278
	if (!empty($config['dyndns'])) {
1279
		$config['dyndnses'] = array();
1280
		$config['dyndnses']['dyndns'] = array();
1281
		if(isset($config['dyndns'][0]['host'])) {
1282
			$tempdyn = array();
1283
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1284
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1285
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1286
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1287
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1288
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1289
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];
1290
			$tempdyn['interface'] = "wan";
1291
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1292
			$config['dyndnses']['dyndns'][] = $tempdyn;
1293
		}
1294
		unset($config['dyndns']);
1295
	}
1296
	if (!empty($config['dnsupdate'])) {
1297
		$pconfig = $config['dnsupdate'][0];
1298
		if (!$pconfig['ttl'])
1299
			$pconfig['ttl'] = 60;
1300
		if (!$pconfig['keytype'])
1301
			$pconfig['keytype'] = "zone";
1302
		$pconfig['interface'] = "wan";
1303
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1304
		unset($config['dnsupdate']);
1305
	}
1306

    
1307
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1308
		$pconfig = array();
1309
		$pconfig['username'] = $config['pppoe'][0]['username'];
1310
		$pconfig['password'] = $config['pppoe'][0]['password'];
1311
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1312
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1313
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1314
		unset($config['pppoe']);
1315
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1316
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1317
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1318
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1319
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1320
	}
1321
	if (is_array($config['pptp'])) {
1322
		$pconfig = array();
1323
		$pconfig['username'] = $config['pptp']['username'];
1324
		$pconfig['password'] = $config['pptp']['password'];
1325
		$pconfig['provider'] = $config['pptp']['provider'];
1326
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1327
		$pconfig['timeout'] = $config['pptp']['timeout'];
1328
		unset($config['pptp']);
1329
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1330
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1331
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1332
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1333
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1334
	}
1335
}
1336

    
1337

    
1338
function upgrade_048_to_049() {
1339
	global $config;
1340
	/* setup new all users group */
1341
	$all = array();
1342
	$all['name'] = "all";
1343
	$all['description'] = gettext("All Users");
1344
	$all['scope'] = "system";
1345
	$all['gid'] = 1998;
1346
	$all['member'] = array();
1347

    
1348
	if (!is_array($config['system']['user']))
1349
		$config['system']['user'] = array();
1350
	if (!is_array($config['system']['group']))
1351
		$config['system']['group'] = array();
1352

    
1353
	/* work around broken uid assignments */
1354
	$config['system']['nextuid'] = 2000;
1355
	foreach ($config['system']['user'] as & $user) {
1356
		if (isset($user['uid']) && !$user['uid'])
1357
			continue;
1358
		$user['uid'] = $config['system']['nextuid']++;
1359
	}
1360

    
1361
	/* work around broken gid assignments */
1362
	$config['system']['nextgid'] = 2000;
1363
	foreach ($config['system']['group'] as & $group) {
1364
		if ($group['name'] == $g['admin_group'])
1365
			$group['gid'] = 1999;
1366
		else
1367
			$group['gid'] = $config['system']['nextgid']++;
1368
	}
1369

    
1370
	/* build group membership information */
1371
	foreach ($config['system']['group'] as & $group) {
1372
		$group['member'] = array();
1373
		foreach ($config['system']['user'] as & $user) {
1374
			$groupnames = explode(",", $user['groupname']);
1375
			if (in_array($group['name'],$groupnames))
1376
				$group['member'][] = $user['uid'];
1377
		}
1378
	}
1379

    
1380
	/* reset user group information */
1381
	foreach ($config['system']['user'] as & $user) {
1382
		unset($user['groupname']);
1383
		$all['member'][] = $user['uid'];
1384
	}
1385

    
1386
	/* reset group scope information */
1387
	foreach ($config['system']['group'] as & $group)
1388
		if ($group['name'] != $g['admin_group'])
1389
		$group['scope'] = "user";
1390

    
1391
	/* insert new all group */
1392
	$groups = Array();
1393
	$groups[] = $all;
1394
	$groups = array_merge($config['system']['group'],$groups);
1395
	$config['system']['group'] = $groups;
1396
}
1397

    
1398

    
1399
function upgrade_049_to_050() {
1400
	global $config;
1401

    
1402
	if (!is_array($config['system']['user']))
1403
		$config['system']['user'] = array();
1404
	/* update user privileges */
1405
	foreach ($config['system']['user'] as & $user) {
1406
		$privs = array();
1407
		if (!is_array($user['priv'])) {
1408
			unset($user['priv']);
1409
			continue;
1410
		}
1411
		foreach ($user['priv'] as $priv) {
1412
			switch($priv['id']) {
1413
				case "hasshell":
1414
				$privs[] = "user-shell-access";
1415
				break;
1416
				case "copyfiles":
1417
				$privs[] = "user-copy-files";
1418
				break;
1419
			}
1420
		}
1421
		$user['priv'] = $privs;
1422
	}
1423

    
1424
	/* update group privileges */
1425
	foreach ($config['system']['group'] as & $group) {
1426
		$privs = array();
1427
		if (!is_array($group['pages'])) {
1428
			unset($group['pages']);
1429
			continue;
1430
		}
1431
		foreach ($group['pages'] as $page) {
1432
			$priv = map_page_privname($page);
1433
			if ($priv)
1434
				$privs[] = $priv;
1435
		}
1436
		unset($group['pages']);
1437
		$group['priv'] = $privs;
1438
	}
1439

    
1440
	/* sync all local account information */
1441
	local_sync_accounts();
1442
}
1443

    
1444

    
1445
function upgrade_050_to_051() {
1446
	global $config;
1447
	$pconfig = array();
1448
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1449
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1450
	$pconfig['value'] = "1";
1451
	$config['sysctl']['item'][] = $pconfig;
1452
	$pconfig = array();
1453
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1454
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1455
	$pconfig['value'] = "0";
1456
	$config['sysctl']['item'][] = $pconfig;
1457

    
1458
	unset($config['bridge']);
1459

    
1460
	$convert_bridges = false;
1461
	foreach($config['interfaces'] as $intf) {
1462
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1463
			$config['bridges'] = array();
1464
			$config['bridges']['bridged'] = array();
1465
			$convert_bridges = true;
1466
			break;
1467
		}
1468
	}
1469
	if ($convert_bridges == true) {
1470
		$i = 0;
1471
		foreach ($config['interfaces'] as $ifr => &$intf) {
1472
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1473
				$nbridge = array();
1474
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1475
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1476
				$nbridge['bridgeif'] = "bridge{$i}";
1477
				$config['bridges']['bridged'][] = $nbridge;
1478
				unset($intf['bridge']);
1479
				$i++;
1480
			}
1481
		}
1482
	}
1483
}
1484

    
1485

    
1486
function upgrade_051_to_052() {
1487
	global $config;
1488
	$config['openvpn'] = array();
1489
	if (!is_array($config['ca']))
1490
		$config['ca'] = array();
1491
	if (!is_array($config['cert']))
1492
		$config['cert'] = array();
1493

    
1494
	$vpnid = 1;
1495

    
1496
	/* openvpn server configurations */
1497
	if (is_array($config['installedpackages']['openvpnserver'])) {
1498
		$config['openvpn']['openvpn-server'] = array();
1499

    
1500
		$index = 1;
1501
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1502

    
1503
			if (!is_array($server))
1504
				continue;
1505

    
1506
			if ($server['auth_method'] == "pki") {
1507

    
1508
				/* create ca entry */
1509
				$ca = array();
1510
				$ca['refid'] = uniqid();
1511
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1512
				$ca['crt'] = $server['ca_cert'];
1513
				$config['ca'][] = $ca;
1514

    
1515
				/* create ca reference */
1516
				unset($server['ca_cert']);
1517
				$server['caref'] = $ca['refid'];
1518

    
1519
				/* create a crl entry if needed */
1520
				if (!empty($server['crl'][0])) {
1521
					$crl = array();
1522
					$crl['refid'] = uniqid();
1523
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1524
					$crl['caref'] = $ca['refid'];
1525
					$crl['text'] = $server['crl'][0];
1526
					if(!is_array($config['crl']))
1527
						$config['crl'] = array();
1528
					$config['crl'][] = $crl;
1529
					$server['crlref'] = $crl['refid'];
1530
				}
1531
				unset($server['crl']);
1532

    
1533
				/* create cert entry */
1534
				$cert = array();
1535
				$cert['refid'] = uniqid();
1536
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1537
				$cert['crt'] = $server['server_cert'];
1538
				$cert['prv'] = $server['server_key'];
1539
				$config['cert'][] = $cert;
1540

    
1541
				/* create cert reference */
1542
				unset($server['server_cert']);
1543
				unset($server['server_key']);
1544
				$server['certref'] = $cert['refid'];
1545

    
1546
				$index++;
1547
			}
1548

    
1549
			/* determine operational mode */
1550
			if ($server['auth_method'] == 'pki') {
1551
				if($server['nopool']) {
1552
					$server['mode'] = "p2p_tls";
1553
				} else {
1554
					$server['mode'] = "server_tls";
1555
				}
1556
			} else {
1557
				$server['mode'] = "p2p_shared_key";
1558
			}
1559
			unset($server['auth_method']);
1560

    
1561
			/* modify configuration values */
1562
			$server['dh_length'] = 1024;
1563
			unset($server['dh_params']);
1564
			if (!$server['interface'])
1565
				$server['interface'] = 'any';
1566
			$server['tunnel_network'] = $server['addresspool'];
1567
			unset($server['addresspool']);
1568
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1569
				$server['compression'] = "on";
1570
				unset($server['use_lzo']);
1571
			}
1572
			if ($server['nopool'])
1573
				$server['pool_enable'] = false;
1574
			else
1575
				$server['pool_enable'] = "yes";
1576
			unset($server['nopool']);
1577
			$server['dns_domain'] = $server['dhcp_domainname'];
1578
			unset($server['dhcp_domainname']);
1579

    
1580
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1581
			$d=1;
1582
			foreach ($tmparr as $tmpa) {
1583
				$server["dns_server{$d}"] = $tmpa;
1584
				$d++;
1585
			}
1586
			unset($server['dhcp_dns']);
1587

    
1588
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1589
			$d=1;
1590
			foreach ($tmparr as $tmpa) {
1591
				$server["ntp_server{$d}"] = $tmpa;
1592
				$d++;
1593
			}
1594
			unset($server['dhcp_ntp']);
1595

    
1596
			if ($server['dhcp_nbtdisable'])
1597
				$server['netbios_enable'] = false;
1598
			else
1599
				$server['netbios_enable'] = "yes";
1600
			unset($server['dhcp_nbtdisable']);
1601
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1602
			unset($server['dhcp_nbttype']);
1603
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1604
			unset($server['dhcp_nbtscope']);
1605

    
1606
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1607
			$d=1;
1608
			foreach ($tmparr as $tmpa) {
1609
				$server["nbdd_server{$d}"] = $tmpa;
1610
				$d++;
1611
			}
1612
			unset($server['dhcp_nbdd']);
1613

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

    
1622
			if (!empty($server['disable']))
1623
				$server['disable'] = true;
1624
			else
1625
				unset($server['disable']);
1626

    
1627
			/* allocate vpnid */
1628
			$server['vpnid'] = $vpnid++;
1629

    
1630
			if (!empty($server['custom_options'])) {
1631
				$cstmopts = array();
1632
				$tmpcstmopts = explode(";", $server['custom_options']);
1633
				$assigned_if = "";
1634
				$tmpstr = "";
1635
				foreach ($tmpcstmopts as $tmpcstmopt) {
1636
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1637
					if (substr($tmpstr,0 ,6) == "devtun") {
1638
						$assigned_if = substr($tmpstr, 3);
1639
						continue;
1640
					} else if (substr($tmpstr, 0, 5) == "local") {
1641
						$localip = substr($tmpstr, 5);
1642
						$server['ipaddr'] = str_replace("\n", "", $localip);
1643
					} else
1644
						$cstmopts[] = $tmpcstmopt;
1645
				}
1646
				$server['custom_options'] = implode(";", $cstmopts);
1647
				if (!empty($assigned_if)) {
1648
					foreach ($config['interfaces'] as $iface => $cfgif) {
1649
						if ($cfgif['if'] == $assigned_if) {
1650
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1651
							break;
1652
						}
1653
					}
1654
				}
1655
			}
1656

    
1657
			$config['openvpn']['openvpn-server'][] = $server;
1658
		}
1659
		unset($config['installedpackages']['openvpnserver']);
1660
	}
1661

    
1662
	/* openvpn client configurations */
1663
	if (is_array($config['installedpackages']['openvpnclient'])) {
1664
		$config['openvpn']['openvpn-client'] = array();
1665

    
1666
		$index = 1;
1667
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1668

    
1669
			if (!is_array($client))
1670
				continue;
1671

    
1672
			if ($client['auth_method'] == "pki") {
1673

    
1674
				/* create ca entry */
1675
				$ca = array();
1676
				$ca['refid'] = uniqid();
1677
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1678
				$ca['crt'] = $client['ca_cert'];
1679
				$ca['crl'] = $client['crl'];
1680
				$config['ca'][] = $ca;
1681

    
1682
				/* create ca reference */
1683
				unset($client['ca_cert']);
1684
				unset($client['crl']);
1685
				$client['caref'] = $ca['refid'];
1686

    
1687
				/* create cert entry */
1688
				$cert = array();
1689
				$cert['refid'] = uniqid();
1690
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1691
				$cert['crt'] = $client['client_cert'];
1692
				$cert['prv'] = $client['client_key'];
1693
				$config['cert'][] = $cert;
1694

    
1695
				/* create cert reference */
1696
				unset($client['client_cert']);
1697
				unset($client['client_key']);
1698
				$client['certref'] = $cert['refid'];
1699

    
1700
				$index++;
1701
			}
1702

    
1703
			/* determine operational mode */
1704
			if ($client['auth_method'] == 'pki')
1705
				$client['mode'] = "p2p_tls";
1706
			else
1707
				$client['mode'] = "p2p_shared_key";
1708
			unset($client['auth_method']);
1709

    
1710
			/* modify configuration values */
1711
			if (!$client['interface'])
1712
				$client['interface'] = 'wan';
1713
			$client['tunnel_network'] = $client['interface_ip'];
1714
			unset($client['interface_ip']);
1715
			$client['server_addr'] = $client['serveraddr'];
1716
			unset($client['serveraddr']);
1717
			$client['server_port'] = $client['serverport'];
1718
			unset($client['serverport']);
1719
			$client['proxy_addr'] = $client['poxy_hostname'];
1720
			unset($client['proxy_addr']);
1721
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1722
				$client['compression'] = "on";
1723
				unset($client['use_lzo']);
1724
			}
1725
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1726
			unset($client['infiniteresolvretry']);
1727

    
1728
			/* allocate vpnid */
1729
			$client['vpnid'] = $vpnid++;
1730

    
1731
			if (!empty($client['custom_options'])) {
1732
				$cstmopts = array();
1733
				$tmpcstmopts = explode(";", $client['custom_options']);
1734
				$assigned_if = "";
1735
				$tmpstr = "";
1736
				foreach ($tmpcstmopts as $tmpcstmopt) {
1737
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1738
					if (substr($tmpstr,0 ,6) == "devtun") {
1739
						$assigned_if = substr($tmpstr, 3);
1740
						continue;
1741
					} else if (substr($tmpstr, 0, 5) == "local") {
1742
						$localip = substr($tmpstr, 5);
1743
						$client['ipaddr'] = str_replace("\n", "", $localip);
1744
					} else
1745
						$cstmopts[] = $tmpcstmopt;
1746
				}
1747
				$client['custom_options'] = implode(";", $cstmopts);
1748
				if (!empty($assigned_if)) {
1749
					foreach ($config['interfaces'] as $iface => $cfgif) {
1750
						if ($cfgif['if'] == $assigned_if) {
1751
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1752
							break;
1753
						}
1754
					}
1755
				}
1756
			}
1757

    
1758
			if (!empty($client['disable']))
1759
				$client['disable'] = true;
1760
			else
1761
				unset($client['disable']);
1762

    
1763
			$config['openvpn']['openvpn-client'][] = $client;
1764
		}
1765

    
1766
		unset($config['installedpackages']['openvpnclient']);
1767
	}
1768

    
1769
	/* openvpn client specific configurations */
1770
	if (is_array($config['installedpackages']['openvpncsc'])) {
1771
		$config['openvpn']['openvpn-csc'] = array();
1772

    
1773
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1774

    
1775
			if (!is_array($csc))
1776
				continue;
1777

    
1778
			/* modify configuration values */
1779
			$csc['common_name'] = $csc['commonname'];
1780
			unset($csc['commonname']);
1781
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1782
			unset($csc['ifconfig_push']);
1783
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1784
			unset($csc['dhcp_domainname']);
1785

    
1786
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1787
			$d=1;
1788
			foreach ($tmparr as $tmpa) {
1789
				$csc["dns_server{$d}"] = $tmpa;
1790
				$d++;
1791
			}
1792
			unset($csc['dhcp_dns']);
1793

    
1794
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1795
			$d=1;
1796
			foreach ($tmparr as $tmpa) {
1797
				$csc["ntp_server{$d}"] = $tmpa;
1798
				$d++;
1799
			}
1800
			unset($csc['dhcp_ntp']);
1801

    
1802
			if ($csc['dhcp_nbtdisable'])
1803
				$csc['netbios_enable'] = false;
1804
			else
1805
				$csc['netbios_enable'] = "yes";
1806
			unset($csc['dhcp_nbtdisable']);
1807
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1808
			unset($csc['dhcp_nbttype']);
1809
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1810
			unset($csc['dhcp_nbtscope']);
1811

    
1812
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1813
			$d=1;
1814
			foreach ($tmparr as $tmpa) {
1815
				$csc["nbdd_server{$d}"] = $tmpa;
1816
				$d++;
1817
			}
1818
			unset($csc['dhcp_nbdd']);
1819

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

    
1828
			if (!empty($csc['disable']))
1829
				$csc['disable'] = true;
1830
			else
1831
				unset($csc['disable']);
1832

    
1833
			$config['openvpn']['openvpn-csc'][] = $csc;
1834
		}
1835

    
1836
		unset($config['installedpackages']['openvpncsc']);
1837
	}
1838

    
1839
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1840
		count($config['openvpn']['openvpn-client']) > 0) {
1841
		$ovpnrule = array();
1842
		$ovpnrule['type'] = "pass";
1843
		$ovpnrule['interface'] = "openvpn";
1844
		$ovpnrule['statetype'] = "keep state";
1845
		$ovpnrule['source'] = array();
1846
		$ovpnrule['destination'] = array();
1847
		$ovpnrule['source']['any'] = true;
1848
		$ovpnrule['destination']['any'] = true;
1849
		$ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1850
		$config['filter']['rule'][] = $ovpnrule;
1851
	}
1852

    
1853
	/*
1854
		* FIXME: hack to keep things working with no installedpackages
1855
		* or carp array in the configuration data.
1856
		*/
1857
	if (!is_array($config['installedpackages']))
1858
		$config['installedpackages'] = array();
1859
	if (!is_array($config['installedpackages']['carp']))
1860
		$config['installedpackages']['carp'] = array();
1861

    
1862
}
1863

    
1864

    
1865
function upgrade_052_to_053() {
1866
	global $config;
1867
	if (!is_array($config['ca']))
1868
		$config['ca'] = array();
1869
	if (!is_array($config['cert']))
1870
		$config['cert'] = array();
1871

    
1872
	/* migrate advanced admin page webui ssl to certifcate mngr */
1873
	if ($config['system']['webgui']['certificate'] &&
1874
	$config['system']['webgui']['private-key']) {
1875

    
1876
		/* create cert entry */
1877
		$cert = array();
1878
		$cert['refid'] = uniqid();
1879
		$cert['descr'] = "webConfigurator SSL Certificate";
1880
		$cert['crt'] = $config['system']['webgui']['certificate'];
1881
		$cert['prv'] = $config['system']['webgui']['private-key'];
1882
		$config['cert'][] = $cert;
1883

    
1884
		/* create cert reference */
1885
		unset($config['system']['webgui']['certificate']);
1886
		unset($config['system']['webgui']['private-key']);
1887
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1888
	}
1889

    
1890
	/* migrate advanced admin page ssh keys to user manager */
1891
	if ($config['system']['ssh']['authorizedkeys']) {
1892
		$admin_user =& getUserEntryByUID(0);
1893
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1894
		unset($config['system']['ssh']['authorizedkeys']);
1895
	}
1896
}
1897

    
1898

    
1899
function upgrade_053_to_054() {
1900
	global $config;
1901
	if(is_array($config['load_balancer']['lbpool'])) {
1902
		$lbpool_arr = $config['load_balancer']['lbpool'];
1903
		$lbpool_srv_arr = array();
1904
		$gateway_group_arr = array();
1905
		$gateways = return_gateways_array();
1906
		$group_name_changes = array();
1907
		if (! is_array($config['gateways']['gateway_item']))
1908
			$config['gateways']['gateway_item'] = array();
1909

    
1910
		$a_gateways =& $config['gateways']['gateway_item'];
1911
		foreach($lbpool_arr as $lbpool) {
1912
			if($lbpool['type'] == "gateway") {
1913
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
1914
				$group_name = preg_replace("/[^A-Za-z0-9]/", "", $lbpool['name'] );
1915
				// If we made and changes, check for collisions and note the change.
1916
				if ($group_name != $lbpool['name']) {
1917
					// Make sure the name isn't already in use.
1918
					foreach ($gateway_group_arr as $gwg) {
1919
						// If the name is in use, add some random bits to avoid collision.
1920
						if ($gwg['name'] == $group_name)
1921
							$group_name .= uniqid();
1922
					}
1923
					$group_name_changes[$lbpool['name']] = $group_name;
1924
				}
1925
				$gateway_group['name'] = $group_name;
1926
				$gateway_group['descr'] = $lbpool['descr'];
1927
				$gateway_group['trigger'] = "down";
1928
				$gateway_group['item'] = array();
1929
				$i = 0;
1930
				foreach($lbpool['servers'] as $member) {
1931
					$split = explode("|", $member);
1932
					$interface = $split[0];
1933
					$monitor = $split[1];
1934
					/* on static upgraded configuration we automatically prepend GW_ */
1935
					$static_name = "GW_" . strtoupper($interface);
1936
					if(is_ipaddr($monitor))
1937
						foreach ($a_gateways as & $gw)
1938
							if ($gw['name'] == $static_name)
1939
								$gw['monitor'] = $monitor;
1940

    
1941
					/* on failover increment tier. Else always assign 1 */
1942
					if($lbpool['behaviour'] == "failover") {
1943
						$i++;
1944
					} else {
1945
						$i = 1;
1946
					}
1947
					$gateway_group['item'][] = "$static_name|$i";
1948
				}
1949
				$gateway_group_arr[] = $gateway_group;
1950
			} else {
1951
				$lbpool_srv_arr[] = $lbpool;
1952
			}
1953
		}
1954
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1955
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1956
	}
1957
	// Unset lbpool if we no longer have any server pools
1958
	if (count($lbpool_srv_arr) == 0) {
1959
		if(empty($config['load_balancer'])) {
1960
			unset($config['load_balancer']);
1961
		} else {
1962
			unset($config['load_balancer']['lbpool']);
1963
		}
1964
	} else {
1965
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1966
	}
1967
	// Only set the gateway group array if we converted any
1968
	if (count($gateway_group_arr) != 0) {
1969
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1970
		// Update any rules that had a gateway change, if any.
1971
		if (count($group_name_changes) > 0)
1972
			foreach ($config['filter']['rule'] as & $rule)
1973
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes))
1974
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
1975
	}
1976
}
1977

    
1978

    
1979
function upgrade_054_to_055() {
1980
	global $config;
1981
	global $g;
1982

    
1983
	/* RRD files changed for quality, traffic and packets graphs */
1984
	//ini_set("max_execution_time", "1800");
1985
	/* convert traffic RRD file */
1986
	global $parsedcfg, $listtags;
1987
	$listtags = array("ds", "v", "rra", "row");
1988

    
1989
	$rrddbpath = "/var/db/rrd/";
1990
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1991
	if ($g['platform'] != "pfSense") {
1992
		/* restore the databases, if we have one */
1993
		if (restore_rrd()) {
1994
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
1995
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
1996
		}
1997
	}
1998

    
1999
	$rrdinterval = 60;
2000
	$valid = $rrdinterval * 2;
2001

    
2002
	/* Asume GigE for now */
2003
	$downstream = 125000000;
2004
	$upstream = 125000000;
2005

    
2006
	/* build a list of quality databases */
2007
	/* roundtrip has become delay */
2008
	function divide_delay($delayval) {
2009
		$delayval = floatval($delayval);
2010
		$delayval = ($delayval / 1000);
2011
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2012
		return $delayval;
2013
	}
2014
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2015
	$databases = array();
2016
	if (!file_exists($rrddbpath))
2017
		@mkdir($rrddbpath);
2018
	chdir($rrddbpath);
2019
	$databases = glob("*-quality.rrd");
2020
	rsort($databases);
2021
	foreach($databases as $database) {
2022
		$xmldump = "{$database}.old.xml";
2023
		$xmldumpnew = "{$database}.new.xml";
2024

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

    
2029
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2030
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2031
		$rrdold = $rrdold['rrd'];
2032

    
2033
		$i = 0;
2034
		foreach($rrdold['rra'] as $rra) {
2035
			$l = 0;
2036
			foreach($rra['database']['row'] as $row) {
2037
				$vnew = divide_delay($row['v'][1]);
2038
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2039
				$l++;
2040
			}
2041
			$i++;
2042
		}
2043

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

    
2047
		unset($rrdold);
2048
		@unlink("{$g['tmp_path']}/{$xmldump}");
2049
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2050
	}
2051
	/* let apinger recreate required files */
2052
	if (!platform_booting())
2053
		setup_gateways_monitor();
2054

    
2055
	/* build a list of traffic and packets databases */
2056
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2057
	rsort($databases);
2058
	foreach($databases as $database) {
2059
		$databasetmp = "{$database}.tmp";
2060
		$xmldump = "{$database}.old.xml";
2061
		$xmldumptmp = "{$database}.tmp.xml";
2062
		$xmldumpnew = "{$database}.new.xml";
2063

    
2064
		if (platform_booting())
2065
			echo "Migrate RRD database {$database} to new format \n";
2066
		/* rename DS source */
2067
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2068
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2069

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

    
2073
		/* create new rrd database file */
2074
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2075
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2076
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2077
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2078
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2079
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2080
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2081
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2082
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2083

    
2084
		create_new_rrd("$rrdcreate");
2085
		/* create temporary xml from new RRD */
2086
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2087

    
2088
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2089
		$rrdold = $rrdold['rrd'];
2090

    
2091
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2092
		$rrdnew = $rrdnew['rrd'];
2093

    
2094
		/* remove any MAX RRA's. Not needed for traffic. */
2095
		$i = 0;
2096
		foreach ($rrdold['rra'] as $rra) {
2097
			if(trim($rra['cf']) == "MAX") {
2098
				unset($rrdold['rra'][$i]);
2099
			}
2100
			$i++;
2101
		}
2102

    
2103
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2104
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2105
		/* we now have the rrd with the new fields, adjust the size now. */
2106
		/* RRA 2 is 60 minutes, RRA 3 is 720 minutes */
2107
		mwexec("/bin/sync");
2108
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 2 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2109
		mwexec("/bin/sync");
2110
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2111
		unset($rrdxmlarray);
2112
		@unlink("{$g['tmp_path']}/{$xmldump}");
2113
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2114
	}
2115
	if (!platform_booting())
2116
		enable_rrd_graphing();
2117
	/* Let's save the RRD graphs after we run enable RRD graphing */
2118
	/* The function will restore the rrd.tgz so we will save it after */
2119
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2120
	unlink_if_exists("{$g['vardb_path']}/rrd/*.xml");
2121
	if (platform_booting())
2122
		echo "Updating configuration...";
2123
}
2124

    
2125

    
2126
function upgrade_055_to_056() {
2127
	global $config;
2128

    
2129
	if (!is_array($config['ca']))
2130
		$config['ca'] = array();
2131
	if (!is_array($config['cert']))
2132
		$config['cert'] = array();
2133

    
2134
	/* migrate ipsec ca's to cert manager */
2135
	if (is_array($config['ipsec']['cacert'])) {
2136
		foreach($config['ipsec']['cacert'] as & $cacert) {
2137
			$ca = array();
2138
			$ca['refid'] = uniqid();
2139
			if (is_array($cacert['cert']))
2140
				$ca['crt'] = $cacert['cert'][0];
2141
			else
2142
				$ca['crt'] = $cacert['cert'];
2143
			$ca['descr'] = $cacert['ident'];
2144
			$config['ca'][] = $ca;
2145
		}
2146
		unset($config['ipsec']['cacert']);
2147
	}
2148

    
2149
	/* migrate phase1 certificates to cert manager */
2150
	if (is_array($config['ipsec']['phase1'])) {
2151
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
2152
			$cert = array();
2153
			$cert['refid'] = uniqid();
2154
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2155
			if (is_array($ph1ent['cert']))
2156
				$cert['crt'] = $ph1ent['cert'][0];
2157
			else
2158
				$cert['crt'] = $ph1ent['cert'];
2159
			$cert['prv'] = $ph1ent['private-key'];
2160
			$config['cert'][] = $cert;
2161
			$ph1ent['certref'] = $cert['refid'];
2162
			if ($ph1ent['cert'])
2163
				unset($ph1ent['cert']);
2164
			if ($ph1ent['private-key'])
2165
				unset($ph1ent['private-key']);
2166
			if ($ph1ent['peercert'])
2167
				unset($ph1ent['peercert']);
2168
		}
2169
	}
2170
}
2171

    
2172

    
2173
function upgrade_056_to_057() {
2174
	global $config;
2175

    
2176
	if (!is_array($config['system']['user']))
2177
		$config['system']['user'] = array();
2178
	/* migrate captivate portal to user manager */
2179
	if (is_array($config['captiveportal']['user'])) {
2180
		foreach($config['captiveportal']['user'] as $user) {
2181
			// avoid user conflicts
2182
			$found = false;
2183
			foreach ($config['system']['user'] as $userent) {
2184
				if ($userent['name'] == $user['name']) {
2185
					$found = true;
2186
					break;
2187
				}
2188
			}
2189
			if ($found)
2190
				continue;
2191
			$user['scope'] = "user";
2192
			if (isset($user['expirationdate'])) {
2193
				$user['expires'] = $user['expirationdate'];
2194
				unset($user['expirationdate']);
2195
			}
2196
			if (isset($user['password'])) {
2197
				$user['md5-hash'] = $user['password'];
2198
				unset($user['password']);
2199
			}
2200
			$user['uid'] = $config['system']['nextuid']++;
2201
			$config['system']['user'][] = $user;
2202
		}
2203
		unset($config['captiveportal']['user']);
2204
	}
2205
}
2206

    
2207
function upgrade_057_to_058() {
2208
	global $config;
2209
	/* set all phase2 entries to tunnel mode */
2210
	if (is_array($config['ipsec']['phase2']))
2211
		foreach($config['ipsec']['phase2'] as & $ph2ent)
2212
			$ph2ent['mode'] = 'tunnel';
2213
}
2214

    
2215
function upgrade_058_to_059() {
2216
	global $config;
2217

    
2218
	if (is_array($config['schedules']['schedule'])) {
2219
		foreach ($config['schedules']['schedule'] as & $schedl)
2220
			$schedl['schedlabel'] = uniqid();
2221
	}
2222
}
2223

    
2224
function upgrade_059_to_060() {
2225
	global $config;
2226
	require_once("/etc/inc/certs.inc");
2227
	if (is_array($config['ca'])) {
2228
		/* Locate issuer for all CAs */
2229
		foreach ($config['ca'] as & $ca) {
2230
			$subject = cert_get_subject($ca['crt']);
2231
			$issuer = cert_get_issuer($ca['crt']);
2232
			if($issuer <> $subject) {
2233
				$issuer_crt =& lookup_ca_by_subject($issuer);
2234
				if($issuer_crt)
2235
					$ca['caref'] = $issuer_crt['refid'];
2236
			}
2237
		}
2238

    
2239
		/* Locate issuer for all certificates */
2240
		if (is_array($config['cert'])) {
2241
			foreach ($config['cert'] as & $cert) {
2242
				$subject = cert_get_subject($cert['crt']);
2243
				$issuer = cert_get_issuer($cert['crt']);
2244
				if($issuer <> $subject) {
2245
					$issuer_crt =& lookup_ca_by_subject($issuer);
2246
					if($issuer_crt)
2247
						$cert['caref'] = $issuer_crt['refid'];
2248
				}
2249
			}
2250
		}
2251
	}
2252
}
2253

    
2254
function upgrade_060_to_061() {
2255
	global $config;
2256

    
2257
	if (is_array($config['interfaces']['wan']))
2258
		$config['interfaces']['wan']['enable'] = true;
2259
	if (is_array($config['interfaces']['lan']))
2260
		$config['interfaces']['lan']['enable'] = true;
2261

    
2262
	/* On 1.2.3 the "mtu" field adjusted MSS.
2263
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2264
	   See redmine ticket #1886
2265
	*/
2266
	foreach ($config['interfaces'] as $ifr => &$intf) {
2267
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2268
			$intf['mss'] = $intf['mtu'];
2269
			unset($intf['mtu']);
2270
		}
2271
	}
2272
}
2273

    
2274
function upgrade_061_to_062() {
2275
	global $config;
2276

    
2277
	/* Convert NAT port forwarding rules */
2278
	if (is_array($config['nat']['rule'])) {
2279
		$a_nat = &$config['nat']['rule'];
2280

    
2281
		foreach ($a_nat as &$natent) {
2282
			$natent['disabled'] = false;
2283
			$natent['nordr']    = false;
2284

    
2285
			$natent['source'] = array(
2286
				"not"     => false,
2287
				"any"     => true,
2288
				"port"    => ""
2289
			);
2290

    
2291
			$natent['destination'] = array(
2292
				"not"     => false,
2293
				"address" => $natent['external-address'],
2294
				"port"    => $natent['external-port']
2295
			);
2296

    
2297
			if (empty($natent['destination']['address'])) {
2298
				unset($natent['destination']['address']);
2299
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2300
			} else if ($natent['destination']['address'] == 'any') {
2301
				unset($natent['destination']['address']);
2302
				$natent['destination']['any'] = true;
2303
			}
2304

    
2305
			unset($natent['external-address']);
2306
			unset($natent['external-port']);
2307
		}
2308

    
2309
		unset($natent);
2310
	}
2311
}
2312

    
2313
function upgrade_062_to_063() {
2314
	/* Upgrade legacy Themes to the new pfsense_ng */
2315
	global $config;
2316

    
2317
	switch($config['theme']) {
2318
		case "nervecenter":
2319
			$config['theme'] = "pfsense_ng";
2320
			break;
2321
	}
2322

    
2323
}
2324

    
2325
function upgrade_063_to_064() {
2326
	global $config;
2327
	$j=0;
2328
	$ifcfg = &$config['interfaces'];
2329

    
2330
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
2331
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2332
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2333
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2334
			$j++;
2335
			if (isset($ppp['port'])){
2336
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2337
				unset($config['ppps']['ppp'][$pppid]['port']);
2338
			}
2339
			if (!isset($ppp['type'])){
2340
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2341
			}
2342
			if (isset($ppp['defaultgw']))
2343
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2344
		}
2345
	}
2346

    
2347
	if (!is_array($config['ppps']['ppp']))
2348
		$config['ppps']['ppp'] = array();
2349
	$a_ppps = &$config['ppps']['ppp'];
2350

    
2351
	foreach ($ifcfg as $ifname => $ifinfo) {
2352
		$ppp = array();
2353
		// For pppoe conversion
2354
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2355
			if (isset($ifinfo['ptpid']))
2356
				continue;
2357
			$ppp['ptpid'] =  $j;
2358
			$ppp['type'] = $ifinfo['ipaddr'];
2359
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2360
			$ppp['ports'] = $ifinfo['if'];
2361
			if ($ifinfo['ipaddr'] == "pppoe"){
2362
				$ppp['username'] = $ifinfo['pppoe_username'];
2363
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2364
			}
2365
			if ($ifinfo['ipaddr'] == "pptp"){
2366
				$ppp['username'] = $ifinfo['pptp_username'];
2367
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2368
			}
2369

    
2370
			if (isset($ifinfo['provider']))
2371
				$ppp['provider'] = $ifinfo['provider'];
2372
			if (isset($ifinfo['ondemand']))
2373
				$ppp['ondemand'] = true;
2374
			if (isset($ifinfo['timeout']))
2375
				$ppp['idletimeout'] = $ifinfo['timeout'];
2376
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2377
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2378
				if (is_array($config['cron']['item'])) {
2379
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2380
						$item = $config['cron']['item'][$i];
2381
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2382
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2383
					}
2384
				}
2385
			}
2386
			if (isset($ifinfo['local']))
2387
				$ppp['localip'] = $ifinfo['local'];
2388
			if (isset($ifinfo['subnet']))
2389
				$ppp['subnet'] = $ifinfo['subnet'];
2390
			if (isset($ifinfo['remote']))
2391
				$ppp['gateway'] = $ifinfo['remote'];
2392

    
2393
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2394
			$j++;
2395

    
2396
			unset($ifcfg[$ifname]['pppoe_username']);
2397
			unset($ifcfg[$ifname]['pppoe_password']);
2398
			unset($ifcfg[$ifname]['provider']);
2399
			unset($ifcfg[$ifname]['ondemand']);
2400
			unset($ifcfg[$ifname]['timeout']);
2401
			unset($ifcfg[$ifname]['pppoe_reset']);
2402
			unset($ifcfg[$ifname]['pppoe_preset']);
2403
			unset($ifcfg[$ifname]['pppoe']);
2404
			unset($ifcfg[$ifname]['pptp_username']);
2405
			unset($ifcfg[$ifname]['pptp_password']);
2406
			unset($ifcfg[$ifname]['local']);
2407
			unset($ifcfg[$ifname]['subnet']);
2408
			unset($ifcfg[$ifname]['remote']);
2409

    
2410
			$a_ppps[] = $ppp;
2411

    
2412
		}
2413
	}
2414
}
2415

    
2416
function upgrade_064_to_065() {
2417
	/* Disable TSO and LRO in upgraded configs */
2418
	global $config;
2419
	$config['system']['disablesegmentationoffloading'] = true;
2420
	$config['system']['disablelargereceiveoffloading'] = true;
2421
}
2422

    
2423
function upgrade_065_to_066() {
2424
	global $config;
2425

    
2426
	$dhcrelaycfg =& $config['dhcrelay'];
2427

    
2428
	if (is_array($dhcrelaycfg)) {
2429
		$dhcrelayifs = array();
2430
		$foundifs = false;
2431
		/* DHCPRelay enabled on any interfaces? */
2432
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2433
			if (isset($dhcrelayifconf['enable'])) {
2434
				$dhcrelayifs[] = $dhcrelayif;
2435
				unset($dhcrelaycfg['dhcrelayif']);
2436
				$foundifs = true;
2437
			}
2438
		}
2439
		if ($foundifs == true)
2440
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2441
	}
2442
}
2443

    
2444
function upgrade_066_to_067() {
2445
	global $config;
2446
	if (isset($config['system']['ca'])) {
2447
		$config['ca'] = $config['system']['ca'];
2448
	}
2449
	if (isset($config['system']['cert'])) {
2450
		$config['cert'] = $config['system']['cert'];
2451
	}
2452
}
2453

    
2454
function upgrade_067_to_068() {
2455
	global $config;
2456

    
2457
	if (!empty($config['pppoe'])) {
2458
		$config['pppoes'] = array();
2459
		$config['pppoes']['pppoe'] = array();
2460
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2461

    
2462
		if (is_array($config['pppoe']['user'])) {
2463
			$username = array();
2464
			foreach ($config['pppoe']['user'] as $user) {
2465
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2466
				if ($user['ip'])
2467
					$usr .= ":{$user['ip']}";
2468
				$username[] = $usr;
2469
			}
2470
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2471
		}
2472
		unset($config['pppoe']);
2473
	}
2474
}
2475

    
2476
function upgrade_068_to_069() {
2477
	global $config;
2478
	if (!is_array($config['system']['user']))
2479
		return;
2480
	foreach ($config['system']['user'] as & $user) {
2481
		if (!is_array($user['cert']))
2482
			continue;
2483
		$rids = array();
2484
		foreach ($user['cert'] as $id => $cert) {
2485
			if (!isset($cert['descr']))
2486
				continue;
2487
			$tcert = $cert;
2488
			// Make sure each cert gets a refid
2489
			if (!isset($tcert['refid']))
2490
				$tcert['refid'] = uniqid();
2491
			// Keep the cert references for this user
2492
			$rids[] = $tcert['refid'];
2493
			$config['cert'][] = $tcert;
2494
		}
2495
		// Replace user certs with cert references instead.
2496
		if (count($rids) > 0)
2497
			$user['cert'] = $rids;
2498
	}
2499
}
2500

    
2501
function upgrade_069_to_070() {
2502
	global $config;
2503

    
2504
	/* Convert NAT 1:1 rules */
2505
	if (is_array($config['nat']['onetoone'])) {
2506
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2507
			if ($natent['subnet'] == 32)
2508
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2509
			else
2510
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2511

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

    
2514
			unset($config['nat']['onetoone'][$nidx]['internal']);
2515
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2516
		}
2517

    
2518
		unset($natent);
2519
	}
2520
}
2521

    
2522
function upgrade_070_to_071() {
2523
	global $config;
2524

    
2525
	if (is_array($config['cron']['item'])) {
2526
		foreach($config['cron']['item'] as $idx => $cronitem) {
2527
			if(stristr($cronitem['command'], "checkreload.sh")) {
2528
				unset($config['cron']['item'][$idx]);
2529
				break;
2530
			}
2531
		}
2532
	}
2533
}
2534

    
2535
function rename_field(& $section, $oldname, $newname) {
2536
	if (is_array($section)) {
2537
		foreach($section as & $item) {
2538
			if (is_array($item) && !empty($item[$oldname]))
2539
				$item[$newname] = $item[$oldname];
2540
			if (is_array($item) && isset($item[$oldname]))
2541
				unset($item[$oldname]);
2542
		}
2543
	}
2544
}
2545

    
2546
function upgrade_071_to_072() {
2547
	global $config;
2548
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2549
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2550
}
2551

    
2552
function upgrade_072_to_073() {
2553
	global $config;
2554
	if (!is_array($config['load_balancer']))
2555
		return;
2556
	if (is_array($config['load_balancer']['monitor_type']))
2557
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2558
	if (is_array($config['load_balancer']['lbpool']))
2559
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2560
	if (is_array($config['load_balancer']['lbaction']))
2561
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2562
	if (is_array($config['load_balancer']['lbprotocol']))
2563
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2564
	if (is_array($config['load_balancer']['virtual_server']))
2565
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2566
}
2567

    
2568
function upgrade_073_to_074() {
2569
	global $config;
2570
	rename_field($config['system']['user'], 'fullname', 'descr');
2571
}
2572

    
2573
function upgrade_074_to_075() {
2574
	global $config;
2575
	if (is_array($config['ca']))
2576
		rename_field($config['ca'], 'name', 'descr');
2577
	if (is_array($config['cert']))
2578
		rename_field($config['cert'], 'name', 'descr');
2579
	if (is_array($config['crl']))
2580
		rename_field($config['crl'], 'name', 'descr');
2581
}
2582

    
2583
function upgrade_075_to_076() {
2584
	global $config;
2585
	$cron_item = array();
2586
	$cron_item['minute'] = "30";
2587
	$cron_item['hour'] = "12";
2588
	$cron_item['mday'] = "*";
2589
	$cron_item['month'] = "*";
2590
	$cron_item['wday'] = "*";
2591
	$cron_item['who'] = "root";
2592
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2593
	$config['cron']['item'][] = $cron_item;
2594
}
2595

    
2596
function upgrade_076_to_077() {
2597
	global $config;
2598
	foreach($config['filter']['rule'] as & $rule) {
2599
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2600
		$rule['protocol'] = strtolower($rule['protocol']);
2601
	}
2602
}
2603

    
2604
function upgrade_077_to_078() {
2605
	global $config;
2606
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius'])
2607
		&& !is_array($config['pptpd']['radius']['server'])) {
2608
		$radarr = array();
2609
		$radsvr = array();
2610
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2611
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2612
		$radsvr['port'] = 1812;
2613
		$radsvr['acctport'] = 1813;
2614
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2615
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2616
		if ($radarr['accounting'])
2617
			$radarr['acct_update'] = $radsvr['ip'];
2618
		$radarr['server'] = $radsvr;
2619
		$config['pptpd']['radius'] = $radarr;
2620
	}
2621
	if (is_array($config['pptpd'])) {
2622
		$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
2623
	}
2624
}
2625
function upgrade_078_to_079() {
2626
	global $g;
2627
	/* Delete old and unused RRD file */
2628
	unlink_if_exists("{$g['vardb_path']}/rrd/captiveportal-totalusers.rrd");
2629
}
2630

    
2631
function upgrade_079_to_080() {
2632
	global $config;
2633

    
2634
	/* Upgrade config in 1.2.3 specifying a username other than admin for synching. */
2635
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2636
		is_array($config['installedpackages']['carpsettings']['config'])) {
2637
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2638
		unset($config['system']['username']);
2639
	}
2640
}
2641

    
2642
function upgrade_080_to_081() {
2643
	global $config;
2644
	global $g;
2645
	/* Welcome to the 2.1 migration path */
2646

    
2647
	/* tag all the existing gateways as being IPv4 */
2648
	$i = 0;
2649
	if(is_array($config['gateways']['gateway_item'])) {
2650
		foreach($config['gateways']['gateway_item'] as $gw) {
2651
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2652
			$i++;
2653
		}
2654
	}
2655

    
2656
	/* RRD files changed for quality, traffic and packets graphs */
2657
	/* convert traffic RRD file */
2658
	global $parsedcfg, $listtags;
2659
	$listtags = array("ds", "v", "rra", "row");
2660

    
2661
	$rrddbpath = "/var/db/rrd/";
2662
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2663

    
2664
	if ($g['platform'] != "pfSense") {
2665
		/* restore the databases, if we have one */
2666
		if (restore_rrd()) {
2667
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2668
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2669
		}
2670
	}
2671

    
2672
	$rrdinterval = 60;
2673
	$valid = $rrdinterval * 2;
2674

    
2675
	/* Asume GigE for now */
2676
	$downstream = 125000000;
2677
	$upstream = 125000000;
2678

    
2679
	/* build a list of traffic and packets databases */
2680
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2681
	rsort($databases);
2682
	foreach($databases as $database) {
2683
		$xmldump = "{$database}.old.xml";
2684
		$xmldumpnew = "{$database}.new.xml";
2685

    
2686
		if (platform_booting())
2687
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2688

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

    
2692
		/* search and replace tags to add data sources */
2693
		$ds_search = "<!-- Round Robin Archives -->";
2694
		$ds_arr = array();
2695
		$ds_arr[] = "	<ds>
2696
				<name> inpass6 </name>
2697
				<type> COUNTER </type>
2698
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2699
				<min> 0.0000000000e+00 </min>
2700
				<max> 1.2500000000e+08 </max>
2701

    
2702
				<!-- PDP Status -->
2703
				<last_ds> 0 </last_ds>
2704
				<value> NaN </value>
2705
				<unknown_sec> 3 </unknown_sec>
2706
			</ds>
2707
			";
2708
		$ds_arr[] = "	<ds>
2709
				<name> outpass6 </name>
2710
				<type> COUNTER </type>
2711
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2712
				<min> 0.0000000000e+00 </min>
2713
				<max> 1.2500000000e+08 </max>
2714

    
2715
				<!-- PDP Status -->
2716
				<last_ds> 0 </last_ds>
2717
				<value> NaN </value>
2718
				<unknown_sec> 3 </unknown_sec>
2719
			</ds>
2720
			";
2721
		$ds_arr[] = "	<ds>
2722
				<name> inblock6 </name>
2723
				<type> COUNTER </type>
2724
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2725
				<min> 0.0000000000e+00 </min>
2726
				<max> 1.2500000000e+08 </max>
2727

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

    
2741
				<!-- PDP Status -->
2742
				<last_ds> 0 </last_ds>
2743
				<value> NaN </value>
2744
				<unknown_sec> 3 </unknown_sec>
2745
			</ds>
2746
			";
2747

    
2748
		$cdp_search = "<\/cdp_prep>";
2749
		$cdp_replace = "</cdp_prep>";
2750
		$cdp_arr = array();
2751
		$cdp_arr[] = "			<ds>
2752
					<primary_value> NaN </primary_value>
2753
					<secondary_value> 0.0000000000e+00 </secondary_value>
2754
					<value> NaN </value>
2755
					<unknown_datapoints> 0 </unknown_datapoints>
2756
					</ds>
2757
		";
2758
		$cdp_arr[] = "			<ds>
2759
					<primary_value> NaN </primary_value>
2760
					<secondary_value> 0.0000000000e+00 </secondary_value>
2761
					<value> NaN </value>
2762
					<unknown_datapoints> 0 </unknown_datapoints>
2763
					</ds>
2764
		";
2765
		$cdp_arr[] = "			<ds>
2766
					<primary_value> NaN </primary_value>
2767
					<secondary_value> 0.0000000000e+00 </secondary_value>
2768
					<value> NaN </value>
2769
					<unknown_datapoints> 0 </unknown_datapoints>
2770
					</ds>
2771
		";
2772
		$cdp_arr[] = "			<ds>
2773
					<primary_value> NaN </primary_value>
2774
					<secondary_value> 0.0000000000e+00 </secondary_value>
2775
					<value> NaN </value>
2776
					<unknown_datapoints> 0 </unknown_datapoints>
2777
					</ds>
2778
		";
2779

    
2780
		$value_search = "<\/row>";
2781
		$value_replace = "</row>";
2782
		$value = "<v> NaN </v>";
2783

    
2784
		$xml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2785
		foreach($ds_arr as $ds) {
2786
			$xml = preg_replace("/$ds_search/s", "$ds{$ds_search}", $xml);
2787
		}
2788
		foreach($cdp_arr as $cdp) {
2789
			$xml = preg_replace("/$cdp_search/s", "$cdp{$cdp_replace}", $xml);
2790
		}
2791
		foreach($ds_arr as $ds) {
2792
			$xml = preg_replace("/$value_search/s", "$value{$value_replace}", $xml);
2793
		}
2794
		
2795
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $xml);
2796
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2797
		unset($xml);
2798
		# Default /tmp tmpfs is ~40mb, do not leave temp files around
2799
		unlink_if_exists("{$g['tmp_path']}/{$xmldump}");
2800
		unlink_if_exists("{$g['tmp_path']}/{$xmldumpnew}");
2801
	}
2802
	if (!platform_booting())
2803
		enable_rrd_graphing();
2804
	/* Let's save the RRD graphs after we run enable RRD graphing */
2805
	/* The function will restore the rrd.tgz so we will save it after */
2806
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2807
	if (platform_booting())
2808
		echo "Updating configuration...";
2809
	foreach($config['filter']['rule'] as & $rule) {
2810
		if (isset($rule['protocol']) && !empty($rule['protocol']))
2811
			$rule['protocol'] = strtolower($rule['protocol']);
2812
	}
2813
	unset($rule);
2814
}
2815

    
2816
function upgrade_081_to_082() {
2817
	/* don't enable the allow IPv6 toggle */
2818
}
2819

    
2820
function upgrade_082_to_083() {
2821
	global $config;
2822

    
2823
	/* Upgrade captiveportal config */
2824
	if (!empty($config['captiveportal'])) {
2825
		$tmpcp = $config['captiveportal'];
2826
		$config['captiveportal'] = array();
2827
		$config['captiveportal']['cpzone'] = array();
2828
		$config['captiveportal']['cpzone'] = $tmpcp;
2829
		$config['captiveportal']['cpzone']['zoneid'] = 8000;
2830
		$config['captiveportal']['cpzone']['zone'] = "cpzone";
2831
		if ($config['captiveportal']['cpzone']['auth_method'] == "radius")
2832
			$config['captiveportal']['cpzone']['radius_protocol'] = "PAP";
2833
	}
2834
	if (!empty($config['voucher'])) {
2835
		$tmpcp = $config['voucher'];
2836
		$config['voucher'] = array();
2837
		$config['voucher']['cpzone'] = array();
2838
		$config['voucher']['cpzone'] = $tmpcp;
2839
	}
2840
}
2841

    
2842
function upgrade_083_to_084() {
2843
	global $config;
2844
	if (!isset($config['hasync'])) {
2845
		if (!empty($config['installedpackages']) &&
2846
		    !empty($config['installedpackages']['carpsettings']) &&
2847
		    !empty($config['installedpackages']['carpsettings']['config'])) {
2848
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
2849
			unset($config['installedpackages']['carpsettings']);
2850
		}
2851
		if (empty($config['installedpackages']['carpsettings'])) {
2852
			unset($config['installedpackages']['carpsettings']);
2853
		}
2854
		if (empty($config['installedpackages'])) {
2855
			unset($config['installedpackages']);
2856
		}
2857
	}
2858
}
2859

    
2860
function upgrade_084_to_085() {
2861
	global $config;
2862

    
2863
	$gateway_group_arr = array();
2864
	$gateways = return_gateways_array();
2865
	$oldnames = array();
2866
	/* setup translation array */
2867
	foreach($gateways as $name => $gw) {
2868
		if(isset($gw['dynamic'])){
2869
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
2870
			$oldnames[$oldname] = $name;
2871
		} else {
2872
			$oldnames[$name] = $name;
2873
		}
2874
	}
2875

    
2876
	/* process the old array */
2877
	if(is_array($config['gateways']['gateway_group'])) {
2878
		$group_array_new = array();
2879
		foreach($config['gateways']['gateway_group'] as $name => $group) {
2880
			if(is_array($group['item'])) {
2881
				$newlist = array();
2882
				foreach($group['item'] as $entry) {
2883
					$elements = explode("|", $entry);
2884
					if($oldnames[$elements[0]] <> "") {
2885
						$newlist[] = "{$oldnames[$elements[0]]}|{$elements[1]}";
2886
					} else {
2887
						$newlist[] = "{$elements[0]}|{$elements[1]}";
2888
					}
2889
				}
2890
				$group['item'] = $newlist;
2891
				$group_array_new[$name] = $group;
2892
			}
2893
		}
2894
		$config['gateways']['gateway_group'] = $group_array_new;
2895
	}
2896
	/* rename old Quality RRD files in the process */
2897
	$rrddbpath = "/var/db/rrd";
2898
	foreach($oldnames as $old => $new) {
2899
		if(is_readable("{$rrddbpath}/{$old}-quality.rrd")) {
2900
			@rename("{$rrddbpath}/{$old}-quality.rrd", "{$rrddbpath}/{$new}-quality.rrd");
2901
		}
2902
	}
2903
	unset($gateways, $oldnames, $gateway_group_arr);
2904
}
2905

    
2906
function upgrade_085_to_086() {
2907
	global $config, $g;
2908

    
2909
	/* XXX: Gross hacks in sight */
2910
	if (is_array($config['virtualip']['vip'])) {
2911
		$vipchg = array();
2912
		foreach ($config['virtualip']['vip'] as $vip) {
2913
			if ($vip['mode'] != "carp")
2914
				continue;
2915
			$config = array_replace_values_recursive(
2916
				$config,
2917
				'^vip' . $vip['vhid'] . '$',
2918
				"{$vip['interface']}_vip{$vip['vhid']}"
2919
			);
2920
		}
2921
	}
2922
}
2923

    
2924
function upgrade_086_to_087() {
2925
	global $config, $dummynet_pipe_list;
2926

    
2927
	if (!is_array($config['filter']) || !is_array($config['filter']['rule']))
2928
		return;
2929
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue']))
2930
		return;
2931

    
2932
	$dnqueue_number = 1;
2933
	$dnpipe_number = 1;
2934

    
2935
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
2936
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
2937
		$dnpipe_number++;
2938
		if (is_array($dnpipe['queue'])) {
2939
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
2940
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
2941
				$dnqueue_number++;
2942
			}
2943
		}
2944
	}
2945

    
2946
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
2947

    
2948
	require_once("shaper.inc");
2949
	read_dummynet_config();
2950

    
2951
	$dn_list = array();
2952
	if (is_array($dummynet_pipe_list)) {
2953
		foreach ($dummynet_pipe_list as $dn) {
2954
			$tmplist =& $dn->get_queue_list();
2955
			foreach ($tmplist as $qname => $link) {
2956
				$dn_list[$link] = $qname;
2957
			}
2958
		}
2959
		unset($dummynet_pipe_list);
2960
	}
2961

    
2962
	foreach ($config['filter']['rule'] as $idx => $rule) {
2963
		if (!empty($rule['dnpipe'])) {
2964
			if (!empty($dn_list[$rule['dnpipe']]))
2965
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
2966
		}
2967
		if (!empty($rule['pdnpipe'])) {
2968
			if (!empty($dn_list[$rule['pdnpipe']]))
2969
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
2970
		}
2971
	}
2972
}
2973
function upgrade_087_to_088() {
2974
	global $config;
2975
	if (isset($config['system']['glxsb_enable'])) {
2976
		unset($config['system']['glxsb_enable']);
2977
		$config['system']['crypto_hardware'] = "glxsb";
2978
	}
2979
}
2980

    
2981
function upgrade_088_to_089() {
2982
	global $config;
2983
	if (!is_array($config['ca']))
2984
		$config['ca'] = array();
2985
	if (!is_array($config['cert']))
2986
		$config['cert'] = array();
2987

    
2988
	/* migrate captive portal ssl to certifcate mngr */
2989
	if (is_array($config['captiveportal'])) {
2990
		foreach ($config['captiveportal'] as $id => &$setting) {
2991
			if (isset($setting['httpslogin'])) {
2992
				/* create cert entry */
2993
				$cert = array();
2994
				$cert['refid'] = uniqid();
2995
				$cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
2996
				$cert['crt'] = $setting['certificate'];
2997
				$cert['prv'] = $setting['private-key'];
2998

    
2999
				if (!empty($setting['cacertificate'])) {
3000
					/* create ca entry */
3001
					$ca = array();
3002
					$ca['refid'] = uniqid();
3003
					$ca['descr'] = "Captive Portal CA - {$setting['zone']}";
3004
					$ca['crt'] = $setting['cacertificate'];
3005
					$config['ca'][] = $ca;
3006

    
3007
					/* add ca reference to certificate */
3008
					$cert['caref'] = $ca['refid'];
3009
				}
3010

    
3011
				$config['cert'][] = $cert;
3012

    
3013
				/* create cert reference */
3014
				$setting['certref'] = $cert['refid'];
3015

    
3016
				unset($setting['certificate']);
3017
				unset($setting['private-key']);
3018
				unset($setting['cacertificate']);
3019

    
3020
			}
3021
		}
3022
	}
3023
}
3024

    
3025
function upgrade_089_to_090() {
3026
	global $config;
3027
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
3028
		$vs_a = &$config['load_balancer']['virtual_server'];
3029
		for ($i = 0; isset($vs_a[$i]); $i++) {
3030
			if (is_array($vs_a[$i]['pool'])) {
3031
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
3032
				unset($vs_a[$i]['pool']);
3033
			} elseif (!empty($vs_a[$i]['pool'])) {
3034
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
3035
				unset($vs_a[$i]['pool']);
3036
			}
3037
		}
3038
	}
3039
}
3040

    
3041
function upgrade_090_to_091() {
3042
	global $config;
3043

    
3044
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
3045
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
3046
			if (!empty($dnqueue['bandwidth'])) {
3047
				$bw = array();
3048
				$bw['bw'] = $dnqueue['bandwidth'];
3049
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
3050
				$bw['bwsched'] = "none";
3051
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
3052
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
3053
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
3054
			}
3055
		}
3056
	}
3057
}
3058

    
3059
function upgrade_091_to_092() {
3060
	global $config;
3061

    
3062
	if (is_array($config['nat']['advancedoutbound']) && is_array($config['nat']['advancedoutbound']['rule'])) {
3063
		$nat_rules = &$config['nat']['advancedoutbound']['rule'];
3064
		for ($i = 0; isset($nat_rules[$i]); $i++) {
3065
			if (empty($nat_rules[$i]['interface'])) {
3066
				$nat_rules[$i]['interface'] = 'wan';
3067
			}
3068
		}
3069
	}
3070
}
3071

    
3072
function upgrade_092_to_093() {
3073
	global $g;
3074

    
3075
	$suffixes = array("concurrent", "loggedin");
3076

    
3077
	foreach ($suffixes as $suffix)
3078
		if (file_exists("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd"))
3079
			rename("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd",
3080
				"{$g['vardb_path']}/rrd/captiveportal-cpZone-{$suffix}.rrd");
3081

    
3082
	if (!platform_booting())
3083
		enable_rrd_graphing();
3084
}
3085

    
3086
function upgrade_093_to_094() {
3087
	global $config;
3088

    
3089
	if (isset($config['system']['powerd_mode'])) {
3090
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3091
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3092
		unset($config['system']['powerd_mode']);
3093
	}
3094
}
3095

    
3096
function upgrade_094_to_095() {
3097
	global $config;
3098

    
3099
	if (!isset($config['interfaces']) || !is_array($config['interfaces']))
3100
		return;
3101

    
3102
	foreach ($config['interfaces'] as $iface => $cfg)
3103
		if (isset($cfg['ipaddrv6']) && ($cfg['ipaddrv6'] == "track6"))
3104
			if (!isset($cfg['track6-prefix-id']) || ($cfg['track6-prefix-id'] == ""))
3105
				$config['interfaces'][$iface]['track6-prefix-id'] = 0;
3106
}
3107

    
3108
function upgrade_095_to_096() {
3109
	global $config, $g;
3110

    
3111
	$names = array("inpass", "outpass", "inblock", "outblock",
3112
		"inpass6", "outpass6", "inblock6", "outblock6");
3113
	$rrddbpath = "/var/db/rrd";
3114
	$rrdtool = "/usr/local/bin/rrdtool";
3115

    
3116
	if ($g['platform'] != "pfSense") {
3117
		/* restore the databases, if we have one */
3118
		if (restore_rrd()) {
3119
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
3120
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
3121
		}
3122
	}
3123

    
3124
	/* Assume 2*10GigE for now */
3125
	$stream = 2500000000;
3126

    
3127
	/* build a list of traffic and packets databases */
3128
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
3129
	rsort($databases);
3130
	foreach($databases as $database) {
3131
		if (platform_booting())
3132
			echo "Update RRD database {$database}.\n";
3133

    
3134
		$cmd = "{$rrdtool} tune {$rrddbpath}/{$database}";
3135
		foreach ($names as $name)
3136
			$cmd .= " -a {$name}:{$stream}";
3137
		mwexec("{$cmd} 2>&1");
3138

    
3139
	}
3140
	if (!platform_booting())
3141
		enable_rrd_graphing();
3142
	/* Let's save the RRD graphs after we run enable RRD graphing */
3143
	/* The function will restore the rrd.tgz so we will save it after */
3144
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
3145
}
3146

    
3147
function upgrade_096_to_097() {
3148
	global $config, $g;
3149
	/* If the user had disabled default block rule logging before, then bogon/private network logging was already off, so respect their choice. */
3150
	if (isset($config['syslog']['nologdefaultblock'])) {
3151
		$config['syslog']['nologbogons'] = true;
3152
		$config['syslog']['nologprivatenets'] = true;
3153
	}
3154
}
3155

    
3156
function upgrade_097_to_098() {
3157
	global $config, $g;
3158
	/* Disable kill_states by default */
3159
	$config['system']['kill_states'] = true;
3160
}
3161

    
3162
function upgrade_098_to_099() {
3163
	global $config;
3164

    
3165
	if (empty($config['dhcpd']) || !is_array($config['dhcpd']))
3166
		return;
3167

    
3168
	foreach ($config['dhcpd'] as & $dhcpifconf) {
3169
		if (isset($dhcpifconf['next-server'])) {
3170
			$dhcpifconf['nextserver'] = $dhcpifconf['next-server'];
3171
			unset($dhcpifconf['next-server']);
3172
		}
3173
	}
3174
}
3175

    
3176
function upgrade_099_to_100() {
3177
	require_once("/etc/inc/services.inc");
3178
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3179
}
3180

    
3181
function upgrade_100_to_101() {
3182
	global $config, $g;
3183

    
3184
	if (!is_array($config['voucher']))
3185
		return;
3186

    
3187
	foreach ($config['voucher'] as $cpzone => $cp) {
3188
		if (!is_array($cp['roll']))
3189
			continue;
3190
		foreach ($cp['roll'] as $ridx => $rcfg) {
3191
			if (!empty($rcfg['comment']))
3192
				$config['voucher'][$cpzone]['roll'][$ridx]['descr'] = $rcfg['comment'];
3193
		}
3194
	}
3195
}
3196

    
3197
function upgrade_101_to_102() {
3198
	global $config, $g;
3199

    
3200
	if (is_array($config['captiveportal'])) {
3201
		foreach ($config['captiveportal'] as $cpzone => $cp) {
3202
			if (!is_array($cp['passthrumac']))
3203
				continue;
3204

    
3205
			foreach ($cp['passthrumac'] as $idx => $passthrumac)
3206
				$config['captiveportal'][$cpzone]['passthrumac'][$idx]['action'] = 'pass';
3207
		}
3208
	}
3209

    
3210
	/* Convert OpenVPN Compression option to the new style */
3211
	// Nothing to do if there is no OpenVPN tag
3212
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
3213
		if (is_array($config['openvpn']['openvpn-server'])) {
3214
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
3215
				if (!empty($vpn['compression']))
3216
					$vpn['compression'] = "adaptive";
3217
			}
3218
		}
3219
		if (is_array($config['openvpn']['openvpn-client'])) {
3220
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
3221
				if (!empty($vpn['compression']))
3222
					$vpn['compression'] = "adaptive";
3223
			}
3224
		}
3225
	}
3226
}
3227

    
3228
function upgrade_102_to_103() {
3229
	global $config;
3230

    
3231
	if (isset($config['nat']['advancedoutbound']['enable'])) {
3232
		$config['nat']['advancedoutbound']['mode'] = "advanced";
3233
		unset($config['nat']['advancedoutbound']['enable']);
3234
	} else
3235
		$config['nat']['advancedoutbound']['mode'] = "automatic";
3236

    
3237
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3238

    
3239
	unset($config['nat']['ipsecpassthru']);
3240
	unset($config['nat']['advancedoutbound']);
3241
}
3242

    
3243
function upgrade_103_to_104() {
3244
	global $config;
3245

    
3246
	$changed_privs = array(
3247
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3248
		"page-interfacess-groups" => "page-interfaces-groups",
3249
		"page-interfacess-lagg" => "page-interfaces-lagg",
3250
		"page-interfacess-qinq" => "page-interfaces-qinq"
3251
	);
3252

    
3253
	/* update user privileges */
3254
	foreach ($config['system']['user'] as & $user) {
3255
		if (!is_array($user['priv']))
3256
			continue;
3257
		foreach ($user['priv'] as & $priv) {
3258
			if (array_key_exists($priv, $changed_privs))
3259
				$priv = $changed_privs[$priv];
3260
		}
3261
	}
3262

    
3263
	/* update group privileges */
3264
	foreach ($config['system']['group'] as & $group) {
3265
		if (!is_array($group['priv']))
3266
			continue;
3267
		foreach ($group['priv'] as & $priv) {
3268
			if (array_key_exists($priv, $changed_privs))
3269
				$priv = $changed_privs[$priv];
3270
		}
3271
	}
3272

    
3273
	/* sync all local account information */
3274
	local_sync_accounts();
3275
}
3276

    
3277
function upgrade_104_to_105() {
3278
	global $config;
3279

    
3280
	if (is_array($config['captiveportal'])) {
3281
		$zoneid = 2;
3282
		foreach ($config['captiveportal'] as $cpzone => $cpcfg) {
3283
			if (empty($cpfg['zoneid'])) {
3284
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3285
				$zoneid += 2;
3286
			} else if ($cpcfg['zoneid'] > 4000) {
3287
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3288
				$zoneid += 2;
3289
			}
3290
		}
3291
	}
3292
}
3293

    
3294
function upgrade_105_to_106() {
3295

    
3296
	/* NOTE: This entry can be reused for something else since the upgrade code was reverted */
3297
}
3298

    
3299
function upgrade_106_to_107() {
3300
	global $config;
3301

    
3302
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
3303
		$tracker = (int)microtime(true);
3304
		foreach ($config['filter']['rule'] as $ridx => $rule) {
3305
			if (empty($rule['tracker'])) {
3306
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
3307
				$tracker++;
3308
			}
3309
		}
3310
		unset($tracker, $ridx);
3311
	}
3312
	if (is_array($config['nat']) && is_array($config['nat']['rule'])) {
3313
		$tracker = (int)microtime(true);
3314
		foreach ($config['nat']['rule'] as $ridx => $rule) {
3315
			if (empty($rule['tracker'])) {
3316
				$config['nat']['rule'][$ridx]['tracker'] = $tracker;
3317
				$tracker++;
3318
			}
3319

    
3320
		}
3321
		unset($tracker, $ridx);
3322
	}
3323
}
3324

    
3325
function upgrade_107_to_108() {
3326
	global $config;
3327

    
3328
	if (isset($config['system']['webgui']['noautocomplete']))
3329
		unset($config['system']['webgui']['noautocomplete']);
3330
	else
3331
		$config['system']['webgui']['loginautocomplete'] = true;
3332
}
3333

    
3334
function upgrade_108_to_109() {
3335
	global $config;
3336

    
3337
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule']))
3338
		return;
3339

    
3340
	foreach ($config['filter']['rule'] as &$rule) {
3341
		if (!isset($rule['dscp']) || empty($rule['dscp']))
3342
			continue;
3343

    
3344
		$pos = strpos($rule['dscp'], ' ');
3345
		if ($pos !== false)
3346
			$rule['dscp'] = substr($rule['dscp'], 0, $pos);
3347
		unset($pos);
3348
	}
3349
}
3350

    
3351
function upgrade_109_to_110() {
3352
	global $config;
3353

    
3354
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2']))
3355
		return;
3356

    
3357
	foreach ($config['ipsec']['phase2'] as &$rule) {
3358
		if (!empty($rule['uniqid']))
3359
			continue;
3360

    
3361
		$rule['uniqid'] = uniqid();
3362
	}
3363
}
3364

    
3365
function upgrade_110_to_111() {
3366
	global $config;
3367

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

    
3372
	/* cleanup old unbound package stuffs */
3373
	unlink_if_exists("/usr/local/pkg/unbound.xml");
3374
	unlink_if_exists("/usr/local/pkg/unbound.inc");
3375
	unlink_if_exists("/usr/local/pkg/unbound_advanced.xml");
3376
	unlink_if_exists("/usr/local/www/unbound_status.php");
3377
	unlink_if_exists("/usr/local/www/unbound_acls.php");
3378
	unlink_if_exists("/usr/local/bin/unbound_monitor.sh");
3379
	unlink_if_exists("/usr/local/etc/rc.d/unbound.sh");
3380

    
3381
	/* Remove old menu and service entries */
3382
	if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
3383
		foreach ($config['installedpackages']['menu'] as $idx => $menu) {
3384
			if ($menu['name'] != 'Unbound DNS')
3385
				continue;
3386

    
3387
			unset($config['installedpackages']['menu'][$idx]);
3388
			break;
3389
		}
3390
	}
3391

    
3392
	if (isset($config['installedpackages']['service']) && is_array($config['installedpackages']['service'])) {
3393
		foreach ($config['installedpackages']['service'] as $idx => $service) {
3394
			if ($service['name'] != 'unbound')
3395
				continue;
3396
			unset($config['installedpackages']['service'][$idx]);
3397
			break;
3398
		}
3399
	}
3400

    
3401
	if (!isset($config['installedpackages']['unbound']['config'][0]))
3402
		return;
3403

    
3404
	$pkg = $config['installedpackages']['unbound']['config'][0];
3405

    
3406
	if (isset($config['installedpackages']['unboundadvanced']['config'][0]))
3407
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3408

    
3409
	$new = array();
3410

    
3411
	/* deal first with boolean fields */
3412
	$fields = array(
3413
		"enable" => "enable",
3414
		"dnssec_status" => "dnssec",
3415
		"forwarding_mode" => "forwarding",
3416
		"regdhcp" => "regdhcp",
3417
		"regdhcpstatic" => "regdhcpstatic",
3418
		"txtsupport" => "txtsupport",
3419
		"hide_id" => "hideidentity",
3420
		"hide_version" => "hideversion",
3421
		"prefetch" => "prefetch",
3422
		"prefetch_key" => "prefetchkey",
3423
		"harden_glue" => "hardenglue",
3424
		"harden_dnssec_stripped" => "dnssec_stripped");
3425

    
3426
	foreach ($fields as $oldk => $newk) {
3427
		if (isset($pkg[$oldk])) {
3428
			if ($pkg[$oldk] == 'on')
3429
				$new[$newk] = true;
3430
			unset($pkg[$oldk]);
3431
		}
3432
	}
3433

    
3434
	$fields = array(
3435
		"active_interface" => "network_interface",
3436
		"query_interface" => "outgoing_interface",
3437
		"unbound_verbosity" => "log_verbosity",
3438
		"unbound_verbosity" => "log_verbosity",
3439
		"msg_cache_size" => "msgcachesize",
3440
		"outgoing_num_tcp" => "outgoing_num_tcp",
3441
		"incoming_num_tcp" => "incoming_num_tcp",
3442
		"edns_buffer_size" => "edns_buffer_size",
3443
		"num_queries_per_thread" => "num_queries_per_thread",
3444
		"jostle_timeout" => "jostle_timeout",
3445
		"cache_max_ttl" => "cache_max_ttl",
3446
		"cache_min_ttl" => "cache_min_ttl",
3447
		"infra_host_ttl" => "infra_host_ttl",
3448
		"infra_cache_numhosts" => "infra_cache_numhosts",
3449
		"unwanted_reply_threshold" => "unwanted_reply_threshold",
3450
		"custom_options" => "custom_options");
3451

    
3452
	foreach ($fields as $oldk => $newk) {
3453
		if (isset($pkg[$oldk])) {
3454
			$new[$newk] = $pkg[$oldk];
3455
			unset($pkg[$oldk]);
3456
		}
3457
	}
3458

    
3459
	if (isset($new['custom_options']) && !empty($new['custom_options']))
3460
		$new['custom_options'] = str_replace("\r\n", "\n", $new['custom_options']);
3461

    
3462
	/* Following options were removed, bring them as custom_options */
3463
	if (isset($pkg['stats']) && $pkg['stats'] == "on") {
3464
		if (isset($pkg['stats_interval']))
3465
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-interval: {$pkg['stats_interval']}";
3466
		if (isset($pkg['cumulative_stats']))
3467
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-cumulative: {$pkg['cumulative_stats']}";
3468
		if (isset($pkg['extended_stats']) && $pkg['extended_stats'] == "on")
3469
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: yes";
3470
		else
3471
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: no";
3472
	}
3473

    
3474
	$new['acls'] = array();
3475
	if (isset($config['installedpackages']['unboundacls']['config']) &&
3476
	    is_array($config['installedpackages']['unboundacls']['config'])) {
3477
		foreach ($config['installedpackages']['unboundacls']['config'] as $acl)
3478
			$new['acls'][] = $acl;
3479
	}
3480

    
3481
	$config['unbound'] = $new;
3482

    
3483
	if(isset($config['installedpackages']['unbound']))
3484
		unset($config['installedpackages']['unbound']);
3485
	if(isset($config['installedpackages']['unboundadvanced']))
3486
		unset($config['installedpackages']['unboundadvanced']);
3487
	if(isset($config['installedpackages']['unboundacls']))
3488
		unset($config['installedpackages']['unboundacls']);
3489

    
3490
	unset($pkg, $new);
3491
}
3492

    
3493
function upgrade_111_to_112() {
3494
	global $config;
3495

    
3496
	$config['cron']['item'][] = array(
3497
		'minute' => '*/60',
3498
		'hour' => '*',
3499
		'mday' => '*',
3500
		'month' => '*',
3501
		'wday' => '*',
3502
		'who' => 'root',
3503
		'command' => '/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 webConfiguratorlockout'
3504
	);
3505
}
3506

    
3507
function upgrade_112_to_113() {
3508
	global $config;
3509

    
3510
	if (isset($config['notifications']['smtp']['ssl']) &&
3511
	    $config['notifications']['smtp']['ssl'] == "checked")
3512
		$config['notifications']['smtp']['ssl'] = true;
3513
	else
3514
		unset($config['notifications']['smtp']['ssl']);
3515

    
3516
	if (isset($config['notifications']['smtp']['tls']) &&
3517
	    $config['notifications']['smtp']['tls'] == "checked")
3518
		$config['notifications']['smtp']['tls'] = true;
3519
	else
3520
		unset($config['notifications']['smtp']['tls']);
3521
}
3522

    
3523
function upgrade_113_to_114() {
3524
	global $config;
3525

    
3526
	if (!isset($config['ipsec']['phase1']) ||
3527
	    !is_array($config['ipsec']['phase1']))
3528
		return;
3529

    
3530
	foreach($config['ipsec']['phase1'] as &$ph1ent)
3531
		if (!isset($ph1ent['iketype']))
3532
			$ph1ent['iketype'] = 'ikev1';
3533
}
3534

    
3535
function upgrade_114_to_115() {
3536
	global $config;
3537

    
3538
	if (isset($config['unbound']['custom_options']))
3539
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3540
}
3541

    
3542
?>
(55-55/68)