tizen 2.4 release
[framework/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-control.cpp
19  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20  * @version     1.0
21  * @brief       This file is implementation of client-control functions.
22  */
23 #include <dpl/log/log.h>
24
25 #include <client-common.h>
26 #include <message-buffer.h>
27 #include <protocols.h>
28
29 #include <ckm/ckm-control.h>
30
31 namespace CKM {
32
33 class ControlImpl : public Control {
34 public:
35     ControlImpl() : m_controlConnection(SERVICE_SOCKET_CKM_CONTROL) {}
36     ControlImpl(const ControlImpl &) = delete;
37     ControlImpl(ControlImpl &&) = delete;
38     ControlImpl& operator=(const ControlImpl &) = delete;
39     ControlImpl& operator=(ControlImpl &&) = delete;
40
41     virtual int unlockUserKey(const ClientInfo &clientInfo, const Password &password) {
42         return try_catch([&] {
43             if((int)clientInfo.getUID() < 0) {
44                 return CKM_API_ERROR_INPUT_PARAM;
45             }
46
47             MessageBuffer recv;
48             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::UNLOCK_USER_KEY),
49                                                  clientInfo.getClientID(),
50                                                  password);
51
52             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
53             if (CKM_API_SUCCESS != retCode)
54                 return retCode;
55
56             recv.Deserialize(retCode);
57
58             return retCode;
59         });
60     }
61
62     virtual int lockUserKey(const ClientInfo &clientInfo) {
63         return try_catch([&] {
64             if((int)clientInfo.getUID() < 0) {
65                 return CKM_API_ERROR_INPUT_PARAM;
66             }
67
68             MessageBuffer recv;
69             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::LOCK_USER_KEY),
70                                                  clientInfo.getClientID());
71
72             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
73             if (CKM_API_SUCCESS != retCode)
74                 return retCode;
75
76             recv.Deserialize(retCode);
77
78             return retCode;
79         });
80     }
81     virtual int removeUserData(const ClientInfo &clientInfo) {
82         return try_catch([&] {
83             if((int)clientInfo.getUID() < 0) {
84                 return CKM_API_ERROR_INPUT_PARAM;
85             }
86
87             MessageBuffer recv;
88             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_USER_DATA),
89                                                  clientInfo.getClientID());
90
91             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
92             if (CKM_API_SUCCESS != retCode)
93                 return retCode;
94
95             recv.Deserialize(retCode);
96
97             return retCode;
98         });
99     }
100     virtual int changeUserPassword(const ClientInfo &clientInfo, const Password &oldPassword, const Password &newPassword) {
101         return try_catch([&] {
102             if((int)clientInfo.getUID() < 0) {
103                 return CKM_API_ERROR_INPUT_PARAM;
104             }
105
106             MessageBuffer recv;
107             auto send = MessageBuffer::Serialize(
108                     static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD),
109                     clientInfo.getClientID(),
110                     oldPassword,
111                     newPassword);
112
113             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
114             if (CKM_API_SUCCESS != retCode)
115                 return retCode;
116
117             recv.Deserialize(retCode);
118
119             return retCode;
120         });
121     }
122
123     virtual int resetUserPassword(const ClientInfo &clientInfo, const Password &newPassword) {
124         return try_catch([&] {
125             if((int)clientInfo.getUID() < 0) {
126                 return CKM_API_ERROR_INPUT_PARAM;
127             }
128
129             MessageBuffer recv;
130             auto send = MessageBuffer::Serialize(
131                     static_cast<int>(ControlCommand::RESET_USER_PASSWORD),
132                     clientInfo.getClientID(),
133                     newPassword);
134
135             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
136             if (CKM_API_SUCCESS != retCode)
137                 return retCode;
138
139             recv.Deserialize(retCode);
140
141             return retCode;
142         });
143     }
144
145     virtual int removeApplicationData(const std::string &zone, const Label &smackLabel) {
146         return try_catch([&] {
147             if (smackLabel.empty()) {
148                 return CKM_API_ERROR_INPUT_PARAM;
149             }
150
151             MessageBuffer recv;
152             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_APP_DATA),
153                                                  zone,
154                                                  smackLabel);
155
156             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
157             if (CKM_API_SUCCESS != retCode)
158                 return retCode;
159
160             recv.Deserialize(retCode);
161
162             return retCode;
163         });
164     }
165
166     virtual int updateCCMode() {
167         return try_catch([&] {
168             MessageBuffer recv;
169             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::UPDATE_CC_MODE));
170
171             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
172             if (CKM_API_SUCCESS != retCode)
173                 return retCode;
174
175             recv.Deserialize(retCode);
176
177             return retCode;
178         });
179     }
180
181     virtual int setPermission(const ClientInfo &clientInfo,
182                               const Alias &alias,
183                               const Label &accessor,
184                               PermissionMask permissionMask)
185     {
186         return try_catch([&] {
187             MessageBuffer recv;
188             AliasSupport helper(alias);
189             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::SET_PERMISSION),
190                                                  clientInfo.getClientID(),
191                                                  helper.getName(),
192                                                  helper.getLabel(),
193                                                  accessor,
194                                                  permissionMask);
195
196             int retCode = m_controlConnection.processRequest(send.Pop(), recv);
197             if (CKM_API_SUCCESS != retCode)
198                 return retCode;
199
200             int command;
201             int counter;
202             recv.Deserialize(command, counter, retCode);
203
204             return retCode;
205         });
206     }
207
208     virtual ~ControlImpl(){}
209 private:
210     CKM::ServiceConnection m_controlConnection;
211 };
212
213 ControlShPtr Control::create() {
214     try {
215         return std::make_shared<ControlImpl>();
216     } catch (const std::bad_alloc &) {
217         LogDebug("Bad alloc was caught during ControlImpl creation.");
218     } catch (...) {
219         LogError("Critical error: Unknown exception was caught druing ControlImpl creation!");
220     }
221     return ControlShPtr();
222 }
223
224 } // namespace CKM
225