File: mount.c

package info (click to toggle)
zulucrypt 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,908 kB
  • sloc: ansic: 25,142; cpp: 23,281; makefile: 24; xml: 10; sh: 2
file content (328 lines) | stat: -rw-r--r-- 8,990 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 *
 *  Copyright (c) 2012-2015
 *  name : Francis Banyikwa
 *  email: mhogomchungu@gmail.com
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "includes.h"
#include "../zuluCrypt-cli/bin/includes.h"
#include <libintl.h>
#include <stdio.h>
#include <unistd.h>

static int _zuluMountPartitionAccess( const char * device,const char * m_opts,uid_t uid )
{
	/*
	 * this function is defined in ../zuluCrypt-cli/lib/mount_volume.c
	 */
	/*
	 * MOUNTOPTIONS constant is defined in ../zuluCrypt-cli/lib/includes.h
	 */

	int ro      ;
	int nouser  ;
	int defaulT ;
	int user    ;
	int users   ;
	int system_partition ;
	int st = 1  ;

	string_t p ;

	stringList_t stl ;

	zuluCryptSecurityGainElevatedPrivileges() ;
	/*
	 * zuluCryptGetFstabEntryList() is defined in ../zuluCrypt-cli/lib/mount_volume.c
	 */
	stl = zuluCryptGetFstabEntryList( device,uid ) ;
	zuluCryptSecurityDropElevatedPrivileges() ;

	if( stl != StringListVoid ){

		if( StringListSize( stl ) != 6 ){

			StringListDelete( &stl ) ;
			return 3 ;
		}
	}

	p = StringListStringAt( stl,MOUNTOPTIONS ) ;

	/*
	 * zuluCryptPartitionIsSystemPartition() is defined in ../zuluCrypt-cli/bin/partition.c
	 */
	system_partition = zuluCryptPartitionIsSystemPartition( device,uid ) ;

	if( system_partition ){

		/*
		 * zuluCryptExeOriginalUserIsNotRoot() is defined in ../zuluCrypt/bin/security.c
		 */
		if( zuluCryptExeOriginalUserIsNotRoot() ){

			/*
			 * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
			 */
			if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){

				system_partition = 0 ;
			}
		}else{
			system_partition = 0 ;
		}
	}

	if( p == StringVoid ){
		/*
		 * partition does not have an entry in fstab
		 */
		if( system_partition ){
			/*
			 * partition is system partition
			 */
			if( uid == 0 ){
				/*
				 * cant say no to root
				 */
				st = 0 ;
			}else{
				/*
				* system partition with no entry in fstab,refuse to mount this one
				*/
				st = 1 ;
			}
		}else{
			/*
			 * no entry in fstab,not a system partition,mount this one
			 */
			st = 0 ;
		}
	}else{
		/*
		 * has an entry in fstab
		 */
		ro      = StringContains( p,"ro" ) ;
		nouser  = StringContains( p,"nouser" ) ;
		defaulT = StringContains( p,"defaults" ) ;
		users   = StringContains( p,"users" );
		user    = StringContains( p,"user" ) ;

		if( ro && StringHasComponent( m_opts,"rw" ) ){
			/*
			 * respect the option for the partition to be mounted read only
			 */
			st = 2 ;
		}else if( uid == 0 ){
			/*
			 * user is root,mount it
			 */
			st = 0 ;
		}else{
			if( nouser ){
				/*
				 * normal user is not allowed to mount it
				 */
				st = 1 ;
			}else if( user || users ){
				/*
				 * the partition has option to allow normal user to mount it,mount it
				 */
				st = 0 ;
			}else if( defaulT ){
				/*
				 * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
				 */
				if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
					/*
					 * user is a member is zulumount group,mount it
					 */
					st = 0 ;
				}else{
					st = 1 ;
				}
			}else{
				/*
				 * remaining options go there
				 */
				/*
				 * zuluCryptUserIsAMemberOfAGroup() is defined in ../zuluCrypt/bin/security.c
				 */
				if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
					/*
					 * user is a member is zulumount group,mount it
					 */
					st = 0 ;
				}else{
					st = 1 ;
				}
			}
		}
	}

	StringListDelete( &stl ) ;
	return st ;
}

