allow all clients to access storage socket and ocsp socket
[platform/core/security/key-manager.git] / src / manager / main / generic-socket-manager.h
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 /*
19  * @file        generic-socket-manager.h
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @author      Zofia Abramowska (z.abramowska@samsung.com)
22  * @version     1.0
23  * @brief       Implementation of GenericSocketService and GenericSocketManager.
24  */
25
26 #ifndef _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
27 #define _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
28
29 #include <vector>
30 #include <string>
31
32 #include <sys/types.h>
33
34 #include <dpl/exception.h>
35 #include <generic-event.h>
36 #include <dpl/raw-buffer.h>
37 #include <ckm/ckm-type.h>
38
39 extern "C" {
40 struct msghdr;
41 } // extern "C"
42
43 namespace CKM {
44
45 typedef int InterfaceID;
46
47 struct Credentials {
48     Credentials() : clientUid(0) {}
49     Credentials(uid_t socketUid, const Label & socketLabel)
50         : clientUid(socketUid), smackLabel(socketLabel) {}
51     uid_t clientUid;
52     Label smackLabel;
53 };
54
55 struct ConnectionID {
56     int sock;                                 // This is decriptor used for connection
57     int counter;                              // Unique handler per socket
58     inline bool operator<(const ConnectionID &second) const {
59         return counter < second.counter;
60     }
61 };
62
63 struct GenericSocketManager;
64
65 struct GenericSocketService {
66     typedef std::string ServiceHandlerPath;
67     struct ServiceDescription {
68         ServiceDescription(const char *path,
69             const char *smackLabel,
70             InterfaceID interfaceID = 0,
71             bool useSendMsg = false)
72           : smackLabel(smackLabel)
73           , interfaceID(interfaceID)
74           , serviceHandlerPath(path)
75           , useSendMsg(useSendMsg)
76         {}
77
78         Label smackLabel;                      // Smack label for socket
79         InterfaceID interfaceID;               // All data from serviceHandlerPath will be marked with this interfaceHandler
80         ServiceHandlerPath serviceHandlerPath; // Path to file
81         bool useSendMsg;
82     };
83
84     typedef std::vector<ServiceDescription> ServiceDescriptionVector;
85
86     struct AcceptEvent : public GenericEvent {
87         ConnectionID connectionID;
88         InterfaceID interfaceID;
89         Credentials credentials;
90     };
91
92     struct WriteEvent : public GenericEvent {
93         ConnectionID connectionID;
94         size_t size;
95         size_t left;
96     };
97
98     struct ReadEvent : public GenericEvent {
99         ConnectionID connectionID;
100         RawBuffer rawBuffer;
101     };
102
103     struct CloseEvent : public GenericEvent {
104         ConnectionID connectionID;
105     };
106
107     virtual void SetSocketManager(GenericSocketManager *manager) {
108         m_serviceManager = manager;
109     }
110
111     virtual ServiceDescriptionVector GetServiceDescription() = 0;
112     virtual void Event(const AcceptEvent &event) = 0;
113     virtual void Event(const WriteEvent &event) = 0;
114     virtual void Event(const ReadEvent &event) = 0;
115     virtual void Event(const CloseEvent &event) = 0;
116
117     virtual void Start() = 0;
118     virtual void Stop() = 0;
119
120     GenericSocketService() : m_serviceManager(NULL) {}
121     virtual ~GenericSocketService(){}
122 protected:
123     GenericSocketManager *m_serviceManager;
124 };
125
126 struct GenericSocketManager {
127     virtual void MainLoop() = 0;
128     virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
129     virtual void Close(ConnectionID connectionID) = 0;
130     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer) = 0;
131     virtual ~GenericSocketManager(){}
132 };
133
134 } // namespace CKM
135
136 #endif // _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_