summaryrefslogtreecommitdiffstats
path: root/src/util/sss_ini.c
blob: e56006c05555d6e0c5e726e83771abce5a72b139 (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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
    SSSD

    sss_ini.c

    Authors:
        Ondrej Kos <okos@redhat.com>

    Copyright (C) 2013 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 <stdio.h>
#include <errno.h>
#include <talloc.h>

#include "config.h"
#include "util/util.h"
#include "util/sss_ini.h"
#include "confdb/confdb_setup.h"
#include "confdb/confdb_private.h"

#ifdef HAVE_LIBINI_CONFIG_V1
#include "ini_configobj.h"
#else
#include "collection.h"
#include "collection_tools.h"
#endif

#include "ini_config.h"


#ifdef HAVE_LIBINI_CONFIG_V1

struct sss_ini_initdata {
    char **error_list;
    struct ref_array *ra_success_list;
    struct ref_array *ra_error_list;
    struct ini_cfgobj *sssd_config;
    struct value_obj *obj;
    const struct stat *cstat;
    struct ini_cfgfile *file;
};

#define sss_ini_get_sec_list                   ini_get_section_list
#define sss_ini_get_attr_list                  ini_get_attribute_list
#define sss_ini_get_const_string_config_value  ini_get_const_string_config_value
#define sss_ini_get_config_obj                 ini_get_config_valueobj

#else

struct sss_ini_initdata {
    struct collection_item *error_list;
    struct collection_item *sssd_config;
    struct collection_item *obj;
    struct stat cstat;
    int file;
};

#define sss_ini_get_sec_list                   get_section_list
#define sss_ini_get_attr_list                  get_attribute_list
#define sss_ini_get_const_string_config_value  get_const_string_config_value
#define sss_ini_get_config_obj(secs,attrs,cfg,flag,attr) \
    get_config_item(secs,attrs,cfg,attr)

#endif


/* Initialize data structure */

struct sss_ini_initdata* sss_ini_initdata_init(TALLOC_CTX *mem_ctx)
{
    return talloc_zero(mem_ctx, struct sss_ini_initdata);
}



/* Close file descriptor */

void sss_ini_close_file(struct sss_ini_initdata *init_data)
{
    if (init_data == NULL) return;
#ifdef HAVE_LIBINI_CONFIG_V1
    if (init_data->file != NULL) {
        ini_config_file_destroy(init_data->file);
        init_data->file = NULL;
    }
#else
    if (init_data->file != -1) {
        close(init_data->file);
        init_data->file = -1;
    }
#endif
}



/* Open configuration file */

int sss_ini_config_file_open(struct sss_ini_initdata *init_data,
                             const char *config_file)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    return ini_config_file_open(config_file,
                                INI_META_STATS,
                                &init_data->file);
#else
    return check_and_open_readonly(config_file, &init_data->file, 0, 0,
                                   S_IFREG|S_IRUSR, /* f r**------ */
                                   S_IFMT|(ALLPERMS & ~(S_IWUSR|S_IXUSR)));
#endif
}



/* Check configuration file permissions */

int sss_ini_config_access_check(struct sss_ini_initdata *init_data)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    return ini_config_access_check(init_data->file,
                                   INI_ACCESS_CHECK_MODE |
                                   INI_ACCESS_CHECK_UID |
                                   INI_ACCESS_CHECK_GID,
                                   0, /* owned by root */
                                   0, /* owned by root */
                                   S_IRUSR, /* r**------ */
                                   ALLPERMS & ~(S_IWUSR|S_IXUSR));
#else
    return EOK;
#endif
}



/* Get cstat */

int sss_ini_get_stat(struct sss_ini_initdata *init_data)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    init_data->cstat = ini_config_get_stat(init_data->file);

    if (!init_data->cstat) return EIO;

    return EOK;
#else

    return fstat(init_data->file, &init_data->cstat);
#endif
}



/* Get mtime */

int sss_ini_get_mtime(struct sss_ini_initdata *init_data,
                      size_t timestr_len,
                      char *timestr)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    return snprintf(timestr, timestr_len, "%llu",
                    (long long unsigned)init_data->cstat->st_mtime);
#else
    return snprintf(timestr, timestr_len, "%llu",
                    (long long unsigned)init_data->cstat.st_mtime);
#endif
}



/* Print ini_config errors */

static void sss_ini_config_print_errors(char **error_list)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    unsigned count = 0;

    if (!error_list) {
        return;
    }

    while (error_list[count]) {
        DEBUG(SSSDBG_FATAL_FAILURE, "%s\n", error_list[count]);
        count++;
    }
#endif

    return;
}



/* Load configuration */

