Revision 26288e68 lib/utils.py

b/lib/utils.py
615 615
  """Class implementing resolver and hostname functionality
616 616

  
617 617
  """
618
  _VALID_NAME_RE = re.compile("^[a-z0-9._-]{1,255}$")
619

  
618 620
  def __init__(self, name=None):
619 621
    """Initialize the host name object.
620 622

  
......
665 667

  
666 668
    return result
667 669

  
670
  @classmethod
671
  def NormalizeName(cls, hostname):
672
    """Validate and normalize the given hostname.
673

  
674
    @attention: the validation is a bit more relaxed than the standards
675
        require; most importantly, we allow underscores in names
676
    @raise errors.OpPrereqError: when the name is not valid
677

  
678
    """
679
    hostname = hostname.lower()
680
    if (not cls._VALID_NAME_RE.match(hostname) or
681
        # double-dots, meaning empty label
682
        ".." in hostname or
683
        # empty initial label
684
        hostname.startswith(".")):
685
      raise errors.OpPrereqError("Invalid hostname '%s'" % hostname,
686
                                 errors.ECODE_INVAL)
687
    if hostname.endswith("."):
688
      hostname = hostname.rstrip(".")
689
    return hostname
690

  
668 691

  
669 692
def GetHostInfo(name=None):
670 693
  """Lookup host name and raise an OpPrereqError for failures"""

Also available in: Unified diff