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)  

create_mount_point.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2013-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 <sys/stat.h>
23 #include <unistd.h>
24 #include "mount_prefix_path.h"
25 #include "reuse_mount_point.h"
26 #include <grp.h>
27 #include <pwd.h>
28 
29 static void _chown( const char * x,uid_t y,gid_t z )
30 {
31  struct passwd * usr ;
32 
33  if( z == ( gid_t ) -1 ){
34 
35  usr = getpwuid( y ) ;
36 
37  if( usr != NULL ){
38 
39  z = usr->pw_gid ;
40  }
41  }
42 
43  if( chown( x,y,z ) ){;}
44 }
45 static void _chmod( const char * x,mode_t y )
46 {
47  if( chmod( x,y ) ){;}
48 }
49 static void _mkdir( const char * x,mode_t y )
50 {
51  if( mkdir( x,y ) ){;}
52 }
53 static void _stat( const char * x,struct stat * y )
54 {
55  if( stat( x,y ) ){;}
56 }
57 
59 {
60  return REUSE_MOUNT_POINT ;
61 }
62 
63 static string_t _create_path_0( const char * m_point,uid_t uid,string_t path )
64 {
65  struct stat st ;
66 
68 
69  if( stat( m_point,&st ) == 0 ){
70 
71  if( S_ISDIR( st.st_mode ) ){
72 
73  /*
74  * zuluCryptMountPointIsActive() is defined in ../lib/mountinfo.c
75  */
76  if( zuluCryptMountPointIsActive( m_point ) ){
77 
78  StringDelete( &path ) ;
79  }else{
80  _chown( m_point,uid,( gid_t )-1 ) ;
81  }
82  }else{
83  StringDelete( &path ) ;
84  }
85  }else{
86  if( mkdir( m_point,S_IRWXU ) == 0 ){
87 
88  _chown( m_point,uid,( gid_t )-1 ) ;
89  }else{
90  StringDelete( &path ) ;
91  }
92  }
93  }else{
94  if( mkdir( m_point,S_IRWXU ) == 0 ){
95 
96  _chown( m_point,uid,( gid_t )-1 ) ;
97  }else{
98  StringDelete( &path ) ;
99  }
100  }
101 
102  return path ;
103 }
104 
105 static string_t _create_path( uid_t uid,string_t path,int need_privileges )
106 {
107  string_t st = StringVoid ;
108 
109  const char * m_point = StringContent( path ) ;
110 
111  if( m_point != NULL ){
112 
113  if( need_privileges ){
114 
116 
117  st = _create_path_0( m_point,uid,path ) ;
118 
120  }else{
121  st = _create_path_0( m_point,uid,path ) ;
122  }
123  }
124 
125  return st ;
126 }
127 
128 static string_t _create_mount_point_1( const char * device,uid_t uid,string_t path,int need_privileges )
129 {
130  string_t st ;
131  char * loop_path = NULL ;
132 
133  if( StringPrefixEqual( device,"/dev/loop" ) ){
134  /*
135  * zuluCryptLoopDeviceAddress_1() is defined in ../lib/create_loop_device.c
136  */
137  device = loop_path = zuluCryptLoopDeviceAddress_1( device ) ;
138  }
139 
140  StringMultipleAppend( path,device + StringLastIndexOfChar_1( device,'/' ) + 1,NULL ) ;
141 
142  st = _create_path( uid,path,need_privileges ) ;
143 
144  StringFree( loop_path ) ;
145 
146  return st ;
147 }
148 
149 static string_t _create_home_default_mount_point( const char * device,uid_t uid,string_t path )
150 {
151  return _create_mount_point_1( device,uid,path,0 ) ;
152 }
153 
154 static string_t _create_default_mount_point( const char * device,uid_t uid,string_t path )
155 {
156  return _create_mount_point_1( device,uid,path,1 ) ;
157 }
158 
159 static string_t _create_mount_point_0( const char * label,uid_t uid,string_t path,int need_privileges )
160 {
161  const char * q = strrchr( label,'/' ) ;
162  const char * e ;
163 
164  if( q == NULL ){
165 
166  StringAppend( path,label ) ;
167  }else{
168  if( *( q + 1 ) == '\0' ){
169  /*
170  * -m option was given with a path that ends with "/",backtrack until you find the second "/"
171  * from the right and use it as the last "/".
172  */
173  e = q - 1 ;
174 
175  if( e < label ){
176  /*
177  * -m option was given with a single "/".
178  */
179  StringDelete( &path ) ;
180  return StringVoid ;
181  }
182  while( 1 ){
183 
184  if( e == label ){
185 
186  StringAppend( path,e + 1 ) ;
187  StringRemoveRight( path,1 ) ;
188 
189  break ;
190 
191  }else if( *e == '/' ){
192 
193  StringAppend( path,e + 1 ) ;
194  StringRemoveRight( path,1 ) ;
195 
196  break ;
197  }else{
198  e-- ;
199  }
200  }
201  }else{
202  StringAppend( path,q + 1 ) ;
203  }
204  }
205 
206  return _create_path( uid,path,need_privileges ) ;
207 }
208 
209 static string_t _create_home_custom_mount_point( const char * label,uid_t uid,string_t path )
210 {
211  return _create_mount_point_0( label,uid,path,0 ) ;
212 }
213 
214 static string_t _create_custom_mount_point( const char * label,uid_t uid,string_t path )
215 {
216  return _create_mount_point_0( label,uid,path,1 ) ;
217 }
218 
219 static string_t create_home_mount_point( const char * device,const char * label,uid_t uid )
220 {
221  /*
222  * zuluCryptGetUserHomePath() is defined in ../lib/user_home_path.c
223  */
224  string_t path = zuluCryptGetUserHomePath( uid ) ;
225 
226  if( label == NULL ){
227 
228  return _create_home_default_mount_point( device,uid,path ) ;
229  }else{
230  return _create_home_custom_mount_point( label,uid,path ) ;
231  }
232 }
233 
234 static int mount_point_prefix_match_0( const char * m_path,uid_t uid,string_t * m_point,int home_prefix )
235 {
236  int st ;
237  /*
238  * zuluCryptGetUserName() is defined in ../lib/user_home_path.c
239  */
240  string_t uname ;
241  /*
242  * below constant are set in ../constants.h
243  */
244  const char * str ;
245 
246  if( home_prefix ){
247 
248  uname = zuluCryptGetUserHomePath( uid ) ;
249  str = StringContent( uname ) ;
250  }else{
251  uname = zuluCryptGetUserName( uid ) ;
252  StringPrepend( uname,"/run/media/private/" ) ;
253  str = StringAppendChar( uname,'/' ) ;
254  }
255 
256  st = StringPrefixEqual( m_path,str ) ;
257 
258  if( m_point ){
259 
260  *m_point = uname ;
261  }else{
262  StringDelete( &uname ) ;
263  }
264 
265  return st ;
266 }
267 
268 static int home_mount_point_prefix_match( const char * m_path,uid_t uid,string_t * m_point )
269 {
270  return mount_point_prefix_match_0( m_path,uid,m_point,1 ) ;
271 }
272 
273 static int mount_point_prefix_match( const char * m_path,uid_t uid,string_t * m_point )
274 {
275  return mount_point_prefix_match_0( m_path,uid,m_point,0 ) ;
276 }
277 
278 static void _zuluCryptCreateMountPath( const char * path )
279 {
280  struct stat st ;
281 
282  _mkdir( path,S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH ) ;
283 
284  _stat( path,&st ) ;
285 
286  _chown( path,0,0 ) ;
287 
288  _chmod( path,st.st_mode | S_IXOTH | S_IROTH ) ;
289 }
290 
291 void zuluCryptCreateMountPath( const char * path )
292 {
293  string_t p = String( path ) ;
294 
295  StringIterator it ;
296  StringIterator end ;
297 
298  char * e ;
299  char * f ;
300 
301  StringGetIterators( p,&it,&end ) ;
302 
303  if( end ){;}
304 
305  f = it + 1 ;
306 
307  while( 1 ){
308 
309  e = strchr( f,'/' ) ;
310 
311  if( e == NULL ){
312 
314  break ;
315  }else{
316  *e = '\0' ;
317 
319 
320  *e = '/' ;
321 
322  f = e + 1 ;
323  }
324  }
325 
326  StringDelete( &p ) ;
327 }
328 
329 static string_t create_mount_point( const char * device,const char * label,uid_t uid )
330 {
331  string_t path = zuluCryptGetUserName( uid ) ;
332 
333  const char * e = StringPrepend( path,"/run/media/private/" ) ;
334 
336 
338 
339  _chmod( e,S_IREAD | S_IXUSR ) ;
340  _chown( e,uid,( gid_t )-1 ) ;
341 
343 
344  StringAppendChar( path,'/' ) ;
345 
346  if( label == NULL ){
347 
348  return _create_default_mount_point( device,uid,path ) ;
349  }else{
350  return _create_custom_mount_point( label,uid,path ) ;
351  }
352 }
353 
354 static int home_mount_prefix( void )
355 {
356  return USE_HOME_PATH_AS_MOUNT_PREFIX ;
357 }
358 
360 {
361  string_t st = zuluCryptGetUserName( uid ) ;
362 
363  const char * e = StringPrepend( st,"/run/media/private/" ) ;
364 
366 
367  StringDelete( &st ) ;
368 }
369 
370 string_t zuluCryptCreateMountPoint( const char * device,const char * label,const char * m_opts,uid_t uid )
371 {
372  if( home_mount_prefix() ){
373 
374  return create_home_mount_point( device,label,uid ) ;
375  }else{
376  if( StringHasComponent( m_opts,"mount-prefix=home" ) ){
377 
378  if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
379 
380  return create_home_mount_point( device,label,uid ) ;
381  }else{
382  return StringVoid ;
383  }
384  }else{
385  return create_mount_point( device,label,uid ) ;
386  }
387  }
388 }
389 
390 int zuluCryptMountPointPrefixMatch( const char * m_path,uid_t uid,string_t * m_point )
391 {
392  if( home_mount_prefix() ){
393 
394  return home_mount_point_prefix_match( m_path,uid,m_point ) ;
395  }else{
396  if( mount_point_prefix_match( m_path,uid,m_point ) ){
397 
398  return 1 ;
399  }else{
400  return home_mount_point_prefix_match( m_path,uid,m_point ) ;
401  }
402  }
403 }
_stat
static void _stat(const char *x, struct stat *y)
Definition: create_mount_point.c:53
zuluCryptMountPointPrefixMatch
int zuluCryptMountPointPrefixMatch(const char *m_path, uid_t uid, string_t *m_point)
Definition: create_mount_point.c:390
create_home_mount_point
static string_t create_home_mount_point(const char *device, const char *label, uid_t uid)
Definition: create_mount_point.c:219
zuluCryptLoopDeviceAddress_1
char * zuluCryptLoopDeviceAddress_1(const char *device)
Definition: create_loop_device.c:140
zuluCryptCreateMountPath
void zuluCryptCreateMountPath(const char *path)
Definition: create_mount_point.c:291
StringPrepend
const char * StringPrepend(string_t st, const char *s)
Definition: String.c:1015
zuluCryptUserIsAMemberOfAGroup
int zuluCryptUserIsAMemberOfAGroup(uid_t uid, const char *groupname)
Definition: mount_fs_options.c:172
zuluCryptGetUserName
string_t zuluCryptGetUserName(uid_t uid)
Definition: user_home_path.c:28
StringMultipleAppend
const char * StringMultipleAppend(string_t st,...)
Definition: String.c:1123
_mkdir
static void _mkdir(const char *x, mode_t y)
Definition: create_mount_point.c:49
_create_home_custom_mount_point
static string_t _create_home_custom_mount_point(const char *label, uid_t uid, string_t path)
Definition: create_mount_point.c:209
_chmod
static void _chmod(const char *x, mode_t y)
Definition: create_mount_point.c:45
_create_home_default_mount_point
static string_t _create_home_default_mount_point(const char *device, uid_t uid, string_t path)
Definition: create_mount_point.c:149
zuluCryptGetUserHomePath
string_t zuluCryptGetUserHomePath(uid_t)
Definition: user_home_path.c:23
_create_mount_point_0
static string_t _create_mount_point_0(const char *label, uid_t uid, string_t path, int need_privileges)
Definition: create_mount_point.c:159
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
StringType
Definition: String.c:49
zuluCryptSecurityDropElevatedPrivileges
int zuluCryptSecurityDropElevatedPrivileges(void)
Definition: security.c:109
mount_point_prefix_match_0
static int mount_point_prefix_match_0(const char *m_path, uid_t uid, string_t *m_point, int home_prefix)
Definition: create_mount_point.c:234
home_mount_prefix
static int home_mount_prefix(void)
Definition: create_mount_point.c:354
StringLastIndexOfChar_1
static __inline__ ssize_t StringLastIndexOfChar_1(const char *str, char s)
Definition: String.h:982
String
string_t String(const char *cstring)
Definition: String.c:318
zuluCryptMountPointIsActive
int zuluCryptMountPointIsActive(const char *)
Definition: mountinfo.c:200
zuluCryptSecurityGainElevatedPrivileges
int zuluCryptSecurityGainElevatedPrivileges(void)
Definition: security.c:64
StringAppendChar
const char * StringAppendChar(string_t st, char c)
Definition: String.c:1205
home_mount_point_prefix_match
static int home_mount_point_prefix_match(const char *m_path, uid_t uid, string_t *m_point)
Definition: create_mount_point.c:268
create_mount_point
static string_t create_mount_point(const char *device, const char *label, uid_t uid)
Definition: create_mount_point.c:329
_create_mount_point_1
static string_t _create_mount_point_1(const char *device, uid_t uid, string_t path, int need_privileges)
Definition: create_mount_point.c:128
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
_create_custom_mount_point
static string_t _create_custom_mount_point(const char *label, uid_t uid, string_t path)
Definition: create_mount_point.c:214
e
static QString e
Definition: about.cpp:31
_zuluCryptCreateMountPath
static void _zuluCryptCreateMountPath(const char *path)
Definition: create_mount_point.c:278
StringIterator
char * StringIterator
Definition: String.h:49
zuluCryptReuseMountPoint
int zuluCryptReuseMountPoint(void)
Definition: create_mount_point.c:58
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
_chown
static void _chown(const char *x, uid_t y, gid_t z)
Definition: create_mount_point.c:29
zuluCryptCreateMountPoint
string_t zuluCryptCreateMountPoint(const char *device, const char *label, const char *m_opts, uid_t uid)
Definition: create_mount_point.c:370
StringGetIterators
void StringGetIterators(string_t st, StringIterator *begin, StringIterator *end)
Definition: String.c:133
StringRemoveRight
const char * StringRemoveRight(string_t st, size_t x)
Definition: String.c:634
_create_path_0
static string_t _create_path_0(const char *m_point, uid_t uid, string_t path)
Definition: create_mount_point.c:63
StringAppend
const char * StringAppend(string_t st, const char *s)
Definition: String.c:1052
includes.h
_create_default_mount_point
static string_t _create_default_mount_point(const char *device, uid_t uid, string_t path)
Definition: create_mount_point.c:154
mount_point_prefix_match
static int mount_point_prefix_match(const char *m_path, uid_t uid, string_t *m_point)
Definition: create_mount_point.c:273
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
zuluCryptCreateMountPointPrefix
void zuluCryptCreateMountPointPrefix(uid_t uid)
Definition: create_mount_point.c:359
_create_path
static string_t _create_path(uid_t uid, string_t path, int need_privileges)
Definition: create_mount_point.c:105
StringVoid
#define StringVoid
Definition: String.h:47