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. #
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Whois module for the pywhoisd server
  5.  
  6. import SocketServer, re, math, syslog
  7. from ipcalc import IP, Network
  8.  
  9. def FormatOutput(field, value):
  10. """
  11. Format a field, value pair for the output
  12. from the server.
  13. """
  14. output = (field + ":").ljust(24)
  15. output += value + "\n"
  16. return " "*4 + output
  17.  
  18. def FormatMessage(message):
  19. """
  20. Prepare a message to be written from the
  21. server.
  22. """
  23. output = "\n"
  24. for line in message.split("\n"):
  25. line = line.strip()
  26. output += """\n %% """ + line.ljust(65) + """ %%"""
  27. return output + "\n\n"
  28.  
  29.  
  30. class Logger():
  31. """
  32. Logger class that allow pywhoisd to log
  33. everything on syslog.
  34. """
  35. def __init__(self, verbose = False):
  36. self.verbose = verbose
  37.  
  38. def Log(self, message):
  39. """Log a message to syslog"""
  40. syslog.syslog ("[pywhoisd] " + message)
  41. if self.verbose:
  42. print message
  43.  
  44. def __call__(self, message):
  45. self.Log (message)
  46.  
  47. class Config():
  48. """
  49. Config class manage configuration contained
  50. in the config file and acts like a dictionary
  51. that provides data to other classes
  52. """
  53. def __init__(self, config_file):
  54. """
  55. Load config file and save data in memory
  56. """
  57. self.config = {}
  58. f = open(config_file, 'r')
  59. content = f.read()
  60. f.close()
  61. for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content):
  62. self.config[key] = value
  63.  
  64. def __getitem__(self, key):
  65. if self.config.has_key(key):
  66. return self.config[key]
  67. else:
  68. return None
  69.  
  70. def has_key(self, key):
  71. return self.config.has_key(key)
  72.  
  73.  
  74. class WhoisRequestHandler(SocketServer.StreamRequestHandler):
  75. """
  76. This handler answers to the whois request that
  77. arrives to the WhoisServer.
  78. """
  79.  
  80. def handle(self):
  81. # Come prima cosa salutiamo
  82. hello = self.server.config['welcome_banner']
  83. if hello:
  84. response = FormatMessage(hello)
  85. else:
  86. response = ""
  87.  
  88. # Obtain the whois request
  89. request = self.rfile.readline().strip()
  90.  
  91. if re.search(r"\d+\.\d+\.\d+\.\d+", request.strip()):
  92. response += self.server.ResolveAddress(request.split("/")[0])
  93. else:
  94. response += self.server.ResolveDomain(request)
  95. response += FormatMessage("Bye")
  96.  
  97. self.request.send (response)
  98. return
  99.  
  100.  
  101. class WhoisServer(SocketServer.ThreadingTCPServer):
  102. """
  103. The real server. We allow reusing of address
  104. so we can stop and start in a moment.
  105. """
  106. allow_reuse_address = True
  107. def __init__(self, (host, port), logger, config):
  108.  
  109. SocketServer.ThreadingTCPServer.__init__(self, (host, port),
  110. WhoisRequestHandler)
  111. self.logger = logger
  112. self.config = config
  113. self.ip_database = {}
  114. self.domain_database = {}
  115. self.LoadDatabase(config['database_file'])
  116.  
  117. def LoadDatabase(self, database_file):
  118. """
  119. Populate server database with the data in the database
  120. file
  121. """
  122. self.logger.Log ("Populating database with entry in %s" % database_file)
  123. try:
  124. f = open(database_file, 'r')
  125. database_data = f.read()
  126. except IOError:
  127. self.logger.Log ("Error while loading config file. exiting")
  128.  
  129. nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};",
  130. database_data)
  131. for net in nets:
  132. self.logger.Log ("Loading net %s" % net[0])
  133. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1])
  134. name, owner, ns = None, None, None
  135. data = {}
  136. for field, value in fields:
  137. if field.lower() == 'name':
  138. name = value.strip()
  139. elif field.lower() == 'owner':
  140. owner = value.strip()
  141. elif field.lower() == 'ns':
  142. ns = value.strip()
  143. else:
  144. data[field] = value
  145. self.ip_database[net[0]] = Net(name = name, owner = owner, ns = ns,
  146. data = data)
  147.  
  148. domains = re.findall(r"domain\s+([^\s]+)[\s|\n]+\{([^\}]+)\};",
  149. database_data)
  150. for domain in domains:
  151. self.logger.Log ("Loading domain %s" % domain[0])
  152. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", domain[1])
  153. name, owner, ns = None, None, None
  154. data = {}
  155. for field, value in fields:
  156. if field.lower() == 'name':
  157. name = value.strip()
  158. elif field.lower() == 'owner':
  159. owner = value.strip()
  160. elif field.lower() == 'ns':
  161. ns = value.strip()
  162. else:
  163. data[field] = value
  164. self.domain_database[domain[0]] = Domain(name = name,
  165. owner = owner,
  166. ns = ns,
  167. data = data)
  168.  
  169. def ResolveDomain(self, domain):
  170. """
  171. Find domain data and return them ready for output
  172. for the server
  173. """
  174. domain = domain.strip(".")
  175. output = ""
  176. domains = self.GetParentDomains(domain)
  177. for d in domains:
  178. output += FormatMessage("GNet whoisd answering for domain %s" % d)
  179. output += FormatOutput ("Domain", d)
  180. domain = self.domain_database[d]
  181. if domain.name is not None:
  182. output += FormatOutput ("Name", domain.name)
  183. if domain.owner is not None:
  184. output += FormatOutput ("Owner", domain.owner)
  185. if domain.ns is not None:
  186. output += FormatOutput ("Nameserver", domain.ns)
  187. if len(domain.data.items()) > 0:
  188. output += FormatMessage("Additional data")
  189. for k, v in domain.data.items():
  190. output += FormatOutput (k.capitalize(),v)
  191. if len(domains) == 0:
  192. return FormatMessage("No domain found for %s" % domain)
  193. return output
  194.  
  195.  
  196. def GetParentDomains(self, domain):
  197. """
  198. Return a list of domains that contain the domain
  199. passed as argument
  200. """
  201. interesting_domains = []
  202. for domain_name, domain_data in self.domain_database.items():
  203. if domain.endswith(domain_name):
  204. interesting_domains.append (domain_name)
  205. return interesting_domains
  206.  
  207. def ResolveAddress(self, ip):
  208. """
  209. Resolve an ip: it searches all the data that match it
  210. (i.e. all the subnets that contain it) and return
  211. output text ready for the server response.
  212. """
  213. output = ""
  214. nets = self.GetParentNetworks(ip)
  215. for net in nets:
  216. output += FormatMessage("GNet whoisd answering for ip %s" % ip)
  217. output += FormatOutput ("Range", net)
  218. net = self.ip_database[net]
  219. if net.name is not None:
  220. output += FormatOutput ("Name", net.name)
  221. if net.owner is not None:
  222. output += FormatOutput ("Owner", net.owner)
  223. if net.ns is not None:
  224. output += FormatOutput ("Nameserver", net.ns)
  225. if len(net.data.items()) > 0:
  226. output += FormatMessage("Additional data")
  227. for k, v in net.data.items():
  228. output += FormatOutput (k.capitalize(),v)
  229. if len(nets) == 0:
  230. output = FormatMessage("Address %s not found\n" % ip)
  231. return output
  232.  
  233. def GetParentNetworks(self, ip):
  234. """
  235. Get network in which the given ip is
  236. """
  237. interesting_networks = []
  238. for (address, net) in self.ip_database.items():
  239. try:
  240. if IP(ip) in Network(address):
  241. interesting_networks.append (address)
  242. except:
  243. continue
  244. return interesting_networks
  245.  
  246. class Net():
  247. """
  248. Net is a class representing the network
  249. contained in the local database and the
  250. net obtained from other whois server.
  251. It act as a C struct containing only the
  252. data and no method.
  253. """
  254. def __init__(self, name, owner, ns, data):
  255. self.name = name
  256. self.owner = owner
  257. self.ns = ns
  258. self.data = data
  259.  
  260. class Domain():
  261. """
  262. The same of Net, but with the domain names.
  263. """
  264. def __init__(self, name, owner, ns, data):
  265. self.name, self.owner, self.ns = name, owner, ns
  266. self.data = data