Projet

Général

Profil

Télécharger (106 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / upgrade_config.inc @ c11b7ffe

1
<?php
2
/*
3
	Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
4
	All rights reserved.
5

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
192

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

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

    
204

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

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

    
223

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

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

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

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

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

    
250

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

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

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

    
271

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

    
286

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

    
299

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

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

    
353

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

    
359

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

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

    
383

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

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

    
398

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

    
403

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

    
410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
491

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

    
496

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

    
501

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

    
515

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

    
522

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

    
528

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

    
534

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

    
540

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

    
546

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

    
552

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

    
558

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

    
564

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

    
570

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

    
576

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
716
	}
717
}
718

    
719

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

    
728

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

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

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

    
781

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

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

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

    
822

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

    
843

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

    
927

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

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

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

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

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

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

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

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

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

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

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

    
964
				/* build new phase1 entry */
965

    
966
				$ph1ent = array();
967

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

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

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

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

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

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

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

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

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

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

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

    
1054
			/* build new phase2 entry */
1055

    
1056
			$ph2ent = array();
1057

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1274

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

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

    
1336

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

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

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

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

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

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

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

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

    
1397

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

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

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

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

    
1443

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

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

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

    
1484

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

    
1493
	$vpnid = 1;
1494

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

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

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

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

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

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

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

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

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

    
1545
				$index++;
1546
			}
1547

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1699
				$index++;
1700
			}
1701

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1861
}
1862

    
1863

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

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

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

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

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

    
1897

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

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

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

    
1977

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

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

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

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

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

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

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

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

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

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

    
2046
		unset($rrdold);
2047
	}
2048
	/* let apinger recreate required files */
2049
	setup_gateways_monitor();
2050

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

    
2060
		if ($g['booting'])
2061
			echo "Migrate RRD database {$database} to new format \n";
2062
		/* rename DS source */
2063
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2064
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2065

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

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

    
2080
		create_new_rrd("$rrdcreate");
2081
		/* create temporary xml from new RRD */
2082
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2083

    
2084
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2085
		$rrdold = $rrdold['rrd'];
2086

    
2087
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2088
		$rrdnew = $rrdnew['rrd'];
2089

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

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

    
2118

    
2119
function upgrade_055_to_056() {
2120
	global $config;
2121

    
2122
	if (!is_array($config['ca']))
2123
		$config['ca'] = array();
2124
	if (!is_array($config['cert']))
2125
		$config['cert'] = array();
2126

    
2127
	/* migrate ipsec ca's to cert manager */
2128
	if (is_array($config['ipsec']['cacert'])) {
2129
		foreach($config['ipsec']['cacert'] as & $cacert) {
2130
			$ca = array();
2131
			$ca['refid'] = uniqid();
2132
			if (is_array($cacert['cert']))
2133
				$ca['crt'] = $cacert['cert'][0];
2134
			else
2135
				$ca['crt'] = $cacert['cert'];
2136
			$ca['descr'] = $cacert['ident'];
2137
			$config['ca'][] = $ca;
2138
		}
2139
		unset($config['ipsec']['cacert']);
2140
	}
2141

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

    
2165

    
2166
function upgrade_056_to_057() {
2167
	global $config;
2168

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

    
2200
function upgrade_057_to_058() {
2201
	global $config;
2202
	/* set all phase2 entries to tunnel mode */
2203
	if (is_array($config['ipsec']['phase2']))
2204
		foreach($config['ipsec']['phase2'] as & $ph2ent)
2205
			$ph2ent['mode'] = 'tunnel';
2206
}
2207

    
2208
function upgrade_058_to_059() {
2209
	global $config;
2210

    
2211
	if (is_array($config['schedules']['schedule'])) {
2212
		foreach ($config['schedules']['schedule'] as & $schedl)
2213
			$schedl['schedlabel'] = uniqid();
2214
	}
2215
}
2216

    
2217
function upgrade_059_to_060() {
2218
	global $config;
2219
	require_once("/etc/inc/certs.inc");
2220
	if (is_array($config['ca'])) {
2221
		/* Locate issuer for all CAs */
2222
		foreach ($config['ca'] as & $ca) {
2223
			$subject = cert_get_subject($ca['crt']);
2224
			$issuer = cert_get_issuer($ca['crt']);
2225
			if($issuer <> $subject) {
2226
				$issuer_crt =& lookup_ca_by_subject($issuer);
2227
				if($issuer_crt)
2228
					$ca['caref'] = $issuer_crt['refid'];
2229
			}
2230
		}
2231

    
2232
		/* Locate issuer for all certificates */
2233
		if (is_array($config['cert'])) {
2234
			foreach ($config['cert'] as & $cert) {
2235
				$subject = cert_get_subject($cert['crt']);
2236
				$issuer = cert_get_issuer($cert['crt']);
2237
				if($issuer <> $subject) {
2238
					$issuer_crt =& lookup_ca_by_subject($issuer);
2239
					if($issuer_crt)
2240
						$cert['caref'] = $issuer_crt['refid'];
2241
				}
2242
			}
2243
		}
2244
	}
2245
}
2246

    
2247
function upgrade_060_to_061() {
2248
	global $config;
2249

    
2250
	if (is_array($config['interfaces']['wan']))
2251
		$config['interfaces']['wan']['enable'] = true;
2252
	if (is_array($config['interfaces']['lan']))
2253
		$config['interfaces']['lan']['enable'] = true;
2254

    
2255
	/* On 1.2.3 the "mtu" field adjusted MSS.
2256
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2257
	   See redmine ticket #1886
2258
	*/
2259
	foreach ($config['interfaces'] as $ifr => &$intf) {
2260
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2261
			$intf['mss'] = $intf['mtu'];
2262
			unset($intf['mtu']);
2263
		}
2264
	}
2265
}
2266

    
2267
function upgrade_061_to_062() {
2268
	global $config;
2269

    
2270
	/* Convert NAT port forwarding rules */
2271
	if (is_array($config['nat']['rule'])) {
2272
		$a_nat = &$config['nat']['rule'];
2273

    
2274
		foreach ($a_nat as &$natent) {
2275
			$natent['disabled'] = false;
2276
			$natent['nordr']    = false;
2277

    
2278
			$natent['source'] = array(
2279
				"not"     => false,
2280
				"any"     => true,
2281
				"port"    => ""
2282
			);
2283

    
2284
			$natent['destination'] = array(
2285
				"not"     => false,
2286
				"address" => $natent['external-address'],
2287
				"port"    => $natent['external-port']
2288
			);
2289

    
2290
			if (empty($natent['destination']['address'])) {
2291
				unset($natent['destination']['address']);
2292
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2293
			} else if ($natent['destination']['address'] == 'any') {
2294
				unset($natent['destination']['address']);
2295
				$natent['destination']['any'] = true;
2296
			}
2297

    
2298
			unset($natent['external-address']);
2299
			unset($natent['external-port']);
2300
		}
2301

    
2302
		unset($natent);
2303
	}
2304
}
2305

    
2306
function upgrade_062_to_063() {
2307
	/* Upgrade legacy Themes to the new pfsense_ng */
2308
	global $config;
2309

    
2310
	switch($config['theme']) {
2311
		case "nervecenter":
2312
			$config['theme'] = "pfsense_ng";
2313
			break;
2314
	}
2315

    
2316
}
2317

    
2318
function upgrade_063_to_064() {
2319
	global $config;
2320
	$j=0;
2321
	$ifcfg = &$config['interfaces'];
2322

    
2323
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
2324
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2325
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2326
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2327
			$j++;
2328
			if (isset($ppp['port'])){
2329
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2330
				unset($config['ppps']['ppp'][$pppid]['port']);
2331
			}
2332
			if (!isset($ppp['type'])){
2333
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2334
			}
2335
			if (isset($ppp['defaultgw']))
2336
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2337
		}
2338
	}
