284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina/*
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina Authors:
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina Pavel Březina <pbrezina@redhat.com>
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina Copyright (C) 2015 Red Hat
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina This program is free software; you can redistribute it and/or modify
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina it under the terms of the GNU General Public License as published by
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina the Free Software Foundation; either version 3 of the License, or
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina (at your option) any later version.
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina This program is distributed in the hope that it will be useful,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina GNU General Public License for more details.
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina You should have received a copy of the GNU General Public License
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina*/
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include <talloc.h>
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include <stdlib.h>
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include <string.h>
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include <popt.h>
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include "config.h"
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include "util/util.h"
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include "confdb/confdb.h"
d2d8f342cd5e90bb9fd947c448492225f959aa86Pavel Březina#include "confdb/confdb_setup.h"
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include "db/sysdb.h"
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina#include "tools/common/sss_tools.h"
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinastruct sss_cmdline {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina const char *exec; /* argv[0] */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina const char *command; /* command name */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int argc; /* rest of arguments */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina const char **argv;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina};
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
81cde110402e088508053aea79670b38d450cb83Pavel Březinastatic void sss_tool_print_common_opts(int min_len)
5e2ffb69dcdd157ea422c6aec256111653e4206bPavel Březina{
5e2ffb69dcdd157ea422c6aec256111653e4206bPavel Březina fprintf(stderr, _("Common options:\n"));
81cde110402e088508053aea79670b38d450cb83Pavel Březina fprintf(stderr, " %-*s\t %s\n", min_len, "--debug=INT",
a6cd927f298ff5c9a603db5acb6c1b0ebea178c0Pavel Březina _("The debug level to run with"));
b963ed8079a4a284611d50d1b79695116c40295dPavel Březina fprintf(stderr, "\n");
b963ed8079a4a284611d50d1b79695116c40295dPavel Březina fprintf(stderr, _("Help options:\n"));
81cde110402e088508053aea79670b38d450cb83Pavel Březina fprintf(stderr, " %-*s\t %s\n", min_len, "-?, --help",
b963ed8079a4a284611d50d1b79695116c40295dPavel Březina _("Show this for a command"));
81cde110402e088508053aea79670b38d450cb83Pavel Březina fprintf(stderr, " %-*s\t %s\n", min_len, "--usage",
b963ed8079a4a284611d50d1b79695116c40295dPavel Březina _("Show brief usage message for a command"));
5e2ffb69dcdd157ea422c6aec256111653e4206bPavel Březina}
5e2ffb69dcdd157ea422c6aec256111653e4206bPavel Březina
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březinastatic struct poptOption *sss_tool_common_opts_table(void)
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina{
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina static struct poptOption common_opts[] = {
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina {"debug", '\0', POPT_ARG_INT, NULL,
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina 0, NULL, NULL },
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina POPT_TABLEEND
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina };
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina common_opts[0].descrip = _("The debug level to run with");
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina return common_opts;
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina}
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinastatic void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int *argc, const char **argv)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptContext pc;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int debug = SSSDBG_DEFAULT;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int orig_argc = *argc;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int opt;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct poptOption options[] = {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &debug,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina 0, _("The debug level to run with"), NULL },
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina POPT_TABLEEND
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina };
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina while ((opt = poptGetNextOpt(pc)) != -1) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* do nothing */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Strip common options from arguments. We will discard_const here,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina * since it is not worth the trouble to convert it back and forth. */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina *argc = poptStrippedArgv(pc, orig_argc, discard_const_p(char *, argv));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG_CLI_INIT(debug);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptFreeContext(pc);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinastatic errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct confdb_ctx **_confdb)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct confdb_ctx *confdb;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina char *path;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina errno_t ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (path == NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ENOMEM;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
cca497b4cbbbf05c4f9181b7d8113cde81754831Michal Židek ret = confdb_setup(mem_ctx, path,
cca497b4cbbbf05c4f9181b7d8113cde81754831Michal Židek SSSD_CONFIG_FILE, CONFDB_DEFAULT_CONFIG_DIR,
cca497b4cbbbf05c4f9181b7d8113cde81754831Michal Židek &confdb);
d2d8f342cd5e90bb9fd947c448492225f959aa86Pavel Březina talloc_zfree(path);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
d2d8f342cd5e90bb9fd947c448492225f959aa86Pavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to setup ConfDB [%d]: %s\n",
d2d8f342cd5e90bb9fd947c448492225f959aa86Pavel Březina ret, sss_strerror(ret));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (_confdb != NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina *_confdb = confdb;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return EOK;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinastatic errno_t sss_tool_domains_init(TALLOC_CTX *mem_ctx,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct confdb_ctx *confdb,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_domain_info **_domains)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_domain_info *domains;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_domain_info *dom;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina errno_t ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = confdb_get_domains(confdb, &domains);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret, sss_strerror(ret));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
ebbeac5c6b8b87ab478ee5a04ec48fbbba0c9efcJakub Hrozek ret = sysdb_init(mem_ctx, domains);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina "Could not initialize connection to the sysdb\n");
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek for (dom = domains; dom != NULL;
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek dom = get_next_domain(dom, SSS_GND_DESCEND)) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (!IS_SUBDOMAIN(dom)) {
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose /* Get flat name and domain ID (SID) from the cache
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose * if available */
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose ret = sysdb_master_domain_update(dom);
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose if (ret != EOK) {
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose DEBUG(SSSDBG_MINOR_FAILURE, "Failed to update domain %s.\n",
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose dom->name);
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose }
3a8b5ccf7c27b72054e1d8b3ab355cb1e28efda9Sumit Bose
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Update list of subdomains for this domain */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = sysdb_update_subdomains(dom);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_MINOR_FAILURE,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina "Failed to update subdomains for domain %s.\n",
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina dom->name);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek for (dom = domains; dom != NULL;
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek dom = get_next_domain(dom, SSS_GND_DESCEND)) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = sss_names_init(mem_ctx, confdb, dom->name, &dom->names);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "sss_names_init() failed\n");
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina *_domains = domains;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozekerrno_t sss_tool_init(TALLOC_CTX *mem_ctx,
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek int *argc, const char **argv,
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek struct sss_tool_ctx **_tool_ctx)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_tool_ctx *tool_ctx;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina tool_ctx = talloc_zero(mem_ctx, struct sss_tool_ctx);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (tool_ctx == NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek return ENOMEM;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina sss_tool_common_opts(tool_ctx, argc, argv);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek *_tool_ctx = tool_ctx;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek return EOK;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březinastatic bool sss_tool_is_delimiter(struct sss_route_cmd *command)
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina{
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina if (command->command != NULL && command->command[0] == '\0') {
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina return true;
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina }
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina return false;
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina}
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozekstatic bool sss_tools_handles_init_error(struct sss_route_cmd *command,
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek errno_t init_err)
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek{
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek if (init_err == EOK) {
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek return true;
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek }
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek return command->handles_init_err == init_err;
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek}
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek
81cde110402e088508053aea79670b38d450cb83Pavel Březinastatic size_t sss_tool_max_length(struct sss_route_cmd *commands)
81cde110402e088508053aea79670b38d450cb83Pavel Březina{
81cde110402e088508053aea79670b38d450cb83Pavel Březina size_t max = 0;
81cde110402e088508053aea79670b38d450cb83Pavel Březina size_t len;
81cde110402e088508053aea79670b38d450cb83Pavel Březina int i;
81cde110402e088508053aea79670b38d450cb83Pavel Březina
81cde110402e088508053aea79670b38d450cb83Pavel Březina for (i = 0; commands[i].command != NULL; i++) {
81cde110402e088508053aea79670b38d450cb83Pavel Březina if (sss_tool_is_delimiter(&commands[i])) {
81cde110402e088508053aea79670b38d450cb83Pavel Březina continue;
81cde110402e088508053aea79670b38d450cb83Pavel Březina }
81cde110402e088508053aea79670b38d450cb83Pavel Březina
81cde110402e088508053aea79670b38d450cb83Pavel Březina len = strlen(commands[i].command);
81cde110402e088508053aea79670b38d450cb83Pavel Březina if (max < len) {
81cde110402e088508053aea79670b38d450cb83Pavel Březina max = len;
81cde110402e088508053aea79670b38d450cb83Pavel Březina }
81cde110402e088508053aea79670b38d450cb83Pavel Březina }
81cde110402e088508053aea79670b38d450cb83Pavel Březina
81cde110402e088508053aea79670b38d450cb83Pavel Březina return max;
81cde110402e088508053aea79670b38d450cb83Pavel Březina}
81cde110402e088508053aea79670b38d450cb83Pavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březinavoid sss_tool_usage(const char *tool_name, struct sss_route_cmd *commands)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
81cde110402e088508053aea79670b38d450cb83Pavel Březina int min_len;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int i;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina fprintf(stderr, _("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina fprintf(stderr, _("Available commands:\n"));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
81cde110402e088508053aea79670b38d450cb83Pavel Březina min_len = sss_tool_max_length(commands);
81cde110402e088508053aea79670b38d450cb83Pavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina for (i = 0; commands[i].command != NULL; i++) {
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina if (sss_tool_is_delimiter(&commands[i])) {
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina fprintf(stderr, "\n%s\n", commands[i].description);
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina continue;
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina }
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina
cf3ba77997dfbd076a1f30fdbb33c7973766ac03Pavel Březina if (commands[i].description == NULL) {
81cde110402e088508053aea79670b38d450cb83Pavel Březina fprintf(stderr, "* %40s\n", commands[i].command);
cf3ba77997dfbd076a1f30fdbb33c7973766ac03Pavel Březina } else {
81cde110402e088508053aea79670b38d450cb83Pavel Březina fprintf(stderr, "* %-*s\t %s\n",
81cde110402e088508053aea79670b38d450cb83Pavel Březina min_len, commands[i].command, commands[i].description);
cf3ba77997dfbd076a1f30fdbb33c7973766ac03Pavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
5e2ffb69dcdd157ea422c6aec256111653e4206bPavel Březina fprintf(stderr, _("\n"));
81cde110402e088508053aea79670b38d450cb83Pavel Březina sss_tool_print_common_opts(min_len);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židekstatic int tool_cmd_init(struct sss_tool_ctx *tool_ctx,
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek struct sss_route_cmd *command)
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek{
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek int ret;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek if (command->flags & SSS_TOOL_FLAG_SKIP_CMD_INIT) {
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek return EOK;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek }
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek /* Connect to confdb. */
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek if (ret != EOK) {
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret, sss_strerror(ret));
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek goto done;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek }
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek /* Setup domains. */
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek if (ret != EOK) {
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret, sss_strerror(ret));
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek goto done;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek }
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek CONFDB_MONITOR_CONF_ENTRY,
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek CONFDB_MONITOR_DEFAULT_DOMAIN,
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek NULL, &tool_ctx->default_domain);
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek if (ret != EOK) {
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret, strerror(ret));
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek goto done;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek }
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret = EOK;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židekdone:
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek return ret;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek}
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březinaerrno_t sss_tool_route(int argc, const char **argv,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct sss_tool_ctx *tool_ctx,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct sss_route_cmd *commands,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina void *pvt)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_cmdline cmdline;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina const char *cmd;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int i;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek int ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (commands == NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Bug: commands can't be NULL!\n");
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (argc < 2) {
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina sss_tool_usage(argv[0], commands);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina cmd = argv[1];
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina for (i = 0; commands[i].command != NULL; i++) {
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina if (sss_tool_is_delimiter(&commands[i])) {
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina continue;
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina }
b03ccb2764a4ccdadb77599cb624b6a17b633438Pavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (strcmp(commands[i].command, cmd) == 0) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina cmdline.exec = argv[0];
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina cmdline.command = argv[1];
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina cmdline.argc = argc - 2;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina cmdline.argv = argv + 2;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek if (!sss_tools_handles_init_error(&commands[i], tool_ctx->init_err)) {
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek "Command %s does not handle initialization error [%d] %s\n",
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek cmdline.command, tool_ctx->init_err,
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek sss_strerror(tool_ctx->init_err));
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek return tool_ctx->init_err;
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek }
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret = tool_cmd_init(tool_ctx, &commands[i]);
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek if (ret != EOK) {
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek DEBUG(SSSDBG_FATAL_FAILURE,
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek "Command initialization failed [%d] %s\n",
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek ret, sss_strerror(ret));
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek return ret;
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek }
cbee11e912bb391ba254b0bac8c1159c1f634533Michal Židek
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return commands[i].fn(&cmdline, tool_ctx, pvt);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina sss_tool_usage(argv[0], commands);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březinastatic struct poptOption *nonnull_popt_table(struct poptOption *options)
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina{
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina static struct poptOption empty[] = {
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina POPT_TABLEEND
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina };
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina if (options == NULL) {
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina return empty;
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina }
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina return options;
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina}
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březinaerrno_t sss_tool_popt_ex(struct sss_cmdline *cmdline,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct poptOption *options,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina enum sss_tool_opt require_option,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina sss_popt_fn popt_fn,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina void *popt_fn_pvt,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina const char *fopt_name,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina const char *fopt_help,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina const char **_fopt,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina bool *_opt_set)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina struct poptOption opts_table[] = {
16065cc731687eb8779d31b79436bbf79c5e3ed3Pavel Březina {NULL, '\0', POPT_ARG_INCLUDE_TABLE, nonnull_popt_table(options), \
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina 0, _("Command options:"), NULL },
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina {NULL, '\0', POPT_ARG_INCLUDE_TABLE, sss_tool_common_opts_table(), \
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina 0, _("Common options:"), NULL },
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina POPT_AUTOHELP
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina POPT_TABLEEND
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina };
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina const char *fopt;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina char *help;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptContext pc;
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina bool opt_set;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Create help option string. We always need to append command name since
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina * we use POPT_CONTEXT_KEEP_FIRST. */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (fopt_name == NULL) {
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina help = talloc_asprintf(NULL, "%s %s %s", cmdline->exec,
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina cmdline->command, _("[OPTIONS...]"));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina } else {
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina help = talloc_asprintf(NULL, "%s %s %s %s", cmdline->exec,
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina cmdline->command, fopt_name, _("[OPTIONS...]"));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (help == NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return ENOMEM;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Create popt context. This function is supposed to be called on
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina * command argv which does not contain executable (argv[0]), therefore
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina * we need to use KEEP_FIRST that ensures argv[0] is also processed. */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina pc = poptGetContext(cmdline->exec, cmdline->argc, cmdline->argv,
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina opts_table, POPT_CONTEXT_KEEP_FIRST);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptSetOtherOptionHelp(pc, help);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Parse options. Invoke custom function if provided. If no parsing
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina * function is provided, print error on unknown option. */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina while ((ret = poptGetNextOpt(pc)) != -1) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (popt_fn != NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = popt_fn(pc, ret, popt_fn_pvt);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina } else {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina fprintf(stderr, _("Invalid option %s: %s\n\n"),
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptBadOption(pc, 0), poptStrerror(ret));
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina poptPrintHelp(pc, stderr, 0);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* Parse free option which is always required if requested. */
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina fopt = poptGetArg(pc);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (_fopt != NULL) {
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina if (fopt == NULL) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina fprintf(stderr, _("Missing option: %s\n\n"), fopt_help);
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina poptPrintHelp(pc, stderr, 0);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina /* No more arguments expected. If something follows it is an error. */
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (poptGetArg(pc)) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina fprintf(stderr, _("Only one free argument is expected!\n\n"));
bda8039465a0084fb380e878c8f9ea3e900505eaPavel Březina poptPrintHelp(pc, stderr, 0);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EINVAL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina *_fopt = fopt;
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina } else if (_fopt == NULL && fopt != NULL) {
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina /* Unexpected free argument. */
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina fprintf(stderr, _("Unexpected parameter: %s\n\n"), fopt);
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina poptPrintHelp(pc, stderr, 0);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EINVAL;
4940ba14100ad11b0ed1f2a8a4fea5daa34d56eePavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina opt_set = true;
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina if ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1) {
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina opt_set = false;
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina /* If at least one option is required and not provided, print error. */
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina if (require_option == SSS_TOOL_OPT_REQUIRED) {
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina fprintf(stderr, _("At least one option is required!\n\n"));
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina poptPrintHelp(pc, stderr, 0);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EINVAL;
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina goto done;
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina }
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina }
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina if (_opt_set != NULL) {
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina *_opt_set = opt_set;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina ret = EOK;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinadone:
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina poptFreeContext(pc);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina talloc_free(help);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březinaerrno_t sss_tool_popt(struct sss_cmdline *cmdline,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct poptOption *options,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina enum sss_tool_opt require_option,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina sss_popt_fn popt_fn,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina void *popt_fn_pvt)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return sss_tool_popt_ex(cmdline, options, require_option,
3bc651a611a3e5be508875f3ae58bfb5ece2525cPavel Březina popt_fn, popt_fn_pvt, NULL, NULL, NULL, NULL);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinaint sss_tool_main(int argc, const char **argv,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_route_cmd *commands,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina void *pvt)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_tool_ctx *tool_ctx;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina uid_t uid;
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina errno_t ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina uid = getuid();
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (uid != 0) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Running under %d, must be root\n", uid);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ERROR("%1$s must be run as root\n", argv[0]);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return EXIT_FAILURE;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek ret = sss_tool_init(NULL, &argc, argv, &tool_ctx);
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek if (ret == ERR_SYSDB_VERSION_TOO_OLD) {
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek tool_ctx->init_err = ret;
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek } else if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tool context\n");
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return EXIT_FAILURE;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = sss_tool_route(argc, argv, tool_ctx, commands, pvt);
a0b824ac01c6b58fe6055d48aa6e29e94219646dJakub Hrozek SYSDB_VERSION_ERROR(ret);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina talloc_free(tool_ctx);
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina if (ret != EOK) {
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return EXIT_FAILURE;
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina }
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina return EXIT_SUCCESS;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březinaerrno_t sss_tool_parse_name(TALLOC_CTX *mem_ctx,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct sss_tool_ctx *tool_ctx,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina const char *input,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina const char **_username,
e98ccef2609811186711b79d8ef5d0a4450ab6e0Pavel Březina struct sss_domain_info **_domain)
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina{
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina char *username = NULL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina char *domname = NULL;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina struct sss_domain_info *domain;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina int ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = sss_parse_name_for_domains(mem_ctx, tool_ctx->domains,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina tool_ctx->default_domain, input,
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina &domname, &username);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret == EAGAIN) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to find domain. The domain name may "
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina "be a subdomain that was not yet found.\n");
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina } else if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse name [%d]: %s\n",
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret, sss_strerror(ret));
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina goto done;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina domain = find_domain_by_name(tool_ctx->domains, domname, true);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina *_username = username;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina *_domain = domain;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina ret = EOK;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březinadone:
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina if (ret != EOK) {
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina talloc_zfree(username);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina talloc_zfree(domname);
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina }
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina return ret;
284937e6b5b0c9d7a1d3382d0d2820d1168842fbPavel Březina}