Handle hostnames with upper-case letters
[webmin.git] / sendmail / save_virtuser.cgi
1 #!/usr/local/bin/perl
2 # save_virtuser.cgi
3 # Save, create or delete an address mapping
4
5 require './sendmail-lib.pl';
6 require './virtusers-lib.pl';
7 &ReadParse();
8 $access{'vmode'} > 0 || &error($text{'vsave_ecannot'});
9 $conf = &get_sendmailcf();
10 $vfile = &virtusers_file($conf);
11 &lock_file($vfile);
12 ($vdbm, $vdbmtype) = &virtusers_dbm($conf);
13 @virts = &list_virtusers($vfile);
14 foreach $vu (@virts) { $already{$vu->{'from'}}++; }
15 if (!$in{'new'}) {
16         $v = $virts[$in{'num'}];
17         &can_edit_virtuser($v) || &error($text{'vsave_ecannot2'});
18         }
19 elsif ($access{'vmax'}) {
20         local @cvirts =
21                 grep { $access{"vedit_".&virt_type($_->{'to'})} } @virts;
22         if ($access{'vmode'} == 2) {
23                 @cvirts = grep { $_->{'from'} =~ /$access{'vaddrs'}/ } @cvirts;
24                 }
25         elsif ($access{'vmode'} == 3) {
26                 @cvirts = grep { $_->{'from'} =~ /^$remote_user\@/ } @cvirts;
27                 }
28         &error(&text('vsave_emax', $access{'vmax'}))
29                 if (@cvirts >= $access{'vmax'});
30         }
31
32 if ($in{'delete'}) {
33         # delete some mapping
34         $logv = $v;
35         &delete_virtuser($v, $vfile, $vdbm, $vdbmtype);
36         }
37 else {
38         # Saving or creating.. check inputs
39         &error_setup($text{'vsave_err'});
40         if ($in{'from_type'} == 0 || !$access{'vcatchall'}) {
41                 $in{'from_addr'} =~ /^(\S+)\@(\S+)$/ ||
42                         &error(&text('vsave_efrom', $in{'from_addr'}));
43                 $from = $in{'from_addr'};
44                 if ($already{$from} && ($in{'new'} || $v->{'from'} ne $from)) {
45                         &error(&text('vsave_efromdup', $in{'from_addr'}));
46                         }
47                 }
48         else {
49                 $in{'from_dom'} =~ /^([^\@ ]+)$/ ||
50                         &error(&text('vsave_edom', $in{'from_dom'}));
51                 $from = "\@$in{'from_dom'}";
52                 if ($already{$from} && ($in{'new'} || $v->{'from'} ne $from)) {
53                         &error(&text('vsave_edomdup', $in{'from_dom'}));
54                         }
55                 }
56         if ($access{'vmode'} == 2) {
57                 $from =~ /$access{'vaddrs'}/ ||
58                         &error(&text('vsave_ematch', $access{'vaddrs'}));
59                 }
60         elsif ($access{'vmode'} == 3) {
61                 $from =~ /^$remote_user\@/ || &error($text{'vsave_esame'});
62                 }
63         if ($in{'to_type'} == 2) {
64                 $access{"vedit_2"} ||
65                         &error($text{'vsave_ecannot3'});
66                 $in{'to_addr'} =~ /^([^\s,]+)$/ ||
67                         &error(&text('vsave_eaddr', $in{'to_addr'}));
68                 $to = $in{'to_addr'};
69                 }
70         elsif ($in{'to_type'} == 1) {
71                 $access{"vedit_1"} ||
72                         &error($text{'vsave_ecannot4'});
73                 $in{'to_dom'} =~ /^([^\@ ]+)$/ ||
74                         &error(&text('vsave_edom', $in{'to_dom'}));
75                 $in{'from_type'} == 1 ||
76                         &error($text{'vsave_edomdom'});
77                 $to = "\%1\@$in{'to_dom'}";
78                 }
79         else {
80                 $access{"vedit_0"} ||
81                         &error($text{'vsave_ecannot5'});
82                 $to = "error:$in{'to_code'} $in{'to_error'}";
83                 }
84
85         $newv{'from'} = $from;
86         $newv{'to'} = $to;
87         $newv{'cmt'} = $in{'cmt'};
88         if ($in{'new'}) { &create_virtuser(\%newv, $vfile, $vdbm, $vdbmtype); }
89         else { &modify_virtuser($v, \%newv, $vfile, $vdbm, $vdbmtype); }
90         $logv = \%newv;
91         }
92 &unlock_file($vfile);
93 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
94             "virtuser", $logv->{'from'}, $logv);
95 &redirect("list_virtusers.cgi");
96