Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_domain_name.rb
1 #
2 # is_domain_name.rb
3 #
4
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.
8     EOS
9   ) do |arguments|
10
11     if (arguments.size != 1) then
12       raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
13     end
14
15     # Only allow string types
16     return false unless arguments[0].is_a?(String)
17
18     domain = arguments[0].dup
19
20     # Limits (rfc1035, 3.1)
21     domain_max_length=255
22     label_min_length=1
23     label_max_length=63
24
25     # Allow ".", it is the top level domain
26     return true if domain == '.'
27
28     # Remove the final dot, if present.
29     domain.chomp!('.')
30
31     # Check the whole domain
32     return false if domain.empty?
33     return false if domain.length > domain_max_length
34
35     # The top level domain must be alphabetic if there are multiple labels.
36     # See rfc1123, 2.1
37     return false if domain.include? '.' and not /\.[A-Za-z]+$/.match(domain)
38
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)
47     end
48     return vlabels == labels
49
50   end
51 end
52
53 # vim: set ts=2 sw=2 et :