File: resolve_paths.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 (431 lines) | stat: -rw-r--r-- 8,849 bytes parent folder | download
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 *
 *  Copyright (c) 2014-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 <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

static char * _zuluCryptResolveDevRoot( void )
{
	char * device = NULL ;

	string_t st = StringGetFromVirtualFile( "/proc/cmdline" ) ;

	stringList_t stl = StringListStringSplit( st,' ' ) ;

	StringListIterator it ;
	StringListIterator end ;

	StringDelete( &st ) ;

	StringListGetIterators( stl,&it,&end ) ;

	while( it != end ){

		st = *it ;

		it++ ;

		if( StringStartsWith( st,"root=/dev/" ) ){

			device = zuluCryptResolvePath( StringContent( st ) + 5 ) ;

			break ;

		}else if( StringStartsWith( st,"root=UUID=" ) ){

			/*
			 * zuluCryptDeviceFromUUID() is defined in blkid_evaluate_tag.c
			 */
			device = zuluCryptDeviceFromUUID( StringContent( st ) + 10 ) ;

			break ;
		}
	}

	StringListDelete( &stl ) ;

	return device ;
}

static string_t zuluExit( DIR * dir,string_t st )
{
	if( dir != NULL ){

		closedir( dir ) ;
	}

	return st ;
}

/*
 * raid path can be in format /dev/mdX or /dev/md/X.
 * We prefer the latter and if given the former,convert it to the latter if possible
 */
string_t zuluCryptResolveMDPath_1( const char * path )
{
	struct dirent * entry ;

	char * e ;

	const char * f = "/dev/md/" ;

	DIR * dir = opendir( f ) ;

	string_t st = String( f ) ;

	if( dir != NULL ){

		while( ( entry = readdir( dir ) ) != NULL ){

			f = entry->d_name ;

			if( !StringAtLeastOneMatch_1( f,".","..",NULL ) ){

				e = zuluCryptRealPath( StringAppendAt( st,8,f ) ) ;

				if( StringsAreEqual( path,e ) ){

					StringFree( e ) ;

					return zuluExit( dir,st ) ;
				}else{
					StringFree( e ) ;
				}
			}
		}
	}

	StringReplace( st,path ) ;

	return zuluExit( dir,st ) ;
}

char * zuluCryptResolveMDPath( const char * path )
{
	string_t st = zuluCryptResolveMDPath_1( path ) ;
	return StringDeleteHandle( &st ) ;
}

/*
 * An assumption is made here that the path is an LVM path if "path"
 * is in /dev/mapper/abc-def format and there exist a path at /dev/abc/def.
 *
 * Info in lvm path structures can be found here:
 * https://www.redhat.com/archives/linux-lvm/2014-January/msg00014.html
 */
string_t zuluCryptConvertIfPathIsLVM( const char * path )
{
	const char * e ;
	const char ** z ;

	struct stat st ;

	StringIterator a ;
	StringIterator b ;
	StringIterator c ;
	StringIterator d ;

	string_t q = String( path ) ;

	StringGetIterators( q,&c,&d ) ;

	for( c = c + 3 ; c < d ; c++ ){

		a = c - 2 ;
		b = c - 1 ;

		if( *a != '-' && *b == '-' && *c != '-' ){
			/*
			 * found a place with a single dash,replace the dash with a slash
			 */
			*b = '/' ;
			/*
			 * replace double dashes if present.
			 */
			z = StringPointer( q ) ;

			while( StringHasComponent( *z,"--" ) ){

				StringReplaceString( q,"--","-" ) ;
			}

			e = StringReplaceString( q,"/dev/mapper/","/dev/" ) ;

			if( stat( e,&st ) == 0 ){
				/*
				 * The path appear to be an LVM path since
				 * "/dev/mapper/ABC-DEF" input path has a corresponding
				 * "/dev/ABC/DEF" path
				 */
			}else{
				/*
				 * Not an LVM volume,replace the string to its original
				 */
				StringReplace( q,path ) ;
			}
			break ;
		}
	}

	return q ;
}

static char * _convert_if_path_is_lvm( const char * path )
{
	string_t st = zuluCryptConvertIfPathIsLVM( path ) ;
	return StringDeleteHandle( &st ) ;
}

/*
 * dm path is a path like "/dev/dm-5".
 * this routine will transform the path to /dev/abc/def if the path is
 * an lvm path or to /dev/mapper/xyz if the volume is any other device manager volume.
 */
char * zuluCryptResolveDMPath( const char * path )
{
	char * e = zuluCryptRealPath( path ) ;

	char * f = _convert_if_path_is_lvm( e ) ;

	StringFree( e ) ;

	return f ;
}

