bus-util.c revision 4d7859d173282e16bb75254c2b4ec14a915ef30b
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/socket.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "sd-event.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "sd-bus.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "macro.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "def.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "bus-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int quit_callback(sd_bus *bus, sd_bus_message *m, void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_event *e = userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_event_request_quit(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_free_ char *match = NULL;
6a4e0b13473baed129522310c39f3bb70f46ed42Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = asprintf(&match, "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameLost',arg0='%s'", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_match(bus, match, quit_callback, e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_release_name(bus, name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r != SD_BUS_NAME_RELEASED)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EIO;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool exiting = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (;;) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_event_get_state(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r == SD_EVENT_FINISHED)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_event_run(e, exiting ? (uint64_t) -1 : timeout);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r == 0 && !exiting) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = bus_async_unregister_and_quit(e, bus, name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering exiting = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_property_get_tristate(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *path,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *interface,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *property,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message *reply,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_error *error,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int *tristate = userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_append(reply, "b", *tristate > 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
8aec412ff697bc14995746953912ca6fdf2c9ba8Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_verify_polkit(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message *m,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *action,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool interactive,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool *_challenge,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_error *e) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *sender;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uid_t uid;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(action);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sender = sd_bus_message_get_sender(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!sender)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EBADMSG;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_get_owner_uid(bus, sender, &uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (uid == 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned authorized = false, challenge = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_call_method(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.PolicyKit1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/PolicyKit1/Authority",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.PolicyKit1.Authority",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "CheckAuthorization",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering e,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &reply,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "(sa{sv})sa{ss}us",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "system-bus-name", 1, "name", "s", sender,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering action,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering 0,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering interactive ? 1 : 0,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Treat no PK available as access denied */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_error_free(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EACCES;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_read(reply, "(bb)", &authorized, &challenge);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (authorized)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (_challenge) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *_challenge = challenge;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering#endif
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering return -EACCES;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering}
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringtypedef struct AsyncPolkitQuery {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message *request, *reply;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_handler_t callback;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint64_t serial;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering} AsyncPolkitQuery;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int async_polkit_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering AsyncPolkitQuery *q = userdata;
a3e7f417d72ba3251fd6b3a228a2721a4b725a03Zbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q->reply = sd_bus_message_ref(reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q->serial = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = sd_bus_message_ref(q->request);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_rewind(m, true);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = q->callback(bus, m, q->userdata);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void async_polkit_query_free(sd_bus *b, AsyncPolkitQuery *q) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!q)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (q->serial > 0 && b)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_send_with_reply_cancel(b, q->serial);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a3e7f417d72ba3251fd6b3a228a2721a4b725a03Zbigniew Jędrzejewski-Szmek sd_bus_message_unref(q->request);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_unref(q->reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_verify_polkit_async(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Hashmap **registry,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message *m,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *action,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool interactive,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_error *error,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_handler_t callback,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *pk = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering AsyncPolkitQuery *q;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *sender;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uid_t uid;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(registry);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(action);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q = hashmap_remove(*registry, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (q) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned authorized, challenge;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* This is the second invocation of this function, and
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * there's already a response from polkit, let's
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * process it */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(q->reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sd_bus_message_is_method_error(q->reply, NULL)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const sd_bus_error *e;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Treat no PK available as access denied */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sd_bus_message_is_method_error(q->reply, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering async_polkit_query_free(bus, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EACCES;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering e = sd_bus_message_get_error(q->reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_error_copy(error, e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_error_get_errno(e);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering async_polkit_query_free(bus, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r >= 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering async_polkit_query_free(bus, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (authorized)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EACCES;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sender = sd_bus_message_get_sender(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!sender)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EBADMSG;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_get_owner_uid(bus, sender, &uid);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (uid == 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_ensure_allocated(registry, trivial_hash_func, trivial_compare_func);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_new_method_call(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.PolicyKit1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/PolicyKit1/Authority",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.PolicyKit1.Authority",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "CheckAuthorization",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &pk);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_append(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering pk,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "(sa{sv})sa{ss}us",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "system-bus-name", 1, "name", "s", sender,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering action,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering 0,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering interactive ? 1 : 0,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q = new0(AsyncPolkitQuery, 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!q)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -ENOMEM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q->request = sd_bus_message_ref(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q->callback = callback;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering q->userdata = userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_put(*registry, m, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering async_polkit_query_free(bus, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_send_with_reply(bus, pk, async_polkit_callback, q, 0, &q->serial);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EACCES;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid bus_verify_polkit_async_registry_free(sd_bus *bus, Hashmap *registry) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef ENABLE_POLKIT
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering AsyncPolkitQuery *q;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((q = hashmap_steal_first(registry)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering async_polkit_query_free(bus, q);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_free(registry);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int bus_check_peercred(sd_bus *c) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct ucred ucred;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering socklen_t l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int fd;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = sd_bus_get_fd(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (fd < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return fd;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering l = sizeof(struct ucred);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (l != sizeof(struct ucred))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -E2BIG;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (ucred.uid != 0 && ucred.uid != geteuid())
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EPERM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_open_system_systemd(sd_bus **_bus) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(_bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (geteuid() != 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return sd_bus_open_system(_bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* If we are root, then let's talk directly to the system
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * instance, instead of going via the bus */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_new(&bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_start(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = bus_check_peercred(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *_bus = bus;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint bus_generic_print_property(const char *name, sd_bus_message *property, bool all) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char type;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *contents;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(property);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_peek_type(property, &type, &contents);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (type) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_STRING: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *s;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &s);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (all || !isempty(s))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%s\n", name, s);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_BOOLEAN: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool b;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &b);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%s\n", name, yes_no(b));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_UINT64: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint64_t u;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Yes, heuristics! But we can change this check
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * should it turn out to not be sufficient */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (endswith(name, "Timestamp")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering t = format_timestamp(timestamp, sizeof(timestamp), u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (t || all)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%s\n", name, strempty(t));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (strstr(name, "USec")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char timespan[FORMAT_TIMESPAN_MAX];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u, 0));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%llu\n", name, (unsigned long long) u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_UINT32: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint32_t u;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strstr(name, "UMask") || strstr(name, "Mode"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%04o\n", name, u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%u\n", name, (unsigned) u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_INT32: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int32_t i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%i\n", name, (int) i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_DOUBLE: {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering double d;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_basic(property, type, &d);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=%g\n", name, d);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SD_BUS_TYPE_ARRAY:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (streq(contents, "s")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool space = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char tp;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *cnt;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_peek_type(property, &tp, &cnt);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (all || cnt) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while(sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s%s", space ? " " : "", str);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering space = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering puts("");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_exit_container(property);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
76e665855edef5b7103cb09d114377d477bfae02Lennart Poettering return 1;
76e665855edef5b7103cb09d114377d477bfae02Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(contents, "y")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const uint8_t *u;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t n;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (all || n > 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned int i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s=", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (i = 0; i < n; i++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%02x", u[i]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering puts("");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(contents, "u")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering uint32_t *u;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t n;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering if (all || n > 0) {
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering unsigned int i;
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering printf("%s=", name);
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering for (i = 0; i < n; i++)
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering printf("%08x", u[i]);
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering puts("");
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering }
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering return 1;
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering }
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering break;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering }
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering return 0;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering}
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poetteringint bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus) {
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering int r;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert(transport >= 0);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert(transport < _BUS_TRANSPORT_MAX);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert(bus);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering switch (transport) {
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering case BUS_TRANSPORT_LOCAL:
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering if (user)
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering r = sd_bus_open_user(bus);
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_open_system(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case BUS_TRANSPORT_REMOTE:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_open_system_remote(host, bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case BUS_TRANSPORT_CONTAINER:
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = sd_bus_open_system_container(host, bus);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering default:
554604b3073467af75dc94fac9e2343148603289Lennart Poettering assert_not_reached("Hmm, unknown transport type.");
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering return r;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering}
554604b3073467af75dc94fac9e2343148603289Lennart Poettering