basestorage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function, unicode_literals
  4. import logging
  5. import re
  6. import time
  7. import ffstatus
  8. from .exceptions import VpnKeyFormatError
  9. def sanitize_node(data, include_raw_data=False):
  10. """
  11. Filters potentially harmful entries from the node's data.
  12. """
  13. export = ffstatus.dict_merge({}, data)
  14. # remove fields from output: __RAW__
  15. if '__RAW__' in export and not include_raw_data:
  16. del export['__RAW__']
  17. return export
  18. class BaseStorage(object):
  19. """
  20. Provides operations on the storage data.
  21. This class gets subclassed to actually write the data
  22. to a file, database, whatever.
  23. """
  24. DATAKEY_VPN = '__VPN__'
  25. FIELDKEY_UPDATED = '__UPDATED__'
  26. __data = None
  27. @property
  28. def data(self):
  29. """Contains the data handled by this storage."""
  30. return self.__data
  31. @data.setter
  32. def data(self, value):
  33. """setter for data property"""
  34. logging.debug('Setting new storage data (old=%d/new=%d items).',
  35. len(self.__data) if self.__data is not None else 0,
  36. len(value) if value is not None else 0)
  37. self.__data = value
  38. def open(self):
  39. """
  40. When overridden in a subclass,
  41. closes the persistent storage.
  42. """
  43. pass
  44. def save(self):
  45. """
  46. When overriden in a subclass,
  47. stores the data to a persistent storage.
  48. """
  49. pass
  50. def close(self):
  51. """
  52. When overridden in a subclass,
  53. closes the persistent storage.
  54. """
  55. pass
  56. def merge_new_data(self, newdata):
  57. """Updates data in the storage by merging the new data."""
  58. if newdata is None or not isinstance(newdata, dict):
  59. raise ValueError("Expected a dict as new data.")
  60. # start merge on a copy of the current data
  61. current = ffstatus.dict_merge(self.data, {})
  62. for item_id in current:
  63. if not item_id in newdata:
  64. continue
  65. current[item_id]['aliases'] = []
  66. current[item_id]['clients'] = []
  67. current[item_id]['neighbours'] = []
  68. if not '__RAW__' in current[item_id]:
  69. current[item_id]['__RAW__'] = {}
  70. if '__RAW__' in newdata[item_id]:
  71. for key in newdata[item_id]['__RAW__']:
  72. if key in current[item_id]['__RAW__']:
  73. del current[item_id]['__RAW__'][key]
  74. # merge the dictionaries
  75. updated = ffstatus.dict_merge(current, newdata)
  76. # sanitize each item's data
  77. for itemid in updated:
  78. if itemid.startswith('__'):
  79. continue
  80. item = updated[itemid]
  81. # remove node's MACs from clients list
  82. clients = [x for x in item.get('clients', [])]
  83. if 'mac' in item and item['mac'] in clients:
  84. clients.remove(item['mac'])
  85. for mac in item.get('macs', []):
  86. if mac in clients:
  87. clients.remove(mac)
  88. # set clientcount
  89. updated[itemid]['clientcount'] = len(clients)
  90. # set the new data
  91. self.__data = updated
  92. def get_nodes(self, sortby=None, include_raw_data=False):
  93. """Gets a list of all known nodes."""
  94. sorted_ids = self.data.keys()
  95. if not sortby is None:
  96. if sortby == 'name':
  97. sortkey = lambda x: self.data[x]['hostname'].lower()
  98. sorted_ids = sorted(self.data, key=sortkey)
  99. elif sortby == 'id':
  100. sorted_ids = sorted(self.data)
  101. result = []
  102. for nodeid in sorted_ids:
  103. if nodeid.startswith('__'):
  104. continue
  105. node = sanitize_node(self.data[nodeid], include_raw_data)
  106. result.append(node)
  107. return result
  108. def find_node(self, rawid):
  109. """
  110. Fetch node data by given id.
  111. If necessary, look through node aliases.
  112. """
  113. # if we have a direct hit, return it immediately
  114. if rawid in self.data:
  115. return sanitize_node(self.data[rawid])
  116. # no direct hit -> search via aliases
  117. nodeid = rawid
  118. for nid in self.data:
  119. node = self.data[nid]
  120. if 'aliases' in node and rawid in node['aliases']:
  121. nodeid = nid
  122. # return found node
  123. if nodeid in self.data:
  124. return sanitize_node(self.data[nodeid])
  125. else:
  126. return None
  127. def find_node_by_mac(self, mac):
  128. """Fetch node data by given MAC address."""
  129. needle = mac.lower()
  130. # iterate over all nodes
  131. for nodeid in self.data:
  132. if nodeid.startswith('__'):
  133. continue
  134. node = self.data[nodeid]
  135. # check node's primary MAC
  136. if 'mac' in node and needle == node['mac'].lower():
  137. return sanitize_node(node)
  138. # check alias MACs
  139. if 'macs' in node:
  140. haystack = [x.lower() for x in node['macs']]
  141. if mac in haystack:
  142. return sanitize_node(node)
  143. # MAC address not found
  144. return None
  145. def get_nodestatus(self, rawid):
  146. """Determine node's status."""
  147. # search node by the given id
  148. node = self.find_node(rawid)
  149. # handle unknown nodes
  150. if node is None:
  151. return None
  152. # check that the last batadv update is noted in the data
  153. updated = node.get(self.FIELDKEY_UPDATED, None)
  154. if updated is None or not 'batadv' in updated:
  155. return 'unknown'
  156. # make decision based on time of last batadv update
  157. diff = time.time() - updated['batadv']
  158. if diff < 150:
  159. return 'active'
  160. elif diff < 300:
  161. return 'stale'
  162. else:
  163. return 'offline'
  164. def resolve_vpn_remotes(self):
  165. if not self.DATAKEY_VPN in self.data:
  166. return
  167. vpn = self.data[self.DATAKEY_VPN]
  168. init_vpn_cache = {}
  169. for key in vpn:
  170. if not isinstance(vpn[key], dict):
  171. continue
  172. for mode in vpn[key]:
  173. if not isinstance(vpn[key][mode], dict):
  174. continue
  175. for gateway in vpn[key][mode]:
  176. if not isinstance(vpn[key][mode][gateway], dict):
  177. continue
  178. item = vpn[key][mode][gateway]
  179. if 'remote' in item and not 'remote_raw' in item:
  180. item['remote_raw'] = item['remote']
  181. resolved = None
  182. if item['remote'] in init_vpn_cache:
  183. resolved = init_vpn_cache[item['remote']]
  184. else:
  185. resolved = ffstatus.resolve_ipblock(item['remote'])
  186. init_vpn_cache[item['remote']] = resolved
  187. if not resolved is None:
  188. logging.info(
  189. 'Resolved VPN entry \'%s\' to net \'%s\'.',
  190. item['remote'],
  191. resolved['name'],
  192. )
  193. if not resolved is None:
  194. item['remote'] = resolved
  195. self.save()
  196. def __get_vpn_item(self, key, create=False):
  197. if key is None or re.match(r'^[a-fA-F0-9]+$', key) is None:
  198. raise VpnKeyFormatError(key)
  199. return
  200. if not self.DATAKEY_VPN in self.data:
  201. if not create:
  202. return None
  203. self.data[self.DATAKEY_VPN] = {}
  204. if not key in self.data[self.DATAKEY_VPN]:
  205. if not create:
  206. return None
  207. self.data[self.DATAKEY_VPN][key] = {'active': {}, 'last': {}}
  208. return self.data[self.DATAKEY_VPN][key]
  209. def get_vpn_gateways(self):
  210. if not self.DATAKEY_VPN in self.data:
  211. return []
  212. gateways = set()
  213. vpn = self.data[self.DATAKEY_VPN]
  214. for key in vpn:
  215. for conntype in vpn[key]:
  216. for gateway in vpn[key][conntype]:
  217. gateways.add(gateway)
  218. return sorted(gateways)
  219. def get_vpn_connections(self):
  220. if not self.DATAKEY_VPN in self.data:
  221. return []
  222. conntypes = ['active', 'last']
  223. result = []
  224. vpn = self.data[self.DATAKEY_VPN]
  225. for key in vpn:
  226. vpn_entry = vpn[key]
  227. if not isinstance(vpn_entry, dict):
  228. continue
  229. item = {
  230. 'key': key,
  231. 'count': {},
  232. 'remote': {},
  233. }
  234. names = set()
  235. for conntype in conntypes:
  236. item['count'][conntype] = 0
  237. item['remote'][conntype] = {}
  238. if conntype in vpn_entry:
  239. for gateway in vpn_entry[conntype]:
  240. if 'remote' in vpn_entry[conntype][gateway]:
  241. remote = vpn_entry[conntype][gateway]['remote']
  242. if isinstance(remote, basestring) and len(remote) == 0:
  243. continue
  244. item['count'][conntype] += 1
  245. item['remote'][conntype][gateway] = remote
  246. if 'peer' in vpn_entry[conntype][gateway]:
  247. names.add(vpn_entry[conntype][gateway]['peer'])
  248. item['names'] = sorted(names)
  249. item['online'] = item['count']['active'] > 0
  250. result.append(item)
  251. return result
  252. def log_vpn_connect(self, key, peername, remote, gateway, timestamp):
  253. item = self.__get_vpn_item(key, create=True)
  254. # resolve remote addr to its netblock
  255. remote_raw = remote
  256. remote_resolved = None
  257. if remote is not None:
  258. remote_resolved = ffstatus.resolve_ipblock(remote)
  259. if not remote_resolved is None:
  260. logging.debug('Resolved IP \'{0}\' to block \'{1}\'.'.format(
  261. remote, remote_resolved['name'],
  262. ))
  263. remote = remote_resolved
  264. # store connection info
  265. item['active'][gateway] = {
  266. 'establish': timestamp,
  267. 'peer': peername,
  268. 'remote': remote,
  269. 'remote_raw': remote_raw,
  270. }
  271. def log_vpn_disconnect(self, key, gateway, timestamp):
  272. item = self.__get_vpn_item(key, create=True)
  273. active = {}
  274. if gateway in item['active']:
  275. active = item['active'][gateway]
  276. del item['active'][gateway]
  277. active['disestablish'] = timestamp
  278. item['last'][gateway] = active