2339

    
2340
	if (!is_array($config['ppps']['ppp']))
2341
		$config['ppps']['ppp'] = array();
2342
	$a_ppps = &$config['ppps']['ppp'];
2343

    
2344
	foreach ($ifcfg as $ifname => $ifinfo) {
2345
		$ppp = array();
2346
		// For pppoe conversion
2347
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2348
			if (isset($ifinfo['ptpid']))
2349
				continue;
2350
			$ppp['ptpid'] =  $j;
2351
			$ppp['type'] = $ifinfo['ipaddr'];
2352
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2353
			$ppp['ports'] = $ifinfo['if'];
2354
			if ($ifinfo['ipaddr'] == "pppoe"){
2355
				$ppp['username'] = $ifinfo['pppoe_username'];
2356
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2357
			}
2358
			if ($ifinfo['ipaddr'] == "pptp"){
2359
				$ppp['username'] = $ifinfo['pptp_username'];
2360
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2361
			}
2362

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

    
2386
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2387
			$j++;
2388

    
2389
			unset($ifcfg[$ifname]['pppoe_username']);
2390
			unset($ifcfg[$ifname]['pppoe_password']);
2391
			unset($ifcfg[$ifname]['provider']);
2392
			unset($ifcfg[$ifname]['ondemand']);
2393
			unset($ifcfg[$ifname]['timeout']);
2394
			unset($ifcfg[$ifname]['pppoe_reset']);
2395
			unset($ifcfg[$ifname]['pppoe_preset']);
2396
			unset($ifcfg[$ifname]['pppoe']);
2397
			unset($ifcfg[$ifname]['pptp_username']);
2398
			unset($ifcfg[$ifname]['pptp_password']);
2399
			unset($ifcfg[$ifname]['local']);
2400
			unset($ifcfg[$ifname]['subnet']);
2401
			unset($ifcfg[$ifname]['remote']);
2402

    
2403
			$a_ppps[] = $ppp;
2404

    
2405
		}
2406
	}
2407
}
2408

    
2409
function upgrade_064_to_065() {
2410
	/* Disable TSO and LRO in upgraded configs */
2411
	global $config;
2412
	$config['system']['disablesegmentationoffloading'] = true;
2413
	$config['system']['disablelargereceiveoffloading'] = true;
2414
}
2415

    
2416
function upgrade_065_to_066() {
2417
	global $config;
2418

    
2419
	$dhcrelaycfg =& $config['dhcrelay'];
2420

    
2421
	if (is_array($dhcrelaycfg)) {
2422
		$dhcrelayifs = array();
2423
		$foundifs = false;
2424
		/* DHCPRelay enabled on any interfaces? */
2425
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2426
			if (isset($dhcrelayifconf['enable'])) {
2427
				$dhcrelayifs[] = $dhcrelayif;
2428
				unset($dhcrelaycfg['dhcrelayif']);
2429
				$foundifs = true;
2430
			}
2431
		}
2432
		if ($foundifs == true)
2433
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2434
	}
