Project

General

Profile

Download (6.97 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
<?php
3
/* $Id$ */
4
/*
5
	rc.initial.setlanip
6
	part of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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

    
37
	function prompt_for_enable_dhcp_server() {
38
		global $config, $fp, $interface;
39
		if($interface == "wan") {
40
			if($config['interfaces']['lan']) 
41
				return "n";
42
		}
43
		/* only allow DHCP server to be enabled when static IP is
44
		  configured on this interface */
45
		if (is_ipaddr($config['interfaces'][$interface]['ipaddr'])) {
46
			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
	$fp = fopen('php://stdin', 'r');
59
	$addr_blank = false;
60

    
61
	/* build an interface collection */
62
	$ifdescrs = array ("wan");
63
	if($config['interfaces']['lan'])  {
64
		$ifdescrs[] = "lan";
65
	}
66
	for ($j = 0; isset ($config['interfaces']['opt' . $j]); $j++) {
67
		if(isset($config['interfaces']['opt' . $j]['enable']))
68
			$ifdescrs['opt' . $j] = filter_get_opt_interface_descr("opt" . $j);
69
	}
70
	if($config['interfaces']['lan']) 
71
		$j++;
72
	// increment for wan
73
	$j++;
74
	
75
	/* 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
	}
89
	
90
	
91
	if($intnum < 1) 
92
		exit;
93
	if($intnum > $j)
94
		exit;
95
		
96
	$interface = $ifdescrs[$intnum-1];
97
	if(!$interface) {
98
		echo "Invalid interface!\n";
99
		exit;
100
	}
101
		
102
	do {
103
		
104
		if($interface == "wan") {
105
			$upperifname = strtoupper($interface);
106
			echo gettext("Configure {$upperifname} interface via DHCP?  [y|n]") . "\n> ";
107
			$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
				$upperifname = strtoupper($interface);
118
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
119
				$intip = chop(fgets($fp));
120
				$addr_blank = false;
121
			} while (!(is_ipaddr($intip) || $intip == ''));
122
		  	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
		}
135

    
136

    
137
	} while ($addr_blank);
138

    
139
	$config['interfaces'][$interface]['ipaddr'] = $intip;
140
	$config['interfaces'][$interface]['subnet'] = $intbits;
141
	
142
	$yn = prompt_for_enable_dhcp_server();
143
	
144
	// TODO: Add DHCP IPv6 support
145
	if ($yn == "y") {
146
		do {
147
			echo gettext("Enter the start address of the client address range:") . " ";
148
			$dhcpstartip = chop(fgets($fp));
149
			if ($dhcpstartip === "") {
150
				fclose($fp);
151
				exit(0);
152
			}
153
		} while (!(is_ipaddr($dhcpstartip)));
154

    
155
		do {
156
			echo gettext("Enter the end address of the client address range:") . " ";
157
			$dhcpendip = chop(fgets($fp));
158
			if ($dhcpendip === "") {
159
				fclose($fp);
160
				exit(0);
161
			}
162
		} while (!(is_ipaddr($dhcpendip)));
163

    
164
		$config['dhcpd'][$interface]['enable'] = true;
165
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
166
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
167
	} else {
168
		/* TODO - this line is causing a "Fatal error: Cannot unset
169
		  string offsets in /etc/rc.initial.setlanip" on below line
170
		  number */
171
		unset($config['dhcpd'][$interface]['enable']);
172
	}
173

    
174
	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

    
188
	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
	}
192

    
193
	if($config['interfaces']['lan']) {
194
		unset($config['dhcpd']['wan']);		
195
	}
196

    
197
	if(!$config['interfaces']['lan']) {
198
		unset($config['interfaces']['lan']);
199
		unset($config['dhcpd']['lan']);
200
		unset($config['shaper']);
201
		unset($config['ezshaper']);
202
		unset($config['nat']);
203
		system("rm /var/dhcpd/var/db/*");
204
		services_dhcpd_configure();
205
	}
206

    
207
	$upperifname = strtoupper($interface);
208
	echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
209
	write_config(gettext("{$interface} IP configuration from console menu"));
210
	interfaces_lan_configure();
211
	echo " Reloading filter...";
212
	filter_configure_sync();
213
	echo "\n";
214
	
215
	if ($intip != '') { 
216
		echo "\n\n" . gettext("The IPv4 {$interface} address has been set to ") . "{$intip}/{$intbits}\n" ;
217
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
218
		if(!empty($config['system']['webgui']['port'])) {
219
			$webuiport = $config['system']['webgui']['port'];
220
		}
221
		echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port/\n";
222
	}
223

    
224
	echo "\n" . gettext('Press <ENTER> to continue.');
225

    
226
	fgets($fp);
227
	fclose($fp);
228
		
229
?>
(48-48/79)