Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
195

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

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

    
207

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

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

    
226

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

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

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

    
244
			$curent['targetpipe'] = $i;
245

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

    
253

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

    
264
	/* Queues are no longer interface specific */
265
	if (isset($config['interfaces']['lan']['schedulertype'])) {
266
		unset($config['interfaces']['lan']['schedulertype']);
267
	}
268
	if (isset($config['interfaces']['wan']['schedulertype'])) {
269
		unset($config['interfaces']['wan']['schedulertype']);
270
	}
271

    
272
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
273
		if (isset($config['interfaces']['opt' . $i]['schedulertype'])) {
274
			unset($config['interfaces']['opt' . $i]['schedulertype']);
275
		}
276
	}
277
}
278

    
279

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

    
294

    
295
function upgrade_016_to_017() {
296
	global $config;
297
	/* wipe previous shaper configuration */
298
	unset($config['shaper']['queue']);
299
	unset($config['shaper']['rule']);
300
	unset($config['interfaces']['wan']['bandwidth']);
301
	unset($config['interfaces']['wan']['bandwidthtype']);
302
	unset($config['interfaces']['lan']['bandwidth']);
303
	unset($config['interfaces']['lan']['bandwidthtype']);
304
	$config['shaper']['enable'] = FALSE;
305
}
306

    
307

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

    
355
	/* enable SSH */
356
	if ($config['version'] == "1.8") {
357
		$config['system']['sshenabled'] = true;
358
	}
359
}
360

    
361

    
362
function upgrade_018_to_019() {
363
	global $config;
364
	$config['theme']="metallic";
365
}
366

    
367

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

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

    
391

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

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

    
406

    
407
function upgrade_023_to_024() {
408
	global $config;
409
}
410

    
411

    
412
function upgrade_024_to_025() {
413
	global $config;
414
	$config['interfaces']['wan']['use_rrd_gateway'] = $config['system']['use_rrd_gateway'];
415
	unset($config['system']['use_rrd_gateway']);
416
}
417

    
418

    
419
function upgrade_025_to_026() {
420
	global $config;
421
	$cron_item = array();
422
	$cron_item['minute'] = "0";
423
	$cron_item['hour'] = "*";
424
	$cron_item['mday'] = "*";
425
	$cron_item['month'] = "*";
426
	$cron_item['wday'] = "*";
427
	$cron_item['who'] = "root";
428
	$cron_item['command'] = "/usr/bin/nice -n20 newsyslog";
429

    
430
	$config['cron']['item'][] = $cron_item;
431

    
432
	$cron_item = array();
433
	$cron_item['minute'] = "1,31";
434
	$cron_item['hour'] = "0-5";
435
	$cron_item['mday'] = "*";
436
	$cron_item['month'] = "*";
437
	$cron_item['wday'] = "*";
438
	$cron_item['who'] = "root";
439
	$cron_item['command'] = "/usr/bin/nice -n20 adjkerntz -a";
440

    
441
	$config['cron']['item'][] = $cron_item;
442

    
443
	$cron_item = array();
444
	$cron_item['minute'] = "1";
445
	$cron_item['hour'] = "*";
446
	$cron_item['mday'] = "1";
447
	$cron_item['month'] = "*";
448
	$cron_item['wday'] = "*";
449
	$cron_item['who'] = "root";
450
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_bogons.sh";
451

    
452
	$config['cron']['item'][] = $cron_item;
453

    
454
	$cron_item = array();
455
	$cron_item['minute'] = "*/60";
456
	$cron_item['hour'] = "*";
457
	$cron_item['mday'] = "*";
458
	$cron_item['month'] = "*";
459
	$cron_item['wday'] = "*";
460
	$cron_item['who'] = "root";
461
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 sshlockout";
462

    
463
	$config['cron']['item'][] = $cron_item;
464

    
465
	$cron_item = array();
466
	$cron_item['minute'] = "1";
467
	$cron_item['hour'] = "1";
468
	$cron_item['mday'] = "*";
469
	$cron_item['month'] = "*";
470
	$cron_item['wday'] = "*";
471
	$cron_item['who'] = "root";
472
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.dyndns.update";
473

    
474
	$config['cron']['item'][] = $cron_item;
475

    
476
	$cron_item = array();
477
	$cron_item['minute'] = "*/60";
478
	$cron_item['hour'] = "*";
479
	$cron_item['mday'] = "*";
480
	$cron_item['month'] = "*";
481
	$cron_item['wday'] = "*";
482
	$cron_item['who'] = "root";
483
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -v -t 3600 virusprot";
484

    
485
	$config['cron']['item'][] = $cron_item;
486

    
487
	$cron_item = array();
488
	$cron_item['minute'] = "*/60";
489
	$cron_item['hour'] = "*";
490
	$cron_item['mday'] = "*";
491
	$cron_item['month'] = "*";
492
	$cron_item['wday'] = "*";
493
	$cron_item['who'] = "root";
494
	$cron_item['command'] = "/usr/bin/nice -n20 /usr/local/sbin/expiretable -t 1800 snort2c";
495

    
496
	$config['cron']['item'][] = $cron_item;
497
}
498

    
499

    
500
function upgrade_026_to_027() {
501
	global $config;
502
}
503

    
504

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

    
509

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

    
523

    
524
function upgrade_029_to_030() {
525
	global $config;
526
	/* enable the rrd config setting by default */
527
	$config['rrd']['enable'] = true;
528
}
529

    
530

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

    
536

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

    
542

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

    
548

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

    
554

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

    
560

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

    
566

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

    
572

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

    
578

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

    
584

    
585
function upgrade_039_to_040() {
586
	global $config, $g;
587
	$config['system']['webgui']['auth_method'] = "session";
588
	$config['system']['webgui']['backing_method'] = "htpasswd";
589

    
590
	if (isset ($config['system']['username'])) {
591
		$config['system']['group'] = array();
592
		$config['system']['group'][0]['name'] = "admins";
593
		$config['system']['group'][0]['description'] = gettext("System Administrators");
594
		$config['system']['group'][0]['scope'] = "system";
595
		$config['system']['group'][0]['priv'] = "page-all";
596
		$config['system']['group'][0]['home'] = "index.php";
597
		$config['system']['group'][0]['gid'] = "110";
598

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

    
609
		$config['system']['user'][0]['priv'] = array();
610
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
611
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
612
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
613
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
614
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
615
		$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).");
616
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
617
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
618
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
619
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
620
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
621
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly)."), $g['product_name']);
622
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
623
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
624
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (you should associate this privilege only with one single user).");
625

    
626
		$config['system']['nextuid'] = "111";
