Project

General

Profile

Download (79.9 KB) Statistics
| Branch: | Tag: | Revision:
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
			echo "\nWarning: filter rule removed " .
114
				"(interface '{$fr['interface']}' does not exist anymore).";
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
				echo "\nWarning: filter rule removed " .
126
					"(source network '{$fr['source']['network']}' does not exist anymore).";
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
				echo "\nWarning: filter rule removed " .
139
					"(destination network '{$fr['destination']['network']}' does not exist anymore).";
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
			echo "\nWarning: traffic shaper rule removed " .
159
				"(interface '{$fr['interface']}' does not exist anymore).";
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
				echo "\nWarning: traffic shaper rule removed " .
171
					"(source network '{$fr['source']['network']}' does not exist anymore).";
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
				echo "\nWarning: traffic shaper rule removed " .
184
					"(destination network '{$fr['destination']['network']}' does not exist anymore).";
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'] = "CARP vhid {$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'] = "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;
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'] = "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'] = "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'] = "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'] = "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'] = "Indicates whether this user is allowed to copy files onto the {$g['product_name']} appliance via SCP/SFTP. If you are going to use this privilege, you must install scponly on the appliance (Hint: pkg_add -r scponly).";
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'] = "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] =    "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'] = "Interface $ifname Static Gateway";			
743
		} else {
744
			$config['gateways']['gateway_item'][$i]['gateway'] = "dynamic";
745
			$config['gateways']['gateway_item'][$i]['descr'] = "Interface $ifname Dynamic Gateway";			
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
			if ($found == false) {
805
				$gateway = array();
806
				$gateway['name'] = "SROUTE{$i}";
807
				$gwmap[$sroute['gateway']] = $gateway['name'];
808
				$gateway['gateway'] = $sroute['gateway'];
809
				$gateway['interface'] = $sroute['interface'];
810
				$gateway['descr'] = "Upgraded static route for {$sroute['network']}";
811
				if (!is_array($config['gateways']['gateway_item']))
812
					$config['gateways']['gateway_item'] = array();
813
				$config['gateways']['gateway_item'][] = $gateway;
814
				$config['staticroutes']['route'][$idx]['gateway'] = $gateway['name'];
815
				$i++;
816
			}
817
		}
818
	}
819
}
820

    
821

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

    
842

    
843
function upgrade_045_to_046() {
844
	global $config;
845
	/* Upgrade load balancer from slb to relayd */
846
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server'])) {
847
		$vs_a = &$config['load_balancer']['virtual_server'];
848
		$pool_a = &$config['load_balancer']['lbpool'];
849
		$pools = array();
850
		/* Index pools by name */
851
		if(is_array($pool_a)) {
852
			for ($i = 0; isset($pool_a[$i]); $i++) {
853
				if($pool_a[$i]['type'] == "server") {
854
					$pools[$pool_a[$i]['name']] = $pool_a[$i];
855
				}
856
			}
857
		}
858
		/* Convert sitedown entries to pools and re-attach */
859
		for ($i = 0; isset($vs_a[$i]); $i++) {
860
			if (isset($vs_a[$i]['sitedown'])) {
861
				$pool = array();
862
				$pool['type'] = 'server';
863
				$pool['behaviour'] = 'balance';
864
				$pool['name'] = "{$vs_a[$i]['name']}-sitedown";
865
				$pool['descr'] = "Sitedown pool for VS: {$vs_a[$i]['name']}";
866
				$pool['port'] = $pools[$vs_a[$i]['pool']]['port'];
867
				$pool['servers'] = array();
868
				$pool['servers'][] = $vs_a[$i]['sitedown'];
869
				$pool['monitor'] = $pools[$vs_a[$i]['pool']]['monitor'];
870
				$pool_a[] = $pool;
871
				$vs_a[$i]['sitedown'] = $pool['name'];
872
			}
873
		}
874
	}
875
	if(count($config['load_balancer']) == 0) {
876
		unset($config['load_balancer']);
877
	}
878
	mwexec('/usr/sbin/pw groupadd -n _relayd -g 913');
879
	mwexec('/usr/sbin/pw useradd -n _relayd -c "Relay Daemon" -d /var/empty -s /usr/sbin/nologin -u 913 -g 913');
880
}
881

    
882

    
883
function upgrade_046_to_047() {
884
	global $config;
885
	/* Upgrade IPsec from tunnel to phase1/phase2 */
886

    
887
	if(is_array($config['ipsec']['tunnel'])) {
888

    
889
		$a_phase1 = array();
890
		$a_phase2 = array();
891
		$ikeid = 0;
892

    
893
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
894

    
895
			unset($ph1ent);
896
			unset($ph2ent);
897

    
898
			/*
899
				*  attempt to locate an enabled phase1
900
				*  entry that matches the peer gateway
901
				*/
902

    
903
			if (!isset($tunnel['disabled'])) {
904

    
905
				$remote_gateway = $tunnel['remote-gateway'];
906

    
907
				foreach ($a_phase1 as $ph1tmp) {
908
					if ($ph1tmp['remote-gateway'] == $remote_gateway) {
909
						$ph1ent = $ph1tmp;
910
						break;
911
					}
912
				}
913
			}
914

    
915
			/* none found, create a new one */
916

    
917
			if (!isset( $ph1ent )) {
918

    
919
				/* build new phase1 entry */
920

    
921
				$ph1ent = array();
922

    
923
				$ph1ent['ikeid'] = ++$ikeid;
924

    
925
				if (isset($tunnel['disabled']))
926
					$ph1ent['disabled'] = $tunnel['disabled'];
927

    
928
				/* convert to the new vip[$vhid] name */
929
				if(preg_match("/^carp/", $tunnel['interface'])) {
930
					$carpid = str_replace("carp", "", $tunnel['interface']);
931
					$tunnel['interface'] = "vip" . $config['virtualip']['vip'][$carpid]['vhid'];
932
				}
933
				$ph1ent['interface'] = $tunnel['interface'];
934
				$ph1ent['remote-gateway'] = $tunnel['remote-gateway'];
935
				$ph1ent['descr'] = $tunnel['descr'];
936

    
937
				$ph1ent['mode'] = $tunnel['p1']['mode'];
938

    
939
				if (isset($tunnel['p1']['myident']['myaddress']))
940
					$ph1ent['myid_type'] = "myaddress";
941
				if (isset($tunnel['p1']['myident']['address'])) {
942
					$ph1ent['myid_type'] = "address";
943
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['address'];
944
				}
945
				if (isset($tunnel['p1']['myident']['fqdn'])) {
946
					$ph1ent['myid_type'] = "fqdn";
947
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['fqdn'];
948
				}
949
				if (isset($tunnel['p1']['myident']['ufqdn'])) {
950
					$ph1ent['myid_type'] = "user_fqdn";
951
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['ufqdn'];
952
				}
953
				if (isset($tunnel['p1']['myident']['asn1dn'])) {
954
					$ph1ent['myid_type'] = "asn1dn";
955
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['asn1dn'];
956
				}
957
				if (isset($tunnel['p1']['myident']['dyn_dns'])) {
958
					$ph1ent['myid_type'] = "dyn_dns";
959
					$ph1ent['myid_data'] = $tunnel['p1']['myident']['dyn_dns'];
960
				}
961

    
962
				$ph1ent['peerid_type'] = "peeraddress";
963

    
964
				switch ($tunnel['p1']['encryption-algorithm']) {
965
					case "des":
966
					$ph1alg = array( 'name' => 'des' );
967
					break;
968
					case "3des":
969
					$ph1alg = array( 'name' => '3des' );
970
					break;
971
					case "blowfish":
972
					$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
973
					break;
974
					case "cast128":
975
					$ph1alg = array( 'name' => 'cast128' );
976
					break;
977
					case "rijndael":
978
					$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
979
					break;
980
					case "rijndael 256":
981
					case "aes 256":
982
					$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
983
					break;
984
				}
985

    
986
				$ph1ent['encryption-algorithm'] = $ph1alg;
987
				$ph1ent['hash-algorithm'] = $tunnel['p1']['hash-algorithm'];
988
				$ph1ent['dhgroup'] = $tunnel['p1']['dhgroup'];
989
				$ph1ent['lifetime'] = $tunnel['p1']['lifetime'];
990
				$ph1ent['authentication_method'] = $tunnel['p1']['authentication_method'];
991

    
992
				if (isset($tunnel['p1']['pre-shared-key']))
993
					$ph1ent['pre-shared-key'] = $tunnel['p1']['pre-shared-key'];
994
				if (isset($tunnel['p1']['cert']))
995
					$ph1ent['cert'] = $tunnel['p1']['cert'];
996
				if (isset($tunnel['p1']['peercert']))
997
					$ph1ent['peercert'] = $tunnel['p1']['peercert'];
998
				if (isset($tunnel['p1']['private-key']))
999
					$ph1ent['private-key'] = $tunnel['p1']['private-key'];
1000

    
1001
				$ph1ent['nat_traversal'] = "on";
1002
				$ph1ent['dpd_enable'] = 1;
1003
				$ph1ent['dpd_delay'] = 10;
1004
				$ph1ent['dpd_maxfail'] = 5;
1005

    
1006
				$a_phase1[] = $ph1ent;
1007
			}
1008

    
1009
			/* build new phase2 entry */
1010

    
1011
			$ph2ent = array();
1012

    
1013
			$ph2ent['ikeid'] = $ph1ent['ikeid'];
1014

    
1015
			if (isset($tunnel['disabled']))
1016
				$ph1ent['disabled'] = $tunnel['disabled'];
1017

    
1018
			$ph2ent['descr'] = "phase2 for ".$tunnel['descr'];
1019

    
1020
			$type = "lan";
1021
			if ($tunnel['local-subnet']['network'])
1022
				$type = $tunnel['local-subnet']['network'];
1023
			if ($tunnel['local-subnet']['address']) {
1024
				list($address,$netbits) = explode("/",$tunnel['local-subnet']['address']);
1025
				if (is_null($netbits))
1026
					$type = "address";
1027
				else
1028
					$type = "network";
1029
			}
1030

    
1031
			switch ($type) {
1032
				case "address":
1033
				$ph2ent['localid'] = array('type' => $type,'address' => $address);
1034
				break;
1035
				case "network":
1036
				$ph2ent['localid'] = array('type' => $type,'address' => $address,'netbits' => $netbits);
1037
				break;
1038
				default:
1039
				$ph2ent['localid'] = array('type' => $type);
1040
				break;
1041
			}
1042

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

    
1046
			$ph2ent['protocol'] = $tunnel['p2']['protocol'];
1047

    
1048
			$aes_count = 0;
1049
			foreach( $tunnel['p2']['encryption-algorithm-option'] as $tunalg ) {
1050
				$aes_found = false;
1051
				switch ($tunalg) {
1052
					case "des":
1053
					$ph2alg = array( 'name' => 'des' );
1054
					break;
1055
					case "3des":
1056
					$ph2alg = array( 'name' => '3des' );
1057
					break;
1058
					case "blowfish":
1059
					$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1060
					break;
1061
					case "cast128":
1062
					$ph2alg = array( 'name' => 'cast128' );
1063
					break;
1064
					case "rijndael":
1065
					case "rijndael 256":
1066
					case "aes 256":
1067
					$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1068
					$aes_found = true;
1069
					$aes_count++;
1070
					break;
1071
				}
1072

    
1073
				if( !$aes_found || ($aes_count < 2))
1074
					$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1075
			}
1076

    
1077
			$ph2ent['hash-algorithm-option'] = $tunnel['p2']['hash-algorithm-option'];
1078
			$ph2ent['pfsgroup'] = $tunnel['p2']['pfsgroup'];
1079
			$ph2ent['lifetime'] = $tunnel['p2']['lifetime'];
1080

    
1081
			if (isset($tunnel['pinghost']['pinghost']))
1082
				$ph2ent['pinghost'] = $tunnel['pinghost'];
1083

    
1084
			$a_phase2[] = $ph2ent;
1085
		}
1086

    
1087
		unset($config['ipsec']['tunnel']);
1088
		$config['ipsec']['phase1'] = $a_phase1;
1089
		$config['ipsec']['phase2'] = $a_phase2;
1090
	}