2435
}
2436

    
2437
function upgrade_066_to_067() {
2438
	global $config;
2439
	if (isset($config['system']['ca'])) {
2440
		$config['ca'] = $config['system']['ca'];
2441
	}
2442
	if (isset($config['system']['cert'])) {
2443
		$config['cert'] = $config['system']['cert'];
2444
	}
2445
}
2446

    
2447
function upgrade_067_to_068() {
2448
	global $config;
2449

    
2450
	if (!empty($config['pppoe'])) {
2451
		$config['pppoes'] = array();
2452
		$config['pppoes']['pppoe'] = array();
2453
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2454

    
2455
		if (is_array($config['pppoe']['user'])) {
2456
			$username = array();
2457
			foreach ($config['pppoe']['user'] as $user) {
2458
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2459
				if ($user['ip'])
2460
					$usr .= ":{$user['ip']}";
2461
				$username[] = $usr;
2462
			}
2463
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2464
		}
2465
		unset($config['pppoe']);
2466
	}
2467
}
2468

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

    
2494
function upgrade_069_to_070() {
2495
	global $config;
2496

    
2497
	/* Convert NAT 1:1 rules */
2498
	if (is_array($config['nat']['onetoone'])) {
2499
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2500
			if ($natent['subnet'] == 32)
2501
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2502
			else
2503
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2504

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

    
2507
			unset($config['nat']['onetoone'][$nidx]['internal']);
2508
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2509
		}
2510

    
2511
		unset($natent);
2512
	}
2513
}
2514

    
2515
function upgrade_070_to_071() {
2516
	global $config;
2517

    
2518
	if (is_array($config['cron']['item'])) {
2519
		foreach($config['cron']['item'] as $idx => $cronitem) {
2520
			if(stristr($cronitem['command'], "checkreload.sh")) {
2521
				unset($config['cron']['item'][$idx]);
2522
				break;
2523
			}
2524
		}
2525
	}
2526
}
2527

    
2528
function rename_field(& $section, $oldname, $newname) {
2529
	if (is_array($section)) {
2530
		foreach($section as & $item) {
2531
			if (is_array($item) && !empty($item[$oldname]))
2532
				$item[$newname] = $item[$oldname];
2533
			if (is_array($item) && isset($item[$oldname]))
2534
				unset($item[$oldname]);
2535
		}
2536
	}
2537
}
2538

    
2539
function upgrade_071_to_072() {
2540
	global $config;
2541
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2542
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2543
}
2544

    
2545
function upgrade_072_to_073() {
2546
	global $config;
2547
	if (!is_array($config['load_balancer']))
2548
		return;
2549
	if (is_array($config['load_balancer']['monitor_type']))
2550
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2551
	if (is_array($config['load_balancer']['lbpool']))
2552
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2553
	if (is_array($config['load_balancer']['lbaction']))
2554
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2555
	if (is_array($config['load_balancer']['lbprotocol']))
2556
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2557
	if (is_array($config['load_balancer']['virtual_server']))
2558
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2559
}
2560

    
2561
function upgrade_073_to_074() {
2562
	global $config;
2563
	rename_field($config['system']['user'], 'fullname', 'descr');
2564
}
2565

    
2566
function upgrade_074_to_075() {
2567
	global $config;
2568
	if (is_array($config['ca']))
2569
		rename_field($config['ca'], 'name', 'descr');
2570
	if (is_array($config['cert']))
2571
		rename_field($config['cert'], 'name', 'descr');
2572
	if (is_array($config['crl']))
2573
		rename_field($config['crl'], 'name', 'descr');
2574
}
2575

    
2576
function upgrade_075_to_076() {
2577
	global $config;
2578
	$cron_item = array();
2579
	$cron_item['minute'] = "30";
2580
	$cron_item['hour'] = "12";
2581
	$cron_item['mday'] = "*";
2582
	$cron_item['month'] = "*";
2583
	$cron_item['wday'] = "*";
2584
	$cron_item['who'] = "root";
2585
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2586
	$config['cron']['item'][] = $cron_item;
2587
}
2588

    
2589
function upgrade_076_to_077() {
2590
	global $config;
2591
	foreach($config['filter']['rule'] as & $rule) {
2592
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2593
		$rule['protocol'] = strtolower($rule['protocol']);
2594
	}
2595
}
2596

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

    
2624
function upgrade_079_to_080() {
2625
	global $config;
2626

    
2627
	/* Upgrade config in 1.2.3 specifying a username other than admin for synching. */
2628
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2629
		is_array($config['installedpackages']['carpsettings']['config'])) {
2630
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2631
		unset($config['system']['username']);
2632
	}
