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)  

mount.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 #include "includes.h"
20 #include "../zuluCrypt-cli/bin/includes.h"
21 #include <libintl.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 
25 static int _zuluMountPartitionAccess( const char * device,const char * m_opts,uid_t uid )
26 {
27  /*
28  * this function is defined in ../zuluCrypt-cli/lib/mount_volume.c
29  */
30  /*
31  * MOUNTOPTIONS constant is defined in ../zuluCrypt-cli/lib/includes.h
32  */
33 
34  int ro ;
35  int nouser ;
36  int defaulT ;
37  int user ;
38  int users ;
39  int system_partition ;
40  int st = 1 ;
41 
42  string_t p ;
43 
44  stringList_t stl ;
45 
47  /*
48  * zuluCryptGetFstabEntryList() is defined in ../zuluCrypt-cli/lib/mount_volume.c
49  */
50  stl = zuluCryptGetFstabEntryList( device,uid ) ;
52 
53  if( stl != StringListVoid ){
54 
55  if( StringListSize( stl ) != 6 ){
56 
57  StringListDelete( &stl ) ;
58  return 3 ;
59  }
60  }
61 
62  p = StringListStringAt( stl,MOUNTOPTIONS ) ;
63 
64  /*
65  * zuluCryptPartitionIsSystemPartition() is defined in ../zuluCrypt-cli/bin/partition.c
66  */
67  system_partition = zuluCryptPartitionIsSystemPartition( device,uid ) ;
68 
69  if( system_partition ){
70 
71  /*
72  * zuluCryptExeOriginalUserIsNotRoot() is defined in ../zuluCrypt/bin/security.c
73  */
75 
76  /*
77  * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
78  */
79  if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
80 
81  system_partition = 0 ;
82  }
83  }else{
84  system_partition = 0 ;
85  }
86  }
87 
88  if( p == StringVoid ){
89  /*
90  * partition does not have an entry in fstab
91  */
92  if( system_partition ){
93  /*
94  * partition is system partition
95  */
96  if( uid == 0 ){
97  /*
98  * cant say no to root
99  */
100  st = 0 ;
101  }else{
102  /*
103  * system partition with no entry in fstab,refuse to mount this one
104  */
105  st = 1 ;
106  }
107  }else{
108  /*
109  * no entry in fstab,not a system partition,mount this one
110  */
111  st = 0 ;
112  }
113  }else{
114  /*
115  * has an entry in fstab
116  */
117  ro = StringContains( p,"ro" ) ;
118  nouser = StringContains( p,"nouser" ) ;
119  defaulT = StringContains( p,"defaults" ) ;
120  users = StringContains( p,"users" );
121  user = StringContains( p,"user" ) ;
122 
123  if( ro && StringHasComponent( m_opts,"rw" ) ){
124  /*
125  * respect the option for the partition to be mounted read only
126  */
127  st = 2 ;
128  }else if( uid == 0 ){
129  /*
130  * user is root,mount it
131  */
132  st = 0 ;
133  }else{
134  if( nouser ){
135  /*
136  * normal user is not allowed to mount it
137  */
138  st = 1 ;
139  }else if( user || users ){
140  /*
141  * the partition has option to allow normal user to mount it,mount it
142  */
143  st = 0 ;
144  }else if( defaulT ){
145  /*
146  * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
147  */
148  if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
149  /*
150  * user is a member is zulumount group,mount it
151  */
152  st = 0 ;
153  }else{
154  st = 1 ;
155  }
156  }else{
157  /*
158  * remaining options go there
159  */
160  /*
161  * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
162  */
163  if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
164  /*
165  * user is a member is zulumount group,mount it
166  */
167  st = 0 ;
168  }else{
169  st = 1 ;
170  }
171  }
172  }
173  }
174 
175  StringListDelete( &stl ) ;
176  return st ;
177 }
178 
180 {
181  const char * device = args->device ;
182  const char * m_point = args->m_point ;
183  const char * m_opts = args->m_opts ;
184  const char * fs_opts = args->fs_opts ;
185  uid_t uid = args->uid ;
186  int share = args->share ;
187 
188  int status ;
189  string_t z = StringVoid ;
190  char * path = NULL ;
191  const char * rm_point ;
192  unsigned long m_flags ;
193  const char * dev = device ;
194  const char * msg = gettext( "\
195 ERROR: Insuffienct privilege to manage a system volume.\nnecessary privileges can be acquired by:\n\
196 1. Adding an entry for the volume in fstab with \"user\" mount option\n\
197 2. Add yourself to \"zulumount\" group" ) ;
198 
199  if( StringPrefixEqual( device,"/dev/loop" ) ){
200  /*
201  * zuluCryptLoopDeviceAddress_1() is defined in ../zuluCrypt-cli/lib/create_loop_devices.c
202  */
203  path = zuluCryptLoopDeviceAddress_1( device ) ;
204 
205  if( path == NULL ){
206 
207  return _zuluExit( 112,z,path,gettext( "ERROR: Could not resolve path to device or device could not be opened in read write mode" ) ) ;
208  }else{
209  dev = path ;
210  }
211  }
212 
213  if( m_opts == NULL ){
214 
215  m_opts = "rw" ;
216  }
217  if( StringHasComponent( m_opts,"rw" ) ){
218  /*
219  * zuluCryptCanOpenPathForWriting() is defined in ../zuluCrypt-cli/bin/path_access.c
220  */
221  status = zuluCryptCanOpenPathForWriting( device,uid ) ;
222  }else{
223  /*
224  * zuluCryptCanOpenPathForReading() is defined in ../zuluCrypt-cli/bin/path_access.c
225  */
226  status = zuluCryptCanOpenPathForReading( device,uid ) ;
227  }
228 
229  if( status != 0 ){
230 
231  return _zuluExit( 112,z,path,gettext( "ERROR: Could not resolve path to device or device could not be opened in read write mode" ) ) ;
232  }
233 
234  /*
235  * zuluCryptMountFlagsAreNotCorrect() is defined in ../zuluCrypt-cli/bin/mount_flags.c
236  */
237  if( zuluCryptMountFlagsAreNotCorrect( m_opts,uid,&m_flags ) ){
238 
239  return _zuluExit( 100,z,path,gettext( "ERROR: Insuffienct privileges to mount the volume with given mount options" ) ) ;
240  }
241  /*
242  * zuluCryptPartitionIsMounted is defined in ../zuluCrypt-cli/lib/process_mountinfo.c
243  */
244  if( zuluCryptPartitionIsMounted( dev ) ){
245 
246  return _zuluExit( 102,z,path,gettext( "ERROR: Device already mounted" ) ) ;
247  }
248 
249  status = _zuluMountPartitionAccess( dev,m_opts,uid ) ;
250 
251  switch( status ){
252 
253  case 0 : break ;
254  case 1 : return _zuluExit( 103,z,path,msg ) ;
255  case 2 : return _zuluExit( 104,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume requires it to be mounted read only" ) ) ;
256  case 3 : return _zuluExit( 113,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume is malformed" ) ) ;
257  default: return _zuluExit( 105,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume does not allow you to mount it" ) ) ;
258  }
259 
260  /*
261  * zuluCryptSecurityCreateMountPoint() is defined in ../zuluCrypt-cli/bin/create_mount_point.c
262  */
263  z = zuluCryptCreateMountPoint( device,m_point,m_opts,uid ) ;
264 
265  if( z == StringVoid ){
266 
267  return _zuluExit( 106,z,path,gettext( "ERROR: Could not create mount point path,path already taken" ) ) ;
268  }
269 
270  rm_point = StringContent( z ) ;
271 
272  if( share ){
273 
274  /*
275  * zuluCryptBindSharedMountPointPathTaken() is defined in ../zuluCrypt-cli/bin/bind.c
276  */
277 
279 
281 
282  rmdir( rm_point ) ;
283 
285 
286  return _zuluExit( 114,z,path,gettext( "ERROR: Shared mount point path aleady taken" ) ) ;
287  }
288  }
289 
291  /*
292  * zuluCryptMountVolume() defined in ../zuluCrypt-cli/lib/mount_volume.c
293  */
294  status = zuluCryptMountVolume( device,rm_point,m_flags,fs_opts,uid ) ;
296 
297  if( status == 0 ){
298 
299  if( share ){
300  /*
301  * user wish to share the mount point publicly, bind the mount point to a publicly accessed path of /run/share
302  */
303  /*
304  * zuluCryptBindMountVolume() is defined in ../zuluCrypt-cli/bin/bind.c
305  */
306  zuluCryptBindMountVolume( device,z,m_flags ) ;
307  }
308 
309  printf( gettext( "SUCCESS: Mount complete successfully\nvolume mounted at: %s\n" ),rm_point ) ;
310 
311  return _zuluExit( 0,z,path,NULL ) ;
312  }else{
314 
315  rmdir( rm_point ) ;
316 
318 
319  switch( status ){
320 
321  case -1: return _zuluExit( 108,z,path,gettext( "ERROR: Failed to mount a filesystem:invalid/unsupported mount option or unsupported file system encountered" ) ) ;
322  case 1 : return _zuluExit( 109,z,path,gettext( "ERROR: Failed to mount ntfs/exfat file system using ntfs-3g,is ntfs-3g/exfat package installed?" ) ) ;
323  case 4 : return _zuluExit( 110,z,path,gettext( "ERROR: Mount failed,no or unrecognized file system" ) ) ;
324  case 12: return _zuluExit( 111,z,path,gettext( "ERROR: Mount failed,could not get a lock on /etc/mtab~" ) ) ;
325  default: return _zuluExit( 115,z,path,gettext( "ERROR: Failed to mount the partition" ) ) ;
326  }
327  }
328 }
StringListVoid
#define StringListVoid
Definition: StringList.h:41
_zuluMountPartitionAccess
static int _zuluMountPartitionAccess(const char *device, const char *m_opts, uid_t uid)
Definition: mount.c:25
ARGS
Definition: includes.h:27
zuluCryptMountFlagsAreNotCorrect
int zuluCryptMountFlagsAreNotCorrect(const char *mode, uid_t uid, unsigned long *flags)
Definition: mount_flags.c:41
StringListSize
size_t StringListSize(stringList_t stl)
Definition: StringList.c:492
zuluCryptCanOpenPathForWriting
int zuluCryptCanOpenPathForWriting(const char *path, uid_t uid)
Definition: path_access.c:103
zuluCryptLoopDeviceAddress_1
char * zuluCryptLoopDeviceAddress_1(const char *device)
Definition: create_loop_device.c:140
zuluCryptBindMountVolume
int zuluCryptBindMountVolume(const char *device, string_t z_path, unsigned long flags)
Definition: bind.c:240
zuluCryptUserIsAMemberOfAGroup
int zuluCryptUserIsAMemberOfAGroup(uid_t uid, const char *groupname)
Definition: mount_fs_options.c:172
StringListDelete
void StringListDelete(stringList_t *stl)
Definition: StringList.c:1184
zuluCryptCanOpenPathForReading
int zuluCryptCanOpenPathForReading(const char *path, uid_t uid)
Definition: path_access.c:98
StringListStringAt
string_t StringListStringAt(stringList_t stl, size_t index)
Definition: StringList.c:1171
StringContains
int StringContains(string_t st, const char *str)
Definition: String.c:420
zuluCryptBindSharedMountPointPathTaken
int zuluCryptBindSharedMountPointPathTaken(string_t path)
Definition: bind.c:223
args
Definition: open_plain.c:25
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
siritask::status
status
Definition: siritask.h:114
MOUNTOPTIONS
#define MOUNTOPTIONS
Definition: includes.h:291
zuluCryptSecurityGainElevatedPrivileges
int zuluCryptSecurityGainElevatedPrivileges(void)
Definition: security.c:64
StringListType
Definition: StringList.c:33
zuluMountMount
int zuluMountMount(ARGS *args)
Definition: mount.c:179
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
zuluCryptGetFstabEntryList
stringList_t zuluCryptGetFstabEntryList(const char *device, uid_t uid)
Definition: parse_fstab.c:154
zuluCryptPartitionIsMounted
int zuluCryptPartitionIsMounted(const char *device)
Definition: mountinfo.c:205
zuluCryptPartitionIsSystemPartition
int zuluCryptPartitionIsSystemPartition(const char *dev, uid_t uid)
Definition: volumes.c:1143
zuluCryptCreateMountPoint
string_t zuluCryptCreateMountPoint(const char *device, const char *label, const char *m_opts, uid_t uid)
Definition: create_mount_point.c:370
includes.h
zuluCryptExeOriginalUserIsNotRoot
int zuluCryptExeOriginalUserIsNotRoot()
Definition: security.c:59
zuluCryptMountVolume
int zuluCryptMountVolume(const char *mapper, const char *m_point, unsigned long fs_opts, const char *m_opts, uid_t id)
Definition: mount_volume.c:433
_zuluExit
static int _zuluExit(int r, char *const *options, stringList_t stl)
Definition: create_tcrypt.c:277
StringPrefixEqual
static __inline__ int StringPrefixEqual(const char *x, const char *y)
Definition: String.h:905
StringVoid
#define StringVoid
Definition: String.h:47