2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file access.cpp
|
|
|
|
/// @brief Windows access methods.
|
|
|
|
/// @ingroup libaegisub windows
|
|
|
|
|
|
|
|
|
2012-01-12 23:31:54 +01:00
|
|
|
#include <libaegisub/access.h>
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2011-07-26 21:51:38 +02:00
|
|
|
#include <libaegisub/log.h>
|
2010-06-03 22:32:25 +02:00
|
|
|
#include <libaegisub/util.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2012-01-12 23:31:54 +01:00
|
|
|
namespace {
|
|
|
|
bool check_permission(bool is_read, SECURITY_DESCRIPTOR *sd, HANDLE client_token) {
|
|
|
|
DWORD access_check = is_read ? FILE_READ_DATA : FILE_APPEND_DATA | FILE_WRITE_DATA;
|
|
|
|
|
|
|
|
GENERIC_MAPPING generic_mapping;
|
|
|
|
MapGenericMask(&access_check, &generic_mapping);
|
|
|
|
|
|
|
|
PRIVILEGE_SET priv_set;
|
|
|
|
DWORD priv_set_size = sizeof(PRIVILEGE_SET);
|
|
|
|
DWORD access;
|
|
|
|
BOOL access_ok;
|
|
|
|
if(!AccessCheck(sd, client_token, access_check, &generic_mapping, &priv_set, &priv_set_size, &access, &access_ok))
|
|
|
|
LOG_W("acs/check") << "AccessCheck failed: " << agi::util::ErrorString(GetLastError());
|
|
|
|
return !!access;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
|
|
|
namespace acs {
|
|
|
|
|
2010-05-23 22:39:14 +02:00
|
|
|
/*
|
|
|
|
This function is still a proof of concept, it's probably rife with bugs, below
|
|
|
|
is a short (and incomplete) todo
|
|
|
|
* "Basic" checks (Read/Write/File/Dir) checks for FAT32 filesystems which
|
|
|
|
requires detecting the filesystem being used.
|
|
|
|
*/
|
2013-01-04 16:01:50 +01:00
|
|
|
void Check(fs::path const& file, acs::Type type) {
|
|
|
|
DWORD file_attr = GetFileAttributes(file.c_str());
|
2010-05-23 22:39:14 +02:00
|
|
|
if ((file_attr & INVALID_FILE_ATTRIBUTES) == INVALID_FILE_ATTRIBUTES) {
|
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::FileNotFound(file);
|
2010-05-23 22:39:14 +02:00
|
|
|
case ERROR_ACCESS_DENIED:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::ReadDenied(file);
|
2010-05-23 22:39:14 +02:00
|
|
|
default:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::FileSystemUnknownError(str(boost::format("Unexpected error when getting attributes for \"%s\": %s") % file % util::ErrorString(GetLastError())));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case FileRead:
|
2011-07-26 21:51:38 +02:00
|
|
|
case FileWrite:
|
2010-05-23 22:39:14 +02:00
|
|
|
if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::NotAFile(file);
|
2010-05-23 22:39:14 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
case DirRead:
|
2011-07-26 21:51:38 +02:00
|
|
|
case DirWrite:
|
2010-05-23 22:39:14 +02:00
|
|
|
if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::NotADirectory(file);
|
2010-05-23 22:39:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-26 21:51:38 +02:00
|
|
|
SECURITY_INFORMATION info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
|
|
|
|
DWORD len = 0;
|
2013-01-04 16:01:50 +01:00
|
|
|
GetFileSecurity(file.c_str(), info, nullptr, 0, &len);
|
2011-07-26 21:51:38 +02:00
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
LOG_W("acs/check") << "GetFileSecurity: fatal: " << util::ErrorString(GetLastError());
|
2010-05-23 22:39:14 +02:00
|
|
|
|
2011-07-26 21:51:38 +02:00
|
|
|
std::vector<uint8_t> sd_buff(len);
|
|
|
|
SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)&sd_buff[0];
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!GetFileSecurity(file.c_str(), info, sd, len, &len))
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_W("acs/check") << "GetFileSecurity failed: " << util::ErrorString(GetLastError());
|
2010-05-23 22:39:14 +02:00
|
|
|
|
|
|
|
ImpersonateSelf(SecurityImpersonation);
|
2011-07-26 21:51:38 +02:00
|
|
|
HANDLE client_token;
|
|
|
|
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &client_token))
|
|
|
|
LOG_W("acs/check") << "OpenThreadToken failed: " << util::ErrorString(GetLastError());
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2012-01-12 23:31:54 +01:00
|
|
|
if (!check_permission(true, sd, client_token))
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::ReadDenied(file);
|
2012-01-12 23:31:54 +01:00
|
|
|
if ((type == DirWrite || type == FileWrite) && !check_permission(false, sd, client_token))
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::WriteDenied(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2010-05-23 22:39:14 +02:00
|
|
|
|
|
|
|
} // namespace Access
|
|
|
|
} // namespace agi
|