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)  

bitlocker.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2019
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 <pwd.h>
22 #include <sys/mount.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 #include <libcryptsetup.h>
29 
30 static int zuluExit( int st,struct crypt_device * cd )
31 {
32  crypt_free( cd ) ;
33  return st ;
34 }
35 
36 const char * zuluCryptBitLockerType()
37 {
38  return "crypto_BitLocker" ;
39 }
40 
41 const char * zuluCryptBitLockerFolderPrefix( void )
42 {
43  return "cryptoBitlocker" ;
44 }
45 
47 {
48 #ifdef CRYPT_BITLK
49  return CRYPT_BITLK ;
50 #else
51  return "" ;
52 #endif
53 }
54 
56 {
57 #ifdef CRYPT_BITLK
58  return r ;
59 #else
60  return 0 ;
61 #endif
62 }
63 
65 {
66  return !zuluCryptUseCryptsetupBitLocker( r ) ;
67 }
68 
69 int zuluCryptIsDislockerMapperPath( const char * e )
70 {
71  return StringEndsWith_1( e,"/dislocker-file" ) ;
72 }
73 
75 {
76  const char * m = zuluCryptBitLockerFolderPrefix() ;
77 
78  return String_1( "/run/media/private/",getpwuid( uid )->pw_name,"/",m,NULL ) ;
79 }
80 
82 {
83  string_t st ;
84 
86 
88  }else{
89  st = String( e ) ;
90  }
91 
92  StringReplaceChar( st,'/','_' ) ;
93 
94  return st ;
95 }
96 
98 {
99  int fd = open( e,O_RDONLY ) ;
100 
101  int r ;
102 
103  int fd_loop ;
104 
105  string_t st = StringVoid ;
106 
107  string_t xt = StringVoid ;
108 
109  if( fd != -1 ){
110 
111  r = zuluCryptAttachLoopDeviceToFileUsingFileDescriptor( fd,&fd_loop,O_RDONLY,&st ) ;
112 
113  if( r == 1 ){
114 
116  }
117 
118  close( fd ) ;
119  close( fd_loop ) ;
120  }
121 
122  StringDelete( &st ) ;
123 
124  return xt ;
125 }
126 
127 string_t zuluCryptBitLockerFullMapperPath( uid_t uid,const char * e )
128 {
131 
132  StringAppendString( a,b ) ;
133 
134  StringAppend( a,"/dislocker-file" ) ;
135 
136  StringDelete( &b ) ;
137 
138  return a ;
139 }
140 
141 int zuluCryptDeviceManagedByDislocker( const char * dev,uid_t uid )
142 {
143  struct stat st ;
145  int r = stat( StringContent( p ),&st ) ;
146  StringDelete( &p ) ;
147  return r == 0 ;
148 }
149 
150 int zuluCryptBitLockerVolume( const char * e )
151 {
152  struct crypt_device * cd = NULL ;
153 
154  int r ;
155 
157 
158  return 1 ;
159  }else{
160  if( !StringPrefixEqual( e,crypt_get_dir() ) ){
161 
162  return 0 ;
163  }
164  if( crypt_init_by_name( &cd,e ) != 0 ){
165 
166  return 0 ;
167  }
168 
169  r = StringsAreEqual( crypt_get_type( cd ),zuluCryptCryptsetupBitLockerType() ) ;
170 
171  crypt_free( cd ) ;
172 
173  return r ;
174  }
175 }
176 
177 char * zuluCryptBitLockerUnmountPath( const char * e )
178 {
179  /*
180  * e will contain something like "/woof/bar/meaw/dislocker-file"
181  * and we want to remove the "/dislocker-file" part.
182  */
183  char * m = StringCopy_2( e ) ;
184 
185  ssize_t s = StringLastIndexOfChar_1( m,'/' ) ;
186 
187  if( s != -1 ){
188 
189  *( m + s ) = '\0' ;
190  }
191 
192  return m ;
193 }
194 
195 int zuluCryptBitLockerVolume_1( const char * e )
196 {
197  stringList_t stl = StringListSplit( e,' ' ) ;
199  StringListDelete( &stl ) ;
200  return s ;
201 }
202 
204 {
205  string_t q = String( e ) ;
207 
209  StringRemoveString( q,"/dislocker-file" ) ;
210 
211  StringReplaceChar( q,'_','/' ) ;
212  StringDelete( &z ) ;
213 
214  return q ;
215 }
216 
218 {
220  StringReplaceChar( e,'/','_' ) ;
221  StringPrependString( e,st ) ;
222  StringDelete( &st ) ;
223  return StringAppend( e,"/dislocker-file" ) ;
224 }
225 
226 int zuluCryptBitLockerlock( string_t mapperPath,char ** mount_point )
227 {
228  const char * n = StringContent( mapperPath ) ;
229 
230  int s ;
231 
232  char * e = NULL ;
233 
235 
236  int r ;
237 
238  if( m == NULL ){
239 
240  return 6 ;
241  }
242 
243  r = zuluCryptUnmountVolume( m,&e ) ;
244 
245  StringFree( m ) ;
246 
247  if( r == 0 ){
248 
249  s = zuluCryptCloseMapper( n ) ;
250 
251  if( s == 0 ){
252 
253  if( mount_point ){
254 
255  *mount_point = e ;
256  }
257  }else{
258  StringFree( e ) ;
259  }
260 
261  return s ;
262  }else{
263  return r ;
264  }
265 }
266 
267 static const char * _dislocker_fuse_path()
268 {
269  struct stat st ;
270  const char * e ;
271  int i ;
272 
273  const char * exe[] = { "/usr/bin/dislocker-fuse",
274  "/usr/sbin/dislocker-fuse",
275  "/usr/local/bin/dislocker-fuse",
276  "/usr/local/sbin/dislocker-fuse",
277  "/bin/dislocker-fuse",
278  "/sbin/dislocker-fuse",
279  "/opt/bin/dislocker-fuse",
280  "/opt/sbin/dislocker-fuse",
281  NULL } ;
282 
283  for( i = 0 ; *( exe + i ) != NULL ; i++ ){
284 
285  e = *( exe + i ) ;
286 
287  if( stat( e,&st ) == 0 ){
288 
289  fprintf( stderr,"Looking for dislocker-fuse at \"%s\" : PASSED\n",e ) ;
290 
291  return e ;
292  }else{
293  fprintf( stderr,"Looking for dislocker-fuse at \"%s\" : FAILED\n",e ) ;
294  }
295  }
296 
297  return NULL ;
298 }
299 
300 static int _cryptsetup_unlock_bitlocker( const open_struct_t * opts,string_t * xt )
301 {
302  int r ;
303 
304  struct crypt_device * cd = NULL ;
305 
306  uint32_t flags = 0 ;
307 
308  if( crypt_init( &cd,opts->device ) != 0 ){
309 
310  return 4 ;
311  }
312  if( crypt_load( cd,zuluCryptCryptsetupBitLockerType(),NULL ) != 0 ){
313 
314  return zuluExit( 4,cd ) ;
315  }
316  if( StringHasComponent( opts->m_opts,"ro" ) ){
317 
318  flags = CRYPT_ACTIVATE_READONLY ;
319  }else{
320  flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
321  }
322 
323  r = crypt_activate_by_passphrase( cd,
324  opts->mapper_name,
325  CRYPT_ANY_SLOT,
326  opts->key,
327  opts->key_len,
328  flags ) ;
329 
330  if( r == 0 ){
331 
332  *xt = String( opts->mapper_path ) ;
333 
334  return zuluExit( 0,cd ) ;
335  }else{
336  return zuluExit( 4,cd ) ;
337  }
338 }
339 
340 static int _dislocker_unlock_bitlocker( const open_struct_t * opts,string_t * xt )
341 {
342  int r ;
343 
344  string_t st ;
345 
346  const char * mapper_path ;
347 
348  process_t p ;
349 
350  string_t m ;
351 
352  char * env[ 2 ] = { NULL,NULL } ;
353 
354  const char * exe = _dislocker_fuse_path() ;
355 
356  if( exe == NULL ){
357 
358  return 15 ;
359  }
360 
361  st = String_1( opts->mapper_path,opts->mapper_name,NULL ) ;
362 
363  mapper_path = StringContent( st ) ;
364 
365  if( mkdir( mapper_path,S_IRWXU ) != 0 ){
366 
367  StringDelete( &st ) ;
368  return 2 ;
369  }else{
370  m = String_1( "DISLOCKER_PASSWORD=",opts->key,NULL ) ;
371 
372  env[ 0 ] = ( char * )StringContent( m ) ;
373 
374  p = Process( exe,opts->device,"-u","--","-oallow_root",mapper_path,NULL ) ;
375 
376  ProcessSetEnvironmentalVariable( p,( char * const * )env ) ;
377 
378  ProcessStart( p ) ;
379 
380  r = ProcessWaitUntilFinished( &p ) ;
381 
382  StringDelete( &m ) ;
383 
384  if( r == 0 ){
385 
386  StringAppend( st,"/dislocker-file" ) ;
387  *xt = st ;
388  return 0 ;
389  }else{
390  rmdir( mapper_path ) ;
391  StringDelete( &st ) ;
392  return 4 ;
393  }
394  }
395 }
396 
398 {
400 
401  return _cryptsetup_unlock_bitlocker( opts,xt ) ;
402  }else{
403  return _dislocker_unlock_bitlocker( opts,xt ) ;
404  }
405 }
StringAppendString
const char * StringAppendString(string_t st, string_t xt)
Definition: String.c:1195
zuluCryptBitLockerResolveMapperPath
string_t zuluCryptBitLockerResolveMapperPath(const char *e, uid_t uid)
Definition: bitlocker.c:203
zuluCryptBitLockerVolumeFS
string_t zuluCryptBitLockerVolumeFS(const char *e)
Definition: bitlocker.c:97
open_struct_t::mapper_name
const char * mapper_name
Definition: includes.h:71
open_struct_t
Definition: includes.h:68
ProcessSetEnvironmentalVariable
void ProcessSetEnvironmentalVariable(process_t p, char *const *env)
Definition: process.c:82
StringListContentAtFirstPlace
static const __inline__ char * StringListContentAtFirstPlace(stringList_t stl)
Definition: StringList.h:182
zuluCryptBitLockerVolume_1
int zuluCryptBitLockerVolume_1(const char *e)
Definition: bitlocker.c:195
zuluCryptLoopDeviceAddress_2
string_t zuluCryptLoopDeviceAddress_2(const char *device)
Definition: create_loop_device.c:101
open_struct_t::use_cryptsetup_for_bitlocker
int use_cryptsetup_for_bitlocker
Definition: includes.h:89
zulucryptGetBlkidFileSystem
string_t zulucryptGetBlkidFileSystem(const char *device)
Definition: blkid_evaluate_tag.c:104
zuluCryptNoPartitionLoopDevice
int zuluCryptNoPartitionLoopDevice(const char *e)
Definition: blkid_evaluate_tag.c:147
zuluCryptBitLockerMapperName
string_t zuluCryptBitLockerMapperName(const char *e)
Definition: bitlocker.c:81
zuluCryptBitLockerMapperPath
string_t zuluCryptBitLockerMapperPath(uid_t uid)
Definition: bitlocker.c:74
StringListDelete
void StringListDelete(stringList_t *stl)
Definition: StringList.c:1184
zuluCryptGetALoopDeviceAssociatedWithAnImageFile
char * zuluCryptGetALoopDeviceAssociatedWithAnImageFile(const char *path)
Definition: create_loop_device.c:174
zuluCryptBitLockerUnmountPath
char * zuluCryptBitLockerUnmountPath(const char *e)
Definition: bitlocker.c:177
StringCopy_2
char * StringCopy_2(const char *str)
Definition: String.c:725
ProcessWaitUntilFinished
int ProcessWaitUntilFinished(process_t *e)
Definition: process.c:614
zuluExit
static int zuluExit(int st, struct crypt_device *cd)
Definition: bitlocker.c:30
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
StringReplaceChar
const char * StringReplaceChar(string_t st, char x, char y)
Definition: String.c:1383
Process
process_t Process(const char *path,...)
Definition: process.c:185
StringRemoveString
const char * StringRemoveString(string_t st, const char *s)
Definition: String.c:1328
_dislocker_fuse_path
static const char * _dislocker_fuse_path()
Definition: bitlocker.c:267
zuluCryptIsDislockerMapperPath
int zuluCryptIsDislockerMapperPath(const char *e)
Definition: bitlocker.c:69
zuluCryptBitLockerlock
int zuluCryptBitLockerlock(string_t mapperPath, char **mount_point)
Definition: bitlocker.c:226
ProcessType_t
Definition: process.c:41
_dislocker_unlock_bitlocker
static int _dislocker_unlock_bitlocker(const open_struct_t *opts, string_t *xt)
Definition: bitlocker.c:340
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
open_struct_t::device
const char * device
Definition: includes.h:69
StringType
Definition: String.c:49
StringLastIndexOfChar_1
static __inline__ ssize_t StringLastIndexOfChar_1(const char *str, char s)
Definition: String.h:982
zuluCryptBitLockerFolderPrefix
const char * zuluCryptBitLockerFolderPrefix(void)
Definition: bitlocker.c:41
String
string_t String(const char *cstring)
Definition: String.c:318
ProcessStart
pid_t ProcessStart(process_t p)
Definition: process.c:337
zuluCryptUseCryptsetupBitLocker
int zuluCryptUseCryptsetupBitLocker(int r)
Definition: bitlocker.c:55
String_1
string_t String_1(const char *cstring,...)
Definition: String.c:363
StringPrependString
const char * StringPrependString(string_t st, string_t xt)
Definition: String.c:1040
zuluCryptBitLockerType
const char * zuluCryptBitLockerType()
Definition: bitlocker.c:36
zuluCryptCryptsetupBitLockerType
const char * zuluCryptCryptsetupBitLockerType()
Definition: bitlocker.c:46
open_struct_t::key
const char * key
Definition: includes.h:73
StringEndsWith_1
int StringEndsWith_1(const char *e, const char *s)
Definition: String.c:790
zuluCryptBitLockerCreateMapperPath
const char * zuluCryptBitLockerCreateMapperPath(string_t e, uid_t uid)
Definition: bitlocker.c:217
StringListType
Definition: StringList.c:33
zuluCryptCloseMapper
int zuluCryptCloseMapper(const char *mapper)
Definition: close_mapper.c:84
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
e
static QString e
Definition: about.cpp:31
StringListSplit
stringList_t StringListSplit(const char *cstring, char splitter)
Definition: StringList.c:428
includes.h
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
zuluCryptUseDislockerBitLocker
int zuluCryptUseDislockerBitLocker(int r)
Definition: bitlocker.c:64
zuluCryptDeviceManagedByDislocker
int zuluCryptDeviceManagedByDislocker(const char *dev, uid_t uid)
Definition: bitlocker.c:141
open_struct_t::mapper_path
const char * mapper_path
Definition: includes.h:72
open_struct_t::key_len
size_t key_len
Definition: includes.h:79
utility::mapperPath
QString mapperPath(const QString &, const QString &component=QString())
Definition: utility.cpp:1289
zuluCryptAttachLoopDeviceToFileUsingFileDescriptor
int zuluCryptAttachLoopDeviceToFileUsingFileDescriptor(int fd_path, int *fd_loop, int mode, string_t *loop_device)
Definition: create_loop_device.c:460
StringAppend
const char * StringAppend(string_t st, const char *s)
Definition: String.c:1052
_cryptsetup_unlock_bitlocker
static int _cryptsetup_unlock_bitlocker(const open_struct_t *opts, string_t *xt)
Definition: bitlocker.c:300
zuluCryptBitLockerVolume
int zuluCryptBitLockerVolume(const char *e)
Definition: bitlocker.c:150
zuluCryptBitLockerUnlock
int zuluCryptBitLockerUnlock(const open_struct_t *opts, string_t *xt)
Definition: bitlocker.c:397
zuluCryptBitLockerFullMapperPath
string_t zuluCryptBitLockerFullMapperPath(uid_t uid, const char *e)
Definition: bitlocker.c:127
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76
zuluCryptUnmountVolume
int zuluCryptUnmountVolume(const char *mapper, char **m_point)
Definition: unmount_volume.c:134
StringPrefixEqual
static __inline__ int StringPrefixEqual(const char *x, const char *y)
Definition: String.h:905
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823
StringVoid
#define StringVoid
Definition: String.h:47