__init__.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2011-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. '''
  26. Qubes OS
  27. :copyright: © 2010-2015 Invisible Things Lab
  28. '''
  29. from __future__ import absolute_import
  30. __author__ = 'Invisible Things Lab'
  31. __license__ = 'GPLv2 or later'
  32. __version__ = 'R3'
  33. import ast
  34. import atexit
  35. import collections
  36. import errno
  37. import grp
  38. import logging
  39. import os
  40. import os.path
  41. import sys
  42. import tempfile
  43. import time
  44. import warnings
  45. import __builtin__
  46. import docutils.core
  47. import docutils.io
  48. import lxml.etree
  49. import qubes.config
  50. import qubes.events
  51. import qubes.exc
  52. import qubes.ext
  53. if os.name == 'posix':
  54. import fcntl
  55. elif os.name == 'nt':
  56. # pylint: disable=import-error
  57. import win32con
  58. import win32file
  59. import pywintypes
  60. else:
  61. raise RuntimeError("Qubes works only on POSIX or WinNT systems")
  62. import libvirt
  63. try:
  64. import xen.lowlevel.xs
  65. import xen.lowlevel.xc
  66. except ImportError:
  67. pass
  68. class VMMConnection(object):
  69. '''Connection to Virtual Machine Manager (libvirt)'''
  70. def __init__(self):
  71. self._libvirt_conn = None
  72. self._xs = None
  73. self._xc = None
  74. self._offline_mode = False
  75. @__builtin__.property
  76. def offline_mode(self):
  77. '''Check or enable offline mode (do not actually connect to vmm)'''
  78. return self._offline_mode
  79. @offline_mode.setter
  80. def offline_mode(self, value):
  81. if value and self._libvirt_conn is not None:
  82. raise qubes.exc.QubesException(
  83. 'Cannot change offline mode while already connected')
  84. self._offline_mode = value
  85. def _libvirt_error_handler(self, ctx, error):
  86. pass
  87. def init_vmm_connection(self):
  88. '''Initialise connection
  89. This method is automatically called when getting'''
  90. if self._libvirt_conn is not None:
  91. # Already initialized
  92. return
  93. if self._offline_mode:
  94. # Do not initialize in offline mode
  95. raise qubes.exc.QubesException(
  96. 'VMM operations disabled in offline mode')
  97. if 'xen.lowlevel.xs' in sys.modules:
  98. self._xs = xen.lowlevel.xs.xs()
  99. if 'xen.lowlevel.cs' in sys.modules:
  100. self._xc = xen.lowlevel.xc.xc()
  101. self._libvirt_conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
  102. if self._libvirt_conn is None:
  103. raise qubes.exc.QubesException('Failed connect to libvirt driver')
  104. libvirt.registerErrorHandler(self._libvirt_error_handler, None)
  105. atexit.register(self._libvirt_conn.close)
  106. @__builtin__.property
  107. def libvirt_conn(self):
  108. '''Connection to libvirt'''
  109. self.init_vmm_connection()
  110. return self._libvirt_conn
  111. @__builtin__.property
  112. def xs(self):
  113. '''Connection to Xen Store
  114. This property in available only when running on Xen.
  115. '''
  116. # XXX what about the case when we run under KVM,
  117. # but xen modules are importable?
  118. if 'xen.lowlevel.xs' not in sys.modules:
  119. raise AttributeError(
  120. 'xs object is available under Xen hypervisor only')
  121. self.init_vmm_connection()
  122. return self._xs
  123. @__builtin__.property
  124. def xc(self):
  125. '''Connection to Xen
  126. This property in available only when running on Xen.
  127. '''
  128. # XXX what about the case when we run under KVM,
  129. # but xen modules are importable?
  130. if 'xen.lowlevel.xc' not in sys.modules:
  131. raise AttributeError(
  132. 'xc object is available under Xen hypervisor only')
  133. self.init_vmm_connection()
  134. return self._xs
  135. class QubesHost(object):
  136. '''Basic information about host machine
  137. :param qubes.Qubes app: Qubes application context (must have \
  138. :py:attr:`Qubes.vmm` attribute defined)
  139. '''
  140. def __init__(self, app):
  141. self.app = app
  142. self._no_cpus = None
  143. self._total_mem = None
  144. self._physinfo = None
  145. def _fetch(self):
  146. if self._no_cpus is not None:
  147. return
  148. # pylint: disable=unused-variable
  149. (model, memory, cpus, mhz, nodes, socket, cores, threads) = \
  150. self.app.vmm.libvirt_conn.getInfo()
  151. self._total_mem = long(memory) * 1024
  152. self._no_cpus = cpus
  153. self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(
  154. self.no_cpus, self.memory_total))
  155. try:
  156. self.app.log.debug('QubesHost: xen_free_memory={}'.format(
  157. self.get_free_xen_memory()))
  158. except NotImplementedError:
  159. pass
  160. @__builtin__.property
  161. def memory_total(self):
  162. '''Total memory, in bytes'''
  163. self._fetch()
  164. return self._total_mem
  165. @__builtin__.property
  166. def no_cpus(self):
  167. '''Number of CPUs'''
  168. self._fetch()
  169. return self._no_cpus
  170. def get_free_xen_memory(self):
  171. '''Get free memory from Xen's physinfo.
  172. :raises NotImplementedError: when not under Xen
  173. '''
  174. try:
  175. self._physinfo = self.app.xc.physinfo()
  176. except AttributeError:
  177. raise NotImplementedError('This function requires Xen hypervisor')
  178. return long(self._physinfo['free_memory'])
  179. def measure_cpu_usage(self, previous_time=None, previous=None,
  180. wait_time=1):
  181. '''Measure cpu usage for all domains at once.
  182. This function requires Xen hypervisor.
  183. .. versionchanged:: 3.0
  184. argument order to match return tuple
  185. :raises NotImplementedError: when not under Xen
  186. '''
  187. if previous is None:
  188. previous_time = time.time()
  189. previous = {}
  190. try:
  191. info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
  192. except AttributeError:
  193. raise NotImplementedError(
  194. 'This function requires Xen hypervisor')
  195. for vm in info:
  196. previous[vm['domid']] = {}
  197. previous[vm['domid']]['cpu_time'] = (
  198. vm['cpu_time'] / vm['online_vcpus'])
  199. previous[vm['domid']]['cpu_usage'] = 0
  200. time.sleep(wait_time)
  201. current_time = time.time()
  202. current = {}
  203. try:
  204. info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
  205. except AttributeError:
  206. raise NotImplementedError(
  207. 'This function requires Xen hypervisor')
  208. for vm in info:
  209. current[vm['domid']] = {}
  210. current[vm['domid']]['cpu_time'] = (
  211. vm['cpu_time'] / max(vm['online_vcpus'], 1))
  212. if vm['domid'] in previous.keys():
  213. current[vm['domid']]['cpu_usage'] = (
  214. float(current[vm['domid']]['cpu_time'] -
  215. previous[vm['domid']]['cpu_time']) /
  216. long(1000 ** 3) / (current_time - previous_time) * 100)
  217. if current[vm['domid']]['cpu_usage'] < 0:
  218. # VM has been rebooted
  219. current[vm['domid']]['cpu_usage'] = 0
  220. else:
  221. current[vm['domid']]['cpu_usage'] = 0
  222. return (current_time, current)
  223. class Label(object):
  224. '''Label definition for virtual machines
  225. Label specifies colour of the padlock displayed next to VM's name.
  226. When this is a :py:class:`qubes.vm.dispvm.DispVM`, padlock is overlayed
  227. with recycling pictogram.
  228. :param int index: numeric identificator of label
  229. :param str color: colour specification as in HTML (``#abcdef``)
  230. :param str name: label's name like "red" or "green"
  231. '''
  232. def __init__(self, index, color, name):
  233. #: numeric identificator of label
  234. self.index = index
  235. #: colour specification as in HTML (``#abcdef``)
  236. self.color = color
  237. #: label's name like "red" or "green"
  238. self.name = name
  239. #: freedesktop icon name, suitable for use in
  240. #: :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
  241. self.icon = 'appvm-' + name
  242. #: freedesktop icon name, suitable for use in
  243. #: :py:meth:`PyQt4.QtGui.QIcon.fromTheme` on DispVMs
  244. self.icon_dispvm = 'dispvm-' + name
  245. @classmethod
  246. def fromxml(cls, xml):
  247. '''Create label definition from XML node
  248. :param lxml.etree._Element xml: XML node reference
  249. :rtype: :py:class:`qubes.Label`
  250. '''
  251. index = int(xml.get('id').split('-', 1)[1])
  252. color = xml.get('color')
  253. name = xml.text
  254. return cls(index, color, name)
  255. def __xml__(self):
  256. element = lxml.etree.Element(
  257. 'label', id='label-{}'.format(self.index), color=self.color)
  258. element.text = self.name
  259. return element
  260. def __repr__(self):
  261. return '{}({!r}, {!r}, {!r})'.format(
  262. self.__class__.__name__,
  263. self.index,
  264. self.color,
  265. self.name)
  266. @__builtin__.property
  267. def icon_path(self):
  268. '''Icon path
  269. .. deprecated:: 2.0
  270. use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon`
  271. '''
  272. return os.path.join(qubes.config.system_path['qubes_icon_dir'],
  273. self.icon) + ".png"
  274. @__builtin__.property
  275. def icon_path_dispvm(self):
  276. '''Icon path
  277. .. deprecated:: 2.0
  278. use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon_dispvm`
  279. '''
  280. return os.path.join(qubes.config.system_path['qubes_icon_dir'],
  281. self.icon_dispvm) + ".png"
  282. class VMCollection(object):
  283. '''A collection of Qubes VMs
  284. VMCollection supports ``in`` operator. You may test for ``qid``, ``name``
  285. and whole VM object's presence.
  286. Iterating over VMCollection will yield machine objects.
  287. '''
  288. def __init__(self, app):
  289. self.app = app
  290. self._dict = dict()
  291. def __repr__(self):
  292. return '<{} {!r}>'.format(
  293. self.__class__.__name__, list(sorted(self.keys())))
  294. def items(self):
  295. '''Iterate over ``(qid, vm)`` pairs'''
  296. for qid in self.qids():
  297. yield (qid, self[qid])
  298. def qids(self):
  299. '''Iterate over all qids
  300. qids are sorted by numerical order.
  301. '''
  302. return iter(sorted(self._dict.keys()))
  303. keys = qids
  304. def names(self):
  305. '''Iterate over all names
  306. names are sorted by lexical order.
  307. '''
  308. return iter(sorted(vm.name for vm in self._dict.values()))
  309. def vms(self):
  310. '''Iterate over all machines
  311. vms are sorted by qid.
  312. '''
  313. return iter(sorted(self._dict.values()))
  314. __iter__ = vms
  315. values = vms
  316. def add(self, value):
  317. '''Add VM to collection
  318. :param qubes.vm.BaseVM value: VM to add
  319. :raises TypeError: when value is of wrong type
  320. :raises ValueError: when there is already VM which has equal ``qid``
  321. '''
  322. # this violates duck typing, but is needed
  323. # for VMProperty to function correctly
  324. if not isinstance(value, qubes.vm.BaseVM):
  325. raise TypeError('{} holds only BaseVM instances'.format(
  326. self.__class__.__name__))
  327. if value.qid in self:
  328. raise ValueError('This collection already holds VM that has '
  329. 'qid={!r} ({!r})'.format(value.qid, self[value.qid]))
  330. if value.name in self:
  331. raise ValueError('This collection already holds VM that has '
  332. 'name={!r} ({!r})'.format(value.name, self[value.name]))
  333. self._dict[value.qid] = value
  334. value.events_enabled = True
  335. self.app.fire_event('domain-added', value)
  336. return value
  337. def __getitem__(self, key):
  338. if isinstance(key, int):
  339. return self._dict[key]
  340. if isinstance(key, basestring):
  341. for vm in self:
  342. if vm.name == key:
  343. return vm
  344. raise KeyError(key)
  345. if isinstance(key, qubes.vm.BaseVM):
  346. if key in self:
  347. return key
  348. raise KeyError(key)
  349. raise KeyError(key)
  350. def __delitem__(self, key):
  351. vm = self[key]
  352. del self._dict[vm.qid]
  353. self.app.fire_event('domain-deleted', vm)
  354. def __contains__(self, key):
  355. return any((key == vm or key == vm.qid or key == vm.name)
  356. for vm in self)
  357. def __len__(self):
  358. return len(self._dict)
  359. def get_vms_based_on(self, template):
  360. template = self[template]
  361. return set(vm for vm in self if vm.template == template)
  362. def get_vms_connected_to(self, netvm):
  363. new_vms = set([self[netvm]])
  364. dependent_vms = set()
  365. # Dependency resolving only makes sense on NetVM (or derivative)
  366. # if not self[netvm_qid].is_netvm():
  367. # return set([])
  368. while len(new_vms) > 0:
  369. cur_vm = new_vms.pop()
  370. for vm in cur_vm.connected_vms.values():
  371. if vm in dependent_vms:
  372. continue
  373. dependent_vms.add(vm.qid)
  374. # if vm.is_netvm():
  375. new_vms.add(vm.qid)
  376. return dependent_vms
  377. # XXX with Qubes Admin Api this will probably lead to race condition
  378. # whole process of creating and adding should be synchronised
  379. def get_new_unused_qid(self):
  380. used_ids = set(self.qids())
  381. for i in range(1, qubes.config.max_qid):
  382. if i not in used_ids:
  383. return i
  384. raise LookupError("Cannot find unused qid!")
  385. def get_new_unused_netid(self):
  386. used_ids = set([vm.netid for vm in self]) # if vm.is_netvm()])
  387. for i in range(1, qubes.config.max_netid):
  388. if i not in used_ids:
  389. return i
  390. raise LookupError("Cannot find unused netid!")
  391. class property(object): # pylint: disable=redefined-builtin,invalid-name
  392. '''Qubes property.
  393. This class holds one property that can be saved to and loaded from
  394. :file:`qubes.xml`. It is used for both global and per-VM properties.
  395. Property can be unset by ordinary ``del`` statement or assigning
  396. :py:attr:`DEFAULT` special value to it. After deletion (or before first
  397. assignment/load) attempting to read a property will get its default value
  398. or, when no default, py:class:`exceptions.AttributeError`.
  399. :param str name: name of the property
  400. :param collections.Callable setter: if not :py:obj:`None`, this is used to \
  401. initialise value; first parameter to the function is holder instance \
  402. and the second is value; this is called before ``type``
  403. :param collections.Callable saver: function to coerce value to something \
  404. readable by setter
  405. :param type type: if not :py:obj:`None`, value is coerced to this type
  406. :param object default: default value; if callable, will be called with \
  407. holder as first argument
  408. :param int load_stage: stage when property should be loaded (see \
  409. :py:class:`Qubes` for description of stages)
  410. :param int order: order of evaluation (bigger order values are later)
  411. :param str ls_head: column head for :program:`qvm-ls`
  412. :param int ls_width: column width in :program:`qvm-ls`
  413. :param str doc: docstring; this should be one paragraph of plain RST, no \
  414. sphinx-specific features
  415. Setters and savers have following signatures:
  416. .. :py:function:: setter(self, prop, value)
  417. :noindex:
  418. :param self: instance of object that is holding property
  419. :param prop: property object
  420. :param value: value being assigned
  421. .. :py:function:: saver(self, prop, value)
  422. :noindex:
  423. :param self: instance of object that is holding property
  424. :param prop: property object
  425. :param value: value being saved
  426. :rtype: str
  427. :raises property.DontSave: when property should not be saved at all
  428. '''
  429. #: Assigning this value to property means setting it to its default value.
  430. #: If property has no default value, this will unset it.
  431. DEFAULT = object()
  432. # internal use only
  433. _NO_DEFAULT = object()
  434. def __init__(self, name, setter=None, saver=None, type=None,
  435. default=_NO_DEFAULT, write_once=False, load_stage=2, order=0,
  436. save_via_ref=False,
  437. ls_head=None, ls_width=None, doc=None):
  438. # pylint: disable=redefined-builtin
  439. self.__name__ = name
  440. self._setter = setter
  441. self._saver = saver if saver is not None else (
  442. lambda self, prop, value: str(value))
  443. self._type = type
  444. self._default = default
  445. self._write_once = write_once
  446. self.order = order
  447. self.load_stage = load_stage
  448. self.save_via_ref = save_via_ref
  449. self.__doc__ = doc
  450. self._attr_name = '_qubesprop_' + name
  451. if ls_head is not None or ls_width is not None:
  452. self.ls_head = ls_head or self.__name__.replace('_', '-').upper()
  453. self.ls_width = max(ls_width or 0, len(self.ls_head) + 1)
  454. def __get__(self, instance, owner):
  455. if instance is None:
  456. return self
  457. # XXX this violates duck typing, shall we keep it?
  458. if not isinstance(instance, PropertyHolder):
  459. raise AttributeError('qubes.property should be used on '
  460. 'qubes.PropertyHolder instances only')
  461. try:
  462. return getattr(instance, self._attr_name)
  463. except AttributeError:
  464. if self._default is self._NO_DEFAULT:
  465. raise AttributeError(
  466. 'property {!r} not set'.format(self.__name__))
  467. elif isinstance(self._default, collections.Callable):
  468. return self._default(instance)
  469. else:
  470. return self._default
  471. def __set__(self, instance, value):
  472. self._enforce_write_once(instance)
  473. if value is self.__class__.DEFAULT:
  474. self.__delete__(instance)
  475. return
  476. try:
  477. oldvalue = getattr(instance, self.__name__)
  478. has_oldvalue = True
  479. except AttributeError:
  480. has_oldvalue = False
  481. if self._setter is not None:
  482. value = self._setter(instance, self, value)
  483. if self._type not in (None, type(value)):
  484. value = self._type(value)
  485. if has_oldvalue:
  486. instance.fire_event_pre('property-pre-set:' + self.__name__,
  487. self.__name__, value, oldvalue)
  488. else:
  489. instance.fire_event_pre('property-pre-set:' + self.__name__,
  490. self.__name__, value)
  491. instance._property_init(self, value) # pylint: disable=protected-access
  492. if has_oldvalue:
  493. instance.fire_event('property-set:' + self.__name__, self.__name__,
  494. value, oldvalue)
  495. else:
  496. instance.fire_event('property-set:' + self.__name__, self.__name__,
  497. value)
  498. def __delete__(self, instance):
  499. self._enforce_write_once(instance)
  500. try:
  501. oldvalue = getattr(instance, self.__name__)
  502. has_oldvalue = True
  503. except AttributeError:
  504. has_oldvalue = False
  505. if has_oldvalue:
  506. instance.fire_event_pre('property-pre-deleted:' + self.__name__,
  507. self.__name__, oldvalue)
  508. else:
  509. instance.fire_event_pre('property-pre-deleted:' + self.__name__,
  510. self.__name__)
  511. delattr(instance, self._attr_name)
  512. if has_oldvalue:
  513. instance.fire_event('property-deleted:' + self.__name__,
  514. self.__name__, oldvalue)
  515. else:
  516. instance.fire_event('property-deleted:' + self.__name__,
  517. self.__name__)
  518. def __repr__(self):
  519. default = ' default={!r}'.format(self._default) \
  520. if self._default is not self._NO_DEFAULT \
  521. else ''
  522. return '<{} object at {:#x} name={!r}{}>'.format(
  523. self.__class__.__name__, id(self), self.__name__, default) \
  524. def __hash__(self):
  525. return hash(self.__name__)
  526. def __eq__(self, other):
  527. return self.__name__ == other.__name__
  528. def _enforce_write_once(self, instance):
  529. if self._write_once and not instance.property_is_default(self):
  530. raise AttributeError(
  531. 'property {!r} is write-once and already set'.format(
  532. self.__name__))
  533. #
  534. # exceptions
  535. #
  536. class DontSave(Exception):
  537. '''This exception may be raised from saver to sign that property should
  538. not be saved.
  539. '''
  540. pass
  541. @staticmethod
  542. def dontsave(self, prop, value):
  543. '''Dummy saver that never saves anything.'''
  544. # pylint: disable=bad-staticmethod-argument,unused-argument
  545. raise property.DontSave()
  546. #
  547. # some setters provided
  548. #
  549. @staticmethod
  550. def forbidden(self, prop, value):
  551. '''Property setter that forbids loading a property.
  552. This is used to effectively disable property in classes which inherit
  553. unwanted property. When someone attempts to load such a property, it
  554. :throws AttributeError: always
  555. ''' # pylint: disable=bad-staticmethod-argument,unused-argument
  556. raise AttributeError(
  557. 'setting {} property on {} instance is forbidden'.format(
  558. prop.__name__, self.__class__.__name__))
  559. @staticmethod
  560. def bool(self, prop, value):
  561. '''Property setter for boolean properties.
  562. It accepts (case-insensitive) ``'0'``, ``'no'`` and ``false`` as
  563. :py:obj:`False` and ``'1'``, ``'yes'`` and ``'true'`` as
  564. :py:obj:`True`.
  565. ''' # pylint: disable=bad-staticmethod-argument,unused-argument
  566. if isinstance(value, basestring):
  567. lcvalue = value.lower()
  568. if lcvalue in ('0', 'no', 'false', 'off'):
  569. return False
  570. if lcvalue in ('1', 'yes', 'true', 'on'):
  571. return True
  572. raise ValueError(
  573. 'Invalid literal for boolean property: {!r}'.format(value))
  574. return bool(value)
  575. class PropertyHolder(qubes.events.Emitter):
  576. '''Abstract class for holding :py:class:`qubes.property`
  577. Events fired by instances of this class:
  578. .. event:: property-load (subject, event)
  579. Fired once after all properties are loaded from XML. Individual
  580. ``property-set`` events are not fired.
  581. .. event:: property-set:<propname> \
  582. (subject, event, name, newvalue[, oldvalue])
  583. Fired when property changes state. Signature is variable,
  584. *oldvalue* is present only if there was an old value.
  585. :param name: Property name
  586. :param newvalue: New value of the property
  587. :param oldvalue: Old value of the property
  588. .. event:: property-pre-set:<propname> \
  589. (subject, event, name, newvalue[, oldvalue])
  590. Fired before property changes state. Signature is variable,
  591. *oldvalue* is present only if there was an old value.
  592. :param name: Property name
  593. :param newvalue: New value of the property
  594. :param oldvalue: Old value of the property
  595. .. event:: property-del:<propname> \
  596. (subject, event, name[, oldvalue])
  597. Fired when property gets deleted (is set to default). Signature is
  598. variable, *oldvalue* is present only if there was an old value.
  599. :param name: Property name
  600. :param oldvalue: Old value of the property
  601. .. event:: property-pre-del:<propname> \
  602. (subject, event, name[, oldvalue])
  603. Fired before property gets deleted (is set to default). Signature
  604. is variable, *oldvalue* is present only if there was an old value.
  605. :param name: Property name
  606. :param oldvalue: Old value of the property
  607. Members:
  608. '''
  609. def __init__(self, xml, **kwargs):
  610. self.xml = xml
  611. propvalues = {}
  612. all_names = set(prop.__name__ for prop in self.property_list())
  613. for key in list(kwargs.keys()):
  614. if not key in all_names:
  615. continue
  616. propvalues[key] = kwargs.pop(key)
  617. super(PropertyHolder, self).__init__(**kwargs)
  618. for key, value in propvalues.items():
  619. setattr(self, key, value)
  620. @classmethod
  621. def property_list(cls, load_stage=None):
  622. '''List all properties attached to this VM's class
  623. :param load_stage: Filter by load stage
  624. :type load_stage: :py:func:`int` or :py:obj:`None`
  625. '''
  626. props = set()
  627. for class_ in cls.__mro__:
  628. props.update(prop for prop in class_.__dict__.values()
  629. if isinstance(prop, property))
  630. if load_stage is not None:
  631. props = set(prop for prop in props
  632. if prop.load_stage == load_stage)
  633. return sorted(props,
  634. key=lambda prop: (prop.load_stage, prop.order, prop.__name__))
  635. def _property_init(self, prop, value):
  636. '''Initialise property to a given value, without side effects.
  637. :param qubes.property prop: property object of particular interest
  638. :param value: value
  639. '''
  640. # pylint: disable=protected-access
  641. setattr(self, self.property_get_def(prop)._attr_name, value)
  642. def property_is_default(self, prop):
  643. '''Check whether property is in it's default value.
  644. Properties when unset may return some default value, so
  645. ``hasattr(vm, prop.__name__)`` is wrong in some circumstances. This
  646. method allows for checking if the value returned is in fact it's
  647. default value.
  648. :param qubes.property prop: property object of particular interest
  649. :rtype: bool
  650. ''' # pylint: disable=protected-access
  651. # both property_get_def() and ._attr_name may throw AttributeError,
  652. # which we don't want to catch
  653. attrname = self.property_get_def(prop)._attr_name
  654. return not hasattr(self, attrname)
  655. @classmethod
  656. def property_get_def(cls, prop):
  657. '''Return property definition object.
  658. If prop is already :py:class:`qubes.property` instance, return the same
  659. object.
  660. :param prop: property object or name
  661. :type prop: qubes.property or str
  662. :rtype: qubes.property
  663. '''
  664. if isinstance(prop, qubes.property):
  665. return prop
  666. for p in cls.property_list():
  667. if p.__name__ == prop:
  668. return p
  669. raise AttributeError('No property {!r} found in {!r}'.format(
  670. prop, cls))
  671. def load_properties(self, load_stage=None):
  672. '''Load properties from immediate children of XML node.
  673. ``property-set`` events are not fired for each individual property.
  674. :param int load_stage: Stage of loading.
  675. '''
  676. if self.xml is None:
  677. return
  678. all_names = set(
  679. prop.__name__ for prop in self.property_list(load_stage))
  680. for node in self.xml.xpath('./properties/property'):
  681. name = node.get('name')
  682. value = node.get('ref') or node.text
  683. if not name in all_names:
  684. continue
  685. setattr(self, name, value)
  686. def xml_properties(self, with_defaults=False):
  687. '''Iterator that yields XML nodes representing set properties.
  688. :param bool with_defaults: If :py:obj:`True`, then it also includes \
  689. properties which were not set explicite, but have default values \
  690. filled.
  691. '''
  692. properties = lxml.etree.Element('properties')
  693. for prop in self.property_list():
  694. # pylint: disable=protected-access
  695. try:
  696. value = getattr(
  697. self, (prop.__name__ if with_defaults else prop._attr_name))
  698. except AttributeError:
  699. continue
  700. try:
  701. value = prop._saver(self, prop, value)
  702. except property.DontSave:
  703. continue
  704. element = lxml.etree.Element('property', name=prop.__name__)
  705. if prop.save_via_ref:
  706. element.set('ref', value)
  707. else:
  708. element.text = value
  709. properties.append(element)
  710. return properties
  711. # this was clone_attrs
  712. def clone_properties(self, src, proplist=None):
  713. '''Clone properties from other object.
  714. :param PropertyHolder src: source object
  715. :param list proplist: list of properties \
  716. (:py:obj:`None` for all properties)
  717. '''
  718. if proplist is None:
  719. proplist = self.property_list()
  720. else:
  721. proplist = [prop for prop in self.property_list()
  722. if prop.__name__ in proplist or prop in proplist]
  723. for prop in self.property_list():
  724. try:
  725. # pylint: disable=protected-access
  726. self._property_init(prop, getattr(src, prop._attr_name))
  727. except AttributeError:
  728. continue
  729. self.fire_event('cloned-properties', src, proplist)
  730. def property_require(self, prop, allow_none=False, hard=False):
  731. '''Complain badly when property is not set.
  732. :param prop: property name or object
  733. :type prop: qubes.property or str
  734. :param bool allow_none: if :py:obj:`True`, don't complain if \
  735. :py:obj:`None` is found
  736. :param bool hard: if :py:obj:`True`, raise :py:class:`AssertionError`; \
  737. if :py:obj:`False`, log warning instead
  738. '''
  739. if isinstance(prop, qubes.property):
  740. prop = prop.__name__
  741. try:
  742. value = getattr(self, prop)
  743. if value is None and not allow_none:
  744. raise AttributeError()
  745. except AttributeError:
  746. # pylint: disable=no-member
  747. msg = 'Required property {!r} not set on {!r}'.format(prop, self)
  748. if hard:
  749. raise AssertionError(msg)
  750. else:
  751. # pylint: disable=no-member
  752. self.log.fatal(msg)
  753. import qubes.vm
  754. class VMProperty(property):
  755. '''Property that is referring to a VM
  756. :param type vmclass: class that returned VM is supposed to be instance of
  757. and all supported by :py:class:`property` with the exception of ``type`` \
  758. and ``setter``
  759. '''
  760. def __init__(self, name, vmclass=qubes.vm.BaseVM, allow_none=False,
  761. **kwargs):
  762. if 'type' in kwargs:
  763. raise TypeError(
  764. "'type' keyword parameter is unsupported in {}".format(
  765. self.__class__.__name__))
  766. if 'setter' in kwargs:
  767. raise TypeError(
  768. "'setter' keyword parameter is unsupported in {}".format(
  769. self.__class__.__name__))
  770. if not issubclass(vmclass, qubes.vm.BaseVM):
  771. raise TypeError(
  772. "'vmclass' should specify a subclass of qubes.vm.BaseVM")
  773. super(VMProperty, self).__init__(name,
  774. saver=(lambda self, prop, value: value.name if value else 'None'),
  775. **kwargs)
  776. self.vmclass = vmclass
  777. self.allow_none = allow_none
  778. def __set__(self, instance, value):
  779. if value is None:
  780. if self.allow_none:
  781. super(VMProperty, self).__set__(instance, value)
  782. return
  783. else:
  784. raise ValueError(
  785. 'Property {!r} does not allow setting to {!r}'.format(
  786. self.__name__, value))
  787. app = instance if isinstance(instance, Qubes) else instance.app
  788. # XXX this may throw LookupError; that's good until introduction
  789. # of QubesNoSuchVMException or whatever
  790. vm = app.domains[value]
  791. if not isinstance(vm, self.vmclass):
  792. raise TypeError('wrong VM class: domains[{!r}] if of type {!s} '
  793. 'and not {!s}'.format(value,
  794. vm.__class__.__name__,
  795. self.vmclass.__name__))
  796. super(VMProperty, self).__set__(instance, vm)
  797. import qubes.vm.qubesvm
  798. import qubes.vm.templatevm
  799. class Qubes(PropertyHolder):
  800. '''Main Qubes application
  801. :param str store: path to ``qubes.xml``
  802. The store is loaded in stages:
  803. 1. In the first stage there are loaded some basic features from store
  804. (currently labels).
  805. 2. In the second stage stubs for all VMs are loaded. They are filled
  806. with their basic properties, like ``qid`` and ``name``.
  807. 3. In the third stage all global properties are loaded. They often
  808. reference VMs, like default netvm, so they should be filled after
  809. loading VMs.
  810. 4. In the fourth stage all remaining VM properties are loaded. They
  811. also need all VMs loaded, because they represent dependencies
  812. between VMs like aforementioned netvm.
  813. 5. In the fifth stage there are some fixups to ensure sane system
  814. operation.
  815. This class emits following events:
  816. .. event:: domain-added (subject, event, vm)
  817. When domain is added.
  818. :param subject: Event emitter
  819. :param event: Event name (``'domain-added'``)
  820. :param vm: Domain object
  821. .. event:: domain-deleted (subject, event, vm)
  822. When domain is deleted. VM still has reference to ``app`` object,
  823. but is not contained within VMCollection.
  824. :param subject: Event emitter
  825. :param event: Event name (``'domain-deleted'``)
  826. :param vm: Domain object
  827. Methods and attributes:
  828. '''
  829. default_netvm = VMProperty('default_netvm', load_stage=3,
  830. default=None, allow_none=True,
  831. doc='''Default NetVM for AppVMs. Initial state is `None`, which means
  832. that AppVMs are not connected to the Internet.''')
  833. default_fw_netvm = VMProperty('default_fw_netvm', load_stage=3,
  834. default=None, allow_none=True,
  835. doc='''Default NetVM for ProxyVMs. Initial state is `None`, which means
  836. that ProxyVMs (including FirewallVM) are not connected to the
  837. Internet.''')
  838. default_template = VMProperty('default_template', load_stage=3,
  839. vmclass=qubes.vm.templatevm.TemplateVM,
  840. doc='Default template for new AppVMs')
  841. updatevm = VMProperty('updatevm', load_stage=3,
  842. allow_none=True,
  843. doc='''Which VM to use as `yum` proxy for updating AdminVM and
  844. TemplateVMs''')
  845. clockvm = VMProperty('clockvm', load_stage=3,
  846. allow_none=True,
  847. doc='Which VM to use as NTP proxy for updating AdminVM')
  848. default_kernel = property('default_kernel', load_stage=3,
  849. doc='Which kernel to use when not overriden in VM')
  850. def __init__(self, store=None, load=True, **kwargs):
  851. #: logger instance for logging global messages
  852. self.log = logging.getLogger('app')
  853. # pylint: disable=no-member
  854. self._extensions = set(
  855. ext(self) for ext in qubes.ext.Extension.register.values())
  856. #: collection of all VMs managed by this Qubes instance
  857. self.domains = VMCollection(self)
  858. #: collection of all available labels for VMs
  859. self.labels = {}
  860. #: Connection to VMM
  861. self.vmm = VMMConnection()
  862. #: Information about host system
  863. self.host = QubesHost(self)
  864. self._store = store if store is not None else os.path.join(
  865. qubes.config.system_path['qubes_base_dir'],
  866. qubes.config.system_path['qubes_store_filename'])
  867. super(Qubes, self).__init__(xml=None, **kwargs)
  868. if load:
  869. self.load()
  870. self.events_enabled = True
  871. def load(self):
  872. '''Open qubes.xml
  873. :throws EnvironmentError: failure on parsing store
  874. :throws xml.parsers.expat.ExpatError: failure on parsing store
  875. :raises lxml.etree.XMLSyntaxError: on syntax error in qubes.xml
  876. '''
  877. fd = os.open(self._store, os.O_RDWR) # no O_CREAT
  878. fh = os.fdopen(fd, 'rb')
  879. if os.name == 'posix':
  880. fcntl.lockf(fh, fcntl.LOCK_EX)
  881. elif os.name == 'nt':
  882. # pylint: disable=protected-access
  883. win32file.LockFileEx(
  884. win32file._get_osfhandle(fh.fileno()),
  885. win32con.LOCKFILE_EXCLUSIVE_LOCK,
  886. 0, -0x10000,
  887. pywintypes.OVERLAPPED())
  888. self.xml = lxml.etree.parse(fh)
  889. # stage 1: load labels
  890. for node in self.xml.xpath('./labels/label'):
  891. label = Label.fromxml(node)
  892. self.labels[label.index] = label
  893. # stage 2: load VMs
  894. for node in self.xml.xpath('./domains/domain'):
  895. # pylint: disable=no-member
  896. cls = qubes.vm.BaseVM.register[node.get('class')]
  897. vm = cls(self, node)
  898. vm.load_properties(load_stage=2)
  899. vm.init_log()
  900. self.domains.add(vm)
  901. if not 0 in self.domains:
  902. self.domains.add(qubes.vm.adminvm.AdminVM(
  903. self, None, qid=0, name='dom0'))
  904. # stage 3: load global properties
  905. self.load_properties(load_stage=3)
  906. # stage 4: fill all remaining VM properties
  907. for vm in self.domains:
  908. vm.load_properties(load_stage=4)
  909. # stage 5: misc fixups
  910. self.property_require('default_fw_netvm', allow_none=True)
  911. self.property_require('default_netvm', allow_none=True)
  912. self.property_require('default_template')
  913. self.property_require('clockvm')
  914. self.property_require('updatevm')
  915. # Disable ntpd in ClockVM - to not conflict with ntpdate (both are
  916. # using 123/udp port)
  917. if hasattr(self, 'clockvm') and self.clockvm is not None:
  918. if 'ntpd' in self.clockvm.services:
  919. if self.clockvm.services['ntpd']:
  920. self.log.warning("VM set as clockvm ({!r}) has enabled "
  921. "'ntpd' service! Expect failure when syncing time in "
  922. "dom0.".format(self.clockvm))
  923. else:
  924. self.clockvm.services['ntpd'] = False
  925. for vm in self.domains:
  926. vm.events_enabled = True
  927. vm.fire_event('domain-loaded')
  928. # intentionally do not call explicit unlock
  929. fh.close()
  930. del fh
  931. def __xml__(self):
  932. element = lxml.etree.Element('qubes')
  933. element.append(self.xml_labels())
  934. element.append(self.xml_properties())
  935. domains = lxml.etree.Element('domains')
  936. for vm in self.domains:
  937. domains.append(vm.__xml__())
  938. element.append(domains)
  939. return element
  940. def save(self):
  941. '''Save all data to qubes.xml
  942. There are several problems with saving :file:`qubes.xml` which must be
  943. mitigated:
  944. - Running out of disk space. No space left should not result in empty
  945. file. This is done by writing to temporary file and the renaming.
  946. - Attempts to write two or more files concurrently. This is done by
  947. sophisticated locking.
  948. :throws EnvironmentError: failure on saving
  949. '''
  950. while True:
  951. fd_old = os.open(self._store, os.O_RDWR | os.O_CREAT)
  952. if os.name == 'posix':
  953. fcntl.lockf(fd_old, fcntl.LOCK_EX)
  954. elif os.name == 'nt':
  955. # pylint: disable=protected-access
  956. overlapped = pywintypes.OVERLAPPED()
  957. win32file.LockFileEx(
  958. win32file._get_osfhandle(fd_old),
  959. win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)
  960. # While we were waiting for lock, someone could have unlink()ed (or
  961. # rename()d) our file out of the filesystem. We have to ensure we
  962. # got lock on something linked to filesystem. If not, try again.
  963. if os.fstat(fd_old) == os.stat(self._store):
  964. break
  965. else:
  966. os.close(fd_old)
  967. fh_new = tempfile.NamedTemporaryFile(prefix=self._store, delete=False)
  968. lxml.etree.ElementTree(self.__xml__()).write(
  969. fh_new, encoding='utf-8', pretty_print=True)
  970. fh_new.flush()
  971. os.chmod(fh_new.name, 0660)
  972. os.chown(fh_new.name, -1, grp.getgrnam('qubes').gr_gid)
  973. os.rename(fh_new.name, self._store)
  974. # intentionally do not call explicit unlock to not unlock the file
  975. # before all buffers are flushed
  976. fh_new.close()
  977. os.close(fd_old)
  978. @classmethod
  979. def create_empty_store(cls, *args, **kwargs):
  980. self = cls(*args, load=False, **kwargs)
  981. self.labels = {
  982. 1: Label(1, '0xcc0000', 'red'),
  983. 2: Label(2, '0xf57900', 'orange'),
  984. 3: Label(3, '0xedd400', 'yellow'),
  985. 4: Label(4, '0x73d216', 'green'),
  986. 5: Label(5, '0x555753', 'gray'),
  987. 6: Label(6, '0x3465a4', 'blue'),
  988. 7: Label(7, '0x75507b', 'purple'),
  989. 8: Label(8, '0x000000', 'black'),
  990. }
  991. self.domains.add(
  992. qubes.vm.adminvm.AdminVM(self, None, qid=0, name='dom0'))
  993. self.save()
  994. return self
  995. def xml_labels(self):
  996. '''Serialise labels
  997. :rtype: lxml.etree._Element
  998. '''
  999. labels = lxml.etree.Element('labels')
  1000. for label in sorted(self.labels.values(), key=lambda labl: labl.index):
  1001. labels.append(label.__xml__())
  1002. return labels
  1003. def add_new_vm(self, cls, qid=None, **kwargs):
  1004. '''Add new Virtual Machine to colletion
  1005. '''
  1006. if qid is None:
  1007. qid = self.domains.get_new_unused_qid()
  1008. return self.domains.add(cls(self, None, qid=qid, **kwargs))
  1009. def get_label(self, label):
  1010. '''Get label as identified by index or name
  1011. :throws KeyError: when label is not found
  1012. '''
  1013. # first search for index, verbatim
  1014. try:
  1015. return self.labels[label]
  1016. except KeyError:
  1017. pass
  1018. # then search for name
  1019. for i in self.labels.values():
  1020. if i.name == label:
  1021. return i
  1022. # last call, if label is a number represented as str, search in indices
  1023. try:
  1024. return self.labels[int(label)]
  1025. except (KeyError, ValueError):
  1026. pass
  1027. raise KeyError(label)
  1028. @qubes.events.handler('domain-pre-deleted')
  1029. def on_domain_pre_deleted(self, event, vm):
  1030. # pylint: disable=unused-argument
  1031. if isinstance(vm, qubes.vm.templatevm.TemplateVM):
  1032. appvms = self.domains.get_vms_based_on(vm)
  1033. if appvms:
  1034. raise qubes.exc.QubesException(
  1035. 'Cannot remove template that has dependent AppVMs. '
  1036. 'Affected are: {}'.format(', '.join(
  1037. vm.name for name in sorted(appvms))))
  1038. @qubes.events.handler('domain-deleted')
  1039. def on_domain_deleted(self, event, vm):
  1040. # pylint: disable=unused-argument
  1041. for propname in (
  1042. 'default_netvm',
  1043. 'default_fw_netvm',
  1044. 'clockvm',
  1045. 'updatevm',
  1046. 'default_template',
  1047. ):
  1048. try:
  1049. if getattr(self, propname) == vm:
  1050. delattr(self, propname)
  1051. except AttributeError:
  1052. pass
  1053. @qubes.events.handler('property-pre-set:clockvm')
  1054. def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):
  1055. # pylint: disable=unused-argument,no-self-use
  1056. if newvalue is None:
  1057. return
  1058. if 'ntpd' in newvalue.services:
  1059. if newvalue.services['ntpd']:
  1060. raise qubes.exc.QubesVMError(newvalue,
  1061. 'Cannot set {!r} as {!r} since it has ntpd enabled.'.format(
  1062. newvalue.name, name))
  1063. else:
  1064. newvalue.services['ntpd'] = False
  1065. @qubes.events.handler(
  1066. 'property-pre-set:default_netvm',
  1067. 'property-pre-set:default_fw_netvm')
  1068. def on_property_pre_set_default_netvm(self, event, name, newvalue,
  1069. oldvalue=None):
  1070. # pylint: disable=unused-argument,invalid-name
  1071. if newvalue is not None and oldvalue is not None \
  1072. and oldvalue.is_running() and not newvalue.is_running() \
  1073. and self.domains.get_vms_connected_to(oldvalue):
  1074. raise qubes.exc.QubesVMNotRunningError(newvalue,
  1075. 'Cannot change {!r} to domain that '
  1076. 'is not running ({!r}).'.format(name, newvalue.name))
  1077. @qubes.events.handler('property-set:default_fw_netvm')
  1078. def on_property_set_default_fw_netvm(self, event, name, newvalue,
  1079. oldvalue=None):
  1080. # pylint: disable=unused-argument,invalid-name
  1081. for vm in self.domains:
  1082. if not vm.provides_network and vm.property_is_default('netvm'):
  1083. # fire property-del:netvm as it is responsible for resetting
  1084. # netvm to it's default value
  1085. vm.fire_event('property-del:netvm', 'netvm', newvalue, oldvalue)
  1086. @qubes.events.handler('property-set:default_netvm')
  1087. def on_property_set_default_netvm(self, event, name, newvalue,
  1088. oldvalue=None):
  1089. # pylint: disable=unused-argument
  1090. for vm in self.domains:
  1091. if vm.provides_network and vm.property_is_default('netvm'):
  1092. # fire property-del:netvm as it is responsible for resetting
  1093. # netvm to it's default value
  1094. vm.fire_event('property-del:netvm', 'netvm', newvalue, oldvalue)
  1095. # load plugins
  1096. import qubes._pluginloader