2633
}
2634

    
2635
function upgrade_080_to_081() {
2636
	global $config;
2637
	global $g;
2638
	/* Welcome to the 2.1 migration path */
2639

    
2640
	/* tag all the existing gateways as being IPv4 */
2641
	$i = 0;
2642
	if(is_array($config['gateways']['gateway_item'])) {
2643
		foreach($config['gateways']['gateway_item'] as $gw) {
2644
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2645
			$i++;
2646
		}
2647
	}
2648

    
2649
	/* RRD files changed for quality, traffic and packets graphs */
2650
	/* convert traffic RRD file */
2651
	global $parsedcfg, $listtags;
2652
	$listtags = array("ds", "v", "rra", "row");
2653

    
2654
	$rrddbpath = "/var/db/rrd/";
2655
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2656

    
2657
	if ($g['platform'] != "pfSense") {
2658
		/* restore the databases, if we have one */
2659
		if (restore_rrd()) {
2660
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2661
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2662
		}
2663
	}
2664

    
2665
	$rrdinterval = 60;
2666
	$valid = $rrdinterval * 2;
2667

    
2668
	/* Asume GigE for now */
2669
	$downstream = 125000000;
2670
	$upstream = 125000000;
2671

    
2672
	/* build a list of traffic and packets databases */
2673
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2674
	rsort($databases);
2675
	foreach($databases as $database) {
2676
		$xmldump = "{$database}.old.xml";
2677
		$xmldumpnew = "{$database}.new.xml";
2678

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

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

    
2685
		/* search and replace tags to add data sources */
2686
		$ds_search = "<!-- Round Robin Archives -->";
2687
		$ds_arr = array();
2688
		$ds_arr[] = "	<ds>
2689
				<name> inpass6 </name>
2690
				<type> COUNTER </type>
2691
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2692
				<min> 0.0000000000e+00 </min>
2693
				<max> 1.2500000000e+08 </max>
2694

    
2695
				<!-- PDP Status -->
2696
				<last_ds> 0 </last_ds>
2697
				<value> NaN </value>
2698
				<unknown_sec> 3 </unknown_sec>
2699
			</ds>
2700
			";
2701
		$ds_arr[] = "	<ds>
2702
				<name> outpass6 </name>
2703
				<type> COUNTER </type>
2704
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2705
				<min> 0.0000000000e+00 </min>
2706
				<max> 1.2500000000e+08 </max>
2707

    
2708
				<!-- PDP Status -->
2709
				<last_ds> 0 </last_ds>
2710
				<value> NaN </value>
2711
				<unknown_sec> 3 </unknown_sec>
2712
			</ds>
2713
			";
2714
		$ds_arr[] = "	<ds>
2715
				<name> inblock6 </name>
2716
				<type> COUNTER </type>
2717
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2718
				<min> 0.0000000000e+00 </min>
2719
				<max> 1.2500000000e+08 </max>
2720

    
2721
				<!-- PDP Status -->
2722
				<last_ds> 0 </last_ds>
2723
				<value> NaN </value>
2724
				<unknown_sec> 3 </unknown_sec>
2725
			</ds>
2726
			";
2727
		$ds_arr[] = "	<ds>
2728
				<name> outblock6 </name>
2729
				<type> COUNTER </type>
2730
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2731
				<min> 0.0000000000e+00 </min>
2732
				<max> 1.2500000000e+08 </max>
2733

    
2734
				<!-- PDP Status -->
2735
				<last_ds> 0 </last_ds>
2736
				<value> NaN </value>
2737
				<unknown_sec> 3 </unknown_sec>
2738
			</ds>
2739
			";
2740

    
2741
		$cdp_search = "<\/cdp_prep>";
2742
		$cdp_replace = "</cdp_prep>";
2743
		$cdp_arr = array();
2744
		$cdp_arr[] = "			<ds>
2745
					<primary_value> NaN </primary_value>
2746
					<secondary_value> 0.0000000000e+00 </secondary_value>
2747
					<value> NaN </value>
2748
					<unknown_datapoints> 0 </unknown_datapoints>
2749
					</ds>
2750
		";
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

    
2773
		$value_search = "<\/row>";
2774
		$value_replace = "</row>";
2775
		$value = "<v> NaN </v>";
2776

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

    
2808
function upgrade_081_to_082() {
2809
	/* don't enable the allow IPv6 toggle */
2810
}
2811

    
2812
function upgrade_082_to_083() {
2813
	global $config;
2814

    
2815
	/* Upgrade captiveportal config */
2816
	if (!empty($config['captiveportal'])) {
2817
		$tmpcp = $config['captiveportal'];
2818
		$config['captiveportal'] = array();
2819
		$config['captiveportal']['cpzone'] = array();
2820
		$config['captiveportal']['cpzone'] = $tmpcp;
2821
		$config['captiveportal']['cpzone']['zoneid'] = 8000;
2822
		$config['captiveportal']['cpzone']['zone'] = "cpzone";
2823
		if ($config['captiveportal']['cpzone']['auth_method'] == "radius")
2824
			$config['captiveportal']['cpzone']['radius_protocol'] = "PAP";
2825
	}
2826
	if (!empty($config['voucher'])) {
2827
		$tmpcp = $config['voucher'];
2828
		$config['voucher'] = array();
2829
		$config['voucher']['cpzone'] = array();
2830
		$config['voucher']['cpzone'] = $tmpcp;
2831
	}
2832
}
2833

    
2834
function upgrade_083_to_084() {
2835
	global $config;
2836
	if (!isset($config['hasync'])) {
2837
		if (!empty($config['installedpackages']) &&
2838
		    !empty($config['installedpackages']['carpsettings']) &&
2839
		    !empty($config['installedpackages']['carpsettings']['config'])) {
2840
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
2841
			unset($config['installedpackages']['carpsettings']);
2842
		}
2843
		if (empty($config['installedpackages']['carpsettings'])) {
2844
			unset($config['installedpackages']['carpsettings']);
2845
		}
2846
		if (empty($config['installedpackages'])) {
2847
			unset($config['installedpackages']);
2848
		}
2849
	}
2850
}
2851

    
2852
function upgrade_084_to_085() {
2853
	global $config;
2854

    
2855
	$gateway_group_arr = array();
2856
	$gateways = return_gateways_array();
2857
	$oldnames = array();
2858
	/* setup translation array */
2859
	foreach($gateways as $name => $gw) {
2860
		if(isset($gw['dynamic'])){
2861
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
2862
			$oldnames[$oldname] = $name;
2863
		} else {
2864
			$oldnames[$name] = $name;
2865
		}
2866
	}
2867

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

    
2898
function upgrade_085_to_086() {
2899
	global $config, $g;
2900

    
2901
	/* XXX: Gross hacks in sight */
2902
	if (is_array($config['virtualip']['vip'])) {
2903
		$vipchg = array();
2904
		foreach ($config['virtualip']['vip'] as $vip) {
2905
			if ($vip['mode'] != "carp")
2906
				continue;
2907
			$config = array_replace_values_recursive(
2908
				$config,
2909
				'^vip' . $vip['vhid'] . '$',
2910
				"{$vip['interface']}_vip{$vip['vhid']}"
2911
			);
2912
		}
2913
	}
2914
}
2915

    
2916
function upgrade_086_to_087() {
2917
	global $config, $dummynet_pipe_list;
2918

    
2919
	if (!is_array($config['filter']) || !is_array($config['filter']['rule']))
2920
		return;
2921
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue']))
2922
		return;
2923

    
2924
	$dnqueue_number = 1;
2925
	$dnpipe_number = 1;
2926

    
2927
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
2928
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
2929
		$dnpipe_number++;
2930
		if (is_array($dnpipe['queue'])) {
2931
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
2932
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
2933
				$dnqueue_number++;
2934
			}
2935
		}
2936
	}
2937

    
2938
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
2939

    
2940
	require_once("shaper.inc");
2941
	read_dummynet_config();
2942

    
2943
	$dn_list = array();
2944
	if (is_array($dummynet_pipe_list)) {
2945
		foreach ($dummynet_pipe_list as $dn) {
2946
			$tmplist =& $dn->get_queue_list();
2947
			foreach ($tmplist as $qname => $link) {
2948
				$dn_list[$link] = $qname;
2949
			}
2950
		}
2951
		unset($dummynet_pipe_list);
2952
	}
2953

    
2954
	foreach ($config['filter']['rule'] as $idx => $rule) {
2955
		if (!empty($rule['dnpipe'])) {
2956
			if (!empty($dn_list[$rule['dnpipe']]))
2957
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
2958
		}
2959
		if (!empty($rule['pdnpipe'])) {
2960
			if (!empty($dn_list[$rule['pdnpipe']]))
2961
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
2962
		}
2963
	}
2964
}
2965
function upgrade_087_to_088() {
2966
	global $config;
2967
	if (isset($config['system']['glxsb_enable'])) {
2968
		unset($config['system']['glxsb_enable']);
2969
		$config['system']['crypto_hardware'] = "glxsb";
2970
	}
2971
}
2972

    
2973
function upgrade_088_to_089() {
2974
	global $config;
2975
	if (!is_array($config['ca']))
2976
		$config['ca'] = array();
2977
	if (!is_array($config['cert']))
2978
		$config['cert'] = array();
2979

    
2980
	/* migrate captive portal ssl to certifcate mngr */
2981
	if (is_array($config['captiveportal'])) {
2982
		foreach ($config['captiveportal'] as $id => &$setting) {
2983
			if (isset($setting['httpslogin'])) {
2984
				/* create cert entry */
2985
				$cert = array();
2986
				$cert['refid'] = uniqid();
2987
				$cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
2988
				$cert['crt'] = $setting['certificate'];
2989
				$cert['prv'] = $setting['private-key'];
2990

    
2991
				if (!empty($setting['cacertificate'])) {
2992
					/* create ca entry */
2993
					$ca = array();
2994
					$ca['refid'] = uniqid();
2995
					$ca['descr'] = "Captive Portal CA - {$setting['zone']}";
2996
					$ca['crt'] = $setting['cacertificate'];
2997
					$config['ca'][] = $ca;
2998

    
2999
					/* add ca reference to certificate */
3000
					$cert['caref'] = $ca['refid'];
3001
				}
3002

    
3003
				$config['cert'][] = $cert;
3004

    
3005
				/* create cert reference */
3006
				$setting['certref'] = $cert['refid'];
3007

    
3008
				unset($setting['certificate']);
3009
				unset($setting['private-key']);
3010
				unset($setting['cacertificate']);
3011

    
3012
			}
3013
		}
3014
	}
3015
}
3016

    
3017
function upgrade_089_to_090() {
3018
	global $config;
3019
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
3020
		$vs_a = &$config['load_balancer']['virtual_server'];
3021
		for ($i = 0; isset($vs_a[$i]); $i++) {
3022
			if (is_array($vs_a[$i]['pool'])) {
3023
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
3024
				unset($vs_a[$i]['pool']);
3025
			} elseif (!empty($vs_a[$i]['pool'])) {
3026
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
3027
				unset($vs_a[$i]['pool']);
3028
			}
3029
		}
3030
	}
3031
}
3032

    
3033
function upgrade_090_to_091() {
3034
	global $config;
3035

    
3036
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
3037
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
3038
			if (!empty($dnqueue['bandwidth'])) {
3039
				$bw = array();
3040
				$bw['bw'] = $dnqueue['bandwidth'];
3041
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
3042
				$bw['bwsched'] = "none";
3043
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
3044
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
3045
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
3046
			}
3047
		}
