Handle hostnames with upper-case letters
[webmin.git] / sendmail / access-lib.pl
1 # access-lib.pl
2 # Functions for the access_db table
3
4 # access_dbm(&config)
5 # Returns the filename and type of the access database, or undef if none
6 sub access_dbm
7 {
8 foreach $f (&find_type("K", $_[0])) {
9         if ($f->{'value'} =~ /^access\s+(\S+)[^\/]+(\S+)$/) {
10                 return ($2, $1);
11                 }
12         }
13 return undef;
14 }
15
16 # access_file(&config)
17 # Returns the filename of the text access file, or undef if none
18 sub access_file
19 {
20 return &find_textfile($config{'access_file'}, &access_dbm($_[0]));
21 }
22
23 # list_access(textfile)
24 sub list_access
25 {
26 if (!scalar(@list_access_cache)) {
27         @list_access_cache = ( );
28         local $lnum = 0;
29         open(ACC, $_[0]);
30         while(<ACC>) {
31                 s/\r|\n//g;     # remove newlines
32                 if (/^\s*#+\s*(.*)/) {
33                         # A comment line
34                         $cmt = &is_table_comment($_);
35                         }
36                 elsif (/^(\S+)\s+(.*)/) {
37                         local(%acc);
38                         $acc{'from'} = $1;
39                         $acc{'action'} = $2;
40                         $acc{'line'} = $cmt ? $lnum-1 : $lnum;
41                         $acc{'eline'} = $lnum;
42                         $acc{'num'} = scalar(@list_access_cache);
43                         if ($acc{'from'} =~ /^(Connect|From|To):(.*)/i) {
44                                 $acc{'tag'} = $1;
45                                 $acc{'from'} = $2;
46                                 }
47                         $acc{'cmt'} = $cmt;
48                         push(@list_access_cache, \%acc);
49                         $cmt = undef;
50                         }
51                 else {
52                         $cmt = undef;
53                         }
54                 $lnum++;
55                 }
56         close(ACC);
57         }
58 return @list_access_cache;
59 }
60
61 # create_access(&details, textfile, dbmfile, dbmtype)
62 # Create a new access database entry
63 sub create_access
64 {
65 &list_access($_[1]);     # force cache init
66 local(%acc);
67
68 # Write to the file
69 local $lref = &read_file_lines($_[1]);
70 $_[0]->{'line'} = scalar(@$lref);
71 push(@$lref, &make_table_comment($_[0]->{'cmt'}));
72 local $from = $_[0]->{'tag'} ? "$_[0]->{'tag'}:$_[0]->{'from'}"
73                              : $_[0]->{'from'};
74 push(@$lref, "$from\t$_[0]->{'action'}");
75 $_[0]->{'eline'} = scalar(@$lref)-1;
76 &flush_file_lines($_[1]);
77
78 # Add to DBM
79 if (!&rebuild_map_cmd($_[1])) {
80         if ($_[3] eq "dbm") {
81                 dbmopen(%acc, $_[2], 0644);
82                 $acc{$from} = $_[0]->{'action'};
83                 dbmclose(%acc);
84                 }
85         else { &run_makemap($_[1], $_[2], $_[3]); }
86         }
87
88 # Add to cache
89 $_[0]->{'num'} = scalar(@list_access_cache);
90 $_[0]->{'file'} = $_[1];
91 push(@list_access_cache, $_[0]);
92 }
93
94 # delete_access(&details, textfile, dbmfile, dbmtype)
95 # Delete an existing access entry
96 sub delete_access
97 {
98 local(%acc);
99
100 # Delete form file
101 local $lref = &read_file_lines($_[1]);
102 local $len = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
103 splice(@$lref, $_[0]->{'line'}, $len);
104 &flush_file_lines($_[1]);
105
106 # Delete from DBM
107 local $oldfrom = $_[0]->{'tag'} ? "$_[0]->{'tag'}:$_[0]->{'from'}"
108                                 : $_[0]->{'from'};
109 if (!&rebuild_map_cmd($_[1])) {
110         if ($_[3] eq "dbm") {
111                 dbmopen(%acc, $_[2], 0644);
112                 delete($acc{$oldfrom});
113                 dbmclose(%acc);
114                 }
115         else { &run_makemap($_[1], $_[2], $_[3]); }
116         }
117
118 # Delete from cache
119 local $idx = &indexof($_[0], @list_access_cache);
120 splice(@list_access_cache, $idx, 1) if ($idx != -1);
121 &renumber_list(\@list_access_cache, $_[0], -$len);
122 }
123
124 # modify_access(&old, &details, textfile, dbmfile, dbmtype)
125 # Change an existing access entry
126 sub modify_access
127 {
128 local %acc;
129 local $oldfrom = $_[0]->{'tag'} ? "$_[0]->{'tag'}:$_[0]->{'from'}"
130                              : $_[0]->{'from'};
131 local $from = $_[1]->{'tag'} ? "$_[1]->{'tag'}:$_[1]->{'from'}"
132                              : $_[1]->{'from'};
133
134 # Update in file
135 local $lref = &read_file_lines($_[2]);
136 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
137 local @newlines;
138 push(@newlines, &make_table_comment($_[1]->{'cmt'}));
139 push(@newlines, "$from\t$_[1]->{'action'}");
140 splice(@$lref, $_[0]->{'line'}, $oldlen, @newlines);
141 &flush_file_lines($_[2]);
142
143 # Update DBM
144 if (!&rebuild_map_cmd($_[2])) {
145         if ($_[4] eq "dbm") {
146                 dbmopen(%virt, $_[3], 0644);
147                 delete($virt{$oldfrom});
148                 $virt{$from} = $_[1]->{'action'};
149                 dbmclose(%virt);
150                 }
151         else { &run_makemap($_[2], $_[3], $_[4]); }
152         }
153
154 # Update cache
155 local $idx = &indexof($_[0], @list_generics_cache);
156 $_[1]->{'line'} = $_[0]->{'line'};
157 $_[1]->{'eline'} = $_[1]->{'cmt'} ? $_[0]->{'line'}+1 : $_[0]->{'line'};
158 $list_generics_cache[$idx] = $_[1] if ($idx != -1);
159 &renumber_list(\@list_generics_cache, $_[0], scalar(@newlines)-$oldlen);
160 }
161
162 # access_form([&details])
163 sub access_form
164 {
165 local ($v) = @_;
166 local ($mode, $addr);
167
168 print &ui_form_start("save_access.cgi", "post");
169 if ($v) {
170         print &ui_hidden("num", $v->{'num'}),"\n";
171         }
172 else {
173         print &ui_hidden("new", 1),"\n";
174         }
175 print &ui_table_start($v ? $text{'sform_edit'} : $text{'sform_create'},
176                       undef, 2);
177
178 # Comment
179 print &ui_table_row($text{'vform_cmt'},
180                     &ui_textbox("cmt", $v->{'cmt'}, 50));
181
182 # Mail source
183 local $src = $v->{'from'} =~ /^\S+\@\S+$/ ? 0 :
184              $v->{'from'} =~ /^[0-9\.]+$/ ? 1 :
185              $v->{'from'} =~ /^\S+\@$/ ? 2 :
186              $v->{'from'} =~ /^[A-z0-9\-\.]+$/ ? 3 : 0;
187 print &ui_table_row($text{'sform_source'},
188     &ui_select("from_type", $src,
189                [ map { [ $_, $text{"sform_type$_"} ] } (0 .. 3) ])."\n".
190     &ui_textbox("from", $v->{'from'}, 25));
191
192 # Match against tag
193 local $ver = &get_sendmail_version();
194 if ($v->{'tag'} || $ver >= 8.10) {
195         print &ui_table_row($text{'sform_tag'},
196             &ui_select("tag", $v->{'tag'},
197                        [ [ "", $text{'sform_tag_'} ],
198                          [ "From", $text{'sform_tag_from'} ],
199                          [ "To", $text{'sform_tag_to'} ],
200                          [ "Connect", $text{'sform_tag_connect'} ],
201                          [ "Spam", $text{'sform_tag_spam'} ] ]));
202         }
203
204 # Action
205 local $atable = "<table>\n";
206 $atable .= "<tr>";
207 $atable .= "<td>".&ui_oneradio("action", "OK", $text{'sform_ok'},
208                        $v->{'action'} eq "OK" || !$v->{'action'})."</td>\n";
209 $atable .= "<td>".&ui_oneradio("action", "RELAY", $text{'sform_relay'},
210                                $v->{'action'} eq "RELAY")."</td>\n";
211 $atable .= "</tr>";
212 $atable .= "<tr>";
213 $atable .= "<td>".&ui_oneradio("action", "REJECT", $text{'sform_reject'},
214                                $v->{'action'} eq "REJECT")."</td>\n";
215 $atable .= "<td>".&ui_oneradio("action", "DISCARD", $text{'sform_discard'},
216                                $v->{'action'} eq "DISCARD")."</td>\n";
217 $atable .= "</tr>";
218 $atable .= "<tr>";
219 local ($err, $msg) = $v->{'action'} =~ /(\d+)\s*(.*)$/ ? ($1, $2) : ( );
220 $atable .= "<td colspan=2>".&ui_oneradio("action", 0,  $text{'sform_err'},
221                                          $err)."\n";
222 $atable .= &ui_textbox("err", $err, 4)." ".$text{'sform_msg'}."\n";
223 $atable .= &ui_textbox("msg", $msg, 20)."</td>\n";
224 $atable .= "</tr>";
225 $atable .= "</table>\n";
226 print &ui_table_row($text{'sform_action'}, $atable);
227
228 print &ui_table_end();
229 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
230                              [ "delete", $text{'delete'} ] ]
231                          : [ [ "create", $text{'create'} ] ]);
232 }
233
234 sub can_edit_access
235 {
236 local ($g) = @_;
237 return $access{'smode'} == 1 ||
238        $access{'smode'} == 2 && $g->{'from'} =~ /$access{'saddrs'}/;
239 }
240
241 1;
242