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 "+
|
---|
13 | "given #{arguments.size} for 1")
|
---|
14 | end
|
---|
15 |
|
---|
16 | # Only allow string types
|
---|
17 | return false unless arguments[0].is_a?(String)
|
---|
18 |
|
---|
19 | domain = arguments[0].dup
|
---|
20 |
|
---|
21 | # Limits (rfc1035, 3.1)
|
---|
22 | domain_max_length=255
|
---|
23 | label_min_length=1
|
---|
24 | label_max_length=63
|
---|
25 |
|
---|
26 | # Allow ".", it is the top level domain
|
---|
27 | return true if domain == '.'
|
---|
28 |
|
---|
29 | # Remove the final dot, if present.
|
---|
30 | domain.chomp!('.')
|
---|
31 |
|
---|
32 | # Check the whole domain
|
---|
33 | return false if domain.empty?
|
---|
34 | return false if domain.length > domain_max_length
|
---|
35 |
|
---|
36 | # The top level domain must be alphabetic if there are multiple labels.
|
---|
37 | # See rfc1123, 2.1
|
---|
38 | return false if domain.include? '.' and not /\.[A-Za-z]+$/.match(domain)
|
---|
39 |
|
---|
40 | # Check each label in the domain
|
---|
41 | labels = domain.split('.')
|
---|
42 | vlabels = labels.each do |label|
|
---|
43 | break if label.length < label_min_length
|
---|
44 | break if label.length > label_max_length
|
---|
45 | break if label[-1..-1] == '-'
|
---|
46 | break if label[0..0] == '-'
|
---|
47 | break unless /^[a-z\d-]+$/i.match(label)
|
---|
48 | end
|
---|
49 | return vlabels == labels
|
---|
50 |
|
---|
51 | end
|
---|
52 | end
|
---|
53 |
|
---|
54 | # vim: set ts=2 sw=2 et :
|
---|