Project

General

Profile

Download (6.97 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
	$ifdescrs = array ("wan");
63 534334ba Scott Ullrich
	if($config['interfaces']['lan'])  {
64
		$ifdescrs[] = "lan";
65
	}
66 d01ccca8 Chris Buechler
	for ($j = 0; isset ($config['interfaces']['opt' . $j]); $j++) {
67 8dee794b Scott Ullrich
		if(isset($config['interfaces']['opt' . $j]['enable']))
68
			$ifdescrs['opt' . $j] = filter_get_opt_interface_descr("opt" . $j);
69
	}
70 534334ba Scott Ullrich
	if($config['interfaces']['lan']) 
71
		$j++;
72 d01ccca8 Chris Buechler
	// increment for wan
73 534334ba Scott Ullrich
	$j++;
74
	
75 d01ccca8 Chris Buechler
	/* grab interface that we will operate on, unless there is only one
76
	interface */
77
	if ($j > 1) {
78
		echo "Available interfaces:\n\n";
79
		$x=1;
80
		foreach($ifdescrs as $iface) {
81
			echo "{$x} - " . strtoupper($iface) . "\n";
82
			$x++;
83
		}
84
		echo "\nEnter the number of the interface you wish to configure: ";
85
		$intnum = chop(fgets($fp));	
86
	} else {
87
		$intnum = $j;
88 8dee794b Scott Ullrich
	}
89 d01ccca8 Chris Buechler
	
90 8dee794b Scott Ullrich
	
91
	if($intnum < 1) 
92
		exit;
93
	if($intnum > $j)
94
		exit;
95
		
96 534334ba Scott Ullrich
	$interface = $ifdescrs[$intnum-1];
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
				$intip = "DHCP";
110
				$intbits = "";
111
				$isintdhcp = true;
112
			} 
113
		}
114
		
115
		if($isintdhcp == false or $interface <> "wan") {
116
			do {
117 d01ccca8 Chris Buechler
				$upperifname = strtoupper($interface);
118
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
119 8dee794b Scott Ullrich
				$intip = chop(fgets($fp));
120
				$addr_blank = false;
121
			} while (!(is_ipaddr($intip) || $intip == ''));
122 d01ccca8 Chris Buechler
		  	if ($intip != '') {
123
		  		echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
124
		  		echo "e.g. 255.255.255.0 = 24\n";
125
		  		echo "     255.255.0.0   = 16\n";
126
		  		echo "     255.0.0.0     = 8\n";
127
	
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
		  		} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
133
		  	} 
134 5b237745 Scott Ullrich
		}
135
136 bbc3533f Scott Ullrich
137 8dee794b Scott Ullrich
	} while ($addr_blank);
138 bbc3533f Scott Ullrich
139 8dee794b Scott Ullrich
	$config['interfaces'][$interface]['ipaddr'] = $intip;
140
	$config['interfaces'][$interface]['subnet'] = $intbits;
141 3faf822d Scott Ullrich
	
142 e1454e42 Scott Ullrich
	$yn = prompt_for_enable_dhcp_server();
143
	
144 8dee794b Scott Ullrich
	// TODO: Add DHCP IPv6 support
145 78e63d64 Bill Marquette
	if ($yn == "y") {
146 5b237745 Scott Ullrich
		do {
147 8dee794b Scott Ullrich
			echo gettext("Enter the start address of the client address range:") . " ";
148 5b237745 Scott Ullrich
			$dhcpstartip = chop(fgets($fp));
149
			if ($dhcpstartip === "") {
150 942ed037 Scott Ullrich
				fclose($fp);
151 5b237745 Scott Ullrich
				exit(0);
152
			}
153 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpstartip)));
154 bbc3533f Scott Ullrich
155 5b237745 Scott Ullrich
		do {
156 8dee794b Scott Ullrich
			echo gettext("Enter the end address of the client address range:") . " ";
157 5b237745 Scott Ullrich
			$dhcpendip = chop(fgets($fp));
158
			if ($dhcpendip === "") {
159 942ed037 Scott Ullrich
				fclose($fp);
160 5b237745 Scott Ullrich
				exit(0);
161
			}
162 3faf822d Scott Ullrich
		} while (!(is_ipaddr($dhcpendip)));
