Music Hub  ..
A session-wide music playback service
ubuntu.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef CORE_UBUNTU_MEDIA_APPARMOR_UBUNTU_H_
19 #define CORE_UBUNTU_MEDIA_APPARMOR_UBUNTU_H_
20 
23 
24 #include <functional>
25 #include <memory>
26 #include <regex>
27 #include <string>
28 #include <vector>
29 
30 namespace core
31 {
32 namespace dbus
33 {
34 class Bus;
35 }
36 
37 namespace ubuntu
38 {
39 namespace media
40 {
41 namespace helper
42 {
43 struct ExternalServices;
44 }
45 namespace apparmor
46 {
47 // Collects Ubuntu-specific apparmor conventions, e.g., format
48 // of short and full package names as well as convenience functionality
49 // to inspect apparmor::Context instances.
50 namespace ubuntu
51 {
52 // The unconfined profile, unconditionally trusted
53 // by the system.
54 static constexpr const char* unconfined
55 {
56  "unconfined"
57 };
58 
59 class Context : public apparmor::Context
60 {
61 public:
62  // Constructs a new Context instance for the given raw name.
63  // Throws std::logic_error for empty names or for names not
64  // complying to Ubuntu conventions.
65  Context(const std::string& name);
66 
67  // Returns true iff the context is unconfined.
68  virtual bool is_unconfined() const;
69 
70  // Returns true iff the context contains a package name.
71  virtual bool has_package_name() const;
72 
73  // Returns the package name or throws if no package name can be found.
74  virtual std::string package_name() const;
75 
76 private:
77  std::smatch match_;
78  const bool unconfined_;
79  const bool has_package_name_;
80 };
81 
82 // Abstracts query for the apparmor context of an incoming request
84 {
85 public:
86  // To save us some typing.
87  typedef std::shared_ptr<RequestContextResolver> Ptr;
88 
89  // Callback for resolve context operations.
90  typedef std::function<void(const Context&)> ResolveCallback;
91 
92  // Resolves the given name (of a dbus participant) to its apparmor context,
93  // invoking the callback whenever a result is available.
94  virtual void resolve_context_for_dbus_name_async(const std::string& name, ResolveCallback cb) = 0;
95 
96 protected:
97  RequestContextResolver() = default;
99  virtual ~RequestContextResolver() = default;
101 };
102 
103 // An implementation of RequestContextResolver that queries the dbus
104 // daemon to resolve the apparmor context.
106 {
107 public:
108  // To save us some typing.
109  typedef std::shared_ptr<DBusDaemonRequestContextResolver> Ptr;
110 
111  // Constructs a new instance for the given bus connection.
112  DBusDaemonRequestContextResolver(const core::dbus::Bus::Ptr &);
113 
114  // From RequestContextResolver
115  void resolve_context_for_dbus_name_async(const std::string& name, ResolveCallback) override;
116 
117 private:
119 };
120 
121 // Abstracts an apparmor-based authentication of
122 // incoming requests from clients.
124 {
125 public:
126  // To save us some typing.
127  typedef std::shared_ptr<RequestAuthenticator> Ptr;
128 
129  // Return type of an authentication call.
130  typedef std::tuple
131  <
132  bool, // True if authenticated, false if not.
133  std::string // Reason for the result.
135 
136  virtual ~RequestAuthenticator() = default;
137 
138  // Returns true iff the client identified by the given apparmor::Context is allowed
139  // to access the given uri, false otherwise.
140  virtual Result authenticate_open_uri_request(const Context&, const std::string& uri) = 0;
141 
142 protected:
143  RequestAuthenticator() = default;
144  RequestAuthenticator(const RequestAuthenticator&) = default;
146 };
147 
148 // Takes the existing logic and exposes it as an implementation
149 // of the RequestAuthenticator interface.
151 {
152  ExistingAuthenticator() = default;
153  // From RequestAuthenticator
154  Result authenticate_open_uri_request(const Context&, const std::string& uri) override;
155 };
156 
157 // Returns the platform-default implementation of RequestContextResolver.
159 // Returns the platform-default implementation of RequestAuthenticator.
161 }
162 }
163 }
164 }
165 }
166 
167 #endif // CORE_UBUNTU_MEDIA_APPARMOR_UBUNTU_H_
RequestAuthenticator & operator=(const RequestAuthenticator &)=default
Definition: player.h:32
RequestAuthenticator::Ptr make_platform_default_request_authenticator()
RequestContextResolver & operator=(const RequestContextResolver &)=delete
virtual Result authenticate_open_uri_request(const Context &, const std::string &uri)=0
std::shared_ptr< RequestContextResolver > Ptr
Definition: ubuntu.h:87
RequestContextResolver::Ptr make_platform_default_request_context_resolver(helper::ExternalServices &es)
void resolve_context_for_dbus_name_async(const std::string &name, ResolveCallback) override
Definition: ubuntu.cpp:125
std::shared_ptr< DBusDaemonRequestContextResolver > Ptr
Definition: ubuntu.h:109
std::function< void(const Context &)> ResolveCallback
Definition: ubuntu.h:90
virtual void resolve_context_for_dbus_name_async(const std::string &name, ResolveCallback cb)=0
Result authenticate_open_uri_request(const Context &, const std::string &uri) override
Definition: ubuntu.cpp:135
virtual std::string package_name() const
Definition: ubuntu.cpp:116
std::shared_ptr< RequestAuthenticator > Ptr
Definition: ubuntu.h:127