Handle hostnames with upper-case letters
[webmin.git] / net / save_host.cgi
1 #!/usr/local/bin/perl
2 # save_host.cgi
3 # Create, update or delete a host address
4
5 require './net-lib.pl';
6 $access{'hosts'} == 2 || &error($text{'hosts_ecannot'});
7 &ReadParse();
8 &lock_file($config{'hosts_file'});
9 @hosts = &list_hosts();
10 if ($in{'delete'}) {
11         # deleting a host
12         $host = $hosts[$in{'idx'}];
13         &delete_host($host);
14         }
15 else {
16         # saving or updating a host
17         $whatfailed = "Failed to save host";
18         &check_ipaddress_any($in{'address'}) ||
19                 &error("'$in{'address'}' is not a valid IP address");
20         @h = split(/\s+/, $in{'hosts'});
21         foreach $h (@h) {
22                 $h =~ /^[A-z0-9\-\.]+$/ ||
23                         &error("'$h' is not a valid hostname");
24                 }
25         @h>0 || &error("You must enter at least one hostname");
26         if ($in{'new'}) {
27                 # saving a host
28                 $host = { 'address' => $in{'address'},
29                           'hosts' => \@h };
30                 &create_host($host);
31                 }
32         else {
33                 # updating a host
34                 $host = $hosts[$in{'idx'}];
35                 $host->{'address'} = $in{'address'};
36                 $host->{'hosts'} = \@h;
37                 &modify_host($host);
38                 }
39         }
40 &unlock_file($config{'hosts_file'});
41 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
42             'host', $host->{'address'}, $host);
43 &redirect("list_hosts.cgi");
44