<commit_before>#
# $Id$
#
# Copyright (c) 2009 Nominet UK. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

require 'rubygems'
require 'syslog'
include Syslog::Constants
require 'dnsruby'
include Dnsruby
require 'kasp_auditor/config.rb'
require 'kasp_auditor/key_tracker.rb'
require 'kasp_auditor/auditor.rb'
require 'kasp_auditor/parse.rb'
require 'kasp_auditor/preparser.rb'
require 'kasp_auditor/auditor_daemon.rb'

# This module provides auditing capabilities to OpenDNSSEC.
# Once an unsigned zone has been signed, this module is used to check that
# the signing process has run successfully. It checks that no data has been
# lost (or non-DNSSEC data added), and that all the DNSSEC records are correct.
# It used the OpenDNSSEC standard logging (defined in /etc/opendnssec/conf.xml)
# Several transient files are created during this process - they are removed
# when the process is complete.
module KASPAuditor
  ENV['LC_ALL']= "C"

  def KASPAuditor.exit(msg, err)
    # @TODO@ Log exit msg
    print msg + "\n"
    Kernel.exit(err)
  end
  $SAFE = 1
  # The KASPAuditor takes the signed and unsigned zones and compares them.
  # It first parses both files, and creates transient files which are then
  # sorted into canonical order. These files are then processed by the
  # Auditor. If processing an NSEC3-signed file, the Auditor will create
  # additional temporary files, which are processed after the main auditing
  # run.
  class Runner

    attr_accessor :kasp_file, :zone_name, :signed_temp, :conf_file
    attr_accessor :enable_timeshift

    # Run the auditor.
    def run
      conf_file = @conf_file
      if (!conf_file)
        KASPAuditor.exit("No configuration file specified", 1)
      end
      syslog_facility, working, zonelist, kasp_file, enforcer_interval =
        load_config_xml(conf_file)
      if (@kasp_file)
        kasp_file = @kasp_file
      end

      Syslog.open("ods-auditor", Syslog::LOG_PID |
        Syslog::LOG_CONS, syslog_facility) { |syslog|
        run_with_syslog(zonelist, kasp_file, syslog, working, enforcer_interval)
      }
    end

    def run_as_daemon
      daemon = AuditorDaemon.new
      daemon.conf_file = @conf_file
      if (@kasp_file)
        daemon.kasp_file = @kasp_file
      end
      if (@zone_name)
        daemon.zone_name = @zone_name
        if (@signed_temp)
          daemon.signed_temp = @signed_temp
        end
      end
      daemon.run
    end

    # This method is provided so that the test code can use its own syslog
    def run_with_syslog(zonelist_file, kasp_file, syslog, working, enforcer_interval) # :nodoc: all
      if (@enable_timeshift)
        configure_timeshift(syslog)
      end
      zones = Parse.parse(File.dirname(kasp_file)  + File::SEPARATOR,
        zonelist_file, kasp_file, syslog)
      check_zones_to_audit(zones)
      # Now check the input and output zones using the config
      if (zones.length == 0)
        syslog.log(LOG_ERR, "Couldn't find any zones to load")
        KASPAuditor.exit("Couldn't find any zones to load", -LOG_ERR)
      end
      pid = Process.pid
      ret = 999 # Return value to controlling process
      zones.each {|config, input_file, output_file|
        do_audit = true
        [{input_file => "Unsigned"}, {output_file => "Signed"}].each {|hash|<commit_msg>Should get nicer error messages now<commit_after>#
# $Id$
#
# Copyright (c) 2009 Nominet UK. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

require 'rubygems'
require 'syslog'
include Syslog::Constants
require 'dnsruby'
include Dnsruby
require 'kasp_auditor/config.rb'
require 'kasp_auditor/key_tracker.rb'
require 'kasp_auditor/auditor.rb'
require 'kasp_auditor/parse.rb'
require 'kasp_auditor/preparser.rb'
require 'kasp_auditor/auditor_daemon.rb'

# This module provides auditing capabilities to OpenDNSSEC.
# Once an unsigned zone has been signed, this module is used to check that
# the signing process has run successfully. It checks that no data has been
# lost (or non-DNSSEC data added), and that all the DNSSEC records are correct.
# It used the OpenDNSSEC standard logging (defined in /etc/opendnssec/conf.xml)
# Several transient files are created during this process - they are removed
# when the process is complete.
module KASPAuditor
  ENV['LC_ALL']= "C"

  def KASPAuditor.exit(msg, err)
    # @TODO@ Log exit msg
    print msg + "\n"
    Kernel.exit(err)
  end
  $SAFE = 1
  # The KASPAuditor takes the signed and unsigned zones and compares them.
  # It first parses both files, and creates transient files which are then
  # sorted into canonical order. These files are then processed by the
  # Auditor. If processing an NSEC3-signed file, the Auditor will create
  # additional temporary files, which are processed after the main auditing
  # run.
  class Runner

    attr_accessor :kasp_file, :zone_name, :signed_temp, :conf_file
    attr_accessor :enable_timeshift

    # Run the auditor.
    def run
      conf_file = @conf_file
      if (!conf_file)
        KASPAuditor.exit("No configuration file specified", 1)
      end
      syslog_facility, working, zonelist, kasp_file, enforcer_interval =
        load_config_xml(conf_file)
      if (@kasp_file)
        kasp_file = @kasp_file
      end

      Syslog.open("ods-auditor", Syslog::LOG_PID |
        Syslog::LOG_CONS, syslog_facility) { |syslog|
        run_with_syslog(zonelist, kasp_file, syslog, working, enforcer_interval)
      }
    end

    def run_as_daemon
      daemon = AuditorDaemon.new
      daemon.conf_file = @conf_file
      if (@kasp_file)
        daemon.kasp_file = @kasp_file
      end
      if (@zone_name)
        daemon.zone_name = @zone_name
        if (@signed_temp)
          daemon.signed_temp = @signed_temp
        end
      end
      daemon.run
    end

    # This method is provided so that the test code can use its own syslog
    def run_with_syslog(zonelist_file, kasp_file, syslog, working, enforcer_interval) # :nodoc: all
      if (@enable_timeshift)
        configure_timeshift(syslog)
      end
      zones = nil
      begin
      zones = Parse.parse(File.dirname(kasp_file)  + File::SEPARATOR,
        zonelist_file, kasp_file, syslog)
      rescue Exception => e
        KASPAuditor.exit("Couldn't load configuration files - try running ods-kaspcheck", -LOG_ERR)
      end
      check_zones_to_audit(zones)
      # Now check the input and output zones using the config
      if (zones.length == 0)
        syslog.log(LOG_ERR, "Couldn't find any zones to load")
        KASPAuditor.exit("Couldn't find any zones to load", -LOG_ERR)
      end
      pid = Process.pid
      ret = 999 # Return value to controlling process
      zones.each {|config, input_file, output_file|
        do_audit = true
        [{input_file => "Unsigned"}, {output_file => "Signed"}].each {|hash|
{'fraction_non_alphanumeric': 0.0722041259500543, 'fraction_numerical': 0.0028230184581976114, 'mean_word_length': 3.484420642648491, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1177919', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2706, 'n_words': 1031}