Project

General

Profile

Download (8.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
	require_once("filter.inc");
37
	require_once("shaper.inc");
38
	require_once("rrd.inc");
39

    
40
	function console_get_interface_from_ppp($realif) {
41
		global $config;
42

    
43
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
44
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
45
				if ($realif == $ppp['if']) {
46
					$ifaces = explode(",", $ppp['ports']);
47
					return $ifaces[0];
48
				}
49
			}
50
		}
51

    
52
		return "";
53
	}
54

    
55
	function prompt_for_enable_dhcp_server() {
56
		global $config, $fp, $interface;
57
		if($interface == "wan") {
58
			if($config['interfaces']['lan']) 
59
				return "n";
60
		}
61
		/* only allow DHCP server to be enabled when static IP is
62
		  configured on this interface */
63
		if (is_ipaddr($config['interfaces'][$interface]['ipaddr'])) {
64
			do {
65
				$good = false;
66
				$upperifname = strtoupper($interface);
67
				echo "\n" . gettext("Do you want to enable the DHCP server on {$upperifname}? [y|n]") . "  ";
68
				$yn = strtolower(chop(fgets($fp)));
69
				if ($yn[0] == "y" or $yn[0] == "n")
70
				$good = true;
71
			} while (!$good);
72
		}
73
		return $yn;
74
	}
75

    
76
	$fp = fopen('php://stdin', 'r');
77
	$addr_blank = false;
78

    
79
	/* build an interface collection */
80
	$ifdescrs = get_configured_interface_with_descr(false, true);
81
	$j = count($ifdescrs);
82
	
83
	/* grab interface that we will operate on, unless there is only one
84
	interface */
85
	if ($j > 1) {
86
		echo "Available interfaces:\n\n";
87
		$x=1;
88
		foreach($ifdescrs as $iface) {
89
			echo "{$x} - {$iface}\n";
90
			$x++;
91
		}
92
		echo "\nEnter the number of the interface you wish to configure: ";
93
		$intnum = chop(fgets($fp));	
94
	} else {
95
		$intnum = $j;
96
	}
97
	
98
	if($intnum < 1) 
99
		exit;
100
	if($intnum > $j)
101
		exit;
102
		
103
	$index = 1;
104
	foreach ($ifdescrs as $ifname => $ifdesc) {
105
		if ($intnum == $index)  {
106
			$interface = $ifname;
107
			break;
108
		} else {
109
			$index++;
110
		}
111
	}	
112
	if(!$interface) {
113
		echo "Invalid interface!\n";
114
		exit;
115
	}
116

    
117
	$ifaceassigned = "";
118
	do {
119
		if($interface == "wan") {
120
			$upperifname = strtoupper($interface);
121
			echo gettext("Configure {$upperifname} interface via DHCP?  [y|n]") . "\n> ";
122
			$intdhcp = chop(fgets($fp));
123
			if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
124
				$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
125
				if (!empty($ifppp))
126
					$ifaceassigned = $ifppp;
127
				$intip = "dhcp";
128
				$intbits = "";
129
				$isintdhcp = true;
130
				$restart_dhcpd = true;
131
			} 
132
		}
133
		
134
		if($isintdhcp == false or $interface <> "wan") {
135
			do {
136
				$upperifname = strtoupper($interface);
137
				echo "\n" . gettext("Enter the new {$upperifname} IPv4 address.  Press <ENTER> for none:") . "\n> ";
138
				$intip = chop(fgets($fp));
139
				$addr_blank = false;
140
			} while (!(is_ipaddr($intip) || $intip == ''));
141
		  	if ($intip != '') {
142
		  		echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
143
		  		echo "e.g. 255.255.255.0 = 24\n";
144
		  		echo "     255.255.0.0   = 16\n";
145
		  		echo "     255.0.0.0     = 8\n";
146
		  		do {
147
					$upperifname = strtoupper($interface);
148
		  			echo "\n" . gettext("Enter the new {$upperifname} IPv4 subnet bit count:") . "\n> ";
149
		  			$intbits = chop(fgets($fp));
150
					$restart_dhcpd = true;
151
		  		} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
152
		  	}
153
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
154
			if (!empty($ifppp))
155
				$ifaceassigned = $ifppp;
156
		}
157

    
158

    
159
	} while ($addr_blank);
