-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgkn-milter.new
executable file
·320 lines (254 loc) · 12.2 KB
/
gkn-milter.new
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env perl
use strict;
use Sendmail::PMilter qw(
:all
);
use Sys::Syslog;
use constant DEBUGGING => 1;
# Blocking reason codes:
# Reason 1: Sender and Recipient are the same.
# Reason 2: Sender claims to be from gknstl.com
# Reason 3: Recipient is gknstl.com
# Reason 4: Sender is null.
# Reason 5: Sender is simply [email protected]
my $milter = new Sendmail::PMilter;
my $milter_callbacks = {
'helo' => \&helo_callback,
'envfrom' => \&envfrom_callback,
'envrcpt' => \&envrcpt_callback,
'header' => \&header_callback,
'eom' => \&eom_callback,
};
sub helo_callback{
my $ctx = shift;
my $helo = shift;
# Just store the helo and move on.
# I'm not using it right now, but at some point I might choose
# to keep a database log of which helo's are spammers.
my $private = {
'helo' => $helo
};
$ctx->setpriv($private);
return(SMFIS_CONTINUE);
}
# This gets run when the sender identifies themselves with an e-mail address.
# This is the first things that comes after the ehlo (or helo).
sub envfrom_callback {
my $ctx = shift;
my $sender = shift;
syslog("info","Entered envfrom callback.") if DEBUGGING;
my $private = $ctx->getpriv();
# Clean up the sender address
# Remove any newlines or whitespace.
chomp($sender);
$sender =~ s/\s//g;
# Make sure it is 100% lowercased.
$sender = lc($sender);
# Remove anything before the opening carat, if anything.
$sender =~ s/^.*?</</;
syslog("info","Got the sender: $sender") if DEBUGGING;
# Kill the message if it claims to come from gknstl.com...
if($sender =~ /\@gknstl\.com/){
# Set a proper reply and reject the message before the body even comes through.
syslog("warning","Sender claims to be from gknstl.com. DIE!");
$ctx->setreply('550','5.7.1',"Blocked by GKN Milter, reason 2");
return(SMFIS_REJECT);
}
# Kill the message if it claims to be from [email protected]
if($sender =~ /user\@domain\.com/){
# Set a proper reply and reject the message before the body even comes through.
syslog("warning","Sender claims to be from user\@domain.com. DIE!");
$ctx->setreply('550','5.7.1',"Blocked by GKN Milter, reason 5");
return(SMFIS_REJECT);
}
# Kill the message if the sender is null (aside from <>)
my $sender_test = $sender;
# Remove any spaces, greater than or less than.
$sender_test =~ s/(<|>|\s)//g;
unless($sender_test){
# Set a proper reply and reject the message before the body even comes through.
syslog("warning","Sender is null. Kill it.");
$ctx->setreply('550','5.7.1',"Blocked by GKN Milter, reason 4");
return(SMFIS_REJECT);
}
# Store our sender name to pass into the next loop.
# We can't use global values, as each part of the message can
# be handled by a different process.
$private->{'sender'} = $sender;
# $ctx can assign data to private memory readable by all milter processes.
# This is how we can pass data along to the next part of the milter.
$ctx->setpriv($private);
syslog("info","Assigned the sender to private data. Continuing.") if DEBUGGING;
# Return the continue code so that the MTA will keep going.
return(SMFIS_CONTINUE);
}
# Gets called after the sender admits where the message is coming from.
# We should have the sender's address set in $ctx private data, so we
# can compare at this point.
sub envrcpt_callback {
my $ctx = shift;
my $recipient = shift;
my $unaltered_recipient = $recipient; # So I always have this to work with.
my $private = $ctx->getpriv();
syslog("info","Entered envrcpt callback with sender $private->{'sender'}") if DEBUGGING;
# Clean up our recipient.
# Remove any newlines or whitespace.
chomp($recipient);
$recipient =~ s/\s//g;
# Make sure it is 100% lowercased.
$recipient = lc($recipient);
# Remove anything before the opening carat, if anything.
$recipient =~ s/^.*?</</;
syslog("info","Recipient is: $recipient") if DEBUGGING;
# If it wants to send to gknstl.com...
if($recipient =~ /\@gknstl\.com/){
# Set a proper reply and reject the message before the body even comes through.
syslog("warning","Wants to send to gknstl.com. DIE!");
$ctx->setreply('550','5.7.1',"Blocked by GKN Milter, reason 3");
return(SMFIS_REJECT);
}
# Compare the sender and recipient. Reject if they are the same.
# Remember that we are de-referencing the sender scalar, so 2 $'s are
# required.
if($recipient eq $private->{'sender'}){
syslog("warning","Sender and recipient are the same. KILL! KILL! KILL!!!");
# Purge the private data.
$ctx->setpriv();
# Set a proper reply and reject the message before the body even comes through.
$ctx->setreply('550','5.7.1',"Blocked by GKN Milter, reason 1");
return(SMFIS_REJECT);
}
else{
syslog("info","Sender and recipient are different. Carry on then.") if DEBUGGING;
# Store our sender name to pass into the next loop.
# We can't use global values, as each part of the message can
# be handled by a different process.
# Since there can be multiple recipients, these have to be arrays.
push(@{$private->{'recipient'}},$recipient);
push(@{$private->{'unaltered_recipient'}},$unaltered_recipient);
# $ctx can assign data to private memory readable by all milter processes.
# This is how we can pass data along to the next part of the milter.
$ctx->setpriv($private);
syslog("info","Assigned the recipient to private data. Continuing.") if DEBUGGING;
return(SMFIS_CONTINUE);
}
}
sub header_callback{
my $ctx = shift;
my $header_field = shift;
my $header_value = shift;
my $private = $ctx->getpriv();
# The only header we're inspecting right now is the "To" field.
# Move on unless that's the case.
unless($header_field eq 'To'){
$ctx->setpriv($private);
return(SMFIS_CONTINUE);
}
# It is strange, but you really *can* have multiple To fields. :\
push(@{$private->{'to'}},$header_value);
$ctx->setpriv($private);
return(SMFIS_CONTINUE);
}
sub eom_callback{
my $ctx = shift;
my $private = $ctx->getpriv();
my $index = 0;
# This handles the goofball instance of multiple To headers.
foreach my $to(@{$private->{'to'}}){
syslog("info","Checking To field: $to for plussed users.") if DEBUGGING;
# If the "To" field has a plussed address (NOTE: This is NOT the
# same as the rcpt_to - if there are multiple recipients in the "To"
# field, you might have a plussed address in the header but not the
# rcpt_to!
if ($to =~ /<(.*?)\+(.*?)\@usa\.gknaerospace\.com>/ig){
$to =~ s/<(.*?)\+(.*?)\@(usa\.gknaerospace\.com)>/<$1\@$3>/ig;
syslog("info","Changing $private->{'to'}->[$index] to $to here.");
# Add our altered "To" header, and append the old one as
# "X-Orig-To".
$ctx->chgheader('To',$index,$to);
$ctx->addheader('X-Orig-To',$private->{'to'}->[$index]);
}
$index++;
}
$index = 0;
foreach my $recipient(@{$private->{'unaltered_recipient'}}){
syslog("info","Checking recipient $recipient for plussed users.") if DEBUGGING;
if ($recipient =~ /<(.*?)\+(.*?)\@usa\.gknaerospace\.com>/ig){
$recipient =~ s/<(.*?)\+(.*?)\@(usa\.gknaerospace\.com)>/<$1\@$3>/ig;
syslog("info","Recipient $private->{'unaltered_recipient'}->[$index] was altered to be $recipient. Changing message to match.");
$ctx->addrcpt($recipient);
$ctx->delrcpt($private->{'unaltered_recipient'}->[$index]);
}
$index++;
}
return(SMFIS_CONTINUE);
}
# This is our main() loop. The above it what happens as a bunch of subs,
# but beneath this point we're actually instantiating our daemon.
BEGIN:
{
my $unix_socket = shift(@ARGV);
die("You must specify the path to where you want the socket for this milter to exist! - eg /var/spool/postfix/gknmilter.sock") unless $unix_socket;
print "Opening syslog statement.\n" if DEBUGGING;
# Set up our initial syslogging parameters.
openlog('gknmilter','ndelay,nwait,pid','mail');
syslog("info","Attempting to startup.");
if(-d $unix_socket){ # If they tried to pass us a directory...
syslog("error","The path specified, $unix_socket, is a directory. Exiting.");
die("The sock path, $unix_socket is a directory. Please correct this or choose a different socket path.")
}
# If by chance the socket path exists, we'll try to clean it up
# and continue anyway.
if(-e $unix_socket){
print "The socket path specified already exists. Attempting to clean up and continue...\n";
syslog("warning","The socket path specified already exists. Attempting to clean up and continue.");
unless( unlink($unix_socket) ){
my $username = getpwuid($>);
syslog("error","Failed to remove $unix_socket. We are running as $username, maybe a permissions problem? Exiting.");
die("Failed to remove $unix_socket. We are running as $username, maybe a permissions problem?");
}
else{
syslog("info","Cleanup of $unix_socket succeeded. Continuing.");
print "Succeeded in cleanup. Proceeding!\n";
}
}
syslog("info","Setting up socket at $unix_socket");
print "Setting up socket.\n" if DEBUGGING;
# This should create a new socket.
$milter->setconn("local:$unix_socket");
syslog("info","Socket created, attempting to register callbacks.");
print "Socket created, attempting to register callbacks.\n" if DEBUGGING;
print "Attempting to drop permissions to user postfix.\n" if DEBUGGING;
my $postfix_uid = getpwnam('postfix');
syslog("warning",'Failed to determine the uid for user "postfix". Exiting.') unless $postfix_uid;
die("Failed to determine postfix's uid. Does the user not exist?") unless $postfix_uid;
my $postfix_gid = getgrnam('postfix');
syslog("error",'Failed to determine the gid for group "postfix". Exiting.') unless $postfix_gid;
die("Failed to determine postfix's gid. Does the group not exist?") unless $postfix_gid;
syslog("info","Changing ownership of $unix_socket to postfix:postfix");
chown($postfix_uid,$postfix_gid,$unix_socket);
# This should register our new callbacks.
#$milter->register("gknmilter",$milter_callbacks, SMFI_CURR_ACTS);
my $registration = $milter->register("gknmilter",$milter_callbacks,SMFI_CURR_ACTS,SMFIF_ADDHDRS,SMFIF_ADDRCPT,SMFIF_DELRCPT);
die("We failed our registration! Exiting.") unless $registration;
syslog("info","Callbacks registered. Attempting to drop permissions.");
print "Callbacks registered. Attempting to drop permissions.\n" if DEBUGGING;
# Set our effective UID (EUID) to $postfix_uid.
$> = $postfix_uid;
syslog("error","Failed to drop permissions to postfix user with uid $postfix_uid") unless ($> == $postfix_uid);
die("Failed to drop permissions to postfix user with uid $postfix_uid") unless ($> == $postfix_uid);
print "Successfully dropped permissions to user postfix with uid $postfix_uid.\n" if DEBUGGING;
# Set our effective GID (EGID) to $postfix_gid.
# This is not an error. Apparently to force a single gid, you have to give
# the gid, then give it again in a space-separated list to force setgroup() to use just one.
$) = "$postfix_gid $postfix_gid";
syslog("warning","Failed to drop permissions to postfix group with gid $postfix_gid") unless ( $) eq "$postfix_gid $postfix_gid" );
warn("Failed to drop permissions to postfix group with gid $postfix_gid. Our EGID is $).
This is not fatal, as not all systems support it. Continuing.") unless ( $) eq "$postfix_gid $postfix_gid");
print "Successfully dropped permissions to group postfix with gid $postfix_gid.\n"
if ( DEBUGGING && ( $) eq "$postfix_gid $postfix_gid") );
print "Entering main loop to daemonize.\n" if DEBUGGING;
syslog("info","Entering main loop to daemonize.");
$milter->main();
# Never reaches here, callbacks are called from Milter.
}