admin.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. '''
  20. Qubes OS Management API
  21. '''
  22. import asyncio
  23. import functools
  24. import itertools
  25. import os
  26. import string
  27. import subprocess
  28. import libvirt
  29. import pkg_resources
  30. import yaml
  31. import qubes.api
  32. import qubes.backup
  33. import qubes.config
  34. import qubes.devices
  35. import qubes.firewall
  36. import qubes.storage
  37. import qubes.utils
  38. import qubes.vm
  39. import qubes.vm.adminvm
  40. import qubes.vm.qubesvm
  41. class QubesMgmtEventsDispatcher(object):
  42. def __init__(self, filters, send_event):
  43. self.filters = filters
  44. self.send_event = send_event
  45. def vm_handler(self, subject, event, **kwargs):
  46. # do not send internal events
  47. if event.startswith('admin-permission:'):
  48. return
  49. if event.startswith('device-get:'):
  50. return
  51. if event.startswith('device-list:'):
  52. return
  53. if event.startswith('device-list-attached:'):
  54. return
  55. if event in ('domain-is-fully-usable',):
  56. return
  57. if not list(qubes.api.apply_filters([(subject, event, kwargs)],
  58. self.filters)):
  59. return
  60. self.send_event(subject, event, **kwargs)
  61. def app_handler(self, subject, event, **kwargs):
  62. if not list(qubes.api.apply_filters([(subject, event, kwargs)],
  63. self.filters)):
  64. return
  65. self.send_event(subject, event, **kwargs)
  66. def on_domain_add(self, subject, event, vm):
  67. # pylint: disable=unused-argument
  68. vm.add_handler('*', self.vm_handler)
  69. def on_domain_delete(self, subject, event, vm):
  70. # pylint: disable=unused-argument
  71. vm.remove_handler('*', self.vm_handler)
  72. class QubesAdminAPI(qubes.api.AbstractQubesAPI):
  73. '''Implementation of Qubes Management API calls
  74. This class contains all the methods available in the main API.
  75. .. seealso::
  76. https://www.qubes-os.org/doc/mgmt1/
  77. '''
  78. SOCKNAME = '/var/run/qubesd.sock'
  79. @qubes.api.method('admin.vmclass.List', no_payload=True,
  80. scope='global', read=True)
  81. @asyncio.coroutine
  82. def vmclass_list(self):
  83. '''List all VM classes'''
  84. assert not self.arg
  85. assert self.dest.name == 'dom0'
  86. entrypoints = self.fire_event_for_filter(
  87. pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT))
  88. return ''.join('{}\n'.format(ep.name)
  89. for ep in entrypoints)
  90. @qubes.api.method('admin.vm.List', no_payload=True,
  91. scope='global', read=True)
  92. @asyncio.coroutine
  93. def vm_list(self):
  94. '''List all the domains'''
  95. assert not self.arg
  96. if self.dest.name == 'dom0':
  97. domains = self.fire_event_for_filter(self.app.domains)
  98. else:
  99. domains = self.fire_event_for_filter([self.dest])
  100. return ''.join('{} class={} state={}\n'.format(
  101. vm.name,
  102. vm.__class__.__name__,
  103. vm.get_power_state())
  104. for vm in sorted(domains))
  105. @qubes.api.method('admin.vm.property.List', no_payload=True,
  106. scope='local', read=True)
  107. @asyncio.coroutine
  108. def vm_property_list(self):
  109. '''List all properties on a qube'''
  110. return self._property_list(self.dest)
  111. @qubes.api.method('admin.property.List', no_payload=True,
  112. scope='global', read=True)
  113. @asyncio.coroutine
  114. def property_list(self):
  115. '''List all global properties'''
  116. assert self.dest.name == 'dom0'
  117. return self._property_list(self.app)
  118. def _property_list(self, dest):
  119. assert not self.arg
  120. properties = self.fire_event_for_filter(dest.property_list())
  121. return ''.join('{}\n'.format(prop.__name__) for prop in properties)
  122. @qubes.api.method('admin.vm.property.Get', no_payload=True,
  123. scope='local', read=True)
  124. @asyncio.coroutine
  125. def vm_property_get(self):
  126. '''Get a value of one property'''
  127. return self._property_get(self.dest)
  128. @qubes.api.method('admin.property.Get', no_payload=True,
  129. scope='global', read=True)
  130. @asyncio.coroutine
  131. def property_get(self):
  132. '''Get a value of one global property'''
  133. assert self.dest.name == 'dom0'
  134. return self._property_get(self.app)
  135. def _property_get(self, dest):
  136. if self.arg not in dest.property_list():
  137. raise qubes.exc.QubesNoSuchPropertyError(dest, self.arg)
  138. self.fire_event_for_permission()
  139. property_def = dest.property_get_def(self.arg)
  140. # explicit list to be sure that it matches protocol spec
  141. if isinstance(property_def, qubes.vm.VMProperty):
  142. property_type = 'vm'
  143. elif property_def.type is int:
  144. property_type = 'int'
  145. elif property_def.type is bool:
  146. property_type = 'bool'
  147. elif self.arg == 'label':
  148. property_type = 'label'
  149. else:
  150. property_type = 'str'
  151. try:
  152. value = getattr(dest, self.arg)
  153. except AttributeError:
  154. return 'default=True type={} '.format(property_type)
  155. else:
  156. return 'default={} type={} {}'.format(
  157. str(dest.property_is_default(self.arg)),
  158. property_type,
  159. str(value) if value is not None else '')
  160. @qubes.api.method('admin.vm.property.GetDefault', no_payload=True,
  161. scope='local', read=True)
  162. @asyncio.coroutine
  163. def vm_property_get_default(self):
  164. '''Get a value of one property'''
  165. return self._property_get_default(self.dest)
  166. @qubes.api.method('admin.property.GetDefault', no_payload=True,
  167. scope='global', read=True)
  168. @asyncio.coroutine
  169. def property_get_default(self):
  170. '''Get a value of one global property'''
  171. assert self.dest.name == 'dom0'
  172. return self._property_get_default(self.app)
  173. def _property_get_default(self, dest):
  174. if self.arg not in dest.property_list():
  175. raise qubes.exc.QubesNoSuchPropertyError(dest, self.arg)
  176. self.fire_event_for_permission()
  177. property_def = dest.property_get_def(self.arg)
  178. # explicit list to be sure that it matches protocol spec
  179. if isinstance(property_def, qubes.vm.VMProperty):
  180. property_type = 'vm'
  181. elif property_def.type is int:
  182. property_type = 'int'
  183. elif property_def.type is bool:
  184. property_type = 'bool'
  185. elif self.arg == 'label':
  186. property_type = 'label'
  187. else:
  188. property_type = 'str'
  189. try:
  190. value = property_def.get_default(dest)
  191. except AttributeError:
  192. return None
  193. else:
  194. return 'type={} {}'.format(
  195. property_type,
  196. str(value) if value is not None else '')
  197. @qubes.api.method('admin.vm.property.Set',
  198. scope='local', write=True)
  199. @asyncio.coroutine
  200. def vm_property_set(self, untrusted_payload):
  201. '''Set property value'''
  202. return self._property_set(self.dest,
  203. untrusted_payload=untrusted_payload)
  204. @qubes.api.method('admin.property.Set',
  205. scope='global', write=True)
  206. @asyncio.coroutine
  207. def property_set(self, untrusted_payload):
  208. '''Set property value'''
  209. assert self.dest.name == 'dom0'
  210. return self._property_set(self.app,
  211. untrusted_payload=untrusted_payload)
  212. def _property_set(self, dest, untrusted_payload):
  213. if self.arg not in dest.property_list():
  214. raise qubes.exc.QubesNoSuchPropertyError(dest, self.arg)
  215. property_def = dest.property_get_def(self.arg)
  216. newvalue = property_def.sanitize(untrusted_newvalue=untrusted_payload)
  217. self.fire_event_for_permission(newvalue=newvalue)
  218. setattr(dest, self.arg, newvalue)
  219. self.app.save()
  220. @qubes.api.method('admin.vm.property.Help', no_payload=True,
  221. scope='local', read=True)
  222. @asyncio.coroutine
  223. def vm_property_help(self):
  224. '''Get help for one property'''
  225. return self._property_help(self.dest)
  226. @qubes.api.method('admin.property.Help', no_payload=True,
  227. scope='global', read=True)
  228. @asyncio.coroutine
  229. def property_help(self):
  230. '''Get help for one property'''
  231. assert self.dest.name == 'dom0'
  232. return self._property_help(self.app)
  233. def _property_help(self, dest):
  234. if self.arg not in dest.property_list():
  235. raise qubes.exc.QubesNoSuchPropertyError(dest, self.arg)
  236. self.fire_event_for_permission()
  237. try:
  238. doc = dest.property_get_def(self.arg).__doc__
  239. except AttributeError:
  240. return ''
  241. return qubes.utils.format_doc(doc)
  242. @qubes.api.method('admin.vm.property.Reset', no_payload=True,
  243. scope='local', write=True)
  244. @asyncio.coroutine
  245. def vm_property_reset(self):
  246. '''Reset a property to a default value'''
  247. return self._property_reset(self.dest)
  248. @qubes.api.method('admin.property.Reset', no_payload=True,
  249. scope='global', write=True)
  250. @asyncio.coroutine
  251. def property_reset(self):
  252. '''Reset a property to a default value'''
  253. assert self.dest.name == 'dom0'
  254. return self._property_reset(self.app)
  255. def _property_reset(self, dest):
  256. if self.arg not in dest.property_list():
  257. raise qubes.exc.QubesNoSuchPropertyError(dest, self.arg)
  258. self.fire_event_for_permission()
  259. delattr(dest, self.arg)
  260. self.app.save()
  261. @qubes.api.method('admin.vm.volume.List', no_payload=True,
  262. scope='local', read=True)
  263. @asyncio.coroutine
  264. def vm_volume_list(self):
  265. assert not self.arg
  266. volume_names = self.fire_event_for_filter(self.dest.volumes.keys())
  267. return ''.join('{}\n'.format(name) for name in volume_names)
  268. @qubes.api.method('admin.vm.volume.Info', no_payload=True,
  269. scope='local', read=True)
  270. @asyncio.coroutine
  271. def vm_volume_info(self):
  272. assert self.arg in self.dest.volumes.keys()
  273. self.fire_event_for_permission()
  274. volume = self.dest.volumes[self.arg]
  275. # properties defined in API
  276. volume_properties = [
  277. 'pool', 'vid', 'size', 'usage', 'rw', 'source',
  278. 'save_on_stop', 'snap_on_start', 'revisions_to_keep', 'is_outdated']
  279. def _serialize(value):
  280. if callable(value):
  281. value = value()
  282. if value is None:
  283. value = ''
  284. return str(value)
  285. return ''.join('{}={}\n'.format(key, _serialize(getattr(volume, key)))
  286. for key in volume_properties)
  287. @qubes.api.method('admin.vm.volume.ListSnapshots', no_payload=True,
  288. scope='local', read=True)
  289. @asyncio.coroutine
  290. def vm_volume_listsnapshots(self):
  291. assert self.arg in self.dest.volumes.keys()
  292. volume = self.dest.volumes[self.arg]
  293. id_to_timestamp = volume.revisions
  294. revisions = sorted(id_to_timestamp, key=id_to_timestamp.__getitem__)
  295. revisions = self.fire_event_for_filter(revisions)
  296. return ''.join('{}\n'.format(revision) for revision in revisions)
  297. @qubes.api.method('admin.vm.volume.Revert',
  298. scope='local', write=True)
  299. @asyncio.coroutine
  300. def vm_volume_revert(self, untrusted_payload):
  301. assert self.arg in self.dest.volumes.keys()
  302. untrusted_revision = untrusted_payload.decode('ascii').strip()
  303. del untrusted_payload
  304. volume = self.dest.volumes[self.arg]
  305. snapshots = volume.revisions
  306. assert untrusted_revision in snapshots
  307. revision = untrusted_revision
  308. self.fire_event_for_permission(volume=volume, revision=revision)
  309. ret = volume.revert(revision)
  310. if asyncio.iscoroutine(ret):
  311. yield from ret
  312. self.app.save()
  313. # write=True because this allow to clone VM - and most likely modify that
  314. # one - still having the same data
  315. @qubes.api.method('admin.vm.volume.CloneFrom', no_payload=True,
  316. scope='local', write=True)
  317. @asyncio.coroutine
  318. def vm_volume_clone_from(self):
  319. assert self.arg in self.dest.volumes.keys()
  320. volume = self.dest.volumes[self.arg]
  321. self.fire_event_for_permission(volume=volume)
  322. token = qubes.utils.random_string(32)
  323. # save token on self.app, as self is not persistent
  324. if not hasattr(self.app, 'api_admin_pending_clone'):
  325. self.app.api_admin_pending_clone = {}
  326. # don't handle collisions any better - if someone is so much out of
  327. # luck, can try again anyway
  328. assert token not in self.app.api_admin_pending_clone
  329. self.app.api_admin_pending_clone[token] = volume
  330. return token
  331. @qubes.api.method('admin.vm.volume.CloneTo',
  332. scope='local', write=True)
  333. @asyncio.coroutine
  334. def vm_volume_clone_to(self, untrusted_payload):
  335. assert self.arg in self.dest.volumes.keys()
  336. untrusted_token = untrusted_payload.decode('ascii').strip()
  337. del untrusted_payload
  338. assert untrusted_token in getattr(self.app,
  339. 'api_admin_pending_clone', {})
  340. token = untrusted_token
  341. del untrusted_token
  342. src_volume = self.app.api_admin_pending_clone[token]
  343. del self.app.api_admin_pending_clone[token]
  344. # make sure the volume still exists, but invalidate token anyway
  345. assert str(src_volume.pool) in self.app.pools
  346. assert src_volume in self.app.pools[str(src_volume.pool)].volumes
  347. dst_volume = self.dest.volumes[self.arg]
  348. self.fire_event_for_permission(src_volume=src_volume,
  349. dst_volume=dst_volume)
  350. op_retval = dst_volume.import_volume(src_volume)
  351. # clone/import functions may be either synchronous or asynchronous
  352. # in the later case, we need to wait for them to finish
  353. if asyncio.iscoroutine(op_retval):
  354. op_retval = yield from op_retval
  355. self.dest.volumes[self.arg] = op_retval
  356. self.app.save()
  357. @qubes.api.method('admin.vm.volume.Resize',
  358. scope='local', write=True)
  359. @asyncio.coroutine
  360. def vm_volume_resize(self, untrusted_payload):
  361. assert self.arg in self.dest.volumes.keys()
  362. untrusted_size = untrusted_payload.decode('ascii').strip()
  363. del untrusted_payload
  364. assert untrusted_size.isdigit() # only digits, forbid '-' too
  365. assert len(untrusted_size) <= 20 # limit to about 2^64
  366. size = int(untrusted_size)
  367. self.fire_event_for_permission(size=size)
  368. yield from self.dest.storage.resize(self.arg, size)
  369. self.app.save()
  370. @qubes.api.method('admin.vm.volume.Import', no_payload=True,
  371. scope='local', write=True)
  372. @asyncio.coroutine
  373. def vm_volume_import(self):
  374. '''Import volume data.
  375. Note that this function only returns a path to where data should be
  376. written, actual importing is done by a script in /etc/qubes-rpc
  377. When the script finish importing, it will trigger
  378. internal.vm.volume.ImportEnd (with either b'ok' or b'fail' as a
  379. payload) and response from that call will be actually send to the
  380. caller.
  381. '''
  382. assert self.arg in self.dest.volumes.keys()
  383. self.fire_event_for_permission()
  384. if not self.dest.is_halted():
  385. raise qubes.exc.QubesVMNotHaltedError(self.dest)
  386. path = self.dest.storage.import_data(self.arg)
  387. assert ' ' not in path
  388. size = self.dest.volumes[self.arg].size
  389. # when we know the action is allowed, inform extensions that it will
  390. # be performed
  391. self.dest.fire_event('domain-volume-import-begin', volume=self.arg)
  392. return '{} {}'.format(size, path)
  393. @qubes.api.method('admin.vm.volume.Set.revisions_to_keep',
  394. scope='local', write=True)
  395. @asyncio.coroutine
  396. def vm_volume_set_revisions_to_keep(self, untrusted_payload):
  397. assert self.arg in self.dest.volumes.keys()
  398. try:
  399. untrusted_value = int(untrusted_payload.decode('ascii'))
  400. except (UnicodeDecodeError, ValueError):
  401. raise qubes.api.ProtocolError('Invalid value')
  402. del untrusted_payload
  403. assert untrusted_value >= 0
  404. newvalue = untrusted_value
  405. del untrusted_value
  406. self.fire_event_for_permission(newvalue=newvalue)
  407. self.dest.volumes[self.arg].revisions_to_keep = newvalue
  408. self.app.save()
  409. @qubes.api.method('admin.vm.volume.Set.rw',
  410. scope='local', write=True)
  411. @asyncio.coroutine
  412. def vm_volume_set_rw(self, untrusted_payload):
  413. assert self.arg in self.dest.volumes.keys()
  414. try:
  415. newvalue = qubes.property.bool(None, None,
  416. untrusted_payload.decode('ascii'))
  417. except (UnicodeDecodeError, ValueError):
  418. raise qubes.api.ProtocolError('Invalid value')
  419. del untrusted_payload
  420. self.fire_event_for_permission(newvalue=newvalue)
  421. if not self.dest.is_halted():
  422. raise qubes.exc.QubesVMNotHaltedError(self.dest)
  423. self.dest.volumes[self.arg].rw = newvalue
  424. self.app.save()
  425. @qubes.api.method('admin.vm.tag.List', no_payload=True,
  426. scope='local', read=True)
  427. @asyncio.coroutine
  428. def vm_tag_list(self):
  429. assert not self.arg
  430. tags = self.dest.tags
  431. tags = self.fire_event_for_filter(tags)
  432. return ''.join('{}\n'.format(tag) for tag in sorted(tags))
  433. @qubes.api.method('admin.vm.tag.Get', no_payload=True,
  434. scope='local', read=True)
  435. @asyncio.coroutine
  436. def vm_tag_get(self):
  437. qubes.vm.Tags.validate_tag(self.arg)
  438. self.fire_event_for_permission()
  439. return '1' if self.arg in self.dest.tags else '0'
  440. @qubes.api.method('admin.vm.tag.Set', no_payload=True,
  441. scope='local', write=True)
  442. @asyncio.coroutine
  443. def vm_tag_set(self):
  444. qubes.vm.Tags.validate_tag(self.arg)
  445. self.fire_event_for_permission()
  446. self.dest.tags.add(self.arg)
  447. self.app.save()
  448. @qubes.api.method('admin.vm.tag.Remove', no_payload=True,
  449. scope='local', write=True)
  450. @asyncio.coroutine
  451. def vm_tag_remove(self):
  452. qubes.vm.Tags.validate_tag(self.arg)
  453. self.fire_event_for_permission()
  454. try:
  455. self.dest.tags.remove(self.arg)
  456. except KeyError:
  457. raise qubes.exc.QubesTagNotFoundError(self.dest, self.arg)
  458. self.app.save()
  459. @qubes.api.method('admin.pool.List', no_payload=True,
  460. scope='global', read=True)
  461. @asyncio.coroutine
  462. def pool_list(self):
  463. assert not self.arg
  464. assert self.dest.name == 'dom0'
  465. pools = self.fire_event_for_filter(self.app.pools)
  466. return ''.join('{}\n'.format(pool) for pool in pools)
  467. @qubes.api.method('admin.pool.ListDrivers', no_payload=True,
  468. scope='global', read=True)
  469. @asyncio.coroutine
  470. def pool_listdrivers(self):
  471. assert self.dest.name == 'dom0'
  472. assert not self.arg
  473. drivers = self.fire_event_for_filter(qubes.storage.pool_drivers())
  474. return ''.join('{} {}\n'.format(
  475. driver,
  476. ' '.join(qubes.storage.driver_parameters(driver)))
  477. for driver in drivers)
  478. @qubes.api.method('admin.pool.Info', no_payload=True,
  479. scope='global', read=True)
  480. @asyncio.coroutine
  481. def pool_info(self):
  482. assert self.dest.name == 'dom0'
  483. assert self.arg in self.app.pools.keys()
  484. pool = self.app.pools[self.arg]
  485. self.fire_event_for_permission(pool=pool)
  486. size_info = ''
  487. try:
  488. size_info += 'size={}\n'.format(pool.size)
  489. except NotImplementedError:
  490. pass
  491. try:
  492. size_info += 'usage={}\n'.format(pool.usage)
  493. except NotImplementedError:
  494. pass
  495. return ''.join('{}={}\n'.format(prop, val)
  496. for prop, val in sorted(pool.config.items())) + \
  497. size_info
  498. @qubes.api.method('admin.pool.Add',
  499. scope='global', write=True)
  500. @asyncio.coroutine
  501. def pool_add(self, untrusted_payload):
  502. assert self.dest.name == 'dom0'
  503. drivers = qubes.storage.pool_drivers()
  504. assert self.arg in drivers
  505. untrusted_pool_config = untrusted_payload.decode('ascii').splitlines()
  506. del untrusted_payload
  507. assert all(('=' in line) for line in untrusted_pool_config)
  508. # pairs of (option, value)
  509. untrusted_pool_config = [line.split('=', 1)
  510. for line in untrusted_pool_config]
  511. # reject duplicated options
  512. assert len(set(x[0] for x in untrusted_pool_config)) == \
  513. len([x[0] for x in untrusted_pool_config])
  514. # and convert to dict
  515. untrusted_pool_config = dict(untrusted_pool_config)
  516. assert 'name' in untrusted_pool_config
  517. untrusted_pool_name = untrusted_pool_config.pop('name')
  518. allowed_chars = string.ascii_letters + string.digits + '-_.'
  519. assert all(c in allowed_chars for c in untrusted_pool_name)
  520. pool_name = untrusted_pool_name
  521. assert pool_name not in self.app.pools
  522. driver_parameters = qubes.storage.driver_parameters(self.arg)
  523. assert all(key in driver_parameters for key in untrusted_pool_config)
  524. pool_config = untrusted_pool_config
  525. self.fire_event_for_permission(name=pool_name,
  526. pool_config=pool_config)
  527. self.app.add_pool(name=pool_name, driver=self.arg, **pool_config)
  528. self.app.save()
  529. @qubes.api.method('admin.pool.Remove', no_payload=True,
  530. scope='global', write=True)
  531. @asyncio.coroutine
  532. def pool_remove(self):
  533. assert self.dest.name == 'dom0'
  534. assert self.arg in self.app.pools.keys()
  535. self.fire_event_for_permission()
  536. self.app.remove_pool(self.arg)
  537. self.app.save()
  538. @qubes.api.method('admin.pool.Set.revisions_to_keep',
  539. scope='global', write=True)
  540. @asyncio.coroutine
  541. def pool_set_revisions_to_keep(self, untrusted_payload):
  542. assert self.dest.name == 'dom0'
  543. assert self.arg in self.app.pools.keys()
  544. pool = self.app.pools[self.arg]
  545. try:
  546. untrusted_value = int(untrusted_payload.decode('ascii'))
  547. except (UnicodeDecodeError, ValueError):
  548. raise qubes.api.ProtocolError('Invalid value')
  549. del untrusted_payload
  550. assert untrusted_value >= 0
  551. newvalue = untrusted_value
  552. del untrusted_value
  553. self.fire_event_for_permission(newvalue=newvalue)
  554. pool.revisions_to_keep = newvalue
  555. self.app.save()
  556. @qubes.api.method('admin.label.List', no_payload=True,
  557. scope='global', read=True)
  558. @asyncio.coroutine
  559. def label_list(self):
  560. assert self.dest.name == 'dom0'
  561. assert not self.arg
  562. labels = self.fire_event_for_filter(self.app.labels.values())
  563. return ''.join('{}\n'.format(label.name) for label in labels)
  564. @qubes.api.method('admin.label.Get', no_payload=True,
  565. scope='global', read=True)
  566. @asyncio.coroutine
  567. def label_get(self):
  568. assert self.dest.name == 'dom0'
  569. try:
  570. label = self.app.get_label(self.arg)
  571. except KeyError:
  572. raise qubes.exc.QubesValueError
  573. self.fire_event_for_permission(label=label)
  574. return label.color
  575. @qubes.api.method('admin.label.Index', no_payload=True,
  576. scope='global', read=True)
  577. @asyncio.coroutine
  578. def label_index(self):
  579. assert self.dest.name == 'dom0'
  580. try:
  581. label = self.app.get_label(self.arg)
  582. except KeyError:
  583. raise qubes.exc.QubesValueError
  584. self.fire_event_for_permission(label=label)
  585. return str(label.index)
  586. @qubes.api.method('admin.label.Create',
  587. scope='global', write=True)
  588. @asyncio.coroutine
  589. def label_create(self, untrusted_payload):
  590. assert self.dest.name == 'dom0'
  591. # don't confuse label name with label index
  592. assert not self.arg.isdigit()
  593. allowed_chars = string.ascii_letters + string.digits + '-_.'
  594. assert all(c in allowed_chars for c in self.arg)
  595. try:
  596. self.app.get_label(self.arg)
  597. except KeyError:
  598. # ok, no such label yet
  599. pass
  600. else:
  601. raise qubes.exc.QubesValueError('label already exists')
  602. untrusted_payload = untrusted_payload.decode('ascii').strip()
  603. assert len(untrusted_payload) == 8
  604. assert untrusted_payload.startswith('0x')
  605. # besides prefix, only hex digits are allowed
  606. assert all(x in string.hexdigits for x in untrusted_payload[2:])
  607. # SEE: #2732
  608. color = untrusted_payload
  609. self.fire_event_for_permission(color=color)
  610. # allocate new index, but make sure it's outside of default labels set
  611. new_index = max(
  612. qubes.config.max_default_label, *self.app.labels.keys()) + 1
  613. label = qubes.Label(new_index, color, self.arg)
  614. self.app.labels[new_index] = label
  615. self.app.save()
  616. @qubes.api.method('admin.label.Remove', no_payload=True,
  617. scope='global', write=True)
  618. @asyncio.coroutine
  619. def label_remove(self):
  620. assert self.dest.name == 'dom0'
  621. try:
  622. label = self.app.get_label(self.arg)
  623. except KeyError:
  624. raise qubes.exc.QubesValueError
  625. # don't allow removing default labels
  626. assert label.index > qubes.config.max_default_label
  627. # FIXME: this should be in app.add_label()
  628. for vm in self.app.domains:
  629. if vm.label == label:
  630. raise qubes.exc.QubesException('label still in use')
  631. self.fire_event_for_permission(label=label)
  632. del self.app.labels[label.index]
  633. self.app.save()
  634. @qubes.api.method('admin.vm.Start', no_payload=True,
  635. scope='local', execute=True)
  636. @asyncio.coroutine
  637. def vm_start(self):
  638. assert not self.arg
  639. self.fire_event_for_permission()
  640. try:
  641. yield from self.dest.start()
  642. except libvirt.libvirtError as e:
  643. # change to QubesException, so will be reported to the user
  644. raise qubes.exc.QubesException('Start failed: ' + str(e) +
  645. ', see /var/log/libvirt/libxl/libxl-driver.log for details')
  646. @qubes.api.method('admin.vm.Shutdown', no_payload=True,
  647. scope='local', execute=True)
  648. @asyncio.coroutine
  649. def vm_shutdown(self):
  650. assert not self.arg
  651. self.fire_event_for_permission()
  652. yield from self.dest.shutdown()
  653. @qubes.api.method('admin.vm.Pause', no_payload=True,
  654. scope='local', execute=True)
  655. @asyncio.coroutine
  656. def vm_pause(self):
  657. assert not self.arg
  658. self.fire_event_for_permission()
  659. yield from self.dest.pause()
  660. @qubes.api.method('admin.vm.Unpause', no_payload=True,
  661. scope='local', execute=True)
  662. @asyncio.coroutine
  663. def vm_unpause(self):
  664. assert not self.arg
  665. self.fire_event_for_permission()
  666. yield from self.dest.unpause()
  667. @qubes.api.method('admin.vm.Kill', no_payload=True,
  668. scope='local', execute=True)
  669. @asyncio.coroutine
  670. def vm_kill(self):
  671. assert not self.arg
  672. self.fire_event_for_permission()
  673. yield from self.dest.kill()
  674. @qubes.api.method('admin.Events', no_payload=True,
  675. scope='global', read=True)
  676. @asyncio.coroutine
  677. def events(self):
  678. assert not self.arg
  679. # run until client connection is terminated
  680. self.cancellable = True
  681. wait_for_cancel = asyncio.get_event_loop().create_future()
  682. # cache event filters, to not call an event each time an event arrives
  683. event_filters = self.fire_event_for_permission()
  684. dispatcher = QubesMgmtEventsDispatcher(event_filters, self.send_event)
  685. if self.dest.name == 'dom0':
  686. self.app.add_handler('*', dispatcher.app_handler)
  687. self.app.add_handler('domain-add', dispatcher.on_domain_add)
  688. self.app.add_handler('domain-delete', dispatcher.on_domain_delete)
  689. for vm in self.app.domains:
  690. vm.add_handler('*', dispatcher.vm_handler)
  691. else:
  692. self.dest.add_handler('*', dispatcher.vm_handler)
  693. # send artificial event as a confirmation that connection is established
  694. self.send_event(self.app, 'connection-established')
  695. try:
  696. yield from wait_for_cancel
  697. except asyncio.CancelledError:
  698. # the above waiting was already interrupted, this is all we need
  699. pass
  700. if self.dest.name == 'dom0':
  701. self.app.remove_handler('*', dispatcher.app_handler)
  702. self.app.remove_handler('domain-add', dispatcher.on_domain_add)
  703. self.app.remove_handler('domain-delete',
  704. dispatcher.on_domain_delete)
  705. for vm in self.app.domains:
  706. vm.remove_handler('*', dispatcher.vm_handler)
  707. else:
  708. self.dest.remove_handler('*', dispatcher.vm_handler)
  709. @qubes.api.method('admin.vm.feature.List', no_payload=True,
  710. scope='local', read=True)
  711. @asyncio.coroutine
  712. def vm_feature_list(self):
  713. assert not self.arg
  714. features = self.fire_event_for_filter(self.dest.features.keys())
  715. return ''.join('{}\n'.format(feature) for feature in features)
  716. @qubes.api.method('admin.vm.feature.Get', no_payload=True,
  717. scope='local', read=True)
  718. @asyncio.coroutine
  719. def vm_feature_get(self):
  720. # validation of self.arg done by qrexec-policy is enough
  721. self.fire_event_for_permission()
  722. try:
  723. value = self.dest.features[self.arg]
  724. except KeyError:
  725. raise qubes.exc.QubesFeatureNotFoundError(self.dest, self.arg)
  726. return value
  727. @qubes.api.method('admin.vm.feature.CheckWithTemplate', no_payload=True,
  728. scope='local', read=True)
  729. @asyncio.coroutine
  730. def vm_feature_checkwithtemplate(self):
  731. # validation of self.arg done by qrexec-policy is enough
  732. self.fire_event_for_permission()
  733. try:
  734. value = self.dest.features.check_with_template(self.arg)
  735. except KeyError:
  736. raise qubes.exc.QubesFeatureNotFoundError(self.dest, self.arg)
  737. return value
  738. @qubes.api.method('admin.vm.feature.CheckWithNetvm', no_payload=True,
  739. scope='local', read=True)
  740. @asyncio.coroutine
  741. def vm_feature_checkwithnetvm(self):
  742. # validation of self.arg done by qrexec-policy is enough
  743. self.fire_event_for_permission()
  744. try:
  745. value = self.dest.features.check_with_netvm(self.arg)
  746. except KeyError:
  747. raise qubes.exc.QubesFeatureNotFoundError(self.dest, self.arg)
  748. return value
  749. @qubes.api.method('admin.vm.feature.Remove', no_payload=True,
  750. scope='local', write=True)
  751. @asyncio.coroutine
  752. def vm_feature_remove(self):
  753. # validation of self.arg done by qrexec-policy is enough
  754. self.fire_event_for_permission()
  755. try:
  756. del self.dest.features[self.arg]
  757. except KeyError:
  758. raise qubes.exc.QubesFeatureNotFoundError(self.dest, self.arg)
  759. self.app.save()
  760. @qubes.api.method('admin.vm.feature.Set',
  761. scope='local', write=True)
  762. @asyncio.coroutine
  763. def vm_feature_set(self, untrusted_payload):
  764. # validation of self.arg done by qrexec-policy is enough
  765. value = untrusted_payload.decode('ascii', errors='strict')
  766. del untrusted_payload
  767. self.fire_event_for_permission(value=value)
  768. self.dest.features[self.arg] = value
  769. self.app.save()
  770. @qubes.api.method('admin.vm.Create.{endpoint}', endpoints=(ep.name
  771. for ep in pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT)),
  772. scope='global', write=True)
  773. @asyncio.coroutine
  774. def vm_create(self, endpoint, untrusted_payload=None):
  775. return self._vm_create(endpoint, allow_pool=False,
  776. untrusted_payload=untrusted_payload)
  777. @qubes.api.method('admin.vm.CreateInPool.{endpoint}', endpoints=(ep.name
  778. for ep in pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT)),
  779. scope='global', write=True)
  780. @asyncio.coroutine
  781. def vm_create_in_pool(self, endpoint, untrusted_payload=None):
  782. return self._vm_create(endpoint, allow_pool=True,
  783. untrusted_payload=untrusted_payload)
  784. def _vm_create(self, vm_type, allow_pool=False, untrusted_payload=None):
  785. assert self.dest.name == 'dom0'
  786. kwargs = {}
  787. pool = None
  788. pools = {}
  789. # this will raise exception if none is found
  790. vm_class = qubes.utils.get_entry_point_one(qubes.vm.VM_ENTRY_POINT,
  791. vm_type)
  792. # if argument is given, it needs to be a valid template, and only
  793. # when given VM class do need a template
  794. if hasattr(vm_class, 'template'):
  795. if self.arg:
  796. assert self.arg in self.app.domains
  797. kwargs['template'] = self.app.domains[self.arg]
  798. else:
  799. assert not self.arg
  800. for untrusted_param in untrusted_payload.decode('ascii',
  801. errors='strict').split(' '):
  802. untrusted_key, untrusted_value = untrusted_param.split('=', 1)
  803. if untrusted_key in kwargs:
  804. raise qubes.api.ProtocolError('duplicated parameters')
  805. if untrusted_key == 'name':
  806. qubes.vm.validate_name(None, None, untrusted_value)
  807. kwargs['name'] = untrusted_value
  808. elif untrusted_key == 'label':
  809. # don't confuse label name with label index
  810. assert not untrusted_value.isdigit()
  811. allowed_chars = string.ascii_letters + string.digits + '-_.'
  812. assert all(c in allowed_chars for c in untrusted_value)
  813. try:
  814. kwargs['label'] = self.app.get_label(untrusted_value)
  815. except KeyError:
  816. raise qubes.exc.QubesValueError
  817. elif untrusted_key == 'pool' and allow_pool:
  818. if pool is not None:
  819. raise qubes.api.ProtocolError('duplicated pool parameter')
  820. pool = self.app.get_pool(untrusted_value)
  821. elif untrusted_key.startswith('pool:') and allow_pool:
  822. untrusted_volume = untrusted_key.split(':', 1)[1]
  823. # kind of ugly, but actual list of volumes is available only
  824. # after creating a VM
  825. assert untrusted_volume in ['root', 'private', 'volatile',
  826. 'kernel']
  827. volume = untrusted_volume
  828. if volume in pools:
  829. raise qubes.api.ProtocolError(
  830. 'duplicated pool:{} parameter'.format(volume))
  831. pools[volume] = self.app.get_pool(untrusted_value)
  832. else:
  833. raise qubes.api.ProtocolError('Invalid param name')
  834. del untrusted_payload
  835. if 'name' not in kwargs or 'label' not in kwargs:
  836. raise qubes.api.ProtocolError('Missing name or label')
  837. if pool and pools:
  838. raise qubes.api.ProtocolError(
  839. 'Only one of \'pool=\' and \'pool:volume=\' can be used')
  840. if kwargs['name'] in self.app.domains:
  841. raise qubes.exc.QubesValueError(
  842. 'VM {} already exists'.format(kwargs['name']))
  843. self.fire_event_for_permission(pool=pool, pools=pools, **kwargs)
  844. vm = self.app.add_new_vm(vm_class, **kwargs)
  845. # TODO: move this to extension (in race-free fashion)
  846. vm.tags.add('created-by-' + str(self.src))
  847. try:
  848. yield from vm.create_on_disk(pool=pool, pools=pools)
  849. except:
  850. del self.app.domains[vm]
  851. raise
  852. self.app.save()
  853. @qubes.api.method('admin.vm.CreateDisposable', no_payload=True,
  854. scope='global', write=True)
  855. @asyncio.coroutine
  856. def create_disposable(self):
  857. assert not self.arg
  858. if self.dest.name == 'dom0':
  859. dispvm_template = self.src.default_dispvm
  860. else:
  861. dispvm_template = self.dest
  862. self.fire_event_for_permission(dispvm_template=dispvm_template)
  863. dispvm = yield from qubes.vm.dispvm.DispVM.from_appvm(dispvm_template)
  864. # TODO: move this to extension (in race-free fashion, better than here)
  865. dispvm.tags.add('disp-created-by-' + str(self.src))
  866. return dispvm.name
  867. @qubes.api.method('admin.vm.Remove', no_payload=True,
  868. scope='global', write=True)
  869. @asyncio.coroutine
  870. def vm_remove(self):
  871. assert not self.arg
  872. self.fire_event_for_permission()
  873. if not self.dest.is_halted():
  874. raise qubes.exc.QubesVMNotHaltedError(self.dest)
  875. if self.dest.installed_by_rpm:
  876. raise qubes.exc.QubesVMInUseError(self.dest, \
  877. "VM installed by package manager: " + self.dest.name)
  878. del self.app.domains[self.dest]
  879. try:
  880. yield from self.dest.remove_from_disk()
  881. except: # pylint: disable=bare-except
  882. self.app.log.exception('Error wile removing VM \'%s\' files',
  883. self.dest.name)
  884. self.app.save()
  885. @qubes.api.method('admin.vm.device.{endpoint}.Available', endpoints=(ep.name
  886. for ep in pkg_resources.iter_entry_points('qubes.devices')),
  887. no_payload=True,
  888. scope='local', read=True)
  889. @asyncio.coroutine
  890. def vm_device_available(self, endpoint):
  891. devclass = endpoint
  892. devices = self.dest.devices[devclass].available()
  893. if self.arg:
  894. devices = [dev for dev in devices if dev.ident == self.arg]
  895. # no duplicated devices, but device may not exists, in which case
  896. # the list is empty
  897. assert len(devices) <= 1
  898. devices = self.fire_event_for_filter(devices, devclass=devclass)
  899. dev_info = {}
  900. for dev in devices:
  901. non_default_attrs = set(attr for attr in dir(dev) if
  902. not attr.startswith('_')).difference((
  903. 'backend_domain', 'ident', 'frontend_domain',
  904. 'description', 'options', 'regex'))
  905. properties_txt = ' '.join(
  906. '{}={!s}'.format(prop, value) for prop, value
  907. in itertools.chain(
  908. ((key, getattr(dev, key)) for key in non_default_attrs),
  909. # keep description as the last one, according to API
  910. # specification
  911. (('description', dev.description),)
  912. ))
  913. assert '\n' not in properties_txt
  914. dev_info[dev.ident] = properties_txt
  915. return ''.join('{} {}\n'.format(ident, dev_info[ident])
  916. for ident in sorted(dev_info))
  917. @qubes.api.method('admin.vm.device.{endpoint}.List', endpoints=(ep.name
  918. for ep in pkg_resources.iter_entry_points('qubes.devices')),
  919. no_payload=True,
  920. scope='local', read=True)
  921. @asyncio.coroutine
  922. def vm_device_list(self, endpoint):
  923. devclass = endpoint
  924. device_assignments = self.dest.devices[devclass].assignments()
  925. if self.arg:
  926. select_backend, select_ident = self.arg.split('+', 1)
  927. device_assignments = [dev for dev in device_assignments
  928. if (str(dev.backend_domain), dev.ident)
  929. == (select_backend, select_ident)]
  930. # no duplicated devices, but device may not exists, in which case
  931. # the list is empty
  932. assert len(device_assignments) <= 1
  933. device_assignments = self.fire_event_for_filter(device_assignments,
  934. devclass=devclass)
  935. dev_info = {}
  936. for dev in device_assignments:
  937. properties_txt = ' '.join(
  938. '{}={!s}'.format(opt, value) for opt, value
  939. in itertools.chain(
  940. dev.options.items(),
  941. (('persistent', 'yes' if dev.persistent else 'no'),)
  942. ))
  943. assert '\n' not in properties_txt
  944. ident = '{!s}+{!s}'.format(dev.backend_domain, dev.ident)
  945. dev_info[ident] = properties_txt
  946. return ''.join('{} {}\n'.format(ident, dev_info[ident])
  947. for ident in sorted(dev_info))
  948. # Attach/Detach action can both modify persistent state (with
  949. # persistent=True) and volatile state of running VM (with persistent=False).
  950. # For this reason, write=True + execute=True
  951. @qubes.api.method('admin.vm.device.{endpoint}.Attach', endpoints=(ep.name
  952. for ep in pkg_resources.iter_entry_points('qubes.devices')),
  953. scope='local', write=True, execute=True)
  954. @asyncio.coroutine
  955. def vm_device_attach(self, endpoint, untrusted_payload):
  956. devclass = endpoint
  957. options = {}
  958. persistent = False
  959. for untrusted_option in untrusted_payload.decode('ascii').split():
  960. try:
  961. untrusted_key, untrusted_value = untrusted_option.split('=', 1)
  962. except ValueError:
  963. raise qubes.api.ProtocolError('Invalid options format')
  964. if untrusted_key == 'persistent':
  965. persistent = qubes.property.bool(None, None, untrusted_value)
  966. else:
  967. allowed_chars_key = string.digits + string.ascii_letters + '-_.'
  968. allowed_chars_value = allowed_chars_key + ',+:'
  969. if any(x not in allowed_chars_key for x in untrusted_key):
  970. raise qubes.api.ProtocolError(
  971. 'Invalid chars in option name')
  972. if any(x not in allowed_chars_value for x in untrusted_value):
  973. raise qubes.api.ProtocolError(
  974. 'Invalid chars in option value')
  975. options[untrusted_key] = untrusted_value
  976. # qrexec already verified that no strange characters are in self.arg
  977. backend_domain, ident = self.arg.split('+', 1)
  978. # may raise KeyError, either on domain or ident
  979. dev = self.app.domains[backend_domain].devices[devclass][ident]
  980. self.fire_event_for_permission(device=dev,
  981. devclass=devclass, persistent=persistent,
  982. options=options)
  983. assignment = qubes.devices.DeviceAssignment(
  984. dev.backend_domain, dev.ident,
  985. options=options, persistent=persistent)
  986. yield from self.dest.devices[devclass].attach(assignment)
  987. self.app.save()
  988. # Attach/Detach action can both modify persistent state (with
  989. # persistent=True) and volatile state of running VM (with persistent=False).
  990. # For this reason, write=True + execute=True
  991. @qubes.api.method('admin.vm.device.{endpoint}.Detach', endpoints=(ep.name
  992. for ep in pkg_resources.iter_entry_points('qubes.devices')),
  993. no_payload=True,
  994. scope='local', write=True, execute=True)
  995. @asyncio.coroutine
  996. def vm_device_detach(self, endpoint):
  997. devclass = endpoint
  998. # qrexec already verified that no strange characters are in self.arg
  999. backend_domain, ident = self.arg.split('+', 1)
  1000. # may raise KeyError; if device isn't found, it will be UnknownDevice
  1001. # instance - but allow it, otherwise it will be impossible to detach
  1002. # already removed device
  1003. dev = self.app.domains[backend_domain].devices[devclass][ident]
  1004. self.fire_event_for_permission(device=dev,
  1005. devclass=devclass)
  1006. assignment = qubes.devices.DeviceAssignment(
  1007. dev.backend_domain, dev.ident)
  1008. yield from self.dest.devices[devclass].detach(assignment)
  1009. self.app.save()
  1010. # Attach/Detach action can both modify persistent state (with
  1011. # persistent=True) and volatile state of running VM (with persistent=False).
  1012. # For this reason, write=True + execute=True
  1013. @qubes.api.method('admin.vm.device.{endpoint}.Set.persistent',
  1014. endpoints=(ep.name
  1015. for ep in pkg_resources.iter_entry_points('qubes.devices')),
  1016. scope='local', write=True, execute=True)
  1017. @asyncio.coroutine
  1018. def vm_device_set_persistent(self, endpoint, untrusted_payload):
  1019. devclass = endpoint
  1020. assert untrusted_payload in (b'True', b'False')
  1021. persistent = untrusted_payload == b'True'
  1022. del untrusted_payload
  1023. # qrexec already verified that no strange characters are in self.arg
  1024. backend_domain, ident = self.arg.split('+', 1)
  1025. # device must be already attached
  1026. matching_devices = [dev for dev
  1027. in self.dest.devices[devclass].attached()
  1028. if dev.backend_domain.name == backend_domain and dev.ident == ident]
  1029. assert len(matching_devices) == 1
  1030. dev = matching_devices[0]
  1031. self.fire_event_for_permission(device=dev,
  1032. persistent=persistent)
  1033. self.dest.devices[devclass].update_persistent(dev, persistent)
  1034. self.app.save()
  1035. @qubes.api.method('admin.vm.firewall.Get', no_payload=True,
  1036. scope='local', read=True)
  1037. @asyncio.coroutine
  1038. def vm_firewall_get(self):
  1039. assert not self.arg
  1040. self.fire_event_for_permission()
  1041. return ''.join('{}\n'.format(rule.api_rule)
  1042. for rule in self.dest.firewall.rules
  1043. if rule.api_rule is not None)
  1044. @qubes.api.method('admin.vm.firewall.Set',
  1045. scope='local', write=True)
  1046. @asyncio.coroutine
  1047. def vm_firewall_set(self, untrusted_payload):
  1048. assert not self.arg
  1049. rules = []
  1050. for untrusted_line in untrusted_payload.decode('ascii',
  1051. errors='strict').splitlines():
  1052. rule = qubes.firewall.Rule.from_api_string(
  1053. untrusted_rule=untrusted_line)
  1054. rules.append(rule)
  1055. self.fire_event_for_permission(rules=rules)
  1056. self.dest.firewall.rules = rules
  1057. self.dest.firewall.save()
  1058. @qubes.api.method('admin.vm.firewall.Reload', no_payload=True,
  1059. scope='local', execute=True)
  1060. @asyncio.coroutine
  1061. def vm_firewall_reload(self):
  1062. assert not self.arg
  1063. self.fire_event_for_permission()
  1064. self.dest.fire_event('firewall-changed')
  1065. @asyncio.coroutine
  1066. def _load_backup_profile(self, profile_name, skip_passphrase=False):
  1067. '''Load backup profile and return :py:class:`qubes.backup.Backup`
  1068. instance
  1069. :param profile_name: name of the profile
  1070. :param skip_passphrase: do not load passphrase - only backup summary
  1071. can be retrieved when this option is in use
  1072. '''
  1073. profile_path = os.path.join(
  1074. qubes.config.backup_profile_dir, profile_name + '.conf')
  1075. with open(profile_path) as profile_file:
  1076. profile_data = yaml.safe_load(profile_file)
  1077. try:
  1078. dest_vm = profile_data['destination_vm']
  1079. dest_path = profile_data['destination_path']
  1080. include_vms = profile_data['include']
  1081. if include_vms is not None:
  1082. # convert old keywords to new keywords
  1083. include_vms = [vm.replace('$', '@') for vm in include_vms]
  1084. exclude_vms = profile_data.get('exclude', [])
  1085. # convert old keywords to new keywords
  1086. exclude_vms = [vm.replace('$', '@') for vm in exclude_vms]
  1087. compression = profile_data.get('compression', True)
  1088. except KeyError as err:
  1089. raise qubes.exc.QubesException(
  1090. 'Invalid backup profile - missing {}'.format(err))
  1091. try:
  1092. dest_vm = self.app.domains[dest_vm]
  1093. except KeyError:
  1094. raise qubes.exc.QubesException(
  1095. 'Invalid destination_vm specified in backup profile')
  1096. if isinstance(dest_vm, qubes.vm.adminvm.AdminVM):
  1097. dest_vm = None
  1098. if skip_passphrase:
  1099. passphrase = None
  1100. elif 'passphrase_text' in profile_data:
  1101. passphrase = profile_data['passphrase_text']
  1102. elif 'passphrase_vm' in profile_data:
  1103. passphrase_vm_name = profile_data['passphrase_vm']
  1104. try:
  1105. passphrase_vm = self.app.domains[passphrase_vm_name]
  1106. except KeyError:
  1107. raise qubes.exc.QubesException(
  1108. 'Invalid backup profile - invalid passphrase_vm')
  1109. try:
  1110. passphrase, _ = yield from passphrase_vm.run_service_for_stdio(
  1111. 'qubes.BackupPassphrase+' + self.arg)
  1112. # make it foolproof against "echo passphrase" implementation
  1113. passphrase = passphrase.strip()
  1114. assert b'\n' not in passphrase
  1115. except subprocess.CalledProcessError:
  1116. raise qubes.exc.QubesException(
  1117. 'Failed to retrieve passphrase from \'{}\' VM'.format(
  1118. passphrase_vm_name))
  1119. else:
  1120. raise qubes.exc.QubesException(
  1121. 'Invalid backup profile - you need to '
  1122. 'specify passphrase_text or passphrase_vm')
  1123. # handle include
  1124. if include_vms is None:
  1125. vms_to_backup = None
  1126. else:
  1127. vms_to_backup = set(vm for vm in self.app.domains
  1128. if any(qubes.utils.match_vm_name_with_special(vm, name)
  1129. for name in include_vms))
  1130. # handle exclude
  1131. vms_to_backup.difference_update(vm for vm in self.app.domains
  1132. if any(qubes.utils.match_vm_name_with_special(vm, name)
  1133. for name in exclude_vms))
  1134. kwargs = {
  1135. 'target_vm': dest_vm,
  1136. 'target_dir': dest_path,
  1137. 'compressed': bool(compression),
  1138. 'passphrase': passphrase,
  1139. }
  1140. if isinstance(compression, str):
  1141. kwargs['compression_filter'] = compression
  1142. backup = qubes.backup.Backup(self.app, vms_to_backup, **kwargs)
  1143. return backup
  1144. def _backup_progress_callback(self, profile_name, progress):
  1145. self.app.fire_event('backup-progress', backup_profile=profile_name,
  1146. progress=progress)
  1147. @qubes.api.method('admin.backup.Execute', no_payload=True,
  1148. scope='global', read=True, execute=True)
  1149. @asyncio.coroutine
  1150. def backup_execute(self):
  1151. assert self.dest.name == 'dom0'
  1152. assert self.arg
  1153. assert '/' not in self.arg
  1154. self.fire_event_for_permission()
  1155. profile_path = os.path.join(qubes.config.backup_profile_dir,
  1156. self.arg + '.conf')
  1157. if not os.path.exists(profile_path):
  1158. raise qubes.api.PermissionDenied(
  1159. 'Backup profile {} does not exist'.format(self.arg))
  1160. if not hasattr(self.app, 'api_admin_running_backups'):
  1161. self.app.api_admin_running_backups = {}
  1162. backup = yield from self._load_backup_profile(self.arg)
  1163. backup.progress_callback = functools.partial(
  1164. self._backup_progress_callback, self.arg)
  1165. # forbid running the same backup operation twice at the time
  1166. assert self.arg not in self.app.api_admin_running_backups
  1167. backup_task = asyncio.ensure_future(backup.backup_do())
  1168. self.app.api_admin_running_backups[self.arg] = backup_task
  1169. try:
  1170. yield from backup_task
  1171. except asyncio.CancelledError:
  1172. raise qubes.exc.QubesException('Backup cancelled')
  1173. finally:
  1174. del self.app.api_admin_running_backups[self.arg]
  1175. @qubes.api.method('admin.backup.Cancel', no_payload=True,
  1176. scope='global', execute=True)
  1177. @asyncio.coroutine
  1178. def backup_cancel(self):
  1179. assert self.dest.name == 'dom0'
  1180. assert self.arg
  1181. assert '/' not in self.arg
  1182. self.fire_event_for_permission()
  1183. if not hasattr(self.app, 'api_admin_running_backups'):
  1184. self.app.api_admin_running_backups = {}
  1185. if self.arg not in self.app.api_admin_running_backups:
  1186. raise qubes.exc.QubesException('Backup operation not running')
  1187. self.app.api_admin_running_backups[self.arg].cancel()
  1188. @qubes.api.method('admin.backup.Info', no_payload=True,
  1189. scope='local', read=True)
  1190. @asyncio.coroutine
  1191. def backup_info(self):
  1192. assert self.dest.name == 'dom0'
  1193. assert self.arg
  1194. assert '/' not in self.arg
  1195. self.fire_event_for_permission()
  1196. profile_path = os.path.join(qubes.config.backup_profile_dir,
  1197. self.arg + '.conf')
  1198. if not os.path.exists(profile_path):
  1199. raise qubes.api.PermissionDenied(
  1200. 'Backup profile {} does not exist'.format(self.arg))
  1201. backup = yield from self._load_backup_profile(self.arg,
  1202. skip_passphrase=True)
  1203. return backup.get_backup_summary()
  1204. def _send_stats_single(self, info_time, info, only_vm, filters,
  1205. id_to_name_map):
  1206. '''A single iteration of sending VM stats
  1207. :param info_time: time of previous iteration
  1208. :param info: information retrieved in previous iteration
  1209. :param only_vm: send information only about this VM
  1210. :param filters: filters to apply on stats before sending
  1211. :param id_to_name_map: ID->VM name map, may be modified
  1212. :return: tuple(info_time, info) - new information (to be passed to
  1213. the next iteration)
  1214. '''
  1215. (info_time, info) = self.app.host.get_vm_stats(info_time, info,
  1216. only_vm=only_vm)
  1217. for vm_id, vm_info in info.items():
  1218. if vm_id not in id_to_name_map:
  1219. try:
  1220. name = \
  1221. self.app.vmm.libvirt_conn.lookupByID(vm_id).name()
  1222. except libvirt.libvirtError as err:
  1223. if err.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  1224. # stubdomain or so
  1225. name = None
  1226. else:
  1227. raise
  1228. id_to_name_map[vm_id] = name
  1229. else:
  1230. name = id_to_name_map[vm_id]
  1231. # skip VMs with unknown name
  1232. if name is None:
  1233. continue
  1234. if not list(qubes.api.apply_filters([name], filters)):
  1235. continue
  1236. self.send_event(name, 'vm-stats',
  1237. memory_kb=int(vm_info['memory_kb']),
  1238. cpu_time=int(vm_info['cpu_time'] / 1000000),
  1239. cpu_usage=int(vm_info['cpu_usage']))
  1240. return info_time, info
  1241. @qubes.api.method('admin.vm.Stats', no_payload=True,
  1242. scope='global', read=True)
  1243. @asyncio.coroutine
  1244. def vm_stats(self):
  1245. assert not self.arg
  1246. # run until client connection is terminated
  1247. self.cancellable = True
  1248. # cache event filters, to not call an event each time an event arrives
  1249. stats_filters = self.fire_event_for_permission()
  1250. only_vm = None
  1251. if self.dest.name != 'dom0':
  1252. only_vm = self.dest
  1253. self.send_event(self.app, 'connection-established')
  1254. info_time = None
  1255. info = None
  1256. id_to_name_map = {0: 'dom0'}
  1257. try:
  1258. while True:
  1259. info_time, info = self._send_stats_single(info_time, info,
  1260. only_vm, stats_filters, id_to_name_map)
  1261. yield from asyncio.sleep(self.app.stats_interval)
  1262. except asyncio.CancelledError:
  1263. # valid method to terminate this loop
  1264. pass