Handle hostnames with upper-case letters
[webmin.git] / sendmail / domain-lib.pl
1 # domain-lib.pl
2 # Functions for the domains table
3
4 # domains_dbm(&config)
5 # Returns the filename and type of the domains database, or undef if none
6 sub domains_dbm
7 {
8 foreach $f (&find_type("K", $_[0])) {
9         if ($f->{'value'} =~ /^domaintable\s+(\S+)[^\/]+(\S+)$/) {
10                 return ($2, $1);
11                 }
12         }
13 return undef;
14 }
15
16 # domains_file(&config)
17 # Returns the filename of the text domains file, or undef if none
18 sub domains_file
19 {
20 return &find_textfile($config{'domains_file'}, &domains_dbm($_[0]));
21 }
22
23 # list_domains(textfile)
24 sub list_domains
25 {
26 if (!scalar(@list_domains_cache)) {
27         @list_domains_cache = ( );
28         local $lnum = 0;
29         local $cmt;
30         open(DOM, $_[0]);
31         while(<DOM>) {
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                         # A domain mapping
39                         local(%dom);
40                         $dom{'from'} = $1;
41                         $dom{'to'} = $2;
42                         $dom{'line'} = $cmt ? $lnum-1 : $lnum;
43                         $dom{'eline'} = $lnum;
44                         $dom{'num'} = scalar(@list_domains_cache);
45                         $dom{'cmt'} = $cmt;
46                         push(@list_domains_cache, \%dom);
47                         $cmt = undef;
48                         }
49                 else {
50                         $cmt = undef;
51                         }
52                 $lnum++;
53                 }
54         close(DOM);
55         }
56 return @list_domains_cache;
57 }
58
59 # create_domain(&details, textfile, dbmfile, dbmtype)
60 # Create a new domain mapping
61 sub create_domain
62 {
63 &list_domains($_[1]);   # force cache init
64 local %dom;
65
66 # Write to the file
67 local $lref = &read_file_lines($_[1]);
68 $_[0]->{'line'} = scalar(@$lref);
69 push(@$lref, &make_table_comment($_[0]->{'cmt'}));
70 push(@$lref, "$_[0]->{'from'}\t$_[0]->{'to'}");
71 $_[0]->{'eline'} = scalar(@$lref)-1;
72 &flush_file_lines($_[1]);
73
74 # Add to DBM
75 if (!&rebuild_map_cmd($_[1])) {
76         if ($_[3] eq "dbm") {
77                 dbmopen(%dom, $_[2], 0644);
78                 $dom{$_[0]->{'from'}} = $_[0]->{'to'};
79                 dbmclose(%dom);
80                 }
81         else { &run_makemap($_[1], $_[2], $_[3]); }
82         }
83
84 # Add to cache
85 $_[0]->{'num'} = scalar(@list_domains_cache);
86 $_[0]->{'file'} = $_[1];
87 push(@list_domains_cache, $_[0]);
88 }
89
90 # delete_domain(&details, textfile, dbmfile, dbmtype)
91 # Delete an existing domain mapping
92 sub delete_domain
93 {
94 local %dom;
95
96 # Delete from file
97 local $lref = &read_file_lines($_[1]);
98 local $len = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
99 splice(@$lref, $_[0]->{'line'}, $len);
100 &flush_file_lines($_[1]);
101
102 # Delete from DBM
103 if (!&rebuild_map_cmd($_[1])) {
104         if ($_[3] eq "dbm") {
105                 dbmopen(%dom, $_[2], 0644);
106                 delete($dom{$_[0]->{'from'}});
107                 dbmclose(%dom);
108                 }
109         else { &run_makemap($_[1], $_[2], $_[3]); }
110         }
111
112 # Delete from cache
113 local $idx = &indexof($_[0], @list_domains_cache);
114 splice(@list_domains_cache, $idx, 1) if ($idx != -1);
115 &renumber_list(\@list_domains_cache, $_[0], -$len);
116 }
117
118 # modify_domain(&old, &details, textfile, dbmfile, dbmtype)
119 # Change an existing domain
120 sub modify_domain
121 {
122 local %dom;
123
124 # Update in file
125 local $lref = &read_file_lines($_[2]);
126 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
127 local @newlines;
128 push(@newlines, &make_table_comment($_[1]->{'cmt'}));
129 push(@newlines, "$_[1]->{'from'}\t$_[1]->{'to'}");
130 splice(@$lref, $_[0]->{'line'}, $oldlen, @newlines);
131 &flush_file_lines($_[2]);
132
133 # Update DBM
134 if (!&rebuild_map_cmd($_[2])) {
135         if ($_[4] eq "dbm") {
136                 dbmopen(%dom, $_[3], 0644);
137                 delete($dom{$_[0]->{'from'}});
138                 $dom{$_[1]->{'from'}} = $_[1]->{'to'};
139                 dbmclose(%dom);
140                 }
141         else { &run_makemap($_[2], $_[3], $_[4]); }
142         }
143
144 # Update cache
145 local $idx = &indexof($_[0], @list_domains_cache);
146 $_[1]->{'line'} = $_[0]->{'line'};
147 $_[1]->{'eline'} = $_[1]->{'cmt'} ? $_[0]->{'line'}+1 : $_[0]->{'line'};
148 $list_domains_cache[$idx] = $_[1] if ($idx != -1);
149 &renumber_list(\@list_domains_cache, $_[0], scalar(@newlines)-$oldlen);
150 }
151
152 # domain_form([&details])
153 sub domain_form
154 {
155 local $g = $_[0];
156
157 print &ui_form_start("save_domain.cgi", "post");
158 if ($g) {
159         print &ui_hidden("num", $g->{'num'}),"\n";
160         }
161 else {
162         print &ui_hidden("new", 1),"\n";
163         }
164 print &ui_table_start($g ? $text{'gform_edit'} : $text{'gform_create'},
165                       undef, 2);
166
167 print &ui_table_row($text{'vform_cmt'},
168                     &ui_textbox("cmt", $g->{'cmt'}, 50));
169
170 print &ui_table_row($text{'dform_from'},
171                     &ui_textbox("from", $g->{'from'}, 30));
172
173 print &ui_table_row($text{'dform_to'},
174                     &ui_textbox("to", $g->{'to'}, 30));
175
176 print &ui_table_end();
177 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
178                              [ "delete", $text{'delete'} ] ]
179                          : [ [ "create", $text{'create'} ] ]);
180 }
181
182 1;
183