Fix to control platform after that effective policy is actually changed 19/89319/3
authorSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 23 Sep 2016 05:43:18 +0000 (14:43 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Mon, 26 Sep 2016 06:35:12 +0000 (15:35 +0900)
Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
Change-Id: I4afe8afbcd09929099fa8980f9703a35a59770cc

server/bluetooth.cpp
server/location.cpp
server/password.cpp
server/policy-builder.h
server/restriction.cpp
server/server.cpp
server/server.h
server/wifi.cpp

index dcd85d3..a691ed7 100644 (file)
@@ -164,11 +164,15 @@ BluetoothPolicy::~BluetoothPolicy()
 
 int BluetoothPolicy::setModeChangeState(const bool enable)
 {
+       if (!SetPolicyAllowed(context, "bluetooth", enable)) {
+               return 0;
+       }
+
        int ret = __setModeChangeState(enable);
        if (POLICY_ENFORCING_FAILED(ret))
                return -1;
 
-       return SetPolicyAllowed(context, "bluetooth", enable);
+       return 0;
 }
 
 bool BluetoothPolicy::getModeChangeState()
@@ -178,11 +182,16 @@ bool BluetoothPolicy::getModeChangeState()
 
 int BluetoothPolicy::setDesktopConnectivityState(const bool enable)
 {
+       if (!SetPolicyAllowed(context, "bluetooth-desktop-connectivity", enable)) {
+               return 0;
+       }
+
        int ret = __setDesktopConnectivityState(enable);
        if (POLICY_ENFORCING_FAILED(ret))
                return -1;
 
-       return SetPolicyAllowed(context, "bluetooth-desktop-connectivity", enable);
+       return 0;
+
 }
 
 bool BluetoothPolicy::getDesktopConnectivityState()
@@ -192,11 +201,15 @@ bool BluetoothPolicy::getDesktopConnectivityState()
 
 int BluetoothPolicy::setPairingState(const bool enable)
 {
+       if (!SetPolicyAllowed(context, "bluetooth-pairing", enable)) {
+               return 0;
+       }
+
        int ret = __setPairingState(enable);
        if (POLICY_ENFORCING_FAILED(ret))
                return -1;
 
-       return SetPolicyAllowed(context, "bluetooth-pairing", enable);
+       return 0;
 }
 
 bool BluetoothPolicy::getPairingState()
@@ -218,6 +231,10 @@ int BluetoothPolicy::addDeviceToBlacklist(const std::string& mac)
 int BluetoothPolicy::setTetheringState(bool enable)
 {
        try {
+               if (!SetPolicyAllowed(context, "bluetooth-tethering", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(MOBILEAP_INTERFACE,
                                                          "change_policy",
@@ -231,7 +248,8 @@ int BluetoothPolicy::setTetheringState(bool enable)
                return -1;
        }
 
-       return SetPolicyAllowed(context, "bluetooth-tethering", enable);
+       return 0;
+
 }
 
 bool BluetoothPolicy::getTetheringState()
@@ -251,11 +269,15 @@ int BluetoothPolicy::removeDeviceFromBlacklist(const std::string& mac)
 
 int BluetoothPolicy::setDeviceRestriction(const bool enable)
 {
+       if (SetPolicyEnabled(context, "bluetooth-device-restriction", enable)) {
+               return 0;
+       }
+
        int ret = __setDeviceRestriction(enable);
        if (POLICY_ENFORCING_FAILED(ret))
                return -1;
 
-       return SetPolicyEnabled(context, "bluetooth-device-restriction", enable);
+       return 0;
 }
 
 bool BluetoothPolicy::isDeviceRestricted()
@@ -285,11 +307,15 @@ int BluetoothPolicy::removeUuidFromBlacklist(const std::string& uuid)
 
 int BluetoothPolicy::setUuidRestriction(const bool enable)
 {
+       if (SetPolicyEnabled(context, "bluetooth-uuid-restriction", enable)) {
+               return 0;
+       }
+
        int ret = __setUuidRestriction(enable);
        if (POLICY_ENFORCING_FAILED(ret))
                return -1;
 
-       return SetPolicyEnabled(context, "bluetooth-uuid-restriction", enable);
+       return 0;
 }
 
 bool BluetoothPolicy::isUuidRestricted()
index c50aef2..ae16bd3 100644 (file)
@@ -38,10 +38,15 @@ LocationPolicy::~LocationPolicy()
 
 int LocationPolicy::setLocationState(int enable)
 {
-       if (location_manager_enable_restriction(!enable) != LOCATIONS_ERROR_NONE)
+       if (!SetPolicyAllowed(context, "location", enable)) {
+               return 0;
+       }
+
+       if (location_manager_enable_restriction(!enable) != LOCATIONS_ERROR_NONE) {
                return -1;
+       }
 
-       return SetPolicyAllowed(context, "location", enable);
+       return 0;
 }
 
 int LocationPolicy::getLocationState()
index 9d4a053..ab5d070 100644 (file)
@@ -45,7 +45,7 @@ inline int getPasswordPolicy(PolicyControlContext &ctx, const std::string &name)
        return ctx.getPolicy(name, ctx.getPeerUid());
 }
 
-inline int setPasswordPolicy(PolicyControlContext &ctx, const std::string &name, int value)
+inline bool setPasswordPolicy(PolicyControlContext &ctx, const std::string &name, int value)
 {
        return ctx.setPolicy(name, value, "password", name);
 }
@@ -114,6 +114,14 @@ PasswordPolicy::~PasswordPolicy()
 int PasswordPolicy::setQuality(int quality)
 {
        try {
+               if (!setPasswordPolicy(context, "password-quality", quality)) {
+                       return 0;
+               }
+
+               if (quality == DPM_PASSWORD_QUALITY_SIMPLE_PASSWORD) {
+                       setPasswordPolicy(context, "password-minimum-length", SIMPLE_PASSWORD_LENGTH);
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setQuality(getPasswordQualityType(quality));
 
@@ -125,12 +133,6 @@ int PasswordPolicy::setQuality(int quality)
                ERROR(e.what());
                return -1;
        }
-
-       setPasswordPolicy(context, "password-quality", quality);
-       if (quality == DPM_PASSWORD_QUALITY_SIMPLE_PASSWORD) {
-               setPasswordPolicy(context, "password-minimum-length", SIMPLE_PASSWORD_LENGTH);
-       }
-
        return 0;
 }
 
@@ -142,8 +144,11 @@ int PasswordPolicy::getQuality()
 int PasswordPolicy::setMinimumLength(int value)
 {
        try {
-               PasswordManager passwordManager(context.getPeerUid());
+               if (!setPasswordPolicy(context, "password-minimum-length", value)) {
+                       return 0;
+               }
 
+               PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setMinimumLength(value);
                passwordManager.enforce();
        } catch (runtime::Exception &e) {
@@ -151,7 +156,7 @@ int PasswordPolicy::setMinimumLength(int value)
                return -1;
        }
 
-       return setPasswordPolicy(context, "password-minimum-length", value);
+       return 0;
 }
 
 int PasswordPolicy::getMinimumLength()
@@ -162,6 +167,10 @@ int PasswordPolicy::getMinimumLength()
 int PasswordPolicy::setMinComplexChars(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-minimum-complexity", value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setMinimumComplexCharacters(value);
                passwordManager.enforce();
@@ -169,8 +178,7 @@ int PasswordPolicy::setMinComplexChars(int value)
                ERROR("Failed to set minimum complex characters");
                return -1;
        }
-
-       return setPasswordPolicy(context, "password-minimum-complexity", value);
+       return 0;
 }
 
 int PasswordPolicy::getMinComplexChars()
@@ -181,6 +189,10 @@ int PasswordPolicy::getMinComplexChars()
 int PasswordPolicy::setMaximumFailedForWipe(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-maximum-failure-count", (value == 0) ? INT_MAX : value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setMaximumFailedForWipe(value);
                passwordManager.enforce();
@@ -188,8 +200,7 @@ int PasswordPolicy::setMaximumFailedForWipe(int value)
                ERROR("Failed to set maximum failed count for wipe");
                return -1;
        }
-
-       return setPasswordPolicy(context, "password-maximum-failure-count", (value == 0) ? INT_MAX : value);
+       return 0;
 }
 
 int PasswordPolicy::getMaximumFailedForWipe()
@@ -201,6 +212,10 @@ int PasswordPolicy::getMaximumFailedForWipe()
 int PasswordPolicy::setExpires(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-expired", (value == 0) ? INT_MAX : value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setExpires(value);
                passwordManager.enforce();
@@ -208,8 +223,8 @@ int PasswordPolicy::setExpires(int value)
                ERROR("Failed to set expire");
                return -1;
        }
+       return 0;
 
-       return setPasswordPolicy(context, "password-expired", (value == 0) ? INT_MAX : value);
 }
 
 int PasswordPolicy::getExpires()
@@ -221,6 +236,10 @@ int PasswordPolicy::getExpires()
 int PasswordPolicy::setHistory(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-history", value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setHistory(value);
                passwordManager.enforce();
@@ -228,8 +247,7 @@ int PasswordPolicy::setHistory(int value)
                ERROR("Failed to set history size");
                return -1;
        }
-
-       return setPasswordPolicy(context, "password-history", value);
+       return 0;
 }
 
 int PasswordPolicy::getHistory()
