📄 bcmount.c
字号:
// disable suid and dev files if not root // if ( ruid!=0 ) flags|=MS_NOSUID|MS_NODEV; fs_id=getIdByFStype(o_fstype); if ( bc_get_priv(Alg,0)!=0 ) { msg(stderr,"bc_get_priv: %s\n",strerror(errno)); bc_clear_fd(Alg); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } if ( fs_id == 1 || fs_id == 2 || fs_id == 3 || fs_id == 8) { if ( mount(device,argv[2],o_fstype,flags,NULL)!=0 ) { msg(stderr,"mount(%s,%s,%s,%X,NULL): %s\n",device,argv[2],o_fstype,flags,strerror(errno)); bc_clear_fd(Alg); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } } else if ( mount(device,argv[2],o_fstype,flags,options)!=0 && mount(device,argv[2],o_fstype,flags,NULL)!=0 ) { msg(stderr,"mount(%s,%s,%s,%X,%s): %s\n",device,argv[2],o_fstype,flags,options,strerror(errno)); bc_clear_fd(Alg); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } Error=bc_lock_dev(Alg); if ( Error!=0 ) { msg(stderr,"bc_lock_dev(%s): %s\n",device,strerror(-Error)); close(Alg.bc_fd); close(fd); return -1; } mnt.mnt_fsname = rp1; mnt.mnt_dir = rp2; mnt.mnt_type=o_fstype; mnt.mnt_opts= o_ro ? "ro,BestCrypt" : "rw,BestCrypt"; mnt.mnt_freq=0; mnt.mnt_passno=0; mtab = setmntent("/etc/mtab", "r+"); if ( mtab != NULL ) { addmntent(mtab, &mnt); endmntent(mtab); } else msg(stderr,"Can not open /etc/mtab. Mount entry will not be added in mtab file.\n"); FreeKeyHandle(Alg,KeyHandle); close(fd); close(Alg.bc_fd); chown(rp2,o_uid,o_gid); chmod(rp2,o_mode); if ( setreuid(ruid,ruid)!=0 ) { msg(stderr,"setreuid(%d,%d): %s\n",ruid,ruid,strerror(errno)); return -1; } if (!o_ro && lock_container(argv[1])) { msg(stderr,"lock_container(\"%s\"): %s\n",argv[1],strerror(errno)); return -1; } return 0;}int bcumount (int argc, char ** argv){ ALG_SERV Alg; char *device; FILE *mtab,*tmptab; struct mntent *pmnt; char rp[NAME_MAX+PATH_MAX]; if (o_umount_all) { return bcumount_all(argc,argv); } if ( argc < 2 ) { msg(stderr,"Please specify mount point\n"); return -1; } else if ( argc > 2 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( NULL == realpath(argv[1],rp) ) { msg(stderr,"realpath(%s,rp): %s\n",argv[1],strerror(errno)); return -1; }; Error=IsOwner(ruid,rp); if ( Error==FALSE && ruid!=0 ) { msg(stderr,"sorry, you are not owner of this mount point\n"); return -1; } else if ( Error==-1 ) { msg(stderr,"IsOwner(%d,rp): %s\n",ruid,strerror(errno)); return -1; } sync(); device=FindMountedDevice(rp); 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(rp)!=0 ) { msg(stderr,"umount(%s): %s\n",rp,strerror(errno)); bc_lock_dev(Alg); close(Alg.bc_fd); return -1; } mtab = setmntent("/etc/mtab", "r"); tmptab = setmntent("/etc/mtab.tmp", "w+"); if (NULL == mtab || NULL == tmptab) { msg(stderr,"Can not modify /etc/mtab file: %s\n",strerror(errno)); }else { struct stat st; while ( 1 ) { pmnt=getmntent(mtab); if ( feof(mtab) )break; if ( strcmp(rp,pmnt->mnt_dir) ) addmntent(tmptab, pmnt); } endmntent(mtab); endmntent(tmptab); if ( stat("/etc/mtab",&st)!=0 ) { msg(stderr,"stat(\"/etc/mtab\"): %s\n",strerror(errno)); st.st_mode = 0644; st.st_uid = 0; st.st_gid = 0; } if ( rename("/etc/mtab","/etc/mtab~")!=0 ) msg(stderr,"rename(\"/etc/mtab\",\"/etc/mtab~\"): %s\n",strerror(errno)); if ( rename("/etc/mtab.tmp","/etc/mtab")!=0 ) msg(stderr,"rename(\"/etc/mtab.tmp\",\"/etc/mtab\"): %s\n",strerror(errno)); if ( chown("/etc/mtab",st.st_uid,st.st_gid)!=0 ) msg(stderr,"chown(\"/etc/mtab\",%d,%d): %s\n",st.st_uid,st.st_gid,strerror(errno)); if ( chmod("/etc/mtab",st.st_mode)!=0 ) msg(stderr,"chmod(\"/etc/mtab\",%o): %s\n",st.st_mode,strerror(errno)); if ( unlink("/etc/mtab~")!=0 ) msg(stderr,"unlink(\"/etc/mtab~\"): %s\n",strerror(errno)); } if ( setreuid(ruid,ruid)!=0 ) { msg(stderr,"setreuid(%d,%d): %s\n",ruid,ruid,strerror(errno)); 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 bcumount_all (int argc, char ** argv){ FILE *mtab,*tmptab; struct mntent *pmnt; struct stat st; int count = 0;// char tmptab_name[32]; if ( argc > 1 ) { msg(stderr,"Too many parameters. ...ignoring...\n"); } if ( get_priv(0)!=0 ) { return -1; } mtab = setmntent("/etc/mtab", "r"); tmptab = setmntent("/etc/mtab.tmp", "w+"); if (NULL == mtab) { msg(stderr,"Can not read /etc/mtab file: %s\n",strerror(errno)); return -1; } while ( 1 ) { pmnt=getmntent(mtab); if ( feof(mtab) )break; if ( hasmntopt(pmnt, "BestCrypt") && (0 == ruid || IsOwner(ruid,pmnt->mnt_dir)) ) { if (o_verbose) msg(stderr,"Unmounting \"%s\" ... \t",pmnt->mnt_dir); if (0 == local_umount(pmnt->mnt_dir)) { count++; if (o_verbose) msg(stderr,"Done\n"); } }else { if (tmptab) addmntent(tmptab, pmnt); } } endmntent(mtab); if (tmptab) endmntent(tmptab); if (tmptab && 0 != count) { if ( stat("/etc/mtab",&st)!=0 ) { msg(stderr,"stat(\"/etc/mtab\"): %s\n",strerror(errno)); st.st_mode = 0644; st.st_uid = 0; st.st_gid = 0; } if ( rename("/etc/mtab","/etc/mtab~")!=0 ) msg(stderr,"rename(\"/etc/mtab\",\"/etc/mtab~\"): %s\n",strerror(errno)); if ( rename("/etc/mtab.tmp","/etc/mtab")!=0 ) msg(stderr,"rename(\"/etc/mtab.tmp\",\"/etc/mtab\"): %s\n",strerror(errno)); if ( chown("/etc/mtab",st.st_uid,st.st_gid)!=0 ) msg(stderr,"chown(\"/etc/mtab\",%d,%d): %s\n",st.st_uid,st.st_gid,strerror(errno)); if ( chmod("/etc/mtab",st.st_mode)!=0 ) msg(stderr,"chmod(\"/etc/mtab\",%o): %s\n",st.st_mode,strerror(errno)); if ( unlink("/etc/mtab~")!=0 ) msg(stderr,"unlink(\"/etc/mtab~\"): %s\n",strerror(errno)); } unlink("/etc/mtab.tmp"); if (o_verbose) msg(stderr,"Unmounted %d containers.\n",count); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -