File: zc.rb

package info (click to toggle)
zonecheck 2.0.4-13lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,424 kB
  • ctags: 1,177
  • sloc: ruby: 9,582; xml: 731; sh: 574; makefile: 66
file content (315 lines) | stat: -rwxr-xr-x 8,102 bytes parent folder | download | duplicates (3)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/local/bin/ruby
# $Id: zc.rb,v 1.123 2004/01/26 13:25:24 sdalu Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/07/18 10:29:53
# REVISION    : $Revision: 1.123 $ 
# DATE        : $Date: 2004/01/26 13:25:24 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#


## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
##
## WARN: when editing this file on installed ZoneCheck, you should
##       keep in mind that some ZoneCheck variant (cgi, ...) are 
##       more or less strongly connected with this file by:
##       - a copy     : only THIS file will be modified
##       - a hardlink : depending of your editor behaviour when
##                      saving the file, all the files will hold
##                      the modification OR only this file will.
##       - a symlink  : no problem should occured (except if on Windows)
##
## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


## --> CUSTOMIZATION <-- #############################################
# 
# You shouldn't really need to change these values:
#  - This is normally automatically done when performing a: make install
#  - Setting the environment variable ZC_INSTALL_PATH should be enough
#     for testing the code
#

## Installation path
ZC_INSTALL_PATH		= (ENV['ZC_INSTALL_PATH'] || (ENV['HOME'] || '/homes/sdalu') + '/Repository/zonecheck').dup.untaint

## Directories
ZC_DIR			= "#{ZC_INSTALL_PATH}/zc"
ZC_LIB			= "#{ZC_INSTALL_PATH}/lib"

ZC_CONFIG_DIR		= ENV['ZC_CONFIG_DIR']       || "#{ZC_INSTALL_PATH}/etc/zonecheck"
ZC_LOCALIZATION_DIR	= ENV['ZC_LOCALIZATION_DIR'] || "#{ZC_INSTALL_PATH}/locale"
ZC_TEST_DIR		= ENV['ZC_TEST_DIR']         || "#{ZC_INSTALL_PATH}/test"

## Configuration
ZC_CONFIG_FILE		= ENV['ZC_CONFIG_FILE'] || 'zc.conf'

## Lang
ZC_LANG_FILE		= 'zc.%s'
ZC_LANG_DEFAULT		= 'en'		# can have an enconding: en.ascii

## Message catalog fallback
ZC_MSGCAT_FALLBACK	= 'en'		# don't specifie an encoding here

## Input methods
ZC_DEFAULT_INPUT	= 'cli'

ZC_CGI_ENV_KEYS		= [ 'GATEWAY_INTERFACE', 'SERVER_ADDR' ]
ZC_CGI_EXT		= 'cgi'

ZC_GTK_ENV_KEYS		= [] #[ 'DISPLAY' ]

## Publisher
ZC_HTML_PATH		= ENV['ZC_HTML_PATH'] || "#{ZC_INSTALL_PATH}/www"


## Contact / Details
ZC_COPYRIGHT		= 'AFNIC (c) 2003'
ZC_CONTACT		= 'ZoneCheck <zonecheck@nic.fr>'
ZC_MAINTAINER		= 'Stephane D\'Alu <sdalu@nic.fr>'

## Internal
ZC_XML_PARSER		= ENV['ZC_XML_PARSER'] 
ZC_IP_STACK		= ENV['ZC_IP_STACK'] || '46'

## --> END OF CUSTOMIZATION <-- ######################################


#
# Identification
#
ZC_CVS_NAME	= %q$Name: ZC-2_0_4 $
ZC_NAME		= 'ZoneCheck'
ZC_VERSION	= (Proc::new { 
		       n = ZC_CVS_NAME.split[1]
		       n = /^ZC-(.*)/.match(n) unless n.nil?
		       n = n[1]                unless n.nil?
		       n = n.gsub(/_/, '.')    unless n.nil?
		       
		       n || '<unreleased>'
		   }).call
PROGNAME	= File.basename($0)


#
# Constants
#
EXIT_OK		=  0	# Everything went fine, no fatal error, domain ok
EXIT_FAILED	=  1	# The program completed but the result is negative
EXIT_TIMEOUT	=  2	# The program completed but the result is negative
			#  due to timeout.
EXIT_ABORTED	=  3	# The user aborted the program before completion
EXIT_ERROR      =  4	# An error unrelated to the result occured
EXIT_USAGE	=  9	# The user didn't bother reading the man page


#
# Sanity check
#
m = /^(\d+)\.(\d+)\./.match(RUBY_VERSION)
if (m[1].to_i <= 1) && (m[2].to_i < 8)
    $stderr.puts "#{PROGNAME}: ruby version 1.8.0 at least is required."
    exit EXIT_ERROR
