1
|
#!/usr/local/bin/php -q
|
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
|
|
34
|
|
35
|
$options = getopt("hn", array("dry-run", "help"));
|
36
|
|
37
|
if (isset($options["h"]) || isset($options["help"])) {
|
38
|
echo "usage: /etc/rc.initial.setlanip [option ...]\n";
|
39
|
echo " -h, --help show this message\n";
|
40
|
echo " -n, --dry-run do not make any configuration changes\n";
|
41
|
exit(0);
|
42
|
}
|
43
|
|
44
|
$dry_run = isset($options["n"]) || isset($options["dry-run"]);
|
45
|
if ($dry_run) {
|
46
|
echo "DRY RUN MODE IS ON\n";
|
47
|
}
|
48
|
|
49
|
|
50
|
|
51
|
/* parse the configuration and include all functions used below */
|
52
|
require_once("config.inc");
|
53
|
require_once("functions.inc");
|
54
|
require_once("filter.inc");
|
55
|
require_once("shaper.inc");
|
56
|
require_once("rrd.inc");
|
57
|
|
58
|
function console_get_interface_from_ppp($realif) {
|
59
|
global $config;
|
60
|
|
61
|
if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
|
62
|
foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
|
63
|
if ($realif == $ppp['if']) {
|
64
|
$ifaces = explode(",", $ppp['ports']);
|
65
|
return $ifaces[0];
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
return "";
|
71
|
}
|
72
|
|
73
|
function prompt_for_enable_dhcp_server($version = 4) {
|
74
|
global $config, $fp, $interface;
|
75
|
if($interface == "wan") {
|
76
|
if($config['interfaces']['lan'])
|
77
|
return "n";
|
78
|
}
|
79
|
/* only allow DHCP server to be enabled when static IP is
|
80
|
configured on this interface */
|
81
|
if ($version === 6) {
|
82
|
$is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
|
83
|
} else {
|
84
|
$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
|
85
|
}
|
86
|
if ($is_ipaddr) {
|
87
|
$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
|
88
|
do {
|
89
|
$good = false;
|
90
|
$upperifname = strtoupper($interface);
|
91
|
echo "\n" . sprintf(gettext("Do you want to enable the %s server on %s? [y|n]"),
|
92
|
$label_DHCP, $upperifname) . " ";
|
93
|
$yn = strtolower(chop(fgets($fp)));
|
94
|
if ($yn[0] == "y" or $yn[0] == "n")
|
95
|
$good = true;
|
96
|
} while (!$good);
|
97
|
}
|
98
|
return $yn;
|
99
|
}
|
100
|
|
101
|
function get_interface_config_description($iface) {
|
102
|
global $config;
|
103
|
$c = $config['interfaces'][$iface];
|
104
|
if (!$c) { return null; }
|
105
|
$if = $c['if'];
|
106
|
$result = $if;
|
107
|
$result2 = array();
|
108
|
$ipaddr = $c['ipaddr'];
|
109
|
$ipaddrv6 = $c['ipaddrv6'];
|
110
|
if (is_ipaddr($ipaddr)) {
|
111
|
$result2[] = "static";
|
112
|
} else if ($ipaddr == "dhcp") {
|
113
|
$result2[] = "dhcp";
|
114
|
}
|
115
|
if (is_ipaddr($ipaddrv6)) {
|
116
|
$result2[] = "staticv6";
|
117
|
} else if ($ipaddrv6 == "dhcp6") {
|
118
|
$result2[] = "dhcp6";
|
119
|
}
|
120
|
if (count($result2)) {
|
121
|
$result .= " - " . implode(", ", $result2);
|
122
|
}
|
123
|
return $result;
|
124
|
}
|
125
|
|
126
|
$fp = fopen('php://stdin', 'r');
|
127
|
|
128
|
/* build an interface collection */
|
129
|
$ifdescrs = get_configured_interface_with_descr(false, true);
|
130
|
$count = count($ifdescrs);
|
131
|
|
132
|
/* grab interface that we will operate on, unless there is only one
|
133
|
interface */
|
134
|
if ($count > 1) {
|
135
|
echo "Available interfaces:\n\n";
|
136
|
$x=1;
|
137
|
foreach($ifdescrs as $iface => $ifdescr) {
|
138
|
$config_descr = get_interface_config_description($iface);
|
139
|
echo "{$x} - {$ifdescr} ({$config_descr})\n";
|
140
|
$x++;
|
141
|
}
|
142
|
echo "\nEnter the number of the interface you wish to configure: ";
|
143
|
$intnum = chop(fgets($fp));
|
144
|
} else {
|
145
|
$intnum = $count;
|
146
|
}
|
147
|
|
148
|
if($intnum < 1)
|
149
|
exit;
|
150
|
if($intnum > $count)
|
151
|
exit;
|
152
|
|
153
|
$index = 1;
|
154
|
foreach ($ifdescrs as $ifname => $ifdesc) {
|
155
|
if ($intnum == $index) {
|
156
|
$interface = $ifname;
|
157
|
break;
|
158
|
} else {
|
159
|
$index++;
|
160
|
}
|
161
|
}
|
162
|
if(!$interface) {
|
163
|
echo "Invalid interface!\n";
|
164
|
exit;
|
165
|
}
|
166
|
|
167
|
$ifaceassigned = "";
|
168
|
|
169
|
function next_unused_gateway_name($interface) {
|
170
|
global $g, $config;
|
171
|
$new_name = "GW_" . strtoupper($interface);
|
172
|
|
173
|
if (!is_array($config['gateways']['gateway_item'])) { return $new_name; }
|
174
|
$count = 1;
|
175
|
do {
|
176
|
$existing = false;
|
177
|
foreach ($config['gateways']['gateway_item'] as $item) {
|
178
|
if ($item['name'] === $new_name) {
|
179
|
$existing = true;
|
180
|
break;
|
181
|
}
|
182
|
}
|
183
|
if ($existing) {
|
184
|
$count += 1;
|
185
|
$new_name = "GW_" . strtoupper($interface) . "_" . $count;
|
186
|
}
|
187
|
} while ($existing);
|
188
|
return $new_name;
|
189
|
}
|
190
|
|
191
|
function add_gateway_to_config($interface, $gatewayip, $inet_type) {
|
192
|
global $g, $config, $dry_run;
|
193
|
if (!is_array($config['gateways']['gateway_item'])) {
|
194
|
$config['gateways']['gateway_item'] = array();
|
195
|
}
|
196
|
$a_gateways = &$config['gateways']['gateway_item'];
|
197
|
if ($dry_run) {
|
198
|
print_r($a_gateways);
|
199
|
}
|
200
|
$new_name = next_unused_gateway_name($interface);
|
201
|
$is_default = true;
|
202
|
foreach ($a_gateways as $item) {
|
203
|
if ($item['ipprotocol'] === $inet_type) {
|
204
|
$is_default = false;
|
205
|
break;
|
206
|
}
|
207
|
}
|
208
|
$item = array(
|
209
|
"interface" => $interface,
|
210
|
"gateway" => $gatewayip,
|
211
|
"name" => $new_name,
|
212
|
"weight" => 1,
|
213
|
"ipprotocol" => $inet_type,
|
214
|
"interval" => true,
|
215
|
"descr" => "Interface $interface Gateway",
|
216
|
"defaultgw" => $is_default
|
217
|
);
|
218
|
if ($dry_run) {
|
219
|
print_r($item);
|
220
|
}
|
221
|
$a_gateways[] = $item;
|
222
|
}
|
223
|
|
224
|
function console_configure_ip_address($version) {
|
225
|
global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;
|
226
|
|
227
|
$label_IPvX = ($version === 6) ? "IPv6" : "IPv4";
|
228
|
$maxbits = ($version === 6) ? 127 : 31;
|
229
|
$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
|
230
|
|
231
|
$upperifname = strtoupper($interface);
|
232
|
|
233
|
if($interface == "wan") {
|
234
|
echo sprintf(gettext("Configure %s address %s interface via %s? [y|n]"),
|
235
|
$label_IPvX, $upperifname, $label_DHCP) . "\n> ";
|
236
|
$intdhcp = chop(fgets($fp));
|
237
|
if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
|
238
|
$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
|
239
|
if (!empty($ifppp))
|
240
|
$ifaceassigned = $ifppp;
|
241
|
$intip = ($version === 6) ? "dhcp6" : "dhcp";
|
242
|
$intbits = "";
|
243
|
$isintdhcp = true;
|
244
|
$restart_dhcpd = true;
|
245
|
}
|
246
|
}
|
247
|
|
248
|
if($isintdhcp == false or $interface <> "wan") {
|
249
|
do {
|
250
|
echo "\n" . sprintf(gettext("Enter the new %s %s address. Press <ENTER> for none:"),
|
251
|
$upperifname, $label_IPvX) . "\n> ";
|
252
|
$intip = chop(fgets($fp));
|
253
|
$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
|
254
|
} while (!($is_ipaddr || $intip == ''));
|
255
|
if ($intip != '') {
|
256
|
echo "\n" . sprintf(gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
|
257
|
$g['product_name']) . "\n";
|
258
|
if ($version === 6) {
|
259
|
echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
|
260
|
echo " ffff:ffff:ffff:ffff:ffff:ffff:ffff:0 = 112\n";
|
261
|
echo " ffff:ffff:ffff:ffff:ffff:ffff:0:0 = 96\n";
|
262
|
echo " ffff:ffff:ffff:ffff:ffff:0:0:0 = 80\n";
|
263
|
echo " ffff:ffff:ffff:ffff:0:0:0:0 = 64\n";
|
264
|
} else {
|
265
|
echo "e.g. 255.255.255.0 = 24\n";
|
266
|
echo " 255.255.0.0 = 16\n";
|
267
|
echo " 255.0.0.0 = 8\n";
|
268
|
}
|
269
|
do {
|
270
|
$upperifname = strtoupper($interface);
|
271
|
echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count:"),
|
272
|
$upperifname, $label_IPvX) . "\n> ";
|
273
|
$intbits = chop(fgets($fp));
|
274
|
$restart_dhcpd = true;
|
275
|
} while (!is_numeric($intbits) || ($intbits < 1) || ($intbits > $maxbits));
|
276
|
|
277
|
if ($version === 6) {
|
278
|
$subnet = gen_subnetv6($intip, $intbits);
|
279
|
} else {
|
280
|
$subnet = gen_subnet($intip, $intbits);
|
281
|
}
|
282
|
do {
|
283
|
echo "\n" . sprintf(gettext("Enter the new %s %s gateway address. Press <ENTER> for none:"),
|
284
|
$upperifname, $label_IPvX) . "\n> ";
|
285
|
$gwip = chop(fgets($fp));
|
286
|
$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
|
287
|
$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
|
288
|
if ($gwip != '') {
|
289
|
if (!$is_ipaddr) {
|
290
|
echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
|
291
|
} else if (!$is_in_subnet) {
|
292
|
echo gettext("not in subnet!") . "\n";
|
293
|
}
|
294
|
}
|
295
|
} while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));
|
296
|
|
297
|
if ($gwip != '') {
|
298
|
$inet_type = ($version === 6) ? "inet6" : "inet";
|
299
|
add_gateway_to_config($interface, $gwip, $inet_type);
|
300
|
}
|
301
|
}
|
302
|
$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
|
303
|
if (!empty($ifppp))
|
304
|
$ifaceassigned = $ifppp;
|
305
|
}
|
306
|
|
307
|
return array($intip, $intbits, $gwip);
|
308
|
}
|
309
|
|
310
|
list($intip, $intbits, $gwip) = console_configure_ip_address(4);
|
311
|
list($intip6, $intbits6, $gwip6) = console_configure_ip_address(6);
|
312
|
|
313
|
if (!empty($ifaceassigned))
|
314
|
$config['interfaces'][$interface]['if'] = $ifaceassigned;
|
315
|
$config['interfaces'][$interface]['ipaddr'] = $intip;
|
316
|
$config['interfaces'][$interface]['subnet'] = $intbits;
|
317
|
if ($intip6) {
|
318
|
$config['interfaces'][$interface]['ipaddrv6'] = $intip6;
|
319
|
$config['interfaces'][$interface]['subnetv6'] = $intbits6;
|
320
|
}
|
321
|
$config['interfaces'][$interface]['enable'] = true;
|
322
|
|
323
|
function console_configure_dhcpd($version = 4) {
|
324
|
global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run;
|
325
|
|
326
|
$label_IPvX = ($version === 6) ? "IPv6" : "IPv4";
|
327
|
$dhcpd = ($version === 6) ? "dhcpd6" : "dhcpd";
|
328
|
|
329
|
if($g['services_dhcp_server_enable'])
|
330
|
$yn = prompt_for_enable_dhcp_server($version);
|
331
|
if ($yn == "y") {
|
332
|
do {
|
333
|
echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
|
334
|
$dhcpstartip = chop(fgets($fp));
|
335
|
if ($dhcpstartip === "") {
|
336
|
fclose($fp);
|
337
|
exit(0);
|
338
|
}
|
339
|
$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
|
340
|
} while (!$is_ipaddr);
|
341
|
|
342
|
do {
|
343
|
echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
|
344
|
$dhcpendip = chop(fgets($fp));
|
345
|
if ($dhcpendip === "") {
|
346
|
fclose($fp);
|
347
|
exit(0);
|
348
|
}
|
349
|
$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
|
350
|
} while (!$is_ipaddr);
|
351
|
$restart_dhcpd = true;
|
352
|
$config[$dhcpd][$interface]['enable'] = true;
|
353
|
$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
|
354
|
$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
|
355
|
} else {
|
356
|
/* TODO - this line is causing a "Fatal error: Cannot unset
|
357
|
string offsets in /etc/rc.initial.setlanip" on below line
|
358
|
number */
|
359
|
if($config[$dhcpd][$interface])
|
360
|
unset($config[$dhcpd][$interface]['enable']);
|
361
|
echo "Disabling DHCPD...";
|
362
|
if (!$dry_run) {
|
363
|
services_dhcpd_configure();
|
364
|
}
|
365
|
echo "Done!\n";
|
366
|
}
|
367
|
}
|
368
|
|
369
|
console_configure_dhcpd(4);
|
370
|
console_configure_dhcpd(6);
|
371
|
|
372
|
//*****************************************************************************
|
373
|
|
374
|
if ($config['system']['webgui']['protocol'] == "https") {
|
375
|
|
376
|
do {
|
377
|
$good = false;
|
378
|
echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
|
379
|
$yn = strtolower(chop(fgets($fp)));
|
380
|
if ($yn[0] == "y" or $yn[0] == "n")
|
381
|
$good = true;
|
382
|
} while (!$good);
|
383
|
|
384
|
if ($yn == "y") {
|
385
|
$config['system']['webgui']['protocol'] = "http";
|
386
|
$restart_webgui = true;
|
387
|
}
|
388
|
}
|
389
|
|
390
|
if (isset($config['system']['webgui']['noantilockout'])) {
|
391
|
echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
|
392
|
unset($config['system']['webgui']['noantilockout']);
|
393
|
}
|
394
|
|
395
|
if($config['interfaces']['lan']) {
|
396
|
if($config['dhcpd'])
|
397
|
if($config['dhcpd']['wan'])
|
398
|
unset($config['dhcpd']['wan']);
|
399
|
if($config['dhcpd6'])
|
400
|
if($config['dhcpd6']['wan'])
|
401
|
unset($config['dhcpd6']['wan']);
|
402
|
}
|
403
|
|
404
|
if(!$config['interfaces']['lan']) {
|
405
|
unset($config['interfaces']['lan']);
|
406
|
if($config['dhcpd']['lan'])
|
407
|
unset($config['dhcpd']['lan']);
|
408
|
if($config['dhcpd6']['lan'])
|
409
|
unset($config['dhcpd6']['lan']);
|
410
|
unset($config['shaper']);
|
411
|
unset($config['ezshaper']);
|
412
|
unset($config['nat']);
|
413
|
if (!$dry_run) {
|
414
|
system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
|
415
|
services_dhcpd_configure();
|
416
|
}
|
417
|
}
|
418
|
|
419
|
$upperifname = strtoupper($interface);
|
420
|
if (!$dry_run) {
|
421
|
echo "\nPlease wait while the changes are saved to {$upperifname}...";
|
422
|
write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
|
423
|
interface_reconfigure(strtolower($upperifname));
|
424
|
echo " Reloading filter...";
|
425
|
filter_configure_sync();
|
426
|
echo "\n";
|
427
|
if($restart_dhcpd) {
|
428
|
echo " DHCPD...";
|
429
|
services_dhcpd_configure();
|
430
|
}
|
431
|
if($restart_webgui) {
|
432
|
echo " restarting webConfigurator... ";
|
433
|
mwexec("/etc/rc.restart_webgui");
|
434
|
}
|
435
|
}
|
436
|
|
437
|
if ($intip != '') {
|
438
|
if (is_ipaddr($intip)) {
|
439
|
echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
|
440
|
$upperifname, "{$intip}/{$intbits}") . "\n";
|
441
|
} else {
|
442
|
echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
|
443
|
$upperifname, $intip) . "\n";
|
444
|
}
|
445
|
}
|
446
|
if ($intip6 != '') {
|
447
|
if (is_ipaddr($intip6)) {
|
448
|
echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
|
449
|
$upperifname, "${intip6}/${intbits6}") . "\n";
|
450
|
} else {
|
451
|
echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
|
452
|
$upperifname, $intip6) . "\n";
|
453
|
}
|
454
|
}
|
455
|
|
456
|
if ($intip != '' || $intip6 != '') {
|
457
|
if (count($ifdescrs) == "1" or $interface = "lan") {
|
458
|
if ($debug) {
|
459
|
echo "ifdescrs count is " . count($ifdescrs) . "\n";
|
460
|
echo "interface is {$interface} \n";
|
461
|
}
|
462
|
echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
|
463
|
if(!empty($config['system']['webgui']['port'])) {
|
464
|
$webuiport = $config['system']['webgui']['port'];
|
465
|
if ($intip != '') {
|
466
|
echo " {$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
|
467
|
}
|
468
|
if ($intip6 != '') {
|
469
|
if (is_ipaddr($intip6)) {
|
470
|
echo " {$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
|
471
|
} else {
|
472
|
echo " {$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
|
473
|
}
|
474
|
}
|
475
|
} else {
|
476
|
if ($intip != '') {
|
477
|
echo " {$config['system']['webgui']['protocol']}://{$intip}/\n";
|
478
|
}
|
479
|
if ($intip6 != '') {
|
480
|
if (is_ipaddr($intip6)) {
|
481
|
echo " {$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
|
482
|
} else {
|
483
|
echo " {$config['system']['webgui']['protocol']}://{$intip6}/\n";
|
484
|
}
|
485
|
}
|
486
|
}
|
487
|
}
|
488
|
}
|
489
|
|
490
|
echo "\n" . gettext('Press <ENTER> to continue.');
|
491
|
|
492
|
fgets($fp);
|
493
|
fclose($fp);
|
494
|
|
495
|
?>
|