5 module Puppet::Parser::Functions
6 newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS
7 Returns true if the string passed to this function is a syntactically correct domain name.
11 if (arguments.size != 1) then
12 raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
15 # Only allow string types
16 return false unless arguments[0].is_a?(String)
18 domain = arguments[0].dup
20 # Limits (rfc1035, 3.1)
25 # Allow ".", it is the top level domain
26 return true if domain == '.'
28 # Remove the final dot, if present.
31 # Check the whole domain
32 return false if domain.empty?
33 return false if domain.length > domain_max_length
35 # The top level domain must be alphabetic if there are multiple labels.
37 return false if domain.include? '.' and not /\.[A-Za-z]+$/.match(domain)
39 # Check each label in the domain
40 labels = domain.split('.')
41 vlabels = labels.each do |label|
42 break if label.length < label_min_length
43 break if label.length > label_max_length
44 break if label[-1..-1] == '-'
45 break if label[0..0] == '-'
46 break unless /^[a-z\d-]+$/i.match(label)
48 return vlabels == labels
53 # vim: set ts=2 sw=2 et :