check_wins.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/perl -w
  2. # $Id$
  3. # $Log$
  4. # Revision 1.1 2003/02/09 14:16:28 sghosh
  5. # more contribs
  6. #
  7. use strict ;
  8. use Getopt::Long ;
  9. use vars qw($opt_H $opt_D $opt_W $opt_T $debug @my_dcs);
  10. use lib '/usr/local/netsaint/libexec/' ;
  11. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  12. my $PROGNAME = 'check_wins' ;
  13. use constant SAMBA_DEBUG_LVL => 2 ;
  14. use constant MY_DCS => ('dc1','dc2') ;
  15. # use constant MY_DCS => qw(ipa01 ipa02 ipa03) ;
  16. my $NMBLOOKUP_PATH = '/usr/bin/nmblookup' ;
  17. my $NMBLOOKUP = sub { return `$NMBLOOKUP_PATH $_[2] -R -U $_[0] $_[1]` } ;
  18. my $NMBLOOKUP_CMD = $NMBLOOKUP_PATH . ' -R -U' ;
  19. sub print_help ();
  20. sub print_usage ();
  21. sub help ();
  22. sub version ();
  23. delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
  24. $SIG{"ALRM"} = sub { die "Alarm clock restart" } ;
  25. Getopt::Long::Configure('bundling', 'no_ignore_case');
  26. GetOptions
  27. ("V|version" => \&version,
  28. "h|help" => \&help,
  29. "d|debug" => \$debug,
  30. "C|controllers:s" => \@my_dcs,
  31. "T|timeout:i" => \$opt_T,
  32. "W|wins=s" => \$opt_W,
  33. "D|domain=s" => \$opt_D);
  34. ($opt_D) || usage("MS Domain name not specified\n");
  35. my $domain = $1 if $opt_D =~ m#(\w+)# ; # NetBIOS names allow __any__ characters (more than \w)
  36. ($domain) || usage("Invalid MS D name: $opt_D\n");
  37. ($opt_W) || usage("WINS hostname or address not specified\n");
  38. my $wins = $1 if $opt_W =~ m#((?:^\w+$)|(?:\d+(?:\.\d+){3,3}$))# ;
  39. ($wins) || usage("Invalid WINS hostname or address: $opt_W\n");
  40. usage("You must provide the names of your domain controllers by updating MY_DCS in the text or use -C dc_name1 -C dc_name2 ..\n")
  41. unless (@my_dcs or MY_DCS) ;
  42. @my_dcs = MY_DCS unless defined $my_dcs[0] ;
  43. $TIMEOUT = $opt_T if defined $opt_T ;
  44. my ($netbios_name, @dcs_of_domain, @dc_addresses) ;
  45. my (@addr_dcs_of_domain, @found_dcs, %address2dc) ;
  46. my (@dc_query) ;
  47. # tsitc> /usr/local/samba/bin/nmblookup -R -U wins ipa01
  48. # Sending queries to 10.0.100.29
  49. # 10.0.100.16 ipa01<00>
  50. eval {
  51. alarm($TIMEOUT) ;
  52. @dc_query = $debug ? map { $NMBLOOKUP->($wins, "$_#20", '-d ' . SAMBA_DEBUG_LVL) } @my_dcs :
  53. map { ( $NMBLOOKUP->($wins, "$_#20", '') )[1] } @my_dcs ;
  54. alarm(0) ;
  55. } ;
  56. if ($@ and $@ =~ /Alarm clock restart/) {
  57. print qq(Failed. Timeout while waiting for response from (one of) "$NMBLOOKUP_CMD $wins @my_dcs"\n) ;
  58. exit $ERRORS{"CRITICAL"} ;
  59. }
  60. if ($@ and $@ !~ /Alarm clock restart/) {
  61. print qq(Failed. "$@" in response to "NMBLOOKUP_CMD $wins @my_dcs"\n) ;
  62. exit $ERRORS{"UNKNOWN"} ;
  63. }
  64. chomp @dc_query ;
  65. if ( scalar grep(/name_query failed/, @dc_query) ) {
  66. print qq(Failed. WINS "$wins" failed to resolve), scalar @my_dcs > 1 ? ' at least one of ' : ' ', qq("@my_dcs", the domain controller(s) of "$domain". Got "@dc_query"\n) ;
  67. exit $ERRORS{"CRITICAL"} ;
  68. }
  69. # the results of looking up the DCs (by their name) in the WINS
  70. # 10.0.100.16 ipa01<20>
  71. # 10.0.100.1 ipa02<20>
  72. # 10.0.100.104 ipa03<20>
  73. @dc_addresses = () ;
  74. foreach (@dc_query) {
  75. next unless /^(\S+)\s+(\S+?)<\S+?>$/ ;
  76. $address2dc{$1} = $2 ;
  77. push @dc_addresses, $1 ;
  78. }
  79. $netbios_name = "$domain#1C" ;
  80. eval {
  81. alarm($TIMEOUT) ;
  82. @dcs_of_domain = $NMBLOOKUP->($wins, $netbios_name, defined $debug ? '-d ' . SAMBA_DEBUG_LVL : '') ;
  83. alarm(0) ;
  84. } ;
  85. if ($@ and $@ =~ /Alarm clock restart/) {
  86. print qq(Failed. Timeout while waiting for response from "$NMBLOOKUP_CMD $wins $netbios_name"\n) ;
  87. exit $ERRORS{"CRITICAL"} ;
  88. }
  89. if ($@ and $@ !~ /Alarm clock restart/) {
  90. print qq(Failed. "$@" in response to "$NMBLOOKUP_CMD $wins $netbios_name"\n) ;
  91. exit $ERRORS{"UNKNOWN"} ;
  92. }
  93. shift @dcs_of_domain ;
  94. chomp @dcs_of_domain ;
  95. @addr_dcs_of_domain = map /^(\S+)/, @dcs_of_domain ;
  96. # tsitc> /usr/local/samba/bin/nmblookup -R -U wins ipaustralia#1c
  97. # Sending queries to 10.0.100.29
  98. # 10.0.100.114 ipaustralia<1c>
  99. # 168.168.102.129 ipaustralia<1c>
  100. # 192.168.101.221 ipaustralia<1c>
  101. # 10.0.100.61 ipaustralia<1c>
  102. # 192.168.108.129 ipaustralia<1c>
  103. # 192.168.102.128 ipaustralia<1c>
  104. # 10.0.4.126 ipaustralia<1c>
  105. # 192.168.106.214 ipaustralia<1c>
  106. # 10.0.3.165 ipaustralia<1c>
  107. # 192.168.105.214 ipaustralia<1c>
  108. # 10.0.6.145 ipaustralia<1c>
  109. # 192.168.104.128 ipaustralia<1c>
  110. # 10.0.4.59 ipaustralia<1c>
  111. # 10.9.99.99 ipaustralia<1c>
  112. # 10.99.99.99 ipaustralia<1c>
  113. # 10.9.99.254 ipaustralia<1c>
  114. # 10.0.3.15 ipaustralia<1c>
  115. # 192.168.102.129 ipaustralia<1c>
  116. # 192.168.103.129 ipaustralia<1c>
  117. # 192.168.105.129 ipaustralia<1c>
  118. # 192.168.106.129 ipaustralia<1c>
  119. # 192.168.101.129 ipaustralia<1c>
  120. # 192.168.104.129 ipaustralia<1c>
  121. # 10.0.3.123 ipaustralia<1c>
  122. # 10.0.100.67 ipaustralia<1c>
  123. # tsitc>
  124. my %x ;
  125. @found_dcs = grep { ! $x{$_}++ } @address2dc{ grep exists $address2dc{$_}, @addr_dcs_of_domain} ;
  126. # @found_dcs = grep { defined $_} @address2dc{ grep exists $address2dc{$_}, @addr_dcs_of_domain} ;
  127. # Gotcha.
  128. # 'exists' is necessary to prevent autovivificatiton
  129. # of keys in %address2dc
  130. if ( &set_eq( \@found_dcs, [ values %address2dc ] ) ) {
  131. print $debug ? qq(Ok. WINS named "$wins" resolved addresses of "@my_dcs" as "@dc_query" and controllers of domain "$domain" as "@dcs_of_domain"\n) :
  132. qq(Ok. Found controllers named "@my_dcs" in response to "$domain#1C" name query from WINS named "$wins".\n) ;
  133. exit $ERRORS{"OK"} ;
  134. } elsif ( scalar @found_dcs == 0 ) {
  135. print qq(Failed. Found __no__ controllers named "@my_dcs" in response to "$domain#1C" query from WINS named "$wins". Got "@dcs_of_domain"\n) ;
  136. exit $ERRORS{"CRITICAL"} ;
  137. } elsif ( scalar @found_dcs < scalar keys %address2dc ) {
  138. print qq(Warning. Not all domain controllers found in response to "$domain#1C" query from WINS named "$wins". Expected "@my_dcs", got "@found_dcs"\n) ;
  139. exit $ERRORS{"WARNING"} ;
  140. }
  141. sub set_eq {
  142. return 0 unless scalar @{$_[0]} == scalar @{$_[1]} ;
  143. foreach my $a ( @{$_[0]} ) {
  144. return 0 unless scalar grep { $a eq $_ } @{$_[1]} ;
  145. }
  146. return 1 ;
  147. }
  148. sub print_usage () {
  149. print "Usage: $PROGNAME -W <wins> -D <domain>\n";
  150. }
  151. sub print_help () {
  152. print_revision($PROGNAME,'$Revision$ ');
  153. print "Copyright (c) 2001 Karl DeBisschop/S Hopcroft
  154. Perl Check WINS plugin for NetSaint.
  155. Returns OK if the addresses of domain controllers are found in the list of domain controllers returned in the WINS response to a 'domain controllers query'
  156. Why would you want to do this ?
  157. MS File server clients insist on connecting to file servers using NetBIOS names.
  158. If they __don't__ resolve NetBIOS names with a WINS (NBNS) then they'll either fail to logon and connect to shares or they will
  159. broadcast requsts for names.
  160. Both problems are eliminated by a healthy WINS.
  161. Also, you may have a MS domain spanning a number of WAN connected sites, with domain controllers far away from powerful local
  162. domain controllers.
  163. In this case, you want your local clients to have their logon requests validated by the local controllers.
  164. The plugin works by
  165. asking the WINS to resolve the addresses of the domain controllers (supplied by -C or from the constant MY_DCS)
  166. asking the WINS to return the list of addresses of the domain controllers able to validate requests for the domain
  167. whose name is given by -D
  168. returning Ok if all controller addresses are in that list (of addresses of domain controllers) or
  169. returning WARNING if not all the controller addresses are in the list or
  170. returning CRITICAL if there is no reply from the WINS or the list contains none of the contoller addresses
  171. ";
  172. print_usage();
  173. print '
  174. -W, --wins=STRING
  175. Hostname or address of the WINS (Either Samba/nmbd or MS product)
  176. -D, --domain=STRING
  177. MS Domain name to find the Domain controllers of.
  178. -C, --controllers:STRING
  179. Optional __name(s)__ of domain controller that __must__ be found in the response to a domain controller name query.
  180. If not defined, then use the constant value MY_DCS. You must use either -C or make sure that MY_DCS contains the names
  181. of __your__ domain controllers.
  182. -T, --timeout:INTEGER
  183. -d, --debug
  184. Debugging output.
  185. -h, --help
  186. This stuff.
  187. ';
  188. support();
  189. }
  190. sub version () {
  191. print_revision($PROGNAME,'$Revision$ ');
  192. exit $ERRORS{'OK'};
  193. }
  194. sub help () {
  195. print_help();
  196. exit $ERRORS{'OK'};
  197. }