summaryrefslogtreecommitdiffstats
path: root/etc/rc.initial.setlanip
blob: 550fc3981f5128775c124f1abe56d26eaa8d23b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/local/bin/php -f
<?php
/* $Id$ */
/*
	rc.initial.setlanip
	part of m0n0wall (http://m0n0.ch/wall)

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

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

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

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

/* parse the configuration and include all functions used below */
require_once("config.inc");
require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");
require_once("rrd.inc");

function console_get_interface_from_ppp($realif) {
	global $config;

	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
			if ($realif == $ppp['if']) {
				$ifaces = explode(",", $ppp['ports']);
				return $ifaces[0];
			}
		}
	}

	return "";
}

function prompt_for_enable_dhcp_server($version = 4) {
	global $config, $fp, $interface;
	if($interface == "wan") {
		if($config['interfaces']['lan']) 
			return "n";
	}
	/* only allow DHCP server to be enabled when static IP is
	   configured on this interface */
	if ($version === 6) {
		$is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
	} else {
		$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
	}
	if ($is_ipaddr) {
		$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
		do {
			$good = false;
			$upperifname = strtoupper($interface);
			echo "\n" . gettext("Do you want to enable the {$label_DHCP} server on {$upperifname}? [y|n]") . "  ";
			$yn = strtolower(chop(fgets($fp)));
			if ($yn[0] == "y" or $yn[0] == "n")
				$good = true;
		} while (!$good);
	}
	return $yn;
}

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

/* build an interface collection */
$ifdescrs = get_configured_interface_with_descr(false, true);
$count = count($ifdescrs);
	
/* grab interface that we will operate on, unless there is only one
   interface */
if ($count > 1) {
	echo "Available interfaces:\n\n";
	$x=1;
	foreach($ifdescrs as $iface) {
		echo "{$x} - {$iface}\n";
		$x++;
	}
	echo "\nEnter the number of the interface you wish to configure: ";
	$intnum = chop(fgets($fp));	
} else {
	$intnum = $count;
}
	
if($intnum < 1) 
	exit;
if($intnum > $count)
	exit;
		
$index = 1;
foreach ($ifdescrs as $ifname => $ifdesc) {
	if ($intnum == $index)  {
		$interface = $ifname;
		break;
	} else {
		$index++;
	}
}	
if(!$interface) {
	echo "Invalid interface!\n";
	exit;
}

$ifaceassigned = "";

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

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

	$upperifname = strtoupper($interface);

	if($interface == "wan") {
		echo gettext("Configure {$label_IPvX} address {$upperifname} interface via {$label_DHCP}?  [y|n]") . "\n> ";
		$intdhcp = chop(fgets($fp));
		if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
			if (!empty($ifppp))
				$ifaceassigned = $ifppp;
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
			$intbits = "";
			$isintdhcp = true;
			$restart_dhcpd = true;
		} 
	}
		
	if($isintdhcp == false or $interface <> "wan") {
		do {
			echo "\n" . gettext("Enter the new {$upperifname} {$label_IPvX} address.  Press <ENTER> for none:") . "\n> ";
			$intip = chop(fgets($fp));
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
		} while (!($is_ipaddr || $intip == ''));
		if ($intip != '') {
			echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
			if ($version === 6) {
				echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
				echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
				echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
				echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
				echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
			} else {
				echo "e.g. 255.255.255.0 = 24\n";
				echo "     255.255.0.0   = 16\n";
				echo "     255.0.0.0     = 8\n";
			}
			do {
				$upperifname = strtoupper($interface);
				echo "\n" . gettext("Enter the new {$upperifname} {$label_IPvX} subnet bit count:") . "\n> ";
				$intbits = chop(fgets($fp));
				$restart_dhcpd = true;
			} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > $maxbits));
		}
		$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
		if (!empty($ifppp))
			$ifaceassigned = $ifppp;
	}

	return array($intip, $intbits);
}

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

