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