Project

General

Profile

Download (156 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * upgrade_config.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
if (!function_exists("dump_rrd_to_xml")) {
23
	require_once("rrd.inc");
24
}
25
if (!function_exists("read_altq_config")) {
26
	require_once("shaper.inc");
27
}
28

    
29
/* Upgrade functions must be named:
30
*    upgrade_XXX_to_YYY
31
	* where XXX == previous version, zero padded, and YYY == next version, zero padded
32
	*/
33
function upgrade_010_to_011() {
34
	global $config;
35
	$opti = 1;
36
	$ifmap = array('lan' => 'lan', 'wan' => 'wan', 'pptp' => 'pptp');
37

    
38
	/* convert DMZ to optional, if necessary */
39
	if (isset($config['interfaces']['dmz'])) {
40

    
41
		$dmzcfg = &$config['interfaces']['dmz'];
42

    
43
		if ($dmzcfg['if']) {
44
			$config['interfaces']['opt' . $opti] = array();
45
			$optcfg = &$config['interfaces']['opt' . $opti];
46

    
47
			$optcfg['enable'] = $dmzcfg['enable'];
48
			$optcfg['descr'] = "DMZ";
49
			$optcfg['if'] = $dmzcfg['if'];
50
			$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
51
			$optcfg['subnet'] = $dmzcfg['subnet'];
52

    
53
			$ifmap['dmz'] = "opt" . $opti;
54
			$opti++;
55
		}
56

    
57
		unset($config['interfaces']['dmz']);
58
	}
59

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

    
63
		if (!$config['interfaces']['wlan' . $i]['if']) {
64
			unset($config['interfaces']['wlan' . $i]);
65
			continue;
66
		}
67

    
68
		$wlancfg = &$config['interfaces']['wlan' . $i];
69
		$config['interfaces']['opt' . $opti] = array();
70
		$optcfg = &$config['interfaces']['opt' . $opti];
71

    
72
		$optcfg['enable'] = $wlancfg['enable'];
73
		$optcfg['descr'] = "WLAN" . $i;
74
		$optcfg['if'] = $wlancfg['if'];
75
		$optcfg['ipaddr'] = $wlancfg['ipaddr'];
76
		$optcfg['subnet'] = $wlancfg['subnet'];
77
		$optcfg['bridge'] = $wlancfg['bridge'];
78

    
79
		$optcfg['wireless'] = array();
80
		$optcfg['wireless']['mode'] = $wlancfg['mode'];
81
		$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
82
		$optcfg['wireless']['channel'] = $wlancfg['channel'];
83
		$optcfg['wireless']['wep'] = $wlancfg['wep'];
84

    
85
		$ifmap['wlan' . $i] = "opt" . $opti;
86

    
87
		unset($config['interfaces']['wlan' . $i]);
88
		$opti++;
89
	}
90

    
91
	/* convert filter rules */
92
	$n = count($config['filter']['rule']);
93
	for ($i = 0; $i < $n; $i++) {
94

    
95
		$fr = &$config['filter']['rule'][$i];
96

    
97
		/* remap interface */
98
		if (array_key_exists($fr['interface'], $ifmap)) {
99
			$fr['interface'] = $ifmap[$fr['interface']];
100
		} else {
101
			/* remove the rule */
102
			printf(gettext("%sWarning: filter rule removed " .
103
				"(interface '%s' does not exist anymore)."), "\n", $fr['interface']);
104
			unset($config['filter']['rule'][$i]);
105
			continue;
106
		}
107

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

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

    
135
	/* convert shaper rules */
136
	$n = count($config['pfqueueing']['rule']);
137
	if (is_array($config['pfqueueing']['rule'])) {
138
		for ($i = 0; $i < $n; $i++) {
139

    
140
			$fr = &$config['pfqueueing']['rule'][$i];
141

    
142
			/* remap interface */
143
			if (array_key_exists($fr['interface'], $ifmap)) {
144
				$fr['interface'] = $ifmap[$fr['interface']];
145
			} else {
146
				/* remove the rule */
147
				printf(gettext("%sWarning: traffic shaper rule removed " .
148
					"(interface '%s' does not exist anymore)."), "\n", $fr['interface']);
149
				unset($config['pfqueueing']['rule'][$i]);
150
				continue;
151
			}
152

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

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

    
182

    
183
function upgrade_011_to_012() {
184
	global $config;
185
	/* move LAN DHCP server config */
186
	$tmp = $config['dhcpd'];
187
	$config['dhcpd'] = array();
188
	$config['dhcpd']['lan'] = $tmp;
189

    
190
	/* encrypt password */
191
	$config['system']['password'] = crypt($config['system']['password']);
192
}
193

    
194

    
195
function upgrade_012_to_013() {
196
	global $config;
197
	/* convert advanced outbound NAT config */
198
	for ($i = 0; isset($config['nat']['advancedoutbound']['rule'][$i]); $i++) {
199
		$curent = &$config['nat']['advancedoutbound']['rule'][$i];
200
		$src = $curent['source'];
201
		$curent['source'] = array();
202
		$curent['source']['network'] = $src;
203
		$curent['destination'] = array();
204
		$curent['destination']['any'] = true;
205
	}
206

    
207
	/* add an explicit type="pass" to all filter rules to make things consistent */
208
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
209
		$config['filter']['rule'][$i]['type'] = "pass";
210
	}
211
}
212

    
213

    
214
function upgrade_013_to_014() {
215
	global $config;
216
	/* convert shaper rules (make pipes) */
217
	if (is_array($config['pfqueueing']['rule'])) {
218
		$config['pfqueueing']['pipe'] = array();
219

    
220
		for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
221
			$curent = &$config['pfqueueing']['rule'][$i];
222

    
223
			/* make new pipe and associate with this rule */
224
			$newpipe = array();
225
			$newpipe['descr'] = $curent['descr'];
226
			$newpipe['bandwidth'] = $curent['bandwidth'];
227
			$newpipe['delay'] = $curent['delay'];
228
			$newpipe['mask'] = $curent['mask'];
229
			$config['pfqueueing']['pipe'][$i] = $newpipe;
230

    
231
			$curent['targetpipe'] = $i;
232

    
233
			unset($curent['bandwidth']);
234
			unset($curent['delay']);
235
			unset($curent['mask']);
236
		}
237
	}
238
}
239

    
240

    
241
function upgrade_014_to_015() {
242
	global $config;
243
	/* Default route moved */
244
	if (isset($config['interfaces']['wan']['gateway'])) {
245
		if ($config['interfaces']['wan']['gateway'] <> "") {
246
			$config['system']['gateway'] = $config['interfaces']['wan']['gateway'];
247
		}
248
		unset($config['interfaces']['wan']['gateway']);
249
	}
250

    
251
	/* Queues are no longer interface specific */
252
	if (isset($config['interfaces']['lan']['schedulertype'])) {
253
		unset($config['interfaces']['lan']['schedulertype']);
254
	}
255
	if (isset($config['interfaces']['wan']['schedulertype'])) {
256
		unset($config['interfaces']['wan']['schedulertype']);
257
	}
258

    
259
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
260
		if (isset($config['interfaces']['opt' . $i]['schedulertype'])) {
261
			unset($config['interfaces']['opt' . $i]['schedulertype']);
262
		}
263
	}
264
}
265

    
266

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

    
284

    
285
function upgrade_016_to_017() {
286
	global $config;
287
	/* wipe previous shaper configuration */
288
	if (isset($config['shaper']['queue'])) {
289
		unset($config['shaper']['queue']);
290
	}
291
	if (isset($config['shaper']['rule'])) {
292
		unset($config['shaper']['rule']);
293
	}
294
	if (isset($config['interfaces']['wan']['bandwidth'])) {
295
		unset($config['interfaces']['wan']['bandwidth']);
296
	}
297
	if (isset($config['interfaces']['wan']['bandwidthtype'])) {
298
		unset($config['interfaces']['wan']['bandwidthtype']);
299
	}
300
	if (isset($config['interfaces']['lan']['bandwidth'])) {
301
		unset($config['interfaces']['lan']['bandwidth']);
302
	}
303
	if (isset($config['interfaces']['lan']['bandwidthtype'])) {
304
		unset($config['interfaces']['lan']['bandwidthtype']);
305
	}
306
	$config['shaper']['enable'] = FALSE;
307
}
308

    
309

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

    
359
	/* enable SSH */
360
	if ($config['version'] == "1.8") {
361
		$config['system']['sshenabled'] = true;
362
	}
363
}
364

    
365

    
366
function upgrade_018_to_019() {
367
	global $config;
368
}
369

    
370

    
371
function upgrade_019_to_020() {
372
	global $config;
373
	if (is_array($config['ipsec']['tunnel'])) {
374
		reset($config['ipsec']['tunnel']);
375
		while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
376
			/* Sanity check on required variables */
377
			/* This fixes bogus <tunnel> entries - remnant of bug #393 */
378
			if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
379
				unset($config['ipsec']['tunnel'][$tunnel]);
380
			}
381
		}
382
	}
383
}
384

    
385
function upgrade_020_to_021() {
386
	global $config;
387
	/* shaper scheduler moved */
388
	if (isset($config['system']['schedulertype'])) {
389
		$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
390
		unset($config['system']['schedulertype']);
391
	}
392
}
393

    
394

    
395
function upgrade_021_to_022() {
396
	global $config;
397
	/* move gateway to wan interface */
398
	$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
399
}
400

    
401
function upgrade_022_to_023() {
402
	global $config;
403
	if (isset($config['shaper'])) {
404
		/* wipe previous shaper configuration */
405
		unset($config['shaper']);
406
	}
407
}
408

    
409

    
410
function upgrade_023_to_024() {
411
	global $config;
412
}
413

    
414

    
415
function upgrade_024_to_025() {
416
	global $config;
417
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
418
	if (isset($config['system']['use_rrd_gateway'])) {
419
		unset($config['system']['use_rrd_gateway']);
420
	}
421
}
422

    
423

    
424
function upgrade_025_to_026() {
425
	global $config;
426
	$cron_item = array();
427
	$cron_item['minute'] = "0";
428
	$cron_item['hour'] = "*";
429
	$cron_item['mday'] = "*";
430
	$cron_item['month'] = "*";
431
	$cron_item['wday'] = "*";
432
	$cron_item['who'] = "root";
433
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
434

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

    
437
	$cron_item = array();
438
	$cron_item['minute'] = "1,31";
439
	$cron_item['hour'] = "0-5";
440
	$cron_item['mday'] = "*";
441
	$cron_item['month'] = "*";
442
	$cron_item['wday'] = "*";
443
	$cron_item['who'] = "root";
444
	$cron_item['command'] = "/usr/bin/nice -n20 adjkerntz -a";
445

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

    
448
	$cron_item = array();
449
	$cron_item['minute'] = "1";
450
	$cron_item['hour'] = "*";
451
	$cron_item['mday'] = "1";
452
	$cron_item['month'] = "*";
453
	$cron_item['wday'] = "*";
454
	$cron_item['who'] = "root";
455
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
456

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

    
459
	$cron_item = array();
460
	$cron_item['minute'] = "*/60";
461
	$cron_item['hour'] = "*";
462
	$cron_item['mday'] = "*";
463
	$cron_item['month'] = "*";
464
	$cron_item['wday'] = "*";
465
	$cron_item['who'] = "root";
466
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout";
467

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

    
470
	$cron_item = array();
471
	$cron_item['minute'] = "1";
472
	$cron_item['hour'] = "1";
473
	$cron_item['mday'] = "*";
474
	$cron_item['month'] = "*";
475
	$cron_item['wday'] = "*";
476
	$cron_item['who'] = "root";
477
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.dyndns.update";
478

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

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

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

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

    
501
	$config['cron']['item'][] = $cron_item;
502
}
503

    
504

    
505
function upgrade_026_to_027() {
506
	global $config;
507
}
508

    
509

    
510
function upgrade_027_to_028() {
511
	global $config;
512
}
513

    
514

    
515
function upgrade_028_to_029() {
516
	global $config;
517
	$rule_item = array();
518
	$a_filter = &$config['filter']['rule'];
519
	$rule_item['interface'] = "enc0";
520
	$rule_item['type'] = "pass";
521
	$rule_item['source']['any'] = true;
522
	$rule_item['destination']['any'] = true;
523
	$rule_item['descr'] = gettext("Permit IPsec traffic.");
524
	$rule_item['statetype'] = "keep state";
525
	$a_filter[] = $rule_item;
526
}
527

    
528

    
529
function upgrade_029_to_030() {
530
	global $config;
531
	/* enable the rrd config setting by default */
532
	$config['rrd']['enable'] = true;
533
}
534

    
535

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

    
541

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

    
547

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

    
553

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

    
559

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

    
565

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

    
571

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

    
577

    
578
function upgrade_037_to_038() {
579
	global $config;
580
	/* Insert upgrade code here */
581
}
582

    
583

    
584
function upgrade_038_to_039() {
585
	global $config;
586
	/* Insert upgrade code here */
587
}
588

    
589

    
590
function upgrade_039_to_040() {
591
	global $config, $g;
592
	$config['system']['webgui']['auth_method'] = "session";
593
	$config['system']['webgui']['backing_method'] = "htpasswd";
594

    
595
	if (isset($config['system']['username'])) {
596
		$config['system']['group'] = array();
597
		$config['system']['group'][0]['name'] = "admins";
598
		$config['system']['group'][0]['description'] = gettext("System Administrators");
599
		$config['system']['group'][0]['scope'] = "system";
600
		$config['system']['group'][0]['priv'] = "page-all";
601
		$config['system']['group'][0]['home'] = "index.php";
602
		$config['system']['group'][0]['gid'] = "110";
603

    
604
		$config['system']['user'] = array();
605
		$config['system']['user'][0]['name'] = "{$config['system']['username']}";
606
		$config['system']['user'][0]['descr'] = "System Administrator";
607
		$config['system']['user'][0]['scope'] = "system";
608
		$config['system']['user'][0]['groupname'] = "admins";
609
		$config['system']['user'][0]['password'] = "{$config['system']['password']}";
610
		$config['system']['user'][0]['uid'] = "0";
611
		/* Ensure that we follow what this new "admin" username should be in the session. */
612
		$_SESSION["Username"] = "{$config['system']['username']}";
613

    
614
		$config['system']['user'][0]['priv'] = array();
615
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
616
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
617
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
618
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
619
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
620
		$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).");
621
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
622
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
623
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
624
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
625
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
626
		$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."), $g['product_name']);
627
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
628
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
629
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (this privilege should only be associated with one single user).");
630

    
631
		$config['system']['nextuid'] = "111";
632
		$config['system']['nextgid'] = "111";
633

    
634
		/* wipe previous auth configuration */
635
		unset($config['system']['username']);
636
		if (isset($config['system']['password'])) {
637
			unset($config['system']['password']);
638
		}
639
	}
640
}
641

    
642
function upgrade_040_to_041() {
643
	global $config;
644
	if (!$config['sysctl']) {
645
		$config['sysctl']['item'] = array();
646

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

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

    
655
		$config['sysctl']['item'][2]['tunable'] = "net.inet.ip.random_id";
656
		$config['sysctl']['item'][2]['descr'] =    gettext("Randomize the ID field in IP packets (default is 1: Assign random IP IDs)");
657
		$config['sysctl']['item'][2]['value'] =   "default";
658

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

    
663
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
664
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
665
		$config['sysctl']['item'][4]['value'] =   "default";
666

    
667
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
668
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
669
		$config['sysctl']['item'][5]['value'] =   "default";
670

    
671
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
672
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
673
		$config['sysctl']['item'][6]['value'] =   "default";
674

    
675
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
676
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
677
		$config['sysctl']['item'][7]['value'] =   "default";
678

    
679
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
680
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
681
		$config['sysctl']['item'][8]['value'] =   "default";
682

    
683
		$config['sysctl']['item'][9]['tunable'] = "net.inet.tcp.delayed_ack";
684
		$config['sysctl']['item'][9]['descr'] =    gettext("Do not delay ACK to try and piggyback it onto a data packet");
685
		$config['sysctl']['item'][9]['value'] =   "default";
686

    
687
		$config['sysctl']['item'][10]['tunable'] = "net.inet.udp.maxdgram";
688
		$config['sysctl']['item'][10]['descr'] =    gettext("Maximum outgoing UDP datagram size");
689
		$config['sysctl']['item'][10]['value'] =   "default";
690

    
691
		$config['sysctl']['item'][11]['tunable'] = "net.link.bridge.pfil_onlyip";
692
		$config['sysctl']['item'][11]['descr'] =    gettext("Handling of non-IP packets which are not passed to pfil (see if_bridge(4))");
693
		$config['sysctl']['item'][11]['value'] =   "default";
694

    
695
		$config['sysctl']['item'][12]['tunable'] = "net.link.tap.user_open";
696
		$config['sysctl']['item'][12]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
697
		$config['sysctl']['item'][12]['value'] =   "default";
698

    
699
		$config['sysctl']['item'][13]['tunable'] = "kern.randompid";
700
		$config['sysctl']['item'][13]['descr'] =    gettext("Randomize PID's (see src/sys/kern/kern_fork.c: sysctl_kern_randompid())");
701
		$config['sysctl']['item'][13]['value'] =   "default";
702

    
703
		$config['sysctl']['item'][14]['tunable'] = "net.inet.tcp.inflight.enable";
704
		$config['sysctl']['item'][14]['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. ");
705
		$config['sysctl']['item'][14]['value'] =   "default";
706

    
707
		$config['sysctl']['item'][15]['tunable'] = "net.inet.icmp.icmplim";
708
		$config['sysctl']['item'][15]['descr'] =    gettext("Set ICMP Limits");
709
		$config['sysctl']['item'][15]['value'] =   "default";
710

    
711
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.tso";
712
		$config['sysctl']['item'][16]['descr'] =    gettext("TCP Offload engine");
713
		$config['sysctl']['item'][16]['value'] =   "default";
714

    
715
		$config['sysctl']['item'][17]['tunable'] = "net.inet.ip.portrange.first";
716
		$config['sysctl']['item'][17]['descr'] =    "Set the ephemeral port range starting port";
717
		$config['sysctl']['item'][17]['value'] =   "default";
718

    
719
		$config['sysctl']['item'][18]['tunable'] = "hw.syscons.kbd_reboot";
720
		$config['sysctl']['item'][18]['descr'] =    "Enables ctrl+alt+delete";
721
		$config['sysctl']['item'][18]['value'] =   "default";
722

    
723
		$config['sysctl']['item'][19]['tunable'] = "kern.ipc.maxsockbuf";
724
		$config['sysctl']['item'][19]['descr'] =    "Maximum socket buffer size";
725
		$config['sysctl']['item'][19]['value'] =   "default";
726

    
727
	}
728
}
729

    
730

    
731
function upgrade_041_to_042() {
732
	global $config;
733
	if (isset($config['shaper'])) {
734
		unset($config['shaper']);
735
	}
736
	if (isset($config['ezshaper'])) {
737
		unset($config['ezshaper']);
738
	}
739
}
740

    
741

    
742
function upgrade_042_to_043() {
743
	global $config;
744
	/* migrate old interface gateway to the new gateways config */
745
	$iflist = get_configured_interface_list(true);
746
	$gateways = array();
747
	$i = 0;
748
	foreach ($iflist as $ifname => $interface) {
749
		if (!interface_has_gateway($ifname)) {
750
			continue;
751
		}
752
		$config['gateways']['gateway_item'][$i] = array();
753
		if (is_ipaddr($config['interfaces'][$ifname]['gateway'])) {
754
			$config['gateways']['gateway_item'][$i]['gateway'] = $config['interfaces'][$ifname]['gateway'];
755
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Static Gateway"), $ifname);
756
		} else {
757
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
758
			$config['gateways']['gateway_item'][$i]['descr'] = sprintf(gettext("Interface %s Dynamic Gateway"), $ifname);
759
		}
760
		$config['gateways']['gateway_item'][$i]['interface'] = $ifname;
761
		$config['gateways']['gateway_item'][$i]['name'] = "GW_" . strtoupper($ifname);
762
		/* add default gateway bit for wan on upgrade */
763
		if ($ifname == "wan") {
764
			$config['gateways']['gateway_item'][$i]['defaultgw'] = true;
765
		}
766
		if (is_ipaddr($config['interfaces'][$ifname]['use_rrd_gateway'])) {
767
			$config['gateways']['gateway_item'][$i]['monitor'] = $config['interfaces'][$ifname]['use_rrd_gateway'];
768
			unset($config['interfaces'][$ifname]['use_rrd_gateway']);
769
		}
770
		$config['interfaces'][$ifname]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
771

    
772
		/* Update all filter rules which might reference this gateway */
773
		$j = 0;
774
		foreach ($config['filter']['rule'] as $rule) {
775
			if (is_ipaddr($rule['gateway'])) {
776
				if ($rule['gateway'] == $config['gateways']['gateway_item'][$i]['gateway']) {
777
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
778
				} else if ($rule['gateway'] == $ifname) {
779
					$config['filter']['rule'][$j]['gateway'] = $config['gateways']['gateway_item'][$i]['name'];
780
				}
781
			}
782
			$j++;
783
		}
784

    
785
		/* rename old Quality RRD files in the process */
786
		$rrddbpath = "/var/db/rrd";
787
		$gwname = "GW_" . strtoupper($ifname);
788
		if (is_readable("{$rrddbpath}/{$ifname}-quality.rrd")) {
789
			rename("{$rrddbpath}/{$ifname}-quality.rrd", "{$rrddbpath}/{$gwname}-quality.rrd");
790
		}
791
		$i++;
792
	}
793
}
794

    
795

    
796
function upgrade_043_to_044() {
797
	global $config;
798

    
799
	/* migrate static routes to the new gateways config */
800
	$gateways = return_gateways_array(true);
801
	$i = 0;
802
	if (is_array($config['staticroutes']['route'])) {
803
		$gwmap = array();
804
		foreach ($config['staticroutes']['route'] as $idx => $sroute) {
805
			$found = false;
806
			foreach ($gateways as $gwname => $gw) {
807
				if ($gw['gateway'] == $sroute['gateway']) {
808
					$config['staticroutes']['route'][$idx]['gateway'] = $gwname;
809
					$found = true;
810
					break;
811
				}
812
			}
813
			if ($gwmap[$sroute['gateway']]) {
814
				/* We already added a gateway name for this IP */
815
				$config['staticroutes']['route'][$idx]['gateway'] = "{$gwmap[$sroute['gateway']]}";
816
				$found = true;
817
			}
818

    
819
			if ($found == false) {
820
				$gateway = array();
821
				$gateway['name'] = "SROUTE{$i}";
822
				$gwmap[$sroute['gateway']] = $gateway['name'];
823
				$gateway['gateway'] = $sroute['gateway'];
824
				$gateway['interface'] = $sroute['interface'];
825
				$gateway['descr'] = sprintf(gettext("Upgraded static route for %s"), $sroute['network']);
826
				if (!is_array($config['gateways']['gateway_item'])) {
827
					$config['gateways']['gateway_item'] = array();
828
				}
829
				$config['gateways']['gateway_item'][] = $gateway;
830
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
831
				$i++;
832
			}
833
		}
834
	}
835
}
836

    
837

    
838
function upgrade_044_to_045() {
839
	global $config;
840
	$iflist = get_configured_interface_list(true);
841
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
842
		$i = 0;
843
		foreach ($config['vlans']['vlan'] as $id => $vlan) {
844
			/* Make sure to update the interfaces section with the right name */
845
			$vlan_name = "{$vlan['if']}_vlan{$vlan['tag']}";
846
			foreach ($iflist as $ifname) {
847
				if ($config['interfaces'][$ifname]['if'] == "vlan{$i}") {
848
					$config['interfaces'][$ifname]['if'] = $vlan_name;
849
					continue;
850
				}
851
			}
852
			$config['vlans']['vlan'][$i]['vlanif'] = "{$vlan_name}";
853
			$i++;
854
		}
855
	}
856
}
857

    
858

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

    
943

    
944
function upgrade_046_to_047() {
945
	global $config;
946
	/* Upgrade IPsec from tunnel to phase1/phase2 */
947

    
948
	if (is_array($config['ipsec']['tunnel'])) {
949

    
950
		$a_phase1 = array();
951
		$a_phase2 = array();
952
		$ikeid = 0;
953

    
954
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
955

    
956
			unset($ph1ent);
957
			unset($ph2ent);
958

    
959
			/*
960
				*  attempt to locate an enabled phase1
961
				*  entry that matches the peer gateway
962
				*/
963

    
964
			if (!isset($tunnel['disabled'])) {
965

    
966
				$remote_gateway = $tunnel['remote-gateway'];
967

    
968
				foreach ($a_phase1 as $ph1tmp) {
969
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
970
						$ph1ent = $ph1tmp;
971
						break;
972
					}
973
				}
974
			}
975

    
976
			/* none found, create a new one */
977

    
978
			if (!isset($ph1ent)) {
979

    
980
				/* build new phase1 entry */
981

    
982
				$ph1ent = array();
983

    
984
				$ph1ent['ikeid'] = ++$ikeid;
985

    
986
				if (isset($tunnel['disabled'])) {
987
					$ph1ent['disabled'] = $tunnel['disabled'];
988
				}
989

    
990
				/* convert to the new vip[$vhid] name */
991
				if (preg_match("/^carp/", $tunnel['interface'])) {
992
					$carpid = str_replace("carp", "", $tunnel['interface']);
993
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
994
				}
995
				$ph1ent['interface'] = $tunnel['interface'];
996
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
997
				$ph1ent['descr'] = $tunnel['descr'];
998

    
999
				$ph1ent['mode'] = $tunnel['p1']['mode'];
1000

    
1001
				if (isset($tunnel['p1']['myident']['myaddress'])) {
1002
					$ph1ent['myid_type'] = "myaddress";
1003
				}
1004
				if (isset($tunnel['p1']['myident']['address'])) {
1005
					$ph1ent['myid_type'] = "address";
1006
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
1007
				}
1008
				if (isset($tunnel['p1']['myident']['fqdn'])) {
1009
					$ph1ent['myid_type'] = "fqdn";
1010
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
1011
				}
1012
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
1013
					$ph1ent['myid_type'] = "user_fqdn";
1014
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
1015
				}
1016
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
1017
					$ph1ent['myid_type'] = "asn1dn";
1018
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
1019
				}
