[SSSD] [PATCHES] Views: apply user SSH public key override

Jakub Hrozek jhrozek at redhat.com
Tue Nov 4 09:07:12 UTC 2014


On Tue, Oct 28, 2014 at 04:41:44PM +0100, Sumit Bose wrote:
> Hi,
> 
> this series of patches add support for SSH public key overrides. The
> main part is in patch 004. The others mainly enhance the exiting
> functions with respect to multi-values and variable attribute lists.
> 
> In contrast to other overrides the SSH public keys from the override
> attributes do not override any existing ones but are added to the
> existing ones. Please tell me if you think this is not a good idea so
> that I can change it.
> 
> bye,
> Sumit

I'm still running some tests and because I ran into issues updating my
devel server, I'm sending code-wise review now to speed up the process.

btw I discussed the old format of ssh keys with Honza and even though
neither of us tested them, the code is going to handle them fine,
because the conversion is happening as late as the client libraries
(sss_ssh_*).

> From c4fda98f9df7dd665e28f3c71097125bbf2ba07c Mon Sep 17 00:00:00 2001
> From: Sumit Bose <sbose at redhat.com>
> Date: Mon, 27 Oct 2014 16:53:44 +0100
> Subject: [PATCH 1/5] Add add_strings_lists() utilty function
> 
> ---
>  src/tests/cmocka/test_utils.c | 111 ++++++++++++++++++++++++++++++++++++++++++

Thanks for the unit tests!

>  src/util/util.c               |  60 +++++++++++++++++++++++
>  src/util/util.h               |  18 +++++++
>  3 files changed, 189 insertions(+)
> 

[...]

> +/**
> + * @brief Add two list of strings
> + *
> + * Create a new NULL-termintated list of strings by adding two lists together.
> + *
> + * @param[in] mem_ctx      Talloc memory context for the new list.
> + * @param[in] l1           First NULL-termintated list of strings.
> + * @param[in] l2           Second NULL-termintated list of strings.
> + * @param[in] copy_strings If set to 'true' the list items will be copied
> + *                         otherwise only the pointers to the items are
> + *                         copied.
> + * @param[out] new_list    New NULL-terminated list of strings. Must be freed
> + *                         with talloc_free() by the caller. If copy_strings
> + *                         is 'true' the new elements will be freed as well.
> + */
> +errno_t add_strings_lists(TALLOC_CTX *mem_ctx, const char **l1, const char **l2,
> +                          bool copy_strings, char ***new_list);
> +

Can you prefix the output variable with a leading underscore? I know in
this case it's clear from the name it's an output variable, but the
prefix is more or less a convention we follow.

There is also a typo in the commit message (utilty).

Otherwise ACK.

> From 93b96813267feb55c9d12bdddefd58fc4e4f1f2b Mon Sep 17 00:00:00 2001
> From: Sumit Bose <sbose at redhat.com>
> Date: Mon, 27 Oct 2014 13:33:08 +0100
> Subject: [PATCH 2/5] sysdb_get_user_attr_with_views: add mandatory override
>  attributes

ACK. I don't see an easy way around the discard_const either 

> From 3cebae5b5569dc559896945e244e0ae9575b04d1 Mon Sep 17 00:00:00 2001
> From: Sumit Bose <sbose at redhat.com>
> Date: Mon, 27 Oct 2014 15:11:08 +0100
> Subject: [PATCH 3/5] sysdb_add_overrides_to_object: add new parameter and
>  multi-value support

Can you add the new parameter to the doxygen description of
sysdb_add_overrides_to_object() ?

