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
|
/*
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 "config.h"
#include <stdlib.h>
#include <limits.h>
#include <talloc.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <signal.h>
#include <utime.h>
#include <ldb.h>
#include <popt.h>
#include <stdio.h>
#include <glob.h>
#include "util/util.h"
#include "tools/common/sss_tools.h"
#include "tools/common/sss_process.h"
#include "tools/sssctl/sssctl.h"
#include "tools/tools_util.h"
#include "confdb/confdb.h"
#include "sss_iface/sss_iface_sync.h"
#include "responder/ifp/ifp_iface/ifp_iface_sync.h"
#define LOG_FILE(file) " " LOG_PATH "/" file
#define LOG_FILES LOG_FILE("*.log")
#define SSS_ANALYZE SSSD_LIBEXEC_PATH"/sss_analyze"
#define CHECK(expr, done, msg) do { \
if (expr) { \
ERROR(msg "\n"); \
goto done; \
} \
} while(0)
#define POPT_SERV_OPTION(NAME, VAR, DESC) \
{services[SERV_ ## NAME].name, '\0', POPT_BIT_SET, &VAR, \
services[SERV_ ## NAME].mask, DESC, NULL}
#define STARTS_WITH(s, p) (strncmp((s), (p), strlen(p)) == 0)
#define REMOVE_PREFIX(s, p) (STARTS_WITH(s, p) ? (s) + strlen(p) : (s))
#define IS_DOMAIN(c) STARTS_WITH((c), "domain/")
#define DOMAIN_NAME(c) REMOVE_PREFIX((c), "domain/")
#define EMPTY_TARGETS(t) ((t)[0] == NULL)
enum debug_level_action {
ACTION_SET,
ACTION_GET
};
struct debuglevel_tool_ctx {
struct confdb_ctx *confdb;
char **sections;
};
struct sssctl_logs_opts {
int delete;
int archived;
};
struct sssctl_service_desc {
const char *name;
int mask;
};
enum serv_idx {
SERV_SSSD,
SERV_NSS,
SERV_PAM,
SERV_SUDO,
SERV_AUTOFS,
SERV_SSH,
SERV_PAC,
SERV_IFP,
SERV_COUNT
};
struct sssctl_service_desc services[] = {
{ "sssd", 1U << SERV_SSSD },
{ "nss", 1U << SERV_NSS },
{ "pam", 1U << SERV_PAM },
{ "sudo", 1U << SERV_SUDO },
{ "autofs", 1U << SERV_AUTOFS},
{ "ssh", 1U << SERV_SSH },
{ "pac", 1U << SERV_PAC },
{ "ifp", 1U << SERV_IFP }
};
static struct sbus_sync_connection *connect_to_sbus(TALLOC_CTX *mem_ctx)
{
struct sbus_sync_connection *conn;
conn = sbus_sync_connect_private(mem_ctx, SSS_MONITOR_ADDRESS, NULL);
if (conn == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Failed to connect to the sbus monitor\n");
}
return conn;
}
static const char *get_busname(TALLOC_CTX *mem_ctx, struct confdb_ctx *confdb,
const char *component)
{
errno_t ret;
const char *busname;
struct sss_domain_info *domain;
if (strcmp(component, "sssd") == 0) {
busname = talloc_strdup(mem_ctx, SSS_BUS_MONITOR);
} else if (IS_DOMAIN(component)) {
ret = confdb_get_domain(confdb, DOMAIN_NAME(component), &domain);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unknown domain: %s\n", component);
busname = NULL;
goto done;
}
busname = sss_iface_domain_bus(mem_ctx, domain);
} else {
busname = talloc_asprintf(mem_ctx, "sssd.%s", component);
}
done:
return busname;
}
/* in_out_value is an input argument when action is ACTION_SET; it is an output
* argument when action is ACTION_GET. */
static errno_t do_debug_level(enum debug_level_action action,
struct sbus_sync_connection *conn,
struct confdb_ctx *confdb,
const char *component,
uint32_t *in_out_value)
{
errno_t ret;
uint32_t value;
const char *busname;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
return ENOMEM;
}
busname = get_busname(tmp_ctx, confdb, component);
if (busname == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Failed to create the bus name for %s\n",
component);
}
if (action == ACTION_GET) {
ret = sbus_get_service_debug_level(conn, busname, SSS_BUS_PATH, &value);
if (ret != EOK) {
ret = ENOENT;
goto done;
}
*in_out_value = value;
} else {
ret = sbus_set_service_debug_level(conn, busname, SSS_BUS_PATH,
*in_out_value);
if (ret != EOK) {
ret = ENOENT;
goto done;
}
}
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
static errno_t sssctl_do_debug_level(enum debug_level_action action,
struct debuglevel_tool_ctx *tool_ctx,
const char **targets,
uint32_t debug_to_set)
{
bool all_targets = EMPTY_TARGETS(targets);
errno_t ret = EOK;
errno_t final_ret = EOK;
uint32_t current_level = SSSDBG_INVALID;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
const char *stripped_target;
const char **curr_target;
struct sbus_sync_connection *conn;
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
return ENOMEM;
}
conn = connect_to_sbus(tmp_ctx);
if (conn == NULL) {
ERROR("SSSD is not running.\n");
ret = EIO;
goto fini;
}
curr_target = (all_targets ?
discard_const_p(const char *, tool_ctx->sections) : targets);
while (*curr_target != NULL) {
stripped_target = REMOVE_PREFIX(*curr_target, "config/");
if (action == ACTION_GET) {
ret = do_debug_level(ACTION_GET, conn, tool_ctx->confdb,
stripped_target, ¤t_level);
CHECK(ret != EOK && ret != ENOENT, fini,
"Could not read the debug level.");
if (ret == EOK) {
PRINT(_("%1$-25s %2$#.4x\n"), stripped_target, current_level);
} else {
if (!all_targets) {
if (IS_DOMAIN(stripped_target)) {
PRINT(_("%1$-25s Unknown domain\n"), stripped_target);
} else {
PRINT(_("%1$-25s Unreachable service\n"), stripped_target);
}
final_ret = ENOENT;
}
}
} else {
ret = do_debug_level(ACTION_SET, conn, tool_ctx->confdb,
stripped_target, &debug_to_set);
CHECK(ret != EOK && ret != ENOENT, fini,
"Could not set the debug level.");
if (ret == ENOENT && !all_targets) {
final_ret = ret;
}
}
curr_target++;
}
if (ret == EOK) {
ret = final_ret;
}
fini:
talloc_free(tmp_ctx);
return ret;
}
errno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb,
char ***output_sections)
{
int ret;
int domain_count = 0;
int i = 0;
struct sss_domain_info *domain = NULL;
struct sss_domain_info *domain_list = NULL;
char **sections;
const char *known_services[] = {
CONFDB_MONITOR_CONF_ENTRY,
CONFDB_NSS_CONF_ENTRY,
CONFDB_PAM_CONF_ENTRY,
CONFDB_PAC_CONF_ENTRY,
CONFDB_SSH_CONF_ENTRY,
CONFDB_SUDO_CONF_ENTRY,
CONFDB_AUTOFS_CONF_ENTRY,
CONFDB_IFP_CONF_ENTRY,
};
static const int known_services_count = sizeof(known_services)
/ sizeof(*known_services);
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
return ENOMEM;
}
/* get domains */
ret = confdb_get_domains(confdb, &domain_list);
if (ret != EOK)
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get domain list\n");
for (domain = domain_list;
domain;
domain = get_next_domain(domain, 0)) {
domain_count++;
}
/* allocate output space */
sections = talloc_array(ctx, char *,
domain_count + known_services_count + 1);
if (sections == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for sections\n");
ret = ENOMEM;
goto fail;
}
for (i = 0; i < known_services_count; i++) {
sections[i] = talloc_strdup(tmp_ctx, known_services[i]);
if (sections[i] == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n");
ret = ENOMEM;
goto fail;
}
}
for (domain = domain_list;
domain;
domain = get_next_domain(domain, 0), i++) {
sections[i] = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL,
domain->name);
if (sections[i] == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
ret = ENOMEM;
goto fail;
}
}
/* add NULL to the end */
sections[i] = NULL;
*output_sections = talloc_steal(ctx, sections);
return EOK;
fail:
talloc_free(tmp_ctx);
return ret;
}
static const char **get_targets(TALLOC_CTX *mem_ctx, int services_mask,
const char **domainv)
{
int i;
int count = 1;
const char **targets = NULL;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
return NULL;
}
targets = talloc_zero_array(tmp_ctx, const char *, count);
if (targets == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for the list of targets\n");
goto done;
}
if (services_mask != 0) {
for (i = 0; i < SERV_COUNT; i++) {
if (services_mask == 0 || (services_mask & services[i].mask) != 0) {
targets = talloc_realloc(tmp_ctx, targets, const char *, count + 1);
if (targets == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for the list of targets\n");
goto done;
}
targets[count - 1] = talloc_strdup(tmp_ctx, services[i].name);
targets[count++] = NULL;
}
}
}
if (domainv != NULL) {
for (; *domainv != NULL; domainv++) {
targets = talloc_realloc(tmp_ctx, targets, const char *, count + 1);
if (targets == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for the list of targets\n");
goto done;
}
if (IS_DOMAIN(*domainv)) {
targets[count - 1] = talloc_strdup(tmp_ctx, *domainv);
} else {
targets[count - 1] = talloc_asprintf(tmp_ctx, "domain/%s", *domainv);
}
targets[count++] = NULL;
}
}
targets = talloc_steal(mem_ctx, targets);
for (i = 0; i < count; i++) {
targets[i] = talloc_steal(mem_ctx, targets[i]);
}
done:
talloc_free(tmp_ctx);
return targets;
}
int parse_debug_level(const char *strlevel)
{
long value;
char *endptr;
errno = 0;
value = strtol(strlevel, &endptr, 0);
if ((errno != 0) || (endptr == strlevel) || (*endptr != '\0')) {
return SSSDBG_INVALID;
}
return debug_convert_old_level(value);
}
errno_t sssctl_logs_remove(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
struct sssctl_logs_opts opts = {0};
errno_t ret;
glob_t globbuf;
/* Parse command line. */
struct poptOption options[] = {
{"delete", 'd', POPT_ARG_NONE, &opts.delete, 0, _("Delete log files instead of truncating"), 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 (opts.delete) {
PRINT("Deleting log files...\n");
ret = sss_remove_subtree(LOG_PATH);
if (ret != EOK) {
ERROR("Unable to remove log files\n");
return ret;
}
sss_signal(SIGHUP);
} else {
globbuf.gl_offs = 4;
ret = glob(LOG_PATH"/*.log", GLOB_ERR|GLOB_DOOFFS, NULL, &globbuf);
if (ret != 0) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to expand log files list\n");
return ret;
}
globbuf.gl_pathv[0] = discard_const_p(char, "truncate");
globbuf.gl_pathv[1] = discard_const_p(char, "--no-create");
globbuf.gl_pathv[2] = discard_const_p(char, "--size");
globbuf.gl_pathv[3] = discard_const_p(char, "0");
PRINT("Truncating log files...\n");
ret = sssctl_run_command((const char * const*)globbuf.gl_pathv);
globfree(&globbuf);
if (ret != EOK) {
ERROR("Unable to truncate log files\n");
return ret;
}
}
return EOK;
}
errno_t sssctl_logs_fetch(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
const char *file = NULL;
errno_t ret;
glob_t globbuf;
/* Parse command line. */
ret = sss_tool_popt_ex(cmdline, NULL, SSS_TOOL_OPT_OPTIONAL, NULL, NULL,
"FILE", "Output file", SSS_TOOL_OPT_REQUIRED,
&file, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
goto done;
}
globbuf.gl_offs = 3;
ret = glob(LOG_PATH"/*.log", GLOB_ERR|GLOB_DOOFFS, NULL, &globbuf);
if (ret != 0) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to expand log files list\n");
goto done;
}
globbuf.gl_pathv[0] = discard_const_p(char, "tar");
globbuf.gl_pathv[1] = discard_const_p(char, "-czf");
globbuf.gl_pathv[2] = discard_const_p(char, file);
PRINT("Archiving log files into %s...\n", file);
ret = sssctl_run_command((const char * const*)globbuf.gl_pathv);
globfree(&globbuf);
if (ret != EOK) {
ERROR("Unable to archive log files\n");
goto done;
}
done:
free(discard_const(file));
return ret;
}
errno_t sssctl_debug_level(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
int ret;
int pc_services = 0;
uint32_t debug_to_set = SSSDBG_INVALID;
const char **pc_domains = NULL;
const char **targets = NULL;
const char *debug_as_string = NULL;
struct debuglevel_tool_ctx *ctx = NULL;
struct poptOption long_options[] = {
{"domain", '\0', POPT_ARG_ARGV, &pc_domains,
0, _("Target a specific domain"), _("domain")},
POPT_SERV_OPTION(SSSD, pc_services, _("Target the SSSD service")),
POPT_SERV_OPTION(NSS, pc_services, _("Target the NSS service")),
POPT_SERV_OPTION(PAM, pc_services, _("Target the PAM service")),
POPT_SERV_OPTION(SUDO, pc_services, _("Target the SUDO service")),
POPT_SERV_OPTION(AUTOFS, pc_services, _("Target the AUTOFS service")),
POPT_SERV_OPTION(SSH, pc_services, _("Target the SSH service")),
POPT_SERV_OPTION(PAC, pc_services, _("Target the PAC service")),
POPT_SERV_OPTION(IFP, pc_services, _("Target the IFP service")),
POPT_TABLEEND
};
/* allocate context */
ctx = talloc_zero(NULL, struct debuglevel_tool_ctx);
if (ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for tools context\n");
ret = ENOMEM;
goto fini;
}
ret = sss_tool_popt_ex(cmdline, long_options, SSS_TOOL_OPT_OPTIONAL, NULL,
NULL, "DEBUG_LEVEL_TO_SET",
_("Specify debug level you want to set"),
SSS_TOOL_OPT_OPTIONAL, &debug_as_string, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
goto fini;
}
CHECK_ROOT(ret, debug_prg_name);
if (debug_as_string != NULL) {
debug_to_set = (uint32_t) parse_debug_level(debug_as_string);
CHECK(debug_to_set == SSSDBG_INVALID, fini, "Invalid debug level.");
}
/* Create a list with all the target names (services + domains) */
targets = get_targets(ctx, pc_services, pc_domains);
CHECK(targets == NULL, fini, "Could not allocate memory.");
ret = sss_tool_connect_to_confdb(ctx, &ctx->confdb);
CHECK(ret != EOK, fini, "Could not connect to configuration database.");
ret = get_confdb_sections(ctx, ctx->confdb, &ctx->sections);
CHECK(ret != EOK, fini, "Could not get all configuration sections.");
if (debug_as_string == NULL) {
ret = sssctl_do_debug_level(ACTION_GET, ctx, targets, 0);
} else {
ret = sssctl_do_debug_level(ACTION_SET, ctx, targets, debug_to_set);
}
/* Only report missing components that the user requested,
except for the monitor (sssd not running) */
if (ret != ENOENT && ret != EIO && EMPTY_TARGETS(targets)) {
ret = EOK;
}
fini:
talloc_free(ctx);
free(discard_const(debug_as_string));
return ret;
}
errno_t sssctl_analyze(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
#ifndef BUILD_CHAIN_ID
PRINT("ERROR: Tevent chain ID support missing, log analyzer is unsupported.\n");
return EOK;
#endif
errno_t ret;
ret = sssctl_wrap_command(SSS_ANALYZE, NULL, cmdline, tool_ctx, pvt);
return ret;
}
|