summaryrefslogtreecommitdiffstats
path: root/web/attachments/50702-check_pop3.patch
blob: 2c897e383234f8c1dbc7ffc10d20c1fa9faf2f8b (plain)
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
*** check_pop3.pl.orig	2002-02-27 22:42:53.000000000 -0800
--- check_pop3.pl	2003-05-19 14:57:06.000000000 -0700
***************
*** 23,32 ****
  #				with perl -wT and 'use strict'
  # 2000/01/20	RM		Corrected POP3 Exit State.
  # 2000/01/21	RM		Fix Exit Codes Again!!
  # ------------------------------------------------------------------------------
  
  # -----------------------------------------------------------------[ Require ]--
! require 5.004;
  
  # --------------------------------------------------------------------[ Uses ]--
  use Socket;
--- 23,34 ----
  #				with perl -wT and 'use strict'
  # 2000/01/20	RM		Corrected POP3 Exit State.
  # 2000/01/21	RM		Fix Exit Codes Again!!
+ # 2003/05/17	CS		Cleaned up little, simplifed logic flow, better 
+ #                               checking of return status from server.
  # ------------------------------------------------------------------------------
  
  # -----------------------------------------------------------------[ Require ]--
! use 5.004;
  
  # --------------------------------------------------------------------[ Uses ]--
  use Socket;
***************
*** 37,43 ****
  $ENV{BASH_ENV} = "";
  $|=1;
  # ------------------------------------------------------------------[ Global ]--
! my $TIMEOUT = 60;
  	
  # -------------------------------------------------------------------[ usage ]--
  sub usage
--- 39,46 ----
  $ENV{BASH_ENV} = "";
  $|=1;
  # ------------------------------------------------------------------[ Global ]--
! my $TIMEOUT = 30;
! my $DEBUG = 0;
  	
  # -------------------------------------------------------------------[ usage ]--
  sub usage
***************
*** 65,78 ****
  	my $that;
  	my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  
! 	if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; }
  	$sockaddr = 'S n a4 x8';
  	$this = pack($sockaddr, AF_INET, 0, $thisaddr);
  	$that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
! 	if (!bind(ClientSocket, $this)) { print "Connection Refused"; exit 2; }
! 	if (!connect(ClientSocket, $that)) { print "Connection Refused"; exit 2; }
! 	select(ClientSocket); $| = 1; select(STDOUT);
! 	return \*ClientSocket;
  }
  
  # ====================================================================[ MAIN ]==
--- 68,88 ----
  	my $that;
  	my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  
! 	if (!socket(POPS,AF_INET, SOCK_STREAM, $proto)) {
! 		err("$!");
! 	}
  	$sockaddr = 'S n a4 x8';
  	$this = pack($sockaddr, AF_INET, 0, $thisaddr);
  	$that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
! 	if (!bind(POPS, $this)) {
! 		err("$!");
! 	}
! 	if (!connect(POPS, $that)) {
! 		err("$!");
! 	}
! 	select(POPS); $|=1;
! 	select(STDOUT); $|=1;
! 	return \*POPS;
  }
  
  # ====================================================================[ MAIN ]==
***************
*** 86,92 ****
  
  	# Just in case of problems, let's not hang Nagios
  	$SIG{'ALRM'} = sub {
! 		print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  		exit -1;
  	};
  	
--- 96,102 ----
  
  	# Just in case of problems, let's not hang Nagios
  	$SIG{'ALRM'} = sub {
! 		print "POP3 Error: Timeout after $TIMEOUT seconds\n";
  		exit -1;
  	};
  	
***************
*** 94,144 ****
  
  	chop($hostname = `hostname`);
  	my ($name, $alias, $proto) = getprotobyname('tcp');
! 	my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
! 	
! 
! print ClientSocket "user $username\n";
! 
! #Debug Server
! #print "user $username\n";
! 
! #Sleep or 3 secs, incase server is slow.
! sleep 3;
  
! print ClientSocket "pass $password\n";
! 
! #Debug Server
! #print "pass $password\n";
! 
! while (<ClientSocket>) {
  
! print ClientSocket "pass $password\n";
  
! #Debug Server
! #print $_;
  
! err($_) if (m/\-ERR\s+(.*)\s+.*/);
! message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/);
! }
  }
  
! sub message 
  {
! 	my $answer = "UNKNOWN";
!   	$answer = "Pop3 OK - Total Messages On Server :- $1";	
! 	alarm(0);
! 	print ClientSocket "quit\n";
! 	print "$answer";
! 	exit 0;
  }
  
  sub err
  {
! 	my $answer = "UNKNOWN";
! 	$answer = "Pop3 Error :- $1";
  	alarm(0);
! 	print ClientSocket "quit\n";
! 	print "$answer";
  	exit 2;
  }
  
--- 104,162 ----
  
  	chop($hostname = `hostname`);
  	my ($name, $alias, $proto) = getprotobyname('tcp');
! 	my $POPS = &bindRemote($remotehost,$remoteport,$hostname);
  
! 	# Wait for server to come ready
! 	alarm($TIMEOUT);
! 	$_ = <POPS>;
! 	if (! /^.OK/) { err($_, *POPS); }
! 	print STDERR "<< $_" if $DEBUG;
  
! 	# Send login name
! 	alarm($TIMEOUT);
! 	print STDERR ">> user $username\n" if $DEBUG;
! 	print POPS "user $username\n";
! 	$_ = <POPS>;
! 	if (! /^.OK/) { err($_, *POPS); }
! 	print STDERR "<< $_" if $DEBUG;
  
! 	# Send login password
! 	alarm($TIMEOUT);
! 	print STDERR ">> pass $password\n" if $DEBUG;
! 	print POPS "pass $password\n";
! 	$_ = <POPS>;
! 	if (! /^.OK/) { err($_, *POPS); }
! 	print STDERR "<< $_" if $DEBUG;
! 	
! 	# Return the good news
! 	print "POP3 OK - $_";
  
! 	logout(*POPS);
  }
  
! sub logout 
  {
! 	my $pops = shift;
! 
! 	print STDERR ">> quit\n" if $DEBUG;
! 	print $pops "quit\n";
! 	$_ = <$$pops>;
! 	print STDERR "<< $_" if $DEBUG;
! 	close($pops);
! 	print STDERR "\nClosed socket\n" if $DEBUG;
! 
  }
  
  sub err
  {
! 	my ($msg, $pops) = @_;
! 
  	alarm(0);
! 	print "POP3 Error: $msg";
! 
! 	if (defined($pops)) {
! 		logout($pops);
! 	}
  	exit 2;
  }