summaryrefslogtreecommitdiffstats
path: root/src/sbus/server/sbus_server_handler.c
blob: d4e454780a29e321b322dced4b4c0ec7110233ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>
        Stephen Gallagher <sgallagh@redhat.com>
        Simo Sorce <ssorce@redhat.com>

    Copyright (C) 2017 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include <string.h>
#include <tevent.h>
#include <talloc.h>
#include <dbus/dbus.h>

#include "util/util.h"
#include "util/sss_ptr_hash.h"
#include "sbus/sbus_private.h"

static DBusHandlerResult
sbus_server_resend_message(struct sbus_server *server,
                           struct sbus_connection *conn,
                           DBusMessage *message,
                           const char *destination)
{
    struct sbus_connection *destconn;

    destconn = sbus_server_find_connection(server, destination);
    if (destconn == NULL) {
        DEBUG(SSSDBG_TRACE_ALL, "Trying to send a message to an unknown "
              "destination: %s\n", destination);
        sbus_reply_error(conn, message, DBUS_ERROR_SERVICE_UNKNOWN, destination);
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    /* Message is unreferenced by libdbus. */
    dbus_connection_send(destconn->connection, message, NULL);
    return DBUS_HANDLER_RESULT_HANDLED;
}

DBusHandlerResult
sbus_server_route_signal(struct sbus_server *server,
                         struct sbus_connection *conn,
                         DBusMessage *message,
                         const char *destination)
{
    errno_t ret;

    /* If a destination is set (unusual but possible) we simply send the
     * signal to its desired destination. */
    if (destination != NULL) {
        return sbus_server_resend_message(server, conn, message, destination);
    }

    /* Otherwise we need to send it to all connections that listen to it. */
    ret = sbus_server_matchmaker(server, conn, NULL, message);
    if (ret == EOK) {
        return DBUS_HANDLER_RESULT_HANDLED;
    } else if (ret != ENOENT) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to send signal [%d]: %s\n",
              ret, sss_strerror(ret));
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusHandlerResult
sbus_server_route_message(struct sbus_server *server,
                          struct sbus_connection *conn,
                          DBusMessage *message,
                          const char *destination)
{
    if (strcmp(destination, DBUS_SERVICE_DBUS) == 0) {
        /* This message is addressed to D-Bus service. We must reply to it. */
        return sbus_router_filter(conn, server->router, message);
    }

    return sbus_server_resend_message(server, conn, message, destination);
}

static bool
sbus_server_check_access(struct sbus_connection *conn,
                         DBusMessage *message)
{
    const char *destination;
    const char *interface;
    const char *member;
    int type;

    /* Connection must first obtain its unique name through Hello method. */
    if (conn->unique_name != NULL) {
        return true;
    }

    destination = dbus_message_get_destination(message);
    interface = dbus_message_get_interface(message);
    member = dbus_message_get_member(message);
    type = dbus_message_get_type(message);

    if (type != DBUS_MESSAGE_TYPE_METHOD_CALL) {
        return false;
    }

    if (strcmp(destination, DBUS_SERVICE_DBUS) != 0) {
        return false;
    }

    if (strcmp(interface, DBUS_INTERFACE_DBUS) != 0) {
        return false;
    }

    if (strcmp(member, "Hello") != 0) {
        return false;
    }

    return true;
}

DBusHandlerResult
sbus_server_filter(DBusConnection *dbus_conn,
                   DBusMessage *message,
                   void *handler_data)
{
    struct sbus_server *server;
    struct sbus_connection *conn;
    const char *destination;
    const char *sender;
    dbus_bool_t dbret;
    int type;

    server = talloc_get_type(handler_data, struct sbus_server);

    /* We can't really send signals when the server is being destroyed. */
    if (server == NULL || server->disconnecting) {
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    conn = dbus_connection_get_data(dbus_conn, server->data_slot);
    if (conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unknown connection!\n");
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    if (!sbus_server_check_access(conn, message)) {
        sbus_reply_error(conn, message, DBUS_ERROR_ACCESS_DENIED,
                         "Connection did not call org.freedesktop.DBus.Hello");
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    /* We always require a sender but it may not be assigned yet. We prefer
     * well known name if set. */
    sender = sbus_connection_get_name(conn);
    dbret = dbus_message_set_sender(message, sender);
    if (!dbret) {
        sbus_reply_error(conn, message, DBUS_ERROR_FAILED,
                         "Unable to set sender");
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    /* Set sender may reallocate internal fields so this needs to be read
     * after we call dbus_message_set_sender(). */
    destination = dbus_message_get_destination(message);
    type = dbus_message_get_type(message);

    if (type == DBUS_MESSAGE_TYPE_SIGNAL) {
        return sbus_server_route_signal(server, conn, message, destination);
    }

    /* We do not allow method calls without destination. */
    if (destination == NULL) {
        sbus_reply_error(conn, message, DBUS_ERROR_FAILED,
                         "Non-signal multicast calls are not supported");
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    return sbus_server_route_message(server, conn, message, destination);
}