Debian Codegraph @

src/cli/main.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2017 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <time.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <locale.h>
29 #include <sys/stat.h>
30
31 #include "authselect.h"
32 #include "common/common.h"
33 #include "lib/util/string_array.h"
34 #include "cli/cli_tool.h"
35 #include "cli/presets.h"
36
37 #define CLI_ERROR(msg, ...) fprintf(stderr, gettext(msg), ## __VA_ARGS__)
38
39 #define CLI_PRINT(msg, ...) printf(gettext(msg), ## __VA_ARGS__)
40 #define CLI_MSG(quiet, msg, ...) do { \
41 if (!quiet) { \
42 CLI_PRINT((msg), ## __VA_ARGS__); \
43 } \
44 } while (0)
45
46 static size_t
47 list_max_length(char **list)
48 {
49 size_t max = 0;
50 size_t len;
51 int i;
52
53 for (i = 0; list[i] != NULL; i++) {
54 len = strlen(list[i]);
55 if (max < len) {
56 max = len;
57 }
58 }
59
60 return max;
61 }
62
63 static errno_t
64 parse_profile_options(struct cli_cmdline *cmdline,
65 struct poptOption *options,
66 char **_profile_id,
67 char ***_features)
68 {
69 char *profile_id;
70 char *preset_profile_id;
71 char **features;
72 char **merged_features;
73 bool profile_skipped;
74 errno_t ret;
75 int i;
76
77 *_profile_id = NULL;
78
79 ret = cli_tool_popt_ex(cmdline, options, CLI_TOOL_OPT_OPTIONAL,
80 NULL, NULL, "PROFILE-ID", _("Profile identifier."),
81 &profile_id, true, NULL);
82 if (ret != EOK) {
83 ERROR("Unable to parse command arguments");
84 return ret;
85 }
86
87 features = string_array_create(0);
88 if (features == NULL) {
89 ERROR("Unable to create string array");
90 return ENOMEM;
91 }
92
93 profile_skipped = false;
94 for (i = 0; i < cmdline->argc; i++) {
95 /* Skip options. */
96 if (strcmp(cmdline->argv[i], "--backup") == 0) {
97 /* Skip also the next parameter which is the backup name. */
98 i++;
99 continue;
100 }
101
102 if (cmdline->argv[i][0] == '-') {
103 continue;
104 }
105
106 /* First free option is profile name. We must skip it. For example:
107 * authselect select sssd with-feature --force
108 * authselect select --force sssd with-feature
109 */
110 if (!profile_skipped) {
111 profile_skipped = true;
112 continue;
113 }
114
115 features = string_array_add_value(features, cmdline->argv[i], true);
116 if (features == NULL) {
117 ret = ENOMEM;
118 goto done;
119 }
120 }
121
122 /* Resolve preset if needed */
123 if (cli_preset_is_preset(profile_id)) {
124 ret = cli_preset_resolve(profile_id, (const char **)features,
125 &preset_profile_id, &merged_features);
126 if (ret != EOK) {
127 goto done;
128 }
129
130 free(profile_id);
131 string_array_free(features);
132
133 profile_id = preset_profile_id;
134 features = merged_features;
135 }
136
137 *_profile_id = profile_id;
138 *_features = features;
139
140 ret = EOK;
141
142 done:
143 if (ret != EOK) {
144 free(profile_id);
145 string_array_free(features);
146 }
147
148 return ret;
149 }
150
151 static errno_t
152 perform_backup(int quiet,
153 int backup,
154 const char *backup_name)
155 {
156 char *backup_path;
157 errno_t ret;
158
159 if (!backup && backup_name == NULL) {
160 return EOK;
161 }
162
163 ret = authselect_backup(backup_name, &backup_path);
164 if (ret != EOK) {
165 CLI_ERROR("Unable to backup current configuration!\n");
166 return ret;
167 }
168
169 CLI_MSG(quiet, "Backup stored at %s\n", backup_path);
170 free(backup_path);
171
172 return EOK;
173 }
174
175 static errno_t activate(struct cli_cmdline *cmdline)
176 {
177 struct authselect_profile *profile = NULL;
178 char **features = NULL;
179 char *profile_id = NULL;
180 char *requirements = NULL;
181 char *backup_name = NULL;
182 char **maps = NULL;
183 int backup = 0;
184 int nobackup = 0;
185 int enforce = 0;
186 int quiet = 0;
187 errno_t ret;
188 int i;
189
190 struct poptOption options[] = {
191 {"force", 'f', POPT_ARG_VAL, &enforce, 1, _("Enforce changes"), NULL },
192 {NULL, 'b', POPT_ARG_VAL, &backup, 1, _("Backup system files before activating profile (generate unique name)"), NULL },
193 {"backup", '\0', POPT_ARG_STRING | POPT_ARG_NONE, &backup_name, 0, _("Backup system files before activating profile"), _("NAME") },
194 {"nobackup", '\0', POPT_ARG_VAL, &nobackup, 1, _("Do not backup system files when --force is set"), NULL },
195 {"quiet", 'q', POPT_ARG_VAL, &quiet, 1, _("Do not print profile requirements"), NULL },
196 POPT_TABLEEND
197 };
198
199 ret = parse_profile_options(cmdline, options, &profile_id, &features);
200 if (ret != EOK) {
201 goto done;
202 }
203
204 ret = authselect_profile(profile_id, &profile);
205 if (ret != EOK) {
206 ERROR("Unable to get profile information [%d]: %s",
207 ret, strerror(ret));
208 ret = ENOMEM;
209 goto done;
210 }
211
212 requirements = authselect_profile_requirements(profile, (const char **)features);
213 if (requirements == NULL) {
214 ERROR("Unable to read profile requirements!");
215 ret = EFAULT;
216 goto done;
217 }
218
219 if (backup || backup_name != NULL || (enforce && !nobackup)) {
220 ret = perform_backup(quiet, 1, backup_name);
221 if (ret != EOK) {
222 goto done;
223 }
224 }
225
226 ret = authselect_activate(profile_id, (const char **)features, enforce);
227 if (ret == EEXIST) {
228 CLI_ERROR("\nSome unexpected changes to the configuration were "
229 "detected.\nUse --force parameter if you want to overwrite "
230 "these changes.\n");
231 goto done;
232 } else if (ret != EOK) {
233 CLI_ERROR("Unable to activate profile [%d]: %s\n",
234 ret, strerror(ret));
235 goto done;
236 }
237
238 CLI_MSG(quiet, "Profile \"%s\" was selected.\n", profile_id);
239
240 if (maps != NULL && maps[0] != NULL) {
241 CLI_MSG(quiet, "The following nsswitch maps are overwritten "
242 "by the profile:\n");
243 for (i = 0; maps[i] != NULL; i++) {
244 CLI_MSG(quiet, "- %s\n", maps[i]);
245 }
246 }
247
248 if (requirements[0] != '\0') {
249 CLI_MSG(quiet, "\n%s\n", requirements);
250 }
251
252 ret = EOK;
253
254 done:
255 free(requirements);
256 authselect_array_free(maps);
257 authselect_profile_free(profile);
258 authselect_array_free((char **)features);
259 free(profile_id);
260
261 return ret;
262 }
263
264 static errno_t apply_changes(struct cli_cmdline *cmdline)
265 {
266 char *backup_name = NULL;
267 int backup = 0;
268 int upgrade = 0;
269 errno_t ret;
270
271 struct poptOption options[] = {
272 {"upgrade", '\0', POPT_ARG_VAL, &upgrade, 1, _("Only apply changes if installed profiles have changed"), NULL },
273 {NULL, 'b', POPT_ARG_VAL, &backup, 1, _("Backup system files before activating profile (generate unique name)"), NULL },
274 {"backup", '\0', POPT_ARG_STRING | POPT_ARG_NONE, &backup_name, 0, _("Backup system files before activating profile"), _("NAME") },
275 POPT_TABLEEND
276 };
277
278 ret = cli_tool_popt(cmdline, options, CLI_TOOL_OPT_OPTIONAL, NULL, NULL);
279 if (ret != EOK) {
280 ERROR("Unable to parse command arguments");
281 return ret;
282 }
283
284 ret = perform_backup(false, backup, backup_name);
285 if (ret != EOK) {
286 return ret;
287 }
288
289 ret = authselect_apply_changes(upgrade);
290 switch (ret) {
291 case EOK:
292 CLI_PRINT("Changes were successfully applied.\n");
293 break;
294 case EAGAIN:
295 CLI_PRINT("Installed profiles did not change. No action is needed.\n");
296 ret = EOK;
297 break;
298 case ENOENT:
299 CLI_ERROR("No existing configuration detected.\n");
300 break;
301 case EEXIST:
302 CLI_ERROR("Some unexpected changes to the configuration were "
303 "detected. Use 'select' command instead.\n");
304 break;
305 default:
306 CLI_ERROR("Unable to apply changes [%d]: %s\n", ret, strerror(ret));
307 break;
308 }
309
310 return ret;
311 }
312
313 static errno_t current(struct cli_cmdline *cmdline)
314 {
315 int raw_output = 0;
316 char *profile_id;
317 char **features;
318 errno_t ret;
319 int i;
320
321 struct poptOption options[] = {
322 {"raw", 'r', POPT_ARG_VAL, &raw_output, 1,
323 _("Print command parameters instead of formatted output"), NULL },
324 POPT_TABLEEND
325 };
326
327 ret = cli_tool_popt(cmdline, options, CLI_TOOL_OPT_OPTIONAL, NULL, NULL);
328 if (ret != EOK) {
329 ERROR("Unable to parse command arguments");
330 return ret;
331 }
332
333 ret = authselect_current_configuration(&profile_id, &features);
334 if (ret == ENOENT) {
335 CLI_PRINT("No existing configuration detected.\n");
336 return ret;
337 } else if (ret != EOK) {
338 ERROR("Unable to get current configuration [%d]: %s",
339 ret, strerror(ret));
340 return ret;
341 }
342
343 if (raw_output) {
344 printf("%s", profile_id);
345 if (features != NULL) {
346 for (i = 0; features[i] != NULL; i++) {
347 printf(" %s", features[i]);
348 }
349 }
350 printf("\n");
351 } else {
352 CLI_PRINT("Profile ID: %s\n", profile_id);
353 CLI_PRINT("Enabled features:");
354
355 if (features == NULL || features[0] == NULL) {
356 CLI_PRINT(" None\n");
357 } else {
358 printf("\n");
359 for (i = 0; features[i] != NULL; i++) {
360 printf("- %s\n", features[i]);
361 }
362 }
363 }
364
365 free(profile_id);
366 authselect_array_free(features);
367
368 return EOK;
369 }
370
371 static errno_t check(struct cli_cmdline *cmdline)
372 {
373 bool is_valid;
374 errno_t ret;
375
376 ret = cli_tool_popt(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL, NULL, NULL);
377 if (ret != EOK) {
378 ERROR("Unable to parse command arguments");
379 return ret;
380 }
381
382 ret = authselect_validate_configuration(&is_valid);
383 if (ret != EOK && ret != ENOENT && ret != EEXIST) {
384 ERROR("Unable to test current configuration [%d]: %s",
385 ret, strerror(ret));
386
387 return ret;
388 }
389
390 if (!is_valid) {
391 puts(_("Current configuration is not valid. "
392 "It was probably modified outside authselect."));
393 return EBADF;
394 }
395
396 switch (ret) {
397 case EOK:
398 puts(_("Current authselect configuration is valid."));
399 break;
400 case ENOENT:
401 puts(_("No configuration detected."));
402 ret = ENODEV;
403 break;
404 case EEXIST:
405 puts(_("System was not configured with authselect."));
406 ret = ENOENT; /* for backwards compatibility */
407 break;
408 }
409
410 /* EOK = existing configuration is valid,
411 * ENODEV = no configuration detected,
412 * ENOENT = non-authselet configuration is valid */
413 return ret;
414 }
415
416 static errno_t list(struct cli_cmdline *cmdline)
417 {
418 struct authselect_profile *profile;
419 char **profiles;
420 errno_t ret;
421 int preset_maxlen;
422 int maxlen;
423 int i;
424
425 ret = cli_tool_popt(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL, NULL, NULL);
426 if (ret != EOK) {
427 ERROR("Unable to parse command arguments");
428 return ret;
429 }
430
431 profiles = authselect_list();
432 if (profiles == NULL) {
433 ERROR("Unable to get profile list!");
434 return ENOMEM;
435 }
436
437 maxlen = list_max_length(profiles);
438
439 /* Use larger of profile names or preset names for alignment */
440 preset_maxlen = cli_preset_max_name_length();
441 if (preset_maxlen > maxlen) {
442 maxlen = preset_maxlen;
443 }
444
445 for (i = 0; profiles[i] != NULL; i++) {
446 ret = authselect_profile(profiles[i], &profile);
447 if (ret != EOK) {
448 ERROR("Unable to get profile information [%d]: %s",
449 ret, strerror(ret));
450 goto done;
451 }
452
453 printf("- %-*s\t %s\n", maxlen, profiles[i],
454 authselect_profile_name(profile));
455
456 authselect_profile_free(profile);
457 }
458
459 /* Show presets section */
460 struct cli_preset *preset_list = cli_preset_list();
461
462 if (preset_list != NULL && preset_list[0].name != NULL) {
463 printf("\nPresets:\n");
464
465 for (i = 0; preset_list[i].name != NULL; i++) {
466 printf("- %-*s\t %s\n", maxlen, preset_list[i].name,
467 preset_list[i].description);
468 }
469 }
470
471 cli_preset_list_free(preset_list);
472
473 ret = EOK;
474
475 done:
476 authselect_array_free(profiles);
477 return ret;
478 }
479
480 static errno_t list_features(struct cli_cmdline *cmdline)
481 {
482 struct authselect_profile *profile;
483 char *profile_id;
484 char **features;
485 errno_t ret;
486 int i;
487
488 ret = cli_tool_popt_ex(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL,
489 NULL, NULL, "PROFILE-ID", _("Profile identifier."),
490 &profile_id, true, NULL);
491 if (ret != EOK) {
492 ERROR("Unable to parse command arguments");
493 goto done;
494 }
495
496 /* Resolve preset */
497 if (cli_preset_is_preset(profile_id)) {
498 char *resolved_id;
499 ret = cli_preset_resolve(profile_id, NULL, &resolved_id, NULL);
500 if (ret != EOK) {
501 goto done;
502 }
503 free(profile_id);
504 profile_id = resolved_id;
505 }
506
507 ret = authselect_profile(profile_id, &profile);
508 if (ret != EOK) {
509 ERROR("Unable to get profile information [%d]: %s",
510 ret, strerror(ret));
511 goto done;
512 }
513
514 features = authselect_profile_features(profile);
515 authselect_profile_free(profile);
516 if (features == NULL) {
517 ERROR("Unable to get profile features [%d]: %s",
518 ret, strerror(ret));
519 ret = ENOMEM;
520 goto done;
521 }
522
523 for (i = 0; features[i] != NULL; i++) {
524 puts(features[i]);
525 }
526
527 authselect_array_free(features);
528
529 ret = EOK;
530
531 done:
532 free(profile_id);
533 return ret;
534 }
535
536 static errno_t show(struct cli_cmdline *cmdline)
537 {
538 struct authselect_profile *profile;
539 char *profile_id;
540 errno_t ret;
541 int i;
542
543 ret = cli_tool_popt_ex(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL,
544 NULL, NULL, "PROFILE-ID", _("Profile identifier."),
545 &profile_id, false, NULL);
546 if (ret != EOK) {
547 ERROR("Unable to parse command arguments");
548 goto done;
549 }
550
551 /* Handle presets */
552 if (cli_preset_is_preset(profile_id)) {
553 const struct cli_preset *preset = cli_preset_find(profile_id);
554 char *resolved_profile_id;
555 char **resolved_features;
556
557 ret = cli_preset_resolve(profile_id, NULL, &resolved_profile_id,
558 &resolved_features);
559 if (ret != EOK) {
560 goto done;
561 }
562
563 printf(_("Preset: %s\n"), preset->name);
564 printf(_("Description: %s\n"), preset->description);
565 printf(_("Source: %s\n\n"), preset->config_path);
566 printf(_("Resolves to:\n"));
567 printf(_(" Profile: %s\n"), resolved_profile_id);
568
569 if (resolved_features != NULL && resolved_features[0] != NULL) {
570 printf(_(" Features:\n"));
571 for (i = 0; resolved_features[i] != NULL; i++) {
572 printf(" - %s\n", resolved_features[i]);
573 }
574 } else {
575 printf(_(" Features: None\n"));
576 }
577
578 free(resolved_profile_id);
579 authselect_array_free(resolved_features);
580 free(profile_id);
581 return EOK;
582 }
583
584 ret = authselect_profile(profile_id, &profile);
585 if (ret != EOK) {
586 ERROR("Unable to get profile information [%d]: %s",
587 ret, strerror(ret));
588 ret = ENOMEM;
589 goto done;
590 }
591
592 puts(authselect_profile_description(profile));
593
594 authselect_profile_free(profile);
595
596 ret = EOK;
597
598 done:
599 free(profile_id);
600 return ret;
601 }
602
603 static errno_t requirements(struct cli_cmdline *cmdline)
604 {
605 struct authselect_profile *profile = NULL;
606 char *profile_id = NULL;
607 char **features;
608 char *requirements = NULL;
609 errno_t ret;
610
611 ret = parse_profile_options(cmdline, NULL, &profile_id, &features);
612 if (ret != EOK) {
613 goto done;
614 }
615
616 ret = authselect_profile(profile_id, &profile);
617 if (ret != EOK) {
618 ERROR("Unable to get profile information [%d]: %s",
619 ret, strerror(ret));
620 ret = ENOMEM;
621 goto done;
622 }
623
624 requirements = authselect_profile_requirements(profile, (const char **)features);
625 if (requirements == NULL) {
626 ERROR("Unable to read profile requirements!");
627 ret = EFAULT;
628 goto done;
629 } else if (requirements[0] == '\0') {
630 puts("No requirements are specified.");
631 } else {
632 puts(requirements);
633 }
634
635 ret = EOK;
636
637 done:
638 free(requirements);
639 authselect_array_free((char **)features);
640 free(profile_id);
641 authselect_profile_free(profile);
642
643 return ret;
644 }
645
646 static errno_t test(struct cli_cmdline *cmdline)
647 {
648 struct authselect_files *files;
649 char *profile_id = NULL;
650 char **features;
651 const char *content;
652 const char *path;
653 int print_all = 1;
654 int print_nsswitch = 0;
655 int print_systemauth = 0;
656 int print_passwordauth = 0;
657 int print_smartcardauth = 0;
658 int print_fingerprintauth = 0;
659 int print_switchableauth = 0;
660 int print_postlogin = 0;
661 int print_dconfdb = 0;
662 int print_dconflock = 0;
663 errno_t ret;
664 int i;
665
666 struct poptOption options[] = {
667 {"all", 'a', POPT_ARG_VAL, &print_all, 1, _("Print content of all files"), NULL },
668 {"nsswitch", 'n', POPT_ARG_VAL, &print_nsswitch, 1, _("Print nsswitch.conf content"), NULL },
669 {"system-auth", 's', POPT_ARG_VAL, &print_systemauth, 1, _("Print system-auth content"), NULL },
670 {"password-auth", 'p', POPT_ARG_VAL, &print_passwordauth, 1, _("Print password-auth content"), NULL },
671 {"smartcard-auth", 'c', POPT_ARG_VAL, &print_smartcardauth, 1, _("Print smartcard-auth content"), NULL },
672 {"fingerprint-auth", 'f', POPT_ARG_VAL, &print_fingerprintauth, 1, _("Print fingerprint-auth content"), NULL },
673 {"switchable-auth", 'w', POPT_ARG_VAL, &print_switchableauth, 1, _("Print switchable-auth content"), NULL },
674 {"postlogin", 'o', POPT_ARG_VAL, &print_postlogin, 1, _("Print postlogin content"), NULL },
675 {"dconf-db", 'd', POPT_ARG_VAL, &print_dconfdb, 1, _("Print dconf database content"), NULL },
676 {"dconf-lock", 'l', POPT_ARG_VAL, &print_dconflock, 1, _("Print dconf lock content"), NULL },
677 POPT_TABLEEND
678 };
679
680 struct {
681 const char * (*content_fn)(const struct authselect_files *);
682 const char * (*path_fn)(void);
683 int *enabled;
684 } generated[] = {
685 {authselect_files_nsswitch, authselect_path_nsswitch, &print_nsswitch},
686 {authselect_files_systemauth, authselect_path_systemauth, &print_systemauth},
687 {authselect_files_passwordauth, authselect_path_passwordauth, &print_passwordauth},
688 {authselect_files_smartcardauth, authselect_path_smartcardauth, &print_smartcardauth},
689 {authselect_files_fingerprintauth, authselect_path_fingerprintauth, &print_fingerprintauth},
690 {authselect_files_switchableauth, authselect_path_switchableauth, &print_switchableauth},
691 {authselect_files_postlogin, authselect_path_postlogin, &print_postlogin},
692 {authselect_files_dconf_db, authselect_path_dconf_db, &print_dconfdb},
693 {authselect_files_dconf_lock, authselect_path_dconf_lock, &print_dconflock},
694 {NULL, NULL, NULL}
695 };
696
697 ret = parse_profile_options(cmdline, options, &profile_id, &features);
698 if (ret != EOK) {
699 goto done;
700 }
701
702 ret = authselect_files(profile_id, (const char **)features, &files);
703 if (ret != EOK) {
704 ERROR("Unable to get generated content [%d]: %s", ret, strerror(ret));
705 goto done;
706 }
707
708 for (i = 0; generated[i].content_fn != NULL; i++) {
709 if (*generated[i].enabled == 1) {
710 print_all = 0;
711 }
712 }
713
714 for (i = 0; generated[i].content_fn != NULL; i++) {
715 if (!print_all && *generated[i].enabled == 0) {
716 continue;
717 }
718
719 path = generated[i].path_fn();
720 content = generated[i].content_fn(files);
721
722 if (content == NULL) {
723 CLI_PRINT("File %s: Empty\n\n", path);
724 } else {
725 CLI_PRINT("File %s:\n%s\n\n", path, content);
726 }
727 }
728
729 done:
730 authselect_array_free((char **)features);
731 free(profile_id);
732 return ret;
733 }
734
735 static errno_t enable(struct cli_cmdline *cmdline)
736 {
737 struct authselect_profile *profile = NULL;
738 char *backup_name = NULL;
739 char *requirements = NULL;
740 char *profile_id = NULL;
741 char *feature;
742 const char *features[2];
743 int backup = 0;
744 int quiet = 0;
745 errno_t ret;
746
747 struct poptOption options[] = {
748 {NULL, 'b', POPT_ARG_VAL, &backup, 1, _("Backup system files before activating profile (generate unique name)"), NULL },
749 {"backup", '\0', POPT_ARG_STRING | POPT_ARG_NONE, &backup_name, 0, _("Backup system files before activating profile"), _("NAME") },
750 {"quiet", 'q', POPT_ARG_VAL, &quiet, 1, _("Do not print profile requirements"), NULL },
751 POPT_TABLEEND
752 };
753
754 ret = cli_tool_popt_ex(cmdline, options, CLI_TOOL_OPT_OPTIONAL,
755 NULL, NULL, "FEATURE", _("Feature to enable."),
756 &feature, false, NULL);
757 if (ret != EOK) {
758 ERROR("Unable to parse command arguments");
759 return ret;
760 }
761
762 features[0] = feature;
763 features[1] = NULL;
764
765 ret = authselect_current_configuration(&profile_id, NULL);
766 if (ret == ENOENT) {
767 CLI_PRINT("No existing configuration detected.\n");
768 goto done;
769 } else if (ret != EOK) {
770 ERROR("Unable to get current configuration [%d]: %s",
771 ret, strerror(ret));
772 goto done;
773 }
774
775 ret = authselect_profile(profile_id, &profile);
776 if (ret != EOK) {
777 ERROR("Unable to get profile information [%d]: %s",
778 ret, strerror(ret));
779 ret = ENOMEM;
780 goto done;
781 }
782
783 requirements = authselect_profile_requirements(profile, (const char **)features);
784 if (requirements == NULL) {
785 ERROR("Unable to read profile requirements!");
786 ret = EFAULT;
787 goto done;
788 }
789
790 ret = perform_backup(quiet, backup, backup_name);
791 if (ret != EOK) {
792 CLI_ERROR("Unable to backup current configuration [%d]: %s\n",
793 ret, strerror(ret));
794 goto done;
795 }
796
797 ret = authselect_feature_enable(feature);
798 if (ret != EOK) {
799 CLI_ERROR("Unable to enable feature [%d]: %s\n", ret, strerror(ret));
800 goto done;
801 }
802
803 if (requirements[0] != '\0') {
804 CLI_MSG(quiet, "%s\n", requirements);
805 }
806
807 ret = EOK;
808
809 done:
810 free(profile_id);
811 free(requirements);
812 free(feature);
813 authselect_profile_free(profile);
814
815 return ret;
816 }
817
818 static errno_t disable(struct cli_cmdline *cmdline)
819 {
820 int backup = 0;
821 char *backup_name = NULL;
822 char *feature;
823 errno_t ret;
824
825 struct poptOption options[] = {
826 {NULL, 'b', POPT_ARG_VAL, &backup, 1, _("Backup system files before activating profile (generate unique name)"), NULL },
827 {"backup", '\0', POPT_ARG_STRING | POPT_ARG_NONE, &backup_name, 0, _("Backup system files before activating profile"), _("NAME") },
828 POPT_TABLEEND
829 };
830
831 ret = cli_tool_popt_ex(cmdline, options, CLI_TOOL_OPT_OPTIONAL,
832 NULL, NULL, "FEATURE", _("Feature to disable."),
833 &feature, false, NULL);
834 if (ret != EOK) {
835 ERROR("Unable to parse command arguments");
836 goto done;
837 }
838
839 ret = perform_backup(false, backup, backup_name);
840 if (ret != EOK) {
841 goto done;
842 }
843
844 ret = authselect_feature_disable(feature);
845 if (ret != EOK) {
846 CLI_ERROR("Unable to disable feature [%d]: %s\n", ret, strerror(ret));
847 goto done;
848 }
849
850 done:
851 free(feature);
852 return ret;
853 }
854
855 static errno_t is_enabled(struct cli_cmdline *cmdline)
856 {
857 char *feature;
858 errno_t ret;
859
860 ret = cli_tool_popt_ex(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL,
861 NULL, NULL, "FEATURE", _("Feature to check."),
862 &feature, false, NULL);
863 if (ret != EOK) {
864 ERROR("Unable to parse command arguments");
865 goto done;
866 }
867
868 ret = authselect_feature_enabled(feature);
869 if (ret != EOK && ret != ENOENT) {
870 CLI_ERROR("Unable to check feature [%d]: %s\n", ret, strerror(ret));
871 goto done;
872 }
873
874 done:
875 free(feature);
876 return ret;
877 }
878
879 static errno_t create(struct cli_cmdline *cmdline)
880 {
881 char *name;
882 const char *base_id = NULL;
883 enum authselect_profile_type type = AUTHSELECT_PROFILE_CUSTOM;
884 enum authselect_profile_type base_type = AUTHSELECT_PROFILE_ANY;
885 int symlink_flags = AUTHSELECT_SYMLINK_NONE;
886 const char **symlinks = NULL;
887 char *path = NULL;
888 errno_t ret;
889
890 struct poptOption options[] = {
891 {"vendor", 'v', POPT_ARG_VAL, &type, AUTHSELECT_PROFILE_VENDOR, _("Create new profile as a vendor profile instead of a custom profile"), NULL },
892 {"base-on", 'b', POPT_ARG_STRING, &base_id, 0, _("ID of a profile that should be used as a base for the new profile"), NULL },
893 {"base-on-default", '\0', POPT_ARG_VAL, &base_type, AUTHSELECT_PROFILE_DEFAULT, _("Base new profile on a default profile even if vendor profile with the same name exists"), NULL },
894 {"symlink-meta", '\0', POPT_ARG_VAL | POPT_ARGFLAG_OR, &symlink_flags, AUTHSELECT_SYMLINK_META, _("Symlink meta files from the base profile instead of copying them"), NULL },
895 {"symlink-nsswitch", '\0', POPT_ARG_VAL | POPT_ARGFLAG_OR, &symlink_flags, AUTHSELECT_SYMLINK_NSSWITCH, _("Symlink nsswitch files from the base profile instead of copying them"), NULL },
896 {"symlink-pam", '\0', POPT_ARG_VAL | POPT_ARGFLAG_OR, &symlink_flags, AUTHSELECT_SYMLINK_PAM, _("Symlink pam files from the base profile instead of copying them"), NULL },
897 {"symlink-dconf", '\0', POPT_ARG_VAL | POPT_ARGFLAG_OR, &symlink_flags, AUTHSELECT_SYMLINK_DCONF, _("Symlink dconf files from the base profile instead of copying them"), NULL },
898 {"symlink", 's', POPT_ARG_ARGV, &symlinks, 0, _("Symlink specific file (can be set multiple times)"), NULL },
899 POPT_TABLEEND
900 };
901
902 ret = cli_tool_popt_ex(cmdline, options, CLI_TOOL_OPT_OPTIONAL,
903 NULL, NULL, "NAME", _("New profile name."),
904 &name, false, NULL);
905 if (ret != EOK) {
906 ERROR("Unable to parse command arguments");
907 goto done;
908 }
909
910 ret = authselect_profile_create(name, type, base_id, base_type,
911 symlink_flags, symlinks, &path);
912 if (ret != EOK) {
913 CLI_ERROR("Unable to create new profile [%d]: %s\n", ret, strerror(ret));
914 goto done;
915 }
916
917 CLI_PRINT("New profile was created at %s\n", path);
918
919 done:
920 free(path);
921 free(name);
922 return ret;
923 }
924
925 static errno_t backup_list(struct cli_cmdline *cmdline)
926 {
927 int raw_output = 0;
928 char fmttime[255];
929 struct stat st;
930 struct tm tm;
931 char **names;
932 char *path;
933 int max = 0;
934 int len;
935 errno_t ret;
936 int i;
937
938 struct poptOption options[] = {
939 {"raw", 'r', POPT_ARG_VAL, &raw_output, 1,
940 _("Print backup names without any formatting and additional information"), NULL },
941 POPT_TABLEEND
942 };
943
944 ret = cli_tool_popt(cmdline, options, CLI_TOOL_OPT_OPTIONAL, NULL, NULL);
945 if (ret != EOK) {
946 ERROR("Unable to parse command arguments");
947 return ret;
948 }
949
950 names = authselect_backup_list();
951 if (names == NULL) {
952 ERROR("Unable to list available backups!");
953 return ENOMEM;
954 }
955
956 if (raw_output) {
957 for (i = 0; names[i] != NULL; i++) {
958 printf("%s\n", names[i]);
959 }
960 } else {
961 for (i = 0; names[i] != NULL; i++) {
962 len = strlen(names[i]);
963 if (max < len) {
964 max = len;
965 }
966 }
967
968 for (i = 0; names[i] != NULL; i++) {
969 path = format("%s/%s", AUTHSELECT_BACKUP_DIR, names[i]);
970 if (path == NULL) {
971 ERROR("Out of memory!");
972 ret = ENOMEM;
973 goto done;
974 }
975
976 ret = stat(path, &st);
977 if (ret < 0) {
978 ret = errno;
979 ERROR("Unable to stat [%s] [%d]: %s", path, ret, strerror(ret));
980 free(path);
981 goto done;
982 }
983 free(path);
984
985 localtime_r(&st.st_ctim.tv_sec, &tm);
986 memset(fmttime, '\0', sizeof(fmttime));
987 strftime(fmttime, 255, "%c", &tm);
988
989 printf(_("%-*s (created at %s)\n"), max, names[i], fmttime);
990 }
991 }
992
993 ret = EOK;
994
995 done:
996 authselect_array_free(names);
997
998 return ret;
999 }
1000
1001 static errno_t backup_remove(struct cli_cmdline *cmdline)
1002 {
1003 char *name;
1004 errno_t ret;
1005
1006 ret = cli_tool_popt_ex(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL,
1007 NULL, NULL, "BACKUP",
1008 _("Name of the backup to remove."),
1009 &name, false, NULL);
1010 if (ret != EOK) {
1011 ERROR("Unable to parse command arguments");
1012 goto done;
1013 }
1014
1015 ret = authselect_backup_remove(name);
1016 if (ret != EOK) {
1017 CLI_ERROR("Unable to remove backup [%s] [%d]: %s\n",
1018 name, ret, strerror(ret));
1019 goto done;
1020 }
1021
1022 done:
1023 free(name);
1024 return ret;
1025 }
1026
1027 static errno_t backup_restore(struct cli_cmdline *cmdline)
1028 {
1029 char *name;
1030 errno_t ret;
1031
1032 ret = cli_tool_popt_ex(cmdline, NULL, CLI_TOOL_OPT_OPTIONAL,
1033 NULL, NULL, "BACKUP",
1034 _("Name of the backup to restore from."),
1035 &name, false, NULL);
1036 if (ret != EOK) {
1037 ERROR("Unable to parse command arguments");
1038 goto done;
1039 }
1040
1041 ret = authselect_backup_restore(name);
1042 if (ret != EOK) {
1043 CLI_ERROR("Unable to restore backup [%s] [%d]: %s\n",
1044 name, ret, strerror(ret));
1045 goto done;
1046 }
1047
1048 done:
1049 free(name);
1050 return ret;
1051 }
1052
1053 static errno_t uninstall(struct cli_cmdline *cmdline)
1054 {
1055 errno_t ret;
1056
1057 ret = authselect_uninstall();
1058 if (ret != EOK) {
1059 CLI_ERROR("Unable to uninstall authselect configuration [%d]: %s\n",
1060 ret, strerror(ret));
1061 return ret;
1062 }
1063
1064 return EOK;
1065 }
1066
1067 static errno_t version(struct cli_cmdline *cmdline)
1068 {
1069 puts(PACKAGE_VERSION);
1070
1071 return EOK;
1072 }
1073
1074 static errno_t
1075 setup_gettext()
1076 {
1077 char *c;
1078
1079 /* Setup gettext even if we were unable to setup locale. */
1080 setlocale(LC_ALL, "");
1081
1082 errno = 0;
1083 c = bindtextdomain(PACKAGE, LOCALEDIR);
1084 if (c == NULL) {
1085 return errno;
1086 }
1087
1088 errno = 0;
1089 c = textdomain(PACKAGE);
1090 if (c == NULL) {
1091 return errno;
1092 }
1093
1094 return EOK;
1095 }
1096
1097 int main(int argc, const char **argv)
1098 {
1099 errno_t ret;
1100
1101 ret = setup_gettext();
1102 if (ret != EOK) {
1103 /* We can't use gettext here since it would crash. */
1104 fprintf(stderr, "Unable to setup gettext!\n");
1105 return 1;
1106 }
1107
1108 struct cli_route_cmd commands[] = {
1109 CLI_TOOL_COMMAND("select", "Select profile", CLI_CMD_REQUIRE_ROOT, activate),
1110 CLI_TOOL_COMMAND("apply-changes", "Regenerate configuration for currently selected command", CLI_CMD_REQUIRE_ROOT, apply_changes),
1111 CLI_TOOL_COMMAND("list", "List available profiles", CLI_CMD_NONE, list),
1112 CLI_TOOL_COMMAND("list-features", "List available profile features", CLI_CMD_NONE, list_features),
1113 CLI_TOOL_COMMAND("show", "Show profile information", CLI_CMD_NONE, show),
1114 CLI_TOOL_COMMAND("requirements", "Print profile requirements", CLI_CMD_NONE, requirements),
1115 CLI_TOOL_COMMAND("current", "Get identifier of currently selected profile", CLI_CMD_NONE, current),
1116 CLI_TOOL_COMMAND("check", "Check if the current configuration is valid", CLI_CMD_NONE, check),
1117 CLI_TOOL_COMMAND("test", "Print changes that would be otherwise written", CLI_CMD_NONE, test),
1118 CLI_TOOL_COMMAND("enable-feature", "Enable feature in currently selected profile", CLI_CMD_REQUIRE_ROOT, enable),
1119 CLI_TOOL_COMMAND("disable-feature", "Disable feature in currently selected profile", CLI_CMD_REQUIRE_ROOT, disable),
1120 CLI_TOOL_COMMAND("is-feature-enabled", "Check if feature is enabled in currently selected profile", CLI_CMD_NONE, is_enabled),
1121 CLI_TOOL_COMMAND("create-profile", "Create new authselect profile", CLI_CMD_REQUIRE_ROOT, create),
1122 CLI_TOOL_DELIMITER("Backup commands:"),
1123 CLI_TOOL_COMMAND("backup-list", "List available backups", CLI_CMD_NONE, backup_list),
1124 CLI_TOOL_COMMAND("backup-remove", "Remove backup", CLI_CMD_REQUIRE_ROOT, backup_remove),
1125 CLI_TOOL_COMMAND("backup-restore", "Restore from backup", CLI_CMD_REQUIRE_ROOT, backup_restore),
1126 CLI_TOOL_DELIMITER("Other:"),
1127 CLI_TOOL_COMMAND("opt-out", "Opt-out from authselect managed configuration", CLI_CMD_REQUIRE_ROOT, uninstall),
1128 /* Hidden commands */
1129 CLI_TOOL_COMMAND("version", "Print authselect version", CLI_CMD_HIDDEN, version),
1130 CLI_TOOL_LAST
1131 };
1132
1133 return cli_tool_main(argc, argv, commands, NULL);
1134 }