1 # suse-linux-9.0-lib.pl
2 # Networking functions for SuSE linux 9.0 and above
4 $net_scripts_dir = "/etc/sysconfig/network";
5 $routes_config = "/etc/sysconfig/network/routes";
6 $sysctl_config = "/etc/sysconfig/sysctl";
11 # Returns a list of interfaces brought up at boot time
15 local @active = &active_interfaces();
16 opendir(CONF, &translate_filename($net_scripts_dir));
17 while($f = readdir(CONF)) {
18 if ($f =~ /^ifcfg-eth-id-([a-f0-9:]+)$/i) {
19 # An interface identified by MAC address!
22 local ($a) = grep { lc($_->{'ether'}) eq lc($b->{'mac'}) }
25 &read_env_file("$net_scripts_dir/$f", \%conf);
26 $b->{'fullname'} = $a->{'fullname'};
27 $b->{'name'} = $a->{'name'};
28 $b->{'up'} = ($conf{'STARTMODE'} eq 'onboot');
30 if ($conf{'IPADDR'} =~ /^(\S+)\/(\d+)$/) {
35 $b->{'address'} = $conf{'IPADDR'};
37 $pfx = $conf{'PREFIXLEN'} if (!$pfx);
39 $b->{'netmask'} = &prefix_to_mask($pfx);
42 $b->{'netmask'} = $conf{'NETMASK'};
44 $b->{'broadcast'} = $conf{'BROADCAST'};
45 $b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
46 $b->{'mtu'} = $conf{'MTU'};
47 $b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);
48 $b->{'index'} = scalar(@rv);
49 $b->{'file'} = "$net_scripts_dir/$f";
52 elsif ($f =~ /^ifcfg-([a-z0-9:\.]+)$/) {
53 # A normal interface file
55 $b->{'fullname'} = $1;
56 &read_env_file("$net_scripts_dir/$f", \%conf);
57 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
61 else { $b->{'name'} = $b->{'fullname'}; }
62 $b->{'up'} = ($conf{'STARTMODE'} eq 'onboot');
64 if ($conf{'IPADDR'} =~ /^(\S+)\/(\d+)$/) {
69 $b->{'address'} = $conf{'IPADDR'};
71 $pfx = $conf{'PREFIXLEN'} if (!$pfx);
73 $b->{'netmask'} = &prefix_to_mask($pfx);
76 $b->{'netmask'} = $conf{'NETMASK'};
78 $b->{'broadcast'} = $conf{'BROADCAST'};
79 $b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
80 $b->{'mtu'} = $conf{'MTU'};
81 $b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);
82 $b->{'index'} = scalar(@rv);
83 $b->{'file'} = "$net_scripts_dir/$f";
91 # save_interface(&details)
92 # Create or update a boot-time interface
96 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
98 local $file = $_[0]->{'file'} || "$net_scripts_dir/ifcfg-$name";
100 &read_env_file($file, \%conf);
101 $conf{'IPADDR'} = $_[0]->{'address'};
102 local($ip1, $ip2, $ip3, $ip4) = split(/\./, $_[0]->{'address'});
103 $conf{'NETMASK'} = $_[0]->{'netmask'};
104 local($nm1, $nm2, $nm3, $nm4) = split(/\./, $_[0]->{'netmask'});
105 if ($_[0]->{'address'} && $_[0]->{'netmask'}) {
106 $conf{'NETWORK'} = sprintf "%d.%d.%d.%d",
107 ($ip1 & int($nm1))&0xff,
108 ($ip2 & int($nm2))&0xff,
109 ($ip3 & int($nm3))&0xff,
110 ($ip4 & int($nm4))&0xff;
113 $conf{'NETWORK'} = '';
115 delete($conf{'PREFIXLEN'});
116 $conf{'BROADCAST'} = $_[0]->{'broadcast'};
117 $conf{'STARTMODE'} = $_[0]->{'up'} ? "onboot" :
118 $conf{'STARTMODE'} eq "onboot" ? "manual" :
120 $conf{'BOOTPROTO'} = $_[0]->{'dhcp'} ? "dhcp" : "static";
121 $conf{'MTU'} = $_[0]->{'mtu'};
122 $conf{'UNIQUE'} ||= time();
123 &write_env_file($file, \%conf);
127 # delete_interface(&details)
128 # Delete a boot-time interface
131 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
133 local $file = $_[0]->{'file'} || "$net_scripts_dir/ifcfg-$name";
134 &unlink_logged($file);
138 # Can some boot-time interface parameter be edited?
141 return $_[0] ne "bootp";
144 # valid_boot_address(address)
145 # Is some address valid for a bootup interface
146 sub valid_boot_address
148 return &check_ipaddress($_[0]);
154 local $hn = &read_file_contents("/etc/HOSTNAME");
159 return &get_system_hostname(1);
162 # save_hostname(name)
166 &system_logged("hostname $_[0] >/dev/null 2>&1");
167 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
168 &print_tempfile(HOST, $_[0],"\n");
169 &close_tempfile(HOST);
170 undef(@main::get_system_hostname); # clear cache
173 sub routing_config_files
175 return ( $routes_config, $sysctl_config );
178 # get_routes_config()
179 # Returns the list of save static routes
180 sub get_routes_config
183 &open_readfile(ROUTES, $routes_config);
187 local @r = map { $_ eq '-' ? undef : $_ } split(/\s+/, $_);
188 push(@routes, \@r) if (@r);
194 # save_routes_config(&routes)
195 sub save_routes_config
197 &open_tempfile(ROUTES, ">$routes_config");
198 foreach $r (@{$_[0]}) {
199 &print_tempfile(ROUTES, join(" ",
203 $r->[3] || "-"),"\n");
205 &close_tempfile(ROUTES);
210 local @routes = &get_routes_config();
212 # show default router and device
213 local ($def) = grep { $_->[0] eq "default" } @routes;
214 print &ui_table_row($text{'routes_default'},
215 &ui_opt_textbox("gateway", $def->[1], 15, $text{'routes_none'}));
217 print &ui_table_row($text{'routes_device2'},
218 &ui_opt_textbox("gatewaydev", $def->[3], 6, $text{'routes_none'}));
220 # Forwarding enabled?
221 &read_env_file($sysctl_config, \%sysctl);
222 print &ui_table_row($text{'routes_forward'},
223 &ui_yesno_radio("forward", $sysctl{'IP_FORWARD'} eq 'yes'));
225 # show static network routes
228 foreach my $r (@routes, [ ]) {
229 next if ($r eq $def);
230 push(@table, [ &ui_textbox("dev_$i", $r->[3], 6),
231 &ui_textbox("net_$i", $r->[0], 15),
232 &ui_textbox("netmask_$i", $r->[2], 15),
233 &ui_textbox("gw_$i", $r->[1], 15),
234 &ui_textbox("type_$i", $r->[4], 10) ]);
236 print &ui_table_row($text{'routes_static'},
237 &ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
238 $text{'routes_mask'}, $text{'routes_gateway'},
239 $text{'routes_type'} ],
240 undef, \@table, undef, 1));
246 local (@routes, $r, $i);
247 if (!$in{'gateway_def'}) {
248 &to_ipaddress($in{'gateway'}) ||
249 &error(&text('routes_edefault', $in{'gateway'}));
250 local @def = ( "default", $in{'gateway'}, undef, undef );
251 if (!$in{'gatewaydev_def'}) {
252 $in{'gatewaydev'} =~ /^\S+$/ ||
253 &error(&text('routes_edevice', $in{'gatewaydev'}));
254 $def[3] = $in{'gatewaydev'};
256 push(@routes, \@def);
258 for($i=0; defined($in{"dev_$i"}); $i++) {
259 next if (!$in{"net_$i"});
260 &check_ipaddress($in{"net_$i"}) ||
261 $in{"net_$i"} =~ /^(\S+)\/(\d+)$/ && &check_ipaddress($1) ||
262 &error(&text('routes_enet', $in{"net_$i"}));
263 $in{"dev_$i"} =~ /^\S*$/ || &error(&text('routes_edevice', $dev));
264 !$in{"netmask_$i"} || &check_ipaddress($in{"netmask_$i"}) ||
265 &error(&text('routes_emask', $in{"netmask_$i"}));
266 !$in{"gw_$i"} || &check_ipaddress($in{"gw_$i"}) ||
267 &error(&text('routes_egateway', $in{"gw_$i"}));
268 $in{"type_$i"} =~ /^\S*$/ ||
269 &error(&text('routes_etype', $in{"type_$i"}));
270 push(@routes, [ $in{"net_$i"}, $in{"gw_$i"}, $in{"netmask_$i"},
271 $in{"dev_$i"}, $in{"type_$i"} ] );
274 # Save routes and routing option
275 &save_routes_config(\@routes);
276 local $lref = &read_file_lines($sysctl_config);
277 for($i=0; $i<@$lref; $i++) {
278 if ($lref->[$i] =~ /^\s*IP_FORWARD\s*=/) {
279 $lref->[$i] = "IP_FORWARD=".($in{'forward'} ? "yes" : "no");
285 # get_default_gateway()
286 # Returns the default gateway IP (if one is set) and device (if set) boot time
288 sub get_default_gateway
290 local @routes = &get_routes_config();
291 local ($def) = grep { $_->[0] eq "default" } @routes;
293 return ( $def->[1], $def->[3] );
300 # set_default_gateway(gateway, device)
301 # Sets the default gateway to the given IP accessible via the given device,
302 # in the boot time settings.
303 sub set_default_gateway
305 local @routes = &get_routes_config();
306 local ($def) = grep { $_->[0] eq "default" } @routes;
311 elsif ($def && !$_[0]) {
312 @routes = grep { $_ ne $def } @routes;
314 elsif (!$def && $_[0]) {
315 splice(@routes, 0, 0, [ "default", $_[0], undef, $_[1] ]);
317 &save_routes_config(\@routes);
320 sub os_feedback_files
322 opendir(DIR, $net_scripts_dir);
323 local @f = readdir(DIR);
325 return ( (map { "$net_scripts_dir/$_" } grep { /^ifcfg-/ } @f),
326 $network_config, $static_route_config, "/etc/resolv.conf",
327 "/etc/nsswitch.conf", "/etc/HOSTNAME" );
331 # Apply the interface and routing settings
334 &system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) >/dev/null 2>&1");
337 # supports_address6([&iface])
338 # Returns 1 if managing IPv6 interfaces is supported
339 sub supports_address6