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 prompt_for_enable_dhcp_server() {
|
41
|
global $config, $fp, $interface;
|
42
|
if($interface == "wan") {
|
43
|
if($config['interfaces']['lan'])
|
44
|
return "n";
|
45
|
}
|
46
|
/* only allow DHCP server to be enabled when static IP is
|
47
|
configured on this interface */
|
48
|
if (is_ipaddr($config['interfaces'][$interface]['ipaddr'])) {
|
49
|
do {
|
50
|
$good = false;
|
51
|
$upperifname = strtoupper($interface);
|
52
|
echo "\n" . gettext("Do you want to enable the DHCP server on {$upperifname}? [y|n]") . " ";
|
53
|
$yn = strtolower(chop(fgets($fp)));
|
54
|
if ($yn[0] == "y" or $yn[0] == "n")
|
55
|
$good = true;
|
56
|
} while (!$good);
|
57
|
}
|
58
|
return $yn;
|
59
|
}
|
60
|
|
61
|
$fp = fopen('php://stdin', 'r');
|
62
|
$addr_blank = false;
|
63
|
|
64
|
/* build an interface collection */
|
65
|
$ifdescrs = get_configured_interface_with_descr();
|
66
|
$j = count($ifdescrs);
|
67
|
|
68
|
/* grab interface that we will operate on, unless there is only one
|
69
|
interface */
|
70
|
if ($j > 1) {
|
71
|
echo "Available interfaces:\n\n";
|
72
|
$x=1;
|
73
|
foreach($ifdescrs as $iface) {
|
74
|
echo "{$x} - {$iface}\n";
|
75
|
$x++;
|
76
|
}
|
77
|
echo "\nEnter the number of the interface you wish to configure: ";
|
78
|
$intnum = chop(fgets($fp));
|
79
|
} else {
|
80
|
$intnum = $j;
|
81
|
}
|
82
|
|
83
|
if($intnum < 1)
|
84
|
exit;
|
85
|
if($intnum > $j)
|
86
|
exit;
|
87
|
|
88
|
$index = 1;
|
89
|
foreach ($ifdescrs as $ifname => $ifdesc) {
|
90
|
if ($intnum == $index) {
|
91
|
$interface = $ifname;
|
92
|
break;
|
93
|
} else {
|
94
|
$index++;
|
95
|
}
|
96
|
}
|
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
|
$restart_dhcpd = true;
|
113
|
}
|
114
|
}
|
115
|
|
116
|
if($isintdhcp == false or $interface <> "wan") {
|
117
|
do {
|
118
|
$upperifname = strtoupper($interface);
|
119
|
echo "\n" . gettext("Enter the new {$upperifname} IPv4 address. Press <ENTER> for none:") . "\n> ";
|
120
|
$intip = chop(fgets($fp));
|
121
|
$addr_blank = false;
|
122
|
} while (!(is_ipaddr($intip) || $intip == ''));
|
123
|
if ($intip != '') {
|
124
|
echo "\n" . gettext("Subnet masks are entered as bit counts (as in CIDR notation) in {$g['product_name']}.") . "\n";
|
125
|
echo "e.g. 255.255.255.0 = 24\n";
|
126
|
echo " 255.255.0.0 = 16\n";
|
127
|
echo " 255.0.0.0 = 8\n";
|
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
|
$restart_dhcpd = true;
|
133
|
} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > 31));
|
134
|
}
|
135
|
}
|
136
|
|
137
|
|
138
|
} while ($addr_blank);
|
139
|
|
140
|
$config['interfaces'][$interface]['ipaddr'] = $intip;
|
141
|
$config['interfaces'][$interface]['subnet'] = $intbits;
|
142
|
|
143
|
if($g['services_dhcp_server_enable'])
|
144
|
$yn = prompt_for_enable_dhcp_server();
|
145
|
|
146
|
// TODO: Add DHCP IPv6 support
|
147
|
if ($yn == "y") {
|
148
|
do {
|
149
|
echo gettext("Enter the start address of the client address range:") . " ";
|
150
|
$dhcpstartip = chop(fgets($fp));
|
151
|
if ($dhcpstartip === "") {
|
152
|
fclose($fp);
|
153
|
exit(0);
|
154
|
}
|
155
|
} while (!(is_ipaddr($dhcpstartip)));
|
156
|
|
157
|
do {
|
158
|
echo gettext("Enter the end address of the client address range:") . " ";
|
159
|
$dhcpendip = chop(fgets($fp));
|
160
|
if ($dhcpendip === "") {
|
161
|
fclose($fp);
|
162
|
exit(0);
|
163
|
}
|
164
|
} while (!(is_ipaddr($dhcpendip)));
|
165
|
$restart_dhcpd = true;
|
166
|
$config['dhcpd'][$interface]['enable'] = true;
|
167
|
$config['dhcpd'][$interface]['range']['from'] = $dhcpstartip;
|
168
|
$config['dhcpd'][$interface]['range']['to'] = $dhcpendip;
|
169
|
} else {
|
170
|
/* TODO - this line is causing a "Fatal error: Cannot unset
|
171
|
string offsets in /etc/rc.initial.setlanip" on below line
|
172
|
number */
|
173
|
if($config['dhcpd'][$interface])
|
174
|
unset($config['dhcpd'][$interface]['enable']);
|
175
|
echo "Disabling DHCPD...";
|
176
|
services_dhcpd_configure();
|
177
|
echo "Done!\n";
|
178
|
}
|
179
|
|
180
|
if ($config['system']['webgui']['protocol'] == "https") {
|
181
|
|
182
|
do {
|
183
|
$good = false;
|
184
|
echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
|
185
|
$yn = strtolower(chop(fgets($fp)));
|
186
|
if ($yn[0] == "y" or $yn[0] == "n")
|
187
|
$good = true;
|
188
|
} while (!$good);
|
189
|
|
190
|
if ($yn == "y") {
|
191
|
$config['system']['webgui']['protocol'] = "http";
|
192
|
$restart_webgui = true;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
if (isset($config['system']['webgui']['noantilockout'])) {
|
197
|
echo "\n" . gettext("Note: the anti-lockout rule on {$interface} has been re-enabled.") . "\n";
|
198
|
unset($config['system']['webgui']['noantilockout']);
|
199
|
}
|
200
|
|
201
|
if($config['interfaces']['lan']) {
|
202
|
if($config['dhcpd'])
|
203
|
if($config['dhcpd']['wan'])
|
204
|
unset($config['dhcpd']['wan']);
|
205
|
}
|
206
|
|
207
|
if(!$config['interfaces']['lan']) {
|
208
|
unset($config['interfaces']['lan']);
|
209
|
if($config['dhcpd']['lan'])
|
210
|
unset($config['dhcpd']['lan']);
|
211
|
unset($config['shaper']);
|
212
|
unset($config['ezshaper']);
|
213
|
unset($config['nat']);
|
214
|
system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
|
215
|
services_dhcpd_configure();
|
216
|
}
|
217
|
|
218
|
$upperifname = strtoupper($interface);
|
219
|
echo "\nPlease wait, saving and activating your changes to {$upperifname}...";
|
220
|
write_config(gettext("{$interface} IP configuration from console menu"));
|
221
|
interface_configure(strtolower($upperifname));
|
222
|
echo " Reloading filter...";
|
223
|
filter_configure_sync();
|
224
|
echo "\n";
|
225
|
if($restart_dhcpd) {
|
226
|
echo " DHCPD...";
|
227
|
services_dhcpd_configure();
|
228
|
}
|
229
|
if($restart_webgui) {
|
230
|
echo " restarting webConfigurator... ";
|
231
|
mwexec("/etc/rc.restart_webgui");
|
232
|
}
|
233
|
|
234
|
if ($intip != '') {
|
235
|
if (is_ipaddr($intip)) {
|
236
|
echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}/{$intbits}\n";
|
237
|
} else {
|
238
|
echo "\n\n" . gettext("The IPv4 {$upperifname} address has been set to ") . "{$intip}\n";
|
239
|
}
|
240
|
if (count($ifdescrs) == "1" or $interface = "lan") {
|
241
|
if ($debug) {
|
242
|
echo "ifdescrs count is " . count($ifdescrs) . "\n";
|
243
|
echo "interface is {$interface} \n";
|
244
|
}
|
245
|
echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
|
246
|
if(!empty($config['system']['webgui']['port'])) {
|
247
|
$webuiport = $config['system']['webgui']['port'];
|
248
|
echo " {$config['system']['webgui']['protocol']}://{$intip}:{$port}/\n";
|
249
|
} else {
|
250
|
echo " {$config['system']['webgui']['protocol']}://{$intip}/\n";
|
251
|
}
|
252
|
}
|
253
|
}
|
254
|
|
255
|
echo "\n" . gettext('Press <ENTER> to continue.');
|
256
|
|
257
|
fgets($fp);
|
258
|
fclose($fp);
|
259
|
|
260
|
?>
|