<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># redhat-linux-lib.pl
# Networking functions for redhat linux

if ($gconfig{'os_type'} eq 'openmamba-linux') {
	# OpenMamba Linux
	$net_scripts_dir = "/etc/sysconfig/network-devices";
	$devices_dir = "/etc/sysconfig/network-devices";
	}
else {
	# Redhat, Mandrake, etc..
	$net_scripts_dir = "/etc/sysconfig/network-scripts";
	$devices_dir = "/etc/sysconfig/networking/devices";
	}
$route_files_dir = -d $devices_dir ? $devices_dir : $net_scripts_dir;
$network_config = "/etc/sysconfig/network";
$static_route_config = "/etc/sysconfig/static-routes";
$sysctl_config = "/etc/sysctl.conf";

# Redhat 7.2+ and Mandrake 9.1+ support separate gateways in each interface file
$supports_dev_gateway = ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
			 $gconfig{'os_version'} &gt;= 7.2) ||
			($gconfig{'os_type'} eq 'mandrake-linux' &amp;&amp;
			 $gconfig{'os_version'} &gt;= 9.1) ||
			($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
			 $gconfig{'os_version'} &gt;= 3.0) ||
			($gconfig{'os_type'} eq 'trustix-linux');

# Redhat 8.0+ and Mandrake 9.1+ have a separate file for static routes for
# each interface
$supports_dev_routes = ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
		        $gconfig{'os_version'} &gt;= 8.0) ||
			($gconfig{'os_type'} eq 'mandrake-linux' &amp;&amp;
			 $gconfig{'os_version'} &gt;= 9.1) ||
			($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
			 $gconfig{'os_version'} &gt;= 3.0) ||
			($gconfig{'os_type'} eq 'trustix-linux');

# Redhat 10 (ES/AS 3) uses route-$dev instead of $dev.route
$supports_route_dev = ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
		       $gconfig{'os_version'} &gt;= 10.0) ||
		      ($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
		       $gconfig{'os_version'} &gt;= 3.0);

# Redhat 9.0+ uses the ONPARENT variable for virtual interfaces
$uses_on_parent = ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
		   $gconfig{'os_version'} &gt;= 9.0) ||
		  ($gconfig{'os_type'} eq 'mandrake-linux' &amp;&amp;
		   $gconfig{'os_version'} &gt;= 9.1) ||
		  ($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
		   $gconfig{'os_version'} &gt;= 3.0);

# Redhat versions 7.2 and above allow the MTU to be set at boot time
$supports_mtu = ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
		 $gconfig{'os_version'} &gt;= 7.2) ||
		($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
 		 $gconfig{'os_version'} &gt;= 3.0);

do 'linux-lib.pl';

