File: zonecheck.rb

package info (click to toggle)
zonecheck 3.0.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,312 kB
  • ctags: 836
  • sloc: ruby: 6,664; xml: 693; sh: 518; python: 301; makefile: 75
file content (348 lines) | stat: -rw-r--r-- 10,166 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# $Id: zonecheck.rb,v 1.25 2010/06/25 08:37:43 chabannf Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/07/18 10:29:53
# REVISION    : $Revision: 1.25 $ 
# DATE        : $Date: 2010/06/25 08:37:43 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v3
# 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 3 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
#

module ZoneCheck

  require 'config'
  require 'param'
  require 'cachemanager'
  require 'testmanager'
  
  
  
  ##
  ##
  ##
  class ZoneCheck
    
    #
    # Input method
    #   (pseudo parameter: --INPUT=xxx)
    #
    def self.input_method
      im = nil	# Input Method
      
      # Check meta argument
      ARGV.delete_if { |a|
        im = $1 if remove = a =~ /^--INPUT=(.*)/
        remove
      }
      
      # Check environment variable ZC_INPUT
      im ||= ENV['ZC_INPUT']
      
      # Try autoconfiguration
      is_cgi = false
      ZC_CGI_ENV_KEYS.each {|k| is_cgi = true unless ENV[k].nil?}
      im ||= if is_cgi || (PROGNAME =~ /\.#{ZC_CGI_EXT}$/)
             then "cgi"
             else ZC_DEFAULT_INPUT
             end
      
      # Sanity check on Input Method
      if ! (im =~ /^\w+$/)
        l10n_error = $mc.get('word:error').upcase
        l10n_input = $mc.get('input:suspicious_method') % [ im ]
        $console.stderr.puts "#{l10n_error}: #{l10n_input}"
        exit EXIT_ERROR
      end
      im = im.dup.untaint	# object can be frozen, so we need to dup it
      
      # Instanciate input method
      begin
        require "input/#{im}"
      rescue LoadError => e
        $dbg.msg(DBG::INIT, "Unable to require \"input/#{im}\": #{e}")
        l10n_error = $mc.get('word:error').upcase
        l10n_input = $mc.get('input:unsupported_method') % [ im ]
        $console.stderr.puts "#{l10n_error}: #{l10n_input}"
        exit EXIT_ERROR
      end
      input = eval "Input::#{im.upcase}::new"
      $dbg.msg(DBG::INIT, "Input method: '#{im}', new input object created: <#{input.class}>")
      return input
    end
    
    
    def initialize
      @input		= nil
      @param		= nil
      @test_manager	= nil
      @testlist	= nil
    end
    
    
    def start 
      begin
        # Input method selection
        @input = ZoneCheck.input_method
        
        # Initialize parameters (from command line parsing)
        argv_backup = ARGV.clone
        @param = Param::new
        @input.usage(EXIT_USAGE) unless @input.parse(@param)
        @param.preconf.autoconf
        
        # Load the test implementation
        TestManager.load(@param.preconf.testdir)
        
        # Create test manager
        @test_manager = TestManager::new
        @test_manager.add_allclasses
        
        # Load configuration
        @config =  Config::new(@test_manager)
        @config.load(@param.preconf.cfgfile)
        @config.validate(@test_manager)
        @config.profilename = @param.preconf.profile
        
        # Preset
        if @input.allow_preset
          presetname = @param.preconf.preset
          if !presetname.nil? && @config.presets[presetname].nil?
            raise  Config::ConfigError,
            $mc.get('config:unknown_preset') % presetname
          end
          presetname ||=  Config::Preset_Default
          
          if preset = @config.presets[presetname]
            $dbg.msg(DBG::INIT) { 
            "Using preset '#{preset.name}' (reloading parameters)"}
            
            # Create new argument
            @param = Param::new
            
            # Load preset
            begin
              # Can be reverted
              [ 'verbose', 'transp',
                'output', 'error' ].each { |opt|
                  @param.send("#{opt}=",preset[opt]) if preset[opt]
              }
              
              # Cannot be reverted
              @param.rflag.quiet = true if preset['quiet']
              @param.rflag.one   = true if preset['one'  ]
            rescue Param::ParamError => e
              raise  Config::ConfigError,
              ($mc.get('config:error_in_preset') % presetname) +
              " (#{e.message})"
            end
            
            # Restart argument parsing
            ARGV.replace(argv_backup)
            @input.restart
            @input.usage(EXIT_USAGE) unless @input.parse(@param)
          end
        end
        
        # Interaction
        unless @input.interact(@param, @config, @test_manager)
          exit EXIT_ABORTED
        end
        
        # Test selection
        @config.overrideconf(@param.test.tests) if @param.test.tests
        
        # Do the job
        success = if    @param.test.list		  then print_testlist
                  elsif @param.test.desctype	then print_testdesc
                  else				     do_check
                  end
        
        # Everything fine?
        return success
      rescue Param::ParamError   => e
        @input.error(e.to_s, EXIT_ERROR)
      rescue  Config::SyntaxError => e
        @input.error("%s %d: %s\n\t(%s)" % [ 
            $mc.get('word:line').capitalize, e.line, e.to_s,
            e.path ], EXIT_ERROR)
      rescue  Config::ConfigError => e
        @input.error(e.to_s, EXIT_ERROR)
      rescue => e
        raise if $dbg.enabled?(DBG::DONT_RESCUE)
        @input.error(e.to_s, EXIT_ERROR)
      ensure
        # exit() raise an exception ensuring that the following code
        # is executed
        destroy
      end
      # NOT REACHED
    end
    
    def destroy
    end
    
    
    #-- zonecheck ---------------------------------------------------------
    def do_check
      param_config_preamble
      
      # Begin formatter
      @param.publisher.engine.begin
      
      # 
      success = true
      begin
        cm = CacheManager::create(@param.resolver.local)
        cm.domain_name = @param.domain.name.to_s 
        retry_times = @config.constants.fetch('dnsruby:retrytimes').to_i
        retry_delay = @config.constants.fetch('dnsruby:retrydelay').to_i
        query_timeout = @config.constants.fetch('dnsruby:querytimeout').to_i
        cm.init(@param.edns,
                @param.network.query_mode,
                retry_times,
                retry_delay,
                query_timeout)
        
        
        
        param_config_data
        @param.domain.ns.each { |ns_name, ns_addr_list|
                  @param.network.address_wanted?(ns_addr_list).each { |addr|
                      cm[addr] }
                }
        success = zc(cm)
      rescue Param::ParamError => e
        @param.publisher.engine.error(e.message)
        success = false
      end
      
      # End formatter
      @param.publisher.engine.end
      
      #
      return success
    end
    
    def param_config_preamble
      @param.rflag.autoconf
      @param.option.autoconf
      @param.publisher.autoconf(@param.rflag, @param.option)
      @param.network.autoconf
      @param.resolver.autoconf
      @param.test.autoconf
    end
    
    def param_config_data
      @param.info.clear
      @param.info.autoconf
      @param.publisher.engine.info   = @param.info
      @param.domain.autoconf(@param.resolver.local)
      @param.report.autoconf(@param.domain, 
      @param.rflag, @param.publisher.engine)
    end
    
    def zc(cm)
      starttime = Time::now
      
      # Setup publisher (for the domain)
      @param.publisher.engine.setup(@param.domain.name)
      
      # Retrieve specific configuration
      if (cfg = @config.profile(@param.domain.name)).nil?
        l10n_error = $mc.get('input:unsupported_domain')
        @param.publisher.engine.error(l10n_error % @param.domain.name)
        return false
      end
      
      @param.info.profile = [ cfg.name, cfg.longdesc ]
      @param.publisher.engine.constants = cfg.constants
      
      # Display intro (ie: domain and nameserver summary)
      @param.publisher.engine.intro(@param.domain)
      
      # Initialise and check
      @test_manager.init(cfg, cm, @param)
      status = @test_manager.check
      
      # Finish diagnostic (in case of pending output)
      @param.report.finish
      
      # Lastaction hook
      lastaction(status)
      
      # Return status
      return status
    end
    
    
    def lastaction(status)
    end
    
    
    #-- testlist ----------------------------------------------------------
    
    #
    # Print the list of available tests
    # XXX: should use publisher
    #
    def print_testlist
      @param.test.autoconf
      @test_manager.list.sort.each { |testname|
        $console.stdout.puts testname }
      true
    end
    
    
      #-- testdesc ----------------------------------------------------------
    
    #
    # Print the description of the tests
    #  If no selection is done (option -T), the description is
    #  printed for all the available tests
    # XXX: should use other publisher than text
    #
    def print_testdesc
      require 'publisher/text'
      @param.publisher.engine = ::Publisher::Text
      
      param_config_preamble
      
      list		= @param.test.tests || @test_manager.list.sort
      publisher	= @param.publisher.engine
      profilename	= @config.profilename
      
      publisher.constants = (@config.profiles[profilename] || 
                             @config).constants
      
      publisher.begin
      list.each { |test| publisher.testdesc(test, @param.test.desctype) }
      publisher.end
      true
    end
    
  end

end