dma: change field order of mail.cgi and fix required fields
[ipfire-2.x.git] / html / cgi-bin / mail.cgi
CommitLineData
9c7b9020
AM
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2015 IPFire Team <alexander.marx@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22use MIME::Lite;
23
24#enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32#Initialize variables and hashes
33my $dmafile="${General::swroot}/dma/dma.conf";
34my $authfile="${General::swroot}/dma/auth.conf";
35my $mailfile="${General::swroot}/dma/mail.conf";
36my %dma=();
37my %auth=();
38my %mail=();
39my %mainsettings=();
40my %cgiparams=();
41my $errormessage='';
42
43#Read all parameters for site
44&Header::getcgihash(\%cgiparams);
45&General::readhash("${General::swroot}/main/settings", \%mainsettings);
46&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
47
48#Show Headers
49&Header::showhttpheaders();
50
51#Check configfiles
52if ( -f $dmafile){
53 open (FILE, "<", $dmafile) or die $!;
54 foreach my $line (<FILE>) {
55 $line =~ m/^([A-Z]+)\s+?(.*)?$/;
56 my $key = $1;
57 my $val = $2;
58 $dma{$key}=$val;
59 }
60}else{
61 open(FILE, ">$dmafile") or die $!;
62}
63close FILE;
64
65if (exists $dma{'AUTHPATH'}){
66 open (FILE, "<", $dma{'AUTHPATH'}) or die "$dma{'AUTHPATH'} nicht gefunden $! ";
67 my $authline;
68 foreach my $line (<FILE>) {
69 $authline = $line;
70 }
71 my @part1 = split(/\|/,$authline);
72 my @part2 = split(/\:/,$part1[1]);
73 $auth{'AUTHNAME'} = $part1[0];
74 $auth{'AUTHHOST'} = $part2[0];
75 $auth{'AUTHPASS'} = $part2[1];
76}
77
78if ( -f $mailfile){
79 &General::readhash($mailfile, \%mail);
80}
81
82#ACTIONS
83if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){ #SaveButton on configsite
84 #Check fields
85 if ($cgiparams{'USEMAIL'} eq 'on'){
86 $errormessage=&checkmailsettings;
87 }else{
88 $cgiparams{'txt_mailserver'}='';
89 $cgiparams{'txt_mailport'}='';
90 $cgiparams{'txt_mailuser'}='';
91 $cgiparams{'txt_mailpass'}='';
92 $cgiparams{'mail_tls'}='';
93 $cgiparams{'txt_mailsender'}='';
94 $cgiparams{'txt_recipient'}='';
95 }
96 if(!$errormessage){
97 #clear hashes
98 %auth=();
99 %dma=();
100 %mail=();
101
102 #clear configfiles
103 open (TXT, ">$dmafile") or die("Could not open /var/ipfire/dma/dma.conf: $!\n");
2799b3d9
AM
104 open (TXT1, ">$authfile") or die("Could not open /var/ipfire/dma/auth.conf: $!\n");
105 open (TXT2, ">$mailfile") or die("Could not open /var/ipfire/dma/mail.conf: $!\n");
9c7b9020
AM
106 close TXT2;
107
108 #Fill hashes with actual values
109 $mail{'USEMAIL'} = $cgiparams{'USEMAIL'};
110 $mail{'SENDER'} = $cgiparams{'txt_mailsender'};
111 $mail{'RECIPIENT'} = $cgiparams{'txt_recipient'};
112
113 $auth{'AUTHNAME'} = $cgiparams{'txt_mailuser'};
114 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
115 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
116
117 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
118 $dma{'PORT'} = $cgiparams{'txt_mailport'};
119 $dma{'STARTTLS'} = '' if ($cgiparams{'mail_tls'});
120 $dma{'SECURETRANSFER'} = '' if exists $dma{'STARTTLS'};
121 $dma{'SPOOLDIR'} = "/var/spool/dma";
122 $dma{'FULLBOUNCE'} = '';
123 $dma{'MAILNAME'} = "$mainsettings{'HOSTNAME'}.$mainsettings{DOMAINNAME}";
124 $dma{'AUTHPATH'} = "$authfile" if exists $auth{'AUTHNAME'};
125
126 #Create new configfiles
127 &General::writehash("$mailfile", \%mail);
128 while ( ($k,$v) = each %dma ) {
129 print TXT "$k $v\n";
130 }
131 close TXT;
132 print TXT1 "$auth{'AUTHNAME'}|$auth{'AUTHHOST'}:$auth{'AUTHPASS'}\n";
133 close TXT2;
134
135 }else{
136 $cgiparams{'update'}='on';
137 &configsite;
138 }
139}
140if ($cgiparams{'ACTION'} eq "$Lang::tr{'email testmail'}"){ #Testmail button on configsite
141 &testmail;
142}
143
144#Show site
145&configsite;
146
147#FUNCTIONS
148sub configsite{
149
150
151 #If update set fieldvalues new
152 if($cgiparams{'update'} eq 'on'){
153 $dma{'USEMAIL'}= 'on';
154 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
155 $dma{'PORT'} = $cgiparams{'txt_mailport'};
156 $auth{'AUTHUSER'} = $cgiparams{'txt_mailuser'};
157 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
158 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
159 }
160 #find preselections
161 $checked{'usemail'}{$mail{'USEMAIL'}} = 'CHECKED';
162 $checked{'mail_tls'}{'on'} = 'CHECKED' if exists $dma{'STARTTLS'};
163
164 #Open site
165 &Header::openpage($Lang::tr{'email settings'}, 1, '');
166 &Header::openbigbox('100%', 'center');
167 &error;
168 &info;
169 &Header::openbox('100%', 'left', $Lang::tr{'email config'});
170
171 #### JAVA SCRIPT ####
172 print<<END;
173<script>
174 \$(document).ready(function() {
175 // Show/Hide elements when USEMAIL checkbox is checked.
176 if (\$("#MAIL").attr("checked")) {
177 \$(".MAILSRV").show();
178 } else {
179 \$(".MAILSRV").hide();
180 }
181
182 // Toggle MAIL elements when "USEMAIL" checkbox is clicked
183 \$("#MAIL").change(function() {
184 \$(".MAILSRV").toggle();
185 });
186 });
187</script>
188END
189 ##### JAVA SCRIPT END ####
190 my $col="style='background-color:$color{'color22'}'";
191 print<<END;
192 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
193 <table style='width:100%' border='0'>
194 <tr>
195 <th colspan='3'></th>
196 </tr>
197 <tr>
198 <td style='width:24em'>$Lang::tr{'email usemail'}</td>
199 <td><label><input type='checkbox' name='USEMAIL' id='MAIL' $checked{'usemail'}{'on'}></label></td>
200 <td></td>
201 </tr>
202 </table><br>
203
204 <div class="MAILSRV">
205 <table style='width:100%;'>
206 <tr>
afd6e193
AM
207 <td>$Lang::tr{'email mailsender'}<img src='/blob.gif' alt='*' /></td>
208 <td><input type='text' name='txt_mailsender' value='$mail{'SENDER'}' style='width:22em;'></td>
209 </tr>
210 <tr>
211 <td>$Lang::tr{'email mailrcpt'}<img src='/blob.gif' alt='*' /></td>
212 <td><input type='text' name='txt_recipient' value='$mail{'RECIPIENT'}' style='width:22em;'></td>
213 </tr>
214 <tr>
215 <td style='width:24em'>$Lang::tr{'email mailaddr'}<img src='/blob.gif' alt='*' /></td>
9c7b9020
AM
216 <td><input type='text' name='txt_mailserver' value='$dma{'SMARTHOST'}' style='width:22em;'></td>
217 </tr>
218 <tr>
afd6e193 219 <td>$Lang::tr{'email mailport'}<img src='/blob.gif' alt='*' /></td>
9c7b9020
AM
220 <td><input type='text' name='txt_mailport' value='$dma{'PORT'}' size='3'></td>
221 </tr>
222 <tr>
afd6e193 223 <td>$Lang::tr{'email mailuser'}</td>
9c7b9020
AM
224 <td><input type='text' name='txt_mailuser' value='$auth{'AUTHNAME'}' style='width:22em;'></td>
225 </tr>
226 <tr>
afd6e193 227 <td>$Lang::tr{'email mailpass'}</td>
9c7b9020
AM
228 <td><input type='password' name='txt_mailpass' value='$auth{'AUTHPASS'}' style='width:22em;' ></td>
229 </tr>
230 <tr>
231 <td>$Lang::tr{'email tls'}</td>
232 <td><input type='checkbox' name='mail_tls' $checked{'mail_tls'}{'on'}></td>
233 </tr>
9c7b9020
AM
234END
235 if (! -z $dmafile && $mail{'USEMAIL'} eq 'on'){
236 print "<tr>";
237 print "<td></td>";
238 print "<td><input type='submit' name='ACTION' value='$Lang::tr{'email testmail'}'></td>";
239 print "</tr>";
240 }
241 print<<END;;
242 <tr>
243 <td colspan='2'>&nbsp;</td>
244 </tr>
245 </table>
246 </div>
247
248 <table style='width:100%;'>
249 <tr>
250 <td colspan='3' display:inline align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
251 </tr>
252 </table>
253 <br>
254 </form>
255END
256 &Header::closebox();
257 &Header::closebigbox();
258 &Header::closepage();
259 exit 0;
260}
261
262sub checkmailsettings {
263 #Check if mailserver is an ip address or a domain
264 if ($cgiparams{'txt_mailserver'} =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
265 if (! &General::validip($cgiparams{'txt_mailserver'})){
266 $errormessage.="$Lang::tr{'email invalid mailip'} $cgiparams{'txt_mailserver'}<br>";
267 }
268 }elsif(! &General::validfqdn($cgiparams{'txt_mailserver'})){
269 $errormessage.="$Lang::tr{'email invalid mailfqdn'} $cgiparams{'txt_mailserver'}<br>";
270 }
271 #Check valid mailserverport
272 if($cgiparams{'txt_mailport'} < 1 || $cgiparams{'txt_mailport'} > 65535){
273 $errormessage.="$Lang::tr{'email invalid mailport'} $cgiparams{'txt_mailport'}<br>";
274 }
275 #Check valid sender
276 if(! $cgiparams{'txt_mailsender'}){
277 $errormessage.="$Lang::tr{'email empty field'} $Lang::tr{'email mailsender'}<br>";
278 }else{
279 if (! &General::validemail($cgiparams{'txt_mailsender'})){
280 $errormessage.="<br>$Lang::tr{'email invalid'} $Lang::tr{'email mailsender'}<br>";
281 }
282 }
283 return $errormessage;
284}
285
286sub testmail {
287 ### Create a new multipart message:
288 $msg = MIME::Lite->new(
289 From => $mail{'SENDER'},
290 To => $mail{'RECIPIENT'},
291 #Cc => 'some@other.com, some@more.com',
292 Subject => 'IPFire Testmail',
293 Type => 'multipart/mixed'
294 );
295
296 ### Add parts (each "attach" has same arguments as "new"):
297 $msg->attach(
298 Type => 'TEXT',
299 Data => "This is the IPFire test mail."
300 );
301
302 ### Add attachment for testing
303 #$msg->attach(
304 # Type => 'application/txt',
305 # Encoding => 'base64',
306 # Path => '/var/ipfire/dma/dma.conf',
307 # Filename => 'dma.conf',
308 # Disposition => 'attachment'
309 #);
310
311 $msg->send_by_sendmail;
312}
313
314sub info {
315 if ($infomessage) {
316 &Header::openbox('100%', 'left', $Lang::tr{'info messages'});
317 print "<class name='base'>$infomessage\n";
318 print "&nbsp;</class>\n";
319 &Header::closebox();
320 }
321}
322
323sub error {
324 if ($errormessage) {
325 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
326 print "<class name='base'>$errormessage\n";
327 print "&nbsp;</class>\n";
328 &Header::closebox();
329 }
330}
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345