1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl -w
use Mail::GPG;
use MIME::Parser;
use strict;
#no strict 'refs';
my $passphrase = 'MYPW';
my @lines;
my $tmpdir = '/tmp';
########
# Main #
########
my $stdin = &stdin();
my $unencrypted_body = &parse_email($stdin);
if ($unencrypted_body) {
foreach ($$unencrypted_body) {
chomp $_;
push( @lines, $_ );
print "$_\n"; ## Debugging
}
}
###############
# Subroutines #
###############
sub stdin {
#Process stdin
my $stdin = '';
while(<ARGV>){
$stdin .= $_;
}
if ($stdin eq '') {
die ("Error: stdin appears to have no content.");
}
return $stdin;
}
sub parse_email {
my $stdin = shift @_;
my $decoded_body_sref;
my $entity;
#Create new MIME Parser obj
my $parser = MIME::Parser->new;
#Configure MIME Parser
$parser->decode_bodies(0);
$parser->output_under($tmpdir);
#Parse the email from stdin
eval { $entity = $parser->parse_data($stdin) };
# See Mail::GPG for reasoning
# http://search.cpan.org/~jred/Mail-GPG-1.0.7/lib/Mail/GPG.pm#METHODS_FOR_PARSING,_DECRYPTION_AND_VERIFICATION
if ( $entity->effective_type ne 'multipart/signed' and
$entity->effective_type ne 'multipart/encrypted' ) {
#delete tmp files
$parser->filer->purge;
#enable docode_bodies
$parser->decode_bodies(1);
#Parse the email from stdin
eval { $entity = $parser->parse_data($stdin) };
#decrypt
eval { $decoded_body_sref = &decode($entity) };
if ($@) {
warn ("Error: $@");
}
#delete tmp files
$parser->filer->purge;
}
else {
#decrypt
eval { $decoded_body_sref = &decode($entity) };
if ($@) {
warn ("Error $@");
}
#delete tmp files
$parser->filer->purge;
}
return $decoded_body_sref;
}
sub decode {
my $entity = shift @_;
#Create new Mail GPG obj
my $mg = Mail::GPG->new;
#Decrypt the email from stdin
my ($decrypted_entity, $result) = $mg->decrypt (
entity => $entity,
passphrase => $passphrase
);
#Get a reference to the decoded message body
my $decoded_body_sref = $result->get_gpg_stdout;
return $decoded_body_sref;
# my $to = 'chris';
# my $from = 'root';
# my $subject = 'Test Email';
#open(MAIL, "|/usr/sbin/sendmail -t");
#print MAIL "To: $to\n";
#print MAIL "From: $from\n";
#print MAIL "Subject: $subject\n\n";
#print MAIL $decoded_body_sref ;
#close(MAIL);
#print "Email Sent Successfully\n";
}
exit;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
sub decode { my $entity = shift @_; #Create new Mail GPG obj my $mg = Mail::GPG->new; #Decrypt the email from stdin my ($decrypted_entity, $result) = $mg->decrypt ( entity => $entity, passphrase => $passphrase ); #Get a reference to the decoded message body my $decoded_body_sref = $result->get_gpg_stdout; return $decoded_body_sref; # my $to = 'chris'; # my $from = 'root'; # my $subject = 'Test Email'; #open(MAIL, "|/usr/sbin/sendmail -t"); #print MAIL "To: $to\n"; #print MAIL "From: $from\n"; #print MAIL "Subject: $subject\n\n"; #print MAIL $decoded_body_sref ; #close(MAIL); #print "Email Sent Successfully\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
... ### MAIN # we get a reference to body my $unencrypted_body = &parse_email($stdin); # ***NEW*** sent dereferenced body by mail mail_it( $$unencrypted_body ); ... sub decode { my $entity = shift @_; #Create new Mail GPG obj my $mg = Mail::GPG->new; #Decrypt the email from stdin my ($decrypted_entity, $result) = $mg->decrypt ( entity => $entity, passphrase => $passphrase ); #Get a reference to the decoded message body my $decoded_body_sref = $result->get_gpg_stdout; return $decoded_body_sref; # ***NEW*** removed mail sending } # ***NEW*** own subroutine for mail sending sub mail_it { my $mailtext = shift; my $to = 'chris'; my $from = 'root'; my $subject = 'Test Email'; open( my $mailer, "|-", "/usr/sbin/sendmail -t" ) or die "Could not open sendmail: $!"; # one print to print it all print $mailer <<"END_OF_MESSAGE"; To: $to From: $from Subject: $subject $mailtext END_OF_MESSAGE close( $mailer ) or die "Close sendmail failed: $!"; print "Email Sent Successfully\n"; }
return $decoded_body_sref;
#mail_it( $$unencrypted_body );
mail_it( $$decoded_body_sref );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#!/usr/bin/perl -w use Mail::GPG; use MIME::Parser; use Email::Sender::Simple qw(sendmail); use Email::Simple; use Email::Simple::Creator; use strict; #no strict 'refs'; my $passphrase = 'XXX'; my @lines; my $tmpdir = '/tmp'; ######## # Main # ######## my $stdin = &stdin(); my $unencrypted_body = &parse_email($stdin); #mail_it( $$unencrypted_body ); if ($unencrypted_body) { foreach ($$unencrypted_body) { chomp $_; push( @lines, $_ ); print "$_\n"; ## Debugging } } ############### # Subroutines # ############### sub stdin { #Process stdin my $stdin = ''; while(<ARGV>){ $stdin .= $_; } if ($stdin eq '') { die ("Error: stdin appears to have no content."); } return $stdin; } sub parse_email { my $stdin = shift @_; my $decoded_body_sref; my $entity; #Create new MIME Parser obj my $parser = MIME::Parser->new; #Configure MIME Parser $parser->decode_bodies(0); $parser->output_under($tmpdir); #Parse the email from stdin eval { $entity = $parser->parse_data($stdin) }; # See Mail::GPG for reasoning # http://search.cpan.org/~jred/Mail-GPG-1.0.7/lib/Mail/GPG.pm#METHODS_FOR_PARSING,_DECRYPTION_AND_VERIFICATION if ( $entity->effective_type ne 'multipart/signed' and $entity->effective_type ne 'multipart/encrypted' ) { #delete tmp files $parser->filer->purge; #enable docode_bodies $parser->decode_bodies(1); #Parse the email from stdin eval { $entity = $parser->parse_data($stdin) }; #decrypt eval { $decoded_body_sref = &decode($entity) }; if ($@) { warn ("Error: $@"); } #delete tmp files $parser->filer->purge; } else { #decrypt eval { $decoded_body_sref = &decode($entity) }; if ($@) { warn ("Error $@"); } #delete tmp files $parser->filer->purge; } return $decoded_body_sref; } sub decode { my $entity = shift @_; #Create new Mail GPG obj my $mg = Mail::GPG->new; #Decrypt the email from stdin my ($decrypted_entity, $result) = $mg->decrypt ( entity => $entity, passphrase => $passphrase ); #Get a reference to the decoded message body my $decoded_body_sref = $result->get_gpg_stdout; mail_it( $$decoded_body_sref ); return $decoded_body_sref; } # ***NEW*** own subroutine for mail sending sub mail_it { my $mailtext = shift; my $to = 'chris'; my $from = 'root'; my $subject = 'Test Email'; open( my $mailer, "|-", "/usr/sbin/sendmail -t" ) or die "Could not open sendmail: $!"; # one print to print it all print $mailer <<"END_OF_MESSAGE"; To: $to From: $from Subject: $subject $mailtext END_OF_MESSAGE close( $mailer ) or die "Close sendmail failed: $!"; print "Email Sent Successfully\n"; }
1
2
3
4
# Example procmailrc rule
:0
* ^To: <username>@<domain>.<tld>
| /home/<username>/bin/decrypt.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# ***NEW*** own subroutine for mail sending sub mail_it { my $mailtext = shift; my $to = 'chris@localhost'; my $from = 'root@localhost'; my $subject = 'Test Email from decrypt.pl'; my $email = Email::Simple->create( header => [ To => $to, From => $from, Subject => $subject, ], body => $mailtext, ); sendmail($email); # open( my $mailer, "|-", "/usr/sbin/sendmail -t" ) # or die "Could not open sendmail: $!"; # one print to print it all # print $mailer <<"END_OF_MESSAGE"; #To: $to #From: $from #Subject: $subject #$mailtext #END_OF_MESSAGE # close( $mailer ) or die "Close sendmail failed: $!"; # print "Email Sent Successfully\n"; }
cannot chdir to /root from /tmp/msg-1487427712-24006-0: Keine Berechtigung, aborting. at /usr/share/perl5/MIME/Parser/Filer.pm line 715.
%sudo ALL = NOPASSWD: ALL
1
2
3
4
# Example procmailrc rule
:0
* ^To: <username>@<domain>.<tld>
| sudo /home/<username>/bin/decrypt.pl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#!/usr/bin/perl -w use Mail::GPG; use MIME::Parser; use Email::Sender::Simple qw(sendmail); use Email::Simple; use Email::Simple::Creator; use strict; #no strict 'refs'; my $passphrase = 'XXX'; my @lines; my $tmpdir = '/tmp'; ######## # Main # ######## my $stdin = &stdin(); my $unencrypted_body = &parse_email($stdin); mail_it( $$unencrypted_body ); if ($unencrypted_body) { foreach ($$unencrypted_body) { chomp $_; push( @lines, $_ ); print "$_\n"; ## Debugging } } ############### # Subroutines # ############### sub stdin { #Process stdin my $stdin = ''; while(<ARGV>){ $stdin .= $_; } if ($stdin eq '') { die ("Error: stdin appears to have no content."); } return $stdin; } sub parse_email { my $stdin = shift @_; my $decoded_body_sref; my $entity; #Create new MIME Parser obj my $parser = MIME::Parser->new; #Configure MIME Parser $parser->decode_bodies(0); $parser->output_under($tmpdir); #Parse the email from stdin eval { $entity = $parser->parse_data($stdin) }; # See Mail::GPG for reasoning # http://search.cpan.org/~jred/Mail-GPG-1.0.7/lib/Mail/GPG.pm#METHODS_FOR_PARSING,_DECRYPTION_AND_VERIFICATION if ( $entity->effective_type ne 'multipart/signed' and $entity->effective_type ne 'multipart/encrypted' ) { #delete tmp files $parser->filer->purge; #enable docode_bodies $parser->decode_bodies(1); #Parse the email from stdin eval { $entity = $parser->parse_data($stdin) }; #decrypt eval { $decoded_body_sref = &decode($entity) }; if ($@) { warn ("Error: $@"); } #delete tmp files $parser->filer->purge; } else { #decrypt eval { $decoded_body_sref = &decode($entity) }; if ($@) { warn ("Error $@"); } #delete tmp files $parser->filer->purge; } return $decoded_body_sref; } sub decode { my $entity = shift @_; #Create new Mail GPG obj my $mg = Mail::GPG->new; #Decrypt the email from stdin my ($decrypted_entity, $result) = $mg->decrypt ( entity => $entity, passphrase => $passphrase ); #Get a reference to the decoded message body my $decoded_body_sref = $result->get_gpg_stdout; return $decoded_body_sref; } # ***NEW*** own subroutine for mail sending sub mail_it { my $mailtext = shift; my $to = 'chris@localhost'; my $from = 'root@localhost'; my $subject = 'Test Email from decrypt.pl'; my $email = Email::Simple->create( header => [ To => $to, From => $from, Subject => $subject, ], body => $mailtext, ); sendmail($email); }
cannot chdir to /root from /tmp/msg-1487427712-24006-0: Keine Berechtigung, aborting. at /usr/share/perl5/MIME/Parser/Filer.pm line 715.
2017-02-18T15:05:13 Chris_1Ahhhrrr! NIEMALS!!!Vielleicht den User in die root Gruppe packen ?Was besseres ist mir nicht eingefallen.