2 # Functions for the virtusers table
4 # virtusers_dbm(&config)
5 # Returns the filename and type of the virtusers database, or undef if none
8 foreach $f (&find_type("K", $_[0])) {
9 if ($f->{'value'} =~ /^virtuser\s+(\S+)[^\/]+(\S+)$/) {
16 # virtusers_file(&config)
17 # Returns the filename of the text virtusers file, or undef if none
20 return &find_textfile($config{'virtusers_file'}, &virtusers_dbm($_[0]));
23 # list_virtusers(textfile)
26 if (!scalar(@list_virtusers_cache)) {
27 @list_virtusers_cache = ( );
32 s/\r|\n//g; # remove newlines
33 if (/^\s*#+\s*(.*)/) {
35 $cmt = &is_table_comment($_);
37 elsif (/^(\S+)\s+(.*)/) {
38 # An actual virtuser line
42 $virt{'line'} = $cmt ? $lnum-1 : $lnum;
43 $virt{'eline'} = $lnum;
44 $virt{'file'} = $_[0];
45 $virt{'num'} = scalar(@list_virtusers_cache);
47 push(@list_virtusers_cache, \%virt);
57 return @list_virtusers_cache;
60 # create_virtuser(&details, textfile, dbmfile, dbmtype)
61 # Create a new virtuser mapping
64 &list_virtusers($_[1]); # force cache init
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]);
76 if (!&rebuild_map_cmd($_[1])) {
78 dbmopen(%virt, $_[2], 0644);
79 $virt{$_[0]->{'from'}} = $_[0]->{'to'};
82 else { &run_makemap($_[1], $_[2], $_[3]); }
86 $_[0]->{'num'} = scalar(@list_virtusers_cache);
87 $_[0]->{'file'} = $_[1];
88 push(@list_virtusers_cache, $_[0]);
91 # delete_virtuser(&details, textfile, dbmfile, dbmtype)
92 # Delete an existing virtuser mapping
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]);
104 if (!&rebuild_map_cmd($_[1])) {
105 if ($_[3] eq "dbm") {
106 dbmopen(%virt, $_[2], 0644);
107 delete($virt{$_[0]->{'from'}});
110 else { &run_makemap($_[1], $_[2], $_[3]); }
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);
119 # modify_virtuser(&old, &details, textfile, dbmfile, dbmtype)
120 # Change an existing virtuser
126 local $lref = &read_file_lines($_[2]);
127 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
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]);
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'};
142 else { &run_makemap($_[2], $_[3], $_[4]); }
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);
153 # virtuser_form([&details])
157 local ($mode, $addr);
159 print &ui_form_start("save_virtuser.cgi", "post");
161 print &ui_hidden("num", $v->{'num'});
164 print &ui_hidden("new", 1);
166 print &ui_table_start($v ? $text{'vform_edit'} : $text{'vform_create'},
170 print &ui_table_row($text{'vform_cmt'},
171 &ui_textbox("cmt", $v ? $v->{'cmt'} : undef, 50));
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) ] ]));
188 print &ui_table_row($text{'vform_for'},
189 &ui_textbox("from_addr", $addr ? $v->{'from'} : "", 40));
192 # Virtuser destination
194 $v->{'to'} =~ /^error:(\S+)\s*(.*)$/ ? 0 :
195 $v->{'to'} =~ /^\%1\@(\S+)$/ ? 1 :
196 $v->{'to'} =~ /^(.*)$/ ? 2 : 2;
197 local ($one, $two) = ($1, $2);
199 if ($access{'vedit_2'}) {
201 push(@opts, [ 2, $text{'vform_address'},
202 &ui_textbox("to_addr", $mode == 2 ? $one : "", 20) ]);
204 if ($access{'vedit_1'}) {
206 push(@opts, [ 1, $text{'vform_domain'},
207 &ui_textbox("to_dom", $mode == 1 ? $one : "", 15) ]);
209 if ($access{'vedit_0'}) {
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)
219 print &ui_table_row($text{'vform_to'},
220 &ui_radio_table("to_type", $mode, \@opts));
222 print &ui_table_end();
223 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
224 [ "delete", $text{'delete'} ] ]
225 : [ [ "create", $text{'create'} ] ]);
229 # Return the type and destination of some virtuser target
233 if ($_[0] =~ /^error:(.*)$/) {
236 elsif ($_[0] =~ /^\%1\@(\S+)$/) {
242 return wantarray ? @rv : $rv[0];
245 sub can_edit_virtuser
248 if ($v->{'from'} =~ /^\@/ && !$access{'vcatchall'}) {
251 return $access{'vmode'} == 1 ||
252 $access{'vmode'} == 2 && $v->{'from'} =~ /$access{'vaddrs'}/ ||
253 $access{'vmode'} == 3 && $v->{'from'} =~ /^$remote_user\@/;