Prepare security-manager for master-slave mode
[platform/core/security/security-manager.git] / src / server / service / master-service.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@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 /*
19  * @file        master-service.cpp
20  * @author      Lukasz Kostyra <l.kostyra@samsung.com>
21  * @author      Rafal Krypa <r.krypa@samsung.com>
22  * @brief       Implementation of security-manager master service.
23  */
24
25 #include <generic-socket-manager.h>
26
27 #include <dpl/log/log.h>
28 #include <dpl/serialization.h>
29
30 #include "protocols.h"
31 #include "master-service.h"
32 #include "service_impl.h"
33
34 namespace SecurityManager {
35
36 const InterfaceID IFACE = 1;
37
38 MasterService::MasterService()
39 {
40 }
41
42 GenericSocketService::ServiceDescriptionVector MasterService::GetServiceDescription()
43 {
44     return ServiceDescriptionVector {
45         {MASTER_SERVICE_SOCKET, "security-manager-master", IFACE},
46     };
47 }
48
49 bool MasterService::processOne(const ConnectionID &conn, MessageBuffer &buffer,
50                                   InterfaceID interfaceID)
51 {
52     LogDebug("Iteration begin. Interface = " << interfaceID);
53
54     //waiting for all data
55     if (!buffer.Ready()) {
56         return false;
57     }
58
59     MessageBuffer send;
60     bool retval = false;
61
62     uid_t uid;
63     pid_t pid;
64
65     if (!ServiceImpl::getPeerID(conn.sock, uid, pid)) {
66         LogError("Closing socket because of error: unable to get peer's uid and pid");
67         m_serviceManager->Close(conn);
68         return false;
69     }
70
71     if (IFACE == interfaceID) {
72         Try {
73             // deserialize API call type
74             int call_type_int;
75             Deserialization::Deserialize(buffer, call_type_int);
76             SecurityModuleCall call_type = static_cast<SecurityModuleCall>(call_type_int);
77
78             switch (call_type) {
79                 default:
80                     LogError("Invalid call: " << call_type_int);
81                     Throw(MasterServiceException::InvalidAction);
82             }
83             // if we reach this point, the protocol is OK
84             retval = true;
85         } Catch (MessageBuffer::Exception::Base) {
86             LogError("Broken protocol.");
87         } Catch (MasterServiceException::Base) {
88             LogError("Broken protocol.");
89         } catch (const std::exception &e) {
90             LogError("STD exception " << e.what());
91         } catch (...) {
92             LogError("Unknown exception");
93         }
94     }
95     else {
96         LogError("Wrong interface");
97     }
98
99     if (retval) {
100         //send response
101         m_serviceManager->Write(conn, send.Pop());
102     } else {
103         LogError("Closing socket because of error");
104         m_serviceManager->Close(conn);
105     }
106
107     return retval;
108 }
109
110
111 } // namespace SecurityManager