]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/mail.cgi
httpd: include TLS 1.3 cipher suites
[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
4bfec109
AM
113 if ($cgiparams{'txt_mailuser'} && $cgiparams{'txt_mailpass'}) {
114 $auth{'AUTHNAME'} = $cgiparams{'txt_mailuser'};
115 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
116 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
117 print TXT1 "$auth{'AUTHNAME'}|$auth{'AUTHHOST'}:$auth{'AUTHPASS'}\n";
118 }
9c7b9020
AM
119
120 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
121 $dma{'PORT'} = $cgiparams{'txt_mailport'};
122 $dma{'STARTTLS'} = '' if ($cgiparams{'mail_tls'});
123 $dma{'SECURETRANSFER'} = '' if exists $dma{'STARTTLS'};
124 $dma{'SPOOLDIR'} = "/var/spool/dma";
125 $dma{'FULLBOUNCE'} = '';
126 $dma{'MAILNAME'} = "$mainsettings{'HOSTNAME'}.$mainsettings{DOMAINNAME}";
127 $dma{'AUTHPATH'} = "$authfile" if exists $auth{'AUTHNAME'};
128
129 #Create new configfiles
130 &General::writehash("$mailfile", \%mail);
131 while ( ($k,$v) = each %dma ) {
132 print TXT "$k $v\n";
133 }
134 close TXT;
4bfec109 135 close TXT1;
9c7b9020
AM
136 close TXT2;
137
138 }else{
139 $cgiparams{'update'}='on';
140 &configsite;
141 }
142}
143if ($cgiparams{'ACTION'} eq "$Lang::tr{'email testmail'}"){ #Testmail button on configsite
144 &testmail;
145}
146
147#Show site
148&configsite;
149
150#FUNCTIONS
151sub configsite{
152
153
154 #If update set fieldvalues new
155 if($cgiparams{'update'} eq 'on'){
70defeb0
AM
156 $mail{'USEMAIL'} = 'on';
157 $mail{'SENDER'} = $cgiparams{'txt_mailsender'};
158 $mail{'RECIPIENT'} = $cgiparams{'txt_recipient'};
9c7b9020
AM
159 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
160 $dma{'PORT'} = $cgiparams{'txt_mailport'};
70defeb0 161 $auth{'AUTHNAME'} = $cgiparams{'txt_mailuser'};
9c7b9020
AM
162 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
163 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
70defeb0 164 $dma{'STARTTLS'} = $cgiparams{'mail_tls'};
9c7b9020
AM
165 }
166 #find preselections
167 $checked{'usemail'}{$mail{'USEMAIL'}} = 'CHECKED';
168 $checked{'mail_tls'}{'on'} = 'CHECKED' if exists $dma{'STARTTLS'};
169
170 #Open site
171 &Header::openpage($Lang::tr{'email settings'}, 1, '');
172 &Header::openbigbox('100%', 'center');
173 &error;
174 &info;
175 &Header::openbox('100%', 'left', $Lang::tr{'email config'});
176
177 #### JAVA SCRIPT ####
178 print<<END;
179<script>
180 \$(document).ready(function() {
181 // Show/Hide elements when USEMAIL checkbox is checked.
182 if (\$("#MAIL").attr("checked")) {
183 \$(".MAILSRV").show();
184 } else {
185 \$(".MAILSRV").hide();
186 }
187
188 // Toggle MAIL elements when "USEMAIL" checkbox is clicked
189 \$("#MAIL").change(function() {
190 \$(".MAILSRV").toggle();
191 });
192 });
193</script>
194END
195 ##### JAVA SCRIPT END ####
196 my $col="style='background-color:$color{'color22'}'";
197 print<<END;
198 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
199 <table style='width:100%' border='0'>
200 <tr>
201 <th colspan='3'></th>
202 </tr>
203 <tr>
204 <td style='width:24em'>$Lang::tr{'email usemail'}</td>
205 <td><label><input type='checkbox' name='USEMAIL' id='MAIL' $checked{'usemail'}{'on'}></label></td>
206 <td></td>
207 </tr>
208 </table><br>
209
210 <div class="MAILSRV">
211 <table style='width:100%;'>
212 <tr>
afd6e193
AM
213 <td>$Lang::tr{'email mailsender'}<img src='/blob.gif' alt='*' /></td>
214 <td><input type='text' name='txt_mailsender' value='$mail{'SENDER'}' style='width:22em;'></td>
215 </tr>
216 <tr>
217 <td>$Lang::tr{'email mailrcpt'}<img src='/blob.gif' alt='*' /></td>
218 <td><input type='text' name='txt_recipient' value='$mail{'RECIPIENT'}' style='width:22em;'></td>
219 </tr>
220 <tr>
221 <td style='width:24em'>$Lang::tr{'email mailaddr'}<img src='/blob.gif' alt='*' /></td>
9c7b9020
AM
222 <td><input type='text' name='txt_mailserver' value='$dma{'SMARTHOST'}' style='width:22em;'></td>
223 </tr>
224 <tr>
afd6e193 225 <td>$Lang::tr{'email mailport'}<img src='/blob.gif' alt='*' /></td>
9c7b9020
AM
226 <td><input type='text' name='txt_mailport' value='$dma{'PORT'}' size='3'></td>
227 </tr>
228 <tr>
afd6e193 229 <td>$Lang::tr{'email mailuser'}</td>
9c7b9020
AM
230 <td><input type='text' name='txt_mailuser' value='$auth{'AUTHNAME'}' style='width:22em;'></td>
231 </tr>
232 <tr>
afd6e193 233 <td>$Lang::tr{'email mailpass'}</td>
9c7b9020
AM
234 <td><input type='password' name='txt_mailpass' value='$auth{'AUTHPASS'}' style='width:22em;' ></td>
235 </tr>
236 <tr>
237 <td>$Lang::tr{'email tls'}</td>
238 <td><input type='checkbox' name='mail_tls' $checked{'mail_tls'}{'on'}></td>
239 </tr>
9c7b9020 240END
70defeb0 241 if (! -z $dmafile && $mail{'USEMAIL'} eq 'on' && !$errormessage){
9c7b9020
AM
242 print "<tr>";
243 print "<td></td>";
244 print "<td><input type='submit' name='ACTION' value='$Lang::tr{'email testmail'}'></td>";
245 print "</tr>";
246 }
247 print<<END;;
248 <tr>
249 <td colspan='2'>&nbsp;</td>
250 </tr>
251 </table>
252 </div>
253
254 <table style='width:100%;'>
255 <tr>
256 <td colspan='3' display:inline align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
257 </tr>
258 </table>
259 <br>
260 </form>
261END
262 &Header::closebox();
263 &Header::closebigbox();
264 &Header::closepage();
265 exit 0;
266}
267
268sub checkmailsettings {
269 #Check if mailserver is an ip address or a domain
270 if ($cgiparams{'txt_mailserver'} =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
271 if (! &General::validip($cgiparams{'txt_mailserver'})){
272 $errormessage.="$Lang::tr{'email invalid mailip'} $cgiparams{'txt_mailserver'}<br>";
273 }
274 }elsif(! &General::validfqdn($cgiparams{'txt_mailserver'})){
275 $errormessage.="$Lang::tr{'email invalid mailfqdn'} $cgiparams{'txt_mailserver'}<br>";
276 }
277 #Check valid mailserverport
278 if($cgiparams{'txt_mailport'} < 1 || $cgiparams{'txt_mailport'} > 65535){
279 $errormessage.="$Lang::tr{'email invalid mailport'} $cgiparams{'txt_mailport'}<br>";
280 }
281 #Check valid sender
282 if(! $cgiparams{'txt_mailsender'}){
283 $errormessage.="$Lang::tr{'email empty field'} $Lang::tr{'email mailsender'}<br>";
284 }else{
285 if (! &General::validemail($cgiparams{'txt_mailsender'})){
286 $errormessage.="<br>$Lang::tr{'email invalid'} $Lang::tr{'email mailsender'}<br>";
287 }
288 }
289 return $errormessage;
290}
291
292sub testmail {
293 ### Create a new multipart message:
294 $msg = MIME::Lite->new(
295 From => $mail{'SENDER'},
296 To => $mail{'RECIPIENT'},
297 #Cc => 'some@other.com, some@more.com',
298 Subject => 'IPFire Testmail',
299 Type => 'multipart/mixed'
300 );
301
302 ### Add parts (each "attach" has same arguments as "new"):
303 $msg->attach(
304 Type => 'TEXT',
305 Data => "This is the IPFire test mail."
306 );
307
308 ### Add attachment for testing
309 #$msg->attach(
310 # Type => 'application/txt',
311 # Encoding => 'base64',
312 # Path => '/var/ipfire/dma/dma.conf',
313 # Filename => 'dma.conf',
314 # Disposition => 'attachment'
315 #);
316
317 $msg->send_by_sendmail;
318}
319
320sub info {
321 if ($infomessage) {
322 &Header::openbox('100%', 'left', $Lang::tr{'info messages'});
323 print "<class name='base'>$infomessage\n";
324 print "&nbsp;</class>\n";
325 &Header::closebox();
326 }
327}
328
329sub error {
330 if ($errormessage) {
331 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
332 print "<class name='base'>$errormessage\n";
333 print "&nbsp;</class>\n";
334 &Header::closebox();
335 }
336}