Projet

Général

Profil

Télécharger (16,5 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / rc.initial.setlanip @ master

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 = '';
201
	$is_default = true;
202
	foreach ($a_gateways as $item) {
203
		if ($item['ipprotocol'] === $inet_type) {
204
			if (isset($item['defaultgw']))
205
				$is_default = false;
206
			if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip))
207
				$new_name = $item['name'];
208
		}
209
	}
210
	if ($new_name == '') {
211
		$new_name = next_unused_gateway_name($interface);
212
		$item = array(
213
			"interface" => $interface,
214
			"gateway" => $gatewayip,
215
			"name" => $new_name,
216
			"weight" => 1,
217
			"ipprotocol" => $inet_type,
218
			"interval" => true,
219
			"descr" => "Interface $interface Gateway",
220
			"defaultgw" => $is_default
221
		);
222
		if ($dry_run) {
223
			print_r($item);
224
		}
225
		$a_gateways[] = $item;
226
	}
227

    
228
	return $new_name;
229
}
230

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

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

    
238
	$upperifname = strtoupper($interface);
239

    
240
	if($interface == "wan") {
241
		echo sprintf(gettext("Configure %s address %s interface via %s?  [y|n]"),
242
		             $label_IPvX, $upperifname, $label_DHCP) . "\n> ";
243
		$intdhcp = chop(fgets($fp));
244
		if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
245
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
246
			if (!empty($ifppp))
247
				$ifaceassigned = $ifppp;
248
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
249
			$intbits = "";
250
			$isintdhcp = true;
251
			$restart_dhcpd = true;
252
		} 
253
	}
254
		
255
	if($isintdhcp == false or $interface <> "wan") {
256
		while(true) {
257
			do {
258
				echo "\n" . sprintf(gettext("Enter the new %s %s address.  Press <ENTER> for none:"),
259
						    $upperifname, $label_IPvX) . "\n> ";
260
				$intip = chop(fgets($fp));
261
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
262
				if ($is_ipaddr && is_ipaddr_configured($intip, $interface, true)) {
263
					$ip_conflict = true;
264
					echo gettext("This IP address conflicts with another interface or a VIP") . "\n";
265
				} else
266
					$ip_conflict = false;
267
			} while (($ip_conflict === true) || !($is_ipaddr || $intip == ''));
268
			if ($intip != '') {
269
				echo "\n" . sprintf(gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
270
						    $g['product_name']) . "\n";
271
				if ($version === 6) {
272
					echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
273
					echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
274
					echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
275
					echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
276
					echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
277
				} else {
278
					echo "e.g. 255.255.255.0 = 24\n";
279
					echo "     255.255.0.0   = 16\n";
280
					echo "     255.0.0.0     = 8\n";
281
				}
282
				do {
283
					$upperifname = strtoupper($interface);
284
					echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count:"),
285
							    $upperifname, $label_IPvX) . "\n> ";
286
					$intbits = chop(fgets($fp));
287
					$intbits_ok = is_numeric($intbits) && (($intbits >= 1) || ($intbits <= $maxbits));
288
					$restart_dhcpd = true;
289

    
290
					if ($version === 4 && $intbits < $maxbits) {
291
						if ($intip == gen_subnet($intip, $intbits)) {
292
							echo gettext("You cannot set network address to an interface");
293
							continue 2;
294
							$intbits_ok = false;
295
						} else if ($intip == gen_subnet_max($intip, $intbits)) {
296
							echo gettext("You cannot set broadcast address to an interface");
297
							continue 2;
298
							$intbits_ok = false;
299
						}
300
					}
301
				} while (!$intbits_ok);
302

    
303
				if ($version === 6) {
304
					$subnet = gen_subnetv6($intip, $intbits);
305
				} else {
306
					$subnet = gen_subnet($intip, $intbits);
307
				}
308
				do {
309
					echo "\n" . sprintf(gettext("For a WAN, enter the new %s %s upstream gateway address."), $upperifname, $label_IPvX) . "\n" .
310
								gettext("For a LAN, press <ENTER> for none:") . "\n> ";
311
					$gwip = chop(fgets($fp));
312
					$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
313
					$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
314
					if ($gwip != '') {
315
						if (!$is_ipaddr) {
316
							echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
317
						} else if (!$is_in_subnet) {
318
							echo gettext("not in subnet!") . "\n";
319
						}
320
					}
321
				} while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));
322

    
323
				if ($gwip != '') {
324
					$inet_type = ($version === 6) ? "inet6" : "inet";
325
					$gwname = add_gateway_to_config($interface, $gwip, $inet_type);
326
				}
327
			}
328
			$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
329
			if (!empty($ifppp))
330
				$ifaceassigned = $ifppp;
331
			break;
332
		}
333
	}
334

    
335
	return array($intip, $intbits, $gwname);