# boot_interfaces()
# Returns a list of interfaces brought up at boot time
sub boot_interfaces
{
local (@rv, $f);
local %bridge_map;
local @active;
opendir(CONF, &amp;translate_filename($net_scripts_dir));
while($f = readdir(CONF)) {
	local (%conf, $b);
	if ($f =~ /^ifcfg-([a-z0-9:\.]+)\-range([a-z0-9\.\_]+)$/) {
		# A range of addresses
		&amp;read_env_file("$net_scripts_dir/$f", \%conf);
		$b-&gt;{'fullname'} = "$1-range$2";
		$b-&gt;{'name'} = $1;
		$b-&gt;{'range'} = $2;
		$b-&gt;{'start'} = $conf{'IPADDR_START'};
		$b-&gt;{'end'} = $conf{'IPADDR_END'};
		$b-&gt;{'num'} = $conf{'CLONENUM_START'};
		$b-&gt;{'up'} = 1;
		$b-&gt;{'edit'} = 1;
		$b-&gt;{'desc'} = $conf{'NAME'};
		$b-&gt;{'index'} = scalar(@rv);
		$b-&gt;{'file'} = "$net_scripts_dir/$f";
		push(@rv, $b);
		}
	elsif ($f !~ /\.(bak|old)$/i &amp;&amp; $f =~ /^ifcfg-([a-zA-Z_0-9:\.]+)$/) {
		# Normal interface
		my $fname = $1;
		&amp;read_env_file("$net_scripts_dir/$f", \%conf);
		if ($conf{'DEVICE'}) {
			# Device is set in the file
			$b-&gt;{'fullname'} = $conf{'DEVICE'};
			}
		elsif (&amp;iface_type($fname) ne $text{'ifcs_unknown'}) {
			# Filename looks like a regular device
			$b-&gt;{'fullname'} = $fname;
			}
		elsif ($conf{'HWADDR'}) {
			# Filename is something odd, like Auto_Ethernet .. so
			# lookup real device by MAC
			if (!@active) {
				@active = &amp;active_interfaces(1);
				}
			my ($a) = grep { lc($_-&gt;{'ether'}) eq
					 lc($conf{'HWADDR'}) &amp;&amp;
					 $_-&gt;{'name'} !~ /^br/ } @active;
			next if (!$a);
			$b-&gt;{'fullname'} = $a-&gt;{'fullname'};
			# XXX virtuals?
			}
		else {
			# No idea what to do here, probably isn't even an
			# interface file
			next;
			}
		if ($b-&gt;{'fullname'} =~ /(\S+):(\d+)/) {
			$b-&gt;{'name'} = $1;
			$b-&gt;{'virtual'} = $2;
			}
		else {
			$b-&gt;{'name'} = $b-&gt;{'fullname'};
			}
		if ($b-&gt;{'fullname'} =~ /(\S+)\.(\d+)/) {
			my ($k, $v) = split(/\./, $b-&gt;{'fullname'});
			$b-&gt;{'physical'} = $k;
			$b-&gt;{'vlanid'} = $v;
			$b-&gt;{'vlan'} = 1;
			}
		$b-&gt;{'up'} = defined($conf{'ONPARENT'}) &amp;&amp;
			     $b-&gt;{'virtual'} ne '' ?
				($conf{'ONPARENT'} eq 'yes') :
				($conf{'ONBOOT'} eq 'yes');
		$b-&gt;{'address'} = $conf{'IPADDR'} || $conf{'IPADDR0'};
		$b-&gt;{'netmask'} = $conf{'NETMASK'} || $conf{'NETMASK0'};
		if (!$conf{'NETMASK'} &amp;&amp; $conf{'PREFIX'}) {
			$b-&gt;{'netmask'} = &amp;prefix_to_mask($conf{'PREFIX'});
			}
		elsif (!$conf{'NETMASK'} &amp;&amp; $conf{'PREFIX0'}) {
			$b-&gt;{'netmask'} = &amp;prefix_to_mask($conf{'PREFIX0'});
			}
		$b-&gt;{'broadcast'} = $conf{'BROADCAST'};
		if (!$b-&gt;{'broadcast'} &amp;&amp; $b-&gt;{'address'} &amp;&amp; $b-&gt;{'netmask'}) {
			$b-&gt;{'broadcast'} = &amp;compute_broadcast($b-&gt;{'address'},
							       $b-&gt;{'netmask'});
			}
		$b-&gt;{'gateway'} = $conf{'GATEWAY'};
		$b-&gt;{'gateway6'} = $conf{'IPV6_DEFAULTGW'};
		$b-&gt;{'mtu'} = $conf{'MTU'};
		if ($b-&gt;{'fullname'} =~ /^bond/) {
			$b-&gt;{'partner'} = &amp;get_teaming_partner($conf{'DEVICE'});
			}
                my @values = split(/\s+/, $conf{'BONDING_OPTS'});
                foreach my $val (@values) {
                         my ($k, $v) = split(/=/, $val, 2);
				if ($k eq "mode") {
					$b-&gt;{'mode'} = $v;
					}
				elsif ($k eq "miimon") {
					$b-&gt;{'miimon'} = $v;
					}
				elsif ($k eq "updelay") {
					$b-&gt;{'updelay'} = $v;
					}
				elsif ($k eq "downdelay") {
					$b-&gt;{'downdelay'} = $v;
					}
                        }
		$b-&gt;{'ether'} = $conf{'MACADDR'};
		$b-&gt;{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
		$b-&gt;{'bootp'} = ($conf{'BOOTPROTO'} eq 'bootp');
		local @ip6s;
		push(@ip6s, [ split(/\//, $conf{'IPV6ADDR'}) ])
			if ($conf{'IPV6ADDR'});
		push(@ip6s, map { [ split(/\//, $_) ] }
				split(/\s+/, $conf{'IPV6ADDR_SECONDARIES'}));
		if (@ip6s) {
			# Static IPv6 addresses
			$b-&gt;{'address6'} = [ map { $_-&gt;[0] } @ip6s ];
			$b-&gt;{'netmask6'} = [ map { $_-&gt;[1] } @ip6s ];
			}
		elsif (lc($conf{'IPV6INIT'}) eq 'yes') {
			$b-&gt;{'auto6'} = 1;
			}
		$b-&gt;{'edit'} = ($b-&gt;{'name'} !~ /^ppp|irlan/);
		$b-&gt;{'desc'} = $conf{'NAME'};
		$b-&gt;{'index'} = scalar(@rv);
		$b-&gt;{'file'} = "$net_scripts_dir/$f";
		if ($conf{'BRIDGE'}) {
			$bridge_map{$conf{'BRIDGE'}} = $b-&gt;{'fullname'};
			}
		push(@rv, $b);
		}
	}
closedir(CONF);
foreach my $b (@rv) {
	if ($b-&gt;{'fullname'} =~ /^br\d+$/) {
		$b-&gt;{'bridge'} = 1;
		$b-&gt;{'bridgeto'} = $bridge_map{$b-&gt;{'fullname'}};
		}
	}
return @rv;
}

# save_bond_interface(device, master)
# Create or update a boot-time bond slave interface
sub save_bond_interface
{
local(%conf);
&amp;lock_file("$net_scripts_dir/ifcfg-$_[0]");
$conf{'DEVICE'} = $_[0];
$conf{'BOOTPROTO'} = none;
$conf{'ONBOOT'} = yes;
$conf{'MASTER'} = $_[1];
$conf{'SLAVE'} = "yes";
$conf{'USERCTL'} = "no";
&amp;write_env_file("$net_scripts_dir/ifcfg-$_[0]", \%conf);
&amp;unlock_file("$net_scripts_dir/ifcfg-$_[0]");
}

# save_interface(&amp;details)
# Create or update a boot-time interface
sub save_interface
{
my ($b) = @_;
local(%conf);
local $name = $b-&gt;{'range'} ne "" ? $b-&gt;{'name'}."-range".$b-&gt;{'range'} :
	      $b-&gt;{'virtual'} ne "" ? $b-&gt;{'name'}.":".$b-&gt;{'virtual'} :
	      $b-&gt;{'vlanid'} ne "" ? $b-&gt;{'physical'}.".".$b-&gt;{'vlanid'}
				       : $b-&gt;{'name'};
my $file = $b-&gt;{'file'} || "$net_scripts_dir/ifcfg-$name";
&amp;lock_file($file);
&amp;read_env_file($file, \%conf);
if ($b-&gt;{'range'} ne "") {
	# Special case - saving a range
	$conf{'IPADDR_START'} = $b-&gt;{'start'};
	$conf{'IPADDR_END'} = $b-&gt;{'end'};
	$conf{'CLONENUM_START'} = $b-&gt;{'num'};
	}
else {
	# Saving a normal interface
	$conf{'DEVICE'} = $name;
	my $pfx = $conf{'IPADDR0'} ? '0' : '';
	$conf{'IPADDR'.$pfx} = $b-&gt;{'address'};
	$conf{'NETMASK'.$pfx} = $b-&gt;{'netmask'};
	delete($conf{'PREFIX'.$pfx});
	if ($b-&gt;{'address'} &amp;&amp; $b-&gt;{'netmask'}) {
		$conf{'NETWORK'.$pfx} = &amp;compute_network($b-&gt;{'address'},
						         $b-&gt;{'netmask'});
		}
	else {
		$conf{'NETWORK'.$pfx} = '';
		}
	$conf{'BROADCAST'.$pfx} = $b-&gt;{'broadcast'};
	if ($b-&gt;{'gateway'}) {
		$conf{'GATEWAY'} = $b-&gt;{'gateway'};
		}
	else {
		delete($conf{'GATEWAY'});
		}
	if ($b-&gt;{'gateway6'}) {
		$conf{'IPV6_DEFAULTGW'} = $b-&gt;{'gateway6'};
		}
	else {
		delete($conf{'IPV6_DEFAULTGW'});
		}
	$conf{'MTU'} = $b-&gt;{'mtu'};
	$conf{'MACADDR'} = $b-&gt;{'ether'};
	$conf{'ONBOOT'} = $b-&gt;{'up'} ? "yes" : "no";
	$conf{'ONPARENT'} = $b-&gt;{'up'} ? "yes" : "no"
		if ($b-&gt;{'virtual'} ne '');
	$conf{'BOOTPROTO'} = $b-&gt;{'bootp'} ? "bootp" :
			     $b-&gt;{'dhcp'} ? "dhcp" : "none";
	delete($conf{'IPV6ADDR'});
	delete($conf{'IPV6ADDR_SECONDARIES'});
	local @ip6s;
	for(my $i=0; $i&lt;@{$b-&gt;{'address6'}}; $i++) {
		push(@ip6s, $b-&gt;{'address6'}-&gt;[$i]."/".
			    $b-&gt;{'netmask6'}-&gt;[$i]);
		}
	if ((@ip6s || $b-&gt;{'auto6'}) &amp;&amp; lc($conf{'IPV6INIT'}) ne 'yes') {
		$conf{'IPV6INIT'} = 'yes';
		}
	elsif (!@ip6s &amp;&amp; !$b-&gt;{'auto6'}) {
		$conf{'IPV6INIT'} = 'no';
		}
	if (@ip6s) {
		$conf{'IPV6ADDR'} = shift(@ip6s);
		$conf{'IPV6ADDR_SECONDARIES'} = join(" ", @ip6s);
		}
	if ($b-&gt;{'fullname'} =~ /^br(\d+)$/) {
		&amp;has_command("brctl") ||
			&amp;error("Bridges cannot be created unless the brctl ".
			       "command is installed");
		$conf{'TYPE'} = 'Bridge';
		}
	if ($b-&gt;{'fullname'} =~ /^bond(\d+)$/) {
		$conf{'BONDING_OPTS'} = "mode=$b-&gt;{'mode'}";
		if ($b-&gt;{'miimon'}) {
			$conf{'BONDING_OPTS'} .= " miimon=$b-&gt;{'miimon'}";
			}
		if ($b-&gt;{'updelay'}) {
			$conf{'BONDING_OPTS'} .= " updelay=$b-&gt;{'updelay'}";
			}
		if ($b-&gt;{'downdelay'}) {
			$conf{'BONDING_OPTS'} .= " downdelay=$b-&gt;{'downdelay'}";
			}

		my @values = split(/\s+/, $b-&gt;{'partner'});
		foreach my $val (@values) {
			&amp;save_bond_interface($val, $b-&gt;{'fullname'});
			}
		}
	if ($b-&gt;{'vlan'} == 1) {
		$conf{'VLAN'} = "yes";
		}
	}
$conf{'NAME'} = $b-&gt;{'desc'};
if (!-r $file) {
	# New interfaces shouldn't be controller by network manager
	$conf{'NM_CONTROLLED'} = 'no';
	}
&amp;write_env_file($file, \%conf);

# If this is a bridge, set BRIDGE in real interface
if ($b-&gt;{'bridge'}) {
	foreach my $efile (glob("$net_scripts_dir/ifcfg-e*")) {
		local %bconf;
		&amp;lock_file($efile);
		&amp;read_env_file($efile, \%bconf);
		if ($bconf{'DEVICE'} eq $b-&gt;{'bridgeto'} &amp;&amp;
		    $b-&gt;{'bridgeto'}) {
			# Correct device for bridge
			$bconf{'BRIDGE'} = $b-&gt;{'fullname'};
			&amp;write_env_file($efile, \%bconf);
			}
		elsif ($bconf{'BRIDGE'} eq $b-&gt;{'fullname'} &amp;&amp;
		       $bconf{'BRIDGE'}) {
			# Was using this bridge, shouldn't be
			delete($bconf{'BRIDGE'});
			&amp;write_env_file($efile, \%bconf);
			}
		&amp;unlock_file($efile);
		}
	}

# Link to devices directory
if (-d &amp;translate_filename($devices_dir)) {
	&amp;link_file($file, "$devices_dir/ifcfg-$name");
	}
&amp;unlock_file($file);

# Make sure IPv6 is enabled globally
if (@{$b-&gt;{'address6'}}) {
	local %conf;
	&amp;lock_file($network_config);
	&amp;read_env_file($network_config, \%conf);
	if (lc($conf{'NETWORKING_IPV6'}) ne 'yes') {
		$conf{'NETWORKING_IPV6'} = 'yes';
		&amp;write_env_file($network_config, \%conf);
		}
	&amp;unlock_file($network_config);
	}
}

# delete_interface(&amp;details)
# Delete a boot-time interface
sub delete_interface
{
my ($b) = @_;
local $name = $b-&gt;{'range'} ne "" ? $b-&gt;{'name'}."-range".
				    $b-&gt;{'range'} :
	      $b-&gt;{'virtual'} ne "" ? $b-&gt;{'name'}.":".$b-&gt;{'virtual'}
				    : $b-&gt;{'name'};
my $file = $b-&gt;{'file'} || "$net_scripts_dir/ifcfg-$name";
&amp;lock_file($file);
&amp;unlink_file("$net_scripts_dir/ifcfg-$name");
if (-d &amp;translate_filename($devices_dir)) {
	&amp;unlink_file("$devices_dir/ifcfg-$name");
	}
&amp;unlock_file($file);
}

# can_edit(what)
# Can some boot-time interface parameter be edited?
sub can_edit
{
if ($supports_mtu) {
	return 1;
	}
else {
	return $_[0] ne "mtu";
	}
}

# valid_boot_address(address)
# Is some address valid for a bootup interface
sub valid_boot_address
{
return &amp;check_ipaddress($_[0]);
}

# get_hostname()
sub get_hostname
{
local %conf;
&amp;read_env_file($network_config, \%conf);
if ($conf{'HOSTNAME'}) {
	return $conf{'HOSTNAME'};
	}
return &amp;get_system_hostname(1);
}

# save_hostname(name)
sub save_hostname
{
local $old = &amp;get_hostname();
local %conf;
&amp;system_logged("hostname $_[0] &gt;/dev/null 2&gt;&amp;1");
&amp;open_lock_tempfile(HOST, "&gt;/etc/HOSTNAME");
&amp;print_tempfile(HOST, $_[0],"\n");
&amp;close_tempfile(HOST);
&amp;lock_file($network_config);
&amp;read_env_file($network_config, \%conf);
$conf{'HOSTNAME'} = $_[0];
&amp;write_env_file($network_config, \%conf);
&amp;unlock_file($network_config);

# If any ifcfg-XXX files have the old hostname in DHCP_HOSTNAME, fix it
foreach my $b (&amp;boot_interfaces()) {
	local %ifc;
	&amp;read_env_file($b-&gt;{'file'}, \%ifc);
	if ($ifc{'DHCP_HOSTNAME'} eq $old) {
		$ifc{'DHCP_HOSTNAME'} = $_[0];
		&amp;lock_file($b-&gt;{'file'});
		&amp;write_env_file($b-&gt;{'file'}, \%ifc);
		&amp;unlock_file($b-&gt;{'file'});
		}
	}

# Update /etc/hostname if exists
if (-r "/etc/hostname") {
	&amp;open_lock_tempfile(HOST, "&gt;/etc/hostname");
	&amp;print_tempfile(HOST, $_[0],"\n");
	&amp;close_tempfile(HOST);
	}

undef(@main::get_system_hostname);	# clear cache
}

# get_domainname()
sub get_domainname
{
local $d;
&amp;execute_command("domainname", undef, \$d, undef);
chop($d);
return $d;
}

# save_domainname(domain)
sub save_domainname
{
local %conf;
&amp;execute_command("domainname ".quotemeta($_[0]));
&amp;read_env_file($network_config, \%conf);
if ($_[0]) {
	$conf{'NISDOMAIN'} = $_[0];
	}
else {
	delete($conf{'NISDOMAIN'});
	}
&amp;write_env_file($network_config, \%conf);
}

sub routing_config_files
{
local @rv = ( $network_config, $sysctl_config );
if (!$supports_dev_routes) {
	push(@rv, $static_route_config);
	}
else {
	foreach my $dir ($devices_dir, $net_scripts_dir) {
		opendir(DIR, &amp;translate_filename($dir));
		while(my $f = readdir(DIR)) {
			if ($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
			    $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) {
				push(@rv, "$dir/$f");
				}
			}
		closedir(DIR);
		}
	}
return @rv;
}

sub network_config_files
{
return ( "/etc/HOSTNAME", $network_config );
}

sub routing_input
{
local (%conf, @st, @hr, %sysctl);
&amp;read_env_file($network_config, \%conf);
if (!$supports_dev_gateway) {
	# show default router and device
	print &amp;ui_table_row($text{'routes_default'},
		&amp;ui_opt_textbox("gateway", $conf{'GATEWAY'}, 15,
				$text{'routes_none'}));

	print &amp;ui_table_row($text{'routes_device2'},
		&amp;ui_opt_textbox("gatewaydev", $conf{'GATEWAYDEV'}, 6,
			        $text{'routes_none'}));
	}
else {
	# multiple default routers can exist, one per interface
	my @table;
	local $r = 0;
	if ($conf{'GATEWAY'} || $conf{'IPV6_DEFAULTGW'}) {
		push(@table, [
		    &amp;interface_sel("gatewaydev$r",
				   $conf{'GATEWAYDEV'} ||
				     $conf{'IPV6_DEFAULTDEV'} || "*"),
		    &amp;ui_textbox("gateway$r", $conf{'GATEWAY'}, 15),
		    &amp;ui_textbox("gateway6$r", $conf{'IPV6_DEFAULTGW'}, 30),
		    ]);
		$r++;
		}
	local @boot = &amp;boot_interfaces();
	foreach $b (grep { $_-&gt;{'gateway'} &amp;&amp; $_-&gt;{'virtual'} eq '' } @boot) {
		push(@table, [ &amp;interface_sel("gatewaydev$r", $b-&gt;{'name'}),
			       &amp;ui_textbox("gateway$r", $b-&gt;{'gateway'}, 15),
			       &amp;ui_textbox("gateway6$r", $b-&gt;{'gateway6'}, 30),
			     ]);
		$r++;
		}
	push(@table, [ &amp;interface_sel("gatewaydev$r"),
		       &amp;ui_textbox("gateway$r", undef, 15),
		       &amp;ui_textbox("gateway6$r", undef, 30), ]);
	print &amp;ui_table_row($text{'routes_default2'},
		&amp;ui_columns_table(
			[ $text{'routes_ifc'}, $text{'routes_gateway'},
			  $text{'routes_gateway6'} ],
			undef, \@table, undef, 1));
	}

# show routing
if ($gconfig{'os_version'} &lt; 7.0) {
	print &amp;ui_table_row($text{'routes_forward'},
		&amp;ui_yesno_radio("forward",
				$conf{'FORWARD_IPV4'} eq "yes" ? 1 : 0));
	}
else {
	&amp;read_env_file($sysctl_config, \%sysctl);
	print &amp;ui_table_row($text{'routes_forward'},
		&amp;ui_yesno_radio("forward",
				$sysctl{'net.ipv4.ip_forward'} ? 1 : 0));
	}

if (!$supports_dev_routes) {
	# get static routes from single file
	&amp;open_readfile(STATIC, $static_route_config);
	while(&lt;STATIC&gt;) {
		if (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)\s+gw\s+(\S+)/) {
			push(@st, [ $1, $2, $3, $4 ]);
			}
		elsif (/(\S+)\s+host\s+(\S+)\s+gw\s+(\S+)/) {
			push(@st, [ $1, $2, '255.255.255.255', $3 ]);
			}
		elsif (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)/) {
			push(@hr, [ $1, $2, $3 ]);
			}
		elsif (/(\S+)\s+host\s+(\S+)/) {
			push(@hr, [ $1, $2, '255.255.255.255' ]);
			}
		}
	close(STATIC);
	}
else {
	# get static routes from per-interface files
	local $f;
	opendir(DIR, &amp;translate_filename($route_files_dir));
	while($f = readdir(DIR)) {
		if ($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
		    $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) {
			local $dev = $1;
			local (%rfile, $i);
			&amp;read_env_file("$route_files_dir/$f", \%rfile);
			for($i=0; defined($rfile{"ADDRESS$i"}); $i++) {
				if ($rfile{"GATEWAY$i"}) {
					push(@st, [ $dev, $rfile{"ADDRESS$i"},
							  $rfile{"NETMASK$i"},
							  $rfile{"GATEWAY$i"}]);
					}
				else {
					push(@hr, [ $dev, $rfile{"ADDRESS$i"},
							  $rfile{"NETMASK$i"} ||
							  "255.255.255.255" ]);
					}
				}
			}
		}
	closedir(DIR);
	}

# Show static network routes
my @table;
for($i=0; $i&lt;=@st; $i++) {
	local $st = $st[$i];
	push(@table, [ &amp;ui_textbox("dev_$i", $st-&gt;[0], 6),
		       &amp;ui_textbox("net_$i", $st-&gt;[1], 15),
		       &amp;ui_textbox("netmask_$i", $st-&gt;[2], 15),
		       &amp;ui_textbox("gw_$i", $st-&gt;[3], 15) ]);
	}
print &amp;ui_table_row($text{'routes_static'},
	&amp;ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
			    $text{'routes_mask'}, $text{'routes_gateway'} ],
			  undef, \@table, undef, 1));

# Show static host routes
my @table;
for($i=0; $i&lt;=@hr; $i++) {
	local $st = $hr[$i];
	push(@table, [ &amp;ui_textbox("ldev_$i", $st-&gt;[0], 6),
		       &amp;ui_textbox("lnet_$i", $st-&gt;[1], 15),
		       &amp;ui_textbox("lnetmask_$i", $st-&gt;[2], 15) ]);
	}
print &amp;ui_table_row($text{'routes_local'},
	&amp;ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
			    $text{'routes_mask'} ],
			  undef, \@table, undef, 1));
}

sub parse_routing
{
local (%conf, @st, %sysctl, %st, @boot);
&amp;lock_file($network_config);
&amp;read_env_file($network_config, \%conf);
if (!$supports_dev_gateway) {
	# Just update a single file
	if ($in{'gateway_def'}) { delete($conf{'GATEWAY'}); }
	elsif (!&amp;to_ipaddress($in{'gateway'})) {
		&amp;error(&amp;text('routes_edefault', $in{'gateway'}));
		}
	else { $conf{'GATEWAY'} = $in{'gateway'}; }

	if ($in{'gatewaydev_def'}) { delete($conf{'GATEWAYDEV'}); }
	elsif ($in{'gatewaydev'} !~ /^\S+$/) {
		&amp;error(&amp;text('routes_edevice', $in{'gatewaydev'}));
		}
	else { $conf{'GATEWAYDEV'} = $in{'gatewaydev'}; }
	}
else {
	# Multiple defaults can be specified!
	local ($r, $b);
	@boot = grep { $-&gt;{'virtual'} eq '' } &amp;boot_interfaces();
	foreach $b (@boot) {
		delete($b-&gt;{'gateway'});
		}
	delete($conf{'GATEWAY'});
	delete($conf{'GATEWAYDEV'});
	delete($conf{'IPV6_DEFAULTDEV'});
	delete($conf{'IPV6_DEFAULTGW'});

	for($r=0; defined($in{"gatewaydev$r"}); $r++) {
		next if (!$in{"gatewaydev$r"});
		&amp;check_ipaddress($in{"gateway$r"}) ||
			&amp;error(&amp;text('routes_edefault2', $r+1));
		if ($in{"gatewaydev$r"} eq "*") {
			# For any interface
			$conf{'GATEWAY'} &amp;&amp; &amp;error(&amp;text('routes_eclash'));
			$conf{'GATEWAY'} = $in{"gateway$r"};
			$conf{'IPV6_DEFAULTGW'} &amp;&amp;
				&amp;error(&amp;text('routes_eclash6'));
			$conf{'IPV6_DEFAULTGW'} = $in{"gateway6$r"};
			}
		else {
			# For a specific interface
			local ($b) = grep { $_-&gt;{'fullname'} eq
					    $in{"gatewaydev$r"} } @boot;
			$b-&gt;{'gateway'} &amp;&amp; &amp;error(&amp;text('routes_eclash2',
							$in{"gatewaydev$r"}));
			$b-&gt;{'gateway'} = $in{"gateway$r"};
			$b-&gt;{'gateway6'} = $in{"gateway6$r"};
			}
		}
	}

if ($gconfig{'os_version'} &lt; 7.0) {
	if ($in{'forward'}) { $conf{'FORWARD_IPV4'} = 'yes'; }
	else { $conf{'FORWARD_IPV4'} = 'no'; }
	}
else {
	&amp;lock_file($sysctl_config);
	&amp;read_env_file($sysctl_config, \%sysctl);
	$sysctl{'net.ipv4.ip_forward'} = $in{'forward'};
	}

# Parse static and local routes
for($i=0; defined($dev = $in{"dev_$i"}); $i++) {
	next if (!$dev);
	$net = $in{"net_$i"}; $netmask = $in{"netmask_$i"}; $gw = $in{"gw_$i"};
	$dev =~ /^\S+$/ || &amp;error(&amp;text('routes_edevice', $dev));
	&amp;to_ipaddress($net) || &amp;error(&amp;text('routes_enet', $net));
	&amp;check_ipaddress($netmask) || &amp;error(&amp;text('routes_emask', $netmask));
	&amp;to_ipaddress($gw) || &amp;error(&amp;text('routes_egateway', $gw));
	if ($netmask eq "255.255.255.255") {
		push(@st, "$dev host $net gw $gw\n");
		}
	else {
		push(@st, "$dev net $net netmask $netmask gw $gw\n");
		}
	push(@{$st{$dev}}, [ $net, $netmask, $gw ]);
	}
for($i=0; defined($dev = $in{"ldev_$i"}); $i++) {
	$net = $in{"lnet_$i"}; $netmask = $in{"lnetmask_$i"};
	next if (!$dev &amp;&amp; !$net);
	$dev =~ /^\S+$/ || &amp;error(&amp;text('routes_edevice', $dev));
	&amp;to_ipaddress($net) ||
	    $net =~ /^(\S+)\/(\d+)$/ &amp;&amp; &amp;to_ipaddress("$1") ||
		&amp;error(&amp;text('routes_enet', $net));
	&amp;check_ipaddress($netmask) || &amp;error(&amp;text('routes_emask', $netmask));
	if ($netmask eq "255.255.255.255") {
		push(@st, "$dev host $net\n");
		}
	else {
		push(@st, "$dev net $net netmask $netmask\n");
		}
	push(@{$st{$dev}}, [ $net, $netmask ]);
	}
if (!$supports_dev_routes) {
	# Write to a single file
	&amp;open_lock_tempfile(STATIC, "&gt;$static_route_config");
	&amp;print_tempfile(STATIC, @st);
	&amp;close_tempfile(STATIC);
	}
else {
	# Write to one file per interface (delete old, then save new/updated)
	local $f;
	opendir(DIR, &amp;translate_filename($route_files_dir));
	while($f = readdir(DIR)) {
		if (($f =~ /^([a-z]+\d*(\.\d+)?(:\d+)?)\.route$/ ||
		     $f =~ /^route\-([a-z]+\d*(\.\d+)?(:\d+)?)$/) &amp;&amp; !$st{$1}) {
			&amp;unlink_logged("$devices_dir/$f");
			&amp;unlink_logged("$net_scripts_dir/$f");
			}
		}
	closedir(DIR);
	foreach $dev (keys %st) {
		$f = $supports_route_dev ? "route-$dev" : "$dev.route";
		local (%rfile, $i);
		for($i=0; $i&lt;@{$st{$dev}}; $i++) {
			$rfile{"ADDRESS$i"} = $st{$dev}-&gt;[$i]-&gt;[0];
			$rfile{"NETMASK$i"} = $st{$dev}-&gt;[$i]-&gt;[1];
			$rfile{"GATEWAY$i"} = $st{$dev}-&gt;[$i]-&gt;[2];
			}
		&amp;lock_file("$route_files_dir/$f");
		&amp;write_env_file("$route_files_dir/$f", \%rfile);
		&amp;unlock_file("$route_files_dir/$f");
		if ($route_files_dir ne $net_scripts_dir) {
			&amp;lock_file("$net_scripts_dir/$f");
			&amp;link_file("$route_files_dir/$f",
				   "$net_scripts_dir/$f");
			&amp;unlock_file("$net_scripts_dir/$f");
			}
		}
	}
&amp;write_env_file($network_config, \%conf);
&amp;unlock_file($network_config);
if (%sysctl) {
	&amp;write_env_file($sysctl_config, \%sysctl);
	&amp;unlock_file($sysctl_config);
	}
if (@boot) {
	local $b;
	foreach $b (@boot) {
		&amp;save_interface($b);
		}
	}
}

# interface_sel(name, value)
# Returns a menu for all boot-time interfaces
sub interface_sel
{
local ($name, $value) = @_;
local @opts = ( [ "", "&amp;nbsp;" ],
		[ "*", $text{'routes_any'} ] );
@boot_interfaces_cache = sort { $a-&gt;{'fullname'} cmp $b-&gt;{'fullname'} }
	&amp;boot_interfaces() if (!@boot_interfaces_cache);
foreach $b (@boot_interfaces_cache) {
	push(@opts, [ $b-&gt;{'fullname'}, $b-&gt;{'fullname'} ]);
	}
return &amp;ui_select($name, $value, \@opts);
}

# apply_network()
# Apply the interface and routing settings
sub apply_network
{
if ($gconfig{'os_type'} eq 'mandrake-linux') {
	&amp;system_logged("(cd / ; service network stop ; service network start) &gt;/dev/null 2&gt;&amp;1");
	}
else {
	&amp;system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) &gt;/dev/null 2&gt;&amp;1");
	}
}

