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-2020 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
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
$options = getopt("hn", array("dry-run", "help"));
28

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

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

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

    
48
function console_prompt_for_yn ($prompt_text) {
49
	global $fp;
50

    
51
	$good_answer = false;
52

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

    
66
	return $boolean_answer;
67
}
68

    
69
function console_get_interface_from_ppp($realif) {
70
	global $config;
71

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

    
81
	return "";
82
}
83

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

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

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

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

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

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

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

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

    
176
$ifaceassigned = "";
177

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

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

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

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

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

    
241
	//set the new GW as the default if there isnt one set yet
242
	if ($item['ipprotocol'] == "inet" && empty(isset($config['gateways']['defaultgw4']))) {
243
		$config['gateways']['defaultgw4'] = $new_name;
244
	}
245
	if ($item['ipprotocol'] == "inet6" && empty(isset($config['gateways']['defaultgw6']))) {
246
		$config['gateways']['defaultgw6'] = $new_name;
247
	}
248

    
249
	return $new_name;
250
}
251

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

    
255
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
256
	$maxbits    = ($version === 6) ? 127      : 31;
257
	$label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";
258

    
259
	$upperifname = strtoupper($interface);
260

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

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

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

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

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

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

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

    
369
if (!empty($ifaceassigned)) {
370
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
371
}
372
$config['interfaces'][$interface]['ipaddr']    = $intip;
373
$config['interfaces'][$interface]['subnet']    = $intbits;
374
$config['interfaces'][$interface]['gateway']   = $gwname;
375
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
376
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
377
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
378
$config['interfaces'][$interface]['enable']    = true;
379

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

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

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

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

    
437
if (console_configure_dhcpd(4) == 0) {
438
	return 0;
439
}
440
if (console_configure_dhcpd(6) == 0) {
441
	return 0;
442
}
443

    
444
//*****************************************************************************
445

    
446
if ($config['system']['webgui']['protocol'] == "https") {
447

    
448
	if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
449
		$config['system']['webgui']['protocol'] = "http";
450
		$restart_webgui = true;
451
	}
452
}
453

    
454
if (isset($config['system']['webgui']['noantilockout'])) {
455
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
456
	unset($config['system']['webgui']['noantilockout']);
457
}
458

    
459
if ($config['interfaces']['lan']) {
460
	if ($config['dhcpd']) {
461
		if ($config['dhcpd']['wan']) {
462
			unset($config['dhcpd']['wan']);
463
		}
464
	}
465
	if ($config['dhcpdv6']) {
466
		if ($config['dhcpdv6']['wan']) {
467
			unset($config['dhcpdv6']['wan']);
468
		}
469
	}
470
}
471

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

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

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

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

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

    
561
fgets($fp);
562
fclose($fp);
563

    
564
?>
(45-45/83)