Add service for control operations.
[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
34 namespace {
35 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
36 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
37 } // namespace anonymous
38
39 namespace CKM {
40
41 CKMService::CKMService()
42   : m_logic(new CKMLogic)
43 {}
44
45 CKMService::~CKMService() {
46     delete m_logic;
47 }
48
49 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
50 {
51     return ServiceDescriptionVector {
52         {SERVICE_SOCKET_CKM_CONTROL, "ckm::api-control", SOCKET_ID_CONTROL},
53         {SERVICE_SOCKET_CKM_STORAGE, "ckm::api-storage", SOCKET_ID_STORAGE}
54     };
55 }
56
57 void CKMService::accept(const AcceptEvent &event) {
58     LogDebug("Accept event");
59     auto &info = m_connectionInfoMap[event.connectionID.counter];
60     info.interfaceID = event.interfaceID;
61 }
62
63 void CKMService::write(const WriteEvent &event) {
64     LogDebug("Write event (" << event.size << " bytes)");
65 }
66
67 void CKMService::process(const ReadEvent &event) {
68     LogDebug("Read event");
69     auto &info = m_connectionInfoMap[event.connectionID.counter];
70     info.buffer.Push(event.rawBuffer);
71     while(processOne(event.connectionID, info.buffer, info.interfaceID));
72 }
73
74 bool CKMService::processOne(
75     const ConnectionID &conn,
76     MessageBuffer &buffer,
77     InterfaceID interfaceID)
78 {
79     LogDebug ("process One");
80     RawBuffer response;
81
82     Try {
83         if (!buffer.Ready())
84             return false;
85
86         if (interfaceID == SOCKET_ID_CONTROL)
87             response = processControl(buffer);
88         else
89             response = processStorage(conn, 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(const ConnectionID &conn, MessageBuffer &buffer){
137     (void)conn;
138     (void)buffer;
139     return RawBuffer();
140 }
141
142
143 void CKMService::close(const CloseEvent &event) {
144     LogDebug("Close event");
145     m_connectionInfoMap.erase(event.connectionID.counter);
146 }
147
148 } // namespace CKM
149