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)  

clear_dead_mappers.c
Go to the documentation of this file.
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 
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
113 
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 
173 {
174  string_t st = zuluCryptGetUserName( uid ) ;
175 
176  _zuluCryptDeleteDeadMountPoints( stl,StringPrepend( st,"/run/media/private/" ) ) ;
177  _zuluCryptDeleteDeadMountPoints( stl,"/run/media/public" ) ;
178 
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  */
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  */
234 
235  /*
236  * zuluCryptVolumeDeviceName() is defined in ../lib/status.c
237  */
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  */
284 
285  StringListDelete( &stl ) ;
286  StringMultipleDelete( &p,&z,NULL ) ;
287  closedir( dir ) ;
288 }
StringAppendInt
const char * StringAppendInt(string_t st, u_int64_t z)
Definition: String.c:1410
zuluCryptDeleteDeadMountPoints
void zuluCryptDeleteDeadMountPoints(uid_t uid, stringList_t stl)
Definition: clear_dead_mappers.c:172
zuluCryptPartitions
stringList_t zuluCryptPartitions(int option, uid_t uid)
Definition: volumes.c:530
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
StringStartsWith
int StringStartsWith(string_t st, const char *s)
Definition: String.c:821
StringListGetIterators
void StringListGetIterators(stringList_t stl, StringListIterator *begin, StringListIterator *end)
Definition: StringList.c:210
zuluCryptGetMountPointFromPath
char * zuluCryptGetMountPointFromPath(const char *path)
Definition: mountinfo.c:380
StringPrepend
const char * StringPrepend(string_t st, const char *s)
Definition: String.c:1015
zuluCryptGetUserName
string_t zuluCryptGetUserName(uid_t uid)
Definition: user_home_path.c:28
StringListDelete
void StringListDelete(stringList_t *stl)
Definition: StringList.c:1184
_zuluCryptDeleteDeadMountPoints
static void _zuluCryptDeleteDeadMountPoints(stringList_t stl, const char *m)
Definition: clear_dead_mappers.c:59
zuluCryptCloseVolume
int zuluCryptCloseVolume(const char *map, char **mount_point)
Definition: close_volume.c:22
zuluCryptVolumeDeviceName
char * zuluCryptVolumeDeviceName(const char *)
Definition: status.c:895
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
StringType
Definition: String.c:49
StringAppendAt
const char * StringAppendAt(string_t st, size_t x, const char *s)
Definition: String.c:960
_remove_mapper
static void _remove_mapper(const char *path, stringList_t stl, uid_t uid)
Definition: clear_dead_mappers.c:32
zuluCryptSecurityDropElevatedPrivileges
int zuluCryptSecurityDropElevatedPrivileges(void)
Definition: security.c:109
StringMultipleDelete
void StringMultipleDelete(string_t *xt,...)
Definition: String.c:195
zuluCryptBitLockerFolderPrefix
const char * zuluCryptBitLockerFolderPrefix(void)
Definition: bitlocker.c:41
String
string_t String(const char *cstring)
Definition: String.c:318
StringPrefixMatch
static __inline__ int StringPrefixMatch(const char *x, const char *y, size_t z)
Definition: String.h:878
String_1
string_t String_1(const char *cstring,...)
Definition: String.c:363
zuluCryptSecurityGainElevatedPrivileges
int zuluCryptSecurityGainElevatedPrivileges(void)
Definition: security.c:64
StringListHasSequence
ssize_t StringListHasSequence(stringList_t stl, const char *str)
Definition: StringList.c:832
zuluCryptEncodeMountEntry
const char * zuluCryptEncodeMountEntry(string_t)
Definition: mount_volume.c:425
StringListType
Definition: StringList.c:33
zuluCryptBindUnmountVolume
int zuluCryptBindUnmountVolume(stringList_t stx, const char *device, uid_t uid)
Definition: bind.c:200
e
static QString e
Definition: about.cpp:31
_unmount_dead_mount_points
static void _unmount_dead_mount_points(uid_t uid)
Definition: clear_dead_mappers.c:108
StringMultiplePrepend
const char * StringMultiplePrepend(string_t st,...)
Definition: String.c:1159
zuluCryptVolumeManagedByTcplay
int zuluCryptVolumeManagedByTcplay(const char *mapper)
Definition: status.c:406
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
ZULUCRYPTallPartitions
#define ZULUCRYPTallPartitions
Definition: constants.h:31
StringListRemoveIfPresent_1
size_t StringListRemoveIfPresent_1(stringList_t stl, string_t st)
Definition: StringList.c:1025
zuluCryptGetAListOfMountedVolumes
stringList_t zuluCryptGetAListOfMountedVolumes(void)
Definition: mountinfo.c:182
zuluCryptGetMoutedList_1
stringList_t zuluCryptGetMoutedList_1(void)
Definition: mountinfo.c:172
StringAppend
const char * StringAppend(string_t st, const char *s)
Definition: String.c:1052
includes.h
zuluCryptClearDeadMappers
void zuluCryptClearDeadMappers(uid_t uid, int s)
Definition: clear_dead_mappers.c:184
zuluCryptUnmountVolume
int zuluCryptUnmountVolume(const char *mapper, char **m_point)
Definition: unmount_volume.c:134
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823