File: bitlocker.c

package info (click to toggle)
zulucrypt 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,908 kB
  • sloc: ansic: 25,142; cpp: 23,281; makefile: 24; xml: 10; sh: 2
file content (405 lines) | stat: -rw-r--r-- 7,788 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 *
 *  Copyright (c) 2019
 *  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 <pwd.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <libcryptsetup.h>

static int zuluExit( int st,struct crypt_device * cd )
{
	crypt_free( cd ) ;
	return st ;
}

const char * zuluCryptBitLockerType()
{
	return "crypto_BitLocker" ;
}

const char * zuluCryptBitLockerFolderPrefix( void )
{
	return "cryptoBitlocker" ;
}

const char * zuluCryptCryptsetupBitLockerType()
{
#ifdef CRYPT_BITLK
	return CRYPT_BITLK ;
#else
	return "" ;
#endif
}

int zuluCryptUseCryptsetupBitLocker( int r )
{
#ifdef CRYPT_BITLK
	return r ;
#else
	return 0 ;
#endif
}

int zuluCryptUseDislockerBitLocker( int r )
{
	return !zuluCryptUseCryptsetupBitLocker( r ) ;
}

int zuluCryptIsDislockerMapperPath( const char * e )
{
	return StringEndsWith_1( e,"/dislocker-file" ) ;
}

string_t zuluCryptBitLockerMapperPath( uid_t uid )
{
	const char * m = zuluCryptBitLockerFolderPrefix() ;

	return String_1( "/run/media/private/",getpwuid( uid )->pw_name,"/",m,NULL ) ;
}

string_t zuluCryptBitLockerMapperName( const char * e )
{
	string_t st ;

	if( zuluCryptNoPartitionLoopDevice( e ) ){

		st = zuluCryptLoopDeviceAddress_2( e ) ;
	}else{
		st = String( e ) ;
	}

	StringReplaceChar( st,'/','_' ) ;

	return st ;
}

string_t zuluCryptBitLockerVolumeFS( const char * e )
{
	int fd = open( e,O_RDONLY ) ;

	int r  ;

	int fd_loop ;

	string_t st = StringVoid ;

	string_t xt = StringVoid ;

	if( fd != -1 ){

		r = zuluCryptAttachLoopDeviceToFileUsingFileDescriptor( fd,&fd_loop,O_RDONLY,&st ) ;

		if( r == 1 ){

			xt = zulucryptGetBlkidFileSystem( StringContent( st ) ) ;
		}

		close( fd ) ;
		close( fd_loop ) ;
	}

	StringDelete( &st ) ;

	return xt ;
}

string_t zuluCryptBitLockerFullMapperPath( uid_t uid,const char * e )
{
	string_t a = zuluCryptBitLockerMapperPath( uid ) ;
	string_t b = zuluCryptBitLockerMapperName( e ) ;

	StringAppendString( a,b ) ;

	StringAppend( a,"/dislocker-file" ) ;

	StringDelete( &b ) ;

	return a ;
}

int zuluCryptDeviceManagedByDislocker( const char * dev,uid_t uid )
{
	struct stat st ;
	string_t p = zuluCryptBitLockerFullMapperPath( uid,dev ) ;
	int r = stat( StringContent( p ),&st ) ;
	StringDelete( &p ) ;
	return r == 0 ;
}

int zuluCryptBitLockerVolume( const char * e )
{
	struct crypt_device * cd = NULL ;

	int r ;

	if( zuluCryptIsDislockerMapperPath( e ) ){

		return 1 ;
	}else{
		if( !StringPrefixEqual( e,crypt_get_dir() ) ){

			return 0 ;
		}
		if( crypt_init_by_name( &cd,e ) != 0 ){

			return 0 ;
		}

		r = StringsAreEqual( crypt_get_type( cd ),zuluCryptCryptsetupBitLockerType() ) ;

		crypt_free( cd ) ;

		return r ;
	}
}

char * zuluCryptBitLockerUnmountPath( const char * e )
{
	/*
	 * e will contain something like "/woof/bar/meaw/dislocker-file"
	 * and we want to remove the "/dislocker-file" part.
	 */
	char * m = StringCopy_2( e ) ;

	ssize_t s = StringLastIndexOfChar_1( m,'/' ) ;

	if( s != -1 ){

		*( m + s ) = '\0' ;
	}

	return m ;
}

int zuluCryptBitLockerVolume_1( const char * e )
{
	stringList_t stl = StringListSplit( e,' ' ) ;
	int s = zuluCryptBitLockerVolume( StringListContentAtFirstPlace( stl ) ) ;
	StringListDelete( &stl ) ;
	return s ;
}

string_t zuluCryptBitLockerResolveMapperPath( const char * e,uid_t uid )
{
	string_t q = String( e ) ;
	string_t z = zuluCryptBitLockerMapperPath( uid ) ;

	StringRemoveString( q,StringContent( z ) ) ;
	StringRemoveString( q,"/dislocker-file" ) ;

	StringReplaceChar( q,'_','/' ) ;
	StringDelete( &z ) ;

	return q ;
}

