lib/nmap/command.rb in ruby-nmap-1.0.1 vs lib/nmap/command.rb in ruby-nmap-1.0.2

- old
+ new

@@ -184,58 +184,66 @@ # @see http://nmap.org/book/man.html # class Command < CommandMapper::Command # + # Represents a port number. + # # @api private # class Port < CommandMapper::Types::Num - PORT_NUMBER_REGEXP = /\d{1,5}/ + # Regular expression that validates a port number. + PORT_NUMBER_REGEXP = /[1-9][0-9]{0,3}|[1-5][0-9][0-9][0-9][0-9]|6[0-4][0-9][0-9][0-9]|65[0-4][0-9][0-9]|655[0-2][0-9]|6553[0-5]/ - SERVICE_NAME_REGEXP = /[A-Za-z0-9]+(?:[\/_-][A-Za-z0-9]+)*\*?/ + # Regular expression that validates a service name. + SERVICE_NAME_REGEXP = /[A-Za-z][A-Za-z0-9]*(?:[\/_-][A-Za-z0-9]+)*\*?/ + # Regular expression that validates either a port number or service name. PORT_REGEXP = /(?:#{PORT_NUMBER_REGEXP}|#{SERVICE_NAME_REGEXP})/ + # Regular expression that validates either a port number or service name. REGEXP = /\A#{PORT_REGEXP}\z/ # # Initializes the port type. # def initialize super(range: 1..65535) end # - # Validates the given value. + # Validates the given port number value. # # @param [Object] value + # The value to validate. # # @return [true, (false, String)] + # Returns true if the value is valid, or `false` and a validation error + # message if the value is not compatible. # def validate(value) case value when String - case value - when /\A#{PORT_NUMBER_REGEXP}\z/ - super(value) - when /\A#{SERVICE_NAME_REGEXP}\z/ + if value =~ REGEXP return true else - return [false, "must be a port number or service name (#{value.inspect})"] + return [false, "must be a valid port number or service name (#{value.inspect})"] end else super(value) end end # # Formats the given value. # # @param [Integer, String] value + # The port number value to format. # # @return [String] + # The formatted port number. # def format(value) case value when String value @@ -245,24 +253,31 @@ end end # + # Represents a port range. + # # @api private # class PortRange < Port - PORT_RANGE_REGEXP = /(?:#{PORT_REGEXP}|#{PORT_REGEXP}?-#{PORT_REGEXP}?)/ + # Regular expression to validate either a port or a port range. + PORT_RANGE_REGEXP = /(?:#{PORT_NUMBER_REGEXP})?-(?:#{PORT_NUMBER_REGEXP})?|#{PORT_REGEXP}/ + # Regular expression to validate either a port or a port range. REGEXP = /\A#{PORT_RANGE_REGEXP}\z/ # - # Validates the given value. + # Validates the given port or port range value. # # @param [Object] value + # The port or port range value to validate. # # @return [true, (false, String)] + # Returns true if the value is valid, or `false` and a validation error + # message if the value is not compatible. # def validate(value) case value when Range valid, message = super(value.begin) @@ -277,26 +292,28 @@ return [valid, message] end return true when String - unless value =~ REGEXP - return [false, "not a valid port range (#{value.inspect})"] + if value =~ REGEXP + return true + else + return [false, "must be a valid port number, port range, or service name (#{value.inspect})"] end - - return true else super(value) end end # - # Formats the given value. + # Formats the given port or port range value. # # @param [Range, Integer, String] value + # The port or port range value to format. # # @return [String] + # The formatted port or port range. # def format(value) case value when Range "#{value.begin}-#{value.end}" @@ -306,16 +323,20 @@ end end # + # Represents a list of ports or port ranges. + # # @api private # class PortRangeList < CommandMapper::Types::List + # Regular expression for validating a port or port range. PORT_RANGE_REGEXP = PortRange::PORT_RANGE_REGEXP + # Regular expression for validating an nmap port range. REGEXP = /\A(?:[TUS]:)?#{PORT_RANGE_REGEXP}(?:,(?:[TUS]:)?#{PORT_RANGE_REGEXP})*\z/ # # Initializes the port range list type. # @@ -325,12 +346,15 @@ # # Validates the given port range list value. # # @param [Object] value + # The given port range list value to validate. # # @return [true, (false, String)] + # Returns true if the value is valid, or `false` and a validation error + # message if the value is not compatible. # def validate(value) case value when Hash if value.empty? @@ -369,15 +393,17 @@ udp: 'U', sctp: 'S' } # - # Formats the given value. + # Formats the given port range list value. # # @param [Hash, Range, String, Integer] value + # The port range list value. # # @return [String] + # The formatted port range list. # def format(value) case value when Hash # format a hash of protocols and port ranges @@ -398,19 +424,24 @@ end end # + # Represents a list of protocols. + # # @api private # ProtocolList = PortRangeList # + # Represents a unit of time. + # # @api private # class Time < CommandMapper::Types::Str + # Regular expression for validating a unit of time. REGEXP = /\A\d+(?:h|m|s|ms)?\z/ # # Validates a time value. # @@ -442,10 +473,12 @@ end end # + # Represents a hex string. + # # @api private # class HexString < CommandMapper::Types::Str REGEXP = /\A(?:(?:0x)?[0-9A-F]+|(?:\\x[0-9A-F]{2})+)\z/ @@ -477,23 +510,27 @@ end end # + # Represents one or more TCP scan flags. + # # @api private # class ScanFlags < CommandMapper::Types::Str + # Mapping of symbol scan flags to String values. FLAGS = { urg: 'URG', ack: 'ACK', psh: 'PSH', rst: 'RST', syn: 'SYN', fin: 'FIN' } + # Regular expression to validate the given scan flags. REGEXP = /\A(?:\d+|(?:URG|ACK|PSH|RST|SYN|FIN)+)\z/ # # Validates a scanflags value. # @@ -750,10 +787,10 @@ option '--servicedb', value: {type: InputFile.new} option '--versiondb', value: {type: InputFile.new} option '--send-eth' option '--send-ip' option '--privileged' - option '--unprivleged' + option '--unprivileged' option '--release-memory' option '--noninteractive', name: :non_interactive option '-V', name: :version option '-h', name: :help