"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-cli/lib/mountinfo.c" (15 Jan 2020, 8837 Bytes) of package /linux/misc/zuluCrypt-5.7.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mountinfo.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.7.0_vs_5.7.1.

    1 /*
    2  *
    3  *  Copyright (c) 2011-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 "share_mount_prefix_path.h"
   23 
   24 typedef struct{
   25     const char * device ;
   26     const char * mountPoint ;
   27     const char * fileSystem ;
   28     const char * mountOptions ;
   29     const char * rootPath ;
   30 }vInfo ;
   31 
   32 static int _valid_entry( const vInfo * e )
   33 {
   34     if( StringsAreEqual( e->device,"/dev/fuse" ) ){
   35         /*
   36          * We dont support these fuse devices.
   37          */
   38         return 0 ;
   39     }
   40 
   41     if( StringAtLeastOnePrefixMatch( e->mountPoint,"/var/run/media/public",
   42                         "/var/run/media/private",NULL ) ){
   43         /*
   44          * some distributions auto generate these extra mount points and we
   45          * ignore them as they confuse us.
   46          */
   47         return 0 ;
   48     }
   49 
   50     if( StringsAreEqual( e->rootPath,"/" ) || StringsAreEqual( e->fileSystem,"btrfs" ) ){
   51         /*
   52          * we only support bind mount on btrfs.
   53          */
   54         return 1 ;
   55     }
   56 
   57     return 0 ;
   58 }
   59 
   60 static void _add_entry( string_t ( *function )( const vInfo * ),stringList_t tmp,
   61             stringList_t * stx,char * const ** entry,size_t * entry_len )
   62 {
   63     string_t st ;
   64 
   65     u_int64_t e ;
   66 
   67     vInfo volumeInfo ;
   68 
   69     StringListStringArray_1( entry,entry_len,tmp ) ;
   70 
   71     volumeInfo.device       = *( *entry + *entry_len - 2 ) ;
   72     volumeInfo.mountPoint   = *( *entry + 4 ) ;
   73     volumeInfo.fileSystem   = *( *entry + *entry_len - 3 ) ;
   74     volumeInfo.mountOptions = *( *entry + 5 ) ;
   75     volumeInfo.rootPath     = *( *entry + 3 ) ;
   76 
   77     if( StringAtLeastOneMatch_1( volumeInfo.fileSystem,"fuse.encfs","fuse.cryfs",
   78                      "fuse.securefs","fuse.gocryptfs",NULL ) ){
   79 
   80         if( StringAtLeastOnePrefixMatch( volumeInfo.device,"encfs@",
   81                          "cryfs@","securefs@","gocryptfs@",NULL ) ){
   82 
   83             volumeInfo.device += StringFirstIndexOfChar_1( volumeInfo.device,'@' ) + 1 ;
   84         }else{
   85             st = StringListStringAt( tmp,*entry_len - 2 ) ;
   86 
   87             StringReset( st ) ;
   88 
   89             e = StringJenkinsOneAtATimeHash( volumeInfo.mountPoint ) ;
   90 
   91             volumeInfo.device = StringAppendInt( st,e ) ;
   92         }
   93     }
   94 
   95     if( _valid_entry( &volumeInfo ) ){
   96 
   97         st = function( &volumeInfo ) ;
   98 
   99         StringListAppendString_1( stx,&st ) ;
  100     }
  101 }
  102 
  103 static stringList_t _volumeList( string_t ( *function )( const vInfo * ) )
  104 {
  105     char * const * entry = NULL ;
  106 
  107     size_t entry_len = 0 ;
  108 
  109     stringList_t tmp ;
  110     stringList_t stx = StringListVoid ;
  111     stringList_t stl ;
  112 
  113     StringListIterator it  ;
  114     StringListIterator end ;
  115 
  116     string_t st = StringGetFromVirtualFile( "/proc/self/mountinfo" ) ;
  117 
  118     stl = StringListStringSplit( st,'\n' ) ;
  119 
  120     StringDelete( &st ) ;
  121 
  122     StringListGetIterators( stl,&it,&end ) ;
  123 
  124     while( it != end ){
  125 
  126         tmp = StringListStringSplit( *it,' ' ) ;
  127 
  128         it++ ;
  129 
  130         _add_entry( function,tmp,&stx,&entry,&entry_len ) ;
  131 
  132         StringListDelete( &tmp ) ;
  133     }
  134 
  135     StringFree( entry ) ;
  136 
  137     StringListDelete( &stl ) ;
  138 
  139     return stx ;
  140 }
  141 
  142 static string_t _mount_properties( string_t ( *function )( const char * ),const vInfo * e )
  143 {
  144     string_t st = function( e->device ) ;
  145 
  146     StringMultipleAppend( st," ",e->mountPoint," ",e->fileSystem," ",e->mountOptions,NULL ) ;
  147 
  148     return st ;
  149 }
  150 
  151 static string_t _resolve_path_1( const vInfo * e )
  152 {
  153     /*
  154      * zuluCryptResolvePath_1() is defined in resolve_paths.c
  155      */
  156     return _mount_properties( zuluCryptResolvePath_1,e ) ;
  157 }
  158 
  159 stringList_t zuluCryptGetMoutedList( void )
  160 {
  161     return _volumeList( _resolve_path_1 ) ;
  162 }
  163 
  164 static string_t _resolve_path_2( const vInfo * e )
  165 {
  166     /*
  167      * zuluCryptResolvePath_2() is defined in resolve_paths.c
  168      */
  169     return _mount_properties( zuluCryptResolvePath_2,e ) ;
  170 }
  171 
  172 stringList_t zuluCryptGetMoutedList_1( void )
  173 {
  174     return _volumeList( _resolve_path_2 ) ;
  175 }
  176 
  177 static string_t _get_mounted_device_list( const vInfo * e )
  178 {
  179     return zuluCryptResolvePath_1( e->device ) ;
  180 }
  181 
  182 stringList_t zuluCryptGetAListOfMountedVolumes( void )
  183 {
  184     return _volumeList( _get_mounted_device_list ) ;
  185 }
  186 
  187 static int _mounted( ssize_t( *function )( stringList_t,const char * ),string_t st )
  188 {
  189     stringList_t stl = zuluCryptGetMoutedList() ;
  190 
  191     ssize_t r = function( stl,StringContent( st ) ) ;
  192 
  193     StringListDelete( &stl ) ;
  194 
  195     StringDelete( &st ) ;
  196 
  197     return r != -1 ;
  198 }
  199 
  200 int zuluCryptMountPointIsActive( const char * m_point )
  201 {
  202     return _mounted( StringListHasSequence,String_1( " ",m_point," ",NULL ) ) ;
  203 }
  204 
  205 int zuluCryptPartitionIsMounted( const char * path )
  206 {
  207     return _mounted( StringListHasStartSequence,String_1( path," ",NULL ) ) ;
  208 }
  209 
  210 stringList_t zuluCryptOpenedVolumesList( uid_t uid )
  211 {
  212     const char * e ;
  213     const char * c ;
  214     const char * d ;
  215     const char * t ;
  216 
  217     char * f ;
  218     char * g ;
  219 
  220     StringListIterator it  ;
  221     StringListIterator end ;
  222 
  223     ssize_t k ;
  224 
  225     string_t q ;
  226     string_t z ;
  227 
  228     string_t j ;
  229 
  230     stringList_t stx ;
  231     stringList_t list = StringListVoid ;
  232     stringList_t stl = zuluCryptGetMoutedList() ;
  233 
  234     if( uid ){;}
  235 
  236     /*
  237      * zuluCryptMapperPrefix() is defined in create_mapper_name.c
  238      */
  239     j = String_1( zuluCryptMapperPrefix(),"/zuluCrypt-",NULL ) ;
  240     /*
  241      * t will probably contain "/dev/mapper/zuluCrypt-"
  242      */
  243     t = StringContent( j ) ;
  244 
  245     StringListGetIterators( stl,&it,&end ) ;
  246 
  247     while( it != end ){
  248 
  249         c = StringContent( *it ) ;
  250 
  251         it++ ;
  252 
  253         if( StringPrefixNotEqual( c,t ) && !zuluCryptBitLockerVolume_1( c ) ){
  254 
  255             /*
  256              * we only care about zuluCrypt volumes and these volumes that we care about starts with
  257              * "/dev/mapper/zuluCrypt-"
  258              *
  259              * We also care about dislocker volumes so we let them through. These volumes ends with
  260              * "dislocker-file"
  261              */
  262 
  263             continue ;
  264         }
  265 
  266         if( StringHasComponent( c,SHARE_MOUNT_PREFIX "/" ) ){
  267 
  268             /*
  269              * dont show mirror images due to bind mounts
  270              */
  271 
  272             continue ;
  273         }
  274 
  275         stx = StringListSplit( c,' ' ) ;
  276 
  277         e = StringListContentAtFirstPlace( stx ) ;
  278 
  279         k = StringHasComponent_1( e,"-UUID-" ) ;
  280 
  281         if( k != -1 ) {
  282 
  283             q = StringListStringAtFirstPlace( stx ) ;
  284             /*
  285              * zuluCryptDecodeMountEntry() is defined in mount_volume.c
  286              */
  287             d = zuluCryptDecodeMountEntry( StringListStringAtSecondPlace( stx ) ) ;
  288 
  289             /*
  290              * zuluCryptGetVolumeTypeFromMapperPath() is defined in status.c
  291              */
  292             f = zuluCryptGetVolumeTypeFromMapperPath( StringContent( q ) ) ;
  293 
  294             e = StringSubChar( q,StringLastIndexOfChar( q,'-' ),'\0' ) + k + 6 ;
  295 
  296             z = String_1( "UUID=\"",e,"\"\t",d,"\t",f,NULL ) ;
  297 
  298             StringListAppendString_1( &list,&z ) ;
  299 
  300             StringFree( f ) ;
  301 
  302         }else if( zuluCryptIsDislockerMapperPath( e ) ){
  303 
  304             q = zuluCryptBitLockerResolveMapperPath( e,uid ) ;
  305 
  306             d = zuluCryptDecodeMountEntry( StringListStringAtSecondPlace( stx ) ) ;
  307 
  308             StringMultipleAppend( q,"\t",d,"\t",zuluCryptBitLockerType(),NULL ) ;
  309 
  310             StringListAppendString_1( &list,&q ) ;
  311         }else{
  312             /*
  313              * zuluCryptVolumeDeviceName() is defined in status.c
  314              */
  315             g = zuluCryptVolumeDeviceName( e ) ;
  316 
  317             if( g != NULL ){
  318 
  319                 d = zuluCryptDecodeMountEntry( StringListStringAtSecondPlace( stx ) ) ;
  320 
  321                 /*
  322                  * zuluCryptGetVolumeTypeFromMapperPath() is defined in status.c
  323                  */
  324 
  325                 f = zuluCryptGetVolumeTypeFromMapperPath( StringListContentAtFirstPlace( stx ) ) ;
  326 
  327                 z = String_1( g,"\t",d,"\t",f,NULL ) ;
  328 
  329                 StringListAppendString_1( &list,&z ) ;
  330 
  331                 StringFree( f ) ;
  332 
  333                 StringFree( g ) ;
  334             }
  335         }
  336 
  337         StringListDelete( &stx ) ;
  338     }
  339 
  340     StringListDelete( &stl ) ;
  341 
  342     StringDelete( &j ) ;
  343 
  344     return list ;
  345 }
  346 
  347 string_t zuluCryptGetMountEntry( const char * path )
  348 {
  349     stringList_t stl = zuluCryptGetMoutedList() ;
  350 
  351     string_t st = zuluCryptGetMountEntry_1( stl,path ) ;
  352 
  353     StringListDelete( &stl ) ;
  354 
  355     return st ;
  356 }
  357 
  358 string_t zuluCryptGetMountEntry_1( stringList_t stl,const char * path )
  359 {
  360     string_t st ;
  361     string_t xt ;
  362 
  363     if( stl == StringListVoid ){
  364 
  365         return StringVoid ;
  366     }else{
  367         /*
  368          * zuluCryptResolvePath_1() is defined in resolve_paths.c
  369          */
  370         st = zuluCryptResolvePath_1( path ) ;
  371 
  372         xt = StringListHasStartSequence_1( stl,StringAppend( st," " ) ) ;
  373 
  374         StringDelete( &st ) ;
  375 
  376         return StringCopy( xt ) ;
  377     }
  378 }
  379 
  380 char * zuluCryptGetMountPointFromPath( const char * path )
  381 {
  382     string_t st = zuluCryptGetMountEntry( path ) ;
  383     stringList_t stl ;
  384 
  385     if( st == StringVoid ){
  386 
  387         return NULL ;
  388     }else{
  389         stl = StringListStringSplit( st,' ' ) ;
  390 
  391         StringDelete( &st ) ;
  392 
  393         if( stl == StringListVoid ){
  394 
  395             return NULL ;
  396         }else{
  397             st = StringListCopyStringAtSecondPlace( stl ) ;
  398 
  399             StringListDelete( &stl ) ;
  400 
  401             zuluCryptDecodeMountEntry( st ) ;
  402 
  403             return StringDeleteHandle( &st ) ;
  404         }
  405     }
  406 }