Handle hostnames with upper-case letters
[webmin.git] / squid / save_cache_host.cgi
1 #!/usr/local/bin/perl
2 # save_cache_host.cgi
3 # Save, create or delete a cache_host directive
4
5 require './squid-lib.pl';
6 $access{'othercaches'} || &error($text{'eicp_ecannot'});
7 &ReadParse();
8 &lock_file($config{'squid_conf'});
9 $conf = &get_config();
10 $cache_host = $squid_version >= 2 ? "cache_peer" : "cache_host";
11 @ch = &find_config($cache_host, $conf);
12 @chd = &find_config($cache_host."_domain", $conf);
13
14 $dom = $ch[$in{'num'}]->{'values'}->[0];
15 if ($in{'delete'}) {
16         # delete some directive
17         $dir = $ch[$in{'num'}];
18         splice(@ch, $in{'num'}, 1);
19
20         # delete any cache_host directives as well
21         for($i=0; $i<@chd; $i++) {
22                 if ($chd[$i]->{'values'}->[0] eq $dom) {
23                         splice(@chd, $i--, 1);
24                         }
25                 }
26         }
27 else {
28         # validate inputs
29         $whatfailed = $text{'schost_ftsc'};
30         &to_ipaddress($in{'host'}) || &to_ip6address($in{'host'}) ||
31                 &error(&text('schost_emsg1',$in{'host'}));
32         $in{'proxy'} =~ /^\d+$/ ||
33                 &error(&text('schost_emsg2',$in{'proxy'}));
34         $in{'icp'} =~ /^\d+$/ ||
35                 &error(&text('schost_emsg3',$in{'icp'}));
36         $in{'ttl'} =~ /^\d*$/ ||
37                 &error(&text('schost_emsg4',$in{'ttl'}));
38         $in{'weight'} =~ /^\d*$/ ||
39                 &error(&text('schost_emsg5',$in{'weight'}));
40
41         @vals = ($in{'host'}, $in{'type'}, $in{'proxy'}, $in{'icp'});
42         @optlist = ("proxy-only", "weight", "ttl", "no-query", "default",
43                     "round-robin", "multicast-responder",
44                     "closest-only", "no-digest",        # squid 2+
45                     "no-netdb-exchange", "no-delay",
46                     "connect-timeout", "digest-url",    # squid 3+
47                     "allow-miss", "max-conn", "htcp", "forceddomain",
48                     "originserver", "ssl");
49         foreach $o (@optlist) {
50                 $def = $in{$o."_def"};
51                 if (defined($def)) {
52                         if (!$def) { push(@vals, "$o=$in{$o}"); }
53                         }
54                 elsif ($in{$o}) { push(@vals, $o); }
55                 }
56         if ($in{'login'} == 1) {
57                 push(@vals, "login=".$in{'login_user'}.":".$in{'login_pass'});
58                 }
59         elsif ($in{'login'} == 2) {
60                 push(@vals, "login=PASS");
61                 }
62         elsif ($in{'login'} == 3) {
63                 push(@vals, "login=*:".$in{'login_pass2'});
64                 }
65
66         # Add any old options that are not supported
67         if (!$in{'new'}) {
68                 %supported = map { $_, 1 } @optlist;
69                 $supported{'login'} = 1;
70                 @ech = @{$ch[$in{'num'}]->{'values'}};
71                 for($i=4; $i<@ech; $i++) {
72                         if ($ech[$i] =~ /^(\S+)=(\S+)$/ ||
73                             $ech[$i] =~ /^(\S+)$/) {
74                                 if (!$supported{$1}) {
75                                         push(@vals, $ech[$i]);
76                                         }
77                                 }
78                         }
79                 }
80
81         $dir = { 'name' => $cache_host, 'values' => \@vals };
82         @chdl = split(/\s+/, $in{'doq'});
83         foreach $dontq (split(/\s+/, $in{'dontq'})) { push(@chdl, "!$dontq"); }
84         if ($in{'new'}) {
85                 # adding a new cache_host and domains
86                 push(@ch, $dir);
87                 if (@chdl) {
88                         push(@chd, { 'name' => $cache_host."_domain",
89                                      'values' => [ $vals[0], @chdl ] });
90                         }
91                 }
92         else {
93                 # updating existing cache_host and domains
94                 $ch[$in{'num'}] = $dir;
95                 if (@chdl) {
96                         # replace the first cache_host_domain directive
97                         # for this host and remove the rest
98                         $newv = { 'name' => $cache_host."_domain",
99                                   'values' => [ $vals[0], @chdl ] };
100                         for($i=0; $i<@chd; $i++) {
101                                 if ($chd[$i]->{'values'}->[0] eq $dom) {
102                                         if ($already) {
103                                                 splice(@chd, $i--, 1);
104                                                 }
105                                         else {
106                                                 $chd[$i] = $newv;
107                                                 $already++;
108                                                 }
109                                         }
110                                 }
111                         if (!$already) { push(@chd, $newv); }
112                         }
113                 elsif (@chd) {
114                         # remove all the old cache_host_domain directives
115                         # for this host
116                         @chd = grep { $_->{'values'}->[0] ne $dom } @chd;
117                         }
118                 }
119         }
120 &save_directive($conf, $cache_host, \@ch);
121 &save_directive($conf, $cache_host."_domain", \@chd);
122 &flush_file_lines();
123 &unlock_file($config{'squid_conf'});
124 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
125             "host", $dir->{'values'}->[0], \%in);
126 &redirect("edit_icp.cgi");
127