global_settings.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (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 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. #
  22. import os
  23. import subprocess
  24. from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error
  25. from qubesadmin.utils import parse_size
  26. from . import ui_globalsettingsdlg # pylint: disable=no-name-in-module
  27. from . import utils
  28. from configparser import ConfigParser
  29. qmemman_config_path = '/etc/qubes/qmemman.conf'
  30. def _run_qrexec_repo(service, arg=''):
  31. # Set default locale to C in order to prevent error msg
  32. # in subprocess call related to falling back to C locale
  33. env = os.environ.copy()
  34. env['LC_ALL'] = 'C'
  35. # Fake up a "qrexec call" to dom0 because dom0 can't qrexec to itself yet
  36. cmd = '/etc/qubes-rpc/' + service
  37. p = subprocess.run(
  38. ['sudo', cmd, arg],
  39. stdout=subprocess.PIPE,
  40. stderr=subprocess.PIPE,
  41. check=False,
  42. env=env
  43. )
  44. if p.stderr:
  45. raise RuntimeError(
  46. QtCore.QCoreApplication.translate(
  47. "GlobalSettings", 'qrexec call stderr was not empty'),
  48. {'stderr': p.stderr.decode('utf-8')})
  49. if p.returncode != 0:
  50. raise RuntimeError(
  51. QtCore.QCoreApplication.translate(
  52. "GlobalSettings",
  53. 'qrexec call exited with non-zero return code'),
  54. {'returncode': p.returncode})
  55. return p.stdout.decode('utf-8')
  56. class GlobalSettingsWindow(ui_globalsettingsdlg.Ui_GlobalSettings,
  57. QtWidgets.QDialog):
  58. def __init__(self, app, qubes_app, parent=None):
  59. super(GlobalSettingsWindow, self).__init__(parent)
  60. self.app = app
  61. self.qubes_app = qubes_app
  62. self.vm = self.qubes_app.domains[self.qubes_app.local_name]
  63. self.setupUi(self)
  64. self.buttonBox.accepted.connect(self.save_and_apply)
  65. self.buttonBox.rejected.connect(self.reject)
  66. self.__init_system_defaults__()
  67. self.__init_kernel_defaults__()
  68. self.__init_mem_defaults__()
  69. self.__init_updates__()
  70. self.__init_gui_defaults()
  71. def setup_application(self):
  72. self.app.setApplicationName(self.tr("Qubes Global Settings"))
  73. self.app.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
  74. def __init_system_defaults__(self):
  75. # set up updatevm choice
  76. utils.initialize_widget_with_vms(
  77. widget=self.update_vm_combo,
  78. qubes_app=self.qubes_app,
  79. filter_function=(lambda vm: vm.klass != 'TemplateVM'),
  80. allow_none=True,
  81. holder=self.qubes_app,
  82. property_name="updatevm"
  83. )
  84. # set up clockvm choice
  85. utils.initialize_widget_with_vms(
  86. widget=self.clock_vm_combo,
  87. qubes_app=self.qubes_app,
  88. filter_function=(lambda vm: vm.klass != 'TemplateVM'),
  89. allow_none=True,
  90. holder=self.qubes_app,
  91. property_name="clockvm"
  92. )
  93. # set up default netvm
  94. utils.initialize_widget_with_vms(
  95. widget=self.default_netvm_combo,
  96. qubes_app=self.qubes_app,
  97. filter_function=(lambda vm: vm.provides_network),
  98. allow_none=True,
  99. holder=self.qubes_app,
  100. property_name="default_netvm"
  101. )
  102. # default template
  103. utils.initialize_widget_with_vms(
  104. widget=self.default_template_combo,
  105. qubes_app=self.qubes_app,
  106. filter_function=(lambda vm: vm.klass == 'TemplateVM'),
  107. allow_none=True,
  108. holder=self.qubes_app,
  109. property_name="default_template"
  110. )
  111. # default dispvm
  112. utils.initialize_widget_with_vms(
  113. widget=self.default_dispvm_combo,
  114. qubes_app=self.qubes_app,
  115. filter_function=(lambda vm: getattr(
  116. vm, 'template_for_dispvms', False)),
  117. allow_none=True,
  118. holder=self.qubes_app,
  119. property_name="default_dispvm"
  120. )
  121. def __apply_system_defaults__(self):
  122. # updatevm
  123. if utils.did_widget_selection_change(self.update_vm_combo):
  124. self.qubes_app.updatevm = self.update_vm_combo.currentData()
  125. # clockvm
  126. if utils.did_widget_selection_change(self.clock_vm_combo):
  127. self.qubes_app.clockvm = self.clock_vm_combo.currentData()
  128. # default netvm
  129. if utils.did_widget_selection_change(self.default_netvm_combo):
  130. self.qubes_app.default_netvm = \
  131. self.default_netvm_combo.currentData()
  132. # default template
  133. if utils.did_widget_selection_change(self.default_template_combo):
  134. self.qubes_app.default_template = \
  135. self.default_template_combo.currentData()
  136. # default_dispvm
  137. if utils.did_widget_selection_change(self.default_dispvm_combo):
  138. self.qubes_app.default_dispvm = \
  139. self.default_dispvm_combo.currentData()
  140. def __init_kernel_defaults__(self):
  141. utils.initialize_widget_with_kernels(
  142. widget=self.default_kernel_combo,
  143. qubes_app=self.qubes_app,
  144. allow_none=True,
  145. holder=self.qubes_app,
  146. property_name='default_kernel')
  147. def __apply_kernel_defaults__(self):
  148. if utils.did_widget_selection_change(self.default_kernel_combo):
  149. self.qubes_app.default_kernel = \
  150. self.default_kernel_combo.currentData()
  151. def __init_gui_defaults(self):
  152. utils.initialize_widget(
  153. widget=self.allow_fullscreen,
  154. choices=[
  155. ('default (disallow)', None),
  156. ('allow', True),
  157. ('disallow', False)
  158. ],
  159. selected_value=utils.get_boolean_feature(
  160. self.vm,
  161. 'gui-default-allow-fullscreen'))
  162. utils.initialize_widget(
  163. widget=self.allow_utf8,
  164. choices=[
  165. ('default (disallow)', None),
  166. ('allow', True),
  167. ('disallow', False)
  168. ],
  169. selected_value=utils.get_boolean_feature(
  170. self.vm,
  171. 'gui-default-allow-utf8-titles'))
  172. utils.initialize_widget(
  173. widget=self.trayicon,
  174. choices=[
  175. ('default (thin border)', None),
  176. ('full background', 'bg'),
  177. ('thin border', 'border1'),
  178. ('thick border', 'border2'),
  179. ('tinted icon', 'tint'),
  180. ('tinted icon with modified white', 'tint+whitehack'),
  181. ('tinted icon with 50% saturation', 'tint+saturation50')
  182. ],
  183. selected_value=self.vm.features.get('gui-default-trayicon-mode',
  184. None))
  185. utils.initialize_widget(
  186. widget=self.securecopy,
  187. choices=[
  188. ('default (Ctrl+Shift+C)', None),
  189. ('Ctrl+Shift+C', 'Ctrl-Shift-c'),
  190. ('Ctrl+Win+C', 'Ctrl-Mod4-c'),
  191. ],
  192. selected_value=self.vm.features.get(
  193. 'gui-default-secure-copy-sequence', None))
  194. utils.initialize_widget(
  195. widget=self.securepaste,
  196. choices=[
  197. ('default (Ctrl+Shift+V)', None),
  198. ('Ctrl+Shift+V', 'Ctrl-Shift-V'),
  199. ('Ctrl+Win+V', 'Ctrl-Mod4-v'),
  200. ('Ctrl+Insert', 'Ctrl-Ins'),
  201. ],
  202. selected_value=self.vm.features.get(
  203. 'gui-default-secure-paste-sequence', None))
  204. def __apply_feature_change(self, widget, feature):
  205. if utils.did_widget_selection_change(widget):
  206. if widget.currentData() is None:
  207. del self.vm.features[feature]
  208. else:
  209. self.vm.features[feature] = widget.currentData()
  210. def __apply_gui_defaults(self):
  211. self.__apply_feature_change(widget=self.allow_fullscreen,
  212. feature='gui-default-allow-fullscreen')
  213. self.__apply_feature_change(widget=self.allow_utf8,
  214. feature='gui-default-allow-utf8-titles')
  215. self.__apply_feature_change(widget=self.trayicon,
  216. feature='gui-default-trayicon-mode')
  217. self.__apply_feature_change(widget=self.securecopy,
  218. feature='gui-default-secure-copy-sequence')
  219. self.__apply_feature_change(widget=self.securepaste,
  220. feature='gui-default-secure-paste-sequence')
  221. def __init_mem_defaults__(self):
  222. # qmemman settings
  223. self.qmemman_config = ConfigParser()
  224. self.vm_min_mem_val = '200MiB' # str(qmemman_algo.MIN_PREFMEM)
  225. self.dom0_mem_boost_val = '350MiB' # str(qmemman_algo.DOM0_MEM_BOOST)
  226. self.qmemman_config.read(qmemman_config_path)
  227. if self.qmemman_config.has_section('global'):
  228. self.vm_min_mem_val = \
  229. self.qmemman_config.get('global', 'vm-min-mem')
  230. self.dom0_mem_boost_val = \
  231. self.qmemman_config.get('global', 'dom0-mem-boost')
  232. self.vm_min_mem_val = parse_size(self.vm_min_mem_val)
  233. self.dom0_mem_boost_val = parse_size(self.dom0_mem_boost_val)
  234. self.min_vm_mem.setValue(int(self.vm_min_mem_val / 1024 / 1024))
  235. self.dom0_mem_boost.setValue(int(self.dom0_mem_boost_val / 1024 / 1024))
  236. def __apply_mem_defaults__(self):
  237. # qmemman settings
  238. current_min_vm_mem = self.min_vm_mem.value()
  239. current_dom0_mem_boost = self.dom0_mem_boost.value()
  240. if current_min_vm_mem * 1024 * 1024 != self.vm_min_mem_val or \
  241. current_dom0_mem_boost * 1024 * 1024 != self.dom0_mem_boost_val:
  242. current_min_vm_mem = str(current_min_vm_mem) + 'MiB'
  243. current_dom0_mem_boost = str(current_dom0_mem_boost) + 'MiB'
  244. if not self.qmemman_config.has_section('global'):
  245. # add the whole section
  246. self.qmemman_config.add_section('global')
  247. self.qmemman_config.set(
  248. 'global', 'vm-min-mem', current_min_vm_mem)
  249. self.qmemman_config.set(
  250. 'global', 'dom0-mem-boost', current_dom0_mem_boost)
  251. self.qmemman_config.set(
  252. 'global', 'cache-margin-factor', str(1.3))
  253. # removed qmemman_algo.CACHE_FACTOR
  254. qmemman_config_file = open(qmemman_config_path, 'a')
  255. self.qmemman_config.write(qmemman_config_file)
  256. qmemman_config_file.close()
  257. else:
  258. # If there already is a 'global' section, we don't use
  259. # SafeConfigParser.write() - it would get rid of
  260. # all the comments...
  261. lines_to_add = {}
  262. lines_to_add['vm-min-mem'] = \
  263. "vm-min-mem = " + current_min_vm_mem + "\n"
  264. lines_to_add['dom0-mem-boost'] = \
  265. "dom0-mem-boost = " + current_dom0_mem_boost + "\n"
  266. config_lines = []
  267. qmemman_config_file = open(qmemman_config_path, 'r')
  268. for line in qmemman_config_file:
  269. if line.strip().startswith('vm-min-mem'):
  270. config_lines.append(lines_to_add['vm-min-mem'])
  271. del lines_to_add['vm-min-mem']
  272. elif line.strip().startswith('dom0-mem-boost'):
  273. config_lines.append(lines_to_add['dom0-mem-boost'])
  274. del lines_to_add['dom0-mem-boost']
  275. else:
  276. config_lines.append(line)
  277. qmemman_config_file.close()
  278. for line in lines_to_add:
  279. config_lines.append(line)
  280. qmemman_config_file = open(qmemman_config_path, 'w')
  281. qmemman_config_file.writelines(config_lines)
  282. qmemman_config_file.close()
  283. def __init_updates__(self):
  284. self.updates_dom0_val = bool(
  285. self.qubes_app.domains['dom0'].features.get(
  286. 'service.qubes-update-check', True))
  287. self.updates_dom0.setChecked(self.updates_dom0_val)
  288. self.updates_vm.setChecked(self.qubes_app.check_updates_vm)
  289. self.enable_updates_all.clicked.connect(self.__enable_updates_all)
  290. self.disable_updates_all.clicked.connect(self.__disable_updates_all)
  291. self.repos = repos = dict()
  292. for i in _run_qrexec_repo('qubes.repos.List').split('\n'):
  293. lst = i.split('\0')
  294. # Keyed by repo name
  295. dct = repos[lst[0]] = dict()
  296. dct['prettyname'] = lst[1]
  297. dct['enabled'] = lst[2] == 'enabled'
  298. if repos['qubes-dom0-unstable']['enabled']:
  299. self.dom0_updates_repo.setCurrentIndex(3)
  300. elif repos['qubes-dom0-current-testing']['enabled']:
  301. self.dom0_updates_repo.setCurrentIndex(2)
  302. elif repos['qubes-dom0-security-testing']['enabled']:
  303. self.dom0_updates_repo.setCurrentIndex(1)
  304. elif repos['qubes-dom0-current']['enabled']:
  305. self.dom0_updates_repo.setCurrentIndex(0)
  306. else:
  307. raise Exception(
  308. self.tr('Cannot detect enabled dom0 update repositories'))
  309. if repos['qubes-templates-itl-testing']['enabled']:
  310. self.itl_tmpl_updates_repo.setCurrentIndex(1)
  311. elif repos['qubes-templates-itl']['enabled']:
  312. self.itl_tmpl_updates_repo.setCurrentIndex(0)
  313. else:
  314. raise Exception(self.tr('Cannot detect enabled ITL template update '
  315. 'repositories'))
  316. if repos['qubes-templates-community-testing']['enabled']:
  317. self.comm_tmpl_updates_repo.setCurrentIndex(2)
  318. elif repos['qubes-templates-community']['enabled']:
  319. self.comm_tmpl_updates_repo.setCurrentIndex(1)
  320. else:
  321. self.comm_tmpl_updates_repo.setCurrentIndex(0)
  322. def __enable_updates_all(self):
  323. reply = QtWidgets.QMessageBox.question(
  324. self, self.tr("Change state of all qubes"),
  325. self.tr("Are you sure you want to set all qubes to check "
  326. "for updates?"),
  327. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  328. if reply == QtWidgets.QMessageBox.Cancel:
  329. return
  330. self.__set_updates_all(True)
  331. def __disable_updates_all(self):
  332. reply = QtWidgets.QMessageBox.question(
  333. self, self.tr("Change state of all qubes"),
  334. self.tr("Are you sure you want to set all qubes to not check "
  335. "for updates?"),
  336. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  337. if reply == QtWidgets.QMessageBox.Cancel:
  338. return
  339. self.__set_updates_all(False)
  340. def __set_updates_all(self, state):
  341. for vm in self.qubes_app.domains:
  342. if vm.klass != "AdminVM":
  343. vm.features['service.qubes-update-check'] = state
  344. def __apply_updates__(self):
  345. if self.updates_dom0.isChecked() != self.updates_dom0_val:
  346. self.qubes_app.domains['dom0'].features[
  347. 'service.qubes-update-check'] = \
  348. self.updates_dom0.isChecked()
  349. if self.qubes_app.check_updates_vm != self.updates_vm.isChecked():
  350. self.qubes_app.check_updates_vm = self.updates_vm.isChecked()
  351. def _manage_repos(self, repolist, action):
  352. for name in repolist:
  353. if self.repos[name]['enabled'] and action == 'Enable' or \
  354. not self.repos[name]['enabled'] and action == 'Disable':
  355. continue
  356. try:
  357. result = _run_qrexec_repo('qubes.repos.' + action, name)
  358. if result != 'ok\n':
  359. raise RuntimeError(
  360. self.tr('qrexec call stdout did not contain "ok"'
  361. ' as expected'),
  362. {'stdout': result})
  363. except RuntimeError as ex:
  364. msg = '{desc}; {args}'.format(desc=ex.args[0], args=', '.join(
  365. # This is kind of hard to mentally parse but really all
  366. # it does is pretty-print args[1], which is a dictionary
  367. ['{key}: {val}'.format(key=i[0], val=i[1]) for i in
  368. ex.args[1].items()]
  369. ))
  370. QtWidgets.QMessageBox.warning(
  371. None,
  372. self.tr("ERROR!"),
  373. self.tr("Error managing {repo} repository settings:"
  374. " {msg}".format(repo=name, msg=msg)))
  375. def _handle_dom0_updates_combobox(self, idx):
  376. idx += 1
  377. repolist = ['qubes-dom0-current', 'qubes-dom0-security-testing',
  378. 'qubes-dom0-current-testing', 'qubes-dom0-unstable']
  379. enable = repolist[:idx]
  380. disable = repolist[idx:]
  381. self._manage_repos(enable, 'Enable')
  382. self._manage_repos(disable, 'Disable')
  383. # pylint: disable=invalid-name
  384. def _handle_itl_tmpl_updates_combobox(self, idx):
  385. idx += 1
  386. repolist = ['qubes-templates-itl', 'qubes-templates-itl-testing']
  387. enable = repolist[:idx]
  388. disable = repolist[idx:]
  389. self._manage_repos(enable, 'Enable')
  390. self._manage_repos(disable, 'Disable')
  391. # pylint: disable=invalid-name
  392. def _handle_comm_tmpl_updates_combobox(self, idx):
  393. # We don't increment idx by 1 because this is the only combobox that
  394. # has an explicit "disable this repository entirely" option
  395. repolist = ['qubes-templates-community',
  396. 'qubes-templates-community-testing']
  397. enable = repolist[:idx]
  398. disable = repolist[idx:]
  399. self._manage_repos(enable, 'Enable')
  400. self._manage_repos(disable, 'Disable')
  401. def __apply_repos__(self):
  402. self._handle_dom0_updates_combobox(
  403. self.dom0_updates_repo.currentIndex())
  404. self._handle_itl_tmpl_updates_combobox(
  405. self.itl_tmpl_updates_repo.currentIndex())
  406. self._handle_comm_tmpl_updates_combobox(
  407. self.comm_tmpl_updates_repo.currentIndex())
  408. def reject(self):
  409. self.done(0)
  410. def save_and_apply(self):
  411. self.__apply_system_defaults__()
  412. self.__apply_kernel_defaults__()
  413. self.__apply_mem_defaults__()
  414. self.__apply_updates__()
  415. self.__apply_repos__()
  416. self.__apply_gui_defaults()
  417. def main():
  418. utils.run_synchronous(GlobalSettingsWindow)
  419. if __name__ == "__main__":
  420. main()