1091

    
1092
	/* Upgrade Mobile IPsec */
1093
	if (isset($config['ipsec']['mobileclients'])
1094
		&& is_array($config['ipsec']['mobileclients'])
1095
		&& is_array($config['ipsec']['mobileclients']['p1'])
1096
		&& is_array($config['ipsec']['mobileclients']['p2'])) {
1097

    
1098
		if (isset($config['ipsec']['mobileclients']['enable'])) {
1099
			$config['ipsec']['client']['enable'] = true;
1100
			$config['ipsec']['client']['user_source'] = 'system';
1101
			$config['ipsec']['client']['group_source'] = 'system';
1102
		}
1103

    
1104
		$mobilecfg = $config['ipsec']['mobileclients'];
1105

    
1106
		$ph1ent = array();
1107
		$ph1ent['ikeid'] = ++$ikeid;
1108

    
1109
		if (!isset($mobilecfg['enable']))
1110
			$ph1ent['disabled'] = true;
1111

    
1112
		/* Assume WAN since mobile tunnels couldn't be on a separate interface on 1.2.x */
1113
		$ph1ent['interface'] = 'wan';
1114
		$ph1ent['descr'] = "Mobile Clients (upgraded)";
1115
		$ph1ent['mode'] = $mobilecfg['p1']['mode'];
1116

    
1117
		if (isset($mobilecfg['p1']['myident']['myaddress']))
1118
			$ph1ent['myid_type'] = "myaddress";
1119
		if (isset($mobilecfg['p1']['myident']['address'])) {
1120
			$ph1ent['myid_type'] = "address";
1121
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['address'];
1122
		}
1123
		if (isset($mobilecfg['p1']['myident']['fqdn'])) {
1124
			$ph1ent['myid_type'] = "fqdn";
1125
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['fqdn'];
1126
		}
1127
		if (isset($mobilecfg['p1']['myident']['ufqdn'])) {
1128
			$ph1ent['myid_type'] = "user_fqdn";
1129
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['ufqdn'];
1130
		}
1131
		if (isset($mobilecfg['p1']['myident']['asn1dn'])) {
1132
			$ph1ent['myid_type'] = "asn1dn";
1133
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['asn1dn'];
1134
		}
1135
		if (isset($mobilecfg['p1']['myident']['dyn_dns'])) {
1136
			$ph1ent['myid_type'] = "dyn_dns";
1137
			$ph1ent['myid_data'] = $mobilecfg['p1']['myident']['dyn_dns'];
1138
		}
1139
		$ph1ent['peerid_type'] = "fqdn";
1140
		$ph1ent['peerid_data'] = "";
1141

    
1142
		switch ($mobilecfg['p1']['encryption-algorithm']) {
1143
			case "des":
1144
			$ph1alg = array( 'name' => 'des' );
1145
			break;
1146
			case "3des":
1147
			$ph1alg = array( 'name' => '3des' );
1148
			break;
1149
			case "blowfish":
1150
			$ph1alg = array( 'name' => 'blowfish', 'keylen' => '128'  );
1151
			break;
1152
			case "cast128":
1153
			$ph1alg = array( 'name' => 'cast128' );
1154
			break;
1155
			case "rijndael":
1156
			$ph1alg = array( 'name' => 'aes', 'keylen' => '128' );
1157
			break;
1158
			case "rijndael 256":
1159
			case "aes 256":
1160
			$ph1alg = array( 'name' => 'aes', 'keylen' => '256' );
1161
			break;
1162
		}
1163

    
1164
		$ph1ent['encryption-algorithm'] = $ph1alg;
1165
		$ph1ent['hash-algorithm'] = $mobilecfg['p1']['hash-algorithm'];
1166
		$ph1ent['dhgroup'] = $mobilecfg['p1']['dhgroup'];
1167
		$ph1ent['lifetime'] = $mobilecfg['p1']['lifetime'];
1168
		$ph1ent['authentication_method'] = $mobilecfg['p1']['authentication_method'];
1169

    
1170
		if (isset($mobilecfg['p1']['cert']))
1171
			$ph1ent['cert'] = $mobilecfg['p1']['cert'];
1172
		if (isset($mobilecfg['p1']['peercert']))
1173
			$ph1ent['peercert'] = $mobilecfg['p1']['peercert'];
1174
		if (isset($mobilecfg['p1']['private-key']))
1175
			$ph1ent['private-key'] = $mobilecfg['p1']['private-key'];
1176

    
1177
		$ph1ent['nat_traversal'] = "on";
1178
		$ph1ent['dpd_enable'] = 1;
1179
		$ph1ent['dpd_delay'] = 10;
1180
		$ph1ent['dpd_maxfail'] = 5;
1181
		$ph1ent['mobile'] = true;
1182

    
1183
		$ph2ent = array();
1184
		$ph2ent['ikeid'] = $ph1ent['ikeid'];
1185
		$ph2ent['descr'] = "phase2 for ".$mobilecfg['descr'];
1186
		$ph2ent['localid'] = array('type' => 'none');
1187
		$ph2ent['remoteid'] = array('type' => 'mobile');
1188
		$ph2ent['protocol'] = $mobilecfg['p2']['protocol'];
1189

    
1190
		$aes_count = 0;
1191
		foreach( $mobilecfg['p2']['encryption-algorithm-option'] as $tunalg ) {
1192
			$aes_found = false;
1193
			switch ($tunalg) {
1194
				case "des":
1195
				$ph2alg = array( 'name' => 'des' );
1196
				break;
1197
				case "3des":
1198
				$ph2alg = array( 'name' => '3des' );
1199
				break;
1200
				case "blowfish":
1201
				$ph2alg = array( 'name' => 'blowfish', 'keylen' => 'auto'  );
1202
				break;
1203
				case "cast128":
1204
				$ph2alg = array( 'name' => 'cast128' );
1205
				break;
1206
				case "rijndael":
1207
				case "rijndael 256":
1208
				case "aes 256":
1209
				$ph2alg = array( 'name' => 'aes', 'keylen' => 'auto' );
1210
				$aes_found = true;
1211
				$aes_count++;
1212
				break;
1213
			}
1214

    
1215
			if( !$aes_found || ($aes_count < 2))
1216
				$ph2ent['encryption-algorithm-option'][] = $ph2alg;
1217
		}
1218
		$ph2ent['hash-algorithm-option'] = $mobilecfg['p2']['hash-algorithm-option'];
1219
		$ph2ent['pfsgroup'] = $mobilecfg['p2']['pfsgroup'];
1220
		$ph2ent['lifetime'] = $mobilecfg['p2']['lifetime'];
1221
		$ph2ent['mobile'] = true;
1222

    
1223
		$config['ipsec']['phase1'][] = $ph1ent;
1224
		$config['ipsec']['phase2'][] = $ph2ent;
1225
		unset($config['ipsec']['mobileclients']);
1226
	}
