Project

General

Profile

Download (17.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -q
2
<?php
3
/*
4
 * rc.initial.setlanip
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24
 *
25
 * 3. All advertising materials mentioning features or use of this software
26
 *    must display the following acknowledgment:
27
 *    "This product includes software developed by the pfSense Project
28
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
29
 *
30
 * 4. The names "pfSense" and "pfSense Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    coreteam@pfsense.org.
34
 *
35
 * 5. Products derived from this software may not be called "pfSense"
36
 *    nor may "pfSense" appear in their names without prior written
37
 *    permission of the Electric Sheep Fencing, LLC.
38
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *
42
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44
 *
45
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
46
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
49
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56
 * OF THE POSSIBILITY OF SUCH DAMAGE.
57
 */
58

    
59
$options = getopt("hn", array("dry-run", "help"));
60

    
61
if (isset($options["h"]) || isset($options["help"])) {
62
	echo "usage: /etc/rc.initial.setlanip [option ...]\n";
63
	echo "  -h, --help       show this message\n";
64
	echo "  -n, --dry-run    do not make any configuration changes\n";
65
	return 0;
66
}
67

    
68
$dry_run = isset($options["n"]) || isset($options["dry-run"]);
69
if ($dry_run) {
70
	echo "DRY RUN MODE IS ON\n";
71
}
72

    
73
/* parse the configuration and include all functions used below */
74
require_once("config.inc");
75
require_once("functions.inc");
76
require_once("filter.inc");
77
require_once("shaper.inc");
78
require_once("rrd.inc");
79

    
80
function console_prompt_for_yn ($prompt_text) {
81
	global $fp;
82

    
83
	$good_answer = false;
84

    
85
	do {
86
		echo "\n" . $prompt_text . " (y/n) ";
87
		$yn = strtolower(chop(fgets($fp)));
88
		if (($yn == "y") || ($yn == "yes")) {
89
			$boolean_answer = true;
90
			$good_answer = true;
91
		}
92
		if (($yn == "n") || ($yn == "no")) {
93
			$boolean_answer = false;
94
			$good_answer = true;
95
		}
96
	} while (!$good_answer);
97

    
98
	return $boolean_answer;
99
}
100

    
101
function console_get_interface_from_ppp($realif) {
102
	global $config;
103

    
104
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
105
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
106
			if ($realif == $ppp['if']) {
107
				$ifaces = explode(",", $ppp['ports']);
108
				return $ifaces[0];
109
			}
110
		}
111
	}
112

    
113
	return "";
