summaryrefslogtreecommitdiff
path: root/src/db/sysdb_subdomains.c
blob: ef6b0ad346d023e03c6a3c03e4ac491392409e49 (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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
   SSSD

   System Database - Sub-domain related calls

   Copyright (C) 2012 Jan Zeleny <jzeleny@redhat.com>
   Copyright (C) 2012 Sumit Bose <sbose@redhat.com>

   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 "db/sysdb_private.h"

errno_t sysdb_get_subdomains(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
                             size_t *subdomain_count,
                             struct sysdb_subdom ***subdomain_list)
{
    int i;
    errno_t ret;
    TALLOC_CTX *tmp_ctx;
    struct ldb_result *res;
    const char *attrs[] = {"cn",
                           SYSDB_SUBDOMAIN_REALM,
                           SYSDB_SUBDOMAIN_FLAT,
                           SYSDB_SUBDOMAIN_ID,
                           NULL};
    struct sysdb_subdom **list;
    struct ldb_dn *basedn;
    const char *tmp_str;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        ret = ENOMEM;
        goto done;
    }

    basedn = ldb_dn_new(tmp_ctx, sysdb->ldb, SYSDB_BASE);
    if (basedn == NULL) {
        ret = EIO;
        goto done;
    }
    ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
                     basedn, LDB_SCOPE_ONELEVEL,
                     attrs, "objectclass=%s", SYSDB_SUBDOMAIN_CLASS);
    if (ret != LDB_SUCCESS) {
        ret = EIO;
        goto done;
    }

    list = talloc_zero_array(tmp_ctx, struct sysdb_subdom *, res->count + 1);
    if (list == NULL) {
        ret = ENOMEM;
        goto done;
    }

    for (i = 0; i < res->count; i++) {
        list[i] = talloc_zero(list, struct sysdb_subdom);
        if (list[i] == NULL) {
            ret = ENOMEM;
            goto done;
        }
        tmp_str = ldb_msg_find_attr_as_string(res->msgs[i], "cn", NULL);
        if (tmp_str == NULL) {
            DEBUG(SSSDBG_MINOR_FAILURE,
                  ("The object [%s] doesn't have a name\n",
                   ldb_dn_get_linearized(res->msgs[i]->dn)));
            ret = EINVAL;
            goto done;
        }

        list[i]->name = talloc_strdup(list, tmp_str);
        if (list[i]->name == NULL) {
            ret = ENOMEM;
            goto done;
        }

        tmp_str = ldb_msg_find_attr_as_string(res->msgs[i],
                                              SYSDB_SUBDOMAIN_REALM, NULL);
        if (tmp_str != NULL) {
            list[i]->realm = talloc_strdup(list, tmp_str);
            if (list[i]->realm == NULL) {
                ret = ENOMEM;
                goto done;
            }
        }

        tmp_str = ldb_msg_find_attr_as_string(res->msgs[i],
                                              SYSDB_SUBDOMAIN_FLAT, NULL);
        if (tmp_str != NULL) {
            list[i]->flat_name = talloc_strdup(list, tmp_str);
            if (list[i]->flat_name == NULL) {
                ret = ENOMEM;
                goto done;
            }
        }

        tmp_str = ldb_msg_find_attr_as_string(res->msgs[i],
                                              SYSDB_SUBDOMAIN_ID, NULL);
        if (tmp_str != NULL) {
            list[i]->id = talloc_strdup(list, tmp_str);
            if (list[i]->id == NULL) {
                ret = ENOMEM;
                goto done;
            }
        }
    }

    list[res->count] = NULL;

    *subdomain_count = res->count;
    *subdomain_list = talloc_steal(mem_ctx, list);
    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
                                     struct sysdb_ctx *sysdb,
                                     struct sysdb_subdom **_info)
{
    errno_t ret;
    TALLOC_CTX *tmp_ctx;
    const char *tmp_str;
    struct ldb_dn *basedn;
    struct sysdb_subdom *info;
    struct ldb_result *res;
    const char *attrs[] = {"cn",
                           SYSDB_SUBDOMAIN_REALM,
                           SYSDB_SUBDOMAIN_FLAT,
                           SYSDB_SUBDOMAIN_ID,
                           NULL};

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

