summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-slapi-plugins/ipa-sidgen/ipa_sidgen_common.c
blob: cbbb2ef183f2d94826a9ead20ca1fc39daa09599 (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/** 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 <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include <dirsrv/slapi-plugin.h>

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

int get_dom_sid(Slapi_ComponentId *plugin_id, const char *base_dn, char **_sid)
{
    Slapi_PBlock *search_pb = NULL;
    int search_result;
    Slapi_Entry **search_entries = NULL;
    int ret;
    const char *sid;

    search_pb = slapi_pblock_new();
    if (search_pb == NULL) {
        LOG_FATAL("Failed to create new pblock.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    slapi_search_internal_set_pb(search_pb, base_dn,
                                 LDAP_SCOPE_SUBTREE, DOM_ATTRS_FILTER,
                                 NULL, 0, NULL, NULL, plugin_id, 0);

    ret = slapi_search_internal_pb(search_pb);
    if (ret != 0) {
        LOG_FATAL("Starting internal search failed.\n");
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
    if (ret != 0 || search_result != LDAP_SUCCESS) {
        LOG_FATAL("Internal search failed.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
                           &search_entries);
    if (ret != 0) {
        LOG_FATAL("Failed to read searched entries.\n");
        goto done;
    }

    if (search_entries == NULL || search_entries[0] == NULL) {
        LOG("No existing entries.\n");
        ret = LDAP_NO_SUCH_OBJECT;
        goto done;
    }

    if (search_entries[1] != NULL) {
        LOG("Too many results found.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    sid = slapi_entry_attr_get_charptr(search_entries[0], IPA_SID);
    if (sid == NULL) {
        LOG("Domain object does not have a SID.\n");
        ret = LDAP_NO_SUCH_ATTRIBUTE;
        goto done;
    }

    *_sid = slapi_ch_strdup(sid);
    if (*_sid == NULL) {
        LOG("slapi_ch_strdup failed.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    LOG("Found domain SID [%s].\n", *_sid);
    ret = 0;

done:
    slapi_free_search_results_internal(search_pb);
    slapi_pblock_destroy(search_pb);

    return ret;
}

static int slapi_entry_to_range_info(struct slapi_entry *entry,
                                     struct range_info **_range)
{
    int ret;
    unsigned long ul_val;
    struct range_info *range = NULL;

    range = ( struct range_info *) slapi_ch_calloc(1, sizeof(struct range_info));
    if (range == NULL) {
        return LDAP_OPERATIONS_ERROR;
    }

    ul_val = slapi_entry_attr_get_ulong(entry, IPA_BASE_ID);
    if (ul_val == 0 || ul_val >= UINT32_MAX) {
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }
    range->base_id = ul_val;

    ul_val = slapi_entry_attr_get_ulong(entry, IPA_ID_RANGE_SIZE);
    if (ul_val == 0 || ul_val >= UINT32_MAX) {
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }
    range->id_range_size = ul_val;

    ul_val = slapi_entry_attr_get_ulong(entry, IPA_BASE_RID);
    if (ul_val == 0 || ul_val >= UINT32_MAX) {
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }
    range->base_rid = ul_val;

    ul_val = slapi_entry_attr_get_ulong(entry, IPA_SECONDARY_BASE_RID);
    if (ul_val == 0 || ul_val >= UINT32_MAX) {
        ret = ERANGE;
        goto done;
    }
    range->secondary_base_rid = ul_val;

    *_range = range;
    ret = 0;

done:
    if (ret != 0) {
        slapi_ch_free((void **) &range);
    }

    return ret;
}

int get_objectclass_flags(char **objectclasses,
                          bool *has_posix_account,
                          bool *has_posix_group,
                          bool *has_ipa_id_object)
{
    size_t c;

    if (objectclasses == NULL) {
        return LDAP_OPERATIONS_ERROR;
    }

    *has_posix_account = false;
    *has_posix_group = false;
    *has_ipa_id_object = false;

    for (c = 0; objectclasses[c] != NULL; c++) {
        if (strcasecmp(objectclasses[c], POSIX_ACCOUNT) == 0) {
            *has_posix_account = true;
        } else if (strcasecmp(objectclasses[c], POSIX_GROUP) == 0) {
            *has_posix_group = true;
        } else if (strcasecmp(objectclasses[c], IPA_ID_OBJECT) == 0) {
            *has_ipa_id_object = true;
        }
    }

    return 0;
}

void free_ranges(struct range_info ***_ranges)
{
    size_t c;
    struct range_info **ranges = *_ranges;

    if (ranges != NULL) {
        for (c = 0; ranges[c] != NULL; c++) {
            slapi_ch_free((void **) &ranges[c]);
        }

        slapi_ch_free((void **) _ranges);
    }
}

int get_ranges(Slapi_ComponentId *plugin_id, const char *base_dn,
               struct range_info ***_ranges)
{
    Slapi_PBlock *search_pb = NULL;
    Slapi_Entry **search_entries = NULL;
    int search_result;
    size_t c;
    int ret;
    struct range_info **ranges = NULL;

    search_pb = slapi_pblock_new();
    if (search_pb == NULL) {
        LOG_FATAL("Failed to create new pblock.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    slapi_search_internal_set_pb(search_pb, base_dn,
                                 LDAP_SCOPE_SUBTREE, DOMAIN_ID_RANGE_FILTER,
                                 NULL, 0, NULL, NULL, plugin_id, 0);

    ret = slapi_search_internal_pb(search_pb);
    if (ret != 0) {
        LOG_FATAL("Starting internal search failed.\n");
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
    if (ret != 0 || search_result != LDAP_SUCCESS) {
        LOG_FATAL("Internal search failed.\n");
        ret = (search_result != LDAP_SUCCESS) ? search_result:
                                                LDAP_OPERATIONS_ERROR;
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
                           &search_entries);
    if (ret != 0) {
        LOG_FATAL("Failed to read searched entries.\n");
        goto done;
    }

    if (search_entries == NULL || search_entries[0] == NULL) {
        LOG("No ranges found.\n");
        ret = LDAP_NO_SUCH_OBJECT;
        goto done;
    }

    for (c = 0; search_entries[c] != NULL; c++);
    ranges = (struct range_info **) slapi_ch_calloc(c + 1,
                                                    sizeof(struct range_info *));
    if (ranges == NULL) {
        LOG("calloc failed.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    for (c = 0; search_entries[c] != NULL; c++) {
        ret = slapi_entry_to_range_info(search_entries[c], &ranges[c]);
        if (ret != 0) {
            LOG_FATAL("Failed to convert LDAP entry to range struct.\n");
            goto done;
        }
    }

    *_ranges = ranges;
    ret = 0;

done:
    slapi_free_search_results_internal(search_pb);
    slapi_pblock_destroy(search_pb);
    if (ret != 0) {
        free_ranges(&ranges);
    }

    return ret;
}

static int find_sid(const char *sid, Slapi_ComponentId *plugin_id,
                    const char *base_dn)
{
    Slapi_PBlock *search_pb = NULL;
    Slapi_Entry **search_entries = NULL;
    int search_result;
    int ret;
    char *filter = NULL;

    search_pb = slapi_pblock_new();
    if (search_pb == NULL) {
        LOG_FATAL("Failed to create new pblock.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    filter = slapi_ch_smprintf("%s=%s", IPA_SID, sid);
    if (filter == NULL) {
        LOG_FATAL("Cannot create search filter to check if SID is used.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    slapi_search_internal_set_pb(search_pb, base_dn,
                                 LDAP_SCOPE_SUBTREE, filter,
                                 NULL, 0, NULL, NULL, plugin_id, 0);

    ret = slapi_search_internal_pb(search_pb);
    if (ret != 0) {
        LOG_FATAL("Starting internal search failed.\n");
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
    if (ret != 0 || search_result != LDAP_SUCCESS) {
        LOG_FATAL("Internal search failed.\n");
        ret = (search_result != LDAP_SUCCESS) ? search_result:
                                                LDAP_OPERATIONS_ERROR;
        goto done;
    }

    ret = slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
                           &search_entries);
    if (ret != 0) {
        LOG_FATAL("Failed to read searched entries.\n");
        goto done;
    }

    if (search_entries == NULL || search_entries[0] == NULL) {
        LOG("No SID found.\n");
        ret = LDAP_NO_SUCH_OBJECT;
        goto done;
    }

    ret = 0;

done:
    slapi_ch_free_string(&filter);
    slapi_free_search_results_internal(search_pb);
    slapi_pblock_destroy(search_pb);

    return ret;
}

static int rid_to_sid_with_check(uint32_t rid, Slapi_ComponentId *plugin_id,
                                 const char *base_dn, const char *dom_sid,
                                 char **_sid)
{
    char *sid = NULL;
    int ret;

    sid = slapi_ch_smprintf("%s-%lu", dom_sid, (unsigned long) rid);
    if (sid == NULL) {
        LOG("Failed to create SID string.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    LOG("SID is [%s].\n", sid);

    ret = find_sid(sid, plugin_id, base_dn);
    if (ret == LDAP_NO_SUCH_OBJECT) {
        *_sid = sid;
        ret = 0;
        goto done;
    } else if (ret != 0) {
        LOG_FATAL("Cannot check if SID is already used.\n");
        goto done;
    }

    LOG_FATAL("SID [%s] is already used.\n", sid);
    ret = LDAP_CONSTRAINT_VIOLATION;

done:
    if (ret != 0) {
        slapi_ch_free_string(&sid);
    }

    return ret;
}

int find_sid_for_id(uint32_t id, Slapi_ComponentId *plugin_id,
                    const char *base_dn, const char *dom_sid,
                    struct range_info **ranges, char **_sid)
{
    uint32_t rid;
    size_t c;
    char *sid = NULL;
    int ret;

    rid = 0;
    for (c = 0; ranges[c] != NULL; c++) {
        if (id >= ranges[c]->base_id &&
            id < (ranges[c]->base_id + ranges[c]->id_range_size)) {
            rid = ranges[c]->base_rid + (id - ranges[c]->base_id);
            break;
        }
    }

    if (rid == 0) {
        LOG("No matching range found. Cannot add SID.\n");
        ret = LDAP_NO_SUCH_OBJECT;
        goto done;
    }

    ret = rid_to_sid_with_check(rid, plugin_id, base_dn, dom_sid, &sid);
    if (ret != LDAP_CONSTRAINT_VIOLATION) {
        goto done;
    }

    /* SID is already used, try secondary range.*/
    rid = ranges[c]->secondary_base_rid + (id - ranges[c]->base_id);

    ret = rid_to_sid_with_check(rid, plugin_id, base_dn, dom_sid, &sid);
    if (ret != LDAP_CONSTRAINT_VIOLATION) {
        goto done;
    }

    LOG_FATAL("Secondary SID is used as well.\n");

done:
    if (ret != 0) {
        slapi_ch_free_string(&sid);
    } else {
        *_sid = sid;
    }

    return ret;
}

