Service implementation.
[platform/core/security/key-manager.git] / src / manager / service / ckm-service.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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  * @file        ckm-service.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
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>
27
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
30
31 #include <ckm-service.h>
32 #include <ckm-logic.h>
33 #include <client-key-impl.h>
34
35 namespace {
36 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
37 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
38 } // namespace anonymous
39
40 namespace CKM {
41
42 CKMService::CKMService()
43   : m_logic(new CKMLogic)
44 {}
45
46 CKMService::~CKMService() {
47     delete m_logic;
48 }
49
50 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
51 {
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}
55     };
56 }
57
58 void CKMService::accept(const AcceptEvent &event) {
59     LogDebug("Accept event");
60     auto &info = m_connectionInfoMap[event.connectionID.counter];
61     info.interfaceID = event.interfaceID;
62 }
63
64 void CKMService::write(const WriteEvent &event) {
65     LogDebug("Write event (" << event.size << " bytes)");
66 }
67
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));
73 }
74
75 bool CKMService::processOne(
76     const ConnectionID &conn,
77     ConnectionInfo &info)
78 {
79     LogDebug ("process One");
80     RawBuffer response;
81
82     Try {
83         if (!info.buffer.Ready())
84             return false;
85
86         if (info.interfaceID == SOCKET_ID_CONTROL)
87             response = processControl(info.buffer);
88         else
89             response = processStorage(info.credentials, info.buffer);
90
91         m_serviceManager->Write(conn, response);
92
93         return true;
94     } Catch (MessageBuffer::Exception::Base) {
95         LogError("Broken protocol. Closing socket.");
96     } catch (...) {
97         LogError("Unknown exception. Closing socket.");
98     }
99
100     m_serviceManager->Close(conn);
101     return false;
102 }
103
104 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
105     int command;
106     std::string user;
107     ControlCommand cc;
108     RawBuffer newPass, oldPass;
109
110     Deserialization::Deserialize(buffer, command);
111     Deserialization::Deserialize(buffer, user);
112
113     cc = static_cast<ControlCommand>(command);
114
115     switch(cc) {
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);
130     default:
131         // TODO
132         throw 1; // broken protocol
133     }
134 }
135
136 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
137     int command;
138     int commandId;
139     int tmpDataType;
140     Alias alias;
141     std::string user;
142     LogicCommand sc;
143
144     Deserialization::Deserialize(buffer, command);
145     Deserialization::Deserialize(buffer, commandId);
146
147     sc = static_cast<LogicCommand>(command);
148
149     switch(sc) {
150         case LogicCommand::SAVE:
151         {
152             RawData rawData;
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(
159                 cred,
160                 commandId,
161                 static_cast<DBDataType>(tmpDataType),
162                 alias,
163                 rawData,
164                 policy);
165         }
166         case LogicCommand::REMOVE:
167         {
168             Deserialization::Deserialize(buffer, tmpDataType);
169             Deserialization::Deserialize(buffer, alias);
170             return m_logic->removeData(
171                 cred,
172                 commandId,
173                 static_cast<DBDataType>(tmpDataType),
174                 alias);
175         }
176         case LogicCommand::GET:
177         {
178             RawData password;
179             Deserialization::Deserialize(buffer, tmpDataType);
180             Deserialization::Deserialize(buffer, alias);
181             Deserialization::Deserialize(buffer, password);
182             return m_logic->getData(
183                 cred,
184                 commandId,
185                 static_cast<DBDataType>(tmpDataType),
186                 alias,
187                 password);
188         }
189         default:
190         // TODO
191             throw 1; // broken protocol
192     }
193 }
194
195
196 void CKMService::close(const CloseEvent &event) {
197     LogDebug("Close event");
198     m_connectionInfoMap.erase(event.connectionID.counter);
199 }
200
201 } // namespace CKM
202