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.

helpers.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import whoisdb.models
  5. import domains.models
  6. from django.db.models import F, When, Max, Case, IntegerField
  7. import ipaddress
  8. import re
  9. def _addFields(fields, obj, fieldNames):
  10. for fieldName in fieldNames:
  11. fields.append((fieldName.capitalize().replace("_", " "), getattr(obj, fieldName)))
  12. def getWhoisObjectFields(obj, owner):
  13. fields = []
  14. if getattr(obj, "handle", None):
  15. _addFields(fields, obj, ["handle"])
  16. c = type(obj)
  17. if c == whoisdb.models.Maintainer:
  18. _addFields(fields, obj, ["description", "admin_c"])
  19. if owner:
  20. _addFields(fields, obj, ["auth"])
  21. elif c == whoisdb.models.Contact:
  22. _addFields(fields, obj, ["name", "mnt_by"])
  23. elif c == whoisdb.models.ASBlock:
  24. _addFields(fields, obj, ["name"])
  25. fields.append(("AS Range", "%s - %s" % (obj.asBegin, obj.asEnd)))
  26. _addFields(fields, obj, ["description", "parent_block", "mnt_by", "mnt_lower", "admin_c"])
  27. elif c == whoisdb.models.ASNumber:
  28. _addFields(fields, obj, ["name", "number", "description", "asblock", "volatile", "mnt_by", "mnt_lower", "admin_c"])
  29. elif c == whoisdb.models.InetNum:
  30. _addFields(fields, obj, ["name"])
  31. fields.append(("Address CIDR", obj.prefix()))
  32. _addFields(fields, obj, ["description", "parent_range", "origin_as", "mnt_by", "mnt_lower", "admin_c"])
  33. elif c == domains.models.Domain:
  34. _addFields(fields, obj, ["name", "nameservers", "mnt_by", "admin_c"])
  35. elif c == domains.models.Nameserver:
  36. _addFields(fields, obj, ["name", "glueIPv4", "glueIPv6", "mnt_by", "admin_c"])
  37. elif c == domains.models.ReverseZone:
  38. #_addFields(fields, obj, ["name"])
  39. fields.append(("Address CIDR", obj.prefix()))
  40. _addFields(fields, obj, ["parentNet", "nameservers"])
  41. _addFields(fields, obj, ["created", "last_modified"])
  42. return fields
  43. def guessWhoisObject(self, handle):
  44. # is it a normal handle?
  45. pass
  46. def findInDatabase(rawValue):
  47. # is this an ip address?
  48. rawValue = rawValue.strip().upper()
  49. value = None
  50. results = []
  51. # try subnetwork
  52. try:
  53. value = ipaddress.ip_network(rawValue, strict=False)
  54. except ValueError:
  55. pass
  56. if value:
  57. # ssubnet
  58. obj = whoisdb.models.InetNum.objects.filter(address=str(value.network_address), netmask=value.prefixlen)
  59. results.extend(obj)
  60. # single ip
  61. value = None
  62. try:
  63. value = ipaddress.ip_address(rawValue)
  64. except ValueError:
  65. pass
  66. if value:
  67. # NOTE: this is only for "small subnets", we could increase this...
  68. baseaddr = None
  69. if value.version == 4:
  70. basenet = ipaddress.ip_network("%s/24" % value, strict=False)
  71. baseaddr = ".".join(str(basenet).split(".")[0:3]) + "."
  72. else:
  73. basenet = ipaddress.ip_network("%s/56" % value.exploded, strict=False)
  74. baseaddr = ":".join(str(basenet).split(":")[0:4])[-2]
  75. nets = whoisdb.models.InetNum.objects.filter(address__startswith=baseaddr).order_by("-netmask")
  76. for net in nets:
  77. if value in net.getNetwork():
  78. results.append(net)
  79. break
  80. # asnumber?
  81. m = re.match("^(?:AS)?(\d+)$", rawValue)
  82. if m:
  83. # asnumber!
  84. num = int(m.group(1))
  85. obj = whoisdb.models.ASNumber.objects.filter(number=num)
  86. results.extend(obj)
  87. # find a matching block
  88. blocks = whoisdb.models.ASBlock.objects.filter(asBegin__lte=num, asEnd__gte=num).annotate(size=F('asEnd')-F('asBegin')).order_by('size')
  89. if blocks.count() > 0:
  90. results.append(blocks[0])
  91. # asblocks? smallest asblock containing the range
  92. # WHEN anotation foo... could also be done in asnumber match
  93. # also look for number - number queries?
  94. # domain?
  95. if rawValue.endswith("DN") or rawValue.endswith("DN."):
  96. value = rawValue.lower()
  97. if not value.endswith("."):
  98. value += "."
  99. obj = domains.models.Domain.objects.filter(name=value)
  100. results.extend(obj)
  101. # contact by name?
  102. # handlenames for Maintainer, Contact, InetNum, ASNumber, ASBlock
  103. handleObjs = [
  104. whoisdb.models.Contact,
  105. whoisdb.models.Maintainer,
  106. whoisdb.models.InetNum,
  107. whoisdb.models.ASBlock,
  108. whoisdb.models.ASNumber,
  109. ]
  110. for handleObj in handleObjs:
  111. obj = handleObj.objects.filter(handle=rawValue)
  112. if not obj and len(rawValue) >= 3:
  113. obj = handleObj.objects.filter(handle__startswith=rawValue)
  114. results.extend(obj)
  115. return results
  116. def findHandleFromStr(rawValue):
  117. handleObjs = [
  118. whoisdb.models.Contact,
  119. whoisdb.models.Maintainer,
  120. whoisdb.models.InetNum,
  121. whoisdb.models.ASBlock,
  122. whoisdb.models.ASNumber,
  123. ]
  124. for handleObj in handleObjs:
  125. try:
  126. return handleObj.objects.get(handle=rawValue)
  127. except handleObj.DoesNotExist:
  128. pass
  129. return None
  130. def orderQueryset(qs, userOwned, objOwned):
  131. # when for
  132. whens = [When(userOwned, then=2)]
  133. if objOwned:
  134. # add existing
  135. whens.append(When(objOwned, then=1))
  136. qs = qs.annotate(card=Max(Case(*whens, default=0, output_field=IntegerField()))).order_by("-card")
  137. return qs