114
}
115

    
116
function prompt_for_enable_dhcp_server($version = 4) {
117
	global $config, $fp, $interface;
118
	if ($interface == "wan") {
119
		if ($config['interfaces']['lan']) {
120
			return false;
121
		}
122
	}
123
	/* only allow DHCP server to be enabled when static IP is
124
	   configured on this interface */
125
	if ($version === 6) {
126
		$is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
127
	} else {
128
		$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
129
	}
130
	if (!($is_ipaddr)) {
131
		return false;
132
	}
133

    
134
	$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
135
	$upperifname = strtoupper($interface);
136
	return console_prompt_for_yn (sprintf(gettext("Do you want to enable the %s server on %s?"), $label_DHCP, $upperifname));
137
}
138

    
139
function get_interface_config_description($iface) {
140
	global $config;
141
	$c = $config['interfaces'][$iface];
142
	if (!$c) {
143
		return null;
144
	}
145
	$if = $c['if'];
146
	$result = $if;
147
	$result2 = array();
148
	$ipaddr = $c['ipaddr'];
149
	$ipaddrv6 = $c['ipaddrv6'];
150
	if (is_ipaddr($ipaddr)) {
151
		$result2[] = "static";
152
	} else if ($ipaddr == "dhcp") {
153
		$result2[] = "dhcp";
154
	}
155
	if (is_ipaddr($ipaddrv6)) {
156
		$result2[] = "staticv6";
157
	} else if ($ipaddrv6 == "dhcp6") {
158
		$result2[] = "dhcp6";
159
	}
160
	if (count($result2)) {
161
		$result .= " - " . implode(", ", $result2);
162
	}
163
	return $result;
164
}
165

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

    
168
/* build an interface collection */
169
$ifdescrs = get_configured_interface_with_descr(false, true);
170
$count = count($ifdescrs);
171

    
172
/* grab interface that we will operate on, unless there is only one interface */
173
if ($count > 1) {
174
	echo "Available interfaces:\n\n";
175
	$x=1;
176
	foreach ($ifdescrs as $iface => $ifdescr) {
177
		$config_descr = get_interface_config_description($iface);
178
		echo "{$x} - {$ifdescr} ({$config_descr})\n";
179
		$x++;
180
	}
181
	echo "\nEnter the number of the interface you wish to configure: ";
182
	$intnum = chop(fgets($fp));
183
} else {
184
	$intnum = $count;
185
}
186

    
187
if ($intnum < 1) {
188
	return;
189
}
190
if ($intnum > $count) {
191
	return;
192
}
193

    
194
$index = 1;
195
foreach ($ifdescrs as $ifname => $ifdesc) {
196
	if ($intnum == $index) {
197
		$interface = $ifname;
198
		break;
199
	} else {
200
		$index++;
201
	}
202
}
203
if (!$interface) {
204
	echo "Invalid interface!\n";
205
	return;
206
}
207

    
208
$ifaceassigned = "";
209

    
210
function next_unused_gateway_name($interface) {
211
	global $g, $config;
212
	$new_name = "GW_" . strtoupper($interface);
213

    
214
	if (!is_array($config['gateways']['gateway_item'])) {
215
		return $new_name;
216
	}
217
	$count = 1;
218
	do {
219
		$existing = false;
220
		foreach ($config['gateways']['gateway_item'] as $item) {
221
			if ($item['name'] === $new_name) {
222
				$existing = true;
223
				break;
224
			}
225
		}
226
		if ($existing) {
227
			$count += 1;
228
			$new_name = "GW_" . strtoupper($interface) . "_" . $count;
229
		}
230
	} while ($existing);
231
	return $new_name;
232
}
233

    
234
function add_gateway_to_config($interface, $gatewayip, $inet_type) {
235
	global $g, $config, $dry_run;
236
	if (!is_array($config['gateways']['gateway_item'])) {
237
		$config['gateways']['gateway_item'] = array();
238
	}
239
	$a_gateways = &$config['gateways']['gateway_item'];
240
	if ($dry_run) {
241
		print_r($a_gateways);
242
	}
243
	$new_name = '';
244
	$is_default = true;
245
	foreach ($a_gateways as $item) {
246
		if ($item['ipprotocol'] === $inet_type) {
247
			if (isset($item['defaultgw'])) {
248
				$is_default = false;
249
			}
250
			if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip)) {
251
				$new_name = $item['name'];
252
			}
253
		}
254
	}
255
	if ($new_name == '') {
256
		$new_name = next_unused_gateway_name($interface);
257
		$item = array(
258
			"interface" => $interface,
259
			"gateway" => $gatewayip,
260
			"name" => $new_name,
261
			"weight" => 1,
262
			"ipprotocol" => $inet_type,
263
			"interval" => true,
264
			"descr" => "Interface $interface Gateway",
265
			"defaultgw" => $is_default
266
		);
267
		if ($dry_run) {
268
			print_r($item);
269
		}
270
		$a_gateways[] = $item;
271
	}
272

    
273
	return $new_name;
274
}
275

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

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

    
283
	$upperifname = strtoupper($interface);
284

    
285
	if ($interface == "wan") {
286
		if (console_prompt_for_yn (sprintf(gettext("Configure %s address %s interface via %s?"), $label_IPvX, $upperifname, $label_DHCP))) {
287
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
288
			if (!empty($ifppp)) {
289
				$ifaceassigned = $ifppp;
290
			}
291
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
292
			$intbits = "";
293
			$isintdhcp = true;
294
			$restart_dhcpd = true;
295
		}
296
	}
