Project

General

Profile

Download (10.9 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($version = 4) {
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 ($version === 6) {
64
		$is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
65
	} else {
66
		$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
67
	}
68
	if ($is_ipaddr) {
69
		$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
70
		do {
71
			$good = false;
72
			$upperifname = strtoupper($interface);
73
			echo "\n" . gettext("Do you want to enable the {$label_DHCP} server on {$upperifname}? [y|n]") . "  ";
74
			$yn = strtolower(chop(fgets($fp)));
75
			if ($yn[0] == "y" or $yn[0] == "n")
76
				$good = true;
77
		} while (!$good);
78
	}
79
	return $yn;
80
}
81

    
82
$fp = fopen('php://stdin', 'r');
83

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

    
122
$ifaceassigned = "";
123

    
124
function console_configure_ip_address($version) {
125
	global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;
126

    
127
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
128
	$maxbits    = ($version === 6) ? 127      : 31;
129
	$label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";
130

    
131
	$upperifname = strtoupper($interface);
132

    
133
	if($interface == "wan") {
134
		echo gettext("Configure {$label_IPvX} address {$upperifname} interface via {$label_DHCP}?  [y|n]") . "\n> ";
135
		$intdhcp = chop(fgets($fp));
136
		if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
137
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
138
			if (!empty($ifppp))
139
				$ifaceassigned = $ifppp;
140
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
141
			$intbits = "";
142
			$isintdhcp = true;
143
			$restart_dhcpd = true;
144
		} 
145
	}
146
		
147
	if($isintdhcp == false or $interface <> "wan") {
148
		do {
149
			echo "\n" . gettext("Enter the new {$upperifname} {$label_IPvX} address.  Press <ENTER> for none:") . "\n> ";
150
			$intip = chop(fgets($fp));
151
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
152
		} while (!($is_ipaddr || $intip == ''));
153
		if ($intip != '') {
154
			echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
155
			if ($version === 6) {
156
				echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
157
				echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
158
				echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
159
				echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
160
				echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
161
			} else {
162
				echo "e.g. 255.255.255.0 = 24\n";
163
				echo "     255.255.0.0   = 16\n";
164
				echo "     255.0.0.0     = 8\n";
165
			}
166
			do {
167
				$upperifname = strtoupper($interface);
168
				echo "\n" . gettext("Enter the new {$upperifname} {$label_IPvX} subnet bit count:") . "\n> ";
169
				$intbits = chop(fgets($fp));
170
				$restart_dhcpd = true;
171
			} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > $maxbits));
172
		}
173
		$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
174
		if (!empty($ifppp))
175
			$ifaceassigned = $ifppp;
176
	}
177

    
178
	return array($intip, $intbits);
179
}
180

    
181
list($intip,  $intbits)  = console_configure_ip_address(4);
182
list($intip6, $intbits6) = console_configure_ip_address(6);
183

    
184
if (!empty($ifaceassigned))
185
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
186
$config['interfaces'][$interface]['ipaddr'] = $intip;
187
$config['interfaces'][$interface]['subnet'] = $intbits;
188
if ($intip6) {
189
	$config['interfaces'][$interface]['ipaddrv6'] = $intip6;
190
	$config['interfaces'][$interface]['subnetv6'] = $intbits6;
191
}
192
$config['interfaces'][$interface]['enable'] = true;
193

    
194
function console_configure_dhcpd($version = 4) {
195
	global $g, $config, $restart_dhcpd, $fp, $interface;
196

    
197
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
198
	$dhcpd      = ($version === 6) ? "dhcpd6" : "dhcpd";
199

    
200
	if($g['services_dhcp_server_enable'])
201
		$yn = prompt_for_enable_dhcp_server($version);
202
	if ($yn == "y") {
203
		do {
204
			echo gettext("Enter the start address of the {$label_IPvX} client address range:") . " ";
205
			$dhcpstartip = chop(fgets($fp));
206
			if ($dhcpstartip === "") {
207
				fclose($fp);
208
				exit(0);
209
			}
210
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
211
		} while (!$is_ipaddr);
212

    
213
		do {
214
			echo gettext("Enter the end address of the {$label_IPvX} client address range:") . " ";
215
			$dhcpendip = chop(fgets($fp));
216
			if ($dhcpendip === "") {
217
				fclose($fp);
218
				exit(0);
219
			}
220
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
221
		} while (!$is_ipaddr);
222
		$restart_dhcpd = true;
223
		$config[$dhcpd][$interface]['enable'] = true;
224
		$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
225
		$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
226
	} else {
227
		/* TODO - this line is causing a "Fatal error: Cannot unset
228
		   string offsets in /etc/rc.initial.setlanip" on below line
229
		   number */
230
		if($config[$dhcpd][$interface]) 
231
			unset($config[$dhcpd][$interface]['enable']);
232
		echo "Disabling DHCPD...";
233
		services_dhcpd_configure();
234
		echo "Done!\n";
235
	}
236
}
237

    
238
console_configure_dhcpd(4);
239
console_configure_dhcpd(6);
240
	