# apply_interface(&amp;iface)
# Calls an OS-specific function to make a boot-time interface active
sub apply_interface
{
local $out = &amp;backquote_logged("cd / ; ifdown '$_[0]-&gt;{'fullname'}' &gt;/dev/null 2&gt;&amp;1 &lt;/dev/null ; ifup '$_[0]-&gt;{'fullname'}' 2&gt;&amp;1 &lt;/dev/null");
return $? || $out =~ /error/i ? $out : undef;
}

# unapply_interface(&amp;iface)
# Calls an OS-specific function to make a boot-time interface inactive
#sub unapply_interface
#{
#local $out = &amp;backquote_logged("cd / ; ifdown '$_[0]-&gt;{'fullname'}' 2&gt;&amp;1 &lt;/dev/null");
#return $? ? $out : undef;
#}

# get_default_gateway()
# Returns the default gateway IP (if one is set) and device (if set) boot time
# settings.
sub get_default_gateway
{
local %conf;
&amp;read_env_file($network_config, \%conf);
local @boot = &amp;boot_interfaces();
local ($gifc) = grep { $_-&gt;{'gateway'} &amp;&amp; $_-&gt;{'virtual'} eq '' } @boot;
return ( $gifc-&gt;{'gateway'}, $gifc-&gt;{'fullname'} ) if ($gifc);
return $conf{'GATEWAY'} ? ( $conf{'GATEWAY'}, $conf{'GATEWAYDEV'} ) : ( );
}

