Projet

Général

Profil

Télécharger (96,8 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / upgrade_config.inc @ 0ee60267

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
192

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

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

    
204

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

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

    
223

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

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

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

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

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

    
250

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

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

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

    
271

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

    
286

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

    
299

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

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

    
353

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

    
359

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

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

    
383

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

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

    
398

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

    
403

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

    
410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
491

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

    
496

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

    
501

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

    
515

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

    
522

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

    
528

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

    
534

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

    
540

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

    
546

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

    
552

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

    
558

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

    
564

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

    
570

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

    
576

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

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

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

    
601
		$config['system']['user'][0]['priv'] = array();
602
		$config['system']['user'][0]['priv'][0]['id'] = "lockwc";
603
		$config['system']['user'][0]['priv'][0]['name'] = "Lock webConfigurator";
604
		$config['system']['user'][0]['priv'][0]['descr'] = gettext("Indicates whether this user will lock access to the webConfigurator for other users.");
605
		$config['system']['user'][0]['priv'][1]['id'] = "lock-ipages";
606
		$config['system']['user'][0]['priv'][1]['name'] = "Lock individual pages";
607
		$config['system']['user'][0]['priv'][1]['descr'] = gettext("Indicates whether this user will lock individual HTML pages after having accessed a particular page (the lock will be freed if the user leaves or saves the page form).");
608
		$config['system']['user'][0]['priv'][2]['id'] = "hasshell";
609
		$config['system']['user'][0]['priv'][2]['name'] = "Has shell access";
610
		$config['system']['user'][0]['priv'][2]['descr'] = gettext("Indicates whether this user is able to login for example via SSH.");
611
		$config['system']['user'][0]['priv'][3]['id'] = "copyfiles";
612
		$config['system']['user'][0]['priv'][3]['name'] = "Is allowed to copy files";
613
		$config['system']['user'][0]['priv'][3]['descr'] = sprintf(gettext("Indicates whether this user is allowed to copy files onto the %s appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly)."), $g['product_name']);
614
		$config['system']['user'][0]['priv'][4]['id'] = "isroot";
615
		$config['system']['user'][0]['priv'][4]['name'] = "Is root user";
616
		$config['system']['user'][0]['priv'][4]['descr'] = gettext("This user is associated with the UNIX root user (you should associate this privilege only with one single user).");
617

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
716
	}
717
}
718

    
719

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

    
728

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

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

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

    
781

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

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

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

    
822

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

    
843

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

    
927

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

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

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

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

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

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

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

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

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

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

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

    
964
				/* build new phase1 entry */
965

    
966
				$ph1ent = array();
967

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

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

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

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

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

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

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

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

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

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

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

    
1054
			/* build new phase2 entry */
1055

    
1056
			$ph2ent = array();
1057

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1274

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

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

    
1336

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

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

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

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

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

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

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

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

    
1397

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

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

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

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

    
1443

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

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

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

    
1484

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

    
1493
	$vpnid = 1;
1494

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

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

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

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

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

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

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

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

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

    
1545
				$index++;
1546
			}
1547

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1699
				$index++;
1700
			}
1701

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1861
}
1862

    
1863

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

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

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

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

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

    
1897

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

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

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

    
1977

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2117

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

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

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

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

    
2164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2315
}
2316

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

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

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

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

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

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

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

    
2402
			$a_ppps[] = $ppp;
2403

    
2404
		}
2405
	}
2406
}
2407

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2740
		$cdp_search = "<\/cdp_prep>";
2741
		$cdp_replace = "</cdp_prep>";
2742
		$cdp_arr = array();
2743
		$cdp_arr[] = "			<ds>
2744
					<primary_value> NaN </primary_value>
2745
					<secondary_value> 0.0000000000e+00 </secondary_value>
2746
					<value> NaN </value>
2747
					<unknown_datapoints> 0 </unknown_datapoints>
2748
					</ds>
2749
		";
2750
		$cdp_arr[] = "			<ds>
2751
					<primary_value> NaN </primary_value>
2752
					<secondary_value> 0.0000000000e+00 </secondary_value>
2753
					<value> NaN </value>
2754
					<unknown_datapoints> 0 </unknown_datapoints>
2755
					</ds>
2756
		";
2757
		$cdp_arr[] = "			<ds>
2758
					<primary_value> NaN </primary_value>
2759
					<secondary_value> 0.0000000000e+00 </secondary_value>
2760
					<value> NaN </value>
2761
					<unknown_datapoints> 0 </unknown_datapoints>
2762
					</ds>
2763
		";
2764
		$cdp_arr[] = "			<ds>
2765
					<primary_value> NaN </primary_value>
2766
					<secondary_value> 0.0000000000e+00 </secondary_value>
2767
					<value> NaN </value>
2768
					<unknown_datapoints> 0 </unknown_datapoints>
2769
					</ds>
2770
		";
2771

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3011
			}
3012
		}
3013
	}
3014
}
3015

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

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

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

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

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

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

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

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

    
3073
	enable_rrd_graphing();
3074
}
3075

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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