1020
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
1021
					$ph1ent['myid_type'] = "dyn_dns";
1022
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
1023
				}
1024

    
1025
				$ph1ent['peerid_type'] = "peeraddress";
1026

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

    
1049
				$ph1ent['encryption-algorithm'] = $ph1alg;
1050
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1051
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1052
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1053
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1054

    
1055
				if (isset($tunnel['p1']['pre-shared-key'])) {
1056
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
1057
				}
1058
				if (isset($tunnel['p1']['cert'])) {
1059
					$ph1ent['cert'] = $tunnel['p1']['cert'];
1060
				}
1061
				if (isset($tunnel['p1']['peercert'])) {
1062
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
1063
				}
1064
				if (isset($tunnel['p1']['private-key'])) {
1065
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1066
				}
1067

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

    
1073
				$a_phase1[] = $ph1ent;
1074
			}
1075

    
1076
			/* build new phase2 entry */
1077

    
1078
			$ph2ent = array();
1079

    
1080
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1081

    
1082
			if (isset($tunnel['disabled'])) {
1083
				$ph1ent['disabled'] = $tunnel['disabled'];
1084
			}
1085

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

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

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

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

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

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

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

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

    
1152
			if (isset($tunnel['pinghost']['pinghost'])) {
1153
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1154
			}
1155

    
1156
			$a_phase2[] = $ph2ent;
1157
		}
1158

    
1159
		unset($config['ipsec']['tunnel']);
1160
		$config['ipsec']['phase1'] = $a_phase1;
1161
		$config['ipsec']['phase2'] = $a_phase2;
1162
	}
1163

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

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

    
1176
		$mobilecfg = $config['ipsec']['mobileclients'];
1177

    
1178
		$ph1ent = array();
1179
		$ph1ent['ikeid'] = ++$ikeid;
1180

    
1181
		if (!isset($mobilecfg['enable'])) {
1182
			$ph1ent['disabled'] = true;
1183
		}
1184

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

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

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

    
1238
		$ph1ent['encryption-algorithm'] = $ph1alg;
1239
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1240
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1241
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1242
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1243

    
1244
		if (isset($mobilecfg['p1']['cert'])) {
1245
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1246
		}
1247
		if (isset($mobilecfg['p1']['peercert'])) {
1248
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1249
		}
1250
		if (isset($mobilecfg['p1']['private-key'])) {
1251
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1252
		}
1253

    
1254
		$ph1ent['nat_traversal'] = "on";
1255
		$ph1ent['dpd_enable'] = 1;
1256
		$ph1ent['dpd_delay'] = 10;
1257
		$ph1ent['dpd_maxfail'] = 5;
1258
		$ph1ent['mobile'] = true;
1259

    
1260
		$ph2ent = array();
1261
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1262
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1263
		$ph2ent['localid'] = array('type' => 'none');
1264
		$ph2ent['remoteid'] = array('type' => 'mobile');
1265
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1266

    
1267
		$aes_count = 0;
1268
		foreach ($mobilecfg['p2']['encryption-algorithm-option'] as $tunalg) {
1269
			$aes_found = false;
1270
			switch ($tunalg) {
1271
				case "des":
1272
					$ph2alg = array('name' => 'des');
1273
					break;
1274
				case "3des":
1275
					$ph2alg = array('name' => '3des');
1276
					break;
1277
				case "blowfish":
1278
					$ph2alg = array('name' => 'blowfish', 'keylen' => 'auto');
1279
					break;
1280
				case "cast128":
1281
					$ph2alg = array('name' => 'cast128');
1282
					break;
1283
				case "rijndael":
1284
				case "rijndael 256":
1285
				case "aes 256":
1286
					$ph2alg = array('name' => 'aes', 'keylen' => 'auto');
1287
					$aes_found = true;
1288
					$aes_count++;
1289
					break;
1290
			}
1291

    
1292
			if (!$aes_found || ($aes_count < 2)) {
1293
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1294
			}
1295
		}
1296
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1297
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1298
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1299
		$ph2ent['mobile'] = true;
1300

    
1301
		$config['ipsec']['phase1'][] = $ph1ent;
1302
		$config['ipsec']['phase2'][] = $ph2ent;
1303
		unset($config['ipsec']['mobileclients']);
1304
	}
1305
}
1306

    
1307

    
1308
function upgrade_047_to_048() {
1309
	global $config;
1310
	if (!empty($config['dyndns'])) {
1311
		$config['dyndnses'] = array();
1312
		$config['dyndnses']['dyndns'] = array();
1313
		if (isset($config['dyndns'][0]['host'])) {
1314
			$tempdyn = array();
1315
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1316
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1317
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1318
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1319
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1320
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1321
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];
1322
			$tempdyn['interface'] = "wan";
1323
			$tempdyn['descr'] = sprintf(gettext("Upgraded Dyndns %s"), $tempdyn['type']);
1324
			$config['dyndnses']['dyndns'][] = $tempdyn;
1325
		}
1326
		unset($config['dyndns']);
1327
	}
1328
	if (!empty($config['dnsupdate'])) {
1329
		$pconfig = $config['dnsupdate'][0];
1330
		if (!$pconfig['ttl']) {
1331
			$pconfig['ttl'] = 60;
1332
		}
1333
		if (!$pconfig['keytype']) {
1334
			$pconfig['keytype'] = "zone";
1335
		}
1336
		$pconfig['interface'] = "wan";
1337
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1338
		unset($config['dnsupdate']);
1339
	}
1340

    
1341
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1342
		$pconfig = array();
1343
		$pconfig['username'] = $config['pppoe'][0]['username'];
1344
		$pconfig['password'] = $config['pppoe'][0]['password'];
1345
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1346
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1347
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1348
		unset($config['pppoe']);
1349
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1350
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1351
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1352
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1353
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1354
	}
1355
	if (is_array($config['pptp'])) {
1356
		$pconfig = array();
1357
		$pconfig['username'] = $config['pptp']['username'];
1358
		$pconfig['password'] = $config['pptp']['password'];
1359
		$pconfig['provider'] = $config['pptp']['provider'];
1360
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1361
		$pconfig['timeout'] = $config['pptp']['timeout'];
1362
		unset($config['pptp']);
1363
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1364
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1365
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1366
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1367
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1368
	}
1369
}
1370

    
1371

    
1372
function upgrade_048_to_049() {
1373
	global $config;
1374
	/* setup new all users group */
1375
	$all = array();
1376
	$all['name'] = "all";
1377
	$all['description'] = gettext("All Users");
1378
	$all['scope'] = "system";
1379
	$all['gid'] = 1998;
1380
	$all['member'] = array();
1381

    
1382
	if (!is_array($config['system']['user'])) {
1383
		$config['system']['user'] = array();
1384
	}
1385
	if (!is_array($config['system']['group'])) {
1386
		$config['system']['group'] = array();
1387
	}
1388

    
1389
	/* work around broken uid assignments */
1390
	$config['system']['nextuid'] = 2000;
1391
	foreach ($config['system']['user'] as & $user) {
1392
		if (isset($user['uid']) && !$user['uid']) {
1393
			continue;
1394
		}
1395
		$user['uid'] = $config['system']['nextuid']++;
1396
	}
1397

    
1398
	/* work around broken gid assignments */
1399
	$config['system']['nextgid'] = 2000;
1400
	foreach ($config['system']['group'] as & $group) {
1401
		if ($group['name'] == $g['admin_group']) {
1402
			$group['gid'] = 1999;
1403
		} else {
1404
			$group['gid'] = $config['system']['nextgid']++;
1405
		}
1406
	}
1407

    
1408
	/* build group membership information */
1409
	foreach ($config['system']['group'] as & $group) {
1410
		$group['member'] = array();
1411
		foreach ($config['system']['user'] as & $user) {
1412
			$groupnames = explode(",", $user['groupname']);
1413
			if (in_array($group['name'], $groupnames)) {
1414
				$group['member'][] = $user['uid'];
1415
			}
1416
		}
1417
	}
1418

    
1419
	/* reset user group information */
1420
	foreach ($config['system']['user'] as & $user) {
1421
		unset($user['groupname']);
1422
		$all['member'][] = $user['uid'];
1423
	}
1424

    
1425
	/* reset group scope information */
1426
	foreach ($config['system']['group'] as & $group) {
1427
		if ($group['name'] != $g['admin_group']) {
1428
			$group['scope'] = "user";
1429
		}
1430
	}
1431

    
1432
	/* insert new all group */
1433
	$groups = Array();
1434
	$groups[] = $all;
1435
	$groups = array_merge($config['system']['group'], $groups);
1436
	$config['system']['group'] = $groups;
1437
}
1438

    
1439

    
1440
function upgrade_049_to_050() {
1441
	global $config;
1442

    
1443
	if (!is_array($config['system']['user'])) {
1444
		$config['system']['user'] = array();
1445
	}
1446
	/* update user privileges */
1447
	foreach ($config['system']['user'] as & $user) {
1448
		$privs = array();
1449
		if (!is_array($user['priv'])) {
1450
			unset($user['priv']);
1451
			continue;
1452
		}
1453
		foreach ($user['priv'] as $priv) {
1454
			switch ($priv['id']) {
1455
				case "hasshell":
1456
					$privs[] = "user-shell-access";
1457
					break;
1458
				case "copyfiles":
1459
					$privs[] = "user-copy-files";
1460
					break;
1461
			}
1462
		}
1463
		$user['priv'] = $privs;
1464
	}
1465

    
1466
	/* update group privileges */
1467
	foreach ($config['system']['group'] as & $group) {
1468
		$privs = array();
1469
		if (!is_array($group['pages'])) {
1470
			unset($group['pages']);
1471
			continue;
1472
		}
1473
		foreach ($group['pages'] as $page) {
1474
			$priv = map_page_privname($page);
1475
			if ($priv) {
1476
				$privs[] = $priv;
1477
			}
1478
		}
1479
		unset($group['pages']);
1480
		$group['priv'] = $privs;
1481
	}
1482

    
1483
	/* sync all local account information */
1484
	local_sync_accounts();
1485
}
1486

    
1487

    
1488
function upgrade_050_to_051() {
1489
	global $config;
1490
	$pconfig = array();
1491
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1492
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1493
	$pconfig['value'] = "1";
1494
	$config['sysctl']['item'][] = $pconfig;
1495
	$pconfig = array();
1496
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1497
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1498
	$pconfig['value'] = "0";
1499
	$config['sysctl']['item'][] = $pconfig;
1500

    
1501
	if (isset($config['bridge'])) {
1502
		unset($config['bridge']);
1503
	}
1504

    
1505
	$convert_bridges = false;
1506
	foreach ($config['interfaces'] as $intf) {
1507
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1508
			$config['bridges'] = array();
1509
			$config['bridges']['bridged'] = array();
1510
			$convert_bridges = true;
1511
			break;
1512
		}
1513
	}
1514
	if ($convert_bridges == true) {
1515
		$i = 0;
1516
		foreach ($config['interfaces'] as $ifr => &$intf) {
1517
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1518
				$nbridge = array();
1519
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1520
				$nbridge['descr'] = sprintf(gettext("Converted bridged %s"), $ifr);
1521
				$nbridge['bridgeif'] = "bridge{$i}";
1522
				$config['bridges']['bridged'][] = $nbridge;
1523
				unset($intf['bridge']);
1524
				$i++;
1525
			}
1526
		}
1527
	}
1528
}
1529

    
1530

    
1531
function upgrade_051_to_052() {
1532
	global $config;
1533
	$config['openvpn'] = array();
1534
	if (!is_array($config['ca'])) {
1535
		$config['ca'] = array();
1536
	}
1537
	if (!is_array($config['cert'])) {
1538
		$config['cert'] = array();
1539
	}
1540

    
1541
	$vpnid = 1;
1542

    
1543
	/* openvpn server configurations */
1544
	if (is_array($config['installedpackages']['openvpnserver'])) {
1545
		$config['openvpn']['openvpn-server'] = array();
1546

    
1547
		$index = 1;
1548
		foreach ($config['installedpackages']['openvpnserver']['config'] as $server) {
1549

    
1550
			if (!is_array($server)) {
1551
				continue;
1552
			}
1553

    
1554
			if ($server['auth_method'] == "pki") {
1555

    
1556
				/* create ca entry */
1557
				$ca = array();
1558
				$ca['refid'] = uniqid();
1559
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1560
				$ca['crt'] = $server['ca_cert'];
1561
				$config['ca'][] = $ca;
1562

    
1563
				/* create ca reference */
1564
				unset($server['ca_cert']);
1565
				$server['caref'] = $ca['refid'];
1566

    
1567
				/* create a crl entry if needed */
1568
				if (!empty($server['crl'][0])) {
1569
					$crl = array();
1570
					$crl['refid'] = uniqid();
1571
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1572
					$crl['caref'] = $ca['refid'];
1573
					$crl['text'] = $server['crl'][0];
1574
					if (!is_array($config['crl'])) {
1575
						$config['crl'] = array();
1576
					}
1577
					$config['crl'][] = $crl;
1578
					$server['crlref'] = $crl['refid'];
1579
				}
1580
				unset($server['crl']);
1581

    
1582
				/* create cert entry */
1583
				$cert = array();
1584
				$cert['refid'] = uniqid();
1585
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1586
				$cert['crt'] = $server['server_cert'];
1587
				$cert['prv'] = $server['server_key'];
1588
				$config['cert'][] = $cert;
1589

    
1590
				/* create cert reference */
1591
				unset($server['server_cert']);
1592
				unset($server['server_key']);
1593
				$server['certref'] = $cert['refid'];
1594

    
1595
				$index++;
1596
			}
1597

    
1598
			/* determine operational mode */
1599
			if ($server['auth_method'] == 'pki') {
1600
				if ($server['nopool']) {
1601
					$server['mode'] = "p2p_tls";
1602
				} else {
1603
					$server['mode'] = "server_tls";
1604
				}
1605
			} else {
1606
				$server['mode'] = "p2p_shared_key";
1607
			}
1608
			unset($server['auth_method']);
1609

    
1610
			/* modify configuration values */
1611
			$server['dh_length'] = 1024;
1612
			unset($server['dh_params']);
1613
			if (!$server['interface']) {
1614
				$server['interface'] = 'any';
1615
			}
1616
			$server['tunnel_network'] = $server['addresspool'];
1617
			unset($server['addresspool']);
1618
			if (isset($server['use_lzo']) && ($server['use_lzo'] == "on")) {
1619
				$server['compression'] = "on";
1620
				unset($server['use_lzo']);
1621
			}
1622
			if ($server['nopool']) {
1623
				$server['pool_enable'] = false;
1624
			} else {
1625
				$server['pool_enable'] = "yes";
1626
			}
1627
			unset($server['nopool']);
1628
			$server['dns_domain'] = $server['dhcp_domainname'];
1629
			unset($server['dhcp_domainname']);
1630

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

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

    
1647
			if ($server['dhcp_nbtdisable']) {
1648
				$server['netbios_enable'] = false;
1649
			} else {
1650
				$server['netbios_enable'] = "yes";
1651
			}
1652
			unset($server['dhcp_nbtdisable']);
1653
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1654
			unset($server['dhcp_nbttype']);
1655
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1656
			unset($server['dhcp_nbtscope']);
1657

    
1658
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1659
			$d=1;
1660
			foreach ($tmparr as $tmpa) {
1661
				$server["nbdd_server{$d}"] = $tmpa;
1662
				$d++;
1663
			}
1664
			unset($server['dhcp_nbdd']);
1665

    
1666
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1667
			$d=1;
1668
			foreach ($tmparr as $tmpa) {
1669
				$server["wins_server{$d}"] = $tmpa;
1670
				$d++;
1671
			}
1672
			unset($server['dhcp_wins']);
1673

    
1674
			if (!empty($server['disable'])) {
1675
				$server['disable'] = true;
1676
			} else {
1677
				unset($server['disable']);
1678
			}
1679

    
1680
			/* allocate vpnid */
1681
			$server['vpnid'] = $vpnid++;
1682

    
1683
			if (!empty($server['custom_options'])) {
1684
				$cstmopts = array();
1685
				$tmpcstmopts = explode(";", $server['custom_options']);
1686
				$assigned_if = "";
1687
				$tmpstr = "";
1688
				foreach ($tmpcstmopts as $tmpcstmopt) {
1689
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1690
					if (substr($tmpstr, 0, 6) == "devtun") {
1691
						$assigned_if = substr($tmpstr, 3);
1692
						continue;
1693
					} else if (substr($tmpstr, 0, 5) == "local") {
1694
						$localip = substr($tmpstr, 5);
1695
						$server['ipaddr'] = str_replace("\n", "", $localip);
1696
					} else {
1697
						$cstmopts[] = $tmpcstmopt;
1698
					}
1699
				}
1700
				$server['custom_options'] = implode(";", $cstmopts);
1701
				if (!empty($assigned_if)) {
1702
					foreach ($config['interfaces'] as $iface => $cfgif) {
1703
						if ($cfgif['if'] == $assigned_if) {
1704
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1705
							break;
1706
						}
1707
					}
1708
				}
1709
			}
1710

    
1711
			$config['openvpn']['openvpn-server'][] = $server;
1712
		}
1713
		unset($config['installedpackages']['openvpnserver']);
1714
	}
1715

    
1716
	/* openvpn client configurations */
1717
	if (is_array($config['installedpackages']['openvpnclient'])) {
1718
		$config['openvpn']['openvpn-client'] = array();
1719

    
1720
		$index = 1;
1721
		foreach ($config['installedpackages']['openvpnclient']['config'] as $client) {
1722

    
1723
			if (!is_array($client)) {
1724
				continue;
1725
			}
1726

    
1727
			if ($client['auth_method'] == "pki") {
1728

    
1729
				/* create ca entry */
1730
				$ca = array();
1731
				$ca['refid'] = uniqid();
1732
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1733
				$ca['crt'] = $client['ca_cert'];
1734
				$ca['crl'] = $client['crl'];
1735
				$config['ca'][] = $ca;
1736

    
1737
				/* create ca reference */
1738
				unset($client['ca_cert']);
1739
				unset($client['crl']);
1740
				$client['caref'] = $ca['refid'];
1741

    
1742
				/* create cert entry */
1743
				$cert = array();
1744
				$cert['refid'] = uniqid();
1745
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1746
				$cert['crt'] = $client['client_cert'];
1747
				$cert['prv'] = $client['client_key'];
1748
				$config['cert'][] = $cert;
1749

    
1750
				/* create cert reference */
1751
				unset($client['client_cert']);
1752
				unset($client['client_key']);
1753
				$client['certref'] = $cert['refid'];
1754

    
1755
				$index++;
1756
			}
1757

    
1758
			/* determine operational mode */
1759
			if ($client['auth_method'] == 'pki') {
1760
				$client['mode'] = "p2p_tls";
1761
			} else {
1762
				$client['mode'] = "p2p_shared_key";
1763
			}
1764
			unset($client['auth_method']);
1765

    
1766
			/* modify configuration values */
1767
			if (!$client['interface']) {
1768
				$client['interface'] = 'wan';
1769
			}
1770
			$client['tunnel_network'] = $client['interface_ip'];
1771
			unset($client['interface_ip']);
1772
			$client['server_addr'] = $client['serveraddr'];
1773
			unset($client['serveraddr']);
1774
			$client['server_port'] = $client['serverport'];
1775
			unset($client['serverport']);
1776
			$client['proxy_addr'] = $client['poxy_hostname'];
1777
			unset($client['proxy_addr']);
1778
			if (isset($client['use_lzo']) && ($client['use_lzo'] == "on")) {
1779
				$client['compression'] = "on";
1780
				unset($client['use_lzo']);
1781
			}
1782
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1783
			unset($client['infiniteresolvretry']);
1784

    
1785
			/* allocate vpnid */
1786
			$client['vpnid'] = $vpnid++;
1787

    
1788
			if (!empty($client['custom_options'])) {
1789
				$cstmopts = array();
1790
				$tmpcstmopts = explode(";", $client['custom_options']);
1791
				$assigned_if = "";
1792
				$tmpstr = "";
1793
				foreach ($tmpcstmopts as $tmpcstmopt) {
1794
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1795
					if (substr($tmpstr, 0, 6) == "devtun") {
1796
						$assigned_if = substr($tmpstr, 3);
1797
						continue;
1798
					} else if (substr($tmpstr, 0, 5) == "local") {
1799
						$localip = substr($tmpstr, 5);
1800
						$client['ipaddr'] = str_replace("\n", "", $localip);
1801
					} else {
1802
						$cstmopts[] = $tmpcstmopt;
1803
					}
1804
				}
1805
				$client['custom_options'] = implode(";", $cstmopts);
1806
				if (!empty($assigned_if)) {
1807
					foreach ($config['interfaces'] as $iface => $cfgif) {
1808
						if ($cfgif['if'] == $assigned_if) {
1809
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1810
							break;
1811
						}
1812
					}
1813
				}
1814
			}
1815

    
1816
			if (!empty($client['disable'])) {
1817
				$client['disable'] = true;
1818
			} else {
1819
				unset($client['disable']);
1820
			}
1821

    
1822
			$config['openvpn']['openvpn-client'][] = $client;
1823
		}
1824

    
1825
		unset($config['installedpackages']['openvpnclient']);
1826
	}
1827

    
1828
	/* openvpn client specific configurations */
1829
	if (is_array($config['installedpackages']['openvpncsc'])) {
1830
		$config['openvpn']['openvpn-csc'] = array();
1831

    
1832
		foreach ($config['installedpackages']['openvpncsc']['config'] as $csc) {
1833

    
1834
			if (!is_array($csc)) {
1835
				continue;
1836
			}
1837

    
1838
			/* modify configuration values */
1839
			$csc['common_name'] = $csc['commonname'];
1840
			unset($csc['commonname']);
1841
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1842
			unset($csc['ifconfig_push']);
1843
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1844
			unset($csc['dhcp_domainname']);
1845

    
1846
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1847
			$d=1;
1848
			foreach ($tmparr as $tmpa) {
1849
				$csc["dns_server{$d}"] = $tmpa;
1850
				$d++;
1851
			}
1852
			unset($csc['dhcp_dns']);
1853

    
1854
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1855
			$d=1;
1856
			foreach ($tmparr as $tmpa) {
1857
				$csc["ntp_server{$d}"] = $tmpa;
1858
				$d++;
1859
			}
1860
			unset($csc['dhcp_ntp']);
1861

    
1862
			if ($csc['dhcp_nbtdisable']) {
1863
				$csc['netbios_enable'] = false;
1864
			} else {
1865
				$csc['netbios_enable'] = "yes";
1866
			}
1867
			unset($csc['dhcp_nbtdisable']);
1868
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1869
			unset($csc['dhcp_nbttype']);
1870
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1871
			unset($csc['dhcp_nbtscope']);
1872

    
1873
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1874
			$d=1;
1875
			foreach ($tmparr as $tmpa) {
1876
				$csc["nbdd_server{$d}"] = $tmpa;
1877
				$d++;
1878
			}
1879
			unset($csc['dhcp_nbdd']);
1880

    
1881
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1882
			$d=1;
1883
			foreach ($tmparr as $tmpa) {
1884
				$csc["wins_server{$d}"] = $tmpa;
1885
				$d++;
1886
			}
1887
			unset($csc['dhcp_wins']);
1888

    
1889
			if (!empty($csc['disable'])) {
1890
				$csc['disable'] = true;
1891
			} else {
1892
				unset($csc['disable']);
1893
			}
1894

    
1895
			$config['openvpn']['openvpn-csc'][] = $csc;
1896
		}
1897

    
1898
		unset($config['installedpackages']['openvpncsc']);
1899
	}
1900

    
1901
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1902
	    count($config['openvpn']['openvpn-client']) > 0) {
1903
		$ovpnrule = array();
1904
		$ovpnrule['type'] = "pass";
1905
		$ovpnrule['interface'] = "openvpn";
1906
		$ovpnrule['statetype'] = "keep state";
1907
		$ovpnrule['source'] = array();
1908
		$ovpnrule['destination'] = array();
1909
		$ovpnrule['source']['any'] = true;
1910
		$ovpnrule['destination']['any'] = true;
1911
		$ovpnrule['descr'] = gettext("Auto added OpenVPN rule from config upgrade.");
1912
		$config['filter']['rule'][] = $ovpnrule;
1913
	}
