Handle hostnames with upper-case letters
[webmin.git] / net / redhat-linux-lib.pl
1 # redhat-linux-lib.pl
2 # Networking functions for redhat linux
3
4 if ($gconfig{'os_type'} eq 'openmamba-linux') {
5         # OpenMamba Linux
6         $net_scripts_dir = "/etc/sysconfig/network-devices";
7         $devices_dir = "/etc/sysconfig/network-devices";
8         }
9 else {
10         # Redhat, Mandrake, etc..
11         $net_scripts_dir = "/etc/sysconfig/network-scripts";
12         $devices_dir = "/etc/sysconfig/networking/devices";
13         }
14 $network_config = "/etc/sysconfig/network";
15 $static_route_config = "/etc/sysconfig/static-routes";
16 $sysctl_config = "/etc/sysctl.conf";
17
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');
26
27 # Redhat 8.0+ and Mandrake 9.1+ have a separate file for static routes for
28 # each interface
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');
36
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);
42
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);
50
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);
56
57 do 'linux-lib.pl';
58
59 # boot_interfaces()
60 # Returns a list of interfaces brought up at boot time
61 sub boot_interfaces
62 {
63 local (@rv, $f);
64 local %bridge_map;
65 opendir(CONF, &translate_filename($net_scripts_dir));
66 while($f = readdir(CONF)) {
67         local (%conf, $b);
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";
72                 $b->{'name'} = $1;
73                 $b->{'range'} = $2;
74                 $b->{'start'} = $conf{'IPADDR_START'};
75                 $b->{'end'} = $conf{'IPADDR_END'};
76                 $b->{'num'} = $conf{'CLONENUM_START'};
77                 $b->{'up'} = 1;
78                 $b->{'edit'} = 1;
79                 $b->{'desc'} = $conf{'NAME'};
80                 $b->{'index'} = scalar(@rv);
81                 $b->{'file'} = "$net_scripts_dir/$f";
82                 push(@rv, $b);
83                 }
84         elsif ($f !~ /\.(bak|old)$/i && $f =~ /^ifcfg-([a-z0-9:\.]+)$/) {
85                 # Normal interface
86                 &read_env_file("$net_scripts_dir/$f", \%conf);
87                 $b->{'fullname'} = $conf{'DEVICE'} || $1;
88                 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
89                         $b->{'name'} = $1;
90                         $b->{'virtual'} = $2;
91                         }
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'},
102                                                                $b->{'netmask'});
103                         }
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');
110                 local @ip6s;
111                 push(@ip6s, [ split(/\//, $conf{'IPV6ADDR'}) ])
112                         if ($conf{'IPV6ADDR'});
113                 push(@ip6s, map { [ split(/\//, $_) ] }
114                                 split(/\s+/, $conf{'IPV6ADDR_SECONDARIES'}));
115                 if (@ip6s) {
116                         # Static IPv6 addresses
117                         $b->{'address6'} = [ map { $_->[0] } @ip6s ];
118                         $b->{'netmask6'} = [ map { $_->[1] } @ip6s ];
119                         }
120                 elsif (lc($conf{'IPV6INIT'}) eq 'yes') {
121                         $b->{'auto6'} = 1;
122                         }
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'};
129                         }
130                 push(@rv, $b);
131                 }
132         }
133 closedir(CONF);
134 foreach my $b (@rv) {
135         if ($b->{'fullname'} =~ /^br\d+$/) {
136                 $b->{'bridge'} = 1;
137                 $b->{'bridgeto'} = $bridge_map{$b->{'fullname'}};
138                 }
139         }
140 return @rv;
141 }
142
143 # save_interface(&details)
144 # Create or update a boot-time interface
145 sub save_interface
146 {
147 local(%conf);
148 local $name = $_[0]->{'range'} ne "" ? $_[0]->{'name'}."-range".
149                                        $_[0]->{'range'} :
150               $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
151                                        : $_[0]->{'name'};
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'};
159         }
160 else {
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'},
167                                                     $_[0]->{'netmask'});
168                 }
169         else {
170                 $conf{'NETWORK'} = '';
171                 }
172         $conf{'BROADCAST'} = $_[0]->{'broadcast'};
173         if ($_[0]->{'gateway'}) {
174                 $conf{'GATEWAY'} = $_[0]->{'gateway'};
175                 }
176         else {
177                 delete($conf{'GATEWAY'});
178                 }
179         if ($_[0]->{'gateway6'}) {
180                 $conf{'IPV6_DEFAULTGW'} = $_[0]->{'gateway6'};
181                 }
182         else {
183                 delete($conf{'IPV6_DEFAULTGW'});
184                 }
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'});
194         local @ip6s;
195         for(my $i=0; $i<@{$b->{'address6'}}; $i++) {
196                 push(@ip6s, $b->{'address6'}->[$i]."/".
197                             $b->{'netmask6'}->[$i]);
198                 }
199         if ((@ip6s || $b->{'auto6'}) && lc($conf{'IPV6INIT'}) ne 'yes') {
200                 $conf{'IPV6INIT'} = 'yes';
201                 }
202         elsif (!@ip6s && !$b->{'auto6'}) {
203                 $conf{'IPV6INIT'} = 'no';
204                 }
205         if (@ip6s) {
206                 $conf{'IPV6ADDR'} = shift(@ip6s);
207                 $conf{'IPV6ADDR_SECONDARIES'} = join(" ", @ip6s);
208                 }
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';
214                 }
215         }
216 $conf{'NAME'} = $_[0]->{'desc'};
217 &write_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
218
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*")) {
222                 local %bconf;
223                 &lock_file($efile);
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);
229                         }
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);
234                         }
235                 &unlock_file($efile);
236                 }
237         }
238
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");
243         }
244 &unlock_file("$net_scripts_dir/ifcfg-$name");
245
246 # Make sure IPv6 is enabled globally
247 if (@{$b->{'address6'}}) {
248         local %conf;
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);
254                 }
255         &unlock_file($network_config);
256         }
257 }
258
259 # delete_interface(&details)
260 # Delete a boot-time interface
261 sub delete_interface
262 {
263 local $name = $_[0]->{'range'} ne "" ? $_[0]->{'name'}."-range".
264                                        $_[0]->{'range'} :
265               $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
266                                        : $_[0]->{'name'};
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");
271         }
272 &unlock_file("$net_scripts_dir/ifcfg-$name");
273 }
274
275 # can_edit(what)
276 # Can some boot-time interface parameter be edited?
277 sub can_edit
278 {
279 if ($supports_mtu) {
280         return 1;
281         }
282 else {
283         return $_[0] ne "mtu";
284         }
285 }
286
287 # valid_boot_address(address)
288 # Is some address valid for a bootup interface
289 sub valid_boot_address
290 {
291 return &check_ipaddress($_[0]);
292 }
293
294 # get_hostname()
295 sub get_hostname
296 {
297 local %conf;
298 &read_env_file($network_config, \%conf);
299 if ($conf{'HOSTNAME'}) {
300         return $conf{'HOSTNAME'};
301         }
302 return &get_system_hostname(1);
303 }
304
305 # save_hostname(name)
306 sub save_hostname
307 {
308 local $old = &get_hostname();
309 local %conf;
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);
319
320 # If any ifcfg-XXX files have the old hostname in DHCP_HOSTNAME, fix it
321 foreach my $b (&boot_interfaces()) {
322         local %ifc;
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'});
329                 }
330         }
331 undef(@main::get_system_hostname);      # clear cache
332 }
333
334 # get_domainname()
335 sub get_domainname
336 {
337 local $d;
338 &execute_command("domainname", undef, \$d, undef);
339 chop($d);
340 return $d;
341 }
342
343 # save_domainname(domain)
344 sub save_domainname
345 {
346 local %conf;
347 &execute_command("domainname ".quotemeta($_[0]));
348 &read_env_file($network_config, \%conf);
349 if ($_[0]) {
350         $conf{'NISDOMAIN'} = $_[0];
351         }
352 else {
353         delete($conf{'NISDOMAIN'});
354         }
355 &write_env_file($network_config, \%conf);
356 }
357
358 sub routing_config_files
359 {
360 local @rv = ( $network_config, $sysctl_config );
361 if (!$supports_dev_routes) {
362         push(@rv, $static_route_config);
363         }
364 else {
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");
371                                 }
372                         }
373                 closedir(DIR);
374                 }
375         }
376 return @rv;
377 }
378
379 sub network_config_files
380 {
381 return ( "/etc/HOSTNAME", $network_config );
382 }
383
384 sub routing_input
385 {
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'}));
393
394         print &ui_table_row($text{'routes_device2'},
395                 &ui_opt_textbox("gatewaydev", $conf{'GATEWAYDEV'}, 6,
396                                 $text{'routes_none'}));
397         }
398 else {
399         # multiple default routers can exist, one per interface
400         my @table;
401         local $r = 0;
402         if ($conf{'GATEWAY'} || $conf{'IPV6_DEFAULTGW'}) {
403                 push(@table, [
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),
409                     ]);
410                 $r++;
411                 }
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),
417                              ]);
418                 $r++;
419                 }
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'},
424                 &ui_columns_table(
425                         [ $text{'routes_ifc'}, $text{'routes_gateway'},
426                           $text{'routes_gateway6'} ],
427                         undef, \@table, undef, 1));
428         }
429
430 # show routing
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));
435         }
436 else {
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));
441         }
442
443 if (!$supports_dev_routes) {
444         # get static routes from single file
445         &open_readfile(STATIC, $static_route_config);
446         while(<STATIC>) {
447                 if (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)\s+gw\s+(\S+)/) {
448                         push(@st, [ $1, $2, $3, $4 ]);
449                         }
450                 elsif (/(\S+)\s+host\s+(\S+)\s+gw\s+(\S+)/) {
451                         push(@st, [ $1, $2, '255.255.255.255', $3 ]);
452                         }
453                 elsif (/(\S+)\s+net\s+(\S+)\s+netmask\s+(\S+)/) {
454                         push(@hr, [ $1, $2, $3 ]);
455                         }
456                 elsif (/(\S+)\s+host\s+(\S+)/) {
457                         push(@hr, [ $1, $2, '255.255.255.255' ]);
458                         }
459                 }
460         close(STATIC);
461         }
462 else {
463         # get static routes from per-interface files
464         local $f;
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+)?)$/) {
469                         local $dev = $1;
470                         local (%rfile, $i);
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"},
475                                                           $rfile{"NETMASK$i"},
476                                                           $rfile{"GATEWAY$i"}]);
477                                         }
478                                 else {
479                                         push(@hr, [ $dev, $rfile{"ADDRESS$i"},
480                                                           $rfile{"NETMASK$i"} ||
481                                                           "255.255.255.255" ]);
482                                         }
483                                 }
484                         }
485                 }
486         closedir(DIR);
487         }
488
489 # Show static network routes
490 my @table;
491 for($i=0; $i<=@st; $i++) {
492         local $st = $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) ]);
497         }
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));
502
503 # Show static host routes
504 my @table;
505 for($i=0; $i<=@hr; $i++) {
506         local $st = $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) ]);
510         }
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));
515 }
516
517 sub parse_routing
518 {
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'}));
527                 }
528         else { $conf{'GATEWAY'} = $in{'gateway'}; }
529
530         if ($in{'gatewaydev_def'}) { delete($conf{'GATEWAYDEV'}); }
531         elsif ($in{'gatewaydev'} !~ /^\S+$/) {
532                 &error(&text('routes_edevice', $in{'gatewaydev'}));
533                 }
534         else { $conf{'GATEWAYDEV'} = $in{'gatewaydev'}; }
535         }
536 else {
537         # Multiple defaults can be specified!
538         local ($r, $b);
539         @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
540         foreach $b (@boot) {
541                 delete($b->{'gateway'});
542                 }
543         delete($conf{'GATEWAY'});
544         delete($conf{'GATEWAYDEV'});
545         delete($conf{'IPV6_DEFAULTDEV'});
546         delete($conf{'IPV6_DEFAULTGW'});
547
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 "*") {
553                         # For any interface
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"};
559                         }
560                 else {
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"};
568                         }
569                 }
570         }
571
572 if ($gconfig{'os_version'} < 7.0) {
573         if ($in{'forward'}) { $conf{'FORWARD_IPV4'} = 'yes'; }
574         else { $conf{'FORWARD_IPV4'} = 'no'; }
575         }
576 else {
577         &lock_file($sysctl_config);
578         &read_env_file($sysctl_config, \%sysctl);
579         $sysctl{'net.ipv4.ip_forward'} = $in{'forward'};
580         }
581
582 # Parse static and local routes
583 for($i=0; defined($dev = $in{"dev_$i"}); $i++) {
584         next if (!$dev);
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");
592                 }
593         else {
594                 push(@st, "$dev net $net netmask $netmask gw $gw\n");
595                 }
596         push(@{$st{$dev}}, [ $net, $netmask, $gw ]);
597         }
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");
608                 }
609         else {
610                 push(@st, "$dev net $net netmask $netmask\n");
611                 }
612         push(@{$st{$dev}}, [ $net, $netmask ]);
613         }
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);
619         }
620 else {
621         # Write to one file per interface (delete old, then save new/updated)
622         local $f;
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");
629                         }
630                 }
631         closedir(DIR);
632         foreach $dev (keys %st) {
633                 $f = $supports_route_dev ? "route-$dev" : "$dev.route";
634                 local (%rfile, $i);
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];
639                         }
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");
646                 }
647         }
648 &write_env_file($network_config, \%conf);
649 &unlock_file($network_config);
650 if (%sysctl) {
651         &write_env_file($sysctl_config, \%sysctl);
652         &unlock_file($sysctl_config);
653         }
654 if (@boot) {
655         local $b;
656         foreach $b (@boot) {
657                 &save_interface($b);
658                 }
659         }
660 }
661
662 sub os_feedback_files
663 {
664 opendir(DIR, $net_scripts_dir);
665 local @f = readdir(DIR);
666 closedir(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" );
670 }
671
672 # interface_sel(name, value)
673 # Returns a menu for all boot-time interfaces
674 sub interface_sel
675 {
676 local $rv = "<select name=$_[0]>\n";
677 $rv .= sprintf "<option value='' %s>&nbsp;\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'};
686         }
687 $rv .= "</select>\n";
688 return $rv;
689 }
690
691 # apply_network()
692 # Apply the interface and routing settings
693 sub apply_network
694 {
695 &system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) >/dev/null 2>&1");
696 }
697
698 # apply_interface(&iface)
699 # Calls an OS-specific function to make a boot-time interface active
700 sub apply_interface
701 {
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;
704 }
705
706 # unapply_interface(&iface)
707 # Calls an OS-specific function to make a boot-time interface inactive
708 #sub unapply_interface
709 #{
710 #local $out = &backquote_logged("cd / ; ifdown '$_[0]->{'fullname'}' 2>&1 </dev/null");
711 #return $? ? $out : undef;
712 #}
713
714 # get_default_gateway()
715 # Returns the default gateway IP (if one is set) and device (if set) boot time
716 # settings.
717 sub get_default_gateway
718 {
719 local %conf;
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'} ) : ( );
725 }
726
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
731 {
732 &lock_file($network_config);
733 &read_env_file($network_config, \%conf);
734 if (!$supports_dev_gateway) {
735         # Just update the network config file
736         local %conf;
737         if ($_[0]) {
738                 $conf{'GATEWAY'} = $_[0];
739                 $conf{'GATEWAYDEV'} = $_[1];
740                 }
741         else {
742                 delete($conf{'GATEWAY'});
743                 delete($conf{'GATEWAYDEV'});
744                 }
745         }
746 else {
747         # Set the gateway in the specified interface file, and clear the rest
748         local @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
749         foreach $b (@boot) {
750                 delete($b->{'gateway'});
751                 if ($_[0] && $b->{'fullname'} eq $_[1]) {
752                         $b->{'gateway'} = $_[0];
753                         &save_interface($b);
754                         }
755                 }
756         delete($conf{'GATEWAY'});
757         delete($conf{'GATEWAYDEV'});
758         }
759 &write_env_file($network_config, \%conf);
760 &unlock_file($network_config);
761 }
762
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
767 {
768 local %conf;
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'} ) : ( );
775 }
776
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
781 {
782 &lock_file($network_config);
783 &read_env_file($network_config, \%conf);
784 local @boot = grep { $->{'virtual'} eq '' } &boot_interfaces();
785 foreach $b (@boot) {
786         delete($b->{'gateway6'});
787         if ($_[0] && $b->{'fullname'} eq $_[1]) {
788                 $b->{'gateway6'} = $_[0];
789                 &save_interface($b);
790                 }
791         }
792 delete($conf{'IPV6_DEFAULTGW'});
793 delete($conf{'IPV6_DEFAULTDEV'});
794 &write_env_file($network_config, \%conf);
795 &unlock_file($network_config);
796 }
797
798 # supports_ranges()
799 # Returns 1 for newer redhat versions
800 sub supports_ranges
801 {
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);
808 }
809
810 # range_input([&interface])
811 # Print HTML for a IP range interface
812 sub range_input
813 {
814 local $new = !$_[0];
815
816 # Range description
817 print &ui_table_row($text{'ifcs_desc'},
818         &ui_textbox("desc", $_[0] ? $_[0]->{'desc'} : undef, 60));
819
820 # Base interface
821 my $ifaceinput;
822 if ($new) {
823         $ifaceinput = &ui_select("iface", $_[0]->{'name'},
824                 [ map { $_->{'fullname'} } grep { $b->{'virtual'} eq '' }
825                       &boot_interfaces() ]);
826         }
827 else {
828         $ifaceinput = "<tt>$_[0]->{'name'}</tt>";
829         }
830 print &ui_table_row($text{'range_iface'}, $ifaceinput);
831
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>");
836
837 # Start
838 print &ui_table_row($text{'range_start'},
839         &ui_textbox("start", $_[0]->{'start'}, 15));
840
841 # Stop
842 print &ui_table_row($text{'range_end'},
843         &ui_textbox("end", $_[0]->{'end'}, 15));
844
845 # Base number
846 print &ui_table_row($text{'range_num'},
847         &ui_textbox("num", $_[0]->{'num'}, 5));
848 }
849
850 # parse_range(&range, &in)
851 sub parse_range
852 {
853 local %in = %{$_[1]};
854 if ($in{'new'}) {
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'};
859         }
860 $_[0]->{'desc'} = $in{'desc'};
861
862 &check_ipaddress($in{'start'}) || &error($text{'range_estart'});
863 $_[0]->{'start'} = $in{'start'};
864
865 &check_ipaddress($in{'end'}) || &error($text{'range_eend'});
866 $_[0]->{'end'} = $in{'end'};
867
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'});
873
874 $in{'num'} =~ /^\d+$/ || &error($text{'range_enum'});
875 $_[0]->{'num'} = $in{'num'};
876 }
877
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
882 {
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);
888 local %eth;
889 &read_env_file($eth->{'file'}, \%eth);
890 return $eth{'DHCP_HOSTNAME'} ne &get_system_hostname();
891 }
892
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
897 {
898 }
899
900 sub boot_iface_hardware
901 {
902 return $_[0] =~ /^eth/;
903 }
904
905 # supports_address6([&iface])
906 # Returns 1 if managing IPv6 interfaces is supported
907 sub supports_address6
908 {
909 local ($iface) = @_;
910 return !$iface || $iface->{'virtual'} eq '';
911 }
912
913 # Returns 1, as boot-time interfaces on Redhat can exist without an IP (such as
914 # for bridging)
915 sub supports_no_address
916 {
917 return 1;
918 }
919
920 # Bridge interfaces can be created on redhat
921 sub supports_bridges
922 {
923 return 1;
924 }
925
926 1;
927