Project

General

Profile

Download (17.4 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
	foreach (config_get_path('ppps/ppp', []) as $pppid => $ppp) {
76
		if ($realif == $ppp['if']) {
77
			$ifaces = explode(",", $ppp['ports']);
78
			return $ifaces[0];
79
		}
80
	}
81

    
82
	return "";
83
}
84

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

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

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

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

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

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

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

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

    
177
$ifaceassigned = "";
178

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

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

    
188
	$new_name = strtoupper($interface) . $name_suffix;
189

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

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

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

    
254
	return $new_name;
255
}
256

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

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

    
264
	$upperifname = strtoupper($interface);
265

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

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

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

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

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

    
370
	return array($intip, $intbits, $gwname);
371
}
372

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

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

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

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

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

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

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

    
459
//*****************************************************************************
460

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

    
468
if (config_path_enabled('system/webgui', 'noantilockout')) {
469
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
470
	config_del_path('system/webgui/noantilockout');
471
}
472

    
473
if (config_get_path('interfaces/lan')) {
474
	$paths = ['dhcpd/wan', 'dhcpdv6/wan'];
475
	foreach ($paths as $path) {
476
		if (config_get_path($path)) {
477
			config_del_path($path);
478
		}
479
    	}
480
}
481

    
482
if (!config_get_path('interfaces/lan')) {
483
	$paths = ['interfaces/lan', 'dhcpd/lan', 'dhcpdv6/lan',
484
			'shaper', 'ezshaper', 'nat'];
485
    	foreach ($paths as $path) {
486
		if (config_get_path($path)) {
487
			config_del_path($path);
488
		}
489
    	}
490
    	if (!$dry_run) {
491
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
492
		$restart_dhcpd = true;
493
    	}
494
}
495

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

    
519
if ($intip != '') {
520
	if (is_ipaddr($intip)) {
521
		$intipstr = "{$intip}/{$intbits}";
522
	} else {
523
		$intipstr = $intip;
524
	}
525
	echo "\n\n" . sprintf(gettext('The IPv4 %1$s address has been set to %2$s'), $upperifname, $intipstr) . "\n";
526
}
527
if ($intip6 != '') {
528
	if (is_ipaddr($intip6)) {
529
		$intip6str = "${intip6}/${intbits6}";
530
	} else {
531
		$intip6str = $intip6;
532
	}
533
	echo "\n\n" . sprintf(gettext('The IPv6 %1$s address has been set to %2$s'), $upperifname, $intip6str) . "\n";
534
}
535

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

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

    
572
fgets($fp);
573
fclose($fp);
574

    
575
?>
(45-45/85)