1914

    
1915
	/*
1916
		* FIXME: hack to keep things working with no installedpackages
1917
		* or carp array in the configuration data.
1918
		*/
1919
	if (!is_array($config['installedpackages'])) {
1920
		$config['installedpackages'] = array();
1921
	}
1922
	if (!is_array($config['installedpackages']['carp'])) {
1923
		$config['installedpackages']['carp'] = array();
1924
	}
1925

    
1926
}
1927

    
1928

    
1929
function upgrade_052_to_053() {
1930
	global $config;
1931
	if (!is_array($config['ca'])) {
1932
		$config['ca'] = array();
1933
	}
1934
	if (!is_array($config['cert'])) {
1935
		$config['cert'] = array();
1936
	}
1937

    
1938
	/* migrate advanced admin page webui ssl to certificate manager */
1939
	if ($config['system']['webgui']['certificate'] &&
1940
	    $config['system']['webgui']['private-key']) {
1941

    
1942
		/* create cert entry */
1943
		$cert = array();
1944
		$cert['refid'] = uniqid();
1945
		$cert['descr'] = "webConfigurator SSL Certificate";
1946
		$cert['crt'] = $config['system']['webgui']['certificate'];
1947
		$cert['prv'] = $config['system']['webgui']['private-key'];
1948
		$config['cert'][] = $cert;
1949

    
1950
		/* create cert reference */
1951
		unset($config['system']['webgui']['certificate']);
1952
		unset($config['system']['webgui']['private-key']);
1953
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1954
	}
1955

    
1956
	/* migrate advanced admin page ssh keys to user manager */
1957
	if ($config['system']['ssh']['authorizedkeys']) {
1958
		$admin_user =& getUserEntryByUID(0);
1959
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1960
		unset($config['system']['ssh']['authorizedkeys']);
1961
	}
1962
}
1963

    
1964

    
1965
function upgrade_053_to_054() {
1966
	global $config;
1967
	if (is_array($config['load_balancer']['lbpool'])) {
1968
		$lbpool_arr = $config['load_balancer']['lbpool'];
1969
		$lbpool_srv_arr = array();
1970
		$gateway_group_arr = array();
1971
		$gateways = return_gateways_array();
1972
		$group_name_changes = array();
1973
		if (!is_array($config['gateways']['gateway_item'])) {
1974
			$config['gateways']['gateway_item'] = array();
1975
		}
1976

    
1977
		$a_gateways =& $config['gateways']['gateway_item'];
1978
		foreach ($lbpool_arr as $lbpool) {
1979
			if ($lbpool['type'] == "gateway") {
1980
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
1981
				$group_name = preg_replace("/[^A-Za-z0-9]/", "", $lbpool['name']);
1982
				// If we made and changes, check for collisions and note the change.
1983
				if ($group_name != $lbpool['name']) {
1984
					// Make sure the name isn't already in use.
1985
					foreach ($gateway_group_arr as $gwg) {
1986
						// If the name is in use, add some random bits to avoid collision.
1987
						if ($gwg['name'] == $group_name) {
1988
							$group_name .= uniqid();
1989
						}
1990
					}
1991
					$group_name_changes[$lbpool['name']] = $group_name;
1992
				}
1993
				$gateway_group['name'] = $group_name;
1994
				$gateway_group['descr'] = $lbpool['descr'];
1995
				$gateway_group['trigger'] = "down";
1996
				$gateway_group['item'] = array();
1997
				$i = 0;
1998
				foreach ($lbpool['servers'] as $member) {
1999
					$split = explode("|", $member);
2000
					$interface = $split[0];
2001
					$monitor = $split[1];
2002
					/* on static upgraded configuration we automatically prepend GW_ */
2003
					$static_name = "GW_" . strtoupper($interface);
2004
					if (is_ipaddr($monitor)) {
2005
						foreach ($a_gateways as & $gw) {
2006
							if ($gw['name'] == $static_name) {
2007
								$gw['monitor'] = $monitor;
2008
							}
2009
						}
2010
					}
2011

    
2012
					/* on failover increment tier. Else always assign 1 */
2013
					if ($lbpool['behaviour'] == "failover") {
2014
						$i++;
2015
					} else {
2016
						$i = 1;
2017
					}
2018
					$gateway_group['item'][] = "$static_name|$i";
2019
				}
2020
				$gateway_group_arr[] = $gateway_group;
2021
			} else {
2022
				$lbpool_srv_arr[] = $lbpool;
2023
			}
2024
		}
2025
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2026
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2027
	}
2028
	// Unset lbpool if we no longer have any server pools
2029
	if (count($lbpool_srv_arr) == 0) {
2030
		if (empty($config['load_balancer'])) {
2031
			unset($config['load_balancer']);
2032
		} else {
2033
			if (isset($config['load_balancer']['lbpool'])) {
2034
				unset($config['load_balancer']['lbpool']);
2035
			}
2036
		}
2037
	} else {
2038
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
2039
	}
2040
	// Only set the gateway group array if we converted any
2041
	if (count($gateway_group_arr) != 0) {
2042
		$config['gateways']['gateway_group'] = $gateway_group_arr;
2043
		// Update any rules that had a gateway change, if any.
2044
		if (count($group_name_changes) > 0) {
2045
			foreach ($config['filter']['rule'] as & $rule) {
2046
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes)) {
2047
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
2048
				}
2049
			}
2050
		}
2051
	}
2052
}
2053

    
2054

    
2055
function upgrade_054_to_055() {
2056
	global $config;
2057
	global $g;
2058

    
2059
	/* RRD files changed for quality, traffic and packets graphs */
2060
	//ini_set("max_execution_time", "1800");
2061
	/* convert traffic RRD file */
2062
	global $parsedcfg, $listtags;
2063
	$listtags = array("ds", "v", "rra", "row");
2064

    
2065
	$rrddbpath = "/var/db/rrd/";
2066
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2067
	if (isset($config['system']['use_mfs_tmpvar'])) {
2068
		/* restore the databases, if we have one */
2069
		if (restore_rrd()) {
2070
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2071
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2072
		}
2073
	}
2074

    
2075
	$rrdinterval = 60;
2076
	$valid = $rrdinterval * 2;
2077

    
2078
	/* Asume GigE for now */
2079
	$downstream = 125000000;
2080
	$upstream = 125000000;
2081

    
2082
	/* build a list of quality databases */
2083
	/* roundtrip has become delay */
2084
	function divide_delay($delayval) {
2085
		$delayval = floatval($delayval);
2086
		$delayval = ($delayval / 1000);
2087
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
2088
		return $delayval;
2089
	}
2090
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
2091
	$databases = array();
2092
	if (!file_exists($rrddbpath)) {
2093
		@mkdir($rrddbpath);
2094
	}
2095
	chdir($rrddbpath);
2096
	$databases = glob("*-quality.rrd");
2097
	rsort($databases);
2098
	foreach ($databases as $database) {
2099
		$xmldump = "{$database}.old.xml";
2100
		$xmldumpnew = "{$database}.new.xml";
2101

    
2102
		if (platform_booting()) {
2103
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2104
		}
2105
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2106

    
2107
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2108
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2109
		$rrdold = $rrdold['rrd'];
2110

    
2111
		$i = 0;
2112
		foreach ($rrdold['rra'] as $rra) {
2113
			$l = 0;
2114
			foreach ($rra['database']['row'] as $row) {
2115
				$vnew = divide_delay($row['v'][1]);
2116
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2117
				$l++;
2118
			}
2119
			$i++;
2120
		}
2121

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

    
2125
		unset($rrdold);
2126
		@unlink("{$g['tmp_path']}/{$xmldump}");
2127
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2128
	}
2129

    
2130
	/* build a list of traffic and packets databases */
2131
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2132
	rsort($databases);
2133
	foreach ($databases as $database) {
2134
		$databasetmp = "{$database}.tmp";
2135
		$xmldump = "{$database}.old.xml";
2136
		$xmldumptmp = "{$database}.tmp.xml";
2137
		$xmldumpnew = "{$database}.new.xml";
2138

    
2139
		if (platform_booting()) {
2140
			echo "Migrate RRD database {$database} to new format \n";
2141
		}
2142
		/* rename DS source */
2143
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
2144
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
2145

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

    
2149
		/* create new rrd database file */
2150
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2151
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2152
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2153
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2154
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2155
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2156
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2157
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2158
		$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
2159

    
2160
		create_new_rrd("$rrdcreate");
2161
		/* create temporary xml from new RRD */
2162
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2163

    
2164
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2165
		$rrdold = $rrdold['rrd'];
2166

    
2167
		$rrdnew = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldumptmp}"), 1, "tag");
2168
		$rrdnew = $rrdnew['rrd'];
2169

    
2170
		/* remove any MAX RRA's. Not needed for traffic. */
2171
		$i = 0;
2172
		foreach ($rrdold['rra'] as $rra) {
2173
			if (trim($rra['cf']) == "MAX") {
2174
				unset($rrdold['rra'][$i]);
2175
			}
2176
			$i++;
2177
		}
2178

    
2179
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", dump_xml_config_raw(migrate_rrd_format($rrdold, $rrdnew), "rrd"));
2180
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2181
		/* we now have the rrd with the new fields, adjust the size now. */
2182
		/* RRA 2 is 60 minutes, RRA 3 is 720 minutes */
2183
		mwexec("/bin/sync");
2184
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 2 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2185
		mwexec("/bin/sync");
2186
		mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
2187
		unset($rrdxmlarray);
2188
		@unlink("{$g['tmp_path']}/{$xmldump}");
2189
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2190
	}
2191
	if (!platform_booting()) {
2192
		enable_rrd_graphing();
2193
	}
2194
	/* Let's save the RRD graphs after we run enable RRD graphing */
2195
	/* The function will restore the rrd.tgz so we will save it after */
2196
	exec("cd /; LANG=C RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2197
	unlink_if_exists("{$g['vardb_path']}/rrd/*.xml");
2198
	if (platform_booting()) {
2199
		echo "Updating configuration...";
2200
	}
2201
}
2202

    
2203

    
2204
function upgrade_055_to_056() {
2205
	global $config;
2206

    
2207
	if (!is_array($config['ca'])) {
2208
		$config['ca'] = array();
2209
	}
2210
	if (!is_array($config['cert'])) {
2211
		$config['cert'] = array();
2212
	}
2213

    
2214
	/* migrate ipsec ca's to cert manager */
2215
	if (is_array($config['ipsec']['cacert'])) {
2216
		foreach ($config['ipsec']['cacert'] as & $cacert) {
2217
			$ca = array();
2218
			$ca['refid'] = uniqid();
2219
			if (is_array($cacert['cert'])) {
2220
				$ca['crt'] = $cacert['cert'][0];
2221
			} else {
2222
				$ca['crt'] = $cacert['cert'];
2223
			}
2224
			$ca['descr'] = $cacert['ident'];
2225
			$config['ca'][] = $ca;
2226
		}
2227
		unset($config['ipsec']['cacert']);
2228
	}
2229

    
2230
	/* migrate phase1 certificates to cert manager */
2231
	if (is_array($config['ipsec']['phase1'])) {
2232
		foreach ($config['ipsec']['phase1'] as & $ph1ent) {
2233
			$cert = array();
2234
			$cert['refid'] = uniqid();
2235
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2236
			if (is_array($ph1ent['cert'])) {
2237
				$cert['crt'] = $ph1ent['cert'][0];
2238
			} else {
2239
				$cert['crt'] = $ph1ent['cert'];
2240
			}
2241
			$cert['prv'] = $ph1ent['private-key'];
2242
			$config['cert'][] = $cert;
2243
			$ph1ent['certref'] = $cert['refid'];
2244
			if ($ph1ent['cert']) {
2245
				unset($ph1ent['cert']);
2246
			}
2247
			if ($ph1ent['private-key']) {
2248
				unset($ph1ent['private-key']);
2249
			}
2250
			if ($ph1ent['peercert']) {
2251
				unset($ph1ent['peercert']);
2252
			}
2253
		}
2254
	}
2255
}
2256

    
2257

    
2258
function upgrade_056_to_057() {
2259
	global $config;
2260

    
2261
	if (!is_array($config['system']['user'])) {
2262
		$config['system']['user'] = array();
2263
	}
2264
	/* migrate captivate portal to user manager */
2265
	if (is_array($config['captiveportal']['user'])) {
2266
		foreach ($config['captiveportal']['user'] as $user) {
2267
			// avoid user conflicts
2268
			$found = false;
2269
			foreach ($config['system']['user'] as $userent) {
2270
				if ($userent['name'] == $user['name']) {
2271
					$found = true;
2272
					break;
2273
				}
2274
			}
2275
			if ($found) {
2276
				continue;
2277
			}
2278
			$user['scope'] = "user";
2279
			if (isset($user['expirationdate'])) {
2280
				$user['expires'] = $user['expirationdate'];
2281
				unset($user['expirationdate']);
2282
			}
2283
			if (isset($user['password'])) {
2284
				$user['md5-hash'] = $user['password'];
2285
				unset($user['password']);
2286
			}
2287
			$user['uid'] = $config['system']['nextuid']++;
2288
			$config['system']['user'][] = $user;
2289
		}
2290
		unset($config['captiveportal']['user']);
2291
	}
2292
}
2293

    
2294
function upgrade_057_to_058() {
2295
	global $config;
2296
	/* set all phase2 entries to tunnel mode */
2297
	if (is_array($config['ipsec']['phase2'])) {
2298
		foreach ($config['ipsec']['phase2'] as & $ph2ent) {
2299
			$ph2ent['mode'] = 'tunnel';
2300
		}
2301
	}
2302
}
2303

    
2304
function upgrade_058_to_059() {
2305
	global $config;
2306

    
2307
	if (is_array($config['schedules']['schedule'])) {
2308
		foreach ($config['schedules']['schedule'] as & $schedl) {
2309
			$schedl['schedlabel'] = uniqid();
2310
		}
2311
	}
2312
}
2313

    
2314
function upgrade_059_to_060() {
2315
	global $config;
2316
	require_once("/etc/inc/certs.inc");
2317
	if (is_array($config['ca'])) {
2318
		/* Locate issuer for all CAs */
2319
		foreach ($config['ca'] as & $ca) {
2320
			$subject = cert_get_subject($ca['crt']);
2321
			$issuer = cert_get_issuer($ca['crt']);
2322
			if ($issuer <> $subject) {
2323
				$issuer_crt =& lookup_ca_by_subject($issuer);
2324
				if ($issuer_crt) {
2325
					$ca['caref'] = $issuer_crt['refid'];
2326
				}
2327
			}
2328
		}
2329

    
2330
		/* Locate issuer for all certificates */
2331
		if (is_array($config['cert'])) {
2332
			foreach ($config['cert'] as & $cert) {
2333
				$subject = cert_get_subject($cert['crt']);
2334
				$issuer = cert_get_issuer($cert['crt']);
2335
				if ($issuer <> $subject) {
2336
					$issuer_crt =& lookup_ca_by_subject($issuer);
2337
					if ($issuer_crt) {
2338
						$cert['caref'] = $issuer_crt['refid'];
2339
					}
2340
				}
2341
			}
2342
		}
2343
	}
2344
}
2345

    
2346
function upgrade_060_to_061() {
2347
	global $config;
2348

    
2349
	if (is_array($config['interfaces']['wan'])) {
2350
		$config['interfaces']['wan']['enable'] = true;
2351
	}
2352
	if (is_array($config['interfaces']['lan'])) {
2353
		$config['interfaces']['lan']['enable'] = true;
2354
	}
2355

    
2356
	/* On 1.2.3 the "mtu" field adjusted MSS.
2357
	   On 2.x the "mtu" field is actually the MTU. Rename accordingly.
2358
	   See redmine ticket #1886
2359
	*/
2360
	foreach ($config['interfaces'] as $ifr => &$intf) {
2361
		if (isset($intf['mtu']) && is_numeric($intf['mtu'])) {
2362
			$intf['mss'] = $intf['mtu'];
2363
			unset($intf['mtu']);
2364
		}
2365
	}
2366
}
2367

    
2368
function upgrade_061_to_062() {
2369
	global $config;
2370

    
2371
	/* Convert NAT port forwarding rules */
2372
	if (is_array($config['nat']['rule'])) {
2373
		$a_nat = &$config['nat']['rule'];
2374

    
2375
		foreach ($a_nat as &$natent) {
2376
			$natent['disabled'] = false;
2377
			$natent['nordr']    = false;
2378

    
2379
			$natent['source'] = array(
2380
				"not"     => false,
2381
				"any"     => true,
2382
				"port"    => ""
2383
			);
2384

    
2385
			$natent['destination'] = array(
2386
				"not"     => false,
2387
				"address" => $natent['external-address'],
2388
				"port"    => $natent['external-port']
2389
			);
2390

    
2391
			if (empty($natent['destination']['address'])) {
2392
				unset($natent['destination']['address']);
2393
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2394
			} else if ($natent['destination']['address'] == 'any') {
2395
				unset($natent['destination']['address']);
2396
				$natent['destination']['any'] = true;
2397
			}
2398

    
2399
			unset($natent['external-address']);
2400
			unset($natent['external-port']);
2401
		}
2402

    
2403
		unset($natent);
2404
	}
2405
}
2406

    
2407
function upgrade_062_to_063() {
2408
	/* Upgrade legacy Themes to the new pfsense_ng */
2409
	// Not supported in 2.3+
2410

    
2411
}
2412

    
2413
function upgrade_063_to_064() {
2414
	global $config;
2415
	$j = 0;
2416
	$ifcfg = &$config['interfaces'];
2417

    
2418
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
2419
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2420
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2421
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2422
			$j++;
2423
			if (isset($ppp['port'])) {
2424
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2425
				unset($config['ppps']['ppp'][$pppid]['port']);
2426
			}
2427
			if (!isset($ppp['type'])) {
2428
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2429
			}
2430
			if (isset($ppp['defaultgw'])) {
2431
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2432
			}
2433
		}
2434
	}
2435

    
2436
	if (!is_array($config['ppps']['ppp'])) {
2437
		$config['ppps']['ppp'] = array();
2438
	}
2439
	$a_ppps = &$config['ppps']['ppp'];
