Handle hostnames with upper-case letters
[webmin.git] / net / suse-linux-9.0-lib.pl
1 # suse-linux-9.0-lib.pl
2 # Networking functions for SuSE linux 9.0 and above
3
4 $net_scripts_dir = "/etc/sysconfig/network";
5 $routes_config = "/etc/sysconfig/network/routes";
6 $sysctl_config = "/etc/sysconfig/sysctl";
7
8 do 'linux-lib.pl';
9
10 # boot_interfaces()
11 # Returns a list of interfaces brought up at boot time
12 sub boot_interfaces
13 {
14 local(@rv, $f);
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!
20                 local (%conf, $b);
21                 $b->{'mac'} = $1;
22                 local ($a) = grep { lc($_->{'ether'}) eq lc($b->{'mac'}) }
23                                   @active;
24                 next if (!$a);
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');
29                 local $pfx;
30                 if ($conf{'IPADDR'} =~ /^(\S+)\/(\d+)$/) {
31                         $b->{'address'} = $1;
32                         $pfx = $2;
33                         }
34                 else {
35                         $b->{'address'} = $conf{'IPADDR'};
36                         }
37                 $pfx = $conf{'PREFIXLEN'} if (!$pfx);
38                 if ($pfx) {
39                         $b->{'netmask'} = &prefix_to_mask($pfx);
40                         }
41                 else {
42                         $b->{'netmask'} = $conf{'NETMASK'};
43                         }
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";
50                 push(@rv, $b);
51                 }
52         elsif ($f =~ /^ifcfg-([a-z0-9:\.]+)$/) {
53                 # A normal interface file
54                 local (%conf, $b);
55                 $b->{'fullname'} = $1;
56                 &read_env_file("$net_scripts_dir/$f", \%conf);
57                 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
58                         $b->{'name'} = $1;
59                         $b->{'virtual'} = $2;
60                         }
61                 else { $b->{'name'} = $b->{'fullname'}; }
62                 $b->{'up'} = ($conf{'STARTMODE'} eq 'onboot');
63                 local $pfx;
64                 if ($conf{'IPADDR'} =~ /^(\S+)\/(\d+)$/) {
65                         $b->{'address'} = $1;
66                         $pfx = $2;
67                         }
68                 else {
69                         $b->{'address'} = $conf{'IPADDR'};
70                         }
71                 $pfx = $conf{'PREFIXLEN'} if (!$pfx);
72                 if ($pfx) {
73                         $b->{'netmask'} = &prefix_to_mask($pfx);
74                         }
75                 else {
76                         $b->{'netmask'} = $conf{'NETMASK'};
77                         }
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";
84                 push(@rv, $b);
85                 }
86         }
87 closedir(CONF);
88 return @rv;
89 }
90
91 # save_interface(&details)
92 # Create or update a boot-time interface
93 sub save_interface
94 {
95 local(%conf);
96 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
97                                        : $_[0]->{'name'};
98 local $file = $_[0]->{'file'} || "$net_scripts_dir/ifcfg-$name";
99 &lock_file($file);
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;
111         }
112 else {
113         $conf{'NETWORK'} = '';
114         }
115 delete($conf{'PREFIXLEN'});
116 $conf{'BROADCAST'} = $_[0]->{'broadcast'};
117 $conf{'STARTMODE'} = $_[0]->{'up'} ? "onboot" :
118                      $conf{'STARTMODE'} eq "onboot" ? "manual" :
119                                                       $conf{'STARTMODE'};
120 $conf{'BOOTPROTO'} = $_[0]->{'dhcp'} ? "dhcp" : "static";
121 $conf{'MTU'} = $_[0]->{'mtu'};
122 $conf{'UNIQUE'} ||= time();
123 &write_env_file($file, \%conf);
124 &unlock_file($file);
125 }
126
127 # delete_interface(&details)
128 # Delete a boot-time interface
129 sub delete_interface
130 {
131 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
132                                        : $_[0]->{'name'};
133 local $file = $_[0]->{'file'} || "$net_scripts_dir/ifcfg-$name";
134 &unlink_logged($file);
135 }
136
137 # can_edit(what)
138 # Can some boot-time interface parameter be edited?
139 sub can_edit
140 {
141 return $_[0] ne "bootp";
142 }
143
144 # valid_boot_address(address)
145 # Is some address valid for a bootup interface
146 sub valid_boot_address
147 {
148 return &check_ipaddress($_[0]);
149 }
150
151 # get_hostname()
152 sub get_hostname
153 {
154 local $hn = &read_file_contents("/etc/HOSTNAME");
155 $hn =~ s/\r|\n//g;
156 if ($hn) {
157         return $hn;
158         }
159 return &get_system_hostname(1);
160 }
161
162 # save_hostname(name)
163 sub save_hostname
164 {
165 local %conf;
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
171 }
172
173 sub routing_config_files
174 {
175 return ( $routes_config, $sysctl_config );
176 }
177
178 # get_routes_config()
179 # Returns the list of save static routes
180 sub get_routes_config
181 {
182 local (@routes);
183 &open_readfile(ROUTES, $routes_config);
184 while(<ROUTES>) {
185         s/#.*$//;
186         s/\r|\n//g;
187         local @r = map { $_ eq '-' ? undef : $_ } split(/\s+/, $_);
188         push(@routes, \@r) if (@r);
189         }
190 close(ROUTES);
191 return @routes;
192 }
193
194 # save_routes_config(&routes)
195 sub save_routes_config
196 {
197 &open_tempfile(ROUTES, ">$routes_config");
198 foreach $r (@{$_[0]}) {
199         &print_tempfile(ROUTES, join(" ",
200                 $r->[0] || "-",
201                 $r->[1] || "-",
202                 $r->[2] || "-",
203                 $r->[3] || "-"),"\n");
204         }
205 &close_tempfile(ROUTES);
206 }
207
208 sub routing_input
209 {
210 local @routes = &get_routes_config();
211
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'}));
216
217 print &ui_table_row($text{'routes_device2'},
218         &ui_opt_textbox("gatewaydev", $def->[3], 6, $text{'routes_none'}));
219
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'));
224
225 # show static network routes
226 my $i = 0;
227 my @table;
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) ]);
235         }
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));
241 }
242
243 sub parse_routing
244 {
245 # Parse route inputs
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'};
255                 }
256         push(@routes, \@def);
257         }
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"} ] );
272         }
273
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");
280                 }
281         }
282 &flush_file_lines();
283 }
284
285 # get_default_gateway()
286 # Returns the default gateway IP (if one is set) and device (if set) boot time
287 # settings.
288 sub get_default_gateway
289 {
290 local @routes = &get_routes_config();
291 local ($def) = grep { $_->[0] eq "default" } @routes;
292 if ($def) {
293         return ( $def->[1], $def->[3] );
294         }
295 else {
296         return ( );
297         }
298 }
299
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
304 {
305 local @routes = &get_routes_config();
306 local ($def) = grep { $_->[0] eq "default" } @routes;
307 if ($def && $_[0]) {
308         $def->[1] = $_[0];
309         $def->[3] = $_[1];
310         }
311 elsif ($def && !$_[0]) {
312         @routes = grep { $_ ne $def } @routes;
313         }
314 elsif (!$def && $_[0]) {
315         splice(@routes, 0, 0, [ "default", $_[0], undef, $_[1] ]);
316         }
317 &save_routes_config(\@routes);
318 }
319
320 sub os_feedback_files
321 {
322 opendir(DIR, $net_scripts_dir);
323 local @f = readdir(DIR);
324 closedir(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" );
328 }
329
330 # apply_network()
331 # Apply the interface and routing settings
332 sub apply_network
333 {
334 &system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) >/dev/null 2>&1");
335 }
336
337 # supports_address6([&iface])
338 # Returns 1 if managing IPv6 interfaces is supported
339 sub supports_address6
340 {
341 local ($iface) = @_;
342 return 0;
343 }
344
345 1;
346