sss_tools.c revision 3bc651a611a3e5be508875f3ae58bfb5ece2525c
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer/*
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer Authors:
6b26240dca29e026900a83d51c75ca230a072a16Thiemo Wiedemeyer Pavel Březina <pbrezina@redhat.com>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu Copyright (C) 2015 Red Hat
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
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
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.
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
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/>.
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer*/
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
8bdd8707c8e341f1fc57fcbf884520bfe06a5028Christian Maeder#include <talloc.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include <stdlib.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include <string.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include <popt.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "config.h"
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "util/util.h"
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "confdb/confdb.h"
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder#include "db/sysdb.h"
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder#include "tools/common/sss_tools.h"
521045d36343cd17dd217a81d4b9422ad6ab6a07Christian Maeder
9a9860760c6f30558e5e60049692b6fc63904590Markus Grossstruct sss_cmdline {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer const char *exec; /* argv[0] */
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer const char *command; /* command name */
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer int argc; /* rest of arguments */
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer const char **argv;
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer};
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyerstatic void sss_tool_print_common_opts(void)
7ae38566aaf40710cd83ffa3ba25655c4ad22741Thiemo Wiedemeyer{
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyer fprintf(stderr, _("Common options:\n"));
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder fprintf(stderr, " --debug=INT %s\n",
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer _("The debug level to run with"));
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, "\n");
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer fprintf(stderr, _("Help options:\n"));
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer fprintf(stderr, " -?, --help %s\n",
3a6decfd748f532d5cb03fbcb7a42fa37b0faab3Christian Maeder _("Show this for a command"));
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, " --usage %s\n",
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer _("Show brief usage message for a command"));
1842453990fed8a1bd7a5ac792d7982c1d2bfcd5Christian Maeder}
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
b6a59f004903ac7bc96323ee3ef09c01fd221157Christian Maederstatic struct poptOption *sss_tool_common_opts_table(void)
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer{
55c5e901b5c3466300009135585bc70bd576dcb6Christian Maeder static struct poptOption common_opts[] = {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer {"debug", '\0', POPT_ARG_INT, NULL,
ec25781c1180ea07f66b48c34f93cf5634e9277cChristian Maeder 0, NULL, NULL },
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer POPT_TABLEEND
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer };
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer common_opts[0].descrip = _("The debug level to run with");
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer return common_opts;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer}
d1066b8fb69179973dcab47154858d77e72760a7Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyerstatic void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx,
4e9e95ba35a68f3c767bc0b23ebf9e904e442517Christian Maeder int *argc, const char **argv)
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer{
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer poptContext pc;
ba7903578ca168160ed411ef534fa95c5cddf8f0Ewaryst Schulz int debug = SSSDBG_DEFAULT;
ecd98975b8a8ab5a7bc075562bdab51cf47d2a90Christian Maeder int orig_argc = *argc;
f9e0b18852b238ddb649d341194e05d7200d1bbeChristian Maeder int opt;
697e63e30aa3c309a1ef1f9357745111f8dfc5a9Christian Maeder
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder struct poptOption options[] = {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &debug,
4b136ad539bd9f4e115dff4eee4d552a42d4437eChristian Maeder 0, _("The debug level to run with"), NULL },
f456529a89bfb620d39e5fd5b0a53b24643db96dDominik Luecke POPT_TABLEEND
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer };
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer while ((opt = poptGetNextOpt(pc)) != -1) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* do nothing */
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
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));
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer DEBUG_CLI_INIT(debug);
16e45483b5ce48f0b92d01c817242a8c9b8bae02Christian Maeder
ddc662fdf0207eae2034d7b68ae5e2225c575207Thiemo Wiedemeyer poptFreeContext(pc);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer}
28ca54b0d63d1d26a991711c8c7e85c474994715Christian Maeder
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanustatic errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu struct confdb_ctx **_confdb)
02a84229da51532505a93fc2abfca1ccf81b4446Razvan Pascanu{
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu struct confdb_ctx *confdb;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer char *path;
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer errno_t ret;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (path == NULL) {
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer return ENOMEM;
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer }
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer ret = confdb_init(mem_ctx, &confdb, path);
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer if (ret != EOK) {
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE,
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer "Could not initialize connection to the confdb\n");
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer talloc_free(path);
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer return ret;
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer }
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (_confdb != NULL) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer *_confdb = confdb;
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu }
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer return EOK;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer}
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyerstatic errno_t sss_tool_domains_init(TALLOC_CTX *mem_ctx,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer struct confdb_ctx *confdb,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer struct sss_domain_info **_domains)
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer{
e49fd57c63845c7806860a9736ad09f6d44dbaedChristian Maeder struct sss_domain_info *domains;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer struct sss_domain_info *dom;
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer errno_t ret;
0b8b26a22f136a9b2a8e99d655f6fe6b0b96008cThiemo Wiedemeyer
0b8b26a22f136a9b2a8e99d655f6fe6b0b96008cThiemo Wiedemeyer ret = confdb_get_domains(confdb, &domains);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer if (ret != EOK) {
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret, sss_strerror(ret));
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer return ret;
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer }
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer ret = sysdb_init(mem_ctx, domains, false);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer SYSDB_VERSION_ERROR(ret);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (ret != EOK) {
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer "Could not initialize connection to the sysdb\n");
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer return ret;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer }
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer for (dom = domains; dom != NULL;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer dom = get_next_domain(dom, SSS_GND_DESCEND)) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (!IS_SUBDOMAIN(dom)) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer /* Get flat name and domain ID (SID) from the cache
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer * if available */
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer ret = sysdb_master_domain_update(dom);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer if (ret != EOK) {
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer DEBUG(SSSDBG_MINOR_FAILURE, "Failed to update domain %s.\n",
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer dom->name);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer }
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder /* Update list of subdomains for this domain */
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder ret = sysdb_update_subdomains(dom);
0b8b26a22f136a9b2a8e99d655f6fe6b0b96008cThiemo Wiedemeyer if (ret != EOK) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer DEBUG(SSSDBG_MINOR_FAILURE,
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer "Failed to update subdomains for domain %s.\n",
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer dom->name);
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer }
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer }
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer }
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer for (dom = domains; dom != NULL;
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 if (ret != EOK) {
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "sss_names_init() failed\n");
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer return ret;
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer }
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer }
39a2520d13a7d43f0c0fa71b94255c3f7c500005Christian Maeder
39a2520d13a7d43f0c0fa71b94255c3f7c500005Christian Maeder *_domains = domains;
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer return ret;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer}
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyerstruct sss_tool_ctx *sss_tool_init(TALLOC_CTX *mem_ctx,
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer int *argc, const char **argv)
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer{
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer struct sss_tool_ctx *tool_ctx;
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer errno_t ret;
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer tool_ctx = talloc_zero(mem_ctx, struct sss_tool_ctx);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (tool_ctx == NULL) {
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer return NULL;
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer }
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer sss_tool_common_opts(tool_ctx, argc, argv);
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer /* Connect to confdb. */
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
fa1bf658051ac503f27ff1b59edb093398eed6edThiemo Wiedemeyer if (ret != EOK) {
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer ret, sss_strerror(ret));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder goto done;
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer }
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer
92e96be605537638d75e9d3023ab698bd89cf889Thiemo Wiedemeyer /* Setup domains. */
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer if (ret != EOK) {
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret, sss_strerror(ret));
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer goto done;
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer }
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer CONFDB_MONITOR_CONF_ENTRY,
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer CONFDB_MONITOR_DEFAULT_DOMAIN,
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer NULL, &tool_ctx->default_domain);
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer if (ret != EOK) {
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer ret, strerror(ret));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder goto done;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder }
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer
d9f20cf968e246ec283f0c09f60af4b47b174398Thiemo Wiedemeyer ret = EOK;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
39a2520d13a7d43f0c0fa71b94255c3f7c500005Christian Maederdone:
4014fb09362f3e38a91d7bb11b1484a4790e9297Thiemo Wiedemeyer if (ret != EOK) {
da1f9fa9339a0115d0559411929835bcff74e5f5Thiemo Wiedemeyer talloc_zfree(tool_ctx);
39a2520d13a7d43f0c0fa71b94255c3f7c500005Christian Maeder }
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
da1f9fa9339a0115d0559411929835bcff74e5f5Thiemo Wiedemeyer return tool_ctx;
da1f9fa9339a0115d0559411929835bcff74e5f5Thiemo Wiedemeyer}
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
4014fb09362f3e38a91d7bb11b1484a4790e9297Thiemo Wiedemeyerint sss_tool_usage(const char *tool_name,
da1f9fa9339a0115d0559411929835bcff74e5f5Thiemo Wiedemeyer struct sss_route_cmd *commands)
4014fb09362f3e38a91d7bb11b1484a4790e9297Thiemo Wiedemeyer{
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer int i;
3e3efd4ce838940032e875e6d08712a177c9c1d0Thiemo Wiedemeyer
3e3efd4ce838940032e875e6d08712a177c9c1d0Thiemo Wiedemeyer fprintf(stderr, _("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name);
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder fprintf(stderr, _("Available commands:\n"));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder for (i = 0; commands[i].command != NULL; i++) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder if (commands[i].description == NULL) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder fprintf(stderr, "* %s\n", commands[i].command);
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer } else {
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer fprintf(stderr, "* %s\t\t %s\n",
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer commands[i].command, commands[i].description);
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder }
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer }
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer fprintf(stderr, _("\n"));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder sss_tool_print_common_opts();
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer return EXIT_FAILURE;
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer}
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyerint sss_tool_route(int argc, const char **argv,
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder struct sss_tool_ctx *tool_ctx,
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer struct sss_route_cmd *commands,
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder void *pvt)
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer{
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder struct sss_cmdline cmdline;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder const char *cmd;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder int i;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer if (commands == NULL) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: commands can't be NULL!\n");
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return EXIT_FAILURE;
84ba39232a012abf2085c8a421ebce6abc52d56eThiemo Wiedemeyer }
80df5ce65c2bad7a0643106e524fe33cdcfab5b6Thiemo Wiedemeyer
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer if (argc < 2) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return sss_tool_usage(argv[0], commands);
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder }
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder cmd = argv[1];
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder for (i = 0; commands[i].command != NULL; i++) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer if (strcmp(commands[i].command, cmd) == 0) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer cmdline.exec = argv[0];
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer cmdline.command = argv[1];
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer cmdline.argc = argc - 2;
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer cmdline.argv = argv + 2;
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return commands[i].fn(&cmdline, tool_ctx, pvt);
788bd3c33ec5aaeb90a1932c341ff837116410cfThiemo Wiedemeyer }
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer }
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return sss_tool_usage(argv[0], commands);
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder}
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyerstatic struct poptOption *nonnull_popt_table(struct poptOption *options)
84ba39232a012abf2085c8a421ebce6abc52d56eThiemo Wiedemeyer{
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder static struct poptOption empty[] = {
84ba39232a012abf2085c8a421ebce6abc52d56eThiemo Wiedemeyer POPT_TABLEEND
1ac36418f204bbe56f4cd951a979180721758999Christian Maeder };
370e81d7af7821f0ac6ee0643613e87a727841e7Thiemo Wiedemeyer
84ba39232a012abf2085c8a421ebce6abc52d56eThiemo Wiedemeyer if (options == NULL) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer return empty;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder }
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder return options;
545d0cd78159cade346b579d06052638b19b0f72Thiemo Wiedemeyer}
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyer
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyerint sss_tool_popt_ex(struct sss_cmdline *cmdline,
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer struct poptOption *options,
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer enum sss_tool_opt require_option,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu sss_popt_fn popt_fn,
a4e6fb26100f53e3b1e9f5b97c2e0a0c129294e5Christian Maeder void *popt_fn_pvt,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu const char *fopt_name,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu const char *fopt_help,
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu const char **_fopt,
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer bool *_opt_set)
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer{
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu struct poptOption opts_table[] = {
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu {NULL, '\0', POPT_ARG_INCLUDE_TABLE, nonnull_popt_table(options), \
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu 0, _("Command options:"), NULL },
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer {NULL, '\0', POPT_ARG_INCLUDE_TABLE, sss_tool_common_opts_table(), \
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyer 0, _("Common options:"), NULL },
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyer POPT_AUTOHELP
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer POPT_TABLEEND
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer };
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer const char *fopt;
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer char *help;
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer poptContext pc;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer bool opt_set;
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer int ret;
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer /* Create help option string. We always need to append command name since
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu * we use POPT_CONTEXT_KEEP_FIRST. */
a4e6fb26100f53e3b1e9f5b97c2e0a0c129294e5Christian Maeder if (fopt_name == NULL) {
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu help = talloc_asprintf(NULL, "%s %s %s", cmdline->exec,
a4e6fb26100f53e3b1e9f5b97c2e0a0c129294e5Christian Maeder cmdline->command, _("[OPTIONS...]"));
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu } else {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer help = talloc_asprintf(NULL, "%s %s %s %s", cmdline->exec,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer cmdline->command, fopt_name, _("[OPTIONS...]"));
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer if (help == NULL) {
109b67ffce2bad83667e2f4a319d2d7f380f91afThiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer return EXIT_FAILURE;
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer }
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer
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 opts_table, POPT_CONTEXT_KEEP_FIRST);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer poptSetOtherOptionHelp(pc, help);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
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) {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer if (popt_fn != NULL) {
d1066b8fb69179973dcab47154858d77e72760a7Thiemo Wiedemeyer ret = popt_fn(pc, ret, popt_fn_pvt);
d1066b8fb69179973dcab47154858d77e72760a7Thiemo Wiedemeyer if (ret != EOK) {
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder ret = EXIT_FAILURE;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer goto done;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer } else {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, _("Invalid option %s: %s\n\n"),
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer poptBadOption(pc, 0), poptStrerror(ret));
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer poptPrintHelp(pc, stderr, 0);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = EXIT_FAILURE;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer goto done;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer /* Parse free option which is always required if requested. */
8bdd8707c8e341f1fc57fcbf884520bfe06a5028Christian Maeder fopt = poptGetArg(pc);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer if (_fopt != NULL) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer if (fopt == NULL) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer fprintf(stderr, _("Missing option: %s\n\n"), fopt_help);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer poptPrintHelp(pc, stderr, 0);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ret = EXIT_FAILURE;
a4e6fb26100f53e3b1e9f5b97c2e0a0c129294e5Christian Maeder goto done;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
28ca54b0d63d1d26a991711c8c7e85c474994715Christian Maeder
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder /* No more arguments expected. If something follows it is an error. */
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer if (poptGetArg(pc)) {
370e81d7af7821f0ac6ee0643613e87a727841e7Thiemo Wiedemeyer fprintf(stderr, _("Only one free argument is expected!\n\n"));
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer poptPrintHelp(pc, stderr, 0);
28ca54b0d63d1d26a991711c8c7e85c474994715Christian Maeder ret = EXIT_FAILURE;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer goto done;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer *_fopt = fopt;
966a6c024c828387023fccb0cd0049f78687e5dcThiemo Wiedemeyer } else if (_fopt == NULL && fopt != NULL) {
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu /* Unexpected free argument. */
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder fprintf(stderr, _("Unexpected parameter: %s\n\n"), fopt);
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder poptPrintHelp(pc, stderr, 0);
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer ret = EXIT_FAILURE;
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer goto done;
966a6c024c828387023fccb0cd0049f78687e5dcThiemo Wiedemeyer }
966a6c024c828387023fccb0cd0049f78687e5dcThiemo Wiedemeyer
79d103748927615310322af6f7806c7cef11a802Christian Maeder opt_set = true;
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer if ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1) {
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer opt_set = false;
4b136ad539bd9f4e115dff4eee4d552a42d4437eChristian Maeder
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"));
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder poptPrintHelp(pc, stderr, 0);
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder ret = EXIT_FAILURE;
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder goto done;
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder }
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu }
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer
966a6c024c828387023fccb0cd0049f78687e5dcThiemo Wiedemeyer if (_opt_set != NULL) {
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer *_opt_set = opt_set;
e49fd57c63845c7806860a9736ad09f6d44dbaedChristian Maeder }
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer
1ac36418f204bbe56f4cd951a979180721758999Christian Maeder ret = EXIT_SUCCESS;
4b136ad539bd9f4e115dff4eee4d552a42d4437eChristian Maeder
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyerdone:
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer poptFreeContext(pc);
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder talloc_free(help);
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder return ret;
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder}
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maederint sss_tool_popt(struct sss_cmdline *cmdline,
521045d36343cd17dd217a81d4b9422ad6ab6a07Christian Maeder struct poptOption *options,
ba7903578ca168160ed411ef534fa95c5cddf8f0Ewaryst Schulz enum sss_tool_opt require_option,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer sss_popt_fn popt_fn,
1ac36418f204bbe56f4cd951a979180721758999Christian Maeder void *popt_fn_pvt)
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder{
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer return sss_tool_popt_ex(cmdline, options, require_option,
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer popt_fn, popt_fn_pvt, NULL, NULL, NULL, NULL);
66b035879accdc5f8337726173f800286a87fd78Christian Maeder}
1ac36418f204bbe56f4cd951a979180721758999Christian Maeder
1ac36418f204bbe56f4cd951a979180721758999Christian Maederint sss_tool_main(int argc, const char **argv,
1ac36418f204bbe56f4cd951a979180721758999Christian Maeder struct sss_route_cmd *commands,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer void *pvt)
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder{
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer struct sss_tool_ctx *tool_ctx;
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder uid_t uid;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer int ret;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer uid = getuid();
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder if (uid != 0) {
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]);
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder return EXIT_FAILURE;
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder }
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer tool_ctx = sss_tool_init(NULL, &argc, argv);
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maeder if (tool_ctx == NULL) {
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tool context\n");
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer return EXIT_FAILURE;
44c1fff98bd6c54db237bef5030657d3f47058a5Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = sss_tool_route(argc, argv, tool_ctx, commands, pvt);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer talloc_free(tool_ctx);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer return ret;
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder}
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyerint sss_tool_parse_name(TALLOC_CTX *mem_ctx,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer struct sss_tool_ctx *tool_ctx,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer const char *input,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer const char **_username,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer struct sss_domain_info **_domain)
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer{
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer char *username = NULL;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder char *domname = NULL;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer struct sss_domain_info *domain;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer int ret;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = sss_parse_name_for_domains(mem_ctx, tool_ctx->domains,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer tool_ctx->default_domain, input,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer &domname, &username);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer if (ret == EAGAIN) {
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 goto done;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer } else if (ret != EOK) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse name [%d]: %s\n",
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret, sss_strerror(ret));
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer goto done;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder
1937dccb04b363364f7a7de17fdaae1d70583af9Christian Maeder domain = find_domain_by_name(tool_ctx->domains, domname, true);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer *_username = username;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer *_domain = domain;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer ret = EOK;
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
239991d3955da0cfb760af4d506069446e1676b7Christian Maederdone:
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer if (ret != EOK) {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer talloc_zfree(username);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer talloc_zfree(domname);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer return ret;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder}
c41a1c38edbd787d8fd12b9b5f11b73a37dafe0fChristian Maeder