[vlc-devel] [RFC PATCH] add vlc_keystore API

Steve Lhomme robux4 at gmail.com
Mon Nov 23 16:52:26 CET 2015


On Mon, Nov 23, 2015 at 6:10 AM, Thomas Guillem <thomas at gllm.fr> wrote:
> ---
>  include/vlc_keystore.h | 202 +++++++++++++++++++++++++++++++++++++++++++++++++
>  src/Makefile.am        |   2 +
>  src/misc/keystore.c    | 162 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 366 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..d9d1d17
> --- /dev/null
> +++ b/include/vlc_keystore.h
> @@ -0,0 +1,202 @@
> +/*****************************************************************************
> + * vlc_keystore.h:
> + *****************************************************************************
> + * 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.
> + *****************************************************************************/
> +
> +/**
> + * @file
> + * This file declares vlc keystore API
> + */
> +
> +#ifndef VLC_KEYSTORE_H
> +# define VLC_KEYSTORE_H
> +
> +typedef struct vlc_keystore vlc_keystore;
> +typedef struct vlc_keystore_entry vlc_keystore_entry;
> +
> +/**
> + * @defgroup keystore Public API used by access/demux modules
> + * @{
> + */
> +
> +struct vlc_keystore_entry
> +{
> +    vlc_dictionary_t dict;
> +    char psz_passwd[];
> +};
> +
> +/**
> + * Get a keystore object.
> + *
> + * A keystore object is persistent across runtime. It is saved on local
> + * filesystem via a vlc keystore module (KWallet, GnomreKeyring, Apple Keychain
> + * Services ...).
> + *
> + * @note to be released with vlc_keystore_release()
> + *
> + * @param p_obj
> + *
> + * @return a pointer to the keystore object
> + */
> +VLC_API vlc_keystore *
> +vlc_keystore_get(vlc_object_t *p_obj);
> +
> +/**
> + * Release a keystore object.
> + *
> + * @param p_keystore
> + */
> +VLC_API void
> +vlc_keystore_release(vlc_keystore *p_keystore);
> +
> +/**
> + * Store a password associated with user, protocol, server and port keys
> + *
> + * @note This function is an helper for vlc_keystore_dict_store_passwd() when
> + * the dict contains "user", "protocol", "server", "port" keys.
> + *
> + * @param p_keystore
> + * @param psz_user user
> + * @param psz_protocol protocol (http, https, ftp, ftps, smb...)
> + * @param psz_server (www.example.com)
> + * @param psz_port if NULL, use default protocol port
> + * @param psz_passwd password
> + *
> + * @return 0 on success and -1 on error
> + */
> +VLC_API int
> +vlc_keystore_store(vlc_keystore *p_keystore,
> +                   const char *psz_user, const char *psz_protocol,
> +                   const char *psz_server, const char *psz_port,
> +                   const char *psz_passwd);
> +
> +/**
> + * Get a password associated with user, protocol, server and port keys
> + *
> + * @note This function is an helper for vlc_keystore_dict_find() when
> + * the dict contains "user", "protocol", "server", "port" keys.
> + *
> + * @param p_keystore
> + * @param psz_user if NULL, return any password that match protocol/server/port
> + * @param psz_protocol protocol (http, https, ftp, ftps, smb...)
> + * @param psz_server (www.example.com)
> + * @param psz_port if NULL, use default protocol port
> + *
> + * @return the password, a '\0' terminated string to be freed by free()
> + */
> +VLC_API char *
> +vlc_keystore_find(vlc_keystore *p_keystore,
> +                 const char *psz_user, const char *psz_protocol,
> +                 const char *psz_server, const char *psz_port) VLC_USED VLC_MALLOC;
> +
> +/**
> + * Store a password associated with a given dictionary
> + *
> + * @note This function is an helper for vlc_keystore_dict_store() when
> + * the dict contains "user", "protocol", "server", "port" keys.
> +
> + * @param p_keystore
> + * @param p_dict dictionary
> + * @param psz_passwd password
> + *
> + * @return 0 on success and -1 on error
> + */
> +VLC_API int
> +vlc_keystore_dict_store(vlc_keystore *p_keystore,
> +                        const vlc_dictionary_t *p_dict, const char *psz_passwd);
> +
> +/**
> + * Search though the keystore for items that match a dictionary
> + *
> + * @param p_keystore
> + * @param p_dict dictionary
> + * @param b_strict, if true, all elements of the dictionary must match exactly
> + * @param pp_entries to be released with vlc_keystore_dict_release_entries()
> + *
> + * @return the number of entries found, or -1 in case of error
> + */
> +VLC_API int
> +vlc_keystore_dict_find(vlc_keystore *p_keystore,
> +                       const vlc_dictionary_t *p_dict, bool b_strict,
> +                       vlc_keystore_entry **pp_entries) VLC_USED;
> +
> +/**
> + * Release the list of dictionaries returned by vlc_keystore_dict_find()
> + */
> +VLC_API void
> +vlc_keystore_dict_release_entries(vlc_keystore_entry *p_entries,
> +                                  unsigned int i_count);

What happens if the caller doesn't provide the same size than it was
given ? I think we should finish the table with NULL and handle the
size internally.

Can the user keep reference of a vlc_keystore_entry ? Or is there a
way to make a proper copy ?

