[vlc-devel] [PATCH 1/4] add vlc_keystore API

Thomas Guillem thomas at gllm.fr
Wed Nov 25 19:14:25 CET 2015


---
 include/vlc_keystore.h | 248 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/Makefile.am        |   2 +
 src/libvlc-module.c    |   7 ++
 src/libvlccore.sym     |  11 +++
 src/misc/keystore.c    | 215 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 483 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..1bdbe56
--- /dev/null
+++ b/include/vlc_keystore.h
@@ -0,0 +1,248 @@
+/*****************************************************************************
+ * 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 used by access/demux modules
+ * @{
+ */
+
+/**
+ * Common keys used by vlc modules
+ */
+#define KEY_USER "user"
+#define KEY_PROTOCOL "protocol"
+#define KEY_PORT "port"
+#define KEY_SERVER "server" /* use it for KEY_HOST */
+#define KEY_DOMAIN "domain"
+#define KEY_REALM "realm"
+#define KEY_PATH "path"
+
+/**
+ * Get 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_obj the parent object
+ *
+ * @return a pointer to the keystore object
+ */
+VLC_API vlc_keystore *
+vlc_keystore_get(vlc_object_t *p_obj);
+#define vlc_keystore_get(x) vlc_keystore_get(VLC_OBJECT(x))
+
+/**
+ * Release a keystore object.
+ *
+ * @param p_keystore keystore object
+ */
+VLC_API void
+vlc_keystore_release(vlc_keystore *p_keystore);
+
+/**
+ * Store a secret associated with a list of key and values
+ *
+ * @note This function is an helper for vlc_keystore_dict_store()
+ *
+ * @param p_keystore keystore object
+ * @param psz_secret secret
+ * @param ... list of keys and values (each key or value is a '\0' terminated
+ * string), this must be terminated by NULL
+ *
+ * @return VLC_SUCCESS on success and VLC_EGNERIC on error
+ */
+VLC_API int
+vlc_keystore_store(vlc_keystore *p_keystore, const char *psz_secret, ...);
+
+/**
+ * Find entries that match a list of key and values
+ *
+ * @note This function is an helper for vlc_keystore_dict_find()
+ *
+ * @param p_keystore keystore object
+ * @param pp_entries list of found entries. To be released with
+ * vlc_keystore_dict_release_entries()
+ * @param ... list of keys and values (each key or value is a '\0' terminated
+ * string), this must be terminated by NULL
+ *
+ * @return the number of entries
+ */
+VLC_API unsigned int
+vlc_keystore_find(vlc_keystore *p_keystore,
+                  vlc_keystore_entry **pp_entries, ...) VLC_USED;
+
+/**
+ * Store a secret associated with a given dictionary
+ *
+ * @param p_keystore keystore object
+ * @param p_dict dictionary
+ * @param psz_secret secret
+ *
+ * @return VLC_SUCCESS on success, or VLC_EGNERIC on error
+ */
+VLC_API int
+vlc_keystore_dict_store(vlc_keystore *p_keystore,
+                        const vlc_dictionary_t *p_dict, const char *psz_secret);
+
+/**
+ * Find entries that match a given dictionary
+ *
+ * @param p_keystore keystore object
+ * @param p_dict dictionary
+ * @param pp_entries list of found entries. To be released with
+ * vlc_keystore_release_entries()
+ *
+ * @return the number of entries
+ */
+VLC_API int
+vlc_keystore_dict_find(vlc_keystore *p_keystore,
+                       const vlc_dictionary_t *p_dict,
+                       vlc_keystore_entry **pp_entries) VLC_USED;
+
+/**
+ * Release the list of entries returned by vlc_keystore_dict_find()
+ *
+ * @param p_keystore keystore object
+ * @param p_entries entries
+ * @param i_count number of entries
+ * 
+ */
+VLC_API void
+vlc_keystore_release_entries(vlc_keystore *p_keystore,
+                             vlc_keystore_entry *p_entries, unsigned int i_count);
+
+/**
+ * Remove entries that match a dictionary
+ *
+ * @param p_keystore keystore object
+ * @param p_dict dictionary
+ *
+ * @return number of entries removed
+ */
+VLC_API unsigned int
+vlc_keystore_dict_remove(vlc_keystore *p_keystore,
+                         const vlc_dictionary_t *p_dict);
+
+/**
+ * Get a value pair from an entry
+ *
+ * @param p_entry entry
+ * @param psz_key key
+ *
+ * @return a '\0' terminated string
+ */
+VLC_API const char *
+vlc_keystore_entry_get_value(vlc_keystore_entry *p_entry, const char *psz_key);
+
+/**
+ * Load the secret value of an entry
+ *
+ * @note the returned value is valid until vlc_keystore_release_entries() is
+ * called
+ *
+ * @param p_keystore keystore object
+ * @param p_entry entry
+ *
+ * @return a '\0' terminated string
+ */
+VLC_API const char *
+vlc_keystore_entry_load_secret(vlc_keystore *p_keystore,
+                               vlc_keystore_entry *p_entry);
+
+/**
+ * @}
+ * @defgroup keystore_implementation to be implemented by keystore modules
+ * @{
+ */
+
+typedef struct vlc_keystore_secret vlc_keystore_secret;
+struct vlc_keystore_entry
+{
+
+    /* Fill the dictionary with key values pairs found by the keystore. All
+     * values are allocated char *. Clear this dictionary with
+     * vlc_keystore_clear_dict() */
+    vlc_dictionary_t    dict;
+    /* private secret used pf_secret_load() */
+    vlc_keystore_secret *p_secret;
+};
+
+
+/**
+ * Clear a keystore dictionary
+ *
+ * @param p_dict dictionary
+ */
+VLC_API void
+vlc_keystore_clear_dict(vlc_dictionary_t *p_dict);
+
+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_dict_store(), psz_label is the label of the secret */
+    int                 (*pf_store)(vlc_keystore *p_keystore,
+                                    const vlc_dictionary_t *p_dict,
+                                    const char *psz_secret,
+                                    const char *psz_label);
+
+    /* See vlc_keystore_dict_find() */
+    unsigned int        (*pf_find)(vlc_keystore *p_keystore,
+                                   const vlc_dictionary_t *p_dict,
+                                   vlc_keystore_entry **pp_entries);
+
+    /* See vlc_keystore_dict_remove() */
+    unsigned int        (*pf_remove)(vlc_keystore *p_keystore,
+                                     const vlc_dictionary_t *p_dict);
+
+    /* See vlc_keystore_entry_load_secret() */
+    const char *        (*pf_secret_load)(vlc_keystore *p_keystore,
+                                          vlc_keystore_secret *p_secret);
+
+    void                (*pf_secret_release)(vlc_keystore *p_keystore,
+                                             vlc_keystore_secret *p_secret);
+};
+
+/** @} @} */
+
+#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/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 a9e2636..7bab423 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -554,6 +554,17 @@ vlc_ngettext
 vlc_iconv
 vlc_iconv_close
 vlc_iconv_open
