Handle hostnames with upper-case letters
[webmin.git] / tcpwrappers / edit_rule.cgi
1 #!/usr/local/bin/perl
2 # Creating or editing rule
3
4 require './tcpwrappers-lib.pl';
5 &ReadParse();
6 $type = $in{'allow'} ? 'allow' : 'deny';
7
8 @xservices = &list_services();
9 unshift @xservices, "ALL" if (@xservices);
10
11 if ($in{'new'}) {
12     &ui_print_header(undef, $text{'edit_title1'.$type}, "", "edit_rule");
13 } else {
14     &ui_print_header(undef, $text{'edit_title2'.$type}, "", "edit_rule");
15
16     # Get the rule
17     @rules = &list_rules($config{'hosts_'.$type});
18     ($rule) = grep { $_->{'id'} == $in{'id'} } @rules;
19     $rule || &error($text{'edit_eid'});
20
21     # parse services (daemons)
22     if ($rule->{'service'} =~ /^(.+) EXCEPT (.*)$/) {
23         @services = split /,\s?|\s+/, $1;
24         @eservices = split /,\s?|\s+/, $2;
25     } else {
26         @services = split /,\s?|\s+/, $rule->{'service'};
27     } 
28
29     if (@xservices) {
30         # try to find all services (daemons) in xinetd/inetd
31                 foreach my $rule_service (@services, @eservices) {
32                     $found = 0;
33                 foreach my $xinet_service (@xservices) { $found = 1 if ($rule_service eq $xinet_service); }
34                     unless ($found) {
35                         # not found -> let user to edit custom service
36                         @xservices = ();
37                     }
38                 }
39     }
40     # parse hosts
41     if ($rule->{'host'} =~ /^(.+) EXCEPT (.*)$/) {
42         $hosts = $1;
43         $ehosts = $2
44     } else {
45         $hosts = $rule->{'host'};
46     } 
47 }
48
49 # Form header
50 print &ui_form_start("save_rule.cgi", "post");
51 print &ui_hidden("new", $in{'new'}),"\n";
52 print &ui_hidden("id", $in{'id'}),"\n";
53 print &ui_hidden($in{'allow'} ? 'allow' : 'deny', 1),"\n";
54 print &ui_table_start($text{'edit_header'}, "", 2);
55
56 # Services
57 if (@xservices && $config{'inetd_services'}) {
58         # listed from (x)inetd
59         print &ui_table_row($text{'edit_service'},
60                 &ui_select("service", \@services, \@xservices, 5, 1));
61         print &ui_table_row($text{'edit_except'},
62                 &ui_select("service_except", \@eservices, \@xservices, 5, 1));
63         }
64 else {
65         print &ui_table_row($text{'edit_service'},
66                 &ui_textbox("service_custom", join(",",@services), 40));
67         print &ui_table_row($text{'edit_except'},
68                 &ui_textbox("service_except_custom", join(",",@eservices), 40));    
69         }
70
71 print &ui_table_hr();
72
73 # Hosts
74 @wildcards = ("ALL","KNOWN","UNKNOWN","LOCAL","PARANOID");
75 $found = '';
76 foreach my $w (@wildcards) {
77     $found = $w if ($w eq $hosts);
78 }
79 print &ui_table_row($text{'edit_hosts'},
80         &ui_opt_textbox("host_text", ($found ? "" : $hosts), 41,
81                 &ui_select("host_select", $found, \@wildcards)), 3);
82 print &ui_table_row($text{'edit_hostsexcept'},
83         &ui_textbox("host_except", $ehosts, 50), 3);
84
85 print &ui_table_hr();
86
87 # Shell commands
88 @directives = ('none', 'spawn', 'twist');
89 @cmds = split /:/, $rule->{'cmd'} if (!$in{'new'});
90 $label = $text{'edit_cmd'};
91 for ($i = 0; $i <= $#cmds; $i++) {
92     $cmds[$i] =~ s/^\s*//;
93     my $choosed = $cmds[$i] =~ /^(spawn|twist)/ ? $1 : 'none';
94     $cmds[$i] =~ s/^\s*${choosed}\s*// if ($cmds[$i] =~ /^\s*(spawn)|(twist)/); 
95     print &ui_table_row($label, &ui_select("cmd_directive_$i", $choosed, \@directives).' '.&ui_textbox("cmd_$i", $cmds[$i], 50), 3);
96     $label = "";
97 }
98
99 # Row for new command
100 print &ui_table_row($label, &ui_select("cmd_directive_$i", undef, \@directives).' '.&ui_textbox("cmd_$i", "", 50), 3);
101 print &ui_hidden("cmd_count", $i),"\n";
102
103 # Form footer
104 print &ui_table_end();
105 print &ui_form_end([
106         $in{'new'} ? ( [ "create", $text{'create'} ] )
107                    : ( [ "save", $text{'save'} ],
108                        [ "delete", $text{'delete'} ] ) ]);
109
110 &ui_print_footer("index.cgi?type=$type", $text{'index_return'});