int find_sid_for_ldap_entry(struct slapi_entry *entry,
                            Slapi_ComponentId *plugin_id,
                            const char *base_dn,
                            const char *dom_sid,
                            struct range_info **ranges)
{
    int ret;
    const char *dn_str;
    uint32_t uid_number;
    uint32_t gid_number;
    uint32_t id;
    char *sid = NULL;
    char **objectclasses = NULL;
    Slapi_PBlock *mod_pb = NULL;
    Slapi_Mods *smods = NULL;
    int result;
    bool has_posix_account = false;
    bool has_posix_group = false;
    bool has_ipa_id_object = false;
    const char *objectclass_to_add = NULL;

    dn_str = slapi_entry_get_dn_const(entry);
    if (dn_str == NULL) {
        LOG_FATAL("Cannot find DN of an LDAP entry.\n");
        ret = LDAP_NO_SUCH_ATTRIBUTE;
        goto done;
    }
    LOG("Trying to add SID for [%s].\n", dn_str);

    uid_number = slapi_entry_attr_get_ulong(entry, UID_NUMBER);
    gid_number = slapi_entry_attr_get_ulong(entry, GID_NUMBER);

    if (uid_number == 0 && gid_number == 0) {
        LOG("[%s] does not have Posix IDs, nothing to do.\n", dn_str);
        ret = 0;
        goto done;
    }

