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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
/*
*
* Copyright (c) 2011-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 <sys/mount.h>
#include <blkid/blkid.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
/*
* These functions are moved to parse_fstab.c
*
* string_t zuluCryptGetFstabEntry( const char * device )
* string_t zuluCryptGetMountOptionsFromFstab( const char * device,int pos )
* stringList_t zuluCryptGetFstabEntryList( const char * device )
*/
typedef enum { FAT_FAMILY_FS,OTHER_FAMILY_FS,READ_ONLY_FS,DEFAULT_FS_TYPE } FS_TYPE ;
static int zuluExit( int st,int fd,string_t x,string_t y,string_t z )
{
StringMultipleDelete( &x,&y,&z,NULL ) ;
if( fd != -1 ){
close( fd ) ;
}
return st ;
}
static FS_TYPE fs_family( const char * fs )
{
if( StringAtLeastOneMatch_1( fs,"ntfs","vfat","fat","msdos","umsdos","exfat",NULL ) ){
return FAT_FAMILY_FS ;
}else if( StringAtLeastOneMatch_1( fs,"affs","hfs",NULL ) ){
return OTHER_FAMILY_FS ;
}else if( StringAtLeastOneMatch_1( fs,"iso9660","udf","squashfs",NULL ) ){
return READ_ONLY_FS ;
}else{
return DEFAULT_FS_TYPE ;
}
}
/*
* custom options per file system
*/
static void _get_file_system_options_from_config_file_1( const char * fs,string_t st )
{
const char * e ;
StringListIterator it ;
StringListIterator end ;
string_t zt ;
string_t xt = StringGetFromFile( "/etc/zuluCrypt/generic_fs_options" ) ;
stringList_t stl = StringListStringSplit( xt,'\n' ) ;
stringList_t stz ;
StringDelete( &xt ) ;
StringListGetIterators( stl,&it,&end ) ;
while( it != end ){
zt = *it ;
it++ ;
if( StringStartsWith( zt,fs ) ){
stz = StringListStringSplit( zt,' ' ) ;
e = StringListContentAtSecondPlace( stz ) ;
StringMultipleAppend( st,",",e,NULL ) ;
StringListDelete( &stz ) ;
break ;
}
}
StringListDelete( &stl ) ;
}
/*
* custom options per volume set through file system's UUID
*/
static void _get_file_system_options_from_config_file( const char * device,string_t st )
{
char * f ;
const char * e ;
StringListIterator it ;
StringListIterator end ;
string_t xt = StringGetFromFile( "/etc/zuluCrypt/fs_options" ) ;
stringList_t stl = StringListStringSplit( xt,'\n' ) ;
stringList_t stz ;
StringDelete( &xt ) ;
/*
* zuluCryptUUIDFromPath_1() is defined in resolve_paths.c
*/
f = zuluCryptUUIDFromPath_1( device ) ;
StringListGetIterators( stl,&it,&end ) ;
while( it != end ){
e = StringRemoveString( *it,"\"" ) ;
it++ ;
if( StringPrefixMatch( e,"UUID=",5 ) ){
if( StringPrefixEqual( e + 5,f ) ){
stz = StringListSplit( e,' ' ) ;
e = StringListContentAtSecondPlace( stz ) ;
StringMultipleAppend( st,",",e,NULL ) ;
StringListDelete( &stz ) ;
break ;
}
}
}
StringListDelete( &stl ) ;
StringFree( f ) ;
}
static const char * _remove_duplicates( string_t st )
{
const char ** z = StringPointer( st ) ;
while( StringHasComponent( *z,",," ) ){
StringReplaceString( st,",,","," ) ;
}
if( StringEndsWithChar( st,',' ) ){
return StringRemoveRight( st,1 ) ;
}else{
return StringContent( st ) ;
}
}
static string_t set_mount_options( m_struct * mst )
{
/*
* zuluCryptGetMountOptionsFromFstab() is defined in parse_fstab.c
*/
string_t opt = zuluCryptGetMountOptionsFromFstab( mst->device,MOUNTOPTIONS,mst->uid ) ;
FS_TYPE fsFamily = fs_family( mst->fs ) ;
const char * f[] = { "nouser","users","user","defaults","noauto","auto","nodev","dev",
"noexec","exec","nosuid","suid","bind","mandlock","move","noatime","nodiratime","remount","silent",
"synchronous",NULL } ;
const char ** z = f ;
const char * e ;
if( opt == StringVoid ){
if( mst->fs_flags != NULL ){
opt = String( mst->fs_flags ) ;
}else{
opt = StringEmpty() ;
}
}else{
if( StringContains( opt,"ro" ) ){
mst->m_flags |= MS_RDONLY ;
}
StringMultipleAppend( opt,",",mst->fs_flags,NULL ) ;
}
_get_file_system_options_from_config_file_1( mst->fs,opt ) ;
_get_file_system_options_from_config_file( mst->device,opt ) ;
if( fsFamily == FAT_FAMILY_FS ){
if( StringDoesNotContain( opt,"dmask=" ) ){
StringAppend( opt,",dmask=0000" ) ;
}
if( StringDoesNotContain( opt,"umask=" ) ){
StringAppend( opt,",umask=0000" ) ;
}
if( StringDoesNotContain( opt,"uid=" ) ){
StringAppend( opt,",uid=" ) ;
StringAppendInt( opt,mst->uid ) ;
}
if( StringDoesNotContain( opt,"gid=" ) ){
StringAppend( opt,",gid=" ) ;
StringAppendInt( opt,mst->uid ) ;
}
if( StringDoesNotContain( opt,"fmask=" ) ){
StringAppend( opt,",fmask=0111" ) ;
}
if( StringsAreEqual( mst->fs,"vfat" ) ){
if( StringDoesNotContain( opt,"flush" ) ){
StringAppend( opt,",flush" ) ;
}
if( StringDoesNotContain( opt,"shortname=" ) ){
StringAppend( opt,",shortname=mixed" ) ;
}
}
if( StringsAreEqual( mst->fs,"ntfs" ) ){
if( StringDoesNotContain( opt,"big_writes" ) ){
StringAppend( opt,",big_writes" ) ;
}
}
}else if( fsFamily == OTHER_FAMILY_FS ){
if( StringDoesNotContain( opt,"uid=" ) ){
StringAppend( opt,",uid=" ) ;
StringAppendInt( opt,mst->uid ) ;
}
if( StringDoesNotContain( opt,"gid=" ) ){
StringAppend( opt,",gid=" ) ;
StringAppendInt( opt,mst->uid ) ;
}
}else if( fsFamily == READ_ONLY_FS ){
mst->m_flags |= MS_RDONLY ;
}else{
/*
* ext file systems and raiserfs among others go here
* we dont set any options for them.
*/
}
/*
* remove mount options to leave only file system options
*/
while( 1 ){
e = *z ;
z++ ;
if( e == NULL ){
break ;
}else{
StringRemoveString( opt,e ) ;
}
}
/*
* remove below two now because we are going to add them below,reason for removing them
* and readding them is because we want to make sure they are at the beginning of the string
*/
StringRemoveString( opt,"ro" ) ;
StringRemoveString( opt,"rw" ) ;
if( mst->m_flags & MS_RDONLY ){
StringPrepend( opt,"ro," ) ;
}else{
StringPrepend( opt,"rw," ) ;
}
mst->opts = _remove_duplicates( opt ) ;
return opt ;
}
static const char * _mount_options( unsigned long flags,string_t st )
{
if( flags & MS_NODEV ){
StringAppend( st,",nodev" ) ;
}
if( flags & MS_NOEXEC ){
StringAppend( st,",noexec" ) ;
}
if( flags & MS_NOSUID ){
StringAppend( st,",nosuid" ) ;
}
if( flags & MS_BIND ){
StringAppend( st,",bind" ) ;
}
if( flags & MS_MANDLOCK ){
StringAppend( st,",mandlock" ) ;
}
if( flags & MS_MOVE ){
StringAppend( st,",move" ) ;
}
if( flags & MS_NOATIME ){
StringAppend( st,",noatime" ) ;
}
if( flags & MS_NODIRATIME ){
StringAppend( st,",nodiratime" ) ;
}
if( flags & MS_RELATIME ){
StringAppend( st,",relatime" ) ;
}
if( flags & MS_REMOUNT ){
StringAppend( st,",remount" ) ;
}
if( flags & MS_SILENT ){
StringAppend( st,",silent" ) ;
}
if( flags & MS_STRICTATIME ){
StringAppend( st,",strictatime" ) ;
}
if( flags & MS_SYNCHRONOUS ){
StringAppend( st,",synchronous" ) ;
}
return _remove_duplicates( st ) ;
}
static int _mount_FUSEfs( const m_struct * mst,string_t st )
{
const char * opts = _mount_options( mst->m_flags,st ) ;
process_t p = Process( ZULUCRYPTmount,NULL ) ;
if( StringsAreEqual( mst->fs,"ntfs" ) ){
if( StringHasComponent( opts,"ignore_case" ) ){
ProcessSetArgumentList( p,"-n","-t","lowntfs-3g","-o",opts,mst->device,mst->m_point,NULL ) ;
}else{
ProcessSetArgumentList( p,"-n","-t","ntfs-3g","-o",opts,mst->device,mst->m_point,NULL ) ;
}
}else{
ProcessSetArgumentList( p,"-t",mst->fs,"-o",opts,mst->device,mst->m_point,NULL ) ;
}
ProcessStart( p ) ;
return ProcessWaitUntilFinished( &p ) ;
}
static int _mount( const m_struct * mst,string_t st )
{
int h = mount( mst->device,mst->m_point,mst->fs,mst->m_flags,mst->opts + 3 ) ;
if( st ){}
if( h == 0 && mst->m_flags != MS_RDONLY ){
chmod( mst->m_point,S_IRWXU|S_IRWXG|S_IRWXO ) ;
}
return h ;
}
static int _mount_volume( int ( *function )( const m_struct *,string_t ),
const m_struct * m,string_t st )
{
int e = function( m,st ) ;
if( e != 0 ){
/*
* mount failed for some reason,wait 2 second and then try again
*/
sleep( 2 ) ;
e = function( m,st ) ;
}
return e ;
}
const char * zuluCryptDecodeMountEntry( string_t st )
{
StringReplaceString( st,"\\012","\n" ) ;
StringReplaceString( st,"\\040"," " ) ;
StringReplaceString( st,"\\134","\\" ) ;
return StringReplaceString( st,"\\011","\\t" ) ;
}
const char * zuluCryptEncodeMountEntry( string_t st )
{
StringReplaceString( st,"\\","\\134" ) ;
StringReplaceString( st,"\n","\\012" ) ;
StringReplaceString( st," ","\\040" ) ;
return StringReplaceString( st,"\\t","\\011" ) ;
}
int zuluCryptMountVolume( const char * path,const char * m_point,unsigned long mount_opts,const char * fs_opts,uid_t uid )
{
int h ;
string_t opts = StringVoid ;
string_t fs = StringVoid ;
string_t loop = StringVoid ;
int fd = -1 ;
m_struct mst ;
mst.device = path ;
mst.m_point = m_point ;
mst.uid = uid ;
mst.m_flags = mount_opts ;
int bitLockerVolumeUsingDislocker = zuluCryptIsDislockerMapperPath( path ) ;
if( bitLockerVolumeUsingDislocker ) {
fs = zuluCryptBitLockerVolumeFS( path ) ;
}else{
/*
* zuluCryptGetFileSystemFromDevice() is defined in blkid_evaluate_tag.c
*/
fs = zuluCryptGetFileSystemFromDevice( path ) ;
}
if( StringsAreEqual_2( fs,"Nil" ) || fs == StringVoid ){
/*
* failed to read file system,probably because the volume does have any or
* a plain volume was opened with a wrong key
*/
return zuluExit( 4,fd,opts,fs,loop ) ;
}
if( StringStartsWith( fs,"crypto" ) ){
/*
* we cant mount an encrypted volume, exiting
*/
return zuluExit( 4,fd,opts,fs,loop ) ;
}
/*
* zuluCryptMountHasNotAllowedFileSystemOptions() is defined in ./mount_fs_options.c
*/
if( zuluCryptMountHasNotAllowedFileSystemOptions( uid,fs_opts,fs ) ){
return zuluExit( -1,fd,opts,fs,loop ) ;
}
mst.fs_flags = fs_opts ;
mst.fs = StringContent( fs ) ;
opts = set_mount_options( &mst ) ;
if( StringPrefixNotEqual( path,"/dev/" ) || bitLockerVolumeUsingDislocker ){
/*
* zuluCryptAttachLoopDeviceToFile() is defined in ./create_loop_device.c
*/
if( zuluCryptAttachLoopDeviceToFile( mst.device,O_RDWR,&fd,&loop ) ){
mst.device = StringContent( loop ) ;
}else{
return zuluExit( -1,fd,opts,fs,loop ) ;
}
}
/*
* zuluCryptFileSystemIsFUSEbased() is defined in blkid_evaluate_tag.c
*/
if( zuluCryptFileSystemIsFUSEbased( path ) ){
/*
* These file systems dont see to work with mount() command for some reason.
* Them being FUSE based could be a reason.
*/
switch( _mount_volume( _mount_FUSEfs,&mst,opts ) ){
case 0 : return zuluExit( 0,fd,opts,fs,loop ) ;
case 16 : return zuluExit( 12,fd,opts,fs,loop ) ;
case 32 : return zuluExit( -1,fd,opts,fs,loop ) ;
default : return zuluExit( 1,fd,opts,fs,loop ) ;
}
}else{
h = _mount_volume( _mount,&mst,opts ) ;
}
return zuluExit( h,fd,opts,fs,loop ) ;
}
|