stream: merge access_t and stream_t
[vlc.git] / include / vlc_keystore.h
blob67765a4e3eb9654d61491e254a69308bc6071ca4
1 /*****************************************************************************
2 * vlc_keystore.h:
3 *****************************************************************************
4 * Copyright (C) 2015-2016 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifndef VLC_KEYSTORE_H
22 # define VLC_KEYSTORE_H
24 #include <vlc_common.h>
26 typedef struct vlc_keystore vlc_keystore;
27 typedef struct vlc_keystore_entry vlc_keystore_entry;
28 typedef struct vlc_credential vlc_credential;
30 /* Called from src/libvlc.c */
31 int
32 libvlc_InternalKeystoreInit(libvlc_int_t *p_libvlc);
34 /* Called from src/libvlc.c */
35 void
36 libvlc_InternalKeystoreClean(libvlc_int_t *p_libvlc);
38 /**
39 * @defgroup keystore Keystore and credential API
40 * @{
41 * @file
42 * This file declares vlc keystore API
43 * @defgroup keystore_public Keystore public API
44 * @{
47 /**
48 * List of keys that can be stored via the keystore API
50 enum vlc_keystore_key {
51 KEY_PROTOCOL,
52 KEY_USER,
53 KEY_SERVER,
54 KEY_PATH,
55 KEY_PORT,
56 KEY_REALM,
57 KEY_AUTHTYPE,
58 KEY_MAX,
60 #define VLC_KEYSTORE_VALUES_INIT(ppsz_values) memset(ppsz_values, 0, sizeof(const char *) * KEY_MAX)
62 /**
63 * Keystore entry returned by vlc_keystore_find()
65 struct vlc_keystore_entry
67 /** Set of key/values. Values can be NULL */
68 char * ppsz_values[KEY_MAX];
69 /** Secret password */
70 uint8_t * p_secret;
71 /** Length of the secret */
72 size_t i_secret_len;
75 /**
76 * Create a keystore object
78 * A keystore object is persistent across runtime. It is saved on local
79 * filesystem via a vlc keystore module (KWallet, SecretService, Apple Keychain
80 * Service ...).
82 * @note to be released with vlc_keystore_release()
84 * @param p_parent the parent object used to create the keystore object
86 * @return a pointer to the keystore object, or NULL in case of error
88 VLC_API vlc_keystore *
89 vlc_keystore_create(vlc_object_t *p_parent);
90 #define vlc_keystore_create(x) vlc_keystore_create(VLC_OBJECT(x))
92 /**
93 * Release a keystore object
95 VLC_API void
96 vlc_keystore_release(vlc_keystore *p_keystore);
99 /**
100 * Store a secret associated with a set of key/values
102 * @param ppsz_values set of key/values, see vlc_keystore_key.
103 * ppsz_values[KEY_PROTOCOL] and ppsz_values[KEY_SERVER] must be valid
104 * strings
105 * @param p_secret binary secret or string password
106 * @param i_secret_len length of p_secret. If it's less than 0, then p_secret
107 * is assumed to be a '\0' terminated string
108 * @param psz_label user friendly label
110 * @return VLC_SUCCESS on success, or VLC_EGENERIC on error
112 VLC_API int
113 vlc_keystore_store(vlc_keystore *p_keystore,
114 const char *const ppsz_values[KEY_MAX],
115 const uint8_t* p_secret, ssize_t i_secret_len,
116 const char *psz_label);
119 * Find all entries that match a set of key/values
121 * @param ppsz_values set of key/values, see vlc_keystore_key, any values can
122 * be NULL
123 * @param pp_entries list of found entries. To be released with
124 * vlc_keystore_release_entries()
126 * @return the number of entries
128 VLC_API unsigned int
129 vlc_keystore_find(vlc_keystore *p_keystore,
130 const char *const ppsz_values[KEY_MAX],
131 vlc_keystore_entry **pp_entries) VLC_USED;
134 * Remove all entries that match a set of key/values
136 * @note only entries added by VLC can be removed
138 * @param ppsz_values set of key/values, see vlc_keystore_key, any values can
139 * be NULL
141 * @return the number of entries
143 VLC_API unsigned int
144 vlc_keystore_remove(vlc_keystore *p_keystore,
145 const char *const ppsz_values[KEY_MAX]);
148 * Release the list of entries returned by vlc_keystore_find()
150 VLC_API void
151 vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned int i_count);
154 * @}
155 * @defgroup credential Credential API
156 * @{
160 * @note init with vlc_credential_init()
162 struct vlc_credential
164 /** url to store or to search */
165 const vlc_url_t *p_url;
166 /** http realm or smb domain */
167 const char *psz_realm;
168 /** http authtype */
169 const char *psz_authtype;
170 /** valid only if vlc_credential_get() returned true */
171 const char *psz_username;
172 /** valid only if vlc_credential_get() returned true */
173 const char *psz_password;
175 /* internal */
176 enum {
177 GET_FROM_URL,
178 GET_FROM_OPTION,
179 GET_FROM_MEMORY_KEYSTORE,
180 GET_FROM_KEYSTORE,
181 GET_FROM_DIALOG,
182 } i_get_order;
184 vlc_keystore *p_keystore;
185 vlc_keystore_entry *p_entries;
186 unsigned int i_entries_count;
188 char *psz_split_username;
189 char *psz_var_username;
190 char *psz_var_password;
192 char *psz_dialog_username;
193 char *psz_dialog_password;
194 bool b_from_keystore;
195 bool b_store;
199 * Init a credential struct
201 * @note to be cleaned with vlc_credential_clean()
203 * @param psz_url url to store or to search
205 VLC_API void
206 vlc_credential_init(vlc_credential *p_credential, const vlc_url_t *p_url);
209 * Clean a credential struct
211 VLC_API void
212 vlc_credential_clean(vlc_credential *p_credential);
215 * Get a username/password couple
217 * This will search for a credential using url, VLC options, the vlc_keystore
218 * or by asking the user via dialog_Login(). This function can be called
219 * indefinitely, it will first return the user/password from the url (if any),
220 * then from VLC options (if any), then from the keystore (if any), and finally
221 * from the dialog (if any). This function will return true as long as the user
222 * fill the dialog texts and will return false when the user cancel it.
224 * @param p_parent the parent object (for var, keystore and dialog)
225 * @param psz_option_username VLC option name for the username
226 * @param psz_option_password VLC option name for the password
227 * @param psz_dialog_title dialog title, if NULL, this function won't use the
228 * keystore or the dialog
229 * @param psz_dialog_fmt dialog text using format
231 * @return true if vlc_credential.psz_username and vlc_credential.psz_password
232 * are valid, otherwise this function should not be called again.
235 VLC_API bool
236 vlc_credential_get(vlc_credential *p_credential, vlc_object_t *p_parent,
237 const char *psz_option_username,
238 const char *psz_option_password,
239 const char *psz_dialog_title,
240 const char *psz_dialog_fmt, ...) VLC_FORMAT(6, 7);
241 #define vlc_credential_get(a, b, c, d, e, f, ...) \
242 vlc_credential_get(a, VLC_OBJECT(b), c, d, e, f, ##__VA_ARGS__)
245 * Store the last dialog credential returned by vlc_credential_get()
247 * This function will store the credential in the memory keystore if it's
248 * valid, or will store in the permanent one if it comes from the dialog and if
249 * the user asked for it.
251 * @return true if the credential was stored or comes from the keystore, false
252 * otherwise
254 VLC_API bool
255 vlc_credential_store(vlc_credential *p_credential, vlc_object_t *p_parent);
256 #define vlc_credential_store(a, b) \
257 vlc_credential_store(a, VLC_OBJECT(b))
260 * @}
261 * @defgroup keystore_implementation Implemented by keystore modules
262 * @{
265 #define VLC_KEYSTORE_NAME "libVLC"
267 static inline int
268 vlc_keystore_entry_set_secret(vlc_keystore_entry *p_entry,
269 const uint8_t *p_secret, size_t i_secret_len)
271 p_entry->p_secret = (uint8_t*) malloc(i_secret_len);
272 if (!p_entry->p_secret)
273 return VLC_EGENERIC;
274 memcpy(p_entry->p_secret, p_secret, i_secret_len);
275 p_entry->i_secret_len = i_secret_len;
276 return VLC_SUCCESS;
279 static inline void
280 vlc_keystore_release_entry(vlc_keystore_entry *p_entry)
282 for (unsigned int j = 0; j < KEY_MAX; ++j)
284 free(p_entry->ppsz_values[j]);
285 p_entry->ppsz_values[j] = NULL;
287 free(p_entry->p_secret);
288 p_entry->p_secret = NULL;
291 typedef struct vlc_keystore_sys vlc_keystore_sys;
292 struct vlc_keystore
294 VLC_COMMON_MEMBERS
295 module_t *p_module;
296 vlc_keystore_sys *p_sys;
298 /** See vlc_keystore_store() */
299 int (*pf_store)(vlc_keystore *p_keystore,
300 const char *const ppsz_values[KEY_MAX],
301 const uint8_t *p_secret,
302 size_t i_secret_len, const char *psz_label);
303 /** See vlc_keystore_find() */
304 unsigned int (*pf_find)(vlc_keystore *p_keystore,
305 const char *const ppsz_values[KEY_MAX],
306 vlc_keystore_entry **pp_entries);
308 /** See vlc_keystore_remove() */
309 unsigned int (*pf_remove)(vlc_keystore *p_keystore,
310 const char *const ppsz_values[KEY_MAX]);
313 /** @} @} */
315 #endif