if (!empty($ifaceassigned))
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
$config['interfaces'][$interface]['ipaddr'] = $intip;
$config['interfaces'][$interface]['subnet'] = $intbits;
if ($intip6) {
	$config['interfaces'][$interface]['ipaddrv6'] = $intip6;
	$config['interfaces'][$interface]['subnetv6'] = $intbits6;
}
$config['interfaces'][$interface]['enable'] = true;

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

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

	if($g['services_dhcp_server_enable'])
		$yn = prompt_for_enable_dhcp_server($version);
	if ($yn == "y") {
		do {
			echo gettext("Enter the start address of the {$label_IPvX} client address range:") . " ";
			$dhcpstartip = chop(fgets($fp));
			if ($dhcpstartip === "") {
				fclose($fp);
				exit(0);
			}
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
		} while (!$is_ipaddr);

		do {
			echo gettext("Enter the end address of the {$label_IPvX} client address range:") . " ";
			$dhcpendip = chop(fgets($fp));
			if ($dhcpendip === "") {
				fclose($fp);
				exit(0);
			}
			$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
		} while (!$is_ipaddr);
		$restart_dhcpd = true;
		$config[$dhcpd][$interface]['enable'] = true;
		$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
		$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
	} else {
		/* TODO - this line is causing a "Fatal error: Cannot unset
		   string offsets in /etc/rc.initial.setlanip" on below line
		   number */
		if($config[$dhcpd][$interface]) 
			unset($config[$dhcpd][$interface]['enable']);
		echo "Disabling DHCPD...";
		services_dhcpd_configure();
		echo "Done!\n";
	}
}

console_configure_dhcpd(4);
console_configure_dhcpd(6);
	
//*****************************************************************************

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

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

	if ($yn == "y") {
		$config['system']['webgui']['protocol'] = "http";
		$restart_webgui = true;
	}
}

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

if($config['interfaces']['lan']) {
	if($config['dhcpd'])
		if($config['dhcpd']['wan'])
			unset($config['dhcpd']['wan']);		
	if($config['dhcpd6'])
		if($config['dhcpd6']['wan'])
			unset($config['dhcpd6']['wan']);		
}

if(!$config['interfaces']['lan']) {
	unset($config['interfaces']['lan']);
	if($config['dhcpd']['lan'])
		unset($config['dhcpd']['lan']);
	if($config['dhcpd6']['lan'])
		unset($config['dhcpd6']['lan']);
	unset($config['shaper']);
	unset($config['ezshaper']);
	unset($config['nat']);
	system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
	services_dhcpd_configure();
}

$upperifname = strtoupper($interface);
echo "\nPlease wait while the changes are saved to {$upperifname}...";
write_config(gettext("{$interface} IP configuration from console menu"));
interface_reconfigure(strtolower($upperifname));
echo " Reloading filter...";
filter_configure_sync();
echo "\n";
if($restart_dhcpd) {
	echo " DHCPD..."; 
	services_dhcpd_configure();
}
if($restart_webgui) {
	echo " restarting webConfigurator... ";
	mwexec("/etc/rc.restart_webgui");
}
	
if ($intip != '') {
	if (is_ipaddr($intip)) {
		echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
	} else {
		echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
	}
}
if ($intip6 != '') {
	if (is_ipaddr($intip6)) {
		echo "\n\n" . gettext("The IPv6 {$upperifname} address has been set to ") . "{$intip6}/{$intbits6}\n";
	} else {
		echo "\n\n" . gettext("The IPv6 {$upperifname} address has been set to ") . "{$intip6}\n";
	}
}

if ($intip != '' || $intip6 != '') {
	if (count($ifdescrs) == "1" or $interface = "lan") {
		if ($debug) {
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
			echo "interface is {$interface} \n";
		}
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
		if(!empty($config['system']['webgui']['port'])) {
			$webuiport = $config['system']['webgui']['port'];
			if ($intip != '') {
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
			}
			if ($intip6 != '') {
				if (is_ipaddr($intip6)) {
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
				} else {
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
				}
			}
		} else {
			if ($intip != '') {
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
			}
			if ($intip6 != '') {
				if (is_ipaddr($intip6)) {
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
				} else {
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
				}
			}
		}
	}
}

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

fgets($fp);
fclose($fp);
		
?>
OpenPOWER on IntegriCloud