1227
}
1228

    
1229

    
1230
function upgrade_047_to_048() {
1231
	global $config;
1232
	if (!empty($config['dyndns'])) {
1233
		$config['dyndnses'] = array();
1234
		$config['dyndnses']['dyndns'] = array();
1235
		if(isset($config['dyndns'][0]['host'])) {
1236
			$tempdyn = array();
1237
			$tempdyn['enable'] = isset($config['dyndns'][0]['enable']);
1238
			$tempdyn['type'] = $config['dyndns'][0]['type'];
1239
			$tempdyn['wildcard'] = isset($config['dyndns'][0]['wildcard']);
1240
			$tempdyn['username'] = $config['dyndns'][0]['username'];
1241
			$tempdyn['password'] = $config['dyndns'][0]['password'];
1242
			$tempdyn['host'] = $config['dyndns'][0]['host'];
1243
			$tempdyn['mx'] = $config['dyndns'][0]['mx'];		
1244
			$tempdyn['interface'] = "wan";
1245
			$tempdyn['descr'] = "Upgraded Dyndns {$tempdyn['type']}";
1246
			$config['dyndnses']['dyndns'][] = $tempdyn;
1247
		}
1248
		unset($config['dyndns']);
1249
	}		
1250
	if (!empty($config['dnsupdate'])) {
1251
		$pconfig = $config['dnsupdate'][0];
1252
		if (!$pconfig['ttl'])
1253
			$pconfig['ttl'] = 60;
1254
		if (!$pconfig['keytype'])
1255
			$pconfig['keytype'] = "zone";
1256
		$pconfig['interface'] = "wan";
1257
		$config['dnsupdates']['dnsupdate'][] = $pconfig;
1258
		unset($config['dnsupdate']);
1259
	}
1260

    
1261
	if (is_array($config['pppoe']) && is_array($config['pppoe'][0])) {
1262
		$pconfig = array();
1263
		$pconfig['username'] = $config['pppoe'][0]['username'];
1264
		$pconfig['password'] = $config['pppoe'][0]['password'];
1265
		$pconfig['provider'] = $config['pppoe'][0]['provider'];
1266
		$pconfig['ondemand'] = isset($config['pppoe'][0]['ondemand']);
1267
		$pconfig['timeout'] = $config['pppoe'][0]['timeout'];
1268
		unset($config['pppoe']);
1269
		$config['interfaces']['wan']['pppoe_username'] = $pconfig['username'];
1270
		$config['interfaces']['wan']['pppoe_password'] = $pconfig['password'];
1271
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1272
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand']);
1273
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1274
	}
1275
	if (is_array($config['pptp'])) {
1276
		$pconfig = array();
1277
		$pconfig['username'] = $config['pptp']['username'];
1278
		$pconfig['password'] = $config['pptp']['password'];
1279
		$pconfig['provider'] = $config['pptp']['provider'];
1280
		$pconfig['ondemand'] = isset($config['pptp']['ondemand']);
1281
		$pconfig['timeout'] = $config['pptp']['timeout'];
1282
		unset($config['pptp']);
1283
		$config['interfaces']['wan']['pptp_username'] = $pconfig['username'];
1284
		$config['interfaces']['wan']['pptp_password'] = $pconfig['password'];
1285
		$config['interfaces']['wan']['provider'] = $pconfig['provider'];
1286
		$config['interfaces']['wan']['ondemand'] = isset($pconfig['ondemand'] );
1287
		$config['interfaces']['wan']['timeout'] = $pconfig['timeout'];
1288
	}
1289
}
1290

    
1291

    
1292
function upgrade_048_to_049() {
1293
	global $config;
1294
	/* setup new all users group */
1295
	$all = array();
1296
	$all['name'] = "all";
1297
	$all['description'] = "All Users";
1298
	$all['scope'] = "system";
1299
	$all['gid'] = 1998;
1300
	$all['member'] = array();
1301

    
1302
	if (!is_array($config['system']['group']))
1303
		$config['system']['group'] = array();
1304

    
1305
	/* work around broken uid assignments */
1306
	$config['system']['nextuid'] = 2000;
1307
	foreach ($config['system']['user'] as & $user) {
1308
		if (isset($user['uid']) && !$user['uid'])
1309
			continue;
1310
		$user['uid'] = $config['system']['nextuid']++;
1311
	}
1312

    
1313
	/* work around broken gid assignments */
1314
	$config['system']['nextgid'] = 2000;
1315
	foreach ($config['system']['group'] as & $group) {
1316
		if ($group['name'] == $g['admin_group'])
1317
			$group['gid'] = 1999;
1318
		else
1319
			$group['gid'] = $config['system']['nextgid']++;
1320
	}
1321

    
1322
	/* build group membership information */
1323
	foreach ($config['system']['group'] as & $group) {
1324
		$group['member'] = array();
1325
		foreach ($config['system']['user'] as & $user) {
1326
			$groupnames = explode(",", $user['groupname']);
1327
			if (in_array($group['name'],$groupnames))
1328
				$group['member'][] = $user['uid'];
1329
		}
1330
	}
1331

    
1332
	/* reset user group information */
1333
	foreach ($config['system']['user'] as & $user) {
1334
		unset($user['groupname']);
1335
		$all['member'][] = $user['uid'];
1336
	}
1337

    
1338
	/* reset group scope information */
1339
	foreach ($config['system']['group'] as & $group)
1340
		if ($group['name'] != $g['admin_group'])
1341
		$group['scope'] = "user";
1342

    
1343
	/* insert new all group */
1344
	$groups = Array();
1345
	$groups[] = $all;
1346
	$groups = array_merge($config['system']['group'],$groups);
1347
	$config['system']['group'] = $groups;
1348
}
1349

    
1350

    
1351
function upgrade_049_to_050() {
1352
	global $config;
1353
	/* update user privileges */
1354
	foreach ($config['system']['user'] as & $user) {
1355
		$privs = array();
1356
		if (!is_array($user['priv'])) {
1357
			unset($user['priv']);
1358
			continue;
1359
		}
1360
		foreach ($user['priv'] as $priv) {
1361
			switch($priv['id']) {
1362
				case "hasshell":
1363
				$privs[] = "user-shell-access";
1364
				break;
1365
				case "copyfiles":
1366
				$privs[] = "user-copy-files";
1367
				break;
1368
			}
1369
		}
1370
		$user['priv'] = $privs;
1371
	}
1372

    
1373
	/* update group privileges */
1374
	foreach ($config['system']['group'] as & $group) {
1375
		$privs = array();
1376
		if (!is_array($group['pages'])) {
1377
			unset($group['pages']);
1378
			continue;
1379
		}
1380
		foreach ($group['pages'] as $page) {
1381
			$priv = map_page_privname($page);
1382
			if ($priv)
1383
				$privs[] = $priv;
1384
		}
1385
		unset($group['pages']);
1386
		$group['priv'] = $privs;
1387
	}
1388

    
1389
	/* sync all local account information */
1390
	local_sync_accounts();
1391
}
1392

    
1393

    
1394
function upgrade_050_to_051() {
1395
	global $config;
1396
	$pconfig = array();
1397
	$pconfig['descr'] = "Set to 0 to disable filtering on the incoming and outgoing member interfaces.";
1398
	$pconfig['tunable'] = "net.link.bridge.pfil_member";
1399
	$pconfig['value'] = "1";
1400
	$config['sysctl']['item'][] = $pconfig;
1401
	$pconfig = array();
1402
	$pconfig['descr'] = "Set to 1 to enable filtering on the bridge interface";
1403
	$pconfig['tunable'] = "net.link.bridge.pfil_bridge";
1404
	$pconfig['value'] = "0";
1405
	$config['sysctl']['item'][] = $pconfig;
1406

    
1407
	unset($config['bridge']);
1408

    
1409
	$convert_bridges = false;
1410
	foreach($config['interfaces'] as $intf) {
1411
		if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1412
			$config['bridges'] = array();
1413
			$config['bridges']['bridged'] = array();
1414
			$convert_bridges = true;
1415
			break;
1416
		}
1417
	}
1418
	if ($convert_bridges == true) {
1419
		$i = 0;
1420
		foreach ($config['interfaces'] as $ifr => &$intf) {
1421
			if (isset($intf['bridge']) && $intf['bridge'] <> "") {
1422
				$nbridge = array();
1423
				$nbridge['members'] = "{$ifr},{$intf['bridge']}";
1424
				$nbridge['descr'] = "Converted bridged {$ifr}";
1425
				$nbridge['bridgeif'] = "bridge{$i}";
1426
				$config['bridges']['bridged'][] = $nbridge;
1427
				unset($intf['bridge']);
1428
				$i++;
1429
			}
1430
		}
1431
	}
