+//
+// Open Service Platform
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+/**
+ * @file DeviceManager.cpp
+ * @brief This is the implementation file for %DeviceManager class.
+ */
+
+#include <dlfcn.h>
+#include <unique_ptr.h>
+
+#include <FIoRegistry.h>
+#include <FBase_StringConverter.h>
+
+#include "DeviceManager.h"
+#include "InstallerDefs.h"
+
+using namespace Tizen::Base;
+using namespace Tizen::Base::Collection;
+using namespace Tizen::Io;
+
+DeviceManager::DeviceManager(void)
+:__pContext(null)
+,__isMdmEnable(false)
+{
+ if (IsMdmEnable() == true)
+ {
+ __isMdmEnable = true;
+ }
+}
+
+DeviceManager::~DeviceManager(void)
+{
+ if (__isMdmEnable == true)
+ {
+ ReleaseService();
+ }
+}
+
+bool
+DeviceManager::Construct(InstallationContext* pContext)
+{
+ __pContext = pContext;
+
+ return true;
+}
+
+bool
+DeviceManager::IsInstallationDisabled() const
+{
+ if (__isMdmEnable == false)
+ {
+ return false;
+ }
+
+ TryReturn(__pContext, false, "__pContext is null.");
+ TryReturn(__pContext->__packageId.IsEmpty() == false, false, "packageId is empty.");
+
+ std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
+ TryReturn(pPackageId, false, "pPackageId is null.");
+
+ int res = IsInstallationDisabled(pPackageId.get());
+ if (res == 1)
+ {
+ AppLog("[%s] can't be installed.", pPackageId.get());
+ return true;
+ }
+ else
+ {
+ AppLog("[%s] can be installed.", pPackageId.get());
+ return false;
+ }
+}
+
+bool
+DeviceManager::IsUninstallationDisabled() const
+{
+ if (__isMdmEnable == false)
+ {
+ return false;
+ }
+
+ TryReturn(__pContext, false, "__pContext is null.");
+ TryReturn(__pContext->__packageId.IsEmpty() == false, false, "packageId is empty.");
+
+ std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(__pContext->__packageId));
+ TryReturn(pPackageId, false, "pPackageId is null.");
+
+ int res = IsUninstallationDisabled(pPackageId.get());
+ if (res == 1)
+ {
+ AppLog("[%s] can't be uninstalled.", pPackageId.get());
+ return true;
+ }
+ else
+ {
+ AppLog("[%s] can be uninstalled.", pPackageId.get());
+ return false;
+ }
+}
+
+bool
+DeviceManager::IsPrivilegeBlacklist() const
+{
+ if (__isMdmEnable == false)
+ {
+ return true;
+ }
+
+ TryReturn(__pContext, false, "__pContext is null.");
+ TryReturn(__pContext->__pPrivilegeList, false, "pPrivilegeList is null.");
+
+ return IsPrivilegeBlacklist(__pContext->__pPrivilegeList);
+}
+
+bool
+DeviceManager::IsSignatureBlacklist() const
+{
+ if (__isMdmEnable == false)
+ {
+ return true;
+ }
+
+ TryReturn(__pContext, false, "__pContext is null.");
+ TryReturn(__pContext->__pAuthorCertList, false, "pAuthorCertList is null.");
+
+ return IsSignatureBlacklist(__pContext->__pAuthorCertList);
+}
+
+bool
+DeviceManager::IsMdmEnable() const
+{
+ result r;
+ Registry reg;
+ String section(L"feature");
+ String entry(L"mdm");
+ String value;
+
+ r = reg.Construct(CONFIG_PATH, "r");
+ TryReturn(!IsFailed(r), false, "CONFIG file is not found.");
+
+ r = reg.GetValue(section, entry, value);
+ TryReturn(!IsFailed(r), false, "GetValue is failed. entry = [%ls]", entry.GetPointer());
+
+ AppLog("[%ls is %ls.]", entry.GetPointer(), value.GetPointer());
+
+ if (value == L"on")
+ {
+ if (GetService() == 0)
+ {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+int
+DeviceManager::GetService() const
+{
+ int ret = 0;
+ void* pHandle = null;
+ char* pErrorMsg = null;
+ int (*mdm_get_service)(void) = null;
+
+ pHandle = dlopen("/usr/lib/libmdm.so.1", RTLD_LAZY | RTLD_GLOBAL);
+ if (!pHandle)
+ {
+ AppLog("dlopen() failed. [%s]", dlerror());
+ return -1;
+ }
+
+ mdm_get_service = reinterpret_cast <int (*)(void)>(dlsym(pHandle, "mdm_get_service"));
+ pErrorMsg = dlerror();
+ if ((pErrorMsg != null) || (mdm_get_service == null))
+ {
+ AppLog("dlsym() failed. [%s]", pErrorMsg);
+ dlclose(pHandle);
+ return -1;
+ }
+
+ AppLog("[mdm] mdm_get_service()");
+ ret = mdm_get_service();
+ AppLog("[mdm] mdm_get_service, result = [%d]", ret);
+
+ dlclose(pHandle);
+
+ return ret;
+}
+
+int
+DeviceManager::ReleaseService() const
+{
+ int ret = 0;
+ void* pHandle = null;
+ char* pErrorMsg = null;
+ int (*mdm_release_service)(void) = null;
+
+ pHandle = dlopen("/usr/lib/libmdm.so.1", RTLD_LAZY | RTLD_GLOBAL);
+ if (!pHandle)
+ {
+ AppLog("dlopen() failed. [%s]", dlerror());
+ return -1;
+ }
+
+ mdm_release_service = reinterpret_cast <int (*)(void)>(dlsym(pHandle, "mdm_release_service"));
+ pErrorMsg = dlerror();
+ if ((pErrorMsg != null) || (mdm_release_service == null))
+ {
+ AppLog("dlsym() failed. [%s]", pErrorMsg);
+ dlclose(pHandle);
+ return -1;
+ }
+
+ AppLog("[mdm] mdm_release_service()");
+ ret = mdm_release_service();
+ AppLog("[mdm] mdm_release_service, result = [%d]", ret);
+
+ dlclose(pHandle);
+
+ return ret;
+}
+
+int
+DeviceManager::IsInstallationDisabled(const char* pPackageId) const
+{
+ int ret = 0;
+ void* pHandle = null;
+ char* pErrorMsg = null;
+ int (*mdm_get_application_installation_disabled)(const char*) = null;
+
+ pHandle = dlopen("/usr/lib/libmdm.so.1", RTLD_LAZY | RTLD_GLOBAL);
+ if (!pHandle)
+ {
+ AppLog("dlopen() failed. [%s]", dlerror());
+ return -1;
+ }
+
+ mdm_get_application_installation_disabled = reinterpret_cast <int (*)(const char*)>(dlsym(pHandle, "mdm_get_application_installation_disabled"));
+ pErrorMsg = dlerror();
+ if ((pErrorMsg != null) || (mdm_get_application_installation_disabled == null))
+ {
+ AppLog("dlsym() failed. [%s]", pErrorMsg);
+ dlclose(pHandle);
+ return -1;
+ }
+
+ AppLog("[mdm] mdm_get_application_installation_disabled(%s)", pPackageId);
+ ret = mdm_get_application_installation_disabled(pPackageId);
+ AppLog("[mdm] mdm_get_application_installation_disabled(%s), result = [%d]", pPackageId, ret);
+
+ dlclose(pHandle);
+
+ return ret;
+}
+
+int
+DeviceManager::IsUninstallationDisabled(const char* pPackageId) const
+{
+ int ret = 0;
+ void* pHandle = null;
+ char* pErrorMsg = null;
+ int (*mdm_get_application_uninstallation_disabled)(const char*) = null;
+
+ pHandle = dlopen("/usr/lib/libmdm.so.1", RTLD_LAZY | RTLD_GLOBAL);
+ if (!pHandle)
+ {
+ AppLog("dlopen() failed. [%s]", dlerror());
+ return -1;
+ }
+
+ mdm_get_application_uninstallation_disabled = reinterpret_cast <int (*)(const char*)>(dlsym(pHandle, "mdm_get_application_uninstallation_disabled"));
+ pErrorMsg = dlerror();
+ if ((pErrorMsg != null) || (mdm_get_application_uninstallation_disabled == null))
+ {
+ AppLog("dlsym() failed. [%s]", pErrorMsg);
+ dlclose(pHandle);
+ return -1;
+ }
+
+ AppLog("[mdm] mdm_get_application_uninstallation_disabled(%s)", pPackageId);
+ ret = mdm_get_application_uninstallation_disabled(pPackageId);
+ AppLog("[mdm] mdm_get_application_uninstallation_disabled(%s), result = [%d]", pPackageId, ret);
+
+ dlclose(pHandle);
+
+ return ret;
+}
+
+bool
+DeviceManager::IsPrivilegeBlacklist(ArrayList* pPrivilegeList) const
+{
+ //mdm_data_t *mdm_get_app_privilege_blacklist(void);
+ return true;
+}
+
+bool
+DeviceManager::IsSignatureBlacklist(IListT<String *>* pSignatureList) const
+{
+ //mdm_data_t *mdm_get_app_signature_blacklist(void);
+ return true;
+}