end


#
# Run at safe level 1
#  A greater safe level is unfortunately not possible due to some 
#  low level operations in the NResolv library
#
$SAFE = 0	# REXML BUG


#
# Ensure '.' is not one of the possible path (too much trouble)
# Add zonecheck directories to ruby path
#
$LOAD_PATH.delete_if { |path| path == '.' }
$LOAD_PATH << ZC_DIR << ZC_LIB


#
# Version / Name / Contact
#
$zc_version	||= ZC_VERSION
$zc_name	||= ZC_NAME
$zc_contact	||= ZC_CONTACT

#
# Config directory
# 
$zc_config_dir	||= ZC_CONFIG_DIR
$zc_config_file	||= ZC_CONFIG_FILE

#
# Custom
#
$zc_custom	||= 'zc-custom'

#
# Internal
#
$zc_xml_parser	||= ZC_XML_PARSER

# Resolver configuration
$nresolv_rootserver_hintfile	= "#{$zc_config_dir}/rootservers"
$nresolv_dbg			= 0x0000


#
# Requierement
#
# Standard Ruby libraries
require 'socket'

# External libraries
require 'nresolv'

# Modification to standard/core ruby classes
require 'ext/array'
require 'ext/file'
require 'ext/myxml'

# ZoneCheck component
require 'dbg'
require 'locale'
require 'msgcat'
require 'console'
require 'zonecheck'


#
# Debugger object
#  (earlier initialization, can also be set via input interface)
#
$dbg       = DBG::new
$dbg.level = ENV['ZC_DEBUG'] if ENV['ZC_DEBUG']


#
# XML parser
#
ENV['XML_CATALOG_FILES'] = "#{ZC_DIR}/data/catalog.xml"

$dbg.msg(DBG::INIT) {
    shrinked_path = File.shrink_path(ENV['XML_CATALOG_FILES'], ':')
    "Using XML_CATALOG_FILES=#{shrinked_path}"
}
$dbg.msg(DBG::INIT) { "Using XML parser: #{MyXML::Implementation}" }



#
# IPv4/IPv6 stack detection
#  WARN: doesn't implies that we have also the connectivity
#
$ipv4_stack = !(ZC_IP_STACK =~ /4/).nil?
$ipv6_stack = !(ZC_IP_STACK =~ /6/).nil?
$ipv4_stack = $ipv6_stack = true if !$ipv4_stack && !$ipv6_stack

$ipv4_stack &&= begin
		    UDPSocket::new(Socket::AF_INET).close
		    true
		rescue NameError,      # if Socket::AF_INET doesn't exist
			SystemCallError # for the Errno::EAFNOSUPPORT error
		    false
		end
$ipv6_stack &&= begin
		    UDPSocket::new(Socket::AF_INET6).close
		    true
		rescue NameError,      # if Socket::AF_INET6 doesn't exist
			SystemCallError # for the Errno::EAFNOSUPPORT error
		    false
		end


#
# Internationalisation
#  WARN: default locale is mandatory as no human messages are
#        present in the code (except for debugging purpose)
#
begin
    # Initialize locale
    $locale = Locale::new
    $locale.lang = ZC_LANG_DEFAULT if $locale.lang.nil?
    
    # Initialize the console
    $console = Console::new
    
    # Initialize the message catalog
    $mc = MsgCat::new(ZC_LOCALIZATION_DIR, ZC_MSGCAT_FALLBACK)

    # Add watcher for notification of locale changes
    #  ... and force update
    $locale.watch('lang', proc { 
		      $mc.language = $locale.language
		      $mc.country  = $locale.country
		      $mc.reload } )
    $locale.watch('encoding', proc { 
		      $console.encoding = $locale.encoding } )
    $locale.notify('lang', 'encoding')

    # Read msgcat
    $mc.read(ZC_LANG_FILE)

rescue => e
    raise if $dbg.enabled?(DBG::DONT_RESCUE)
    $stderr.puts "ERROR: #{e.to_s}"
    exit EXIT_ERROR
end


#
# Load eventual custom version
#
begin 
    require $zc_custom
rescue LoadError => e
    $dbg.msg(DBG::INIT, "Unable to require '#{$zc_custom}' (#{e})")
end


#
# Adjustement due to zc-custom
#
begin
    hintfile = "#{$zc_config_dir}/rootservers"
    if $nresol_rootserver_hintfile != hintfile
	NResolv::DNS::RootServer.current = NResolv::DNS::RootServer.from_hintfile(hintfile)
    end
rescue YAML::ParseError, SystemCallError => e
    $dbg.msg(DBG::INIT, 
	    "Unable to read/parse rootserver hint file (#{e})")
end


#
# Check it now!
#
exit ZoneCheck::new::start ? EXIT_OK : EXIT_FAILED