From d8720c401d591d5055528b5f2729f2f4308d8a7c Mon Sep 17 00:00:00 2001 From: Jaemin Ryu Date: Tue, 7 Jun 2016 20:09:59 +0900 Subject: [PATCH] Add wifi restriction Change-Id: Icc31dc7c448d046d315e81c8dd8179cc4c5b4798 Signed-off-by: Jaemin Ryu --- libs/dpm/restriction.cpp | 17 ++++---- libs/restriction.cpp | 36 ---------------- libs/wifi.cpp | 36 ++++++++++++++++ policy/restriction.hxx | 6 --- policy/wifi.hxx | 6 +++ server/restriction.cpp | 29 ------------- server/wifi.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++---- 7 files changed, 147 insertions(+), 87 deletions(-) diff --git a/libs/dpm/restriction.cpp b/libs/dpm/restriction.cpp index ae3390a..200f0a8 100644 --- a/libs/dpm/restriction.cpp +++ b/libs/dpm/restriction.cpp @@ -18,6 +18,7 @@ #include "restriction.hxx" #include "location.hxx" #include "bluetooth.hxx" +#include "wifi.hxx" #include "debug.h" #include "policy-client.h" @@ -234,8 +235,8 @@ int dpm_restriction_set_wifi_state(dpm_restriction_policy_h handle, int enable) RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER); DevicePolicyContext &client = GetDevicePolicyContext(handle); - RestrictionPolicy restriction = client.createPolicyInterface(); - return restriction.setWifiState(enable); + WifiPolicy wifi = client.createPolicyInterface(); + return wifi.setState(enable); } int dpm_restriction_get_wifi_state(dpm_restriction_policy_h handle, int *state) @@ -244,8 +245,8 @@ int dpm_restriction_get_wifi_state(dpm_restriction_policy_h handle, int *state) RET_ON_FAILURE(state, DPM_ERROR_INVALID_PARAMETER); DevicePolicyContext &client = GetDevicePolicyContext(handle); - RestrictionPolicy restriction = client.createPolicyInterface(); - int ret = restriction.getWifiState(); + WifiPolicy wifi = client.createPolicyInterface(); + int ret = wifi.getState(); if (ret < 0) { return -1; } @@ -258,8 +259,8 @@ int dpm_restriction_set_wifi_hotspot_state(dpm_restriction_policy_h handle, int RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER); DevicePolicyContext &client = GetDevicePolicyContext(handle); - RestrictionPolicy restriction = client.createPolicyInterface(); - return restriction.setWifiHotspotState(enable); + WifiPolicy wifi = client.createPolicyInterface(); + return wifi.setHotspotState(enable); } int dpm_restriction_get_wifi_hotspot_state(dpm_restriction_policy_h handle, int *state) @@ -268,8 +269,8 @@ int dpm_restriction_get_wifi_hotspot_state(dpm_restriction_policy_h handle, int RET_ON_FAILURE(state, DPM_ERROR_INVALID_PARAMETER); DevicePolicyContext &client = GetDevicePolicyContext(handle); - RestrictionPolicy restriction = client.createPolicyInterface(); - int ret = restriction.getWifiHotspotState(); + WifiPolicy wifi = client.createPolicyInterface(); + int ret = wifi.getHotspotState(); if (ret < 0) { return -1; } diff --git a/libs/restriction.cpp b/libs/restriction.cpp index b0217ce..12b01ca 100644 --- a/libs/restriction.cpp +++ b/libs/restriction.cpp @@ -154,40 +154,4 @@ int RestrictionPolicy::getExternalStorageState() } } -int RestrictionPolicy::setWifiState(bool enable) -{ - try { - return context->methodCall("RestrictionPolicy::setWifiState", enable); - } catch (runtime::Exception& e) { - return -1; - } -} - -bool RestrictionPolicy::getWifiState() -{ - try { - return context->methodCall("RestrictionPolicy::getWifiState"); - } catch (runtime::Exception &e) { - return -1; - } -} - -int RestrictionPolicy::setWifiHotspotState(bool enable) -{ - try { - return context->methodCall("RestrictionPolicy::setWifiHotspotState", enable); - } catch (runtime::Exception& e) { - return -1; - } -} - -bool RestrictionPolicy::getWifiHotspotState() -{ - try { - return context->methodCall("RestrictionPolicy::getWifiHotspotState"); - } catch (runtime::Exception &e) { - return -1; - } -} - } //namespace DevicePolicyManager diff --git a/libs/wifi.cpp b/libs/wifi.cpp index 84111f8..7a274e9 100644 --- a/libs/wifi.cpp +++ b/libs/wifi.cpp @@ -28,6 +28,42 @@ WifiPolicy::~WifiPolicy() { } +int WifiPolicy::setState(bool enable) +{ + try { + return context->methodCall("WifiPolicy::setState", enable); + } catch (runtime::Exception& e) { + return -1; + } +} + +bool WifiPolicy::getState() +{ + try { + return context->methodCall("WifiPolicy::getState"); + } catch (runtime::Exception &e) { + return -1; + } +} + +int WifiPolicy::setHotspotState(bool enable) +{ + try { + return context->methodCall("WifiPolicy::setHotspotState", enable); + } catch (runtime::Exception& e) { + return -1; + } +} + +bool WifiPolicy::getHotspotState() +{ + try { + return context->methodCall("WifiPolicy::getHotspotState"); + } catch (runtime::Exception &e) { + return -1; + } +} + int WifiPolicy::setProfileChangeRestriction(bool enable) { try { diff --git a/policy/restriction.hxx b/policy/restriction.hxx index 81c0e39..bce7ecb 100644 --- a/policy/restriction.hxx +++ b/policy/restriction.hxx @@ -50,12 +50,6 @@ public: int setExternalStorageState(int enable); int getExternalStorageState(); - int setWifiState(bool enable); - bool getWifiState(); - - int setWifiHotspotState(bool enable); - bool getWifiHotspotState(); - private: PolicyControlContext& context; }; diff --git a/policy/wifi.hxx b/policy/wifi.hxx index 5762c2e..2d3fff9 100644 --- a/policy/wifi.hxx +++ b/policy/wifi.hxx @@ -31,6 +31,12 @@ public: WifiPolicy(PolicyControlContext& ctxt); ~WifiPolicy(void); + int setState(bool enable); + bool getState(); + + int setHotspotState(bool enable); + bool getHotspotState(); + int setProfileChangeRestriction(bool restrict); bool isProfileChangeRestricted(void); diff --git a/server/restriction.cpp b/server/restriction.cpp index 2e49548..427b23d 100644 --- a/server/restriction.cpp +++ b/server/restriction.cpp @@ -51,10 +51,6 @@ RestrictionPolicy::RestrictionPolicy(PolicyControlContext& ctxt) : context.registerNonparametricMethod(this, (int)(RestrictionPolicy::getUsbTetheringState)); context.registerParametricMethod(this, (int)(RestrictionPolicy::setExternalStorageState)(int)); context.registerNonparametricMethod(this, (int)(RestrictionPolicy::getExternalStorageState)); - context.registerParametricMethod(this, (int)(RestrictionPolicy::setWifiState)(bool)); - context.registerNonparametricMethod(this, (bool)(RestrictionPolicy::getWifiState)); - context.registerParametricMethod(this, (int)(RestrictionPolicy::setWifiHotspotState)(bool)); - context.registerNonparametricMethod(this, (bool)(RestrictionPolicy::getWifiHotspotState)); context.createNotification("camera"); context.createNotification("clipboard"); @@ -63,8 +59,6 @@ RestrictionPolicy::RestrictionPolicy(PolicyControlContext& ctxt) : context.createNotification("settings-changes"); context.createNotification("usb-debugging"); context.createNotification("usb-tethering"); - context.createNotification("wifi"); - context.createNotification("wifi-hotspot"); } RestrictionPolicy::~RestrictionPolicy() @@ -168,29 +162,6 @@ int RestrictionPolicy::getExternalStorageState() return IsPolicyAllowed(context, "external-storage"); } -int RestrictionPolicy::setWifiState(bool enable) -{ - SetPolicyAllowed(context, "wifi", enable); - return 0; -} - -bool RestrictionPolicy::getWifiState() -{ - return IsPolicyAllowed(context, "wifi"); - return 0; -} - -int RestrictionPolicy::setWifiHotspotState(bool enable) -{ - SetPolicyAllowed(context, "wifi-hotspot", enable); - return 0; -} - -bool RestrictionPolicy::getWifiHotspotState() -{ - return IsPolicyAllowed(context, "wifi-hotspot"); -} - RestrictionPolicy restrictionPolicy(Server::instance()); } // namespace DevicePolicyManager diff --git a/server/wifi.cpp b/server/wifi.cpp index 8ba08b6..926641c 100644 --- a/server/wifi.cpp +++ b/server/wifi.cpp @@ -31,11 +31,26 @@ #include "app-bundle.h" #include "syspopup.h" #include "audit/logger.h" +#include "dbus/connection.h" + +#define NETCONFIG_INTERFACE \ + "net.netconfig", \ + "/net/netconfig/network", \ + "net.netconfig.network" + +#define MOBILEAP_INTERFACE \ + "org.tizen.MobileapAgent", \ + "/MobileapAgent", \ + "org.tizen.tethering" namespace DevicePolicyManager { -static std::unordered_set blockSsidList; -static inline void applyBlocklist(wifi_ap_h ap) +namespace { + +std::atomic blockEnabled; +std::unordered_set blockSsidList; + +inline void applyBlocklist(wifi_ap_h ap) { char *ssid; ::wifi_ap_get_essid(ap, &ssid); @@ -45,8 +60,8 @@ static inline void applyBlocklist(wifi_ap_h ap) ::free(ssid); } -static std::atomic blockEnabled; -static void connectionStateChanged(wifi_connection_state_e state, + +void connectionStateChanged(wifi_connection_state_e state, wifi_ap_h ap, void *user_data) { WifiPolicy* policy = reinterpret_cast(user_data); @@ -56,13 +71,15 @@ static void connectionStateChanged(wifi_connection_state_e state, return; } - if (!policy->isNetworkAccessRestricted()) + if (!policy->isNetworkAccessRestricted()) { return; + } applyBlocklist(ap); } -static inline void applyBlocklistToConnectedAP() { +inline void applyBlocklistToConnectedAP() +{ wifi_ap_h ap; ::wifi_initialize(); @@ -72,9 +89,17 @@ static inline void applyBlocklistToConnectedAP() { } } +} // namespace + WifiPolicy::WifiPolicy(PolicyControlContext& ctx) : context(ctx) { + context.registerParametricMethod(this, (int)(WifiPolicy::setState)(bool)); + context.registerNonparametricMethod(this, (bool)(WifiPolicy::getState)); + + context.registerParametricMethod(this, (int)(WifiPolicy::setHotspotState)(bool)); + context.registerNonparametricMethod(this, (bool)(WifiPolicy::getHotspotState)); + context.registerParametricMethod(this, (int)(WifiPolicy::setProfileChangeRestriction)(bool)); context.registerNonparametricMethod(this, (bool)(WifiPolicy::isProfileChangeRestricted)); @@ -83,6 +108,8 @@ WifiPolicy::WifiPolicy(PolicyControlContext& ctx) : context.registerParametricMethod(this, (int)(WifiPolicy::addSsidToBlocklist)(std::string)); context.registerParametricMethod(this, (int)(WifiPolicy::removeSsidFromBlocklist)(std::string)); + context.createNotification("wifi"); + context.createNotification("wifi-hotspot"); context.createNotification("wifi-profile-change"); context.createNotification("wifi-ssid-restriction"); @@ -95,8 +122,69 @@ WifiPolicy::~WifiPolicy() ::wifi_unset_connection_state_changed_cb(); } +int WifiPolicy::setState(bool enable) +{ + try { + dbus::Connection &systemDBus = dbus::Connection::getSystem(); + systemDBus.methodcall(NETCONFIG_INTERFACE, + "DevicePolicySetWifi", + -1, + "", + "(i)", + enable); + } catch (runtime::Exception& e) { + ERROR("Failed to chaneg Wi-Fi state"); + return -1; + } + + SetPolicyAllowed(context, "wifi", enable); + return 0; +} + +bool WifiPolicy::getState() +{ + return IsPolicyAllowed(context, "wifi"); +} + +int WifiPolicy::setHotspotState(bool enable) +{ + try { + dbus::Connection &systemDBus = dbus::Connection::getSystem(); + systemDBus.methodcall(MOBILEAP_INTERFACE, + "change_policy", + -1, + "", + "(sb)", + "wifi-hotspot", + enable); + } catch (runtime::Exception& e) { + ERROR("Failed to change Wi-Fi hotspot state"); + return -1; + } + + SetPolicyAllowed(context, "wifi-hotspot", enable); + return 0; +} + +bool WifiPolicy::getHotspotState() +{ + return IsPolicyAllowed(context, "wifi-hotspot"); +} + int WifiPolicy::setProfileChangeRestriction(bool enable) { + try { + dbus::Connection &systemDBus = dbus::Connection::getSystem(); + systemDBus.methodcall(NETCONFIG_INTERFACE, + "DevicePolicySetWifiProfile", + -1, + "", + "(i)", + enable); + } catch (runtime::Exception& e) { + ERROR("Failed to set Wi-Fi profile change restriction"); + } + SetPolicyAllowed(context, "wifi-profile-change", enable); return 0; } @@ -141,14 +229,14 @@ int WifiPolicy::removeSsidFromBlocklist(const std::string& ssid) { std::unordered_set::iterator it = blockSsidList.find(ssid); - if (it == blockSsidList.end()) + if (it == blockSsidList.end()) { return -1; + } blockSsidList.erase(it); return 0; } - WifiPolicy wifiPolicy(Server::instance()); } // namespace DevicePolicyManager -- 2.7.4