char * zuluCryptLoopDeviceAddress_1( const char * device ) { int fd ; char * path ; struct loop_info64 l_info ; string_t st = String_1( "/sys/block/",device + 5,"/loop/backing_file",NULL ) ; string_t xt = _StringGetFromVirtualFile( &st ) ; if( xt == StringVoid ){ memset( &l_info,'\0',sizeof( struct loop_info64 ) ) ; fd = open( device,O_RDONLY ) ; ioctl( fd,LOOP_GET_STATUS64,&l_info ) ; path = zuluCryptRealPath( ( char * ) l_info.lo_file_name ) ; close( fd ) ; return path ; }else{ StringRemoveRight( xt,1 ) ; return StringDeleteHandle( &xt ) ; } }
string_t zuluCryptLoopDeviceAddress_2( const char * device ) { int fd ; char * path ; struct loop_info64 l_info ; string_t st = String_1( "/sys/block/",device + 5,"/loop/backing_file",NULL ) ; string_t xt = _StringGetFromVirtualFile( &st ) ; if( xt == StringVoid ){ memset( &l_info,'\0',sizeof( struct loop_info64 ) ) ; fd = open( device,O_RDONLY ) ; ioctl( fd,LOOP_GET_STATUS64,&l_info ) ; path = zuluCryptRealPath( ( char * ) l_info.lo_file_name ) ; close( fd ) ; xt = StringInherit( &path ) ; }else{ StringRemoveRight( xt,1 ) ; } /* * zuluCryptEncodeMountEntry() is defined in mount_volume.c */ zuluCryptEncodeMountEntry( xt ) ; return xt ; }
static stringList_t _zuluCryptAddMDRAIDVolumes( stringList_t stl ) { DIR * dir = opendir( "/dev/md/" ) ; struct dirent * entry ; char * e ; const char * f ; string_t st ; if( dir != NULL ){ while( ( entry = readdir( dir ) ) != NULL ){ f = entry->d_name ; if( !StringAtLeastOneMatch_1( f,".","..","md-device-map",NULL ) ){ st = String( "/dev/md/" ) ; e = zuluCryptRealPath( StringAppend( st,f ) ) ; if( e != NULL ){ StringListRemoveString( stl,e ) ; StringFree( e ) ; } stl = StringListAppendString_1( stl,&st ) ; } } closedir( dir ) ; } return stl ; }
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( StringPrefixEqual( path,"/dev/loop" ) ){ /* * zuluCryptLoopDeviceAddress() is defined in create_loop_device.c */ return zuluCryptLoopDeviceAddress( path ) ; }else{ return StringCopy_2( path ) ; } }
/* * 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 * zuluCryptGetFileNameFromFileDescriptor( int fd ) { char * e ; char * c ; string_t xt = String( "/proc/self/fd/" ) ; e = zuluCryptRealPath( StringAppendInt( xt,fd ) ) ; /* * zuluCryptResolvePath_4() is defined in resolve_paths.c */ c = zuluCryptResolvePath_4( e ) ; StringFree( e ) ; StringDelete( &xt ) ; return c ; }
/* * 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 ) ; }
static int open_plain_as_me_1(const struct_opts * opts,const char * mapping_name,uid_t uid,int op ) { /* * Below is a form of memory management.All strings are collected in a stringlist object to easily delete them * when the function returns.This allows for the function to have multiple exit points without risks of leaking * memory from manually examining each exit point to make sure all strings are deleted or go with multiple goto * code deleting blocks to take into account different exit points. */ stringList_t stl ; string_t * stringArray = StringListArray( &stl,5 ) ; string_t * mapper = &stringArray[ 0 ] ; string_t * passphrase = &stringArray[ 1 ] ; string_t * p = &stringArray[ 2 ] ; string_t * dev_st = &stringArray[ 3 ] ; string_t * dev_1 = &stringArray[ 4 ] ; size_t len = 0 ; const char * source = opts->key_source ; const char * pass = opts->key ; int k = opts->ask_confirmation ; const char * cpass = NULL ; char * d ; const char * device = opts->device ; const char * dev = opts->device ; int j ; int n ; const char * cmapper ; if( StringPrefixEqual( device,"/dev/loop" ) ){ /* * zuluCryptLoopDeviceAddress() is defined in ../lib/create_loop_device.c */ d = zuluCryptLoopDeviceAddress( device ) ; *dev_st = StringInherit( &d ) ; dev = StringContent( *dev_st ) ; *dev_1 = StringCopy( *dev_st ) ; device = StringReplaceString( * dev_1,"\\040"," " ) ; } /* * zuluCryptPartitionIsSystemPartition() is defined in ./partition.c */ if( zuluCryptPartitionIsSystemPartition( device,uid ) ){ if( uid != 0 ){ return zuluExit( stl,8 ) ; } } /* * ZULUCRYPTlongMapperPath and ZULUCRYPTshortMapperPath are in ../constants.h * zuluCryptCreateMapperName() is defined at ../lib/create_mapper_name.c */ *mapper = zuluCryptCreateMapperName( device,mapping_name,uid,ZULUCRYPTshortMapperPath ) ; *p = zuluCryptCreateMapperName( device,mapping_name,uid,ZULUCRYPTlongMapperPath ) ; j = zuluCryptCheckOpenedMapper( StringContent( *p ) ) ; /* * zuluCryptPartitionIsMounted() is defined in ../lib/process_mountinfo.c */ n = zuluCryptPartitionIsMounted( dev ) ; if( j == 1 ){ return zuluExit( stl,13 ) ; } if( n == 1 ){ return zuluExit( stl,14 ) ; } if( k == 0 ){ *passphrase = StringRandomString( 64 ) ; cpass = StringContent( *passphrase ) ; len = StringLength( *passphrase ) ; }else if( source == NULL ){ printf( gettext( "Enter passphrase: " ) ) ; /* * ZULUCRYPT_KEY_MAX_SIZE is set in ../constants.h */ switch( StringSilentlyGetFromTerminal_1( passphrase,ZULUCRYPT_KEY_MAX_SIZE ) ){ case 1 : return zuluExit( stl,16 ) ; case 2 : return zuluExit( stl,17 ) ; } printf( "\n" ) ; cpass = StringContent( *passphrase ) ; len = StringLength( *passphrase ) ; }else{ if( strcmp( source,"-p" ) == 0 ){ *passphrase = String( pass ) ; cpass = StringContent( *passphrase ) ; len = StringLength( *passphrase ) ; }else if( strcmp( source,"-f" ) == 0 ){ /* * zuluCryptGetPassFromFile() is defined at "path_access.c" */ switch( zuluCryptGetPassFromFile( pass,uid,passphrase ) ){ case 1 : return zuluExit( stl,10 ) ; case 2 : return zuluExit( stl,11 ) ; case 4 : return zuluExit( stl,12 ) ; } cpass = StringContent( *passphrase ) ; len = StringLength( *passphrase ) ; } } if( zuluCryptSecurityGainElevatedPrivileges() ){ /* * zuluCryptOpenPlain() is defined in ../lib/open_plain.c */ if( zuluCryptOpenPlain( device,StringContent( *mapper ),"rw",cpass,len ) != 0 ){ zuluCryptSecurityDropElevatedPrivileges() ; return zuluExit( stl,1 ) ; } } zuluCryptSecurityDropElevatedPrivileges() ; /* * Create a mapper path(usually at /dev/mapper) associated with opened plain mapper above. */ cmapper = StringMultiplePrepend( *mapper,"/",crypt_get_dir(),NULL ) ; /* * mapper path is usually a soft link to /dev/dm-X * resolve the mapper path to its respective /dev/dm-X and set permission on it. * * We set permission of /dev/dm-X pointing to the device to "u+rw" because we want notmal user to be able * to write to the device through the mapper. * * Useful when a normal user want to delete content of the device by writing random data to it. */ d = zuluCryptRealPath( cmapper ) ; if( zuluCryptSecurityGainElevatedPrivileges() ){ if( d != NULL ){ _ignore_result( chown( d,uid,0 ) ) ; _ignore_result( chmod( d,S_IRWXU ) ) ; StringFree( d ) ; } zuluCryptSecurityDropElevatedPrivileges() ; }else{ return zuluExit( stl,1 ) ; } if( op == 1 ){ return zuluExit( stl,0 ) ; }else{ StringListClearDelete( &stl ) ; return 0 ; } }