Handle hostnames with upper-case letters
[webmin.git] / webmin / change_bind.cgi
1 #!/usr/local/bin/perl
2 # change_bind.cgi
3 # Update the binding IP address and port for miniserv
4
5 require './webmin-lib.pl';
6 &ReadParse();
7 &get_miniserv_config(\%miniserv);
8 %oldminiserv = %miniserv;
9 &error_setup($text{'bind_err'});
10
11 # Validate inputs
12 for($i=0; defined($in{"ip_def_$i"}); $i++) {
13         next if (!$in{"ip_def_$i"});
14         if ($in{"ip_def_$i"} == 1) {
15                 $ip = "*";
16                 }
17         else {
18                 $ip = $in{"ip_$i"};
19                 &check_ipaddress($ip) ||
20                     $in{'ipv6'} && &check_ip6address($ip) ||
21                         &error(&text('bind_eip2', $ip));
22                 }
23         if ($in{"port_def_$i"} == 1) {
24                 $port = $in{"port_$i"};
25                 $port =~ /^\d+$/ && $port < 65536 ||
26                         &error(&text('bind_eport2', $port));
27                 }
28         else {
29                 $port = "*";
30                 }
31         push(@sockets, [ $ip, $port ]);
32         push(@ports, $port) if ($port && $port ne "*");
33         }
34 @sockets || &error($text{'bind_enone'});
35 $in{'listen_def'} || $in{'listen'} =~ /^\d+$/ || &error($text{'bind_elisten'});
36 $in{'hostname_def'} || $in{'hostname'} =~ /^[a-z0-9\.\-]+$/i ||
37         &error($text{'bind_ehostname'});
38 if ($in{'ipv6'}) {
39         eval "use Socket6";
40         $@ && &error(&text('bind_eipv6', "<tt>Socket6</tt>"));
41         }
42
43 # Update config file
44 &lock_file($ENV{'MINISERV_CONFIG'});
45 $first = shift(@sockets);
46 $miniserv{'port'} = $first->[1];
47 if ($first->[0] eq "*") {
48         delete($miniserv{'bind'});
49         }
50 else {
51         $miniserv{'bind'} = $first->[0];
52         }
53 $miniserv{'sockets'} = join(" ", map { "$_->[0]:$_->[1]" } @sockets);
54 $miniserv{'ipv6'} = $in{'ipv6'};
55 if ($in{'listen_def'}) {
56         delete($miniserv{'listen'});
57         }
58 else {
59         $miniserv{'listen'} = $in{'listen'};
60         }
61 if ($in{'hostname_def'}) {
62         delete($miniserv{'host'});
63         }
64 else {
65         $miniserv{'host'} = $in{'hostname'};
66         }
67 $miniserv{'no_resolv_myname'} = $in{'no_resolv_myname'};
68 &put_miniserv_config(\%miniserv);
69 &unlock_file($ENV{'MINISERV_CONFIG'});
70
71 # Attempt to re-start miniserv
72 $SIG{'TERM'} = 'ignore';
73 &system_logged("$config_directory/stop >/dev/null 2>&1 </dev/null");
74 $temp = &transname();
75 $rv = &system_logged("$config_directory/start >$temp 2>&1 </dev/null");
76 $out = `cat $temp`;
77 $out =~ s/^Starting Webmin server in.*\n//;
78 $out =~ s/at.*line.*//;
79 unlink($temp);
80 if ($rv) {
81         # Failed! Roll back config and start again
82         &lock_file($ENV{'MINISERV_CONFIG'});
83         &put_miniserv_config(\%oldminiserv);
84         &unlock_file($ENV{'MINISERV_CONFIG'});
85         &system_logged("$config_directory/start >/dev/null 2>&1 </dev/null");
86         &error(&text('bind_erestart', $out));
87         }
88
89 # If possible, open the new ports
90 if (&foreign_check("firewall") && $in{'firewall'}) {
91         @oldports = split(/\s+/, $in{'oldports'});
92         @newports = &unique(grep { &indexof($_, @oldports) < 0 } @ports);
93         if (@newports) {
94                 &clean_environment();
95                 $ENV{'WEBMIN_CONFIG'} = $config_directory;
96                 &system_logged(&module_root_directory("firewall").
97                                "/open-ports.pl ".
98                                join(" ", map { $_.":".($_+10) } @newports).
99                                " >/dev/null 2>&1");
100                 &reset_environment();
101                 }
102         }
103
104 &webmin_log("bind", undef, undef, \%in);
105
106 # Work out redirect URL
107 if ($miniserv{'musthost'}) { $miniserv{'musthost'}; }
108 elsif ($miniserv{'bind'}) { $url = $miniserv{'bind'}; }
109 else { $url = $ENV{'SERVER_NAME'}; }
110 if ($ENV{'HTTPS'} eq "ON") { $url = "https://$url"; }
111 else { $url = "http://$url"; }
112
113 if ($tconfig{'inframe'}) {
114         # Theme uses frames, so we need to redirect the whole frameset
115         $url .= ":$miniserv{'port'}";
116         &ui_print_header(undef, $text{'bind_title'}, "");
117         print $text{'bind_redirecting'},"<p>\n";
118         print "<script>\n";
119         print "top.location = '$url';\n";
120         print "</script>\n";
121         &ui_print_footer("", $text{'index_return'});
122         }
123 else {
124         $url .= ":$miniserv{'port'}/webmin/";
125         &redirect($url);
126         }
127