2
# $Id: zc.rb,v 1.125 2010/06/07 08:51:25 chabannf Exp $
5
# CONTACT : zonecheck@nic.fr
6
# AUTHOR : Stephane D'Alu <sdalu@nic.fr>
8
# CREATED : 2002/07/18 10:29:53
9
# REVISION : $Revision: 1.125 $
10
# DATE : $Date: 2010/06/07 08:51:25 $
12
# CONTRIBUTORS: (see also CREDITS file)
16
# COPYRIGHT : AFNIC (c) 2003
18
# This file is part of ZoneCheck.
20
# ZoneCheck is free software; you can redistribute it and/or modify it
21
# under the terms of the GNU General Public License as published by
22
# the Free Software Foundation; either version 3 of the License, or
23
# (at your option) any later version.
25
# ZoneCheck is distributed in the hope that it will be useful, but
26
# WITHOUT ANY WARRANTY; without even the implied warranty of
27
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28
# General Public License for more details.
30
# You should have received a copy of the GNU General Public License
31
# along with ZoneCheck; if not, write to the Free Software Foundation,
32
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36
## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38
## WARN: when editing this file on installed ZoneCheck, you should
39
## keep in mind that some ZoneCheck variant (cgi, ...) are
40
## more or less strongly connected with this file by:
41
## - a copy : only THIS file will be modified
42
## - a hardlink : depending of your editor behaviour when
43
## saving the file, all the files will hold
44
## the modification OR only this file will.
45
## - a symlink : no problem should occured (except if on Windows)
47
## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50
## --> CUSTOMIZATION <-- #############################################
52
# You shouldn't really need to change these values:
53
# - This is normally automatically done when performing a: make install
54
# - Setting the environment variable ZC_INSTALL_PATH should be enough
55
# for testing the code
59
ZC_INSTALL_PATH = (ENV['ZC_INSTALL_PATH'] || (ENV['HOME'] || '/homes/sdalu') + '/Repository/zonecheck').dup.untaint
62
ZC_DIR = "#{ZC_INSTALL_PATH}/zc"
63
ZC_LIB = "#{ZC_INSTALL_PATH}/lib"
65
ZC_CONFIG_DIR = ENV['ZC_CONFIG_DIR'] || "#{ZC_INSTALL_PATH}/etc/zonecheck"
66
ZC_LOCALIZATION_DIR = ENV['ZC_LOCALIZATION_DIR'] || "#{ZC_INSTALL_PATH}/locale"
67
ZC_TEST_DIR = ENV['ZC_TEST_DIR'] || "#{ZC_INSTALL_PATH}/test"
70
ZC_CONFIG_FILE = ENV['ZC_CONFIG_FILE'] || 'zc.conf'
73
ZC_LANG_FILE = 'zc.%s'
74
ZC_LANG_DEFAULT = 'en' # can have an enconding: en.ascii
76
## Message catalog fallback
77
ZC_MSGCAT_FALLBACK = 'en' # don't specifie an encoding here
80
ZC_DEFAULT_INPUT = 'cli'
82
ZC_CGI_ENV_KEYS = [ 'GATEWAY_INTERFACE', 'SERVER_ADDR' ]
86
ZC_HTML_PATH = ENV['ZC_HTML_PATH'] || "#{ZC_INSTALL_PATH}/www"
90
ZC_COPYRIGHT = 'AFNIC (c) 2003'
91
ZC_CONTACT = 'ZoneCheck <zonecheck@nic.fr>'
92
ZC_MAINTAINER = 'Stephane D\'Alu <sdalu@nic.fr>'
95
ZC_XML_PARSER = ENV['ZC_XML_PARSER']
96
ZC_IP_STACK = ENV['ZC_IP_STACK'] || '46'
98
## --> END OF CUSTOMIZATION <-- ######################################
104
ZC_CVS_NAME = %q$Name: ZC-3_0_0 $
105
ZC_NAME = 'ZoneCheck'
106
ZC_VERSION = (Proc::new {
107
n = ZC_CVS_NAME.split[1]
108
n = /^ZC-(.*)/.match(n) unless n.nil?
109
n = n[1] unless n.nil?
110
n = n.gsub(/_/, '.') unless n.nil?
114
PROGNAME = File.basename($0)
120
EXIT_OK = 0 # Everything went fine, no fatal error, domain ok
121
EXIT_FAILED = 1 # The program completed but the result is negative
122
EXIT_TIMEOUT = 2 # The program completed but the result is negative
124
EXIT_ABORTED = 3 # The user aborted the program before completion
125
EXIT_ERROR = 4 # An error unrelated to the result occured
126
EXIT_USAGE = 9 # The user didn't bother reading the man page
132
m = /^(\d+)\.(\d+)\./.match(RUBY_VERSION)
133
if (m[1].to_i <= 1) && (m[2].to_i < 8)
134
$stderr.puts "#{PROGNAME}: ruby version 1.8.0 at least is required."
140
# Run at safe level 1
141
# A greater safe level is unfortunately not possible due to some
142
# low level operations in the Dnsruby library
144
$SAFE = 0 # REXML BUG
148
# Ensure '.' is not one of the possible path (too much trouble)
149
# Add zonecheck directories to ruby path
151
$LOAD_PATH.delete_if { |path| path == '.' }
152
$LOAD_PATH << ZC_DIR << ZC_LIB
156
# Version / Name / Contact
158
$zc_version ||= ZC_VERSION
160
$zc_contact ||= ZC_CONTACT
165
$zc_config_dir ||= ZC_CONFIG_DIR
166
$zc_config_file ||= ZC_CONFIG_FILE
171
$zc_custom ||= 'zc-custom'
176
$zc_xml_parser ||= ZC_XML_PARSER
178
# Resolver configuration
179
$rootserver_hintfile = "#{$zc_config_dir}/rootservers"
185
# Standard Ruby libraries
188
# Modification to standard/core ruby classes
194
# ZoneCheck component
204
# (earlier initialization, can also be set via input interface)
206
$dbg = ZoneCheck::DBG::new
207
$dbg.level = ENV['ZC_DEBUG'] if ENV['ZC_DEBUG']
212
rescue LoadError => e
213
$dbg.msg(ZoneCheck::DBG::INIT, "Unable to require rubygems (#{e})")
214
$stderr.puts "ERROR: You have to install rubygems and Dnsruby in order to execute ZoneCheck"
221
rescue LoadError => e
222
$dbg.msg(ZoneCheck::DBG::INIT, "Unable to require dnsruby (#{e})")
223
$stderr.puts "ERROR: You have to install Dnsruby in order to execute ZoneCheck"
227
if Dnsruby.version < 1.47
228
$dbg.msg(ZoneCheck::DBG::INIT, "Unable to require dnsruby (#{e})")
229
$stderr.puts "ERROR: You have to install at least Dnsruby 1.47 in order to execute ZoneCheck"
238
ENV['XML_CATALOG_FILES'] = "#{ZC_DIR}/data/catalog.xml"
240
$dbg.msg(DBG::INIT) {
241
shrinked_path = File.shrink_path(ENV['XML_CATALOG_FILES'], ':')
242
"Using XML_CATALOG_FILES=#{shrinked_path}"
244
$dbg.msg(DBG::INIT) { "Using XML parser: #{MyXML::Implementation}" }
249
# IPv4/IPv6 stack detection
250
# WARN: doesn't implies that we have also the connectivity
252
$ipv4_stack = !(ZC_IP_STACK =~ /4/).nil?
253
$ipv6_stack = !(ZC_IP_STACK =~ /6/).nil?
254
$ipv4_stack = $ipv6_stack = true if !$ipv4_stack && !$ipv6_stack
256
$ipv4_stack &&= begin
257
UDPSocket::new(Socket::AF_INET).close
259
rescue NameError, # if Socket::AF_INET doesn't exist
260
SystemCallError # for the Errno::EAFNOSUPPORT error
263
$ipv6_stack &&= begin
264
UDPSocket::new(Socket::AF_INET6).close
266
rescue NameError, # if Socket::AF_INET6 doesn't exist
267
SystemCallError # for the Errno::EAFNOSUPPORT error
273
# Internationalisation
274
# WARN: default locale is mandatory as no human messages are
275
# present in the code (except for debugging purpose)
278
$supported_languages = ["en","fr"]
281
$locale = Locale::new
282
$locale.lang = ZC_LANG_DEFAULT if $locale.lang.nil?
284
# Initialize the console
285
$console = Console::new
287
# Initialize the message catalog
288
$mc = MsgCat::new(ZC_LOCALIZATION_DIR, ZC_MSGCAT_FALLBACK)
290
# Add watcher for notification of locale changes
291
# ... and force update
292
$locale.watch('lang', proc {
293
$mc.language = $locale.language
294
$mc.country = $locale.country
296
$locale.watch('encoding', proc {
297
$console.encoding = $locale.encoding } )
298
$locale.notify('lang', 'encoding')
301
$mc.read(ZC_LANG_FILE)
304
raise if $dbg.enabled?(DBG::DONT_RESCUE)
305
$stderr.puts "ERROR: #{e.to_s}"
311
# Load eventual custom version
315
rescue LoadError => e
316
$dbg.msg(DBG::INIT, "Unable to require '#{$zc_custom}' (#{e})")
321
## Adjustement due to zc-custom
324
# hintfile = "#{$zc_config_dir}/rootservers"
325
# if $rootserver_hintfile != hintfile
326
# NResolv::DNS::RootServer.current = NResolv::DNS::RootServer.from_hintfile(hintfile)
328
#rescue YAML::ParseError, SystemCallError => e
329
# $dbg.msg(DBG::INIT,
330
# "Unable to read/parse rootserver hint file (#{e})")
337
exit ZoneCheck::ZoneCheck::new::start ? EXIT_OK : EXIT_FAILED