📄 bcmount.c
字号:
/********************************************************************* Copyright (c) 1994-1999 Jetico, Inc., Finland* All rights reserved.** File: bccreate.c* Revision: $Id: bcmount.c,v 1.33 2005/05/14 07:58:33 crypt Rel-1.6-3 $* Created:* Description: implementation of mount container routine********************************************************************/#include <sys/stat.h>#include <stdlib.h>#include <dirent.h>#include <errno.h>#include <stdio.h>#include <mntent.h>#include <unistd.h>#include <sys/mount.h>#ifndef MS_MGC_VAL #include <linux/fs.h>#endif#include <bc_types.h>#include <bc_ioctl.h>#include "keygen.h"#include <kg_defs.h>#include <fcntl.h>#include <kblock.h>#include <string.h>#include "alg.h"#include "header.h"#include "config.h"#include "bctool.h"#include "misc.h"#include "error.h"char bcmount_c[]="$Id: bcmount.c,v 1.33 2005/05/14 07:58:33 crypt Rel-1.6-3 $";int bcumount_all (int argc, char ** argv);int local_umount(char *mount_point){ char *device; ALG_SERV Alg; int Error; sync(); device=FindMountedDevice(mount_point); if ( device==NULL ) { msg(stderr,"Can not find mounted device\n"); return -1; } if ( (Alg.bc_fd = open(device, O_RDONLY)) < 0 ) { msg(stderr,"open(%s,O_RDONLY): %s\n",device,strerror(errno)); return -1; } if ( bc_get_priv(Alg,0)!=0 ) { msg(stderr,"bc_get_priv: %s\n",strerror(errno)); close(Alg.bc_fd); return -1; } if (o_force) { Error=bc_force_unlock(Alg); if ( Error!=0 ) { msg(stderr,"bc_force_unlock(%s): %s\n",device,strerror(-Error)); close(Alg.bc_fd); return -1; } }else { Error=bc_unlock_dev(Alg); if ( Error!=0 ) { msg(stderr,"bc_unlock_dev(%s): %s\n",device,strerror(-Error)); close(Alg.bc_fd); return -1; } } if ( umount(mount_point)!=0 ) { msg(stderr,"umount(%s): %s\n",mount_point,strerror(errno)); bc_lock_dev(Alg); close(Alg.bc_fd); return -1; } Error=bc_clear_fd(Alg); if ( Error!=0 ) { msg(stderr,"bc_clear_fd: %s\n",strerror(-Error)); close(Alg.bc_fd); return -1; } close(Alg.bc_fd); return 0;}int bcmount (int argc, char ** argv){ struct HiddenSector hs; char *device; int fd, fdd, flag; long long Offset; ALG_SERV Alg; DWORD KeyHandle,DataSize,_offset; DATA_BLOCK db, *pdb; alg_info *alg_inf; FILE *mtab; struct mntent mnt; char rp1[NAME_MAX+PATH_MAX]; char rp2[NAME_MAX+PATH_MAX]; char options[256]; unsigned long flags; int fs_id; if ( argc < 3 ) { msg(stderr,"Please specify container name and mount point\n"); return -1; } else if ( argc > 3 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( (realpath(argv[1],rp1)) == NULL ) { msg(stderr,"realpath(%s,rp1): %s\n",argv[1],strerror(errno)); return -1; } if ( (realpath(argv[2],rp2)) == NULL ) { msg(stderr,"realpath(%s,rp2): %s\n",argv[2],strerror(errno)); return -1; } Error=IsOwner(ruid,argv[2]); if ( Error==FALSE && ruid!=0 ) { msg(stderr,"Sorry, you are not owner of %s\n",rp2); return -1; } else if ( Error==-1 ) { msg(stderr,"lstat(%s): %s\n",rp2,strerror(errno)); return -1; } if ( (device=FindFreeDevice())==NULL ) { msg(stderr,"Can not find free device\n"); return -1; } if ( (Alg.bc_fd = open(device, O_RDONLY)) < 0 ) { msg(stderr,"open(%s): %s\n",device,strerror(errno)); return -1; } if ( (fd = open(argv[1], O_RDONLY|O_LARGEFILE)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[1],strerror(errno)); close(Alg.bc_fd); return -1; } if ( read(fd, &hs, sizeof(hs))!=sizeof(hs) ) { msg(stderr, "read(%s,&hs,%d): %s\n", argv[1], sizeof(hs), strerror(errno)); close(Alg.bc_fd); close(fd); return -1; } if ( strncmp(hs.br.OEMid, "LOCOS94 ", sizeof(hs.br.OEMid))!=0 ) { msg(stderr,"%s is not valid BestCrypt container\n",argv[1]); close(Alg.bc_fd); close(fd); return -1; } if (0 != access(argv[1],R_OK|W_OK)) { msg(stderr,"You do not have write permissions for this container.\n \ Mounting read-only\n"); o_ro=1; } if ('B' == hs.br.jmpCode[0] && FALSE == o_ro) { msg(stderr, "WARNING! Container \"%s\" is probably in use!\n\n" "Two reasons for the report are possible:\n" " 1. The container is already mounted. In this case do not use the container\n" " until someone dismounts it.\n" " 2. The container was not unmounted properly last time, so a lock mask \n" " for the container was not reset. If so, unlock the container by running the command:\n" " \"bctool unlock %s\" \n\n", argv[1],argv[1]); return -1; } if ( read(fd, &db, sizeof (db))!=sizeof(db) ) { msg(stderr,"read(%s,&db,%d): %s\n",argv[1],sizeof(hs),strerror(errno)); close(Alg.bc_fd); close(fd); return -1; } if ( (alg_inf=GetInfoById(hs.algorithmId))==NULL ) { msg(stderr,"Unknown algorithm Id: %d\n",hs.algorithmId); close(Alg.bc_fd); close(fd); return -1; } if ( o_fstype==NULL && (o_fstype=getFStypeById(hs.fileSystemId))==NULL ) { msg(stderr,"Unknown filesystem. Try: -t fstype \n"); return -1; } Alg.alg_id=hs.algorithmId; Alg.alg_module =alg_inf->mod_name; pdb = &db; DataSize = sizeof(db);// Offset=hs.dwDataOffset; if (KGSHA256_ID == hs.keyGenId) { UseSha256(); } CreateKeyHandleEx(Alg, alg_inf->key_size, NULL, "Enter password:", CFLAG_VERIFY_AND_LOAD_KEY, (BYTE **)&pdb, &DataSize, &KeyHandle, &Error, &_offset); if ( Error!=0 ) { print_error(Error); close(Alg.bc_fd); close(fd); return -1; } if (_offset) Offset = (long long)512 * _offset; else Offset = hs.dwDataOffset; Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, 0, 0, (o_ro ? BC_FLAGS_READONLY : 0) | (hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT)); if ( Error!=0 ) { msg(stderr,"bc_set_fd_kh: %s\n",strerror(-Error)); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; }//----------------------------------------------------------------------// BestCrypt for Windows 7.11+ compatibility code//---------------------------------------------------------------------- fdd = open(device, O_RDONLY); // funny workaround for 2.4.x kernels flag = 0; _offset = check_msdos_partition(fdd < 0 ? Alg.bc_fd : fdd, &flag); if (fdd >= 0) close(fdd); if (0 != (flag & 0xFF000000)) { // partition table detected msg(stderr, "Warning! %d (%d extended) partition(s) detected inside container.\n", flag >> 24, (flag >> 16) & 0xFF); if (0x0100 != (flag >> 16)) { // non-trivial case if (!ask_y_n("Do you want to continue? (y/[n]):")) { FreeKeyHandle(Alg, KeyHandle); close(Alg.bc_fd); close(fd); return 0; } }// Offset += (long long) 512 * _offset; bc_clear_fd(Alg); Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, (long long)_offset, 0, (o_ro ? BC_FLAGS_READONLY : 0) | (hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT)); if ( Error!=0 ) { msg(stderr,"bc_set_fd_kh: %s\n",strerror(-Error)); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } }//---------------------------------------------------------------------- //---------------------------------------------------------------------- snprintf(options,sizeof(options),"uid=%d,gid=%d,umask=%o",o_uid,o_gid,(~o_mode)&07777); flags=MS_MGC_VAL|MS_SYNCHRONOUS|(MS_RDONLY & o_ro); //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -