Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_domain_name.rb
1 #
2 # is_domain_name.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:is_domain_name, :type => :rvalue, :doc => <<-DOC
6     Returns true if the string passed to this function is a syntactically correct domain name.
7     DOC
8              ) do |arguments|
9
10     if arguments.size != 1
11       raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
12     end
13
14     # Only allow string types
15     return false unless arguments[0].is_a?(String)
16
17     domain = arguments[0].dup
18
19     # Limits (rfc1035, 3.1)
20     domain_max_length = 255
21     label_min_length = 1
22     label_max_length = 63
23
24     # Allow ".", it is the top level domain
25     return true if domain == '.'
26
27     # Remove the final dot, if present.
28     domain.chomp!('.')
29
30     # Check the whole domain
31     return false if domain.empty?
32     return false if domain.length > domain_max_length
33
34     # The top level domain must be alphabetic if there are multiple labels.
35     # See rfc1123, 2.1
36     return false if domain.include?('.') && !%r{\.[A-Za-z]+$}.match(domain)
37
38     # Check each label in the domain
39     labels = domain.split('.')
40     vlabels = labels.each do |label|
41       break if label.length < label_min_length
42       break if label.length > label_max_length
43       break if label[-1..-1] == '-'
44       break if label[0..0] == '-'
45       break unless %r{^[a-z\d-]+$}i =~ label
46     end
47     return vlabels == labels
48   end
49 end
50
51 # vim: set ts=2 sw=2 et :