summaryrefslogtreecommitdiff
path: root/server/providers/data_provider.c
blob: aacb09ef89eb52ec5aa88c8d8d1b467bc6850f29 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
   SSSD

   Data Provider

   Copyright (C) Simo Sorce <ssorce@redhat.com>	2008

   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 <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include "ldb.h"
#include "ldb_errors.h"
#include "util/util.h"
#include "confdb/confdb.h"
#include "dbus/dbus.h"
#include "sbus/sssd_dbus.h"
#include "sbus_interfaces.h"
#include "util/btreemap.h"
#include "data_provider.h"
#include "util/service_helpers.h"

static int provide_identity(DBusMessage *message, void *data, DBusMessage **r);
static int reply_ping(DBusMessage *message, void *data, DBusMessage **r);

struct sbus_method mon_sbus_methods[] = {
    {SERVICE_METHOD_IDENTITY, provide_identity},
    {SERVICE_METHOD_PING, reply_ping},
    {NULL, NULL}
};

static int provide_identity(DBusMessage *message, void *data, DBusMessage **r)
{
    dbus_uint16_t version = DATA_PROVIDER_VERSION;
    const char *name = DATA_PROVIDER_SERVICE_NAME;
    DBusMessage *reply;
    dbus_bool_t ret;

    reply = dbus_message_new_method_return(message);
    ret = dbus_message_append_args(reply,
                                   DBUS_TYPE_STRING, &name,
                                   DBUS_TYPE_UINT16, &version,
                                   DBUS_TYPE_INVALID);
    if (!ret) {
        return EIO;
    }

    *r = reply;
    return EOK;
}

static int reply_ping(DBusMessage *message, void *data, DBusMessage **r)
{
    DBusMessage *reply;
    dbus_bool_t ret;

    reply = dbus_message_new_method_return(message);
    ret = dbus_message_append_args(reply, DBUS_TYPE_INVALID);
    if (!ret) {
        return EIO;
    }

    *r = reply;
    return EOK;
}

static int dp_monitor_init(struct dp_ctx *dpctx)
{
    struct service_sbus_ctx *ss_ctx;

    /* Set up SBUS connection to the monitor */
    ss_ctx = sssd_service_sbus_init(dpctx, dpctx->ev, dpctx->cdb,
                                    provide_identity,
                                    reply_ping);
    if (ss_ctx == NULL) {
        DEBUG(0, ("Could not initialize D-BUS.\n"));
        return ENOMEM;
    }

    /* Set up DP-specific listeners */
    /* None currently used */

    dpctx->ss_ctx = ss_ctx;

    return EOK;
}

static int dp_db_init(struct dp_ctx *dpctx)
{
    TALLOC_CTX *ctx;
    char *ldb_file;
    char *default_db_file;
    int ret;

    ctx = talloc_new(dpctx);
    if(ctx == NULL) {
        return ENOMEM;
    }

    default_db_file = talloc_asprintf(ctx, "%s/%s", DB_PATH, DATA_PROVIDER_DB_FILE);
    if (default_db_file == NULL) {
        talloc_free(ctx);
        return ENOMEM;
    }

    ret = confdb_get_string(dpctx->cdb, ctx,
                            DATA_PROVIDER_DB_CONF_SEC, "ldbFile",
                            default_db_file, &ldb_file);
    if (ret != EOK) {
        talloc_free(ctx);
        return ret;
    }

    dpctx->ldb = ldb_init(ctx, dpctx->ev);
    if (!dpctx->ldb) {
        talloc_free(ctx);
        return EIO;
    }

    ret = ldb_connect(dpctx->ldb, ldb_file, 0, NULL);
    if (ret != LDB_SUCCESS) {
        talloc_free(ctx);
        return EIO;
    }

    talloc_steal(dpctx,dpctx->ldb);
    talloc_free(ctx);

    return EOK;
}

static int check_online(DBusMessage *message, void *data, DBusMessage **r);

struct sbus_method dp_sbus_methods[] = {
    { DP_METHOD_CHECK_ONLINE, check_online },
    { NULL, NULL }
};

static int dbus_dp_init(struct sbus_conn_ctx *conn_ctx, void *data)
{
    struct dp_ctx *dpctx;
    struct dp_client *dpcli;
    DBusConnection *conn;
    DBusError dbus_error;

    dpctx = talloc_get_type(data, struct dp_ctx);
    conn = sbus_get_connection(conn_ctx);
    dbus_error_init(&dbus_error);

    /* hang off this memory to the connection so that when the connection
     * is freed we can potentially call a destructor */

    dpcli = talloc(conn_ctx, struct dp_client);
    if (!dpcli) {
        DEBUG(0,("Out of memory?!\n"));
        talloc_free(conn_ctx);
        return ENOMEM;
    }
    dpcli->dpctx = dpctx;
    dpcli->conn = conn;

    /* Attach the client context to the connection context, so that it is
     * always available when we need to manage the connection. */
    sbus_conn_set_private_data(conn_ctx, dpcli);

    return EOK;
}

/* monitor_dbus_init
 * Set up the monitor service as a D-BUS Server */
