OpenSecurity/bin/ui/format_drive_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Thu, 26 Jun 2014 12:14:08 +0200
changeset 208 973b5888eec6
parent 207 ae931a692b54
child 213 2e0b94e12bfc
permissions -rwxr-xr-x
password request loop done
oliver@205
     1
#!/bin/env python
oliver@205
     2
# -*- coding: utf-8 -*-
oliver@205
     3
oliver@205
     4
# ------------------------------------------------------------
oliver@207
     5
# format_drive_dialog.pyw
oliver@205
     6
# 
oliver@207
     7
# letting the user format a drive attached to a certain VM
oliver@205
     8
#
oliver@205
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
oliver@205
    10
#
oliver@205
    11
# Copyright (C) 2014 AIT Austrian Institute of Technology
oliver@205
    12
# AIT Austrian Institute of Technology GmbH
oliver@205
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
oliver@205
    14
# http://www.ait.ac.at
oliver@205
    15
#
oliver@205
    16
# This program is free software; you can redistribute it and/or
oliver@205
    17
# modify it under the terms of the GNU General Public License
oliver@205
    18
# as published by the Free Software Foundation version 2.
oliver@205
    19
# 
oliver@205
    20
# This program is distributed in the hope that it will be useful,
oliver@205
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
oliver@205
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
oliver@205
    23
# GNU General Public License for more details.
oliver@205
    24
# 
oliver@205
    25
# You should have received a copy of the GNU General Public License
oliver@205
    26
# along with this program; if not, write to the Free Software
oliver@205
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
oliver@205
    28
# Boston, MA  02110-1301, USA.
oliver@205
    29
# ------------------------------------------------------------
oliver@205
    30
oliver@205
    31
oliver@205
    32
# ------------------------------------------------------------
oliver@205
    33
# imports
oliver@205
    34
oliver@205
    35
import base64
oliver@205
    36
import sys
oliver@205
    37
oliver@205
    38
from PyQt4 import QtCore
oliver@205
    39
from PyQt4 import QtGui
oliver@205
    40
oliver@207
    41
from ui_FormatDriveDialog import Ui_FormatDriveDialog 
oliver@205
    42
from about_dialog import AboutDialog
oliver@205
    43
oliver@205
    44
oliver@205
    45
# ------------------------------------------------------------
oliver@205
    46
# code
oliver@205
    47
oliver@205
    48
oliver@207
    49
class FormatDriveDialog(QtGui.QDialog):
oliver@205
    50
oliver@205
    51
    """A dialog for letting the user pass on a password/keyfile combo"""
oliver@205
    52
oliver@207
    53
    def __init__(self, ip):
oliver@205
    54
oliver@205
    55
        QtGui.QDialog.__init__(self)
oliver@205
    56
oliver@207
    57
        user_text = """
oliver@207
    58
<b>Attention!</b><br/>
oliver@207
    59
You are going to wipe all data stored<br/>
oliver@207
    60
at device attached to IP <b>%s</b>.<br/>
oliver@207
    61
<br/>
oliver@207
    62
<b>This is irreversible.</b><br/>
oliver@207
    63
<br/>
oliver@207
    64
Please provide an approbitate password and keyfile to proceed:
oliver@207
    65
""" % ip
oliver@207
    66
oliver@205
    67
        # setup the user interface
oliver@207
    68
        self.ui = Ui_FormatDriveDialog()
oliver@205
    69
        self.ui.setupUi(self)
oliver@205
    70
    
oliver@205
    71
        # local members
oliver@205
    72
        self._about_dialog = AboutDialog()
oliver@205
    73
        self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
oliver@205
    74
        self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
oliver@205
    75
        self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
oliver@205
    76
oliver@205
    77
        # connectors
oliver@205
    78
        self.ui.lblText.setText(user_text)
oliver@205
    79
        self.ui.btnAbout.clicked.connect(self.clicked_about)
oliver@205
    80
        self.ui.btnCancel.clicked.connect(self.reject)
oliver@205
    81
        self.ui.btnOk.clicked.connect(self.clicked_ok)
oliver@205
    82
        self.ui.btnBrowse.clicked.connect(self.clicked_browse)
oliver@205
    83
oliver@205
    84
        # positionate ourself central
oliver@205
    85
        screen = QtGui.QDesktopWidget().screenGeometry()
oliver@205
    86
        size = self.geometry()
oliver@205
    87
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
oliver@205
    88
 
oliver@205
    89
oliver@205
    90
    def clicked_about(self):
oliver@205
    91
oliver@205
    92
        """About button has been clicked."""
oliver@205
    93
        self._about_dialog.show()
oliver@205
    94
oliver@205
    95
oliver@205
    96
    def clicked_browse(self):
oliver@205
    97
oliver@205
    98
        """Browse button has been clicked."""
oliver@205
    99
        if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
oliver@205
   100
            self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
oliver@205
   101
oliver@205
   102
oliver@205
   103
    def clicked_ok(self):
oliver@205
   104
        
oliver@205
   105
        """Ok button has been clicked."""
oliver@205
   106
oliver@207
   107
        init_data = {}
oliver@207
   108
        
oliver@207
   109
        # pick the password
oliver@207
   110
        init_data['password'] = self.ui.edtPassword.text()
oliver@208
   111
        if len(init_data['password']) == 0:
oliver@208
   112
            QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
oliver@208
   113
            return
oliver@207
   114
oliver@205
   115
        # read the content of the keyfile
oliver@205
   116
        keyfile_content = ''
oliver@205
   117
        try:
oliver@207
   118
            if len(self.ui.edtKeyfile.text()) > 0:
oliver@207
   119
                keyfile = open(self.ui.edtKeyfile.text(), 'r')
oliver@207
   120
                keyfile_content = keyfile.read()
oliver@205
   121
        except Exception as e:
oliver@205
   122
            sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
oliver@205
   123
            QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
oliver@205
   124
            return
oliver@205
   125
oliver@205
   126
        # turn into Base64
oliver@207
   127
        if len(keyfile_content) > 0:
oliver@207
   128
            keyfile_content_base64 = base64.b64encode(keyfile_content)
oliver@207
   129
            init_data['keyfile'] = keyfile_content_base64
oliver@205
   130
oliver@207
   131
        try:
oliver@207
   132
            req = urllib2.Request('http://' + ip + ':58081/init', urllib.urlencode(init_data))
oliver@207
   133
            res = urllib2.urlopen(req)
oliver@207
   134
        except:
oliver@207
   135
            print('EXCEPTION')
oliver@207
   136
            pass
oliver@207
   137
oliver@205
   138
        self.accept()
oliver@205
   139
oliver@205
   140
oliver@205
   141
    def set_user_text(user_text):
oliver@205
   142
oliver@205
   143
        """Set a text to explain which password we need."""
oliver@205
   144
        self.ui.lblText.setText(user_text)
oliver@205
   145
oliver@205
   146
oliver@205
   147
if __name__ == "__main__":
oliver@207
   148
    
oliver@207
   149
    print(sys.argv)
oliver@207
   150
    
oliver@207
   151
    ip = None
oliver@207
   152
    try:
oliver@207
   153
        ip = sys.argv[-1:][0]
oliver@207
   154
    except:
oliver@207
   155
        pass
oliver@207
   156
    
oliver@205
   157
    a = QtGui.QApplication(sys.argv)
oliver@207
   158
    d = FormatDriveDialog(ip)
oliver@205
   159
    d.show()
oliver@205
   160
    sys.exit(a.exec_())     
oliver@205
   161