app.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. """
  21. Main Qubes() class and related classes.
  22. """
  23. import os
  24. import shlex
  25. import socket
  26. import shutil
  27. import subprocess
  28. import sys
  29. import logging
  30. import qubesadmin.base
  31. import qubesadmin.exc
  32. import qubesadmin.label
  33. import qubesadmin.storage
  34. import qubesadmin.utils
  35. import qubesadmin.vm
  36. import qubesadmin.config
  37. import qubesadmin.devices
  38. class VMCollection(object):
  39. """Collection of VMs objects"""
  40. def __init__(self, app):
  41. self.app = app
  42. self._vm_list = None
  43. self._vm_objects = {}
  44. def clear_cache(self):
  45. """Clear cached list of VMs"""
  46. self._vm_list = None
  47. def refresh_cache(self, force=False):
  48. """Refresh cached list of VMs"""
  49. if not force and self._vm_list is not None:
  50. return
  51. vm_list_data = self.app.qubesd_call(
  52. 'dom0',
  53. 'admin.vm.List'
  54. )
  55. new_vm_list = {}
  56. # FIXME: this will probably change
  57. for vm_data in vm_list_data.splitlines():
  58. vm_name, props = vm_data.decode('ascii').split(' ', 1)
  59. vm_name = str(vm_name)
  60. props = props.split(' ')
  61. new_vm_list[vm_name] = dict(
  62. [vm_prop.split('=', 1) for vm_prop in props])
  63. self._vm_list = new_vm_list
  64. for name, vm in list(self._vm_objects.items()):
  65. if vm.name not in self._vm_list:
  66. # VM no longer exists
  67. del self._vm_objects[name]
  68. elif vm.__class__.__name__ != self._vm_list[vm.name]['class']:
  69. # VM class have changed
  70. del self._vm_objects[name]
  71. # TODO: some generation ID, to detect VM re-creation
  72. elif name != vm.name:
  73. # renamed
  74. self._vm_objects[vm.name] = vm
  75. del self._vm_objects[name]
  76. def __getitem__(self, item):
  77. if isinstance(item, qubesadmin.vm.QubesVM):
  78. item = item.name
  79. if not self.app.blind_mode and item not in self:
  80. raise KeyError(item)
  81. return self.get_blind(item)
  82. def get_blind(self, item):
  83. """
  84. Get a vm without downloading the list
  85. and checking if exists
  86. """
  87. if item not in self._vm_objects:
  88. cls = qubesadmin.vm.QubesVM
  89. # provide class name to constructor, if already cached (which can be
  90. # done by 'item not in self' check above, unless blind_mode is
  91. # enabled
  92. klass = None
  93. if self._vm_list and item in self._vm_list:
  94. klass = self._vm_list[item]['class']
  95. self._vm_objects[item] = cls(self.app, item, klass=klass)
  96. return self._vm_objects[item]
  97. def __contains__(self, item):
  98. if isinstance(item, qubesadmin.vm.QubesVM):
  99. item = item.name
  100. self.refresh_cache()
  101. return item in self._vm_list
  102. def __delitem__(self, key):
  103. self.app.qubesd_call(key, 'admin.vm.Remove')
  104. self.clear_cache()
  105. def __iter__(self):
  106. self.refresh_cache()
  107. for vm in sorted(self._vm_list):
  108. yield self[vm]
  109. def keys(self):
  110. """Get list of VM names."""
  111. self.refresh_cache()
  112. return self._vm_list.keys()
  113. def values(self):
  114. """Get list of VM objects."""
  115. self.refresh_cache()
  116. return [self[name] for name in self._vm_list]
  117. class QubesBase(qubesadmin.base.PropertyHolder):
  118. """Main Qubes application.
  119. This is a base abstract class, don't use it directly. Use specialized
  120. class in py:class:`qubesadmin.Qubes` instead, which points at
  121. :py:class:`QubesLocal` or :py:class:`QubesRemote`.
  122. """
  123. #: domains (VMs) collection
  124. domains = None
  125. #: labels collection
  126. labels = None
  127. #: storage pools
  128. pools = None
  129. #: type of qubesd connection: either 'socket' or 'qrexec'
  130. qubesd_connection_type = None
  131. #: logger
  132. log = None
  133. #: do not check for object (VM, label etc) existence before really needed
  134. blind_mode = False
  135. def __init__(self):
  136. super(QubesBase, self).__init__(self, 'admin.property.', 'dom0')
  137. self.domains = VMCollection(self)
  138. self.labels = qubesadmin.base.WrapperObjectsCollection(
  139. self, 'admin.label.List', qubesadmin.label.Label)
  140. self.pools = qubesadmin.base.WrapperObjectsCollection(
  141. self, 'admin.pool.List', qubesadmin.storage.Pool)
  142. #: cache for available storage pool drivers and options to create them
  143. self._pool_drivers = None
  144. self.log = logging.getLogger('app')
  145. self._local_name = None
  146. def list_vmclass(self):
  147. """Call Qubesd in order to obtain the vm classes list"""
  148. vmclass = self.qubesd_call('dom0', 'admin.vmclass.List') \
  149. .decode().splitlines()
  150. return sorted(vmclass)
  151. def list_deviceclass(self):
  152. """Call Qubesd in order to obtain the device classes list"""
  153. deviceclasses = self.qubesd_call('dom0', 'admin.deviceclass.List') \
  154. .decode().splitlines()
  155. return sorted(deviceclasses)
  156. def _refresh_pool_drivers(self):
  157. """
  158. Refresh cached storage pool drivers and their parameters.
  159. :return: None
  160. """
  161. if self._pool_drivers is None:
  162. pool_drivers_data = self.qubesd_call(
  163. 'dom0', 'admin.pool.ListDrivers', None, None)
  164. assert pool_drivers_data.endswith(b'\n')
  165. pool_drivers = {}
  166. for driver_line in pool_drivers_data.decode('ascii').splitlines():
  167. if not driver_line:
  168. continue
  169. driver_name, driver_options = driver_line.split(' ', 1)
  170. pool_drivers[driver_name] = driver_options.split(' ')
  171. self._pool_drivers = pool_drivers
  172. @property
  173. def pool_drivers(self):
  174. """ Available storage pool drivers """
  175. self._refresh_pool_drivers()
  176. return self._pool_drivers.keys()
  177. def pool_driver_parameters(self, driver):
  178. """ Parameters to initialize storage pool using given driver """
  179. self._refresh_pool_drivers()
  180. return self._pool_drivers[driver]
  181. def add_pool(self, name, driver, **kwargs):
  182. """ Add a storage pool to config
  183. :param name: name of storage pool to create
  184. :param driver: driver to use, see :py:meth:`pool_drivers` for
  185. available drivers
  186. :param kwargs: configuration parameters for storage pool,
  187. see :py:meth:`pool_driver_parameters` for a list
  188. """
  189. # sort parameters only to ease testing, not required by API
  190. payload = 'name={}\n'.format(name) + \
  191. ''.join('{}={}\n'.format(key, value)
  192. for key, value in sorted(kwargs.items()))
  193. self.qubesd_call('dom0', 'admin.pool.Add', driver,
  194. payload.encode('utf-8'))
  195. def remove_pool(self, name):
  196. """ Remove a storage pool """
  197. self.qubesd_call('dom0', 'admin.pool.Remove', name, None)
  198. @property
  199. def local_name(self):
  200. """ Get localhost name """
  201. if not self._local_name:
  202. self._local_name = os.uname()[1]
  203. return self._local_name
  204. def get_label(self, label):
  205. """Get label as identified by index or name
  206. :throws KeyError: when label is not found
  207. """
  208. # first search for name, verbatim
  209. try:
  210. return self.labels[label]
  211. except KeyError:
  212. pass
  213. # then search for index
  214. if isinstance(label, int) or label.isdigit():
  215. for i in self.labels.values():
  216. if i.index == int(label):
  217. return i
  218. raise KeyError(label)
  219. @staticmethod
  220. def get_vm_class(clsname):
  221. """Find the class for a domain.
  222. Compatibility function, client tools use str to identify domain classes.
  223. :param str clsname: name of the class
  224. :return str: class
  225. """
  226. return clsname
  227. def add_new_vm(self, cls, name, label, template=None, pool=None,
  228. pools=None):
  229. """Create new Virtual Machine
  230. Example usage with custom storage pools:
  231. >>> app = qubesadmin.Qubes()
  232. >>> pools = {'private': 'external'}
  233. >>> vm = app.add_new_vm('AppVM', 'my-new-vm', 'red',
  234. >>> 'my-template', pools=pools)
  235. >>> vm.netvm = app.domains['sys-whonix']
  236. :param str cls: name of VM class (`AppVM`, `TemplateVM` etc)
  237. :param str name: name of VM
  238. :param str label: label color for new VM
  239. :param str template: template to use (if apply for given VM class),
  240. can be also VM object; use None for default value
  241. :param str pool: storage pool to use instead of default one
  242. :param dict pools: storage pool for specific volumes
  243. :return new VM object
  244. """
  245. if not isinstance(cls, str):
  246. cls = cls.__name__
  247. if template is qubesadmin.DEFAULT:
  248. template = None
  249. elif template is not None:
  250. template = str(template)
  251. if pool and pools:
  252. raise ValueError('only one of pool= and pools= can be used')
  253. method_prefix = 'admin.vm.Create.'
  254. payload = 'name={} label={}'.format(name, label)
  255. if pool:
  256. payload += ' pool={}'.format(str(pool))
  257. method_prefix = 'admin.vm.CreateInPool.'
  258. if pools:
  259. payload += ''.join(' pool:{}={}'.format(vol, str(pool))
  260. for vol, pool in sorted(pools.items()))
  261. method_prefix = 'admin.vm.CreateInPool.'
  262. self.qubesd_call('dom0', method_prefix + cls, template,
  263. payload.encode('utf-8'))
  264. self.domains.clear_cache()
  265. return self.domains[name]
  266. def clone_vm(self, src_vm, new_name, new_cls=None, pool=None, pools=None,
  267. ignore_errors=False, ignore_volumes=None,
  268. ignore_devices=False):
  269. # pylint: disable=too-many-statements
  270. """Clone Virtual Machine
  271. Example usage with custom storage pools:
  272. >>> app = qubesadmin.Qubes()
  273. >>> pools = {'private': 'external'}
  274. >>> src_vm = app.domains['personal']
  275. >>> vm = app.clone_vm(src_vm, 'my-new-vm', pools=pools)
  276. >>> vm.label = app.labels['green']
  277. :param QubesVM or str src_vm: source VM
  278. :param str new_name: name of new VM
  279. :param str new_cls: name of VM class (`AppVM`, `TemplateVM` etc) - use
  280. None to copy it from *src_vm*
  281. :param str pool: storage pool to use instead of default one
  282. :param dict pools: storage pool for specific volumes
  283. :param bool ignore_errors: should errors on meta-data setting be only
  284. logged, or abort the whole operation?
  285. :param list ignore_volumes: do not clone volumes on this list,
  286. like 'private' or 'root'
  287. :param bool ignore_devices: if True, do not copy device assignments
  288. :return new VM object
  289. """
  290. if pool and pools:
  291. raise ValueError('only one of pool= and pools= can be used')
  292. if isinstance(src_vm, str):
  293. src_vm = self.domains[src_vm]
  294. if new_cls is None:
  295. new_cls = src_vm.klass
  296. template = getattr(src_vm, 'template', None)
  297. if template is not None:
  298. template = str(template)
  299. label = src_vm.label
  300. if pool is None and pools is None:
  301. # use the same pools as the source - check if non default is used
  302. for volume in sorted(src_vm.volumes.values()):
  303. if not volume.save_on_stop:
  304. # clone only persistent volumes
  305. continue
  306. if ignore_volumes and volume.name in ignore_volumes:
  307. continue
  308. default_pool = getattr(self.app, 'default_pool_' + volume.name,
  309. volume.pool)
  310. if default_pool != volume.pool:
  311. if pools is None:
  312. pools = {}
  313. pools[volume.name] = volume.pool
  314. method_prefix = 'admin.vm.Create.'
  315. payload = 'name={} label={}'.format(new_name, label)
  316. if pool:
  317. payload += ' pool={}'.format(str(pool))
  318. method_prefix = 'admin.vm.CreateInPool.'
  319. if pools:
  320. payload += ''.join(' pool:{}={}'.format(vol, str(pool))
  321. for vol, pool in sorted(pools.items()))
  322. method_prefix = 'admin.vm.CreateInPool.'
  323. self.qubesd_call('dom0', method_prefix + new_cls, template,
  324. payload.encode('utf-8'))
  325. self.domains.clear_cache()
  326. dst_vm = self.domains[new_name]
  327. try:
  328. assert isinstance(dst_vm, qubesadmin.vm.QubesVM)
  329. for prop in src_vm.property_list():
  330. # handled by admin.vm.Create call
  331. if prop in ('name', 'qid', 'template', 'label', 'uuid',
  332. 'installed_by_rpm'):
  333. continue
  334. if src_vm.property_is_default(prop):
  335. continue
  336. try:
  337. setattr(dst_vm, prop, getattr(src_vm, prop))
  338. except AttributeError:
  339. pass
  340. except qubesadmin.exc.QubesException as e:
  341. dst_vm.log.error(
  342. 'Failed to set {!s} property: {!s}'.format(prop, e))
  343. if not ignore_errors:
  344. raise
  345. for tag in src_vm.tags:
  346. if tag.startswith('created-by-'):
  347. continue
  348. try:
  349. dst_vm.tags.add(tag)
  350. except qubesadmin.exc.QubesException as e:
  351. dst_vm.log.error(
  352. 'Failed to add {!s} tag: {!s}'.format(tag, e))
  353. if not ignore_errors:
  354. raise
  355. for feature, value in src_vm.features.items():
  356. try:
  357. dst_vm.features[feature] = value
  358. except qubesadmin.exc.QubesException as e:
  359. dst_vm.log.error(
  360. 'Failed to set {!s} feature: {!s}'.format(feature, e))
  361. if not ignore_errors:
  362. raise
  363. try:
  364. dst_vm.firewall.save_rules(src_vm.firewall.rules)
  365. except qubesadmin.exc.QubesException as e:
  366. self.log.error('Failed to set firewall: %s', e)
  367. if not ignore_errors:
  368. raise
  369. try:
  370. # FIXME: convert to qrexec calls to dom0/GUI VM
  371. appmenus_cmd = \
  372. ['qvm-appmenus', '--init', '--update',
  373. '--source', src_vm.name, dst_vm.name]
  374. subprocess.check_output(appmenus_cmd, stderr=subprocess.STDOUT)
  375. except OSError:
  376. # this file needs to be python 2.7 compatible,
  377. # so no FileNotFoundError
  378. self.log.error('Failed to clone appmenus, qvm-appmenus missing')
  379. if not ignore_errors:
  380. raise qubesadmin.exc.QubesException(
  381. 'Failed to clone appmenus')
  382. except subprocess.CalledProcessError as e:
  383. self.log.error('Failed to clone appmenus: %s',
  384. e.output.decode())
  385. if not ignore_errors:
  386. raise qubesadmin.exc.QubesException(
  387. 'Failed to clone appmenus')
  388. except qubesadmin.exc.QubesException:
  389. if not ignore_errors:
  390. del self.domains[dst_vm.name]
  391. raise
  392. try:
  393. for dst_volume in sorted(dst_vm.volumes.values()):
  394. if not dst_volume.save_on_stop:
  395. # clone only persistent volumes
  396. continue
  397. if ignore_volumes and dst_volume.name in ignore_volumes:
  398. continue
  399. src_volume = src_vm.volumes[dst_volume.name]
  400. dst_vm.log.info('Cloning {} volume'.format(dst_volume.name))
  401. dst_volume.clone(src_volume)
  402. except qubesadmin.exc.QubesException:
  403. del self.domains[dst_vm.name]
  404. raise
  405. if not ignore_devices:
  406. try:
  407. for devclass in src_vm.devices:
  408. for assignment in src_vm.devices[devclass].assignments(
  409. persistent=True):
  410. new_assignment = qubesadmin.devices.DeviceAssignment(
  411. backend_domain=assignment.backend_domain,
  412. ident=assignment.ident,
  413. options=assignment.options,
  414. persistent=assignment.persistent)
  415. dst_vm.devices[devclass].attach(new_assignment)
  416. except qubesadmin.exc.QubesException:
  417. if not ignore_errors:
  418. del self.domains[dst_vm.name]
  419. raise
  420. return dst_vm
  421. def qubesd_call(self, dest, method, arg=None, payload=None,
  422. payload_stream=None):
  423. """
  424. Execute Admin API method.
  425. If `payload` and `payload_stream` are both specified, they will be sent
  426. in that order.
  427. :param dest: Destination VM name
  428. :param method: Full API method name ('admin...')
  429. :param arg: Method argument (if any)
  430. :param payload: Payload send to the method
  431. :param payload_stream: file-like object to read payload from
  432. :return: Data returned by qubesd (string)
  433. .. warning:: *payload_stream* will get closed by this function
  434. """
  435. raise NotImplementedError(
  436. 'qubesd_call not implemented in QubesBase class; use specialized '
  437. 'class: qubesadmin.Qubes()')
  438. def run_service(self, dest, service, filter_esc=False, user=None,
  439. localcmd=None, wait=True, autostart=True, **kwargs):
  440. """Run qrexec service in a given destination
  441. *kwargs* are passed verbatim to :py:meth:`subprocess.Popen`.
  442. :param str dest: Destination - may be a VM name or empty
  443. string for default (for a given service)
  444. :param str service: service name
  445. :param bool filter_esc: filter escape sequences to protect terminal \
  446. emulator
  447. :param str user: username to run service as
  448. :param str localcmd: Command to connect stdin/stdout to
  449. :param bool wait: Wait service run
  450. :param bool autostart: Automatically start the target VM
  451. :rtype: subprocess.Popen
  452. """
  453. raise NotImplementedError(
  454. 'run_service not implemented in QubesBase class; use specialized '
  455. 'class: qubesadmin.Qubes()')
  456. @staticmethod
  457. def _call_with_stream(command, payload, payload_stream):
  458. """Helper method to pass data to qubesd. Calls a command with
  459. payload and payload_stream as input.
  460. :param command: command to run
  461. :param payload: Initial payload, or None
  462. :param payload_stream: File-like object with the rest of data
  463. :return: (process, stdout, stderr)
  464. """
  465. if payload:
  466. # It's not strictly correct to write data to stdin in this way,
  467. # because the process can get blocked on stdout or stderr pipe.
  468. # However, in practice the output should be always smaller than 4K.
  469. proc = subprocess.Popen(
  470. command,
  471. stdin=subprocess.PIPE,
  472. stdout=subprocess.PIPE,
  473. stderr=subprocess.PIPE)
  474. proc.stdin.write(payload)
  475. try:
  476. shutil.copyfileobj(payload_stream, proc.stdin)
  477. except BrokenPipeError:
  478. # We might receive an error from qubesd before we sent
  479. # everything (for instance, because we are sending too much
  480. # data).
  481. pass
  482. else:
  483. # Connect the stream directly.
  484. proc = subprocess.Popen(
  485. command,
  486. stdin=payload_stream,
  487. stdout=subprocess.PIPE,
  488. stderr=subprocess.PIPE)
  489. payload_stream.close()
  490. stdout, stderr = proc.communicate()
  491. return proc, stdout, stderr
  492. class QubesLocal(QubesBase):
  493. """Application object communicating through local socket.
  494. Used when running in dom0.
  495. """
  496. qubesd_connection_type = 'socket'
  497. def qubesd_call(self, dest, method, arg=None, payload=None,
  498. payload_stream=None):
  499. """
  500. Execute Admin API method.
  501. If `payload` and `payload_stream` are both specified, they will be sent
  502. in that order.
  503. :param dest: Destination VM name
  504. :param method: Full API method name ('admin...')
  505. :param arg: Method argument (if any)
  506. :param payload: Payload send to the method
  507. :param payload_stream: file-like object to read payload from
  508. :return: Data returned by qubesd (string)
  509. .. warning:: *payload_stream* will get closed by this function
  510. """
  511. if payload_stream:
  512. # payload_stream can be used for large amount of data,
  513. # so optimize for throughput, not latency: spawn actual qrexec
  514. # service implementation, which may use some optimization there (
  515. # see admin.vm.volume.Import - actual data handling is done with dd)
  516. method_path = os.path.join(
  517. qubesadmin.config.QREXEC_SERVICES_DIR, method)
  518. if not os.path.exists(method_path):
  519. raise qubesadmin.exc.QubesDaemonCommunicationError(
  520. '{} not found'.format(method_path))
  521. command = ['env', 'QREXEC_REMOTE_DOMAIN=dom0',
  522. 'QREXEC_REQUESTED_TARGET=' + dest, method_path, arg]
  523. if os.getuid() != 0:
  524. command.insert(0, 'sudo')
  525. (_, stdout, _) = self._call_with_stream(
  526. command, payload, payload_stream)
  527. return self._parse_qubesd_response(stdout)
  528. try:
  529. client_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  530. client_socket.connect(qubesadmin.config.QUBESD_SOCKET)
  531. except (IOError, OSError) as e:
  532. raise qubesadmin.exc.QubesDaemonCommunicationError(
  533. 'Failed to connect to qubesd service: %s', str(e))
  534. # src, method, dest, arg
  535. for call_arg in ('dom0', method, dest, arg):
  536. if call_arg is not None:
  537. client_socket.sendall(call_arg.encode('ascii'))
  538. client_socket.sendall(b'\0')
  539. if payload is not None:
  540. client_socket.sendall(payload)
  541. client_socket.shutdown(socket.SHUT_WR)
  542. return_data = client_socket.makefile('rb').read()
  543. client_socket.close()
  544. return self._parse_qubesd_response(return_data)
  545. def run_service(self, dest, service, filter_esc=False, user=None,
  546. localcmd=None, wait=True, autostart=True, **kwargs):
  547. """Run qrexec service in a given destination
  548. :param str dest: Destination - may be a VM name or empty
  549. string for default (for a given service)
  550. :param str service: service name
  551. :param bool filter_esc: filter escape sequences to protect terminal \
  552. emulator
  553. :param str user: username to run service as
  554. :param str localcmd: Command to connect stdin/stdout to
  555. :param bool wait: wait for remote process to finish
  556. :rtype: subprocess.Popen
  557. """
  558. if not dest:
  559. raise ValueError('Empty destination name allowed only from a VM')
  560. if not wait and localcmd:
  561. raise ValueError('wait=False incompatible with localcmd')
  562. if autostart:
  563. try:
  564. self.qubesd_call(dest, 'admin.vm.Start')
  565. except qubesadmin.exc.QubesVMNotHaltedError:
  566. pass
  567. elif not self.domains.get_blind(dest).is_running():
  568. raise qubesadmin.exc.QubesVMNotRunningError(
  569. '%s is not running', dest)
  570. qrexec_opts = ['-d', dest]
  571. if filter_esc:
  572. qrexec_opts.extend(['-t'])
  573. if filter_esc or os.isatty(sys.stderr.fileno()):
  574. qrexec_opts.extend(['-T'])
  575. if localcmd:
  576. qrexec_opts.extend(['-l', localcmd])
  577. if user is None:
  578. user = 'DEFAULT'
  579. if not wait:
  580. qrexec_opts.extend(['-e'])
  581. if 'connect_timeout' in kwargs:
  582. qrexec_opts.extend(['-w', str(kwargs.pop('connect_timeout'))])
  583. kwargs.setdefault('stdin', subprocess.PIPE)
  584. kwargs.setdefault('stdout', subprocess.PIPE)
  585. kwargs.setdefault('stderr', subprocess.PIPE)
  586. proc = subprocess.Popen(
  587. [qubesadmin.config.QREXEC_CLIENT] + qrexec_opts + [
  588. '{}:QUBESRPC {} dom0'.format(user, service)], **kwargs)
  589. return proc
  590. class QubesRemote(QubesBase):
  591. """Application object communicating through qrexec services.
  592. Used when running in VM.
  593. """
  594. qubesd_connection_type = 'qrexec'
  595. def qubesd_call(self, dest, method, arg=None, payload=None,
  596. payload_stream=None):
  597. """
  598. Execute Admin API method.
  599. If `payload` and `payload_stream` are both specified, they will be sent
  600. in that order.
  601. :param dest: Destination VM name
  602. :param method: Full API method name ('admin...')
  603. :param arg: Method argument (if any)
  604. :param payload: Payload send to the method
  605. :param payload_stream: file-like object to read payload from
  606. :return: Data returned by qubesd (string)
  607. .. warning:: *payload_stream* will get closed by this function
  608. """
  609. service_name = method
  610. if arg is not None:
  611. service_name += '+' + arg
  612. command = [qubesadmin.config.QREXEC_CLIENT_VM,
  613. dest, service_name]
  614. if payload_stream:
  615. (p, stdout, stderr) = self._call_with_stream(
  616. command, payload, payload_stream)
  617. else:
  618. p = subprocess.Popen(command,
  619. stdin=subprocess.PIPE,
  620. stdout=subprocess.PIPE,
  621. stderr=subprocess.PIPE)
  622. (stdout, stderr) = p.communicate(payload)
  623. if p.returncode != 0:
  624. raise qubesadmin.exc.QubesDaemonNoResponseError(
  625. 'Service call error: %s', stderr.decode())
  626. return self._parse_qubesd_response(stdout)
  627. def run_service(self, dest, service, filter_esc=False, user=None,
  628. localcmd=None, wait=True, autostart=True, **kwargs):
  629. """Run qrexec service in a given destination
  630. :param str dest: Destination - may be a VM name or empty
  631. string for default (for a given service)
  632. :param str service: service name
  633. :param bool filter_esc: filter escape sequences to protect terminal \
  634. emulator
  635. :param str user: username to run service as
  636. :param str localcmd: Command to connect stdin/stdout to
  637. :param bool wait: wait for process to finish
  638. :rtype: subprocess.Popen
  639. """
  640. if not autostart and not dest:
  641. raise ValueError(
  642. 'autostart=False makes sense only with a defined target')
  643. if user:
  644. raise ValueError(
  645. 'non-default user not possible for calls from VM')
  646. if not wait and localcmd:
  647. raise ValueError('wait=False incompatible with localcmd')
  648. qrexec_opts = []
  649. if filter_esc:
  650. qrexec_opts.extend(['-t'])
  651. if filter_esc or (
  652. os.isatty(sys.stderr.fileno()) and 'stderr' not in kwargs):
  653. qrexec_opts.extend(['-T'])
  654. if not autostart and not self.domains.get_blind(dest).is_running():
  655. raise qubesadmin.exc.QubesVMNotRunningError(
  656. '%s is not running', dest)
  657. if not wait:
  658. # qrexec-client-vm can only request service calls, which are
  659. # started using MSG_EXEC_CMDLINE qrexec protocol message; this
  660. # message means "start the process, pipe its stdin/out/err,
  661. # and when it terminates, send exit code back".
  662. # According to the protocol qrexec-client-vm needs to wait for
  663. # MSG_DATA_EXIT_CODE, so implementing wait=False would require
  664. # some protocol change (or protocol violation).
  665. raise NotImplementedError(
  666. 'wait=False not implemented for calls from VM')
  667. kwargs.setdefault('stdin', subprocess.PIPE)
  668. kwargs.setdefault('stdout', subprocess.PIPE)
  669. kwargs.setdefault('stderr', subprocess.PIPE)
  670. proc = subprocess.Popen(
  671. [qubesadmin.config.QREXEC_CLIENT_VM] +
  672. qrexec_opts +
  673. [dest or '', service] +
  674. (shlex.split(localcmd) if localcmd else []),
  675. **kwargs)
  676. return proc