297

    
298
	if ($isintdhcp == false or $interface <> "wan") {
299
		while (true) {
300
			do {
301
				echo "\n" . sprintf(gettext("Enter the new %s %s address.  Press <ENTER> for none:"),
302
							$upperifname, $label_IPvX) . "\n> ";
303
				$intip = chop(fgets($fp));
304
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
305
				if ($is_ipaddr && is_ipaddr_configured($intip, $interface, true)) {
306
					$ip_conflict = true;
307
					echo gettext("This IP address conflicts with another interface or a VIP") . "\n";
308
				} else {
309
					$ip_conflict = false;
310
				}
311
			} while (($ip_conflict === true) || !($is_ipaddr || $intip == ''));
312
			if ($intip != '') {
313
				echo "\n" . sprintf(gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
314
							$g['product_name']) . "\n";
315
				if ($version === 6) {
316
					echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
317
					echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
318
					echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
319
					echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
320
					echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
321
				} else {
322
					echo "e.g. 255.255.255.0 = 24\n";
323
					echo "     255.255.0.0   = 16\n";
324
					echo "     255.0.0.0     = 8\n";
325
				}
326
				do {
327
					$upperifname = strtoupper($interface);
328
					echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count (1 to %s):"),
329
								$upperifname, $label_IPvX, $maxbits) . "\n> ";
330
					$intbits = chop(fgets($fp));
331
					$intbits_ok = is_numeric($intbits) && (($intbits >= 1) && ($intbits <= $maxbits));
332
					$restart_dhcpd = true;
333

    
334
					if ($version === 4 && $intbits < $maxbits) {
335
						if ($intip == gen_subnet($intip, $intbits)) {
336
							echo gettext("You cannot set network address to an interface");
337
							continue 2;
338
							$intbits_ok = false;
339
						} else if ($intip == gen_subnet_max($intip, $intbits)) {
340
							echo gettext("You cannot set broadcast address to an interface");
341
							continue 2;
342
							$intbits_ok = false;
343
						}
344
					}
345
				} while (!$intbits_ok);
346

    
347
				if ($version === 6) {
348
					$subnet = gen_subnetv6($intip, $intbits);
349
				} else {
350
					$subnet = gen_subnet($intip, $intbits);
351
				}
352
				do {
353
					echo "\n" . sprintf(gettext("For a WAN, enter the new %s %s upstream gateway address."), $upperifname, $label_IPvX) . "\n" .
354
								gettext("For a LAN, press <ENTER> for none:") . "\n> ";
355
					$gwip = chop(fgets($fp));
356
					$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
357
					$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
358
					if ($gwip != '') {
359
						if (!$is_ipaddr) {
360
							echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
361
						} else if (!$is_in_subnet) {
362
							echo gettext("not in subnet!") . "\n";
363
						}
364
					}
365
				} while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));
366

    
367
				if ($gwip != '') {
368
					$inet_type = ($version === 6) ? "inet6" : "inet";
369
					$gwname = add_gateway_to_config($interface, $gwip, $inet_type);
370
				}
371
			}
372
			$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
373
			if (!empty($ifppp)) {
374
				$ifaceassigned = $ifppp;
375
			}
376
			break;
377
		}
378
	}
379

    
380
	return array($intip, $intbits, $gwname);
381
}
382

    
383
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
384
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
385

    
386
if (!empty($ifaceassigned)) {
387
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
388
}
389
$config['interfaces'][$interface]['ipaddr']    = $intip;
390
$config['interfaces'][$interface]['subnet']    = $intbits;
391
$config['interfaces'][$interface]['gateway']   = $gwname;
392
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
393
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
394
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
395
$config['interfaces'][$interface]['enable']    = true;
396

    
397
function console_configure_dhcpd($version = 4) {
398
	global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
399

    
400
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
401
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
402

    
403
	if ($g['services_dhcp_server_enable'] && prompt_for_enable_dhcp_server($version)) {
404
		$subnet_start = ($version === 6) ? gen_subnetv6($intip6, $intbits6) : gen_subnet($intip, $intbits);
405
		$subnet_end = ($version === 6) ? gen_subnetv6_max($intip6, $intbits6) : gen_subnet_max($intip, $intbits);
406
		do {
407
			do {
408
				echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
409
				$dhcpstartip = chop(fgets($fp));
410
				if ($dhcpstartip === "") {
411
					fclose($fp);
412
					return 0;
413
				}
414
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
415
				$is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
416
				if (!$is_inrange) {
417
					echo gettext("This IP address must be in the interface's subnet") . "\n";
418
				}
419
			} while (!$is_ipaddr || !$is_inrange);
420

    
421
			do {
422
				echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
423
				$dhcpendip = chop(fgets($fp));
424
				if ($dhcpendip === "") {
425
					fclose($fp);
426
					return 0;
427
				}
428
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
429
				$is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
430
				if (!$is_inrange) {
431
					echo gettext("This IP address must be in the interface's subnet") . "\n";
432
				}
433
				$not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
434
				if ($not_inorder) {
435
					echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
436
				}
437
			} while (!$is_ipaddr || !$is_inrange);
438
		} while ($not_inorder);
439
		$restart_dhcpd = true;
440
		$config[$dhcpd][$interface]['enable'] = true;
441
		$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
442
		$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
443
	} else {
444
		if (isset($config[$dhcpd][$interface]['enable'])) {
445
			unset($config[$dhcpd][$interface]['enable']);
446
			printf(gettext("Disabling %s DHCPD..."), $label_IPvX);
447
			$restart_dhcpd = true;
448
		}
449
	}
450
	return 1;
451
}
452

    
453
if (console_configure_dhcpd(4) == 0) {
454
	return 0;
455
}
456
if (console_configure_dhcpd(6) == 0) {
457
	return 0;
458
}
459

    
460
//*****************************************************************************
461

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

    
464
	if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
