Code refactoring:
[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 Password &password) {
40         return try_catch([&] {
41             if((int)user < 0) {
42                 return CKM_API_ERROR_INPUT_PARAM;
43             }
44
45             MessageBuffer send, recv;
46             Serialization::Serialize(send, static_cast<int>(ControlCommand::UNLOCK_USER_KEY));
47             Serialization::Serialize(send, user);
48             Serialization::Serialize(send, password);
49
50             int retCode = sendToServer(
51                 SERVICE_SOCKET_CKM_CONTROL,
52                 send.Pop(),
53                 recv);
54
55             if (CKM_API_SUCCESS != retCode) {
56                 return retCode;
57             }
58
59             Deserialization::Deserialize(recv, retCode);
60
61             return retCode;
62         });
63     }
64
65     virtual int lockUserKey(uid_t user) {
66         return try_catch([&] {
67             if((int)user < 0) {
68                 return CKM_API_ERROR_INPUT_PARAM;
69             }
70
71             MessageBuffer send, recv;
72             Serialization::Serialize(send, static_cast<int>(ControlCommand::LOCK_USER_KEY));
73             Serialization::Serialize(send, user);
74
75             int retCode = sendToServer(
76                 SERVICE_SOCKET_CKM_CONTROL,
77                 send.Pop(),
78                 recv);
79
80             if (CKM_API_SUCCESS != retCode) {
81                 return retCode;
82             }
83
84             Deserialization::Deserialize(recv, retCode);
85
86             return retCode;
87         });
88     }
89
90     virtual int removeUserData(uid_t user) {
91         return try_catch([&] {
92             if((int)user < 0) {
93                 return CKM_API_ERROR_INPUT_PARAM;
94             }
95
96             MessageBuffer send, recv;
97             Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_USER_DATA));
98             Serialization::Serialize(send, user);
99
100             int retCode = sendToServer(
101                 SERVICE_SOCKET_CKM_CONTROL,
102                 send.Pop(),
103                 recv);
104
105             if (CKM_API_SUCCESS != retCode) {
106                 return retCode;
107             }
108
109             Deserialization::Deserialize(recv, retCode);
110
111             return retCode;
112         });
113     }
114
115     virtual int changeUserPassword(uid_t user, const Password &oldPassword, const Password &newPassword) {
116         return try_catch([&] {
117             if((int)user < 0) {
118                 return CKM_API_ERROR_INPUT_PARAM;
119             }
120
121             MessageBuffer send, recv;
122             Serialization::Serialize(send, static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD));
123             Serialization::Serialize(send, user);
124             Serialization::Serialize(send, oldPassword);
125             Serialization::Serialize(send, newPassword);
126
127             int retCode = sendToServer(
128                 SERVICE_SOCKET_CKM_CONTROL,
129                 send.Pop(),
130                 recv);
131
132             if (CKM_API_SUCCESS != retCode) {
133                 return retCode;
134             }
135
136             Deserialization::Deserialize(recv, retCode);
137
138             return retCode;
139         });
140     }
141
142     virtual int resetUserPassword(uid_t user, const Password &newPassword) {
143         return try_catch([&] {
144             if((int)user < 0) {
145                 return CKM_API_ERROR_INPUT_PARAM;
146             }
147
148             MessageBuffer send, recv;
149             Serialization::Serialize(send, static_cast<int>(ControlCommand::RESET_USER_PASSWORD));
150             Serialization::Serialize(send, user);
151             Serialization::Serialize(send, newPassword);
152
153             int retCode = sendToServer(
154                 SERVICE_SOCKET_CKM_CONTROL,
155                 send.Pop(),
156                 recv);
157
158             if (CKM_API_SUCCESS != retCode) {
159                 return retCode;
160             }
161
162             retCode = sendToServer(
163                 SERVICE_SOCKET_CKM_CONTROL,
164                 send.Pop(),
165                 recv);
166
167             if (CKM_API_SUCCESS != retCode) {
168                 return retCode;
169             }
170
171             Deserialization::Deserialize(recv, retCode);
172
173             return retCode;
174         });
175     }
176
177     virtual ~ControlImpl(){}
178 };
179
180 ControlShPtr Control::create() {
181     return ControlShPtr(new ControlImpl());
182 }
183
184 } // namespace CKM
185