+vlc_keystore_clear_dict
+vlc_keystore_dict_find
+vlc_keystore_dict_remove
+vlc_keystore_dict_store
+vlc_keystore_entry_get_value
+vlc_keystore_entry_load_secret
+vlc_keystore_find
+vlc_keystore_get
+vlc_keystore_release
+vlc_keystore_release_entries
+vlc_keystore_store
 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..eed75bb
--- /dev/null
+++ b/src/misc/keystore.c
@@ -0,0 +1,215 @@
+/*****************************************************************************
+ * 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>
+
+#undef vlc_keystore_get
+vlc_keystore *
+vlc_keystore_get(vlc_object_t *p_obj)
+{
+    assert(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);
+    assert(p_keystore->pf_secret_load);
+
+    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);
+}
+
+static int
+dictionnary_fill(vlc_dictionary_t *p_dict, va_list args)
+{
+    vlc_dictionary_init(p_dict, 10);
+    if (!p_dict->i_size)
+        return VLC_EGENERIC;
+
+    for (;;)
+    {
+        const char *psz_key = (const char *)va_arg(args, const char *);
+        if (!psz_key)
+            break;
+
+        const char *psz_value = (const char *)va_arg(args, const char *);
+        if (!psz_value)
+            continue;
+        vlc_dictionary_insert(p_dict, psz_key, (void *) psz_value);
+    }
+
+    return VLC_SUCCESS;
+}
+
+int
+vlc_keystore_store(vlc_keystore *p_keystore, const char *psz_secret, ...)
+{
+    assert(p_keystore && psz_secret);
+
+    va_list args;
+    va_start(args, psz_secret);
+    vlc_dictionary_t dict;
+
+    if (dictionnary_fill(&dict, args))
+    {
+        va_end(args);
+        return VLC_EGENERIC;
+    }
+    va_end(args);
+
+    int i_ret = vlc_keystore_dict_store(p_keystore, &dict, psz_secret);
+    vlc_dictionary_clear(&dict, NULL, NULL);
+    return i_ret;
+}
+
+unsigned int
+vlc_keystore_find(vlc_keystore *p_keystore,
+                  vlc_keystore_entry **pp_entries, ...)
+{
+    assert(p_keystore && pp_entries);
+    va_list args;
+    va_start(args, pp_entries);
+    vlc_dictionary_t dict;
+
+    if (dictionnary_fill(&dict, args))
+    {
+        va_end(args);
+        return 0;
+    }
+    va_end(args);
+
+    unsigned int i_count = p_keystore->pf_find(p_keystore, &dict, pp_entries);
+    vlc_dictionary_clear(&dict, NULL, NULL);
+
+    return i_count;
+}
+
+int
+vlc_keystore_dict_store(vlc_keystore *p_keystore,
+                        const vlc_dictionary_t *p_dict, const char *psz_secret)
+{
+    assert(p_keystore && p_dict && psz_secret);
+    const char *psz_protocol = vlc_dictionary_value_for_key(p_dict,
+                                                            KEY_PROTOCOL);
+    const char *psz_server = vlc_dictionary_value_for_key(p_dict, KEY_SERVER);
+    if (!psz_server)
+        psz_server = vlc_dictionary_value_for_key(p_dict, KEY_DOMAIN);
+    char *psz_label;
+    if (asprintf(&psz_label, "libVLC password%s%s%s%s",
+                 psz_protocol || psz_server ? " for " : "",
+                 psz_protocol ? psz_protocol : "",
+                 psz_protocol ? "://" : "",
+                 psz_server ? psz_server : "") == -1)
+        return VLC_EGENERIC;
+    int i_ret = p_keystore->pf_store(p_keystore, p_dict, psz_secret, psz_label);
+    free(psz_label);
+    return i_ret;
+}
+
+int
+vlc_keystore_dict_find(vlc_keystore *p_keystore,
+                      const vlc_dictionary_t *p_dict,
+                      vlc_keystore_entry **pp_entries)
+{
+    assert(p_keystore && p_dict && pp_entries);
+    return p_keystore->pf_find(p_keystore, p_dict, pp_entries);
+}
+
+unsigned int
+vlc_keystore_dict_remove(vlc_keystore *p_keystore,
+                         const vlc_dictionary_t *p_dict)
+{
+    assert(p_keystore && 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_clear_dict(vlc_dictionary_t *p_dict)
+{
+    assert(p_dict);
+    vlc_dictionary_clear(p_dict, dictionnary_free_data, NULL);
+}
+
+void
+vlc_keystore_release_entries(vlc_keystore *p_keystore,
+                             vlc_keystore_entry *p_entries, unsigned int i_count)
+{
+    assert(p_keystore);
+    for (unsigned int i = 0; i < i_count; ++i)
+    {
+        vlc_keystore_entry *p_entry = &p_entries[i];
+        vlc_keystore_clear_dict(&p_entry->dict);
+        if (p_entry->p_secret && p_keystore->pf_secret_release)
+            p_keystore->pf_secret_release(p_keystore, p_entry->p_secret);
+    }
+    free (p_entries);
+}
+
+const char *
+vlc_keystore_entry_get_value(vlc_keystore_entry *p_entry, const char *psz_key)
+{
+    assert(p_entry && psz_key);
+    return vlc_dictionary_value_for_key(&p_entry->dict, psz_key);
+}
+
+const char *
+vlc_keystore_entry_load_secret(vlc_keystore *p_keystore,
+                               vlc_keystore_entry *p_entry)
+{
+    assert(p_keystore && p_entry);
+    if (p_entry->p_secret)
+        return p_keystore->pf_secret_load(p_keystore, p_entry->p_secret);
+    return NULL;
+}
-- 
2.1.4



More information about the vlc-devel mailing list