int zuluMountMount( ARGS * args )
{
	const char * device    = args->device  ;
	const char * m_point   = args->m_point ;
	const char * m_opts    = args->m_opts  ;
	const char * fs_opts   = args->fs_opts ;
	uid_t        uid       = args->uid     ;
	int share              = args->share   ;

	int status ;
	string_t z = StringVoid ;
	char * path = NULL ;
	const char * rm_point ;
	unsigned long m_flags ;
	const char * dev = device ;
	const char * msg = gettext( "\
ERROR: Insuffienct privilege to manage a system volume.\nnecessary privileges can be acquired by:\n\
1. Adding an entry for the volume in fstab with \"user\" mount option\n\
2. Add yourself to \"zulumount\" group" ) ;

	if( StringPrefixEqual( device,"/dev/loop" ) ){
		/*
		 * zuluCryptLoopDeviceAddress_1() is defined in ../zuluCrypt-cli/lib/create_loop_devices.c
		 */
		path = zuluCryptLoopDeviceAddress_1( device ) ;

		if( path == NULL ){

			return _zuluExit( 112,z,path,gettext( "ERROR: Could not resolve path to device or device could not be opened in read write mode" ) ) ;
		}else{
			dev = path ;
		}
	}

	if( m_opts == NULL ){

		m_opts = "rw" ;
	}
	if( StringHasComponent( m_opts,"rw" ) ){
		/*
		 * zuluCryptCanOpenPathForWriting() is defined in ../zuluCrypt-cli/bin/path_access.c
		 */
		status = zuluCryptCanOpenPathForWriting( device,uid ) ;
	}else{
		/*
		 * zuluCryptCanOpenPathForReading() is defined in ../zuluCrypt-cli/bin/path_access.c
		 */
		status = zuluCryptCanOpenPathForReading( device,uid ) ;
	}

	if( status != 0 ){

		return _zuluExit( 112,z,path,gettext( "ERROR: Could not resolve path to device or device could not be opened in read write mode" ) ) ;
	}

	/*
	 * zuluCryptMountFlagsAreNotCorrect() is defined in ../zuluCrypt-cli/bin/mount_flags.c
	 */
	if( zuluCryptMountFlagsAreNotCorrect( m_opts,uid,&m_flags ) ){

		return _zuluExit( 100,z,path,gettext( "ERROR: Insuffienct privileges to mount the volume with given mount options" ) ) ;
	}
	/*
	 * zuluCryptPartitionIsMounted is defined in ../zuluCrypt-cli/lib/process_mountinfo.c
	 */
	if( zuluCryptPartitionIsMounted( dev ) ){

		return _zuluExit( 102,z,path,gettext( "ERROR: Device already mounted" ) ) ;
	}

	status = _zuluMountPartitionAccess( dev,m_opts,uid ) ;

	switch( status ){

		case 0 : break ;
		case 1 : return _zuluExit( 103,z,path,msg ) ;
		case 2 : return _zuluExit( 104,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume requires it to be mounted read only" ) ) ;
		case 3 : return _zuluExit( 113,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume is malformed" ) ) ;
		default: return _zuluExit( 105,z,path,gettext( "ERROR: \"/etc/fstab\" entry for this volume does not allow you to mount it" ) ) ;
	}

	/*
	 * zuluCryptSecurityCreateMountPoint() is defined in ../zuluCrypt-cli/bin/create_mount_point.c
	 */
	z = zuluCryptCreateMountPoint( device,m_point,m_opts,uid ) ;

	if( z == StringVoid ){

		return _zuluExit( 106,z,path,gettext( "ERROR: Could not create mount point path,path already taken" ) ) ;
	}

	rm_point = StringContent( z ) ;

	if( share ){

		/*
		 * zuluCryptBindSharedMountPointPathTaken() is defined in ../zuluCrypt-cli/bin/bind.c
		 */

		if( zuluCryptBindSharedMountPointPathTaken( z ) ){

			zuluCryptSecurityGainElevatedPrivileges() ;

			rmdir( rm_point ) ;

			zuluCryptSecurityDropElevatedPrivileges() ;

			return _zuluExit( 114,z,path,gettext( "ERROR: Shared mount point path aleady taken" ) ) ;
		}
	}

	zuluCryptSecurityGainElevatedPrivileges() ;
	/*
	 * zuluCryptMountVolume() defined in ../zuluCrypt-cli/lib/mount_volume.c
	 */
	status = zuluCryptMountVolume( device,rm_point,m_flags,fs_opts,uid ) ;
	zuluCryptSecurityDropElevatedPrivileges() ;

	if( status == 0 ){

		if( share ){
			/*
			 * user wish to share the mount point publicly, bind the mount point to a publicly accessed path of /run/share
			 */
			/*
			 * zuluCryptBindMountVolume() is defined in ../zuluCrypt-cli/bin/bind.c
			 */
			zuluCryptBindMountVolume( device,z,m_flags ) ;
		}

		printf( gettext( "SUCCESS: Mount complete successfully\nvolume mounted at: %s\n" ),rm_point ) ;

		return _zuluExit( 0,z,path,NULL ) ;
	}else{
		zuluCryptSecurityGainElevatedPrivileges() ;

		rmdir( rm_point ) ;

		zuluCryptSecurityDropElevatedPrivileges() ;

		switch( status ){

			case -1: return _zuluExit( 108,z,path,gettext( "ERROR: Failed to mount a filesystem:invalid/unsupported mount option or unsupported file system encountered" ) ) ;
			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?" ) ) ;
			case 4 : return _zuluExit( 110,z,path,gettext( "ERROR: Mount failed,no or unrecognized file system" ) )	;
			case 12: return _zuluExit( 111,z,path,gettext( "ERROR: Mount failed,could not get a lock on /etc/mtab~" ) ) ;
			default: return _zuluExit( 115,z,path,gettext( "ERROR: Failed to mount the partition" ) ) ;
		}
	}
}