Project

General

Profile

Download (7.56 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 = get_configured_interface_with_descr();
63
	$j = count($ifdescrs);
64
	
65
	/* 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
			echo "{$x} - {$iface}\n";
72
			$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
	}
79
	
80
	if($intnum < 1) 
81
		exit;
82
	if($intnum > $j)
83
		exit;
84
		
85
	$index = 1;
86
	foreach ($ifdescrs as $ifname => $ifdesc) {
87
		if ($intnum == $index)  {
88
			$interface = $ifname;
89
			break;
90
		} else {
91
			$index++;
92
		}
93
	}	
94
	if(!$interface) {
95
		echo "Invalid interface!\n";
96
		exit;
97
	}
98
		
99
	do {
100
		
101
		if($interface == "wan") {
102
			$upperifname = strtoupper($interface);
103
			echo gettext("Configure {$upperifname} interface via DHCP?  [y|n]") . "\n> ";
104
			$intdhcp = chop(fgets($fp));
105
			if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
106
				$intip = "dhcp";
107
				$intbits = "";
108
				$isintdhcp = true;
109
				$restart_dhcpd = true;
110
			} 
111
		}
112
		
113
		if($isintdhcp == false or $interface <> "wan") {
114
			do {
115
				$upperifname = strtoupper($interface);
116
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
117
				$intip = chop(fgets($fp));
118
				$addr_blank = false;
119
			} while (!(is_ipaddr($intip) || $intip == ''));
120
		  	if ($intip != '') {
121
		  		echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
122
		  		echo "e.g. 255.255.255.0 = 24\n";
123
		  		echo "     255.255.0.0   = 16\n";
124
		  		echo "     255.0.0.0     = 8\n";
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
					$restart_dhcpd = true;
130
		  		} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
131
		  	} 
132
		}
133

    
134

    
135
	} while ($addr_blank);
136

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

    
154
		do {
155
			echo gettext("Enter the end address of the client address range:") . " ";
156
			$dhcpendip = chop(fgets($fp));
157
			if ($dhcpendip === "") {
158
				fclose($fp);
159
				exit(0);
160
			}
161
		} while (!(is_ipaddr($dhcpendip)));
162
		$restart_dhcpd = true;
163
		$config['dhcpd'][$interface]['enable'] = true;
164
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
165
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
166
	} else {
167
		/* TODO - this line is causing a "Fatal error: Cannot unset
168
		  string offsets in /etc/rc.initial.setlanip" on below line
169
		  number */
170
		if($config['dhcpd'][$interface]) 
171
			unset($config['dhcpd'][$interface]['enable']);
172
		echo "Disabling DHCPD...";
173
		services_dhcpd_configure();
174
		echo "Done!\n";
175
	}
176

    
177
	if ($config['system']['webgui']['protocol'] == "https") {
178

    
179
	  	do {
180
	      $good = false;
181
	  		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
182
	      $yn = strtolower(chop(fgets($fp)));
183
	      if ($yn[0] == "y" or $yn[0] == "n")
184
	        $good = true;
185
	    } while (!$good);
186

    
187
		if ($yn == "y")
188
			$config['system']['webgui']['protocol'] = "http";
189
	}
190

    
191
	if (isset($config['system']['webgui']['noantilockout'])) {
192
		echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
193
		unset($config['system']['webgui']['noantilockout']);
194
	}
195

    
196
	if($config['interfaces']['lan']) {
197
		unset($config['dhcpd']['wan']);		
198
	}
199

    
200
	if(!$config['interfaces']['lan']) {
201
		unset($config['interfaces']['lan']);
202
		if($config['dhcpd']['lan'])
203
			unset($config['dhcpd']['lan']);
204
		unset($config['shaper']);
205
		unset($config['ezshaper']);
206
		unset($config['nat']);
207
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
208
		services_dhcpd_configure();
209
	}
210

    
211
	$upperifname = strtoupper($interface);
212
	echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
213
	write_config(gettext("{$interface} IP configuration from console menu"));
214
	interface_configure(strtolower($upperifname));
215
	echo " Reloading filter...";
216
	filter_configure_sync();
217
	echo "\n";
218
	if($restart_dhcpd) {
219
		echo " DHCPD..."; 
220
		services_dhcpd_configure();
221
	}
222
	
223
	if ($intip != '') {
224
		if (is_ipaddr($intip)) {
225
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
226
		} else {
227
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
228
		}
229
		if (count($ifdescrs) == "1" or $interface = "lan") {
230
			if ($debug) {
231
				echo "ifdescrs count is " . count($ifdescrs) . "\n";
232
				echo "interface is {$interface} \n";
233
			}
234
			echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
235
			if(!empty($config['system']['webgui']['port'])) {
236
				$webuiport = $config['system']['webgui']['port'];
237
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
238
			} else {
239
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";	
240
			}
241
		}
242
	}
243

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

    
246
	fgets($fp);
247
	fclose($fp);
248
		
249
?>
(56-56/89)