app.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # -*- encoding: utf8 -*-
  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 subprocess
  27. import sys
  28. import logging
  29. import qubesadmin.base
  30. import qubesadmin.exc
  31. import qubesadmin.label
  32. import qubesadmin.storage
  33. import qubesadmin.utils
  34. import qubesadmin.vm
  35. import qubesadmin.config
  36. BUF_SIZE = 4096
  37. class VMCollection(object):
  38. '''Collection of VMs objects'''
  39. def __init__(self, app):
  40. self.app = app
  41. self._vm_list = None
  42. self._vm_objects = {}
  43. def clear_cache(self):
  44. '''Clear cached list of VMs'''
  45. self._vm_list = None
  46. def refresh_cache(self, force=False):
  47. '''Refresh cached list of VMs'''
  48. if not force and self._vm_list is not None:
  49. return
  50. vm_list_data = self.app.qubesd_call(
  51. 'dom0',
  52. 'admin.vm.List'
  53. )
  54. new_vm_list = {}
  55. # FIXME: this will probably change
  56. for vm_data in vm_list_data.splitlines():
  57. vm_name, props = vm_data.decode('ascii').split(' ', 1)
  58. vm_name = str(vm_name)
  59. props = props.split(' ')
  60. new_vm_list[vm_name] = dict(
  61. [vm_prop.split('=', 1) for vm_prop in props])
  62. self._vm_list = new_vm_list
  63. for name, vm in list(self._vm_objects.items()):
  64. if vm.name not in self._vm_list:
  65. # VM no longer exists
  66. del self._vm_objects[name]
  67. elif vm.__class__.__name__ != self._vm_list[vm.name]['class']:
  68. # VM class have changed
  69. del self._vm_objects[name]
  70. # TODO: some generation ID, to detect VM re-creation
  71. elif name != vm.name:
  72. # renamed
  73. self._vm_objects[vm.name] = vm
  74. del self._vm_objects[name]
  75. def __getitem__(self, item):
  76. if not self.app.blind_mode and item not in self:
  77. raise KeyError(item)
  78. return self.get_blind(item)
  79. def get_blind(self, item):
  80. '''
  81. Get a vm without downloading the list
  82. and checking if exists
  83. '''
  84. if item not in self._vm_objects:
  85. cls = qubesadmin.vm.QubesVM
  86. # provide class name to constructor, if already cached (which can be
  87. # done by 'item not in self' check above, unless blind_mode is
  88. # enabled
  89. klass = None
  90. if self._vm_list and item in self._vm_list:
  91. klass = self._vm_list[item]['class']
  92. self._vm_objects[item] = cls(self.app, item, klass=klass)
  93. return self._vm_objects[item]
  94. def __contains__(self, item):
  95. self.refresh_cache()
  96. return item in self._vm_list
  97. def __delitem__(self, key):
  98. self.app.qubesd_call(key, 'admin.vm.Remove')
  99. self.clear_cache()
  100. def __iter__(self):
  101. self.refresh_cache()
  102. for vm in sorted(self._vm_list):
  103. yield self[vm]
  104. def keys(self):
  105. '''Get list of VM names.'''
  106. self.refresh_cache()
  107. return self._vm_list.keys()
  108. def values(self):
  109. '''Get list of VM objects.'''
  110. self.refresh_cache()
  111. return [self[name] for name in self._vm_list]
  112. class QubesBase(qubesadmin.base.PropertyHolder):
  113. '''Main Qubes application'''
  114. #: domains (VMs) collection
  115. domains = None
  116. #: labels collection
  117. labels = None
  118. #: storage pools
  119. pools = None
  120. #: type of qubesd connection: either 'socket' or 'qrexec'
  121. qubesd_connection_type = None
  122. #: logger
  123. log = None
  124. #: do not check for object (VM, label etc) existence before really needed
  125. blind_mode = False
  126. def __init__(self):
  127. super(QubesBase, self).__init__(self, 'admin.property.', 'dom0')
  128. self.domains = VMCollection(self)
  129. self.labels = qubesadmin.base.WrapperObjectsCollection(
  130. self, 'admin.label.List', qubesadmin.label.Label)
  131. self.pools = qubesadmin.base.WrapperObjectsCollection(
  132. self, 'admin.pool.List', qubesadmin.storage.Pool)
  133. #: cache for available storage pool drivers and options to create them
  134. self._pool_drivers = None
  135. self.log = logging.getLogger('app')
  136. def _refresh_pool_drivers(self):
  137. '''
  138. Refresh cached storage pool drivers and their parameters.
  139. :return: None
  140. '''
  141. if self._pool_drivers is None:
  142. pool_drivers_data = self.qubesd_call(
  143. 'dom0', 'admin.pool.ListDrivers', None, None)
  144. assert pool_drivers_data.endswith(b'\n')
  145. pool_drivers = {}
  146. for driver_line in pool_drivers_data.decode('ascii').splitlines():
  147. if not driver_line:
  148. continue
  149. driver_name, driver_options = driver_line.split(' ', 1)
  150. pool_drivers[driver_name] = driver_options.split(' ')
  151. self._pool_drivers = pool_drivers
  152. @property
  153. def pool_drivers(self):
  154. ''' Available storage pool drivers '''
  155. self._refresh_pool_drivers()
  156. return self._pool_drivers.keys()
  157. def pool_driver_parameters(self, driver):
  158. ''' Parameters to initialize storage pool using given driver '''
  159. self._refresh_pool_drivers()
  160. return self._pool_drivers[driver]
  161. def add_pool(self, name, driver, **kwargs):
  162. ''' Add a storage pool to config
  163. :param name: name of storage pool to create
  164. :param driver: driver to use, see :py:meth:`pool_drivers` for
  165. available drivers
  166. :param kwargs: configuration parameters for storage pool,
  167. see :py:meth:`pool_driver_parameters` for a list
  168. '''
  169. # sort parameters only to ease testing, not required by API
  170. payload = 'name={}\n'.format(name) + \
  171. ''.join('{}={}\n'.format(key, value)
  172. for key, value in sorted(kwargs.items()))
  173. self.qubesd_call('dom0', 'admin.pool.Add', driver,
  174. payload.encode('utf-8'))
  175. def remove_pool(self, name):
  176. ''' Remove a storage pool '''
  177. self.qubesd_call('dom0', 'admin.pool.Remove', name, None)
  178. def get_label(self, label):
  179. '''Get label as identified by index or name
  180. :throws KeyError: when label is not found
  181. '''
  182. # first search for name, verbatim
  183. try:
  184. return self.labels[label]
  185. except KeyError:
  186. pass
  187. # then search for index
  188. if label.isdigit():
  189. for i in self.labels:
  190. if i.index == int(label):
  191. return i
  192. raise KeyError(label)
  193. @staticmethod
  194. def get_vm_class(clsname):
  195. '''Find the class for a domain.
  196. Compatibility function, client tools use str to identify domain classes.
  197. :param str clsname: name of the class
  198. :return str: class
  199. '''
  200. return clsname
  201. def add_new_vm(self, cls, name, label, template=None, pool=None,
  202. pools=None):
  203. '''Create new Virtual Machine
  204. Example usage with custom storage pools:
  205. >>> app = qubesadmin.Qubes()
  206. >>> pools = {'private': 'external'}
  207. >>> vm = app.add_new_vm('AppVM', 'my-new-vm', 'red',
  208. >>> 'my-template', pools=pools)
  209. >>> vm.netvm = app.domains['sys-whonix']
  210. :param str cls: name of VM class (`AppVM`, `TemplateVM` etc)
  211. :param str name: name of VM
  212. :param str label: label color for new VM
  213. :param str template: template to use (if apply for given VM class),
  214. can be also VM object; use None for default value
  215. :param str pool: storage pool to use instead of default one
  216. :param dict pools: storage pool for specific volumes
  217. :return new VM object
  218. '''
  219. if not isinstance(cls, str):
  220. cls = cls.__name__
  221. if template is qubesadmin.DEFAULT:
  222. template = None
  223. elif template is not None:
  224. template = str(template)
  225. if pool and pools:
  226. raise ValueError('only one of pool= and pools= can be used')
  227. method_prefix = 'admin.vm.Create.'
  228. payload = 'name={} label={}'.format(name, label)
  229. if pool:
  230. payload += ' pool={}'.format(str(pool))
  231. method_prefix = 'admin.vm.CreateInPool.'
  232. if pools:
  233. payload += ''.join(' pool:{}={}'.format(vol, str(pool))
  234. for vol, pool in sorted(pools.items()))
  235. method_prefix = 'admin.vm.CreateInPool.'
  236. self.qubesd_call('dom0', method_prefix + cls, template,
  237. payload.encode('utf-8'))
  238. self.domains.clear_cache()
  239. return self.domains[name]
  240. def clone_vm(self, src_vm, new_name, new_cls=None,
  241. pool=None, pools=None, ignore_errors=False):
  242. '''Clone Virtual Machine
  243. Example usage with custom storage pools:
  244. >>> app = qubesadmin.Qubes()
  245. >>> pools = {'private': 'external'}
  246. >>> src_vm = app.domains['personal']
  247. >>> vm = app.clone_vm(src_vm, 'my-new-vm', pools=pools)
  248. >>> vm.label = app.labels['green']
  249. :param QubesVM or str src_vm: source VM
  250. :param str new_name: name of new VM
  251. :param str new_cls: name of VM class (`AppVM`, `TemplateVM` etc) - use
  252. None to copy it from *src_vm*
  253. :param str pool: storage pool to use instead of default one
  254. :param dict pools: storage pool for specific volumes
  255. :param bool ignore_errors: should errors on meta-data setting be only
  256. logged, or abort the whole operation?
  257. :return new VM object
  258. '''
  259. if pool and pools:
  260. raise ValueError('only one of pool= and pools= can be used')
  261. if isinstance(src_vm, str):
  262. src_vm = self.domains[src_vm]
  263. if new_cls is None:
  264. new_cls = src_vm.klass
  265. template = getattr(src_vm, 'template', None)
  266. if template is not None:
  267. template = str(template)
  268. label = src_vm.label
  269. method_prefix = 'admin.vm.Create.'
  270. payload = 'name={} label={}'.format(new_name, label)
  271. if pool:
  272. payload += ' pool={}'.format(str(pool))
  273. method_prefix = 'admin.vm.CreateInPool.'
  274. if pools:
  275. payload += ''.join(' pool:{}={}'.format(vol, str(pool))
  276. for vol, pool in sorted(pools.items()))
  277. method_prefix = 'admin.vm.CreateInPool.'
  278. self.qubesd_call('dom0', method_prefix + new_cls, template,
  279. payload.encode('utf-8'))
  280. self.domains.clear_cache()
  281. dst_vm = self.domains[new_name]
  282. try:
  283. assert isinstance(dst_vm, qubesadmin.vm.QubesVM)
  284. for prop in src_vm.property_list():
  285. # handled by admin.vm.Create call
  286. if prop in ('name', 'qid', 'template', 'label', 'uuid', \
  287. 'installed_by_rpm'):
  288. continue
  289. if src_vm.property_is_default(prop):
  290. continue
  291. try:
  292. setattr(dst_vm, prop, getattr(src_vm, prop))
  293. except AttributeError:
  294. pass
  295. except qubesadmin.exc.QubesException as e:
  296. dst_vm.log.error(
  297. 'Failed to set {!s} property: {!s}'.format(prop, e))
  298. if not ignore_errors:
  299. raise
  300. for tag in src_vm.tags:
  301. if tag.startswith('created-by-'):
  302. continue
  303. try:
  304. dst_vm.tags.add(tag)
  305. except qubesadmin.exc.QubesException as e:
  306. dst_vm.log.error(
  307. 'Failed to add {!s} tag: {!s}'.format(tag, e))
  308. if not ignore_errors:
  309. raise
  310. for feature, value in src_vm.features.items():
  311. try:
  312. dst_vm.features[feature] = value
  313. except qubesadmin.exc.QubesException as e:
  314. dst_vm.log.error(
  315. 'Failed to set {!s} feature: {!s}'.format(feature, e))
  316. if not ignore_errors:
  317. raise
  318. try:
  319. dst_vm.firewall.save_rules(src_vm.firewall.rules)
  320. except qubesadmin.exc.QubesException as e:
  321. self.log.error('Failed to set firewall: %s', e)
  322. if not ignore_errors:
  323. raise
  324. except qubesadmin.exc.QubesException:
  325. if not ignore_errors:
  326. del self.domains[dst_vm.name]
  327. raise
  328. try:
  329. for dst_volume in sorted(dst_vm.volumes.values()):
  330. if not dst_volume.save_on_stop:
  331. # clone only persistent volumes
  332. continue
  333. src_volume = src_vm.volumes[dst_volume.name]
  334. dst_vm.log.info('Cloning {} volume'.format(dst_volume.name))
  335. dst_volume.clone(src_volume)
  336. except qubesadmin.exc.QubesException:
  337. del self.domains[dst_vm.name]
  338. raise
  339. return dst_vm
  340. def run_service(self, dest, service, filter_esc=False, user=None,
  341. localcmd=None, wait=True, **kwargs):
  342. '''Run qrexec service in a given destination
  343. *kwargs* are passed verbatim to :py:meth:`subprocess.Popen`.
  344. :param str dest: Destination - may be a VM name or empty
  345. string for default (for a given service)
  346. :param str service: service name
  347. :param bool filter_esc: filter escape sequences to protect terminal \
  348. emulator
  349. :param str user: username to run service as
  350. :param str localcmd: Command to connect stdin/stdout to
  351. :rtype: subprocess.Popen
  352. '''
  353. raise NotImplementedError
  354. class QubesLocal(QubesBase):
  355. '''Application object communicating through local socket.
  356. Used when running in dom0.
  357. '''
  358. qubesd_connection_type = 'socket'
  359. def qubesd_call(self, dest, method, arg=None, payload=None,
  360. payload_stream=None):
  361. '''
  362. Execute Admin API method.
  363. Only one of `payload` and `payload_stream` can be specified.
  364. :param dest: Destination VM name
  365. :param method: Full API method name ('admin...')
  366. :param arg: Method argument (if any)
  367. :param payload: Payload send to the method
  368. :param payload_stream: file-like object to read payload from
  369. :return: Data returned by qubesd (string)
  370. .. warning:: *payload_stream* will get closed by this function
  371. '''
  372. if payload and payload_stream:
  373. raise ValueError(
  374. 'Only one of payload and payload_stream can be used')
  375. if payload_stream:
  376. # payload_stream can be used for large amount of data,
  377. # so optimize for throughput, not latency: spawn actual qrexec
  378. # service implementation, which may use some optimization there (
  379. # see admin.vm.volume.Import - actual data handling is done with dd)
  380. method_path = os.path.join(
  381. qubesadmin.config.QREXEC_SERVICES_DIR, method)
  382. if not os.path.exists(method_path):
  383. raise qubesadmin.exc.QubesDaemonCommunicationError(
  384. '{} not found'.format(method_path))
  385. command = ['env', 'QREXEC_REMOTE_DOMAIN=dom0',
  386. 'QREXEC_REQUESTED_TARGET=' + dest, method_path, arg]
  387. if os.getuid() != 0:
  388. command.insert(0, 'sudo')
  389. proc = subprocess.Popen(command, stdin=payload_stream,
  390. stdout=subprocess.PIPE)
  391. payload_stream.close()
  392. (return_data, _) = proc.communicate()
  393. return self._parse_qubesd_response(return_data)
  394. try:
  395. client_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  396. client_socket.connect(qubesadmin.config.QUBESD_SOCKET)
  397. except (IOError, OSError) as e:
  398. raise qubesadmin.exc.QubesDaemonCommunicationError(
  399. 'Failed to connect to qubesd service: %s', str(e))
  400. # src, method, dest, arg
  401. for call_arg in ('dom0', method, dest, arg):
  402. if call_arg is not None:
  403. client_socket.sendall(call_arg.encode('ascii'))
  404. client_socket.sendall(b'\0')
  405. if payload is not None:
  406. client_socket.sendall(payload)
  407. client_socket.shutdown(socket.SHUT_WR)
  408. return_data = client_socket.makefile('rb').read()
  409. client_socket.close()
  410. return self._parse_qubesd_response(return_data)
  411. def run_service(self, dest, service, filter_esc=False, user=None,
  412. localcmd=None, wait=True, **kwargs):
  413. '''Run qrexec service in a given destination
  414. :param str dest: Destination - may be a VM name or empty
  415. string for default (for a given service)
  416. :param str service: service name
  417. :param bool filter_esc: filter escape sequences to protect terminal \
  418. emulator
  419. :param str user: username to run service as
  420. :param str localcmd: Command to connect stdin/stdout to
  421. :param bool wait: wait for remote process to finish
  422. :param int connect_timeout: qrexec client connection timeout
  423. :rtype: subprocess.Popen
  424. '''
  425. if not dest:
  426. raise ValueError('Empty destination name allowed only from a VM')
  427. if not wait and localcmd:
  428. raise ValueError('wait=False incompatible with localcmd')
  429. try:
  430. self.qubesd_call(dest, 'admin.vm.Start')
  431. except qubesadmin.exc.QubesVMNotHaltedError:
  432. pass
  433. qrexec_opts = ['-d', dest]
  434. if filter_esc:
  435. qrexec_opts.extend(['-t'])
  436. if filter_esc or os.isatty(sys.stderr.fileno()):
  437. qrexec_opts.extend(['-T'])
  438. if localcmd:
  439. qrexec_opts.extend(['-l', localcmd])
  440. if user is None:
  441. user = 'DEFAULT'
  442. if not wait:
  443. qrexec_opts.extend(['-e'])
  444. if 'connect_timeout' in kwargs:
  445. qrexec_opts.extend(['-w', str(kwargs.pop('connect_timeout'))])
  446. kwargs.setdefault('stdin', subprocess.PIPE)
  447. kwargs.setdefault('stdout', subprocess.PIPE)
  448. kwargs.setdefault('stderr', subprocess.PIPE)
  449. proc = subprocess.Popen([qubesadmin.config.QREXEC_CLIENT] +
  450. qrexec_opts + ['{}:QUBESRPC {} dom0'.format(user, service)],
  451. **kwargs)
  452. return proc
  453. class QubesRemote(QubesBase):
  454. '''Application object communicating through qrexec services.
  455. Used when running in VM.
  456. '''
  457. qubesd_connection_type = 'qrexec'
  458. def qubesd_call(self, dest, method, arg=None, payload=None,
  459. payload_stream=None):
  460. '''
  461. Execute Admin API method.
  462. Only one of `payload` and `payload_stream` can be specified.
  463. :param dest: Destination VM name
  464. :param method: Full API method name ('admin...')
  465. :param arg: Method argument (if any)
  466. :param payload: Payload send to the method
  467. :param payload_stream: file-like object to read payload from
  468. :return: Data returned by qubesd (string)
  469. .. warning:: *payload_stream* will get closed by this function
  470. '''
  471. if payload and payload_stream:
  472. raise ValueError(
  473. 'Only one of payload and payload_stream can be used')
  474. service_name = method
  475. if arg is not None:
  476. service_name += '+' + arg
  477. p = subprocess.Popen([qubesadmin.config.QREXEC_CLIENT_VM,
  478. dest, service_name],
  479. stdin=(payload_stream or subprocess.PIPE),
  480. stdout=subprocess.PIPE,
  481. stderr=subprocess.PIPE)
  482. if payload_stream is not None:
  483. payload_stream.close()
  484. (stdout, stderr) = p.communicate(payload)
  485. if p.returncode != 0:
  486. raise qubesadmin.exc.QubesDaemonNoResponseError(
  487. 'Service call error: %s', stderr.decode())
  488. return self._parse_qubesd_response(stdout)
  489. def run_service(self, dest, service, filter_esc=False, user=None,
  490. localcmd=None, wait=True, **kwargs):
  491. '''Run qrexec service in a given destination
  492. :param str dest: Destination - may be a VM name or empty
  493. string for default (for a given service)
  494. :param str service: service name
  495. :param bool filter_esc: filter escape sequences to protect terminal \
  496. emulator
  497. :param str user: username to run service as
  498. :param str localcmd: Command to connect stdin/stdout to
  499. :param bool wait: wait for process to finish
  500. :rtype: subprocess.Popen
  501. '''
  502. if filter_esc:
  503. raise NotImplementedError(
  504. 'filter_esc not implemented for calls from VM')
  505. if user:
  506. raise ValueError(
  507. 'non-default user not possible for calls from VM')
  508. if not wait and localcmd:
  509. raise ValueError('wait=False incompatible with localcmd')
  510. if not wait:
  511. # qrexec-client-vm can only request service calls, which are
  512. # started using MSG_EXEC_CMDLINE qrexec protocol message; this
  513. # message means "start the process, pipe its stdin/out/err,
  514. # and when it terminates, send exit code back".
  515. # According to the protocol qrexec-client-vm needs to wait for
  516. # MSG_DATA_EXIT_CODE, so implementing wait=False would require
  517. # some protocol change (or protocol violation).
  518. raise NotImplementedError(
  519. 'wait=False not implemented for calls from VM')
  520. kwargs.setdefault('stdin', subprocess.PIPE)
  521. kwargs.setdefault('stdout', subprocess.PIPE)
  522. kwargs.setdefault('stderr', subprocess.PIPE)
  523. proc = subprocess.Popen([qubesadmin.config.QREXEC_CLIENT_VM,
  524. dest or '', service] + (shlex.split(localcmd) if localcmd else []),
  525. **kwargs)
  526. return proc