Project

General

Profile

Download (16.7 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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * originally part of m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27
 */
28

    
29
$options = getopt("hn", array("dry-run", "help"));
30

    
31
if (isset($options["h"]) || isset($options["help"])) {
32
	echo "usage: /etc/rc.initial.setlanip [option ...]\n";
33
	echo "  -h, --help       show this message\n";
34
	echo "  -n, --dry-run    do not make any configuration changes\n";
35
	return 0;
36
}
37

    
38
$dry_run = isset($options["n"]) || isset($options["dry-run"]);
39
if ($dry_run) {
40
	echo "DRY RUN MODE IS ON\n";
41
}
42

    
43
/* parse the configuration and include all functions used below */
44
require_once("config.inc");
45
require_once("functions.inc");
46
require_once("filter.inc");
47
require_once("shaper.inc");
48
require_once("rrd.inc");
49

    
50
function console_prompt_for_yn ($prompt_text) {
51
	global $fp;
52

    
53
	$good_answer = false;
54

    
55
	do {
56
		echo "\n" . $prompt_text . " (y/n) ";
57
		$yn = strtolower(chop(fgets($fp)));
58
		if (($yn == "y") || ($yn == "yes")) {
59
			$boolean_answer = true;
60
			$good_answer = true;
61
		}
62
		if (($yn == "n") || ($yn == "no")) {
63
			$boolean_answer = false;
64
			$good_answer = true;
65
		}
66
	} while (!$good_answer);
67

    
68
	return $boolean_answer;
69
}
70

    
71
function console_get_interface_from_ppp($realif) {
72
	global $config;
73

    
74
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
75
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
76
			if ($realif == $ppp['if']) {
77
				$ifaces = explode(",", $ppp['ports']);
78
				return $ifaces[0];
79
			}
80
		}
81
	}
82

    
83
	return "";
84
}
85

    
86
function prompt_for_enable_dhcp_server($version = 4) {
87
	global $config, $fp, $interface;
88
	if ($interface == "wan") {
89
		if ($config['interfaces']['lan']) {
90
			return false;
91
		}
92
	}
93
	/* only allow DHCP server to be enabled when static IP is
94
	   configured on this interface */
95
	if ($version === 6) {
96
		$is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
97
	} else {
98
		$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
99
	}
100
	if (!($is_ipaddr)) {
101
		return false;
102
	}
103

    
104
	$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
105
	$upperifname = strtoupper($interface);
106
	return console_prompt_for_yn (sprintf(gettext('Do you want to enable the %1$s server on %2$s?'), $label_DHCP, $upperifname));
107
}
108

    
109
function get_interface_config_description($iface) {
110
	global $config;
111
	$c = $config['interfaces'][$iface];
112
	if (!$c) {
113
		return null;
114
	}
115
	$if = $c['if'];
116
	$result = $if;
117
	$result2 = array();
118
	$ipaddr = $c['ipaddr'];
119
	$ipaddrv6 = $c['ipaddrv6'];
120
	if (is_ipaddr($ipaddr)) {
121
		$result2[] = "static";
122
	} else if ($ipaddr == "dhcp") {
123
		$result2[] = "dhcp";
124
	}
125
	if (is_ipaddr($ipaddrv6)) {
126
		$result2[] = "staticv6";
127
	} else if ($ipaddrv6 == "dhcp6") {
128
		$result2[] = "dhcp6";
129
	}
130
	if (count($result2)) {
131
		$result .= " - " . implode(", ", $result2);
132
	}
133
	return $result;
134
}
135

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

    
138
/* build an interface collection */
139
$ifdescrs = get_configured_interface_with_descr(true);
140
$count = count($ifdescrs);
141

    
142
/* grab interface that we will operate on, unless there is only one interface */
143
if ($count > 1) {
144
	echo "Available interfaces:\n\n";
145
	$x=1;
146
	foreach ($ifdescrs as $iface => $ifdescr) {
147
		$config_descr = get_interface_config_description($iface);
148
		echo "{$x} - {$ifdescr} ({$config_descr})\n";
149
		$x++;
150
	}
151
	echo "\nEnter the number of the interface you wish to configure: ";
152
	$intnum = chop(fgets($fp));
153
} else {
154
	$intnum = $count;
155
}
156

    
157
if ($intnum < 1) {
158
	return;
159
}
160
if ($intnum > $count) {
161
	return;
162
}
163

    
164
$index = 1;
165
foreach ($ifdescrs as $ifname => $ifdesc) {
166
	if ($intnum == $index) {
167
		$interface = $ifname;
168
		break;
169
	} else {
170
		$index++;
171
	}
172
}
173
if (!$interface) {
174
	echo "Invalid interface!\n";
175
	return;
176
}
177

    
178
$ifaceassigned = "";
179

    
180
function next_unused_gateway_name($interface) {
181
	global $g, $config;
182
	$new_name = "GW_" . strtoupper($interface);
183

    
184
	if (!is_array($config['gateways']['gateway_item'])) {
185
		return $new_name;
186
	}
187
	$count = 1;
188
	do {
189
		$existing = false;
190
		foreach ($config['gateways']['gateway_item'] as $item) {
191
			if ($item['name'] === $new_name) {
192
				$existing = true;
193
				break;
194
			}
195
		}
196
		if ($existing) {
197
			$count += 1;
198
			$new_name = "GW_" . strtoupper($interface) . "_" . $count;
199
		}
200
	} while ($existing);
201
	return $new_name;
202
}
203

    
204
function add_gateway_to_config($interface, $gatewayip, $inet_type) {
205
	global $g, $config, $dry_run;
206
	init_config_arr(array('gateways', 'gateway_item'));
207
	$a_gateways = &$config['gateways']['gateway_item'];
208
	if ($dry_run) {
209
		print_r($a_gateways);
210
	}
211
	$new_name = '';
212
	foreach ($a_gateways as $item) {
213
		if ($item['ipprotocol'] === $inet_type) {
214
			if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip)) {
215
				$new_name = $item['name'];
216
			}
217
		}
