summaryrefslogtreecommitdiffstats
path: root/contrib/check_pop3.pl
diff options
context:
space:
mode:
authorKarl DeBisschop <kdebisschop@users.sourceforge.net>2004-03-03 04:24:35 +0000
committerKarl DeBisschop <kdebisschop@users.sourceforge.net>2004-03-03 04:24:35 +0000
commit3d87bf2c712e16e25956a2306383bd8a108344de (patch)
treed7d213c7095b1e669bc57c0d6c4c9cf580fa13d8 /contrib/check_pop3.pl
parent190419aee19deee6a107ebff0410118639d91147 (diff)
downloadmonitoring-plugins-3d87bf2c712e16e25956a2306383bd8a108344de.tar.gz
fix loop and \r\n (Jason Burnett - http://sourceforge.net/tracker/index.php?func=detail&aid=895677&group_id=29880&atid=397599)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@840 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'contrib/check_pop3.pl')
-rw-r--r--contrib/check_pop3.pl94
1 files changed, 49 insertions, 45 deletions
diff --git a/contrib/check_pop3.pl b/contrib/check_pop3.pl
index c0c2712c..37f2ce03 100644
--- a/contrib/check_pop3.pl
+++ b/contrib/check_pop3.pl
@@ -1,12 +1,12 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2# ------------------------------------------------------------------------------ 2# ------------------------------------------------------------------------------
3# File Name: check_pop3.pl 3# File Name: check_pop3.pl
4# Author: Richard Mayhew - South Africa 4# Author: Richard Mayhew - South Africa
5# Date: 2000/01/21 5# Date: 2000/01/21
6# Version: 1.0 6# Version: 1.0
7# Description: This script will check to see if an POP3 is running 7# Description: This script will check to see if an POP3 is running
8# and whether authentication can take place. 8# and whether authentication can take place.
9# Email: netsaint@splash.co.za 9# Email: netsaint@splash.co.za
10# ------------------------------------------------------------------------------ 10# ------------------------------------------------------------------------------
11# Copyright 1999 (c) Richard Mayhew 11# Copyright 1999 (c) Richard Mayhew
12# Credits go to Ethan Galstad for coding Nagios 12# Credits go to Ethan Galstad for coding Nagios
@@ -14,15 +14,20 @@
14# changes :) 14# changes :)
15# License GPL 15# License GPL
16# ------------------------------------------------------------------------------ 16# ------------------------------------------------------------------------------
17# Date Author Reason 17# Date Author Reason
18# ---- ------ ------ 18# ---- ------ ------
19# 1999/09/20 RM Creation 19# 1999/09/20 RM Creation
20# 1999/09/20 TP Changed script to use strict, more secure by 20# 1999/09/20 TP Changed script to use strict, more secure by
21# specifying $ENV variables. The bind command is 21# specifying $ENV variables. The bind command is
22# still insecure through. Did most of my work 22# still insecure through. Did most of my work
23# with perl -wT and 'use strict' 23# with perl -wT and 'use strict'
24# 2000/01/20 RM Corrected POP3 Exit State. 24# 2000/01/20 RM Corrected POP3 Exit State.
25# 2000/01/21 RM Fix Exit Codes Again!! 25# 2000/01/21 RM Fix Exit Codes Again!!
26# 2003/12/30 CZ Proper CRLF in communication w/server
27# Fixed infinite loop
28# Error checking on welcome banner, USER, PASS commands
29# Better error condition handling
30
26# ------------------------------------------------------------------------------ 31# ------------------------------------------------------------------------------
27 32
28# -----------------------------------------------------------------[ Require ]-- 33# -----------------------------------------------------------------[ Require ]--
@@ -38,7 +43,7 @@ $ENV{BASH_ENV} = "";
38$|=1; 43$|=1;
39# ------------------------------------------------------------------[ Global ]-- 44# ------------------------------------------------------------------[ Global ]--
40my $TIMEOUT = 60; 45my $TIMEOUT = 60;
41 46
42# -------------------------------------------------------------------[ usage ]-- 47# -------------------------------------------------------------------[ usage ]--
43sub usage 48sub usage
44{ 49{
@@ -69,8 +74,8 @@ sub bindRemote
69 $sockaddr = 'S n a4 x8'; 74 $sockaddr = 'S n a4 x8';
70 $this = pack($sockaddr, AF_INET, 0, $thisaddr); 75 $this = pack($sockaddr, AF_INET, 0, $thisaddr);
71 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr); 76 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
72 if (!bind(ClientSocket, $this)) { print "Connection Refused"; exit 2; } 77 if (!bind(ClientSocket, $this)) { print "Connection Refused\n"; exit 2; }
73 if (!connect(ClientSocket, $that)) { print "Connection Refused"; exit 2; } 78 if (!connect(ClientSocket, $that)) { print "Connection Refused\n"; exit 2; }
74 select(ClientSocket); $| = 1; select(STDOUT); 79 select(ClientSocket); $| = 1; select(STDOUT);
75 return \*ClientSocket; 80 return \*ClientSocket;
76} 81}
@@ -97,48 +102,47 @@ MAIN:
97 my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname); 102 my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
98 103
99 104
100print ClientSocket "user $username\n"; 105 &err("no welcome banner\n") unless $_ = <ClientSocket>;
101 106 &err("bad welcome banner: " . $_) unless $_ =~ /^\+OK/;
102#Debug Server
103#print "user $username\n";
104 107
105#Sleep or 3 secs, incase server is slow. 108 print ClientSocket "USER $username\r\n";
106sleep 3;
107 109
108print ClientSocket "pass $password\n"; 110 &err("no response to USER command\n") unless $_ = <ClientSocket>;
111 &err("bad response to USER: " . $_) unless $_ =~ /^\+OK/;
109 112
110#Debug Server 113 print ClientSocket "PASS $password\r\n";
111#print "pass $password\n";
112 114
113while (<ClientSocket>) { 115 &err("no response to PASS command\n") unless $_ = <ClientSocket>;
116 &err("bad response to PASS: " . $_) unless $_ =~ /^\+OK/;
114 117
115print ClientSocket "pass $password\n"; 118 print ClientSocket "LIST\r\n";
116 119
117#Debug Server 120 my $bad = 1;
118#print $_; 121 my $msgs = 0;
119 122 while (<ClientSocket>) {
120err($_) if (m/\-ERR\s+(.*)\s+.*/); 123 &err(($1||' UNKNOWN')."\n") if (m/\-ERR(.*)/);
121message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/); 124 $bad = 0 if /^\+OK/;
122} 125 $msgs = $1 if /^(\d+)\s+/;
126 last if /^\./;
127 }
128 &message("$msgs\n") unless $bad;
129 &err("missing +OK to LIST command\n");
123} 130}
124 131
125sub message 132sub message
126{ 133{
127 my $answer = "UNKNOWN"; 134 my $msg = shift;
128 $answer = "Pop3 OK - Total Messages On Server :- $1";
129 alarm(0); 135 alarm(0);
130 print ClientSocket "quit\n"; 136 print ClientSocket "QUIT\r\n";
131 print "$answer"; 137 print "POP3 OK - Total Messages On Server: $msg";
132 exit 0; 138 exit 0;
133} 139}
134 140
135sub err 141sub err
136{ 142{
137 my $answer = "UNKNOWN"; 143 my $msg = shift;
138 $answer = "Pop3 Error :- $1";
139 alarm(0); 144 alarm(0);
140 print ClientSocket "quit\n"; 145 print ClientSocket "QUIT\r\n";
141 print "$answer"; 146 print "POP3 Error: $msg";
142 exit 2; 147 exit 2;
143} 148}
144