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