2440

    
2441
	foreach ($ifcfg as $ifname => $ifinfo) {
2442
		$ppp = array();
2443
		// For pppoe conversion
2444
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp") {
2445
			if (isset($ifinfo['ptpid'])) {
2446
				continue;
2447
			}
2448
			$ppp['ptpid'] = $j;
2449
			$ppp['type'] = $ifinfo['ipaddr'];
2450
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2451
			$ppp['ports'] = $ifinfo['if'];
2452
			if ($ifinfo['ipaddr'] == "pppoe") {
2453
				$ppp['username'] = $ifinfo['pppoe_username'];
2454
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2455
			}
2456
			if ($ifinfo['ipaddr'] == "pptp") {
2457
				$ppp['username'] = $ifinfo['pptp_username'];
2458
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2459
			}
2460

    
2461
			if (isset($ifinfo['provider'])) {
2462
				$ppp['provider'] = $ifinfo['provider'];
2463
			}
2464
			if (isset($ifinfo['ondemand'])) {
2465
				$ppp['ondemand'] = true;
2466
			}
2467
			if (isset($ifinfo['timeout'])) {
2468
				$ppp['idletimeout'] = $ifinfo['timeout'];
2469
			}
2470
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])) {
2471
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2472
				if (is_array($config['cron']['item'])) {
2473
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2474
						$item = $config['cron']['item'][$i];
2475
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false) {
2476
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2477
						}
2478
					}
2479
				}
2480
			}
2481
			if (isset($ifinfo['local'])) {
2482
				$ppp['localip'] = $ifinfo['local'];
2483
			}
2484
			if (isset($ifinfo['subnet'])) {
2485
				$ppp['subnet'] = $ifinfo['subnet'];
2486
			}
2487
			if (isset($ifinfo['remote'])) {
2488
				$ppp['gateway'] = $ifinfo['remote'];
2489
			}
2490

    
2491
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2492
			$j++;
2493

    
2494
			unset($ifcfg[$ifname]['pppoe_username']);
2495
			unset($ifcfg[$ifname]['pppoe_password']);
2496
			unset($ifcfg[$ifname]['provider']);
2497
			unset($ifcfg[$ifname]['ondemand']);
2498
			unset($ifcfg[$ifname]['timeout']);
2499
			unset($ifcfg[$ifname]['pppoe_reset']);
2500
			unset($ifcfg[$ifname]['pppoe_preset']);
2501
			unset($ifcfg[$ifname]['pppoe']);
2502
			unset($ifcfg[$ifname]['pptp_username']);
2503
			unset($ifcfg[$ifname]['pptp_password']);
2504
			unset($ifcfg[$ifname]['local']);
2505
			unset($ifcfg[$ifname]['subnet']);
2506
			unset($ifcfg[$ifname]['remote']);
2507

    
2508
			$a_ppps[] = $ppp;
2509

    
2510
		}
2511
	}
2512
}
2513

    
2514
function upgrade_064_to_065() {
2515
	/* Disable TSO and LRO in upgraded configs */
2516
	global $config;
2517
	$config['system']['disablesegmentationoffloading'] = true;
2518
	$config['system']['disablelargereceiveoffloading'] = true;
2519
}
2520

    
2521
function upgrade_065_to_066() {
2522
	global $config;
2523

    
2524
	$dhcrelaycfg =& $config['dhcrelay'];
2525

    
2526
	if (is_array($dhcrelaycfg)) {
2527
		$dhcrelayifs = array();
2528
		$foundifs = false;
2529
		/* DHCPRelay enabled on any interfaces? */
2530
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2531
			if (isset($dhcrelayifconf['enable'])) {
2532
				$dhcrelayifs[] = $dhcrelayif;
2533
				unset($dhcrelaycfg['dhcrelayif']);
2534
				$foundifs = true;
2535
			}
2536
		}
2537
		if ($foundifs == true) {
2538
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2539
		}
2540
	}
2541
}
2542

    
2543
function upgrade_066_to_067() {
2544
	global $config;
2545
	if (isset($config['system']['ca'])) {
2546
		$config['ca'] = $config['system']['ca'];
2547
		unset($config['system']['ca']);
2548
	}
2549
	if (isset($config['system']['cert'])) {
2550
		$config['cert'] = $config['system']['cert'];
2551
		unset($config['system']['cert']);
2552
	}
2553
}
2554

    
2555
function upgrade_067_to_068() {
2556
	global $config;
2557

    
2558
	if (!empty($config['pppoe'])) {
2559
		$config['pppoes'] = array();
2560
		$config['pppoes']['pppoe'] = array();
2561
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2562

    
2563
		if (is_array($config['pppoe']['user'])) {
2564
			$username = array();
2565
			foreach ($config['pppoe']['user'] as $user) {
2566
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2567
				if ($user['ip']) {
2568
					$usr .= ":{$user['ip']}";
2569
				}
2570
				$username[] = $usr;
2571
			}
2572
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2573
		}
2574
		unset($config['pppoe']);
2575
	}
2576
}
2577

    
2578
function upgrade_068_to_069() {
2579
	global $config;
2580
	if (!is_array($config['system']['user'])) {
2581
		return;
2582
	}
2583
	foreach ($config['system']['user'] as & $user) {
2584
		if (!is_array($user['cert'])) {
2585
			continue;
2586
		}
2587
		$rids = array();
2588
		foreach ($user['cert'] as $id => $cert) {
2589
			if (!isset($cert['descr'])) {
2590
				continue;
2591
			}
2592
			$tcert = $cert;
2593
			// Make sure each cert gets a refid
2594
			if (!isset($tcert['refid'])) {
2595
				$tcert['refid'] = uniqid();
2596
			}
2597
			// Keep the cert references for this user
2598
			$rids[] = $tcert['refid'];
2599
			$config['cert'][] = $tcert;
2600
		}
2601
		// Replace user certs with cert references instead.
2602
		if (count($rids) > 0) {
2603
			$user['cert'] = $rids;
2604
		}
2605
	}
2606
}
2607

    
2608
function upgrade_069_to_070() {
2609
	global $config;
2610

    
2611
	/* Convert NAT 1:1 rules */
2612
	if (is_array($config['nat']['onetoone'])) {
2613
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2614
			if ($natent['subnet'] == 32) {
2615
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2616
			} else {
2617
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2618
			}
2619

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

    
2622
			unset($config['nat']['onetoone'][$nidx]['internal']);
2623
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2624
		}
2625

    
2626
		unset($natent);
2627
	}
2628
}
2629

    
2630
function upgrade_070_to_071() {
2631
	global $config;
2632

    
2633
	if (is_array($config['cron']['item'])) {
2634
		foreach ($config['cron']['item'] as $idx => $cronitem) {
2635
			if (stristr($cronitem['command'], "checkreload.sh")) {
2636
				unset($config['cron']['item'][$idx]);
2637
				break;
2638
			}
2639
		}
2640
	}
2641
}
2642

    
2643
function rename_field(& $section, $oldname, $newname) {
2644
	if (is_array($section)) {
2645
		foreach ($section as & $item) {
2646
			if (is_array($item) && !empty($item[$oldname])) {
2647
				$item[$newname] = $item[$oldname];
2648
			}
2649
			if (is_array($item) && isset($item[$oldname])) {
2650
				unset($item[$oldname]);
2651
			}
2652
		}
2653
	}
2654
}
2655

    
2656
function upgrade_071_to_072() {
2657
	global $config;
2658
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item'])) {
2659
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2660
	}
2661
}
2662

    
2663
function upgrade_072_to_073() {
2664
	global $config;
2665
	if (!is_array($config['load_balancer'])) {
2666
		return;
2667
	}
2668
	if (is_array($config['load_balancer']['monitor_type'])) {
2669
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2670
	}
2671
	if (is_array($config['load_balancer']['lbpool'])) {
2672
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2673
	}
2674
	if (is_array($config['load_balancer']['lbaction'])) {
2675
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2676
	}
2677
	if (is_array($config['load_balancer']['lbprotocol'])) {
2678
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2679
	}
2680
	if (is_array($config['load_balancer']['virtual_server'])) {
2681
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2682
	}
2683
}
2684

    
2685
function upgrade_073_to_074() {
2686
	global $config;
2687
	rename_field($config['system']['user'], 'fullname', 'descr');
2688
}
2689

    
2690
function upgrade_074_to_075() {
2691
	global $config;
2692
	if (is_array($config['ca'])) {
2693
		rename_field($config['ca'], 'name', 'descr');
2694
	}
2695
	if (is_array($config['cert'])) {
2696
		rename_field($config['cert'], 'name', 'descr');
2697
	}
2698
	if (is_array($config['crl'])) {
2699
		rename_field($config['crl'], 'name', 'descr');
2700
	}
2701
}
2702

    
2703
function upgrade_075_to_076() {
2704
	global $config;
2705
	$cron_item = array();
2706
	$cron_item['minute'] = "30";
2707
	$cron_item['hour'] = "12";
2708
	$cron_item['mday'] = "*";
2709
	$cron_item['month'] = "*";
2710
	$cron_item['wday'] = "*";
2711
	$cron_item['who'] = "root";
2712
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2713
	$config['cron']['item'][] = $cron_item;
2714
}
2715

    
2716
function upgrade_076_to_077() {
2717
	global $config;
2718
	foreach ($config['filter']['rule'] as & $rule) {
2719
		if (isset($rule['protocol']) && !empty($rule['protocol'])) {
2720
			$rule['protocol'] = strtolower($rule['protocol']);
2721
		}
2722
	}
2723
}
2724

    
2725
function upgrade_077_to_078() {
2726
	global $config;
2727
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius']) &&
2728
	    !is_array($config['pptpd']['radius']['server'])) {
2729
		$radarr = array();
2730
		$radsvr = array();
2731
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2732
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2733
		$radsvr['port'] = 1812;
2734
		$radsvr['acctport'] = 1813;
2735
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2736
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2737
		if ($radarr['accounting']) {
2738
			$radarr['acct_update'] = $radsvr['ip'];
2739
		}
2740
		$radarr['server'] = $radsvr;
2741
		$config['pptpd']['radius'] = $radarr;
2742
	}
2743
	if (is_array($config['pptpd'])) {
2744
		$config['pptpd']['n_pptp_units'] = empty($config['pptpd']['n_pptp_units']) ? 16 : $config['pptpd']['n_pptp_units'];
2745
	}
2746
}
2747
function upgrade_078_to_079() {
2748
	global $g;
2749
	/* Delete old and unused RRD file */
2750
	unlink_if_exists("{$g['vardb_path']}/rrd/captiveportal-totalusers.rrd");
2751
}
2752

    
2753
function upgrade_079_to_080() {
2754
	global $config;
2755

    
2756
	/* Upgrade config in 1.2.3 specifying a username other than admin for syncing. */
2757
	if (!empty($config['system']['username']) && is_array($config['installedpackages']['carpsettings']) &&
2758
	    is_array($config['installedpackages']['carpsettings']['config'])) {
2759
		$config['installedpackages']['carpsettings']['config'][0]['username'] = $config['system']['username'];
2760
		unset($config['system']['username']);
2761
	}
2762
}
2763

    
2764
function upgrade_080_to_081() {
2765
	global $config;
2766
	global $g;
2767
	/* Welcome to the 2.1 migration path */
2768

    
2769
	/* tag all the existing gateways as being IPv4 */
2770
	$i = 0;
2771
	if (is_array($config['gateways']['gateway_item'])) {
2772
		foreach ($config['gateways']['gateway_item'] as $gw) {
2773
			$config['gateways']['gateway_item'][$i]['ipprotocol'] = "inet";
2774
			$i++;
2775
		}
2776
	}
2777

    
2778
	/* RRD files changed for quality, traffic and packets graphs */
2779
	/* convert traffic RRD file */
2780
	global $parsedcfg, $listtags;
2781
	$listtags = array("ds", "v", "rra", "row");
2782

    
2783
	$rrddbpath = "/var/db/rrd/";
2784
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2785

    
2786
	if (isset($config['system']['use_mfs_tmpvar'])) {
2787
		/* restore the databases, if we have one */
2788
		if (restore_rrd()) {
2789
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2790
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2791
		}
2792
	}
2793

    
2794
	$rrdinterval = 60;
2795
	$valid = $rrdinterval * 2;
2796

    
2797
	/* Asume GigE for now */
2798
	$downstream = 125000000;
2799
	$upstream = 125000000;
2800

    
2801
	/* build a list of traffic and packets databases */
2802
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
2803
	rsort($databases);
2804
	foreach ($databases as $database) {
2805
		$xmldump = "{$database}.old.xml";
2806
		$xmldumpnew = "{$database}.new.xml";
2807

    
2808
		if (platform_booting()) {
2809
			echo "Migrate RRD database {$database} to new format for IPv6.\n";
2810
		}
2811

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

    
2815
		/* search and replace tags to add data sources */
2816
		$ds_search = "<!-- Round Robin Archives -->";
2817
		$ds_arr = array();
2818
		$ds_arr[] = "	<ds>
2819
				<name> inpass6 </name>
2820
				<type> COUNTER </type>
2821
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2822
				<min> 0.0000000000e+00 </min>
2823
				<max> 1.2500000000e+08 </max>
2824

    
2825
				<!-- PDP Status -->
2826
				<last_ds> 0 </last_ds>
2827
				<value> NaN </value>
2828
				<unknown_sec> 3 </unknown_sec>
2829
			</ds>
2830
			";
2831
		$ds_arr[] = "	<ds>
2832
				<name> outpass6 </name>
2833
				<type> COUNTER </type>
2834
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2835
				<min> 0.0000000000e+00 </min>
2836
				<max> 1.2500000000e+08 </max>
2837

    
2838
				<!-- PDP Status -->
2839
				<last_ds> 0 </last_ds>
2840
				<value> NaN </value>
2841
				<unknown_sec> 3 </unknown_sec>
2842
			</ds>
2843
			";
2844
		$ds_arr[] = "	<ds>
2845
				<name> inblock6 </name>
2846
				<type> COUNTER </type>
2847
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2848
				<min> 0.0000000000e+00 </min>
2849
				<max> 1.2500000000e+08 </max>
2850

    
2851
				<!-- PDP Status -->
2852
				<last_ds> 0 </last_ds>
2853
				<value> NaN </value>
2854
				<unknown_sec> 3 </unknown_sec>
2855
			</ds>
2856
			";
2857
		$ds_arr[] = "	<ds>
2858
				<name> outblock6 </name>
2859
				<type> COUNTER </type>
2860
				<minimal_heartbeat> {$valid} </minimal_heartbeat>
2861
				<min> 0.0000000000e+00 </min>
2862
				<max> 1.2500000000e+08 </max>
2863

    
2864
				<!-- PDP Status -->
2865
				<last_ds> 0 </last_ds>
2866
				<value> NaN </value>
2867
				<unknown_sec> 3 </unknown_sec>
2868
			</ds>
2869
			";
2870

    
2871
		$cdp_search = "<\/cdp_prep>";
2872
		$cdp_replace = "</cdp_prep>";
2873
		$cdp_arr = array();
2874
		$cdp_arr[] = "			<ds>
2875
					<primary_value> NaN </primary_value>
2876
					<secondary_value> 0.0000000000e+00 </secondary_value>
2877
					<value> NaN </value>
2878
					<unknown_datapoints> 0 </unknown_datapoints>
2879
					</ds>
2880
		";
2881
		$cdp_arr[] = "			<ds>
2882
					<primary_value> NaN </primary_value>
2883
					<secondary_value> 0.0000000000e+00 </secondary_value>
2884
					<value> NaN </value>
2885
					<unknown_datapoints> 0 </unknown_datapoints>
2886
					</ds>
2887
		";
2888
		$cdp_arr[] = "			<ds>
2889
					<primary_value> NaN </primary_value>
2890
					<secondary_value> 0.0000000000e+00 </secondary_value>
2891
					<value> NaN </value>
2892
					<unknown_datapoints> 0 </unknown_datapoints>
2893
					</ds>
2894
		";
2895
		$cdp_arr[] = "			<ds>
2896
					<primary_value> NaN </primary_value>
2897
					<secondary_value> 0.0000000000e+00 </secondary_value>
2898
					<value> NaN </value>
2899
					<unknown_datapoints> 0 </unknown_datapoints>
2900
					</ds>
2901
		";
2902

    
2903
		$value_search = "<\/row>";
2904
		$value_replace = "</row>";
2905
		$value = "<v> NaN </v>";
2906

    
2907
		$xml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2908
		foreach ($ds_arr as $ds) {
2909
			$xml = preg_replace("/$ds_search/s", "$ds{$ds_search}", $xml);
2910
		}
2911
		foreach ($cdp_arr as $cdp) {
2912
			$xml = preg_replace("/$cdp_search/s", "$cdp{$cdp_replace}", $xml);
2913
		}
2914
		foreach ($ds_arr as $ds) {
2915
			$xml = preg_replace("/$value_search/s", "$value{$value_replace}", $xml);
2916
		}
2917

    
2918
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $xml);
2919
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2920
		unset($xml);
2921
		# Default /tmp tmpfs is ~40mb, do not leave temp files around
2922
		unlink_if_exists("{$g['tmp_path']}/{$xmldump}");
2923
		unlink_if_exists("{$g['tmp_path']}/{$xmldumpnew}");
2924
	}
2925
	if (!platform_booting()) {
2926
		enable_rrd_graphing();
2927
	}
2928
	/* Let's save the RRD graphs after we run enable RRD graphing */
2929
	/* The function will restore the rrd.tgz so we will save it after */
2930
	exec("cd /; LANG=C RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
2931
	if (platform_booting()) {
2932
		echo "Updating configuration...";
2933
	}
2934
	foreach ($config['filter']['rule'] as & $rule) {
2935
		if (isset($rule['protocol']) && !empty($rule['protocol'])) {
2936
			$rule['protocol'] = strtolower($rule['protocol']);
2937
		}
2938
	}
2939
	unset($rule);
2940
}
2941

    
2942
function upgrade_081_to_082() {
2943
	/* don't enable the allow IPv6 toggle */
2944
}
2945

    
2946
function upgrade_082_to_083() {
2947
	global $config;
2948

    
2949
	/* Upgrade captiveportal config */
2950
	if (!empty($config['captiveportal'])) {
2951
		$tmpcp = $config['captiveportal'];
2952
		$config['captiveportal'] = array();
2953
		$config['captiveportal']['cpzone'] = array();
2954
		$config['captiveportal']['cpzone'] = $tmpcp;
2955
		$config['captiveportal']['cpzone']['zoneid'] = 8000;
2956
		$config['captiveportal']['cpzone']['zone'] = "cpzone";
2957
		if ($config['captiveportal']['cpzone']['auth_method'] == "radius") {
2958
			$config['captiveportal']['cpzone']['radius_protocol'] = "PAP";
2959
		}
2960
	}
2961
	if (!empty($config['voucher'])) {
2962
		$tmpcp = $config['voucher'];
2963
		$config['voucher'] = array();
2964
		$config['voucher']['cpzone'] = array();
2965
		$config['voucher']['cpzone'] = $tmpcp;
2966
	}
2967
}
2968

    
2969
function upgrade_083_to_084() {
2970
	global $config;
2971
	if (!isset($config['hasync'])) {
2972
		if (!empty($config['installedpackages']) &&
2973
		    !empty($config['installedpackages']['carpsettings']) &&
2974
		    !empty($config['installedpackages']['carpsettings']['config'])) {
2975
			$config['hasync'] = $config['installedpackages']['carpsettings']['config'][0];
2976
			unset($config['installedpackages']['carpsettings']);
2977
		}
2978
		if (empty($config['installedpackages']['carpsettings']) && isset($config['installedpackages']['carpsettings'])) {
2979
			unset($config['installedpackages']['carpsettings']);
2980
		}
2981
		if (empty($config['installedpackages']) && isset($config['installedpackages'])) {
2982
			unset($config['installedpackages']);
2983
		}
2984
	}
2985
}
2986

    
2987
function upgrade_084_to_085() {
2988
	global $config;
2989

    
2990
	$gateway_group_arr = array();
2991
	$gateways = return_gateways_array();
2992
	$oldnames = array();
2993
	/* setup translation array */
2994
	foreach ($gateways as $name => $gw) {
2995
		if (isset($gw['dynamic'])) {
2996
			$oldname = strtoupper($config['interfaces'][$gw['friendlyiface']]['descr']);
2997
			$oldnames[$oldname] = $name;
2998
		} else {
2999
			$oldnames[$name] = $name;
3000
		}
3001
	}
3002

    
3003
	/* process the old array */
3004
	if (is_array($config['gateways']['gateway_group'])) {
3005
		$group_array_new = array();
3006
		foreach ($config['gateways']['gateway_group'] as $name => $group) {
3007
			if (is_array($group['item'])) {
3008
				$newlist = array();
3009
				foreach ($group['item'] as $entry) {
3010
					$elements = explode("|", $entry);
3011
					if ($oldnames[$elements[0]] <> "") {
3012
						$newlist[] = "{$oldnames[$elements[0]]}|{$elements[1]}";
3013
					} else {
3014
						$newlist[] = "{$elements[0]}|{$elements[1]}";
3015
					}
3016
				}
3017
				$group['item'] = $newlist;
3018
				$group_array_new[$name] = $group;
3019
			}
3020
		}
3021
		$config['gateways']['gateway_group'] = $group_array_new;
3022
	}
3023
	/* rename old Quality RRD files in the process */
3024
	$rrddbpath = "/var/db/rrd";
3025
	foreach ($oldnames as $old => $new) {
3026
		if (is_readable("{$rrddbpath}/{$old}-quality.rrd")) {
3027
			@rename("{$rrddbpath}/{$old}-quality.rrd", "{$rrddbpath}/{$new}-quality.rrd");
3028
		}
3029
	}
3030
	unset($gateways, $oldnames, $gateway_group_arr);
3031
}
3032

    
3033
function upgrade_085_to_086() {
3034
	global $config, $g;
3035

    
3036
	/* XXX: Gross hacks in sight */
3037
	if (is_array($config['virtualip']['vip'])) {
3038
		$vipchg = array();
3039
		foreach ($config['virtualip']['vip'] as $vip) {
3040
			if ($vip['mode'] != "carp") {
3041
				continue;
3042
			}
3043
			$config = array_replace_values_recursive(
3044
				$config,
3045
				'^vip' . $vip['vhid'] . '$',
3046
				"{$vip['interface']}_vip{$vip['vhid']}"
3047
			);
3048
		}
3049
	}
3050
}
3051

    
3052
function upgrade_086_to_087() {
3053
	global $config, $dummynet_pipe_list;
3054

    
3055
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue'])) {
3056
		return;
3057
	}
3058

    
3059
	$dnqueue_number = 1;
3060
	$dnpipe_number = 1;
3061

    
3062
	foreach ($config['dnshaper']['queue'] as $idx => $dnpipe) {
3063
		$config['dnshaper']['queue'][$idx]['number'] = $dnpipe_number;
3064
		$dnpipe_number++;
3065
		if (is_array($dnpipe['queue'])) {
3066
			foreach ($dnpipe['queue'] as $qidx => $dnqueue) {
3067
				$config['dnshaper']['queue'][$idx]['queue'][$qidx]['number'] = $dnqueue_number;
3068
				$dnqueue_number++;
3069
			}
3070
		}
3071
	}