@@ -295,7 +313,13 @@ int PasswordPolicy::enforceChange()
 
 int PasswordPolicy::setMaxInactivityTimeDeviceLock(int value)
 {
-       return setPasswordPolicy(context, "password-inactivity-timeout", value);
+       try {
+               setPasswordPolicy(context, "password-inactivity-timeout", value);
+       } catch (runtime::Exception &e) {
+               ERROR(e.what());
+               return -1;
+       }
+       return 0;
 }
 
 int PasswordPolicy::getMaxInactivityTimeDeviceLock()
@@ -362,6 +386,10 @@ std::string PasswordPolicy::getPattern()
 int PasswordPolicy::setMaximumCharacterOccurrences(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-maximum-character-occurrences", (value == 0) ? INT_MAX : value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setMaximumCharacterOccurrences(value);
                passwordManager.enforce();
@@ -369,8 +397,7 @@ int PasswordPolicy::setMaximumCharacterOccurrences(int value)
                ERROR(e.what());
                return -1;
        }
-
-       return setPasswordPolicy(context, "password-maximum-character-occurrences", (value == 0) ? INT_MAX : value);
+       return 0;
 }
 
 int PasswordPolicy::getMaximumCharacterOccurrences()
@@ -383,6 +410,10 @@ int PasswordPolicy::getMaximumCharacterOccurrences()
 int PasswordPolicy::setMaximumNumericSequenceLength(int value)
 {
        try {
+               if (!setPasswordPolicy(context, "password-numeric-sequences-length", (value == 0) ? INT_MAX : value)) {
+                       return 0;
+               }
+
                PasswordManager passwordManager(context.getPeerUid());
                passwordManager.setMaximumNumericSequenceLength(value);
                passwordManager.enforce();
@@ -390,8 +421,7 @@ int PasswordPolicy::setMaximumNumericSequenceLength(int value)
                ERROR(e.what());
                return -1;
        }
-
-       return setPasswordPolicy(context, "password-numeric-sequences-length", (value == 0) ? INT_MAX : value);
+       return 0;
 }
 
 int PasswordPolicy::getMaximumNumericSequenceLength()
