From: Sungjun, Lee Date: Wed, 14 Dec 2016 02:00:41 +0000 (+0900) Subject: Add Error routine to eCryptfs engine X-Git-Tag: submit/tizen/20170213.020148~29 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cffd74fbca85d38752c973284634b426758f60ad;p=platform%2Fcore%2Fsecurity%2Fode.git Add Error routine to eCryptfs engine Change-Id: Ief5c50b65e419a768f87f76bef9e7563a89c86a7 Signed-off-by: Sungjun, Lee --- diff --git a/server/engine/ecryptfs-engine.cpp b/server/engine/ecryptfs-engine.cpp index 328fed4..d4c772d 100644 --- a/server/engine/ecryptfs-engine.cpp +++ b/server/engine/ecryptfs-engine.cpp @@ -15,6 +15,7 @@ */ #include #include +#include #include "ecryptfs-engine.h" @@ -63,7 +64,7 @@ int EcryptfsEngine::add_user_auth_token_to_keyring(ecryptfs_payload *payload) (void *)payload, sizeof(ecryptfs_payload), KEY_SPEC_USER_KEYRING) <= 0) { result = ERR_FAILED_AUTH; } else { - std::cout << "Token added = " << payload->token.password.signature << std::endl; + INFO("Token added = " + std::string((const char *)payload->token.password.signature)); } } @@ -93,43 +94,43 @@ void EcryptfsEngine::build_ecryptfs_options(char *ecryptfs_opts, char *sig, int ECRYPTFS_KEY_BYTES"%d", sig, ECRYPTFS_FEK_CIPHER, MAX_KEY_BYTES); if (excludeMediaTypes == EXCL_MEDIA_ON) { - std::cout << "building options with media files filtering." << std::endl; + INFO("building options with media files filtering."); ::strcat(ecryptfs_opts, ","); ::strcat(ecryptfs_opts, ECRYPTFS_ENABLE_FILTERING); ::strcat(ecryptfs_opts, ECRYPTFS_MEDIA_EXCLUSION_LIST); } else if (excludeMediaTypes == EXCL_ALL_NEW_ON) { - std::cout << "building options with all new files filtering." << std::endl; + INFO("building options with all new files filtering."); ::strcat(ecryptfs_opts, ","); ::strcat(ecryptfs_opts, ECRYPTFS_ENABLE_FILTERING); ::strcat(ecryptfs_opts, ECRYPTFS_GLOBAL_EXCLUSION_LIST); } else { - std::cout << "building options without file encryption filtering." << std::endl; + INFO("building options without file encryption filtering."); } if (smack_check()) { - std::cout << "smack fs was enabled, add smack labeling" << std::endl; + INFO("smack fs was enabled, add smack labeling"); ::strcat(ecryptfs_opts, ","); ::strcat(ecryptfs_opts, SMACKFS_MOUNT_OPT); } } -int EcryptfsEngine::mount(const data &key) +void EcryptfsEngine::mount(const data &key) { int result = ERR_NO; unsigned char master_key[MAX_KEY_BYTES] = {0}; char ecryptfs_opts[ECRYPTFS_MAX_OPTIONS] = {0}; - std::cout << "Source: " << mSource << std::endl; - std::cout << "Destination: " << mDestination << std::endl; - std::cout << "EcryptfsMount: "; + INFO("Source: " + mSource); + INFO("Destination: " + mDestination); + INFO("EcryptfsMount: "); if (key.size() == 0) - return -1; + throw runtime::Exception("Key size is zero."); for (unsigned int iter = 0; iter < key.size(); iter++) { master_key[iter] = key[iter]; } if (isEcryptfsMountpointMounted(mDestination) == 0) { - std::cout << "Already Mounted" << std::endl; - return 0; + INFO("Already Mounted"); + throw runtime::Exception("Already Mounted."); } //MOUNT_ECRYPTFS_DRIVE @@ -146,83 +147,89 @@ int EcryptfsEngine::mount(const data &key) result = add_user_auth_token_to_keyring(&sde_payload); if (result == ERR_NO) { build_ecryptfs_options(ecryptfs_opts, (char *)sde_payload.token.password.signature, mExcludeMedia); - std::cout << "encrypted fs option : " << ecryptfs_opts << ", source : " << mSource << ", dest : " << mDestination << std::endl; + INFO("encrypted fs option : " + std::string(ecryptfs_opts) + ", source : " + mSource + ", dest : " + mDestination); if (::mount(mSource.c_str(), mDestination.c_str(), ECRYPTFS_MOUNT_DEVICE, MS_NODEV, ecryptfs_opts) != 0) { - std::cout << "Unable to mount drive: " << strerror(errno) << std::endl; - result = ERR_MOUNT; + INFO("Unable to mount drive: " + std::string(strerror(errno))); + throw runtime::Exception("Unable to mount drive."); } else { - std::cout << "Mount is completed." << std::endl; + INFO("Mount is completed."); } } else { - std::cout << "Unable to add token to keyring" << std::endl; + INFO("Unable to add token to keyring"); + throw runtime::Exception("Unable to add token to keyring."); } - - return result; } -int EcryptfsEngine::umount() +void EcryptfsEngine::umount() { int result = ERR_NO; - std::cout << "EcryptfsUmount" << std::endl; + INFO("EcryptfsUmount"); if (isEcryptfsMountpointMounted(mDestination) == 0) { if (::umount(mDestination.c_str()) != 0) { if (::umount2(mDestination.c_str(), MNT_EXPIRE) != 0) { - std::cout << "Unmount failed for drive " << mDestination.c_str() << "err(" << errno << strerror(errno) << ")" << std::endl; + INFO("Unmount failed for drive " + mDestination + "err(" + std::to_string(errno) + std::string(strerror(errno)) + ")"); if (errno == EAGAIN) { - std::cout << "Trying Unmount again" << std::endl; + INFO("Trying Unmount again"); if (::umount2(mDestination.c_str(), MNT_EXPIRE) != 0) { - result = ERR_UNMOUNT; - std::cout << "Unmount failed for drive" << mDestination.c_str() << "err(" << errno << strerror(errno) << ")" << std::endl; + INFO("Unmount failed for drive" + mDestination + "err(" + std::to_string(errno) + std::string(strerror(errno)) + ")"); + throw runtime::Exception("Unmount failed for drive."); } } else { - std::cout << "Drive " << mDestination.c_str() << "unmounted failed " << std::endl; - result = ERR_UNMOUNT; + INFO("Drive " + mDestination + "unmounted failed "); + throw runtime::Exception("Unmount failed for drive."); } } } if (result == ERR_NO) - std::cout << "Drive " << mDestination.c_str() << " unmounted successfully" << std::endl; - else - std::cout << "Drive " << mDestination.c_str() << " unmounted failed" << std::endl; + INFO("Drive " + mDestination + " unmounted successfully"); + else { + INFO("Drive " + mDestination + " unmounted failed"); + throw runtime::Exception("Unmount failed for drive."); + } } - return result; } -int EcryptfsEngine::encrypt(const data &key) +void EcryptfsEngine::encrypt(const data &key) { int result = ERR_NO; - std::cout << "EcryptfsEncrypt" << std::endl; + INFO("EcryptfsEncrypt"); if (!isMountpointMounted(mSource)) { - std::cout << "SD Card not inserted!" << std::endl; - return -1; + INFO("SD Card not inserted!"); + throw runtime::Exception("SD Card not inserted."); } result = DoCrypt(key, mDestination.c_str(), 1, EXCL_MEDIA_ON); - - return result; + if (result != ERR_NO) { + INFO("Encrypt failed!"); + throw runtime::Exception("Encrypt failed."); + } } -int EcryptfsEngine::decrypt(const data &key) +void EcryptfsEngine::decrypt(const data &key) { int result = ERR_NO; - std::cout << "EcryptfsDecrypt" << std::endl; + INFO("EcryptfsDecrypt"); if (!isMountpointMounted(mSource)) { - std::cout << "SD Card not inserted!" << std::endl; - return -1; + INFO("SD Card not inserted!"); + throw runtime::Exception("SD Card not inserted."); } result = DoCrypt(key, mDestination.c_str(), 0, EXCL_ALL_NEW_ON); - return result; + if (result != ERR_NO) { + INFO("Decrypt failed!"); + throw runtime::Exception("Decrypt failed."); + } } int EcryptfsEngine::DoCrypt(const data &key, const char *path, int reqEnc, int excludeMedia) { + int result = ERR_NO; const char *cryptTempFile = CRYPT_META_FILE; - std::cout << "DoCrypt: reqEnc: " << reqEnc << ", excludeMedia: " << excludeMedia << std::endl; + INFO("DoCrypt: reqEnc: " + std::to_string(reqEnc) + ", excludeMedia: " + std::to_string(excludeMedia)); mPath = path; mReqEnc = reqEnc; @@ -233,19 +240,21 @@ int EcryptfsEngine::DoCrypt(const data &key, const char *path, int reqEnc, int e } if (reqEnc) - DoEncrypt(key); + result = DoEncrypt(key); else - DoDecrypt(key); + result = DoDecrypt(key); - return 0; + return result; } int EcryptfsEngine::DoEncrypt(const data &key) { - std::cout << "DoEncrypt()" << std::endl; + INFO("DoEncrypt()"); - if (mount(key) != ERR_NO) { - std::cout << "EcryptfsEngine: Error mounting " << mPath << std::endl; + try { + mount(key); + } catch (runtime::Exception &e) { + INFO("EcryptfsEngine: Error mounting " + std::string(mPath)); goto error; } @@ -259,9 +268,9 @@ int EcryptfsEngine::DoEncrypt(const data &key) if (mTotalFileCt) { mTotalCopied = 0; - std::cout << "calling the recursive function EncryptFile (" << mPath << ")" << std::endl; + INFO("calling the recursive function EncryptFile (" + std::string(mPath) + ")"); if (cryptInplace(mPath, "", false) != 0) { - std::cout << "Ecryptfs: Full Encryption couldn't complete" << std::endl; + INFO("Ecryptfs: Full Encryption couldn't complete"); goto error; } } @@ -275,14 +284,16 @@ error: int EcryptfsEngine::DoDecrypt(const data &key) { - std::cout << "DoDecrypt()" << std::endl; + INFO("DoDecrypt()"); if (checkEncryptMetaData(ORIG_META_FILE_PATH) != 0) { goto success; } - if (mount(key) != ERR_NO) { - std::cout << "EcryptfsEngine: Error mounting " << mPath << std::endl; + try { + mount(key); + } catch (runtime::Exception &e) { + INFO("EcryptfsEngine: Error mounting " + std::string(mPath)); goto error; } @@ -292,16 +303,16 @@ int EcryptfsEngine::DoDecrypt(const data &key) if (mTotalFileCt) { mTotalCopied = 0; - std::cout << "calling the recursive function EncryptFile (" << mPath << ")" << std::endl; + INFO("calling the recursive function EncryptFile (" + std::string(mPath) + ")"); if (cryptInplace(mPath, "", true) != 0) { - std::cout << "Ecryptfs: Full Decryption couldn't complete" << std::endl; + INFO("Ecryptfs: Full Decryption couldn't complete"); goto error; } } deleteEncryptMetaData(mMetaDataFile); success: - std::cout << "Decryption Completed !!!" << std::endl; + INFO("Decryption Completed !!!"); umount(); return 0; error: @@ -316,7 +327,7 @@ long long EcryptfsEngine::CopyImpl(int sfd, int dfd, long long fullsz, bool enct char buffer [ECRYPTFS_BUFFER_SIZE]; - std::cout << "CopyImpl" << std::endl; + INFO("CopyImpl"); while (1) { ssize_t rdsz = 0; @@ -327,14 +338,14 @@ long long EcryptfsEngine::CopyImpl(int sfd, int dfd, long long fullsz, bool enct } if (rdsz < 0) { - std::cout << "Error reading src file" << std::endl; + INFO("Error reading src file"); break; } if (dfd >= 0) { ssize_t wrsz = FullWrite(dfd, buffer, rdsz); if (wrsz < rdsz) { - std::cout << "Write Error" << std::endl; + INFO("Write Error"); break; } } @@ -386,18 +397,18 @@ int EcryptfsEngine::CopyFile(const char *src, const char *dest, struct stat *src struct utimbuf times; long long retval = 0; - std::cout << "Copy start " << src << " => " << dest << std::endl; + INFO("Copy start " + std::string(src) + " => " + std::string(dest)); sfd = ::open(src, O_RDONLY, 0666); if (sfd < 0) { - std::cout << "Cann't open " << src << std::endl; + INFO("Cann't open " + std::string(src)); return -1; } dfd = ::open(dest, O_WRONLY | O_CREAT | O_TRUNC, src_stat->st_mode); if (dfd < 0) { - std::cout << "Error opening the destination file " << dest << std::endl; + INFO("Error opening the destination file " + std::string(dest)); ::close(sfd); return -1; } @@ -405,7 +416,7 @@ int EcryptfsEngine::CopyFile(const char *src, const char *dest, struct stat *src retval = CopyImpl(sfd, dfd, src_stat->st_size, enctype); if (retval < 0) { - std::cout << "Encryption Error CopyData returned <" << retval << "> : <" << src << "> errno " << errno << " " << strerror(errno) << std::endl; + INFO("Encryption Error CopyData returned <" + std::to_string(retval) + "> : <" + std::string(src) + "> errno " + std::to_string(errno) + " " + std::string(strerror(errno))); ::close(sfd); ::close(dfd); return retval; @@ -415,14 +426,14 @@ int EcryptfsEngine::CopyFile(const char *src, const char *dest, struct stat *src times.modtime = src_stat->st_mtime; if (::utime(dest, ×) < 0) - std::cout << "can't preserve times of '" << src << "'" << std::endl; + INFO("can't preserve times of '" + std::string(src) + "'"); - std::cout << "Copy Completed" << std::endl; + INFO("Copy Completed"); if (::fsync(dfd) != 0) - std::cout << "can't fsync of '" << src << "'" << std::endl; + INFO("can't fsync of '" + std::string(src) + "'"); if (::posix_fadvise(dfd, 0, src_stat->st_size, POSIX_FADV_DONTNEED) < 0) - std::cout << "can't fadvise of '" << dest << "'" << std::endl; + INFO("can't fadvise of '" + std::string(dest) + "'"); ::close(sfd); ::close(dfd); @@ -435,11 +446,11 @@ void EcryptfsEngine::syncdatafile(const char *src) int sfd; sfd = ::open(src, O_WRONLY); if (sfd < 0) { - std::cout << "Cann't open " << src << std::endl; + INFO("Cann't open " + std::string(src)); return; } if (::fsync(sfd) != 0) - std::cout << "can't fsync of '" << src << "'" << std::endl; + INFO("can't fsync of '" + std::string(src) + "'"); /* just open and close for flushing file to ecryptfs */ ::close(sfd); } @@ -450,7 +461,7 @@ int EcryptfsEngine::cryptInplace(const char *src, const char *tmpdest, bool dec_ int enc_fl = 0; if (::lstat(src, & src_stat) < 0) - std::cout << "source : < " << src << "> is a dangling link" << std::endl; + INFO("source : < " + std::string(src) + "> is a dangling link"); if (S_ISDIR(src_stat.st_mode)) { /* dir call recursively */ @@ -474,7 +485,7 @@ int EcryptfsEngine::cryptInplace(const char *src, const char *tmpdest, bool dec_ } if (cryptInplace(nsrc, ntmpdest, dec_fl) < 0) { - std::cout << "Encryptfile returned error for nsrc <" << nsrc << ">" << std::endl; + INFO("Encryptfile returned error for nsrc <" + std::string(nsrc) + ">"); retval = -1; delete [] ntmpdest; delete [] nsrc; @@ -492,22 +503,22 @@ int EcryptfsEngine::cryptInplace(const char *src, const char *tmpdest, bool dec_ if ((!dec_fl && enc_fl == WAS_NOT_ENCRYPTED) || (dec_fl && enc_fl == WAS_ENCRYPTED)) { - std::cout << "ENC/DEC " << src << std::endl; + INFO("ENC/DEC " + std::string(src)); retval = CopyFile(src, tmpdest, &src_stat, dec_fl); if (retval) { - std::cout << "CopyFileImpl returned error <" << retval << "> errno " << errno << strerror(errno) << std::endl; + INFO("CopyFileImpl returned error <" + std::to_string(retval) + "> errno " + std::to_string(errno) + std::string(strerror(errno))); return retval; } ::unlink(src); retval = ::rename(tmpdest, src); syncdatafile(src); if (retval) { - std::cout << "rename returned error <" << retval << "> errno " << errno << strerror(errno) << std::endl; + INFO("rename returned error <" + std::to_string(retval) + "> errno " + std::to_string(errno) + std::string(strerror(errno))); return retval; } } else - std::cout << "File already encrpyted/Decrypted : <" << src << ">" << std::endl; + INFO("File already encrpyted/Decrypted : <" + std::string(src) + ">"); } return retval; @@ -519,24 +530,24 @@ int EcryptfsEngine::preScanForEncrypt(const char *src) struct statfs statbuf; int result = ERR_NO; - std::cout << "preScanForEncrypt" << std::endl; + INFO("preScanForEncrypt"); do { ::memset(&szinfo, 0, sizeof(sizeinfo_type)); if (!::statfs(src, &statbuf)) { szinfo.availsz = (long long)statbuf.f_bfree * statbuf.f_bsize; szinfo.blocksz = (int)statbuf.f_bsize; - std::cout << "Free size available from statfs : <" << szinfo.availsz << ">" << std::endl; + INFO("Free size available from statfs : <" + std::to_string(szinfo.availsz) + ">"); } else { - std::cout << "can't access " << src << std::endl; + INFO("can't access " + std::string(src)); return PRESCAN_ERR; } mPreScanEncryptErr = 0; result = getEncryptedSize(src, &szinfo); - std::cout << "Prescan Free <" << szinfo.availsz << ">, source size <" << szinfo.cursz << ">, Source Encrypted size <" << szinfo.encsz << "> largeFileSz <" - << szinfo.largesz << "> filecount <" << szinfo.filecount << "> for <" << src << ">" << std::endl; - std::cout << "retval <" << result << "> PrescanencryptErr <" << mPreScanEncryptErr << ">" << std::endl; + INFO("Prescan Free <" + std::to_string(szinfo.availsz) + ">, source size <" + std::to_string(szinfo.cursz) + ">, Source Encrypted size <" + std::to_string(szinfo.encsz) + "> largeFileSz <" + + std::to_string(szinfo.largesz) + "> filecount <" + std::to_string(szinfo.filecount) + "> for <" + std::string(src) + ">"); + INFO("retval <" + std::to_string(result) + "> PrescanencryptErr <" + std::to_string(mPreScanEncryptErr) + ">"); mTotalFileCt = szinfo.filecount; mTotalStSz = szinfo.totalstsz; @@ -549,24 +560,24 @@ int EcryptfsEngine::preScanForEncrypt(const char *src) if (isEcryptfsMountpointMounted(mPath)) { umount(); - std::cout << "Unmounting mpath <" << mPath << ">" << std::endl; + INFO("Unmounting mpath <" + std::string(mPath) + ">"); } - std::cout << "Prescan failed with Too full error, need more space, " << needed << " extra, total " << total << std::endl; + INFO("Prescan failed with Too full error, need more space, " + std::to_string(needed) + " extra, total " + std::to_string(total)); result = PRESCAN_ERR; } if (result == PRESCAN_TEMP_FILE_EXIST_AND_SIZE_ERR) { - std::cout << "EcryptfsEngine: Need to run Prescan Again" << std::endl; + INFO("EcryptfsEngine: Need to run Prescan Again"); continue; } if (result == 0) { - std::cout << "EcryptfsEngine: Prescan completed successfully" << std::endl; + INFO("EcryptfsEngine: Prescan completed successfully"); return result; } - std::cout << "EcryptfsEngine: Cannot encrypt, Disk Space is not enough" << std::endl; + INFO("EcryptfsEngine: Cannot encrypt, Disk Space is not enough"); return PRESCAN_ERR; } while (1); } @@ -577,24 +588,24 @@ int EcryptfsEngine::preScanForDecrypt(const char *src) struct statfs statbuf; int result = ERR_NO; - std::cout << "preScanForDecrypt" << std::endl; + INFO("preScanForDecrypt"); do { ::memset(&szinfo, 0, sizeof(EcryptfsEngine::sizeinfo_type)); if (!::statfs(src, &statbuf)) { szinfo.availsz = (long long)statbuf.f_bfree * statbuf.f_bsize; szinfo.blocksz = (int)statbuf.f_bsize; - std::cout << "Free size available from statfs : <" << szinfo.availsz << ">" << std::endl; + INFO("Free size available from statfs : <" + std::to_string(szinfo.availsz) + ">"); } else { - std::cout << "can't access " << src << std::endl; + INFO("can't access " + std::string(src)); return PRESCAN_ERR; } mPreScanEncryptErr = 0; result = getDecryptedSize(src, &szinfo); - std::cout << "Prescan Decrypt Free <" << szinfo.availsz << ">, source size <" << szinfo.cursz << ">, Source Decrypted size <" << szinfo.decsz << "> largeFileSz <" - << szinfo.largesz << "> filecount <" << szinfo.filecount << "> for <" << src << ">" << std::endl; - std::cout << "retval <" << result << "> PrescanencryptErr <" << mPreScanEncryptErr << ">" << std::endl; + INFO("Prescan Decrypt Free <" + std::to_string(szinfo.availsz) + ">, source size <" + std::to_string(szinfo.cursz) + ">, Source Decrypted size <" + std::to_string(szinfo.decsz) + "> largeFileSz <" + + std::to_string(szinfo.largesz) + "> filecount <" + std::to_string(szinfo.filecount) + "> for <" + std::string(src) + ">"); + INFO("retval <" + std::to_string(result) + "> PrescanencryptErr <" + std::to_string(mPreScanEncryptErr) + ">"); mTotalFileCt = szinfo.filecount; mTotalStSz = szinfo.totalstsz; @@ -607,24 +618,24 @@ int EcryptfsEngine::preScanForDecrypt(const char *src) if (isEcryptfsMountpointMounted(mPath)) { umount(); - std::cout << "Unmounting mpath <" << mPath << ">" << std::endl; + INFO("Unmounting mpath <" + std::string(mPath) + ">"); } - std::cout << "Prescan failed with Too full error, need more space, " << needed << " extra, total " << total << std::endl; + INFO("Prescan failed with Too full error, need more space, " + std::to_string(needed) + " extra, total " + std::to_string(total)); result = PRESCAN_ERR; } if (result == PRESCAN_TEMP_FILE_EXIST_AND_SIZE_ERR) { - std::cout << "EcryptfsEngine: Need to run Prescan Again" << std::endl; + INFO("EcryptfsEngine: Need to run Prescan Again"); continue; } if (result == 0) { - std::cout << "EcryptfsEngine: Prescan completed successfully" << std::endl; + INFO("EcryptfsEngine: Prescan completed successfully"); return 0; } - std::cout << "EcryptfsEngine: Cannot encrypt, Disk Space is not enough" << std::endl; + INFO("EcryptfsEngine: Cannot encrypt, Disk Space is not enough"); return PRESCAN_ERR; } while (1); } @@ -633,9 +644,9 @@ int EcryptfsEngine::getEncryptedSize(const char *src, EcryptfsEngine::sizeinfo_t { struct stat src_stat; int result = ERR_NO; - std::cout << "getEncryptedSize: " << src << std::endl; + INFO("getEncryptedSize: " + std::string(src)); if (::lstat(src, &src_stat) < 0) - std::cout << "getEncryptedSize: source : <" << src << "> is a dangling link" << std::endl; + INFO("getEncryptedSize: source : <" + std::string(src) + "> is a dangling link"); if (S_ISDIR(src_stat.st_mode)) { /* dir call recursively */ DIR *dp; @@ -647,7 +658,7 @@ int EcryptfsEngine::getEncryptedSize(const char *src, EcryptfsEngine::sizeinfo_t dp = ::opendir(src); if (NULL == dp) { result = -1; - std::cout << "getEncryptedSize: opendir return null <" << src << ">" << std::endl; + INFO("getEncryptedSize: opendir return null <" + std::string(src) + ">"); return result; } @@ -663,7 +674,7 @@ int EcryptfsEngine::getEncryptedSize(const char *src, EcryptfsEngine::sizeinfo_t } if ((result = getEncryptedSize(nsrc, szinfo)) < 0) { - std::cout << "getEncryptedSize() reutnred error for nsrc <" << nsrc << "> result < " << result << ">" << std::endl; + INFO("getEncryptedSize() reutnred error for nsrc <" + std::string(nsrc) + "> result < " + std::to_string(result) + ">"); if (result == PRESCAN_ERR) mPreScanEncryptErr = result; } @@ -676,17 +687,17 @@ int EcryptfsEngine::getEncryptedSize(const char *src, EcryptfsEngine::sizeinfo_t long long new_enc_sz = 0; int enc_fl = fn_was_encrypted(src); - std::cout << "fn_was_encrypted returned: " << enc_fl << std::endl; + INFO("fn_was_encrypted returned: " + std::to_string(enc_fl)); if (padded_sz % 4096) padded_sz = (1 + padded_sz / 4096) * 4096; if (enc_fl != WAS_NOT_ENCRYPTED) { - std::cout << "Skipping " << src << std::endl; + INFO("Skipping " + std::string(src)); new_enc_sz = padded_sz; } else { - std::cout << "PRE ENC " << src << std::endl; - std::cout << "fn_was_encrypted returned: " << enc_fl << std::endl; + INFO("PRE ENC " + std::string(src)); + INFO("fn_was_encrypted returned: " + std::to_string(enc_fl)); new_enc_sz = padded_sz + 2 * 4096; if (szinfo->largesz < src_stat.st_size) @@ -726,9 +737,9 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t { struct stat src_stat; int result = ERR_NO; - std::cout << "getDecryptedSize: " << src << std::endl; + INFO("getDecryptedSize: " + std::string(src)); if (::lstat(src, & src_stat) < 0) - std::cout << "getDecryptedSize: source : <" << src << "> is a dangling link" << std::endl; + INFO("getDecryptedSize: source : <" + std::string(src) + "> is a dangling link"); if (S_ISDIR(src_stat.st_mode)) { /* dir call recursively */ @@ -741,7 +752,7 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t dp = ::opendir(src); if (NULL == dp) { result = -1; - std::cout << "getDecryptedSize: opendir return null <" << src << ">" << std::endl; + INFO("getDecryptedSize: opendir return null <" + std::string(src) + ">"); return result; } @@ -757,7 +768,7 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t } if ((result = getDecryptedSize(nsrc, szinfo)) < 0) { - std::cout << "getDecryptedSize() reutnred error for nsrc <" << nsrc << "> result < " << result << ">" << std::endl; + INFO("getDecryptedSize() reutnred error for nsrc <" + std::string(nsrc) + "> result < " + std::to_string(result) + ">"); if (result == PRESCAN_ERR) { mPreScanDecryptErr = result; } @@ -772,7 +783,7 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t long long paddedstsz = 0; int enc_fl = fn_was_encrypted(src); - std::cout << "fn_was_encrypted returned: " << enc_fl << std::endl; + INFO("fn_was_encrypted returned: " + std::to_string(enc_fl)); if (src_stat.st_size > 2 * 4096) decsize = src_stat.st_size - 2 * 4096; @@ -786,7 +797,7 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t paddedstsz = (1 + paddedstsz / szinfo->blocksz) * szinfo->blocksz; if (enc_fl == WAS_ENCRYPTED) { - std::cout << "PRE ENC " << src << std::endl; + INFO("PRE ENC " + std::string(src)); szinfo->filecount += 1; szinfo->totalstsz += src_stat.st_size; @@ -796,8 +807,8 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t if ((decsize > szinfo->availsz) || (!decsize && (paddedstsz > szinfo->availsz))) { int tmpneeded; - std::cout << "Runtime Error source: " << src << ", decsz <" << szinfo->decsz << "> cursz <" << szinfo->cursz << ">, largesz <" << - szinfo->largesz << "> st_size <" << src_stat.st_size << "> availsz <" << szinfo->availsz << ">" << std::endl; + INFO("Runtime Error source: " + std::string(src) + ", decsz <" + std::to_string(szinfo->decsz) + "> cursz <" + std::to_string(szinfo->cursz) + ">, largesz <" + + std::to_string(szinfo->largesz) + "> st_size <" + std::to_string(src_stat.st_size) + "> availsz <" + std::to_string(szinfo->availsz) + ">"); if (mPreScanDecryptErr == PRESCAN_TEMP_FILE_EXIST_ERR) result = PRESCAN_TEMP_FILE_EXIST_AND_SIZE_ERR; else @@ -828,8 +839,8 @@ int EcryptfsEngine::getDecryptedSize(const char *src, EcryptfsEngine::sizeinfo_t szinfo->cursz += paddedstsz; } } else { - std::cout << "in decrypting case fn_was_encrypted returned: " << enc_fl << std::endl; - std::cout << "Skipping " << src << std::endl; + INFO("in decrypting case fn_was_encrypted returned: " + std::to_string(enc_fl)); + INFO("Skipping " << std::string(src)); szinfo->decsz += paddedstsz; szinfo->cursz += paddedstsz; } @@ -866,7 +877,7 @@ char *EcryptfsEngine::catSubpathFile(const char *src, const char *filename, char } if (!::strncmp(filename, prefix, ::strlen(prefix))) { - std::cout << "Deleting Existing Temp file " << filename << std::endl; + INFO("Deleting Existing Temp file " + std::string(filename)); if (!mPreScanEncryptErr) mPreScanEncryptErr = PRESCAN_TEMP_FILE_EXIST_ERR; else @@ -892,23 +903,23 @@ int EcryptfsEngine::fn_was_encrypted(const char *filePath) __u32 attrs = 0; if (filePath == NULL) { - std::cout << "Ecryptfs: fn_was_encrypted: source file path is null" << std::endl; + INFO("Ecryptfs: fn_was_encrypted: source file path is null"); return WAS_ECRYPTED_ERROR; } fd = ::open(filePath, O_RDWR); if (fd < 0) { - std::cout << "Ecryptfs: fn_was_encrypted: cannot open file " << strerror(errno) << std::endl; + INFO("Ecryptfs: fn_was_encrypted: cannot open file " + std::string(strerror(errno))); return WAS_ECRYPTED_ERROR; } if (::ioctl(fd, ECRYPTFS_IOCTL_GET_ATTRIBUTES, &attrs)) { - std::cout << "Ecryptfs: fn_was_encrypted: ioctl fail " << strerror(errno) << std::endl; + INFO("Ecryptfs: fn_was_encrypted: ioctl fail " + std::string(strerror(errno))); ::close(fd); return WAS_ECRYPTED_ERROR; } - std::cout << "Ecryptfs: IOCT_GET_ATTRIBUTE = " << attrs << std::endl; + INFO("Ecryptfs: IOCT_GET_ATTRIBUTE = " + std::to_string(attrs)); if ((attrs & ECRYPTFS_WAS_ENCRYPTED) == ECRYPTFS_WAS_ENCRYPTED) ret = WAS_ENCRYPTED; else if ((attrs & ECRYPTFS_WAS_ENCRYPTED_OTHER_DEVICE) == ECRYPTFS_WAS_ENCRYPTED_OTHER_DEVICE) @@ -922,15 +933,15 @@ int EcryptfsEngine::fn_was_encrypted(const char *filePath) int EcryptfsEngine::createEncryptMetaData(const char *filename) { int fd = -1; - std::cout << "createEncryptMetaData" << std::endl; + INFO("createEncryptMetaData"); fd = ::open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600); if (fd < 0) { - std::cout << "Cann't open " << filename << std::endl; + INFO("Cann't open " + std::string(filename)); return -1; } if (::fsync(fd) != 0) - std::cout << "Cann't fsync of '" << filename << "'" << std::endl; + INFO("Cann't fsync of '" + std::string(filename) + "'"); ::close(fd); return 0; } @@ -971,7 +982,7 @@ int EcryptfsEngine::isMountpointMounted(const std::string &path) char line[1024]; if (!(fp = ::fopen("/proc/mounts", "r"))) { - std::cout << "Error opening /proc/mounts (" << strerror(errno) << ")" << std::endl; + INFO("Error opening /proc/mounts (" + std::string(strerror(errno)) + ")"); return 0; } @@ -998,7 +1009,7 @@ int EcryptfsEngine::isEcryptfsMountpointMounted(const std::string &path) char line[1024]; if (!(fp = ::fopen("/proc/mounts", "r"))) { - std::cout << "Error opening /proc/mounts (" << strerror(errno) << ")" << std::endl; + INFO("Error opening /proc/mounts (" + std::string(strerror(errno)) + ")"); return -1; } @@ -1007,7 +1018,7 @@ int EcryptfsEngine::isEcryptfsMountpointMounted(const std::string &path) ::sscanf(line, "%255s %255s %255s %255s\n", dev_path, mount_path, device, rest); if (!::strcmp(mount_path, path.c_str()) && !strcmp(device, "ecryptfs")) { ::fclose(fp); - std::cout << "Returning True mount_path<" << mount_path << ">,path <" << path << ">, device <" << device << ">" << std::endl; + INFO("Returning True mount_path<" + std::string(mount_path) + ">,path <" + path + ">, device <" + std::string(device) + ">"); return 0; } } diff --git a/server/engine/ecryptfs-engine.h b/server/engine/ecryptfs-engine.h index bef3ac1..ba6029c 100644 --- a/server/engine/ecryptfs-engine.h +++ b/server/engine/ecryptfs-engine.h @@ -193,11 +193,11 @@ public: int smack_check(void); void build_ecryptfs_options(char *ecryptfs_opts, char *sig, int excludeMediaTypes); - int mount(const data& key); - int umount(); + void mount(const data& key); + void umount(); - int encrypt(const data& key); - int decrypt(const data& key); + void encrypt(const data& key); + void decrypt(const data& key); int DoCrypt(const data& key, const char *path, int reqEnc, int excludeMedia); int DoEncrypt(const data& key);