Handle hostnames with upper-case letters
[webmin.git] / sendmail / virtusers-lib.pl
1 # virtusers-lib.pl
2 # Functions for the virtusers table
3
4 # virtusers_dbm(&config)
5 # Returns the filename and type of the virtusers database, or undef if none
6 sub virtusers_dbm
7 {
8 foreach $f (&find_type("K", $_[0])) {
9         if ($f->{'value'} =~ /^virtuser\s+(\S+)[^\/]+(\S+)$/) {
10                 return ($2, $1);
11                 }
12         }
13 return undef;
14 }
15
16 # virtusers_file(&config)
17 # Returns the filename of the text virtusers file, or undef if none
18 sub virtusers_file
19 {
20 return &find_textfile($config{'virtusers_file'}, &virtusers_dbm($_[0]));
21 }
22
23 # list_virtusers(textfile)
24 sub list_virtusers
25 {
26 if (!scalar(@list_virtusers_cache)) {
27         @list_virtusers_cache = ( );
28         local $lnum = 0;
29         local $cmt;
30         open(VIRT, $_[0]);
31         while(<VIRT>) {
32                 s/\r|\n//g;     # remove newlines
33                 if (/^\s*#+\s*(.*)/) {
34                         # A comment line
35                         $cmt = &is_table_comment($_);
36                         }
37                 elsif (/^(\S+)\s+(.*)/) {
38                         # An actual virtuser line
39                         local(%virt);
40                         $virt{'from'} = $1;
41                         $virt{'to'} = $2;
42                         $virt{'line'} = $cmt ? $lnum-1 : $lnum;
43                         $virt{'eline'} = $lnum;
44                         $virt{'file'} = $_[0];
45                         $virt{'num'} = scalar(@list_virtusers_cache);
46                         $virt{'cmt'} = $cmt;
47                         push(@list_virtusers_cache, \%virt);
48                         $cmt = undef;
49                         }
50                 else {
51                         $cmt = undef;
52                         }
53                 $lnum++;
54                 }
55         close(VIRT);
56         }
57 return @list_virtusers_cache;
58 }
59
60 # create_virtuser(&details, textfile, dbmfile, dbmtype)
61 # Create a new virtuser mapping
62 sub create_virtuser
63 {
64 &list_virtusers($_[1]); # force cache init
65 local(%virt);
66
67 # Add to file
68 local $lref = &read_file_lines($_[1]);
69 $_[0]->{'line'} = scalar(@$lref);
70 push(@$lref, &make_table_comment($_[0]->{'cmt'}));
71 push(@$lref, "$_[0]->{'from'}\t$_[0]->{'to'}");
72 $_[0]->{'eline'} = scalar(@$lref)-1;
73 &flush_file_lines($_[1]);
74
75 # Add to DBM
76 if (!&rebuild_map_cmd($_[1])) {
77         if ($_[3] eq "dbm") {
78                 dbmopen(%virt, $_[2], 0644);
79                 $virt{$_[0]->{'from'}} = $_[0]->{'to'};
80                 dbmclose(%virt);
81                 }
82         else { &run_makemap($_[1], $_[2], $_[3]); }
83         }
84
85 # Add to cache
86 $_[0]->{'num'} = scalar(@list_virtusers_cache);
87 $_[0]->{'file'} = $_[1];
88 push(@list_virtusers_cache, $_[0]);
89 }
90
91 # delete_virtuser(&details, textfile, dbmfile, dbmtype)
92 # Delete an existing virtuser mapping
93 sub delete_virtuser
94 {
95 local %virt;
96
97 # Delete  from file
98 local $lref = &read_file_lines($_[1]);
99 local $len = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
100 splice(@$lref, $_[0]->{'line'}, $len);
101 &flush_file_lines($_[1]);
102
103 # Delete from DBM
104 if (!&rebuild_map_cmd($_[1])) {
105         if ($_[3] eq "dbm") {
106                 dbmopen(%virt, $_[2], 0644);
107                 delete($virt{$_[0]->{'from'}});
108                 dbmclose(%virt);
109                 }
110         else { &run_makemap($_[1], $_[2], $_[3]); }
111         }
112
113 # Delete from cache
114 local $idx = &indexof($_[0], @list_virtusers_cache);
115 splice(@list_virtusers_cache, $idx, 1) if ($idx != -1);
116 &renumber_list(\@list_virtusers_cache, $_[0], -$len);
117 }
118
119 # modify_virtuser(&old, &details, textfile, dbmfile, dbmtype)
120 # Change an existing virtuser
121 sub modify_virtuser
122 {
123 local %virt;
124
125 # Update in file
126 local $lref = &read_file_lines($_[2]);
127 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
128 local @newlines;
129 push(@newlines, &make_table_comment($_[1]->{'cmt'}));
130 push(@newlines, "$_[1]->{'from'}\t$_[1]->{'to'}");
131 splice(@$lref, $_[0]->{'line'}, $oldlen, @newlines);
132 &flush_file_lines($_[2]);
133
134 # Update DBM
135 if (!&rebuild_map_cmd($_[2])) {
136         if ($_[4] eq "dbm") {
137                 dbmopen(%virt, $_[3], 0644);
138                 delete($virt{$_[0]->{'from'}});
139                 $virt{$_[1]->{'from'}} = $_[1]->{'to'};
140                 dbmclose(%virt);
141                 }
142         else { &run_makemap($_[2], $_[3], $_[4]); }
143         }
144
145 # Update cache
146 local $idx = &indexof($_[0], @list_virtusers_cache);
147 $_[1]->{'line'} = $_[0]->{'line'};
148 $_[1]->{'eline'} = $_[1]->{'cmt'} ? $_[0]->{'line'}+1 : $_[0]->{'line'};
149 $list_virtusers_cache[$idx] = $_[1] if ($idx != -1);
150 &renumber_list(\@list_virtusers_cache, $_[0], scalar(@newlines)-$oldlen);
151 }
152
153 # virtuser_form([&details])
154 sub virtuser_form
155 {
156 local ($v) = @_;
157 local ($mode, $addr);
158
159 print &ui_form_start("save_virtuser.cgi", "post");
160 if ($v) {
161         print &ui_hidden("num", $v->{'num'});
162         }
163 else {
164         print &ui_hidden("new", 1);
165         }
166 print &ui_table_start($v ? $text{'vform_edit'} : $text{'vform_create'},
167                       undef, 2);
168
169 # Description
170 print &ui_table_row($text{'vform_cmt'},
171         &ui_textbox("cmt", $v ? $v->{'cmt'} : undef, 50));
172
173 # Source address
174 $addr = !$v || $v->{'from'} =~ /^(\S+)\@(\S+)$/;
175 if ($access{'vcatchall'}) {
176         # Can be address or whole domain
177         print &ui_table_row($text{'vform_for'},
178                 &ui_radio_table("from_type", $addr ? 0 : 1,
179                   [ [ 0, $text{'vform_address'},
180                          &ui_textbox("from_addr",
181                              $addr ? $v->{'from'} : "", 20) ],
182                     [ 1, $text{'vform_domain'},
183                          &ui_textbox("from_dom",
184                              $addr ? "" : substr($v->{'from'}, 1), 20) ] ]));
185         }
186 else {
187         # Just address
188         print &ui_table_row($text{'vform_for'},
189                 &ui_textbox("from_addr", $addr ? $v->{'from'} : "", 40));
190         }
191                 
192 # Virtuser destination
193 $mode = !$v ? 2 :
194         $v->{'to'} =~ /^error:(\S+)\s*(.*)$/ ? 0 :
195         $v->{'to'} =~ /^\%1\@(\S+)$/ ? 1 :
196         $v->{'to'} =~ /^(.*)$/ ? 2 : 2;
197 local ($one, $two) = ($1, $2);
198 local @opts;
199 if ($access{'vedit_2'}) {
200         # Some address
201         push(@opts, [ 2, $text{'vform_address'},
202                       &ui_textbox("to_addr", $mode == 2 ? $one : "", 20) ]);
203         }
204 if ($access{'vedit_1'}) {
205         # Another domain
206         push(@opts, [ 1, $text{'vform_domain'},
207                       &ui_textbox("to_dom", $mode == 1 ? $one : "", 15) ]);
208         }
209 if ($access{'vedit_0'}) {
210         # Return an error
211         push(@opts, [ 0, $text{'vform_error'},
212                       &ui_select("to_code", $one,
213                         [ map { [ $_, $text{'vform_err_'.$_} ] }
214                               ( "nouser", "nohost", "unavailable",
215                                 "tempfail", "protocol" ) ])." ".
216                       &ui_textbox("to_error", $mode == 0 ? $two : "", 15)
217                       ]);
218         }
219 print &ui_table_row($text{'vform_to'},
220         &ui_radio_table("to_type", $mode, \@opts));
221
222 print &ui_table_end();
223 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
224                              [ "delete", $text{'delete'} ] ]
225                          : [ [ "create", $text{'create'} ] ]);
226 }
227
228 # virt_type(string)
229 # Return the type and destination of some virtuser target
230 sub virt_type
231 {
232 local @rv;
233 if ($_[0] =~ /^error:(.*)$/) {
234         @rv = (0, $1);
235         }
236 elsif ($_[0] =~ /^\%1\@(\S+)$/) {
237         @rv = (1, $1);
238         }
239 else {
240         @rv = (2, $_[0]);
241         }
242 return wantarray ? @rv : $rv[0];
243 }
244
245 sub can_edit_virtuser
246 {
247 local ($v) = @_;
248 if ($v->{'from'} =~ /^\@/ && !$access{'vcatchall'}) {
249         return 0;
250         }
251 return $access{'vmode'} == 1 ||
252        $access{'vmode'} == 2 && $v->{'from'} =~ /$access{'vaddrs'}/ ||
253        $access{'vmode'} == 3 && $v->{'from'} =~ /^$remote_user\@/;
254 }
255
256 1;
257