summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-slapi-plugins/ipa-sidgen/ipa_sidgen_task.c
blob: ffbc9c636e32b0c0a3960ec76eda378a94c504fe (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
/** BEGIN COPYRIGHT BLOCK
 * 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/>.
 *
 * Additional permission under GPLv3 section 7:
 *
 * In the following paragraph, "GPL" means the GNU General Public
 * License, version 3 or any later version, and "Non-GPL Code" means
 * code that is governed neither by the GPL nor a license
 * compatible with the GPL.
 *
 * You may link the code of this Program with Non-GPL Code and convey
 * linked combinations including the two, provided that such Non-GPL
 * Code only links to the code of this Program through those well
 * defined interfaces identified in the file named EXCEPTION found in
 * the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline
 * functions from the Approved Interfaces without causing the resulting
 * work to be covered by the GPL. Only the copyright holders of this
 * Program may make changes or additions to the list of Approved
 * Interfaces.
 *
 * Authors:
 * Sumit Bose <sbose@redhat.com>
 *
 * Copyright (C) 2012 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>

#include <pthread.h>
#include <dirsrv/slapi-plugin.h>

#include "util.h"
#include "ipa_sidgen.h"

#define NSEC_PER_SEC     1000000000UL


#define AT_CN "cn"

Slapi_ComponentId *global_sidgen_plugin_id = NULL;

struct worker_ctx {
    long delay;
    char *base_dn;
    Slapi_ComponentId *plugin_id;
    pthread_t tid;
    char *dom_sid;
    struct range_info **ranges;
};

static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
                                              const char *default_val)
{
    Slapi_Attr *attr;
    Slapi_Value *val = NULL;

    if (slapi_entry_attr_find(e, attrname, &attr) != 0)
        return default_val;
    slapi_attr_first_value(attr, &val);
    return slapi_value_get_string(val);
}

static void free_pblock(void *arg)
{
    Slapi_PBlock *pb = (Slapi_PBlock *) arg;

    slapi_free_search_results_internal(pb);
    slapi_pblock_destroy(pb);
}

static int do_work(struct worker_ctx *worker_ctx)
{
    Slapi_PBlock *pb;
    int ret;
    size_t c;
    char *filter = NULL;
    char *attrs[] = { OBJECTCLASS, UID_NUMBER, GID_NUMBER, NULL };
    Slapi_Entry **e = NULL;
    struct timespec ts;

    pb = slapi_pblock_new();
    if (pb == NULL) {
        return ENOMEM;
    }

    pthread_cleanup_push(free_pblock, (void *) pb);

    filter = slapi_ch_smprintf("(&(%s=%s)(!(%s=%s))(|(%s=%s)(%s=%s)(%s=%s))(!(%s=*)))",
                               OBJECTCLASS, IPA_OBJECT,
                               OBJECTCLASS, MEP_MANAGED_ENTRY,
                               OBJECTCLASS, POSIX_ACCOUNT,
                               OBJECTCLASS, POSIX_GROUP,
                               OBJECTCLASS, IPA_ID_OBJECT,
                               IPA_SID);
    if (filter == NULL) {
        LOG_FATAL("Cannot generate search filter for objects without a SID.\n");
        ret = ENOMEM;
        goto done;
    }
    LOG("Base DN: [%s], Filter: [%s].\n", worker_ctx->base_dn, filter);

    slapi_search_internal_set_pb(pb, worker_ctx->base_dn, LDAP_SCOPE_SUBTREE,
                                 filter, attrs, 0, NULL, NULL,
                                 worker_ctx->plugin_id, 0);
    ret = slapi_search_internal_pb(pb);
    if (ret != 0) {
        slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
        if (ret != 0) {
            LOG_FATAL("Search failed with [%d].\n", ret);
        } else {
            LOG_FATAL("slapi_search_internal_pb failed, "
                      "but no error code available.\n");
            ret = EFAULT;
        }
        goto done;
    }

    ret = slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &e);
    if (ret != 0) {
        LOG_FATAL("slapi_pblock_get failed.\n");
        ret = EFAULT;
        goto done;
    }

    if (e == NULL || e[0] == NULL) {
        LOG("No entry with missing SID found.\n");
        ret = 0;
        goto done;
    }

    for (c = 0; e[c] != NULL; c++) {
        ret = find_sid_for_ldap_entry(e[c], worker_ctx->plugin_id,
                                      worker_ctx->base_dn, worker_ctx->dom_sid,
                                      worker_ctx->ranges);
        if (ret != 0) {
            LOG_FATAL("Cannot add SID to existing entry.\n");
            goto done;
        }

        if (worker_ctx->delay != 0) {
            ts.tv_nsec = worker_ctx->delay % NSEC_PER_SEC;
            ts.tv_sec = (worker_ctx->delay - ts.tv_nsec) / NSEC_PER_SEC;
            nanosleep(&ts, NULL);
        }
    };

done:
    slapi_ch_free_string(&filter);
    pthread_cleanup_pop(1);

    LOG("do_work finished with [%d].\n", ret);

    return ret;
}

static void *sidgen_task_thread(void *arg)
{
    Slapi_Task *task = (Slapi_Task *)arg;
    struct worker_ctx *worker_ctx;
    int ret;

    if (task == NULL) {
        LOG_FATAL("Missing task data!\n");
        ret =SLAPI_DSE_CALLBACK_OK;
        goto done;
    }

    worker_ctx = slapi_task_get_data(task);
    if (worker_ctx == NULL) {
        LOG_FATAL("Missing context!\n");
        ret =SLAPI_DSE_CALLBACK_OK;
        goto done;
    }

    slapi_task_begin(task, 1);
    LOG_FATAL("Sidgen task starts ...\n");

    ret = do_work(worker_ctx);

done:
    LOG_FATAL("Sidgen task finished [%d].\n", ret);
    slapi_task_inc_progress(task);
    slapi_task_finish(task, ret);

    return NULL;
}

static void sidgen_task_destructor(Slapi_Task *task)
{
    struct worker_ctx *worker_ctx;

    if (task != NULL) {
        worker_ctx = slapi_task_get_data(task);
        if (worker_ctx != NULL) {
            free_ranges(&worker_ctx->ranges);
            slapi_ch_free_string(&worker_ctx->dom_sid);
            slapi_ch_free_string(&worker_ctx->base_dn);
            slapi_ch_free((void **) &worker_ctx);
        }
    }
}

int sidgen_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
                    Slapi_Entry *eAfter, int *returncode,
                    char *returntext, void *arg)
{
    int ret = SLAPI_DSE_CALLBACK_ERROR;
    const char *str;
    struct worker_ctx *worker_ctx = NULL;
    char *endptr;
    Slapi_Task *task = NULL;

    *returncode = LDAP_OPERATIONS_ERROR;
    returntext[0] = '\0';

    worker_ctx = (struct worker_ctx *) slapi_ch_calloc(1,
                                                     sizeof(struct worker_ctx));
    if (worker_ctx == NULL) {
        LOG_FATAL("slapi_ch_malloc failed!\n");
        *returncode = LDAP_OPERATIONS_ERROR;
        ret = SLAPI_DSE_CALLBACK_ERROR;
        goto done;
    }

    worker_ctx->plugin_id = global_sidgen_plugin_id;

    str = fetch_attr(e, "delay", NULL);
    if (str != NULL) {
        errno = 0;
        worker_ctx->delay = strtol(str, &endptr, 10);
        if (errno != 0 || worker_ctx->delay < 0) {
            LOG_FATAL("invalid delay [%s]!\n", str);
            *returncode = LDAP_CONSTRAINT_VIOLATION;
            ret = SLAPI_DSE_CALLBACK_ERROR;
            goto done;
        }
    }
    LOG("delay is [%li].\n", worker_ctx->delay);

    str = fetch_attr(e, "nsslapd-basedn", NULL);
    if (str == NULL) {
        LOG_FATAL("Missing nsslapd-basedn!\n");
        *returncode = LDAP_CONSTRAINT_VIOLATION;
        ret = SLAPI_DSE_CALLBACK_ERROR;
        goto done;
    }
    worker_ctx->base_dn = slapi_ch_strdup(str);
    if (worker_ctx->base_dn == NULL) {
        LOG_FATAL("Failed to copy base DN.\n");
        *returncode = LDAP_OPERATIONS_ERROR;
        ret = ENOMEM;
        goto done;
    }

    ret = get_dom_sid(worker_ctx->plugin_id, worker_ctx->base_dn,
                      &worker_ctx->dom_sid);
    if (ret != 0) {
        LOG_FATAL("Cannot find domain SID.\n");
        goto done;
    }

    ret = get_ranges(worker_ctx->plugin_id, worker_ctx->base_dn,
                     &worker_ctx->ranges);
    if (ret != 0) {
        LOG_FATAL("Cannot find ranges.\n");
        goto done;
    }

    task = slapi_new_task(slapi_entry_get_ndn(e));
    if (task == NULL) {
        LOG_FATAL("unable to allocate new task!\n");
        *returncode = LDAP_OPERATIONS_ERROR;
        ret = SLAPI_DSE_CALLBACK_ERROR;
        goto done;
    }

    slapi_task_set_destructor_fn(task, sidgen_task_destructor);
    slapi_task_set_data(task, worker_ctx);

    ret = pthread_create(&worker_ctx->tid, NULL, sidgen_task_thread, task);
    if (ret != 0) {
        LOG_FATAL("unable to create sidgen task thread!\n");
        *returncode = LDAP_OPERATIONS_ERROR;
        ret = SLAPI_DSE_CALLBACK_ERROR;
        slapi_task_finish(task, *returncode);
        goto done;
    }

    ret = SLAPI_DSE_CALLBACK_OK;
    *returncode = LDAP_SUCCESS;

done:
    if (ret != SLAPI_DSE_CALLBACK_OK) {
        slapi_ch_free((void **) &worker_ctx->base_dn);
        slapi_ch_free((void **) &worker_ctx);
    }
    return ret;
}

static int sigden_task_start(Slapi_PBlock *pb)
{
    int ret = 0;

    ret = slapi_task_register_handler("ipa-sidgen-task", sidgen_task_add);

    return ret;
}

int sidgen_task_init(Slapi_PBlock *pb)
{
    int ret = 0;

    ret = slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY,
                           &global_sidgen_plugin_id);
    if (ret != 0 || global_sidgen_plugin_id == NULL) {
        LOG_FATAL("Plugin identity not available.\n");
        ret = (ret != 0) ? ret : EINVAL;
        goto done;
    }

    ret = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
                            (void *) SLAPI_PLUGIN_VERSION_03);

    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
                            (void *) sigden_task_start);

done:
    if (ret != 0) {
        LOG_FATAL("Failed to initialize plug-in\n" );
    }

    return ret;
}