basestorage.py 11 KB

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