On Fri, Jul 24, 2015 at 01:08:17PM +0200, Pavel Březina wrote:
https://fedorahosted.org/sssd/ticket/2584
If you have any idea how to improve manual page, please, share it.
Hi,
please see my comments inline.
From 58e7ac9f61c9dcc33e14a255daf026b563f06a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= pbrezina@redhat.com Date: Fri, 24 Jul 2015 09:55:28 +0200 Subject: [PATCH 1/3] SYSDB: prepare for LOCAL view
Objects doesn't have to have overrideDN specified when using LOCAL view. Since the view is not stored on the server we do not want to contact LDAP therefore we special case LOCAL view saying that it is OK that this attribute is missing.
Preparation for: https://fedorahosted.org/sssd/ticket/2584
src/db/sysdb.h | 1 + src/db/sysdb_views.c | 7 +++++++ src/tests/cmocka/test_sysdb_views.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+)
diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 0f745ccb1a646d77ba4ad3d714d5f4dce0a51211..7b18bbbe3f9249e59c4ffc6ee6ddcacebddf7e9f 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -160,6 +160,7 @@ #define SYSDB_VIEW_CLASS "view" #define SYSDB_VIEW_NAME "viewName" #define SYSDB_DEFAULT_VIEW_NAME "default" +#define SYSDB_LOCAL_VIEW_NAME "LOCAL"
Please put a comment here that this name is reserved for client-side overrides. You can also move the definition to the top or bottom of the view-related options, perhaps also with DEFAULT_VIEW_NAME, but that's less important to me.
#define SYSDB_OVERRIDE_CLASS "overrride" #define SYSDB_OVERRIDE_ANCHOR_UUID "overrideAnchorUUID" #define SYSDB_OVERRIDE_USER_CLASS "userOverride"
[...]
diff --git a/src/tests/cmocka/test_sysdb_views.c b/src/tests/cmocka/test_sysdb_views.c index 1fb598219e9ee581e465ddbb32ba9f2544600c26..19c04d58e47aa400aee4745dc40f937497081d10 100644 --- a/src/tests/cmocka/test_sysdb_views.c +++ b/src/tests/cmocka/test_sysdb_views.c @@ -275,6 +275,33 @@ void test_sysdb_add_overrides_to_object(void **state) assert_int_equal(ldb_val_string_cmp(&el->values[1], "OVERRIDEKEY2"), 0); }
+void test_sysdb_add_overrides_to_object_local(void **state) +{
- int ret;
- struct ldb_message *orig;
- struct ldb_message_element *el;
- char *tmp_str;
- struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
struct sysdb_test_ctx);
- orig = ldb_msg_new(test_ctx);
- assert_non_null(orig);
- tmp_str = talloc_strdup(orig, "ORIGNAME");
- ret = ldb_msg_add_string(orig, SYSDB_NAME, tmp_str);
- assert_int_equal(ret, EOK);
- tmp_str = talloc_strdup(orig, "ORIGGECOS");
- ret = ldb_msg_add_string(orig, SYSDB_GECOS, tmp_str);
- assert_int_equal(ret, EOK);
- test_ctx->domain->has_views = true;
- test_ctx->domain->view_name = "LOCAL";
- ret = sysdb_add_overrides_to_object(test_ctx->domain, orig, NULL, NULL);
- assert_int_equal(ret, EOK);
+}
It would also be nice to add a negative test to check that a non-local override requires overrideDN ?
From 5f18058a302c5ac3901dd5694724cc9a38adef36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= pbrezina@redhat.com Date: Wed, 22 Jul 2015 10:02:02 +0200 Subject: [PATCH 2/3] TOOLS: add common command framework
Add general framework to simplify creating "cmd COMMAND [OPTIONS...]" style tools.
Preparation for: https://fedorahosted.org/sssd/ticket/2584
Makefile.am | 2 + src/tools/common/sss_tools.c | 407 +++++++++++++++++++++++++++++++++++++++++++ src/tools/common/sss_tools.h | 103 +++++++++++ 3 files changed, 512 insertions(+) create mode 100644 src/tools/common/sss_tools.c create mode 100644 src/tools/common/sss_tools.h
diff --git a/Makefile.am b/Makefile.am index b8cbc6df23ded1edb945a709b6dbe1c44eb54017..e2c919b9adaf69f1ea6fde7e4a3e1746ccdcbedf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -445,6 +445,7 @@ SSSD_TOOLS_OBJ = \ src/tools/tools_util.c \ src/tools/files.c \ src/tools/selinux.c \
- src/tools/common/sss_tools.c \ src/util/nscd.c
Please add $(NULL) to the end of the list.
SSSD_LCL_TOOLS_OBJ = \ @@ -641,6 +642,7 @@ dist_noinst_HEADERS = \ src/lib/idmap/sss_idmap_private.h \ src/lib/sifp/sss_sifp_private.h \ src/tests/cmocka/test_utils.h \
- src/tools/common/sss_tools.h \ $(NULL)
diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c new file mode 100644 index 0000000000000000000000000000000000000000..1c26e3b520743f41ed9376c52e8ac18736d73daa --- /dev/null +++ b/src/tools/common/sss_tools.c @@ -0,0 +1,407 @@ +/*
- Authors:
Pavel Březina <pbrezina@redhat.com>
- Copyright (C) 2015 Red Hat
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see http://www.gnu.org/licenses/.
+*/
+#include <talloc.h> +#include <stdlib.h> +#include <string.h> +#include <popt.h>
+#include "config.h" +#include "util/util.h" +#include "confdb/confdb.h" +#include "db/sysdb.h" +#include "tools/common/sss_tools.h"
+static void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx,
int *argc, const char **argv)
+{
- poptContext pc;
- int debug = SSSDBG_DEFAULT;
- int orig_argc = *argc;
- int opt;
- struct poptOption options[] = {
{"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &debug,
0, _("The debug level to run with"), NULL },
POPT_TABLEEND
- };
- pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
- while((opt = poptGetNextOpt(pc)) != -1) {
Put a space between while and "("
/* do nothing */
- }
- /* Strip --debug and --config from arguments. We will discard_const here,
~~~~~~~~ There is no --config anymore
* since it is not worth the trouble to convert it back and forth. */
- *argc = poptStrippedArgv(pc, orig_argc, discard_const_p(char *, argv));
- DEBUG_CLI_INIT(debug);
- poptFreeContext(pc);
+}
+static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx **_confdb,
char **_path)
~~~~~~~~~~~~~ I don't think returning path should be part of the API. It's really an internal detail of the confdb.
+{
- struct confdb_ctx *confdb;
- char *path;
- errno_t ret;
- path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
- if (path == NULL) {
return ENOMEM;
- }
- ret = confdb_init(mem_ctx, &confdb, path);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not initialize connection to the confdb\n");
talloc_free(path);
return ret;
- }
- if (_confdb != NULL) {
*_confdb = confdb;
- }
- if (_path != NULL) {
*_path = path;
- }
- return EOK;
+}
[...]
+static errno_t sss_tool_domains_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *confdb,
struct sss_domain_info **_domains)
+{
- struct sss_domain_info *domains;
- struct sss_domain_info *dom;
- errno_t ret;
- ret = confdb_get_domains(confdb, &domains);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
ret, sss_strerror(ret));
return ret;
- }
- ret = sysdb_init(mem_ctx, domains, false);
- SYSDB_VERSION_ERROR(ret);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not initialize connection to the sysdb\n");
return ret;
- }
- for (dom = domains; dom != NULL; dom = get_next_domain(dom, false)) {
~~~~~~~ This looks like a bug, you can never descend into a subdomain with descend=false
btw (unrelated to these patches) it seems strange to require the user of the sysdb database to connect to the db and then separately initialize subdomains...I would vote for merging that code..
if (!IS_SUBDOMAIN(dom)) {
/* Update list of subdomains for this domain */
ret = sysdb_update_subdomains(dom);
if (ret != EOK) {
DEBUG(SSSDBG_MINOR_FAILURE,
"Failed to update subdomains for domain %s.\n",
dom->name);
}
}
- }
- for (dom = domains; dom != NULL; dom = get_next_domain(dom, false)) {
~~~~~ I think you should use descend="true" here as well.
ret = sss_names_init(mem_ctx, confdb, dom->name, &dom->names);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "sss_names_init() failed\n");
return ret;
}
- }
- *_domains = domains;
- return ret;
+}
+struct sss_tool_ctx * sss_tool_init(TALLOC_CTX *mem_ctx,
~~ We don't normally put a space between asterisk and identifier.
int *argc, const char **argv)
+{
- struct sss_tool_ctx *tool_ctx;
- errno_t ret;
[...]
+int sss_tool_usage(const char *tool_name,
struct sss_route_cmd *commands)
+{
- int i;
- fprintf(stderr, "Usage:\n%s COMMAND COMMAND-ARGS\n\n", tool_name);
- fprintf(stderr, "Available commands:\n");
Can you make the strings translatable with _(foo) ?
- for (i = 0; commands[i].command != NULL; i++) {
fprintf(stderr, "* %s\n", commands[i].command);
- }
- return EXIT_FAILURE;
+}
[...]
+int sss_tool_popt_ex(struct sss_cmdline *cmdline,
struct poptOption *options,
enum sss_tool_opt require_option,
sss_popt_fn popt_fn,
void *popt_fn_pvt,
const char *fopt_name,
const char *fopt_help,
const char **_fopt)
+{
- char *help;
- poptContext pc;
- int ret;
- /* Create help option string. We always need to append command name since
* we use POPT_CONTEXT_KEEP_FIRST. */
- if (fopt_name == NULL) {
help = talloc_asprintf(NULL, "%s %s [OPTIONS...]",
cmdline->exec, cmdline->command);
I think the OPTIONS string should also be made translatable...I guess you need to add another format expanstion instead: help = talloc_asprintf(NULL, "%s %s %s", cmdline->exec, cmdline->command, _("[OPTIONS...]"));
- } else {
help = talloc_asprintf(NULL, "%s %s %s [OPTIONS...]",
cmdline->exec, cmdline->command, fopt_name);
Same here.
- }
- if (help == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "tallos_asprintf() failed\n");
typo: tallos.
Also please set ret here or maybe even better, just return, because "pc" is not defined at this point in the done handler yet.
goto done;
- }
- /* Create popt context. */
- pc = poptGetContext(cmdline->exec, cmdline->argc, cmdline->argv,
options, POPT_CONTEXT_KEEP_FIRST);
~~~~~~~~~~~~~~~~~~~~~~~
What does this flag do? popt.h is not exactly explanatory..
- poptSetOtherOptionHelp(pc, help);
- /* Parse options. Invoke custom function if provided. If no parsing
* function is provided, print error on unknown option. */
- while((ret = poptGetNextOpt(pc)) != -1) {
~~~~ Missing space.
if (popt_fn != NULL) {
ret = popt_fn(pc, ret, popt_fn_pvt);
if (ret != EOK) {
ret = EXIT_FAILURE;
goto done;
}
} else {
fprintf(stderr, "Invalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(ret));
Translation again..
poptPrintHelp(pc, stderr, 0);
ret = EXIT_FAILURE;
goto done;
}
- }
- /* Parse free option which is always required if requested. */
- if (_fopt != NULL) {
*_fopt = poptGetArg(pc);
if (*_fopt == NULL) {
fprintf(stderr, "Missing option: %s\n\n", fopt_help);
Missing translation.
poptPrintHelp(pc, stderr, 0);
ret = EXIT_FAILURE;
goto done;
}
/* No more arguments expected. If something follows it is an error. */
if (poptGetArg(pc)) {
fprintf(stderr, "Only one free argument is expected!\n\n");
Missing translation.
poptPrintHelp(pc, stderr, 0);
ret = EXIT_FAILURE;
goto done;
}
- }
- /* If at least one option is required and not provided, print error. */
- if (require_option == SSS_TOOL_OPT_REQUIRED
&& ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1)) {
fprintf(stderr, "At least one option is required!\n\n");
Missing translation.
poptPrintHelp(pc, stderr, 0);
ret = EXIT_FAILURE;
goto done;
- }
- ret = EXIT_SUCCESS;
+done:
- poptFreeContext(pc);
- talloc_free(help);
- return ret;
+}
+int sss_tool_popt(struct sss_cmdline *cmdline,
struct poptOption *options,
enum sss_tool_opt require_option,
sss_popt_fn popt_fn,
void *popt_fn_pvt)
+{
- return sss_tool_popt_ex(cmdline, options, require_option,
popt_fn, popt_fn_pvt, NULL, NULL, NULL);
+}
+int sss_tool_main(int argc, const char **argv,
struct sss_route_cmd *commands,
void *pvt)
+{
- struct sss_tool_ctx *tool_ctx;
- uid_t uid;
- int ret;
- uid = getuid();
- if (uid != 0) {
DEBUG(SSSDBG_CRIT_FAILURE, "Running under %d, must be root\n", uid);
I think here it should be either root or sssd user. Even better might be to try and gracefully fail if opening the databases fail -- I don't mind which solution you choose.
ERROR("%1$s must be run as root\n", argv[0]);
return EXIT_FAILURE;
- }
- tool_ctx = sss_tool_init(NULL, &argc, argv);
- if (tool_ctx == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tool context\n");
return EXIT_FAILURE;
- }
- ret = sss_tool_route(argc, argv, tool_ctx, commands, pvt);
- talloc_free(tool_ctx);
- return ret;
+}
[...]
--- /dev/null +++ b/src/tools/common/sss_tools.h @@ -0,0 +1,103 @@ +/*
- Authors:
Pavel Březina <pbrezina@redhat.com>
- Copyright (C) 2015 Red Hat
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see http://www.gnu.org/licenses/.
+*/
+#ifndef SRC_TOOLS_COMMON_SSS_TOOLS_H_ +#define SRC_TOOLS_COMMON_SSS_TOOLS_H_
I don't really like the autogenerated include guards. Elsewhere in SSSD we use just the filename -- _filename_h_
+#include <talloc.h> +#include <popt.h>
+#include "confdb/confdb.h"
+struct sss_tool_ctx {
- struct confdb_ctx *confdb;
- char *confdb_path;
- char *default_domain;
- struct sss_domain_info *domains;
I wonder if the tool_ctx must be open for consumers? Currently only domains is used, maybe we could get away with a getter and an opaque structure? We can always make the structure public in future, but if it's public from the start, then it's easier to add cruft..
+};
+struct sss_tool_ctx * sss_tool_init(TALLOC_CTX *mem_ctx,
~~ Extra space.
int *argc, const char **argv);
+struct sss_cmdline {
- const char *exec; /* argv[0] */
- const char *command; /* command name */
- int argc; /* rest of arguments */
- const char **argv;
+};
Can you define the structure in the source and make it opaque?
+typedef int +(*sss_route_fn)(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt);
+struct sss_route_cmd {
- const char *command;
- sss_route_fn fn;
+};
+int sss_tool_usage(const char *tool_name,
struct sss_route_cmd *commands);
+int sss_tool_route(int argc, const char **argv,
struct sss_tool_ctx *tool_ctx,
struct sss_route_cmd *commands,
void *pvt);
+typedef int (*sss_popt_fn)(poptContext pc, char option, void *pvt);
+enum sss_tool_opt {
- SSS_TOOL_OPT_REQUIRED,
- SSS_TOOL_OPT_OPTIONAL
+};
+int sss_tool_popt_ex(struct sss_cmdline *cmdline,
struct poptOption *options,
enum sss_tool_opt require_option,
sss_popt_fn popt_fn,
void *popt_fn_pvt,
const char *free_opt_name,
const char *free_opt_help,
const char **_free_opt);
+int sss_tool_popt(struct sss_cmdline *cmdline,
struct poptOption *options,
enum sss_tool_opt require_option,
sss_popt_fn popt_fn,
void *popt_fn_pvt);
+int sss_tool_main(int argc, const char **argv,
struct sss_route_cmd *commands,
void *pvt);
+enum sss_tool_domain {
- SSS_TOOL_DOMAIN_REQUIRED,
- SSS_TOOL_DOMAIN_OPTIONAL
+};
+int sss_tool_parse_name(TALLOC_CTX *mem_ctx,
struct sss_tool_ctx *tool_ctx,
const char *input,
enum sss_tool_domain require_domain,
Do we need require_domain now that you added the getpwnam?
const char **_username,
struct sss_domain_info **_domain);
+#endif /* SRC_TOOLS_COMMON_SSS_TOOLS_H_ */
2.1.0
From f451abca1214d16ed4b80ee7c3db80b8c9eb53b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= pbrezina@redhat.com Date: Fri, 24 Jul 2015 09:58:11 +0200 Subject: [PATCH 3/3] TOOLS: add sss_override for local overrides
Resolves: https://fedorahosted.org/sssd/ticket/2584
Makefile.am | 9 + src/man/Makefile.am | 1 + src/man/sss_override.8.xml | 106 +++++++ src/tools/sss_override.c | 698 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 814 insertions(+) create mode 100644 src/man/sss_override.8.xml create mode 100644 src/tools/sss_override.c
diff --git a/Makefile.am b/Makefile.am index e2c919b9adaf69f1ea6fde7e4a3e1746ccdcbedf..722eeb4737250e14745f346c6b809863a33d0f07 100644 --- a/Makefile.am +++ b/Makefile.am @@ -117,6 +117,7 @@ sbin_PROGRAMS = \ sss_groupshow \ sss_cache \ sss_debuglevel \
- sss_override \
Please also append NULL to the list.
sss_seed
sssdlibexec_PROGRAMS = \ @@ -1296,6 +1297,14 @@ sss_signal_LDADD = \ $(SSSD_INTERNAL_LTLIBS) \ $(NULL)
+sss_override_SOURCES = \
- src/tools/sss_override.c \
- $(SSSD_TOOLS_OBJ)
Also end this list with $(NULL)
+sss_override_LDADD = \
- $(TOOLS_LIBS) \
- $(SSSD_INTERNAL_LTLIBS)
And here
+sss_override_CFLAGS = $(AM_CFLAGS)
And here (yes I know it's not consistent with the other tools, but in new additions to Makefile.am, that's what we try to do.)
if BUILD_SUDO sss_sudo_cli_SOURCES = \ src/sss_client/common.c \ diff --git a/src/man/Makefile.am b/src/man/Makefile.am index 1ef1da48cce74f7d1ad77e3751ee6ac3450f0259..70cadf5951f56b78ff0bfbcb303e255478af5fec 100644 --- a/src/man/Makefile.am +++ b/src/man/Makefile.am @@ -51,6 +51,7 @@ man_MANS = \ sssd-krb5.5 sssd-simple.5 \ sssd_krb5_locator_plugin.8 sss_groupshow.8 \ pam_sss.8 sss_obfuscate.8 sss_cache.8 sss_debuglevel.8 sss_seed.8 \
- sss_override.8
The new man page must be added to src/man/po/po4a.cfg
$(NULL)
if BUILD_SAMBA diff --git a/src/man/sss_override.8.xml b/src/man/sss_override.8.xml new file mode 100644 index 0000000000000000000000000000000000000000..1ebd5adc18434cccf7f9bfc8e289947dde9bb8f5 --- /dev/null +++ b/src/man/sss_override.8.xml @@ -0,0 +1,106 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE reference PUBLIC "-//OASIS//DTD DocBook V4.4//EN" +"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> +<reference> +<title>SSSD Manual pages</title> +<refentry>
- <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="include/upstream.xml" />
<refmeta>
<refentrytitle>sss_override</refentrytitle>
<manvolnum>8</manvolnum>
</refmeta>
<refnamediv id='name'>
<refname>sss_override</refname>
<refpurpose>create client-side overrides of user and object attributes</refpurpose>
In general I wonder if client-side is well understood by admins (as opposed to local or just saying stored on the local machine)
</refnamediv>
<refsynopsisdiv id='synopsis'>
<cmdsynopsis>
<command>sss_debuglevel</command>
~~~~~~~~~~~~~~ Copy-n-paste bug :-)
<arg choice='plain'><replaceable>COMMAND</replaceable></arg>
<arg choice='opt'>
<replaceable>options</replaceable>
</arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1 id='description'>
<title>DESCRIPTION</title>
<para>
<command>sss_override</command> enables to create a client-side
view and allows to change selected values of specific user
and groups. This change takes effect only on client machine.
~~~~~~~~~~~~~~~ Here I think we should definitely say local.
</para>
</refsect1>
We should say that overriding attributes of users and groups only is supported (IIRC we have an RFE somewhere to override netgroups per-client...)
<refsect1 id='commands'>
<title>AVAILABLE COMMANDS</title>
We should say that in all commands, NAME refers to the name of the original object.
<variablelist remap='IP'>
<cmdsynopsis>
<command>user-add</command>
<arg choice='plain'><replaceable>NAME</replaceable></arg>
<arg choice='opt'>
<replaceable>options</replaceable>
</arg>
</cmdsynopsis>
<varlistentry>
<term>
<option>user-add</option>
<emphasis>NAME</emphasis>
<optional><option>-n,--name</option> NAME</optional>
<optional><option>-u,--uid</option> UID</optional>
<optional><option>-g,--gid</option> GID</optional>
<optional><option>-h,--home</option> HOME</optional>
<optional><option>-s,--shell</option> SHELL</optional>
<optional><option>-c,--gecos</option> GECOS</optional>
</term>
<listitem>
<para>
Override attributes of an user.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>user-del</option>
<emphasis>NAME</emphasis>
</term>
<listitem>
<para>
Remove user overrides.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>group-add</option>
<emphasis>NAME</emphasis>
<optional><option>-n,--name</option> NAME</optional>
<optional><option>-g,--gid</option> GID</optional>
</term>
<listitem>
<para>
Override attributes of a group.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>group-del</option>
<emphasis>NAME</emphasis>
</term>
<listitem>
<para>
Remove group overrides.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
- <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="include/seealso.xml" />
+</refentry> +</reference> diff --git a/src/tools/sss_override.c b/src/tools/sss_override.c new file mode 100644 index 0000000000000000000000000000000000000000..0460737d0428ecd9036f439fe6885a471a21fad0 --- /dev/null +++ b/src/tools/sss_override.c @@ -0,0 +1,698 @@ +/*
- Authors:
Pavel Březina <pbrezina@redhat.com>
- Copyright (C) 2015 Red Hat
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see http://www.gnu.org/licenses/.
+*/
+#include <stdlib.h>
+#include "util/util.h" +#include "db/sysdb.h" +#include "tools/common/sss_tools.h"
+#define LOCALVIEW SYSDB_LOCAL_VIEW_NAME
+struct override_user {
- const char *input_name;
- const char *orig_name;
- struct sss_domain_info *domain;
- const char *name;
- uid_t uid;
- gid_t gid;
- const char *home;
- const char *shell;
- const char *gecos;
+};
+struct override_group {
- const char *input_name;
- const char *orig_name;
- struct sss_domain_info *domain;
- const char *name;
- gid_t gid;
+};
+static int parse_cmdline(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
struct poptOption *options,
const char **_input_name,
const char **_orig_name,
struct sss_domain_info **_domain)
+{
- enum sss_tool_opt require;
- const char *input_name;
- const char *orig_name;
- struct sss_domain_info *domain;
- int ret;
- require = options == NULL ? SSS_TOOL_OPT_OPTIONAL : SSS_TOOL_OPT_REQUIRED;
- ret = sss_tool_popt_ex(cmdline, options, require,
NULL, NULL, "NAME", "Specify name of modified "
Missing translation.
"object.", &input_name);
- if (ret != EXIT_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
return ret;
- }
- ret = sss_tool_parse_name(tool_ctx, tool_ctx, input_name,
SSS_TOOL_DOMAIN_OPTIONAL,
&orig_name, &domain);
- if (ret != EOK) {
fprintf(stderr, "Unable to parse name.\n");
Missing translation.
return ret;
- }
- *_input_name = input_name;
- *_orig_name = orig_name;
- *_domain = domain;
- return EXIT_SUCCESS;
+}
[...]
+static errno_t prepare_view(struct sss_domain_info *domain) +{
- char *viewname = NULL;
- errno_t ret;
- ret = sysdb_get_view_name(NULL, domain->sysdb, &viewname);
- if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_get_view_name() failed.\n");
return ret;
- }
- if (ret == EOK) {
if (!is_default_view(viewname)) {
DEBUG(SSSDBG_TRACE_FUNC, "There already exists view %s. "
"Only one view is supported. Nothing to do.\n", viewname);
This debug level should be more verbose.
ret = EEXIST;
goto done;
} else if (strcmp(viewname, LOCALVIEW) == 0) {
DEBUG(SSSDBG_TRACE_FUNC, "%s view is already present.\n", viewname);
ret = EOK;
goto done;
}
- }
- DEBUG(SSSDBG_TRACE_FUNC, "Creating %s view.\n", LOCALVIEW);
- ret = sysdb_update_view_name(domain->sysdb, LOCALVIEW);
- if (ret == EOK) {
printf("SSSD needs to be restarted for the changes to take effect.\n");
Hmm, why? Can we instead send a signal to SSSD to pick up the changes? (If yes, then it can be a follow-up patch..)
- }
+done:
- talloc_free(viewname);
- return ret;
+}
+static char * build_anchor(TALLOC_CTX *mem_ctx, const char *obj_dn)
~~ Extra space
+{
- char *anchor;
- char *safe_dn;
- errno_t ret;
- ret = sysdb_dn_sanitize(mem_ctx, obj_dn, &safe_dn);
Is this sanitization required? Shouldn't a sysdb DN be already sanitized?
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "sysdb_dn_sanitize() failed\n");
return NULL;
- }
- anchor = talloc_asprintf(mem_ctx, ":%s:%s", LOCALVIEW, safe_dn);
- talloc_free(safe_dn);
- return anchor;
+}
+static struct sysdb_attrs * build_attrs(TALLOC_CTX *mem_ctx,
~~ Extra space
const char *name,
uid_t uid,
gid_t gid,
const char *home,
const char *shell,
const char *gecos)
+{
- struct sysdb_attrs *attrs;
- errno_t ret;
- attrs = sysdb_new_attrs(mem_ctx);
- if (attrs == NULL) {
return NULL;
- }
- if (name != NULL) {
ret = sysdb_attrs_add_string(attrs, SYSDB_NAME, name);
if (ret != EOK) {
goto done;
}
- }
- if (uid != 0) {
ret = sysdb_attrs_add_uint32(attrs, SYSDB_UIDNUM, uid);
if (ret != EOK) {
goto done;
}
- }
- if (gid != 0) {
ret = sysdb_attrs_add_uint32(attrs, SYSDB_GIDNUM, gid);
if (ret != EOK) {
goto done;
}
- }
- if (home != NULL) {
ret = sysdb_attrs_add_string(attrs, SYSDB_HOMEDIR, home);
if (ret != EOK) {
goto done;
}
- }
- if (shell != NULL) {
ret = sysdb_attrs_add_string(attrs, SYSDB_SHELL, shell);
if (ret != EOK) {
goto done;
}
- }
- if (gecos != NULL) {
ret = sysdb_attrs_add_string(attrs, SYSDB_GECOS, gecos);
if (ret != EOK) {
goto done;
}
- }
+done:
- if (ret != EOK) {
talloc_free(attrs);
return NULL;
- }
- return attrs;
+}
+static struct sysdb_attrs * build_user_attrs(TALLOC_CTX *mem_ctx,
~~ Extra space
struct override_user *user)
+{
- return build_attrs(mem_ctx, user->name, user->uid, user->gid, user->home,
user->shell, user->gecos);
+}
+static struct sysdb_attrs * build_group_attrs(TALLOC_CTX *mem_ctx,
~~ Extra space
struct override_group *group)
+{
- return build_attrs(mem_ctx, group->name, 0, group->gid, 0, NULL, NULL);
+}
+static const char * get_object_dn_and_domain(TALLOC_CTX *mem_ctx,
~~ Extra space
enum sysdb_member_type type,
const char *name,
struct sss_domain_info *domain,
struct sss_domain_info *domains,
struct sss_domain_info **_new_domain)
+{
- TALLOC_CTX *tmp_ctx;
- struct sss_domain_info *dom;
- struct ldb_result *res;
- const char *dn;
- const char *strtype;
- bool check_next;
- errno_t ret;
- tmp_ctx = talloc_new(NULL);
- if (tmp_ctx == NULL) {
return NULL;
- }
- /* Ensure that the object is in cache. */
- switch (type) {
- case SYSDB_MEMBER_USER:
if (getpwnam(name) == NULL) {
ret = ENOENT;
goto done;
}
break;
- case SYSDB_MEMBER_GROUP:
if (getgrnam(name) == NULL) {
ret = ENOENT;
goto done;
}
break;
- default:
DEBUG(SSSDBG_CRIT_FAILURE, "Unsupported member type %d\n", type);
ret = ERR_INTERNAL;
goto done;
- }
- /* Find domain if it is unknown. */
- if (domain == NULL) {
check_next = true;
dom = domains;
- } else {
check_next = false;
dom = domain;
- }
- do {
switch (type) {
case SYSDB_MEMBER_USER:
DEBUG(SSSDBG_TRACE_FUNC, "Trying to find user %s@%s\n",
name, dom->name);
ret = sysdb_getpwnam(tmp_ctx, dom, name, &res);
strtype = "user";
break;
case SYSDB_MEMBER_GROUP:
DEBUG(SSSDBG_TRACE_FUNC, "Trying to find group %s@%s\n",
name, dom->name);
ret = sysdb_getgrnam(tmp_ctx, dom, name, &res);
strtype = "group";
break;
default:
DEBUG(SSSDBG_CRIT_FAILURE, "Unsupported member type %d\n", type);
ret = ERR_INTERNAL;
goto done;
}
if (ret == EOK && res->count == 0) {
ret = ENOENT;
if (check_next) {
dom = dom->next;
continue;
}
}
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to find %s %s@%s [%d]: %s\n",
strtype, name, dom->name, ret, sss_strerror(ret));
goto done;
} else if (res->count != 1) {
DEBUG(SSSDBG_CRIT_FAILURE, "More than one %s found?\n", strtype);
ret = ERR_INTERNAL;
goto done;
}
check_next = false;
- } while (check_next && dom != NULL);
- DEBUG(SSSDBG_TRACE_FUNC, "Domain of %s %s is %s\n",
strtype, name, dom->name);
Hmm looks like you should have a check for res validity here, for cases no domain match..
btw it might be nice to split this large function into smaller ones. Maybe the do-while check could be a separate function returning res.
- dn = ldb_dn_get_linearized(res->msgs[0]->dn);
- if (dn == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "ldb_dn_get_linearized() failed.\n");
ret = ENOMEM;
goto done;
- }
- talloc_steal(mem_ctx, dn);
- *_new_domain = dom;
- ret = EOK;
+done:
- talloc_free(tmp_ctx);
- if (ret != EOK) {
return NULL;
- }
- return dn;
+}
[...]
+static errno_t override_object_del(struct sss_domain_info *domain,
const char *obj_dn)
+{
- TALLOC_CTX *tmp_ctx;
- const char *anchor;
- struct ldb_dn *override_dn;
- struct ldb_message *msg;
- errno_t ret;
- int sret;
- bool in_transaction;
- struct ldb_context *ldb = sysdb_ctx_get_ldb(domain->sysdb);
- tmp_ctx = talloc_new(NULL);
- if (tmp_ctx == NULL) {
return ENOMEM;
- }
- anchor = build_anchor(tmp_ctx, obj_dn);
- if (anchor == NULL) {
ret = ENOMEM;
goto done;
- }
- override_dn = ldb_dn_new_fmt(tmp_ctx, ldb,
SYSDB_TMPL_OVERRIDE, anchor, LOCALVIEW);
- if (override_dn == NULL) {
ret = ENOMEM;
goto done;
- }
- DEBUG(SSSDBG_TRACE_FUNC, "Removing override for %s\n", obj_dn);
- ret = sysdb_transaction_start(domain->sysdb);
- if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "sysdb_transaction_start() failed.\n");
goto done;
- }
- in_transaction = true;
- ret = sysdb_delete_entry(domain->sysdb, override_dn, true);
ret is not checked here.
- msg = ldb_msg_new(tmp_ctx);
- if (msg == NULL) {
ret = ENOMEM;
goto done;
- }
- msg->dn = ldb_dn_new(msg, ldb, obj_dn);
- if (msg->dn == NULL) {
ret = ENOMEM;
goto done;
- }
- ret = ldb_msg_add_empty(msg, SYSDB_OVERRIDE_DN, LDB_FLAG_MOD_DELETE, NULL);
- if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add_empty() failed\n");
ret = sysdb_error_to_errno(ret);
goto done;
- }
- ret = ldb_modify(ldb, msg);
- if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_ATTRIBUTE) {
DEBUG(SSSDBG_OP_FAILURE,
"ldb_modify() failed: [%s](%d)[%s]\n",
ldb_strerror(ret), ret, ldb_errstring(ldb));
ret = sysdb_error_to_errno(ret);
goto done;
- }
[...]
+static int override_user_add(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
+{
- struct override_user user = {NULL};
- struct sysdb_attrs *attrs;
- const char *dn;
- int ret;
- ret = parse_cmdline_user_add(cmdline, tool_ctx, &user);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command line.\n");
return EXIT_FAILURE;
- }
- dn = get_user_dn_and_domain(tool_ctx, tool_ctx->domains, &user);
- if (dn == NULL) {
fprintf(stderr, "Unable to find user %s@%s.\n",
Missing translation.
user.orig_name, user.domain->name);
return EXIT_FAILURE;
- }
- ret = prepare_view(user.domain);
- if (ret == ENOENT) {
fprintf(stderr, "Other than LOCAL view already exist in domain %s.\n",
Missing translation.
user.domain->name);
return EXIT_FAILURE;
- }
- attrs = build_user_attrs(tool_ctx, &user);
- if (attrs == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
return EXIT_FAILURE;
- }
- ret = override_object_add(user.domain, SYSDB_MEMBER_USER, attrs, dn);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
+}
+static int override_user_del(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
+{
- struct override_user user = {NULL};
- const char *dn;
- int ret;
- ret = parse_cmdline_user_del(cmdline, tool_ctx, &user);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command line.\n");
return EXIT_FAILURE;
- }
- dn = get_user_dn_and_domain(tool_ctx, tool_ctx->domains, &user);
- if (dn == NULL) {
fprintf(stderr, "Unable to find user %s@%s.\n",
Missing translation.
user.orig_name, user.domain->name);
return EXIT_FAILURE;
- }
- ret = override_object_del(user.domain, dn);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
+}
+static int override_group_add(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
+{
- struct override_group group = {NULL};
- struct sysdb_attrs *attrs;
- const char *dn;
- int ret;
- ret = parse_cmdline_group_add(cmdline, tool_ctx, &group);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command line.\n");
return EXIT_FAILURE;
- }
- dn = get_group_dn_and_domain(tool_ctx, tool_ctx->domains, &group);
- if (dn == NULL) {
fprintf(stderr, "Unable to find group %s@%s.\n",
Missing translation.
group.orig_name, group.domain->name);
return EXIT_FAILURE;
- }
- ret = prepare_view(group.domain);
- if (ret == ENOENT) {
fprintf(stderr, "Other than LOCAL view already exist in domain %s.\n",
Missing translation.
group.domain->name);
return EXIT_FAILURE;
- }
- attrs = build_group_attrs(tool_ctx, &group);
- if (attrs == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
return EXIT_FAILURE;
- }
- ret = override_object_add(group.domain, SYSDB_MEMBER_GROUP, attrs, dn);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
+}
+static int override_group_del(struct sss_cmdline *cmdline,
struct sss_tool_ctx *tool_ctx,
void *pvt)
+{
- struct override_group group = {NULL};
- const char *dn;
- int ret;
- ret = parse_cmdline_group_del(cmdline, tool_ctx, &group);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command line.\n");
return EXIT_FAILURE;
- }
- dn = get_group_dn_and_domain(tool_ctx, tool_ctx->domains, &group);
- if (dn == NULL) {
fprintf(stderr, "Unable to find group %s@%s.\n",
Missing translation.
group.orig_name, group.domain->name);
return EXIT_FAILURE;
- }
- ret = override_object_del(group.domain, dn);
- if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
+}
+int main(int argc, const char **argv) +{
- struct sss_route_cmd commands[] = {
{"user-add", override_user_add},
{"user-del", override_user_del},
{"group-add", override_group_add},
{"group-del", override_group_del},
{NULL, NULL}
- };
- return sss_tool_main(argc, argv, commands, NULL);
OK, this is really nice :-)
It would be also nice to add integration tests for the overrides. But maybe it would be better if someone else than you actually wrote the tests to catch some issues..