    info = talloc_zero(tmp_ctx, struct sysdb_subdom);
    if (info == NULL) {
        ret = ENOMEM;
        goto done;
    }

    basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_DOM_BASE,
                            sysdb->domain->name);
    if (basedn == NULL) {
        ret = EIO;
        goto done;
    }
    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs,
                     NULL);
    if (ret != LDB_SUCCESS) {
        ret = EIO;
        goto done;
    }

    if (res->count == 0) {
        ret = ENOENT;
        goto done;
    }

    if (res->count > 1) {
        DEBUG(SSSDBG_OP_FAILURE, ("Base search returned [%d] results, "
                                 "expected 1.\n", res->count));
        ret = EINVAL;
        goto done;
    }

    tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_REALM,
                                          NULL);
    if (tmp_str != NULL) {
        info->realm = talloc_strdup(info, tmp_str);
        if (info->realm == NULL) {
            ret = ENOMEM;
            goto done;
        }
    }

    tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_FLAT,
                                          NULL);
    if (tmp_str != NULL) {
        info->flat_name = talloc_strdup(info, tmp_str);
        if (info->flat_name == NULL) {
            ret = ENOMEM;
            goto done;
        }
    }

    tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_ID,
                                          NULL);
    if (tmp_str != NULL) {
        info->id = talloc_strdup(info, tmp_str);
        if (info->id == NULL) {
            ret = ENOMEM;
            goto done;
        }
    }

    *_info = talloc_steal(mem_ctx, info);
done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
                                     struct sysdb_subdom *domain_info)
{
    TALLOC_CTX *tmp_ctx;
    struct ldb_message *msg;
    int ret;
    bool do_update = false;
    struct sysdb_subdom *current_info;

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

    ret = sysdb_master_domain_get_info(tmp_ctx, sysdb, &current_info);
    if (ret != EOK) {
        goto done;
    }

    msg = ldb_msg_new(tmp_ctx);
    if (msg == NULL) {
        ret = ENOMEM;
        goto done;
    }

    msg->dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_DOM_BASE,
                             sysdb->domain->name);
    if (msg->dn == NULL) {
        ret = EIO;
        goto done;
    }

    if (domain_info->realm != NULL &&
        (current_info->realm == NULL ||
         strcmp(current_info->realm, domain_info->realm) != 0) ) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM,
                                LDB_FLAG_MOD_REPLACE, NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_REALM,
                                 domain_info->realm);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        do_update = true;
    }

    if (domain_info->flat_name != NULL &&
        (current_info->flat_name == NULL ||
         strcmp(current_info->flat_name, domain_info->flat_name) != 0) ) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT,
                                LDB_FLAG_MOD_REPLACE, NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_FLAT,
                                 domain_info->flat_name);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        do_update = true;
    }

    if (domain_info->id != NULL &&
        (current_info->id == NULL ||
         strcmp(current_info->id, domain_info->id) != 0) ) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID, LDB_FLAG_MOD_REPLACE,
                                NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_ID, domain_info->id);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        do_update = true;
    }

    if (do_update == false) {
        ret = EOK;
        goto done;
    }

    ret = ldb_modify(sysdb->ldb, msg);
    if (ret != LDB_SUCCESS) {
        DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add subdomain attributes to "
                                     "[%s]: [%d][%s]!\n",
                                     domain_info->name, ret,
                                     ldb_errstring(sysdb->ldb)));
        ret = sysdb_error_to_errno(ret);
        goto done;
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);

    return ret;
}
static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
                                             struct sysdb_subdom *domain_info)
{
    TALLOC_CTX *tmp_ctx;
    struct ldb_message *msg;
    int ret;

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

