Project

General

Profile

Download (7.28 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-2024 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
	$pppUsername,
49
	$pppPassword,
50
	$pppOnDemand,
51
	$pppIdleTimeout,
52
	$pppoeServiceName,
53
	$ppppoeLocalIP) {
54

    
55
	global $g;
56

    
57
	// General ====================================================================================================================
58
	config_set_path('system/hostname', $hostname);
59
	config_set_path('system/domain', $domain);
60

    
61
	// Time =======================================================================================================================
62
	config_set_path('system/timezone', $timezone);
63
	config_set_path('system/timeservers', $timeservers);
64

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

    
73
	if ($bogons == "on") {
74
		config_set_path('interfaces/wan/blockbogons', true);
75
	} else {
76
		config_del_path('interfaces/wan/blockbogons');
77
	}
78

    
79
	if ($rfc1918 == "on") {
80
		config_set_path('interfaces/wan/blockpriv', true);
81
	} else {
82
		config_del_path('interfaces/wan/blockpriv');
83
	}
84

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

    
117
	$pppfg = array();
118
	$type = $wantype;
119

    
120
	config_init_path('ppps/ppp');
121
	if (count(config_get_path('ppps/ppp', []))) {
122
		foreach (config_get_path('ppps/ppp', []) as $pppid => $ppp) {
123
			if ($ppp['ptpid'] == "0") {
124
				if ((substr(config_get_path('interfaces/wan/if'),0,5) == "pppoe") || (substr(config_get_path('interfaces/wan/if'),0,4) == "pptp")) {
125
					$oldif = explode(",", $ppp['ports']);
126
					config_set_path('interfaces/wan/if', $oldif[0]);
127
				}
128
				if ($type == "pppoe" || $type == "pptp")
129
					config_del_path("ppps/ppp/{$pppid}");
130
			}
131
		}
132
	}
133

    
134
	if ($type == "pppoe" || $type == "pptp") {
135
		if ($type == "pptp") {
136
			$pppfg['username'] = $pppUsername;
137
			$pppfg['ondemand'] = $pppOnDemand;
138
			$pppfg['idletimeout'] = $pppIdleTimeout;
139
		}
140

    
141
		$pppfg['password'] = base64_encode($pppPassword);
142
		$tmp = array();
143
		$tmp['ptpid'] = "0";
144
		$tmp['type'] = $type;
145
		$tmp['if'] = $type . "0";
146
		$tmp['ports'] = config_get_path('interfaces/wan/if');
147
		config_set_path('ppps/ppp/', array_merge($tmp, $pppfg));
148
		unset($tmp);
149
		config_set_path('interfaces/wan/if', $type."0");
150
	}
151

    
152
	if(strpos($wanip, "/") !== false){
153
		$ip = explode("/", $wanip);
154
		config_set_path('interfaces/wan/ipaddr', $ip[0]);
155
		config_set_path('interfaces/wan/subnet', $ip[1]);		
156
	} else {
157
		config_set_path('interfaces/wan/ipaddr', $wanip);
158
	}
159

    
160
	// LAN ========================================================================================================================
161
	$lp = explode("/", $lanip);
162
	$addr = $lp[0];
163
	$mask = $lp[1];
164

    
165
	config_set_path('interfaces/lan/ipaddr', $addr);
166
	config_set_path('interfaces/lan/subnet', $mask);
167

    
168
	config_init_path('dhcpd/lan/range/from');
169
	config_init_path('dhcpd/lan/range/to');
170

    
171
	if (!ip_in_subnet(config_get_path('dhcpd/lan/range/from'), "{$addr}/{$mask}") ||
172
	    !ip_in_subnet(config_get_path('dhcpd/lan/range/to'), "{$addr}/{$mask}")) {
173

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

    
203
	// DNS ========================================================================================================================
204
	if ($dnsoverride == "on") {
205
		config_set_path('system/dnsallowoverride', "on");
206
	} else {
207
		config_del_path('system/dnsallowoverride');
208
	}
209

    
210
	config_set_path('system/dnsserver', array($primarydns, $secondarydns));
211

    
212
	write_config("Configuration updated by setup wizard");
213
	reload_all();
214
	mwexec_bg("/etc/rc.update_bogons.sh now");
215
	touch("{$g['cf_conf_path']}/copynotice_display");
216
}
217

    
218
function setorforget(&$node, $value) {
219
   if(strlen($value) > 0) {
220
      $node = $value;
221
   } else {
222
      unset($node);
223
   }
224
}
225

    
226
?>
(57-57/61)