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