Project

General

Profile

Download (17.2 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-2024 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
	foreach (config_get_path('ppps/ppp', []) as $pppid => $ppp) {
74
		// skip modems, e.g. /dev/cuaZ99.0
75
		if ($ppp['type'] == 'ppp') {
76
			continue;
77
		}
78
		if ($realif == $ppp['if']) {
79
			$ifaces = explode(",", $ppp['ports']);
80
			return $ifaces[0];
81
		}
82
	}
83

    
84
	return "";
85
}
86

    
87
function prompt_for_enable_dhcp_server($version = 4) {
88
	global $fp, $interface;
89

    
90
	/* only allow DHCP server to be enabled when static IP is
91
	   configured on this interface */
92
	if ($version === 6) {
93
		$is_ipaddr = is_ipaddrv6(config_get_path("interfaces/{$interface}/ipaddrv6"));
94
	} else {
95
		$is_ipaddr = is_ipaddrv4(config_get_path("interfaces/{$interface}/ipaddr"));
96
	}
97
	if (!($is_ipaddr)) {
98
		return false;
99
	}
100

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

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

    
132
$fp = fopen('php://stdin', 'r');
133

    
134
/* build an interface collection */
135
$ifdescrs = get_configured_interface_with_descr(true);
136
$count = count($ifdescrs);
137

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

    
153
if ($intnum < 1) {
154
	return;
155
}
156
if ($intnum > $count) {
157
	return;
158
}
159

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

    
174
$ifaceassigned = "";
175

    
176
function next_unused_gateway_name($interface, $inet_type = 'inet') {
177
	if ($inet_type == 'inet') {
178
		$name_suffix = "GW";
179
	} else {
180
		$name_suffix = "GWv6";
181
	}
182

    
183
	$new_name = strtoupper($interface) . $name_suffix;
184

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

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

    
238
	//set the new GW as the default if there isnt one set yet
239
	if (console_prompt_for_yn (gettext("Should this gateway be set as the default gateway?"))) {
240
		if ($item['ipprotocol'] == "inet") {
241
			config_set_path('gateways/defaultgw4', $new_name);
242
		}
243
		if ($item['ipprotocol'] == "inet6") {
244
			config_set_path('gateways/defaultgw6', $new_name);
245
		}
246
	}
247

    
248
	return $new_name;
249
}
250

    
251
function console_configure_ip_address($version) {
252
	global $g, $interface, $restart_dhcpd, $ifaceassigned, $fp;
253

    
254
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
255
	$maxbits    = ($version === 6) ? 128      : 32;
256
	$label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";
257

    
258
	$upperifname = strtoupper($interface);
259

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

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

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

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

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

    
362
	return array($intip, $intbits, $gwname);
363
}
364

    
365
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
366
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
367

    
368
if (!empty($ifaceassigned)) {
369
	config_set_path("interfaces/{$interface}/if", $ifaceassigned);
370
}
371
config_set_path("interfaces/{$interface}/ipaddr", $intip);
372
config_set_path("interfaces/{$interface}/subnet", $intbits);
373
config_set_path("interfaces/{$interface}/gateway", $gwname);
374
config_set_path("interfaces/{$interface}/ipaddrv6", $intip6);
375
config_set_path("interfaces/{$interface}/subnetv6", $intbits6);
376
config_set_path("interfaces/{$interface}/gatewayv6", $gwname6);
377
config_set_path("interfaces/{$interface}/enable", true);
378

    
379
function console_configure_dhcpd($version = 4) {
380
	global $g, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
381

    
382
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
383
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
384

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

    
403
			do {
404
				echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
405
				$dhcpendip = chop(fgets($fp));
406
				if ($dhcpendip === "") {
407
					fclose($fp);
408
					return 0;
409
				}
410
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
411
				$is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
412
				if (!$is_inrange) {
413
					echo gettext("This IP address must be in the interface's subnet") . "\n";
414
				}
415
				$not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
416
				if ($not_inorder) {
417
					echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
418
				}
419
			} while (!$is_ipaddr || !$is_inrange);
420
		} while ($not_inorder);
421
		$restart_dhcpd = true;
422
		config_set_path("{$dhcpd}/{$interface}/enable", true);
423
		config_set_path("{$dhcpd}/{$interface}/range/from", $dhcpstartip);
424
		config_set_path("{$dhcpd}/{$interface}/range/to", $dhcpendip);
425
	} else {
426
		if (config_path_enabled("{$dhcpd}/{$interface}")) {
427
			config_del_path("{$dhcpd}/{$interface}/enable");
428
			$restart_dhcpd = true;
429
		}
430
		/* disable RA mode, see https://redmine.pfsense.org/issues/11609 */
431
		if ((config_get_path("{$dhcpd}/{$interface}/ramode") !== null) &&
432
		    (config_get_path("{$dhcpd}/{$interface}/ramode") != 'disabled')) {
433
			config_set_path("{$dhcpd}/{$interface}/ramode", 'disabled');
434
			$restart_dhcpd = true;
435
		}
436
		if ($restart_dhcpd) {
437
			printf(gettext("Disabling %s DHCPD...") . "\n", $label_IPvX);
438
		}
439
	}
