summaryrefslogtreecommitdiffstats
path: root/src/tools/sssctl/sssctl_domains.c
blob: 545ed95f4415da597b191146409ea6ba028f36f8 (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
/*
    Authors:
        Pavel Březina <pbrezina@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 <popt.h>
#include <stdio.h>

#include "util/util.h"
#include "tools/common/sss_tools.h"
#include "tools/sssctl/sssctl.h"
#include "sbus/sssd_dbus.h"
#include "responder/ifp/ifp_iface.h"

errno_t sssctl_domain_list(struct sss_cmdline *cmdline,
                           struct sss_tool_ctx *tool_ctx,
                           void *pvt)
{
    sss_sifp_ctx *sifp;
    sss_sifp_error error;
    char **domains;
    int start = 0;
    errno_t ret;
    int i;

    /* Parse command line. */
    struct poptOption options[] = {
        {"start", 's', POPT_ARG_NONE, &start, 0, _("Start SSSD if it is not running"), NULL },
        POPT_TABLEEND
    };

    ret = sss_tool_popt(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
        return ret;
    }

    if (!sssctl_start_sssd(start)) {
        return ERR_SSSD_NOT_RUNNING;
    }

    error = sssctl_sifp_init(tool_ctx, &sifp);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to connect to the InfoPipe");
        return EFAULT;
    }

    error = sss_sifp_list_domains(sifp, &domains);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to get domains list");
        return EIO;
    }

    for (i = 0; domains[i] != NULL; i++) {
        puts(domains[i]);
    }

    return EOK;
}

static errno_t sssctl_domain_status_online(struct sss_tool_ctx *tool_ctx,
                                           sss_sifp_ctx *sifp,
                                           const char *domain_path)
{
    TALLOC_CTX *tmp_ctx;
    sss_sifp_error error;
    DBusMessage *reply;
    bool is_online;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
        return ENOMEM;
    }

    error = sssctl_sifp_send(tmp_ctx, sifp, &reply, domain_path,
                             IFACE_IFP_DOMAINS_DOMAIN,
                             IFACE_IFP_DOMAINS_DOMAIN_ISONLINE);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to get online status");
        ret = EIO;
        goto done;
    }

    ret = sbus_parse_reply(reply, DBUS_TYPE_BOOLEAN, &is_online);
    if (ret != EOK) {
        goto done;
    }

    printf(_("Online status: %s\n"), is_online ? _("Online") : _("Offline"));

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

static const char *proper_service_name(const char *service)
{
    if (strcasecmp(service, "AD_GC") == 0) {
        return "AD Global Catalog";
    } else if (strcasecmp(service, "AD") == 0) {
        return "AD Domain Controller";
    } else if (strncasecmp(service, "sd_gc_", strlen("sd_gc_")) == 0) {
        return "AD Global Catalog";
    } else if (strncasecmp(service, "sd_", strlen("sd_")) == 0) {
        return "AD Domain Controller";
    }

    return service;
}