int sss_ini_get_config(struct sss_ini_initdata *init_data,
                       const char *config_file,
                       const char *config_dir)
{
    int ret;
#ifdef HAVE_LIBINI_CONFIG_V1
#ifdef HAVE_LIBINI_CONFIG_V1_3
    const char *patterns[] = { "^[^\\.].*\\.conf$", NULL };
    const char *sections[] = { ".*", NULL };
    uint32_t i = 0;
    char *msg = NULL;
    struct access_check snip_check;
    struct ini_cfgobj *modified_sssd_config = NULL;
#endif /* HAVE_LIBINI_CONFIG_V1_3 */

    /* Create config object */
    ret = ini_config_create(&(init_data->sssd_config));
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
                "Failed to create config object. Error %d.\n", ret);
        return ret;
    }

    /* Parse file */
    ret = ini_config_parse(init_data->file,
                           INI_STOP_ON_ANY,
                           INI_MV1S_OVERWRITE,
                           INI_PARSE_NOWRAP,
                           init_data->sssd_config);

    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
                "Failed to parse configuration. Error %d.\n", ret);

        if (ini_config_error_count(init_data->sssd_config)) {
            DEBUG(SSSDBG_FATAL_FAILURE,
                    "Errors detected while parsing: %s\n",
                     ini_config_get_filename(init_data->file));

            ini_config_get_errors(init_data->sssd_config,
                                  &init_data->error_list);
            sss_ini_config_print_errors(init_data->error_list);
            ini_config_free_errors(init_data->error_list);
        }
        ini_config_destroy(init_data->sssd_config);
        init_data->sssd_config = NULL;
        return ret;
    }

#ifdef HAVE_LIBINI_CONFIG_V1_3
    snip_check.flags = INI_ACCESS_CHECK_MODE | INI_ACCESS_CHECK_UID
                       | INI_ACCESS_CHECK_GID;
    snip_check.uid = 0; /* owned by root */
    snip_check.gid = 0; /* owned by root */
    snip_check.mode = S_IRUSR; /* r**------ */
    snip_check.mask = ALLPERMS & ~(S_IWUSR | S_IXUSR);

    ret = ini_config_augment(init_data->sssd_config,
                             config_dir,
                             patterns,
                             sections,
                             &snip_check,
                             INI_STOP_ON_ANY,
                             INI_MV1S_OVERWRITE,
                             INI_PARSE_NOWRAP,
                             INI_MV2S_OVERWRITE,
                             &modified_sssd_config,
                             &init_data->ra_error_list,
                             &init_data->ra_success_list);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Failed to augment configuration [%d]: %s",
              ret, sss_strerror(ret));
    }

    while (ref_array_get(init_data->ra_success_list, i, &msg) != NULL) {
        DEBUG(SSSDBG_TRACE_FUNC,
              "Config merge success: %s\n", msg);
        i++;
    }

    i = 0;
    while (ref_array_get(init_data->ra_error_list, i, &msg) != NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Config merge error: %s\n", msg);
        i++;
    }

    /* switch config objects if there are no errors */
    if (modified_sssd_config != NULL) {
        ini_config_destroy(init_data->sssd_config);
        init_data->sssd_config = modified_sssd_config;
    } else {
        DEBUG(SSSDBG_TRACE_FUNC,
              "Using only main configuration file due to errors in merging\n");
    }
#endif

    return ret;

#else

    /* Read the configuration into a collection */
    ret = config_from_fd("sssd",
                         init_data->file,
                         config_file,
                         &init_data->sssd_config,
                         INI_STOP_ON_ANY,
                         &init_data->error_list);

    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
                "Parse error reading configuration file [%s]\n",
                 config_file);

        print_file_parsing_errors(stderr, init_data->error_list);

        free_ini_config_errors(init_data->error_list);
        free_ini_config(init_data->sssd_config);

        return ret;
    }

    return EOK;

#endif
}

struct ref_array *
sss_ini_get_ra_success_list(struct sss_ini_initdata *init_data)
{
#ifdef HAVE_LIBINI_CONFIG_V1_3
    return init_data->ra_success_list;
#else
    return NULL;
#endif /* HAVE_LIBINI_CONFIG_V1_3 */
}

struct ref_array *
sss_ini_get_ra_error_list(struct sss_ini_initdata *init_data)
{
#ifdef HAVE_LIBINI_CONFIG_V1_3
    return init_data->ra_error_list;
#else
    return NULL;
#endif /* HAVE_LIBINI_CONFIG_V1_3 */
}

/* Get configuration object */

int sss_ini_get_cfgobj(struct sss_ini_initdata *init_data,
                       const char *section, const char *name)
{
    return sss_ini_get_config_obj(section,name, init_data->sssd_config,
                                  INI_GET_FIRST_VALUE, &init_data->obj);
}

/* Check configuration object */

