summaryrefslogtreecommitdiffstats
path: root/src/tools/sssctl/sssctl_config.c
blob: fc13582accd63f58c9d8bce59c4d6e898a96b170 (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
/*
    Authors:
        Michal Židek <mzidek@redhat.com>

    Copyright (C) 2016 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 "config.h"

#include <popt.h>
#include <stdio.h>
#include <ini_configobj.h>

#include "util/util.h"
#include "util/sss_ini.h"
#include "tools/common/sss_tools.h"
#include "tools/common/sss_process.h"
#include "tools/sssctl/sssctl.h"
#include "confdb/confdb.h"

#ifdef HAVE_LIBINI_CONFIG_V1_3
errno_t sssctl_config_check(struct sss_cmdline *cmdline,
                            struct sss_tool_ctx *tool_ctx,
                            void *pvt)
{
    errno_t ret;
    struct ini_errobj *errobj = NULL;
    struct sss_ini_initdata *init_data;
    struct ref_array *ra;
    char *msg;
    uint32_t i = 0;
    size_t num_errors;
    size_t num_ra_error;
    char **strs = NULL;
    TALLOC_CTX *tmp_ctx = NULL;

    tmp_ctx = talloc_new(NULL);
    init_data = sss_ini_initdata_init(tmp_ctx);
    if (!init_data) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory.\n");
        ret = ENOMEM;
        goto done;
    }

    /* Open config file */
    ret = sss_ini_config_file_open(init_data, SSSD_CONFIG_FILE);
    if (ret != EOK) {
        DEBUG(SSSDBG_TRACE_FUNC,
              "sss_ini_config_file_open failed: %s [%d]\n",
              sss_strerror(ret),
              ret);
        goto done;
    }

    /* Check the file permissions */
    ret = sss_ini_config_access_check(init_data);
    if (ret != EOK) {
        printf(_("Access check on sssd.conf file failed.\n"));
        ret = EPERM;
        goto done;
    }

    ret = sss_ini_get_config(init_data,
                             SSSD_CONFIG_FILE,
                             CONFDB_DEFAULT_CONFIG_DIR);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Failed to load configuration\n");
        goto done;
    }

    /* Read rules */
    ret = sss_ini_call_validators_strs(tmp_ctx, init_data,
                                       SSSDDATADIR"/cfg_rules.ini",
                                       &strs, &num_errors);
    if (ret) {
        goto done;
    }

    /* Output from validators */
    printf(_("Issues identified by validators: %lu\n"), num_errors);
    for (i = 0; i < num_errors; i++) {
        printf("%s\n", strs[i]);
    }

    /* Merging issues */
    ra = sss_ini_get_ra_error_list(init_data);
    num_ra_error = ref_array_len(ra);

    printf("\n");
    printf(_("Messages generated during configuration merging: %zu\n"),
           num_ra_error);

    i = 0;
    while (ref_array_get(ra, i, &msg) != NULL) {
        printf("%s\n", msg);
        i++;
    }

    /* Used snippet files */
    ra = sss_ini_get_ra_success_list(init_data);

    printf("\n");
    printf(_("Used configuration snippet files: %u\n"),
           ref_array_len(ra));

    i = 0;
    while (ref_array_get(ra, i, &msg) != NULL) {
        printf("%s\n", msg);
        i++;
    }

    if (num_errors != 0 || num_ra_error != 0) {
        ret = EINVAL;
    } else {
        ret = EOK;
    }

done:
    ini_errobj_destroy(&errobj);
    sss_ini_config_destroy(init_data);
    return ret;
}
#endif /* HAVE_LIBINI_CONFIG_V1_3 */