3048
	}
3049
}
3050

    
3051
function upgrade_091_to_092() {
3052
	global $config;
3053

    
3054
	if (is_array($config['nat']['advancedoutbound']) && is_array($config['nat']['advancedoutbound']['rule'])) {
3055
		$nat_rules = &$config['nat']['advancedoutbound']['rule'];
3056
		for ($i = 0; isset($nat_rules[$i]); $i++) {
3057
			if (empty($nat_rules[$i]['interface'])) {
3058
				$nat_rules[$i]['interface'] = 'wan';
3059
			}
3060
		}
3061
	}
3062
}
3063

    
3064
function upgrade_092_to_093() {
3065
	global $g;
3066

    
3067
	$suffixes = array("concurrent", "loggedin");
3068

    
3069
	foreach ($suffixes as $suffix)
3070
		if (file_exists("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd"))
3071
			rename("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd",
3072
				"{$g['vardb_path']}/rrd/captiveportal-cpZone-{$suffix}.rrd");
3073

    
3074
	enable_rrd_graphing();
3075
}
3076

    
3077
function upgrade_093_to_094() {
3078
	global $config;
3079

    
3080
	if (isset($config['system']['powerd_mode'])) {
3081
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3082
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3083
		unset($config['system']['powerd_mode']);
3084
	}
3085
}
3086

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

    
3090
	if (!isset($config['interfaces']) || !is_array($config['interfaces']))
