Project

General

Profile

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

    
24
require_once("config.gui.inc");
25
require_once("util.inc");
26
require_once("globals.inc");
27
require_once("pfsense-utils.inc");
28

    
29
// Save all of the parameters changed by the setup wizard
30
function save_setupwizard_config(
31
	$hostname,
32
	$domain,
33
	$timezone,
34
	$timeservers,
35
	$spoofmac,
36
	$mtu,
37
	$mss,
38
	$bogons,
39
	$rfc1918,
40
	$lanip,
41
	$dnsoverride,
42
	$primarydns,
43
	$secondarydns,
44
	$dhcphostname,
45
	$wangateway,
46
	$wanip,
47
	$wantype,
48
	$pppoeUsername,
49
	$pppoePassword,
50
	$pppoeOnDemand,
51
	$pppoeIdelTimeout,
52
	$pppoeServiceName,
53
	$ppppoeLocalIP) {
54

    
55
	global $config, $g;
56

    
57
	// General ====================================================================================================================
58
	$config['system']['hostname'] = $hostname;
59
	$config['system']['domain'] = $domain;
60

    
61
	// Time =======================================================================================================================
62
	$config['system']['timezone'] = $timezone;
63
	$config['system']['timeservers'] = $timeservers;
64

    
65
	// WAN ========================================================================================================================
66
	setorforget($config['interfaces']['wan']['spoofmac'], $spoofmac);
67
	setorforget($config['interfaces']['wan']['mtu'], $mtu);
68
	setorforget($config['interfaces']['wan']['mss'], $mss);
69
	$config['interfaces']['wan']['dhcphostname'] = $dhcphostname;
70

    
71
	if ($bogons == "on") {
72
		$config['interfaces']['wan']['blockbogons'] = "on";
73
	} else {
74
		unset($config['interfaces']['wan']['blockbogons']);
75
	}
76

    
77
	if ($rfc1918 == "on") {
78
		$config['interfaces']['wan']['blockpriv'] = "on";
79
	} else {
80
		unset($config['interfaces']['wan']['blockpriv']);
81
	}
82

    
83
	if (strlen($wangateway) > 0) {
84
		init_config_arr(array('gateways', 'gateway_item'));
85
		$found = false;
86
		$defaultgw_found = false;
87
		foreach ($config['gateways']['gateway_item'] as & $gw) {
88
			if ($gw['interface'] != "wan")
89
				continue;
90
			if (isset($gw['defaultgw']))
91
				$defaultgw_found = true;
92
			if ($gw['name'] == 'WANGW' || $gw['gateway'] == $wangateway) {
93
				$found = true;
94
				$gw['gateway'] = $wangateway;
95
				$config['interfaces']['wan']['gateway'] = $gw['name'];
96
			}
97
		}
98
		if (!$found) {
99
			$newgw = array();
100
			$newgw['interface'] = "wan";
101
			$newgw['gateway'] = $wangateway;
102
			$newgw['name'] = "WANGW";
103
			$newgw['weight'] = 1;
104
			$newgw['descr'] = "WAN Gateway";
105
			$newgw['defaultgw'] = !$defaultgw_found;
106
			$config['gateways']['gateway_item'][] = $newgw;
107
			$config['interfaces']['wan']['gateway'] = "WANGW";
108
		}
109
	}
110

    
111
	$pppfg = array();
112
	$type = $wantype;
113

    
114
	init_config_arr(array('ppps', 'ppp'));
115
	if (count($config['ppps']['ppp'])) {
116
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
117
			if ($ppp['ptpid'] == "0") {
118
				if ((substr($config['interfaces']['wan']['if'],0,5) == "pppoe") || (substr($config['interfaces']['wan']['if'],0,4) == "pptp")) {
119
					$oldif = explode(",", $ppp['ports']);
120
					$config['interfaces']['wan']['if'] = $oldif[0];
121
				}
122
				if ($type == "pppoe" || $type == "pptp")
123
					unset($config['ppps']['ppp'][$pppid]);
124
			}
125
		}
126
	}
127

    
128
	if ($type == "pppoe" || $type == "pptp") {
129
		if ($type == "pptp") {
130
			$pppfg['username'] = $pppUsername;
131
			$pppfg['ondemand'] = $pppOnDemand;
132
			$pppfg['idletimeout'] = $pppIdleTimeout;
133
		}
134

    
135
		$pppfg['password'] = base64_encode($pppPassword);
136
		$tmp = array();
137
		$tmp['ptpid'] = "0";
138
		$tmp['type'] = $type;
139
		$tmp['if'] = $type . "0";
140
		$tmp['ports'] = $config['interfaces']['wan']['if'];
141
		$config['ppps']['ppp'][] = array_merge($tmp, $pppfg);
142
		unset($tmp);
143
		$config['interfaces']['wan']['if'] = $type."0";
144
	}
145

    
146
	if(strpos($wanip, "/") !== false){
147
		$ip = explode("/", $wanip);
148
		$config['interfaces']['wan']['ipaddr'] = $ip[0];
149
		$config['interfaces']['wan']['subnet'] = $ip[1];		
150
	} else {
151
		$config['interfaces']['wan']['ipaddr'] = $wanip;
152
	}
153

    
154
	// LAN ========================================================================================================================
155
	$lp = explode("/", $lanip);
156
	$addr = $lp[0];
157
	$mask = $lp[1];
158

    
159
	$config['interfaces']['lan']['ipaddr'] = $addr;
160
	$config['interfaces']['lan']['subnet'] = $mask;
161

    
162
	init_config_arr(array('dhcpd', 'lan', 'range', 'from'));
163
	init_config_arr(array('dhcpd', 'lan', 'range', 'to'));
164

    
165
	if (!ip_in_subnet($config['dhcpd']['lan']['range']['from'], "{$addr}/{$mask}") ||
166
	    !ip_in_subnet($config['dhcpd']['lan']['range']['to'], "{$addr}/{$mask}")) {
167

    
168
		$ipaddresses_before = ip_range_size_v4($lowestip, $addr);
169
		$ipaddresses_after = ip_range_size_v4($addr, $highestip);
170
		if ($ipaddresses_after >= $ipaddresses_before) {
171
			// The LAN IP is in the 1st half of the subnet, so put DHCP in the 2nd half.
172
			if ($ipaddresses_after > 30) {
173
				// There is reasonable space in the subnet, use a smaller chunk of the space for DHCP
174
				// This case will work out like the old defaults if the user has specified the ".1" address.
175
				// The range will be something like ".10" to ".245"
176
				$config['dhcpd']['lan']['range']['from'] = ip_after($addr, 9);
177
				$config['dhcpd']['lan']['range']['to'] = ip_before($highestip, 10);
178
			} else {
179
				// There is not much space in the subnet, so allocate everything above the LAN IP to DHCP.
180
				$config['dhcpd']['lan']['range']['from'] = ip_after($addr);
181
				$config['dhcpd']['lan']['range']['to'] = ip_before($highestip);
182
			}
183
		} else {
184
			// The LAN IP is in the 2nd half of the subnet, so put DHCP in the 1st half.
185
			if ($ipaddresses_before > 30) {
186
				// There is reasonable space in the subnet, use a smaller chunk of the space for DHCP
187
				$config['dhcpd']['lan']['range']['from'] = ip_after($lowestip, 10);
188
				$config['dhcpd']['lan']['range']['to'] = ip_before($addr, 9);
189
			} else {
190
				// There is not much space in the subnet, so allocate everything below the LAN IP to DHCP.
191
				$config['dhcpd']['lan']['range']['from'] = ip_after($lowestip);
192
				$config['dhcpd']['lan']['range']['to'] = ip_before($addr);
193
			}
194
		}
195
	}
196

    
197
	// DNS ========================================================================================================================
198
	if ($dnsoverride == "on") {
199
		$config['system']['dnsallowoverride'] = "on";
200
	} else {
201
		unset($config['system']['dnsallowoverride']);
202
	}
203

    
204
	$config['system']['dnsserver'] = array($primarydns, $secondarydns);
205

    
206
	write_config("Configuration updated by setup wizard");
207
	reload_all();
208
	mwexec_bg("/etc/rc.update_bogons.sh now");
209
	touch("{$g['cf_conf_path']}/copynotice_display");
210
}
211

    
212
function setorforget(&$node, $value) {
213
   if(strlen($value) > 0) {
214
      $node = $value;
215
   } else {
216
      unset($node);
217
   }
218
}
219

    
220
?>
(57-57/62)