Project

General

Profile

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

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

    
119
	if (count(config_get_path('ppps/ppp', []))) {
120
		foreach (config_get_path('ppps/ppp', []) as $pppid => $ppp) {
121
			if ($ppp['ptpid'] == "0") {
122
				if ((substr(config_get_path('interfaces/wan/if'),0,5) == "pppoe") || (substr(config_get_path('interfaces/wan/if'),0,4) == "pptp")) {
123
					$oldif = explode(",", $ppp['ports']);
124
					if (!empty($oldif[0])) {
125
						config_set_path('interfaces/wan/if', $oldif[0]);
126
					}
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
	if (!ip_in_subnet(config_get_path('dhcpd/lan/range/from'), "{$addr}/{$mask}") ||
169
	    !ip_in_subnet(config_get_path('dhcpd/lan/range/to'), "{$addr}/{$mask}")) {
170

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

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

    
207
	config_set_path('system/dnsserver', array($primarydns, $secondarydns));
208

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

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

    
223
?>
(57-57/61)