# set_default_gateway(gateway, device)
# Sets the default gateway to the given IP accessible via the given device,
# in the boot time settings.
sub set_default_gateway
{
&amp;lock_file($network_config);
&amp;read_env_file($network_config, \%conf);
if (!$supports_dev_gateway) {
	# Just update the network config file
	local %conf;
	if ($_[0]) {
		$conf{'GATEWAY'} = $_[0];
		$conf{'GATEWAYDEV'} = $_[1];
		}
	else {
		delete($conf{'GATEWAY'});
		delete($conf{'GATEWAYDEV'});
		}
	}
else {
	# Set the gateway in the specified interface file, and clear the rest
	local @boot = grep { $-&gt;{'virtual'} eq '' } &amp;boot_interfaces();
	foreach $b (@boot) {
		delete($b-&gt;{'gateway'});
		if ($_[0] &amp;&amp; $b-&gt;{'fullname'} eq $_[1]) {
			$b-&gt;{'gateway'} = $_[0];
			&amp;save_interface($b);
			}
		}
	delete($conf{'GATEWAY'});
	delete($conf{'GATEWAYDEV'});
	}
&amp;write_env_file($network_config, \%conf);
&amp;unlock_file($network_config);
}

# get_default_ipv6_gateway()
# Returns the default gateway IPv6 address (if one is set) and device (if set)
# boot time settings.
sub get_default_ipv6_gateway
{
local %conf;
&amp;read_env_file($network_config, \%conf);
local @boot = &amp;boot_interfaces();
local ($gifc) = grep { $_-&gt;{'gateway6'} &amp;&amp; $_-&gt;{'virtual'} eq '' } @boot;
return ( $gifc-&gt;{'gateway6'}, $gifc-&gt;{'fullname'} ) if ($gifc);
return $conf{'IPV6_DEFAULTGW'} ? ( $conf{'IPV6_DEFAULTGW'},
				   $conf{'IPV6_DEFAULTDEV'} ) : ( );
}