1432
}
1433

    
1434

    
1435
function upgrade_051_to_052() {
1436
	global $config;
1437
	$config['openvpn'] = array();
1438
	if (!is_array($config['ca']))
1439
		$config['ca'] = array();
1440
	if (!is_array($config['cert']))
1441
		$config['cert'] = array();
1442

    
1443
	$vpnid = 1;
1444

    
1445
	/* openvpn server configurations */
1446
	if (is_array($config['installedpackages']['openvpnserver'])) {
1447
		$config['openvpn']['openvpn-server'] = array();
1448

    
1449
		$index = 1;
1450
		foreach($config['installedpackages']['openvpnserver']['config'] as $server) {
1451

    
1452
			if (!is_array($server))
1453
				continue;
1454

    
1455
			if ($server['auth_method'] == "pki") {
1456

    
1457
				/* create ca entry */
1458
				$ca = array();
1459
				$ca['refid'] = uniqid();
1460
				$ca['descr'] = "OpenVPN Server CA #{$index}";
1461
				$ca['crt'] = $server['ca_cert'];
1462
				$config['ca'][] = $ca;
1463

    
1464
				/* create ca reference */
1465
				unset($server['ca_cert']);
1466
				$server['caref'] = $ca['refid'];
1467

    
1468
				/* create a crl entry if needed */
1469
				if (!empty($server['crl'][0])) {
1470
					$crl = array();
1471
					$crl['refid'] = uniqid();
1472
					$crl['descr'] = "Imported OpenVPN CRL #{$index}";
1473
					$crl['caref'] = $ca['refid'];
1474
					$crl['text'] = $server['crl'][0];
1475
					if(!is_array($config['crl']))
1476
						$config['crl'] = array();
1477
					$config['crl'][] = $crl;
1478
					$server['crlref'] = $crl['refid'];
1479
				}
1480
				unset($server['crl']);
1481

    
1482
				/* create cert entry */
1483
				$cert = array();
1484
				$cert['refid'] = uniqid();
1485
				$cert['descr'] = "OpenVPN Server Certificate #{$index}";
1486
				$cert['crt'] = $server['server_cert'];
1487
				$cert['prv'] = $server['server_key'];
1488
				$config['cert'][] = $cert;
1489

    
1490
				/* create cert reference */
1491
				unset($server['server_cert']);
1492
				unset($server['server_key']);
1493
				$server['certref'] = $cert['refid'];
1494

    
1495
				$index++;
1496
			}
1497

    
1498
			/* determine operational mode */
1499
			if ($server['auth_method'] == 'pki') {
1500
				if($server['nopool']) {
1501
					$server['mode'] = "p2p_tls";
1502
				} else {
1503
					$server['mode'] = "server_tls";
1504
				}
1505
			} else {
1506
				$server['mode'] = "p2p_shared_key";
1507
			}
1508
			unset($server['auth_method']);
1509

    
1510
			/* modify configuration values */
1511
			$server['dh_length'] = 1024;
1512
			unset($server['dh_params']);
1513
			if (!$server['interface'])
1514
				$server['interface'] = 'wan';
1515
			$server['tunnel_network'] = $server['addresspool'];
1516
			unset($server['addresspool']);
1517
			if (isset($server['use_lzo'])) {
1518
				$server['compression'] = "on";
1519
				unset($server['use_lzo']);
1520
			}
1521
			if ($server['nopool'])
1522
				$server['pool_enable'] = false;
1523
			else
1524
				$server['pool_enable'] = "yes";
1525
			unset($server['nopool']);
1526
			$server['dns_domain'] = $server['dhcp_domainname'];
1527
			unset($server['dhcp_domainname']);
1528
			$server['dns_server1'] = $server['dhcp_dns'];
1529
			unset($server['dhcp_dns']);
1530
			$server['ntp_server1'] = $server['dhcp_ntp'];
1531
			unset($server['dhcp_ntp']);
1532
			if ($server['dhcp_nbtdisable'])
1533
				$server['netbios_enable'] = false;
1534
			else
1535
				$server['netbios_enable'] = "yes";
1536
			unset($server['dhcp_nbtdisable']);
1537
			$server['netbios_ntype'] = $server['dhcp_nbttype'];
1538
			unset($server['dhcp_nbttype']);
1539
			$server['netbios_scope'] = $server['dhcp_nbtscope'];
1540
			unset($server['dhcp_nbtscope']);
1541
			$server['nbdd_server1'] = $server['dhcp_nbdd'];
1542
			unset($server['dhcp_nbdd']);
1543
			$server['wins_server1'] = $server['dhcp_wins'];
1544
			unset($server['dhcp_wins']);
1545

    
1546
			if (!empty($server['disable']))
1547
				$server['disable'] = true;
1548
			else
1549
				unset($server['disable']);
1550

    
1551
			/* allocate vpnid */
1552
			$server['vpnid'] = $vpnid++;
1553

    
1554
			if (!empty($server['custom_options'])) {
1555
				$cstmopts = array();
1556
				$tmpcstmopts = explode(";", $server['custom_options']);
1557
				$assigned = false;
1558
				$tmpstr = "";
1559
				foreach ($tmpcstmopts as $tmpcstmopt) {
1560
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1561
					if (substr($tmpstr,0 ,6) == "devtun") {
1562
						$assigned = true;
1563
						continue;
1564
					} else if (substr($tmpstr, 0, 5) == "local") {
1565
						$localip = substr($tmpstr, 6);
1566
						$server['ipaddr'] = str_replace("\n", "", $localip);
1567
					} else
1568
						$cstmopts[] = $tmpcstmopt;
1569
				}
1570
				$server['custom_options'] = implode(";", $cstmopts);
1571
				if ($assigned == true) {
1572
					$realif = substr($tmpstr, 3);
1573
					foreach ($config['interfaces'] as $iface => $cfgif) {
1574
						if ($cfgif['if'] == $realif) {
1575
							$config['interfaces'][$iface]['if'] = "ovpns{$server['vpnid']}";
1576
							break;
1577
						}
1578
					}
1579
				}
1580
			}
1581

    
1582
			$config['openvpn']['openvpn-server'][] = $server;
1583
		}
1584
		unset($config['installedpackages']['openvpnserver']);
1585
	}
1586

    
1587
	/* openvpn client configurations */
1588
	if (is_array($config['installedpackages']['openvpnclient'])) {
1589
		$config['openvpn']['openvpn-client'] = array();
1590

    
1591
		$index = 1;
1592
		foreach($config['installedpackages']['openvpnclient']['config'] as $client) {
1593

    
1594
			if (!is_array($client))
1595
				continue;
1596

    
1597
			if ($client['auth_method'] == "pki") {
1598

    
1599
				/* create ca entry */
1600
				$ca = array();
1601
				$ca['refid'] = uniqid();
1602
				$ca['descr'] = "OpenVPN Client CA #{$index}";
1603
				$ca['crt'] = $client['ca_cert'];
1604
				$ca['crl'] = $client['crl'];
1605
				$config['ca'][] = $ca;
1606

    
1607
				/* create ca reference */
1608
				unset($client['ca_cert']);
1609
				unset($client['crl']);
1610
				$client['caref'] = $ca['refid'];
1611

    
1612
				/* create cert entry */
1613
				$cert = array();
1614
				$cert['refid'] = uniqid();
1615
				$cert['descr'] = "OpenVPN Client Certificate #{$index}";
1616
				$cert['crt'] = $client['client_cert'];
1617
				$cert['prv'] = $client['client_key'];
1618
				$config['cert'][] = $cert;
1619

    
1620
				/* create cert reference */
1621
				unset($client['client_cert']);
1622
				unset($client['client_key']);
1623
				$client['certref'] = $cert['refid'];
1624

    
1625
				$index++;
1626
			}
1627

    
1628
			/* determine operational mode */
1629
			if ($client['auth_method'] == 'pki')
1630
				$client['mode'] = "p2p_tls";
1631
			else
1632
				$client['mode'] = "p2p_shared_key";
1633
			unset($client['auth_method']);
1634

    
1635
			/* modify configuration values */
1636
			if (!$client['interface'])
1637
				$client['interface'] = 'wan';
1638
			$client['tunnel_network'] = $client['interface_ip'];
1639
			unset($client['interface_ip']);
1640
			$client['server_addr'] = $client['serveraddr'];
1641
			unset($client['serveraddr']);
1642
			$client['server_port'] = $client['serverport'];
1643
			unset($client['serverport']);
1644
			$client['proxy_addr'] = $client['poxy_hostname'];
1645
			unset($client['proxy_addr']);
1646
			if (isset($client['use_lzo'])) {
1647
				$client['compression'] = "on";
1648
				unset($client['use_lzo']);
1649
			}
1650
			$client['resolve_retry'] = $client['infiniteresolvretry'];
1651
			unset($client['infiniteresolvretry']);
1652

    
1653
			/* allocate vpnid */
1654
			$client['vpnid'] = $vpnid++;
1655

    
1656
			if (!empty($client['custom_options'])) {
1657
				$cstmopts = array();
1658
				$tmpcstmopts = explode(";", $client['custom_options']);
1659
				$assigned = false;
1660
				$tmpstr = "";
1661
				foreach ($tmpcstmopts as $tmpcstmopt) {
1662
					$tmpstr = str_replace(" ", "", $tmpcstmopt);
1663
					if (substr($tmpstr,0 ,6) == "devtun") {
1664
						$assigned = true;
1665
						continue;
1666
					} else if (substr($tmpstr, 0, 5) == "local") {
1667
                                                $localip = substr($tmpstr, 6);
1668
                                                $client['ipaddr'] = str_replace("\n", "", $localip);
1669
					} else
1670
						$cstmopts[] = $tmpcstmopt;
1671
				}
1672
				$client['custom_options'] = implode(";", $cstmopts);
1673
				if ($assigned == true) {
1674
					$realif = substr($tmpstr, 3);
1675
					foreach ($config['interfaces'] as $iface => $cfgif) {
1676
						if ($cfgif['if'] == $realif) {
1677
							$config['interfaces'][$iface]['if'] = "ovpnc{$client['vpnid']}";
1678
							break;
1679
						}
1680
					}
1681
				}
1682
			}
1683

    
1684
			if (!empty($client['disable']))
1685
				$client['disable'] = true;
1686
			else
1687
				unset($client['disable']);
1688

    
1689
			$config['openvpn']['openvpn-client'][] = $client;
1690
		}
1691

    
1692
		unset($config['installedpackages']['openvpnclient']);
1693
	}
1694

    
1695
	/* openvpn client specific configurations */
1696
	if (is_array($config['installedpackages']['openvpncsc'])) {
1697
		$config['openvpn']['openvpn-csc'] = array();
1698

    
1699
		foreach($config['installedpackages']['openvpncsc']['config'] as $csc) {
1700

    
1701
			if (!is_array($csc))
1702
				continue;
1703

    
1704
			/* modify configuration values */
1705
			$csc['common_name'] = $csc['commonname'];
1706
			unset($csc['commonname']);
1707
			$csc['tunnel_network'] = $csc['ifconfig_push'];
1708
			unset($csc['ifconfig_push']);
1709
			$csc['dns_domain'] = $csc['dhcp_domainname'];
1710
			unset($csc['dhcp_domainname']);
1711
			$csc['dns_server1'] = $csc['dhcp_dns'];
1712
			unset($csc['dhcp_dns']);
1713
			$csc['ntp_server1'] = $csc['dhcp_ntp'];
1714
			unset($csc['dhcp_ntp']);
1715
			if ($csc['dhcp_nbtdisable'])
1716
				$csc['netbios_enable'] = false;
1717
			else
1718
				$csc['netbios_enable'] = "yes";
1719
			unset($csc['dhcp_nbtdisable']);
1720
			$csc['netbios_ntype'] = $csc['dhcp_nbttype'];
1721
			unset($csc['dhcp_nbttype']);
1722
			$csc['netbios_scope'] = $csc['dhcp_nbtscope'];
1723
			unset($csc['dhcp_nbtscope']);
1724
			$csc['nbdd_server1'] = $csc['dhcp_nbdd'];
1725
			unset($csc['dhcp_nbdd']);
1726
			$csc['wins_server1'] = $csc['dhcp_wins'];
1727
			unset($csc['dhcp_wins']);
1728

    
1729
			if (!empty($csc['disable']))
1730
				$csc['disable'] = true;
1731
			else
1732
				unset($csc['disable']);
1733

    
1734
			$config['openvpn']['openvpn-csc'][] = $csc;
1735
		}
1736

    
1737
		unset($config['installedpackages']['openvpncsc']);
1738
	}
1739

    
1740
	if (count($config['openvpn']['openvpn-server']) > 0 ||
1741
		count($config['openvpn']['openvpn-client']) > 0) {
1742
		$ovpnrule = array();
1743
                $ovpnrule['type'] = "pass";
1744
                $ovpnrule['interface'] = "openvpn";
1745
                $ovpnrule['statetype'] = "keep state";
1746
                $ovpnrule['source'] = array();
1747
                $ovpnrule['destination'] = array();
1748
                $ovpnrule['source']['any'] = true;
1749
                $ovpnrule['destination']['any'] = true;
1750
                $ovpnrule['descr'] = "Auto added OpenVPN rule from config upgrade.";
1751
		$config['filter']['rule'][] = $ovpnrule;
1752
	}
1753

    
1754
	/*
1755
		* FIXME: hack to keep things working with no installedpackages
1756
		* or carp array in the configuration data.
1757
		*/
1758
	if (!is_array($config['installedpackages']))
1759
		$config['installedpackages'] = array();
1760
	if (!is_array($config['installedpackages']['carp']))
1761
		$config['installedpackages']['carp'] = array();
1762

    
1763
}
1764

    
1765

    
1766
function upgrade_052_to_053() {
1767
	global $config;
1768
	if (!is_array($config['ca']))
1769
		$config['ca'] = array();
1770
	if (!is_array($config['cert']))
1771
		$config['cert'] = array();
1772

    
1773
	/* migrate advanced admin page webui ssl to certifcate mngr */
1774
	if ($config['system']['webgui']['certificate'] &&
1775
	$config['system']['webgui']['private-key']) {
1776

    
1777
		/* create cert entry */
1778
		$cert = array();
1779
		$cert['refid'] = uniqid();
1780
		$cert['descr'] = "webConfigurator SSL Certificate";
1781
		$cert['crt'] = $config['system']['webgui']['certificate'];
1782
		$cert['prv'] = $config['system']['webgui']['private-key'];
1783
		$config['cert'][] = $cert;
1784

    
1785
		/* create cert reference */
1786
		unset($config['system']['webgui']['certificate']);
1787
		unset($config['system']['webgui']['private-key']);
1788
		$config['system']['webgui']['ssl-certref'] = $cert['refid'];
1789
	}
1790

    
1791
	/* migrate advanced admin page ssh keys to user manager */
1792
	if ($config['system']['ssh']['authorizedkeys']) {
1793
		$admin_user =& getUserEntryByUID(0);
1794
		$admin_user['authorizedkeys'] = $config['system']['ssh']['authorizedkeys'];
1795
		unset($config['system']['ssh']['authorizedkeys']);
1796
	}
1797
}
1798

    
1799

    
1800
function upgrade_053_to_054() {
1801
	global $config;
1802
	if(is_array($config['load_balancer']['lbpool'])) {
1803
		$lbpool_arr = $config['load_balancer']['lbpool'];
1804
		$lbpool_srv_arr = array();
1805
		$gateway_group_arr = array();
1806
		$gateways = return_gateways_array();
1807
		$group_name_changes = array();
1808
		if (! is_array($config['gateways']['gateway_item']))
1809
			$config['gateways']['gateway_item'] = array();
1810

    
1811
		$a_gateways =& $config['gateways']['gateway_item'];
1812
		foreach($lbpool_arr as $lbpool) {
1813
			if($lbpool['type'] == "gateway") {
1814
				// Gateway Groups have to have valid names in pf, old lb pools did not. Clean them up.
1815
				$group_name = ereg_replace("[^A-Za-z0-9]", "", $lbpool['name'] );
1816
				// If we made and changes, check for collisions and note the change.
1817
				if ($group_name != $lbpool['name']) {
1818
					// Make sure the name isn't already in use.
1819
					foreach ($gateway_group_arr as $gwg) {
1820
						// If the name is in use, add some random bits to avoid collision.
1821
						if ($gwg['name'] == $group_name)
1822
							$group_name .= uniqid();
1823
					}
1824
					$group_name_changes[$lbpool['name']] = $group_name;
1825
				}
1826
				$gateway_group['name'] = $group_name;
1827
				$gateway_group['descr'] = $lbpool['descr'];
1828
				$gateway_group['trigger'] = "down";
1829
				$gateway_group['item'] = array();
1830
				$i = 0;
1831
				foreach($lbpool['servers'] as $member) {
1832
					$split = split("\|", $member);
1833
					$interface = $split[0];
1834
					$monitor = $split[1];
1835
					/* on static upgraded configuration we automatically prepend GW_ */
1836
					$static_name = "GW_" . strtoupper($interface);
1837
					if(is_ipaddr($monitor))
1838
						foreach ($a_gateways as & $gw)
1839
							if ($gw['name'] == $static_name)
1840
								$gw['monitor'] = $monitor;
1841

    
1842
					/* on failover increment tier. Else always assign 1 */
1843
					if($lbpool['behaviour'] == "failover") {
1844
						$i++;
1845
					} else {
1846
						$i = 1;
1847
					}
1848
					$gateway_group['item'][] = "$static_name|$i";
1849
				}
1850
				$gateway_group_arr[] = $gateway_group;
1851
			} else {
1852
				$lbpool_srv_arr[] = $lbpool;
1853
			}
1854
		}
1855
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1856
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1857
	}
1858
	// Unset lbpool if we no longer have any server pools
1859
	if (count($lbpool_srv_arr) == 0) {
1860
		if(empty($config['load_balancer'])) {
1861
			unset($config['load_balancer']);
1862
		} else {
1863
			unset($config['load_balancer']['lbpool']);
1864
		}
1865
	} else {
1866
		$config['load_balancer']['lbpool'] = $lbpool_srv_arr;
1867
	}
1868
	// Only set the gateway group array if we converted any
1869
	if (count($gateway_group_arr) != 0) {
1870
		$config['gateways']['gateway_group'] = $gateway_group_arr;
1871
		// Update any rules that had a gateway change, if any.
1872
		if (count($group_name_changes) > 0)
1873
			foreach ($config['filter']['rule'] as & $rule)
1874
				if (!empty($rule["gateway"]) && array_key_exists($rule["gateway"], $group_name_changes))
1875
					$rule["gateway"] = $group_name_changes[$rule["gateway"]];
1876
	}
1877
}
1878

    
1879

    
1880
function upgrade_054_to_055() {
1881
	global $config;
1882
	global $g;
1883

    
1884
	/* RRD files changed for quality, traffic and packets graphs */
1885
	//ini_set("max_execution_time", "1800");
1886
	/* convert traffic RRD file */
1887
	global $parsedcfg, $listtags;
1888
	$listtags = array("ds", "v", "rra", "row");
1889

    
1890
	$rrddbpath = "/var/db/rrd/";
1891
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
1892

    
1893
	$rrdinterval = 60;
1894
	$valid = $rrdinterval * 2;
1895

    
1896
	/* Asume GigE for now */
1897
	$downstream = 125000000;
1898
	$upstream = 125000000;
1899

    
1900
	/* build a list of quality databases */
1901
	/* roundtrip has become delay */
1902
	function divide_delay($delayval) {
1903
		$delayval = floatval($delayval);
1904
		$delayval = ($delayval / 1000);
1905
		$delayval = " ". sprintf("%1.10e", $delayval) ." ";
1906
		return $delayval;
1907
	}
1908
	/* the roundtrip times need to be divided by 1000 to get seconds, really */
1909
	$databases = array();
1910
	if (!file_exists($rrddbpath))
1911
		@mkdir($rrddbpath);
1912
	chdir($rrddbpath);
1913
	$databases = glob("*-quality.rrd");
1914
	rsort($databases);
1915
	foreach($databases as $database) {
1916
		$xmldump = "{$database}.old.xml";
1917
		$xmldumpnew = "{$database}.new.xml";
1918

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

    
1923
		dump_rrd_to_xml("{$rrddbpath}/{$database}", "{$g['tmp_path']}/{$xmldump}");
1924
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1925
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1926
		$rrdold = $rrdold['rrd'];
1927

    
1928
		$i = 0;
1929
		foreach($rrdold['rra'] as $rra) {
1930
			$l = 0;
1931
			foreach($rra['database']['row'] as $row) {
1932
				$vnew = divide_delay($row['v'][1]);
1933
				$rrdold['rra'][$i]['database']['row'][$l]['v'][1] = $vnew;
1934
				$l++;
1935
			}
1936
			$i++;
1937
		}
1938

    
1939
		$rrdxml = dump_xml_config_raw($rrdold, "rrd");
1940
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
1941
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
1942

    
1943
	}
1944
	/* let apinger recreate required files */
1945
	setup_gateways_monitor();
1946

    
1947
	/* build a list of traffic and packets databases */
1948
	$databases = array();
1949
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
1950
	rsort($databases);
1951
	foreach($databases as $database) {
1952
		$databasetmp = "{$database}.tmp";
1953
		$xmldump = "{$database}.old.xml";
1954
		$xmldumptmp = "{$database}.tmp.xml";
1955
		$xmldumpnew = "{$database}.new.xml";
1956

    
1957
		if ($g['booting'])
1958
			echo "Migrate RRD database {$database} to new format \n";
1959
		/* rename DS source */
1960
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r in:inpass 2>&1");
1961
		mwexec("$rrdtool tune {$rrddbpath}/{$database} -r out:outpass 2>71");
1962

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

    
1966
		/* create new rrd database file */
1967
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
1968
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
1969
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
1970
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
1971
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
1972
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
1973
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
1974
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
1975
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
1976

    
1977
		create_new_rrd("$rrdcreate");
1978
		/* create temporary xml from new RRD */
1979
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
1980

    
1981
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
1982
		$rrdold = xml2array($rrdoldxml, 1, "tag");
1983
		$rrdold = $rrdold['rrd'];
1984

    
1985
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
1986
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
1987
		$rrdnew = $rrdnew['rrd'];
1988

    
1989
		/* remove any MAX RRA's. Not needed for traffic. */
1990
		$i = 0;
1991
		foreach ($rrdold['rra'] as $rra) {
1992
			if(trim($rra['cf']) == "MAX") {
1993
				unset($rrdold['rra'][$i]);
1994
			}
1995
			$i++;
1996
		}
1997

    
1998
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
1999
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
2000
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
2001
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2002

    
2003
	}
2004
	enable_rrd_graphing();
2005
	if ($g['booting'])
2006
		echo "Updating configuration...";
2007
}
2008

    
2009

    
2010
function upgrade_055_to_056() {
2011
	global $config;
2012

    
2013
	if (!is_array($config['ca']))
2014
		$config['ca'] = array();
2015
	if (!is_array($config['cert']))
2016
		$config['cert'] = array();
2017

    
2018
	/* migrate ipsec ca's to cert manager */
2019
	if (is_array($config['ipsec']['cacert'])) {
2020
		foreach($config['ipsec']['cacert'] as & $cacert) {
2021
			$ca = array();
2022
			$ca['refid'] = uniqid();
2023
			if (is_array($cacert['cert']))
2024
				$ca['crt'] = $cacert['cert'][0];
2025
			else
2026
				$ca['crt'] = $cacert['cert'];
2027
			$ca['descr'] = $cacert['ident'];
2028
			$config['ca'][] = $ca;
2029
		}
2030
		unset($config['ipsec']['cacert']);
2031
	}
2032

    
2033
	/* migrate phase1 certificates to cert manager */
2034
	if (is_array($config['ipsec']['phase1'])) {
2035
		foreach($config['ipsec']['phase1'] as & $ph1ent) {
2036
			$cert = array();
2037
			$cert['refid'] = uniqid();
2038
			$cert['descr'] = "IPsec Peer {$ph1ent['remote-gateway']} Certificate";
2039
			if (is_array($ph1ent['cert']))
2040
				$cert['crt'] = $ph1ent['cert'][0];
2041
			else
2042
				$cert['crt'] = $ph1ent['cert'];
2043
			$cert['prv'] = $ph1ent['private-key'];
2044
			$config['cert'][] = $cert;
2045
			$ph1ent['certref'] = $cert['refid'];
2046
			if ($ph1ent['cert'])
2047
				unset($ph1ent['cert']);
2048
			if ($ph1ent['private-key'])
2049
				unset($ph1ent['private-key']);
2050
			if ($ph1ent['peercert'])
2051
				unset($ph1ent['peercert']);
2052
		}
2053
	}
2054
}
2055

    
2056

    
2057
function upgrade_056_to_057() {
2058
	global $config;
2059
	if (!is_array($config['system']['user']))
2060
		$config['system']['user'] = array();
2061
	/* migrate captivate portal to user manager */
2062
	if (is_array($config['captiveportal']['user'])) {
2063
		foreach($config['captiveportal']['user'] as $user) {
2064
			// avoid user conflicts
2065
			$found = false;
2066
			foreach ($config['system']['user'] as $userent) {
2067
				if ($userent['name'] == $user['name']) {
2068
					$found = true;
2069
					break;
2070
				}
2071
			}
2072
			if ($found)
2073
				continue;
2074
			$user['scope'] = "user";
2075
			if (isset($user['expirationdate'])) {
2076
				$user['expires'] = $user['expirationdate'];
2077
				unset($user['expirationdate']);
2078
			}
2079
			if (isset($user['password'])) {
2080
				$user['md5-hash'] = $user['password'];
2081
				unset($user['password']);
2082
			}
2083
			$user['uid'] = $config['system']['nextuid']++;
2084
			$config['system']['user'][] = $user;
2085
		}
2086
		unset($config['captiveportal']['user']);
2087
	}
2088
}
2089

    
2090
function upgrade_057_to_058() {
2091
	global $config;
2092
	/* set all phase2 entries to tunnel mode */
2093
	if (is_array($config['ipsec']['phase2']))
2094
		foreach($config['ipsec']['phase2'] as & $ph2ent)
2095
			$ph2ent['mode'] = 'tunnel';
2096
}
2097

    
2098
function upgrade_058_to_059() {
2099
	global $config;
2100

    
2101
	if (is_array($config['schedules']['schedule'])) {
2102
		foreach ($config['schedules']['schedule'] as & $schedl)
2103
			$schedl['schedlabel'] = uniqid();
2104
	}
2105
}
2106

    
2107
function upgrade_059_to_060() {
2108
	global $config;
2109
	require_once("/etc/inc/certs.inc");
2110
	if (is_array($config['ca'])) {
2111
		/* Locate issuer for all CAs */
2112
		foreach ($config['ca'] as & $ca) {
2113
			$subject = cert_get_subject($ca['crt']);
2114
			$issuer = cert_get_issuer($ca['crt']);
2115
			if($issuer <> $subject) {
2116
				$issuer_crt =& lookup_ca_by_subject($issuer);
2117
				if($issuer_crt)
2118
					$ca['caref'] = $issuer_crt['refid'];
2119
			}
2120
		}
2121
		
2122
		/* Locate issuer for all certificates */
2123
		if (is_array($config['cert'])) {
2124
			foreach ($config['cert'] as & $cert) {
2125
				$subject = cert_get_subject($cert['crt']);
2126
				$issuer = cert_get_issuer($cert['crt']);
2127
				if($issuer <> $subject) {
2128
					$issuer_crt =& lookup_ca_by_subject($issuer);
2129
					if($issuer_crt)
2130
						$cert['caref'] = $issuer_crt['refid'];
2131
				}
2132
			}
2133
		}
2134
	}
2135
}
2136

    
2137
function upgrade_060_to_061() {
2138
	global $config;
2139

    
2140
	if (is_array($config['interfaces']['wan']))
2141
		$config['interfaces']['wan']['enable'] = true;
2142
	if (is_array($config['interfaces']['lan']))
2143
		$config['interfaces']['lan']['enable'] = true;
2144
}
2145

    
2146
function upgrade_061_to_062() {
2147
	global $config;
2148

    
2149
	/* Convert NAT port forwarding rules */
2150
	if (is_array($config['nat']['rule'])) {
2151
		$a_nat = &$config['nat']['rule'];
2152

    
2153
		foreach ($a_nat as &$natent) {
2154
			$natent['disabled'] = false;
2155
			$natent['nordr']    = false;
2156

    
2157
			$natent['source'] = array(
2158
				"not"     => false,
2159
				"any"     => true,
2160
				"port"    => ""
2161
			);
2162

    
2163
			$natent['destination'] = array(
2164
				"not"     => false,
2165
				"address" => $natent['external-address'],
2166
				"port"    => $natent['external-port']
2167
			);
2168

    
2169
			if (empty($natent['destination']['address'])) {
2170
				unset($natent['destination']['address']);
2171
				$natent['destination']['network'] = $natent['interface'] . 'ip';
2172
			} else if ($natent['destination']['address'] == 'any') {
2173
				unset($natent['destination']['address']);
2174
				$natent['destination']['any'] = true;
2175
			}
2176

    
2177
			unset($natent['external-address']);
2178
			unset($natent['external-port']);
2179
		}
2180

    
2181
		unset($natent);
2182
	}
2183
}
2184

    
2185
function upgrade_062_to_063() {
2186
	/* Upgrade legacy Themes to the new pfsense_ng */
2187
	global $config;
2188

    
2189
	switch($config['theme']) {
2190
		case "nervecenter":
2191
			$config['theme'] = "pfsense_ng";
2192
			break;
2193
	}
2194
	
2195
}
2196

    
2197
function upgrade_063_to_064() {
2198
	global $config;
2199
	$j=0;
2200
	$ifcfg = &$config['interfaces'];
2201
	
2202
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {	
2203
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
2204
			$config['ppps']['ppp'][$pppid]['if'] = "ppp".$j;
2205
			$config['ppps']['ppp'][$pppid]['ptpid'] = $j;
2206
			$j++;
2207
			if (isset($ppp['port'])){
2208
				$config['ppps']['ppp'][$pppid]['ports'] = $ppp['port'];
2209
				unset($config['ppps']['ppp'][$pppid]['port']);
2210
			}
2211
			if (!isset($ppp['type'])){
2212
				$config['ppps']['ppp'][$pppid]['type'] = "ppp";
2213
			}
2214
			if (isset($ppp['defaultgw']))
2215
				unset($config['ppps']['ppp'][$pppid]['defaultgw']);
2216
		}
2217
	}
