Project

General

Profile

Download (16.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-2016 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 %s server on %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(false, 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) {
179
	global $g, $config;
180
	$new_name = "GW_" . strtoupper($interface);
181

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

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

    
241
	return $new_name;
242
}
243

    
244
function console_configure_ip_address($version) {
245
	global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;
246

    
247
	$label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
248
	$maxbits    = ($version === 6) ? 127      : 31;
249
	$label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";
250

    
251
	$upperifname = strtoupper($interface);
252

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

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

    
302
					if ($version === 4 && $intbits < $maxbits) {
303
						if ($intip == gen_subnet($intip, $intbits)) {
304
							echo gettext("You cannot set network address to an interface");
305
							continue 2;
306
							$intbits_ok = false;
307
						} else if ($intip == gen_subnet_max($intip, $intbits)) {
308
							echo gettext("You cannot set broadcast address to an interface");
309
							continue 2;
310
							$intbits_ok = false;
311
						}
312
					}
313
				} while (!$intbits_ok);
314

    
315
				if ($version === 6) {
316
					$subnet = gen_subnetv6($intip, $intbits);
317
				} else {
318
					$subnet = gen_subnet($intip, $intbits);
319
				}
320
				do {
321
					echo "\n" . sprintf(gettext("For a WAN, enter the new %s %s upstream gateway address."), $upperifname, $label_IPvX) . "\n" .
322
								gettext("For a LAN, press <ENTER> for none:") . "\n> ";
323
					$gwip = chop(fgets($fp));
324
					$is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
325
					$is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
326
					if ($gwip != '') {
327
						if (!$is_ipaddr) {
328
							echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
329
						} else if (!$is_in_subnet) {
330
							echo gettext("not in subnet!") . "\n";
331
						}
332
					}
333
				} while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));
334

    
335
				if ($gwip != '') {
336
					$inet_type = ($version === 6) ? "inet6" : "inet";
337
					$gwname = add_gateway_to_config($interface, $gwip, $inet_type);
338
				}
339
			}
340
			$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
341
			if (!empty($ifppp)) {
342
				$ifaceassigned = $ifppp;
343
			}
344
			break;
345
		}
346
	}
347

    
348
	return array($intip, $intbits, $gwname);
349
}
350

    
351
list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
352
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
353

    
354
if (!empty($ifaceassigned)) {
355
	$config['interfaces'][$interface]['if'] = $ifaceassigned;
356
}
357
$config['interfaces'][$interface]['ipaddr']    = $intip;
358
$config['interfaces'][$interface]['subnet']    = $intbits;
359
$config['interfaces'][$interface]['gateway']   = $gwname;
360
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
361
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
362
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
363
$config['interfaces'][$interface]['enable']    = true;
364

    
365
function console_configure_dhcpd($version = 4) {
366
	global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
367

    
368
	$label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
369
	$dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";
370

    
371
	if ($g['services_dhcp_server_enable'] && prompt_for_enable_dhcp_server($version)) {
372
		$subnet_start = ($version === 6) ? gen_subnetv6($intip6, $intbits6) : gen_subnet($intip, $intbits);
373
		$subnet_end = ($version === 6) ? gen_subnetv6_max($intip6, $intbits6) : gen_subnet_max($intip, $intbits);
374
		do {
375
			do {
376
				echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
377
				$dhcpstartip = chop(fgets($fp));
378
				if ($dhcpstartip === "") {
379
					fclose($fp);
380
					return 0;
381
				}
382
				$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
383
				$is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
384
				if (!$is_inrange) {
385
					echo gettext("This IP address must be in the interface's subnet") . "\n";
386
				}
387
			} while (!$is_ipaddr || !$is_inrange);
388

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

    
421
if (console_configure_dhcpd(4) == 0) {
422
	return 0;
423
}
424
if (console_configure_dhcpd(6) == 0) {
425
	return 0;
426
}
427

    
428
//*****************************************************************************
429

    
430
if ($config['system']['webgui']['protocol'] == "https") {
431

    
432
	if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
433
		$config['system']['webgui']['protocol'] = "http";
434
		$restart_webgui = true;
435
	}
436
}
437

    
438
if (isset($config['system']['webgui']['noantilockout'])) {
439
	echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
440
	unset($config['system']['webgui']['noantilockout']);
441
}
442

    
443
if ($config['interfaces']['lan']) {
444
	if ($config['dhcpd']) {
445
		if ($config['dhcpd']['wan']) {
446
			unset($config['dhcpd']['wan']);
447
		}
448
	}
449
	if ($config['dhcpdv6']) {
450
		if ($config['dhcpdv6']['wan']) {
451
			unset($config['dhcpdv6']['wan']);
452
		}
453
	}
454
}
455

    
456
if (!$config['interfaces']['lan']) {
457
	unset($config['interfaces']['lan']);
458
	if ($config['dhcpd']['lan']) {
459
		unset($config['dhcpd']['lan']);
460
	}
461
	if ($config['dhcpdv6']['lan']) {
462
		unset($config['dhcpdv6']['lan']);
463
	}
464
	unset($config['shaper']);
465
	unset($config['ezshaper']);
466
	unset($config['nat']);
467
	if (!$dry_run) {
468
		system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
469
		$restart_dhcpd = true;
470
	}
471
}
472

    
473
$upperifname = strtoupper($interface);
474
if (!$dry_run) {
475
	echo "\nPlease wait while the changes are saved to {$upperifname}...";
476
	write_config(sprintf(gettext("%s IP configuration from console menu"), $interface));
477
	interface_reconfigure(strtolower($upperifname));
478
	echo "\n Reloading filter...";
479
	filter_configure_sync();
480
	echo "\n Reloading routing configuration...";
481
	system_routing_configure();
482
	if ($restart_dhcpd) {
483
		echo "\n DHCPD...";
484
		services_dhcpd_configure();
485
	}
486
	if ($restart_webgui) {
487
		echo "\n Restarting webConfigurator... ";
488
		mwexec("/etc/rc.restart_webgui");
489
	}
490
}
491

    
492
if ($intip != '') {
493
	if (is_ipaddr($intip)) {
494
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
495
							  $upperifname, "{$intip}/{$intbits}") . "\n";
496
	} else {
497
		echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
498
							  $upperifname, $intip) . "\n";
499
	}
500
}
501
if ($intip6 != '') {
502
	if (is_ipaddr($intip6)) {
503
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
504
							  $upperifname, "${intip6}/${intbits6}") . "\n";
505
	} else {
506
		echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
507
							  $upperifname, $intip6) . "\n";
508
	}
509
}
510

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

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

    
547
fgets($fp);
548
fclose($fp);
549

    
550
?>
(43-43/78)