Project

General

Profile

Download (7.89 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/usr/local/bin/php -f
2
<?php
3 1b8df11b Bill Marquette
/* $Id$ */
4 5b237745 Scott Ullrich
/*
5
	rc.initial.setlanip
6
	part of m0n0wall (http://m0n0.ch/wall)
7 bbc3533f Scott Ullrich
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 bbc3533f Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 bbc3533f Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 bbc3533f Scott Ullrich
17 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20 bbc3533f Scott Ullrich
21 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
	/* parse the configuration and include all functions used below */
34
	require_once("config.inc");
35
	require_once("functions.inc");
36 5f2d078e Scott Ullrich
	require_once("filter.inc");
37
	require_once("shaper.inc");
38
	require_once("rrd.inc");
39 bbc3533f Scott Ullrich
40 e1454e42 Scott Ullrich
	function prompt_for_enable_dhcp_server() {
41 9f0280d9 Chris Buechler
		global $config, $fp, $interface;
42 e1454e42 Scott Ullrich
		if($interface == "wan") {
43 5daa88a6 Chris Buechler
			if($config['interfaces']['lan']) 
44 e1454e42 Scott Ullrich
				return "n";
45
		}
46
		/* only allow DHCP server to be enabled when static IP is
47
		  configured on this interface */
48 9f0280d9 Chris Buechler
		if (is_ipaddr($config['interfaces'][$interface]['ipaddr'])) {
49 e1454e42 Scott Ullrich
			do {
50
				$good = false;
51
				$upperifname = strtoupper($interface);
52
				echo "\n" . gettext("Do you want to enable the DHCP server on {$upperifname}? [y|n]") . "  ";
53
				$yn = strtolower(chop(fgets($fp)));
54
				if ($yn[0] == "y" or $yn[0] == "n")
55
				$good = true;
56
			} while (!$good);
57
		}
58
		return $yn;
59
	}
60
61 5b237745 Scott Ullrich
	$fp = fopen('php://stdin', 'r');
62 8dee794b Scott Ullrich
	$addr_blank = false;
63 bbc3533f Scott Ullrich
64 8dee794b Scott Ullrich
	/* build an interface collection */
65 c9fa8254 Scott Ullrich
	$ifdescrs = get_configured_interface_with_descr(false, true);
66 bba3a775 Ermal Luçi
	$j = count($ifdescrs);
67 534334ba Scott Ullrich
	
68 d01ccca8 Chris Buechler
	/* grab interface that we will operate on, unless there is only one
69
	interface */
70
	if ($j > 1) {
71
		echo "Available interfaces:\n\n";
72
		$x=1;
73
		foreach($ifdescrs as $iface) {
74 bba3a775 Ermal Luçi
			echo "{$x} - {$iface}\n";
75 d01ccca8 Chris Buechler
			$x++;
76
		}
77
		echo "\nEnter the number of the interface you wish to configure: ";
78
		$intnum = chop(fgets($fp));	
79
	} else {
80
		$intnum = $j;
81 8dee794b Scott Ullrich
	}
82 d01ccca8 Chris Buechler
	
83 8dee794b Scott Ullrich
	if($intnum < 1) 
84
		exit;
85
	if($intnum > $j)
86
		exit;
87
		
88 424b758b Scott Ullrich
	$index = 1;
89
	foreach ($ifdescrs as $ifname => $ifdesc) {
90
		if ($intnum == $index)  {
91
			$interface = $ifname;
92
			break;
93
		} else {
94
			$index++;
95
		}
96
	}	
97 8dee794b Scott Ullrich
	if(!$interface) {
98
		echo "Invalid interface!\n";
99
		exit;
100
	}
101
		
102 5b237745 Scott Ullrich
	do {
103 8dee794b Scott Ullrich
		
104
		if($interface == "wan") {
105 d01ccca8 Chris Buechler
			$upperifname = strtoupper($interface);
106
			echo gettext("Configure {$upperifname} interface via DHCP?  [y|n]") . "\n> ";
107 8dee794b Scott Ullrich
			$intdhcp = chop(fgets($fp));
108
			if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
109 79b322f0 Ermal Luçi
				$intip = "dhcp";
110 8dee794b Scott Ullrich
				$intbits = "";
111
				$isintdhcp = true;
112 1dcba27c Scott Ullrich
				$restart_dhcpd = true;
113 8dee794b Scott Ullrich
			} 
114
		}
115
		
116
		if($isintdhcp == false or $interface <> "wan") {
117
			do {
118 d01ccca8 Chris Buechler
				$upperifname = strtoupper($interface);
119
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
120 8dee794b Scott Ullrich
				$intip = chop(fgets($fp));
121
				$addr_blank = false;
122
			} while (!(is_ipaddr($intip) || $intip == ''));
123 d01ccca8 Chris Buechler
		  	if ($intip != '') {
124
		  		echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
125
		  		echo "e.g. 255.255.255.0 = 24\n";
126
		  		echo "     255.255.0.0   = 16\n";
127
		  		echo "     255.0.0.0     = 8\n";
128
		  		do {
129
					$upperifname = strtoupper($interface);
130
		  			echo "\n" . gettext("Enter the new {$upperifname} IPv4 subnet bit count:") . "\n> ";
131
		  			$intbits = chop(fgets($fp));
132 1dcba27c Scott Ullrich
					$restart_dhcpd = true;
133 d01ccca8 Chris Buechler
		  		} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
134
		  	} 
135 5b237745 Scott Ullrich
		}
136
137 bbc3533f Scott Ullrich
138 8dee794b Scott Ullrich
	} while ($addr_blank);