3072

    
3073
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
3074

    
3075
	if (!is_array($config['filter']) || !is_array($config['filter']['rule'])) {
3076
		return;
3077
	}
3078

    
3079
	require_once("shaper.inc");
3080
	read_dummynet_config();
3081

    
3082
	$dn_list = array();
3083
	if (is_array($dummynet_pipe_list)) {
3084
		foreach ($dummynet_pipe_list as $dn) {
3085
			$tmplist =& $dn->get_queue_list();
3086
			foreach ($tmplist as $qname => $link) {
3087
				$dn_list[$link] = $qname;
3088
			}
3089
		}
3090
		unset($dummynet_pipe_list);
3091
	}
3092

    
3093
	foreach ($config['filter']['rule'] as $idx => $rule) {
3094
		if (!empty($rule['dnpipe'])) {
3095
			if (!empty($dn_list[$rule['dnpipe']])) {
3096
				$config['filter']['rule'][$idx]['dnpipe'] = $dn_list[$rule['dnpipe']];
3097
			}
3098
		}
3099
		if (!empty($rule['pdnpipe'])) {
3100
			if (!empty($dn_list[$rule['pdnpipe']])) {
3101
				$config['filter']['rule'][$idx]['pdnpipe'] = $dn_list[$rule['pdnpipe']];
3102
			}
3103
		}
3104
	}
3105
}
3106
function upgrade_087_to_088() {
3107
	global $config;
3108
	if (isset($config['system']['glxsb_enable'])) {
3109
		unset($config['system']['glxsb_enable']);
3110
		$config['system']['crypto_hardware'] = "glxsb";
3111
	}
3112
}
3113

    
3114
function upgrade_088_to_089() {
3115
	global $config;
3116
	if (!is_array($config['ca'])) {
3117
		$config['ca'] = array();
3118
	}
3119
	if (!is_array($config['cert'])) {
3120
		$config['cert'] = array();
3121
	}
3122

    
3123
	/* migrate captive portal ssl to certificate manager */
3124
	if (is_array($config['captiveportal'])) {
3125
		foreach ($config['captiveportal'] as $id => &$setting) {
3126
			if (isset($setting['httpslogin'])) {
3127
				/* create cert entry */
3128
				$cert = array();
3129
				$cert['refid'] = uniqid();
3130
				$cert['descr'] = "Captive Portal Cert - {$setting['zone']}";
3131
				$cert['crt'] = $setting['certificate'];
3132
				$cert['prv'] = $setting['private-key'];
3133

    
3134
				if (!empty($setting['cacertificate'])) {
3135
					/* create ca entry */
3136
					$ca = array();
3137
					$ca['refid'] = uniqid();
3138
					$ca['descr'] = "Captive Portal CA - {$setting['zone']}";
3139
					$ca['crt'] = $setting['cacertificate'];
3140
					$config['ca'][] = $ca;
3141

    
3142
					/* add ca reference to certificate */
3143
					$cert['caref'] = $ca['refid'];
3144
				}
3145

    
3146
				$config['cert'][] = $cert;
3147

    
3148
				/* create cert reference */
3149
				$setting['certref'] = $cert['refid'];
3150

    
3151
				unset($setting['certificate']);
3152
				unset($setting['private-key']);
3153
				unset($setting['cacertificate']);
3154

    
3155
			}
3156
		}
3157
	}
3158
}
3159

    
3160
function upgrade_089_to_090() {
3161
	global $config;
3162
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
3163
		$vs_a = &$config['load_balancer']['virtual_server'];
3164
		for ($i = 0; isset($vs_a[$i]); $i++) {
3165
			if (is_array($vs_a[$i]['pool'])) {
3166
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'][0];
3167
				unset($vs_a[$i]['pool']);
3168
			} elseif (!empty($vs_a[$i]['pool'])) {
3169
				$vs_a[$i]['poolname'] = $vs_a[$i]['pool'];
3170
				unset($vs_a[$i]['pool']);
3171
			}
3172
		}
3173
	}
3174
}
3175

    
3176
function upgrade_090_to_091() {
3177
	global $config;
3178

    
3179
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
3180
		foreach ($config['dnshaper']['queue'] as $idx => $dnqueue) {
3181
			if (!empty($dnqueue['bandwidth'])) {
3182
				$bw = array();
3183
				$bw['bw'] = $dnqueue['bandwidth'];
3184
				$bw['bwscale'] = $dnqueue['bandwidthtype'];
3185
				$bw['bwsched'] = "none";
3186
				$config['dnshaper']['queue'][$idx]['bandwidth'] = array();
3187
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'] = array();
3188
				$config['dnshaper']['queue'][$idx]['bandwidth']['item'][] = $bw;
3189
			}
3190
		}
3191
	}
3192
}
3193

    
3194
function upgrade_091_to_092() {
3195
	global $config;
3196

    
3197
	if (is_array($config['nat']['advancedoutbound']) && is_array($config['nat']['advancedoutbound']['rule'])) {
3198
		$nat_rules = &$config['nat']['advancedoutbound']['rule'];
3199
		for ($i = 0; isset($nat_rules[$i]); $i++) {
3200
			if (empty($nat_rules[$i]['interface'])) {
3201
				$nat_rules[$i]['interface'] = 'wan';
3202
			}
3203
		}
3204
	}
3205
}
3206

    
3207
function upgrade_092_to_093() {
3208
	global $g;
3209

    
3210
	$suffixes = array("concurrent", "loggedin");
3211

    
3212
	foreach ($suffixes as $suffix) {
3213
		if (file_exists("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd")) {
3214
			rename("{$g['vardb_path']}/rrd/captiveportal-{$suffix}.rrd",
3215
				"{$g['vardb_path']}/rrd/captiveportal-cpZone-{$suffix}.rrd");
3216
		}
3217
	}
3218

    
3219
	if (!platform_booting()) {
3220
		enable_rrd_graphing();
3221
	}
3222
}
3223

    
3224
function upgrade_093_to_094() {
3225
	global $config;
3226

    
3227
	if (isset($config['system']['powerd_mode'])) {
3228
		$config['system']['powerd_ac_mode'] = $config['system']['powerd_mode'];
3229
		$config['system']['powerd_battery_mode'] = $config['system']['powerd_mode'];
3230
		unset($config['system']['powerd_mode']);
3231
	}
3232
}
3233

    
3234
function upgrade_094_to_095() {
3235
	global $config;
3236

    
3237
	if (!isset($config['interfaces']) || !is_array($config['interfaces'])) {
3238
		return;
3239
	}
3240

    
3241
	foreach ($config['interfaces'] as $iface => $cfg) {
3242
		if (isset($cfg['ipaddrv6']) && ($cfg['ipaddrv6'] == "track6")) {
3243
			if (!isset($cfg['track6-prefix-id']) || ($cfg['track6-prefix-id'] == "")) {
3244
				$config['interfaces'][$iface]['track6-prefix-id'] = 0;
3245
			}
3246
		}
3247
	}
3248
}
3249

    
3250
function upgrade_095_to_096() {
3251
	global $config, $g;
3252

    
3253
	$names = array("inpass", "outpass", "inblock", "outblock",
3254
		"inpass6", "outpass6", "inblock6", "outblock6");
3255
	$rrddbpath = "/var/db/rrd";
3256
	$rrdtool = "/usr/local/bin/rrdtool";
3257

    
3258
	if (isset($config['system']['use_mfs_tmpvar'])) {
3259
		/* restore the databases, if we have one */
3260
		if (restore_rrd()) {
3261
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
3262
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
3263
		}
3264
	}
3265

    
3266
	/* Assume 2*10GigE for now */
3267
	$stream = 2500000000;
3268

    
3269
	/* build a list of traffic and packets databases */
3270
	$databases = return_dir_as_array($rrddbpath, '/-(traffic|packets)\.rrd$/');
3271
	rsort($databases);
3272
	foreach ($databases as $database) {
3273
		if (platform_booting()) {
3274
			echo "Update RRD database {$database}.\n";
3275
		}
3276

    
3277
		$cmd = "{$rrdtool} tune {$rrddbpath}/{$database}";
3278
		foreach ($names as $name) {
3279
			$cmd .= " -a {$name}:{$stream}";
3280
		}
3281
		mwexec("{$cmd} 2>&1");
3282

    
3283
	}
3284
	if (!platform_booting()) {
3285
		enable_rrd_graphing();
3286
	}
3287
	/* Let's save the RRD graphs after we run enable RRD graphing */
3288
	/* The function will restore the rrd.tgz so we will save it after */
3289
	exec("cd /; LANG=C RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
3290
}
3291

    
3292
function upgrade_096_to_097() {
3293
	global $config, $g;
3294
	/* If the user had disabled default block rule logging before, then bogon/private network logging was already off, so respect their choice. */
3295
	if (isset($config['syslog']['nologdefaultblock'])) {
3296
		$config['syslog']['nologbogons'] = true;
3297
		$config['syslog']['nologprivatenets'] = true;
3298
	}
3299
}
3300

    
3301
function upgrade_097_to_098() {
3302
	// no longer used (used to set kill_states)
3303
	return;
3304
}
3305

    
3306
function upgrade_098_to_099() {
3307
	global $config;
3308

    
3309
	if (empty($config['dhcpd']) || !is_array($config['dhcpd'])) {
3310
		return;
3311
	}
3312

    
3313
	foreach ($config['dhcpd'] as & $dhcpifconf) {
3314
		if (isset($dhcpifconf['next-server'])) {
3315
			$dhcpifconf['nextserver'] = $dhcpifconf['next-server'];
3316
			unset($dhcpifconf['next-server']);
3317
		}
3318
	}
3319
}
3320

    
3321
function upgrade_099_to_100() {
3322
	require_once("/etc/inc/services.inc");
3323
	/* See #7146 for detail on why the extra parameters are needed for the time being. */
3324
	install_cron_job("/usr/bin/nice -n20 newsyslog", false, null, null, null, null, null, null, false);
3325
}
3326

    
3327
function upgrade_100_to_101() {
3328
	global $config, $g;
3329

    
3330
	if (!is_array($config['voucher'])) {
3331
		return;
3332
	}
3333

    
3334
	foreach ($config['voucher'] as $cpzone => $cp) {
3335
		if (!is_array($cp['roll'])) {
3336
			continue;
3337
		}
3338
		foreach ($cp['roll'] as $ridx => $rcfg) {
3339
			if (!empty($rcfg['comment'])) {
3340
				$config['voucher'][$cpzone]['roll'][$ridx]['descr'] = $rcfg['comment'];
3341
			}
3342
		}
3343
	}
3344
}
3345

    
3346
function upgrade_101_to_102() {
3347
	global $config, $g;
3348

    
3349
	if (is_array($config['captiveportal'])) {
3350
		foreach ($config['captiveportal'] as $cpzone => $cp) {
3351
			if (!is_array($cp['passthrumac'])) {
3352
				continue;
3353
			}
3354

    
3355
			foreach ($cp['passthrumac'] as $idx => $passthrumac) {
3356
				$config['captiveportal'][$cpzone]['passthrumac'][$idx]['action'] = 'pass';
3357
			}
3358
		}
3359
	}
3360

    
3361
	/* Convert OpenVPN Compression option to the new style */
3362
	// Nothing to do if there is no OpenVPN tag
3363
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
3364
		if (is_array($config['openvpn']['openvpn-server'])) {
3365
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
3366
				if (!empty($vpn['compression'])) {
3367
					$vpn['compression'] = "adaptive";
3368
				}
3369
			}
3370
		}
3371
		if (is_array($config['openvpn']['openvpn-client'])) {
3372
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
3373
				if (!empty($vpn['compression'])) {
3374
					$vpn['compression'] = "adaptive";
3375
				}
3376
			}
3377
		}
3378
	}
3379
}
3380

    
3381
function upgrade_102_to_103() {
3382
	global $config;
3383

    
3384
	if (isset($config['nat']['advancedoutbound']['enable'])) {
3385
		$config['nat']['advancedoutbound']['mode'] = "advanced";
3386
		unset($config['nat']['advancedoutbound']['enable']);
3387
	} else {
3388
		$config['nat']['advancedoutbound']['mode'] = "automatic";
3389
	}
3390

    
3391
	$config['nat']['outbound'] = $config['nat']['advancedoutbound'];
3392

    
3393
	if (isset($config['nat']['ipsecpassthru'])) {
3394
		unset($config['nat']['ipsecpassthru']);
3395
	}
3396
	if (isset($config['nat']['advancedoutbound'])) {
3397
		unset($config['nat']['advancedoutbound']);
3398
	}
3399
}
3400

    
3401
function upgrade_103_to_104() {
3402
	global $config;
3403

    
3404
	$changed_privs = array(
3405
		"page-diag-system-activity" => "page-diagnostics-system-activity",
3406
		"page-interfacess-groups" => "page-interfaces-groups",
3407
		"page-interfacess-lagg" => "page-interfaces-lagg",
3408
		"page-interfacess-qinq" => "page-interfaces-qinq"
3409
	);
3410

    
3411
	/* update user privileges */
3412
	foreach ($config['system']['user'] as & $user) {
3413
		if (!is_array($user['priv'])) {
3414
			continue;
3415
		}
3416
		foreach ($user['priv'] as & $priv) {
3417
			if (array_key_exists($priv, $changed_privs)) {
3418
				$priv = $changed_privs[$priv];
3419
			}
3420
		}
3421
	}
3422

    
3423
	/* update group privileges */
3424
	foreach ($config['system']['group'] as & $group) {
3425
		if (!is_array($group['priv'])) {
3426
			continue;
3427
		}
3428
		foreach ($group['priv'] as & $priv) {
3429
			if (array_key_exists($priv, $changed_privs)) {
3430
				$priv = $changed_privs[$priv];
3431
			}
3432
		}
3433
	}
3434

    
3435
	/* sync all local account information */
3436
	local_sync_accounts();
3437
}
3438

    
3439
function upgrade_104_to_105() {
3440
	global $config;
3441

    
3442
	if (is_array($config['captiveportal'])) {
3443
		$zoneid = 2;
3444
		foreach ($config['captiveportal'] as $cpzone => $cpcfg) {
3445
			if (empty($cpcfg['zoneid'])) {
3446
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3447
				$zoneid += 2;
3448
			} else if ($cpcfg['zoneid'] > 4000) {
3449
				$config['captiveportal'][$cpzone]['zoneid'] = $zoneid;
3450
				$zoneid += 2;
3451
			}
3452
		}
3453
	}
3454
}
3455

    
3456
function upgrade_105_to_106() {
3457
	/* NOTE: This upgrade code was reverted. See redmine ticket #3967 and
3458
	   https://github.com/pfsense/pfsense/commit/6f55af1c25f5232ffe905a90f5f97aad4c87bdfa */
3459
}
3460

    
3461
function upgrade_106_to_107() {
3462
	global $config;
3463

    
3464
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
3465
		$tracker = (int)microtime(true);
3466
		foreach ($config['filter']['rule'] as $ridx => $rule) {
3467
			if (empty($rule['tracker'])) {
3468
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
3469
				$tracker++;
3470
			}
3471
		}
3472
		unset($tracker, $ridx);
3473
	}
3474
	if (is_array($config['nat']) && is_array($config['nat']['rule'])) {
3475
		$tracker = (int)microtime(true);
3476
		foreach ($config['nat']['rule'] as $ridx => $rule) {
3477
			if (empty($rule['tracker'])) {
3478
				$config['nat']['rule'][$ridx]['tracker'] = $tracker;
3479
				$tracker++;
3480
			}
3481
		}
3482
		unset($tracker, $ridx);
3483
	}
3484
}
3485

    
3486
function upgrade_107_to_108() {
3487
	global $config;
3488

    
3489
	if (isset($config['system']['webgui']['noautocomplete'])) {
3490
		unset($config['system']['webgui']['noautocomplete']);
3491
	} else {
3492
		$config['system']['webgui']['loginautocomplete'] = true;
3493
	}
3494
}
3495

    
3496
function upgrade_108_to_109() {
3497
	global $config;
3498

    
3499
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule'])) {
3500
		return;
3501
	}
3502

    
3503
	foreach ($config['filter']['rule'] as &$rule) {
3504
		if (!isset($rule['dscp']) || empty($rule['dscp'])) {
3505
			continue;
3506
		}
3507

    
3508
		$pos = strpos($rule['dscp'], ' ');
3509
		if ($pos !== false) {
3510
			$rule['dscp'] = substr($rule['dscp'], 0, $pos);
3511
		}
3512
		unset($pos);
3513
	}
3514
}
3515

    
3516
function upgrade_109_to_110() {
3517
	global $config;
3518

    
3519
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3520
		return;
3521
	}
3522

    
3523
	foreach ($config['ipsec']['phase2'] as &$rule) {
3524
		if (!empty($rule['uniqid'])) {
3525
			continue;
3526
		}
3527

    
3528
		$rule['uniqid'] = uniqid();
3529
	}
3530
}
3531

    
3532
function upgrade_110_to_111() {
3533
	global $config;
3534

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

    
3539
	/* cleanup old unbound package stuffs */
3540
	unlink_if_exists("/usr/local/pkg/unbound.xml");
3541
	unlink_if_exists("/usr/local/pkg/unbound.inc");
3542
	unlink_if_exists("/usr/local/pkg/unbound_advanced.xml");
3543
	unlink_if_exists("/usr/local/www/unbound_status.php");
3544
	unlink_if_exists("/usr/local/www/unbound_acls.php");
3545
	unlink_if_exists("/usr/local/bin/unbound_monitor.sh");
3546
	unlink_if_exists("/usr/local/etc/rc.d/unbound.sh");
3547

    
3548
	/* Remove old menu and service entries */
3549
	if (isset($config['installedpackages']['menu']) && is_array($config['installedpackages']['menu'])) {
3550
		foreach ($config['installedpackages']['menu'] as $idx => $menu) {
3551
			if ($menu['name'] != 'Unbound DNS') {
3552
				continue;
3553
			}
3554

    
3555
			unset($config['installedpackages']['menu'][$idx]);
3556
			break;
3557
		}
3558
	}
3559

    
3560
	if (isset($config['installedpackages']['service']) && is_array($config['installedpackages']['service'])) {
3561
		foreach ($config['installedpackages']['service'] as $idx => $service) {
3562
			if ($service['name'] != 'unbound') {
3563
				continue;
3564
			}
3565
			unset($config['installedpackages']['service'][$idx]);
3566
			break;
3567
		}
3568
	}
3569

    
3570
	if (!isset($config['installedpackages']['unbound']['config'][0])) {
3571
		return;
3572
	}
3573

    
3574
	$pkg = $config['installedpackages']['unbound']['config'][0];
3575

    
3576
	if (isset($config['installedpackages']['unboundadvanced']['config'][0])) {
3577
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3578
	}
3579

    
3580
	$new = array();
3581

    
3582
	/* deal first with boolean fields */
3583
	$fields = array(
3584
		"enable" => "enable",
3585
		"dnssec_status" => "dnssec",
3586
		"forwarding_mode" => "forwarding",
3587
		"regdhcp" => "regdhcp",
3588
		"regdhcpstatic" => "regdhcpstatic",
3589
		"txtsupport" => "txtsupport",
3590
		"hide_id" => "hideidentity",
3591
		"hide_version" => "hideversion",
3592
		"prefetch" => "prefetch",
3593
		"prefetch_key" => "prefetchkey",
3594
		"harden_glue" => "hardenglue",
3595
		"harden_dnssec_stripped" => "dnssec_stripped");
3596

    
3597
	foreach ($fields as $oldk => $newk) {
3598
		if (isset($pkg[$oldk])) {
3599
			if ($pkg[$oldk] == 'on') {
3600
				$new[$newk] = true;
3601
			}
3602
			unset($pkg[$oldk]);
3603
		}
3604
	}
3605

    
3606
	$fields = array(
3607
		"active_interface" => "network_interface",
3608
		"query_interface" => "outgoing_interface",
3609
		"unbound_verbosity" => "log_verbosity",
3610
		"msg_cache_size" => "msgcachesize",
3611
		"outgoing_num_tcp" => "outgoing_num_tcp",
3612
		"incoming_num_tcp" => "incoming_num_tcp",
3613
		"edns_buffer_size" => "edns_buffer_size",
3614
		"num_queries_per_thread" => "num_queries_per_thread",
3615
		"jostle_timeout" => "jostle_timeout",
3616
		"cache_max_ttl" => "cache_max_ttl",
3617
		"cache_min_ttl" => "cache_min_ttl",
3618
		"infra_host_ttl" => "infra_host_ttl",
3619
		"infra_cache_numhosts" => "infra_cache_numhosts",
3620
		"unwanted_reply_threshold" => "unwanted_reply_threshold",
3621
		"custom_options" => "custom_options");
3622

    
3623
	foreach ($fields as $oldk => $newk) {
3624
		if (isset($pkg[$oldk])) {
3625
			$new[$newk] = $pkg[$oldk];
3626
			unset($pkg[$oldk]);
3627
		}
3628
	}
3629

    
3630
	if (isset($new['custom_options']) && !empty($new['custom_options'])) {
3631
		$new['custom_options'] = str_replace("\r\n", "\n", $new['custom_options']);
3632
	}
3633

    
3634
	/* Following options were removed, bring them as custom_options */
3635
	if (isset($pkg['stats']) && $pkg['stats'] == "on") {
3636
		if (isset($pkg['stats_interval'])) {
3637
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-interval: {$pkg['stats_interval']}";
3638
		}
3639
		if (isset($pkg['cumulative_stats'])) {
3640
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "statistics-cumulative: {$pkg['cumulative_stats']}";
3641
		}
3642
		if (isset($pkg['extended_stats']) && $pkg['extended_stats'] == "on") {
3643
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: yes";
3644
		} else {
3645
			$new['custom_options'] .= (empty($new['custom_options']) ? "" : "\n") . "extended-statistics: no";
3646
		}
3647
	}
3648

    
3649
	$new['acls'] = array();
3650
	if (isset($config['installedpackages']['unboundacls']['config']) &&
3651
	    is_array($config['installedpackages']['unboundacls']['config'])) {
3652
		foreach ($config['installedpackages']['unboundacls']['config'] as $acl) {
3653
			$new['acls'][] = $acl;
3654
		}
3655
	}
3656

    
3657
	$config['unbound'] = $new;
3658

    
3659
	if (isset($config['installedpackages']['unbound'])) {
3660
		unset($config['installedpackages']['unbound']);
3661
	}
3662
	if (isset($config['installedpackages']['unboundadvanced'])) {
3663
		unset($config['installedpackages']['unboundadvanced']);
3664
	}
3665
	if (isset($config['installedpackages']['unboundacls'])) {
3666
		unset($config['installedpackages']['unboundacls']);
3667
	}
3668

    
3669
	unset($pkg, $new);
3670
}
3671

    
3672
function upgrade_111_to_112() {
3673
	global $config;
3674

    
3675
	$config['cron']['item'][] = array(
3676
		'minute' => '*/60',
3677
		'hour' => '*',
3678
		'mday' => '*',
3679
		'month' => '*',
3680
		'wday' => '*',
3681
		'who' => 'root',
3682
		'command' => '/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 webConfiguratorlockout'
3683
	);
3684
}
3685

    
3686
function upgrade_112_to_113() {
3687
	global $config;
3688

    
3689
	if (isset($config['notifications']['smtp']['ssl'])) {
3690
		if ($config['notifications']['smtp']['ssl'] == "checked") {
3691
			$config['notifications']['smtp']['ssl'] = true;
3692
		} else {
3693
			unset($config['notifications']['smtp']['ssl']);
3694
		}
3695
	}
3696

    
3697
	if (isset($config['notifications']['smtp']['tls'])) {
3698
		if ($config['notifications']['smtp']['tls'] == "checked") {
3699
			$config['notifications']['smtp']['tls'] = true;
3700
		} else {
3701
			unset($config['notifications']['smtp']['tls']);
3702
		}
3703
	}
3704
}
3705

    
3706
function upgrade_113_to_114() {
3707
	global $config;
3708

    
3709
	if (!isset($config['ipsec']['phase1']) ||
3710
	    !is_array($config['ipsec']['phase1'])) {
3711
		return;
3712
	}
3713

    
3714
	foreach ($config['ipsec']['phase1'] as &$ph1ent) {
3715
		if (!isset($ph1ent['iketype'])) {
3716
			$ph1ent['iketype'] = 'ikev1';
3717
		}
3718
	}
3719
}
3720

    
3721
function upgrade_114_to_115() {
3722
	global $config;
3723

    
3724
	if (isset($config['unbound']['custom_options'])) {
3725
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3726
	}
3727
}
3728

    
3729
function upgrade_115_to_116() {
3730
	global $config;
3731

    
3732
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3733
		return;
3734
	}