627
		$config['system']['nextgid'] = "111";
628

    
629
		/* wipe previous auth configuration */
630
		unset ($config['system']['username']);
631
		unset ($config['system']['password']);
632
	}
633
}
634

    
635
function upgrade_040_to_041() {
636
	global $config;
637
	if (!$config['sysctl']) {
638
		$config['sysctl']['item'] = array();
639

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

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

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

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

    
656
		$config['sysctl']['item'][4]['tunable'] = "net.inet.ip.redirect";
657
		$config['sysctl']['item'][4]['descr'] =    gettext("Sending of IPv4 ICMP redirects");
658
		$config['sysctl']['item'][4]['value'] =   "default";
659

    
660
		$config['sysctl']['item'][5]['tunable'] = "net.inet6.ip6.redirect";
661
		$config['sysctl']['item'][5]['descr'] =    gettext("Sending of IPv6 ICMP redirects");
662
		$config['sysctl']['item'][5]['value'] =   "default";
663

    
664
		$config['sysctl']['item'][6]['tunable'] = "net.inet.tcp.syncookies";
665
		$config['sysctl']['item'][6]['descr'] =    gettext("Generate SYN cookies for outbound SYN-ACK packets");
666
		$config['sysctl']['item'][6]['value'] =   "default";
667

    
668
		$config['sysctl']['item'][7]['tunable'] = "net.inet.tcp.recvspace";
669
		$config['sysctl']['item'][7]['descr'] =    gettext("Maximum incoming TCP datagram size");
670
		$config['sysctl']['item'][7]['value'] =   "default";
671

    
672
		$config['sysctl']['item'][8]['tunable'] = "net.inet.tcp.sendspace";
673
		$config['sysctl']['item'][8]['descr'] =    gettext("Maximum outgoing TCP datagram size");
674
		$config['sysctl']['item'][8]['value'] =   "default";
675

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

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

    
684
		$config['sysctl']['item'][11]['tunable'] = "net.inet.udp.maxdgram";
685
		$config['sysctl']['item'][11]['descr'] =    gettext("Maximum outgoing UDP datagram size");
686
		$config['sysctl']['item'][11]['value'] =   "default";
687

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

    
692
		$config['sysctl']['item'][13]['tunable'] = "net.link.tap.user_open";
693
		$config['sysctl']['item'][13]['descr'] =    gettext("Allow unprivileged access to tap(4) device nodes");
694
		$config['sysctl']['item'][13]['value'] =   "default";
695

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

    
700
		$config['sysctl']['item'][16]['tunable'] = "net.inet.tcp.inflight.enable";
701
		$config['sysctl']['item'][16]['descr'] =    gettext("The system will attempt to calculate the bandwidth delay product for each connection and limit the amount of data queued to the network to just the amount required to maintain optimum throughput. ");
702
		$config['sysctl']['item'][16]['value'] =   "default";
703

    
704
		$config['sysctl']['item'][17]['tunable'] = "net.inet.icmp.icmplim";
705
		$config['sysctl']['item'][17]['descr'] =    gettext("Set ICMP Limits");
706
		$config['sysctl']['item'][17]['value'] =   "default";
707

    
708
		$config['sysctl']['item'][18]['tunable'] = "net.inet.tcp.tso";
709
		$config['sysctl']['item'][18]['descr'] =    gettext("TCP Offload engine");
710
		$config['sysctl']['item'][18]['value'] =   "default";
711

    
712
		$config['sysctl']['item'][19]['tunable'] = "net.inet.ip.portrange.first";
713
		$config['sysctl']['item'][19]['descr'] =    "Set the ephemeral port range starting port";
714
		$config['sysctl']['item'][19]['value'] =   "default";
715

    
716
		$config['sysctl']['item'][20]['tunable'] = "hw.syscons.kbd_reboot";
717
		$config['sysctl']['item'][20]['descr'] =    "Enables ctrl+alt+delete";
718
		$config['sysctl']['item'][20]['value'] =   "default";
719

    
720
		$config['sysctl']['item'][21]['tunable'] = "kern.ipc.maxsockbuf";
721
		$config['sysctl']['item'][21]['descr'] =    "Maximum socket buffer size";
722
		$config['sysctl']['item'][21]['value'] =   "default";
723

    
724
	}
725
}
726

    
727

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

    
738

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

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

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

    
792

    
793
function upgrade_043_to_044() {
794
	global $config;
795

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

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

    
834

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

    
855

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

    
940

    
941
function upgrade_046_to_047() {
942
	global $config;
943
	/* Upgrade IPsec from tunnel to phase1/phase2 */
944

    
945
	if (is_array($config['ipsec']['tunnel'])) {
946

    
947
		$a_phase1 = array();
948
		$a_phase2 = array();
949
		$ikeid = 0;
950

    
951
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
952

    
953
			unset($ph1ent);
954
			unset($ph2ent);
955

    
956
			/*
957
				*  attempt to locate an enabled phase1
958
				*  entry that matches the peer gateway
959
				*/
960

    
961
			if (!isset($tunnel['disabled'])) {
962

    
963
				$remote_gateway = $tunnel['remote-gateway'];
964

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

    
973
			/* none found, create a new one */
974

    
975
			if (!isset($ph1ent)) {
976

    
977
				/* build new phase1 entry */
978

    
979
				$ph1ent = array();
980

    
981
				$ph1ent['ikeid'] = ++$ikeid;
982

    
983
				if (isset($tunnel['disabled'])) {
984
					$ph1ent['disabled'] = $tunnel['disabled'];
985
				}
986

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

    
996
				$ph1ent['mode'] = $tunnel['p1']['mode'];
997

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

    
1022
				$ph1ent['peerid_type'] = "peeraddress";
1023

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

    
1046
				$ph1ent['encryption-algorithm'] = $ph1alg;
1047
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
1048
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
1049
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
1050
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
1051

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

    
1065
				$ph1ent['nat_traversal'] = "on";
1066
				$ph1ent['dpd_enable'] = 1;
1067
				$ph1ent['dpd_delay'] = 10;
1068
				$ph1ent['dpd_maxfail'] = 5;
1069

    
1070
				$a_phase1[] = $ph1ent;
1071
			}
1072

    
1073
			/* build new phase2 entry */
1074

    
1075
			$ph2ent = array();
1076

    
1077
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1078

    
1079
			if (isset($tunnel['disabled'])) {
1080
				$ph1ent['disabled'] = $tunnel['disabled'];
1081
			}
1082

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

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

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

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

    
1113
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1251
		$ph1ent['nat_traversal'] = "on";
1252
		$ph1ent['dpd_enable'] = 1;
1253
		$ph1ent['dpd_delay'] = 10;
1254
		$ph1ent['dpd_maxfail'] = 5;
1255
		$ph1ent['mobile'] = true;
1256

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

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

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

    
1298
		$config['ipsec']['phase1'][] = $ph1ent;
1299
		$config['ipsec']['phase2'][] = $ph2ent;
1300
		unset($config['ipsec']['mobileclients']);
1301
	}
1302
}
1303

    
1304

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

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

    
1368

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

    
1379
	if (!is_array($config['system']['user'])) {
1380
		$config['system']['user'] = array();
1381
	}
1382
	if (!is_array($config['system']['group'])) {
1383
		$config['system']['group'] = array();
1384
	}
1385

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

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

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

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

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

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

    
1436

    
1437
function upgrade_049_to_050() {
1438
	global $config;
1439

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

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

    
1480
	/* sync all local account information */
1481
	local_sync_accounts();
1482
}
1483

    
1484

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

    
1498
	unset($config['bridge']);
1499

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

    
1525

    
1526
function upgrade_051_to_052() {
1527
	global $config;
1528
	$config['openvpn'] = array();
1529
	if (!is_array($config['ca'])) {
1530
		$config['ca'] = array();
1531
	}
1532
	if (!is_array($config['cert'])) {
1533
		$config['cert'] = array();
1534
	}
1535

    
1536
	$vpnid = 1;
1537

    
1538
	/* openvpn server configurations */
1539
	if (is_array($config['installedpackages']['openvpnserver'])) {
1540
		$config['openvpn']['openvpn-server'] = array();
1541

    
1542
		$index = 1;
1543
		foreach ($config['installedpackages']['openvpnserver']['config'] as $server) {
1544

    
1545
			if (!is_array($server)) {
1546
				continue;
1547
			}
1548

    
1549
			if ($server['auth_method'] == "pki") {
1550

    
1551
				/* create ca entry */
1552
				$ca = array();
1553
				$ca['refid'] = uniqid();
1554
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1555
				$ca['crt'] = $server['ca_cert'];
1556
				$config['ca'][] = $ca;
1557

    
1558
				/* create ca reference */
1559
				unset($server['ca_cert']);
1560
				$server['caref'] = $ca['refid'];
1561

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

    
1577
				/* create cert entry */
1578
				$cert = array();
1579
				$cert['refid'] = uniqid();
1580
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1581
				$cert['crt'] = $server['server_cert'];
1582
				$cert['prv'] = $server['server_key'];
1583
				$config['cert'][] = $cert;
1584

    
1585
				/* create cert reference */
1586
				unset($server['server_cert']);
1587
				unset($server['server_key']);
1588
				$server['certref'] = $cert['refid'];
1589

    
1590
				$index++;
1591
			}
1592

    
1593
			/* determine operational mode */
1594
			if ($server['auth_method'] == 'pki') {
1595
				if ($server['nopool']) {
1596
					$server['mode'] = "p2p_tls";
1597
				} else {
1598
					$server['mode'] = "server_tls";
1599
				}
1600
			} else {
1601
				$server['mode'] = "p2p_shared_key";
1602
			}
1603
			unset($server['auth_method']);
1604

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

    
1626
			$tmparr = explode(";", $server['dhcp_dns'], 4);
1627
			$d=1;
1628
			foreach ($tmparr as $tmpa) {
1629
				$server["dns_server{$d}"] = $tmpa;
1630
				$d++;
1631
			}
1632
			unset($server['dhcp_dns']);
1633

    
1634
			$tmparr = explode(";", $server['dhcp_ntp'], 2);
1635
			$d=1;
1636
			foreach ($tmparr as $tmpa) {
1637
				$server["ntp_server{$d}"] = $tmpa;
1638
				$d++;
1639
			}
1640
			unset($server['dhcp_ntp']);
1641

    
1642
			if ($server['dhcp_nbtdisable']) {
1643
				$server['netbios_enable'] = false;
1644
			} else {
1645
				$server['netbios_enable'] = "yes";
1646
			}
1647
			unset($server['dhcp_nbtdisable']);
1648
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1649
			unset($server['dhcp_nbttype']);
1650
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1651
			unset($server['dhcp_nbtscope']);
1652

    
1653
			$tmparr = explode(";", $server['dhcp_nbdd'], 2);
1654
			$d=1;
1655
			foreach ($tmparr as $tmpa) {
1656
				$server["nbdd_server{$d}"] = $tmpa;
1657
				$d++;
1658
			}
1659
			unset($server['dhcp_nbdd']);
1660

    
1661
			$tmparr = explode(";", $server['dhcp_wins'], 2);
1662
			$d=1;
1663
			foreach ($tmparr as $tmpa) {
1664
				$server["wins_server{$d}"] = $tmpa;
1665
				$d++;
1666
			}
1667
			unset($server['dhcp_wins']);
1668

    
1669
			if (!empty($server['disable'])) {
1670
				$server['disable'] = true;
1671
			} else {
1672
				unset($server['disable']);
1673
			}
1674

    
1675
			/* allocate vpnid */
1676
			$server['vpnid'] = $vpnid++;
1677

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

    
1706
			$config['openvpn']['openvpn-server'][] = $server;
1707
		}
1708
		unset($config['installedpackages']['openvpnserver']);
1709
	}
1710

    
1711
	/* openvpn client configurations */
1712
	if (is_array($config['installedpackages']['openvpnclient'])) {
1713
		$config['openvpn']['openvpn-client'] = array();
1714

    
1715
		$index = 1;
1716
		foreach ($config['installedpackages']['openvpnclient']['config'] as $client) {
1717

    
1718
			if (!is_array($client)) {
1719
				continue;
1720
			}
1721

    
1722
			if ($client['auth_method'] == "pki") {
1723

    
1724
				/* create ca entry */
1725
				$ca = array();
1726
				$ca['refid'] = uniqid();
1727
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1728
				$ca['crt'] = $client['ca_cert'];
1729
				$ca['crl'] = $client['crl'];
1730
				$config['ca'][] = $ca;
1731

    
1732
				/* create ca reference */
1733
				unset($client['ca_cert']);
1734
				unset($client['crl']);
1735
				$client['caref'] = $ca['refid'];
1736

    
1737
				/* create cert entry */
1738
				$cert = array();
1739
				$cert['refid'] = uniqid();
1740
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1741
				$cert['crt'] = $client['client_cert'];
1742
				$cert['prv'] = $client['client_key'];
1743
				$config['cert'][] = $cert;
1744

    
1745
				/* create cert reference */
1746
				unset($client['client_cert']);
1747
				unset($client['client_key']);
1748
				$client['certref'] = $cert['refid'];
1749

    
1750
				$index++;
1751
			}
1752

    
1753
			/* determine operational mode */
1754
			if ($client['auth_method'] == 'pki') {
1755
				$client['mode'] = "p2p_tls";
1756
			} else {
1757
				$client['mode'] = "p2p_shared_key";
1758
			}
1759
			unset($client['auth_method']);
1760

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

    
1780
			/* allocate vpnid */
1781
			$client['vpnid'] = $vpnid++;
1782

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

    
1811
			if (!empty($client['disable'])) {
1812
				$client['disable'] = true;
1813
			} else {
1814
				unset($client['disable']);
1815
			}
1816

    
1817
			$config['openvpn']['openvpn-client'][] = $client;
1818
		}
1819

    
1820
		unset($config['installedpackages']['openvpnclient']);
1821
	}
1822

    
1823
	/* openvpn client specific configurations */
1824
	if (is_array($config['installedpackages']['openvpncsc'])) {
1825
		$config['openvpn']['openvpn-csc'] = array();
1826

    
1827
		foreach ($config['installedpackages']['openvpncsc']['config'] as $csc) {
1828

    
1829
			if (!is_array($csc)) {
1830
				continue;
1831
			}
1832

    
1833
			/* modify configuration values */
1834
			$csc['common_name'] = $csc['commonname'];
1835
			unset($csc['commonname']);
1836
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1837
			unset($csc['ifconfig_push']);
1838
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1839
			unset($csc['dhcp_domainname']);
1840

    
1841
			$tmparr = explode(";", $csc['dhcp_dns'], 4);
1842
			$d=1;
1843
			foreach ($tmparr as $tmpa) {
1844
				$csc["dns_server{$d}"] = $tmpa;
1845
				$d++;
1846
			}
1847
			unset($csc['dhcp_dns']);
1848

    
1849
			$tmparr = explode(";", $csc['dhcp_ntp'], 2);
1850
			$d=1;
1851
			foreach ($tmparr as $tmpa) {
1852
				$csc["ntp_server{$d}"] = $tmpa;
1853
				$d++;
1854
			}
1855
			unset($csc['dhcp_ntp']);
1856

    
1857
			if ($csc['dhcp_nbtdisable']) {
1858
				$csc['netbios_enable'] = false;
1859
			} else {
1860
				$csc['netbios_enable'] = "yes";
1861
			}
1862
			unset($csc['dhcp_nbtdisable']);
1863
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1864
			unset($csc['dhcp_nbttype']);
1865
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1866
			unset($csc['dhcp_nbtscope']);
1867

    
1868
			$tmparr = explode(";", $csc['dhcp_nbdd'], 2);
1869
			$d=1;
1870
			foreach ($tmparr as $tmpa) {
1871
				$csc["nbdd_server{$d}"] = $tmpa;
1872
				$d++;
1873
			}
1874
			unset($csc['dhcp_nbdd']);
1875

    
1876
			$tmparr = explode(";", $csc['dhcp_wins'], 2);
1877
			$d=1;
1878
			foreach ($tmparr as $tmpa) {
1879
				$csc["wins_server{$d}"] = $tmpa;
1880
				$d++;
1881
			}
1882
			unset($csc['dhcp_wins']);
1883

    
1884
			if (!empty($csc['disable'])) {
1885
				$csc['disable'] = true;
1886
			} else {
1887
				unset($csc['disable']);
1888
			}
1889

    
1890
			$config['openvpn']['openvpn-csc'][] = $csc;
1891
		}
1892

    
1893
		unset($config['installedpackages']['openvpncsc']);
1894
	}
1895

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

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

    
1921
}
1922

    
1923

    
1924
function upgrade_052_to_053() {
1925
	global $config;
1926
	if (!is_array($config['ca'])) {
1927
		$config['ca'] = array();
1928
	}
1929
	if (!is_array($config['cert'])) {
1930
		$config['cert'] = array();
1931
	}
1932

    
1933
	/* migrate advanced admin page webui ssl to certificate manager */
1934
	if ($config['system']['webgui']['certificate'] &&
1935
	    $config['system']['webgui']['private-key']) {
1936

    
1937
		/* create cert entry */
1938
		$cert = array();
1939
		$cert['refid'] = uniqid();
1940
		$cert['descr'] = "webConfigurator SSL Certificate";
1941
		$cert['crt'] = $config['system']['webgui']['certificate'];
1942
		$cert['prv'] = $config['system']['webgui']['private-key'];
1943
		$config['cert'][] = $cert;
1944

    
1945
		/* create cert reference */
1946
		unset($config['system']['webgui']['certificate']);
1947
		unset($config['system']['webgui']['private-key']);
1948
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1949
	}
1950

    
1951
	/* migrate advanced admin page ssh keys to user manager */
1952
	if ($config['system']['ssh']['authorizedkeys']) {
1953
		$admin_user =& getUserEntryByUID(0);
1954
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1955
		unset($config['system']['ssh']['authorizedkeys']);
1956
	}
1957
}
1958

    
1959

    
1960
function upgrade_053_to_054() {
1961
	global $config;
1962
	if (is_array($config['load_balancer']['lbpool'])) {
1963
		$lbpool_arr = $config['load_balancer']['lbpool'];
1964
		$lbpool_srv_arr = array();
1965
		$gateway_group_arr = array();
1966
		$gateways = return_gateways_array();
1967
		$group_name_changes = array();
1968
		if (! is_array($config['gateways']['gateway_item'])) {
1969
			$config['gateways']['gateway_item'] = array();
1970
		}
1971

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

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

    
2047

    
2048
function upgrade_054_to_055() {
2049
	global $config;
2050
	global $g;
2051

    
2052
	/* RRD files changed for quality, traffic and packets graphs */
2053
	//ini_set("max_execution_time", "1800");
2054
	/* convert traffic RRD file */
2055
	global $parsedcfg, $listtags;
2056
	$listtags = array("ds", "v", "rra", "row");
2057

    
2058
	$rrddbpath = "/var/db/rrd/";
2059
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2060
	if ($g['platform'] != "pfSense") {
2061
		/* restore the databases, if we have one */
2062
		if (restore_rrd()) {
2063
			/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
2064
			@rename("{$g['cf_conf_path']}/rrd.tgz", "{$g['cf_conf_path']}/backup/rrd.tgz");
2065
		}
2066
	}
2067

    
2068
	$rrdinterval = 60;
2069
	$valid = $rrdinterval * 2;
2070

    
2071
	/* Asume GigE for now */
2072
	$downstream = 125000000;
2073
	$upstream = 125000000;
2074

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

    
2095
		if (platform_booting()) {
2096
			echo "Migrate RRD database {$database} to new format for IPv6 \n";
2097
		}
2098
		mwexec("$rrdtool tune {$rrddbpath}{$database} -r roundtrip:delay 2>&1");
2099

    
2100
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
2101
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2102
		$rrdold = $rrdold['rrd'];
2103

    
2104
		$i = 0;
2105
		foreach ($rrdold['rra'] as $rra) {
2106
			$l = 0;
2107
			foreach ($rra['database']['row'] as $row) {
2108
				$vnew = divide_delay($row['v'][1]);
2109
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
2110
				$l++;
2111
			}
2112
			$i++;
2113
		}
2114

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

    
2118
		unset($rrdold);
2119
		@unlink("{$g['tmp_path']}/{$xmldump}");
2120
		@unlink("{$g['tmp_path']}/{$xmldumpnew}");
2121
	}
2122
	/* let apinger recreate required files */
2123
	if (!platform_booting()) {
2124
		setup_gateways_monitor();
2125
	}
2126

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

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

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

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

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

    
2161
		$rrdold = xml2array(file_get_contents("{$g['tmp_path']}/{$xmldump}"), 1, "tag");
2162
		$rrdold = $rrdold['rrd'];
2163

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

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

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

    
2200

    
2201
function upgrade_055_to_056() {
2202
	global $config;
2203

    
2204
	if (!is_array($config['ca'])) {
2205
		$config['ca'] = array();
2206
	}
2207
	if (!is_array($config['cert'])) {
2208
		$config['cert'] = array();
2209
	}
2210

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

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

    
2254

    
2255
function upgrade_056_to_057() {
2256
	global $config;
2257

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

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

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

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

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

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

    
2343
function upgrade_060_to_061() {
2344
	global $config;
2345

    
2346
	if (is_array($config['interfaces']['wan'])) {
2347
		$config['interfaces']['wan']['enable'] = true;
2348
	}
2349
	if (is_array($config['interfaces']['lan'])) {
2350
		$config['interfaces']['lan']['enable'] = true;
2351
	}
2352

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

    
2365
function upgrade_061_to_062() {
2366
	global $config;
2367

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

    
2372
		foreach ($a_nat as &$natent) {
2373
			$natent['disabled'] = false;
2374
			$natent['nordr']    = false;
2375

    
2376
			$natent['source'] = array(
2377
				"not"     => false,
2378
				"any"     => true,
2379
				"port"    => ""
2380
			);
2381

    
2382
			$natent['destination'] = array(
2383
				"not"     => false,
2384
				"address" => $natent['external-address'],
2385
				"port"    => $natent['external-port']
2386
			);
2387

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

    
2396
			unset($natent['external-address']);
2397
			unset($natent['external-port']);
2398
		}
2399

    
2400
		unset($natent);
2401
	}
2402
}
2403

    
2404
function upgrade_062_to_063() {
2405
	/* Upgrade legacy Themes to the new pfsense_ng */
2406
	global $config;
2407

    
2408
	switch ($config['theme']) {
2409
		case "nervecenter":
2410
			$config['theme'] = "pfsense_ng";
2411
			break;
2412
	}
2413

    
2414
}
2415

    
2416
function upgrade_063_to_064() {
2417
	global $config;
2418
	$j=0;
2419
	$ifcfg = &$config['interfaces'];
2420

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

    
2439
	if (!is_array($config['ppps']['ppp'])) {
2440
		$config['ppps']['ppp'] = array();
2441
	}
2442
	$a_ppps = &$config['ppps']['ppp'];
2443

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

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

    
2494
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2495
			$j++;
2496

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

    
2511
			$a_ppps[] = $ppp;
2512

    
2513
		}
2514
	}
2515
}
2516

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

    
2524
function upgrade_065_to_066() {
2525
	global $config;
2526

    
2527
	$dhcrelaycfg =& $config['dhcrelay'];
2528

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3056
	if (!is_array($config['filter']) || !is_array($config['filter']['rule'])) {
3057
		return;
3058
	}
3059
	if (!is_array($config['dnshaper']) || !is_array($config['dnshaper']['queue'])) {
3060
		return;
3061
	}
3062

    
3063
	$dnqueue_number = 1;
3064
	$dnpipe_number = 1;
3065

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

    
3077
	unset($dnqueue_number, $dnpipe_number, $qidx, $idx, $dnpipe, $dnqueue);
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 ($g['platform'] != "pfSense") {
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 NO_REMOUNT=1 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
	global $config, $g;
3303
	/* Disable kill_states by default */
3304
	$config['system']['kill_states'] = true;
3305
}
3306

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

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

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

    
3322
function upgrade_099_to_100() {
3323
	require_once("/etc/inc/services.inc");
3324
	install_cron_job("/usr/bin/nice -n20 newsyslog", false);
3325
}
3326

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

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

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

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

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

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

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

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

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

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

    
3393
	unset($config['nat']['ipsecpassthru']);
3394
	unset($config['nat']['advancedoutbound']);
3395
}
3396

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

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

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

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

    
3431
	/* sync all local account information */
3432
	local_sync_accounts();
3433
}
3434

    
3435
function upgrade_104_to_105() {
3436
	global $config;
3437

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

    
3452
function upgrade_105_to_106() {
3453

    
3454
	/* NOTE: This entry can be reused for something else since the upgrade code was reverted */
3455
}
3456

    
3457
function upgrade_106_to_107() {
3458
	global $config;
3459

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

    
3482
function upgrade_107_to_108() {
3483
	global $config;
3484

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

    
3492
function upgrade_108_to_109() {
3493
	global $config;
3494

    
3495
	if (!isset($config['filter']['rule']) || !is_array($config['filter']['rule'])) {
3496
		return;
3497
	}
3498

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

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

    
3512
function upgrade_109_to_110() {
3513
	global $config;
3514

    
3515
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3516
		return;
3517
	}
3518

    
3519
	foreach ($config['ipsec']['phase2'] as &$rule) {
3520
		if (!empty($rule['uniqid'])) {
3521
			continue;
3522
		}
3523

    
3524
		$rule['uniqid'] = uniqid();
3525
	}
3526
}
3527

    
3528
function upgrade_110_to_111() {
3529
	global $config;
3530

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

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

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

    
3551
			unset($config['installedpackages']['menu'][$idx]);
3552
			break;
3553
		}
3554
	}
3555

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

    
3566
	if (!isset($config['installedpackages']['unbound']['config'][0])) {
3567
		return;
3568
	}
3569

    
3570
	$pkg = $config['installedpackages']['unbound']['config'][0];
3571

    
3572
	if (isset($config['installedpackages']['unboundadvanced']['config'][0])) {
3573
		$pkg = array_merge($pkg, $config['installedpackages']['unboundadvanced']['config'][0]);
3574
	}
3575

    
3576
	$new = array();
3577

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

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

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

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

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

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

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

    
3654
	$config['unbound'] = $new;
3655

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

    
3666
	unset($pkg, $new);
3667
}
3668

    
3669
function upgrade_111_to_112() {
3670
	global $config;
3671

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

    
3683
function upgrade_112_to_113() {
3684
	global $config;
3685

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

    
3693
	if (isset($config['notifications']['smtp']['tls']) &&
3694
	    $config['notifications']['smtp']['tls'] == "checked") {
3695
		$config['notifications']['smtp']['tls'] = true;
3696
	} else {
3697
		unset($config['notifications']['smtp']['tls']);
3698
	}
3699
}
3700

    
3701
function upgrade_113_to_114() {
3702
	global $config;
3703

    
3704
	if (!isset($config['ipsec']['phase1']) ||
3705
	    !is_array($config['ipsec']['phase1'])) {
3706
		return;
3707
	}
3708

    
3709
	foreach ($config['ipsec']['phase1'] as &$ph1ent) {
3710
		if (!isset($ph1ent['iketype'])) {
3711
			$ph1ent['iketype'] = 'ikev1';
3712
		}
3713
	}
3714
}
3715

    
3716
function upgrade_114_to_115() {
3717
	global $config;
3718

    
3719
	if (isset($config['unbound']['custom_options'])) {
3720
		$config['unbound']['custom_options'] = base64_encode($config['unbound']['custom_options']);
3721
	}
3722
}
3723

    
3724
function upgrade_115_to_116() {
3725
	global $config;
3726

    
3727
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
3728
		return;
3729
	}
3730

    
3731
	$keyid = 1;
3732
	foreach ($config['ipsec']['phase2'] as $idx => $ph2) {
3733
		$config['ipsec']['phase2'][$idx]['reqid'] = $keyid;
3734
		$keyid++;
3735
	}
3736
}
3737

    
3738
function upgrade_116_to_117() {
3739
	global $config;
3740

    
3741
	if (!isset($config['ipsec']['client']) ||
3742
	    !isset($config['ipsec']['client']['dns_split']) ||
3743
	    empty($config['ipsec']['client']['dns_split'])) {
3744
		return;
3745
	}
3746

    
3747
	$config['ipsec']['client']['dns_split'] =
3748
		preg_replace('/\s*,\s*/', ' ', trim($config['ipsec']['client']['dns_split']));
3749
}
3750

    
3751
function upgrade_117_to_118() {
3752
	global $config;
3753

    
3754
	if (!isset($config['installedpackages']['miniupnpd']['config'][0])) {
3755
		return;
3756
	}
3757

    
3758
	$miniupnpd =& $config['installedpackages']['miniupnpd']['config'][0];
3759

    
3760
	$miniupnpd['row'] = array();
3761

    
3762
	for ($i = 1; $i <= 4; $i++) {
3763
		if (isset($miniupnpd["permuser{$i}"]) && !empty($miniupnpd["permuser{$i}"])) {
3764
			$miniupnpd['row'][] = array('permuser' => $miniupnpd["permuser{$i}"]);
3765
		}
3766
		unset($miniupnpd["permuser{$i}"]);
3767
	}
3768
}
3769
?>
3770

    
(55-55/68)