diff --git a/app/models/cname.rb b/app/models/cname.rb index 42c28bb..bba090b 100644 --- a/app/models/cname.rb +++ b/app/models/cname.rb @@ -8,7 +8,9 @@ # Obtained from http://www.zytrax.com/books/dns/ch8/cname.html # class CNAME < Record - validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true} + validates :name, + :length => { :maximum => 1024 }, + :hostname2 => { :allow_underscore => true, :allow_wildcard_hostname => true } validates :content, :presence => true, :hostname => true end diff --git a/app/validators/hostname2_validator.rb b/app/validators/hostname2_validator.rb new file mode 100644 index 0000000..e90e093 --- /dev/null +++ b/app/validators/hostname2_validator.rb @@ -0,0 +1,47 @@ +# skips length validations +class Hostname2Validator < PAK::ValidatesHostname::HostnameValidator + + def validate_each(record, attribute, value) + value ||= '' + + # split each hostname into labels and do various checks + if value.is_a?(String) + labels = value.split '.' + labels.each_with_index do |label, index| + + # CHECK 2: hostname label cannot begin or end with hyphen + add_error(record, attribute, :label_begins_or_ends_with_hyphen) if label =~ /^[-]/i or label =~ /[-]$/ + + # Take care of wildcard first label + next if options[:allow_wildcard_hostname] and label == '*' and index == 0 + + # CHECK 3: hostname can only contain characters: + # a-z, 0-9, hyphen, optional underscore, optional asterisk + valid_chars = 'a-z0-9\-' + valid_chars << '_' if options[:allow_underscore] == true + add_error(record, attribute, :label_contains_invalid_characters, :valid_chars => valid_chars) unless label =~ /^[#{valid_chars}]+$/i + end + + # CHECK 4: the unqualified hostname portion cannot consist of + # numeric values only + if options[:allow_numeric_hostname] == false + is_numeric_only = ( + ( + Integer(labels[0]) rescue false + ) ? true : false + ) + add_error(record, attribute, :hostname_label_is_numeric) if is_numeric_only + end + + # CHECK 5: in order to be fully qualified, the full hostname's + # TLD must be valid + if options[:require_valid_tld] == true + has_tld = options[:valid_tlds].select { + |tld| tld =~ /^#{Regexp.escape(labels.last || '')}$/i + }.empty? ? false : true + add_error(record, attribute, :hostname_is_not_fqdn) unless has_tld + end + end + end + +end \ No newline at end of file