-
Notifications
You must be signed in to change notification settings - Fork 27
/
imapPing.pl
executable file
·633 lines (496 loc) · 15.1 KB
/
imapPing.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
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
#!/usr/bin/perl
# $Header: /mhub4/sources/imap-tools/imapPing.pl,v 1.5 2015/12/05 14:57:32 rick Exp $
############################################################################
# Program imapPing.pl #
# Date 20 January 2008 #
# #
# Description #
# #
# This script performs some basic IMAP operations on a user's #
# account and displays the time as each one is executed. The #
# operations are: #
# 1. Connect to the IMAP server #
# 2. Log in with the user's name and password #
# 3. Get a list of mailboxes in the user's account #
# 4. Select the INBOX #
# 5. Get a list of messages in the INBOX #
# 6. Log off the server #
# #
# Usage: imapPing.pl -h <host> -u <user> -p <password> #
# #
############################################################################
# Copyright (c) 2008 Rick Sanders <[email protected]> #
# #
# Permission to use, copy, modify, and distribute this software for any #
# purpose with or without fee is hereby granted, provided that the above #
# copyright notice and this permission notice appear in all copies. #
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES #
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF #
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES #
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN #
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF #
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #
############################################################################
use Getopt::Std;
use Socket;
use FileHandle;
use Fcntl;
use IO::Socket;
use MIME::Base64 qw(encode_base64 decode_base64);
init();
($host,$user,$pwd) = getArgs();
print STDOUT pack( "A35 A10", "Connecting to $host", getTime() );
connectToHost( $host, \$conn );
print STDOUT pack( "A35 A10","Logging in as $user", getTime() );
login( $user,$pwd, $conn );
print STDOUT pack( "A35 A10","Get list of mailboxes", getTime() );
getMailboxList( $conn );
print STDOUT pack( "A35 A10","Selecting the INBOX", getTime() );
selectMbx( 'INBOX', $conn ) if $rc;
print STDOUT pack( "A35 A10","Get list of msgs in INBOX", getTime() );
getMsgList( 'INBOX', $conn );
print STDOUT pack( "A35 A10","Logging out", getTime() );
logout( $conn );
print STDOUT pack( "A35 A10","Done", getTime() );
exit;
exit 1;
sub init {
# Determine whether we have SSL support via openSSL and IO::Socket::SSL
$ssl_installed = 1;
eval 'use IO::Socket::SSL';
if ( $@ ) {
$ssl_installed = 0;
}
getTime();
$debug = 1;
}
sub getTime {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
if ($year < 99) { $yr = 2000; }
else { $yr = 1900; }
$date = sprintf ("%.2d-%.2d-%d.%.2d:%.2d:%.2d \n",
$mon+1,$mday,$year+$yr,$hour,$min,$sec);
$time = sprintf ("%.2d:%.2d:%.2d \n",$hour,$min,$sec);
return $time;
}
sub getArgs {
getopts( "h:u:p:A:I" );
$host = $opt_h;
$user = $opt_u;
$pwd = $opt_p;
$admin_user = $opt_A;
$showIMAP = 1 if $opt_I;
print "opt_h $opt_h\n";
print "opt_p $opt_p\n";
print "opt_u $opt_u\n";
$method = 'CRAM-MD5' if $opt_h =~ /CRAM-MD5/i;
$host =~ s/\/CRAM-MD5//i;
if ( $opt_H ) {
usage();
}
if ( $admin_user ) {
$pwd = 'XXX'; # Don't need the user's password
}
print "host $host pwd $pwd\n";
unless ( $host and $user and $pwd ) {
usage();
exit;
}
return ($host,$user,$pwd);
}
# sendCommand
#
# This subroutine formats and sends an IMAP protocol command to an
# IMAP server on a specified connection.
#
sub sendCommand
{
local($fd) = shift @_;
local($cmd) = shift @_;
print $fd "$cmd\r\n";
print STDOUT ">> $cmd\n" if $showIMAP;
}
#
# readResponse
#
# This subroutine reads and formats an IMAP protocol response from an
# IMAP server on a specified connection.
#
sub readResponse
{
local($fd) = shift @_;
$response = <$fd>;
chop $response;
$response =~ s/\r//g;
push (@response,$response);
print STDOUT "<< $response\n" if $showIMAP;
}
# Make a connection to an IMAP host
sub connectToHost {
my $host = shift;
my $conn = shift;
($host,$port) = split(/:/, $host);
$port = 143 unless $port;
# We know whether to use SSL for ports 143 and 993. For any
# other ones we'll have to figure it out.
$mode = sslmode( $host, $port );
if ( $mode eq 'SSL' ) {
unless( $ssl_installed == 1 ) {
warn("You must have openSSL and IO::Socket::SSL installed to use an SSL connection");
exit;
}
$$conn = IO::Socket::SSL->new(
Proto => "tcp",
SSL_verify_mode => 0x00,
PeerAddr => $host,
PeerPort => $port,
Domain => AF_INET,
);
unless ( $$conn ) {
$error = IO::Socket::SSL::errstr();
warn("Error connecting to $host: $error");
exit;
}
} else {
# Non-SSL connection
$$conn = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port,
);
unless ( $$conn ) {
warn "Error connecting to $host:$port: $@";
exit;
}
}
}
sub sslmode {
my $host = shift;
my $port = shift;
my $mode;
# Determine whether to make an SSL connection
# to the host. Return 'SSL' if so.
if ( $port == 143 ) {
# Standard non-SSL port
return '';
} elsif ( $port == 993 ) {
# Standard SSL port
return 'SSL';
}
unless ( $ssl_installed ) {
# We don't have SSL installed on this machine
return '';
}
# For any other port we need to determine whether it supports SSL
my $conn = IO::Socket::SSL->new(
Proto => "tcp",
SSL_verify_mode => 0x00,
PeerAddr => $host,
PeerPort => $port,
);
if ( $conn ) {
close( $conn );
$mode = 'SSL';
} else {
$mode = '';
}
return $mode;
}
# login
#
# login in at the source host with the user's name and password
#
sub login {
my $user = shift;
my $pwd = shift;
my $conn = shift;
if ( uc( $method ) eq 'CRAM-MD5' ) {
# A CRAM-MD5 login is requested
Log("login method $method");
my $rc = login_cram_md5( $user, $pwd, $conn );
return $rc;
}
if ( $admin_user ) {
# An AUTHENTICATE = PLAIN login has been requested
($authuser,$authpwd) = split(/:/, $admin_user );
login_plain( $user, $authuser, $authpwd, $conn ) or exit;
return 1;
}
if ( $pwd =~ /^oauth2:(.+)/i ) {
$token = $1;
Log("password is an OAUTH2 token");
login_xoauth2( $user, $token, $conn );
return 1;
}
sendCommand ($conn, "1 LOGIN $user $pwd");
while (1) {
readResponse ($conn);
if ($response =~ /^1 OK/i) {
last;
}
elsif ($response !~ /^\*/) {
print STDOUT "Unexpected login response $response\n";
return 0;
}
}
return 1;
}
# login_plain
#
# login in at the source host with the user's name and password. If provided
# with administrator credential, use them as this eliminates the need for the
# user's password.
#
sub login_plain {
my $user = shift;
my $admin = shift;
my $pwd = shift;
my $conn = shift;
# Do an AUTHENTICATE = PLAIN. If an admin user has been provided then use it.
if ( !$admin ) {
# Log in as the user
$admin = $user
}
$login_str = sprintf("%s\x00%s\x00%s", $user,$admin,$pwd);
$login_str = encode_base64("$login_str", "");
$len = length( $login_str );
# sendCommand ($conn, "1 AUTHENTICATE \"PLAIN\" {$len}" );
sendCommand ($conn, "1 AUTHENTICATE PLAIN" );
my $loops;
while (1) {
readResponse ( $conn );
last if $response =~ /\+/;
if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
Log ("unexpected LOGIN response: $response");
exit;
}
$last if $loops++ > 5;
}
sendCommand ($conn, "$login_str" );
my $loops;
while (1) {
readResponse ( $conn );
if ( $response =~ /Microsoft Exchange/i and $conn eq $dst ) {
# The destination is an Exchange server
$exchange = 1;
Log("The destination is an Exchange server");
}
last if $response =~ /^1 OK/i;
if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
Log ("unexpected LOGIN response: $response");
exit;
}
$last if $loops++ > 5;
}
return 1;
}
# login_xoauth2
#
# login in at the source host with the user's name and an XOAUTH2 token.
#
sub login_xoauth2 {
my $user = shift;
my $token = shift;
my $conn = shift;
# Do an AUTHENTICATE = XOAUTH2 login
$login_str = encode_base64("user=". $user ."\x01auth=Bearer ". $token ."\x01\x01", '');
sendCommand ($conn, "1 AUTHENTICATE XOAUTH2 $login_str" );
my $loops;
while (1) {
readResponse ( $conn );
if ( $response =~ /^\+ (.+)/ ) {
$error = decode_base64( $1 );
Log("XOAUTH authentication as $user failed: $error");
return 0;
}
last if $response =~ /^1 OK/;
if ($response =~ /^1 NO|^1 BAD|^\* BYE|failed/i) {
Log ("unexpected LOGIN response: $response");
return 0;
}
$last if $loops++ > 5;
}
Log("login complete") if $debug;
return 1;
}
sub login_cram_md5 {
my $user = shift;
my $pwd = shift;
my $conn = shift;
sendCommand ($conn, "1 AUTHENTICATE CRAM-MD5");
while (1) {
readResponse ( $conn );
last if $response =~ /^\+/;
if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
Log ("unexpected LOGIN response: $response");
return 0;
}
}
my ($challenge) = $response =~ /^\+ (.+)/;
Log("challenge $challenge") if $debug;
$response = cram_md5( $challenge, $user, $pwd );
Log("response $response") if $debug;
sendCommand ($conn, $response);
while (1) {
readResponse ( $conn );
if ( $response =~ /Microsoft Exchange/i and $conn eq $dst ) {
# The destination is an Exchange server
$exchange = 1;
Log("The destination is an Exchange server");
}
last if $response =~ /^1 OK/i;
if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
Log ("unexpected LOGIN response: $response");
return 0;
}
}
Log("Logged in as $user") if $debug;
return 1;
}
sub cram_md5 {
my $challenge = shift;
my $user = shift;
my $password = shift;
eval 'use Digest::HMAC_MD5 qw(hmac_md5_hex)';
use MIME::Base64 qw(decode_base64 encode_base64);
# Adapated from script by Paul Makepeace <http://paulm.com>, 2002-10-12
# Takes user, key, and base-64 encoded challenge and returns base-64
# encoded CRAM. See,
# IMAP/POP AUTHorize Extension for Simple Challenge/Response:
# RFC 2195 http://www.faqs.org/rfcs/rfc2195.html
# SMTP Service Extension for Authentication:
# RFC 2554 http://www.faqs.org/rfcs/rfc2554.html
# Args: tim tanstaaftanstaaf PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
# should yield: dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
my $challenge_data = decode_base64($challenge);
my $hmac_digest = hmac_md5_hex($challenge_data, $password);
my $response = encode_base64("$user $hmac_digest");
chomp $response;
if ( $debug ) {
Log("Challenge: $challenge_data");
Log("HMAC digest: $hmac_digest");
Log("CRAM Base64: $response");
}
return $response;
}
# logout
#
# log out from the source host
#
sub logout {
my $conn = shift;
# print STDOUT "Logging out\n" if $debug;
sendCommand ($conn, "1 LOGOUT");
while ( 1 ) {
readResponse ($conn);
if ( $response =~ /^1 OK/i ) {
last;
}
elsif ( $response !~ /^\*/ ) {
print STDOUT "unexpected LOGOUT response: $response\n";
last;
}
}
close $conn;
return;
}
sub usage {
print STDOUT "\nUsage: imapPing.pl <args> \n\n";
print STDOUT " -h <hostname>\n";
print STDOUT " -u <user>\n";
print STDOUT " -p <password>\n";
print STDOUT "To use CRAM-MD5 for logins add /CRAM-MD5 like this: -h hostname/CRAM-MD5\n";
exit;
}
sub selectInbox {
my $mbx = shift;
my $conn = shift;
# Select a mailbox
sendCommand ($conn, "1 SELECT $mbx");
while (1) {
readResponse ($conn);
if ($response =~ /^1 OK/i) {
last;
}
elsif ($response !~ /^\*/) {
print STDOUT "Unexpected SELECT INBOX response: $response\n";
return 0;
}
}
}
sub getMailboxList {
my $conn = shift;
# Get a list of the user's mailboxes
sendCommand ($conn, "1 LIST \"\" *");
@response = ();
while ( 1 ) {
readResponse ($conn);
last if $response =~ /^1 OK/i;
if ( $response !~ /^\*/ ) {
print STDOUT "unexpected response: $response\n";
return 0;
}
}
@mbxs = ();
for $i (0 .. $#response) {
# print STDERR "$response[$i]\n";
$response[$i] =~ s/\s+/ /;
($dmy,$mbx) = split(/"\/"/,$response[$i]);
$mbx =~ s/^\s+//; $mbx =~ s/\s+$//;
$mbx =~ s/"//g;
if ($mbx =~ /^\#/) {
# Skip public mbxs
next;
}
if ($mbx ne '') {
push(@mbxs,$mbx);
}
}
return 1;
}
sub getMsgList {
my $mailbox = shift;
my $conn = shift;
# Select the mailbox in read-only mode
sendCommand ($conn, "1 EXAMINE \"$mailbox\"");
undef @response;
$empty=0;
while ( 1 ) {
readResponse ($conn);
last if $response =~ /^1 OK/i;
if ( $response !~ /^\*/ ) {
print STDOUT "Error: $response\n";
return 0;
}
}
sendCommand ($conn, "1 FETCH 1:* (UID FLAGS)");
undef @response;
while ( 1 ) {
readResponse ($conn);
last if $response =~ /^1 OK/i;
if ( $response !~ /^\*/ ) {
print STDOUT "Unexpected response: $response\n";
return 0;
}
}
# Get a list of the msgs in the mailbox
#
undef @msgs;
for $i (0 .. $#response) {
$_ = $response[$i];
$_ =~ /\* ([^FETCH]*)/;
$uid = $1;
$uid =~ s/\s+$//;
if ($response[$i] =~ /\\Seen/) { $seen = 1; }
if (($uid ne 'OK') && ($uid ne '')) {
push (@msgs,"$uid $seen");
}
}
return 1;
}
sub Log {
my $string = shift;
print STDERR "$string\n";
}