2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
22 #include <service-thread.h>
23 #include <generic-socket-manager.h>
24 #include <connection-info.h>
25 #include <message-buffer.h>
26 #include <protocols.h>
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
31 #include <ckm-service.h>
32 #include <ckm-logic.h>
33 #include <client-key-impl.h>
36 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
37 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
38 } // namespace anonymous
42 CKMService::CKMService()
43 : m_logic(new CKMLogic)
46 CKMService::~CKMService() {
50 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
52 return ServiceDescriptionVector {
53 {SERVICE_SOCKET_CKM_CONTROL, "ckm::api-control", SOCKET_ID_CONTROL},
54 {SERVICE_SOCKET_CKM_STORAGE, "ckm::api-storage", SOCKET_ID_STORAGE}
58 void CKMService::accept(const AcceptEvent &event) {
59 LogDebug("Accept event");
60 auto &info = m_connectionInfoMap[event.connectionID.counter];
61 info.interfaceID = event.interfaceID;
64 void CKMService::write(const WriteEvent &event) {
65 LogDebug("Write event (" << event.size << " bytes)");
68 void CKMService::process(const ReadEvent &event) {
69 LogDebug("Read event");
70 auto &info = m_connectionInfoMap[event.connectionID.counter];
71 info.buffer.Push(event.rawBuffer);
72 while(processOne(event.connectionID, info));
75 bool CKMService::processOne(
76 const ConnectionID &conn,
79 LogDebug ("process One");
83 if (!info.buffer.Ready())
86 if (info.interfaceID == SOCKET_ID_CONTROL)
87 response = processControl(info.buffer);
89 response = processStorage(info.credentials, info.buffer);
91 m_serviceManager->Write(conn, response);
94 } Catch (MessageBuffer::Exception::Base) {
95 LogError("Broken protocol. Closing socket.");
97 LogError("Unknown exception. Closing socket.");
100 m_serviceManager->Close(conn);
104 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
108 std::string newPass, oldPass;
110 Deserialization::Deserialize(buffer, command);
111 Deserialization::Deserialize(buffer, user);
113 cc = static_cast<ControlCommand>(command);
116 case ControlCommand::UNLOCK_USER_KEY:
117 Deserialization::Deserialize(buffer, newPass);
118 return m_logic->unlockUserKey(user, newPass);
119 case ControlCommand::LOCK_USER_KEY:
120 return m_logic->lockUserKey(user);
121 case ControlCommand::REMOVE_USER_DATA:
122 return m_logic->removeUserData(user);
123 case ControlCommand::CHANGE_USER_PASSWORD:
124 Deserialization::Deserialize(buffer, oldPass);
125 Deserialization::Deserialize(buffer, newPass);
126 return m_logic->changeUserPassword(user, oldPass, newPass);
127 case ControlCommand::RESET_USER_PASSWORD:
128 Deserialization::Deserialize(buffer, newPass);
129 return m_logic->resetUserPassword(user, newPass);
132 throw 1; // broken protocol
136 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
144 Deserialization::Deserialize(buffer, command);
145 Deserialization::Deserialize(buffer, commandId);
147 sc = static_cast<LogicCommand>(command);
150 case LogicCommand::SAVE:
153 PolicySerializable policy;
154 Deserialization::Deserialize(buffer, tmpDataType);
155 Deserialization::Deserialize(buffer, alias);
156 Deserialization::Deserialize(buffer, rawData);
157 Deserialization::Deserialize(buffer, policy);
158 return m_logic->saveData(
161 static_cast<DBDataType>(tmpDataType),
166 case LogicCommand::REMOVE:
168 Deserialization::Deserialize(buffer, tmpDataType);
169 Deserialization::Deserialize(buffer, alias);
170 return m_logic->removeData(
173 static_cast<DBDataType>(tmpDataType),
176 case LogicCommand::GET:
178 std::string password;
179 Deserialization::Deserialize(buffer, tmpDataType);
180 Deserialization::Deserialize(buffer, alias);
181 Deserialization::Deserialize(buffer, password);
182 return m_logic->getData(
185 static_cast<DBDataType>(tmpDataType),
189 case LogicCommand::GET_LIST:
191 Deserialization::Deserialize(buffer, tmpDataType);
192 return m_logic->getDataList(
195 static_cast<DBDataType>(tmpDataType));
199 throw 1; // broken protocol
204 void CKMService::close(const CloseEvent &event) {
205 LogDebug("Close event");
206 m_connectionInfoMap.erase(event.connectionID.counter);