]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/mail.cgi
ddns: Import latest upstream patches for ddns-013
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / mail.cgi
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
22 use MIME::Lite;
23
24 #enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 #Initialize variables and hashes
33 my $dmafile="${General::swroot}/dma/dma.conf";
34 my $authfile="${General::swroot}/dma/auth.conf";
35 my $mailfile="${General::swroot}/dma/mail.conf";
36 my %dma=();
37 my %auth=();
38 my %mail=();
39 my %mainsettings=();
40 my %cgiparams=();
41 my $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
52 if ( -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 }
63 close FILE;
64
65 if (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
78 if ( -f $mailfile){
79 &General::readhash($mailfile, \%mail);
80 }
81
82 #ACTIONS
83 if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){ #SaveButton on configsite
84 # Check fields
85 $errormessage = &checkmailsettings();
86
87 if (!$errormessage) {
88 #clear hashes
89 %auth=();
90 %dma=();
91 %mail=();
92
93 #clear configfiles
94 open (TXT, ">$dmafile") or die("Could not open /var/ipfire/dma/dma.conf: $!\n");
95 open (TXT1, ">$authfile") or die("Could not open /var/ipfire/dma/auth.conf: $!\n");
96 open (TXT2, ">$mailfile") or die("Could not open /var/ipfire/dma/mail.conf: $!\n");
97 close TXT2;
98
99 #Fill hashes with actual values
100 $mail{'USEMAIL'} = $cgiparams{'USEMAIL'};
101 $mail{'SENDER'} = $cgiparams{'txt_mailsender'};
102 $mail{'RECIPIENT'} = $cgiparams{'txt_recipient'};
103
104 if ($cgiparams{'txt_mailuser'} && $cgiparams{'txt_mailpass'}) {
105 $auth{'AUTHNAME'} = $cgiparams{'txt_mailuser'};
106 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
107 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
108 print TXT1 "$auth{'AUTHNAME'}|$auth{'AUTHHOST'}:$auth{'AUTHPASS'}\n";
109 }
110
111 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
112 $dma{'PORT'} = $cgiparams{'txt_mailport'};
113 $dma{'STARTTLS'} = '' if ($cgiparams{'mail_tls'});
114 $dma{'SECURETRANSFER'} = '' if exists $dma{'STARTTLS'};
115 $dma{'SPOOLDIR'} = "/var/spool/dma";
116 $dma{'FULLBOUNCE'} = '';
117 $dma{'MAILNAME'} = "$mainsettings{'HOSTNAME'}.$mainsettings{DOMAINNAME}";
118 $dma{'AUTHPATH'} = "$authfile" if exists $auth{'AUTHNAME'};
119
120 #Create new configfiles
121 &General::writehash("$mailfile", \%mail);
122 while ( ($k,$v) = each %dma ) {
123 print TXT "$k $v\n";
124 }
125 close TXT;
126 close TXT1;
127 close TXT2;
128
129 }else{
130 $cgiparams{'update'}='on';
131 &configsite;
132 }
133 }
134 if ($cgiparams{'ACTION'} eq "$Lang::tr{'email testmail'}"){ #Testmail button on configsite
135 &testmail;
136 }
137
138 #Show site
139 &configsite;
140
141 #FUNCTIONS
142 sub configsite{
143
144
145 #If update set fieldvalues new
146 if($cgiparams{'update'} eq 'on'){
147 $mail{'USEMAIL'} = 'on';
148 $mail{'SENDER'} = $cgiparams{'txt_mailsender'};
149 $mail{'RECIPIENT'} = $cgiparams{'txt_recipient'};
150 $dma{'SMARTHOST'} = $cgiparams{'txt_mailserver'};
151 $dma{'PORT'} = $cgiparams{'txt_mailport'};
152 $auth{'AUTHNAME'} = $cgiparams{'txt_mailuser'};
153 $auth{'AUTHHOST'} = $cgiparams{'txt_mailserver'};
154 $auth{'AUTHPASS'} = $cgiparams{'txt_mailpass'};
155 $dma{'STARTTLS'} = $cgiparams{'mail_tls'};
156 }
157 #find preselections
158 $checked{'usemail'}{$mail{'USEMAIL'}} = 'CHECKED';
159 $checked{'mail_tls'}{'on'} = 'CHECKED' if exists $dma{'STARTTLS'};
160
161 #Open site
162 &Header::openpage($Lang::tr{'email settings'}, 1, '');
163 &Header::openbigbox('100%', 'center');
164 &error;
165 &info;
166 &Header::openbox('100%', 'left', $Lang::tr{'email config'});
167
168 #### JAVA SCRIPT ####
169 print<<END;
170 <script>
171 \$(document).ready(function() {
172 // Show/Hide elements when USEMAIL checkbox is checked.
173 if (\$("#MAIL").attr("checked")) {
174 \$(".MAILSRV").show();
175 } else {
176 \$(".MAILSRV").hide();
177 }
178
179 // Toggle MAIL elements when "USEMAIL" checkbox is clicked
180 \$("#MAIL").change(function() {
181 \$(".MAILSRV").toggle();
182 });
183 });
184 </script>
185 END
186 ##### JAVA SCRIPT END ####
187 my $col="style='background-color:$color{'color22'}'";
188 print<<END;
189 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
190 <table style='width:100%' border='0'>
191 <tr>
192 <th colspan='3'></th>
193 </tr>
194 <tr>
195 <td style='width:24em'>$Lang::tr{'email usemail'}</td>
196 <td><label><input type='checkbox' name='USEMAIL' id='MAIL' $checked{'usemail'}{'on'}></label></td>
197 <td></td>
198 </tr>
199 </table><br>
200
201 <div class="MAILSRV">
202 <table style='width:100%;'>
203 <tr>
204 <td>$Lang::tr{'email mailsender'}<img src='/blob.gif' alt='*' /></td>
205 <td><input type='text' name='txt_mailsender' value='$mail{'SENDER'}' style='width:22em;'></td>
206 </tr>
207 <tr>
208 <td>$Lang::tr{'email mailrcpt'}<img src='/blob.gif' alt='*' /></td>
209 <td><input type='text' name='txt_recipient' value='$mail{'RECIPIENT'}' style='width:22em;'></td>
210 </tr>
211 <tr>
212 <td style='width:24em'>$Lang::tr{'email mailaddr'}<img src='/blob.gif' alt='*' /></td>
213 <td><input type='text' name='txt_mailserver' value='$dma{'SMARTHOST'}' style='width:22em;'></td>
214 </tr>
215 <tr>
216 <td>$Lang::tr{'email mailport'}<img src='/blob.gif' alt='*' /></td>
217 <td><input type='text' name='txt_mailport' value='$dma{'PORT'}' size='3'></td>
218 </tr>
219 <tr>
220 <td>$Lang::tr{'email mailuser'}</td>
221 <td><input type='text' name='txt_mailuser' value='$auth{'AUTHNAME'}' style='width:22em;'></td>
222 </tr>
223 <tr>
224 <td>$Lang::tr{'email mailpass'}</td>
225 <td><input type='password' name='txt_mailpass' value='$auth{'AUTHPASS'}' style='width:22em;' ></td>
226 </tr>
227 <tr>
228 <td>$Lang::tr{'email tls'}</td>
229 <td><input type='checkbox' name='mail_tls' $checked{'mail_tls'}{'on'}></td>
230 </tr>
231 END
232 if (! -z $dmafile && $mail{'USEMAIL'} eq 'on' && !$errormessage){
233 print "<tr>";
234 print "<td></td>";
235 print "<td><input type='submit' name='ACTION' value='$Lang::tr{'email testmail'}'></td>";
236 print "</tr>";
237 }
238 print<<END;;
239 <tr>
240 <td colspan='2'>&nbsp;</td>
241 </tr>
242 </table>
243 </div>
244
245 <table style='width:100%;'>
246 <tr>
247 <td colspan='3' display:inline align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
248 </tr>
249 </table>
250 <br>
251 </form>
252 END
253 &Header::closebox();
254 &Header::closebigbox();
255 &Header::closepage();
256 exit 0;
257 }
258
259 sub checkmailsettings {
260 #Check if mailserver is an ip address or a domain
261 if ($cgiparams{'txt_mailserver'} =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
262 if (! &General::validip($cgiparams{'txt_mailserver'})){
263 $errormessage .= $Lang::tr{'email invalid mailip'} . "<br>";
264 }
265 }elsif(! &General::validfqdn($cgiparams{'txt_mailserver'})){
266 $errormessage .= $Lang::tr{'email invalid mailfqdn'} . "<br>";
267 }
268 #Check valid mailserverport
269 if($cgiparams{'txt_mailport'} < 1 || $cgiparams{'txt_mailport'} > 65535){
270 $errormessage .= $Lang::tr{'email invalid mailport'} . "<br>";
271 }
272 #Check valid sender
273 if(! $cgiparams{'txt_mailsender'}){
274 $errormessage .= $Lang::tr{'email empty field'} . "<br>";
275 }else{
276 if (! &General::validemail($cgiparams{'txt_mailsender'})){
277 $errormessage .= "$Lang::tr{'email invalid'} $Lang::tr{'email mailsender'}<br>";
278 }
279 }
280 return $errormessage;
281 }
282
283 sub testmail {
284 ### Create a new multipart message:
285 $msg = MIME::Lite->new(
286 From => $mail{'SENDER'},
287 To => $mail{'RECIPIENT'},
288 #Cc => 'some@other.com, some@more.com',
289 Subject => 'IPFire Testmail',
290 Type => 'multipart/mixed'
291 );
292
293 ### Add parts (each "attach" has same arguments as "new"):
294 $msg->attach(
295 Type => 'TEXT',
296 Data => "This is the IPFire test mail."
297 );
298
299 ### Add attachment for testing
300 #$msg->attach(
301 # Type => 'application/txt',
302 # Encoding => 'base64',
303 # Path => '/var/ipfire/dma/dma.conf',
304 # Filename => 'dma.conf',
305 # Disposition => 'attachment'
306 #);
307
308 $msg->send_by_sendmail;
309 }
310
311 sub info {
312 if ($infomessage) {
313 &Header::openbox('100%', 'left', $Lang::tr{'info messages'});
314 print "<class name='base'>$infomessage\n";
315 print "&nbsp;</class>\n";
316 &Header::closebox();
317 }
318 }
319
320 sub error {
321 if ($errormessage) {
322 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
323 print "<class name='base'>$errormessage\n";
324 print "&nbsp;</class>\n";
325 &Header::closebox();
326 }
327 }