> 
> With the new parameter an attribute list other than the default one can
> be used.
> 
> Override attributes with multiple values (e.g. SSH public keys) are noew
> supported as well.
> ---
>  src/db/sysdb.h                 |  3 ++-
>  src/db/sysdb_search.c          | 24 ++++++++++++++++--------
>  src/db/sysdb_views.c           | 39 +++++++++++++++++++++++----------------
>  src/responder/nss/nsssrv_cmd.c |  2 +-
>  4 files changed, 42 insertions(+), 26 deletions(-)
> 
> diff --git a/src/db/sysdb.h b/src/db/sysdb.h
> index ebb1bbe..f582f6a 100644
> --- a/src/db/sysdb.h
> +++ b/src/db/sysdb.h
> @@ -487,7 +487,8 @@ errno_t sysdb_search_group_override_by_gid(TALLOC_CTX *mem_ctx,
>  
>  errno_t sysdb_add_overrides_to_object(struct sss_domain_info *domain,
>                                        struct ldb_message *obj,
> -                                      struct ldb_message *override_obj);
> +                                      struct ldb_message *override_obj,
> +                                      const char **req_attrs);
>  
>  errno_t sysdb_add_group_member_overrides(struct sss_domain_info *domain,
>                                           struct ldb_message *obj);
> diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
> index dacbd23..c039aa8 100644
> --- a/src/db/sysdb_search.c
> +++ b/src/db/sysdb_search.c
> @@ -124,7 +124,8 @@ errno_t sysdb_getpwnam_with_views(TALLOC_CTX *mem_ctx,
>       * the original object. */
>      if (DOM_HAS_VIEWS(domain) && orig_obj->count == 1) {
>          ret = sysdb_add_overrides_to_object(domain, orig_obj->msgs[0],
> -                          override_obj == NULL ? NULL : override_obj ->msgs[0]);
> +                          override_obj == NULL ? NULL : override_obj ->msgs[0],

Is the space after override_obj intentional? It looks a bit odd ...

> +                          NULL);
>          if (ret != EOK && ret != ENOENT) {
>              DEBUG(SSSDBG_OP_FAILURE, "sysdb_add_overrides_to_object failed.\n");
>              goto done;

The rest of the patch looks good to me.

> From 2d14490b0369e98afb17c1e340e2555d79187faa Mon Sep 17 00:00:00 2001
> From: Sumit Bose <sbose at redhat.com>
> Date: Thu, 16 Oct 2014 13:17:37 +0200
> Subject: [PATCH 4/5] Views: apply user SSH public key override
> 
> With this patch the SSH public key override attribute is read from the
> FreeIPA server and saved in the cache with the other override data.
> 
> Since it is possible to have multiple public SSH keys this override
> value does not replace any other data but will be added to existing
> values.
> 
> Fixes https://fedorahosted.org/sssd/ticket/2454
> ---
>  src/db/sysdb_views.c           |  38 +++++++++----
>  src/man/sssd-ipa.5.xml         |   3 +
>  src/providers/ipa/ipa_common.h |   1 +
>  src/providers/ipa/ipa_opts.h   |   1 +
>  src/responder/ssh/sshsrv_cmd.c | 123 +++++++++++++++++++++++++++++++----------
>  5 files changed, 126 insertions(+), 40 deletions(-)
> 
> diff --git a/src/db/sysdb_views.c b/src/db/sysdb_views.c
> index b8e90dd..d6cae9a 100644
> --- a/src/db/sysdb_views.c
> +++ b/src/db/sysdb_views.c
> @@ -560,6 +560,8 @@ errno_t sysdb_apply_default_override(struct sss_domain_info *domain,
>      TALLOC_CTX *tmp_ctx;
>      struct sysdb_attrs *attrs;
>      size_t c;
> +    size_t d;
> +    size_t num_values;
>      struct ldb_message_element *el = NULL;
>      const char *allowed_attrs[] = { SYSDB_UIDNUM,
>                                      SYSDB_GIDNUM,
> @@ -567,6 +569,7 @@ errno_t sysdb_apply_default_override(struct sss_domain_info *domain,
>                                      SYSDB_HOMEDIR,
>                                      SYSDB_SHELL,
>                                      SYSDB_NAME,
> +                                    SYSDB_SSH_PUBKEY,
>                                      NULL };
>      bool override_attrs_found = false;
>  
> @@ -584,7 +587,6 @@ errno_t sysdb_apply_default_override(struct sss_domain_info *domain,
>      }
>  
>      for (c = 0; allowed_attrs[c] != NULL; c++) {
> -        /* TODO: add nameAlias for case-insentitive searches */
>          ret = sysdb_attrs_get_el_ext(override_attrs, allowed_attrs[c], false,
>                                       &el);
>          if (ret == EOK) {
> @@ -607,17 +609,30 @@ errno_t sysdb_apply_default_override(struct sss_domain_info *domain,
>                      goto done;
>                  }
>              } else {
> -                ret = sysdb_attrs_add_val(attrs,  allowed_attrs[c],
> -                                          &el->values[0]);
> -                if (ret != EOK) {
> -                    DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_val failed.\n");
> -                    goto done;
> +                num_values = el->num_values;
> +                /* Only SYSDB_SSH_PUBKEY is allowed to have multiple values. */
> +                if (strcmp(allowed_attrs[c], SYSDB_SSH_PUBKEY) != 0
> +                        && num_values != 1) {
> +                    DEBUG(SSSDBG_MINOR_FAILURE,
> +                          "Override attribute for [%s] has more [%d] " \
> +                          "than one value, using onlye the first.\n",
                                                    ^^^^^
                                                    Typo..

Also adds a compilation warning:
src/db/sysdb_views.c: In function 'sysdb_apply_default_override':
src/db/sysdb_views.c:616:21: warning: format '%d' expects argument of type 'int', but argument 7 has type 'size_t' [-Wformat=]

> +                          allowed_attrs[c], num_values);
> +                    num_values = 1;
> +                }
> +
> +                for (d = 0; d < num_values; d++) {
> +                    ret = sysdb_attrs_add_val(attrs,  allowed_attrs[c],
> +                                              &el->values[d]);
> +                    if (ret != EOK) {
> +                        DEBUG(SSSDBG_OP_FAILURE,
> +                              "sysdb_attrs_add_val failed.\n");
> +                        goto done;
> +                    }
> +                    DEBUG(SSSDBG_TRACE_ALL,
> +                          "Override [%s] with [%.*s] for [%s].\n",
> +                          allowed_attrs[c], (int) el->values[d].length,
> +                          el->values[d].data, ldb_dn_get_linearized(obj_dn));
>                  }
> -                DEBUG(SSSDBG_TRACE_ALL, "Override [%s] with [%.*s] for [%s].\n",
> -                                        allowed_attrs[c],
> -                                        (int) el->values[0].length,
> -                                        el->values[0].data,
> -                                        ldb_dn_get_linearized(obj_dn));
>              }
>          } else if (ret != ENOENT) {
>              DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_el_ext failed.\n");
> @@ -981,6 +996,7 @@ errno_t sysdb_add_overrides_to_object(struct sss_domain_info *domain,
>          {SYSDB_HOMEDIR, OVERRIDE_PREFIX SYSDB_HOMEDIR},
>          {SYSDB_SHELL, OVERRIDE_PREFIX SYSDB_SHELL},
>          {SYSDB_NAME, OVERRIDE_PREFIX SYSDB_NAME},
> +        {SYSDB_SSH_PUBKEY, OVERRIDE_PREFIX SYSDB_SSH_PUBKEY},
>          {NULL, NULL}
>      };
>      size_t c;
> diff --git a/src/man/sssd-ipa.5.xml b/src/man/sssd-ipa.5.xml
> index 51f14f8..e8a716c 100644
> --- a/src/man/sssd-ipa.5.xml
> +++ b/src/man/sssd-ipa.5.xml
> @@ -626,6 +626,9 @@
>                                      <listitem>
>                                          <para>ldap_user_shell</para>
>                                      </listitem>
> +                                    <listitem>
> +                                        <para>ldap_user_ssh_public_key</para>
> +                                    </listitem>
>                                  </itemizedlist>
>                              </para>
>                              <para>
> diff --git a/src/providers/ipa/ipa_common.h b/src/providers/ipa/ipa_common.h
> index 0e9324f..4952765 100644
> --- a/src/providers/ipa/ipa_common.h
> +++ b/src/providers/ipa/ipa_common.h
> @@ -128,6 +128,7 @@ enum ipa_override_attrs {
>      IPA_AT_OVERRIDE_SHELL,
>      IPA_AT_OVERRIDE_GROUP_NAME,
>      IPA_AT_OVERRIDE_GROUP_GID_NUMBER,
> +    IPA_AT_OVERRIDE_USER_SSH_PUBLIC_KEY,
>  
>      IPA_OPTS_OVERRIDE
>  };
> diff --git a/src/providers/ipa/ipa_opts.h b/src/providers/ipa/ipa_opts.h
> index 4785e01..32f5ebe 100644
> --- a/src/providers/ipa/ipa_opts.h
> +++ b/src/providers/ipa/ipa_opts.h
> @@ -283,6 +283,7 @@ struct sdap_attr_map ipa_override_map[] = {
>      { "ldap_user_shell", "loginShell", SYSDB_SHELL, NULL },
>      { "ldap_group_name", "cn", SYSDB_NAME, NULL },
>      { "ldap_group_gid_number", "gidNumber", SYSDB_GIDNUM, NULL },
> +    { "ldap_user_ssh_public_key", "ipaSshPubKey", SYSDB_SSH_PUBKEY, NULL },
>      SDAP_ATTR_MAP_TERMINATOR
>  };
>  
> diff --git a/src/responder/ssh/sshsrv_cmd.c b/src/responder/ssh/sshsrv_cmd.c
> index ad83163..5bed2e0 100644
> --- a/src/responder/ssh/sshsrv_cmd.c
> +++ b/src/responder/ssh/sshsrv_cmd.c
> @@ -232,8 +232,8 @@ ssh_user_pubkeys_search_next(struct ssh_cmd_ctx *cmd_ctx)
>          return EFAULT;
>      }
>  
> -    ret = sysdb_get_user_attr(cmd_ctx, cmd_ctx->domain,
> -                              cmd_ctx->name, attrs, &res);
> +    ret = sysdb_get_user_attr_with_views(cmd_ctx, cmd_ctx->domain,
> +                                         cmd_ctx->name, attrs, &res);
>      if (ret != EOK) {
>          DEBUG(SSSDBG_CRIT_FAILURE,
>                "Failed to make request to our cache!\n");
> @@ -782,6 +782,65 @@ ssh_cmd_parse_request(struct ssh_cmd_ctx *cmd_ctx)
>      return EOK;
>  }
>  
> +static errno_t decode_and_add_base64_data(struct ssh_cmd_ctx *cmd_ctx,
> +                                          struct ldb_message_element *el,
> +                                          size_t fqname_len,
> +                                          const char *fqname,
> +                                          size_t *c)
> +{
> +    struct cli_ctx *cctx = cmd_ctx->cctx;
> +    uint8_t *key;
> +    size_t key_len;
> +    uint8_t *body;
> +    size_t body_len;
> +    int ret;
> +    size_t d;
> +    TALLOC_CTX *tmp_ctx;
> +
> +    if (el == NULL) {
> +        DEBUG(SSSDBG_TRACE_ALL, "Mssing element, nothing to do.\n");
> +        return EOK;
> +    }
> +
> +    tmp_ctx = talloc_new(NULL);
> +    if (tmp_ctx == NULL) {
> +        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
> +        return ENOMEM;
> +    }
> +
> +    for (d = 0; d < el->num_values; d++) {
> +        key = sss_base64_decode(tmp_ctx, (const char *) el->values[d].data,
> +                                &key_len);
> +        if (key == NULL) {
> +            DEBUG(SSSDBG_OP_FAILURE, "sss_base64_decode failed.\n");
> +            ret = ENOMEM;
> +            goto done;
> +        }
> +
> +        ret = sss_packet_grow(cctx->creq->out,
> +                              3*sizeof(uint32_t) + key_len + fqname_len);
> +        if (ret != EOK) {
> +            DEBUG(SSSDBG_OP_FAILURE, "sss_packet_grow failed.\n");
> +            goto done;
> +        }
> +        sss_packet_get_body(cctx->creq->out, &body, &body_len);
> +
> +        SAFEALIGN_SET_UINT32(body+(*c), 0, c);
> +        SAFEALIGN_SET_UINT32(body+(*c), fqname_len, c);
> +        safealign_memcpy(body+(*c), fqname, fqname_len, c);
> +        SAFEALIGN_SET_UINT32(body+(*c), key_len, c);
> +        safealign_memcpy(body+(*c), key, key_len, c);
> +
> +    }
> +
> +    ret = EOK;
> +
> +done:
> +    talloc_free(tmp_ctx);
> +
> +    return ret;
> +}
> +
>  static errno_t
>  ssh_cmd_build_reply(struct ssh_cmd_ctx *cmd_ctx)
>  {
> @@ -790,14 +849,13 @@ ssh_cmd_build_reply(struct ssh_cmd_ctx *cmd_ctx)
>      uint8_t *body;
>      size_t body_len;
>      size_t c = 0;
> -    unsigned int i;
> -    struct ldb_message_element *el;
> +    struct ldb_message_element *el = NULL;
> +    struct ldb_message_element *el_override = NULL;
> +    struct ldb_message_element *el_orig = NULL;
>      uint32_t count = 0;
>      const char *name;
>      char *fqname;
>      uint32_t fqname_len;
> -    uint8_t *key;
> -    size_t key_len;
>  
>      ret = sss_packet_new(cctx->creq, 0,
>                           sss_packet_get_cmd(cctx->creq->in),
> @@ -811,6 +869,20 @@ ssh_cmd_build_reply(struct ssh_cmd_ctx *cmd_ctx)
>          count = el->num_values;
>      }
>  
> +    el_orig = ldb_msg_find_element(cmd_ctx->result,
> +                                  ORIGINALAD_PREFIX SYSDB_SSH_PUBKEY);
> +    if (el_orig) {
> +        count = el_orig->num_values;
> +    }
> +
> +    if (DOM_HAS_VIEWS(cmd_ctx->domain)) {
> +        el_override = ldb_msg_find_element(cmd_ctx->result,
> +                                           OVERRIDE_PREFIX SYSDB_SSH_PUBKEY);
> +        if (el_override) {
> +            count += el_override->num_values;
> +        }
> +    }
> +
>      ret = sss_packet_grow(cctx->creq->out, 2*sizeof(uint32_t));
>      if (ret != EOK) {
>          return ret;
> @@ -820,7 +892,7 @@ ssh_cmd_build_reply(struct ssh_cmd_ctx *cmd_ctx)
>      SAFEALIGN_SET_UINT32(body+c, count, &c);
>      SAFEALIGN_SET_UINT32(body+c, 0, &c);
>  
> -    if (!el) {
> +    if (count == 0) {
>          return EOK;
>      }
>  
> @@ -840,30 +912,23 @@ ssh_cmd_build_reply(struct ssh_cmd_ctx *cmd_ctx)
>  
>      fqname_len = strlen(fqname)+1;
>  
> -    for (i = 0; i < el->num_values; i++) {
> -        key = sss_base64_decode(cmd_ctx,
> -                                (const char *)el->values[i].data,
> -                                &key_len);
> -        if (!key) {
> -            return ENOMEM;
> -        }
> -
> -        ret = sss_packet_grow(cctx->creq->out,
> -                              3*sizeof(uint32_t) + key_len + fqname_len);
> -        if (ret != EOK) {
> -            talloc_free(key);
> -            return ret;
> -        }
> -        sss_packet_get_body(cctx->creq->out, &body, &body_len);
> +    ret = decode_and_add_base64_data(cmd_ctx, el, fqname_len, fqname, &c);
> +    if (ret != EOK) {
> +        DEBUG(SSSDBG_OP_FAILURE, "decode_and_add_base64_data failed.\n");
> +        return ret;
> +    }
>  
> -        SAFEALIGN_SET_UINT32(body+c, 0, &c);
> -        SAFEALIGN_SET_UINT32(body+c, fqname_len, &c);
> -        safealign_memcpy(body+c, fqname, fqname_len, &c);
> -        SAFEALIGN_SET_UINT32(body+c, key_len, &c);
> -        safealign_memcpy(body+c, key, key_len, &c);
> +    ret = decode_and_add_base64_data(cmd_ctx, el_orig, fqname_len, fqname, &c);
> +    if (ret != EOK) {
> +        DEBUG(SSSDBG_OP_FAILURE, "decode_and_add_base64_data failed.\n");
> +        return ret;
> +    }
>  
> -        talloc_free(key);
> -        count++;
> +    ret = decode_and_add_base64_data(cmd_ctx, el_override, fqname_len, fqname,
> +                                     &c);
> +    if (ret != EOK) {
> +        DEBUG(SSSDBG_OP_FAILURE, "decode_and_add_base64_data failed.\n");
> +        return ret;
>      }
>  
>      return EOK;
> -- 
> 1.8.3.1
> 

> From 60fb14f60d9a301d6913bc1c29fc652225c3d21d Mon Sep 17 00:00:00 2001
> From: Sumit Bose <sbose at redhat.com>
> Date: Tue, 28 Oct 2014 15:53:16 +0100
> Subject: [PATCH 5/5] Add test for sysdb_add_overrides_to_object()

ACK



More information about the sssd-devel mailing list