static errno_t sssctl_domain_status_active_server(struct sss_tool_ctx *tool_ctx,
                                                  sss_sifp_ctx *sifp,
                                                  const char *domain_path)
{
    TALLOC_CTX *tmp_ctx;
    sss_sifp_error error;
    DBusMessage *reply;
    const char *server;
    const char **services;
    int num_services;
    errno_t ret;
    int i;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
        return ENOMEM;
    }

    error = sssctl_sifp_send(tmp_ctx, sifp, &reply, domain_path,
                             IFACE_IFP_DOMAINS_DOMAIN,
                             IFACE_IFP_DOMAINS_DOMAIN_LISTSERVICES);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to list services");
        ret = EIO;
        goto done;
    }

    ret = sbus_parse_reply(reply, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
                           &services, &num_services);
    if (ret != EOK) {
        goto done;
    }

    printf(_("Active servers:\n"));
    for (i = 0; i < num_services; i++) {
        error = sssctl_sifp_send(tmp_ctx, sifp, &reply, domain_path,
                                 IFACE_IFP_DOMAINS_DOMAIN,
                                 IFACE_IFP_DOMAINS_DOMAIN_ACTIVESERVER,
                                 DBUS_TYPE_STRING, &services[i]);
        if (error != SSS_SIFP_OK) {
            sssctl_sifp_error(sifp, error, "Unable to get active server");
            ret = EIO;
            goto done;
        }

        ret = sbus_parse_reply(reply, DBUS_TYPE_STRING, &server);
        if (ret != EOK) {
            goto done;
        }

        server = SBUS_IS_STRING_EMPTY(server) ? _("not connected") : server;
        printf("%s: %s\n", proper_service_name(services[i]), server);
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

static errno_t sssctl_domain_status_server_list(struct sss_tool_ctx *tool_ctx,
                                                sss_sifp_ctx *sifp,
                                                const char *domain_path)
{
    TALLOC_CTX *tmp_ctx;
    sss_sifp_error error;
    DBusMessage *reply;
    const char **servers;
    int num_servers;
    const char **services;
    int num_services;
    errno_t ret;
    int i, j;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
        return ENOMEM;
    }

    error = sssctl_sifp_send(tmp_ctx, sifp, &reply, domain_path,
                             IFACE_IFP_DOMAINS_DOMAIN,
                             IFACE_IFP_DOMAINS_DOMAIN_LISTSERVICES);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to list services");
        ret = EIO;
        goto done;
    }

    ret = sbus_parse_reply(reply, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
                           &services, &num_services);
    if (ret != EOK) {
        goto done;
    }

    for (i = 0; i < num_services; i++) {
        printf(_("Discovered %s servers:\n"), proper_service_name(services[i]));
        error = sssctl_sifp_send(tmp_ctx, sifp, &reply, domain_path,
                                 IFACE_IFP_DOMAINS_DOMAIN,
                                 IFACE_IFP_DOMAINS_DOMAIN_LISTSERVERS,
                                 DBUS_TYPE_STRING, &services[i]);
        if (error != SSS_SIFP_OK) {
            sssctl_sifp_error(sifp, error, "Unable to get active server");
            ret = EIO;
            goto done;
        }

        ret = sbus_parse_reply(reply, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
                               &servers, &num_servers);
        if (ret != EOK) {
            goto done;
        }

        if (num_servers == 0) {
            puts(_("None so far.\n"));
            continue;
        }

        for (j = 0; j < num_servers; j++) {
            printf("- %s\n", servers[j]);
        }

        printf("\n");
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

struct sssctl_domain_status_opts {
    const char *domain;
    int online;
    int last;
    int active;
    int servers;
    int force_start;
};

errno_t sssctl_domain_status(struct sss_cmdline *cmdline,
                             struct sss_tool_ctx *tool_ctx,
                             void *pvt)
{
    struct sssctl_domain_status_opts opts = {0};
    sss_sifp_ctx *sifp;
    sss_sifp_error error;
    const char *path;
    bool opt_set;
    errno_t ret;

    /* Parse command line. */
    struct poptOption options[] = {
        {"online", 'o', POPT_ARG_NONE , &opts.online, 0, _("Show online status"), NULL },
        {"active-server", 'a', POPT_ARG_NONE, &opts.active, 0, _("Show information about active server"), NULL },
        {"servers", 'r', POPT_ARG_NONE, &opts.servers, 0, _("Show list of discovered servers"), NULL },
        {"start", 's', POPT_ARG_NONE, &opts.force_start, 0, _("Start SSSD if it is not running"), NULL },
        POPT_TABLEEND
    };

    ret = sss_tool_popt_ex(cmdline, options, SSS_TOOL_OPT_OPTIONAL,
                           NULL, NULL, "DOMAIN", _("Specify domain name."),
                           &opts.domain, &opt_set);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
        return ret;
    }

    if (opt_set == false) {
        opts.online = true;
        opts.last = true;
        opts.active = true;
        opts.servers = true;
    }

    path = sbus_opath_compose(tool_ctx, IFP_PATH_DOMAINS, opts.domain);
    if (path == NULL) {
        printf(_("Out of memory!\n"));
        return ENOMEM;
    }

    if (!sssctl_start_sssd(opts.force_start)) {
        return ERR_SSSD_NOT_RUNNING;
    }

    error = sssctl_sifp_init(tool_ctx, &sifp);
    if (error != SSS_SIFP_OK) {
        sssctl_sifp_error(sifp, error, "Unable to connect to the InfoPipe");
        return EFAULT;
    }

    if (opts.online) {
        ret = sssctl_domain_status_online(tool_ctx, sifp, path);
        if (ret != EOK) {
            fprintf(stderr, _("Unable to get online status\n"));
            return ret;
        }

        printf("\n");
    }

    if (opts.active) {
        ret = sssctl_domain_status_active_server(tool_ctx, sifp, path);
        if (ret != EOK) {
            fprintf(stderr, _("Unable to get online status\n"));
            return ret;
        }

        printf("\n");
    }

    if (opts.servers) {
        ret = sssctl_domain_status_server_list(tool_ctx, sifp, path);
        if (ret != EOK) {
            fprintf(stderr, _("Unable to get server list\n"));
            return ret;
        }
    }

    return EOK;
}