160

    
161
	if (!empty($ifaceassigned))
162
		$config['interfaces'][$interface]['if'] = $ifaceassigned;
163
	$config['interfaces'][$interface]['ipaddr'] = $intip;
164
	$config['interfaces'][$interface]['subnet'] = $intbits;
165
	$config['interfaces'][$interface]['enable'] = true;
166
	
167
	if($g['services_dhcp_server_enable'])
168
		$yn = prompt_for_enable_dhcp_server();
169
	
170
	// TODO: Add DHCP IPv6 support
171
	if ($yn == "y") {
172
		do {
173
			echo gettext("Enter the start address of the client address range:") . " ";
174
			$dhcpstartip = chop(fgets($fp));
175
			if ($dhcpstartip === "") {
176
				fclose($fp);
177
				exit(0);
178
			}
179
		} while (!(is_ipaddr($dhcpstartip)));
180

    
181
		do {
182
			echo gettext("Enter the end address of the client address range:") . " ";
183
			$dhcpendip = chop(fgets($fp));
184
			if ($dhcpendip === "") {
185
				fclose($fp);
186
				exit(0);
187
			}
188
		} while (!(is_ipaddr($dhcpendip)));
189
		$restart_dhcpd = true;
190
		$config['dhcpd'][$interface]['enable'] = true;
191
		$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
192
		$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
193
	} else {
194
		/* TODO - this line is causing a "Fatal error: Cannot unset
195
		  string offsets in /etc/rc.initial.setlanip" on below line
196
		  number */
197
		if($config['dhcpd'][$interface]) 
198
			unset($config['dhcpd'][$interface]['enable']);
199
		echo "Disabling DHCPD...";
200
		services_dhcpd_configure();
201
		echo "Done!\n";
202
	}
203

    
204
	if ($config['system']['webgui']['protocol'] == "https") {
205

    
206
	  	do {
207
	      $good = false;
208
	  		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
209
	      $yn = strtolower(chop(fgets($fp)));
210
	      if ($yn[0] == "y" or $yn[0] == "n")
211
	        $good = true;
212
	    } while (!$good);
213

    
214
		if ($yn == "y") {
215
			$config['system']['webgui']['protocol'] = "http";
216
			$restart_webgui = true;
217
		}
218
	}
219

    
220
	if (isset($config['system']['webgui']['noantilockout'])) {
221
		echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
222
		unset($config['system']['webgui']['noantilockout']);
223
	}
224

    
225
	if($config['interfaces']['lan']) {
226
		if($config['dhcpd'])
227
			if($config['dhcpd']['wan'])
228
				unset($config['dhcpd']['wan']);		
229
	}
230

    
231
	if(!$config['interfaces']['lan']) {
232
		unset($config['interfaces']['lan']);
233
		if($config['dhcpd']['lan'])
234
			unset($config['dhcpd']['lan']);
235
		unset($config['shaper']);
236
		unset($config['ezshaper']);
237
		unset($config['nat']);
238
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
239
		services_dhcpd_configure();
240
	}
241

    
242
	$upperifname = strtoupper($interface);
243
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
244
	write_config(gettext("{$interface} IP configuration from console menu"));
245
	interface_reconfigure(strtolower($upperifname));
246
	echo " Reloading filter...";
247
	filter_configure_sync();
248
	echo "\n";
249
	if($restart_dhcpd) {
250
		echo " DHCPD..."; 
251
		services_dhcpd_configure();
252
	}
253
	if($restart_webgui) {
254
		echo " restarting webConfigurator... ";
255
		mwexec("/etc/rc.restart_webgui");
256
	}
257
	
258
	if ($intip != '') {
259
		if (is_ipaddr($intip)) {
260
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
261
		} else {
262
			echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
263
		}
264
		if (count($ifdescrs) == "1" or $interface = "lan") {
265
			if ($debug) {
266
				echo "ifdescrs count is " . count($ifdescrs) . "\n";
267
				echo "interface is {$interface} \n";
268
			}
269
			echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
270
			if(!empty($config['system']['webgui']['port'])) {
271
				$webuiport = $config['system']['webgui']['port'];
272
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
273
			} else {
274
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";	
275
			}
276
		}
277
	}
278

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

    
281
	fgets($fp);
282
	fclose($fp);
283
		
284
?>
(64-64/104)