From c5b94a8bc8377f66b1e6e59b3421cfdc16949d93 Mon Sep 17 00:00:00 2001 From: Miquel Lionel Date: Tue, 17 Nov 2020 00:41:20 +0100 Subject: Local SMTP server OR external SMTP server shenanigans - The print_smtp_method sub has been added and behavior depends on the HAS_MAILSERVER variable value. If 0 we assume the admin has no mailserver available locally so we use Net:SMTPS and external server. If 1, we switch to Mail::Sendmail. - We renamed some variables so they make more sense now. - Text strings are translated from Baguette to Shakespeare. it was time. --- gpigeon-template.cgi | 187 ++++++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 83 deletions(-) diff --git a/gpigeon-template.cgi b/gpigeon-template.cgi index 3117277..097973c 100755 --- a/gpigeon-template.cgi +++ b/gpigeon-template.cgi @@ -1,8 +1,8 @@ #! /usr/bin/perl -wT -use Digest::SHA qw(sha256_hex); use warnings; use strict; +use Digest::SHA qw(sha256_hex); use Email::Valid; use String::Random; use CGI qw(param); @@ -10,12 +10,53 @@ use CGI qw(param); delete @ENV{qw(IFS PATH CDPATH BASH_ENV)}; +sub print_smtp_method { + my $m_addr = shift; + my $m_addr_pw = shift; + my $m_addr_escaped = shift; + my $smtp_serv = shift; + my $smtp_port = shift; + + if ($HAS_MAILSERVER eq 0){ + print 'use Net::SMTP;',"\n", + 'use Net::SMTPS;',"\n", + 'my $smtp = Net::SMTPS->new(\''. $smtp_serv .'\', Port => \''. $smt_port .'\', + doSSL => \'ssl\', Debug_SSL => 0);', "\n", + '$smtp->auth(\''. $m_addr .'\', \''. $m_addr_pw .'\') or die;', "\n", + '$smtp->mail(\''. $m_addr .'\') or die "Net::SMTP module has broke: + $!.";', "\n", + 'if ($smtp->to(\''. $m_addr .'\')){', "\n", + ' $smtp->data();', "\n", + ' $smtp->datasend("To: '. $m_addr_escaped .'\n");', "\n", + ' $smtp->datasend("\n");', "\n", + ' $smtp->datasend("$enc_msg\n");', "\n", + ' $smtp->dataend();', "\n", + '}', "\n", + 'else {', "\n", + ' die $smtp->message();', "\n", + '}', "\n"; + } + else { + undef $m_addr_escaped; + print 'use Mail::Sendmail;', + '%mail = ( To => \''.$m_addr.'\',' , "\n", + ' From => \''.$m_addr.'\',', "\n", + ' Subject => \'Gpigeon\' ', "\n", + ' Message => "$enc_msg\n" ', "\n", + ');', "\n", + 'sendmail(%mail) or die $Mail::Sendmail::error;', "\n" + ; + + } + +} + sub escape_arobase { my $mailaddress = shift; my $arobase = '@'; - my $escarobase = q{\@}; + my $espaced_arob = q{\@}; my $escapedmailaddress = $mailaddress; - $escapedmailaddress =~ s/$arobase/$escarobase/; + $escapedmailaddress =~ s/$arobase/$espaced_arob/; return $escapedmailaddress; } @@ -44,6 +85,7 @@ sub notif_if_defined{ my $PASSWD_HASH = q{password_hash_goes_here}; my $cgi_query_get = CGI->new; my $PASSWD = $cgi_query_get->param('password'); +my $HAS_MAILSERVER = 0; if ( sha256_hex($PASSWD) eq $PASSWD_HASH and $ENV{'REQUEST_METHOD'} eq 'POST'){ @@ -59,36 +101,36 @@ href="/gpigeon.css">'; my $mymail_smtport = q{smtp_port_goes_here}; my $mymail_gpgid = q{gpgid_goes_here}; #0xlong keyid form my $myescapedmailaddr = escape_arobase($mymailaddr); - my @text_strings = ('La suppression a réussi !', - 'L'adresse', - 'est valide !', - 'n'est pas valide !', - 'sed "s/Inconnu', # displays on main page table when supposed sender isn't identified - 'La longueur du message doit être inférieure à 10000 charactères.', - 'Formulaire d'envoi de messages GPG', # title for generated links - 'Rentrez votre message ci-dessous, ', - 'M'envoyer le message', - 'Lien généré pour', #displays if link gen is successful - 'lien formulaire gpg', # mail subject when clicking a mailto: link in table - 'Ton lien est', # message when clicking a mailto: link in table - 'Supprimer', # text on button for deleting links - 'Mince! Je ne peux pas ouvrir', # message when file opening fails - 'GPIGEON.CGI: mails GPG pour le non-initié.', # main page title! - 'Salut et bienvenue.', # a greeting at the top of the main page. - 'Se déconnecter', # disconnect button text on main page - 'Actualiser la page', # refresh button text - 'Générer lien', #link generation button text - "Liens générés pour $mymailaddr:", # label above links table - 'Supprimer tous les liens', # delete all links button text - 'Lien', # first table header, 'Link' - 'Pour', # second table header, 'For' - 'Suppression', # third table header, 'Delete' - 'La suppression a échoué. Voici la cause: ' + my @text_strings = ('Succesful deletion!', + 'Address', + 'is valid!', + 'is not valid !', + 'Unknown', # displays on main page table when supposed sender isn't identified + 'Message length must be under 10000 chars.', + 'One time GPG messaging form', # title for generated links + 'Type your message below, ', + 'Send me', + 'Generated a link for', #displays if link gen is successful + 'Link to your one time GPG messaging form', # mail subject when clicking a mailto: link in table + 'Your link is ', # message body when clicking a mailto: link in table + 'Delete', # text on button for deleting links + 'Damn! I cannot open ', # message when file opening fails + 'GPIGEON.CGI: generate one time GPG messaging links !', # main page title! + 'Hi and welcome.', # a greeting at the top of the main page. + 'Disconnect', # disconnect button text on main page + 'Refresh', # refresh button text + 'Generate link', #link generation button text + "Generated links by you, $mymailaddr:", # label above links table + 'Delete all links', # delete all links button text + 'Link', # first table header, 'Link' + 'For', # second table header, 'For' + 'Deletion', # third table header, 'Delete' + 'Deletion failed and here is why : ' ); my $psswd_formfield = '',"\n"; my $SRV_NAME = $ENV{'SERVER_NAME'}; my ($notif_de_creation, $notif_mail_valide, $notif_suppression) = undef; - my @table_des_liens_crees = (); + my @created_links = (); if (defined $cgi_query_get->param('supprlien')){ my $pending_deletion = $cgi_query_get->param('supprlien'); @@ -117,11 +159,11 @@ href="/gpigeon.css">'; } if (defined $cgi_query_get->param('mail')){ - my $entered_mail_addr = scalar $cgi_query_get->param('mail'); - if ( Email::Valid->address($entered_mail_addr) ){ + my $non_gpguser = scalar $cgi_query_get->param('mail'); + if ( Email::Valid->address($non_gpguser) ){ $notif_mail_valide = "$text_strings[1] - $entered_mail_addr $text_strings[2]"; - my $escaped_entered_mail_addr = escape_arobase($entered_mail_addr); + $non_gpguser $text_strings[2]"; + my $escaped_non_gpguser = escape_arobase($input_mail_addr); my $random_mailform_fn_str = String::Random->new; my @mailform_fn_str_buffer = (); for (1..5){ @@ -135,21 +177,16 @@ href="/gpigeon.css">'; my $MAILFORM_RELPATH = "./l/$GENERATED_FORM_FILENAME"; if (open my $mailform_fh, ">", $MAILFORM_RELPATH){ print $mailform_fh '#! /usr/bin/perl -wT',"\n\n", - ' my $demandeur_du_lien = q{', $entered_mail_addr + ' my $non_gpguser = q{', $non_gpguser , '};', "\n", 'delete @ENV{qw(IFS PATH CDPATH BASH_ENV)};', "\n", '$ENV{\'PATH\'}="/usr/bin";', 'use warnings;', "\n", 'use strict;',"\n", 'use GPG;',"\n", - 'use Net::SMTP;',"\n", - 'use Net::SMTPS;',"\n", - 'use CGI::Carp qw(fatalsToBrowser);', - 'use CGI qw(param cookie);', "\n", + '#use CGI::Carp qw(fatalsToBrowser);', + 'use CGI qw(param);', "\n", 'my $cgi_query_get = CGI->new;', "\n", - 'my $smtp = Net::SMTPS->new(\''. $mymail_smtp .'\', Port => - \''. $mymail_smtport .'\', - doSSL => \'ssl\', Debug_SSL => 0);', "\n", 'my ($msg, $enc_msg, $error_processing_msg) = undef;', "\n", 'if (defined $cgi_query_get->param(\'msg\') and $ENV{\'REQUEST_METHOD\'} eq \'POST\'){',"\n", ' $msg = $cgi_query_get->param(\'msg\');', "\n", @@ -160,25 +197,11 @@ href="/gpigeon.css">'; ' my $gpg = new GPG(gnupg_path => "/usr/bin", homedir => "/usr/share/www-data/.gnupg/");', "\n", ' $enc_msg = $gpg->encrypt("De la part de " . - $demandeur_du_lien . ":\n". $msg, \'0x'. $mymail_gpgid .'\') or die - $gpg->error();', "\n", - ' $smtp->auth(\''. $mymailaddr .'\', \''. - $mymailaddr_password .'\') - or die;', "\n", - ' $smtp->mail(\''. $mymailaddr .'\') or die "Net::SMTP module has broke: - $!.";', "\n", - 'if ($smtp->to(\''. $mymailaddr .'\')){', "\n", - '$smtp->data();', "\n", - '$smtp->datasend("To: '. $myescapedmailaddr .'\n");', "\n", - '$smtp->datasend("\n");', "\n", - '$smtp->datasend("$enc_msg\n");', "\n", - '$smtp->dataend();', "\n", - 'unlink "../' . $MAILFORM_RELPATH . '";', "\n", - 'print "Location: /gpigeon/merci/index.html\n\n";', "\n", - '}', "\n", - 'else {', "\n", - 'die $smtp->message();', "\n", - '}', "\n", + $non_gpguser . ":\n". $msg, \'0x'. $mymail_gpgid .'\') or die + $gpg->error();', "\n"; + print_smtp_method($mymailaddr,$mymailaddr_password,$mymailaddr_escaped,$mymail_smtp,$mymail_smtport); + print 'unlink "../' . $MAILFORM_RELPATH . '";', "\n", + 'print "Location: /gpigeon/merci/index.html\n\n";', "\n", '}', "\n", 'print "Content-type: text/html", "\n\n";', "\n", 'print qq{', "\n", @@ -193,7 +216,7 @@ href="/gpigeon.css">'; ' ', "\n", ' ', "\n", '

'. $text_strings[7] . '' - .$escaped_entered_mail_addr .' :

', "\n", + .$escaped_non_gpguser .' :

', "\n", '
', "\n", '