Add CynaraThread
[platform/core/appfw/rpc-port.git] / src / ac-internal.hh
1 /*
2  * Copyright (c) 2017 - 2021 Samsung Electronics Co., Ltd.
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 #ifndef AC_INTERNAL_HH_
18 #define AC_INTERNAL_HH_
19
20 #include <cynara-client.h>
21 #include <gio/gio.h>
22 #include <glib-unix.h>
23 #include <glib.h>
24
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30
31 namespace rpc_port {
32 namespace internal {
33
34 using CompleteCallback = std::function<void(int)>;
35
36 class AccessController {
37  public:
38   explicit AccessController(bool trusted = false) : trusted_(trusted) {}
39
40   void AddPrivilege(std::string privilege);
41   void SetTrusted(const bool trusted);
42   int Check(int fd, const std::string& sender_appid);
43   void CheckAsync(int fd, std::string sender_appid, CompleteCallback callback);
44
45  private:
46   class Cynara {
47    public:
48     Cynara();
49     ~Cynara();
50
51     int FetchCredsFromSocket(int fd);
52     int Check(const std::string& privilege) const;
53
54    private:
55     std::unique_ptr<cynara, decltype(cynara_finish)*> cynara_;
56     std::unique_ptr<char, decltype(std::free)*> client_;
57     std::unique_ptr<char, decltype(std::free)*> user_;
58   };
59
60   int CheckTrusted(const std::string& sender_appid);
61   int CheckPrivilege(const Cynara& c);
62
63  private:
64   std::vector<std::string> privileges_;
65   std::map<std::string, bool> cache_;
66   bool trusted_;
67   std::string appid_;
68 };
69
70 }  // namespace internal
71 }  // namespace rpc_port
72
73 #endif  // AC_INTERNAL_HH_