1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
*
* Copyright (c) 2011-2015
* name : Francis Banyikwa
* email: mhogomchungu@gmail.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include <fcntl.h>
#include <sys/mount.h>
#include <unistd.h>
static inline int zuluExit( int x,string_t p )
{
StringDelete( &p ) ;
return x ;
}
static int _mount_volume( const char * mapper,
const open_struct_t * opts,
int( *function )( const char * ) )
{
/*
* zuluCryptMountVolume() is defined in mount_volume.c
*/
int h = zuluCryptMountVolume( mapper,opts->m_point,opts->m_flags,opts->fs_opts,opts->uid ) ;
if( h != 0 ){
function( mapper ) ;
}
return h ;
}
int zuluCryptOpenVolume_0( int( *function )( const open_struct_t * ),const open_struct_t * opts )
{
int h ;
string_t p ;
const char * mapper ;
/*
* zuluCryptPathIsNotValid() is defined in is_path_valid.c
*/
if( zuluCryptPathIsNotValid( opts->device ) ){
return 3 ;
}
/*
* zuluCryptMapperPrefix() is defined in create_mapper_name.c
*/
p = String( zuluCryptMapperPrefix() ) ;
mapper = StringMultipleAppend( p,"/",opts->mapper_name,NULL ) ;
/*
* zuluCryptPathIsValid() is defined in is_path_valid.c
*/
if( zuluCryptPathIsValid( mapper ) ){
return zuluExit( 2,p ) ;
}
h = function( opts ) ;
switch( h ){
case 1 : return zuluExit( 4,p ) ;
case 2 : return zuluExit( 8,p ) ;
case 3 : return zuluExit( 3,p ) ;
}
if( opts->m_point != NULL ){
h = _mount_volume( mapper,opts,zuluCryptCloseMapper ) ;
}
return zuluExit( h,p ) ;
}
int zuluCryptOpenVolume( const char * dev,const char * mapper,const char * m_point,uid_t uid,
unsigned long m_flags,const char * fs_opts,const char * key,size_t key_len )
{
open_struct_t opts ;
memset( &opts,'\0',sizeof( open_struct_t ) ) ;
opts.device = dev ;
opts.mapper_name = mapper ;
opts.m_point = m_point ;
opts.uid = uid ;
opts.m_flags = m_flags ;
opts.fs_opts = fs_opts ;
opts.key = key ;
opts.key_len = key_len ;
if( m_flags & MS_RDONLY ){
opts.m_opts = "ro" ;
}else{
opts.m_opts = "rw" ;
}
return zuluCryptOpenVolume_1( &opts ) ;
}
static int _open_mapper( const open_struct_t * opts )
{
int r ;
/*
* zuluCryptVolumeIsLuks() is defined in is_luks.c
*/
if( opts->luks_detached_header ){
/*
* zuluCryptOpenLuks_1() is defined in open_luks.c
*/
r = zuluCryptOpenLuks_1( opts ) ;
}else if( zuluCryptVolumeIsLuks( opts->device ) ){
/*
* zuluCryptOpenLuks_2() is defined in open_luks.c
*/
r = zuluCryptOpenLuks_2( opts ) ;
if( r != 0 ){
/*
* just assumed wrong password when a volume fail to unlock
*/
r = 1 ;
}
}else{
/*
* zuluCryptOpenPlain_1() is defined in open_plain.c
*/
r = zuluCryptOpenPlain_1( opts ) ;
}
return r ;
}
/*
* this function tries to unlock luks and plain volumes only
*/
int zuluCryptOpenVolume_1( const open_struct_t * opts )
{
return zuluCryptOpenVolume_0( _open_mapper,opts ) ;
}
string_t zuluCryptUpdatePlainDmcryptProperties( const char * e )
{
stringList_t stx = StringListSplit( e,'.' ) ;
size_t n = StringListSize( stx ) ;
string_t xt = String( StringListContentAt( stx,0 ) ) ;
size_t s ;
if( StringPrefixEqual( e,"/dev/" ) ){
s = 4 ;
}else{
s = 3 ;
}
for( size_t m = 1 ; m < n ; m++ ){
if( m == s ){
StringAppend( xt,".null" ) ;
}else{
StringMultipleAppend( xt,".",StringListContentAt( stx,m ),NULL ) ;
}
}
StringListDelete( &stx ) ;
return xt ;
}
static int _open_plain( const open_struct_t * opts )
{
string_t st ;
open_struct_t opts_1 ;
/*
* zuluCryptOpenPlain_1() is defined in open_plain.c
*/
int r = zuluCryptOpenVolume_0( zuluCryptOpenPlain_1,opts ) ;
if( r != 0 ){
memcpy( &opts_1,opts,sizeof( open_struct_t ) ) ;
if( opts_1.plain_dm_properties == NULL ){
opts_1.plain_dm_properties = "/dev/urandom.aes.cbc-essiv:sha256.256.null.0" ;
r = zuluCryptOpenVolume_0( zuluCryptOpenPlain_1,&opts_1 ) ;
}else{
st = zuluCryptUpdatePlainDmcryptProperties( opts->plain_dm_properties ) ;
opts_1.plain_dm_properties = StringContent( st ) ;
r = zuluCryptOpenVolume_0( zuluCryptOpenPlain_1,&opts_1 ) ;
StringDelete( &st ) ;
}
}
return r ;
}
static int _unlock_tcrypt_vcrypt( const open_struct_t * opts )
{
int r ;
open_struct_t opts_1 ;
string_t zt = StringVoid ;
const char * keyfile ;
memcpy( &opts_1,opts,sizeof( open_struct_t ) ) ;
if( opts_1.key_source == TCRYPT_KEYFILE ){
/*
* zuluCryptCreateKeyFile() is defined in open_tcrypt.c
*/
zt = zuluCryptCreateKeyFile( opts_1.key,opts_1.key_len,"keyfile" ) ;
if( zt != StringVoid ){
keyfile = StringContent( zt ) ;
opts_1.tcrypt_keyfiles_count = 1 ;
opts_1.tcrypt_keyfiles = &keyfile ;
opts_1.key = "" ;
opts_1.key_len = 0 ;
r = zuluCryptOpenTcrypt_1( &opts_1 ) ;
/*
* zuluCryptDeleteFile() is defined in file_path_security.c
*/
zuluCryptDeleteFile( keyfile ) ;
StringDelete( &zt ) ;
}else{
r = -1 ;
}
}else{
r = zuluCryptOpenTcrypt_1( &opts_1 ) ;
}
return r ;
}
/*
* this function tries to unlock luks,plain and truecrypt volumes
*/
int zuluCryptOpenVolume_2( const open_struct_t * opts )
{
int r ;
string_t xt = StringVoid ;
const char * mapper ;
if( opts->bitlocker_volume ){
r = zuluCryptBitLockerUnlock( opts,&xt ) ;
if( r == 0 ){
mapper = StringContent( xt ) ;
r = _mount_volume( mapper,opts,zuluCryptCloseMapper ) ;
}
StringDelete( &xt ) ;
}else if( opts->plain_dm_properties != NULL ){
r = _open_plain( opts ) ;
}else if( opts->luks_detached_header || zuluCryptVolumeIsLuks( opts->device ) ){
/*
* zuluCryptOpenLuks_2() is defined in open_luks.c
*/
r = zuluCryptOpenVolume_0( zuluCryptOpenLuks_2,opts ) ;
}else if( opts->veraCrypt_volume || opts->trueCrypt_volume ){
r = _unlock_tcrypt_vcrypt( opts ) ;
if( r != 0 ){
r = 4 ;
}
}else{
r = _open_plain( opts ) ;
if( r != 0 ){
open_struct_t opts_1 ;
memcpy( &opts_1,opts,sizeof( open_struct_t ) ) ;
opts_1.trueCrypt_volume = 1 ;
r = _unlock_tcrypt_vcrypt( &opts_1 ) ;
if( r != 0 ){
r = 4 ;
}
}
}
return r ;
}
|