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)  

resolve_paths.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-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 
22 #include <dirent.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 
27 static char * _zuluCryptResolveDevRoot( void )
28 {
29  char * device = NULL ;
30 
31  string_t st = StringGetFromVirtualFile( "/proc/cmdline" ) ;
32 
33  stringList_t stl = StringListStringSplit( st,' ' ) ;
34 
36  StringListIterator end ;
37 
38  StringDelete( &st ) ;
39 
40  StringListGetIterators( stl,&it,&end ) ;
41 
42  while( it != end ){
43 
44  st = *it ;
45 
46  it++ ;
47 
48  if( StringStartsWith( st,"root=/dev/" ) ){
49 
50  device = zuluCryptResolvePath( StringContent( st ) + 5 ) ;
51 
52  break ;
53 
54  }else if( StringStartsWith( st,"root=UUID=" ) ){
55 
56  /*
57  * zuluCryptDeviceFromUUID() is defined in blkid_evaluate_tag.c
58  */
59  device = zuluCryptDeviceFromUUID( StringContent( st ) + 10 ) ;
60 
61  break ;
62  }
63  }
64 
65  StringListDelete( &stl ) ;
66 
67  return device ;
68 }
69 
70 static string_t zuluExit( DIR * dir,string_t st )
71 {
72  if( dir != NULL ){
73 
74  closedir( dir ) ;
75  }
76 
77  return st ;
78 }
79 
80 /*
81  * raid path can be in format /dev/mdX or /dev/md/X.
82  * We prefer the latter and if given the former,convert it to the latter if possible
83  */
84 string_t zuluCryptResolveMDPath_1( const char * path )
85 {
86  struct dirent * entry ;
87 
88  char * e ;
89 
90  const char * f = "/dev/md/" ;
91 
92  DIR * dir = opendir( f ) ;
93 
94  string_t st = String( f ) ;
95 
96  if( dir != NULL ){
97 
98  while( ( entry = readdir( dir ) ) != NULL ){
99 
100  f = entry->d_name ;
101 
102  if( !StringAtLeastOneMatch_1( f,".","..",NULL ) ){
103 
104  e = zuluCryptRealPath( StringAppendAt( st,8,f ) ) ;
105 
106  if( StringsAreEqual( path,e ) ){
107 
108  StringFree( e ) ;
109 
110  return zuluExit( dir,st ) ;
111  }else{
112  StringFree( e ) ;
113  }
114  }
115  }
116  }
117 
118  StringReplace( st,path ) ;
119 
120  return zuluExit( dir,st ) ;
121 }
122 
123 char * zuluCryptResolveMDPath( const char * path )
124 {
125  string_t st = zuluCryptResolveMDPath_1( path ) ;
126  return StringDeleteHandle( &st ) ;
127 }
128 
129 /*
130  * An assumption is made here that the path is an LVM path if "path"
131  * is in /dev/mapper/abc-def format and there exist a path at /dev/abc/def.
132  *
133  * Info in lvm path structures can be found here:
134  * https://www.redhat.com/archives/linux-lvm/2014-January/msg00014.html
135  */
137 {
138  const char * e ;
139  const char ** z ;
140 
141  struct stat st ;
142 
143  StringIterator a ;
144  StringIterator b ;
145  StringIterator c ;
146  StringIterator d ;
147 
148  string_t q = String( path ) ;
149 
150  StringGetIterators( q,&c,&d ) ;
151 
152  for( c = c + 3 ; c < d ; c++ ){
153 
154  a = c - 2 ;
155  b = c - 1 ;
156 
157  if( *a != '-' && *b == '-' && *c != '-' ){
158  /*
159  * found a place with a single dash,replace the dash with a slash
160  */
161  *b = '/' ;
162  /*
163  * replace double dashes if present.
164  */
165  z = StringPointer( q ) ;
166 
167  while( StringHasComponent( *z,"--" ) ){
168 
169  StringReplaceString( q,"--","-" ) ;
170  }
171 
172  e = StringReplaceString( q,"/dev/mapper/","/dev/" ) ;
173 
174  if( stat( e,&st ) == 0 ){
175  /*
176  * The path appear to be an LVM path since
177  * "/dev/mapper/ABC-DEF" input path has a corresponding
178  * "/dev/ABC/DEF" path
179  */
180  }else{
181  /*
182  * Not an LVM volume,replace the string to its original
183  */
184  StringReplace( q,path ) ;
185  }
186  break ;
187  }
188  }
189 
190  return q ;
191 }
192 
193 static char * _convert_if_path_is_lvm( const char * path )
194 {
196  return StringDeleteHandle( &st ) ;
197 }
198 
199 /*
200  * dm path is a path like "/dev/dm-5".
201  * this routine will transform the path to /dev/abc/def if the path is
202  * an lvm path or to /dev/mapper/xyz if the volume is any other device manager volume.
203  */
204 char * zuluCryptResolveDMPath( const char * path )
205 {
206  char * e = zuluCryptRealPath( path ) ;
207 
208  char * f = _convert_if_path_is_lvm( e ) ;
209 
210  StringFree( e ) ;
211 
212  return f ;
213 }
214 
215 char * zuluCryptResolvePath( const char * path )
216 {
217  char * e ;
218  char * f ;
219 
220  if( StringsAreEqual( path,"/dev/root" ) ){
221 
223 
224  if( e == NULL ){
225 
226  return StringCopy_2( path ) ;
227  }else{
228  return e ;
229  }
230  }else if( StringPrefixEqual( path,"/dev/disk/by-" ) ){
231  /*
232  * zuluCryptRealPath() is defined in real_path.c
233  */
234  e = zuluCryptRealPath( path ) ;
235 
236  if( e == NULL ){
237 
238  return StringCopy_2( path ) ;
239  }else{
240  if( StringPrefixEqual( e,"/dev/mapper/" ) ){
241 
242  f = _convert_if_path_is_lvm( e ) ;
243  StringFree( e ) ;
244 
245  return f ;
246  }else{
247  return e ;
248  }
249  }
250  }else if( StringPrefixEqual( path,"/dev/mapper/" ) ){
251 
252  return _convert_if_path_is_lvm( path ) ;
253 
254  }else if( StringPrefixEqual( path,"/dev/md" ) ){
255 
256  return zuluCryptResolveMDPath( path ) ;
257 
258  }else if( StringPrefixEqual( path,"/dev/dm-" ) ){
259 
260  return zuluCryptResolveDMPath( path ) ;
261 
262  }else if( zuluCryptNoPartitionLoopDevice( path ) ){
263  /*
264  * zuluCryptLoopDeviceAddress() is defined in create_loop_device.c
265  */
266  return zuluCryptLoopDeviceAddress( path ) ;
267  }else{
268  return StringCopy_2( path ) ;
269  }
270 }
271 
272 string_t zuluCryptResolvePath_1( const char * path )
273 {
274  char * e = zuluCryptResolvePath( path ) ;
275  return StringInherit( &e ) ;
276 }
277 
278 string_t zuluCryptResolvePath_2( const char * path )
279 {
280  if( zuluCryptNoPartitionLoopDevice( path ) ){
281 
282  return String( path ) ;
283  }else{
284  return zuluCryptResolvePath_1( path ) ;
285  }
286 }
287 
288 char * zuluCryptResolvePath_3( const char * path )
289 {
290  if( zuluCryptNoPartitionLoopDevice( path ) ){
291 
292  /*
293  * zuluCryptLoopDeviceAddress_1() is defined in create_loop_device.c
294  */
295  return zuluCryptLoopDeviceAddress_1( path ) ;
296  }else{
297  return zuluCryptResolvePath( path ) ;
298  }
299 }
300 
301 char * zuluCryptResolvePath_4( const char * path )
302 {
303  if( StringPrefixEqual( path,"/dev/loop" ) ){
304 
305  return StringCopy_2( path ) ;
306  }else{
307  return zuluCryptResolvePath( path ) ;
308  }
309 }
310 
311 typedef struct{
312 
314 
315  int result_0 ;
316  int( *function_0 )( const char *,const resolve_path_t * ) ;
317  int error_0 ;
318 
319  char * result_1 ;
320  char *( *function_1 )( const char *,const resolve_path_t * ) ;
321  char * error_1 ;
322 
323 } arguments;
324 
325 static void _get_result_0( const char * device,arguments * args )
326 {
327  if( args->function_0 != NULL ){
328 
329  args->result_0 = args->function_0( device,args->opts ) ;
330  }else{
331  args->result_1 = args->function_1( device,args->opts ) ;
332  }
333 }
334 
335 static void _get_error( arguments * args )
336 {
337  if( args->function_0 != NULL ){
338 
339  args->result_0 = args->error_0 ;
340  }else{
341  args->result_1 = args->error_1 ;
342  }
343 }
344 
345 static void _get_result( arguments * args )
346 {
347  string_t st ;
348 
349  int fd ;
350 
351  const char * device = args->opts->device ;
352 
353  if( StringPrefixEqual( device,"/dev/" ) ){
354 
355  _get_result_0( device,args ) ;
356  }else{
357  /*
358  * zuluCryptAttachLoopDeviceToFile() is defined in create_loop_device.c
359  */
360  if( zuluCryptAttachLoopDeviceToFile( device,args->opts->open_mode,&fd,&st ) ){
361 
362  _get_result_0( StringContent( st ),args ) ;
363 
364  StringDelete( &st ) ;
365 
366  close( fd ) ;
367  }else{
368  _get_error( args ) ;
369  }
370  }
371 }
372 
373 static int _get_result_1( int( *function )( const char *,const resolve_path_t * ),
374  const resolve_path_t * opts,int error )
375 {
376  arguments args ;
377 
378  memset( &args,'\0',sizeof( args ) ) ;
379 
380  args.function_0 = function ;
381  args.error_0 = error ;
382  args.opts = opts ;
383 
384  _get_result( &args ) ;
385 
386  return args.result_0 ;
387 }
388 
389 int zuluCryptResolveDevicePath( int( *function )( const char *,const resolve_path_t * ),
390  const resolve_path_t * opts )
391 {
392  return _get_result_1( function,opts,opts->error_value ) ;
393 }
394 
395 int zuluCryptResolveDevicePath_0( int( *function )( const char *,const resolve_path_t * ),
396  const open_struct_t * opt,int error )
397 {
398  /*
399  * resolve_path_t is defined in includes.h
400  */
401  resolve_path_t opts ;
402 
403  memset( &opts,'\0',sizeof( opts ) ) ;
404 
405  opts.args = opt ;
406  opts.device = opt->device ;
407 
408  if( StringHasComponent( opt->m_opts,"ro" ) ){
409 
410  opts.open_mode = O_RDONLY ;
411  }else{
412  opts.open_mode = O_RDWR ;
413  }
414 
415  return _get_result_1( function,&opts,error ) ;
416 }
417 
418 char * zuluCryptResolveDevicePath_1( char * ( *function )( const char *,const resolve_path_t * ),
419  const resolve_path_t * opts )
420 {
421  arguments args ;
422 
423  memset( &args,'\0',sizeof( args ) ) ;
424 
425  args.function_1 = function ;
426  args.error_1 = opts->error_value_1 ;
427  args.opts = opts ;
428 
429  _get_result( &args ) ;
430 
431  return args.result_1 ;
432 }
StringReplace
const char * StringReplace(string_t st, const char *s)
Definition: String.c:984
StringGetFromVirtualFile
string_t StringGetFromVirtualFile(const char *path)
Definition: String.c:2050
zuluCryptDeviceFromUUID
char * zuluCryptDeviceFromUUID(const char *uuid)
Definition: blkid_evaluate_tag.c:42
open_struct_t
Definition: includes.h:68
zuluCryptResolveMDPath_1
string_t zuluCryptResolveMDPath_1(const char *path)
Definition: resolve_paths.c:84
zuluCryptResolvePath_2
string_t zuluCryptResolvePath_2(const char *path)
Definition: resolve_paths.c:278
arguments
Definition: add_key.c:25
StringStartsWith
int StringStartsWith(string_t st, const char *s)
Definition: String.c:821
arguments::opts
const resolve_path_t * opts
Definition: resolve_paths.c:313
arguments::error_1
char * error_1
Definition: resolve_paths.c:321
zuluCryptResolvePath
char * zuluCryptResolvePath(const char *path)
Definition: resolve_paths.c:215
StringListGetIterators
void StringListGetIterators(stringList_t stl, StringListIterator *begin, StringListIterator *end)
Definition: StringList.c:210
_zuluCryptResolveDevRoot
static char * _zuluCryptResolveDevRoot(void)
Definition: resolve_paths.c:27
zuluCryptLoopDeviceAddress_1
char * zuluCryptLoopDeviceAddress_1(const char *device)
Definition: create_loop_device.c:140
zuluCryptNoPartitionLoopDevice
int zuluCryptNoPartitionLoopDevice(const char *e)
Definition: blkid_evaluate_tag.c:147
resolve_path_t::args
const void * args
Definition: includes.h:119
StringListDelete
void StringListDelete(stringList_t *stl)
Definition: StringList.c:1184
zuluCryptResolveMDPath
char * zuluCryptResolveMDPath(const char *path)
Definition: resolve_paths.c:123
StringCopy_2
char * StringCopy_2(const char *str)
Definition: String.c:725
StringListStringSplit
stringList_t StringListStringSplit(string_t st, char splitter)
Definition: StringList.c:483
_get_error
static void _get_error(arguments *args)
Definition: resolve_paths.c:335
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
zuluCryptResolvePath_1
string_t zuluCryptResolvePath_1(const char *path)
Definition: resolve_paths.c:272
zuluCryptResolvePath_3
char * zuluCryptResolvePath_3(const char *path)
Definition: resolve_paths.c:288
arguments
struct arguments arguments
_get_result_1
static int _get_result_1(int(*function)(const char *, const resolve_path_t *), const resolve_path_t *opts, int error)
Definition: resolve_paths.c:373
_get_result
static void _get_result(arguments *args)
Definition: resolve_paths.c:345
args
Definition: open_plain.c:25
StringDeleteHandle
char * StringDeleteHandle(string_t *xt)
Definition: String.c:233
zuluCryptResolveDevicePath_1
char * zuluCryptResolveDevicePath_1(char *(*function)(const char *, const resolve_path_t *), const resolve_path_t *opts)
Definition: resolve_paths.c:418
StringInherit
string_t StringInherit(char **data)
Definition: String.c:391
resolve_path_t::error_value_1
char * error_value_1
Definition: includes.h:117
StringAtLeastOneMatch_1
int StringAtLeastOneMatch_1(const char *x,...)
Definition: String.c:1504
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
StringAppendAt
const char * StringAppendAt(string_t st, size_t x, const char *s)
Definition: String.c:960
zuluCryptResolvePath_4
char * zuluCryptResolvePath_4(const char *path)
Definition: resolve_paths.c:301
_convert_if_path_is_lvm
static char * _convert_if_path_is_lvm(const char *path)
Definition: resolve_paths.c:193
resolve_path_t::error_value
int error_value
Definition: includes.h:116
zuluCryptConvertIfPathIsLVM
string_t zuluCryptConvertIfPathIsLVM(const char *path)
Definition: resolve_paths.c:136
arguments::result_1
char * result_1
Definition: resolve_paths.c:319
String
string_t String(const char *cstring)
Definition: String.c:318
_get_result_0
static void _get_result_0(const char *device, arguments *args)
Definition: resolve_paths.c:325
zuluExit
static string_t zuluExit(DIR *dir, string_t st)
Definition: resolve_paths.c:70
resolve_path_t::device
const char * device
Definition: includes.h:115
zuluCryptResolveDevicePath_0
int zuluCryptResolveDevicePath_0(int(*function)(const char *, const resolve_path_t *), const open_struct_t *opt, int error)
Definition: resolve_paths.c:395
StringReplaceString
const char * StringReplaceString(string_t st, const char *x, const char *s)
Definition: String.c:1318
StringListType
Definition: StringList.c:33
zuluCryptResolveDevicePath
int zuluCryptResolveDevicePath(int(*function)(const char *, const resolve_path_t *), const resolve_path_t *opts)
Definition: resolve_paths.c:389
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
arguments::error_0
int error_0
Definition: resolve_paths.c:317
e
static QString e
Definition: about.cpp:31
StringIterator
char * StringIterator
Definition: String.h:49
includes.h
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
arguments::result_0
int result_0
Definition: resolve_paths.c:315
resolve_path_t::open_mode
int open_mode
Definition: includes.h:118
zuluCryptRealPath
char * zuluCryptRealPath(const char *path)
Definition: real_path.c:28
StringGetIterators
void StringGetIterators(string_t st, StringIterator *begin, StringIterator *end)
Definition: String.c:133
zuluCryptAttachLoopDeviceToFile
int zuluCryptAttachLoopDeviceToFile(const char *path, int mode, int *loop_fd, string_t *loop_device)
Definition: create_loop_device.c:425
StringPointer
static const __inline__ char ** StringPointer(string_t st)
Definition: String.h:1006
zuluCryptResolveDMPath
char * zuluCryptResolveDMPath(const char *path)
Definition: resolve_paths.c:204
resolve_path_t
Definition: includes.h:114
zuluCryptLoopDeviceAddress
char * zuluCryptLoopDeviceAddress(const char *device)
Definition: create_loop_device.c:134
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76
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