imaps.monitor 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/perl
  2. #
  3. # Use try to connect to an IMAP server, and
  4. # wait for the right output.
  5. #
  6. # For use with "mon".
  7. #
  8. # Arguments are "-p port -t timeout host [host...]"
  9. #
  10. # Adapted from "http.monitor" by
  11. # Jim Trocki, trockij@transmeta.com
  12. #
  13. # http.monitor written by
  14. #
  15. # Jon Meek
  16. # American Cyanamid Company
  17. # Princeton, NJ
  18. #
  19. # $Id: imap.monitor,v 1.3 2005/08/20 15:27:56 vitroth Exp $
  20. #
  21. # Copyright (C) 1998, Jim Trocki
  22. #
  23. # This program is free software; you can redistribute it and/or modify
  24. # it under the terms of the GNU General Public License as published by
  25. # the Free Software Foundation; either version 2 of the License, or
  26. # (at your option) any later version.
  27. #
  28. # This program is distributed in the hope that it will be useful,
  29. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. # GNU General Public License for more details.
  32. #
  33. # You should have received a copy of the GNU General Public License
  34. # along with this program; if not, write to the Free Software
  35. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36. #
  37. use Getopt::Std;
  38. use English;
  39. use Net::SSLeay::Handle;
  40. getopts ("m:p:t:u:w:s");
  41. $PORT = $opt_p || 143;
  42. $TIMEOUT = $opt_t || 30;
  43. $MAILBOX=$opt_m || undef;
  44. $USERNAME=$opt_u || 'ANONYMOUS';
  45. $PASSWORD=$opt_w || 'ANONYMOUS';
  46. @failures = ();
  47. foreach $host (@ARGV) {
  48. if (! &imapGET($host, $PORT)) {
  49. push (@failures, $host);
  50. }
  51. }
  52. if (@failures == 0) {
  53. exit 0;
  54. }
  55. print join (" ", sort @failures), "\n\n", join ("\n", @longerr), "\n";
  56. exit 1;
  57. sub imapGET {
  58. use Socket;
  59. use Sys::Hostname;
  60. my($Server, $Port) = @_;
  61. my($ServerOK, $TheContent, $cmd);
  62. $ServerOK = 0;
  63. $TheContent = '';
  64. $Path = '/';
  65. ###############################################################
  66. eval {
  67. local $SIG{ALRM} = sub { die "Timeout Alarm" };
  68. alarm $TIMEOUT;
  69. $result = &OpenSocket($Server, $Port); # Open a connection to the server
  70. if ($result == 0) { # Failure to open the socket
  71. push @longerr, "$Server: Unable to connect";
  72. return '';
  73. }
  74. $in = <S>;
  75. if ($in !~ /^\* (OK|PREAUTH|BYE)/) {
  76. alarm 0;
  77. push @longerr, "$Server: No IMAP banner received";
  78. return 0;
  79. }
  80. $cmd="login";
  81. print S "A1 LOGIN ", $USERNAME, " ", $PASSWORD, "\r\n";
  82. while (defined($in=<S>)) {
  83. if ($in =~ /^A1 (\w+) (.*)/) {
  84. if ($1 eq "OK") {
  85. $ServerOK = 1;
  86. } else {
  87. $errmsg="$1 $2";
  88. }
  89. last;
  90. }
  91. }
  92. if ($ServerOK && $MAILBOX) {
  93. $cmd="examine";
  94. $ServerOK=0;
  95. print S "A2 EXAMINE $MAILBOX\r\n";
  96. while (defined($in=<S>)) {
  97. if ($in =~ /^A2 (\w+) (.*)/) {
  98. if ($1 eq "OK") {
  99. $ServerOK = 1;
  100. } else {
  101. $errmsg="$1 $2";
  102. }
  103. last;
  104. }
  105. }
  106. }
  107. if ($ServerOK) {
  108. $cmd="logout";
  109. $ServerOK=0;
  110. print S "A3 LOGOUT\r\n";
  111. while (defined($in=<S>)) {
  112. if ($in =~ /^A3 (\w+) (.*)/) {
  113. if ($1 eq "OK") {
  114. $ServerOK = 1;
  115. } else {
  116. $errmsg="$1 $2";
  117. }
  118. last;
  119. }
  120. }
  121. }
  122. if (!$ServerOK) {
  123. if ($errmsg) {
  124. push @longerr, "$Server: bad response to $cmd: $errmsg";
  125. } else {
  126. push @longerr, "$Server: No response to $cmd";
  127. }
  128. }
  129. close(S);
  130. alarm 0; # Cancel the alarm
  131. };
  132. if ($EVAL_ERROR and ($EVAL_ERROR =~ /^Timeout Alarm/)) {
  133. push @longerr, "$Server: **** Time Out\n";
  134. return 0;
  135. } elsif ($EVAL_ERROR) {
  136. push @longerr, "$Server: $EVAL_ERROR";
  137. return 0;
  138. }
  139. return $ServerOK;
  140. }
  141. sub OpenSocket {
  142. #
  143. # Make a Berkeley socket connection between this program and a TCP port
  144. # on another (or this) host. Port can be a number or a named service
  145. #
  146. local($OtherHostname, $Port) = @_;
  147. local($OurHostname, $sockaddr, $name, $aliases, $proto, $type, $len,
  148. $ThisAddr, $that);
  149. $OurHostname = &hostname;
  150. ($name, $aliases, $proto) = getprotobyname('tcp');
  151. ($name, $aliases, $Port) = getservbyname($Port, 'tcp') unless $Port =~ /^\d+$/;
  152. ($name, $aliases, $type, $len, $ThisAddr) = gethostbyname($OurHostname);
  153. ($name, $aliases, $type, $len, $OtherHostAddr) = gethostbyname($OtherHostname);
  154. my $that = sockaddr_in ($Port, $OtherHostAddr);
  155. if ($opt_s)
  156. {
  157. tie(*S, "Net::SSLeay::Handle", $OtherHostname, $Port) ||
  158. return undef;
  159. }
  160. else
  161. {
  162. $result = socket(S, &PF_INET, &SOCK_STREAM, $proto) || return undef;
  163. $result = connect(S, $that) || return undef;
  164. }
  165. select(S); $| = 1; select(STDOUT); # set S to be un-buffered
  166. return 1; # success
  167. }