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, syslog
  9. from ipcalc import IP, Network
  10.  
  11. # Dictionaries that will contain runtime data
  12. # for the server
  13. ip_database = {}
  14. domain_database = {}
  15. config = {}
  16.  
  17. def Log(message):
  18. syslog.syslog ("[pywhoisd] " + message)
  19.  
  20. def get_net_of_ip(ip):
  21. """
  22. Get network in which the given ip is
  23. """
  24. interesting_networks = []
  25. for (address, net) in ip_database.items():
  26. if IP(ip) in Network(address):
  27. interesting_networks.append (address)
  28. return interesting_networks
  29.  
  30. def format_output(field, value):
  31. """
  32. Append a value to the output
  33. """
  34. output = (field + ":").ljust(24)
  35. output += value + "\n"
  36. return " "*4 + output
  37.  
  38. def format_message(message):
  39. """
  40. Normal message of the server
  41. """
  42. output = "\n"
  43. for line in message.split("\n"):
  44. line = line.strip()
  45. output += """\n %% """ + line.ljust(65) + """ %%"""
  46. return output + "\n\n"
  47.  
  48. def resolve_ip(ip):
  49. """
  50. Resolve an ip: it search all the data that match it
  51. """
  52. output = ""
  53. nets = get_net_of_ip(ip)
  54. for net in nets:
  55. output += format_message("GNet whoisd answering for ip %s" % ip)
  56. output += format_output ("Range", net)
  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.  
  70. output = format_message("IP %s not found\n" % ip)
  71. return output
  72.  
  73. def get_domains_of_name(domain):
  74. interesting_domains = []
  75. for domain_name, domain_data in domain_database.items():
  76. if domain.endswith(domain_name):
  77. interesting_domains.append (domain_name)
  78.  
  79. return interesting_domains
  80.  
  81. def resolve_domain(domain):
  82. """
  83. Resolve a domain. It is not yet implemented
  84. """
  85. domain = domain.strip(".")
  86. output = ""
  87. domains = get_domains_of_name(domain)
  88. for d in domains:
  89. output += format_message("GNet whoisd answering for domain %s" % d)
  90. output += format_output ("Domain", d)
  91. domain = domain_database[d]
  92. if domain.name is not None:
  93. output += format_output ("Name", domain.name)
  94. if domain.owner is not None:
  95. output += format_output ("Owner", domain.owner)
  96. if domain.ns is not None:
  97. output += format_output ("Nameserver", domain.ns)
  98. if len(domain.data.items()) > 0:
  99. output += format_message("Additional data")
  100. for k, v in domain.data.items():
  101. output += format_output (k.capitalize(),v)
  102. if len(domains) == 0:
  103. return format_message("No domain found for %s" % domain)
  104. return output
  105.  
  106. def hello():
  107. """
  108. Say hello, server!
  109. """
  110. if config.has_key('welcome_banner'):
  111. return config['welcome_banner']
  112. else:
  113. return ""
  114.  
  115.  
  116. class WhoisRequestHandler(SocketServer.StreamRequestHandler):
  117. """
  118. This handler answers to the whois request
  119. """
  120.  
  121. def handle(self):
  122. # Come prima cosa salutiamo
  123. response = format_message(hello())
  124.  
  125. # Obtain the whois request
  126. request = self.rfile.readline().strip()
  127.  
  128. if re.search(r"\d+\.\d+\.\d+\.\d+", request.strip()):
  129. response += resolve_ip(request.split("/")[0])
  130. else:
  131. response += resolve_domain(request)
  132. response += format_message("Bye")
  133.  
  134. self.request.send (response)
  135. return
  136.  
  137. class Net():
  138. #
  139. # Net is a class representing the network
  140. # contained in the local database and the
  141. # net obtained from other whois server.
  142. #
  143. # It act as a C struct containing only the
  144. # data and no method.
  145. def __init__(self, name, owner, ns, data):
  146. self.name = name
  147. self.owner = owner
  148. self.ns = ns
  149. self.data = data
  150.  
  151. class Domain():
  152. #
  153. #
  154. #
  155. def __init__(self, name, owner, ns, data):
  156. self.name, self.owner, self.ns = name, owner, ns
  157. self.data = data
  158.  
  159. def populate_databases(config_file):
  160. """
  161. Populate databases with the data of our zones
  162. """
  163. Log ("Populating database with entry in %s" % config_file)
  164. try:
  165. f = open(config_file, 'r')
  166. config_data = f.read()
  167. except IOError:
  168. Log ("Error while loading config file. exiting")
  169.  
  170. nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};", config_data)
  171. for net in nets:
  172. Log ("Loading net %s" % net[0])
  173. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1])
  174. name, owner, ns = None, None, None
  175. data = {}
  176. for field, value in fields:
  177. if field.lower() == 'name':
  178. name = value.strip()
  179. elif field.lower() == 'owner':
  180. owner = value.strip()
  181. elif field.lower() == 'ns':
  182. ns = value.strip()
  183. else:
  184. data[field] = value
  185. ip_database[net[0]] = Net(name = name, owner = owner, ns = ns,
  186. data = data)
  187.  
  188. domains = re.findall(r"domain\s+([^\s]+)[\s|\n]+\{([^\}]+)\};", config_data)
  189. for domain in domains:
  190. Log ("Loading domain %s" % domain[0])
  191. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", domain[1])
  192. name, owner, ns = None, None, None
  193. data = {}
  194. for field, value in fields:
  195. if field.lower() == 'name':
  196. name = value.strip()
  197. elif field.lower() == 'owner':
  198. owner = value.strip()
  199. elif field.lower() == 'ns':
  200. ns = value.strip()
  201. else:
  202. data[field] = value
  203. domain_database[domain[0]] = Domain(name = name, owner = owner, ns = ns,
  204. data = data)
  205.  
  206.  
  207. def parse_config_file(config_file):
  208. """
  209. Parse config file and load_data into memory
  210. """
  211. config = {}
  212. f = open(config_file, 'r')
  213. content = f.read()
  214. f.close()
  215. for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content):
  216. # Gestiamo in maniera particolare
  217. # i campi che mi forniscono un array.
  218. if key == "master_domains":
  219. config[key] = []
  220. for domain in eval(value):
  221. config[key].append(domain)
  222. else:
  223. config[key] = value
  224. return config
  225.  
  226.  
  227.  
  228.  
  229. if __name__ == "__main__":
  230.  
  231. config = parse_config_file ('pywhoisd.conf')
  232. populate_databases(config['config_file'])
  233. host, port = "0.0.0.0", 43
  234. server = SocketServer.ThreadingTCPServer((host, port), WhoisRequestHandler)
  235. server.allow_reuse_address = True
  236. try:
  237. server.serve_forever(0.1)
  238. except KeyboardInterrupt:
  239. Log ("Exiting...")
  240.  
  241.  
  242.