3735

    
3736
	$keyid = 1;
3737
	foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
3738
		$config['ipsec']['phase2'][$idx]['reqid'] = $keyid;
3739
		$keyid++;
3740
	}
3741
}
3742

    
3743
function upgrade_116_to_117() {
3744
	global $config;
3745

    
3746
	if (!isset($config['ipsec']['client']) ||
3747
	    !isset($config['ipsec']['client']['dns_split']) ||
3748
	    empty($config['ipsec']['client']['dns_split'])) {
3749
		return;
3750
	}
3751

    
3752
	$config['ipsec']['client']['dns_split'] =
3753
		preg_replace('/\s*,\s*/', ' ', trim($config['ipsec']['client']['dns_split']));
3754

    
3755
}
3756

    
3757
function upgrade_117_to_118() {
3758
	global $config;
3759

    
3760
	// Unset any old CA and Cert in the system section that might still be there from when upgrade_066_to_067 did not unset them.
3761
	if (isset($config['system']['ca'])) {
3762
		unset($config['system']['ca']);
3763
	}
3764
	if (isset($config['system']['cert'])) {
3765
		unset($config['system']['cert']);
3766
	}
3767

    
3768
	if (!isset($config['ipsec']['phase1'])) {
3769
		return;
3770
	}
3771

    
3772
	$a_phase1 =& $config['ipsec']['phase1'];
3773

    
3774
	foreach ($a_phase1 as &$ph1_entry) {
3775
		// update asn1dn strings from racoon's format to strongswan's
3776
		if (isset($ph1_entry['myid_type']) && $ph1_entry['myid_type'] == 'asn1dn') {
3777
			$ph1_entry['myid_data'] =
3778
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['myid_data']);
3779
		}
3780
		if (isset($ph1_entry['peerid_type']) && $ph1_entry['peerid_type'] == 'asn1dn') {
3781
			$ph1_entry['peerid_data'] =
3782
			    preg_replace('/\/\s*emailAddress\s*=\s*/', ', E=', $ph1_entry['peerid_data']);
3783
		}
3784
	}
3785
}
3786

    
3787
function upgrade_118_to_119() {
3788
	global $config;
3789

    
3790
	if (!isset($config['ipsec']['phase1'])) {
3791
		return;
3792
	}
3793

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

    
3797
	foreach ($a_phase1 as &$ph1_entry) {
3798
		if (strstr($ph1_entry['authentication_method'], 'eap')) {
3799
			$ph1_entry['peerid_type'] = "any";
3800
		}
3801
	}
3802
}
3803

    
3804
function upgrade_119_to_120() {
3805
	require_once("ipsec.inc");
3806
	global $config, $ipsec_log_cats;
3807

    
3808
	if (!is_array($config['ipsec'])) {
3809
		return;
3810
	}
3811

    
3812
	// add 1 to configured log levels as part of redmine #5340
3813
	foreach ($ipsec_log_cats as $lkey => $ldescr) {
3814
		if (isset($config['ipsec']["ipsec_{$lkey}"])) {
3815
			$config['ipsec']["ipsec_{$lkey}"] = $config['ipsec']["ipsec_{$lkey}"] + 1;
3816
		}
3817
	}
3818

    
3819
}
3820

    
3821

    
3822
function upgrade_120_to_121() {
3823
	global $config;
3824

    
3825
	if (!isset($config['installedpackages']['miniupnpd']['config'][0])) {
3826
		return;
3827
	}
3828

    
3829
	$miniupnpd =& $config['installedpackages']['miniupnpd']['config'][0];
3830

    
3831
	$miniupnpd['row'] = array();
3832

    
3833
	for ($i = 1; $i <= 4; $i++) {
3834
		if (isset($miniupnpd["permuser{$i}"]) && !empty($miniupnpd["permuser{$i}"])) {
3835
			$miniupnpd['row'][] = array('permuser' => $miniupnpd["permuser{$i}"]);
3836
		}
3837
		unset($miniupnpd["permuser{$i}"]);
3838
	}
3839
}
3840

    
3841
function upgrade_121_to_122() {
3842
	global $config;
3843
	foreach ($config['system']['user'] as &$user) {
3844
		if (isset($user['nt-hash'])) {
3845
			unset($user['nt-hash']);
3846
		}
3847
	}
3848
}
3849

    
3850
function upgrade_122_to_123() {
3851
	global $config;
3852

    
3853
	// PPTP server was removed
3854
	if (isset($config['pptpd'])) {
3855
		unset($config['pptpd']);
3856
	}
3857

    
3858
	// Cleanup firewall rules
3859
	if (isset($config['filter']['rule']) && is_array($config['filter']['rule'])) {
3860
		$rules =& $config['filter']['rule'];
3861
		$last_rule = count($rules) - 1;
3862
		// Process in reverse order to be able to unset items
3863
		for ($i = $last_rule; $i >= 0; $i--) {
3864
			if (isset($rules[$i]['interface']) && $rules[$i]['interface'] == 'pptp') {
3865
				unset($config['filter']['rule'][$i]);
3866
				continue;
3867
			}
3868
			if (isset($rules[$i]['source']['network']) && $rules[$i]['source']['network'] == 'pptp') {
3869
				unset($config['filter']['rule'][$i]);
3870
				continue;
3871
			}
3872
			if (isset($rules[$i]['destination']['network']) && $rules[$i]['destination']['network'] == 'pptp') {
3873
				unset($config['filter']['rule'][$i]);
3874
				continue;
3875
			}
3876
		}
3877
	}
3878

    
3879
	// Cleanup 1:1 NAT rules
3880
	if (isset($config['nat']['onetoone']) && is_array($config['nat']['onetoone'])) {
3881
		$onetoone =& $config['nat']['onetoone'];
3882
		$last_rule = count($onetoone) - 1;
3883
		// Process in reverse order to be able to unset items
3884
		for ($i = $last_rule; $i >= 0; $i--) {
3885
			if (isset($onetoone[$i]['interface']) && $onetoone[$i]['interface'] == 'pptp') {
3886
				unset($config['nat']['onetoone'][$i]);
3887
				continue;
3888
			}
3889
			if (isset($onetoone[$i]['source']['network']) && $onetoone[$i]['source']['network'] == 'pptp') {
3890
				unset($config['nat']['onetoone'][$i]);
3891
				continue;
3892
			}
3893
			if (isset($onetoone[$i]['destination']['network']) && $onetoone[$i]['destination']['network'] == 'pptp') {
3894
				unset($config['nat']['onetoone'][$i]);
3895
				continue;
3896
			}
3897
		}
3898
	}
3899

    
3900
	// Cleanup npt NAT rules
3901
	if (isset($config['nat']['npt']) && is_array($config['nat']['npt'])) {
3902
		$npt =& $config['nat']['npt'];
3903
		$last_rule = count($npt) - 1;
3904
		// Process in reverse order to be able to unset items
3905
		for ($i = $last_rule; $i >= 0; $i--) {
3906
			if (isset($npt[$i]['interface']) && $npt[$i]['interface'] == 'pptp') {
3907
				unset($config['nat']['npt'][$i]);
3908
				continue;
3909
			}
3910
		}
3911
	}
3912

    
3913
	// Cleanup Port-forward NAT rules
3914
	if (isset($config['nat']['rule']) && is_array($config['nat']['rule'])) {
3915
		$nat_rules =& $config['nat']['rule'];
3916
		$last_rule = count($nat_rules) - 1;
3917
		// Process in reverse order to be able to unset items
3918
		for ($i = $last_rule; $i >= 0; $i--) {
3919
			if (isset($nat_rules[$i]['interface']) && $nat_rules[$i]['interface'] == 'pptp') {
3920
				unset($config['nat']['rule'][$i]);
3921
				continue;
3922
			}
3923
			if (isset($nat_rules[$i]['source']['network']) && $nat_rules[$i]['source']['network'] == 'pptp') {
3924
				unset($config['nat']['rule'][$i]);
3925
				continue;
3926
			}
3927
			if (isset($nat_rules[$i]['destination']['network']) && $nat_rules[$i]['destination']['network'] == 'pptp') {
3928
				unset($config['nat']['rule'][$i]);
3929
				continue;
3930
			}
3931
		}
3932
	}
3933

    
3934
	// Cleanup Port-forward NAT rules
3935
	if (isset($config['nat']['outbound']['rule']) && is_array($config['nat']['outbound']['rule'])) {
3936
		$out_rules =& $config['nat']['outbound']['rule'];
3937
		$last_rule = count($out_rules) - 1;
3938
		// Process in reverse order to be able to unset items
3939
		for ($i = $last_rule; $i >= 0; $i--) {
3940
			if (isset($out_rules[$i]['interface']) && $out_rules[$i]['interface'] == 'pptp') {
3941
				unset($config['nat']['outbound']['rule'][$i]);
3942
				continue;
3943
			}
3944
		}
3945
	}
3946
}
3947

    
3948
function upgrade_123_to_124() {
3949
	if (isset($config['system']['altpkgrepo'])) {
3950
		unset($config['system']['altpkgrepo']);
3951
	}
3952

    
3953
	if (isset($config['theme'])) {
3954
		unset($config['theme']);
3955
	}
3956
}
3957

    
3958
function upgrade_124_to_125() {
3959
	global $config;
3960

    
3961
	/* Find interfaces with WEP configured. */
3962
	foreach ($config['interfaces'] as $ifname => $intf) {
3963
		if (!is_array($intf['wireless'])) {
3964
			continue;
3965
		}
3966

    
3967
		/* Generate a notice, disable interface, remove WEP settings */
3968
		if (isset($intf['wireless']['wep']['enable'])) {
3969
			if (!function_exists("file_notice")) {
3970
				require_once("notices.inc");
3971
			}
3972
			file_notice("WirelessSettings", sprintf(gettext("WEP is no longer supported. It will be disabled on the %s interface and the interface will be disabled. Please reconfigure the interface."), $ifname));
3973
			unset($config['interfaces'][$ifname]['wireless']['wep']);
3974
			if (isset($intf['enable'])) {
3975
				unset($config['interfaces'][$ifname]['enable']);
3976
			}
3977
		}
3978
	}
3979
}
3980

    
3981
function upgrade_125_to_126() {
3982
	require_once("ipsec.inc");
3983
	global $config, $ipsec_log_cats, $ipsec_log_sevs;
3984

    
3985
	$def_loglevel = 1;
3986
	if (!is_array($config['ipsec'])) {
3987
		return;
3988
	}
3989

    
3990
	if (!isset($config['ipsec']['logging']) || !is_array($config['ipsec']['logging'])) {
3991
		$config['ipsec']['logging'] = array();
3992
	}
3993

    
3994
	/* subtract 2 from ipsec log levels. the value stored in the config.xml
3995
	 * will now match the strongswan level exactly.
3996
	 */
3997
	foreach (array_keys($ipsec_log_cats) as $cat) {
3998
		if (!isset($config['ipsec']["ipsec_{$cat}"])) {
3999
			$new_level = $def_loglevel;
4000
		} else {
4001
			$new_level = intval($config['ipsec']["ipsec_{$cat}"]) - 2;
4002
		}
4003

    
4004
		if (in_array($new_level, array_keys($ipsec_log_sevs))) {
4005
			$config['ipsec']['logging'][$cat] = $new_level;
4006
		} else {
4007
			$config['ipsec']['logging'][$cat] = $def_loglevel;
4008
		}
4009
		unset($config['ipsec']["ipsec_{$cat}"]);
4010
	}
4011
}
4012

    
4013
// prior to v2.3 <widgets><sequence> contains a list of widgets with display types:
4014
//		none, close, hide, & show
4015
// v2.3 & later uses:
4016
//		close & open
4017
// widgets not in use are simply not in the list
4018
function upgrade_126_to_127() {
4019
	global $config;
4020

    
4021
	if (!isset($config['widgets']['sequence'])) {
4022
		return;
4023
	}
4024

    
4025
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4026
	$new_widgets = array();
4027

    
4028
	foreach ($cur_widgets as $widget) {
4029
		list($file, $col, $display) = explode(':', $widget);
4030

    
4031
		switch ($display) {
4032
			case 'hide':
4033
				$display = 'close';
4034
				break;
4035
			case 'show':
4036
				$display = 'open';
4037
				break;
4038
			case 'open':
4039
				break;
4040
			default:
4041
				continue 2;
4042
		}
4043

    
4044
		/* Remove '-container' from widget name */
4045
		$file = preg_replace('/-container$/', '', $file);
4046

    
4047
		$new_widgets[] = "{$file}:{$col}:{$display}";
4048
	}
4049

    
4050
	$config['widgets']['sequence'] = implode(',', $new_widgets);
4051

    
4052
}
4053

    
4054
function upgrade_127_to_128() {
4055
	global $config;
4056

    
4057
	// If bindip is not already specified then migrate the old SNMP bindlan flag to a bindip setting
4058
	if (isset($config['snmpd']['bindlan'])) {
4059
		if (!isset($config['snmpd']['bindip'])) {
4060
			$config['snmpd']['bindip'] = 'lan';
4061
		}
4062
		unset($config['snmpd']['bindlan']);
4063
	}
4064
}
4065

    
4066
function upgrade_128_to_129() {
4067
	global $config;
4068

    
4069
	/* net.inet.ip.fastforwarding does not exist in 2.3. */
4070
	if (!isset($config['sysctl']['item']) ||
4071
	    !is_array($config['sysctl']['item'])) {
4072
		return;
4073
	}
4074

    
4075
	foreach ($config['sysctl']['item'] as $idx => $sysctl) {
4076
		if ($sysctl['tunable'] == "net.inet.ip.fastforwarding") {
4077
			unset($config['sysctl']['item'][$idx]);
4078
		}
4079
		if ($sysctl['tunable'] == "net.inet.ipsec.debug") {
4080
			$config['sysctl']['item'][$idx]['value'] = "0";
4081
		}
4082
	}
4083

    
4084
	/* IPSEC is always on in 2.3. */
4085
	if (isset($config['ipsec']['enable'])) {
4086
		unset($config['ipsec']['enable']);
4087
	} else if (is_array($config['ipsec']['phase1'])) {
4088
		/*
4089
		 * If IPsec was globally disabled, disable all
4090
		 * phase1 entries
4091
		 */
4092
		foreach ($config['ipsec']['phase1'] as $idx => $p1) {
4093
			$config['ipsec']['phase1'][$idx]['disabled'] = true;
4094
		}
4095
	}
4096
}
4097

    
4098
function upgrade_129_to_130() {
4099
	global $config;
4100

    
4101
	/* Change OpenVPN topology_subnet checkbox into topology multi-select #5526 */
4102
	if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
4103
		foreach ($config['openvpn']['openvpn-server'] as & $serversettings) {
4104
			if (strtolower($serversettings['topology_subnet']) == "yes") {
4105
				unset($serversettings['topology_subnet']);
4106
				$serversettings['topology'] = "subnet";
4107
			} else {
4108
				$serversettings['topology'] = "net30";
4109
			}
4110
		}
4111
	}
4112
}
4113

    
4114
function upgrade_130_to_131() {
4115
	global $config;
4116

    
4117
	// Default dpinger parameters at time of this upgrade (2.3)
4118
	$default_interval = 500;
4119
	$default_alert_interval = 1000;
4120
	$default_loss_interval = 2000;
4121
	$default_time_period = 60000;
4122

    
4123
	if (isset($config['syslog']['apinger'])) {
4124
		$config['syslog']['dpinger'] = true;
4125
		unset($config['syslog']['apinger']);
4126
	}
4127

    
4128
	if (isset($config['system']['apinger_debug'])) {
4129
		unset($config['system']['apinger_debug']);
4130
	}
4131

    
4132
	if (!isset($config['gateways']['gateway_item']) ||
4133
	    !is_array($config['gateways']['gateway_item'])) {
4134
		return;
4135
	}
4136

    
4137
	if (is_array($config['gateways']['gateway_item'])) {
4138
		foreach ($config['gateways']['gateway_item'] as &$gw) {
4139
			// dpinger uses milliseconds
4140
			if (isset($gw['interval']) &&
4141
				is_numeric($gw['interval'])) {
4142
				$gw['interval'] = $gw['interval'] * 1000;
4143
			}
4144

    
4145
			if (isset($gw['interval'])) {
4146
				$effective_interval = $gw['interval'];
4147
			} else {
4148
				$effective_interval = $default_interval;
4149
			}
4150

    
4151
			if (isset($gw['down']) &&
4152
				is_numeric($gw['down'])) {
4153
				$gw['time_period'] = $gw['down'] * 1000;
4154
				unset($gw['down']);
4155
			}
4156

    
4157
			if (isset($gw['time_period'])) {
4158
				$effective_time_period = $gw['time_period'];
4159
			} else {
4160
				$effective_time_period = $default_time_period;
4161
			}
4162

    
4163
			if (isset($gw['latencyhigh'])) {
4164
				// Default loss_interval is 2000, but must be set
4165
				// higher if latencyhigh is higher.
4166
				if ($gw['latencyhigh'] > $default_loss_interval) {
4167
					$gw['loss_interval'] = $gw['latencyhigh'];
4168
				}
4169
			}
4170

    
4171
			if (isset($gw['loss_interval'])) {
4172
				$effective_loss_interval = $gw['loss_interval'];
4173
			} else {
4174
				$effective_loss_interval = $default_loss_interval;
4175
			}
4176

    
4177
			if (isset($gw['interval'])) {
4178
				// Default alert_interval is 1000, but must be set
4179
				// higher if interval is higher.
4180
				if ($gw['interval'] > $default_alert_interval) {
4181
					$gw['alert_interval'] = $gw['interval'];
4182
				}
4183
			}
4184

    
4185
			if ((($effective_interval * 2) + $effective_loss_interval) >= $effective_time_period) {
4186
				$gw['time_period'] = ($effective_interval * 2) + $effective_loss_interval + 1;
4187
			}
4188

    
4189
			if (isset($gw['avg_delay_samples'])) {
4190
				unset($gw['avg_delay_samples']);
4191
			}
4192
			if (isset($gw['avg_delay_samples_calculated'])) {
4193
				unset($gw['avg_delay_samples_calculated']);
4194
			}
4195
			if (isset($gw['avg_loss_samples'])) {
4196
				unset($gw['avg_loss_samples']);
4197
			}
4198
			if (isset($gw['avg_loss_samples_calculated'])) {
4199
				unset($gw['avg_loss_samples_calculated']);
4200
			}
4201
			if (isset($gw['avg_loss_delay_samples'])) {
4202
				unset($gw['avg_loss_delay_samples']);
4203
			}
4204
			if (isset($gw['avg_loss_delay_samples_calculated'])) {
4205
				unset($gw['avg_loss_delay_samples_calculated']);
4206
			}
4207
		}
4208
	}
4209
}
4210

    
4211
function upgrade_131_to_132() {
4212
	global $config;
4213
	if (isset($config['system']['usefifolog'])) {
4214
		unset($config['system']['usefifolog']);
4215
		clear_all_log_files(false);
4216
	}
4217
}
4218

    
4219
function upgrade_132_to_133() {
4220
	global $config;
4221

    
4222
	if (isset($config['ipsec']['phase1']) &&
4223
	    is_array($config['ipsec']['phase1'])) {
4224
		foreach ($config['ipsec']['phase1'] as &$p1) {
4225
			if (isset($p1['encryption-algorithm']['name']) &&
4226
			    $p1['encryption-algorithm']['name'] == 'des') {
4227
				$p1['disabled'] = true;
4228
				file_notice("IPsec",
4229
				    sprintf(gettext("DES is no longer supported, IPsec phase 1 item '%s' is being disabled."), $p1['descr']));
4230
			}
4231
		}
4232
	}
4233

    
4234
	if (isset($config['ipsec']['phase2']) &&
4235
	    is_array($config['ipsec']['phase2'])) {
4236
		foreach ($config['ipsec']['phase2'] as &$p2) {
4237
			if (!isset($p2['encryption-algorithm-option']) ||
4238
			    !is_array($p2['encryption-algorithm-option'])) {
4239
				continue;
4240
			}
4241

    
4242
			foreach ($p2['encryption-algorithm-option'] as $ealgo) {
4243
				if ($ealgo['name'] == 'des') {
4244
					$p2['disabled'] = true;
4245
					file_notice("IPsec",
4246
					    sprintf(gettext("DES is no longer supported, IPsec phase 2 item '%s' is being disabled."), $p2['descr']));
4247
				}
4248
			}
4249
		}
4250
	}
4251
}
4252

    
4253
// Determine the highest column number in use and set dashboardcolumns accordingly
4254
function upgrade_133_to_134() {
4255
	global $config;
4256

    
4257
	if (!isset($config['widgets']['sequence']) || isset($config['system']['webgui']['dashboardcolumns'])) {
4258
		return;
4259
	}
4260

    
4261
	$cur_widgets = explode(',', trim($config['widgets']['sequence']));
4262
	$maxcols = 2;
4263

    
4264
	foreach ($cur_widgets as $widget) {
4265
		list($file, $col, $display) = explode(':', $widget);
4266

    
4267
		if (($display != 'none') && ($display != 'hide')) {
4268
			preg_match('#[0-9]+$#', $col, $column);
4269
			if ($column[0] > $maxcols) {
4270
				$maxcols = $column[0];
4271
			}
4272
		}
4273
	}
4274

    
4275
	$config['system']['webgui']['dashboardcolumns'] = $maxcols % 10;
4276
}
4277

    
4278
function upgrade_134_to_135() {
4279
	global $config;
4280

    
4281
	if (isset($config['syslog']['nologlighttpd'])) {
4282
		unset($config['syslog']['nologlighttpd']);
4283
		$config['syslog']['nolognginx'] = true;
4284
	}
4285
}
4286

    
4287
function upgrade_135_to_136() {
4288
	global $config;
4289

    
4290
	$l7_active = false;
4291
	if (isset($config['l7shaper'])) {
4292
		unset($config['l7shaper']);
4293
		if (is_array($config['filter']['rule'])) {
4294
			foreach ($config['filter']['rule'] as $idx => $rule) {
4295
				if (isset($rule['l7container'])) {
4296
					unset($config['filter']['rule'][$idx]['l7container']);
4297
					$l7_active = true;
4298
				}
4299
			}
4300
		}
4301
		if ($l7_active) {
4302
			file_notice("L7shaper", gettext("Layer 7 shaping is no longer supported. Its configuration has been removed."));
4303
		}
4304
	}
4305
}
4306

    
4307
function upgrade_136_to_137() {
4308
	global $config;
4309

    
4310
	if (is_array($config['dhcpd'])) {
4311
		foreach ($config['dhcpd'] as &$dhcpd) {
4312
			if (!is_array($dhcpd['numberoptions']['item'])) {
4313
				continue;
4314
			}
4315

    
4316
			foreach ($dhcpd['numberoptions']['item'] as &$item) {
4317
				$item['value'] = base64_encode($item['value']);
4318
			}
4319
		}
4320
	}
4321

    
4322
	if (is_array($config['dhcpdv6'])) {
4323
		foreach ($config['dhcpdv6'] as &$dhcpdv6) {
4324
			if (!is_array($dhcpdv6['numberoptions']['item'])) {
4325
				continue;
4326
			}
4327

    
4328
			foreach ($dhcpdv6['numberoptions']['item'] as &$item) {
4329
				$item['value'] = base64_encode($item['value']);
4330
			}
4331
		}
4332
	}
4333
}
4334

    
4335
function upgrade_137_to_138() {
4336
	global $config;
4337

    
4338
	// the presence of unityplugin tag used to disable loading of unity plugin
4339
	// it's now disabled by default, and config tag is to enable. Unset accordingly.
4340
	if (is_array($config['ipsec'])) {
4341
		if (isset($config['ipsec']['unityplugin'])) {
4342
			unset($config['ipsec']['unityplugin']);
4343
		}
4344
	}
4345
}
4346

    
4347
function upgrade_138_to_139() {
4348
	global $config;
4349

    
4350
	// clean up state killing on gateway failure. having kill_states set used to mean it was disabled
4351
	// now set gw_down_kill_states if enabled.
4352
	if (!isset($config['system']['kill_states'])) {
4353
		$config['system']['gw_down_kill_states'] = true;
4354
	} else {
4355
		unset($config['system']['kill_states']);
4356
	}
4357
}
4358

    
4359
function upgrade_139_to_140() {
4360
	global $config;
4361

    
4362
	if (is_array($config['virtualip']['vip'])) {
4363
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4364
			if ($vip['mode'] == "carp") {
4365
				if (!isset($vip['uniqid'])) {
4366
					$config['virtualip']['vip'][$idx]['uniqid'] = uniqid();
4367
				}
4368
			}
4369
		}
4370
	}
