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
|
/*
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 "db/sysdb.h"
#include "db/sysdb_private.h"
#include "confdb/confdb.h"
#include "confdb/confdb_private.h"
#include "tools/common/sss_process.h"
#include "tools/sssctl/sssctl.h"
#define SSS_BACKUP_DIR SSS_STATEDIR "/backup"
#define SSS_BACKUP_USER_OVERRIDES SSS_BACKUP_DIR "/sssd_user_overrides.bak"
#define SSS_BACKUP_GROUP_OVERRIDES SSS_BACKUP_DIR "/sssd_group_overrides.bak"
#define SSS_CACHE "sss_cache"
struct sssctl_data_opts {
int override;
int restore;
int start;
int stop;
int restart;
};
static errno_t sssctl_create_backup_dir(const char *path)
{
mode_t old_umask;
errno_t ret;
old_umask = umask(SSS_DFL_X_UMASK);
ret = mkdir(path, 0700);
umask(old_umask);
if (ret != EOK && errno != EEXIST) {
ret = errno;
DEBUG(SSSDBG_MINOR_FAILURE, "Unable to create backup directory "
"[%d]: %s\n", ret, sss_strerror(ret));
return ret;
}
return EOK;
}
static bool sssctl_backup_file_exists(const char *file)
{
return access(file, F_OK) == 0;
}
static bool sssctl_backup_exist(const char **files)
{
int i;
for (i = 0; files[i] != NULL; i++) {
if (sssctl_backup_file_exists(files[i])) {
return true;
}
}
return false;
}
static errno_t sssctl_backup(bool force)
{
const char *files[] = {SSS_BACKUP_USER_OVERRIDES,
SSS_BACKUP_GROUP_OVERRIDES,
NULL};
enum sssctl_prompt_result prompt;
errno_t ret;
ret = sssctl_create_backup_dir(SSS_BACKUP_DIR);
if (ret != EOK) {
ERROR("Unable to create backup directory [%d]: %s",
ret, sss_strerror(ret));
return ret;
}
if (sssctl_backup_exist(files) && !force) {
prompt = sssctl_prompt(_("SSSD backup of local data already exists, "
"override?"), SSSCTL_PROMPT_NO);
switch (prompt) {
case SSSCTL_PROMPT_YES:
/* continue */
break;
case SSSCTL_PROMPT_NO:
return EEXIST;
case SSSCTL_PROMPT_ERROR:
return EIO;
}
}
ret = sssctl_run_command((const char *[]){"sss_override", "user-export",
SSS_BACKUP_USER_OVERRIDES, NULL});
if (ret != EOK) {
ERROR("Unable to export user overrides\n");
return ret;
}
ret = sssctl_run_command((const char *[]){"sss_override", "group-export",
SSS_BACKUP_GROUP_OVERRIDES, NULL});
if (ret != EOK) {
ERROR("Unable to export group overrides\n");
return ret;
}
return ret;
}
errno_t sssctl_client_data_backup(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
struct sssctl_data_opts opts = {0};
errno_t ret;
/* Parse command line. */
struct poptOption options[] = {
{"override", 'o', POPT_ARG_NONE, &opts.override, 0, _("Override existing backup"), 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;
}
ret = sssctl_backup(opts.override);
if (ret == EEXIST) {
return EOK;
}
return ret;
}
static errno_t sssctl_restore(bool force_start, bool force_restart)
{
errno_t ret;
if (!sssctl_start_sssd(force_start)) {
return ERR_SSSD_NOT_RUNNING;
}
if (sssctl_backup_file_exists(SSS_BACKUP_USER_OVERRIDES)) {
ret = sssctl_run_command((const char *[]){"sss_override", "user-import",
SSS_BACKUP_USER_OVERRIDES, NULL});
if (ret != EOK) {
ERROR("Unable to import user overrides\n");
return ret;
}
}
if (sssctl_backup_file_exists(SSS_BACKUP_USER_OVERRIDES)) {
ret = sssctl_run_command((const char *[]){"sss_override", "group-import",
SSS_BACKUP_GROUP_OVERRIDES, NULL});
if (ret != EOK) {
ERROR("Unable to import group overrides\n");
return ret;
}
}
sssctl_restart_sssd(force_restart);
ret = EOK;
return ret;
}
errno_t sssctl_client_data_restore(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
struct sssctl_data_opts opts = {0};
errno_t ret;
/* Parse command line. */
struct poptOption options[] = {
{"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD if it is not running"), NULL },
{"restart", 'r', POPT_ARG_NONE, &opts.restart, 0, _("Restart SSSD after data import"), 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;
}
return sssctl_restore(opts.start, opts.restart);
}
errno_t sssctl_cache_remove(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
struct sssctl_data_opts opts = {0};
errno_t ret;
/* Parse command line. */
struct poptOption options[] = {
{"override", 'o', POPT_ARG_NONE, &opts.override, 0, _("Override existing backup"), NULL },
{"restore", 'r', POPT_ARG_NONE, &opts.restore, 0, _("Create clean cache files and import local data"), NULL },
{"stop", 'p', POPT_ARG_NONE, &opts.stop, 0, _("Stop SSSD before removing the cache"), NULL },
{"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD when the cache is removed"), 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_stop_sssd(opts.stop)) {
fprintf(stderr, "Unable to remove the cache unless SSSD is stopped.\n");
return ERR_SSSD_RUNNING;
}
PRINT("Creating backup of local data...\n");
ret = sssctl_backup(opts.override);
if (ret != EOK) {
ERROR("Unable to create backup of local data,"
" can not remove the cache.\n");
return ret;
}
PRINT("Removing cache files...\n");
ret = sss_remove_subtree(DB_PATH);
if (ret != EOK) {
ERROR("Unable to remove cache files\n");
return ret;
}
if (opts.restore) {
PRINT("Restoring local data...\n");
sssctl_restore(opts.start, opts.start);
} else {
sssctl_start_sssd(opts.start);
}
return EOK;
}
errno_t sssctl_cache_upgrade(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
struct sysdb_upgrade_ctx db_up_ctx;
errno_t ret;
ret = sss_tool_popt(cmdline, NULL, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
return ret;
}
if (sss_daemon_running()) {
return ERR_SSSD_RUNNING;
}
ret = confdb_get_domains(tool_ctx->confdb, &tool_ctx->domains);
if (ret != EOK) {
DEBUG(SSSDBG_FATAL_FAILURE, "No domains configured.\n");
return ret;
}
db_up_ctx.cdb = tool_ctx->confdb;
ret = sysdb_init_ext(tool_ctx, tool_ctx->domains, &db_up_ctx,
true, 0, 0);
if (ret != EOK) {
SYSDB_VERSION_ERROR_DAEMON(ret);
return ret;
}
return EOK;
}
errno_t sssctl_cache_expire(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
errno_t ret;
const char **args = talloc_array_size(tool_ctx,
sizeof(char *),
cmdline->argc + 2);
if (!args) {
return ENOMEM;
}
memcpy(&args[1], cmdline->argv, sizeof(char *) * cmdline->argc);
args[0] = SSS_CACHE;
args[cmdline->argc + 1] = NULL;
ret = sssctl_run_command(args);
talloc_free(args);
return ret;
}
errno_t get_confdb_domains(TALLOC_CTX *ctx, struct confdb_ctx *confdb,
char ***_domains)
{
int ret;
int domain_count = 0;
int i;
struct sss_domain_info *domain = NULL;
struct sss_domain_info *domain_list = NULL;
char **domains;
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");
goto done;
}
for (domain = domain_list;
domain;
domain = get_next_domain(domain, 0)) {
domain_count++;
}
/* allocate output space */
domains = talloc_array(tmp_ctx, char *, domain_count + 1);
if (domains == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not allocate memory for domains\n");
ret = ENOMEM;
goto done;
}
for (domain = domain_list, i = 0;
domain != NULL;
domain = get_next_domain(domain, 0), i++) {
domains[i] = talloc_asprintf(domains, "%s", domain->name);
if (domains[i] == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
ret = ENOMEM;
goto done;
}
}
/* add NULL to the end */
domains[i] = NULL;
*_domains = talloc_steal(ctx, domains);
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
static errno_t sssctl_cache_index_action(enum sysdb_index_actions action,
const char **domains,
const char *attr)
{
errno_t ret;
TALLOC_CTX *tmp_ctx = NULL;
struct confdb_ctx *confdb = NULL;
char *cache;
const char **domain;
const char **index;
const char **indexes = NULL;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Failed to allocate the context\n");
return ENOMEM;
}
if (domains == NULL) {
/* If the user selected no domain, act on all of them */
ret = sss_tool_connect_to_confdb(tmp_ctx, &confdb);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"Could not connect to configuration database.\n");
goto done;
}
ret = get_confdb_domains(tmp_ctx, confdb, discard_const(&domains));
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "Could not list all the domains.\n");
goto done;
}
}
for (domain = domains; *domain != NULL; domain++) {
if (action == SYSDB_IDX_CREATE) {
PRINT("Creating cache index for domain %1$s\n", *domain);
} else if (action == SYSDB_IDX_DELETE) {
PRINT("Deleting cache index for domain %1$s\n", *domain);
} else if (action == SYSDB_IDX_LIST) {
PRINT("Indexes for domain %1$s:\n", *domain);
} else {
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid action: %i\n", action);
ret = EINVAL;
goto done;
}
ret = sysdb_get_db_file(tmp_ctx, NULL, *domain, DB_PATH, &cache, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "Failed to get the cache db name\n");
goto done;
}
ret = sysdb_manage_index(tmp_ctx, action, cache, attr, &indexes);
if (ret != EOK) {
goto done;
}
if (action == SYSDB_IDX_LIST) {
for (index = indexes; *index != NULL; index++) {
PRINT(" Attribute: %1$s\n", *index);
}
talloc_zfree(indexes);
}
}
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t sssctl_cache_index(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
{
const char *attr = NULL;
const char *action_str = NULL;
const char **domains = NULL;
const char **p;
enum sysdb_index_actions action;
errno_t ret;
/* Parse command line. */
struct poptOption options[] = {
{ "domain", 'd', POPT_ARG_ARGV, &domains,
0, _("Target a specific domain"), _("domain") },
{ "attribute", 'a', POPT_ARG_STRING, &attr,
0, _("Attribute to index"), _("attribute") },
POPT_TABLEEND
};
ret = sss_tool_popt_ex(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL,
"ACTION", "create | delete | list",
SSS_TOOL_OPT_REQUIRED, &action_str, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
goto done;
}
if (action_str == NULL) {
ERROR("Action not provided\n");
ret = EINVAL;
goto done;
}
if (strcmp(action_str, "list") == 0) {
action = SYSDB_IDX_LIST;
} else {
if (strcmp(action_str, "create") == 0) {
action = SYSDB_IDX_CREATE;
} else if (strcmp(action_str, "delete") == 0) {
action = SYSDB_IDX_DELETE;
} else {
ERROR("Unknown action: %1$s\nValid actions are "
"\"%2$s\", \"%3$s and \"%4$s\"\n",
action_str, "create", "delete", "list");
ret = EINVAL;
goto done;
}
if (attr == NULL) {
ERROR("Attribute (-a) not provided\n");
ret = EINVAL;
goto done;
}
}
ret = sssctl_cache_index_action(action, domains, attr);
if (ret == ENOENT) {
ERROR("Attribute %1$s not indexed.\n", attr);
goto done;
} if (ret == EEXIST) {
ERROR("Attribute %1$s already indexed.\n", attr);
goto done;
} else if (ret != EOK) {
ERROR("Index operation failed: %1$s\n", sss_strerror(ret));
goto done;
}
if (action != SYSDB_IDX_LIST) {
PRINT("Don't forget to also update the indexes on the remote providers.\n");
}
ret = EOK;
done:
free(discard_const(action_str));
free(discard_const(attr));
if (domains != NULL) {
for (p = domains; *p != NULL; p++) {
free(discard_const(*p));
}
free(discard_const(domains));
}
return ret;
}
|