char * zuluCryptResolvePath( const char * path )
{
	char * e ;
	char * f ;

	if( StringsAreEqual( path,"/dev/root" ) ){

		e = _zuluCryptResolveDevRoot() ;

		if( e == NULL ){

			return StringCopy_2( path ) ;
		}else{
			return e ;
		}
	}else if( StringPrefixEqual( path,"/dev/disk/by-" ) ){
		/*
		 * zuluCryptRealPath() is defined in real_path.c
		 */
		e = zuluCryptRealPath( path ) ;

		if( e == NULL ){

			return StringCopy_2( path ) ;
		}else{
			if( StringPrefixEqual( e,"/dev/mapper/" ) ){

				f = _convert_if_path_is_lvm( e ) ;
				StringFree( e ) ;

				return f ;
			}else{
				return e ;
			}
		}
	}else if( StringPrefixEqual( path,"/dev/mapper/" ) ){

		return _convert_if_path_is_lvm( path ) ;

	}else if( StringPrefixEqual( path,"/dev/md" ) ){

		return zuluCryptResolveMDPath( path ) ;

	}else if( StringPrefixEqual( path,"/dev/dm-" ) ){

		return zuluCryptResolveDMPath( path ) ;

	}else if( zuluCryptNoPartitionLoopDevice( path ) ){
		/*
		 * zuluCryptLoopDeviceAddress() is defined in create_loop_device.c
		 */
		return zuluCryptLoopDeviceAddress( path ) ;
	}else{
		return StringCopy_2( path ) ;
	}
}

string_t zuluCryptResolvePath_1( const char * path )
{
	char * e = zuluCryptResolvePath( path ) ;
	return StringInherit( &e ) ;
}

string_t zuluCryptResolvePath_2( const char * path )
{
	if( zuluCryptNoPartitionLoopDevice( path ) ){

		return String( path ) ;
	}else{
		return zuluCryptResolvePath_1( path ) ;
	}
}

char * zuluCryptResolvePath_3( const char * path )
{
	if( zuluCryptNoPartitionLoopDevice( path ) ){

		/*
		 * zuluCryptLoopDeviceAddress_1() is defined in create_loop_device.c
		 */
		return zuluCryptLoopDeviceAddress_1( path ) ;
	}else{
		return zuluCryptResolvePath( path ) ;
	}
}

char * zuluCryptResolvePath_4( const char * path )
{
	if( StringPrefixEqual( path,"/dev/loop" ) ){

		return StringCopy_2( path ) ;
	}else{
		return zuluCryptResolvePath( path ) ;
	}
}

typedef struct{

	int result_0 ;
	int error_0 ;

	char * result_1 ;
	char * error_1 ;

	const resolve_path_t * opts ;
	int( *function_0 )( const char *,const resolve_path_t * ) ;
	char *( *function_1 )( const char *,const resolve_path_t * ) ;
} arguments;

static void _get_result_0( const char * device,arguments * args )
{
	if( args->function_0 != NULL ){

		args->result_0 = args->function_0( device,args->opts ) ;
	}else{
		args->result_1 = args->function_1( device,args->opts ) ;
	}
}

static void _get_error( arguments * args )
{
	if( args->function_0 != NULL ){

		args->result_0 = args->error_0 ;
	}else{
		args->result_1 = args->error_1 ;
	}
}

static void _get_result( arguments * args )
{
	string_t st ;

	int fd ;

	const char * device = args->opts->device ;

	if( StringPrefixEqual( device,"/dev/" ) ){

		_get_result_0( device,args ) ;
	}else{
		/*
		 * zuluCryptAttachLoopDeviceToFile() is defined in create_loop_device.c
		 */
		if( zuluCryptAttachLoopDeviceToFile( device,args->opts->open_mode,&fd,&st ) ){

			_get_result_0( StringContent( st ),args ) ;

			StringDelete( &st ) ;

			close( fd ) ;
		}else{
			_get_error( args ) ;
		}
	}
}

static int _get_result_1( int( *function )( const char *,const resolve_path_t * ),
			  const resolve_path_t * opts,int error )
{
	arguments args ;

	memset( &args,'\0',sizeof( args ) ) ;

	args.function_0 = function ;
	args.error_0    = error ;
	args.opts       = opts ;

	_get_result( &args ) ;

	return args.result_0 ;
}

int zuluCryptResolveDevicePath( int( *function )( const char *,const resolve_path_t * ),
				const resolve_path_t * opts )
{
	return _get_result_1( function,opts,opts->error_value ) ;
}

int zuluCryptResolveDevicePath_0( int( *function )( const char *,const resolve_path_t * ),
				  const open_struct_t * opt,int error )
{
	/*
	 * resolve_path_t is defined in includes.h
	 */
	resolve_path_t opts ;

	memset( &opts,'\0',sizeof( opts ) ) ;

	opts.args   = opt ;
	opts.device = opt->device ;

	if( StringHasComponent( opt->m_opts,"ro" ) ){

		opts.open_mode = O_RDONLY ;
	}else{
		opts.open_mode = O_RDWR ;
	}

	return _get_result_1( function,&opts,error ) ;
}

char * zuluCryptResolveDevicePath_1( char * ( *function )( const char *,const resolve_path_t * ),
				     const resolve_path_t * opts )
{
	arguments args ;

	memset( &args,'\0',sizeof( args ) ) ;

	args.function_1 = function ;
	args.error_1    = opts->error_value_1 ;
	args.opts       = opts ;

	_get_result( &args ) ;

	return args.result_1 ;
}