440
	return 1;
441
}
442

    
443
if (console_configure_dhcpd(4) == 0) {
444
	return 0;
445
}
446
if (console_configure_dhcpd(6) == 0) {
447
	return 0;
448
}
449

    
450
//*****************************************************************************
451

    
452
if (config_get_path('system/webgui/protocol') == "https") {
453
	if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
454
		config_set_path('system/webgui/protocol', "http");
455
		$restart_webgui = true;
456
	}
457
}
458

    
459
if (config_path_enabled('system/webgui', 'noantilockout')) {
460
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
461
	config_del_path('system/webgui/noantilockout');
462
}
463

    
464
if (config_get_path('interfaces/lan')) {
465
	$paths = ['dhcpd/wan', 'dhcpdv6/wan'];
466
	foreach ($paths as $path) {
467
		if (config_get_path($path)) {
468
			config_del_path($path);
469
		}
470
    	}
471
}
472

    
473
if (!config_get_path('interfaces/lan')) {
474
	$paths = ['interfaces/lan', 'dhcpd/lan', 'dhcpdv6/lan',
475
			'shaper', 'ezshaper', 'nat'];
476
    	foreach ($paths as $path) {
477
		if (config_get_path($path)) {
478
			config_del_path($path);
479
		}
480
    	}
481
    	if (!$dry_run) {
482
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
483
		$restart_dhcpd = true;
484
    	}
485
}
486

    
487
$upperifname = strtoupper($interface);
488
if (!$dry_run) {
489
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
490
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
491
	if (file_exists("{$g['conf_path']}/trigger_initial_wizard")) {
492
		// if any of the interfaces is manually configured, it means that the assignment is OK
493
		touch("{$g['conf_path']}/assign_complete");
494
	}
495
	interface_reconfigure(strtolower($upperifname));
496
	echo "\n Reloading filter...";
497
	filter_configure_sync();
498
	echo "\n Reloading routing configuration...";
499
	system_routing_configure();
500
	if ($restart_dhcpd) {
501
		echo "\n DHCPD...";
502
		services_dhcpd_configure();
503
	}
504
	if ($restart_webgui) {
505
		echo "\n Restarting webConfigurator... ";
506
		mwexec("/etc/rc.restart_webgui");
507
	}
508
}
509

    
510
if ($intip != '') {
511
	if (is_ipaddr($intip)) {
512
		$intipstr = "{$intip}/{$intbits}";
513
	} else {
514
		$intipstr = $intip;
515
	}
516
	echo "\n\n" . sprintf(gettext('The IPv4 %1$s address has been set to %2$s'), $upperifname, $intipstr) . "\n";
517
}
518
if ($intip6 != '') {
519
	if (is_ipaddr($intip6)) {
520
		$intip6str = "{$intip6}/{$intbits6}";
521
	} else {
522
		$intip6str = $intip6;
523
	}
524
	echo "\n\n" . sprintf(gettext('The IPv6 %1$s address has been set to %2$s'), $upperifname, $intip6str) . "\n";
525
}
526

    
527
if ($intip != '' || $intip6 != '') {
528
	if ((count($ifdescrs) == "1") ||
529
	    (!interface_has_gateway($interface) && !interface_has_gatewayv6($interface))) {
530
		if ($debug) {
531
			echo "ifdescrs count is " . count($ifdescrs) . "\n";
532
			echo "interface is {$interface} \n";
533
		}
534
		echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
535
		$webuiport = config_get_path('system/webgui/port');
536
		$webuiprotocol = config_get_path('system/webgui/protocol');
537
		if (!empty($webuiport)) {
538
			if ($intip != '') {
539
				echo "		{$webuiprotocol}://{$intip}:{$webuiport}/\n";
540
			}
541
			if ($intip6 != '') {
542
				if (is_ipaddr($intip6)) {
543
					echo "		{$webuiprotocol}://[{$intip6}]:{$webuiport}/\n";
544
				} else {
545
					echo "		{$webuiprotocol}://{$intip6}:{$webuiport}/\n";
546
				}
547
			}
548
		} else {
549
			if ($intip != '') {
550
				echo "		{$webuiprotocol}://{$intip}/\n";
551
			}
552
			if ($intip6 != '') {
553
				if (is_ipaddr($intip6)) {
554
					echo "		{$webuiprotocol}://[{$intip6}]/\n";
555
				} else {
556
					echo "		{$webuiprotocol}://{$intip6}/\n";
557
				}
558
			}
559
		}
560
	}
561
}
562

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

    
565
fgets($fp);
566
fclose($fp);
567

    
568
?>
(45-45/85)