"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-cli/lib/open_volume.c" (4 Jan 2020, 5253 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 "open_volume.c" 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 "includes.h"
   21 #include <fcntl.h>
   22 #include <sys/mount.h>
   23 #include <unistd.h>
   24 
   25 static inline int zuluExit( int x,string_t p )
   26 {
   27     StringDelete( &p ) ;
   28     return x ;
   29 }
   30 
   31 static int _mount_volume( const char * mapper,
   32               const open_struct_t * opts,
   33               int( *function )( const char * ) )
   34 {
   35     /*
   36      * zuluCryptMountVolume() is defined in mount_volume.c
   37      */
   38     int h = zuluCryptMountVolume( mapper,opts->m_point,opts->m_flags,opts->fs_opts,opts->uid ) ;
   39 
   40     if( h != 0 ){
   41 
   42         function( mapper ) ;
   43     }
   44 
   45     return h ;
   46 }
   47 
   48 int zuluCryptOpenVolume_0( int( *function )( const open_struct_t * ),const open_struct_t * opts )
   49 {
   50     int h ;
   51 
   52     string_t p ;
   53 
   54     const char * mapper ;
   55 
   56     /*
   57      * zuluCryptPathIsNotValid() is defined in is_path_valid.c
   58      */
   59     if( zuluCryptPathIsNotValid( opts->device ) ){
   60 
   61         return 3 ;
   62     }
   63     /*
   64      * zuluCryptMapperPrefix() is defined in create_mapper_name.c
   65      */
   66     p = String( zuluCryptMapperPrefix() ) ;
   67 
   68     mapper = StringMultipleAppend( p,"/",opts->mapper_name,NULL ) ;
   69 
   70     /*
   71      * zuluCryptPathIsValid() is defined in is_path_valid.c
   72      */
   73     if( zuluCryptPathIsValid( mapper ) ){
   74 
   75         return zuluExit( 2,p ) ;
   76     }
   77 
   78     h = function( opts ) ;
   79 
   80     switch( h ){
   81 
   82         case 1 : return zuluExit( 4,p ) ;
   83         case 2 : return zuluExit( 8,p ) ;
   84         case 3 : return zuluExit( 3,p ) ;
   85     }
   86 
   87     if( opts->m_point != NULL ){
   88 
   89         h = _mount_volume( mapper,opts,zuluCryptCloseMapper ) ;
   90     }
   91 
   92     return zuluExit( h,p ) ;
   93 }
   94 
   95 int zuluCryptOpenVolume( const char * dev,const char * mapper,const char * m_point,uid_t uid,
   96              unsigned long m_flags,const char * fs_opts,const char * key,size_t key_len )
   97 {
   98     open_struct_t opts ;
   99 
  100     memset( &opts,'\0',sizeof( open_struct_t ) ) ;
  101 
  102     opts.device      = dev ;
  103     opts.mapper_name = mapper ;
  104     opts.m_point     = m_point ;
  105     opts.uid         = uid ;
  106     opts.m_flags     = m_flags ;
  107     opts.fs_opts     = fs_opts ;
  108     opts.key         = key ;
  109     opts.key_len     = key_len ;
  110 
  111     if( m_flags & MS_RDONLY ){
  112 
  113         opts.m_opts = "ro" ;
  114     }else{
  115         opts.m_opts = "rw" ;
  116     }
  117 
  118     return zuluCryptOpenVolume_1( &opts ) ;
  119 }
  120 
  121 static int _open_mapper( const open_struct_t * opts )
  122 {
  123     int r ;
  124     /*
  125      * zuluCryptVolumeIsLuks() is defined in is_luks.c
  126      */
  127 
  128     if( opts->luks_detached_header ){
  129 
  130         /*
  131          * zuluCryptOpenLuks_1() is defined in open_luks.c
  132          */
  133         r = zuluCryptOpenLuks_1( opts ) ;
  134 
  135     }else if( zuluCryptVolumeIsLuks( opts->device ) ){
  136 
  137         /*
  138          * zuluCryptOpenLuks_2() is defined in open_luks.c
  139          */
  140         r = zuluCryptOpenLuks_2( opts ) ;
  141 
  142         if( r != 0 ){
  143             /*
  144              * just assumed wrong password when a volume fail to unlock
  145              */
  146             r = 1 ;
  147         }
  148     }else{
  149         /*
  150          * zuluCryptOpenPlain_1() is defined in open_plain.c
  151          */
  152         r = zuluCryptOpenPlain_1( opts ) ;
  153     }
  154 
  155     return r ;
  156 }
  157 
  158 /*
  159  * this function tries to unlock luks and plain volumes only
  160  */
  161 int zuluCryptOpenVolume_1( const open_struct_t * opts )
  162 {
  163     return zuluCryptOpenVolume_0( _open_mapper,opts ) ;
  164 }
  165 
  166 /*
  167  * this function tries to unlock luks,plain and truecrypt volumes
  168  */
  169 int zuluCryptOpenVolume_2( const open_struct_t * opts )
  170 {
  171     int r ;
  172 
  173     open_struct_t opts_1 ;
  174 
  175     string_t zt = StringVoid ;
  176 
  177     string_t xt = StringVoid ;
  178 
  179     const char * keyfile ;
  180 
  181     const char * mapper ;
  182 
  183     if( opts->bitlocker_volume ){
  184 
  185         r = zuluCryptBitLockerUnlock( opts,&xt ) ;
  186 
  187         if( r == 0 ){
  188 
  189             mapper = StringContent( xt ) ;
  190 
  191             r = _mount_volume( mapper,opts,zuluCryptCloseMapper ) ;
  192         }
  193 
  194         StringDelete( &xt ) ;
  195 
  196     }else if( opts->plain_dm_properties != NULL ){
  197         /*
  198          * zuluCryptOpenPlain_1() is defined in open_plain.c
  199          */
  200         r = zuluCryptOpenVolume_0( zuluCryptOpenPlain_1,opts ) ;
  201     }else{
  202         if( opts->veraCrypt_volume || opts->trueCrypt_volume ){
  203 
  204             r = 4 ;
  205         }else{
  206             r = zuluCryptOpenVolume_1( opts ) ;
  207         }
  208 
  209         if( r == 4 && zuluCryptVolumeIsNotLuks( opts->device ) ){
  210 
  211             memcpy( &opts_1,opts,sizeof( open_struct_t ) ) ;
  212 
  213             if( opts_1.key_source == TCRYPT_KEYFILE ){
  214                 /*
  215                  * zuluCryptCreateKeyFile() is defined in open_tcrypt.c
  216                  */
  217                 zt = zuluCryptCreateKeyFile( opts_1.key,opts_1.key_len,"keyfile" ) ;
  218 
  219                 if( zt != StringVoid ){
  220 
  221                     keyfile = StringContent( zt ) ;
  222                     opts_1.tcrypt_keyfiles_count = 1 ;
  223                     opts_1.tcrypt_keyfiles       = &keyfile ;
  224                     opts_1.key = "" ;
  225                     opts_1.key_len = 0 ;
  226 
  227                     r = zuluCryptOpenTcrypt_1( &opts_1 ) ;
  228                     /*
  229                      * zuluCryptDeleteFile() is defined in file_path_security.c
  230                      */
  231                     zuluCryptDeleteFile( keyfile ) ;
  232 
  233                     StringDelete( &zt ) ;
  234                 }else{
  235                     r = -1 ;
  236                 }
  237             }else{
  238                 r = zuluCryptOpenTcrypt_1( &opts_1 ) ;
  239             }
  240         }
  241         if( r == 0 || r == -1 ){
  242             ;
  243         }else{
  244             r = 4 ;
  245         }
  246     }
  247     return r ;
  248 }