    msg = ldb_msg_new(tmp_ctx);
    if (msg == NULL) {
        ret = ENOMEM;
        goto done;
    }

    msg->dn = ldb_dn_new_fmt(msg, sysdb->ldb, SYSDB_DOM_BASE,
                             domain_info->name);
    if (msg->dn == NULL) {
        ret = ENOMEM;
        goto done;
    }

    ret = ldb_msg_add_empty(msg, SYSDB_OBJECTCLASS, LDB_FLAG_MOD_ADD, NULL);
    if (ret != LDB_SUCCESS) {
        ret = sysdb_error_to_errno(ret);
        goto done;
    }

    ret = ldb_msg_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SUBDOMAIN_CLASS);
    if (ret != LDB_SUCCESS) {
        ret = sysdb_error_to_errno(ret);
        goto done;
    }

    if (domain_info->realm != NULL) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM, LDB_FLAG_MOD_ADD,
                                NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_REALM,
                                 domain_info->realm);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }
    }

    if (domain_info->flat_name != NULL) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT, LDB_FLAG_MOD_ADD,
                                NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_FLAT,
                                 domain_info->flat_name);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }
    }

    if (domain_info->id != NULL) {
        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID, LDB_FLAG_MOD_ADD,
                                NULL);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }

        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_ID, domain_info->id);
        if (ret != LDB_SUCCESS) {
            ret = sysdb_error_to_errno(ret);
            goto done;
        }
    }

    ret = ldb_modify(sysdb->ldb, msg);
    if (ret != LDB_SUCCESS) {
        DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add subdomain attributes to "
                                     "[%s]: [%d][%s]!\n",
                                     domain_info->name, ret,
                                     ldb_errstring(sysdb->ldb)));
        ret = sysdb_error_to_errno(ret);
        goto done;
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);

    return ret;
}

errno_t sysdb_update_subdomains(struct sysdb_ctx *sysdb,
                                int num_subdoms,
                                struct sysdb_subdom *subdoms)
{
    int ret;
    int sret;
    size_t c;
    size_t d;
    TALLOC_CTX *tmp_ctx = NULL;
    size_t cur_subdomains_count;
    struct sysdb_subdom **cur_subdomains;
    struct ldb_dn *dn;
    bool in_transaction = false;
    bool *keep_subdomain;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* Retrieve all subdomains that are currently in sysdb */
    ret = sysdb_get_subdomains(tmp_ctx, sysdb, &cur_subdomains_count,
                               &cur_subdomains);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, ("sysdb_get_subdomains failed.\n"));
        goto done;
    }

    keep_subdomain = talloc_zero_array(tmp_ctx, bool, cur_subdomains_count);
    if (keep_subdomain == NULL) {
        ret = ENOMEM;
        DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero_array failed.\n"));
        goto done;
    }

    ret = sysdb_transaction_start(sysdb);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, ("sysdb_transaction_start failed.\n"));
        goto done;
    }
    in_transaction = true;

    /* Go through a list of retrieved subdomains and:
     * - if a subdomain already exists in sysdb, mark it for preservation
     * - if the subdomain doesn't exist in sysdb, create its bare structure
     */
    for (c = 0; c < num_subdoms; c++) {
        for (d = 0; d < cur_subdomains_count; d++) {
            if (strcasecmp(subdoms[c].name,
                           cur_subdomains[d]->name) == 0) {
                keep_subdomain[d] = true;
                /* sub-domain already in cache, nothing to do */
                break;
            }
        }

        if (d == cur_subdomains_count) {
            DEBUG(SSSDBG_TRACE_FUNC, ("Adding sub-domain [%s].\n",
                                      subdoms[c].name));
            ret = sysdb_domain_create(sysdb, subdoms[c].name);
            if (ret != EOK) {
                DEBUG(SSSDBG_OP_FAILURE, ("sysdb_domain_create failed.\n"));
                goto done;
            }

            ret = sysdb_add_subdomain_attributes(sysdb, &subdoms[c]);
            if (ret != EOK) {
                DEBUG(SSSDBG_OP_FAILURE,
                      ("sysdb_add_subdomain_attributes failed.\n"));
                goto done;
            }
        }
    }

    /* Now delete all subdomains that have been in sysdb prior to
     * refreshing the list and are not marked for preservation
     * (i.e. they are not in the new list of subdomains)
     */
    for (d = 0; d < cur_subdomains_count; d++) {
        if (!keep_subdomain[d]) {
            DEBUG(SSSDBG_TRACE_FUNC, ("Removing sub-domain [%s].\n",
                                      cur_subdomains[d]->name));
            dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_DOM_BASE,
                                cur_subdomains[d]->name);
            if (dn == NULL) {
                ret = ENOMEM;
                goto done;
            }

            ret = sysdb_delete_recursive(sysdb, dn, true);
            if (ret != EOK) {
                DEBUG(SSSDBG_OP_FAILURE, ("sysdb_delete_recursive failed.\n"));
                goto done;
            }
        }
    }

    ret = sysdb_transaction_commit(sysdb);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, ("Could not commit transaction\n"));
        goto done;
    }
    in_transaction = false;