4371
}
4372

    
4373
function upgrade_140_to_141() {
4374
	global $config;
4375

    
4376
	// retain OpenVPN's net30 default topology for upgraded client configs so they still work
4377
	// This is for 2.3 ALPHA to a later 2.3, not 2.2.x upgrades, which had no topology setting on clients
4378
	if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
4379
		foreach ($config['openvpn']['openvpn-client'] as $idx => $ovpnclient) {
4380
			if (!isset($ovpnclient['topology'])) {
4381
				$config['openvpn']['openvpn-client'][$idx]['topology'] = "net30";
4382
			}
4383
		}
4384
	}
4385

    
4386
	// repeat addition of filter tracker IDs from 106_to_107 where missing since associated filter rules were missing them
4387
	if (is_array($config['filter']) && is_array($config['filter']['rule'])) {
4388
		$tracker = (int)microtime(true);
4389
		foreach ($config['filter']['rule'] as $ridx => $rule) {
4390
			if (empty($rule['tracker'])) {
4391
				$config['filter']['rule'][$ridx]['tracker'] = $tracker;
4392
				$tracker++;
4393
			}
4394
		}
4395
		unset($tracker, $ridx);
4396
	}
4397

    
4398
}
4399

    
4400
function upgrade_141_to_142() {
4401
	global $config;
4402
	/* Convert Namecheap type DynDNS entries to the new split hostname and domain format */
4403

    
4404
	if (!is_array($config['dyndnses'])) {
4405
		$config['dyndnses'] = array();
4406
	}
4407
	if (!is_array($config['dyndnses']['dyndns'])) {
4408
		$config['dyndnses']['dyndns'] = array();
4409
	}
4410
	$a_dyndns = &$config['dyndnses']['dyndns'];
4411

    
4412
	foreach ($a_dyndns as &$dyndns) {
4413
		if ($dyndns['type'] == "namecheap") {
4414
			/* Use the old style logic to split the host and domain one last time. */
4415
			$dparts = explode(".", trim($dyndns['host']));
4416
			$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
4417
			$domain_offset = count($dparts) - $domain_part_count;
4418
			$dyndns['host'] = implode(".", array_slice($dparts, 0, $domain_offset));
4419
			$dyndns['domainname'] = implode(".", array_slice($dparts, $domain_offset));
4420
		}
4421
	}
4422

    
4423
	/* unset old pppoerestart cron job if it exists. redmine 1905 */
4424
	if (is_array($config['cron']['item'])) {
4425
		foreach ($config['cron']['item'] as $idx => $cronitem) {
4426
			if ($cronitem['command'] == "/etc/pppoerestart") {
4427
				unset($config['cron']['item'][$idx]);
4428
			}
4429
		}
4430
	}
4431
}
4432

    
4433
// Updated to check for empty separator definitions via is_array()
4434
function upgrade_142_to_143() {
4435
	global $config;
4436

    
4437
	/* Re-index firewall rule separators per interface */
4438
	if (is_array($config['filter']['separator'])) {
4439
		foreach ($config['filter']['separator'] as $interface => $separators) {
4440

    
4441
			if (is_array($separators)) {
4442
				foreach ($separators as $sepn => $separator) {
4443

    
4444
					$seprow = substr($separator['row']['0'], 2);
4445
					$sepif  = $separator['if'];
4446

    
4447
					// Determine position of separator within the interface rules.
4448
					$i = -1; $j = 0;
4449
					foreach ($config['filter']['rule'] as $rulen => $filterent) {
4450

    
4451
						if ($i == $seprow) {
4452
							// Set separator row to it's position within the interface rules.
4453
							$config['filter']['separator'][$sepif][$sepn]['row'] = 'fr' . $j;
4454
							continue 2;	// Advance to next separator
4455
						}
4456

    
4457
						// Position within the interface rules.
4458
						if (($filterent['interface'] == $sepif && !isset($filterent['floating'])) || (isset($filterent['floating']) && "floatingrules" == $sepif)) {
4459
							$j++;
4460
						}
4461
						$i++;
4462
					}
4463
				}
4464
			}
4465
		}
4466
	}
4467

    
4468
	/* Re-index nat rule separators */
4469
	if (is_array($config['nat']['separator'])) {
4470
		foreach ($config['nat']['separator'] as $sepn => $separator) {
4471
			if (is_array($separator)) {
4472
				$seprow = substr($separator['row']['0'], 2);
4473
				$config['nat']['separator'][$sepn]['row'] = 'fr' . ($seprow + 1);
4474
			}
4475
		}
4476
	}
4477
}
4478

    
4479
function get_vip_from_ip_alias($ipalias) {
4480
	global $config;
4481

    
4482
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4483
		if ($vip['mode'] != "ipalias") {
4484
			continue;
4485
		}
4486
		if ($ipalias == $vip['subnet']) {
4487
			return ("_vip{$vip['uniqid']}");
4488
		}
4489
	}
4490

    
4491
	return ($ipalias);
4492
}
4493

    
4494
function get_vip_from_oldcarp($carp) {
4495
	global $config;
4496

    
4497
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4498
		if ($vip['mode'] != "carp") {
4499
			continue;
4500
		}
4501
		if ($carp == "{$vip['interface']}_vip{$vip['vhid']}") {
4502
			return ("_vip{$vip['uniqid']}");
4503
		}
4504
	}
4505

    
4506
	return ($carp);
4507
}
4508

    
4509
function upgrade_143_to_144() {
4510
	global $config;
4511

    
4512
	if (is_array($config['virtualip']['vip'])) {
4513
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4514
			if ($vip['mode'] == "ipalias") {
4515
				if (!isset($vip['uniqid'])) {
4516
					$config['virtualip']['vip'][$idx]['uniqid'] = uniqid();
4517
				}
4518
			}
4519
		}
4520
	}
4521

    
4522
	/* Convert IPsec phase 1 entries. */
4523
	if (is_array($config['ipsec']['phase1'])) {
4524
		foreach ($config['ipsec']['phase1'] as $idx => $ph1ent) {
4525
			if (is_ipaddr($ph1ent['interface']) || is_ipaddrv6($ph1ent['interface'])) {
4526
				$config['ipsec']['phase1'][$idx]['interface'] = get_vip_from_ip_alias($ph1ent['interface']);
4527
			} else if (strpos($ph1ent['interface'], "_vip")) {
4528
				$config['ipsec']['phase1'][$idx]['interface'] = get_vip_from_oldcarp($ph1ent['interface']);
4529
			}
4530
		}
4531
	}
4532

    
4533
	/* Convert openvpn. */
4534
	if (is_array($config['openvpn']['openvpn-server'])) {
4535
		foreach ($config['openvpn']['openvpn-server'] as $idx => $ovpn) {
4536
			if (empty($ovpn['interface'])) {
4537
				continue;
4538
			}
4539
			if (is_ipaddr($ovpn['interface']) || is_ipaddrv6($ovpn['interface'])) {
4540
				$config['openvpn']['openvpn-server'][$idx]['interface'] = get_vip_from_ip_alias($ovpn['interface']);
4541
			} else if (strpos($ovpn['interface'], "_vip")) {
4542
				$config['openvpn']['openvpn-server'][$idx]['interface'] = get_vip_from_oldcarp($ovpn['interface']);
4543
			}
4544
		}
4545
	}
4546
	if (is_array($config['openvpn']['openvpn-client'])) {
4547
		foreach ($config['openvpn']['openvpn-client'] as $idx => $ovpn) {
4548
			if (empty($ovpn['interface'])) {
4549
				continue;
4550
			}
4551
			if (is_ipaddr($ovpn['interface']) || is_ipaddrv6($ovpn['interface'])) {
4552
				$config['openvpn']['openvpn-client'][$idx]['interface'] = get_vip_from_ip_alias($ovpn['interface']);
4553
			} else if (strpos($ovpn['interface'], "_vip")) {
4554
				$config['openvpn']['openvpn-client'][$idx]['interface'] = get_vip_from_oldcarp($ovpn['interface']);
4555
			}
4556
		}
4557
	}
4558

    
4559
	/* Convert unbound. */
4560
	if (is_array($config['unbound']) && !empty($config['unbound']['active_interface'])) {
4561
		$active_ifs = explode(",", $config['unbound']['active_interface']);
4562
		$ifs = array();
4563
		foreach ($active_ifs as $if) {
4564
			if (is_ipaddr($if) || is_ipaddrv6($if)) {
4565
				$ifs[] = get_vip_from_ip_alias($if);
4566
			} else if (strpos($if, "_vip")) {
4567
				$ifs[] = get_vip_from_oldcarp($if);
4568
			} else {
4569
				$ifs[] = $if;
4570
			}
4571
		}
4572
		$config['unbound']['active_interface'] = implode(",", $ifs);
4573
	}
4574

    
4575
	/* Convert dnsmasq. */
4576
	if (is_array($config['dnsmasq']) && !empty($config['dnsmasq']['interface'])) {
4577
		$active_ifs = explode(",", $config['dnsmasq']['interface']);
4578
		$ifs = array();
4579
		foreach ($active_ifs as $if) {
4580
			if (is_ipaddr($if) || is_ipaddrv6($if)) {
4581
				$ifs[] = get_vip_from_ip_alias($if);
4582
			} else if (strpos($if, "_vip")) {
4583
				$ifs[] = get_vip_from_oldcarp($if);
4584
			} else {
4585
				$ifs[] = $if;
4586
			}
4587
		}
4588
		$config['dnsmasq']['interface'] = implode(",", $ifs);
4589
	}
4590
}
4591

    
4592
function upgrade_144_to_145() {
4593
	global $config;
4594

    
4595
	// Enable DHCPv6 server and radvd config for track6 interfaces,
4596
	// matching what used to be automatically enabled with no user
4597
	// configurability.
4598
	if (is_array($config['interfaces'])) {
4599
		foreach ($config['interfaces'] as $ifname => $ifcfg) {
4600
			if (isset($ifcfg['enable'])) {
4601
				if ($ifcfg['ipaddrv6'] == "track6") {
4602
					$config['dhcpdv6'][$ifname]['enable'] = true;
4603
					$config['dhcpdv6'][$ifname]['range']['from'] = "::1000";
4604
					$config['dhcpdv6'][$ifname]['range']['to'] = "::2000";
4605
					$config['dhcpdv6'][$ifname]['ramode'] = "assist";
4606
					$config['dhcpdv6'][$ifname]['rapriority'] = "medium";
4607
				}
4608
			}
4609
		}
4610
	}
4611
}
4612

    
4613
function upgrade_145_to_146() {
4614
	// Add standard deviation to the quality rrds
4615
	global $config, $g;
4616

    
4617
	$rrddbpath = "/var/db/rrd";
4618
	$rrdtool = "/usr/local/bin/rrdtool";
4619

    
4620
	$awkcmd = "/usr/bin/awk '";
4621
	$awkcmd .= "{\n";
4622
	$awkcmd .= "    if (sub(/<\\/v><\\/row>/, \"</v><v>NaN</v></row>\") == 0)\n";
4623
	$awkcmd .= "    {\n";
4624
	$awkcmd .= "        if (/<\\/cdp_prep>/)\n";
4625
	$awkcmd .= "        {\n";
4626
	$awkcmd .= "            print \"			<ds>\"\n";
4627
	$awkcmd .= "            print \"			<primary_value> 0.0000000000e+00 </primary_value>\"\n";
4628
	$awkcmd .= "            print \"			<secondary_value> 0.0000000000e+00 </secondary_value>\"\n";
4629
	$awkcmd .= "            print \"			<value> NaN </value>\"\n";
4630
	$awkcmd .= "            print \"			<unknown_datapoints> 0 </unknown_datapoints>\"\n";
4631
	$awkcmd .= "            print \"			</ds>\"\n";
4632
	$awkcmd .= "        }\n";
4633
	$awkcmd .= "        else if (/<!-- Round Robin Archives -->/)\n";
4634
	$awkcmd .= "        {\n";
4635
	$awkcmd .= "            print \"	<ds>\"\n";
4636
	$awkcmd .= "            print \"		<name> stddev </name>\"\n";
4637
	$awkcmd .= "            print \"		<type> GAUGE </type>\"\n";
4638
	$awkcmd .= "            print \"		<minimal_heartbeat> 120 </minimal_heartbeat>\"\n";
4639
	$awkcmd .= "            print \"		<min> 0.0000000000e+00 </min>\"\n";
4640
	$awkcmd .= "            print \"		<max> 1.0000000000e+05 </max>\\n\"\n";
4641
	$awkcmd .= "            print \"		<!-- PDP Status -->\"\n";
4642
	$awkcmd .= "            print \"		<last_ds> 0 </last_ds>\"\n";
4643
	$awkcmd .= "            print \"		<value> 0.0000000000e+00 </value>\"\n";
4644
	$awkcmd .= "            print \"		<unknown_sec> 0 </unknown_sec>\"\n";
4645
	$awkcmd .= "            print \"	</ds>\\n\"\n";
4646
	$awkcmd .= "        }\n";
4647
	$awkcmd .= "    }\n";
4648
	$awkcmd .= "    print;\n";
4649
	$awkcmd .= "}'";
4650

    
4651
	if (isset($config['system']['use_mfs_tmpvar'])) {
4652
		/* restore the databases, if we have one */
4653
		if (restore_rrd()) {
4654
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
4655
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
4656
		}
4657
	}
4658

    
4659
	$databases = return_dir_as_array($rrddbpath, '/-quality\.rrd$/');
4660
	foreach ($databases as $database) {
4661
		$xmldump = "{$g['tmp_path']}/{$database}.xml";
4662

    
4663
		if (platform_booting()) {
4664
			echo "Update RRD database {$database}.\n";
4665
		}
4666

    
4667
		exec("$rrdtool dump {$rrddbpath}/{$database} | {$awkcmd} > {$xmldump}");
4668
		exec("$rrdtool restore -f {$xmldump} {$rrddbpath}/{$database}");
4669
		@unlink("{$xmldump}");
4670
	}
4671

    
4672
	if (!platform_booting()) {
4673
		enable_rrd_graphing();
4674
	}
4675
	/* Let's save the RRD graphs after we run enable RRD graphing */
4676
	/* The function will restore the rrd.tgz so we will save it after */
4677
	exec("cd /; LANG=C RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
4678
}
4679

    
4680
function upgrade_bgpd_146_to_147() {
4681
	global $config;
4682

    
4683
	if (!isset($config['installedpackages']['openbgpd']['config']) ||
4684
	    !is_array($config['installedpackages']['openbgpd']['config'])) {
4685
		return;
4686
	}
4687
	$openbgpd_conf = &$config['installedpackages']['openbgpd']['config'][0];
4688
	if (!isset($openbgpd_conf['carpstatusip']) &&
4689
	    !is_ipaddr($openbgpd_conf['carpstatusip'])) {
4690
		return;
4691
	}
4692

    
4693
	if (!is_array($config['virtualip']['vip']))
4694
		return;
4695
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4696
		if ($vip['subnet'] == $openbgpd_conf['carpstatusip']) {
4697
			$openbgpd_conf['carpstatusvid'] = "_vip{$vip['uniqid']}";
4698
			unset($openbgpd_conf['carpstatusip']);
4699
			return;
4700
		}
4701
	}
4702
}
4703

    
4704
function upgrade_quagga_146_to_147() {
4705
	global $config;
4706

    
4707
	if (!isset($config['installedpackages']['quaggaospfd']['config']) ||
4708
	    !is_array($config['installedpackages']['quaggaospfd']['config'])) {
4709
		return;
4710
	}
4711
	$ospfd_conf = &$config['installedpackages']['quaggaospfd']['config'][0];
4712
	if (!isset($ospfd_conf['carpstatusip']) &&
4713
	    !is_ipaddr($ospfd_conf['carpstatusip'])) {
4714
		return;
4715
	}
4716

    
4717
	if (!is_array($config['virtualip']['vip']))
4718
		return;
4719
	foreach ($config['virtualip']['vip'] as $idx => $vip) {
4720
		if ($vip['subnet'] == $ospfd_conf['carpstatusip']) {
4721
			$ospfd_conf['carpstatusvid'] = "_vip{$vip['uniqid']}";
4722
			unset($ospfd_conf['carpstatusip']);
4723
			return;
4724
		}
4725
	}
4726
}
4727

    
4728
function upgrade_146_to_147() {
4729

    
4730
	upgrade_bgpd_146_to_147();
4731
	upgrade_quagga_146_to_147();
4732
}
4733

    
4734
function upgrade_147_to_148() {
4735
	global $config;
4736

    
4737
	// Ensure there are no spaces in group names by
4738
	// replacing spaces with underscores
4739
	if (is_array($config['system']['group'])) {
4740
		$cleargroups = false;
4741
		foreach ($config['system']['group'] as $idx => $grp) {
4742
			if (strstr($grp['name'], " ")) {
4743
				$cleargroups = true;
4744
				$config['system']['group'][$idx]['scope'] = "remote";
4745
			}
4746
		}
4747

    
4748
		// if there was a space in a group name, there may be multiple
4749
		// groups with the same name in the group file. To prevent pw 
4750
		// from getting into a neverending loop, delete all user-defined
4751
		// groups here. local_sync_accounts will run shortly after this
4752
		// and add them back. redmine #6012
4753
		if ($cleargroups) {
4754
			foreach ($config['system']['group'] as $grp) {
4755
				mwexec("/usr/sbin/pw groupdel -g {$grp['gid']}");
4756
			}
4757
		}
4758
	}
4759
}
4760

    
4761
function upgrade_148_to_149() {
4762
	global $config;
4763
	global $altq_list_queues;
4764

    
4765
        if (!isset($config['shaper']['queue']) || !is_array($config['shaper']['queue']))
4766
                return;
4767

    
4768
	read_altq_config();
4769

    
4770
	/* Set root queue bandwidth. */
4771
	foreach ($altq_list_queues as $altq) {
4772
		$sum = $altq->GetTotalBw();
4773
		while ($sum > get_queue_bandwidth($altq)) {
4774
			if (intval(($sum / 1000) * 1.2) < (1024 * 1024)) {
4775
				/* 1Gb where possible. */
4776
				$bw = 1024 * 1024;
4777
			} else {
4778
				/* Increase by 20% until it fits. */
4779
				$bw = intval(($sum / 1000) * 1.2);
4780
			}
4781
			$altq->SetBandwidth($bw);
4782
			$altq->SetBwscale("Kb");
4783
			$altq->wconfig();
4784
			$sum = $altq->GetTotalBw();
4785
		}
4786
	}
4787
}
4788

    
4789
function upgrade_149_to_150() {
4790
	global $config;
4791

    
4792
	if (is_array($config['dhcpdv6'])) {
4793
                foreach ($config['dhcpdv6'] as &$dhcpdv6) {
4794
			if (isset($dhcpdv6['rainterface'])) {
4795
				if (strstr($dhcpdv6['rainterface'], "_vip")) {
4796
					$dhcpdv6['rainterface'] = get_vip_from_oldcarp($dhcpdv6['rainterface']);
4797
				}
4798
			}
4799
		}
4800
	}
4801
}
4802

    
4803
function upgrade_150_to_151() {
4804
	global $config;
4805

    
4806
	// Default dpinger parameters at time of this upgrade (2.3.1)
4807
	$default_interval = 500;
4808
	$default_alert_interval = 1000;
4809
	$default_loss_interval = 2000;
4810
	$default_time_period = 60000;
4811
	$default_latencyhigh = 500;
4812

    
4813
	// Check advanced gateway parameter relationships in case they are incorrect
4814
	if (is_array($config['gateways']['gateway_item'])) {
4815
		foreach ($config['gateways']['gateway_item'] as &$gw) {
4816
			if (isset($gw['interval'])) {
4817
				$effective_interval = $gw['interval'];
4818
			} else {
4819
				$effective_interval = $default_interval;
4820
			}
4821

    
4822
			if (isset($gw['alert_interval'])) {
4823
				$effective_alert_interval = $gw['alert_interval'];
4824
			} else {
4825
				$effective_alert_interval = $default_alert_interval;
4826
			}
4827

    
4828
			if (isset($gw['loss_interval'])) {
4829
				$effective_loss_interval = $gw['loss_interval'];
4830
			} else {
4831
				$effective_loss_interval = $default_loss_interval;
4832
			}
4833

    
4834
			if (isset($gw['time_period'])) {
4835
				$effective_time_period = $gw['time_period'];
4836
			} else {
4837
				$effective_time_period = $default_time_period;
4838
			}
4839

    
4840
			if (isset($gw['latencyhigh'])) {
4841
				$effective_latencyhigh = $gw['latencyhigh'];
4842
			} else {
4843
				$effective_latencyhigh = $default_latencyhigh;
4844
			}
4845

    
4846
			// Loss interval has to be at least as big as high latency.
4847
			if ($effective_latencyhigh > $effective_loss_interval) {
4848
				$effective_loss_interval = $gw['loss_interval'] = $effective_latencyhigh;
4849
			}
4850

    
4851
			// Alert interval has to be at least as big as probe interval.
4852
			if ($effective_interval > $effective_alert_interval) {
4853
				$gw['alert_interval'] = $effective_interval;
4854
			}
4855

    
4856
			// The time period for averaging has to be more than 2 probes plus the loss interval.
4857
			if ((($effective_interval * 2) + $effective_loss_interval) >= $effective_time_period) {
4858
				$gw['time_period'] = ($effective_interval * 2) + $effective_loss_interval + 1;
4859
			}
4860
		}
4861
	}
4862
}
4863

    
4864
function upgrade_151_to_152() {
4865
	global $g, $config;
4866

    
4867
	require_once("/etc/inc/services.inc");
4868

    
4869
	// Remove these cron jobs on full install if not using ramdisk.
4870
	if (!isset($config['system']['use_mfs_tmpvar'])) {
4871
		/* See #7146 for detail on why the extra parameters are needed for the time being. */
4872
		install_cron_job("/etc/rc.backup_rrd.sh", false, null, null, null, null, null, null, false);
4873
		install_cron_job("/etc/rc.backup_dhcpleases.sh", false, null, null, null, null, null, null, false);
4874
	}
4875
}
4876

    
4877
function upgrade_152_to_153() {
4878
	global $config;
4879

    
4880
	if (is_array($config['virtualip']['vip'])) {
4881
		foreach ($config['virtualip']['vip'] as $idx => $vip) {
4882
			if (substr($vip['interface'], 0, 4) == "_vip") {
4883
				// using new VIP format
4884
				continue;
4885
			} else if (strstr($vip['interface'], "_vip")) {
4886
				// using old VIP format, update
4887
				$config['virtualip']['vip'][$idx]['interface'] = get_vip_from_oldcarp($vip['interface']);
4888
			}
4889
		}
4890
	}
4891

    
4892
	// upgrade GIFs using VIP to new format
4893
	if (is_array($config['gifs']['gif'])) {
4894
		foreach ($config['gifs']['gif'] as $idx => $gif) {
4895
			if (substr($gif['if'], 0, 4) == "_vip") {
4896
				// using new VIP format
4897
				continue;
4898
			} else if (strstr($gif['if'], "_vip")) {
4899
				// using old VIP format, update
4900
				$config['gifs']['gif'][$idx]['if'] = get_vip_from_oldcarp($gif['if']);
4901
			}
4902
		}
4903
	}
4904

    
4905
	// upgrade GREs using VIP to new format
4906
	if (is_array($config['gres']['gre'])) {
4907
		foreach ($config['gres']['gre'] as $idx => $gre) {
4908
			if (substr($gre['if'], 0, 4) == "_vip") {
4909
				// using new VIP format
4910
				continue;
4911
			} else if (strstr($gre['if'], "_vip")) {
4912
				// using old VIP format, update
4913
				$config['gres']['gre'][$idx]['if'] = get_vip_from_oldcarp($gre['if']);
4914
			}
4915
		}
4916
	}
4917

    
4918
	// upgrade gateway groups using VIPs
4919
	if (is_array($config['gateways']['gateway_group'])) {
4920
		foreach ($config['gateways']['gateway_group'] as $idx => $gw) {
4921
			if (is_array($gw['item'])) {
4922
				$newitems = array();
4923
				$gwvipchange = false;
4924
				foreach ($gw['item'] as $item) {
4925
					if (strstr($item, "|_vip")) {
4926
						// using new VIP format
4927
						$newitems[] = $item;
4928
						continue;
4929
					} else if (strstr($item, "_vip")) {
4930
						// using old VIP format, update
4931
						$gwitemarr = explode("|", $item);
4932
						$gwitemarr[2] = get_vip_from_oldcarp($gwitemarr[2]);
4933
						$newitems[] = implode("|", $gwitemarr);
4934
						$gwvipchange = true;
4935
					} else {
4936
						$newitems[] = $item;
4937
					}
4938
				}
4939
				if ($gwvipchange) {
4940
					$config['gateways']['gateway_group'][$idx]['item'] = $newitems;
4941
				}
4942
			}
4943
		}
4944
	}
4945
}
4946

    
4947
function upgrade_153_to_154() {
4948
	/* NOTE: This upgrade code was reverted. See redmine ticket #6118 and
4949
	   https://github.com/pfsense/pfsense/commit/538a3c04a6b6671151e913b06b2f340b6f8ee222 */
4950
}
4951

    
4952
/* Clean up old GRE/GIF options. See Redmine tickets #6586 and #6587 */
4953
function upgrade_154_to_155() {
4954
	global $config;
4955

    
4956
	if (is_array($config['gifs']['gif'])) {
4957
		foreach ($config['gifs']['gif'] as $idx => $gif) {
4958
			if (isset($gif['link0'])) {
4959
				unset($config['gifs']['gif'][$idx]['link0']);
4960
			}
4961
		}
4962
	}
4963

    
4964
	if (is_array($config['gres']['gre'])) {
4965
		foreach ($config['gres']['gre'] as $idx => $gre) {
4966
			if (isset($gre['link0'])) {
4967
				unset($config['gres']['gre'][$idx]['link0']);
4968
			}
4969
			if (isset($gre['link2'])) {
4970
				unset($config['gres']['gre'][$idx]['link2']);
4971
			}
4972
		}
4973
	}
4974
}
4975

    
4976
function upgrade_155_to_156() {
4977
	// Unused
4978
}
4979

    
4980
function upgrade_156_to_157() {
4981
	global $config;
4982
	/* Convert Cloudflare and GratisDNS type DynDNS entries to the new split hostname and domain format */
4983

    
4984
	if (!is_array($config['dyndnses'])) {
4985
		$config['dyndnses'] = array();
4986
	}
4987
	if (!is_array($config['dyndnses']['dyndns'])) {
4988
		$config['dyndnses']['dyndns'] = array();
4989
	}
4990
	$a_dyndns = &$config['dyndnses']['dyndns'];
4991

    
4992
	foreach ($a_dyndns as &$dyndns) {
4993
		if (($dyndns['type'] == "cloudflare") || ($dyndns['type'] == "cloudflare-v6") || ($dyndns['type'] == "gratisdns")) {
4994
			/* Use the old style logic to split the host and domain one last time. */
4995
			$dparts = explode(".", trim($dyndns['host']));
4996
			$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
4997
			$domain_offset = count($dparts) - $domain_part_count;
4998
			$dyndns['host'] = implode(".", array_slice($dparts, 0, $domain_offset));
4999
			$dyndns['domainname'] = implode(".", array_slice($dparts, $domain_offset));
5000
		}
5001
	}
5002

    
5003
	/* unset old pppoerestart cron job if it exists. redmine 1905 */
5004
	if (is_array($config['cron']['item'])) {
5005
		foreach ($config['cron']['item'] as $idx => $cronitem) {
5006
			if ($cronitem['command'] == "/etc/pppoerestart") {
5007
				unset($config['cron']['item'][$idx]);
5008
			}
5009
		}
5010
	}
5011
}
5012

    
5013
function upgrade_157_to_158() {
5014
	global $config;
5015
	/* Convert Dynamic DNS passwords to base64 encoding. Redmine #6688 */
5016

    
5017
	if (!is_array($config['dyndnses'])) {
5018
		$config['dyndnses'] = array();
5019
	}
5020
	if (!is_array($config['dyndnses']['dyndns'])) {
5021
		$config['dyndnses']['dyndns'] = array();
5022
	}
5023
	$a_dyndns = &$config['dyndnses']['dyndns'];
5024

    
5025
	foreach ($a_dyndns as &$dyndns) {
5026
		$dyndns['password'] = base64_encode($dyndns['password']);
5027
	}
5028
}
5029

    
5030
/* Unset references to glxsb in the config. See #6755 */
5031
function upgrade_158_to_159() {
5032
	global $config;
5033

    
5034
	if ($config['system']['crypto_hardware'] == "glxsb") {
5035
		unset($config['system']['crypto_hardware']);
5036
	}
5037
}
5038

    
5039
/* Convert OpenVPN "protocol" to new style for OpenVPN 2.4, old udp/tcp was
5040
 * IPv4 only, now is dual stack, so change it to udp4/tcp4
5041
 */
