Include device-policy-manager.h for the shared types
[platform/core/security/dpm-security.git] / api / security.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <tizen.h>
18 #include <tizen_type.h>
19
20 #include <dpm/pil/policy-client.h>
21
22 #include "security.h"
23
24 #define RET_ON_FAILURE(cond, ret) \
25 {                                 \
26         if (!(cond))                  \
27                 return (ret);             \
28 }
29
30 EXPORT_API int dpm_security_lockout_screen(device_policy_manager_h handle)
31 {
32         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
33
34         DevicePolicyClient &client = GetDevicePolicyClient(handle);
35         try {
36                 Status<int> status { -1 };
37                 status = client.methodCall<int>("Security::lockoutScreen");
38                 return status.get();
39         } catch (...) {
40                 return -1;
41         }
42 }
43
44 EXPORT_API int dpm_security_set_internal_storage_encryption(device_policy_manager_h handle, bool encrypt)
45 {
46         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
47
48         DevicePolicyClient &client = GetDevicePolicyClient(handle);
49         try {
50                 Status<int> status { -1 };
51                 status = client.methodCall<int>("Security::setInternalStorageEncryption", encrypt);
52                 return status.get();
53         } catch (...) {
54                 return -1;
55         }
56 }
57
58 EXPORT_API int dpm_security_is_internal_storage_encrypted(device_policy_manager_h handle, bool *is_encrypted)
59 {
60         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
61         RET_ON_FAILURE(is_encrypted, DPM_ERROR_INVALID_PARAMETER);
62
63         DevicePolicyClient &client = GetDevicePolicyClient(handle);
64         try {
65                 Status<bool> status { false };
66                 status = client.methodCall<int>("Security::isInternalStorageEncrypted");
67                 *is_encrypted = status.get();
68         } catch (...) {
69                 return -1;
70         }
71
72         return DPM_ERROR_NONE;
73 }
74
75 EXPORT_API int dpm_security_set_external_storage_encryption(device_policy_manager_h handle, bool encrypt)
76 {
77         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
78
79         DevicePolicyClient &client = GetDevicePolicyClient(handle);
80         try {
81                 Status<int> status { -1 };
82                 status = client.methodCall<int>("Security::setExternalStorageEncryption", encrypt);
83                 return status.get();
84         } catch (...) {
85                 return -1;
86         }
87 }
88
89 EXPORT_API int dpm_security_is_external_storage_encrypted(device_policy_manager_h handle, bool *is_encrypted)
90 {
91         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
92         RET_ON_FAILURE(is_encrypted, DPM_ERROR_INVALID_PARAMETER);
93
94         DevicePolicyClient &client = GetDevicePolicyClient(handle);
95         try {
96                 Status<bool> status { false };
97                 status = client.methodCall<int>("Security::isExternalStorageEncrypted");
98                 *is_encrypted = status.get();
99         } catch (...) {
100                 return -1;
101         }
102
103         return DPM_ERROR_NONE;
104 }
105
106 EXPORT_API int dpm_security_wipe_data(device_policy_manager_h handle, dpm_security_wipe_type_e type)
107 {
108         int mask = type & (DPM_SECURITY_WIPE_INTERNAL_STORAGE | DPM_SECURITY_WIPE_EXTERNAL_STORAGE);
109
110         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
111         RET_ON_FAILURE(mask, DPM_ERROR_INVALID_PARAMETER);
112
113         DevicePolicyClient &client = GetDevicePolicyClient(handle);
114         try {
115                 Status<int> status { -1 };
116                 status = client.methodCall<int>("Security::wipeData", mask);
117                 return status.get();
118         } catch (...) {
119                 return -1;
120         }
121 }