int sss_ini_check_config_obj(struct sss_ini_initdata *init_data)
{
    if (init_data->obj == NULL) {
        return ENOENT;
    }

    return EOK;
}



/* Get integer value */

int sss_ini_get_int_config_value(struct sss_ini_initdata *init_data,
                                 int strict, int def, int *error)
{
#ifdef HAVE_LIBINI_CONFIG_V1
    return ini_get_int_config_value(init_data->obj, strict, def, error);
#else
    return get_int_config_value(init_data->obj, strict, def, error);
#endif
}



/* Destroy ini config (v1) */

void sss_ini_config_destroy(struct sss_ini_initdata *init_data)
{
    if (init_data == NULL) return;
#ifdef HAVE_LIBINI_CONFIG_V1
    if (init_data->sssd_config != NULL) {
        ini_config_destroy(init_data->sssd_config);
        init_data->sssd_config = NULL;
    }
#else
    free_ini_config(init_data->sssd_config);
#endif
}



/* Create LDIF */

int sss_confdb_create_ldif(TALLOC_CTX *mem_ctx,
                           struct sss_ini_initdata *init_data,
                           const char **config_ldif)
{
    int ret, i, j;
    char *ldif;
    char *tmp_ldif;
    char **sections;
    int section_count;
    char *dn;
    char *tmp_dn;
    char *sec_dn;
    char **attrs;
    int attr_count;
    char *ldif_attr;
    TALLOC_CTX *tmp_ctx;
    size_t dn_size;
    size_t ldif_len;
    size_t attr_len;
#ifdef HAVE_LIBINI_CONFIG_V1
    struct value_obj *obj = NULL;
#else
    struct collection_item *obj = NULL;
#endif

    ldif_len = strlen(CONFDB_INTERNAL_LDIF);
    ldif = talloc_array(mem_ctx, char, ldif_len+1);
    if (!ldif) return ENOMEM;

    tmp_ctx = talloc_new(ldif);
    if (!tmp_ctx) {
        ret = ENOMEM;
        goto error;
    }

    memcpy(ldif, CONFDB_INTERNAL_LDIF, ldif_len);

    /* Read in the collection and convert it to an LDIF */
    /* Get the list of sections */
    sections = sss_ini_get_sec_list(init_data->sssd_config,
                                    &section_count, &ret);
    if (ret != EOK) {
        goto error;
    }

    for (i = 0; i < section_count; i++) {
        const char *rdn = NULL;
        DEBUG(SSSDBG_TRACE_FUNC,
                "Processing config section [%s]\n", sections[i]);
        ret = parse_section(tmp_ctx, sections[i], &sec_dn, &rdn);
        if (ret != EOK) {
            goto error;
        }

        dn = talloc_asprintf(tmp_ctx,
                             "dn: %s,cn=config\n"
                             "cn: %s\n",
                             sec_dn, rdn);
        if (!dn) {
            ret = ENOMEM;
            free_section_list(sections);
            goto error;
        }
        dn_size = strlen(dn);

        /* Get all of the attributes and their values as LDIF */
        attrs = sss_ini_get_attr_list(init_data->sssd_config, sections[i],
                                   &attr_count, &ret);
        if (ret != EOK) {
            free_section_list(sections);
            goto error;
        }

        for (j = 0; j < attr_count; j++) {
            DEBUG(SSSDBG_TRACE_FUNC,
                    "Processing attribute [%s]\n", attrs[j]);
            ret = sss_ini_get_config_obj(sections[i], attrs[j],
                                         init_data->sssd_config,
                                         INI_GET_FIRST_VALUE, &obj);
            if (ret != EOK) goto error;

            const char *value = sss_ini_get_const_string_config_value(obj, &ret);
            if (ret != EOK) goto error;
            if (value && value[0] == '\0') {
                DEBUG(SSSDBG_CRIT_FAILURE,
                        "Attribute '%s' has empty value, ignoring\n",
                         attrs[j]);
                continue;
            }

            ldif_attr = talloc_asprintf(tmp_ctx,
                                        "%s: %s\n", attrs[j], value);
            DEBUG(SSSDBG_TRACE_ALL, "%s\n", ldif_attr);

            attr_len = strlen(ldif_attr);

            tmp_dn = talloc_realloc(tmp_ctx, dn, char,
                                    dn_size+attr_len+1);
            if (!tmp_dn) {
                ret = ENOMEM;
                free_attribute_list(attrs);
                free_section_list(sections);
                goto error;
            }
            dn = tmp_dn;
            memcpy(dn+dn_size, ldif_attr, attr_len+1);
            dn_size += attr_len;
        }

        dn_size ++;
        tmp_dn = talloc_realloc(tmp_ctx, dn, char,
                                dn_size+1);
        if (!tmp_dn) {
            ret = ENOMEM;
            free_attribute_list(attrs);
            free_section_list(sections);
            goto error;
        }
        dn = tmp_dn;
        dn[dn_size-1] = '\n';
        dn[dn_size] = '\0';

        DEBUG(SSSDBG_TRACE_ALL, "Section dn\n%s\n", dn);

        tmp_ldif = talloc_realloc(mem_ctx, ldif, char,
                                  ldif_len+dn_size+1);
        if (!tmp_ldif) {
            ret = ENOMEM;
            free_attribute_list(attrs);
            free_section_list(sections);
            goto error;
        }
        ldif = tmp_ldif;
        memcpy(ldif+ldif_len, dn, dn_size);
        ldif_len += dn_size;

        free_attribute_list(attrs);
        talloc_free(dn);
    }

    ldif[ldif_len] = '\0';

    free_section_list(sections);

    *config_ldif = (const char *)ldif;
    talloc_free(tmp_ctx);
    return EOK;

error:
    talloc_free(ldif);
    return ret;
}

