2013-01-23 15:55:14 +01:00
|
|
|
#!/usr/bin/python2
|
2012-02-22 06:09:25 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
2017-11-06 21:06:30 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2012-02-22 06:09:25 +01:00
|
|
|
#
|
|
|
|
#
|
2014-01-13 05:20:54 +01:00
|
|
|
import re
|
2018-03-15 00:18:43 +01:00
|
|
|
import socket
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
from PyQt5 import QtWidgets # pylint: disable=import-error
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
import subprocess
|
2017-12-10 21:14:14 +01:00
|
|
|
from . import utils
|
|
|
|
import yaml
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2014-01-13 05:20:54 +01:00
|
|
|
path_re = re.compile(r"[a-zA-Z0-9/:.,_+=() -]*")
|
|
|
|
path_max_len = 512
|
2014-01-12 05:42:19 +01:00
|
|
|
|
2017-12-10 21:14:14 +01:00
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
def fill_appvms_list(dialog):
|
2018-01-06 02:08:34 +01:00
|
|
|
"""
|
|
|
|
Helper function, designed to fill the destination vm combobox in both backup
|
|
|
|
and restore GUI tools.
|
|
|
|
:param dialog: QtGui.QWizard with a combobox called appvm_combobox
|
|
|
|
"""
|
2013-09-27 09:19:38 +02:00
|
|
|
dialog.appvm_combobox.clear()
|
2013-11-28 03:52:02 +01:00
|
|
|
dialog.appvm_combobox.addItem("dom0")
|
2013-09-27 09:19:38 +02:00
|
|
|
|
2017-12-10 21:14:14 +01:00
|
|
|
dialog.appvm_combobox.setCurrentIndex(0) # current selected is null ""
|
2013-11-28 03:45:22 +01:00
|
|
|
|
2018-01-06 02:08:34 +01:00
|
|
|
for vm in dialog.qubes_app.domains:
|
2020-08-04 22:50:20 +02:00
|
|
|
if utils.get_feature(vm, 'internal', False) or vm.klass == 'TemplateVM':
|
2013-09-27 09:19:38 +02:00
|
|
|
continue
|
|
|
|
|
2020-08-11 01:08:48 +02:00
|
|
|
if utils.is_running(vm, False) and vm.klass != 'AdminVM':
|
2013-09-27 09:19:38 +02:00
|
|
|
dialog.appvm_combobox.addItem(vm.name)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2017-12-10 21:14:14 +01:00
|
|
|
|
2012-02-22 06:09:25 +01:00
|
|
|
def enable_dir_line_edit(dialog, boolean):
|
|
|
|
dialog.dir_line_edit.setEnabled(boolean)
|
2013-11-28 03:45:22 +01:00
|
|
|
dialog.select_path_button.setEnabled(boolean)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2017-12-10 21:14:14 +01:00
|
|
|
|
2017-12-11 02:04:06 +01:00
|
|
|
def select_path_button_clicked(dialog, select_file=False, read_only=False):
|
2018-01-06 02:08:34 +01:00
|
|
|
"""
|
|
|
|
Helper function that displays a file/directory selection wizard. Used by
|
|
|
|
backup and restore GUI tools.
|
|
|
|
:param dialog: QtGui.QWizard with a dir_line_edit text box that wants to
|
|
|
|
receive a file/directory path and appvm_combobox with VM to use
|
|
|
|
:param select_file: True: select file dialog; False: select directory
|
|
|
|
dialog
|
|
|
|
:param read_only: should the dir_line_edit be changed after selecting a file
|
|
|
|
or directory
|
|
|
|
:return:
|
|
|
|
"""
|
2014-01-11 22:57:34 +01:00
|
|
|
backup_location = str(dialog.dir_line_edit.text())
|
2012-03-02 02:50:12 +01:00
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
new_path = None
|
2017-12-10 21:14:14 +01:00
|
|
|
|
|
|
|
new_appvm = str(dialog.appvm_combobox.currentText())
|
2018-01-06 02:08:34 +01:00
|
|
|
vm = dialog.qubes_app.domains[new_appvm]
|
2017-12-10 21:14:14 +01:00
|
|
|
try:
|
2018-03-15 00:18:43 +01:00
|
|
|
if vm.name == socket.gethostname():
|
2019-05-22 23:10:09 +02:00
|
|
|
file_dialog = QtWidgets.QFileDialog()
|
2018-03-15 00:18:43 +01:00
|
|
|
|
|
|
|
if select_file:
|
|
|
|
file_dialog_function = file_dialog.getOpenFileName
|
|
|
|
else:
|
|
|
|
file_dialog_function = file_dialog.getExistingDirectory
|
2019-10-29 00:15:29 +01:00
|
|
|
result = file_dialog_function(
|
2018-03-15 00:18:43 +01:00
|
|
|
dialog,
|
|
|
|
dialog.tr("Select backup location."),
|
|
|
|
backup_location if backup_location else '/')
|
2019-10-29 00:15:29 +01:00
|
|
|
if isinstance(result, tuple):
|
|
|
|
new_path = result[0]
|
|
|
|
else:
|
|
|
|
new_path = result
|
2018-03-15 00:18:43 +01:00
|
|
|
else:
|
2019-10-29 00:15:29 +01:00
|
|
|
try:
|
|
|
|
new_path = utils.get_path_from_vm(
|
|
|
|
vm,
|
|
|
|
"qubes.SelectFile" if select_file
|
|
|
|
else "qubes.SelectDirectory")
|
|
|
|
except ValueError:
|
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
dialog,
|
|
|
|
dialog.tr("Unexpected characters in path!"),
|
|
|
|
dialog.tr("Backup path can only contain the following "
|
|
|
|
"special characters: /:.,_+=() -"))
|
2020-08-23 02:53:36 +02:00
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
2020-08-04 22:50:20 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
dialog,
|
|
|
|
dialog.tr("Failed to select path!"),
|
|
|
|
dialog.tr("Error {} occurred.".format(str(ex))))
|
2019-10-29 00:15:29 +01:00
|
|
|
|
2017-12-11 01:09:29 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2017-12-11 02:04:06 +01:00
|
|
|
if not read_only:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
dialog,
|
2017-12-11 02:04:06 +01:00
|
|
|
dialog.tr("Nothing selected!"),
|
|
|
|
dialog.tr("No file or directory selected."))
|
|
|
|
else:
|
|
|
|
return
|
2017-12-10 21:14:14 +01:00
|
|
|
|
2017-12-11 02:04:06 +01:00
|
|
|
if new_path and not read_only:
|
2017-12-10 21:14:14 +01:00
|
|
|
dialog.dir_line_edit.setText(new_path)
|
|
|
|
|
2017-12-11 02:04:06 +01:00
|
|
|
if new_path and backup_location and not read_only:
|
2019-05-22 23:10:09 +02:00
|
|
|
dialog.select_dir_page.completeChanged.emit()
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
|
2018-01-06 02:08:34 +01:00
|
|
|
def get_profile_name(use_temp):
|
|
|
|
backup_profile_name = 'qubes-manager-backup'
|
|
|
|
temp_backup_profile_name = 'qubes-manager-backup-tmp'
|
|
|
|
|
|
|
|
return temp_backup_profile_name if use_temp else backup_profile_name
|
|
|
|
|
|
|
|
|
|
|
|
def get_profile_path(use_temp):
|
|
|
|
path = '/etc/qubes/backup/' + get_profile_name(use_temp) + '.conf'
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2017-12-10 23:17:39 +01:00
|
|
|
def load_backup_profile(use_temp=False):
|
|
|
|
|
|
|
|
path = get_profile_path(use_temp)
|
|
|
|
|
|
|
|
with open(path) as profile_file:
|
2017-12-10 21:14:14 +01:00
|
|
|
profile_data = yaml.safe_load(profile_file)
|
|
|
|
return profile_data
|
|
|
|
|
|
|
|
|
2017-12-10 23:17:39 +01:00
|
|
|
def write_backup_profile(args, use_temp=False):
|
2017-12-10 21:14:14 +01:00
|
|
|
|
|
|
|
acceptable_fields = ['include', 'passphrase_text', 'compression',
|
|
|
|
'destination_vm', 'destination_path']
|
|
|
|
|
|
|
|
profile_data = {key: value for key, value in args.items()
|
|
|
|
if key in acceptable_fields}
|
|
|
|
|
2017-12-10 23:17:39 +01:00
|
|
|
path = get_profile_path(use_temp)
|
|
|
|
|
|
|
|
with open(path, 'w') as profile_file:
|
2017-12-10 21:14:14 +01:00
|
|
|
yaml.safe_dump(profile_data, profile_file)
|