libext4_utils_src_files += \
key_control.cpp \
- ext4_crypt.cpp \
- unencrypted_properties.cpp
+ ext4_crypt.cpp
ifneq ($(HOST_OS),windows)
LOCAL_CFLAGS += -fno-strict-aliasing
LOCAL_STATIC_LIBRARIES := \
libsparse_static \
- libselinux
+ libselinux \
+ libbase
include $(BUILD_STATIC_LIBRARY)
#include <cutils/klog.h>
-#include "unencrypted_properties.h"
-
#define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
#define EXT4_KEYREF_DELIMITER ((char)'.')
#include <sys/stat.h>
#include <unistd.h>
+#include <android-base/file.h>
+
#include <cutils/klog.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
#include <poll.h>
#include "key_control.h"
-#include "unencrypted_properties.h"
static const std::string arbitrary_sequence_number = "42";
static const int vold_command_timeout_ms = 60 * 1000;
int e4crypt_create_device_key(const char* dir,
int ensure_dir_exists(const char*))
{
- // Already encrypted with password? If so bail
- std::string temp_folder = std::string() + dir + "/tmp_mnt";
- DIR* temp_dir = opendir(temp_folder.c_str());
- if (temp_dir) {
- closedir(temp_dir);
- return 0;
- }
-
// Make sure folder exists. Use make_dir to set selinux permissions.
- if (ensure_dir_exists(UnencryptedProperties::GetPath(dir).c_str())) {
+ std::string unencrypted_dir = std::string(dir) + "/unencrypted";
+ if (ensure_dir_exists(unencrypted_dir.c_str())) {
KLOG_ERROR(TAG, "Failed to create %s (%s)\n",
- UnencryptedProperties::GetPath(dir).c_str(),
+ unencrypted_dir.c_str(),
strerror(errno));
return -1;
}
return 0;
}
}
- UnencryptedProperties props("/data");
- std::string policy = props.Get<std::string>(properties::ref);
- if (policy.empty()) {
- // ext4enc:TODO why is this OK?
+
+ std::string ref_filename = std::string("/data") + e4crypt_key_ref;
+ std::string policy;
+ if (!android::base::ReadFileToString(ref_filename, &policy)) {
+ KLOG_INFO(TAG, "Not file encrypted so no policy for %s\n", dir);
return 0;
}
int e4crypt_do_init_user0();
int do_policy_set(const char *directory, const char *policy, int policy_length);
+static const char* e4crypt_unencrypted_folder = "/unencrypted";
+static const char* e4crypt_key_ref = "/unencrypted/ref";
+
__END_DECLS
+++ /dev/null
-#include "unencrypted_properties.h"
-
-#include <sys/stat.h>
-#include <dirent.h>
-
-namespace properties {
- const char* key = "key";
- const char* ref = "ref";
- const char* props = "props";
- const char* is_default = "is_default";
-}
-
-namespace
-{
- const char* unencrypted_folder = "unencrypted";
-}
-
-std::string UnencryptedProperties::GetPath(const char* device)
-{
- return std::string() + device + "/" + unencrypted_folder;
-}
-
-UnencryptedProperties::UnencryptedProperties(const char* device)
- : folder_(GetPath(device))
-{
- DIR* dir = opendir(folder_.c_str());
- if (dir) {
- closedir(dir);
- } else {
- folder_.clear();
- }
-}
-
-UnencryptedProperties::UnencryptedProperties()
-{
-}
-
-template<> std::string UnencryptedProperties::Get(const char* name,
- std::string default_value) const
-{
- if (!OK()) return default_value;
- std::ifstream i(folder_ + "/" + name, std::ios::binary);
- if (!i) {
- return default_value;
- }
-
- i.seekg(0, std::ios::end);
- int length = i.tellg();
- i.seekg(0, std::ios::beg);
- if (length == -1) {
- return default_value;
- }
-
- std::string s(length, 0);
- i.read(&s[0], length);
- if (!i) {
- return default_value;
- }
-
- return s;
-}
-
-template<> bool UnencryptedProperties::Set(const char* name, std::string const& value)
-{
- if (!OK()) return false;
- std::ofstream o(folder_ + "/" + name, std::ios::binary);
- o << value;
- return !o.fail();
-}
-
-UnencryptedProperties UnencryptedProperties::GetChild(const char* name) const
-{
- UnencryptedProperties up;
- if (!OK()) return up;
-
- std::string directory(folder_ + "/" + name);
- if (mkdir(directory.c_str(), 700) == -1 && errno != EEXIST) {
- return up;
- }
-
- up.folder_ = directory;
- return up;
-}
-
-bool UnencryptedProperties::Remove(const char* name)
-{
- if (!OK()) return false;
- if (remove((folder_ + "/" + name).c_str())
- && errno != ENOENT) {
- return false;
- }
-
- return true;
-}
-
-bool UnencryptedProperties::OK() const
-{
- return !folder_.empty();
-}
+++ /dev/null
-#include <string>
-#include <fstream>
-
-// key names for properties we use
-namespace properties {
- extern const char* key;
- extern const char* ref;
- extern const char* props;
- extern const char* is_default;
-}
-
-/**
- * Class to store data on the unencrypted folder of a device.
- * Note that the folder must exist before this class is constructed.
- * All names must be valid single level (no '/') file or directory names
- * Data is organized hierarchically so we can get a child folder
- */
-class UnencryptedProperties
-{
-public:
- // Get path of folder. Must create before using any properties
- // This is to allow proper setting of SELinux policy
- static std::string GetPath(const char* device);
-
- // Opens properties folder on named device.
- // If folder does not exist, OK will return false, all
- // getters will return default properties and setters will fail.
- UnencryptedProperties(const char* device);
-
- // Get named object. Return default if object does not exist or error.
- template<typename t> t Get(const char* name, t default_value = t()) const;
-
- // Set named object. Return true if success, false otherwise
- template<typename t> bool Set(const char* name, t const& value);
-
- // Get child properties
- UnencryptedProperties GetChild(const char* name) const;
-
- // Remove named object
- bool Remove(const char* name);
-
- // Does folder exist?
- bool OK() const;
-
-private:
- UnencryptedProperties();
- std::string folder_;
-};
-
-
-template<typename t> t UnencryptedProperties::Get(const char* name,
- t default_value) const
-{
- if (!OK()) return default_value;
- t value = default_value;
- std::ifstream(folder_ + "/" + name) >> value;
- return value;
-}
-
-template<typename t> bool UnencryptedProperties::Set(const char* name,
- t const& value)
-{
- if (!OK()) return false;
- std::ofstream o(folder_ + "/" + name);
- o << value;
- return !o.fail();
-}
-
-// Specialized getters/setters for strings
-template<> std::string UnencryptedProperties::Get(const char* name,
- std::string default_value) const;
-
-template<> bool UnencryptedProperties::Set(const char* name,
- std::string const& value);