OpenSecurity/bin/ui/format_drive_dialog.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 213 2e0b94e12bfc
permissions -rwxr-xr-x
lizenz fixed in all files
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@240
    11
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
oliver@205
    12
# 
oliver@205
    13
# 
oliver@240
    14
#     X-Net Services GmbH
oliver@240
    15
#     Elisabethstrasse 1
oliver@240
    16
#     4020 Linz
oliver@240
    17
#     AUSTRIA
oliver@240
    18
#     https://www.x-net.at
oliver@240
    19
# 
oliver@240
    20
#     AIT Austrian Institute of Technology
oliver@240
    21
#     Donau City Strasse 1
oliver@240
    22
#     1220 Wien
oliver@240
    23
#     AUSTRIA
oliver@240
    24
#     http://www.ait.ac.at
oliver@240
    25
# 
oliver@240
    26
# 
oliver@240
    27
# Licensed under the Apache License, Version 2.0 (the "License");
oliver@240
    28
# you may not use this file except in compliance with the License.
oliver@240
    29
# You may obtain a copy of the License at
oliver@240
    30
# 
oliver@240
    31
#    http://www.apache.org/licenses/LICENSE-2.0
oliver@240
    32
# 
oliver@240
    33
# Unless required by applicable law or agreed to in writing, software
oliver@240
    34
# distributed under the License is distributed on an "AS IS" BASIS,
oliver@240
    35
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
oliver@240
    36
# See the License for the specific language governing permissions and
oliver@240
    37
# limitations under the License.
oliver@205
    38
# ------------------------------------------------------------
oliver@205
    39
oliver@205
    40
oliver@205
    41
# ------------------------------------------------------------
oliver@205
    42
# imports
oliver@205
    43
oliver@205
    44
import base64
oliver@205
    45
import sys
oliver@205
    46
BarthaM@213
    47
import urllib
BarthaM@213
    48
import urllib2
BarthaM@213
    49
oliver@205
    50
from PyQt4 import QtCore
oliver@205
    51
from PyQt4 import QtGui
oliver@205
    52
oliver@207
    53
from ui_FormatDriveDialog import Ui_FormatDriveDialog 
oliver@205
    54
from about_dialog import AboutDialog
oliver@205
    55
oliver@205
    56
# ------------------------------------------------------------
oliver@205
    57
# code
oliver@205
    58
oliver@205
    59
oliver@207
    60
class FormatDriveDialog(QtGui.QDialog):
oliver@205
    61
oliver@205
    62
    """A dialog for letting the user pass on a password/keyfile combo"""
oliver@205
    63
oliver@207
    64
    def __init__(self, ip):
oliver@205
    65
oliver@205
    66
        QtGui.QDialog.__init__(self)
oliver@205
    67
oliver@207
    68
        user_text = """
oliver@207
    69
<b>Attention!</b><br/>
oliver@207
    70
You are going to wipe all data stored<br/>
oliver@207
    71
at device attached to IP <b>%s</b>.<br/>
oliver@207
    72
<br/>
oliver@207
    73
<b>This is irreversible.</b><br/>
oliver@207
    74
<br/>
BarthaM@213
    75
Please provide an appropriate password or keyfile to proceed:
oliver@207
    76
""" % ip
oliver@207
    77
oliver@205
    78
        # setup the user interface
oliver@207
    79
        self.ui = Ui_FormatDriveDialog()
oliver@205
    80
        self.ui.setupUi(self)
oliver@205
    81
    
oliver@205
    82
        # local members
oliver@205
    83
        self._about_dialog = AboutDialog()
oliver@205
    84
        self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
oliver@205
    85
        self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
oliver@205
    86
        self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
oliver@205
    87
oliver@205
    88
        # connectors
oliver@205
    89
        self.ui.lblText.setText(user_text)
oliver@205
    90
        self.ui.btnAbout.clicked.connect(self.clicked_about)