# set_default_ipv6_gateway(gateway, device)
# Sets the default gateway to the given IPv6 address accessible via the given
# device, in the boot time settings.
sub set_default_ipv6_gateway
{
&amp;lock_file($network_config);
&amp;read_env_file($network_config, \%conf);
local @boot = grep { $-&gt;{'virtual'} eq '' } &amp;boot_interfaces();
foreach $b (@boot) {
	delete($b-&gt;{'gateway6'});
	if ($_[0] &amp;&amp; $b-&gt;{'fullname'} eq $_[1]) {
		$b-&gt;{'gateway6'} = $_[0];
		&amp;save_interface($b);
		}
	}
delete($conf{'IPV6_DEFAULTGW'});
delete($conf{'IPV6_DEFAULTDEV'});
&amp;write_env_file($network_config, \%conf);
&amp;unlock_file($network_config);
}

# supports_ranges()
# Returns 1 for newer redhat versions
sub supports_ranges
{
return ($gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
	$gconfig{'os_version'} &gt;= 7.3) ||
       ($gconfig{'os_type'} eq 'mandrake-linux' &amp;&amp;
	$gconfig{'os_version'} &gt;= 8.0) ||
       ($gconfig{'os_type'} eq 'coherant-linux' &amp;&amp;
	$gconfig{'os_version'} &gt;= 3.0);
}