> +/**
> + * Remove entries that match a dictionary
> + *
> + * @param p_keystore
> + * @param p_dict dictionary, if NULL, all entries are removed
> + *
> + * @return number of entries removed
> + */
> +VLC_API int
> +vlc_keystore_dict_remove(vlc_keystore *p_keystore,
> +                         const vlc_dictionary_t *p_dict);
> +
> +/**
> + * @}
> + * @defgroup keystore_implementation to be implemented by keystore modules
> + * @{
> + */
> +
> +/* TODO: vlc_keystore_entry helpers to create/release/find */
> +
> +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 */
> +
> +    /**
> +     * Store a password associated with a given dictionary
> +     * TODO: more implementation details
> +     */
> +    int                 (*pf_store)(vlc_keystore *p_keystore,
> +                                    const vlc_dictionary_t *p_dict,
> +                                    const char *psz_passwd);

Not sure password is the right term for an OAuth token. Maybe psz_secret ?

> +    /**
> +     * Search though the keystore for items that match a dictionary
> +     * TODO: more implementation details
> +     */
> +    int                 (*pf_find)(vlc_keystore *p_keystore,
> +                                  const vlc_dictionary_t *p_dict, bool b_strict,
> +                                  vlc_keystore_entry **pp_entries);
> +
> +    /**
> +     * Remove entries that match a dictionary
> +     * TODO: more implementation details
> +     */
> +    int                 (*pf_remove)(vlc_keystore *p_keystore,
> +                                     const vlc_dictionary_t *p_dict);
> +};
> +
> +/** @} @} */
> +
> +#endif
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 2520d67..739fc95 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -58,6 +58,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 \
> @@ -441,6 +442,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/misc/keystore.c b/src/misc/keystore.c
> new file mode 100644
> index 0000000..a96fa8f
> --- /dev/null
> +++ b/src/misc/keystore.c
> @@ -0,0 +1,162 @@
> +/*****************************************************************************
> + * 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 <vlc_arrays.h>
> +#include <libvlc.h>
> +
> +#include <assert.h>
> +
> +vlc_keystore *
> +vlc_keystore_get(vlc_object_t *p_obj)
> +{
> +    vlc_keystore *p_keystore = vlc_custom_create(p_obj, 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)
> +{
> +    module_unneed(p_keystore, p_keystore->p_module);
> +
> +    vlc_object_release(p_keystore);
> +}
> +
> +static int
> +dictionnary_fill(vlc_dictionary_t *p_dict,
> +                 const char *psz_user, const char *psz_protocol,
> +                 const char *psz_server, const char *psz_port)
> +{
> +    vlc_dictionary_init(p_dict, 4);
> +    if (!p_dict->i_size)
> +        return -1;
> +    if (psz_user)
> +        vlc_dictionary_insert(p_dict, "user", (void *) psz_user);
> +    if (psz_protocol)
> +        vlc_dictionary_insert(p_dict, "protocol", (void *) psz_protocol);
> +    if (psz_server)
> +        vlc_dictionary_insert(p_dict, "server", (void *) psz_server);
> +    if (psz_port)
> +        vlc_dictionary_insert(p_dict, "port", (void *) psz_port);
> +
> +    return 0;
> +}
> +
> +int
> +vlc_keystore_store(vlc_keystore *p_keystore,
> +                   const char *psz_user, const char *psz_protocol,
> +                   const char *psz_server, const char *psz_port,
> +                   const char *psz_passwd)
> +{
> +    vlc_dictionary_t dict;
> +
> +    if (dictionnary_fill(&dict, psz_user, psz_protocol, psz_server, psz_port))
> +        return -1;
> +    int i_ret = p_keystore->pf_store(p_keystore, &dict, psz_passwd);
> +    vlc_dictionary_clear(&dict, NULL, NULL);
> +    return i_ret;
> +}
> +
> +char *
> +vlc_keystore_find(vlc_keystore *p_keystore,
> +                 const char *psz_user, const char *psz_protocol,
> +                 const char *psz_server, const char *psz_port)
> +{
> +    vlc_dictionary_t dict;
> +    vlc_keystore_entry *p_entries;
> +    char *psz_passwd = NULL;
> +
> +    if (dictionnary_fill(&dict, psz_user, psz_protocol, psz_server, psz_port))
> +        return NULL;
> +
> +    int i_count = p_keystore->pf_find(p_keystore, &dict, false, &p_entries);
> +    vlc_dictionary_clear(&dict, NULL, NULL);
> +
> +    if (i_count > 0)
> +    {
> +        psz_passwd = strdup(p_entries[0].psz_passwd);
> +        vlc_keystore_dict_release_entries(p_entries, i_count);
> +    }
> +
> +    return psz_passwd;
> +}
> +
> +int
> +vlc_keystore_dict_store(vlc_keystore *p_keystore,
> +                        const vlc_dictionary_t *p_dict, const char *psz_passwd)
> +{
> +    return p_keystore->pf_store(p_keystore, p_dict, psz_passwd);
> +}
> +
> +int
> +vlc_keystore_dict_find(vlc_keystore *p_keystore,
> +                      const vlc_dictionary_t *p_dict, bool b_strict,
> +                      vlc_keystore_entry **pp_entries)
> +{
> +    return p_keystore->pf_find(p_keystore, p_dict, b_strict, pp_entries);
> +}
> +
> +int
> +vlc_keystore_dict_remove(vlc_keystore *p_keystore,
> +                         const vlc_dictionary_t *p_dict)
> +{
> +    return p_keystore->pf_remove(p_keystore, p_dict);
> +}
> +
> +static void
> +dictionnary_free_data(void *p_data, void *p_obj)
> +{
> +    (void) p_obj;
> +    free(p_data);
> +}
> +
> +void
> +vlc_keystore_dict_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];
> +        vlc_dictionary_clear(&p_entry->dict, dictionnary_free_data, NULL);
> +        free(p_entry->psz_passwd);
> +    }
> +    free (p_entries);
> +}
> --
> 2.1.4
>
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list