-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowbox
executable file
·788 lines (695 loc) · 18.1 KB
/
snowbox
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#!/usr/bin/perl
#
# Snowbox - a simple POP3 server written in Perl
# Version 1.0.1
#
# Copyright 2004-2008 Oliver Feiler <[email protected]>
# http://snowbox.kcore.de/
#
#############################################################################
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License version 2 as #
# published by the Free Software Foundation. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
#############################################################################
use strict;
use English;
use Digest::MD5 qw(md5_hex);
use Sys::Syslog qw(:DEFAULT setlogsock);
use Fcntl':flock';
use Fcntl;
use Socket;
use Time::HiRes qw( usleep );
###
# Default mail system configuration. Edit the config file
# /etc/snowbox/config to reflect your local configuration.
###
my $snowbox_config = "/etc/snowbox/config"; ### change this if neccessary ###
my $passwordfile = "/etc/snowbox/user.auth";
my $maildrops = "/var/mail/";
my $maildrop_gid = "mail";
#my $maildrop_format = "mbox";
my $loglevel = 1; # 0: none
# 1: failed logins
# 2: +all other failed commands
# 3: a lot
config();
###
# Internal variables
###
my $logged_in = 0; # Set to 1 after successful user login
my $input;
my $command;
my $argument;
my $argument2;
my $user;
my $pass;
my $num_messages;
my $maildrop_size;
my $myhostname = `hostname`;
chomp($myhostname);
my $apop_stamp = "<$PID.".time()."\@$myhostname>";
my @maildrop;
my $maildrop_override; # Configfile override
my $maildrop_changed = 0;
my $conn_timeout = 60; # Connection times out after 1 Min of
# inactivity. Snowbox then exits.
my $remote = "127.0.0.1";
# Get IP of connected client
# If we are not connected to a socket (run from cmd line) ignore the lookup
if (getsockname(STDIN)) {
$remote = inet_ntoa((unpack_sockaddr_in(getpeername(STDIN)))[1]);
}
# Install signal handlers to clean up after sudden disconnect
$SIG{'HUP'} = 'signal_handler';
$SIG{'INT'} = 'signal_handler';
$SIG{'PIPE'} = 'signal_handler';
$SIG{'ALRM'} = 'signal_handler'; # This fires when $conn_timeout is reached
$| = 1; # make unbuffered
openlog ('snowbox', 'pid', 'user'); # open the syslog facility
setlogsock('unix'); # specify unix socket for logging?
if ($loglevel >= 3) {
syslog('debug', "connection from $remote.");
}
###
# server starts
###
print "+OK Hi, nice to meet you. POP3 server ready $apop_stamp\r\n";
while (1) {
eval {
alarm($conn_timeout);
$input = <STDIN>;
alarm(0);
};
$input =~ tr/\r\n//d; # Remove trailing \r\n
# Commands in the POP3 consist of a case-insensitive keyword, possibly
# followed by one or more arguments.
($command,$argument,$argument2) = split(/ /,$input);
if (!defined($command)) {
pop_cmd_unknown();
next;
}
# Check length of input. Command may be up to 4 char, arg up to 40
if ((length($command) > 4) ||
(length($argument) > 40) ||
(length($argument2) > 40)) {
pop_cmd_too_long();
next;
}
$command =~ tr/a-z/A-Z/; # Convert commands to uppercase
# Recalc $num_messages and $maildrop_size
$num_messages = 0;
$maildrop_size = 0;
foreach (@maildrop) {
if (%$_->{"deleted"} == 0) {
$num_messages++;
$maildrop_size += %$_->{"len"};
}
}
if ($command eq "USER") {
if (!defined($argument)) {
pop_cmd_unknown();
next;
}
# Don't allow switching of usernames once logged in.
if ($logged_in == 1) {
print "-ERR Already logged in.\r\n";
next;
}
if (defined($user)) {
print "-ERR User name already given.\r\n";
next;
}
$user = $argument;
$user =~ tr/a-zA-Z0-9\-\._+//cd; # Sanitize user name
print "+OK May I have your password please?\r\n";
}
elsif ($command eq "PASS") {
if ($logged_in == 1) {
print "-ERR Already logged in.\r\n";
next;
}
$pass = $argument;
auth_user("PLAIN", $user, $pass);
# authenticated
$logged_in = 1;
load_maildrop($user, "LOGIN");
}
elsif ($command eq "APOP") {
if ($logged_in == 1) {
print "-ERR Already logged in.\r\n";
next;
}
$user = $argument;
$pass = $argument2;
$user =~ tr/a-zA-Z0-9\-\._+//cd; # Sanitize user name
auth_user("APOP", $user, $pass);
# authenticated
$logged_in = 1;
load_maildrop($user, "LOGIN");
}
elsif ($command eq "AUTH") {
if ($logged_in == 1) {
print "-ERR Already logged in.\r\n";
next;
}
pop_cmd_auth();
}
elsif ($command eq "STAT") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_stat();
}
elsif ($command eq "LIST") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_list($argument);
}
elsif ($command eq "RETR") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_retr($argument);
}
elsif ($command eq "TOP") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_top($argument, $argument2);
}
elsif ($command eq "DELE") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_dele($argument);
}
elsif ($command eq "NOOP") {
pop_cmd_noop();
}
elsif ($command eq "RSET") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
# this is very slow
@maildrop = ();
load_maildrop($user, "RSET");
}
elsif ($command eq "QUIT") {
pop_cmd_quit($logged_in);
}
elsif ($command eq "UIDL") {
if (!$logged_in) {
pop_cmd_not_logged_in();
next;
}
pop_cmd_uidl($argument);
}
elsif ($command eq "CAPA") {
pop_cmd_capa();
}
else {
pop_cmd_unknown($command);
}
}
sub auth_user {
my $sysuser; # Username in passwordfile
my $syspass; # Password in passwordfile
my $digest;
my $access_granted = 0;
my $uid;
my $gid;
my @pwdarray;
my $auth = shift;
my $user = shift;
my $pass = shift;
my $user_override; # Configfile override
if (!open (AUTHFILE, "$passwordfile")) {
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: could not open authorization file for reading.");
}
die "-ERR Internal error. Could not read authorization file.\r\n";
}
while (<AUTHFILE>) {
chomp;
if (/^#/) {
next;
}
# Reset!
$sysuser = undef;
$syspass = undef;
$maildrop_override = undef;
$user_override = undef;
/^([^\s]+)\s+([^\s]+)(\s+([^\s]+))?(\s+([^\s]+))?/;
$sysuser = $1;
$syspass = $2;
if (defined($4)) {
$maildrop_override = $4;
}
if (defined($6)) {
$user_override = $6;
}
if ($user eq $sysuser) {
if ($auth eq "APOP") {
$digest = md5_hex($apop_stamp.$syspass);
if ($pass eq $digest) {
$access_granted = 1;
last;
}
} elsif ($auth eq "PLAIN") {
if ($pass eq $syspass) {
$access_granted = 1;
last;
}
}
}
}
close (AUTHFILE);
# Delay for some microseconds, since it might be possible to guess
# if a given user account exists with the above login code based
# on the time the server needs to answer the request.
my $random_sleep_usec = int(rand(100000));
if ($loglevel >= 3) {
syslog('debug', "delaying login for $random_sleep_usec usecs.");
}
usleep($random_sleep_usec);
if (!$access_granted) {
print "-ERR Login incorrect.\r\n";
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: failed login for \'$user\' using $auth.");
}
exit(1);
}
if ($loglevel >= 3) {
syslog('debug', "connection from $remote: \'$user\' logged in successfully using $auth.");
}
# Change to uid of logged user and gid of mail spool
@pwdarray = getgrnam($maildrop_gid);
$gid = $pwdarray[2];
if (defined($user_override)) {
@pwdarray = getpwnam($user_override);
} else {
@pwdarray = getpwnam($user);
}
$uid = $pwdarray[2];
if (!defined($uid)) {
syslog('warning', "connection from $remote: Configuration error. Login user \'$user\' is not a valid user on the system and does not have a valid override! Please check the config!");
print "-ERR Configuration error. Login user is not valid and does not have a valid override. Check config!\r\n";
exit(2);
}
$GID = $gid;
$EGID = "$gid $gid";
if ($loglevel >= 3) {
syslog('debug', "connection from $remote: changed gid to \'$EGID\'.");
}
$UID = $uid;
$EUID = $uid;
if ($EUID != $uid) {
syslog('warning', "connection from $remote: Internal error. Could not change uid to \'$uid\'. Not good.");
print "-ERR Internal error. setuid() failed. Not good.\r\n";
exit(2);
}
if ($loglevel >= 3) {
syslog('debug', "connection from $remote: changed uid to \'$EUID\'.");
}
}
sub load_maildrop {
my $user = shift;
my $state = shift;
my $line = "";
my $blankline = 1; # First mesg is not preceded by a blank line
my $count = 1;
my $maildroppath = "$maildrops/$user";
if (defined($maildrop_override)) {
$maildroppath = $maildrop_override;
}
$num_messages = 0;
$maildrop_size = 0;
$maildrop_changed = 0;
if (!open (MAILDROP, "+<$maildroppath")) {
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: mailbox \'$maildroppath\' for \'$user\' does not exist or is unreadable. Please check file permissions and configuration!");
}
die "-ERR Mailbox for this user does not exist.\r\n";
}
if ($state eq "LOGIN") {
acquire_locks();
}
while (<MAILDROP>) {
if ((/^From /) && ($blankline == 1)) {
$blankline = 0;
$num_messages++;
if ($line ne "") {
my %msghash;
$maildrop_size += length($line);
$msghash{"body"} = $line;
$msghash{"deleted"} = 0;
$msghash{"num"} = $count;
$msghash{"len"} = length($line);
my $uid;
$line =~ /\n(Message-ID: .*?)\n/i;
$uid = $1;
if (!$uid) {
$line =~ /^(From .*?)\n/i;
$uid = $1;
}
$msghash{"uid"} = $uid;
push(@maildrop, \%msghash);
$count++;
}
$line = "";
}
$line = $line.$_;
if ($_ eq "\n") {
$blankline = 1;
} else {
$blankline = 0;
}
}
if ($num_messages >= 1) {
my %msghash;
$maildrop_size += length($line);
$msghash{"body"} = $line;
$msghash{"deleted"} = 0;
$msghash{"num"} = $count;
$msghash{"len"} = length($line);
my $uid;
$line =~ /\n(Message-ID: .*?)\n/i;
$uid = $1;
if (!$uid) {
$line =~ /^(From .*?)\n/i;
$uid = $1;
}
$msghash{"uid"} = $uid;
push(@maildrop, \%msghash);
}
print "+OK Welcome $user, you have $num_messages messages ($maildrop_size octets)\r\n";
}
###
# POP3 commands
###
sub pop_cmd_auth {
print "+OK I know the following authentication methods:\r\n".
"APOP\r\n".
".\r\n";
}
sub pop_cmd_capa {
print "+OK Capability list follows\r\n".
"TOP\r\n".
"USER\r\n".
"UIDL\r\n".
"EXPIRE NEVER\r\n".
".\r\n";
}
sub pop_cmd_dele {
my $message = shift;
$message =~ tr/0-9//cd; # Must only contain 0-9
if (check_valid_msg_num($message) == -1) {
return;
}
if ($message <= scalar(@maildrop)) {
if ($maildrop[$message-1]->{"deleted"} == 0) {
$maildrop[$message-1]->{"deleted"} = 1;
$maildrop_changed = 1;
print "+OK message $message deleted.\r\n";
} else {
print "-ERR message $message already deleted.\r\n";
}
} else {
print_err_no_such_msg();
}
}
sub pop_cmd_list {
my $message = shift;
if (!defined($message)) {
print "+OK $num_messages messages ($maildrop_size octets)\r\n";
foreach (@maildrop) {
# Do not include deleted messages in listing
if (%$_->{"deleted"} == 0) {
print %$_->{"num"}." ".%$_->{"len"}."\r\n";
}
}
print ".\r\n";
} else {
$message =~ tr/0-9//cd; # Must only contain 0-9
if (check_valid_msg_num($message) == -1) {
return;
}
# Print -ERR for deleted messages
if (($message <= scalar(@maildrop)) && ($maildrop[$message-1]->{"deleted"} == 0)) {
print "+OK $message ".$maildrop[$message-1]->{"len"}."\r\n";
} else {
print_err_no_such_msg();
}
}
}
sub pop_cmd_noop {
print "+OK *sighs idly*\r\n";
}
sub pop_cmd_quit {
my $logged_in = shift;
if ($logged_in) {
if ($maildrop_changed == 1) {
# Write maildrop
seek (MAILDROP, 0, 0);
truncate (MAILDROP, 0);
foreach (@maildrop) {
if (%$_->{"deleted"} == 0) {
print MAILDROP %$_->{"body"};
}
}
}
nuke_locks();
}
print "+OK POP3 server signing off. Have a nice day.\r\n";
closelog(); # Close the syslog
exit(0);
}
sub pop_cmd_retr {
my $message = shift;
my $msg;
if (!defined($message)) {
print_err_no_such_msg();
return;
}
$message =~ tr/0-9//cd; # Must only contain 0-9
if (check_valid_msg_num($message) == -1) {
return;
}
# Don't return deleted message
if ($message <= scalar(@maildrop)) {
if ($maildrop[$message-1]->{"deleted"} == 1) {
print "-ERR message was deleted.\r\n";
return;
}
}
if ($message <= scalar(@maildrop)) {
print "+OK ".$maildrop[$message-1]->{"len"}." octets\r\n";
$msg = $maildrop[$message-1]->{"body"};
$msg =~ s/\n/\r\n/g; # Replace all line endings with CRLF
$msg =~ s/\r\n\.(?=\r\n)/\r\n\.\./g; # Prepad .CRLF -> ..CRLF
print $msg;
print ".\r\n";
} else {
print_err_no_such_msg();
}
}
sub pop_cmd_stat {
print "+OK $num_messages $maildrop_size\r\n";
}
sub pop_cmd_top {
my $message = shift;
my $lines = shift;
my $msg;
if (!defined($message)) {
print_err_no_such_msg();
return;
}
if (!defined($lines) || $lines eq '' || $lines < 0 ) {
pop_cmd_unknown();
return;
}
$message =~ tr/0-9//cd; # Must only contain 0-9
if (check_valid_msg_num($message) == -1) {
return;
}
# Don't return deleted message
if ($message <= scalar(@maildrop)) {
if ($maildrop[$message-1]->{"deleted"} == 1) {
print "-ERR message was deleted.\r\n";
return;
}
}
if ($message <= scalar(@maildrop)) {
print "+OK\r\n";
$msg = $maildrop[$message-1]->{"body"};
my $foundnl = 0;
my @lines = split (/\n/, $msg, -1);
# Mail always ends by \n. Drop off last empty field
pop (@lines);
foreach my $line (@lines)
{
$line =~ s/^\.$/\.\./g; # Prepad .CRLF -> ..CRLF
print "$line\r\n";
$foundnl |= ($line eq "");
last if ($foundnl && ($argument2-- <= 0))
}
print ".\r\n";
} else {
print_err_no_such_msg();
}
}
sub pop_cmd_uidl {
my $message = shift;
if (!defined($message)) {
print "+OK Just a second, UIDL listing follows.\r\n";
foreach (@maildrop) {
if (%$_->{"deleted"} == 0) {
print %$_->{"num"}." ".md5_hex(%$_->{"uid"})."\r\n";
}
}
print ".\r\n";
} else {
$message =~ tr/0-9//cd; # Must only contain 0-9
if (check_valid_msg_num($message) == -1) {
return;
}
if (($message <= scalar(@maildrop)) && ($maildrop[$message-1]->{"deleted"} == 0)) {
print "+OK ".$maildrop[$message-1]->{"num"}." ".md5_hex($maildrop[$message-1]->{"uid"})."\r\n";
} else {
print_err_no_such_msg();
}
}
}
sub pop_cmd_unknown {
my $command = shift;
print "-ERR I have no idea what you want from me.\r\n";
if ($loglevel >= 3) {
syslog('debug', "connection from $remote: client does not speak proper POP3 (unknown command: $command).");
}
}
sub check_valid_msg_num {
my $message = shift;
if (($message eq "") || ($message < 1)) {
print_err_no_such_msg();
return -1;
}
return 0;
}
sub pop_cmd_not_logged_in {
print "-ERR You are not logged in.\r\n";
if ($loglevel >= 2) {
syslog('info', "connection from $remote: client sent transaction command while not logged in.");
}
}
sub pop_cmd_too_long {
print "-ERR Command too long.\r\n";
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: command sent was too long.");
}
}
sub print_err_no_such_msg {
print "-ERR No such message.\r\n";
}
sub acquire_locks {
my $mtime;
# Lock the maildrop and exit if it is locked
if (!flock (MAILDROP, LOCK_EX | LOCK_NB)) {
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: could not lock mailbox for \'$user\' (flock).");
}
die "-ERR Mailbox is locked. Is another POP3 session active?\r\n";
}
my($dotlockfile);
if (defined($maildrop_override)) {
$dotlockfile = "$maildrop_override.lock";
} else {
$dotlockfile = "$maildrops/$user.lock";
}
# see of there is a dotlock. try to delete it if older than 15 mins
if (-e"$dotlockfile") {
# Racey!
$mtime = (stat("$dotlockfile"))[9];
if (time()-$mtime > 900) {
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: dotlock for \'$user\' is older than 15 min, ignoring.");
}
unlink ("$dotlockfile");
}
}
# also set a dotlock here in case other programs ignore flock.
if (!sysopen (DOTLOCK, "$dotlockfile", O_CREAT | O_RDWR | O_EXCL)) {
if ($loglevel >= 1) {
syslog('warning', "connection from $remote: could not lock mailbox for \'$user\' (dotlock).");
}
die "-ERR Mailbox is locked. Is another POP3 session active?\r\n";
}
print DOTLOCK $PID;
close (DOTLOCK);
}
sub nuke_locks {
flock (MAILDROP, LOCK_UN);
my($dotlockfile);
if (defined($maildrop_override)) {
$dotlockfile = "$maildrop_override.lock";
} else {
$dotlockfile = "$maildrops/$user.lock";
}
unlink ("$dotlockfile");
close (MAILDROP);
}
sub signal_handler {
# Remove the locks if we get killed from inetd (SIGPIPE?)
if ($logged_in) {
nuke_locks();
if ($loglevel >= 2) {
syslog('info', "connection from $remote: connection aborted, removing locks.");
}
} else {
if ($loglevel >= 2) {
syslog('info', "connection from $remote: connection aborted.");
}
}
exit(2);
}
###
# Load global configuration. If no config file exists the defaults
# specified on top of the script are used.
###
sub config {
open (CONFIG, "$snowbox_config") || return; # and use default config
while (<CONFIG>) {
chomp;
if (/^#/) {
next;
}
/^(.*?)\s+(.*)$/;
if ($1 eq "authfile") {
$passwordfile = $2;
} elsif ($1 eq "maildir") {
$maildrops = $2;
} elsif ($1 eq "maildir_gid") {
$maildrop_gid = $2;
} elsif ($1 eq "loglevel") {
$loglevel = $2;
#} elsif ($1 eq "maildrop_format") {
# $maildrop_format = $2;
}
}
close (CONFIG);
}