139 bbc3533f Scott Ullrich
140 8dee794b Scott Ullrich
	$config['interfaces'][$interface]['ipaddr'] = $intip;
141
	$config['interfaces'][$interface]['subnet'] = $intbits;
142 9b2e42c9 Ermal
	$config['interfaces'][$interface]['enable'] = true;
143 3faf822d Scott Ullrich
	
144 e3a13b00 Scott Ullrich
	if($g['services_dhcp_server_enable'])
145
		$yn = prompt_for_enable_dhcp_server();
146 e1454e42 Scott Ullrich
	
147 8dee794b Scott Ullrich
	// TODO: Add DHCP IPv6 support
148 78e63d64 Bill Marquette
	if ($yn == "y") {
149 5b237745 Scott Ullrich
		do {
150 8dee794b Scott Ullrich
			echo gettext("Enter the start address of the client address range:") . " ";
151 5b237745 Scott Ullrich
			$dhcpstartip = chop(fgets($fp));
152
			if ($dhcpstartip === "") {
153 942ed037 Scott Ullrich
				fclose($fp);
154 5b237745 Scott Ullrich
				exit(0);
155
			}
156 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpstartip)));
157 bbc3533f Scott Ullrich
158 5b237745 Scott Ullrich
		do {
159 8dee794b Scott Ullrich
			echo gettext("Enter the end address of the client address range:") . " ";
160 5b237745 Scott Ullrich
			$dhcpendip = chop(fgets($fp));
161
			if ($dhcpendip === "") {
162 942ed037 Scott Ullrich
				fclose($fp);
163 5b237745 Scott Ullrich
				exit(0);
164
			}
165 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpendip)));
166 1dcba27c Scott Ullrich
		$restart_dhcpd = true;
167 8dee794b Scott Ullrich
		$config['dhcpd'][$interface]['enable'] = true;
168
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
169
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
170 5b237745 Scott Ullrich
	} else {
171 8e2a7e8a Chris Buechler
		/* TODO - this line is causing a "Fatal error: Cannot unset
172
		  string offsets in /etc/rc.initial.setlanip" on below line
173
		  number */
174 af25d415 Chris Buechler
		if($config['dhcpd'][$interface]) 
175 a4c59720 Scott Ullrich
			unset($config['dhcpd'][$interface]['enable']);
176 1dcba27c Scott Ullrich
		echo "Disabling DHCPD...";
177 af25d415 Chris Buechler
		services_dhcpd_configure();
178 1dcba27c Scott Ullrich
		echo "Done!\n";
179 5b237745 Scott Ullrich
	}