sub supports_bonding
{
return $gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
       $gconfig{'os_version'} &gt;= 13.0 &amp;&amp;
       &amp;has_command("ifenslave");
}

sub supports_vlans
{
return $gconfig{'os_type'} eq 'redhat-linux' &amp;&amp;
	$gconfig{'os_version'} &gt;= 13.0 &amp;&amp;
	&amp;has_command("vconfig");
}


# range_input([&amp;interface])
# Print HTML for a IP range interface
sub range_input
{
local $new = !$_[0];

# Range description
print &amp;ui_table_row($text{'ifcs_desc'},
	&amp;ui_textbox("desc", $_[0] ? $_[0]-&gt;{'desc'} : undef, 60));

# Base interface
my $ifaceinput;
if ($new) {
	$ifaceinput = &amp;ui_select("iface", $_[0]-&gt;{'name'},
		[ map { $_-&gt;{'fullname'} } grep { $b-&gt;{'virtual'} eq '' }
		      &amp;boot_interfaces() ]);
	}
else {
	$ifaceinput = "&lt;tt&gt;$_[0]-&gt;{'name'}&lt;/tt&gt;";
	}
print &amp;ui_table_row($text{'range_iface'}, $ifaceinput);

# Name for this range
print &amp;ui_table_row($text{'range_name'},
	$new ? &amp;ui_textbox("range", undef, 10)
	     : "&lt;tt&gt;$_[0]-&gt;{'range'}&lt;/tt&gt;");

# Start
print &amp;ui_table_row($text{'range_start'},
	&amp;ui_textbox("start", $_[0]-&gt;{'start'}, 15));

# Stop
print &amp;ui_table_row($text{'range_end'},
	&amp;ui_textbox("end", $_[0]-&gt;{'end'}, 15));

# Base number
print &amp;ui_table_row($text{'range_num'},
	&amp;ui_textbox("num", $_[0]-&gt;{'num'}, 5));
}

