No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # This file is part of dnmgmt, a number resource management system
  2. # Licensed under GNU General Public License v3 or later
  3. # Written by Sebastian Lohff (seba@someserver.de)
  4. from django.db import models
  5. from django.urls import reverse
  6. from whoisdb.models import MntdObject, WhoisObject, Contact, InetNum
  7. import ipaddress
  8. # generally allow domains for .dn to be created
  9. # allow owners of a subnet to create reverse dns record?
  10. class Nameserver(MntdObject):
  11. handleSuffix = "NS"
  12. # dns name
  13. # ip address, if glue
  14. # ipv4/ipv6 address?
  15. handle = None
  16. name = models.CharField(max_length=256)
  17. glueIPv4 = models.GenericIPAddressField(protocol='IPv4', blank=True, null=True)
  18. glueIPv6 = models.GenericIPAddressField(protocol='IPv6', blank=True, null=True)
  19. admin_c = models.ManyToManyField(Contact)
  20. def getPK(self):
  21. return self.pk
  22. def get_absolute_url(self):
  23. return reverse("domains:nameserver-show", args=(self.pk,))
  24. def __str__(self):
  25. return self.name
  26. def getAppName(self):
  27. return "domains"
  28. def getNoDeleteReasons(self):
  29. # nameservers can always be deleted
  30. return []
  31. class Domain(MntdObject):
  32. handle = None
  33. handleSuffix = "DOM"
  34. name = models.CharField(max_length=67, unique=True, db_index=True)
  35. nameservers = models.ManyToManyField(Nameserver, blank=True)
  36. admin_c = models.ManyToManyField(Contact)
  37. def getPK(self):
  38. return self.name
  39. def getZone(self):
  40. return self.name
  41. def get_absolute_url(self):
  42. return reverse("domains:domain-show", args=(self.name,))
  43. def __str__(self):
  44. return self.name
  45. def getAppName(self):
  46. return "domains"
  47. def getNoDeleteReasons(self):
  48. reasons = []
  49. nameservers = Nameserver.objects.filter(name__endswith="." + self.name)
  50. for ns in nameservers:
  51. # FIXME: check if the nameserver has a glue record.... and also if it is maintained by us
  52. reasons.append("Nameserver %s depends on this domain" % ns.name)
  53. return reasons
  54. @classmethod
  55. def fixName(clazz, name):
  56. if not name.endswith("."):
  57. name += "."
  58. return name.lower()
  59. class ReverseZone(WhoisObject):
  60. handle = None
  61. parentNet = models.ForeignKey(InetNum)
  62. address = models.GenericIPAddressField(db_index=True)
  63. netmask = models.PositiveIntegerField()
  64. nameservers = models.ManyToManyField(Nameserver)
  65. def getPK(self):
  66. return self.pk
  67. def prefix(self):
  68. """ Helper function, mainly used in templates """
  69. return "%s/%s" % (self.address, self.netmask)
  70. def getNetwork(self):
  71. return ipaddress.ip_network(self.prefix())
  72. def getZone(self):
  73. net = self.parentNet.getNetwork()
  74. if net.version == 4:
  75. # does not work with python3.4
  76. ## for these we delegate the full domain
  77. #if 0 < net.prefixlen < 32 and net.prefixlen % 8 == 0:
  78. # zoneParts = net.reverse_pointer.split(".")
  79. # return ".".join(zoneParts[1:])
  80. #else:
  81. # # return RFC2317 compliant zone
  82. # return net.reverse_pointer
  83. parts = list(reversed(str(net.network_address).split(".")))
  84. domain = ".".join(parts[4 - net.prefixlen // 8:]) + ".in-addr.arpa."
  85. if net.prefixlen % 8 == 0:
  86. # clean cut!
  87. return domain
  88. else:
  89. # RFC2317 compliant!
  90. rfc2317Domain = "%s/%s.%s" % (parts[4 - net.prefixlen // 8 - 1], net.prefixlen, domain)
  91. return rfc2317Domain
  92. else:
  93. ## does not work with python3.4
  94. ## ipv6
  95. ## thefuck ipaddress lib... _the_ _fuck_
  96. #zoneParts = net.reverse_pointer.split(".")[-2 - net.prefixlen // 4:]
  97. #return ".".join(zoneParts)
  98. parts = list(reversed(net.exploded.split("/")[0].replace(":", "")))[32 - net.prefixlen // 4:]
  99. return ".".join(parts) + ".ip6.arpa."
  100. def get_absolute_url(self):
  101. return reverse("domains:reversezone-show", args=(self.pk,))
  102. def getAppName(self):
  103. return "domains"
  104. def __str__(self):
  105. return "%s @ %s" % (self.prefix(), self.parentNet)
  106. def getNoDeleteReasons(self):
  107. return []
  108. def canEdit(self, user):
  109. if self.parentNet:
  110. return self.parentNet.canEdit(user)
  111. else:
  112. return False