"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-cli/bin/clear_dead_mappers.c" (15 Jan 2020, 5974 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 "clear_dead_mappers.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) 2012-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 #include "../lib/includes.h"
   22 #include "mount_prefix_path.h"
   23 #include <sys/types.h>
   24 #include <string.h>
   25 #include <dirent.h>
   26 #include <stdlib.h>
   27 #include <stdio.h>
   28 #include <unistd.h>
   29 #include <libintl.h>
   30 #include <libcryptsetup.h>
   31 
   32 static void _remove_mapper( const char * path,stringList_t stl,uid_t uid )
   33 {
   34     char * m_point = NULL ;
   35     /*
   36      * zuluCryptBindUnmountVolume() is defined in ./bind.c
   37      */
   38     int r = zuluCryptBindUnmountVolume( stl,path,uid ) ;
   39 
   40     if( r == 3 || r == 4 ){
   41         /*
   42          * shared mount is busy or belong to another user
   43          */
   44         return ;
   45     }
   46 
   47     /*
   48      * zuluCryptCloseVolume() is defined in ../lib/close_volume.c
   49      */
   50     r = zuluCryptCloseVolume( path,&m_point ) ;
   51 
   52     if( r == 0 && m_point != NULL ){
   53 
   54         remove( m_point ) ;
   55         StringFree( m_point ) ;
   56     }
   57 }
   58 
   59 static void _zuluCryptDeleteDeadMountPoints( stringList_t stl,const char * m )
   60 {
   61     struct dirent * entry ;
   62     const char * e ;
   63     string_t st ;
   64     string_t xt ;
   65 
   66     const char * bl = zuluCryptBitLockerFolderPrefix() ;
   67 
   68     DIR * dir = opendir( m ) ;
   69 
   70     if( dir == NULL ){
   71 
   72         return ;
   73     }
   74 
   75     while( ( entry = readdir( dir ) ) != NULL ){
   76 
   77         e = entry->d_name ;
   78 
   79         if( !StringAtLeastOneMatch_1( e,".","..",bl,NULL ) ){
   80 
   81             st = String( e ) ;
   82 
   83             zuluCryptEncodeMountEntry( st ) ;
   84 
   85             StringMultiplePrepend( st,"/",m,NULL ) ;
   86 
   87             e = StringAppend( st," " ) ;
   88 
   89             if( StringListHasSequence( stl,e ) < 0 ){
   90 
   91                 xt = String_1( m,"/",entry->d_name,NULL ) ;
   92 
   93                 if( rmdir( StringContent( xt ) ) != 0 ){
   94 
   95                     /*
   96                      * Failed to delete an unmounted folder for some reason
   97                      */
   98                 }
   99 
  100                 StringDelete( &xt ) ;
  101             }
  102 
  103             StringDelete( &st ) ;
  104         }
  105     }
  106 }
  107 
  108 static void _unmount_dead_mount_points( uid_t uid )
  109 {
  110     if( uid ){}
  111 #if 0
  112     stringList_t stl = zuluCryptPartitions( ZULUCRYPTallPartitions,uid ) ;
  113 
  114     stringList_t stx = zuluCryptGetAListOfMountedVolumes() ;
  115 
  116     StringListIterator it ;
  117     StringListIterator end ;
  118 
  119     string_t st ;
  120 
  121     char * mout_point = NULL ;
  122 
  123     char * m_point = NULL ;
  124 
  125     const char * device ;
  126 
  127     StringListGetIterators( stl,&it,&end ) ;
  128 
  129     while( it != end ){
  130 
  131         st = *it ;
  132 
  133         it++ ;
  134 
  135         StringListRemoveIfPresent_1( stx,st ) ;
  136     }
  137 
  138     StringListGetIterators( stx,&it,&end ) ;
  139 
  140     while( it != end ){
  141 
  142         st = *it ;
  143 
  144         it++ ;
  145 
  146         if( StringStartsWith( st,"/dev/" ) ){
  147 
  148             device = StringContent( st ) ;
  149 
  150             mout_point = zuluCryptGetMountPointFromPath( device ) ;
  151 
  152             printf( "unmounting : %s\n",device ) ;
  153 
  154             if( zuluCryptUnmountVolume( device,&m_point ) == 0 ){
  155 
  156                 if( m_point ){
  157 
  158                     rmdir( m_point ) ;
  159                     printf( "unmounted : %s:%s\n",device,m_point ) ;
  160                 }
  161             }else{
  162                 perror( "failed" ) ;
  163             }
  164         }
  165     }
  166 
  167     StringFree( mout_point ) ;
  168     StringFree( m_point ) ;
  169 #endif
  170 }
  171 
  172 void zuluCryptDeleteDeadMountPoints( uid_t uid,stringList_t stl )
  173 {
  174     string_t st = zuluCryptGetUserName( uid ) ;
  175 
  176     _zuluCryptDeleteDeadMountPoints( stl,StringPrepend( st,"/run/media/private/" ) ) ;
  177     _zuluCryptDeleteDeadMountPoints( stl,"/run/media/public" ) ;
  178 
  179     _unmount_dead_mount_points( uid ) ;
  180 
  181     StringDelete( &st ) ;
  182 }
  183 
  184 void zuluCryptClearDeadMappers( uid_t uid,int s )
  185 {
  186     struct crypt_device * cd ;
  187     const char * dir_path = crypt_get_dir() ;
  188     DIR * dir = opendir( dir_path ) ;
  189     struct dirent * entry ;
  190     const char * m ;
  191     const char * e ;
  192 
  193     char * r ;
  194 
  195     size_t len ;
  196     size_t len1 ;
  197 
  198     stringList_t stl ;
  199     string_t p ;
  200     string_t z ;
  201 
  202     if( dir == NULL ){
  203 
  204         return ;
  205     }
  206 
  207     /*
  208      * zuluCryptGetMoutedList_1()  is defined in ../lib/process_mountinfo.c
  209      */
  210     stl = zuluCryptGetMoutedList_1() ;
  211     z = String_1( dir_path,"/",NULL ) ;
  212 
  213     len1 = StringLength( z ) ;
  214 
  215     p = String( "zuluCrypt-" ) ;
  216     m = StringAppendInt( p,uid ) ;
  217     len = StringLength( p ) ;
  218 
  219     /*
  220      * zuluCryptSecurityGainElevatedPrivileges() is defined in security.c
  221      */
  222     zuluCryptSecurityGainElevatedPrivileges() ;
  223 
  224     while( ( entry = readdir( dir ) ) != NULL ){
  225 
  226         if( StringPrefixMatch( entry->d_name,m,len ) ){
  227 
  228             e = StringAppendAt( z,len1,entry->d_name ) ;
  229 
  230             /*
  231              * zuluCryptTrueCryptOrVeraCryptVolume() is defined in ../lib/status.c
  232              */
  233             if( zuluCryptVolumeManagedByTcplay( e ) ){
  234 
  235                 /*
  236                  * zuluCryptVolumeDeviceName() is defined in ../lib/status.c
  237                  */
  238                 r = zuluCryptVolumeDeviceName( e ) ;
  239 
  240                 if( *( r + 0 ) != '/' ){
  241 
  242                     /*
  243                      * tcplay seems to report device name as something like "8:33"
  244                      * when a mapper exists but its underlying device is gone and we exploit
  245                      * this behavior by checking if path starts with "/" and we assume the
  246                      * device is gone if it isnt.
  247                      */
  248                     _remove_mapper( e,stl,uid ) ;
  249                 }
  250 
  251                 StringFree( r ) ;
  252             }else{
  253                 if( crypt_init_by_name( &cd,e ) == 0 ){
  254 
  255                     if( crypt_get_device_name( cd ) == NULL ){
  256 
  257                         /*
  258                          * we will get here if none LUKS mapper is active but the underlying device is gone
  259                          */
  260 
  261                         _remove_mapper( e,stl,uid ) ;
  262                     }
  263 
  264                     crypt_free( cd ) ;
  265                 }else{
  266                     /*
  267                     * we will get here if the LUKS mapper is active but the underlying device is gone
  268                     */
  269                     _remove_mapper( e,stl,uid ) ;
  270                 }
  271             }
  272         }
  273     }
  274 
  275     if( s ){
  276 
  277         zuluCryptDeleteDeadMountPoints( uid,stl ) ;
  278     }
  279 
  280     /*
  281      * zuluCryptSecurityDropElevatedPrivileges() is defined in security.c
  282      */
  283     zuluCryptSecurityDropElevatedPrivileges() ;
  284 
  285     StringListDelete( &stl ) ;
  286     StringMultipleDelete( &p,&z,NULL ) ;
  287     closedir( dir ) ;
  288 }