index b2a1faf..1b500f4 100644 (file)
@@ -39,12 +39,12 @@ struct PolicyBuilder {
        std::unique_ptr<T> instance;
 };
 
-inline int SetPolicyAllowed(PolicyControlContext& context, const std::string& name, bool allow)
+inline bool SetPolicyAllowed(PolicyControlContext& context, const std::string& name, bool allow)
 {
        return context.setPolicy(name, allow, name, allow ? "allowed" : "disallowed");
 }
 
-inline int SetPolicyEnabled(PolicyControlContext& context, const std::string& name, bool enable)
+inline bool SetPolicyEnabled(PolicyControlContext& context, const std::string& name, bool enable)
 {
        return context.setPolicy(name, enable, name, enable ? "enabled" : "disabled");
 }
index fb1ca84..dd287a0 100644 (file)
@@ -90,7 +90,13 @@ RestrictionPolicy::~RestrictionPolicy()
 
 int RestrictionPolicy::setCameraState(int enable)
 {
-       return SetPolicyAllowed(context, "camera", enable);
+       try {
+               SetPolicyAllowed(context, "camera", enable);
+       } catch (runtime::Exception& e) {
+               ERROR("Failed to enforce camera policy");
+               return -1;
+       }
+       return 0;
 }
 
 int RestrictionPolicy::getCameraState()
