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
30namespace core
31{
32namespace dbus
33{
34class Bus;
35}
36
37namespace ubuntu
38{
39namespace media
40{
41namespace helper
42{
43struct ExternalServices;
44}
45namespace 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.
50namespace ubuntu
51{
52// The unconfined profile, unconditionally trusted
53// by the system.
54static constexpr const char* unconfined
55{
56 "unconfined"
57};
58
60{
61public:
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 matches Unity.
71 virtual bool is_unity() const;
72
73 // Returns true iff the context contains a package name.
74 virtual bool has_package_name() const;
75
76 // Returns the package name or throws if no package name can be found.
77 virtual std::string package_name() const;
78
79 virtual std::string profile_name() const;
80
81private:
82 std::smatch match_;
83 std::string pkg_name_;
84 const bool unconfined_;
85 const bool unity_;
86 const bool has_package_name_;
87};
88
89// Abstracts query for the apparmor context of an incoming request
91{
92public:
93 // To save us some typing.
94 typedef std::shared_ptr<RequestContextResolver> Ptr;
95
96 // Callback for resolve context operations.
97 typedef std::function<void(const Context&)> ResolveCallback;
98
99 // Resolves the given name (of a dbus participant) to its apparmor context,
100 // invoking the callback whenever a result is available.
101 virtual void resolve_context_for_dbus_name_async(const std::string& name, ResolveCallback cb) = 0;
102
103protected:
106 virtual ~RequestContextResolver() = default;
108};
109
110// An implementation of RequestContextResolver that queries the dbus
111// daemon to resolve the apparmor context.
113{
114public:
115 // To save us some typing.
116 typedef std::shared_ptr<DBusDaemonRequestContextResolver> Ptr;
117
118 // Constructs a new instance for the given bus connection.
119 DBusDaemonRequestContextResolver(const core::dbus::Bus::Ptr &);
120
121 // From RequestContextResolver
122 void resolve_context_for_dbus_name_async(const std::string& name, ResolveCallback) override;
123
124private:
126};
127
128// Abstracts an apparmor-based authentication of
129// incoming requests from clients.
131{
132public:
133 // To save us some typing.
134 typedef std::shared_ptr<RequestAuthenticator> Ptr;
135
136 // Return type of an authentication call.
137 typedef std::tuple
138 <
139 bool, // True if authenticated, false if not.
140 std::string // Reason for the result.
142
143 virtual ~RequestAuthenticator() = default;
144
145 // Returns true iff the client identified by the given apparmor::Context is allowed
146 // to access the given uri, false otherwise.
147 virtual Result authenticate_open_uri_request(const Context&, const std::string& uri) = 0;
148
149protected:
153};
154
155// Takes the existing logic and exposes it as an implementation
156// of the RequestAuthenticator interface.
158{
160 // From RequestAuthenticator
161 Result authenticate_open_uri_request(const Context&, const std::string& uri) override;
162};
163
164// Returns the platform-default implementation of RequestContextResolver.
166// Returns the platform-default implementation of RequestAuthenticator.
168}
169}
170}
171}
172}
173
174#endif // CORE_UBUNTU_MEDIA_APPARMOR_UBUNTU_H_
virtual std::string package_name() const
Definition: ubuntu.cpp:143
virtual std::string profile_name() const
Definition: ubuntu.cpp:148
std::shared_ptr< DBusDaemonRequestContextResolver > Ptr
Definition: ubuntu.h:116
void resolve_context_for_dbus_name_async(const std::string &name, ResolveCallback) override
Definition: ubuntu.cpp:157
RequestAuthenticator(const RequestAuthenticator &)=default
RequestAuthenticator & operator=(const RequestAuthenticator &)=default
virtual Result authenticate_open_uri_request(const Context &, const std::string &uri)=0
std::shared_ptr< RequestAuthenticator > Ptr
Definition: ubuntu.h:134
std::shared_ptr< RequestContextResolver > Ptr
Definition: ubuntu.h:94
virtual void resolve_context_for_dbus_name_async(const std::string &name, ResolveCallback cb)=0
RequestContextResolver(const RequestContextResolver &)=delete
std::function< void(const Context &)> ResolveCallback
Definition: ubuntu.h:97
RequestContextResolver & operator=(const RequestContextResolver &)=delete
RequestAuthenticator::Ptr make_platform_default_request_authenticator()
RequestContextResolver::Ptr make_platform_default_request_context_resolver(helper::ExternalServices &es)
Definition: player.h:34
Result authenticate_open_uri_request(const Context &, const std::string &uri) override
Definition: ubuntu.cpp:167