oliver@205
    91
        self.ui.btnCancel.clicked.connect(self.reject)
oliver@205
    92
        self.ui.btnOk.clicked.connect(self.clicked_ok)
oliver@205
    93
        self.ui.btnBrowse.clicked.connect(self.clicked_browse)
oliver@205
    94
oliver@205
    95
        # positionate ourself central
oliver@205
    96
        screen = QtGui.QDesktopWidget().screenGeometry()
oliver@205
    97
        size = self.geometry()
oliver@205
    98
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
oliver@205
    99
 
oliver@205
   100
oliver@205
   101
    def clicked_about(self):
oliver@205
   102
oliver@205
   103
        """About button has been clicked."""
oliver@205
   104
        self._about_dialog.show()
oliver@205
   105
oliver@205
   106
oliver@205
   107
    def clicked_browse(self):
oliver@205
   108
oliver@205
   109
        """Browse button has been clicked."""
oliver@205
   110
        if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
oliver@205
   111
            self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
oliver@205
   112
oliver@205
   113
oliver@205
   114
    def clicked_ok(self):
oliver@205
   115
        
oliver@205
   116
        """Ok button has been clicked."""
oliver@205
   117
BarthaM@213
   118
        init_data = dict()
oliver@207
   119
        
oliver@207
   120
        # pick the password
BarthaM@213
   121
        init_data['password'] = str(self.ui.edtPassword.text())
oliver@208
   122
        if len(init_data['password']) == 0:
oliver@208
   123
            QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
oliver@208
   124
            return
oliver@207
   125
oliver@205
   126
        # read the content of the keyfile
oliver@205
   127
        keyfile_content = ''
oliver@205
   128
        try:
oliver@207
   129
            if len(self.ui.edtKeyfile.text()) > 0:
oliver@207
   130
                keyfile = open(self.ui.edtKeyfile.text(), 'r')
oliver@207
   131
                keyfile_content = keyfile.read()
oliver@205
   132
        except Exception as e:
oliver@205
   133
            sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
oliver@205
   134
            QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
oliver@205
   135
            return
oliver@205
   136
oliver@205
   137
        # turn into Base64
oliver@207
   138
        if len(keyfile_content) > 0:
oliver@207
   139
            keyfile_content_base64 = base64.b64encode(keyfile_content)
oliver@207
   140
            init_data['keyfile'] = keyfile_content_base64
oliver@205
   141
BarthaM@213
   142
        res = ""
oliver@207
   143
        try:
BarthaM@213
   144
            req_data = urllib.urlencode(init_data)
BarthaM@213
   145
            req = urllib2.Request('http://' + ip + ':58081/init', req_data)
oliver@207
   146
            res = urllib2.urlopen(req)
oliver@207
   147
        except:
BarthaM@213
   148
            print('EXCEPTION ' + res)
oliver@207
   149
            pass
oliver@207
   150
oliver@205
   151
        self.accept()
oliver@205
   152
oliver@205
   153
BarthaM@213
   154
    def set_user_text(self, user_text):
oliver@205
   155
oliver@205
   156
        """Set a text to explain which password we need."""
oliver@205
   157
        self.ui.lblText.setText(user_text)
oliver@205
   158
oliver@205
   159
oliver@205
   160
if __name__ == "__main__":
oliver@207
   161
    
oliver@207
   162
    print(sys.argv)
oliver@207
   163
    
oliver@207
   164
    ip = None
oliver@207
   165
    try:
oliver@207
   166
        ip = sys.argv[-1:][0]
oliver@207
   167
    except:
oliver@207
   168
        pass
oliver@207
   169
    
oliver@205
   170
    a = QtGui.QApplication(sys.argv)
oliver@207
   171
    d = FormatDriveDialog(ip)
oliver@205
   172
    d.show()
oliver@205
   173
    sys.exit(a.exec_())     
oliver@205
   174