[SSSD] [PATCH 3/4] Add authtok utility functions.

Pavel Březina pbrezina at redhat.com
Mon Dec 3 14:34:54 UTC 2012


On 12/02/2012 05:59 AM, Simo Sorce wrote:
> These functions allow handling of auth tokens in a completely opaque way,
> with clear semantics and accessor fucntions that guarantee consistency,
> proper access to data and error conditions.
> ---

/home/pbrezina/workspace/sssd/.git/rebase-apply/patch:229: new blank 
line at EOF.
+
warning: 1 line adds whitespace errors.

Can we move enum sss_authtok_type to authtok.h?

> +size_t sss_authtok_get_size(struct sss_auth_token *tok)
> +{
> +    switch (tok->type) {
> +    case SSS_AUTHTOK_TYPE_PASSWORD:
> +    case SSS_AUTHTOK_TYPE_CCFILE:
> +        return tok->length;
> +    default:
> +        return 0;
> +    }
> +}

Return 0 for SSS_AUTHTOK_TYPE_CCFILE and print error otherwise?
Also not using default branch for enum would be nice, because we'll get 
compiler error if we add new enum value and not handle it in switch.

> +
> +uint8_t *sss_authtok_get_data(struct sss_auth_token *tok)
> +{
> +    return (void *)tok->data;
> +}

Why are you typecasting to void* when you are returning uint8_t*? And 
tok->data is already uint8_t*.

> +static errno_t sss_authtok_set_string(TALLOC_CTX *mem_ctx,
> +                                      struct sss_auth_token *tok,
> +                                      enum sss_authtok_type type,
> +                                      const char *context_name,
> +                                      const char *str, size_t len)
> +{
> +    size_t size;
> +
> +    if (len == 0) {
> +        len = strlen(str);
> +    } else {
> +        while (len > 0 && str[len - 1] == '\0') len--;
> +    }
> +
> +    if (len == 0) {

You should check len <= 0, because if negative len was passed in 
parameter list, it will get here unchanged.

> +        /* we do not allow zero length ttyped tokens */

                                           ^ typo
> +        return EINVAL;
> +    }
> +
> +    size = len + 1;
> +
> +    tok->data = talloc_named(mem_ctx, size, context_name);
> +    if (!tok->data) {
> +        return ENOMEM;
> +    }
> +    memcpy(tok->data, str, len);
> +    tok->data[len] = '\0';
> +    tok->type = type;
> +    tok->length = size;
> +
> +    return EOK;
> +
> +}

> +
> diff --git a/src/util/authtok.h b/src/util/authtok.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..c750711ea373506118aab75807dc706c217f6842
> --- /dev/null
> +++ b/src/util/authtok.h
> @@ -0,0 +1,180 @@
> +/*
> +   SSSD - auth utils
> +
> +   Copyright (C) Simo Sorce <simo at redhat.com> 2012
> +
> +   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 __AUTHTOK_H__
> +#define __AUTHTOK_H__
> +
> +#include "util/util.h"
> +#include "sss_client/sss_cli.h"
> +
> +/* Auth token structure,
> + * please never use directly.
> + * Use ss_authtok_* accesor functions instead
           ^typo




More information about the sssd-devel mailing list