# parse_range(&amp;range, &amp;in)
sub parse_range
{
local %in = %{$_[1]};
if ($in{'new'}) {
	$_[0]-&gt;{'name'} = $in{'iface'};
	$in{'range'} =~ /^[a-z0-9\.\_]+$/ || &amp;error($text{'range_ename'});
	$_[0]-&gt;{'range'} = $in{'range'};
	$_[0]-&gt;{'fullname'} = $in{'iface'}."-range".$in{'range'};
	}
$_[0]-&gt;{'desc'} = $in{'desc'};

&amp;check_ipaddress($in{'start'}) || &amp;error($text{'range_estart'});
$_[0]-&gt;{'start'} = $in{'start'};

&amp;check_ipaddress($in{'end'}) || &amp;error($text{'range_eend'});
$_[0]-&gt;{'end'} = $in{'end'};

local @sip = split(/\./, $in{'start'});
local @eip = split(/\./, $in{'end'});
$sip[0] == $eip[0] &amp;&amp; $sip[1] == $eip[1] &amp;&amp; $sip[2] == $eip[2] ||
	&amp;error($text{'range_eclass'});
$sip[3] &lt;= $eip[3] || &amp;error($text{'range_ebefore'});

$in{'num'} =~ /^\d+$/ || &amp;error($text{'range_enum'});
$_[0]-&gt;{'num'} = $in{'num'};
}