180 bbc3533f Scott Ullrich
181 01d72b37 Scott Ullrich
	if ($config['system']['webgui']['protocol'] == "https") {
182
183
	  	do {
184
	      $good = false;
185
	  		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
186
	      $yn = strtolower(chop(fgets($fp)));
187
	      if ($yn[0] == "y" or $yn[0] == "n")
188
	        $good = true;
189
	    } while (!$good);
190
191 a64b5d3f Chris Buechler
		if ($yn == "y") {
192 01d72b37 Scott Ullrich
			$config['system']['webgui']['protocol'] = "http";
193 a64b5d3f Chris Buechler
			$restart_webgui = true;
194
		}
195 01d72b37 Scott Ullrich
	}
196 bbc3533f Scott Ullrich
197 01d72b37 Scott Ullrich
	if (isset($config['system']['webgui']['noantilockout'])) {
198
		echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
199
		unset($config['system']['webgui']['noantilockout']);
200 8dee794b Scott Ullrich
	}
201 bbc3533f Scott Ullrich
202 68ad6d22 Scott Ullrich
	if($config['interfaces']['lan']) {
203 20252ff5 Scott Ullrich
		if($config['dhcpd'])
204
			if($config['dhcpd']['wan'])
205
				unset($config['dhcpd']['wan']);		
206 68ad6d22 Scott Ullrich
	}
207
208 3faf822d Scott Ullrich
	if(!$config['interfaces']['lan']) {
209 5bf11e36 Scott Ullrich
		unset($config['interfaces']['lan']);
210 b980f9e4 Bill Marquette
		if($config['dhcpd']['lan'])
211 7993688e Scott Ullrich
			unset($config['dhcpd']['lan']);
212 5bf11e36 Scott Ullrich
		unset($config['shaper']);
213
		unset($config['ezshaper']);
214
		unset($config['nat']);
215 12294e72 Bill Marquette
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
216 9f0280d9 Chris Buechler
		services_dhcpd_configure();
217 68ad6d22 Scott Ullrich
	}
218
219 61d6fbf1 Chris Buechler
	$upperifname = strtoupper($interface);
220 be824897 Chris Buechler
	echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
221 8dee794b Scott Ullrich
	write_config(gettext("{$interface} IP configuration from console menu"));
222 e8dba746 Scott Ullrich
	interface_configure(strtolower($upperifname));
223 d1c46aef Scott Ullrich
	echo " Reloading filter...";
224
	filter_configure_sync();
225 2c382106 Scott Ullrich
	echo "\n";
226 1dcba27c Scott Ullrich
	if($restart_dhcpd) {
227
		echo " DHCPD..."; 
228
		services_dhcpd_configure();
229
	}
230 a64b5d3f Chris Buechler
	if($restart_webgui) {
231
		echo " restarting webConfigurator... ";
232
		mwexec("/etc/rc.restart_webgui");
233
	}
234 d1c46aef Scott Ullrich
	
235 db747fb1 Chris Buechler
	if ($intip != '') {
236
		if (is_ipaddr($intip)) {
237
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
238
		} else {
239
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
240
		}
241
		if (count($ifdescrs) == "1" or $interface = "lan") {
242 ac6ddeff Bill Marquette
			if ($debug) {
243
				echo "ifdescrs count is " . count($ifdescrs) . "\n";
244
				echo "interface is {$interface} \n";
245
			}
246 db747fb1 Chris Buechler
			echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
247
			if(!empty($config['system']['webgui']['port'])) {
248
				$webuiport = $config['system']['webgui']['port'];
249
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
250
			} else {
251
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";	
252
			}
253 35380504 Seth Mos
		}
254 8dee794b Scott Ullrich
	}
255 5b237745 Scott Ullrich
256 8dee794b Scott Ullrich
	echo "\n" . gettext('Press <ENTER> to continue.');
257 5b237745 Scott Ullrich
258
	fgets($fp);
259 942ed037 Scott Ullrich
	fclose($fp);
260 68ad6d22 Scott Ullrich
		
261 9b2e42c9 Ermal
?>