summaryrefslogtreecommitdiffstats
path: root/src/providers/proxy/proxy_client.c
blob: 74957caeec5bf50b5cb959d6f5b8ec1ca9ecba37 (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
/*
    SSSD

    proxy_init.c

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2010 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 "util/util.h"
#include "providers/proxy/proxy_iface_generated.h"
#include "providers/proxy/proxy.h"

struct proxy_client {
    struct proxy_auth_ctx *proxy_auth_ctx;
    struct sbus_connection *conn;
    struct tevent_timer *timeout;
    bool initialized;
};

static int proxy_client_register(struct sbus_request *sbus_req,
                                 void *data,
                                 uint32_t cli_id)
{
    struct sbus_connection *conn;
    struct proxy_client *proxy_cli;
    int hret;
    hash_key_t key;
    hash_value_t value;
    struct tevent_req *req;
    struct proxy_child_ctx *child_ctx;
    struct pc_init_ctx *init_ctx;

    conn = sbus_req->conn;
    proxy_cli = talloc_get_type(data, struct proxy_client);
    if (proxy_cli == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Connection holds no valid init data\n");
        return EINVAL;
    }

    /* First thing, cancel the timeout */
    DEBUG(SSSDBG_CONF_SETTINGS,
          "Cancel proxy client ID timeout [%p]\n", proxy_cli->timeout);
    talloc_zfree(proxy_cli->timeout);

    DEBUG(SSSDBG_FUNC_DATA, "Proxy client [%"PRIu32"] connected\n", cli_id);

    /* Check the hash table */
    key.type = HASH_KEY_ULONG;
    key.ul = cli_id;
    if (!hash_has_key(proxy_cli->proxy_auth_ctx->request_table, &key)) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Unknown child ID. Killing the connection\n");
        sbus_disconnect(proxy_cli->conn);
        return EIO;
    }

    iface_proxy_client_Register_finish(sbus_req);

    hret = hash_lookup(proxy_cli->proxy_auth_ctx->request_table, &key, &value);
    if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Hash error [%d]: %s\n", hret, hash_error_string(hret));
        sbus_disconnect(conn);
        return EIO;
    }

    /* Signal that the child is up and ready to receive the request */
    req = talloc_get_type(value.ptr, struct tevent_req);
    child_ctx = tevent_req_data(req, struct proxy_child_ctx);

    if (!child_ctx->running) {
        /* This should hopefully be impossible, but protect
         * against it anyway. If we're not marked running, then
         * the init_req will be NULL below and things will
         * break.
         */
        DEBUG(SSSDBG_CRIT_FAILURE, "Client connection from a request "
              "that's not marked as running\n");
        return EIO;
    }

    init_ctx = tevent_req_data(child_ctx->init_req, struct pc_init_ctx);
    init_ctx->conn = conn;
    tevent_req_done(child_ctx->init_req);
    child_ctx->init_req = NULL;

    return EOK;
}

static void proxy_client_timeout(struct tevent_context *ev,
                                 struct tevent_timer *te,
                                 struct timeval t,
                                 void *ptr)
{
    struct proxy_client *proxy_cli;

    DEBUG(SSSDBG_OP_FAILURE,
          "Client timed out before Identification [%p]!\n", te);

    proxy_cli = talloc_get_type(ptr, struct proxy_client);

    sbus_disconnect(proxy_cli->conn);
    talloc_zfree(proxy_cli);

    /* If we time out here, we will also time out to
     * pc_init_timeout(), so we'll finish the request
     * there.
     */
}

int proxy_client_init(struct sbus_connection *conn, void *data)
{
    struct proxy_auth_ctx *auth_ctx;
    struct proxy_client *proxy_cli;
    struct timeval tv;
    errno_t ret;

    static struct iface_proxy_client iface_proxy_client = {
        { &iface_proxy_client_meta, 0 },

        .Register = proxy_client_register,
    };

    auth_ctx = talloc_get_type(data, struct proxy_auth_ctx);

    /* When connection is lost we also free the client. */
    proxy_cli = talloc_zero(conn, struct proxy_client);
    if (proxy_cli == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory, killing connection.\n");
        talloc_free(conn);
        return ENOMEM;
    }

    proxy_cli->proxy_auth_ctx = auth_ctx;
    proxy_cli->conn = conn;
    proxy_cli->initialized = false;

    /* Setup timeout in case client fails to register himself in time. */
    tv = tevent_timeval_current_ofs(5, 0);
    proxy_cli->timeout = tevent_add_timer(auth_ctx->be->ev, proxy_cli, tv,
                                          proxy_client_timeout, proxy_cli);
    if (proxy_cli->timeout == NULL) {
        /* Connection is closed in the caller. */
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory, killing connection\n");
        return ENOMEM;
    }

    DEBUG(SSSDBG_CONF_SETTINGS,
          "Set-up proxy client ID timeout [%p]\n", proxy_cli->timeout);

    /* Setup D-Bus interfaces and methods. */
    ret = sbus_conn_register_iface(conn, &iface_proxy_client.vtable,
                                   PROXY_CHILD_PATH, proxy_cli);
    if (ret != EOK) {
        /* Connection is closed in the caller. */
        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to register D-Bus interface, "
              "killing connection [%d]: %s\n", ret, sss_strerror(ret));
        return ret;
    }

    return ret;
}