# get_dhcp_hostname()
# Returns 0 if the hostname is not set by DHCP, 1 if it is, or -1 if this
# feature is not supported on this OS.
sub get_dhcp_hostname
{
return -1 if ($gconfig{'os_type'} ne 'redhat-linux' ||
	      $gconfig{'os_version'} &lt; 11);
local @boot = &amp;boot_interfaces();
local ($eth) = grep { $_-&gt;{'fullname'} =~ /^(eth|em)\d+$/ } @boot;
return -1 if (!$eth);
local %eth;
&amp;read_env_file($eth-&gt;{'file'}, \%eth);
return $eth{'DHCP_HOSTNAME'} ne &amp;get_system_hostname();
}

# save_dhcp_hostname(set)
# If called with a parameter of 0, the hostname is fixed and not set by
# DHCP. If called with 1, the hostname is chosen by DHCP.
sub save_dhcp_hostname
{
}

# get_teaming_partner(devicename)
# Gets the teamingpartners of a configured bond interface
sub get_teaming_partner
{
local ($g, $return);
opendir(CONF2, &amp;translate_filename($net_scripts_dir));
while($g = readdir(CONF2)) {
        local %conf2;
        if ($g !~ /\.(bak|old)$/i &amp;&amp; $g =~ /^ifcfg-([a-z0-9:\.]+)$/) {
                &amp;read_env_file("$net_scripts_dir/$g", \%conf2);
                if ($conf2{'MASTER'} eq "$_[0]") {
			$return .= $conf2{'DEVICE'}." ";
                	}
        	}
	}
return $return;
}

sub boot_iface_hardware
{
return $_[0] =~ /^(eth|em)/;
}

# supports_address6([&amp;iface])
# Returns 1 if managing IPv6 interfaces is supported
sub supports_address6
{
local ($iface) = @_;
return !$iface || $iface-&gt;{'virtual'} eq '';
}

# Returns 1, as boot-time interfaces on Redhat can exist without an IP (such as
# for bridging)
sub supports_no_address
{
return 1;
}

# Bridge interfaces can be created on redhat
sub supports_bridges
{
return 1;
}

# os_save_dns_config(&amp;config)
# Updates DNSx lines in all network-scripts files that have them
sub os_save_dns_config
{
local ($conf) = @_;
foreach my $b (&amp;boot_interfaces()) {
	local %ifc;
	&amp;read_env_file($b-&gt;{'file'}, \%ifc);
	next if (!defined($ifc{'DNS1'}));
	&amp;lock_file($b-&gt;{'file'});
	foreach my $k (keys %ifc) {
		delete($ifc{$k}) if ($k =~ /^DNS\d+$/);
		}
	&amp;write_env_file($b-&gt;{'file'}, \%ifc);
	local $i = 1;
	foreach my $ns (@{$conf-&gt;{'nameserver'}}) {
		$ifc{'DNS'.$i} = $ns;
		$i++;
		}
	if (!@{$conf-&gt;{'nameserver'}}) {
		# Add an empty DNS1 line so that we know to update this file
		# later if DNS resolves come back
		$ifc{'DNS1'} = ''
		}
	&amp;write_env_file($b-&gt;{'file'}, \%ifc);
	&amp;unlock_file($b-&gt;{'file'});
	}
return (0, 0);
}

1;

</pre></body></html>