336
}
337

    
338
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
339
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
340

    
341
if (!empty($ifaceassigned))
342
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
343
$config['interfaces'][$interface]['ipaddr']    = $intip;
344
$config['interfaces'][$interface]['subnet']    = $intbits;
345
$config['interfaces'][$interface]['gateway']   = $gwname;
346
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
347
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
348
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
349
$config['interfaces'][$interface]['enable']    = true;
350

    
351
function console_configure_dhcpd($version = 4) {
352
	global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
353

    
354
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
355
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
356

    
357
	if($g['services_dhcp_server_enable'])
358
		$yn = prompt_for_enable_dhcp_server($version);
359
	if ($yn == "y") {
360
		$subnet_start = ($version === 6) ? gen_subnetv6($intip6, $intbits6) : gen_subnet($intip, $intbits);
361
		$subnet_end = ($version === 6) ? gen_subnetv6_max($intip6, $intbits6) : gen_subnet_max($intip, $intbits);
362
		do {
363
			do {
364
				echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
365
				$dhcpstartip = chop(fgets($fp));
366
				if ($dhcpstartip === "") {
367
					fclose($fp);
368
					exit(0);
369
				}
370
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
371
				$is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
372
				if (!$is_inrange)
373
					echo gettext("This IP address must be in the interface's subnet") . "\n";
374
			} while (!$is_ipaddr || !$is_inrange);
375

    
376
			do {
377
				echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
378
				$dhcpendip = chop(fgets($fp));
379
				if ($dhcpendip === "") {
380
					fclose($fp);
381
					exit(0);
382
				}
383
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
384
				$is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
385
				if (!$is_inrange)
386
					echo gettext("This IP address must be in the interface's subnet") . "\n";
387
				$not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
388
				if ($not_inorder) {
389
					echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
390
				}
391
			} while (!$is_ipaddr || !$is_inrange);
392
		} while ($not_inorder);
393
		$restart_dhcpd = true;
394
		$config[$dhcpd][$interface]['enable'] = true;
395
		$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
396
		$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
397
	} else {
398
		if(isset($config[$dhcpd][$interface]['enable'])) {
399
			unset($config[$dhcpd][$interface]['enable']);
400
			printf(gettext("Disabling %s DHCPD..."), $label_IPvX);
401
			$restart_dhcpd = true;
402
		}
403
	}
404
}
405

    
406
console_configure_dhcpd(4);
407
console_configure_dhcpd(6);
408
	
409
//*****************************************************************************
410

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

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

    
421
	if ($yn == "y") {
422
		$config['system']['webgui']['protocol'] = "http";
423
		$restart_webgui = true;
424
	}
425
}
426

    
427
if (isset($config['system']['webgui']['noantilockout'])) {
428
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
429
	unset($config['system']['webgui']['noantilockout']);
430
}
431

    
432
if($config['interfaces']['lan']) {
433
	if($config['dhcpd'])
434
		if($config['dhcpd']['wan'])
435
			unset($config['dhcpd']['wan']);		
436
	if($config['dhcpdv6'])
437
		if($config['dhcpdv6']['wan'])
438
			unset($config['dhcpdv6']['wan']);
439
}
440

    
441
if(!$config['interfaces']['lan']) {
442
	unset($config['interfaces']['lan']);
443
	if($config['dhcpd']['lan'])
444
		unset($config['dhcpd']['lan']);
445
	if($config['dhcpdv6']['lan'])
446
		unset($config['dhcpdv6']['lan']);
447
	unset($config['shaper']);
448
	unset($config['ezshaper']);
449
	unset($config['nat']);
450
	if (!$dry_run) {
451
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
452
		$restart_dhcpd = true;
453
	}
454
}
455

    
456
$upperifname = strtoupper($interface);
457
if (!$dry_run) {
458
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
459
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
460
	interface_reconfigure(strtolower($upperifname));
461
	echo "\n Reloading filter...";
462
	filter_configure_sync();
463
	if($restart_dhcpd) {
464
		echo "\n DHCPD..."; 
465
		services_dhcpd_configure();
466
	}
467
	if($restart_webgui) {
468
		echo "\n Restarting webConfigurator... ";
469
		mwexec("/etc/rc.restart_webgui");
470
	}
471
}
472
	
473
if ($intip != '') {
474
	if (is_ipaddr($intip)) {
475
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
476
		                      $upperifname, "{$intip}/{$intbits}") . "\n";
477
	} else {
478
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
479
		                      $upperifname, $intip) . "\n";
480
	}
481
}
482
if ($intip6 != '') {
483
	if (is_ipaddr($intip6)) {
484
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
485
		                      $upperifname, "${intip6}/${intbits6}") . "\n";
486
	} else {
487
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
488
		                      $upperifname, $intip6) . "\n";
489
	}
490
}
491

    
492
if ($intip != '' || $intip6 != '') {
493
	if (count($ifdescrs) == "1" or $interface == "lan") {
494
		if ($debug) {
495
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
496
			echo "interface is {$interface} \n";
497
		}
498
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
499
		if(!empty($config['system']['webgui']['port'])) {
500
			$webuiport = $config['system']['webgui']['port'];
501
			if ($intip != '') {
502
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
503
			}
504
			if ($intip6 != '') {
505
				if (is_ipaddr($intip6)) {
506
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
507
				} else {
508
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
509
				}
510
			}
511
		} else {
512
			if ($intip != '') {
513
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
514
			}
515
			if ($intip6 != '') {
516
				if (is_ipaddr($intip6)) {
517
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
518
				} else {
519
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
520
				}
521
			}
522
		}
523
	}
524
}
525

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

    
528
fgets($fp);
529
fclose($fp);
530
		
531
?>
(61-61/103)