const char * zuluCryptBitLockerCreateMapperPath( string_t e,uid_t uid )
{
	string_t st = zuluCryptBitLockerMapperPath( uid ) ;
	StringReplaceChar( e,'/','_' ) ;
	StringPrependString( e,st ) ;
	StringDelete( &st ) ;
	return StringAppend( e,"/dislocker-file" ) ;
}

int zuluCryptBitLockerlock( string_t mapperPath,char ** mount_point )
{
	const char * n = StringContent( mapperPath ) ;

	int s ;

	char * e = NULL ;

	char * m = zuluCryptGetALoopDeviceAssociatedWithAnImageFile( n ) ;

	int r ;

	if( m == NULL ){

		return 6 ;
	}

	r = zuluCryptUnmountVolume( m,&e ) ;

	StringFree( m ) ;

	if( r == 0 ){

		s = zuluCryptCloseMapper( n ) ;

		if( s == 0 ){

			if( mount_point ){

				*mount_point = e ;
			}
		}else{
			StringFree( e ) ;
		}

		return s ;
	}else{
		return r ;
	}
}

static const char * _dislocker_fuse_path()
{
	struct stat st ;
	const char * e ;
	int i ;

	const char * exe[] = { "/usr/bin/dislocker-fuse",
			       "/usr/sbin/dislocker-fuse",
			       "/usr/local/bin/dislocker-fuse",
			       "/usr/local/sbin/dislocker-fuse",
			       "/bin/dislocker-fuse",
			       "/sbin/dislocker-fuse",
			       "/opt/bin/dislocker-fuse",
			       "/opt/sbin/dislocker-fuse",
			       NULL } ;

	for( i = 0 ; *( exe + i ) != NULL ; i++ ){

		e = *( exe + i ) ;

		if( stat( e,&st ) == 0 ){

			fprintf( stderr,"Looking for dislocker-fuse at \"%s\" : PASSED\n",e ) ;

			return e ;
		}else{
			fprintf( stderr,"Looking for dislocker-fuse at \"%s\" : FAILED\n",e ) ;
		}
	}

	return NULL ;
}

static int _cryptsetup_unlock_bitlocker( const open_struct_t * opts,string_t * xt )
{
	int r ;

	struct crypt_device * cd = NULL ;

	uint32_t flags = 0 ;

	if( crypt_init( &cd,opts->device ) != 0 ){

		return 4 ;
	}
	if( crypt_load( cd,zuluCryptCryptsetupBitLockerType(),NULL ) != 0 ){

		return zuluExit( 4,cd ) ;
	}
	if( StringHasComponent( opts->m_opts,"ro" ) ){

		flags = CRYPT_ACTIVATE_READONLY ;
	}else{
		flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
	}

	r = crypt_activate_by_passphrase( cd,
					  opts->mapper_name,
					  CRYPT_ANY_SLOT,
					  opts->key,
					  opts->key_len,
					  flags ) ;

	if( r == 0 ){

		*xt = String( opts->mapper_path ) ;

		return zuluExit( 0,cd ) ;
	}else{
		return zuluExit( 4,cd ) ;
	}
}

static int _dislocker_unlock_bitlocker( const open_struct_t * opts,string_t * xt )
{
	int r ;

	string_t st ;

	const char * mapper_path ;

	process_t p ;

	string_t m ;

	char * env[ 2 ] = { NULL,NULL } ;

	const char * exe = _dislocker_fuse_path() ;

	if( exe == NULL ){

		return 15 ;
	}

	st = String_1( opts->mapper_path,opts->mapper_name,NULL ) ;

	mapper_path = StringContent( st ) ;

	if( mkdir( mapper_path,S_IRWXU ) != 0 ){

		StringDelete( &st ) ;
		return 2 ;
	}else{
		m = String_1( "DISLOCKER_PASSWORD=",opts->key,NULL ) ;

		env[ 0 ] = ( char * )StringContent( m ) ;

		p = Process( exe,opts->device,"-u","--","-oallow_root",mapper_path,NULL ) ;

		ProcessSetEnvironmentalVariable( p,( char * const * )env ) ;

		ProcessStart( p ) ;

		r = ProcessWaitUntilFinished( &p ) ;

		StringDelete( &m ) ;

		if( r == 0 ){

			StringAppend( st,"/dislocker-file" ) ;
			*xt = st ;
			return 0 ;
		}else{
			rmdir( mapper_path ) ;
			StringDelete( &st ) ;
			return 4 ;
		}
	}
}

int zuluCryptBitLockerUnlock( const open_struct_t * opts,string_t * xt )
{
	if( zuluCryptUseCryptsetupBitLocker( opts->use_cryptsetup_for_bitlocker ) ){

		return _cryptsetup_unlock_bitlocker( opts,xt ) ;
	}else{
		return _dislocker_unlock_bitlocker( opts,xt ) ;
	}
}