5042
function upgrade_159_to_160() {
5043
	global $config;
5044

    
5045
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
5046
		if (is_array($config['openvpn']['openvpn-server'])) {
5047
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
5048
				if ($vpn['protocol'] == "UDP") {
5049
					$vpn['protocol'] = "UDP4";
5050
				}
5051
				if ($vpn['protocol'] == "TCP") {
5052
					$vpn['protocol'] = "TCP4";
5053
				}
5054
			}
5055
		}
5056
		if (is_array($config['openvpn']['openvpn-client'])) {
5057
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
5058
				if ($vpn['protocol'] == "UDP") {
5059
					$vpn['protocol'] = "UDP4";
5060
				}
5061
				if ($vpn['protocol'] == "TCP") {
5062
					$vpn['protocol'] = "TCP4";
5063
				}
5064
			}
5065
		}
5066
	}
5067
}
5068

    
5069
/* RAM Disk Management */
5070
function upgrade_160_to_161() {
5071
	global $g, $config;
5072

    
5073
	if (!isset($config['system']['use_mfs_tmpvar'])) {
5074
		return;
5075
	}
5076

    
5077
	// Move existing RRD backup to the RAM Disk Store if it don't already exist there.
5078
	// Restore existing RRD XML dump backup.
5079
	if (file_exists("{$g['cf_conf_path']}/rrd.tgz") && !file_exists("{$g['cf_conf_path']}/RAM_Disk_Store/rrd.tgz")) {
5080
		$rrddbpath = "{$g['vardb_path']}/rrd/";
5081
		$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
5082

    
5083
		$rrdrestore = "";
5084
		$rrdreturn = "";
5085
		unlink_if_exists("{$rrddbpath}/*.xml");
5086

    
5087
		unset($rrdrestore);
5088
		$_gb = exec("LANG=C /usr/bin/tar -tf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
5089
		if ($rrdreturn != 0) {
5090
			log_error(sprintf(gettext('RRD restore failed exited with %1$s, the error is: %2$s'), $rrdreturn, $rrdrestore));
5091
		} else {
5092
			foreach ($rrdrestore as $xml_file) {
5093
				$rrd_file = '/' . substr($xml_file, 0, -4) . '.rrd';
5094
				unlink_if_exists("{$rrd_file}"); 
5095

    
5096
				file_put_contents("{$g['tmp_path']}/rrd_restore", $xml_file);
5097
				$_gb = exec("LANG=C /usr/bin/tar -xf {$g['cf_conf_path']}/rrd.tgz -C / -T {$g['tmp_path']}/rrd_restore");
5098
				if (!file_exists("/{$xml_file}")) {
5099
					log_error(sprintf(gettext("Could not extract %s RRD xml file from archive!"), $xml_file));
5100
					continue;
5101
				}
5102
				$_gb = exec("$rrdtool restore -f '/{$xml_file}' '{$rrd_file}'", $output, $status);
5103
				if ($status) {
5104
					log_error(sprintf(gettext("rrdtool restore -f '%1\$s' '%2\$s' failed returning %3\$s."), $xml_file, $rrd_file, $status));
5105
					continue;
5106
				}
5107
				unset($output);
5108
				@unlink("/{$xml_file}");
5109
			}
5110
			unset($rrdrestore);
5111
			@unlink("{$g['tmp_path']}/rrd_restore");
5112

    
5113
			// Create a new RRD backup to the RAM Disk Store (without RRD XML dump).
5114
			exec("/etc/rc.backup_rrd.sh");
5115
			$ramds_updated = true;
5116

    
5117
			// Rename previous RRD backup so it will not restore again.  Don't delete in case needed for recovery.
5118
			rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/rrd.tgz.old");
5119
		}
5120
	}
5121

    
5122
	// Move existing DHCP leases backup to the RAM Disk Store if it don't already exist there.
5123
	if (file_exists("{$g['cf_conf_path']}/dhcpleases.tgz") && ! file_exists("{$g['cf_conf_path']}/RAM_Disk_Store/dhcpleases.tgz")) {
5124
		rename("{$g['cf_conf_path']}/dhcpleases.tgz", "{$g['cf_conf_path']}/RAM_Disk_Store/dhcpleases.tgz");
5125
		$ramds_updated = true;
5126
	}
5127

    
5128
	// Move existing alias table backups to the RAM Disk Store if they don't already exist there.
5129
	$dbpath = "{$g['vardb_path']}/aliastables/";
5130
	$files = glob("{$g['cf_conf_path']}/RAM_Disk_Store{$dbpath}*.tgz");
5131
	if (count($files)) {
5132
		foreach ($files as $file) {
5133
			if (! file_exists("{$g['cf_conf_path']}/RAM_Disk_Store/".basename($file))) {
5134
				rename($file, "{$g['cf_conf_path']}/RAM_Disk_Store/".basename($file));
5135
				$ramds_updated = true;
5136
			}
5137
		}
5138
		// Remove existing alias table backups directory if empty.
5139
		@rmdir("{$g['cf_conf_path']}/RAM_Disk_Store/var/db/aliastables");
5140
		@rmdir("{$g['cf_conf_path']}/RAM_Disk_Store/var/db/");
5141
		@rmdir("{$g['cf_conf_path']}/RAM_Disk_Store/var/");
5142
	}
5143

    
5144
	// Restore RAM Disk Store if updated.
5145
	if ($ramds_updated) {
5146
		exec("/etc/rc.restore_ramdisk_store");
5147
	}
5148
}
5149

    
5150
/* Previous versions of pfSense had cryptodev built into the kernel.
5151
 * To retain the expected behavior on upgrade, load the cryptodev
5152
 * module for users that did not choose a module.
5153
 */
5154
function upgrade_161_to_162() {
5155
	global $config;
5156
	if (empty($config['system']['crypto_hardware'])) {
5157
		$config['system']['crypto_hardware'] = "cryptodev";
5158
	}
5159
}
5160

    
5161
/* Traffic graphs widget settings are now stored in a layout similar
5162
 * to other widgets. Migrate any old settings.
5163
 */
5164
function upgrade_162_to_163() {
5165
	require_once("ipsec.inc");
5166
	global $config;
5167

    
5168
	foreach (array('refreshinterval', 'invert', 'size', 'backgroundupdate') as $setting) {
5169
		if (isset($config['widgets']['trafficgraphs'][$setting])) {
5170
			$config['widgets']['traffic_graphs'][$setting] = $config['widgets']['trafficgraphs'][$setting];
5171
			unset($config['widgets']['trafficgraphs'][$setting]);
5172
		}
5173
	}
5174

    
5175
	if (isset($config['widgets']['trafficgraphs']['shown'])) {
5176
		if (is_array($config['widgets']['trafficgraphs']['shown']['item'])) {
5177
			$ifdescrs = get_configured_interface_with_descr();
5178

    
5179
			if (ipsec_enabled()) {
5180
				$ifdescrs['enc0'] = "IPsec";
5181
			}
5182

    
5183
			$validNames = array();
5184

    
5185
			foreach ($ifdescrs as $ifdescr => $ifname) {
5186
				array_push($validNames, $ifdescr);
5187
			}
5188

    
5189
			$config['widgets']['traffic_graphs']['filter'] = implode(',', array_diff($validNames, $config['widgets']['trafficgraphs']['shown']['item']));
5190
		}
5191

    
5192
		unset($config['widgets']['trafficgraphs']['shown']);
5193
	}
5194
}
5195

    
5196
/* Dashboard widget settings config format has changed to support having possibly multiple
5197
 * of a widget on the dashboard. Migrate any old settings.
5198
 */
5199
function convert_widget_164($oldname, $newname, $settings_keys) {
5200
	global $config;
5201

    
5202
	if ($newname == '') {
5203
		$newname = $oldname . '-0';
5204
	}
5205

    
5206
	if ($oldname == '') {
5207
		// These settings were stored directly in $config['widgets']
5208
		// Move them down under their new key.
5209
		// e.g. $config['widgets']['filterlogentries']
5210
		// becomes $config['widgets']['log-0']['filterlogentries']
5211
		foreach ($settings_keys as $oldkey => $newkey) {
5212
			if ($newkey == '') {
5213
				$newkey = $oldkey;
5214
			}
5215

    
5216
			// Modify the system-wide entry
5217
			if (isset($config['widgets'][$oldkey])) {
5218
				$config['widgets'][$newname][$newkey] = $config['widgets'][$oldkey];
5219
				unset($config['widgets'][$oldkey]);
5220
			}
5221

    
5222
			// Modify any user-specific entries
5223
			foreach ($config['system']['user'] as & $user) {
5224
				if (isset($user['widgets'][$oldkey])) {
5225
					$user['widgets'][$newname][$newkey] = $user['widgets'][$oldkey];
5226
					unset($user['widgets'][$oldkey]);
5227
				}
5228
			}
5229
		}
5230
	} else {
5231
		// These settings were stored in some key under 'widgets',
5232
		// e.g. $config['widgets']['gateways_widget']['display_type']
5233
		// becomes $config['widgets']['gateways-0']['display_type']
5234
		foreach ($settings_keys as $oldkey => $newkey) {
5235
			if ($newkey == '') {
5236
				$newkey = $oldkey;
5237
			}
5238

    
5239
			// Modify the system-wide entry
5240
			if (isset($config['widgets'][$oldname][$oldkey])) {
5241
				$config['widgets'][$newname][$newkey] = $config['widgets'][$oldname][$oldkey];
5242
				unset($config['widgets'][$oldname][$oldkey]);
5243
			}
5244

    
5245
			// Modify any user-specific entries
5246
			foreach ($config['system']['user'] as & $user) {
5247
				if (isset($user['widgets'][$oldname][$oldkey])) {
5248
					$user['widgets'][$newname][$newkey] = $user['widgets'][$oldname][$oldkey];
5249
					unset($user['widgets'][$oldname][$oldkey]);
5250
				}
5251

    
5252
				if (isset($user['widgets'][$oldname])) {
5253
					unset($user['widgets'][$oldname]);
5254
				}
5255
			}
5256
		}
5257

    
5258
		if (isset($config['widgets'][$oldname])) {
5259
			unset($config['widgets'][$oldname]);
5260
		}
5261
	}
5262
}
5263

    
5264
function upgrade_163_to_164() {
5265
	global $config;
5266

    
5267
	convert_widget_164('dyn_dns_status', '', array('filter' => ''));
5268
	convert_widget_164('gateways_widget', 'gateways-0', array('display_type' => '', 'gatewaysfilter' => ''));
5269
	convert_widget_164('interface_statistics', '', array('iffilter' => ''));
5270
	convert_widget_164('interfaces', '', array('iffilter' => ''));
5271
	convert_widget_164('', 'log-0',
5272
		array(
5273
			'filterlogentries' => '',
5274
			'filterlogentriesacts' => '',
5275
			'filterlogentriesinterfaces' => '',
5276
			'filterlogentriesinterval' => ''));
5277
	convert_widget_164('openvpn', '', array('filter' => ''));
5278
	convert_widget_164('', 'picture-0', array('picturewidget' => '', 'picturewidget_filename' => ''));
5279
	convert_widget_164('', 'rss-0', array('rssfeed' => '', 'rssmaxitems' => '', 'rsswidgetheight' => '', 'rsswidgettextlength' => ''));
5280
	convert_widget_164('', 'services_status-0', array('servicestatusfilter' => 'filter'));
5281
	convert_widget_164('smart_status', '', array('filter' => ''));
5282
	convert_widget_164('system_information', '', array('filter' => ''));
5283
	convert_widget_164('thermal_sensors_widget', 'thermal_sensors-0',
5284
		array(
5285
			'thermal_sensors_widget_zone_warning_threshold' => '',
5286
			'thermal_sensors_widget_zone_critical_threshold' => '',
5287
			'thermal_sensors_widget_core_warning_threshold' => '',
5288
			'thermal_sensors_widget_core_critical_threshold' => '',
5289
			'thermal_sensors_widget_show_raw_output' => '',
5290
			'thermal_sensors_widget_show_full_sensor_name' => '',
5291
			'thermal_sensors_widget_pulsate_warning' => '',
5292
			'thermal_sensors_widget_pulsate_critical' => ''
5293
		));
5294
	convert_widget_164('wol', 'wake_on_lan-0', array('filter' => ''));
5295
}
5296

    
5297
/* Work around broken wizard rules. See https://redmine.pfsense.org/issues/7434 */
5298
function upgrade_164_to_165() {
5299
	global $config;
5300
	foreach ($config['filter']['rule'] as & $rule) {
5301
		if ($rule['destination']['port'] == "137-139-137-139") {
5302
			$rule['destination']['port'] = "137-139";
5303
		}
5304
	}
5305
}
5306

    
5307
/* Fixup digest algorithm selection for OpenVPN clients and servers so they do not use aliased names. */
5308
function upgrade_165_to_166() {
5309
	require_once('openvpn.inc');
5310
	global $config;
5311

    
5312
	if (isset($config['openvpn']) && is_array($config['openvpn'])) {
5313
		if (is_array($config['openvpn']['openvpn-server'])) {
5314
			foreach ($config['openvpn']['openvpn-server'] as &$vpn) {
5315
				$vpn['digest'] = openvpn_remap_digest($vpn['digest']);
5316
			}
5317
		}
5318
		if (is_array($config['openvpn']['openvpn-client'])) {
5319
			foreach ($config['openvpn']['openvpn-client'] as &$vpn) {
5320
				$vpn['digest'] = openvpn_remap_digest($vpn['digest']);
5321
			}
5322
		}
5323
	}
5324
}
5325

    
5326
?>
(45-45/54)