sss_tools.c revision 3bc651a611a3e5be508875f3ae58bfb5ece2525c
6b26240dca29e026900a83d51c75ca230a072a16Thiemo Wiedemeyer Pavel Březina <pbrezina@redhat.com>
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu Copyright (C) 2015 Red Hat
2eeec5240b424984e3ee26296da1eeab6c6d739eChristian Maeder This program is free software; you can redistribute it and/or modify
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer it under the terms of the GNU General Public License as published by
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer the Free Software Foundation; either version 3 of the License, or
7520452bb30b5abbd471f82352fc4c1c937e02c5Till Mossakowski (at your option) any later version.
7520452bb30b5abbd471f82352fc4c1c937e02c5Till Mossakowski This program is distributed in the hope that it will be useful,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer but WITHOUT ANY WARRANTY; without even the implied warranty of
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer GNU General Public License for more details.
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer You should have received a copy of the GNU General Public License
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer along with this program. If not, see <http://www.gnu.org/licenses/>.
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyerstatic void sss_tool_print_common_opts(void)
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer _("The debug level to run with"));
3a6decfd748f532d5cb03fbcb7a42fa37b0faab3Christian Maeder _("Show this for a command"));
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer _("Show brief usage message for a command"));
b6a59f004903ac7bc96323ee3ef09c01fd221157Christian Maederstatic struct poptOption *sss_tool_common_opts_table(void)
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer common_opts[0].descrip = _("The debug level to run with");
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyerstatic void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &debug,
4b136ad539bd9f4e115dff4eee4d552a42d4437eChristian Maeder 0, _("The debug level to run with"), NULL },
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer while ((opt = poptGetNextOpt(pc)) != -1) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* do nothing */
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* Strip common options from arguments. We will discard_const here,
ba7903578ca168160ed411ef534fa95c5cddf8f0Ewaryst Schulz * since it is not worth the trouble to convert it back and forth. */
2028dc2c091bb60343e15985948a59b955276cbfChristian Maeder *argc = poptStrippedArgv(pc, orig_argc, discard_const_p(char *, argv));
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanustatic errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer ret = confdb_init(mem_ctx, &confdb, path);
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer "Could not initialize connection to the confdb\n");
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyerstatic errno_t sss_tool_domains_init(TALLOC_CTX *mem_ctx,
0b8b26a22f136a9b2a8e99d655f6fe6b0b96008cThiemo Wiedemeyer ret = confdb_get_domains(confdb, &domains);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer ret = sysdb_init(mem_ctx, domains, false);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer "Could not initialize connection to the sysdb\n");
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer dom = get_next_domain(dom, SSS_GND_DESCEND)) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer /* Get flat name and domain ID (SID) from the cache
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer * if available */
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer DEBUG(SSSDBG_MINOR_FAILURE, "Failed to update domain %s.\n",
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder /* Update list of subdomains for this domain */
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer "Failed to update subdomains for domain %s.\n",
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer dom = get_next_domain(dom, SSS_GND_DESCEND)) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer ret = sss_names_init(mem_ctx, confdb, dom->name, &dom->names);
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "sss_names_init() failed\n");
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyerstruct sss_tool_ctx *sss_tool_init(TALLOC_CTX *mem_ctx,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer tool_ctx = talloc_zero(mem_ctx, struct sss_tool_ctx);
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer sss_tool_common_opts(tool_ctx, argc, argv);
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer /* Connect to confdb. */
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer /* Setup domains. */
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
3e3efd4ce838940032e875e6d08712a177c9c1d0Thiemo Wiedemeyer fprintf(stderr, _("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name);
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder fprintf(stderr, _("Available commands:\n"));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder for (i = 0; commands[i].command != NULL; i++) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder fprintf(stderr, "* %s\n", commands[i].command);
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer commands[i].command, commands[i].description);
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyerint sss_tool_route(int argc, const char **argv,
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: commands can't be NULL!\n");
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder for (i = 0; commands[i].command != NULL; i++) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer if (strcmp(commands[i].command, cmd) == 0) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return commands[i].fn(&cmdline, tool_ctx, pvt);
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyerstatic struct poptOption *nonnull_popt_table(struct poptOption *options)
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyerint sss_tool_popt_ex(struct sss_cmdline *cmdline,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu const char **_fopt,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu {NULL, '\0', POPT_ARG_INCLUDE_TABLE, nonnull_popt_table(options), \
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer {NULL, '\0', POPT_ARG_INCLUDE_TABLE, sss_tool_common_opts_table(), \
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer /* Create help option string. We always need to append command name since
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu * we use POPT_CONTEXT_KEEP_FIRST. */
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu help = talloc_asprintf(NULL, "%s %s %s", cmdline->exec,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer help = talloc_asprintf(NULL, "%s %s %s %s", cmdline->exec,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer cmdline->command, fopt_name, _("[OPTIONS...]"));
109b67ffce2bad83667e2f4a319d2d7f380f91afThiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* Create popt context. This function is supposed to be called on
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer * command argv which does not contain executable (argv[0]), therefore
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer * we need to use KEEP_FIRST that ensures argv[0] is also processed. */
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer pc = poptGetContext(cmdline->exec, cmdline->argc, cmdline->argv,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* Parse options. Invoke custom function if provided. If no parsing
f39c70229e74147a02d15bd45c05a0b1b325532dChristian Maeder * function is provided, print error on unknown option. */
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer while ((ret = poptGetNextOpt(pc)) != -1) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, _("Invalid option %s: %s\n\n"),
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* Parse free option which is always required if requested. */
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, _("Missing option: %s\n\n"), fopt_help);
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder /* No more arguments expected. If something follows it is an error. */
370e81d7af7821f0ac6ee0643613e87a727841e7Thiemo Wiedemeyer fprintf(stderr, _("Only one free argument is expected!\n\n"));
966a6c024c828387023fccb0cd0049f78687e5dcThiemo Wiedemeyer } else if (_fopt == NULL && fopt != NULL) {
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu /* Unexpected free argument. */
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder fprintf(stderr, _("Unexpected parameter: %s\n\n"), fopt);
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer if ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1) {
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder /* If at least one option is required and not provided, print error. */
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu if (require_option == SSS_TOOL_OPT_REQUIRED) {
28ca54b0d63d1d26a991711c8c7e85c474994715Christian Maeder fprintf(stderr, _("At least one option is required!\n\n"));
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maederint sss_tool_popt(struct sss_cmdline *cmdline,
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer return sss_tool_popt_ex(cmdline, options, require_option,
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer popt_fn, popt_fn_pvt, NULL, NULL, NULL, NULL);
1ac36418f204bbe56f4cd951a979180721758999Christian Maederint sss_tool_main(int argc, const char **argv,
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Running under %d, must be root\n", uid);
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder ERROR("%1$s must be run as root\n", argv[0]);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer tool_ctx = sss_tool_init(NULL, &argc, argv);
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tool context\n");
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = sss_tool_route(argc, argv, tool_ctx, commands, pvt);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyerint sss_tool_parse_name(TALLOC_CTX *mem_ctx,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = sss_parse_name_for_domains(mem_ctx, tool_ctx->domains,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to find domain. The domain name may "
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer "be a subdomain that was not yet found.\n");
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse name [%d]: %s\n",