-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonal.sign.pl
94 lines (84 loc) · 2.3 KB
/
personal.sign.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
#!/bin/perl
#
# SMIME signer for CommuniGate CGP free (implemented as a Content-Filtering script)
#
# Version: 0.1
#
# Copyright (c) 2009 Valera V.Kharseko. This program is free software.
# You can redistribute it and/or modify it under the terms of the
# GNU Public License as found at http://www.fsf.org/copyleft/gpl.html.
#
# Written by [email protected].
use Crypt::SMIME;
use Getopt::Long;
use Pod::Usage;
my $personal_key_path="/var/CommuniGate/personal.keys";
sub Log {
print "* $_[0]\n";
}
$| = 1;
Log "SMIME signer is running";Log "";
mkdir "Submitted" if ( !-d "Submitted" );
while (<>) {
my @line = split( / /, $_ );
chomp( $line[0] );
print "$line[0] OK\n" and next if ( $line[1] =~ /^quit$/i );
print "$line[0] INTF 3\n" and next if ( $line[1] =~ /^intf$/i );
print "$line[0] OK\n" and next if ( $line[1] =~ /^key$/i );
print "$line[0] FAILURE\n" and next if ( $line[1] !~ /^file$/i );
$line[2] =~ s|\\|/|g;
chomp( $line[2] );
Log "SMIME signer process: $line[2]";
if ( !open( MSG, $line[2] ) ) {
Log "Error: file not found $line[2]";
print "$line[0] OK\n";
}
else {
my ( $sender, @recipients );
#CGP headers
while (1) {
$line = <MSG>;
chomp($line);
last if ( $line eq '');
if ( $line =~ /^(\w).+<(.+)>/ ) {
if ( $1 eq 'P' ) {
$sender = lc($2);
}
else {
push @recipients, $2;
}
}
}
#mail headers and body
my $EntireMessage=join("",<MSG>);
close MSG;
if ( $EntireMessage !~ /x-pkcs7-signature/i ) {
if (open(FILE,"$personal_key_path/$sender"))
{
Log "SMIME sign from user=$sender ";
#get keys
my $key;
$key = join("", <FILE>);
close FILE;
#sign
my $smime = Crypt::SMIME->new();
$smime->setPrivateKey($key, $key,"1111");
my $signed=$smime->sign($EntireMessage);
$signed=~s/\r\n/\n/g;
my $alertFileName.="Submitted/A".time().int(rand(10000));
open(SUBM,">$alertFileName.tmp");
print SUBM $signed;
close SUBM;
rename("$alertFileName.tmp","$alertFileName.sub");
print "$line[0] DISCARD\n";
}
else {
Log "SMIME key not found $personal_key_path/$sender"; print "$line[0] OK\n";
}
}
else {
Log "SMIME signer skip (already signed): $line[2]"; print "$line[0] OK\n";
}
}
open STDOUT, ">&STDOUT";
}