done:
    if (in_transaction) {
        sret = sysdb_transaction_cancel(sysdb);
        if (sret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Could not cancel transaction\n"));
        }
    }
    talloc_free(tmp_ctx);
    return ret;
}

errno_t sysdb_get_subdomain_context(TALLOC_CTX *mem_ctx,
                                    struct sysdb_ctx *sysdb,
                                    struct sss_domain_info *subdomain,
                                    struct sysdb_ctx **subdomain_ctx)
{
    struct sysdb_ctx *new_ctx;

    new_ctx = talloc_zero(mem_ctx, struct sysdb_ctx);
    if (new_ctx == NULL) {
        return ENOMEM;
    }

    new_ctx->domain = subdomain;
    new_ctx->mpg = true;

    new_ctx->ldb = sysdb->ldb;
    new_ctx->ldb_file = sysdb->ldb_file;

    *subdomain_ctx = new_ctx;

    return EOK;
}

#define CHECK_DOMAIN_INFO(dom_info) do { \
    if (dom_info == NULL || dom_info->sysdb == NULL) { \
        DEBUG(SSSDBG_OP_FAILURE, ("Invalid domain info.\n")); \
        return EINVAL; \
    } \
} while(0)

errno_t sysdb_store_domuser(struct sss_domain_info *domain,
                            const char *name,
                            const char *pwd,
                            uid_t uid, gid_t gid,
                            const char *gecos,
                            const char *homedir,
                            const char *shell,
                            struct sysdb_attrs *attrs,
                            char **remove_attrs,
                            uint64_t cache_timeout,
                            time_t now)
{
    CHECK_DOMAIN_INFO(domain);

    return sysdb_store_user(domain->sysdb, name, pwd, uid, gid, gecos, homedir,
                            shell, NULL, attrs, remove_attrs, cache_timeout, now);
}

errno_t sysdb_delete_domuser(struct sss_domain_info *domain,
                             const char *name, uid_t uid)
{
    CHECK_DOMAIN_INFO(domain);

    return sysdb_delete_user(domain->sysdb, name, uid);
}

errno_t sysdb_store_domgroup(struct sss_domain_info *domain,
                             const char *name,
                             gid_t gid,
                             struct sysdb_attrs *attrs,
                             uint64_t cache_timeout,
                             time_t now)
{
    CHECK_DOMAIN_INFO(domain);

    return sysdb_store_group(domain->sysdb, name, gid, attrs, cache_timeout,
                             now);
}

errno_t sysdb_delete_domgroup(struct sss_domain_info *domain,
                              const char *name, gid_t gid)
{
    CHECK_DOMAIN_INFO(domain);

    return sysdb_delete_group(domain->sysdb, name, gid);
}