Project

General

Profile

Download (146 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 0: sequential 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(false, 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(false, 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
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3324
}
3325

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3579
	$new = array();
3580

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3754
}
3755

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

    
3759
	// 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.
3760
	if (isset($config['system']['ca'])) {
3761
		unset($config['system']['ca']);
3762
	}
3763
	if (isset($config['system']['cert'])) {
3764
		unset($config['system']['cert']);
3765
	}
3766

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

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

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

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

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

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

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

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

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

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

    
3818
}
3819

    
3820

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3966
		/* Generate a notice, disable interface, remove WEP settings */
3967
		if (isset($intf['wireless']['wep']['enable'])) {
3968
			if (!function_exists("file_notice")) {
3969
				require_once("notices.inc");
3970
			}
3971
			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));
3972
			unset($config['interfaces'][$ifname]['wireless']['wep']);
3973
			if (isset($intf['enable'])) {
3974
				unset($config['interfaces'][$ifname]['enable']);
3975
			}
3976
		}
3977
	}
3978
}
3979

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4051
}
4052

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4397
}
4398

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4490
	return ($ipalias);
4491
}
4492

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

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

    
4505
	return ($carp);
4506
}
4507

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4727
function upgrade_146_to_147() {
4728

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

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

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

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

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

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

    
4767
	read_altq_config();
4768

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4868
	// Remove these cron jobs on full install if not using ramdisk.
4869
	if (!isset($config['system']['use_mfs_tmpvar'])) {
4870
		install_cron_job("/etc/rc.backup_rrd.sh", false);
4871
		install_cron_job("/etc/rc.backup_dhcpleases.sh", false);
4872
	}
4873
}
4874

    
4875
function upgrade_152_to_153() {
4876
	global $config;
4877

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

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

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

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

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

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

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

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

    
4974
function upgrade_155_to_156() {
4975
	// Unused
4976
}
4977

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

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

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

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

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

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

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

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

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

    
5037
?>
(42-42/51)