163 bbc3533f Scott Ullrich
164 8dee794b Scott Ullrich
		$config['dhcpd'][$interface]['enable'] = true;
165
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
166
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
167 5b237745 Scott Ullrich
	} else {
168 8e2a7e8a Chris Buechler
		/* TODO - this line is causing a "Fatal error: Cannot unset
169
		  string offsets in /etc/rc.initial.setlanip" on below line
170
		  number */
171 8dee794b Scott Ullrich
		unset($config['dhcpd'][$interface]['enable']);
172 5b237745 Scott Ullrich
	}
173 bbc3533f Scott Ullrich
174 01d72b37 Scott Ullrich
	if ($config['system']['webgui']['protocol'] == "https") {
175
176
	  	do {
177
	      $good = false;
178
	  		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
179
	      $yn = strtolower(chop(fgets($fp)));
180
	      if ($yn[0] == "y" or $yn[0] == "n")
181
	        $good = true;
182
	    } while (!$good);
183
184
		if ($yn == "y")
185
			$config['system']['webgui']['protocol'] = "http";
186
	}
187 bbc3533f Scott Ullrich
188 01d72b37 Scott Ullrich
	if (isset($config['system']['webgui']['noantilockout'])) {
189
		echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
190
		unset($config['system']['webgui']['noantilockout']);
191 8dee794b Scott Ullrich
	}
192 bbc3533f Scott Ullrich
193 68ad6d22 Scott Ullrich
	if($config['interfaces']['lan']) {
194
		unset($config['dhcpd']['wan']);		
195
	}
196
197 3faf822d Scott Ullrich
	if(!$config['interfaces']['lan']) {
198 5bf11e36 Scott Ullrich
		unset($config['interfaces']['lan']);
199 68ad6d22 Scott Ullrich
		unset($config['dhcpd']['lan']);
200 5bf11e36 Scott Ullrich
		unset($config['shaper']);
201
		unset($config['ezshaper']);
202
		unset($config['nat']);
203 f2366cc0 Scott Ullrich
		system("rm /var/dhcpd/var/db/*");
204 9f0280d9 Chris Buechler
		services_dhcpd_configure();
205 68ad6d22 Scott Ullrich
	}
206
207 61d6fbf1 Chris Buechler
	$upperifname = strtoupper($interface);
208 be824897 Chris Buechler
	echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
209 8dee794b Scott Ullrich
	write_config(gettext("{$interface} IP configuration from console menu"));
210
	interfaces_lan_configure();
211 d1c46aef Scott Ullrich
	echo " Reloading filter...";
212
	filter_configure_sync();
213 2c382106 Scott Ullrich
	echo "\n";
214 d1c46aef Scott Ullrich
	
215 8dee794b Scott Ullrich
	if ($intip != '') { 
216 534334ba Scott Ullrich
		echo "\n\n" . gettext("The IPv4 {$interface} address has been set to ") . "{$intip}/{$intbits}\n" ;
217 8dee794b Scott Ullrich
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
218 35380504 Seth Mos
		if(!empty($config['system']['webgui']['port'])) {
219
			$webuiport = $config['system']['webgui']['port'];
220
		}
221
		echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port/\n";
222 8dee794b Scott Ullrich
	}
223 5b237745 Scott Ullrich
224 8dee794b Scott Ullrich
	echo "\n" . gettext('Press <ENTER> to continue.');
225 5b237745 Scott Ullrich
226
	fgets($fp);
227 942ed037 Scott Ullrich
	fclose($fp);
228 68ad6d22 Scott Ullrich
		
229 35380504 Seth Mos
?>