3091
		return;
3092

    
3093
	foreach ($config['interfaces'] as $iface => $cfg)
3094
		if (isset($cfg['ipaddrv6']) && ($cfg['ipaddrv6'] == "track6"))
3095
			if (!isset($cfg['track6-prefix-id']) || ($cfg['track6-prefix-id'] == ""))
3096
				$config['interfaces'][$iface]['track6-prefix-id'] = 0;
3097
}
3098

    
3099
function upgrade_095_to_096() {
3100
	global $config, $g;
3101

    
3102
	$names = array("inpass", "outpass", "inblock", "outblock",
3103
		"inpass6", "outpass6", "inblock6", "outblock6");
3104
	$rrddbpath = "/var/db/rrd";
3105
	$rrdtool = "/usr/local/bin/rrdtool";
3106

    
3107
	if ($g['platform'] != "pfSense") {
3108
		/* restore the databases, if we have one */
3109
		if (restore_rrd()) {
3110
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
3111
			rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup");
3112
		}
3113
	}
3114

    
3115
	/* Assume 2*10GigE for now */
3116
	$stream = 2500000000;
3117

    
3118
	/* build a list of traffic and packets databases */
3119
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
3120
	rsort($databases);
3121
	foreach($databases as $database) {
3122
		if ($g['booting'])
3123
			echo "Update RRD database {$database}.\n";
3124

    
3125
		$cmd = "{$rrdtool} tune {$rrddbpath}/{$database}";
3126
		foreach ($names as $name)
3127
			$cmd .= " -a {$name}:{$stream}";
3128
		mwexec("{$cmd} 2>&1");
3129

    
3130
	}
3131
	enable_rrd_graphing();
3132
	/* Let's save the RRD graphs after we run enable RRD graphing */
3133
	/* The function will restore the rrd.tgz so we will save it after */
3134
	exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
3135
}
3136

    
3137
function upgrade_096_to_097() {
3138
	global $config, $g;
3139
	/* If the user had disabled default block rule logging before, then bogon/private network logging was already off, so respect their choice. */
3140
	if (isset($config['syslog']['nologdefaultblock'])) {
3141
		$config['syslog']['nologbogons'] = true;
3142
		$config['syslog']['nologprivatenets'] = true;
3143
	}
3144
}
3145

    
3146
function upgrade_097_to_098() {
3147
	global $config, $g;
3148
	/* Disable kill_states by default */
3149
	$config['system']['kill_states'] = true;
3150
}
3151

    
3152
function upgrade_098_to_099() {
3153
	global $config;
3154

    
3155
	if (empty($config['dhcpd']) || !is_array($config['dhcpd']))
3156
		return;
3157

    
3158
	foreach ($config['dhcpd'] as & $dhcpifconf) {
3159
		if (isset($dhcpifconf['next-server'])) {
3160
			$dhcpifconf['nextserver'] = $dhcpifconf['next-server'];
3161
			unset($dhcpifconf['next-server']);
3162
		}
3163
	}
3164
}
3165

    
3166
function upgrade_099_to_100() {
3167
	require_once("/etc/inc/services.inc");
3168
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3169
}
3170

    
3171
function upgrade_100_to_101() {
3172
	global $config, $g;
3173

    
3174
	if (!is_array($config['voucher']))
3175
		return;
3176

    
3177
	foreach ($config['voucher'] as $cpzone => $cp) {
3178
		if (!is_array($cp['roll']))
3179
			continue;
3180
		foreach ($cp['roll'] as $ridx => $rcfg) {
3181
			if (!empty($rcfg['comment']))
3182
				$config['voucher'][$cpzone]['roll'][$ridx]['descr'] = $rcfg['comment'];
3183
		}
3184
	}
3185
}
3186

    
3187
function upgrade_101_to_102() {
3188
	global $config, $g;
3189

    
3190
	if (is_array($config['captiveportal'])) {
3191
		foreach ($config['captiveportal'] as $cpzone => $cp) {
3192
			if (!is_array($cp['passthrumac']))
3193
				continue;
3194

    
3195
			foreach ($cp['passthrumac'] as $idx => $passthrumac)
3196
				$config['captiveportal'][$cpzone]['passthrumac'][$idx]['action'] = 'pass';
3197
		}
3198
	}
3199

    
3200
	/* Convert OpenVPN Compression option to the new style */
3201
	// Nothing to do if there is no OpenVPN tag
3202
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
3203
		if (is_array($config['openvpn']['openvpn-server'])) {
3204
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
3205
				if (!empty($vpn['compression']))
3206
					$vpn['compression'] = "adaptive";
3207
			}
3208
		}
3209
		if (is_array($config['openvpn']['openvpn-client'])) {
3210
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
3211
				if (!empty($vpn['compression']))
3212
					$vpn['compression'] = "adaptive";
3213
			}
3214
		}
