Project

General

Profile

Download (7.27 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 bbc3533f Scott Ullrich
37 e1454e42 Scott Ullrich
	function prompt_for_enable_dhcp_server() {
38 9f0280d9 Chris Buechler
		global $config, $fp, $interface;
39 e1454e42 Scott Ullrich
		if($interface == "wan") {
40 5daa88a6 Chris Buechler
			if($config['interfaces']['lan']) 
41 e1454e42 Scott Ullrich
				return "n";
42
		}
43
		/* only allow DHCP server to be enabled when static IP is
44
		  configured on this interface */
45 9f0280d9 Chris Buechler
		if (is_ipaddr($config['interfaces'][$interface]['ipaddr'])) {
46 e1454e42 Scott Ullrich
			do {
47
				$good = false;
48
				$upperifname = strtoupper($interface);
49
				echo "\n" . gettext("Do you want to enable the DHCP server on {$upperifname}? [y|n]") . "  ";
50
				$yn = strtolower(chop(fgets($fp)));
51
				if ($yn[0] == "y" or $yn[0] == "n")
52
				$good = true;
53
			} while (!$good);
54
		}
55
		return $yn;
56
	}
57
58 5b237745 Scott Ullrich
	$fp = fopen('php://stdin', 'r');
59 8dee794b Scott Ullrich
	$addr_blank = false;
60 bbc3533f Scott Ullrich
61 8dee794b Scott Ullrich
	/* build an interface collection */
62 bba3a775 Ermal Luçi
	$ifdescrs = get_configured_interface_with_descr();
63
	$j = count($ifdescrs);
64 534334ba Scott Ullrich
	
65 d01ccca8 Chris Buechler
	/* grab interface that we will operate on, unless there is only one
66
	interface */
67
	if ($j > 1) {
68
		echo "Available interfaces:\n\n";
69
		$x=1;
70
		foreach($ifdescrs as $iface) {
71 bba3a775 Ermal Luçi
			echo "{$x} - {$iface}\n";
72 d01ccca8 Chris Buechler
			$x++;
73
		}
74
		echo "\nEnter the number of the interface you wish to configure: ";
75
		$intnum = chop(fgets($fp));	
76
	} else {
77
		$intnum = $j;
78 8dee794b Scott Ullrich
	}
79 d01ccca8 Chris Buechler
	
80 8dee794b Scott Ullrich
	if($intnum < 1) 
81
		exit;
82
	if($intnum > $j)
83
		exit;
84
		
85 424b758b Scott Ullrich
	$index = 1;
86
	foreach ($ifdescrs as $ifname => $ifdesc) {
87
		if ($intnum == $index)  {
88
			$interface = $ifname;
89
			break;
90
		} else {
91
			$index++;
92
		}
93
	}	
94 8dee794b Scott Ullrich
	if(!$interface) {
95
		echo "Invalid interface!\n";
96
		exit;
97
	}
98
		
99 5b237745 Scott Ullrich
	do {
100 8dee794b Scott Ullrich
		
101
		if($interface == "wan") {
102 d01ccca8 Chris Buechler
			$upperifname = strtoupper($interface);
103
			echo gettext("Configure {$upperifname} interface via DHCP?  [y|n]") . "\n> ";
104 8dee794b Scott Ullrich
			$intdhcp = chop(fgets($fp));
105
			if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
106 79b322f0 Ermal Luçi
				$intip = "dhcp";
107 8dee794b Scott Ullrich
				$intbits = "";
108
				$isintdhcp = true;
109
			} 
110
		}
111
		
112
		if($isintdhcp == false or $interface <> "wan") {
113
			do {
114 d01ccca8 Chris Buechler
				$upperifname = strtoupper($interface);
115
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
116 8dee794b Scott Ullrich
				$intip = chop(fgets($fp));
117
				$addr_blank = false;
118
			} while (!(is_ipaddr($intip) || $intip == ''));
119 d01ccca8 Chris Buechler
		  	if ($intip != '') {
120
		  		echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
121
		  		echo "e.g. 255.255.255.0 = 24\n";
122
		  		echo "     255.255.0.0   = 16\n";
123
		  		echo "     255.0.0.0     = 8\n";
124
	
125
		  		do {
126
					$upperifname = strtoupper($interface);
127
		  			echo "\n" . gettext("Enter the new {$upperifname} IPv4 subnet bit count:") . "\n> ";
128
		  			$intbits = chop(fgets($fp));
129
		  		} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
130
		  	} 
131 5b237745 Scott Ullrich
		}
132
133 bbc3533f Scott Ullrich
134 8dee794b Scott Ullrich
	} while ($addr_blank);
135 bbc3533f Scott Ullrich
136 8dee794b Scott Ullrich
	$config['interfaces'][$interface]['ipaddr'] = $intip;
137
	$config['interfaces'][$interface]['subnet'] = $intbits;
138 3faf822d Scott Ullrich
	
139 e1454e42 Scott Ullrich
	$yn = prompt_for_enable_dhcp_server();
140
	
