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.

views.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #from django.shortcuts import render
  2. from django.http import JsonResponse
  3. from django.core.exceptions import ValidationError
  4. from django.contrib.auth.views import login_required
  5. from django.db.models import Q
  6. from whoisdb.models import ASBlock, ASNumber, InetNum
  7. from domains.models import Domain, ReverseZone
  8. from dnmgmt.settings import TLD_NAMESERVERS
  9. @login_required
  10. def asblockFreeAS(request):
  11. ret = {
  12. "success": False,
  13. "errorMsg": None,
  14. "number": -1,
  15. }
  16. try:
  17. blockName = request.GET.get('block', None)
  18. if not blockName:
  19. raise ValidationError("No block given")
  20. try:
  21. mnts = request.user.maintainer_set.all()
  22. block = ASBlock.objects.filter(mnt_by__in=mnts).distinct().get(handle=blockName)
  23. if block.asblock_set.count() > 0:
  24. raise ValidationError("AS Block already has sub AS Blocks")
  25. if block.asnumber_set.count() > 0:
  26. num = block.asnumber_set.order_by("-number")[0].number + 1
  27. if num > block.asEnd:
  28. num = None
  29. for n in range(block.asBegin, block.asEnd+1):
  30. try:
  31. ASNumber.objects.get(number=n)
  32. except ASNumber.DoesNotExist:
  33. num = n
  34. break
  35. if not num:
  36. raise ValidationError("No free AS Number in block")
  37. ret["number"] = num
  38. else:
  39. ret["number"] = block.asBegin
  40. except ASBlock.DoesNotExist:
  41. raise ValidationError("Could not get AS Block")
  42. ret["success"] = True
  43. except ValidationError as e:
  44. ret["errorMsg"] = e.message
  45. return JsonResponse(ret)
  46. @login_required
  47. def freeSubnet(request):
  48. ret = {
  49. "success": False,
  50. "errorMsg": None,
  51. "network": None,
  52. }
  53. try:
  54. parentRangeName = request.GET.get('parentRange', None)
  55. if not parentRangeName:
  56. raise ValidationError("No subnet given")
  57. parentRange = None
  58. try:
  59. mnts = request.user.maintainer_set.all()
  60. parentRange = InetNum.objects.filter(mnt_by__in=mnts).distinct().get(handle=parentRangeName)
  61. except InetNum.DoesNotExist:
  62. raise ValidationError("Parent range does not exist / is not maintained by you")
  63. prefixLen = 0
  64. try:
  65. prefixLen = request.GET.get("prefixLen", None)
  66. if not prefixLen:
  67. if parentRange.protocol == InetNum.IPv4:
  68. prefixLen = 27
  69. else:
  70. prefixLen = 60
  71. prefixLen = int(prefixLen)
  72. if prefixLen < 8 or \
  73. (parentRange.protocol == InetNum.IPv4 and prefixLen > 32) or \
  74. (parentRange.protocol == InetNum.IPv6 and prefixLen > 128):
  75. raise ValidationError("Given prefix length is out of range")
  76. except ValueError:
  77. raise ValidationError("PrefixLen is not a number")
  78. usableNet = None
  79. # FIXME: use first biggest usable netblock...
  80. #biggestNet = parentRange.inetnum_set.order_by("-address")
  81. #if biggestNet
  82. # candidateNet =
  83. # and (biggestNet.getNetwork().broadcast_address.:
  84. if not usableNet:
  85. # search for free network
  86. nets = list(parentRange.getNetwork().subnets())
  87. for subRange in parentRange.inetnum_set.all():
  88. newNet = None
  89. for net in nets:
  90. if subRange.getNetwork().network_address in net:
  91. newNet = net
  92. if not newNet:
  93. # critical error, we want a 500 here
  94. raise ValueError("Subnet not in range")
  95. nets.remove(newNet)
  96. nets.extend(newNet.address_exclude(subRange.getNetwork()))
  97. nets = sorted(nets)
  98. for net in nets:
  99. if net.prefixlen <= prefixLen:
  100. usableNet = net
  101. break
  102. if not usableNet:
  103. raise ValidationError("No space left in given range")
  104. ret["network"] = "%s/%s" % (usableNet.network_address, prefixLen)
  105. ret["success"] = True
  106. except ValidationError as e:
  107. ret["errorMsg"] = e.message
  108. return JsonResponse(ret)
  109. @login_required
  110. def getSubnet(request):
  111. ret = {
  112. "success": False,
  113. "errorMsg": None,
  114. "network": None,
  115. }
  116. try:
  117. netName = request.GET.get('net', None)
  118. mnts = request.user.maintainer_set.all()
  119. nets = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  120. net = nets.get(handle=netName)
  121. ret["success"] = True
  122. ret["network"] = net.prefix()
  123. except InetNum.DoesNotExist:
  124. ret["errorMsg"] = "Chosen network does not exist"
  125. return JsonResponse(ret)
  126. @login_required
  127. def checkDomain(request):
  128. ret = {
  129. "success": False,
  130. "errorMsg": None,
  131. "domain": None,
  132. "result": None,
  133. }
  134. try:
  135. domainName = Domain.fixName(request.GET.get('domain', ''))
  136. domain = Domain.objects.get(name=domainName)
  137. if not domain.canEdit(request.user):
  138. raise Domain.DoesNotExist()
  139. ret["success"] = True
  140. ret["domain"] = domain.name
  141. ret["result"] = checkDomain(domain.name, TLD_NAMESERVERS, domain.nameservers.all())
  142. except Domain.DoesNotExist:
  143. ret["errorMsg"] = "Domain does not exist"
  144. return JsonResponse(ret)
  145. @login_required
  146. def checkRzone(request):
  147. ret = {
  148. "success": False,
  149. "errorMsg": None,
  150. "domain": None,
  151. "result": None,
  152. }
  153. try:
  154. rzonePk = ReverseZone.fixName(request.GET.get('rzone', ''))
  155. rzone = ReverseZone.objects.get(pk=rzonePk)
  156. if not rzone.canEdit(request.user):
  157. raise ReverseZone.DoesNotExist()
  158. ret["success"] = True
  159. ret["result"] = checkDomain(rzone.name, TLD_NAMESERVERS, rzone.nameservers.all())
  160. except Domain.DoesNotExist:
  161. ret["errorMsg"] = "ReverseZone does not exist"
  162. return JsonResponse(ret)