465
		$config['system']['webgui']['protocol'] = "http";
466
		$restart_webgui = true;
467
	}
468
}
469

    
470
if (isset($config['system']['webgui']['noantilockout'])) {
471
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
472
	unset($config['system']['webgui']['noantilockout']);
473
}
474

    
475
if ($config['interfaces']['lan']) {
476
	if ($config['dhcpd']) {
477
		if ($config['dhcpd']['wan']) {
478
			unset($config['dhcpd']['wan']);
479
		}
480
	}
481
	if ($config['dhcpdv6']) {
482
		if ($config['dhcpdv6']['wan']) {
483
			unset($config['dhcpdv6']['wan']);
484
		}
485
	}
486
}
487

    
488
if (!$config['interfaces']['lan']) {
489
	unset($config['interfaces']['lan']);
490
	if ($config['dhcpd']['lan']) {
491
		unset($config['dhcpd']['lan']);
492
	}
493
	if ($config['dhcpdv6']['lan']) {
494
		unset($config['dhcpdv6']['lan']);
495
	}
496
	unset($config['shaper']);
497
	unset($config['ezshaper']);
498
	unset($config['nat']);
499
	if (!$dry_run) {
500
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
501
		$restart_dhcpd = true;
502
	}
503
}
504

    
505
$upperifname = strtoupper($interface);
506
if (!$dry_run) {
507
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
508
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
509
	interface_reconfigure(strtolower($upperifname));
510
	echo "\n Reloading filter...";
511
	filter_configure_sync();
512
	echo "\n Reloading routing configuration...";
513
	system_routing_configure();
514
	if ($restart_dhcpd) {
515
		echo "\n DHCPD...";
516
		services_dhcpd_configure();
517
	}
518
	if ($restart_webgui) {
519
		echo "\n Restarting webConfigurator... ";
520
		mwexec("/etc/rc.restart_webgui");
521
	}
522
}
523

    
524
if ($intip != '') {
525
	if (is_ipaddr($intip)) {
526
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
527
							  $upperifname, "{$intip}/{$intbits}") . "\n";
528
	} else {
529
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
530
							  $upperifname, $intip) . "\n";
531
	}
532
}
533
if ($intip6 != '') {
534
	if (is_ipaddr($intip6)) {
535
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
536
							  $upperifname, "${intip6}/${intbits6}") . "\n";
537
	} else {
538
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
539
							  $upperifname, $intip6) . "\n";
540
	}
541
}
542

    
543
if ($intip != '' || $intip6 != '') {
544
	if (count($ifdescrs) == "1" or $interface == "lan") {
545
		if ($debug) {
546
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
547
			echo "interface is {$interface} \n";
548
		}
549
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
550
		if (!empty($config['system']['webgui']['port'])) {
551
			$webuiport = $config['system']['webgui']['port'];
552
			if ($intip != '') {
553
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
554
			}
555
			if ($intip6 != '') {
556
				if (is_ipaddr($intip6)) {
557
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
558
				} else {
559
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
560
				}
561
			}
562
		} else {
563
			if ($intip != '') {
564
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
565
			}
566
			if ($intip6 != '') {
567
				if (is_ipaddr($intip6)) {
568
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
569
				} else {
570
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
571
				}
572
			}
573
		}
574
	}
575
}
576

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

    
579
fgets($fp);
580
fclose($fp);
581

    
582
?>
(54-54/94)