summaryrefslogtreecommitdiffstats
path: root/src/sss_iface/sss_iface.c
blob: 5711f2dd3cf89bfa89654955277be387adc2c12c (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
191
192
193
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2018 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 <unistd.h>
#include <sys/stat.h>

#include "util/util.h"
#include "sbus/sbus.h"
#include "sbus/sbus_opath.h"
#include "sss_iface/sss_iface_async.h"

char *
sss_iface_domain_address(TALLOC_CTX *mem_ctx,
                         struct sss_domain_info *domain)
{
    struct sss_domain_info *head;

    /* There is only one bus that belongs to the top level domain. */
    head = get_domains_head(domain);

    return talloc_asprintf(mem_ctx, SSS_BACKEND_ADDRESS, head->name);
}

char *
sss_iface_domain_bus(TALLOC_CTX *mem_ctx,
                     struct sss_domain_info *domain)
{
    struct sss_domain_info *head;
    char *safe_name;
    char *bus_name;


    /* There is only one bus that belongs to the top level domain. */
    head = get_domains_head(domain);

    safe_name = sbus_opath_escape(mem_ctx, head->name);
    if (safe_name == NULL) {
        return NULL;
    }

    /* Parts of bus names must not start with digit thus we concatenate
     * the name with underscore instead of period. */
    bus_name = talloc_asprintf(mem_ctx, "sssd.domain_%s", safe_name);
    talloc_free(safe_name);

    return bus_name;
}

char *
sss_iface_proxy_bus(TALLOC_CTX *mem_ctx,
                    uint32_t id)
{
    /* Parts of bus names must not start with digit thus we concatenate
     * the name with underscore instead of period. */
    return talloc_asprintf(mem_ctx, "sssd.proxy_%"PRIu32, id);
}

errno_t
sss_iface_connect_address(TALLOC_CTX *mem_ctx,
                          struct tevent_context *ev,
                          const char *conn_name,
                          const char *address,
                          time_t *last_request_time,
                          struct sbus_connection **_conn)
{
    struct sbus_connection *conn;
    const char *filename;
    errno_t ret;
    uid_t check_uid;
    gid_t check_gid;

    if (address == NULL) {
        return EINVAL;
    }

    filename = strchr(address, '/');
    if (filename == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Unexpected dbus address [%s].\n", address);
        return EIO;
    }

    check_uid = geteuid();
    check_gid = getegid();

    /* Ignore ownership checks when the server runs as root. This is the
     * case when privileged monitor is setting up sockets for unprivileged
     * responders */
    if (check_uid == 0) check_uid = -1;
    if (check_gid == 0) check_gid = -1;

    ret = check_file(filename, check_uid, check_gid,
                     S_IFSOCK|S_IRUSR|S_IWUSR, 0, NULL, true);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "check_file failed for [%s].\n", filename);
        return EIO;
    }

    conn = sbus_connect_private(mem_ctx, ev, address,
                                conn_name, last_request_time);
    if (conn == NULL) { /* most probably sbus_dbus_connect_address() failed */
        return EFAULT;
    }

    *_conn = conn;

    return EOK;
}

static void
sss_monitor_service_init_done(struct tevent_req *req);

errno_t
sss_monitor_service_init(TALLOC_CTX *mem_ctx,
                         struct tevent_context *ev,
                         const char *conn_name,
                         const char *svc_name,
                         uint16_t svc_version,
                         uint16_t svc_type,
                         time_t *last_request_time,
                         struct sbus_connection **_conn)
{
    struct sbus_connection *conn;
    struct tevent_req *req;
    errno_t ret;

    ret = sss_iface_connect_address(mem_ctx, ev, conn_name,
                                    SSS_MONITOR_ADDRESS,
                                    last_request_time, &conn);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to connect to monitor sbus server "
              "[%d]: %s\n", ret, sss_strerror(ret));
        return ret;
    }

    req = sbus_call_monitor_RegisterService_send(conn, conn, SSS_BUS_MONITOR,
                                                 SSS_BUS_PATH, svc_name,
                                                 svc_version, svc_type);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        ret = ENOMEM;
        goto done;
    }

    tevent_req_set_callback(req, sss_monitor_service_init_done, conn);

    *_conn = conn;

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(conn);
    }

    return ret;
}

static void
sss_monitor_service_init_done(struct tevent_req *req)
{
    uint16_t version;
    errno_t ret;

    ret = sbus_call_monitor_RegisterService_recv(req, &version);
    talloc_zfree(req);

    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register client in monitor "
              "[%d]: %s\n", ret, sss_strerror(ret));
        return;
    }

    DEBUG(SSSDBG_CONF_SETTINGS, "Got id ack and version (%d) from Monitor\n",
          version);
}