OpenSecurity/bin/ui/format_drive_dialog.py
author BarthaM@N3SIM1218.D03.arc.local
Fri, 18 Jul 2014 13:45:09 +0100
changeset 213 2e0b94e12bfc
parent 208 973b5888eec6
child 240 d7ef04254e9c
permissions -rwxr-xr-x
Addded http proxy server support.
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
BarthaM@213
    38
import urllib
BarthaM@213
    39
import urllib2
BarthaM@213
    40
oliver@205
    41
from PyQt4 import QtCore
oliver@205
    42
from PyQt4 import QtGui
oliver@205
    43
oliver@207
    44
from ui_FormatDriveDialog import Ui_FormatDriveDialog 
oliver@205
    45
from about_dialog import AboutDialog
oliver@205
    46
oliver@205
    47
# ------------------------------------------------------------
oliver@205
    48
# code
oliver@205
    49
oliver@205
    50
oliver@207
    51
class FormatDriveDialog(QtGui.QDialog):
oliver@205
    52
oliver@205
    53
    """A dialog for letting the user pass on a password/keyfile combo"""
oliver@205
    54
oliver@207
    55
    def __init__(self, ip):
oliver@205
    56
oliver@205
    57
        QtGui.QDialog.__init__(self)
oliver@205
    58
oliver@207
    59
        user_text = """
oliver@207
    60
<b>Attention!</b><br/>
oliver@207
    61
You are going to wipe all data stored<br/>
oliver@207
    62
at device attached to IP <b>%s</b>.<br/>
oliver@207
    63
<br/>
oliver@207
    64
<b>This is irreversible.</b><br/>
oliver@207
    65
<br/>
BarthaM@213
    66
Please provide an appropriate password or keyfile to proceed:
oliver@207
    67
""" % ip
oliver@207
    68
oliver@205
    69
        # setup the user interface
oliver@207
    70
        self.ui = Ui_FormatDriveDialog()
oliver@205
    71
        self.ui.setupUi(self)
oliver@205
    72
    
oliver@205
    73
        # local members
oliver@205
    74
        self._about_dialog = AboutDialog()
oliver@205
    75
        self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
oliver@205
    76
        self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
oliver@205
    77
        self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
oliver@205
    78
oliver@205
    79
        # connectors
oliver@205
    80
        self.ui.lblText.setText(user_text)
oliver@205
    81
        self.ui.btnAbout.clicked.connect(self.clicked_about)
oliver@205
    82
        self.ui.btnCancel.clicked.connect(self.reject)
oliver@205
    83
        self.ui.btnOk.clicked.connect(self.clicked_ok)
oliver@205
    84
        self.ui.btnBrowse.clicked.connect(self.clicked_browse)
oliver@205
    85
oliver@205
    86
        # positionate ourself central
oliver@205
    87
        screen = QtGui.QDesktopWidget().screenGeometry()
oliver@205
    88
        size = self.geometry()
oliver@205
    89
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
oliver@205
    90
 
oliver@205
    91
oliver@205
    92
    def clicked_about(self):
oliver@205
    93
oliver@205
    94
        """About button has been clicked."""
oliver@205
    95
        self._about_dialog.show()
oliver@205
    96
oliver@205
    97
oliver@205
    98
    def clicked_browse(self):
oliver@205
    99
oliver@205
   100
        """Browse button has been clicked."""
oliver@205
   101
        if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
oliver@205
   102
            self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
oliver@205
   103
oliver@205
   104
oliver@205
   105
    def clicked_ok(self):
oliver@205
   106
        
oliver@205
   107
        """Ok button has been clicked."""
oliver@205
   108
BarthaM@213
   109
        init_data = dict()
oliver@207
   110
        
oliver@207
   111
        # pick the password
BarthaM@213
   112
        init_data['password'] = str(self.ui.edtPassword.text())
oliver@208
   113
        if len(init_data['password']) == 0:
oliver@208
   114
            QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
oliver@208
   115
            return
oliver@207
   116
oliver@205
   117
        # read the content of the keyfile
oliver@205
   118
        keyfile_content = ''
oliver@205
   119
        try:
oliver@207
   120
            if len(self.ui.edtKeyfile.text()) > 0:
oliver@207
   121
                keyfile = open(self.ui.edtKeyfile.text(), 'r')
oliver@207
   122
                keyfile_content = keyfile.read()
oliver@205
   123
        except Exception as e:
oliver@205
   124
            sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
oliver@205
   125
            QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
oliver@205
   126
            return
oliver@205
   127
oliver@205
   128
        # turn into Base64
oliver@207
   129
        if len(keyfile_content) > 0:
oliver@207
   130
            keyfile_content_base64 = base64.b64encode(keyfile_content)
oliver@207
   131
            init_data['keyfile'] = keyfile_content_base64
oliver@205
   132
BarthaM@213
   133
        res = ""
oliver@207
   134
        try:
BarthaM@213
   135
            req_data = urllib.urlencode(init_data)
BarthaM@213
   136
            req = urllib2.Request('http://' + ip + ':58081/init', req_data)
oliver@207
   137
            res = urllib2.urlopen(req)
oliver@207
   138
        except:
BarthaM@213
   139
            print('EXCEPTION ' + res)
oliver@207
   140
            pass
oliver@207
   141
oliver@205
   142
        self.accept()
oliver@205
   143
oliver@205
   144
BarthaM@213
   145
    def set_user_text(self, user_text):
oliver@205
   146
oliver@205
   147
        """Set a text to explain which password we need."""
oliver@205
   148
        self.ui.lblText.setText(user_text)
oliver@205
   149
oliver@205
   150
oliver@205
   151
if __name__ == "__main__":
oliver@207
   152
    
oliver@207
   153
    print(sys.argv)
oliver@207
   154
    
oliver@207
   155
    ip = None
oliver@207
   156
    try:
oliver@207
   157
        ip = sys.argv[-1:][0]
oliver@207
   158
    except:
oliver@207
   159
        pass
oliver@207
   160
    
oliver@205
   161
    a = QtGui.QApplication(sys.argv)
oliver@207
   162
    d = FormatDriveDialog(ip)
oliver@205
   163
    d.show()
oliver@205
   164
    sys.exit(a.exec_())     
oliver@205
   165