2218
	
2219
	if (!is_array($config['ppps']['ppp']))
2220
		$config['ppps']['ppp'] = array();
2221
	$a_ppps = &$config['ppps']['ppp'];
2222

    
2223
	foreach ($ifcfg as $ifname => $ifinfo) {
2224
		$ppp = array();
2225
		// For pppoe conversion
2226
		if ($ifinfo['ipaddr'] == "pppoe" || $ifinfo['ipaddr'] == "pptp"){
2227
			if (isset($ifinfo['ptpid']))
2228
				continue;
2229
			$ppp['ptpid'] =  $j;
2230
			$ppp['type'] = $ifinfo['ipaddr'];
2231
			$ppp['if'] = $ifinfo['ipaddr'].$j;
2232
			$ppp['ports'] = $ifinfo['if'];
2233
			if ($ifinfo['ipaddr'] == "pppoe"){
2234
				$ppp['username'] = $ifinfo['pppoe_username'];
2235
				$ppp['password'] = base64_encode($ifinfo['pppoe_password']);
2236
			}
2237
			if ($ifinfo['ipaddr'] == "pptp"){
2238
				$ppp['username'] = $ifinfo['pptp_username'];
2239
				$ppp['password'] = base64_encode($ifinfo['pptp_password']);
2240
			}
2241
			
2242
			if (isset($ifinfo['provider']))
2243
				$ppp['provider'] = $ifinfo['provider'];
2244
			if (isset($ifinfo['ondemand']))
2245
				$ppp['ondemand'] = true;
2246
			if (isset($ifinfo['timeout']))
2247
				$ppp['idletimeout'] = $ifinfo['timeout'];
2248
			if (isset($ifinfo['pppoe']['pppoe-reset-type'])){
2249
				$ppp['pppoe-reset-type'] = $ifinfo['pppoe']['pppoe-reset-type'];
2250
				if (is_array($config['cron']['item'])) {
2251
					for ($i = 0; $i < count($config['cron']['item']); $i++) {
2252
						$item = $config['cron']['item'][$i];
2253
						if (strpos($item['command'], "/conf/pppoe{$ifname}restart") !== false)
2254
							$config['cron']['item'][$i]['command'] = "/var/etc/pppoe_restart_" . $ppp['if'];
2255
					}
2256
				}
2257
			}
2258
			if (isset($ifinfo['local']))
2259
				$ppp['localip'] = $ifinfo['local'];
2260
			if (isset($ifinfo['subnet']))
2261
				$ppp['subnet'] = $ifinfo['subnet'];
2262
			if (isset($ifinfo['remote']))
2263
				$ppp['gateway'] = $ifinfo['remote'];
2264

    
2265
			$ifcfg[$ifname]['if'] = $ifinfo['ipaddr'].$j;
2266
			$j++;
2267
			
2268
			unset($ifcfg[$ifname]['pppoe_username']);
2269
			unset($ifcfg[$ifname]['pppoe_password']);
2270
			unset($ifcfg[$ifname]['provider']);
2271
			unset($ifcfg[$ifname]['ondemand']);
2272
			unset($ifcfg[$ifname]['timeout']);
2273
			unset($ifcfg[$ifname]['pppoe_reset']);
2274
			unset($ifcfg[$ifname]['pppoe_preset']);
2275
			unset($ifcfg[$ifname]['pppoe']);
2276
			unset($ifcfg[$ifname]['pptp_username']);
2277
			unset($ifcfg[$ifname]['pptp_password']);
2278
			unset($ifcfg[$ifname]['local']);
2279
			unset($ifcfg[$ifname]['subnet']);
2280
			unset($ifcfg[$ifname]['remote']);
2281
			
2282
			$a_ppps[] = $ppp;
2283
			
2284
		}
2285
	}
