2020-05-18 16:30:09 +02:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
2020-07-16 17:54:18 +02:00
|
|
|
* Copyright © 2020 Wim Taymans
|
2020-05-18 16:30:09 +02:00
|
|
|
*
|
2020-07-16 17:54:18 +02:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2020-05-18 16:30:09 +02:00
|
|
|
*
|
2020-07-16 17:54:18 +02:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2020-05-18 16:30:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
2020-07-16 17:54:18 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
#include <time.h>
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/support/dbus.h>
|
2020-07-16 17:54:18 +02:00
|
|
|
#include <spa/debug/dict.h>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
#include "media-session.h"
|
|
|
|
|
|
|
|
|
|
#define NAME "access-portal"
|
|
|
|
|
#define SESSION_KEY "access-portal"
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
enum media_role {
|
|
|
|
|
MEDIA_ROLE_INVALID = -1,
|
|
|
|
|
MEDIA_ROLE_NONE = 0,
|
|
|
|
|
MEDIA_ROLE_CAMERA = 1 << 0,
|
|
|
|
|
};
|
|
|
|
|
#define MEDIA_ROLE_ALL (MEDIA_ROLE_CAMERA)
|
|
|
|
|
|
|
|
|
|
struct impl {
|
2020-07-16 17:54:18 +02:00
|
|
|
struct sm_media_session *session;
|
|
|
|
|
struct spa_hook listener;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
struct spa_list client_list;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
DBusConnection *bus;
|
2020-05-18 16:30:09 +02:00
|
|
|
DBusPendingCall *portal_pid_pending;
|
|
|
|
|
pid_t portal_pid;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
|
|
|
|
|
struct client {
|
2020-05-18 16:30:09 +02:00
|
|
|
struct impl *impl;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
struct sm_client *obj;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
|
|
|
|
struct spa_list link; /**< link in impl client_list */
|
|
|
|
|
|
|
|
|
|
uint32_t id;
|
|
|
|
|
unsigned int portal_managed:1;
|
|
|
|
|
unsigned int setup_complete:1;
|
|
|
|
|
unsigned int is_portal:1;
|
2020-05-18 16:30:09 +02:00
|
|
|
char *app_id;
|
|
|
|
|
enum media_role media_roles;
|
|
|
|
|
enum media_role allowed_media_roles;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static void client_info_changed(struct client *client, const struct pw_client_info *info);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
static enum media_role media_role_from_string(const char *media_role_str)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(media_role_str, "Camera") == 0)
|
|
|
|
|
return MEDIA_ROLE_CAMERA;
|
|
|
|
|
else
|
|
|
|
|
return MEDIA_ROLE_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum media_role parse_media_roles(const char *media_types_str)
|
|
|
|
|
{
|
|
|
|
|
enum media_role media_roles = 0;
|
|
|
|
|
char *buf_orig;
|
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
|
|
buf_orig = strdup(media_types_str);
|
|
|
|
|
buf = buf_orig;
|
|
|
|
|
while (buf) {
|
|
|
|
|
char *media_role_str;
|
|
|
|
|
enum media_role media_role;
|
|
|
|
|
|
|
|
|
|
media_role_str = buf;
|
|
|
|
|
strsep(&buf, ",");
|
|
|
|
|
|
|
|
|
|
media_role = media_role_from_string(media_role_str);
|
|
|
|
|
if (media_role != MEDIA_ROLE_INVALID) {
|
|
|
|
|
media_roles |= MEDIA_ROLE_CAMERA;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pw_log_debug("Client specified unknown media role '%s'",
|
|
|
|
|
media_role_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-05 13:51:20 +02:00
|
|
|
free(buf_orig);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
return media_roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum media_role media_role_from_properties(const struct pw_properties *props)
|
|
|
|
|
{
|
|
|
|
|
const char *media_class_str;
|
|
|
|
|
const char *media_role_str;
|
|
|
|
|
|
|
|
|
|
media_class_str = pw_properties_get(props, "media.class");
|
|
|
|
|
media_role_str = pw_properties_get(props, "media.role");
|
|
|
|
|
|
|
|
|
|
if (media_class_str == NULL)
|
|
|
|
|
return MEDIA_ROLE_INVALID;
|
|
|
|
|
|
|
|
|
|
if (media_role_str == NULL)
|
|
|
|
|
return MEDIA_ROLE_INVALID;
|
|
|
|
|
|
|
|
|
|
if (strcmp(media_class_str, "Video/Source") != 0)
|
|
|
|
|
return MEDIA_ROLE_INVALID;
|
|
|
|
|
|
|
|
|
|
return media_role_from_string(media_role_str);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static void object_update(void *data)
|
2020-05-18 16:30:09 +02:00
|
|
|
{
|
2020-07-16 17:54:18 +02:00
|
|
|
struct client *client = data;
|
|
|
|
|
struct impl *impl = client->impl;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: client %p %08x", impl, client, client->obj->obj.changed);
|
|
|
|
|
|
|
|
|
|
if (client->obj->obj.avail & SM_CLIENT_CHANGE_MASK_INFO)
|
|
|
|
|
client_info_changed(client, client->obj->info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_object_events object_events = {
|
|
|
|
|
SM_VERSION_OBJECT_EVENTS,
|
|
|
|
|
.update = object_update
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int check_portal_managed(struct client *client)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = client->impl;
|
|
|
|
|
const char *str;
|
|
|
|
|
pid_t pid;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
if (impl->portal_pid == 0)
|
2020-07-16 17:54:18 +02:00
|
|
|
return -EBUSY;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->obj->obj.props == NULL)
|
|
|
|
|
return -ENOTSUP;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if ((str = pw_properties_get(client->obj->obj.props, PW_KEY_SEC_PID)) == NULL)
|
|
|
|
|
return -ENOENT;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
pid = atoi(str);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (pid == impl->portal_pid) {
|
|
|
|
|
client->portal_managed = true;
|
|
|
|
|
pw_log_info(NAME " %p: portal managed client %d added",
|
|
|
|
|
impl, client->id);
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
return 0;
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2020-07-16 17:54:18 +02:00
|
|
|
handle_client(struct impl *impl, struct sm_object *object)
|
2020-05-18 16:30:09 +02:00
|
|
|
{
|
2020-07-16 17:54:18 +02:00
|
|
|
struct client *client;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: client", impl);
|
|
|
|
|
|
|
|
|
|
client = sm_object_add_data(object, SESSION_KEY, sizeof(struct client));
|
|
|
|
|
client->obj = (struct sm_client*)object;
|
|
|
|
|
client->id = object->id;
|
|
|
|
|
client->impl = impl;
|
|
|
|
|
spa_list_append(&impl->client_list, &client->link);
|
|
|
|
|
|
|
|
|
|
client->obj->obj.mask |= SM_CLIENT_CHANGE_MASK_INFO;
|
|
|
|
|
sm_object_add_listener(&client->obj->obj, &client->listener, &object_events, client);
|
|
|
|
|
|
|
|
|
|
check_portal_managed(client);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
set_global_permissions(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct client *client = data;
|
|
|
|
|
struct impl *impl = client->impl;
|
2020-05-18 16:30:09 +02:00
|
|
|
struct pw_permission permissions[1];
|
2020-07-16 17:54:18 +02:00
|
|
|
const struct pw_properties *props;
|
2020-05-18 16:30:09 +02:00
|
|
|
int n_permissions = 0;
|
|
|
|
|
bool set_permission;
|
|
|
|
|
bool allowed = false;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
props = object->props;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_debug(NAME" %p: object %d type:%s", impl, object->id, object->type);
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Core) == 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
set_permission = true;
|
|
|
|
|
allowed = true;
|
2020-07-16 17:54:18 +02:00
|
|
|
} else if (strcmp(object->type, PW_TYPE_INTERFACE_Client) == 0) {
|
|
|
|
|
set_permission = allowed = object->id == client->id;
|
|
|
|
|
} else if (props) {
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Factory) == 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
const char *factory_name;
|
|
|
|
|
|
|
|
|
|
factory_name = pw_properties_get(props, "factory.name");
|
|
|
|
|
if (factory_name &&
|
|
|
|
|
strcmp(factory_name, "client-node") == 0) {
|
|
|
|
|
set_permission = true;
|
|
|
|
|
allowed = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
set_permission = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
else if (strcmp(object->type, PW_TYPE_INTERFACE_Node) == 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
enum media_role media_role;
|
|
|
|
|
|
|
|
|
|
media_role = media_role_from_properties(props);
|
|
|
|
|
|
|
|
|
|
if (media_role == MEDIA_ROLE_INVALID) {
|
|
|
|
|
set_permission = false;
|
|
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
else if (client->allowed_media_roles & media_role) {
|
2020-05-18 16:30:09 +02:00
|
|
|
set_permission = true;
|
|
|
|
|
allowed = true;
|
|
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
else if (client->media_roles & media_role) {
|
2020-05-18 16:30:09 +02:00
|
|
|
set_permission = true;
|
|
|
|
|
allowed = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
set_permission = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
set_permission = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
set_permission = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (set_permission) {
|
|
|
|
|
permissions[n_permissions++] =
|
2020-07-16 17:54:18 +02:00
|
|
|
PW_PERMISSION_INIT(object->id, allowed ? PW_PERM_R | PW_PERM_X : 0);
|
|
|
|
|
pw_log_debug(NAME" %p: object %d allowed:%d", impl, object->id, allowed);
|
|
|
|
|
pw_client_update_permissions(client->obj->obj.proxy,
|
|
|
|
|
n_permissions, permissions);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static void session_create(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: create global '%d'", impl, object->id);
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Client) == 0) {
|
|
|
|
|
handle_client(impl, object);
|
|
|
|
|
} else {
|
|
|
|
|
struct client *client;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(client, &impl->client_list, link) {
|
|
|
|
|
if (client->portal_managed &&
|
|
|
|
|
!client->is_portal)
|
|
|
|
|
set_global_permissions(client, object);
|
|
|
|
|
}
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
}
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static void destroy_client(struct impl *impl, struct client *client)
|
|
|
|
|
{
|
|
|
|
|
spa_list_remove(&client->link);
|
|
|
|
|
spa_hook_remove(&client->listener);
|
|
|
|
|
free(client->app_id);
|
|
|
|
|
sm_object_remove_data((struct sm_object*)client->obj, SESSION_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_remove(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
pw_log_debug(NAME " %p: remove global '%d'", impl, object->id);
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Client) == 0) {
|
|
|
|
|
struct client *client;
|
|
|
|
|
|
|
|
|
|
if ((client = sm_object_get_data(object, SESSION_KEY)) != NULL)
|
|
|
|
|
destroy_client(impl, client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct client *client;
|
|
|
|
|
|
|
|
|
|
spa_list_consume(client, &impl->client_list, link)
|
|
|
|
|
destroy_client(impl, client);
|
|
|
|
|
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
|
|
|
|
free(impl);
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
|
|
|
|
.create = session_create,
|
|
|
|
|
.remove = session_remove,
|
|
|
|
|
.destroy = session_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-18 16:30:09 +02:00
|
|
|
static bool
|
|
|
|
|
check_permission_allowed(DBusMessageIter *iter)
|
|
|
|
|
{
|
|
|
|
|
bool allowed = false;
|
|
|
|
|
|
|
|
|
|
while (dbus_message_iter_get_arg_type (iter) != DBUS_TYPE_INVALID) {
|
|
|
|
|
const char *permission_value;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(iter, &permission_value);
|
|
|
|
|
|
|
|
|
|
if (strcmp(permission_value, "yes") == 0) {
|
|
|
|
|
allowed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
dbus_message_iter_next(iter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allowed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static void do_permission_store_check(struct client *client)
|
2020-05-18 16:30:09 +02:00
|
|
|
{
|
2020-07-16 17:54:18 +02:00
|
|
|
struct impl *impl = client->impl;
|
2020-05-18 16:30:09 +02:00
|
|
|
DBusMessage *m = NULL, *r = NULL;
|
|
|
|
|
DBusError error;
|
|
|
|
|
DBusMessageIter msg_iter;
|
|
|
|
|
const char *table;
|
|
|
|
|
const char *id;
|
|
|
|
|
DBusMessageIter r_iter;
|
|
|
|
|
DBusMessageIter permissions_iter;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->app_id == NULL) {
|
2020-05-18 16:30:09 +02:00
|
|
|
pw_log_debug("Ignoring portal check for broken portal managed client %p",
|
|
|
|
|
client);
|
|
|
|
|
goto err_not_allowed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->media_roles == 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
pw_log_debug("Ignoring portal check for portal client %p with static permissions",
|
|
|
|
|
client);
|
2020-07-16 17:54:18 +02:00
|
|
|
sm_media_session_for_each_object(impl->session,
|
2020-05-18 16:30:09 +02:00
|
|
|
set_global_permissions,
|
2020-07-16 17:54:18 +02:00
|
|
|
client);
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (strcmp(client->app_id, "") == 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
pw_log_debug("Ignoring portal check for non-sandboxed portal client %p",
|
|
|
|
|
client);
|
2020-07-16 17:54:18 +02:00
|
|
|
client->allowed_media_roles = MEDIA_ROLE_ALL;
|
|
|
|
|
sm_media_session_for_each_object(impl->session,
|
2020-05-18 16:30:09 +02:00
|
|
|
set_global_permissions,
|
2020-07-16 17:54:18 +02:00
|
|
|
client);
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
client->allowed_media_roles = MEDIA_ROLE_NONE;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
|
|
m = dbus_message_new_method_call("org.freedesktop.impl.portal.PermissionStore",
|
|
|
|
|
"/org/freedesktop/impl/portal/PermissionStore",
|
|
|
|
|
"org.freedesktop.impl.portal.PermissionStore",
|
|
|
|
|
"Lookup");
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_init_append(m, &msg_iter);
|
|
|
|
|
table = "devices";
|
|
|
|
|
dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, &table);
|
|
|
|
|
id = "camera";
|
|
|
|
|
dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, &id);
|
|
|
|
|
|
|
|
|
|
if (!(r = dbus_connection_send_with_reply_and_block(impl->bus, m, -1, &error))) {
|
|
|
|
|
pw_log_error("Failed to call permission store: %s", error.message);
|
|
|
|
|
dbus_error_free(&error);
|
|
|
|
|
goto err_not_allowed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_unref(m);
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_init(r, &r_iter);
|
|
|
|
|
dbus_message_iter_recurse(&r_iter, &permissions_iter);
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&permissions_iter) !=
|
|
|
|
|
DBUS_TYPE_INVALID) {
|
|
|
|
|
DBusMessageIter permissions_entry_iter;
|
|
|
|
|
const char *app_id;
|
|
|
|
|
DBusMessageIter permission_values_iter;
|
|
|
|
|
bool camera_allowed;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&permissions_iter,
|
|
|
|
|
&permissions_entry_iter);
|
|
|
|
|
dbus_message_iter_get_basic(&permissions_entry_iter, &app_id);
|
|
|
|
|
|
|
|
|
|
pw_log_info("permissions %s", app_id);
|
2020-07-16 17:54:18 +02:00
|
|
|
if (strcmp(app_id, client->app_id) != 0) {
|
2020-05-18 16:30:09 +02:00
|
|
|
dbus_message_iter_next(&permissions_iter);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&permissions_entry_iter);
|
|
|
|
|
dbus_message_iter_recurse(&permissions_entry_iter,
|
|
|
|
|
&permission_values_iter);
|
|
|
|
|
|
|
|
|
|
camera_allowed = check_permission_allowed(&permission_values_iter);
|
|
|
|
|
pw_log_info("allowed %d", camera_allowed);
|
2020-07-16 17:54:18 +02:00
|
|
|
client->allowed_media_roles |=
|
2020-05-18 16:30:09 +02:00
|
|
|
camera_allowed ? MEDIA_ROLE_CAMERA : MEDIA_ROLE_NONE;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
sm_media_session_for_each_object(impl->session,
|
|
|
|
|
set_global_permissions,
|
|
|
|
|
client);
|
2020-05-18 16:30:09 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_unref(r);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
err_not_allowed:
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
static void client_info_changed(struct client *client, const struct pw_client_info *info)
|
2020-05-18 16:30:09 +02:00
|
|
|
{
|
2020-07-16 17:54:18 +02:00
|
|
|
struct impl *impl = client->impl;
|
|
|
|
|
const struct spa_dict *props;
|
2020-05-18 16:30:09 +02:00
|
|
|
const char *is_portal;
|
|
|
|
|
const char *app_id;
|
|
|
|
|
const char *media_roles;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (!client->portal_managed || client->is_portal)
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->setup_complete)
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if ((props = info->props) == NULL) {
|
2020-05-18 16:30:09 +02:00
|
|
|
pw_log_error("Portal managed client didn't have any properties");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
is_portal = spa_dict_lookup(props, "pipewire.access.portal.is_portal");
|
2020-05-18 16:30:09 +02:00
|
|
|
if (is_portal != NULL &&
|
|
|
|
|
(strcmp(is_portal, "yes") == 0 || pw_properties_parse_bool(is_portal))) {
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_debug(NAME " %p: client %d is the portal itself",
|
|
|
|
|
impl, client->id);
|
|
|
|
|
client->is_portal = true;
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
app_id = spa_dict_lookup(props, "pipewire.access.portal.app_id");
|
2020-05-18 16:30:09 +02:00
|
|
|
if (app_id == NULL) {
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_error(NAME" %p: Portal managed client %d didn't set app_id",
|
|
|
|
|
impl, client->id);
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2020-07-16 17:54:18 +02:00
|
|
|
media_roles = spa_dict_lookup(props, "pipewire.access.portal.media_roles");
|
2020-05-18 16:30:09 +02:00
|
|
|
if (media_roles == NULL) {
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_error(NAME" %p: Portal managed client %d didn't set media_roles",
|
|
|
|
|
impl, client->id);
|
2020-05-18 16:30:09 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
client->app_id = strdup(app_id);
|
|
|
|
|
client->media_roles = parse_media_roles(media_roles);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_info(NAME" %p: client %d with app_id '%s' set to portal access",
|
|
|
|
|
impl, client->id, client->app_id);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
do_permission_store_check(client);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
client->setup_complete = true;
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_portal_pid_received(DBusPendingCall *pending,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = user_data;
|
|
|
|
|
DBusMessage *m;
|
|
|
|
|
DBusError error;
|
|
|
|
|
uint32_t portal_pid = 0;
|
|
|
|
|
|
|
|
|
|
m = dbus_pending_call_steal_reply(pending);
|
|
|
|
|
dbus_pending_call_unref(pending);
|
|
|
|
|
impl->portal_pid_pending = NULL;
|
|
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
|
pw_log_error("Failed to receive portal pid");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
dbus_message_get_args(m, &error, DBUS_TYPE_UINT32, &portal_pid,
|
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
dbus_message_unref(m);
|
|
|
|
|
|
|
|
|
|
if (dbus_error_is_set(&error)) {
|
|
|
|
|
impl->portal_pid = 0;
|
2020-07-16 17:54:18 +02:00
|
|
|
} else {
|
|
|
|
|
struct client *client;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
pw_log_info("got portal pid %d", portal_pid);
|
2020-05-18 16:30:09 +02:00
|
|
|
impl->portal_pid = portal_pid;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
spa_list_for_each(client, &impl->client_list, link) {
|
|
|
|
|
if (client->portal_managed)
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
check_portal_managed(client);
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update_portal_pid(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
DBusMessage *m;
|
|
|
|
|
const char *name;
|
|
|
|
|
DBusPendingCall *pending;
|
|
|
|
|
|
|
|
|
|
impl->portal_pid = 0;
|
|
|
|
|
|
|
|
|
|
m = dbus_message_new_method_call("org.freedesktop.DBus",
|
|
|
|
|
"/",
|
|
|
|
|
"org.freedesktop.DBus",
|
|
|
|
|
"GetConnectionUnixProcessID");
|
|
|
|
|
|
|
|
|
|
name = "org.freedesktop.portal.Desktop";
|
|
|
|
|
dbus_message_append_args(m,
|
|
|
|
|
DBUS_TYPE_STRING, &name,
|
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
|
|
dbus_connection_send_with_reply(impl->bus, m, &pending, -1);
|
|
|
|
|
dbus_pending_call_set_notify(pending, on_portal_pid_received, impl, NULL);
|
|
|
|
|
if (impl->portal_pid_pending != NULL) {
|
|
|
|
|
dbus_pending_call_cancel(impl->portal_pid_pending);
|
|
|
|
|
dbus_pending_call_unref(impl->portal_pid_pending);
|
|
|
|
|
}
|
|
|
|
|
impl->portal_pid_pending = pending;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult name_owner_changed_handler(DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = user_data;
|
|
|
|
|
const char *name;
|
|
|
|
|
const char *old_owner;
|
|
|
|
|
const char *new_owner;
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_is_signal(message, "org.freedesktop.DBus",
|
|
|
|
|
"NameOwnerChanged"))
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args(message, NULL,
|
|
|
|
|
DBUS_TYPE_STRING, &name,
|
|
|
|
|
DBUS_TYPE_STRING, &old_owner,
|
|
|
|
|
DBUS_TYPE_STRING, &new_owner,
|
|
|
|
|
DBUS_TYPE_INVALID)) {
|
|
|
|
|
pw_log_error("Failed to get OwnerChanged args");
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "org.freedesktop.portal.Desktop") != 0)
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
|
|
|
|
if (strcmp(new_owner, "") == 0) {
|
|
|
|
|
impl->portal_pid = 0;
|
|
|
|
|
if (impl->portal_pid_pending != NULL) {
|
|
|
|
|
dbus_pending_call_cancel(impl->portal_pid_pending);
|
|
|
|
|
dbus_pending_call_unref(impl->portal_pid_pending);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
update_portal_pid(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult permission_store_changed_handler(DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = user_data;
|
2020-07-16 17:54:18 +02:00
|
|
|
struct client *client;
|
2020-05-18 16:30:09 +02:00
|
|
|
DBusMessageIter iter;
|
|
|
|
|
const char *table;
|
|
|
|
|
const char *id;
|
|
|
|
|
dbus_bool_t deleted;
|
|
|
|
|
DBusMessageIter permissions_iter;
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_is_signal(message, "org.freedesktop.impl.portal.PermissionStore",
|
|
|
|
|
"Changed"))
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
spa_list_for_each(client, &impl->client_list, link) {
|
|
|
|
|
if (!client->portal_managed)
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
client->allowed_media_roles = MEDIA_ROLE_NONE;
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_init(message, &iter);
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &table);
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &id);
|
|
|
|
|
|
|
|
|
|
if (strcmp(table, "devices") != 0 || strcmp(id, "camera") != 0)
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &deleted);
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
|
/* data variant (ignored) */
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
|
dbus_message_iter_recurse(&iter, &permissions_iter);
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&permissions_iter) !=
|
|
|
|
|
DBUS_TYPE_INVALID) {
|
|
|
|
|
DBusMessageIter permissions_entry_iter;
|
|
|
|
|
const char *app_id;
|
|
|
|
|
DBusMessageIter permission_values_iter;
|
|
|
|
|
bool camera_allowed;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&permissions_iter,
|
|
|
|
|
&permissions_entry_iter);
|
|
|
|
|
dbus_message_iter_get_basic(&permissions_entry_iter, &app_id);
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&permissions_entry_iter);
|
|
|
|
|
dbus_message_iter_recurse(&permissions_entry_iter,
|
|
|
|
|
&permission_values_iter);
|
|
|
|
|
|
|
|
|
|
camera_allowed = check_permission_allowed(&permission_values_iter);
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
spa_list_for_each(client, &impl->client_list, link) {
|
|
|
|
|
if (!client->portal_managed)
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->is_portal)
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (client->app_id == NULL ||
|
|
|
|
|
strcmp(client->app_id, app_id) != 0)
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (!(client->media_roles & MEDIA_ROLE_CAMERA))
|
2020-05-18 16:30:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (camera_allowed)
|
2020-07-16 17:54:18 +02:00
|
|
|
client->allowed_media_roles |= MEDIA_ROLE_CAMERA;
|
|
|
|
|
|
|
|
|
|
sm_media_session_for_each_object(impl->session,
|
2020-05-18 16:30:09 +02:00
|
|
|
set_global_permissions,
|
2020-07-16 17:54:18 +02:00
|
|
|
client);
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&permissions_iter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int init_dbus_connection(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
|
|
dbus_bus_add_match(impl->bus,
|
|
|
|
|
"type='signal',\
|
|
|
|
|
sender='org.freedesktop.DBus',\
|
|
|
|
|
interface='org.freedesktop.DBus',\
|
|
|
|
|
member='NameOwnerChanged'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set(&error)) {
|
|
|
|
|
pw_log_error("Failed to add name owner changed listener: %s",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free(&error);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_bus_add_match(impl->bus,
|
|
|
|
|
"type='signal',\
|
|
|
|
|
sender='org.freedesktop.impl.portal.PermissionStore',\
|
|
|
|
|
interface='org.freedesktop.impl.portal.PermissionStore',\
|
|
|
|
|
member='Changed'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set(&error)) {
|
|
|
|
|
pw_log_error("Failed to add permission store changed listener: %s",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free(&error);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_connection_add_filter(impl->bus, name_owner_changed_handler,
|
|
|
|
|
impl, NULL);
|
|
|
|
|
dbus_connection_add_filter(impl->bus, permission_store_changed_handler,
|
|
|
|
|
impl, NULL);
|
|
|
|
|
update_portal_pid(impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
int sm_access_portal_start(struct sm_media_session *session)
|
2020-05-18 16:30:09 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
2020-07-16 17:54:18 +02:00
|
|
|
int res;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
2020-07-16 17:54:18 +02:00
|
|
|
return -errno;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
spa_list_init(&impl->client_list);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
impl->session = session;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if (session->dbus_connection)
|
|
|
|
|
impl->bus = spa_dbus_connection_get(session->dbus_connection);
|
|
|
|
|
if (impl->bus == NULL)
|
|
|
|
|
pw_log_warn("no dbus connection, portal access disabled");
|
|
|
|
|
else
|
|
|
|
|
pw_log_debug("got dbus connection %p", impl->bus);
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
if ((res = init_dbus_connection(impl)) < 0)
|
|
|
|
|
goto error_free;
|
2020-05-18 16:30:09 +02:00
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
sm_media_session_add_listener(impl->session,
|
|
|
|
|
&impl->listener,
|
|
|
|
|
&session_events, impl);
|
2020-05-18 16:30:09 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2020-07-16 17:54:18 +02:00
|
|
|
error_free:
|
2020-05-18 16:30:09 +02:00
|
|
|
free(impl);
|
2020-07-16 17:54:18 +02:00
|
|
|
return res;
|
2020-05-18 16:30:09 +02:00
|
|
|
}
|