"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-cli/lib/open_luks.c" (4 Jan 2020, 2901 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_luks.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 <libcryptsetup.h>
   22 #include <fcntl.h>
   23 #include <unistd.h>
   24 
   25 /*
   26  * This header file is created by the build process and it checks if the version of cryptsetup >= 1.4.0
   27  */
   28 #include "luks_external_header.h"
   29 
   30 static int zuluExit( int st,struct crypt_device * cd )
   31 {
   32     crypt_free( cd ) ;
   33     return st ;
   34 }
   35 
   36 static int _open_luks_2( const char * device,const resolve_path_t * opt )
   37 {
   38     struct crypt_device * cd ;
   39     uint32_t flags ;
   40     int st ;
   41 
   42     /*
   43      * open_struct_t is defined in includes.h
   44      */
   45     const open_struct_t * opts = opt->args ;
   46 
   47     if( zuluCryptPathIsNotValid( device ) ){
   48 
   49         return 3 ;
   50     }
   51     if( opts->luks_detached_header ){
   52 #if LUKS_EXTERNAL_HEADER
   53         if( crypt_init( &cd,opts->luks_detached_header ) != 0 ){
   54 
   55             return 1 ;
   56         }
   57 
   58         if( crypt_load( cd,NULL,NULL ) != 0 ){
   59 
   60             return zuluExit( 2,cd ) ;
   61         }
   62 
   63         if( crypt_set_data_device( cd,device ) != 0 ){
   64 
   65             return zuluExit( 1,cd ) ;
   66         }
   67 #else
   68         return 1 ;
   69 #endif
   70     }else{
   71         if( crypt_init( &cd,device ) != 0 ){
   72 
   73             return 2 ;
   74         }
   75 
   76         if( crypt_load( cd,NULL,NULL ) != 0 ){
   77 
   78             return zuluExit( 2,cd ) ;
   79         }
   80     }
   81 
   82     if( opt->open_mode == O_RDONLY ){
   83 
   84         flags = CRYPT_ACTIVATE_READONLY ;
   85     }else{
   86         flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
   87     }
   88 
   89     st = crypt_activate_by_passphrase( cd,opts->mapper_name,CRYPT_ANY_SLOT,
   90                        opts->key,opts->key_len,flags ) ;
   91 
   92     if( st >= 0 ){
   93 
   94         return zuluExit( 0,cd ) ;
   95 
   96     }else if( st == -1 ){
   97 
   98         return zuluExit( 1,cd ) ;
   99     }else{
  100         return zuluExit( 2,cd ) ;
  101     }
  102 }
  103 
  104 int zuluCryptOpenLuks( const char * device,const char * mapper,
  105                const char * mode,const char * key,size_t key_len )
  106 {
  107     open_struct_t opts ;
  108 
  109     memset( &opts,'\0',sizeof( open_struct_t ) ) ;
  110 
  111     opts.device      = device ;
  112     opts.mapper_name = mapper ;
  113     opts.m_opts      = mode   ;
  114     opts.key         = key    ;
  115     opts.key_len     = key_len;
  116 
  117     return zuluCryptOpenLuks_2( &opts ) ;
  118 }
  119 
  120 int zuluCryptOpenLuks_1( const open_struct_t * opts )
  121 {
  122     return zuluCryptOpenLuks_2( opts ) ;
  123 }
  124 
  125 int zuluCryptOpenLuks_2( const open_struct_t * opts )
  126 {
  127     /*
  128      * zuluCryptResolveDevicePath_0() is defined in resolve_path.c
  129      */
  130     return zuluCryptResolveDevicePath_0( _open_luks_2,opts,2 ) ;
  131 }