    if (uid_number == IPA_DNA_MAGIC || gid_number == IPA_DNA_MAGIC) {
        LOG_FATAL("Looks that DNA plugin was not run before.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    if (uid_number >= UINT32_MAX || gid_number >= UINT32_MAX) {
        LOG_FATAL("ID value too large.\n");
        ret = LDAP_CONSTRAINT_VIOLATION;
        goto done;
    }

    sid = slapi_entry_attr_get_charptr(entry, IPA_SID);
    if (sid != NULL) {
        LOG("Object already has a SID, nothing to do.\n");
        ret = 0;
        goto done;
    }

    objectclasses = slapi_entry_attr_get_charray(entry, OBJECTCLASS);
    ret = get_objectclass_flags(objectclasses, &has_posix_account,
                                               &has_posix_group,
                                               &has_ipa_id_object);
    if (ret != 0) {
        LOG_FATAL("Cannot determine objectclasses.\n");
        goto done;
    }

    if (has_posix_account && uid_number != 0 && gid_number != 0) {
        id = uid_number;
        objectclass_to_add = IPANT_USER_ATTRS;
    } else if (has_posix_group && gid_number != 0) {
        id = gid_number;
        objectclass_to_add = IPANT_GROUP_ATTRS;
    } else if (has_ipa_id_object) {
        id = (uid_number != 0) ? uid_number : gid_number;
        objectclass_to_add = NULL;
    } else {
        LOG_FATAL("Inconsistent objectclasses and attributes, nothing to do.\n");
        ret = 0;
        goto done;
    }

    ret = find_sid_for_id(id, plugin_id, base_dn, dom_sid, ranges, &sid);
    if (ret != 0) {
        LOG_FATAL("Cannot convert Posix ID [%ul] into an unused SID.\n", id);
        goto done;
    }

    smods = slapi_mods_new();
    if (smods == NULL) {
        LOG("slapi_mods_new failed.\n");
        ret = LDAP_OPERATIONS_ERROR;
        goto done;
    }

    if (objectclass_to_add != NULL) {
        slapi_mods_add_string(smods, LDAP_MOD_ADD,
                              OBJECTCLASS, objectclass_to_add);
    }
    slapi_mods_add_string(smods, LDAP_MOD_REPLACE, IPA_SID, sid);

    mod_pb = slapi_pblock_new();
    slapi_modify_internal_set_pb(mod_pb, dn_str,
                                 slapi_mods_get_ldapmods_byref(smods),
                                 NULL, NULL, plugin_id, 0);

    ret = slapi_modify_internal_pb(mod_pb);
    if (ret != 0) {
        LOG_FATAL("Modify failed with [%d] on entry [%s]\n", ret, dn_str);
        goto done;
    }

    ret = slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
    if (ret != 0 || result != LDAP_SUCCESS){
        LOG_FATAL("Modify failed on entry [%s]\n", dn_str);
        goto done;
    }

done:
    slapi_ch_free_string(&sid);
    slapi_pblock_destroy(mod_pb);
    slapi_mods_free(&smods);
    slapi_ch_array_free(objectclasses);

    return ret;
}