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. print message
  19. syslog.syslog (message)
  20.  
  21. def get_net_of_ip(ip):
  22. """
  23. Get network in which the given ip is
  24. """
  25. interesting_networks = []
  26. for (address, net) in ip_database.items():
  27. if IP(ip) in Network(address):
  28. interesting_networks.append (address)
  29. return interesting_networks
  30.  
  31. def format_output(field, value):
  32. """
  33. Append a value to the output
  34. """
  35. output = (field + ":").ljust(24)
  36. output += value + "\n"
  37. return " "*4 + output
  38.  
  39. def format_message(message):
  40. """
  41. Normal message of the server
  42. """
  43. output = "\n"
  44. for line in message.split("\n"):
  45. line = line.strip()
  46. output += """\n %% """ + line.ljust(65) + """ %%"""
  47. return output + "\n\n"
  48.  
  49. def resolve_ip(ip):
  50. """
  51. Resolve an ip: it search all the data that match it
  52. """
  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. output += format_output ("Range", net)
  58. net = ip_database[net]
  59. if net.name is not None:
  60. output += format_output ("Name", net.name)
  61. if net.owner is not None:
  62. output += format_output ("Owner", net.owner)
  63. if net.ns is not None:
  64. output += format_output ("Nameserver", net.ns)
  65. if len(net.data.items()) > 0:
  66. output += format_message("Additional data")
  67. for k, v in net.data.items():
  68. output += format_output (k.capitalize(),v)
  69. if len(nets) == 0:
  70.  
  71. output = format_message("IP %s not found\n" % ip)
  72. return output
  73.  
  74. def get_domains_of_name(domain):
  75. interesting_domains = []
  76. for domain_name, domain_data in domain_database.items():
  77. if domain.endswith(domain_name):
  78. interesting_domains.append (domain_name)
  79.  
  80. return interesting_domains
  81.  
  82. def resolve_domain(domain):
  83. """
  84. Resolve a domain. It is not yet implemented
  85. """
  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.