"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-gui/luksdeletekey.cpp" (31 Jan 2020, 10571 Bytes) of package /linux/misc/zuluCrypt-5.7.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "luksdeletekey.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.7.0_vs_5.7.1.

    1 /*
    2  *
    3  *  Copyright ( c ) 2011-2015
    4  *  name : Francis Banyikwa
    5  *  email: mhogomchungu@gmail.com
    6  *  This program is free software: you can redistribute it and/or modify
    7  *  it under the terms of the GNU General Public License as published by
    8  *  the Free Software Foundation, either version 2 of the License, or
    9  *  ( at your option ) any later version.
   10  *
   11  *  This program is distributed in the hope that it will be useful,
   12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *  GNU General Public License for more details.
   15  *
   16  *  You should have received a copy of the GNU General Public License
   17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include "luksdeletekey.h"
   21 #include "zulucrypt.h"
   22 #include "../zuluCrypt-cli/constants.h"
   23 #include "utility.h"
   24 
   25 #include <QObject>
   26 #include <Qt>
   27 #include <QObject>
   28 #include <QFileDialog>
   29 #include <QRadioButton>
   30 #include <QPushButton>
   31 #include <QMessageBox>
   32 #include <QCloseEvent>
   33 #include <QIcon>
   34 #include <QKeyEvent>
   35 
   36 #include "ui_openvolume.h"
   37 #include "openvolume.h"
   38 #include "ui_luksdeletekey.h"
   39 #include "task.hpp"
   40 #include "dialogmsg.h"
   41 #include "plugin.h"
   42 
   43 luksdeletekey::luksdeletekey( QWidget * parent ) : QDialog( parent ),m_ui( new Ui::luksdeletekey )
   44 {
   45     m_isWindowClosable = true ;
   46     m_ui->setupUi( this ) ;
   47 
   48     m_label.setOptions( m_ui->label_2,m_ui->pushButton ) ;
   49 
   50     this->setFixedSize( this->size() ) ;
   51     this->setFont( parent->font() ) ;
   52 
   53     m_ui->lineEditVolumePath->clear() ;
   54 
   55     connect( m_ui->pushButtonDelete,SIGNAL( clicked() ),this,SLOT( pbDelete() ) ) ;
   56     connect( m_ui->pushButtonCancel,SIGNAL( clicked() ),this,SLOT( pbCancel() ) ) ;
   57     connect( m_ui->pushButtonOpenKeyFile,SIGNAL( clicked() ),this,SLOT( pbOpenKeyFile() ) ) ;
   58     connect( m_ui->pushButtonOpenVolume,SIGNAL( clicked() ),this,SLOT( pbOpenVolume() ) ) ;
   59     connect( m_ui->pushButtonOpenPartition,SIGNAL( clicked() ),this,SLOT( pbOpenPartition() ) ) ;
   60 
   61     connect( m_ui->cbKey,SIGNAL( activated( int ) ),this,SLOT( Key( int ) ) ) ;
   62 
   63     this->Key( 0 ) ;
   64 
   65     m_ui->cbKey->addItem( tr( "YubiKey Challenge/Response" ) ) ;
   66 
   67     this->installEventFilter( this ) ;
   68 }
   69 
   70 bool luksdeletekey::eventFilter( QObject * watched,QEvent * event )
   71 {
   72     return utility::eventFilter( this,watched,event,[ this ](){ this->HideUI() ; } ) ;
   73 }
   74 
   75 void luksdeletekey::closeEvent( QCloseEvent * e )
   76 {
   77     e->ignore() ;
   78 
   79     if(  m_isWindowClosable ){
   80 
   81         this->pbCancel() ;
   82     }
   83 }
   84 
   85 void luksdeletekey::Key( int e )
   86 {
   87     auto _key_ui = [ this ](){
   88 
   89         m_ui->lineEditPassphrase->setToolTip( tr( "Enter a key" ) ) ;
   90         m_ui->labelPassphrase->setText( tr( "Password" ) ) ;
   91         m_ui->lineEditPassphrase->setEchoMode( QLineEdit::Password ) ;
   92         m_ui->lineEditPassphrase->clear() ;
   93         m_ui->pushButtonOpenKeyFile->setEnabled( false ) ;
   94         m_ui->pushButtonOpenKeyFile->setIcon( QIcon( ":/passphrase.png" ) ) ;
   95         m_ui->lineEditPassphrase->setFocus() ;
   96         m_ui->lineEditPassphrase->setEnabled( true ) ;
   97     } ;
   98 
   99     if( e == 0 || e == 3 ){
  100 
  101         _key_ui() ;
  102 
  103     }else if( e == 1 ){
  104 
  105         m_ui->lineEditPassphrase->setToolTip( tr( "Enter a path to a keyfile location" ) ) ;
  106         m_ui->labelPassphrase->setText( tr( "KeyFile path" ) ) ;
  107         m_ui->lineEditPassphrase->setEchoMode( QLineEdit::Normal ) ;
  108         m_ui->lineEditPassphrase->clear() ;
  109         m_ui->pushButtonOpenKeyFile->setEnabled( true ) ;
  110         m_ui->pushButtonOpenKeyFile->setIcon( QIcon( ":/keyfile.png" ) ) ;
  111         m_ui->lineEditPassphrase->setEnabled( true ) ;
  112     }else{
  113         _key_ui() ;
  114 
  115         m_ui->lineEditPassphrase->setEnabled( false ) ;
  116 
  117         plugin::instance( this,plugins::plugin::hmac_key,[ this ]( const QString& key ){
  118 
  119             m_ui->lineEditPassphrase->setText( key ) ;
  120 
  121             if( key.isEmpty() ){
  122 
  123                 m_ui->cbKey->setCurrentIndex( 0 ) ;
  124                 this->Key( 0 ) ;
  125             }
  126         } ) ;
  127     }
  128 }
  129 
  130 void luksdeletekey::pbOpenKeyFile()
  131 {
  132     auto Z = QFileDialog::getOpenFileName( this,tr( "Key File With A Passphrase To Delete" ),utility::homePath() ) ;
  133 
  134     m_ui->lineEditPassphrase->setText( Z ) ;
  135 
  136     if( m_ui->lineEditVolumePath->text().isEmpty() ){
  137 
  138         m_ui->lineEditVolumePath->setFocus() ;
  139     }else{
  140         m_ui->pushButtonDelete->setFocus() ;
  141     }
  142 }
  143 
  144 void luksdeletekey::ShowUI()
  145 {
  146     this->enableAll() ;
  147 
  148     m_ui->labelPassphrase->setText( tr( "Password" ) ) ;
  149 
  150     if( m_ui->lineEditVolumePath->text().isEmpty() ){
  151 
  152         m_ui->lineEditVolumePath->setFocus() ;
  153     }else{
  154         m_ui->lineEditPassphrase->setFocus() ;
  155     }
  156 
  157     m_ui->pushButtonOpenPartition->setIcon( QIcon( ":/partition.png" ) ) ;
  158     m_ui->pushButtonOpenVolume->setIcon( QIcon( ":/file.png" ) ) ;
  159 
  160     this->show() ;
  161 }
  162 
  163 void luksdeletekey::ShowUI( const QString& path )
  164 {
  165     m_ui->lineEditVolumePath->setText( path ) ;
  166     this->ShowUI() ;
  167 }
  168 
  169 void luksdeletekey::disableAll()
  170 {
  171     m_ui->cbKey->setEnabled( false ) ;
  172     m_ui->label->setEnabled( false ) ;
  173     m_ui->labelPassphrase->setEnabled( false ) ;
  174     m_ui->lineEditPassphrase->setEnabled( false ) ;
  175     m_ui->lineEditVolumePath->setEnabled( false ) ;
  176     m_ui->pushButtonCancel->setEnabled( false ) ;
  177     m_ui->pushButtonDelete->setEnabled( false ) ;
  178     m_ui->pushButtonOpenKeyFile->setEnabled( false ) ;
  179     m_ui->pushButtonOpenPartition->setEnabled( false ) ;
  180     m_ui->pushButtonOpenVolume->setEnabled( false ) ;
  181 }
  182 
  183 void luksdeletekey::enableAll()
  184 {
  185     m_ui->cbKey->setEnabled( true ) ;
  186     m_ui->label->setEnabled( true ) ;
  187     m_ui->labelPassphrase->setEnabled( true ) ;
  188     m_ui->lineEditPassphrase->setEnabled( true ) ;
  189     m_ui->lineEditVolumePath->setEnabled( true ) ;
  190     m_ui->pushButtonCancel->setEnabled( true ) ;
  191     m_ui->pushButtonDelete->setEnabled( true ) ;
  192     m_ui->pushButtonOpenPartition->setEnabled( true ) ;
  193     m_ui->pushButtonOpenVolume->setEnabled( true ) ;
  194 
  195     if( m_ui->cbKey->currentIndex() == 1 ){
  196 
  197         m_ui->pushButtonOpenKeyFile->setEnabled( true ) ;
  198     }
  199 }
  200 
  201 void luksdeletekey::pbCancel()
  202 {
  203     this->HideUI() ;
  204 }
  205 
  206 void luksdeletekey::pbOpenPartition()
  207 {
  208     openvolume::instance( this,false ).showLuksOnly().ShowAllPartitions( [ this ]( const QString& e ){
  209 
  210         this->ShowUI( e ) ;
  211     } ) ;
  212 }
  213 
  214 void luksdeletekey::pbDelete()
  215 {
  216     DialogMsg msg( this ) ;
  217 
  218     m_volumePath = utility::resolvePath( m_ui->lineEditVolumePath->text() ) ;
  219 
  220     this->disableAll() ;
  221 
  222     utility::raii raii( [ this ](){ this->enableAll() ; } ) ;
  223 
  224     if( m_volumePath.isEmpty() ){
  225 
  226         msg.ShowUIOK( tr( "ERROR!" ),tr( "Atleast one required field is empty" ) ) ;
  227     }else{
  228         if( utility::requireSystemPermissions( m_volumePath ) ){
  229 
  230             if( !utility::enablePolkit( utility::background_thread::False ) ){
  231 
  232                 return msg.ShowUIOK( tr( "ERROR!" ),tr( "Failed to enable polkit support" ) ) ;
  233             }
  234         }
  235 
  236         m_volumePath.replace( "\"","\"\"\"" ) ;
  237 
  238         raii.cancel() ;
  239 
  240         this->deleteKey( utility::luksEmptySlots( m_volumePath ).await() ) ;
  241     }
  242 }
  243 
  244 void luksdeletekey::deleteKey( const QStringList& l )
  245 {
  246     if( l.isEmpty() ){
  247 
  248         m_label.show( tr( "Volume is not a luks volume" ) ) ;
  249 
  250         return this->enableAll() ;
  251 
  252     }else if( l.first().toInt() == 1 ){
  253 
  254         auto s = tr( "There is only one last key in the volume." ) ;
  255         s = s + tr( "\nDeleting it will make the volume unopenable and lost forever." ) ;
  256         s = s + tr( "\nAre you sure you want to delete this key?" ) ;
  257 
  258         if( DialogMsg( this ).ShowUIYesNoDefaultNo( tr( "WARNING" ),s ) == QMessageBox::No ){
  259 
  260             return this->enableAll() ;
  261         }
  262     }else{
  263         //auto s = tr( "Are you sure you want to delete a key from this volume?" ) ;
  264 
  265         //if( DialogMsg( this ).ShowUIYesNoDefaultNo( tr( "WARNING" ),s ) == QMessageBox::No ){
  266 
  267         //  return this->enableAll() ;
  268         //}
  269     }
  270 
  271     auto keyNumber = l.first().toInt() - 1 ;
  272     auto totalKeys = l.at( 1 ) ;
  273 
  274     QString keypath ;
  275 
  276     if( m_ui->cbKey->currentIndex() == 1 ){
  277 
  278         keypath = utility::resolvePath( m_ui->lineEditPassphrase->text() ).replace( "\"","\"\"\"" ) ;
  279     }else{
  280         keypath = utility::keyPath() ;
  281 
  282         auto key = m_ui->lineEditPassphrase->text() ;
  283 
  284         if( m_ui->cbKey->currentIndex() == 3 ){
  285 
  286             auto m = utility::yubiKey( key ) ;
  287 
  288             if( m.has_value() ){
  289 
  290                 key = m.value() ;
  291             }else{
  292                 m_label.show( tr( "Failed To Locate Or Run Yubikey's \"ykchalresp\" Program." ) ) ;
  293                 return this->enableAll() ;
  294             }
  295         }
  296 
  297         utility::keySend( keypath,key ) ;
  298     }
  299 
  300     auto r = "%1 -k -r -d \"%2\" -f \"%3\"" ;
  301 
  302     auto exe = utility::appendUserUID( r ).arg( ZULUCRYPTzuluCrypt,m_volumePath,keypath ) ;
  303 
  304     m_isWindowClosable = false ;
  305 
  306     auto e = utility::Task::run( exe ).await() ;
  307 
  308     m_isWindowClosable = true ;
  309 
  310     QString success ;
  311 
  312     switch( e.exitCode() ){
  313         case 0 :
  314             success = tr( "Key removed successfully.\n%1 / %2 slots are now in use" ).arg( QString::number( keyNumber ) ).arg( totalKeys ) ;
  315             m_label.show( success ) ;
  316             return this->HideUI() ;
  317         case 2 : m_label.show( tr( "There is no key in the volume that match the presented key" ) ) ;               break ;
  318         case 3 : m_label.show( tr( "Could not open the volume" ) ) ;                                break ;
  319         case 4 : m_label.show( tr( "Insufficient privilege to open a system device,\
  320 only root user or members of group zulucrypt can do that" ) ) ;                                         break ;
  321         case 5 : m_label.show( tr( "Could not open the volume in write mode" ) ) ;                      break ;
  322         case 6 : m_label.show( tr( "Insufficient memory to hold your response" ) ) ;                        break ;
  323         case 7 : m_label.show( tr( "Operation terminated per user request" ) ) ;                            break ;
  324         case 8 : m_label.show( tr( "Can not get passphrase in silent mode" ) ) ;                            break ;
  325         case 9 : m_label.show( tr( "Insufficient memory to hold passphrase" ) ) ;                       break ;
  326         case 10: m_label.show( tr( "One or more required argument(s) for this operation is missing" ) ) ;           break ;
  327         case 11: m_label.show( tr( "Keyfile does not exist" ) ) ;                               break;
  328         case 12: m_label.show( tr( "Could not get enough memory to open the key file") ) ;                  break ;
  329         case 13: m_label.show( tr( "Insufficient privilege to open key file for reading" ) ) ;                  break ;
  330         case 14: m_label.show( tr( "Could not get a key from a socket" ) ) ;                            break ;
  331         case 110:m_label.show( tr( "Can not find a partition that match presented UUID" ) ) ;                   break ;
  332         default: m_label.show( tr( "Error Code: %1\n--\nStdOut: %2\n--\nStdError: %3").arg( QString::number( e.exitCode() ),QString( e.stdError() ),QString( e.stdOut() ) ) ) ;
  333     }
  334 
  335     this->enableAll() ;
  336 
  337     if( e.exitCode() == 2 ){
  338 
  339         this->Key( 0 ) ;
  340         m_ui->cbKey->setCurrentIndex( 0 ) ;
  341     }
  342 }
  343 
  344 void luksdeletekey::pbOpenVolume()
  345 {
  346     auto Z = QFileDialog::getOpenFileName( this,tr( "Volume Path" ),utility::homePath() ) ;
  347 
  348     m_ui->lineEditVolumePath->setText( Z ) ;
  349 
  350     if( m_ui->lineEditPassphrase->text().isEmpty() ){
  351 
  352         m_ui->lineEditPassphrase->setFocus() ;
  353     }else{
  354         m_ui->pushButtonDelete->setFocus() ;
  355     }
  356 }
  357 
  358 void luksdeletekey::HideUI()
  359 {
  360     this->hide() ;
  361     this->deleteLater() ;
  362 }
  363 
  364 luksdeletekey::~luksdeletekey()
  365 {
  366     delete m_ui ;
  367 }