141 8dee794b Scott Ullrich
	// TODO: Add DHCP IPv6 support
142 78e63d64 Bill Marquette
	if ($yn == "y") {
143 5b237745 Scott Ullrich
		do {
144 8dee794b Scott Ullrich
			echo gettext("Enter the start address of the client address range:") . " ";
145 5b237745 Scott Ullrich
			$dhcpstartip = chop(fgets($fp));
146
			if ($dhcpstartip === "") {
147 942ed037 Scott Ullrich
				fclose($fp);
148 5b237745 Scott Ullrich
				exit(0);
149
			}
150 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpstartip)));
151 bbc3533f Scott Ullrich
152 5b237745 Scott Ullrich
		do {
153 8dee794b Scott Ullrich
			echo gettext("Enter the end address of the client address range:") . " ";
154 5b237745 Scott Ullrich
			$dhcpendip = chop(fgets($fp));
155
			if ($dhcpendip === "") {
156 942ed037 Scott Ullrich
				fclose($fp);
157 5b237745 Scott Ullrich
				exit(0);
158
			}
159 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpendip)));
160 bbc3533f Scott Ullrich
161 8dee794b Scott Ullrich
		$config['dhcpd'][$interface]['enable'] = true;
162
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
163
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
164 5b237745 Scott Ullrich
	} else {
165 8e2a7e8a Chris Buechler
		/* TODO - this line is causing a "Fatal error: Cannot unset
166
		  string offsets in /etc/rc.initial.setlanip" on below line
167
		  number */
168 a4c59720 Scott Ullrich
		if($config['dhcpd'][$interface])
169
			unset($config['dhcpd'][$interface]['enable']);
170 5b237745 Scott Ullrich
	}
171 bbc3533f Scott Ullrich
172 01d72b37 Scott Ullrich
	if ($config['system']['webgui']['protocol'] == "https") {
173
174
	  	do {
175
	      $good = false;
176
	  		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
177
	      $yn = strtolower(chop(fgets($fp)));
178
	      if ($yn[0] == "y" or $yn[0] == "n")
179
	        $good = true;
180
	    } while (!$good);
181
182
		if ($yn == "y")
183
			$config['system']['webgui']['protocol'] = "http";
184
	}
185 bbc3533f Scott Ullrich
186 01d72b37 Scott Ullrich
	if (isset($config['system']['webgui']['noantilockout'])) {
187
		echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
188
		unset($config['system']['webgui']['noantilockout']);
189 8dee794b Scott Ullrich
	}
190 bbc3533f Scott Ullrich
191 68ad6d22 Scott Ullrich
	if($config['interfaces']['lan']) {
192
		unset($config['dhcpd']['wan']);		
193
	}
194
195 3faf822d Scott Ullrich
	if(!$config['interfaces']['lan']) {
196 5bf11e36 Scott Ullrich
		unset($config['interfaces']['lan']);
197 b980f9e4 Bill Marquette
		if($config['dhcpd']['lan'])
198 7993688e Scott Ullrich
			unset($config['dhcpd']['lan']);
199 5bf11e36 Scott Ullrich
		unset($config['shaper']);
200
		unset($config['ezshaper']);
201
		unset($config['nat']);
202 12294e72 Bill Marquette
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
203 9f0280d9 Chris Buechler
		services_dhcpd_configure();
204 68ad6d22 Scott Ullrich
	}
205
206 61d6fbf1 Chris Buechler
	$upperifname = strtoupper($interface);
207 be824897 Chris Buechler
	echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
208 8dee794b Scott Ullrich
	write_config(gettext("{$interface} IP configuration from console menu"));
209 69e5a8be Ermal Luçi
	interface_configure("lan");
210 d1c46aef Scott Ullrich
	echo " Reloading filter...";
211
	filter_configure_sync();
212 2c382106 Scott Ullrich
	echo "\n";
213 d1c46aef Scott Ullrich
	
214 db747fb1 Chris Buechler
	if ($intip != '') {
215
		if (is_ipaddr($intip)) {
216
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
217
		} else {
218
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
219
		}
220
		if (count($ifdescrs) == "1" or $interface = "lan") {
221 ac6ddeff Bill Marquette
			if ($debug) {
222
				echo "ifdescrs count is " . count($ifdescrs) . "\n";
223
				echo "interface is {$interface} \n";
224
			}
225 db747fb1 Chris Buechler
			echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
226
			if(!empty($config['system']['webgui']['port'])) {
227
				$webuiport = $config['system']['webgui']['port'];
228
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
229
			} else {
230
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";	
231
			}
232 35380504 Seth Mos
		}
233 8dee794b Scott Ullrich
	}
234 5b237745 Scott Ullrich
235 8dee794b Scott Ullrich
	echo "\n" . gettext('Press <ENTER> to continue.');
236 5b237745 Scott Ullrich
237
	fgets($fp);
238 942ed037 Scott Ullrich
	fclose($fp);
239 68ad6d22 Scott Ullrich
		
240 b980f9e4 Bill Marquette
?>