Project

General

Profile

Download (17.5 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-2022 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
$restart_webgui = false;
31

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

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

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

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

    
54
	$good_answer = false;
55

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

    
69
	return $boolean_answer;
70
}
71

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

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

    
84
	return "";
85
}
86

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

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

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

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

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

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

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

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

    
179
$ifaceassigned = "";
180

    
181
function next_unused_gateway_name($interface, $inet_type = 'inet') {
182
	global $g, $config;
183

    
184
	if ($inet_type == 'inet') {
185
		$name_suffix = "GW";
186
	} else {
187
		$name_suffix = "GWv6";
188
	}
189

    
190
	$new_name = strtoupper($interface) . $name_suffix;
191

    
192
	if (!is_array($config['gateways']['gateway_item'])) {
193
		return $new_name;
194
	}
195
	$count = 1;
196
	do {
197
		$existing = false;
198
		foreach ($config['gateways']['gateway_item'] as $item) {
199
			if ($item['name'] === $new_name) {
200
				$existing = true;
201
				break;
202
			}
203
		}
204
		if ($existing) {
205
			$count += 1;
206
			$new_name = strtoupper($interface) . $name_suffix . "_" . $count;
207
		}
208
	} while ($existing);
209
	return $new_name;
210
}
211

    
212
function add_gateway_to_config($interface, $gatewayip, $inet_type, $nonlocalgateway=false) {
213
	global $g, $config, $dry_run;
214
	init_config_arr(array('gateways', 'gateway_item'));
215
	$a_gateways = &$config['gateways']['gateway_item'];
216
	if ($dry_run) {
217
		print_r($a_gateways);
218
	}
219
	$new_name = '';
220
	foreach ($a_gateways as $item) {
221
		if ($item['ipprotocol'] === $inet_type) {
222
			if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip)) {
223
				$new_name = $item['name'];
224
			}
225
		}
226
	}
227
	if ($new_name == '') {
228
		$new_name = next_unused_gateway_name($interface, $inet_type);
229
		$item = array(
230
			"interface" => $interface,
231
			"gateway" => $gatewayip,
232
			"name" => $new_name,
233
			"weight" => 1,
234
			"ipprotocol" => $inet_type,
235
			"interval" => true,
236
			"descr" => "Interface $interface Gateway"
237
		);
238
		if ($nonlocalgateway) {
239
			$item['nonlocalgateway'] = true;
240
		}
241
		if ($dry_run) {
242
			print_r($item);
243
		}
244
		$a_gateways[] = $item;
245
	}
246

    
247
	//set the new GW as the default if there isnt one set yet
248
	init_config_arr('gateways');
249
	if ($item['ipprotocol'] == "inet" && empty(config_get_path('gateways/defaultgw4', ''))) {
250
		config_set_path('gateways/defaultgw4', $new_name);
251
	}
252
	if ($item['ipprotocol'] == "inet6" && empty(config_get_path('gateways/defaultgw4', ''))) {
253
		config_set_path('gateways/defaultgw4', $new_name);
254
	}
255

    
256
	return $new_name;
257
}
258

    
259
function console_configure_ip_address($version) {
260
	global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;
261

    
262
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
263
	$maxbits    = ($version === 6) ? 128      : 32;
264
	$label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";
265

    
266
	$upperifname = strtoupper($interface);
267

    
268
	if ($interface == "wan") {
269
		if (console_prompt_for_yn (sprintf(gettext('Configure %1$s address %2$s interface via %3$s?'), $label_IPvX, $upperifname, $label_DHCP))) {
270
			$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
271
			if (!empty($ifppp)) {
272
				$ifaceassigned = $ifppp;
273
			}
274
			$intip = ($version === 6) ? "dhcp6" : "dhcp";
275
			$intbits = "";
276
			$isintdhcp = true;
277
			$restart_dhcpd = true;
278
		}
279
	}
280

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

    
324
					if ($version === 4 && $intbits < $maxbits) {
325
						if ($intip == gen_subnet($intip, $intbits)) {
326
							echo gettext("You cannot set network address to an interface");
327
							continue 2;
328
							$intbits_ok = false;
329
						} else if ($intip == gen_subnet_max($intip, $intbits)) {
330
							echo gettext("You cannot set broadcast address to an interface");
331
							continue 2;
332
							$intbits_ok = false;
333
						}
334
					}
335
				}
336

    
337
				if ($version === 6) {
338
					$subnet = gen_subnetv6($intip, $intbits);
339
				} else {
340
					$subnet = gen_subnet($intip, $intbits);
341
				}
342
				do {
343
					echo "\n" . sprintf(gettext('For a WAN, enter the new %1$s %2$s upstream gateway address.'), $upperifname, $label_IPvX) . "\n" .
344
								gettext("For a LAN, press <ENTER> for none:") . "\n> ";
345
					$gwip = chop(fgets($fp));
346
					$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
347
					$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
348
					$nonlocalgateway = false;
349
					if ($gwip != '') {
350
						if (!$is_ipaddr) {
351
							echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
352
						} else if (!$is_in_subnet) {
353
							$nonlocalgateway = true;
354
							echo gettext("Non-local gateway detected.");
355
						}
356
					}
357
				} while (!(empty($gwip) || $is_ipaddr));
358

    
359
				if ($gwip != '') {
360
					$inet_type = ($version === 6) ? "inet6" : "inet";
361
					$gwname = add_gateway_to_config($interface, $gwip, $inet_type, $nonlocalgateway);
362
				}
363
			}
364
			$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
365
			if (!empty($ifppp)) {
366
				$ifaceassigned = $ifppp;
367
			}
368
			break;
369
		}
370
	}
371

    
372
	return array($intip, $intbits, $gwname);
373
}
374

    
375
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
376
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
377

    
378
if (!empty($ifaceassigned)) {
379
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
380
}
381
$config['interfaces'][$interface]['ipaddr']    = $intip;
382
$config['interfaces'][$interface]['subnet']    = $intbits;
383
$config['interfaces'][$interface]['gateway']   = $gwname;
384
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
385
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
386
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
387
$config['interfaces'][$interface]['enable']    = true;
388

    
389
function console_configure_dhcpd($version = 4) {
390
	global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
391

    
392
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
393
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
394

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

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

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

    
461
//*****************************************************************************
462

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

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

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

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

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

    
506
$upperifname = strtoupper($interface);
507
if (!$dry_run) {
508
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
509
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
510
	if (file_exists("{$g['conf_path']}/trigger_initial_wizard")) {
511
		// if any of the interfaces is manually configured, it means that the assignment is OK
512
		touch("{$g['conf_path']}/assign_complete");
513
	}
514
	interface_reconfigure(strtolower($upperifname));
515
	echo "\n Reloading filter...";
516
	filter_configure_sync();
517
	echo "\n Reloading routing configuration...";
518
	system_routing_configure();
519
	if ($restart_dhcpd) {
520
		echo "\n DHCPD...";
521
		services_dhcpd_configure();
522
	}
523
	if ($restart_webgui) {
524
		echo "\n Restarting webConfigurator... ";
525
		mwexec("/etc/rc.restart_webgui");
526
	}
527
}
528

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

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

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

    
582
fgets($fp);
583
fclose($fp);
584

    
585
?>
(45-45/85)