#ifdef HAVE_LIBINI_CONFIG_V1_3
static int sss_ini_call_validators_errobj(struct sss_ini_initdata *data,
                                          const char *rules_path,
                                          struct ini_errobj *errobj)
{
    int ret;
    struct ini_cfgobj *rules_cfgobj = NULL;

    ret = ini_rules_read_from_file(rules_path, &rules_cfgobj);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
              "Failed to read sssd.conf schema %d [%s]\n", ret, strerror(ret));
        goto done;
    }

    ret = ini_rules_check(rules_cfgobj, data->sssd_config, NULL, errobj);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
              "ini_rules_check failed %d [%s]\n", ret, strerror(ret));
        goto done;
    }

done:
    if (rules_cfgobj) ini_config_destroy(rules_cfgobj);

    return ret;
}
#endif /* HAVE_LIBINI_CONFIG_V1_3 */

int sss_ini_call_validators(struct sss_ini_initdata *data,
                            const char *rules_path)
{
#ifdef HAVE_LIBINI_CONFIG_V1_3
    int ret;
    struct ini_errobj *errobj = NULL;

    ret = ini_errobj_create(&errobj);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Failed to create error list\n");
        goto done;
    }

    ret = sss_ini_call_validators_errobj(data,
                                         rules_path,
                                         errobj);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Failed to get errors from validators.\n");
        goto done;
    }

    /* Do not error out when validators find some issue */
    while (!ini_errobj_no_more_msgs(errobj)) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "%s\n", ini_errobj_get_msg(errobj));
        ini_errobj_next(errobj);
    }

    ret = EOK;

done:
    ini_errobj_destroy(&errobj);
    return ret;
#else
    DEBUG(SSSDBG_TRACE_FUNC,
          "libini_config does not support configuration file validataion\n");
    return EOK;
#endif /* HAVE_LIBINI_CONFIG_V1_3 */
}

int sss_ini_call_validators_strs(TALLOC_CTX *mem_ctx,
                                 struct sss_ini_initdata *data,
                                 const char *rules_path,
                                 char ***_errors,
                                 size_t *_num_errors)
{
#ifdef HAVE_LIBINI_CONFIG_V1_3
    TALLOC_CTX *tmp_ctx = NULL;
    struct ini_errobj *errobj = NULL;
    int ret;
    size_t num_errors;
    char **errors = NULL;

    if (_num_errors == NULL || _errors == NULL) {
        return EINVAL;
    }

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

    ret = ini_errobj_create(&errobj);
    if (ret != EOK) {
        goto done;
    }

    ret = sss_ini_call_validators_errobj(data,
                                         rules_path,
                                         errobj);
    if (ret != EOK) {
        goto done;
    }
    num_errors = ini_errobj_count(errobj);
    if (num_errors == 0) {
        *_num_errors = num_errors;
        goto done;
    }

    errors = talloc_array(tmp_ctx, char *, num_errors);
    if (errors == NULL) {
        ret = ENOMEM;
        goto done;
    }

    for (int i = 0; i < num_errors; i++) {
        errors[i] = talloc_strdup(errors, ini_errobj_get_msg(errobj));
        if (errors[i] == NULL) {
            ret = ENOMEM;
            goto done;
        }

        ini_errobj_next(errobj);
    }

    *_num_errors = num_errors;
    *_errors = talloc_steal(mem_ctx, errors);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    ini_errobj_destroy(&errobj);

    return ret;

#else
    DEBUG(SSSDBG_TRACE_FUNC,
          "libini_config does not support configuration file validataion\n");

    if (_num_errors == NULL || _errors == NULL) {
        return EINVAL;
    }

    _num_errors = 0;
    return EOK;
#endif /* HAVE_LIBINI_CONFIG_V1_3 */
}