summaryrefslogtreecommitdiffstats
path: root/src/sbus/connection/sbus_dbus.c
blob: 69b18bc42c07b0f6210969b41599a701e9ada743 (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
/*
    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 <talloc.h>
#include <dbus/dbus.h>

#include "util/util.h"
#include "sbus/connection/sbus_dbus_private.h"

static errno_t
sbus_dbus_request_name(DBusConnection *dbus_conn, const char *name)
{
    DBusError dbus_error;
    errno_t ret;
    int flags;
    int dbret;

    dbus_error_init(&dbus_error);

    /* We are interested only in being the primary owner of this name. */
    flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;

    dbret = dbus_bus_request_name(dbus_conn, name, flags, &dbus_error);
    if (dbret == -1) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to request name '%s' on the system"
              " bus [%s]: %s\n", name, dbus_error.name, dbus_error.message);
        if (dbus_error_has_name(&dbus_error, DBUS_ERROR_ACCESS_DENIED)) {
            DEBUG(SSSDBG_FATAL_FAILURE,
                  "Access denied - check dbus service configuration.\n");
            sss_log(SSS_LOG_CRIT, "SSSD dbus service can't acquire bus name"
                    " - check dbus service configuration.");
        }
        ret = EIO;
        goto done;
    } else if (dbret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to request name on the "
              "system bus [%d]\n", dbret);
        ret = EIO;
        goto done;
    }

    ret = EOK;

done:
    dbus_error_free(&dbus_error);

    return ret;
}

DBusConnection *
sbus_dbus_connect_bus(DBusBusType bus, const char *name)
{
    DBusConnection *dbus_conn;
    DBusError dbus_error;
    const char *busname = "not-set";
    errno_t ret;

    switch (bus) {
    case DBUS_BUS_SESSION:
        busname = "session";
        break;
    case DBUS_BUS_SYSTEM:
        busname = "system";
        break;
    case DBUS_BUS_STARTER:
        busname = "starter";
        break;
    }

    dbus_error_init(&dbus_error);

    /* Connect to the system bus. */
    dbus_conn = dbus_bus_get(bus, &dbus_error);
    if (dbus_conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to connect to %s bus [%s]: %s\n",
              busname, dbus_error.name, dbus_error.message);
        ret = EIO;
        goto done;
    }

    if (name != NULL) {
        /* Request a well-known name. */
        ret = sbus_dbus_request_name(dbus_conn, name);
        if (ret != EOK) {
            dbus_connection_unref(dbus_conn);
            goto done;
        }
    }

    if (name == NULL) {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as anonymous\n", busname);
    } else {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as %s\n", busname, name);
    }

    ret = EOK;

done:
    dbus_error_free(&dbus_error);

    if (ret != EOK) {
        return NULL;
    }

    return dbus_conn;
}

DBusConnection *
sbus_dbus_connect_address(const char *address, const char *name, bool init)
{
    DBusConnection *dbus_conn;
    DBusError dbus_error;
    dbus_bool_t dbret;
    errno_t ret;

    if (address == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Can not connect to an empty address!\n");
        return NULL;
    }

    dbus_error_init(&dbus_error);

    dbus_conn = dbus_connection_open(address, &dbus_error);
    if (dbus_conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to connect to %s [%s]: %s\n",
              address, dbus_error.name, dbus_error.message);
        ret = EIO;
        goto done;
    }

    if (!init) {
        ret = EOK;
        goto done;
    }

    dbret = dbus_bus_register(dbus_conn, &dbus_error);
    if (!dbret) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register to %s [%s]: %s\n",
              address, dbus_error.name, dbus_error.message);
        ret = EIO;
        goto done;
    }

    /* Request a well-known name. */
    if (name != NULL) {
        ret = sbus_dbus_request_name(dbus_conn, name);
        if (ret != EOK) {
            goto done;
        }
    }

    if (name == NULL) {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as anonymous\n", address);
    } else {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as %s\n", address, name);
    }

    ret = EOK;

done:
    dbus_error_free(&dbus_error);
    if (ret != EOK && dbus_conn != NULL) {
        dbus_connection_unref(dbus_conn);
        dbus_conn = NULL;
    }

    return dbus_conn;
}