2 # Networking functions for redhat linux
4 if ($gconfig{'os_type'} eq 'openmamba-linux') {
6 $net_scripts_dir = "/etc/sysconfig/network-devices";
7 $devices_dir = "/etc/sysconfig/network-devices";
10 # Redhat, Mandrake, etc..
11 $net_scripts_dir = "/etc/sysconfig/network-scripts";
12 $devices_dir = "/etc/sysconfig/networking/devices";
14 $network_config = "/etc/sysconfig/network";
15 $static_route_config = "/etc/sysconfig/static-routes";
16 $sysctl_config = "/etc/sysctl.conf";
18 # Redhat 7.2+ and Mandrake 9.1+ support separate gateways in each interface file
19 $supports_dev_gateway = ($gconfig{'os_type'} eq 'redhat-linux' &&
20 $gconfig{'os_version'} >= 7.2) ||
21 ($gconfig{'os_type'} eq 'mandrake-linux' &&
22 $gconfig{'os_version'} >= 9.1) ||
23 ($gconfig{'os_type'} eq 'coherant-linux' &&
24 $gconfig{'os_version'} >= 3.0) ||
25 ($gconfig{'os_type'} eq 'trustix-linux');
27 # Redhat 8.0+ and Mandrake 9.1+ have a separate file for static routes for
29 $supports_dev_routes = ($gconfig{'os_type'} eq 'redhat-linux' &&
30 $gconfig{'os_version'} >= 8.0) ||
31 ($gconfig{'os_type'} eq 'mandrake-linux' &&
32 $gconfig{'os_version'} >= 9.1) ||
33 ($gconfig{'os_type'} eq 'coherant-linux' &&
34 $gconfig{'os_version'} >= 3.0) ||
35 ($gconfig{'os_type'} eq 'trustix-linux');
37 # Redhat 10 (ES/AS 3) uses route-$dev instead of $dev.route
38 $supports_route_dev = ($gconfig{'os_type'} eq 'redhat-linux' &&
39 $gconfig{'os_version'} >= 10.0) ||
40 ($gconfig{'os_type'} eq 'coherant-linux' &&
41 $gconfig{'os_version'} >= 3.0);
43 # Redhat 9.0+ uses the ONPARENT variable for virtual interfaces
44 $uses_on_parent = ($gconfig{'os_type'} eq 'redhat-linux' &&
45 $gconfig{'os_version'} >= 9.0) ||
46 ($gconfig{'os_type'} eq 'mandrake-linux' &&
47 $gconfig{'os_version'} >= 9.1) ||
48 ($gconfig{'os_type'} eq 'coherant-linux' &&
49 $gconfig{'os_version'} >= 3.0);
51 # Redhat versions 7.2 and above allow the MTU to be set at boot time
52 $supports_mtu = ($gconfig{'os_type'} eq 'redhat-linux' &&
53 $gconfig{'os_version'} >= 7.2) ||
54 ($gconfig{'os_type'} eq 'coherant-linux' &&
55 $gconfig{'os_version'} >= 3.0);
60 # Returns a list of interfaces brought up at boot time
65 opendir(CONF, &translate_filename($net_scripts_dir));
66 while($f = readdir(CONF)) {
68 if ($f =~ /^ifcfg-([a-z0-9:\.]+)\-range([a-z0-9\.\_]+)$/) {
69 # A range of addresses
70 &read_env_file("$net_scripts_dir/$f", \%conf);
71 $b->{'fullname'} = "$1-range$2";
74 $b->{'start'} = $conf{'IPADDR_START'};
75 $b->{'end'} = $conf{'IPADDR_END'};
76 $b->{'num'} = $conf{'CLONENUM_START'};
79 $b->{'desc'} = $conf{'NAME'};
80 $b->{'index'} = scalar(@rv);
81 $b->{'file'} = "$net_scripts_dir/$f";
84 elsif ($f !~ /\.(bak|old)$/i && $f =~ /^ifcfg-([a-z0-9:\.]+)$/) {
86 &read_env_file("$net_scripts_dir/$f", \%conf);
87 $b->{'fullname'} = $conf{'DEVICE'} || $1;
88 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
92 else { $b->{'name'} = $b->{'fullname'}; }
93 $b->{'up'} = defined($conf{'ONPARENT'}) &&
94 $b->{'virtual'} ne '' ?
95 ($conf{'ONPARENT'} eq 'yes') :
96 ($conf{'ONBOOT'} eq 'yes');
97 $b->{'address'} = $conf{'IPADDR'};
98 $b->{'netmask'} = $conf{'NETMASK'};
99 $b->{'broadcast'} = $conf{'BROADCAST'};
100 if (!$b->{'broadcast'} && $b->{'address'} && $b->{'netmask'}) {
101 $b->{'broadcast'} = &compute_broadcast($b->{'address'},
104 $b->{'gateway'} = $conf{'GATEWAY'};
105 $b->{'gateway6'} = $conf{'IPV6_DEFAULTGW'};
106 $b->{'mtu'} = $conf{'MTU'};
107 $b->{'ether'} = $conf{'MACADDR'};
108 $b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
109 $b->{'bootp'} = ($conf{'BOOTPROTO'} eq 'bootp');
111 push(@ip6s, [ split(/\//, $conf{'IPV6ADDR'}) ])
112 if ($conf{'IPV6ADDR'});
113 push(@ip6s, map { [ split(/\//, $_) ] }
114 split(/\s+/, $conf{'IPV6ADDR_SECONDARIES'}));
116 # Static IPv6 addresses
117 $b->{'address6'} = [ map { $_->[0] } @ip6s ];
118 $b->{'netmask6'} = [ map { $_->[1] } @ip6s ];
120 elsif (lc($conf{'IPV6INIT'}) eq 'yes') {
123 $b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);
124 $b->{'desc'} = $conf{'NAME'};
125 $b->{'index'} = scalar(@rv);
126 $b->{'file'} = "$net_scripts_dir/$f";
127 if ($conf{'BRIDGE'}) {
128 $bridge_map{$conf{'BRIDGE'}} = $b->{'fullname'};
134 foreach my $b (@rv) {
135 if ($b->{'fullname'} =~ /^br\d+$/) {
137 $b->{'bridgeto'} = $bridge_map{$b->{'fullname'}};
143 # save_interface(&details)
144 # Create or update a boot-time interface
148 local $name = $_[0]->{'range'} ne "" ? $_[0]->{'name'}."-range".
150 $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
152 &lock_file("$net_scripts_dir/ifcfg-$name");
153 &read_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
154 if ($_[0]->{'range'} ne "") {
155 # Special case - saving a range
156 $conf{'IPADDR_START'} = $_[0]->{'start'};
157 $conf{'IPADDR_END'} = $_[0]->{'end'};
158 $conf{'CLONENUM_START'} = $_[0]->{'num'};
161 # Saving a normal interface
162 $conf{'DEVICE'} = $name;
163 $conf{'IPADDR'} = $_[0]->{'address'};
164 $conf{'NETMASK'} = $_[0]->{'netmask'};
165 if ($_[0]->{'address'} && $_[0]->{'netmask'}) {
166 $conf{'NETWORK'} = &compute_network($_[0]->{'address'},
170 $conf{'NETWORK'} = '';
172 $conf{'BROADCAST'} = $_[0]->{'broadcast'};
173 if ($_[0]->{'gateway'}) {
174 $conf{'GATEWAY'} = $_[0]->{'gateway'};
177 delete($conf{'GATEWAY'});
179 if ($_[0]->{'gateway6'}) {
180 $conf{'IPV6_DEFAULTGW'} = $_[0]->{'gateway6'};
183 delete($conf{'IPV6_DEFAULTGW'});
185 $conf{'MTU'} = $_[0]->{'mtu'};
186 $conf{'MACADDR'} = $_[0]->{'ether'};
187 $conf{'ONBOOT'} = $_[0]->{'up'} ? "yes" : "no";
188 $conf{'ONPARENT'} = $_[0]->{'up'} ? "yes" : "no"
189 if ($_[0]->{'virtual'} ne '');
190 $conf{'BOOTPROTO'} = $_[0]->{'bootp'} ? "bootp" :
191 $_[0]->{'dhcp'} ? "dhcp" : "none";
192 delete($conf{'IPV6ADDR'});
193 delete($conf{'IPV6ADDR_SECONDARIES'});
195 for(my $i=0; $i<@{$b->{'address6'}}; $i++) {
196 push(@ip6s, $b->{'address6'}->[$i]."/".
197 $b->{'netmask6'}->[$i]);
199 if ((@ip6s || $b->{'auto6'}) && lc($conf{'IPV6INIT'}) ne 'yes') {
200 $conf{'IPV6INIT'} = 'yes';
202 elsif (!@ip6s && !$b->{'auto6'}) {
203 $conf{'IPV6INIT'} = 'no';
206 $conf{'IPV6ADDR'} = shift(@ip6s);
207 $conf{'IPV6ADDR_SECONDARIES'} = join(" ", @ip6s);
209 if ($_[0]->{'fullname'} =~ /^br(\d+)$/) {
210 &has_command("brctl") ||
211 &error("Bridges cannot be created unless the brctl ".
212 "command is installed");
213 $conf{'TYPE'} = 'Bridge';
216 $conf{'NAME'} = $_[0]->{'desc'};
217 &write_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
219 # If this is a bridge, set BRIDGE in real interface
220 if ($_[0]->{'bridge'}) {
221 foreach my $efile (glob("$net_scripts_dir/ifcfg-eth*")) {
224 &read_env_file($efile, \%bconf);
225 if ($bconf{'DEVICE'} eq $_[0]->{'bridgeto'}) {
226 # Correct device for bridge
227 $bconf{'BRIDGE'} = $_[0]->{'fullname'};
228 &write_env_file($efile, \%bconf);
230 elsif ($bconf{'BRIDGE'} eq $_[0]->{'fullname'}) {
231 # Was using this bridge, shouldn't be
232 delete($bconf{'BRIDGE'});
233 &write_env_file($efile, \%bconf);
235 &unlock_file($efile);
239 # Link to devices directory
240 if (-d &translate_filename($devices_dir)) {
241 &link_file("$net_scripts_dir/ifcfg-$name",
242 "$devices_dir/ifcfg-$name");
244 &unlock_file("$net_scripts_dir/ifcfg-$name");
246 # Make sure IPv6 is enabled globally
247 if (@{$b->{'address6'}}) {
249 &lock_file($network_config);
250 &read_env_file($network_config, \%conf);
251 if (lc($conf{'NETWORKING_IPV6'}) ne 'yes') {
252 $conf{'NETWORKING_IPV6'} = 'yes';
253 &write_env_file($network_config, \%conf);
255 &unlock_file($network_config);
259 # delete_interface(&details)
260 # Delete a boot-time interface
263 local $name = $_[0]->{'range'} ne "" ? $_[0]->{'name'}."-range".
265 $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
267 &lock_file("$net_scripts_dir/ifcfg-$name");
268 &unlink_file("$net_scripts_dir/ifcfg-$name");
269 if (-d &translate_filename($devices_dir)) {
270 &unlink_file("$devices_dir/ifcfg-$name");
272 &unlock_file("$net_scripts_dir/ifcfg-$name");
276 # Can some boot-time interface parameter be edited?
283 return $_[0] ne "mtu";
287 # valid_boot_address(address)
288 # Is some address valid for a bootup interface
289 sub valid_boot_address
291 return &check_ipaddress($_[0]);
298 &read_env_file($network_config, \%conf);
299 if ($conf{'HOSTNAME'}) {
300 return $conf{'HOSTNAME'};
302 return &get_system_hostname(1);
305 # save_hostname(name)
308 local $old = &get_hostname();
310 &system_logged("hostname $_[0] >/dev/null 2>&1");
311 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
312 &print_tempfile(HOST, $_[0],"\n");
313 &close_tempfile(HOST);
314 &lock_file($network_config);
315 &read_env_file($network_config, \%conf);
316 $conf{'HOSTNAME'} = $_[0];
317 &write_env_file($network_config, \%conf);
318 &unlock_file($network_config);
320 # If any ifcfg-XXX files have the old hostname in DHCP_HOSTNAME, fix it
321 foreach my $b (&boot_interfaces()) {
323 &read_env_file($b->{'file'}, \%ifc);
324 if ($ifc{'DHCP_HOSTNAME'} eq $old) {
325 $ifc{'DHCP_HOSTNAME'} = $_[0];
326 &lock_file($b->{'file'});
327 &write_env_file($b->{'file'}, \%ifc);
328 &unlock_file($b->{'file'});
331 undef(@main::get_system_hostname); # clear cache
338 &execute_command("domainname", undef, \$d, undef);
343 # save_domainname(domain)
347 &execute_command("domainname ".quotemeta($_[0]));
348 &read_env_file($network_config, \%conf);
350 $conf{'NISDOMAIN'} = $_[0];
353 delete($conf{'NISDOMAIN'});
355 &write_env_file($network_config, \%conf);
358 sub routing_config_files
360 local @rv = ( $network_config, $sysctl_config );
361 if (!$supports_dev_routes) {
362 push(@rv, $static_route_config);
365 foreach my $dir ($devices_dir, $net_scripts_dir) {
366 opendir(DIR, &translate_filename($dir));
367 while(my $f = readdir(DIR)) {
368 if ($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
369 $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) {
370 push(@rv, "$dir/$f");
379 sub network_config_files
381 return ( "/etc/HOSTNAME", $network_config );
386 local (%conf, @st, @hr, %sysctl);
387 &read_env_file($network_config, \%conf);
388 if (!$supports_dev_gateway) {
389 # show default router and device
390 print &ui_table_row($text{'routes_default'},
391 &ui_opt_textbox("gateway", $conf{'GATEWAY'}, 15,
392 $text{'routes_none'}));
394 print &ui_table_row($text{'routes_device2'},
395 &ui_opt_textbox("gatewaydev", $conf{'GATEWAYDEV'}, 6,
396 $text{'routes_none'}));
399 # multiple default routers can exist, one per interface
402 if ($conf{'GATEWAY'} || $conf{'IPV6_DEFAULTGW'}) {
404 &interface_sel("gatewaydev$r",
405 $conf{'GATEWAYDEV'} ||
406 $conf{'IPV6_DEFAULTDEV'} || "*"),
407 &ui_textbox("gateway$r", $conf{'GATEWAY'}, 15),
408 &ui_textbox("gateway6$r", $conf{'IPV6_DEFAULTGW'}, 30),
412 local @boot = &boot_interfaces();
413 foreach $b (grep { $_->{'gateway'} && $_->{'virtual'} eq '' } @boot) {
414 push(@table, [ &interface_sel("gatewaydev$r", $b->{'name'}),
415 &ui_textbox("gateway$r", $b->{'gateway'}, 15),
416 &ui_textbox("gateway6$r", $b->{'gateway6'}, 30),
420 push(@table, [ &interface_sel("gatewaydev$r"),
421 &ui_textbox("gateway$r", undef, 15),
422 &ui_textbox("gateway6$r", undef, 30), ]);
423 print &ui_table_row($text{'routes_default2'},
425 [ $text{'routes_ifc'}, $text{'routes_gateway'},
426 $text{'routes_gateway6'} ],
427 undef, \@table, undef, 1));
431 if ($gconfig{'os_version'} < 7.0) {
432 print &ui_table_row($text{'routes_forward'},
433 &ui_yesno_radio("forward",
434 $conf{'FORWARD_IPV4'} eq "yes" ? 1 : 0));
437 &read_env_file($sysctl_config, \%sysctl);
438 print &ui_table_row($text{'routes_forward'},
439 &ui_yesno_radio("forward",
440 $sysctl{'net.ipv4.ip_forward'} ? 1 : 0));
443 if (!$supports_dev_routes) {
444 # get static routes from single file
445 &open_readfile(STATIC, $static_route_config);
447 if (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)\s+gw\s+(\S+)/) {
448 push(@st, [ $1, $2, $3, $4 ]);
450 elsif (/(\S+)\s+host\s+(\S+)\s+gw\s+(\S+)/) {
451 push(@st, [ $1, $2, '255.255.255.255', $3 ]);
453 elsif (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)/) {
454 push(@hr, [ $1, $2, $3 ]);
456 elsif (/(\S+)\s+host\s+(\S+)/) {
457 push(@hr, [ $1, $2, '255.255.255.255' ]);
463 # get static routes from per-interface files
465 opendir(DIR, &translate_filename($devices_dir));
466 while($f = readdir(DIR)) {
467 if ($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
468 $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) {
471 &read_env_file("$devices_dir/$f", \%rfile);
472 for($i=0; defined($rfile{"ADDRESS$i"}); $i++) {
473 if ($rfile{"GATEWAY$i"}) {
474 push(@st, [ $dev, $rfile{"ADDRESS$i"},
476 $rfile{"GATEWAY$i"}]);
479 push(@hr, [ $dev, $rfile{"ADDRESS$i"},
480 $rfile{"NETMASK$i"} ||
481 "255.255.255.255" ]);
489 # Show static network routes
491 for($i=0; $i<=@st; $i++) {
493 push(@table, [ &ui_textbox("dev_$i", $st->[0], 6),
494 &ui_textbox("net_$i", $st->[1], 15),
495 &ui_textbox("netmask_$i", $st->[2], 15),
496 &ui_textbox("gw_$i", $st->[3], 15) ]);
498 print &ui_table_row($text{'routes_static'},
499 &ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
500 $text{'routes_mask'}, $text{'routes_gateway'} ],
501 undef, \@table, undef, 1));
503 # Show static host routes
505 for($i=0; $i<=@hr; $i++) {
507 push(@table, [ &ui_textbox("ldev_$i", $st->[0], 6),
508 &ui_textbox("lnet_$i", $st->[1], 15),
509 &ui_textbox("lnetmask_$i", $st->[2], 15) ]);
511 print &ui_table_row($text{'routes_local'},
512 &ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
513 $text{'routes_mask'} ],
514 undef, \@table, undef, 1));
519 local (%conf, @st, %sysctl, %st, @boot);
520 &lock_file($network_config);
521 &read_env_file($network_config, \%conf);
522 if (!$supports_dev_gateway) {
523 # Just update a single file
524 if ($in{'gateway_def'}) { delete($conf{'GATEWAY'}); }
525 elsif (!&to_ipaddress($in{'gateway'})) {
526 &error(&text('routes_edefault', $in{'gateway'}));
528 else { $conf{'GATEWAY'} = $in{'gateway'}; }
530 if ($in{'gatewaydev_def'}) { delete($conf{'GATEWAYDEV'}); }
531 elsif ($in{'gatewaydev'} !~ /^\S+$/) {
532 &error(&text('routes_edevice', $in{'gatewaydev'}));
534 else { $conf{'GATEWAYDEV'} = $in{'gatewaydev'}; }
537 # Multiple defaults can be specified!
539 @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
541 delete($b->{'gateway'});
543 delete($conf{'GATEWAY'});
544 delete($conf{'GATEWAYDEV'});
545 delete($conf{'IPV6_DEFAULTDEV'});
546 delete($conf{'IPV6_DEFAULTGW'});
548 for($r=0; defined($in{"gatewaydev$r"}); $r++) {
549 next if (!$in{"gatewaydev$r"});
550 &check_ipaddress($in{"gateway$r"}) ||
551 &error(&text('routes_edefault2', $r+1));
552 if ($in{"gatewaydev$r"} eq "*") {
554 $conf{'GATEWAY'} && &error(&text('routes_eclash'));
555 $conf{'GATEWAY'} = $in{"gateway$r"};
556 $conf{'IPV6_DEFAULTGW'} &&
557 &error(&text('routes_eclash6'));
558 $conf{'IPV6_DEFAULTGW'} = $in{"gateway6$r"};
561 # For a specific interface
562 local ($b) = grep { $_->{'fullname'} eq
563 $in{"gatewaydev$r"} } @boot;
564 $b->{'gateway'} && &error(&text('routes_eclash2',
565 $in{"gatewaydev$r"}));
566 $b->{'gateway'} = $in{"gateway$r"};
567 $b->{'gateway6'} = $in{"gateway6$r"};
572 if ($gconfig{'os_version'} < 7.0) {
573 if ($in{'forward'}) { $conf{'FORWARD_IPV4'} = 'yes'; }
574 else { $conf{'FORWARD_IPV4'} = 'no'; }
577 &lock_file($sysctl_config);
578 &read_env_file($sysctl_config, \%sysctl);
579 $sysctl{'net.ipv4.ip_forward'} = $in{'forward'};
582 # Parse static and local routes
583 for($i=0; defined($dev = $in{"dev_$i"}); $i++) {
585 $net = $in{"net_$i"}; $netmask = $in{"netmask_$i"}; $gw = $in{"gw_$i"};
586 $dev =~ /^\S+$/ || &error(&text('routes_edevice', $dev));
587 &to_ipaddress($net) || &error(&text('routes_enet', $net));
588 &check_ipaddress($netmask) || &error(&text('routes_emask', $netmask));
589 &to_ipaddress($gw) || &error(&text('routes_egateway', $gw));
590 if ($netmask eq "255.255.255.255") {
591 push(@st, "$dev host $net gw $gw\n");
594 push(@st, "$dev net $net netmask $netmask gw $gw\n");
596 push(@{$st{$dev}}, [ $net, $netmask, $gw ]);
598 for($i=0; defined($dev = $in{"ldev_$i"}); $i++) {
599 $net = $in{"lnet_$i"}; $netmask = $in{"lnetmask_$i"};
600 next if (!$dev && !$net);
601 $dev =~ /^\S+$/ || &error(&text('routes_edevice', $dev));
602 &to_ipaddress($net) ||
603 $net =~ /^(\S+)\/(\d+)$/ && &to_ipaddress("$1") ||
604 &error(&text('routes_enet', $net));
605 &check_ipaddress($netmask) || &error(&text('routes_emask', $netmask));
606 if ($netmask eq "255.255.255.255") {
607 push(@st, "$dev host $net\n");
610 push(@st, "$dev net $net netmask $netmask\n");
612 push(@{$st{$dev}}, [ $net, $netmask ]);
614 if (!$supports_dev_routes) {
615 # Write to a single file
616 &open_lock_tempfile(STATIC, ">$static_route_config");
617 &print_tempfile(STATIC, @st);
618 &close_tempfile(STATIC);
621 # Write to one file per interface (delete old, then save new/updated)
623 opendir(DIR, &translate_filename($devices_dir));
624 while($f = readdir(DIR)) {
625 if (($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
626 $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) && !$st{$1}) {
627 &unlink_logged("$devices_dir/$f");
628 &unlink_logged("$net_scripts_dir/$f");
632 foreach $dev (keys %st) {
633 $f = $supports_route_dev ? "route-$dev" : "$dev.route";
635 for($i=0; $i<@{$st{$dev}}; $i++) {
636 $rfile{"ADDRESS$i"} = $st{$dev}->[$i]->[0];
637 $rfile{"NETMASK$i"} = $st{$dev}->[$i]->[1];
638 $rfile{"GATEWAY$i"} = $st{$dev}->[$i]->[2];
640 &lock_file("$devices_dir/$f");
641 &write_env_file("$devices_dir/$f", \%rfile);
642 &unlock_file("$devices_dir/$f");
643 &lock_file("$net_scripts_dir/$f");
644 &link_file("$devices_dir/$f", "$net_scripts_dir/$f");
645 &unlock_file("$net_scripts_dir/$f");
648 &write_env_file($network_config, \%conf);
649 &unlock_file($network_config);
651 &write_env_file($sysctl_config, \%sysctl);
652 &unlock_file($sysctl_config);
662 sub os_feedback_files
664 opendir(DIR, $net_scripts_dir);
665 local @f = readdir(DIR);
667 return ( (map { "$net_scripts_dir/$_" } grep { /^ifcfg-/ } @f),
668 $network_config, $static_route_config, "/etc/resolv.conf",
669 "/etc/nsswitch.conf", "/etc/HOSTNAME" );
672 # interface_sel(name, value)
673 # Returns a menu for all boot-time interfaces
676 local $rv = "<select name=$_[0]>\n";
677 $rv .= sprintf "<option value='' %s> \n",
678 $_[1] eq "" ? "selected" : "";
679 $rv .= sprintf "<option value='*' %s>%s\n",
680 $_[1] eq "*" ? "selected" : "", $text{'routes_any'};
681 @boot_interfaces_cache = &boot_interfaces() if (!@boot_interfaces_cache);
682 foreach $b (@boot_interfaces_cache) {
683 next if ($b->{'virtual'} ne '');
684 $rv .= sprintf "<option value=%s %s>%s\n",
685 $b->{'name'}, $b->{'name'} eq $_[1] ? "selected" : "", $b->{'name'};
687 $rv .= "</select>\n";
692 # Apply the interface and routing settings
695 &system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) >/dev/null 2>&1");
698 # apply_interface(&iface)
699 # Calls an OS-specific function to make a boot-time interface active
702 local $out = &backquote_logged("cd / ; ifdown '$_[0]->{'fullname'}' >/dev/null 2>&1 </dev/null ; ifup '$_[0]->{'fullname'}' 2>&1 </dev/null");
703 return $? || $out =~ /error/i ? $out : undef;
706 # unapply_interface(&iface)
707 # Calls an OS-specific function to make a boot-time interface inactive
708 #sub unapply_interface
710 #local $out = &backquote_logged("cd / ; ifdown '$_[0]->{'fullname'}' 2>&1 </dev/null");
711 #return $? ? $out : undef;
714 # get_default_gateway()
715 # Returns the default gateway IP (if one is set) and device (if set) boot time
717 sub get_default_gateway
720 &read_env_file($network_config, \%conf);
721 local @boot = &boot_interfaces();
722 local ($gifc) = grep { $_->{'gateway'} && $_->{'virtual'} eq '' } @boot;
723 return ( $gifc->{'gateway'}, $gifc->{'fullname'} ) if ($gifc);
724 return $conf{'GATEWAY'} ? ( $conf{'GATEWAY'}, $conf{'GATEWAYDEV'} ) : ( );
727 # set_default_gateway(gateway, device)
728 # Sets the default gateway to the given IP accessible via the given device,
729 # in the boot time settings.
730 sub set_default_gateway
732 &lock_file($network_config);
733 &read_env_file($network_config, \%conf);
734 if (!$supports_dev_gateway) {
735 # Just update the network config file
738 $conf{'GATEWAY'} = $_[0];
739 $conf{'GATEWAYDEV'} = $_[1];
742 delete($conf{'GATEWAY'});
743 delete($conf{'GATEWAYDEV'});
747 # Set the gateway in the specified interface file, and clear the rest
748 local @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
750 delete($b->{'gateway'});
751 if ($_[0] && $b->{'fullname'} eq $_[1]) {
752 $b->{'gateway'} = $_[0];
756 delete($conf{'GATEWAY'});
757 delete($conf{'GATEWAYDEV'});
759 &write_env_file($network_config, \%conf);
760 &unlock_file($network_config);
763 # get_default_ipv6_gateway()
764 # Returns the default gateway IPv6 address (if one is set) and device (if set)
765 # boot time settings.
766 sub get_default_ipv6_gateway
769 &read_env_file($network_config, \%conf);
770 local @boot = &boot_interfaces();
771 local ($gifc) = grep { $_->{'gateway6'} && $_->{'virtual'} eq '' } @boot;
772 return ( $gifc->{'gateway6'}, $gifc->{'fullname'} ) if ($gifc);
773 return $conf{'IPV6_DEFAULTGW'} ? ( $conf{'IPV6_DEFAULTGW'},
774 $conf{'IPV6_DEFAULTDEV'} ) : ( );
777 # set_default_ipv6_gateway(gateway, device)
778 # Sets the default gateway to the given IPv6 address accessible via the given
779 # device, in the boot time settings.
780 sub set_default_ipv6_gateway
782 &lock_file($network_config);
783 &read_env_file($network_config, \%conf);
784 local @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
786 delete($b->{'gateway6'});
787 if ($_[0] && $b->{'fullname'} eq $_[1]) {
788 $b->{'gateway6'} = $_[0];
792 delete($conf{'IPV6_DEFAULTGW'});
793 delete($conf{'IPV6_DEFAULTDEV'});
794 &write_env_file($network_config, \%conf);
795 &unlock_file($network_config);
799 # Returns 1 for newer redhat versions
802 return ($gconfig{'os_type'} eq 'redhat-linux' &&
803 $gconfig{'os_version'} >= 7.3) ||
804 ($gconfig{'os_type'} eq 'mandrake-linux' &&
805 $gconfig{'os_version'} >= 8.0) ||
806 ($gconfig{'os_type'} eq 'coherant-linux' &&
807 $gconfig{'os_version'} >= 3.0);
810 # range_input([&interface])
811 # Print HTML for a IP range interface
817 print &ui_table_row($text{'ifcs_desc'},
818 &ui_textbox("desc", $_[0] ? $_[0]->{'desc'} : undef, 60));
823 $ifaceinput = &ui_select("iface", $_[0]->{'name'},
824 [ map { $_->{'fullname'} } grep { $b->{'virtual'} eq '' }
825 &boot_interfaces() ]);
828 $ifaceinput = "<tt>$_[0]->{'name'}</tt>";
830 print &ui_table_row($text{'range_iface'}, $ifaceinput);
832 # Name for this range
833 print &ui_table_row($text{'range_name'},
834 $new ? &ui_textbox("range", undef, 10)
835 : "<tt>$_[0]->{'range'}</tt>");
838 print &ui_table_row($text{'range_start'},
839 &ui_textbox("start", $_[0]->{'start'}, 15));
842 print &ui_table_row($text{'range_end'},
843 &ui_textbox("end", $_[0]->{'end'}, 15));
846 print &ui_table_row($text{'range_num'},
847 &ui_textbox("num", $_[0]->{'num'}, 5));
850 # parse_range(&range, &in)
853 local %in = %{$_[1]};
855 $_[0]->{'name'} = $in{'iface'};
856 $in{'range'} =~ /^[a-z0-9\.\_]+$/ || &error($text{'range_ename'});
857 $_[0]->{'range'} = $in{'range'};
858 $_[0]->{'fullname'} = $in{'iface'}."-range".$in{'range'};
860 $_[0]->{'desc'} = $in{'desc'};
862 &check_ipaddress($in{'start'}) || &error($text{'range_estart'});
863 $_[0]->{'start'} = $in{'start'};
865 &check_ipaddress($in{'end'}) || &error($text{'range_eend'});
866 $_[0]->{'end'} = $in{'end'};
868 local @sip = split(/\./, $in{'start'});
869 local @eip = split(/\./, $in{'end'});
870 $sip[0] == $eip[0] && $sip[1] == $eip[1] && $sip[2] == $eip[2] ||
871 &error($text{'range_eclass'});
872 $sip[3] <= $eip[3] || &error($text{'range_ebefore'});
874 $in{'num'} =~ /^\d+$/ || &error($text{'range_enum'});
875 $_[0]->{'num'} = $in{'num'};
878 # get_dhcp_hostname()
879 # Returns 0 if the hostname is not set by DHCP, 1 if it is, or -1 if this
880 # feature is not supported on this OS.
881 sub get_dhcp_hostname
883 return -1 if ($gconfig{'os_type'} ne 'redhat-linux' ||
884 $gconfig{'os_version'} < 11);
885 local @boot = &boot_interfaces();
886 local ($eth) = grep { $_->{'fullname'} =~ /^eth\d+$/ } @boot;
887 return -1 if (!$eth);
889 &read_env_file($eth->{'file'}, \%eth);
890 return $eth{'DHCP_HOSTNAME'} ne &get_system_hostname();
893 # save_dhcp_hostname(set)
894 # If called with a parameter of 0, the hostname is fixed and not set by
895 # DHCP. If called with 1, the hostname is chosen by DHCP.
896 sub save_dhcp_hostname
900 sub boot_iface_hardware
902 return $_[0] =~ /^eth/;
905 # supports_address6([&iface])
906 # Returns 1 if managing IPv6 interfaces is supported
907 sub supports_address6
910 return !$iface || $iface->{'virtual'} eq '';
913 # Returns 1, as boot-time interfaces on Redhat can exist without an IP (such as
915 sub supports_no_address
920 # Bridge interfaces can be created on redhat