static int dp_srv_init(struct dp_ctx *dpctx)
{
    TALLOC_CTX *tmp_ctx;
    struct sbus_method_ctx *sd_ctx;
    char *dpbus_address;
    char *default_dp_address;
    int ret;

    tmp_ctx = talloc_new(dpctx);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    DEBUG(3, ("Initializing Data Provider D-BUS Server\n"));
    default_dp_address = talloc_asprintf(tmp_ctx, "unix:path=%s/%s",
                                         PIPE_PATH, DATA_PROVIDER_PIPE);
    if (default_dp_address == NULL) {
        ret = ENOMEM;
        goto done;
    }

    ret = confdb_get_string(dpctx->cdb, tmp_ctx,
                            "config/services/dataprovider", "dpbusAddress",
                            default_dp_address, &dpbus_address);
    if (ret != EOK) goto done;

    sd_ctx = talloc_zero(tmp_ctx, struct sbus_method_ctx);
    if (!sd_ctx) {
        ret = ENOMEM;
        goto done;
    }

    /* Set up globally-available D-BUS methods */
    sd_ctx->interface = talloc_strdup(sd_ctx, DATA_PROVIDER_DBUS_INTERFACE);
    if (!sd_ctx->interface) {
        ret = ENOMEM;
        goto done;
    }
    sd_ctx->path = talloc_strdup(sd_ctx, DATA_PROVIDER_DBUS_PATH);
    if (!sd_ctx->path) {
        ret = ENOMEM;
        goto done;
    }
    sd_ctx->methods = dp_sbus_methods;
    sd_ctx->message_handler = sbus_message_handler;

    ret = sbus_new_server(dpctx->ev, sd_ctx, dpbus_address,
                          dbus_dp_init, dpctx);
    if (ret != EOK) {
        goto done;
    }
    talloc_steal(dpctx, sd_ctx);

done:
    talloc_free(tmp_ctx);
    return ret;
}

static int check_online(DBusMessage *message, void *data, DBusMessage **r)
{
    return EOK;
}

/* find list of backends */
/* find library implementing them */
/* dload() said library and set up a structure to hold pointers */

static int init_data_providers(struct dp_ctx *dpctx)
{
    TALLOC_CTX *tmp_ctx;
    struct dp_mod_ctx *module;
    char **doms;
    char *sec;
    char *mod_name;
    char *path;
    void *handle;
    char *mod_init_fn_name;
    sssm_init_fn_t mod_init_fn;
    int num_mods;
    int i, ret;

    tmp_ctx = talloc_new(dpctx);
    if (!tmp_ctx) {
        return ENOMEM;
    }

    ret = confdb_get_domains(dpctx->cdb, tmp_ctx, &doms);
    if (ret != EOK) {
        DEBUG(2, ("No domains configured!\n"));
        return ret;
    }

    num_mods = 0;
    for (i = 0; doms[i]; i++) {
        sec = talloc_asprintf(tmp_ctx, "config/domains/%s", doms[i]);
        if (!sec) {
            ret = ENOMEM;
            goto done;
        }
        ret = confdb_get_string(dpctx->cdb, tmp_ctx, sec,
                                "provider", NULL, &mod_name);
        if (ret != EOK) {
            goto done;
        }

        /* the LOCAL domain does not have a backend at the moment */
        if (strcasecmp(mod_name, "LOCAL") == 0) {
            continue;
        }

        path = talloc_asprintf(tmp_ctx, "%s/libsss_%s.so",
                               DATA_PROVIDER_PLUGINS_PATH, mod_name);

        handle = dlopen(path, RTLD_NOW);
        if (!handle) {
            DEBUG(0, ("Unable to load %s module with path (%s), error: %s\n",
                      mod_name, path, dlerror()));
            ret = ELIBACC;
            goto done;
        }

        mod_init_fn_name = talloc_asprintf(tmp_ctx, "sssm_%s_init", mod_name);
        if (!mod_init_fn_name) {
            ret = ENOMEM;
            goto done;
        }

        mod_init_fn = (sssm_init_fn_t)dlsym(handle, mod_init_fn_name);
        if (!mod_init_fn) {
            DEBUG(0, ("Unable to load init fn from module %s, error: %s\n",
                      mod_name, dlerror()));
            ret = ELIBBAD;
            goto done;
        }

        dpctx->modules = talloc_array(tmp_ctx, struct dp_mod_ctx *, num_mods +1);
        if (!dpctx->modules) {
            ret = ENOMEM;
            goto done;
        }
        module = talloc(dpctx->modules, struct dp_mod_ctx);
        if (!module) {
            ret = ENOMEM;
            goto done;
        }
        dpctx->modules[num_mods] = module;

        module->dp_ctx = dpctx;
        module->domain = talloc_strdup(dpctx->modules, doms[i]);
        module->name = talloc_steal(dpctx->modules, mod_name);

        ret = mod_init_fn(module);
        if (ret != EOK) {
            DEBUG(0, ("Error (%d) in module (%s) initialization!\n",
                      ret, mod_name));
            continue;
        }

        num_mods++;
    }

    ret = EOK;

done:
    talloc_free(doms);
    return ret;
}

int dp_process_init(TALLOC_CTX *mem_ctx,
                    struct event_context *ev,
                    struct confdb_ctx *cdb)
{
    struct dp_ctx *dpctx;
    int ret;

    dpctx = talloc_zero(mem_ctx, struct dp_ctx);
    if (!dpctx) {
        DEBUG(0, ("fatal error initializing dp_ctx\n"));
        return ENOMEM;
    }
    dpctx->ev = ev;
    dpctx->cdb = cdb;

    ret = dp_db_init(dpctx);
    if (ret != EOK) {
        DEBUG(0, ("fatal error opening database\n"));
        return ret;
    }

    ret = dp_monitor_init(dpctx);
    if (ret != EOK) {
        DEBUG(0, ("fatal error setting up monitor bus\n"));
        return ret;
    }

    ret = dp_srv_init(dpctx);
    if (ret != EOK) {
        DEBUG(0, ("fatal error setting up server bus\n"));
        return ret;
    }

    ret = init_data_providers(dpctx);
    if (ret != EOK) {
        DEBUG(0, ("fatal error initializing data providers\n"));
        return ret;
    }

    return EOK;
}