3215
	}
3216
}
3217

    
3218
function upgrade_102_to_103() {
3219
	global $config;
3220

    
3221
	if (isset($config['nat']['advancedoutbound']['enable'])) {
3222
		$config['nat']['advancedoutbound']['mode'] = "advanced";
3223
		unset($config['nat']['advancedoutbound']['enable']);
3224
	} else
3225
		$config['nat']['advancedoutbound']['mode'] = "automatic";
3226

    
3227
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3228

    
3229
	unset($config['nat']['ipsecpassthru']);
3230
	unset($config['nat']['advancedoutbound']);
3231
}
3232

    
3233
function upgrade_103_to_104() {
3234
	global $config;
3235

    
3236
	$changed_privs = array(
3237
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3238
		"page-interfacess-groups" => "page-interfaces-groups",
3239
		"page-interfacess-lagg" => "page-interfaces-lagg",
3240
		"page-interfacess-qinq" => "page-interfaces-qinq"
3241
	);
3242

    
3243
	/* update user privileges */
3244
	foreach ($config['system']['user'] as & $user) {
3245
		if (!is_array($user['priv']))
3246
			continue;
3247
		foreach ($user['priv'] as & $priv) {
3248
			if (array_key_exists($priv, $changed_privs))
3249
				$priv = $changed_privs[$priv];
3250
		}
3251
	}
3252

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

    
3263
	/* sync all local account information */
3264
	local_sync_accounts();
3265
}
3266

    
3267
function upgrade_104_to_105() {
3268
	global $config;
3269

    
3270
	if (is_array($config['captiveportal'])) {
3271
		$zoneid = 2;
3272
		foreach ($config['captiveportal'] as $cpzone => $cpcfg) {
3273
			if (empty($cpfg['zoneid'])) {
3274
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3275
				$zoneid += 2;
3276
			} else if ($cpcfg['zoneid'] > 4000) {
3277
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3278
				$zoneid += 2;
3279
			}
3280
		}
3281
	}
3282
}
3283

    
3284
function upgrade_105_to_106() {
3285
	global $config;
3286

    
3287
	if (is_array($config['virtualip']) && is_array($config['virtualip']['vip'])) {
3288
		foreach ($config['virtualip']['vip'] as $vipidx => $vip) {
3289
			if ($vip['mode'] == "ipalias" && strstr($vip['interface'], "_vip")) {
3290
				/* Convert to a carp with same properties as its carp version */
3291
				$converted = false;
3292
				foreach ($config['virtualip']['vip'] as $bvip) {
3293
					if ($bvip['mode'] == "carp" && "{$bvip['interface']}_vip{$bvip['vhid']}" == $vip['interface']) {
3294
						$ipaaddr = $vip['subnet'];
3295
						$ipamask = $vip['subnet_bits'];
3296
						$config['virtualip']['vip'][$vipidx] = $bvip;
3297
						$config['virtualip']['vip'][$vipidx]['subnet'] = $ipaaddr;
3298
						$config['virtualip']['vip'][$vipidx]['subnet_bits'] = $ipamask;
3299
						$converted = true;
3300
						break;
3301
					}
3302
				}
3303
				if ($converted === false)
3304
					log_error("WARNING: IPalias {$vip['subnet']}/{$vip['subnet_bits']} was not completed successfully. Upgrading it yourself is the only remaining option!");
3305
			}
3306
		}
3307
		unset($vip, $bvip, $vipidx);
3308
	}
3309
}
3310

    
3311
function upgrade_106_to_107() {
3312
	global $config;
3313

    
3314
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
3315
		$tracker = (int)microtime(true);
3316
		foreach ($config['filter']['rule'] as $ridx => $rule) {
3317
			if (empty($rule['tracker'])) {
3318
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
3319
				$tracker++;
3320
			}
3321
		}
3322
		unset($tracker, $ridx);
3323
	}
3324
	if (is_array($config['nat']) && is_array($config['nat']['rule'])) {
3325
		$tracker = (int)microtime(true);
3326
		foreach ($config['nat']['rule'] as $ridx => $rule) {
3327
			if (empty($rule['tracker'])) {
3328
				$config['nat']['rule'][$ridx]['tracker'] = $tracker;
3329
				$tracker++;
3330
			}
3331

    
3332
		}
3333
		unset($tracker, $ridx);
3334
	}
3335
}
3336

    
3337
function upgrade_107_to_108() {
3338
	global $config;
3339

    
3340
	if (isset($config['system']['webgui']['noautocomplete']))
3341
		unset($config['system']['webgui']['noautocomplete']);
3342
	else
3343
		$config['system']['webgui']['loginautocomplete'] = true;
3344
}
3345

    
3346
function upgrade_108_to_109() {
3347
	global $config;
3348

    
3349
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule']))
3350
		return;
3351

    
3352
	foreach ($config['filter']['rule'] as &$rule) {
3353
		if (!isset($rule['dscp']) || empty($rule['dscp']))
3354
			continue;
3355

    
3356
		$pos = strpos($rule['dscp'], ' ');
3357
		if ($pos !== false)
3358
			$rule['dscp'] = substr($rule['dscp'], 0, $pos);
3359
		unset($pos);
3360
	}
3361
}
3362

    
3363
function upgrade_109_to_110() {
3364
	global $config;
3365

    
3366
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2']))
3367
		return;
3368

    
3369
	foreach ($config['ipsec']['phase2'] as &$rule) {
3370
		if (!empty($rule['uniqid']))
3371
			continue;
3372

    
3373
		$rule['uniqid'] = uniqid();
3374
	}
3375
}
3376

    
3377
function upgrade_110_to_111() {
3378
	global $config;
3379

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

    
3384
	/* cleanup old unbound package stuffs */
3385
	unlink_if_exists("/usr/local/pkg/unbound.xml");
3386
	unlink_if_exists("/usr/local/pkg/unbound.inc");
3387
	unlink_if_exists("/usr/local/pkg/unbound_advanced.xml");
3388
	unlink_if_exists("/usr/local/www/unbound_status.php");
3389
	unlink_if_exists("/usr/local/www/unbound_acls.php");
3390
	unlink_if_exists("/usr/local/bin/unbound_monitor.sh");
3391

    
3392
	/* Remove old menu and service entries */
3393
	if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
3394
		foreach ($config['installedpackages']['menu'] as $idx => $menu) {
3395
			if ($menu['name'] != 'Unbound DNS')
3396
				continue;
3397

    
3398
			unset($config['installedpackages']['menu'][$idx]);
3399
			break;
3400
		}
3401
	}