2286
}
2287

    
2288
function upgrade_064_to_065() {
2289
	/* Disable TSO and LRO in upgraded configs */
2290
	global $config;
2291
	$config['system']['disablesegmentationoffloading'] = true;
2292
	$config['system']['disablelargereceiveoffloading'] = true;
2293
}
2294

    
2295
function upgrade_065_to_066() {
2296
	global $config;
2297

    
2298
	$dhcrelaycfg =& $config['dhcrelay'];
2299

    
2300
        if (is_array($dhcrelaycfg)) {
2301
        	$dhcrelayifs = array();
2302
		$foundifs = false;
2303
        	/* DHCPRelay enabled on any interfaces? */
2304
                foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
2305
                        if (isset($dhcrelayifconf['enable'])) {
2306
				$dhcrelayifs[] = $dhcrelayif;
2307
				unset($dhcrelaycfg['dhcrelayif']);
2308
				$foundifs = true;
2309
			}
2310
                }
2311
		if ($foundifs == true)
2312
			$dhcrelaycfg['interface'] = implode(",", $dhcrelayifs);
2313
        }
2314
}
2315

    
2316
function upgrade_066_to_067() {
2317
	global $config;
2318
	if (isset($config['system']['ca'])) {
2319
		$config['ca'] = $config['system']['ca'];
2320
	}
2321
	if (isset($config['system']['cert'])) {
2322
		$config['cert'] = $config['system']['cert'];
2323
	}
2324
}
2325

    
2326
function upgrade_067_to_068() {
2327
	global $config;
2328

    
2329
	if (!empty($config['pppoe'])) {
2330
		$config['pppoes'] = array();
2331
		$config['pppoes']['pppoe'] = array();
2332
		$config['pppoes']['pppoe'][] = $config['pppoe'][0];
2333

    
2334
		if (is_array($config['pppoe']['user'])) {
2335
			$username = array(); 
2336
			foreach ($config['pppoe']['user'] as $user) {
2337
				$usr = $user['name'] . ":" . base64_encode($user['password']);
2338
				if ($user['ip'])
2339
					$usr .= ":{$user['ip']}";
2340
				$username[] = $usr;
2341
			}
2342
			$config['pppoes']['pppoe'][0]['username'] = implode(" ", $username);
2343
		}
2344
		unset($config['pppoe']);
2345
	}
2346
}
2347

    
2348
function upgrade_068_to_069() {
2349
	global $config;
2350
	if (!is_array($config['system']['user']))
2351
		return;
2352
	foreach ($config['system']['user'] as & $user) {
2353
		if (!is_array($user['cert']))
2354
			continue;
2355
		$rids = array();
2356
		foreach ($user['cert'] as $id => $cert) {
2357
			if (!isset($cert['descr']))
2358
				continue;
2359
			$tcert = $cert;
2360
			// Make sure each cert gets a refid
2361
			if (!isset($tcert['refid']))
2362
				$tcert['refid'] = uniqid();
2363
			// Keep the cert references for this user
2364
			$rids[] = $tcert['refid'];
2365
			$config['cert'][] = $tcert;
2366
		}
2367
		// Replace user certs with cert references instead.
2368
		if (count($rids) > 0)
2369
			$user['cert'] = $rids;
2370
	}
2371
}
2372

    
2373
function upgrade_069_to_070() {
2374
	global $config;
2375

    
2376
	/* Convert NAT 1:1 rules */
2377
	if (is_array($config['nat']['onetoone'])) {
2378
		foreach ($config['nat']['onetoone'] as $nidx => $natent) {
2379
			if ($natent['subnet'] == 32)
2380
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal']);
2381
			else
2382
				$config['nat']['onetoone'][$nidx]['source'] = array("address" => $natent['internal'] . "/" . $natent['subnet']);
2383

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

    
2386
			unset($config['nat']['onetoone'][$nidx]['internal']);
2387
			unset($config['nat']['onetoone'][$nidx]['subnet']);
2388
		}
2389

    
2390
		unset($natent);
2391
	}
2392
}
2393

    
2394
function upgrade_070_to_071() {
2395
	global $config;
2396

    
2397
	if (is_array($config['cron']['item'])) {
2398
		foreach($config['cron']['item'] as $idx => $cronitem) {
2399
			if(stristr($cronitem['command'], "checkreload.sh")) {
2400
				unset($config['cron']['item'][$idx]);
2401
				break;
2402
			}
2403
		}
2404
	}
2405
}
2406

    
2407
function rename_field(& $section, $oldname, $newname) {
2408
	if (is_array($section)) {
2409
		foreach($section as & $item) {
2410
			if (!empty($item[$oldname]))
2411
				$item[$newname] = $item[$oldname];
2412
			if (isset($item[$oldname]))
2413
				unset($item[$oldname]);
2414
		}
2415
	}
2416
}
2417

    
2418
function upgrade_071_to_072() {
2419
	global $config;
2420
	if (is_array($config['sysctl']) && is_array($config['sysctl']['item']))
2421
		rename_field($config['sysctl']['item'], 'desc', 'descr');
2422
}
2423

    
2424
function upgrade_072_to_073() {
2425
	global $config;
2426
	if (!is_array($config['load_balancer']))
2427
		return;
2428
	if (is_array($config['load_balancer']['monitor_type']))
2429
		rename_field($config['load_balancer']['monitor_type'], 'desc', 'descr');
2430
	if (is_array($config['load_balancer']['lbpool']))
2431
		rename_field($config['load_balancer']['lbpool'], 'desc', 'descr');
2432
	if (is_array($config['load_balancer']['lbaction']))
2433
		rename_field($config['load_balancer']['lbaction'], 'desc', 'descr');
2434
	if (is_array($config['load_balancer']['lbprotocol']))
2435
		rename_field($config['load_balancer']['lbprotocol'], 'desc', 'descr');
2436
	if (is_array($config['load_balancer']['virtual_server']))
2437
		rename_field($config['load_balancer']['virtual_server'], 'desc', 'descr');
2438
}
2439

    
2440
function upgrade_073_to_074() {
2441
	global $config;
2442
	rename_field($config['system']['user'], 'fullname', 'descr');
2443
}
2444

    
2445
function upgrade_074_to_075() {
2446
	global $config;
2447
	if (is_array($config['ca']))
2448
		rename_field($config['ca'], 'name', 'descr');
2449
	if (is_array($config['cert']))
2450
		rename_field($config['cert'], 'name', 'descr');
2451
	if (is_array($config['crl']))
2452
		rename_field($config['crl'], 'name', 'descr');
2453
}
2454

    
2455
function upgrade_075_to_076() {
2456
	global $config;
2457
	$cron_item = array();
2458
	$cron_item['minute'] = "30";
2459
	$cron_item['hour'] = "12";
2460
	$cron_item['mday'] = "*";
2461
	$cron_item['month'] = "*";
2462
	$cron_item['wday'] = "*";
2463
	$cron_item['who'] = "root";
2464
	$cron_item['command'] = "/usr/bin/nice -n20 /etc/rc.update_urltables";
2465
	$config['cron']['item'][] = $cron_item;
2466
}
2467

    
2468
function upgrade_076_to_077() {
2469
	global $config;
2470
	foreach($config['filter']['rule'] as & $rule) {
2471
	if (isset($rule['protocol']) && !empty($rule['protocol']))
2472
		$rule['protocol'] = strtolower($rule['protocol']);
2473
	}
2474
}
2475

    
2476
function upgrade_077_to_078() {
2477
	if (is_array($config['pptpd']) && is_array($config['pptpd']['radius'])
2478
		&& !is_array($config['pptpd']['radius']['server'])) {
2479
		$radarr = array();
2480
		$radsvr = array();
2481
		$radsvr['ip'] = $config['pptpd']['radius']['server'];
2482
		$radsvr['secret'] = $config['pptpd']['radius']['secret'];
2483
		$radsvr['port'] = 1812;
2484
		$radsvr['acctport'] = 1813;
2485
		$radsvr['enable'] = isset($config['pptpd']['radius']['enable']);
2486
		$radarr['accounting'] = isset($config['pptpd']['radius']['accounting']);
2487
		if ($radarr['accounting'])
2488
			$radarr['acct_update'] = $radsvr['ip'];
2489
		$radarr['server'] = $radsvr;
2490
		$config['pptpd']['radius'] = $radarr;
2491
	}
2492
}
2493

    
2494
function upgrade_078_to_079() {
2495
	global $config;
2496
	global $g;
2497

    
2498
	/* RRD files changed for quality, traffic and packets graphs */
2499
	/* convert traffic RRD file */
2500
	global $parsedcfg, $listtags;
2501
	$listtags = array("ds", "v", "rra", "row");
2502

    
2503
	$rrddbpath = "/var/db/rrd/";
2504
	$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
2505

    
2506
	$rrdinterval = 60;
2507
	$valid = $rrdinterval * 2;
2508

    
2509
	/* Asume GigE for now */
2510
	$downstream = 125000000;
2511
	$upstream = 125000000;
2512

    
2513
	/* build a list of traffic and packets databases */
2514
	$databases = array();
2515
	exec("cd $rrddbpath;/usr/bin/find *-traffic.rrd *-packets.rrd", $databases);
2516
	rsort($databases);
2517
	foreach($databases as $database) {
2518
		$databasetmp = "{$database}.tmp";
2519
		$xmldump = "{$database}.old.xml";
2520
		$xmldumptmp = "{$database}.tmp.xml";
2521
		$xmldumpnew = "{$database}.new.xml";
2522

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

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

    
2529
		/* create new rrd database file */
2530
		$rrdcreate = "$rrdtool create {$g['tmp_path']}/{$databasetmp} --step $rrdinterval ";
2531
		$rrdcreate .= "DS:inpass:COUNTER:$valid:0:$downstream ";
2532
		$rrdcreate .= "DS:outpass:COUNTER:$valid:0:$upstream ";
2533
		$rrdcreate .= "DS:inblock:COUNTER:$valid:0:$downstream ";
2534
		$rrdcreate .= "DS:outblock:COUNTER:$valid:0:$upstream ";
2535
		$rrdcreate .= "DS:inpass6:COUNTER:$valid:0:$downstream ";
2536
		$rrdcreate .= "DS:outpass6:COUNTER:$valid:0:$upstream ";
2537
		$rrdcreate .= "DS:inblock6:COUNTER:$valid:0:$downstream ";
2538
		$rrdcreate .= "DS:outblock6:COUNTER:$valid:0:$upstream ";
2539
		$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
2540
		$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
2541
		$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
2542
		$rrdcreate .= "RRA:AVERAGE:0.5:720:3000 ";
2543

    
2544
		create_new_rrd("$rrdcreate");
2545
		/* create temporary xml from new RRD */
2546
		dump_rrd_to_xml("{$g['tmp_path']}/{$databasetmp}", "{$g['tmp_path']}/{$xmldumptmp}");
2547

    
2548
		$rrdoldxml = file_get_contents("{$g['tmp_path']}/{$xmldump}");
2549
		$rrdold = xml2array($rrdoldxml, 1, "tag");
2550
		$rrdold = $rrdold['rrd'];
2551

    
2552
		$rrdnewxml = file_get_contents("{$g['tmp_path']}/{$xmldumptmp}");
2553
		$rrdnew = xml2array($rrdnewxml, 1, "tag");
2554
		$rrdnew = $rrdnew['rrd'];
2555

    
2556
		/* remove any MAX RRA's. Not needed for traffic. */
2557
		$i = 0;
2558
		foreach ($rrdold['rra'] as $rra) {
2559
			if(trim($rra['cf']) == "MAX") {
2560
				unset($rrdold['rra'][$i]);
2561
			}
2562
			$i++;
2563
		}
2564

    
2565
		$rrdxmlarray = migrate_rrd_format($rrdold, $rrdnew);
2566
		$rrdxml = dump_xml_config_raw($rrdxmlarray, "rrd");
2567
		file_put_contents("{$g['tmp_path']}/{$xmldumpnew}", $rrdxml);
2568
		mwexec("$rrdtool restore -f {$g['tmp_path']}/{$xmldumpnew} {$rrddbpath}/{$database} 2>&1");
2569

    
2570
	}
2571
	enable_rrd_graphing();
2572
	if ($g['booting'])
2573
		echo "Updating configuration...";
2574
	foreach($config['filter']['rule'] as & $rule) {
2575
		if (isset($rule['protocol']) && !empty($rule['protocol']))
2576
			$rule['protocol'] = strtolower($rule['protocol']);
2577
	}
2578
}
2579

    
2580
function upgrade_079_to_080() {
2581
	global $config;
2582
	/* enable the allow IPv6 toggle */
2583
	$config['system']['ipv6allow'] = true;
2584
}
2585

    
2586
?>
(48-48/61)