@@ -102,6 +108,10 @@ int RestrictionPolicy::setMicrophoneState(int enable)
 {
        char *result = NULL;
        try {
+               if (!SetPolicyAllowed(context, "microphone", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(PULSEAUDIO_LOGIN_INTERFACE,
                                                          "UpdateRestriction",
@@ -111,15 +121,15 @@ int RestrictionPolicy::setMicrophoneState(int enable)
                                                          "block_recording_media",
                                                          !enable).get("(s)", &result);
        } catch (runtime::Exception& e) {
-               ERROR("Failed to enforce location policy");
+               ERROR("Failed to enforce microphone policy");
                return -1;
        }
 
-       if (strcmp(result, "STREAM_MANAGER_RETURN_OK") == 0) {
-               return SetPolicyAllowed(context, "microphone", enable);
+       if (strcmp(result, "STREAM_MANAGER_RETURN_OK") != 0) {
+               return -1;
        }
 
-       return -1;
+       return 0;
 }
 
 int RestrictionPolicy::getMicrophoneState()
@@ -129,7 +139,13 @@ int RestrictionPolicy::getMicrophoneState()
 
 int RestrictionPolicy::setClipboardState(int enable)
 {
-       return SetPolicyAllowed(context, "clipboard", enable);
+       try {
+               SetPolicyAllowed(context, "clipboard", enable);
+       } catch (runtime::Exception& e) {
+               ERROR("Failed to enforce clipboard policy");
+               return -1;
+       }
+       return 0;
 }
 
 int RestrictionPolicy::getClipboardState()
@@ -139,7 +155,13 @@ int RestrictionPolicy::getClipboardState()
 
 int RestrictionPolicy::setUsbDebuggingState(int enable)
 {
-       return SetPolicyAllowed(context, "usb-debugging", enable);
+       try {
+               SetPolicyAllowed(context, "usb-debugging", enable);
+       } catch (runtime::Exception& e) {
+               ERROR("Failed to enforce usb debugging policy");
+               return -1;
+       }
+       return 0;
 }
 
 int RestrictionPolicy::getUsbDebuggingState()
@@ -150,6 +172,10 @@ int RestrictionPolicy::getUsbDebuggingState()
 int RestrictionPolicy::setUsbTetheringState(bool enable)
 {
        try {
+               if (!SetPolicyAllowed(context, "usb-tethering", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(MOBILEAP_INTERFACE,
                                                          "change_policy",
@@ -162,8 +188,7 @@ int RestrictionPolicy::setUsbTetheringState(bool enable)
                ERROR("Failed to change USB tethering state");
                return -1;
        }
-
-       return SetPolicyAllowed(context, "usb-tethering", enable);
+       return 0;
 }
 
 bool RestrictionPolicy::getUsbTetheringState()
@@ -175,9 +200,12 @@ int RestrictionPolicy::setExternalStorageState(int enable)
 {
        int ret;
        try {
+               if (!SetPolicyAllowed(context, "external-storage", enable)) {
+                       return 0;
+               }
+
                std::string pid(std::to_string(::getpid()));
                std::string state(std::to_string(enable));
-
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(DEVICED_SYSNOTI_INTERFACE,
                                                          -1, "(i)", "(sisss)",
@@ -187,11 +215,11 @@ int RestrictionPolicy::setExternalStorageState(int enable)
                return -1;
        }
 
-       if (ret == 0) {
-               return SetPolicyAllowed(context, "external-storage", enable);
+       if (ret != 0) {
+               return -1;
        }
 
-       return -1;
+       return 0;
 }
 
 int RestrictionPolicy::getExternalStorageState()
@@ -201,7 +229,13 @@ int RestrictionPolicy::getExternalStorageState()
 
 int RestrictionPolicy::setPopImapEmailState(int enable)
 {
-       return SetPolicyAllowed(context, "popimap-email", enable);
+       try {
+               SetPolicyAllowed(context, "popimap-email", enable);
+    } catch (runtime::Exception& e) {
+        ERROR("Failed to enforce pop/imap email policy");
+        return -1;
+    }
+    return 0;
 }
 
 int RestrictionPolicy::getPopImapEmailState()
@@ -211,7 +245,13 @@ int RestrictionPolicy::getPopImapEmailState()
 
 int RestrictionPolicy::setMessagingState(int enable)
 {
-       return SetPolicyAllowed(context, "messaging", enable);
+       try {
+               SetPolicyAllowed(context, "messaging", enable);
+    } catch (runtime::Exception& e) {
+        ERROR("Failed to enforce messaging policy");
+        return -1;
+    }
+       return 0;
 }
 
 int RestrictionPolicy::getMessagingState()
@@ -221,7 +261,13 @@ int RestrictionPolicy::getMessagingState()
 
 int RestrictionPolicy::setBrowserState(int enable)
 {
-       return SetPolicyAllowed(context, "browser", enable);
+       try {
+               SetPolicyAllowed(context, "browser", enable);
+       } catch (runtime::Exception& e) {
+        ERROR("Failed to enforce browser [olicy");
+        return -1;
+    }
+       return 0;
 }
 
 int RestrictionPolicy::getBrowserState()
index 3dcf378..9cc337b 100644 (file)
@@ -113,22 +113,18 @@ int Server::unregisterNotificationSubscriber(const std::string& name, int id)
        return service->unsubscribeNotification(name, id);
 }
 
-int Server::setPolicy(const std::string& name, int value, const std::string& event, const std::string& info)
+bool Server::setPolicy(const std::string& name, int value, const std::string& event, const std::string& info)
 {
        uid_t uid = getPeerUid();
-       try {
-               std::string pkgid = GetPackageId(uid, getPeerPid());
-               if (policyManager->setPolicy(pkgid, uid, name, value)) {
-                       if (event.empty() == false) {
-                               service->notify(event, info);
-                       }
+       std::string pkgid = GetPackageId(uid, getPeerPid());
+       if (policyManager->setPolicy(pkgid, uid, name, value)) {
+               if (event.empty() == false) {
+                       service->notify(event, info);
                }
-       } catch (runtime::Exception& e) {
-               ERROR(e.what());
-               return -1;
+               return true;
        }
 
-       return 0;
+       return false;
 }
 
 bool Server::checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege)
index 9d4e405..eb8357f 100644 (file)
@@ -77,9 +77,9 @@ public:
                }
        }
 
-       int setPolicy(const std::string& name, int value, const std::string& event, const std::string& info);
+       bool setPolicy(const std::string& name, int value, const std::string& event, const std::string& info);
 
-       int setPolicy(const std::string& name, int value, const std::string& info)
+       bool setPolicy(const std::string& name, int value, const std::string& info)
        {
                return setPolicy(name, value, name, info);
        }
index 095f8a9..286e6a1 100644 (file)
@@ -126,6 +126,10 @@ WifiPolicy::~WifiPolicy()
 int WifiPolicy::setState(bool enable)
 {
        try {
+               if (!SetPolicyAllowed(context, "wifi", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(NETCONFIG_INTERFACE,
                                                          "DevicePolicySetWifi",
@@ -138,7 +142,6 @@ int WifiPolicy::setState(bool enable)
                return -1;
        }
 
-       SetPolicyAllowed(context, "wifi", enable);
        return 0;
 }
 
@@ -150,6 +153,10 @@ bool WifiPolicy::getState()
 int WifiPolicy::setHotspotState(bool enable)
 {
        try {
+               if (!SetPolicyAllowed(context, "wifi-hotspot", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(MOBILEAP_INTERFACE,
                                                          "change_policy",
@@ -163,7 +170,6 @@ int WifiPolicy::setHotspotState(bool enable)
                return -1;
        }
 
-       SetPolicyAllowed(context, "wifi-hotspot", enable);
        return 0;
 }
 
@@ -175,6 +181,10 @@ bool WifiPolicy::getHotspotState()
 int WifiPolicy::setProfileChangeRestriction(bool enable)
 {
        try {
+               if (!SetPolicyAllowed(context, "wifi-profile-change", enable)) {
+                       return 0;
+               }
+
                dbus::Connection &systemDBus = dbus::Connection::getSystem();
                systemDBus.methodcall(NETCONFIG_INTERFACE,
                                                          "DevicePolicySetWifiProfile",
@@ -186,7 +196,6 @@ int WifiPolicy::setProfileChangeRestriction(bool enable)
                ERROR("Failed to set Wi-Fi profile change restriction");
        }
 
-       SetPolicyAllowed(context, "wifi-profile-change", enable);
        return 0;
 }
 
@@ -197,10 +206,16 @@ bool WifiPolicy::isProfileChangeRestricted(void)
 
 int WifiPolicy::setNetworkAccessRestriction(bool enable)
 {
-       SetPolicyEnabled(context, "wifi-ssid-restriction", enable);
+       try {
+                       if (!SetPolicyEnabled(context, "wifi-ssid-restriction", enable)) {
+                               return 0;
+                       }
 
-       if (enable) {
-               applyBlocklistToConnectedAP();
+                       if (enable) {
+                               applyBlocklistToConnectedAP();
+                       }
+       } catch (runtime::Exception& e) {
+               ERROR("Failed to set Wi-Fi profile change restriction");
        }
 
        return 0;