#include <security_filedb/AtomicFile.h>
-#include <security_utilities/devrandom.h>
#include <CommonCrypto/CommonDigest.h>
#include <security_cdsa_utilities/cssmerrors.h>
#include <Security/cssm.h>
+#include <Security/SecRandom.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
{
}
-// Aquire the write lock and remove the file.
+// Acquire the write lock and remove the file.
void
AtomicFile::performDelete()
{
if (::unlink(mPath.c_str()) != 0)
{
int error = errno;
- secnotice("atomicfile", "unlink %s: %s", mPath.c_str(), strerror(error));
+ secinfo("atomicfile", "unlink %s: %s", mPath.c_str(), strerror(error));
if (error == ENOENT)
CssmError::throwMe(CSSMERR_DL_DATASTORE_DOESNOT_EXIST);
else
::unlink(mLockFilePath.c_str());
}
-// Aquire the write lock and rename the file (and bump the version and stuff).
+// Acquire the write lock and rename the file (and bump the version and stuff).
void
AtomicFile::rename(const std::string &inNewPath)
{
if (::rename(path, newPath) != 0)
{
int error = errno;
- secnotice("atomicfile", "rename(%s, %s): %s", path, newPath, strerror(error));
+ secinfo("atomicfile", "rename(%s, %s): %s", path, newPath, strerror(error));
UnixError::throwMe(error);
}
}
if (fileRef == -1)
{
int error = errno;
- secnotice("atomicfile", "open %s: %s", path, strerror(error));
+ secinfo("atomicfile", "open %s: %s", path, strerror(error));
// Do the obvious error code translations here.
// @@@ Consider moving these up a level.
int result = sandbox_check(getpid(), "file-read-data", (sandbox_filter_type) (SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT), name);
if (result != 0)
{
+ secdebug("atomicfile", "sandboxing rejected read access to %s", name);
return -1;
}
}
int result = sandbox_check(getpid(), "file-write-data", (sandbox_filter_type) (SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT), name);
if (result != 0)
{
+ secdebug("atomicfile", "sandboxing rejected write access to %s", name);
return -1;
}
}
// In release mode, the assert() is compiled out so rv may be unused.
__unused int rv = AtomicFile::rclose(mFileRef);
assert(rv == 0);
- secnotice("atomicfile", "%p closed %s", this, mPath.c_str());
+ secinfo("atomicfile", "%p closed %s", this, mPath.c_str());
}
if (mBuffer)
const char *path = mPath.c_str();
if (mFileRef >= 0)
{
- secnotice("atomicfile", "open %s: already open, closing and reopening", path);
+ secinfo("atomicfile", "open %s: already open, closing and reopening", path);
close();
}
UnixError::throwMe(error);
}
- secnotice("atomicfile", "%p opened %s: %qd bytes", this, path, mLength);
+ secinfo("atomicfile", "%p opened %s: %qd bytes", this, path, mLength);
return mLength;
}
{
if(mBuffer) {
delete [] mBuffer;
+ mBuffer = NULL;
}
}
AtomicBufferedFile::loadBuffer()
{
// make a buffer big enough to hold the entire file
- mBuffer = new uint8[mLength];
+ mBuffer = new uint8[(size_t) mLength];
if(lseek(mFileRef, 0, SEEK_SET) < 0) {
int error = errno;
secinfo("atomicfile", "lseek(%s, BEGINNING): %s", mPath.c_str(), strerror(error));
{
int error = errno;
secinfo("atomicfile", "read(%s, %zd): %s", mPath.c_str(), bytesToRead, strerror(error));
- if (mFileRef >= 0) {
- AtomicFile::rclose(mFileRef);
- mFileRef = -1;
- }
+ AtomicFile::rclose(mFileRef);
+ mFileRef = -1;
UnixError::throwMe(error);
}
}
{
if (mFileRef < 0)
{
- secnotice("atomicfile", "close %s: already closed", mPath.c_str());
+ secinfo("atomicfile", "close %s: already closed", mPath.c_str());
}
else
{
UnixError::throwMe(error);
}
- secnotice("atomicfile", "%p closed %s", this, mPath.c_str());
+ secinfo("atomicfile", "%p closed %s", this, mPath.c_str());
}
}
}
}
- secnotice("atomicfile", "%p created %s", this, path);
+ secinfo("atomicfile", "%p created %s", this, path);
}
void
UnixError::throwMe(error);
}
- secnotice("atomicfile", "%p closed %s", this, mPath.c_str());
+ secinfo("atomicfile", "%p closed %s", this, mPath.c_str());
}
}
NetworkFileLocker::unique(mode_t mode)
{
static const int randomPart = 16;
- DevRandomGenerator randomGen;
std::string::size_type dirSize = mDir.size();
std::string fullname(dirSize + randomPart + 2, '\0');
fullname.replace(0, dirSize, mDir);
for (int retries = 0; retries < 10; ++retries)
{
/* Make a random filename. */
- randomGen.random(buf, randomPart);
+ MacOSError::check(SecRandomCopyBytes(kSecRandomDefault, randomPart, buf));
for (int ix = 0; ix < randomPart; ++ix)
{
char ch = buf[ix] & 0x3f;