218
	}
219
	if ($new_name == '') {
220
		$new_name = next_unused_gateway_name($interface);
221
		$item = array(
222
			"interface" => $interface,
223
			"gateway" => $gatewayip,
224
			"name" => $new_name,
225
			"weight" => 1,
226
			"ipprotocol" => $inet_type,
227
			"interval" => true,
228
			"descr" => "Interface $interface Gateway"
229
		);
230
		if ($dry_run) {
231
			print_r($item);
232
		}
233
		$a_gateways[] = $item;
234
	}
235

    
236
	//set the new GW as the default if there isnt one set yet
237
	if ($item['ipprotocol'] == "inet" && empty(isset($config['gateways']['defaultgw4']))) {
238
		$config['gateways']['defaultgw4'] = $new_name;
239
	}
240
	if ($item['ipprotocol'] == "inet6" && empty(isset($config['gateways']['defaultgw6']))) {
241
		$config['gateways']['defaultgw6'] = $new_name;
242
	}
243

    
244
	return $new_name;
245
}
246

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

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

    
254
	$upperifname = strtoupper($interface);
255

    
256
	if ($interface == "wan") {
257
		if (console_prompt_for_yn (sprintf(gettext('Configure %1$s address %2$s interface via %3$s?'), $label_IPvX, $upperifname, $label_DHCP))) {
258
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
259
			if (!empty($ifppp)) {
260
				$ifaceassigned = $ifppp;
261
			}
262
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
263
			$intbits = "";
264
			$isintdhcp = true;
265
			$restart_dhcpd = true;
266
		}
267
	}
268

    
269
	if ($isintdhcp == false or $interface <> "wan") {
270
		while (true) {
271
			do {
272
				echo "\n" . sprintf(gettext('Enter the new %1$s %2$s address.  Press <ENTER> for none:'),
273
							$upperifname, $label_IPvX) . "\n> ";
274
				$intip = chop(fgets($fp));
275
				$intbits_ok = false;
276
				if (strstr($intip, "/")) {
277
					list($intip, $intbits) = explode("/", $intip);
278
					$intbits_ok = (is_numeric($intbits) && (($intbits >= 1) && ($intbits <= $maxbits))) ? true : false;
279
				}
280
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
281
				if ($is_ipaddr && is_ipaddr_configured($intip, $interface, true)) {
282
					$ip_conflict = true;
283
					echo gettext("This IP address conflicts with another interface or a VIP") . "\n";
284
				} else {
285
					$ip_conflict = false;
286
				}
287
			} while (($ip_conflict === true) || !($is_ipaddr || $intip == ''));
288
			if ($is_ipaddr && $intip != '') {
289
				if ($intbits_ok == false) {
290
					echo "\n" . sprintf(gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
291
							$g['product_name']) . "\n";
292
					if ($version === 6) {
293
						echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
294
						echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
295
						echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
296
						echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
297
						echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
298
					} else {
299
						echo "e.g. 255.255.255.0 = 24\n";
300
						echo "     255.255.0.0   = 16\n";
301
						echo "     255.0.0.0     = 8\n";
302
					}
303
				}
304
				while ($intbits_ok == false) {
305
					$upperifname = strtoupper($interface);
306
					echo "\n" . sprintf(gettext('Enter the new %1$s %2$s subnet bit count (1 to %3$s):'),
307
								$upperifname, $label_IPvX, $maxbits) . "\n> ";
308
					$intbits = chop(fgets($fp));
309
					$intbits_ok = is_numeric($intbits) && (($intbits >= 1) && ($intbits <= $maxbits));
310
					$restart_dhcpd = true;
311

    
312
					if ($version === 4 && $intbits < $maxbits) {
313
						if ($intip == gen_subnet($intip, $intbits)) {
314
							echo gettext("You cannot set network address to an interface");
315
							continue 2;
316
							$intbits_ok = false;
317
						} else if ($intip == gen_subnet_max($intip, $intbits)) {
318
							echo gettext("You cannot set broadcast address to an interface");
319
							continue 2;
320
							$intbits_ok = false;
321
						}
322
					}
323
				}
324

    
325
				if ($version === 6) {
326
					$subnet = gen_subnetv6($intip, $intbits);
327
				} else {
328
					$subnet = gen_subnet($intip, $intbits);
329
				}
330
				do {
331
					echo "\n" . sprintf(gettext('For a WAN, enter the new %1$s %2$s upstream gateway address.'), $upperifname, $label_IPvX) . "\n" .
332
								gettext("For a LAN, press <ENTER> for none:") . "\n> ";
333
					$gwip = chop(fgets($fp));
334
					$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
335
					$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
336
					if ($gwip != '') {
337
						if (!$is_ipaddr) {
338
							echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
339
						} else if (!$is_in_subnet) {
340
							echo gettext("not in subnet!") . "\n";
341
						}
342
					}
343
				} while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));
344

    
345
				if ($gwip != '') {
346
					$inet_type = ($version === 6) ? "inet6" : "inet";
347
					$gwname = add_gateway_to_config($interface, $gwip, $inet_type);
348
				}
349
			}
350
			$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
351
			if (!empty($ifppp)) {
352
				$ifaceassigned = $ifppp;
353
			}
354
			break;
355
		}
356
	}
357

    
358
	return array($intip, $intbits, $gwname);
359
}
360

    
361
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
362
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
363

    
364
if (!empty($ifaceassigned)) {
365
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
366
}
367
$config['interfaces'][$interface]['ipaddr']    = $intip;
368
$config['interfaces'][$interface]['subnet']    = $intbits;
369
$config['interfaces'][$interface]['gateway']   = $gwname;
370
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
371
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
372
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
373
$config['interfaces'][$interface]['enable']    = true;
374

    
375
function console_configure_dhcpd($version = 4) {
376
	global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
377

    
378
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
379
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
380

    
381
	if ($g['services_dhcp_server_enable'] && prompt_for_enable_dhcp_server($version)) {
382
		$subnet_start = ($version === 6) ? gen_subnetv6($intip6, $intbits6) : gen_subnet($intip, $intbits);
383
		$subnet_end = ($version === 6) ? gen_subnetv6_max($intip6, $intbits6) : gen_subnet_max($intip, $intbits);
384
		do {
385
			do {
386
				echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
387
				$dhcpstartip = chop(fgets($fp));
388
				if ($dhcpstartip === "") {
389
					fclose($fp);
390
					return 0;
391
				}
392
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
393
				$is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
394
				if (!$is_inrange) {
395
					echo gettext("This IP address must be in the interface's subnet") . "\n";
396
				}
397
			} while (!$is_ipaddr || !$is_inrange);
398

    
399
			do {
400
				echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
401
				$dhcpendip = chop(fgets($fp));
402
				if ($dhcpendip === "") {
403
					fclose($fp);
404
					return 0;
405
				}
406
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
407
				$is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
408
				if (!$is_inrange) {
409
					echo gettext("This IP address must be in the interface's subnet") . "\n";
410
				}
411
				$not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
412
				if ($not_inorder) {
413
					echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
414
				}
415
			} while (!$is_ipaddr || !$is_inrange);
416
		} while ($not_inorder);
417
		$restart_dhcpd = true;
418
		init_config_arr(array($dhcpd, $interface, 'range'));
419
		$config[$dhcpd][$interface]['enable'] = true;
420
		$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
421
		$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
422
	} else {
423
		if (isset($config[$dhcpd][$interface]['enable'])) {
424
			unset($config[$dhcpd][$interface]['enable']);
425
			printf(gettext("Disabling %s DHCPD..."), $label_IPvX);
426
			$restart_dhcpd = true;
427
		}
428
	}
429
	return 1;
430
}
431

    
432
if (console_configure_dhcpd(4) == 0) {
433
	return 0;
434
}
435
if (console_configure_dhcpd(6) == 0) {
436
	return 0;
437
}
438

    
439
//*****************************************************************************
440

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

    
443
	if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
444
		$config['system']['webgui']['protocol'] = "http";
445
		$restart_webgui = true;
446
	}
447
}
448

    
449
if (isset($config['system']['webgui']['noantilockout'])) {
450
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
451
	unset($config['system']['webgui']['noantilockout']);
452
}
453

    
454
if ($config['interfaces']['lan']) {
455
	if ($config['dhcpd']) {
456
		if ($config['dhcpd']['wan']) {
457
			unset($config['dhcpd']['wan']);
458
		}
459
	}
460
	if ($config['dhcpdv6']) {
461
		if ($config['dhcpdv6']['wan']) {
462
			unset($config['dhcpdv6']['wan']);
463
		}
464
	}
465
}
466

    
467
if (!$config['interfaces']['lan']) {
468
	unset($config['interfaces']['lan']);
469
	if ($config['dhcpd']['lan']) {
470
		unset($config['dhcpd']['lan']);
471
	}
472
	if ($config['dhcpdv6']['lan']) {
473
		unset($config['dhcpdv6']['lan']);
474
	}
475
	unset($config['shaper']);
476
	unset($config['ezshaper']);
477
	unset($config['nat']);
478
	if (!$dry_run) {
479
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
480
		$restart_dhcpd = true;
481
	}
482
}
483

    
484
$upperifname = strtoupper($interface);
485
if (!$dry_run) {
486
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
487
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
488
	interface_reconfigure(strtolower($upperifname));
489
	echo "\n Reloading filter...";
490
	filter_configure_sync();
491
	echo "\n Reloading routing configuration...";
492
	system_routing_configure();
493
	if ($restart_dhcpd) {
494
		echo "\n DHCPD...";
495
		services_dhcpd_configure();
496
	}
497
	if ($restart_webgui) {
498
		echo "\n Restarting webConfigurator... ";
499
		mwexec("/etc/rc.restart_webgui");
500
	}
501
}
502

    
503
if ($intip != '') {
504
	if (is_ipaddr($intip)) {
505
		$intipstr = "{$intip}/{$intbits}";
506
	} else {
507
		$intipstr = $intip;
508
	}
509
	echo "\n\n" . sprintf(gettext('The IPv4 %1$s address has been set to %2$s'), $upperifname, $intipstr) . "\n";
510
}
511
if ($intip6 != '') {
512
	if (is_ipaddr($intip6)) {
513
		$intip6str = "${intip6}/${intbits6}";
514
	} else {
515
		$intip6str = $intip6;
516
	}
517
	echo "\n\n" . sprintf(gettext('The IPv6 %1$s address has been set to %2$s'), $upperifname, $intip6str) . "\n";
518
}
519

    
520
if ($intip != '' || $intip6 != '') {
521
	if (count($ifdescrs) == "1" or $interface == "lan") {
522
		if ($debug) {
523
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
524
			echo "interface is {$interface} \n";
525
		}
526
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
527
		if (!empty($config['system']['webgui']['port'])) {
528
			$webuiport = $config['system']['webgui']['port'];
529
			if ($intip != '') {
530
				echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
531
			}
532
			if ($intip6 != '') {
533
				if (is_ipaddr($intip6)) {
534
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
535
				} else {
536
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
537
				}
538
			}
539
		} else {
540
			if ($intip != '') {
541
				echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
542
			}
543
			if ($intip6 != '') {
544
				if (is_ipaddr($intip6)) {
545
					echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
546
				} else {
547
					echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
548
				}
549
			}
550
		}
551
	}
552
}
553

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

    
556
fgets($fp);
557
fclose($fp);
558

    
559
?>
(43-43/81)