zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

open_volume.c
Go to the documentation of this file.
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  */
162 {
163  return zuluCryptOpenVolume_0( _open_mapper,opts ) ;
164 }
165 
166 /*
167  * this function tries to unlock luks,plain and truecrypt volumes
168  */
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  */
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 }
open_struct_t::fs_opts
const char * fs_opts
Definition: includes.h:75
open_struct_t::mapper_name
const char * mapper_name
Definition: includes.h:71
zuluCryptOpenLuks_2
int zuluCryptOpenLuks_2(const open_struct_t *)
Definition: open_luks.c:125
open_struct_t
Definition: includes.h:68
_mount_volume
static int _mount_volume(const char *mapper, const open_struct_t *opts, int(*function)(const char *))
Definition: open_volume.c:31
open_struct_t::uid
uid_t uid
Definition: includes.h:80
open_struct_t::key_source
int key_source
Definition: includes.h:82
open_struct_t::plain_dm_properties
const char * plain_dm_properties
Definition: includes.h:70
open_struct_t::tcrypt_keyfiles_count
int tcrypt_keyfiles_count
Definition: includes.h:84
open_struct_t::m_point
const char * m_point
Definition: includes.h:74
open_struct_t::bitlocker_volume
int bitlocker_volume
Definition: includes.h:87
zuluCryptDeleteFile
void zuluCryptDeleteFile(const char *file)
Definition: file_path_security.c:57
zuluCryptOpenLuks_1
int zuluCryptOpenLuks_1(const open_struct_t *)
Definition: open_luks.c:120
StringMultipleAppend
const char * StringMultipleAppend(string_t st,...)
Definition: String.c:1123
zuluCryptPathIsNotValid
int zuluCryptPathIsNotValid(const char *path)
Definition: is_path_valid.c:29
zuluCryptOpenVolume_0
int zuluCryptOpenVolume_0(int(*function)(const open_struct_t *), const open_struct_t *opts)
Definition: open_volume.c:48
zuluCryptMapperPrefix
const char * zuluCryptMapperPrefix(void)
Definition: create_mapper_name.c:95
zuluCryptOpenVolume_2
int zuluCryptOpenVolume_2(const open_struct_t *opts)
Definition: open_volume.c:169
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
open_struct_t::device
const char * device
Definition: includes.h:69
zuluCryptCreateKeyFile
string_t zuluCryptCreateKeyFile(const char *key, size_t key_len, const char *fileName)
Definition: open_tcrypt.c:125
StringType
Definition: String.c:49
open_struct_t::trueCrypt_volume
int trueCrypt_volume
Definition: includes.h:86
zuluCryptVolumeIsLuks
int zuluCryptVolumeIsLuks(const char *device)
Definition: is_luks.c:45
zuluCryptOpenVolume_1
int zuluCryptOpenVolume_1(const open_struct_t *opts)
Definition: open_volume.c:161
zuluExit
static int zuluExit(int x, string_t p)
Definition: open_volume.c:25
zuluCryptVolumeIsNotLuks
int zuluCryptVolumeIsNotLuks(const char *dev)
Definition: is_luks.c:63
String
string_t String(const char *cstring)
Definition: String.c:318
open_struct_t::key
const char * key
Definition: includes.h:73
zuluCryptCloseMapper
int zuluCryptCloseMapper(const char *mapper)
Definition: close_mapper.c:84
open_struct_t::tcrypt_keyfiles
const char *const * tcrypt_keyfiles
Definition: includes.h:90
TCRYPT_KEYFILE
#define TCRYPT_KEYFILE
Definition: libzuluCrypt.h:306
open_struct_t::veraCrypt_volume
int veraCrypt_volume
Definition: includes.h:85
includes.h
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
zuluCryptOpenPlain_1
int zuluCryptOpenPlain_1(const open_struct_t *)
Definition: open_plain.c:233
zuluCryptOpenTcrypt_1
int zuluCryptOpenTcrypt_1(const open_struct_t *)
Definition: open_tcrypt.c:440
_open_mapper
static int _open_mapper(const open_struct_t *opts)
Definition: open_volume.c:121
open_struct_t::luks_detached_header
const char * luks_detached_header
Definition: includes.h:77
open_struct_t::key_len
size_t key_len
Definition: includes.h:79
open_struct_t::m_flags
unsigned long m_flags
Definition: includes.h:91
zuluCryptBitLockerUnlock
int zuluCryptBitLockerUnlock(const open_struct_t *opts, string_t *xt)
Definition: bitlocker.c:397
zuluCryptMountVolume
int zuluCryptMountVolume(const char *mapper, const char *m_point, unsigned long fs_opts, const char *m_opts, uid_t id)
Definition: mount_volume.c:433
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76
zuluCryptOpenVolume
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)
Definition: open_volume.c:95
zuluCryptPathIsValid
int zuluCryptPathIsValid(const char *path)
Definition: is_path_valid.c:23
StringVoid
#define StringVoid
Definition: String.h:47