241
//*****************************************************************************
242

    
243
if ($config['system']['webgui']['protocol'] == "https") {
244

    
245
	do {
246
		$good = false;
247
		echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
248
		$yn = strtolower(chop(fgets($fp)));
249
		if ($yn[0] == "y" or $yn[0] == "n")
250
			$good = true;
251
	} while (!$good);
252

    
253
	if ($yn == "y") {
254
		$config['system']['webgui']['protocol'] = "http";
255
		$restart_webgui = true;
256
	}
257
}
258

    
259
if (isset($config['system']['webgui']['noantilockout'])) {
260
	echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
261
	unset($config['system']['webgui']['noantilockout']);
262
}
263

    
264
if($config['interfaces']['lan']) {
265
	if($config['dhcpd'])
266
		if($config['dhcpd']['wan'])
267
			unset($config['dhcpd']['wan']);		
268
	if($config['dhcpd6'])
269
		if($config['dhcpd6']['wan'])
270
			unset($config['dhcpd6']['wan']);		
271
}
272

    
273
if(!$config['interfaces']['lan']) {
274
	unset($config['interfaces']['lan']);
275
	if($config['dhcpd']['lan'])
276
		unset($config['dhcpd']['lan']);
277
	if($config['dhcpd6']['lan'])
278
		unset($config['dhcpd6']['lan']);
279
	unset($config['shaper']);
280
	unset($config['ezshaper']);
281
	unset($config['nat']);
282
	system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
283
	services_dhcpd_configure();
284
}
285

    
286
$upperifname = strtoupper($interface);
287
echo "\nPlease wait while the changes are saved to {$upperifname}...";
288
write_config(gettext("{$interface} IP configuration from console menu"));
289
interface_reconfigure(strtolower($upperifname));
290
echo " Reloading filter...";
291
filter_configure_sync();
292
echo "\n";
293
if($restart_dhcpd) {
294
	echo " DHCPD..."; 
295
	services_dhcpd_configure();
296
}
297
if($restart_webgui) {
298
	echo " restarting webConfigurator... ";
299
	mwexec("/etc/rc.restart_webgui");
300
}
301
	
302
if ($intip != '') {
303
	if (is_ipaddr($intip)) {
304
		echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
305
	} else {
306
		echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
307
	}
308
}
309
if ($intip6 != '') {
310
	if (is_ipaddr($intip6)) {
311
		echo "\n\n" . gettext("The IPv6 {$upperifname} address has been set to ") . "{$intip6}/{$intbits6}\n";
312
	} else {
313
		echo "\n\n" . gettext("The IPv6 {$upperifname} address has been set to ") . "{$intip6}\n";
314
	}
315
}
316

    
317
if ($intip != '' || $intip6 != '') {
318
	if (count($ifdescrs) == "1" or $interface = "lan") {
319
		if ($debug) {
320
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
321
			echo "interface is {$interface} \n";
322
		}
323
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
324
		if(!empty($config['system']['webgui']['port'])) {
325
			$webuiport = $config['system']['webgui']['port'];
326
			if ($intip != '') {
327
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
328
			}
329
			if ($intip6 != '') {
330
				if (is_ipaddr($intip6)) {
331
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$port}/\n";
332
				} else {
333
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$port}/\n";
334
				}
335
			}
336
		} else {
337
			if ($intip != '') {
338
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
339
			}
340
			if ($intip6 != '') {
341
				if (is_ipaddr($intip6)) {
342
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
343
				} else {
344
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
345
				}
346
			}
347
		}
348
	}
349
}
350

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

    
353
fgets($fp);
354
fclose($fp);
355
		
356
?>
(66-66/107)