viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This is a simple whois daemon intended to
  5. # provide a basic whois for GNet.
  6. #
  7.  
  8. import SocketServer, re, math
  9. from ipcalc import IP, Network
  10.  
  11. ip_database = {}
  12. domain_database = {}
  13. config = {}
  14.  
  15. def ip_in_subnet(ip, network, subnet):
  16. ip = map(int, ip.split("."))
  17. network = map(int, network.split("."))
  18. if "." in subnet:
  19. subnet = map(int, subnet.split("."))
  20. subnet = map(lambda x: 255 - x, subnet)
  21. subnet = math.log(subnet[3] + subnet[2] * 256 + subnet[1] * pow(256,2) + subnet[0] * pow(256,3), 2)
  22. subnet = pow(2, 32 - int(subnet))
  23. ip = ip[3] + ip[2] * 256 + ip[1] * pow(256,2) + ip[0] * pow(256,3)
  24. network = network[3] + network[2] * 256 + network[1] * pow(256,2) + network[0]*pow(256,3)
  25. if ((ip - network) < subnet) and (ip-network >= 0):
  26. return True
  27. else:
  28. return False
  29.  
  30. def get_net_of_ip(ip):
  31. interesting_networks = []
  32. for (address, net) in ip_database.items():
  33. if IP(ip) in Network(address):
  34. interesting_networks.append (address)
  35. return address
  36.  
  37. def format_output(field, value):
  38. """
  39. Append a value to the output
  40. """
  41. output = (field + ":").ljust(24)
  42. output += value + "\n"
  43. return " "*4 + output
  44.  
  45. def format_message(message):
  46. output = "\n"
  47. for line in message.split("\n"):
  48. line = line.strip()
  49. output += """\n %% """ + line.ljust(85) + """ %%"""
  50. return output + "\n\n"
  51.  
  52. def resolve_ip(ip):
  53. output = ""
  54. nets = get_net_of_ip(ip)
  55. for net in nets:
  56. output += format_message("GNet whoisd answering for ip %s" % ip)
  57. net = ip_database[net]
  58. if net.name is not None:
  59. output += format_output ("Name", net.name)
  60. if net.owner is not None:
  61. output += format_output ("Owner", net.owner)
  62. if net.ns is not None:
  63. output += format_output ("Nameserver", net.ns)
  64. if len(net.data.items()) > 0:
  65. output += format_message("Additional data")
  66. for k, v in net.data.items():
  67. output += format_output (k.capitalize(),v)
  68. if len(nets) == 0:
  69. output = format_message("IP %s not found\n" % ip)
  70. output += format_message("Bye")
  71. return output
  72.  
  73. def resolve_domain(domain):
  74. return format_message("Feature not yet implemented")
  75.  
  76. def hello():
  77. if config.has_key('welcome_banner'):
  78. return config['welcome_banner']
  79. else:
  80. return ""
  81.  
  82.  
  83. class WhoisRequestHandler(SocketServer.StreamRequestHandler):
  84. """
  85. This handler answers to the whois request
  86. """
  87.  
  88. def handle(self):
  89. # Come prima cosa salutiamo
  90. response = format_message(hello())
  91.  
  92. # Obtain the whois request
  93. request = self.rfile.readline().strip()
  94.  
  95. if re.search(r"\d+\.\d+\.\d+\.\d+", request.strip()):
  96. try:
  97. response += resolve_ip(request.split("/")[0])
  98. except Exception, e:
  99. response = format_message("An error occured while processing the request, aborting.\n%s" % e)
  100. else:
  101. response += resolve_domain(request)
  102.  
  103. self.request.send (response)
  104. return
  105.  
  106. class Net():
  107.  
  108. def __init__(self, name, owner, ns, data):
  109. self.name = name
  110. self.owner = owner
  111. self.ns = ns
  112. self.data = data
  113.  
  114. def populate_databases(config_file):
  115. """
  116. Populate databases with the data of our zones
  117. """
  118. print "Populating database"
  119. try:
  120. f = open(config_file, 'r')
  121. data = f.read()
  122. except IOError:
  123. print >>stderr, "Error while loading config file. exiting"
  124.  
  125. nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};", data)
  126. for net in nets:
  127. print "> net %s loaded" % net[0]
  128. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1])
  129. name, owner, ns = None, None, None
  130. data = {}
  131. for field, value in fields:
  132. if field.lower() == 'name':
  133. name = value.strip()
  134. elif field.lower() == 'owner':
  135. owner = value.strip()
  136. elif field.lower() == 'ns':
  137. ns = value.strip()
  138. else:
  139. data[field] = value
  140. ip_database[net[0]] = Net(name = name, owner = owner, ns = ns,
  141. data = data)
  142.  
  143. def parse_config_file(config_file):
  144.  
  145. config = {}
  146. f = open(config_file, 'r')
  147. content = f.read()
  148. f.close()
  149. for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content):
  150. config[key] = value
  151. return config
  152.  
  153.  
  154.  
  155.  
  156. if __name__ == "__main__":
  157.  
  158. config = parse_config_file ('pywhoisd.conf')
  159. populate_databases(config['config_file'])
  160. host, port = "localhost", 43
  161. server = SocketServer.TCPServer((host, port), WhoisRequestHandler)
  162. try:
  163. server.serve_forever(0.1)
  164. except KeyboardInterrupt:
  165. print "Exiting...",
  166. print "done"
  167.  
  168.  
  169.