3402

    
3403
	if (isset($config['installedpackages']['service']) && is_array($config['installedpackages']['service'])) {
3404
		foreach ($config['installedpackages']['service'] as $idx => $service) {
3405
			if ($service['name'] != 'unbound')
3406
				continue;
3407
			unset($config['installedpackages']['service'][$idx]);
3408
			break;
3409
		}
3410
	}
3411

    
3412
	if (!isset($config['installedpackages']['unbound']['config'][0]))
3413
		return;
3414

    
3415
	$pkg = $config['installedpackages']['unbound']['config'][0];
3416

    
3417
	if (isset($config['installedpackages']['unboundadvanced']['config'][0]))
3418
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3419

    
3420
	$new = array();
3421

    
3422
	/* deal first with boolean fields */
3423
	$fields = array(
3424
		"enable" => "enable",
3425
		"dnssec_status" => "dnssec",
3426
		"forwarding_mode" => "forwarding",
3427
		"regdhcp" => "regdhcp",
3428
		"regdhcpstatic" => "regdhcpstatic",
3429
		"txtsupport" => "txtsupport",
3430
		"hide_id" => "hideidentity",
3431
		"hide_version" => "hideversion",
3432
		"prefetch" => "prefetch",
3433
		"prefetch_key" => "prefetchkey",
3434
		"harden_glue" => "hardenglue",
3435
		"harden_dnssec_stripped" => "dnssec_stripped");
3436

    
3437
	foreach ($fields as $oldk => $newk) {
3438
		if (isset($pkg[$oldk])) {
3439
			if ($pkg[$oldk] == 'on')
3440
				$new[$newk] = true;
3441
			unset($pkg[$oldk]);
3442
		}
3443
	}
3444

    
3445
	$fields = array(
3446
		"active_interface" => "network_interface",
3447
		"query_interface" => "outgoing_interface",
3448
		"unbound_verbosity" => "log_verbosity",
3449
		"unbound_verbosity" => "log_verbosity",
3450
		"msg_cache_size" => "msgcachesize",
3451
		"outgoing_num_tcp" => "outgoing_num_tcp",
3452
		"incoming_num_tcp" => "incoming_num_tcp",
3453
		"edns_buffer_size" => "edns_buffer_size",
3454
		"num_queries_per_thread" => "num_queries_per_thread",
3455
		"jostle_timeout" => "jostle_timeout",
3456
		"cache_max_ttl" => "cache_max_ttl",
3457
		"cache_min_ttl" => "cache_min_ttl",
3458
		"infra_host_ttl" => "infra_host_ttl",
3459
		"infra_cache_numhosts" => "infra_cache_numhosts",
3460
		"unwanted_reply_threshold" => "unwanted_reply_threshold",
3461
		"custom_options" => "custom_options");
3462

    
3463
	foreach ($fields as $oldk => $newk) {
3464
		if (isset($pkg[$oldk])) {
3465
			$new[$newk] = $pkg[$oldk];
3466
			unset($pkg[$oldk]);
3467
		}
3468
	}
3469

    
3470
	/* Following options were removed, bring them as custom_options */
3471
	if (isset($pkg['stats']) && $pkg['stats'] == "on") {
3472
		if (isset($pkg['stats_interval']))
3473
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : ";") . "statistics-interval: {$pkg['stats_interval']}";
3474
		if (isset($pkg['cumulative_stats']))
3475
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : ";") . "statistics-cumulative: {$pkg['cumulative_stats']}";
3476
		if (isset($pkg['extended_stats']) && $pkg['extended_stats'] == "on")
3477
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : ";") . "extended-statistics: yes";
3478
		else
3479
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : ";") . "extended-statistics: no";
3480
	}
3481

    
3482
	$new['acls'] = array();
3483
	if (isset($config['installedpackages']['unboundacls']['config']) &&
3484
	    is_array($config['installedpackages']['unboundacls']['config'])) {
3485
		foreach ($config['installedpackages']['unboundacls']['config'] as $acl)
3486
			$new['acls'][] = $acl;
3487
	}
3488

    
3489
	$config['unbound'] = $new;
3490

    
3491
	if(isset($config['installedpackages']['unbound']))
3492
		unset($config['installedpackages']['unbound']);
3493
	if(isset($config['installedpackages']['unboundadvanced']))
3494
		unset($config['installedpackages']['unboundadvanced']);
3495
	if(isset($config['installedpackages']['unboundacls']))
3496
		unset($config['installedpackages']['unboundacls']);
3497

    
3498
	unset($pkg, $new);
3499
}
3500

    
3501
?>
(55-55/68)