[vlc-devel] [RFC PATCH 1/8] add vlc_keystore API

Rémi Denis-Courmont remi at remlab.net
Thu Dec 31 17:00:55 CET 2015


Le 2015-12-30 20:36, Thomas Guillem a écrit :
> ---
>  include/vlc_keystore.h | 194
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  src/Makefile.am        |   2 +
>  src/libvlc-module.c    |   7 ++
>  src/libvlccore.sym     |   6 ++
>  src/misc/keystore.c    | 125 +++++++++++++++++++++++++++++++
>  5 files changed, 334 insertions(+)
>  create mode 100644 include/vlc_keystore.h
>  create mode 100644 src/misc/keystore.c
>
> diff --git a/include/vlc_keystore.h b/include/vlc_keystore.h
> new file mode 100644
> index 0000000..1a1910f
> --- /dev/null
> +++ b/include/vlc_keystore.h
> @@ -0,0 +1,194 @@
> 
> +/*****************************************************************************
> + * vlc_keystore.h:
> +
> 
> *****************************************************************************
> + * Copyright (C) 2015 VLC authors and VideoLAN
> + *
> + * This program is free software; you can redistribute it and/or 
> modify it
> + * under the terms of the GNU Lesser General Public License as 
> published by
> + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public 
> License
> + * along with this program; if not, write to the Free Software 
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> +
> 
> *****************************************************************************/
> +
> +/**
> + * @file
> + * This file declares vlc keystore API
> + */
> +
> +#ifndef VLC_KEYSTORE_H
> +# define VLC_KEYSTORE_H
> +
> +#include <vlc_common.h>
> +
> +typedef struct vlc_keystore vlc_keystore;
> +typedef struct vlc_keystore_entry vlc_keystore_entry;
> +
> +/**
> + * @defgroup keystore Public API
> + * @{
> + */
> +
> +/**
> + * List of keys that can be stored via the keystore API
> + */
> +enum vlc_keystore_key {
> +    KEY_PROTOCOL,
> +    KEY_USER,
> +    KEY_SERVER,
> +    KEY_PATH,
> +    KEY_PORT,
> +    KEY_REALM,
> +    KEY_AUTHTYPE,
> +    KEY_MAX,
> +};
> +#define VLC_KEYSTORE_VALUES_INIT(ppsz_values) memset(ppsz_values, 0,
> sizeof(const char *) * KEY_MAX)
> +
> +/**
> + * Keystore entry returned by vlc_keystore_find()
> + */
> +struct vlc_keystore_entry
> +{
> +    /* Set of key/values. Values can be NULL */
> +    char *              ppsz_values[KEY_MAX];
> +    /* Secret password */
> +    uint8_t *           p_secret;
> +    /* Length of the secret */
> +    size_t              i_secret_len;
> +};
> +
> +/**
> + * Create a keystore object
> + *
> + * A keystore object is persistent across runtime. It is saved on 
> local
> + * filesystem via a vlc keystore module (KWallet, SecretService,
> Apple Keychain
> + * Service ...).
> + *
> + * @note to be released with vlc_keystore_release()
> + *
> + * @param p_parent the parent object used to create the keystore 
> object
> + *
> + * @return a pointer to the keystore object, or NULL in case of 
> error
> + */
> +VLC_API vlc_keystore *
> +vlc_keystore_create(vlc_object_t *p_parent);
> +#define vlc_keystore_create(x) vlc_keystore_create(VLC_OBJECT(x))
> +
> +/**
> + * Release a keystore object
> + */
> +VLC_API void
> +vlc_keystore_release(vlc_keystore *p_keystore);
> +
> +
> +/**
> + * Store a secret associated with a set of key/values
> + *
> + * @param ppsz_values set of key/values, see vlc_keystore_key.
> + *        ppsz_values[KEY_PROTOCOL], ppsz_values[KEY_SERVER],
> + *        ppsz_values[KEY_PATH] and ppsz_values[KEY_USER] must be
> valid strings
> + * @param p_secret binary secret or string password
> + * @param i_secret_len length of p_secret. If it's less than 0, then
> p_secret
> + * is assumed to be a '\0' terminated string
> + * @param psz_label user friendly label
> + *
> + * @return VLC_SUCCESS on success, or VLC_EGENERIC on error
> + */
> +VLC_API int
> +vlc_keystore_store(vlc_keystore *p_keystore,
> +                   const char *const ppsz_values[KEY_MAX],
> +                   const uint8_t* p_secret, ssize_t i_secret_len,
> +                   const char *psz_label);
> +
> +/**
> + * Find all entries that match a set of key/values
> + *
> + * @param ppsz_values set of key/values, see vlc_keystore_key, any
> values can
> + * be NULL
> + * @param pp_entries list of found entries. To be released with
> + * vlc_keystore_release_entries()
> + *
> + * @return the number of entries
> + */
> +VLC_API unsigned int
> +vlc_keystore_find(vlc_keystore *p_keystore,
> +                  const char *const ppsz_values[KEY_MAX],
> +                  vlc_keystore_entry **pp_entries) VLC_USED;
> +
> +/**
> + * Remove all entries that match a set of key/values
> + *
> + * @note only entries added by VLC can be removed
> + *
> + * @param ppsz_values set of key/values, see vlc_keystore_key, any
> values can
> + * be NULL
> + *
> + * @return the number of entries
> + */
> +VLC_API unsigned int
> +vlc_keystore_remove(vlc_keystore *p_keystore,
> +                    const char *const ppsz_values[KEY_MAX]);
> +
> +/**
> + * Release the list of entries returned by vlc_keystore_find()
> + */
> +VLC_API void
> +vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned
> int i_count);
> +
> +/**
> + * @}
> + * @defgroup keystore_implementation to be implemented by keystore 
> modules
> + * @{
> + */
> +
> +#define VLC_KEYSTORE_NAME "libVLC"
> +
> +static inline int
> +vlc_keystore_entry_set_secret(vlc_keystore_entry *p_entry,
> +                              const uint8_t *p_secret, size_t 
> i_secret_len)
> +{
> +    p_entry->p_secret = (uint8_t*) malloc(i_secret_len);
> +    if (!p_entry->p_secret)
> +        return VLC_EGENERIC;
> +    memcpy(p_entry->p_secret, p_secret, i_secret_len);
> +    p_entry->i_secret_len = i_secret_len;
> +    return VLC_SUCCESS;
> +}
> +
> +typedef struct vlc_keystore_sys vlc_keystore_sys;
> +struct vlc_keystore
> +{
> +    VLC_COMMON_MEMBERS
> +
> +    /* Module properties */
> +    module_t            *p_module;
> +    vlc_keystore_sys    *p_sys;
> +
> +    /* functions to implement */
> +
> +    /* See vlc_keystore_store() */
> +    int                 (*pf_store)(vlc_keystore *p_keystore,
> +                                    const char *const 
> ppsz_values[KEY_MAX],
> +                                    const uint8_t *p_secret,
> +                                    size_t i_secret_len, const char
> *psz_label);
> +
> +    /* See vlc_keystore_find() */
> +    unsigned int        (*pf_find)(vlc_keystore *p_keystore,
> +                                   const char *const 
> ppsz_values[KEY_MAX],
> +                                   vlc_keystore_entry **pp_entries);
> +
> +    /* See vlc_keystore_remove() */
> +    unsigned int        (*pf_remove)(vlc_keystore *p_keystore,
> +                                     const char *const
> ppsz_values[KEY_MAX]);
> +};
> +
> +/** @} @} */
> +
> +#endif
> diff --git a/src/Makefile.am b/src/Makefile.am
> index e0155dd..da72a4c 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -59,6 +59,7 @@ pluginsinclude_HEADERS = \
>  	../include/vlc_input_item.h \
>  	../include/vlc_interface.h \
>  	../include/vlc_keys.h \
> +	../include/vlc_keystore.h \
>  	../include/vlc_main.h \
>  	../include/vlc_md5.h \
>  	../include/vlc_messages.h \
> @@ -442,6 +443,7 @@ SOURCES_libvlc_common = \
>  	misc/picture_pool.c \
>  	misc/interrupt.h \
>  	misc/interrupt.c \
> +	misc/keystore.c \
>  	modules/modules.h \
>  	modules/modules.c \
>  	modules/bank.c \
> diff --git a/src/libvlc-module.c b/src/libvlc-module.c
> index 3556c98..c3e4a01 100644
> --- a/src/libvlc-module.c
> +++ b/src/libvlc-module.c
> @@ -1026,6 +1026,11 @@ static const char *const ppsz_prefres[] = {
>  #define PLUGINS_CACHE_LONGTEXT N_( \
>      "Use a plugins cache which will greatly improve the startup time
> of VLC.")
>
> +#define KEYSTORE_TEXT N_("Preferred keystore list")
> +#define KEYSTORE_LONGTEXT N_( \
> +    "List of keystores that VLC will use in " \
> +    "priority. Only advanced users should alter this option." )
> +
>  #define STATS_TEXT N_("Locally collect statistics")
>  #define STATS_LONGTEXT N_( \
>       "Collect miscellaneous local statistics about the playing 
> media.")
> @@ -1952,6 +1957,8 @@ vlc_module_begin ()
>                PLUGINS_CACHE_LONGTEXT, true )
>      add_obsolete_string( "plugin-path" ) /* since 2.0.0 */
>      add_obsolete_string( "data-path" ) /* since 2.1.0 */
> +    add_string( "keystore", NULL, KEYSTORE_TEXT,
> +                KEYSTORE_LONGTEXT, true )
>
>      set_section( N_("Performance options"), NULL )
>
> diff --git a/src/libvlccore.sym b/src/libvlccore.sym
> index 43b1ba0..e624fcb 100644
> --- a/src/libvlccore.sym
> +++ b/src/libvlccore.sym
> @@ -552,6 +552,12 @@ vlc_ngettext
>  vlc_iconv
>  vlc_iconv_close
>  vlc_iconv_open
> +vlc_keystore_create
> +vlc_keystore_release
> +vlc_keystore_find
> +vlc_keystore_remove
> +vlc_keystore_store
> +vlc_keystore_release_entries
>  vlc_poll_i11e
>  vlc_read_i11e
>  vlc_readv_i11e
> diff --git a/src/misc/keystore.c b/src/misc/keystore.c
> new file mode 100644
> index 0000000..8af13dc
> --- /dev/null
> +++ b/src/misc/keystore.c
> @@ -0,0 +1,125 @@
> 
> +/*****************************************************************************
> + * keystore.c:
> +
> 
> *****************************************************************************
> + * Copyright (C) 2015 VLC authors, VideoLAN and VideoLabs
> + *
> + * This program is free software; you can redistribute it and/or 
> modify it
> + * under the terms of the GNU Lesser General Public License as 
> published by
> + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public 
> License
> + * along with this program; if not, write to the Free Software 
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> +
> 
> *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include <config.h>
> +#endif
> +
> +#include <vlc_common.h>
> +#include <vlc_keystore.h>
> +#include <vlc_modules.h>
> +#include <libvlc.h>
> +
> +#include <assert.h>
> +#include <limits.h>
> +
> +#undef vlc_keystore_create
> +vlc_keystore *
> +vlc_keystore_create(vlc_object_t *p_parent)
> +{
> +    assert(p_parent);
> +    vlc_keystore *p_keystore = vlc_custom_create(p_parent, sizeof
> (*p_keystore),
> +                                                 "keystore");
> +    if (unlikely(p_keystore == NULL))
> +        return NULL;
> +
> +    p_keystore->p_module = module_need(p_keystore, "keystore", 
> "$keystore",
> +                                       true);
> +    if (p_keystore->p_module == NULL)
> +    {
> +        vlc_object_release(p_keystore);
> +        return NULL;
> +    }
> +    assert(p_keystore->pf_store);
> +    assert(p_keystore->pf_find);
> +    assert(p_keystore->pf_remove);
> +
> +    return p_keystore;
> +}
> +
> +void
> +vlc_keystore_release(vlc_keystore *p_keystore)
> +{
> +    assert(p_keystore);
> +    module_unneed(p_keystore, p_keystore->p_module);
> +
> +    vlc_object_release(p_keystore);
> +}
> +
> +int
> +vlc_keystore_store(vlc_keystore *p_keystore,
> +                   const char * const ppsz_values[KEY_MAX],
> +                   const uint8_t *p_secret, ssize_t i_secret_len,
> +                   const char *psz_label)
> +{
> +    assert(p_keystore && ppsz_values && p_secret && i_secret_len);
> +
> +    if (!ppsz_values[KEY_PROTOCOL] || !ppsz_values[KEY_SERVER]
> +     || !ppsz_values[KEY_PATH] || !ppsz_values[KEY_USER])

So how does this work if the username is not known?

Also AFAIK, FTP and SSH credentials don't depend on the path.

> +    {
> +        msg_Err(p_keystore, "invalid store request: "
> +                "protocol, server, path and user should be valid");
> +        return VLC_EGENERIC;
> +    }
> +    if (ppsz_values[KEY_PORT])
> +    {
> +        long int i_port = strtol(ppsz_values[KEY_PORT], NULL, 10);
> +        if (i_port == LONG_MIN || i_port == LONG_MAX)
> +        {
> +            msg_Err(p_keystore, "invalid store request: "
> +                    "port is not valid number");
> +            return VLC_EGENERIC;
> +        }
> +    }
> +    if (i_secret_len < 0)
> +        i_secret_len = strlen((const char *)p_secret) + 1;
> +    return p_keystore->pf_store(p_keystore, ppsz_values, p_secret,
> i_secret_len,
> +                                psz_label);
> +}
> +
> +unsigned int
> +vlc_keystore_find(vlc_keystore *p_keystore,
> +                  const char * const ppsz_values[KEY_MAX],
> +                  vlc_keystore_entry **pp_entries)
> +{
> +    assert(p_keystore && ppsz_values && pp_entries);
> +    return p_keystore->pf_find(p_keystore, ppsz_values, pp_entries);
> +}
> +
> +unsigned int
> +vlc_keystore_remove(vlc_keystore *p_keystore,
> +                    const char *const ppsz_values[KEY_MAX])
> +{
> +    assert(p_keystore && ppsz_values);
> +    return p_keystore->pf_remove(p_keystore, ppsz_values);
> +}
> +
> +void
> +vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned
> int i_count)
> +{
> +    for (unsigned int i = 0; i < i_count; ++i)
> +    {
> +        vlc_keystore_entry *p_entry = &p_entries[i];
> +        for (unsigned int j = 0; j < KEY_MAX; ++j)
> +            free(p_entry->ppsz_values[j]);
> +        free(p_entry->p_secret);
> +    }
> +    free (p_entries);
> +}

-- 
Rémi Denis-Courmont
http://www.remlab.net/


More information about the vlc-devel mailing list