Replace private implemetation with interface.
[platform/core/security/key-manager.git] / src / manager / client / client-control.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        client-common.cpp
19  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20  * @version     1.0
21  * @brief       This file is implementation of client-common functions.
22  */
23 #include <client-common.h>
24 #include <message-buffer.h>
25 #include <protocols.h>
26
27 #include <ckm/ckm-control.h>
28
29 namespace CKM {
30
31 class ControlImpl : public Control {
32 public:
33     ControlImpl(){}
34     ControlImpl(const ControlImpl &) = delete;
35     ControlImpl(ControlImpl &&) = delete;
36     ControlImpl& operator=(const ControlImpl &) = delete;
37     ControlImpl& operator=(ControlImpl &&) = delete;
38
39     virtual int unlockUserKey(uid_t user, const std::string &password) const {
40         return try_catch([&] {
41             MessageBuffer send, recv;
42             Serialization::Serialize(send, static_cast<int>(ControlCommand::UNLOCK_USER_KEY));
43             Serialization::Serialize(send, user);
44             Serialization::Serialize(send, password);
45             int retCode;
46
47             if((int)user < 0) {
48                 retCode = CKM_API_ERROR_INPUT_PARAM;
49                 return retCode;
50             }
51
52             retCode = sendToServer(
53                 SERVICE_SOCKET_CKM_CONTROL,
54                 send.Pop(),
55                 recv);
56
57             if (CKM_API_SUCCESS != retCode) {
58                 return retCode;
59             }
60
61             Deserialization::Deserialize(recv, retCode);
62
63             return retCode;
64         });
65     }
66
67     virtual int lockUserKey(uid_t user) const {
68         return try_catch([&] {
69             MessageBuffer send, recv;
70             Serialization::Serialize(send, static_cast<int>(ControlCommand::LOCK_USER_KEY));
71             Serialization::Serialize(send, user);
72             int retCode;
73
74             if((int)user < 0) {
75                 retCode = CKM_API_ERROR_INPUT_PARAM;
76                 return retCode;
77             }
78
79             retCode = sendToServer(
80                 SERVICE_SOCKET_CKM_CONTROL,
81                 send.Pop(),
82                 recv);
83
84             if (CKM_API_SUCCESS != retCode) {
85                 return retCode;
86             }
87
88             Deserialization::Deserialize(recv, retCode);
89
90             return retCode;
91         });
92     }
93
94     virtual int removeUserData(uid_t user) const {
95         return try_catch([&] {
96             MessageBuffer send, recv;
97             Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_USER_DATA));
98             Serialization::Serialize(send, user);
99             int retCode;
100
101             if((int)user < 0) {
102                 retCode = CKM_API_ERROR_INPUT_PARAM;
103                 return retCode;
104             }
105
106             retCode = sendToServer(
107                 SERVICE_SOCKET_CKM_CONTROL,
108                 send.Pop(),
109                 recv);
110
111             if (CKM_API_SUCCESS != retCode) {
112                 return retCode;
113             }
114
115             Deserialization::Deserialize(recv, retCode);
116
117             return retCode;
118         });
119     }
120
121     virtual int changeUserPassword(uid_t user, const std::string &oldPassword, const std::string &newPassword) const {
122         return try_catch([&] {
123             MessageBuffer send, recv;
124             Serialization::Serialize(send, static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD));
125             Serialization::Serialize(send, user);
126             Serialization::Serialize(send, oldPassword);
127             Serialization::Serialize(send, newPassword);
128             int retCode;
129
130             if((int)user < 0) {
131                 retCode = CKM_API_ERROR_INPUT_PARAM;
132                 return retCode;
133             }
134
135             retCode = sendToServer(
136                 SERVICE_SOCKET_CKM_CONTROL,
137                 send.Pop(),
138                 recv);
139
140             if (CKM_API_SUCCESS != retCode) {
141                 return retCode;
142             }
143
144             Deserialization::Deserialize(recv, retCode);
145
146             return retCode;
147         });
148     }
149
150     virtual int resetUserPassword(uid_t user, const std::string &newPassword) const {
151         return try_catch([&] {
152             MessageBuffer send, recv;
153             Serialization::Serialize(send, static_cast<int>(ControlCommand::RESET_USER_PASSWORD));
154             Serialization::Serialize(send, user);
155             Serialization::Serialize(send, newPassword);
156             int retCode;
157
158             if((int)user < 0) {
159                 retCode = CKM_API_ERROR_INPUT_PARAM;
160                 return retCode;
161             }
162
163             retCode = sendToServer(
164                 SERVICE_SOCKET_CKM_CONTROL,
165                 send.Pop(),
166                 recv);
167
168             if (CKM_API_SUCCESS != retCode) {
169                 return retCode;
170             }
171
172             Deserialization::Deserialize(recv, retCode);
173
174             return retCode;
175         });
176     }
177
178     virtual ~ControlImpl(){}
179
180 };
181
182 ControlShPtr Control::create() {
183     return ControlShPtr(new ControlImpl());
184 }
185
186 } // namespace CKM
187