access: dvdnav: add comment
[vlc.git] / src / misc / keystore.c
blob02537f00a8ece91aaa3df2b75c35ce5243e00a43
1 /*****************************************************************************
2 * keystore.c:
3 *****************************************************************************
4 * Copyright (C) 2015-2016 VLC authors, VideoLAN and VideoLabs
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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <vlc_common.h>
26 #include <vlc_dialog.h>
27 #include <vlc_keystore.h>
28 #include <vlc_modules.h>
29 #include <vlc_url.h>
30 #include <libvlc.h>
32 #include <assert.h>
33 #include <limits.h>
35 static vlc_keystore *
36 keystore_create(vlc_object_t *p_parent, const char *psz_name)
38 vlc_keystore *p_keystore = vlc_custom_create(p_parent, sizeof (*p_keystore),
39 "keystore");
40 if (unlikely(p_keystore == NULL))
41 return NULL;
43 p_keystore->p_module = module_need(p_keystore, "keystore", psz_name, true);
44 if (p_keystore->p_module == NULL)
46 vlc_object_release(p_keystore);
47 return NULL;
49 assert(p_keystore->pf_store);
50 assert(p_keystore->pf_find);
51 assert(p_keystore->pf_remove);
53 return p_keystore;
56 #undef vlc_keystore_create
57 vlc_keystore *
58 vlc_keystore_create(vlc_object_t *p_parent)
60 assert(p_parent);
61 return keystore_create(p_parent, "$keystore");
64 void
65 vlc_keystore_release(vlc_keystore *p_keystore)
67 assert(p_keystore);
68 module_unneed(p_keystore, p_keystore->p_module);
70 vlc_object_release(p_keystore);
73 int
74 vlc_keystore_store(vlc_keystore *p_keystore,
75 const char * const ppsz_values[KEY_MAX],
76 const uint8_t *p_secret, ssize_t i_secret_len,
77 const char *psz_label)
79 assert(p_keystore && ppsz_values && p_secret && i_secret_len);
81 if (!ppsz_values[KEY_PROTOCOL] || !ppsz_values[KEY_SERVER])
83 msg_Err(p_keystore, "invalid store request: "
84 "protocol and server should be valid");
85 return VLC_EGENERIC;
87 if (ppsz_values[KEY_PORT])
89 long int i_port = strtol(ppsz_values[KEY_PORT], NULL, 10);
90 if (i_port == LONG_MIN || i_port == LONG_MAX)
92 msg_Err(p_keystore, "invalid store request: "
93 "port is not valid number");
94 return VLC_EGENERIC;
97 if (i_secret_len < 0)
98 i_secret_len = strlen((const char *)p_secret) + 1;
99 return p_keystore->pf_store(p_keystore, ppsz_values, p_secret, i_secret_len,
100 psz_label);
103 unsigned int
104 vlc_keystore_find(vlc_keystore *p_keystore,
105 const char * const ppsz_values[KEY_MAX],
106 vlc_keystore_entry **pp_entries)
108 assert(p_keystore && ppsz_values && pp_entries);
109 return p_keystore->pf_find(p_keystore, ppsz_values, pp_entries);
112 unsigned int
113 vlc_keystore_remove(vlc_keystore *p_keystore,
114 const char *const ppsz_values[KEY_MAX])
116 assert(p_keystore && ppsz_values);
117 return p_keystore->pf_remove(p_keystore, ppsz_values);
120 void
121 vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned int i_count)
123 for (unsigned int i = 0; i < i_count; ++i)
124 vlc_keystore_release_entry(&p_entries[i]);
125 free(p_entries);
129 libvlc_InternalKeystoreInit(libvlc_int_t *p_libvlc)
131 assert(p_libvlc != NULL);
132 libvlc_priv_t *p_priv = libvlc_priv(p_libvlc);
134 p_priv->p_memory_keystore = keystore_create(VLC_OBJECT(p_libvlc), "memory");
135 return p_priv->p_memory_keystore != NULL ? VLC_SUCCESS : VLC_EGENERIC;
138 void
139 libvlc_InternalKeystoreClean(libvlc_int_t *p_libvlc)
141 assert(p_libvlc != NULL);
142 libvlc_priv_t *p_priv = libvlc_priv(p_libvlc);
144 if (p_priv->p_memory_keystore != NULL)
146 vlc_keystore_release(p_priv->p_memory_keystore);
147 p_priv->p_memory_keystore = NULL;
151 static vlc_keystore *
152 get_memory_keystore(vlc_object_t *p_obj)
154 return libvlc_priv(p_obj->obj.libvlc)->p_memory_keystore;
157 static vlc_keystore_entry *
158 find_closest_path(vlc_keystore_entry *p_entries, unsigned i_count,
159 const char *psz_path)
161 vlc_keystore_entry *p_match_entry = NULL;
162 size_t i_last_pathlen = 0;
163 char *psz_decoded_path = vlc_uri_decode_duplicate(psz_path);
164 if (psz_decoded_path == NULL)
165 return NULL;
167 /* Try to find the entry that has the closest path to psz_url */
168 for (unsigned int i = 0; i < i_count; ++i)
170 vlc_keystore_entry *p_entry = &p_entries[i];
171 const char *psz_entry_path = p_entry->ppsz_values[KEY_PATH];
172 if (psz_entry_path == NULL)
174 if (p_match_entry == NULL)
175 p_match_entry = p_entry;
176 continue;
178 size_t i_entry_pathlen = strlen(psz_entry_path);
180 if (strncasecmp(psz_decoded_path, psz_entry_path, i_entry_pathlen) == 0
181 && i_entry_pathlen > i_last_pathlen)
183 i_last_pathlen = i_entry_pathlen;
184 p_match_entry = p_entry;
187 free(psz_decoded_path);
188 return p_match_entry;
191 static bool
192 is_credential_valid(vlc_credential *p_credential)
194 if (p_credential->psz_username && *p_credential->psz_username != '\0'
195 && p_credential->psz_password)
196 return true;
197 else
199 p_credential->psz_password = NULL;
200 return false;
205 static bool
206 is_url_valid(const vlc_url_t *p_url)
208 return p_url && p_url->psz_protocol && p_url->psz_protocol[0]
209 && p_url->psz_host && p_url->psz_host[0];
212 /* Default port for each protocol */
213 static struct
215 const char * psz_protocol;
216 uint16_t i_port;
217 } protocol_default_ports [] = {
218 { "rtsp", 80 },
219 { "http", 80 },
220 { "https", 443 },
221 { "ftp", 21 },
222 { "sftp", 22 },
223 { "smb", 445 },
226 /* Don't store a port if it's the default one */
227 static bool
228 protocol_set_port(const vlc_url_t *p_url, char *psz_port)
230 int i_port = -1;
232 if (p_url->i_port != 0 && p_url->i_port <= UINT16_MAX)
233 i_port = p_url->i_port;
234 else
236 for (unsigned int i = 0; i < sizeof(protocol_default_ports)
237 / sizeof(*protocol_default_ports); ++i)
239 if (strcasecmp(p_url->psz_protocol,
240 protocol_default_ports[i].psz_protocol) == 0)
242 i_port = protocol_default_ports[i].i_port;
243 break;
247 if (i_port != -1)
249 sprintf(psz_port, "%u", (uint16_t) i_port);
250 return true;
252 return false;
255 static bool
256 protocol_is_smb(const vlc_url_t *p_url)
258 return strcasecmp(p_url->psz_protocol, "smb") == 0;
261 static bool
262 protocol_store_path(const vlc_url_t *p_url)
264 return p_url->psz_path
265 && (strncasecmp(p_url->psz_protocol, "http", 4) == 0
266 || strcasecmp(p_url->psz_protocol, "rtsp") == 0
267 || protocol_is_smb(p_url));
270 /* Split domain;user in userinfo */
271 static void
272 smb_split_domain(vlc_credential *p_credential)
274 char *psz_delim = strchr(p_credential->psz_username, ';');
275 if (psz_delim)
277 size_t i_len = psz_delim - p_credential->psz_username;
278 if (i_len > 0)
280 free(p_credential->psz_split_domain);
281 p_credential->psz_split_domain =
282 strndup(p_credential->psz_username, i_len);
283 p_credential->psz_realm = p_credential->psz_split_domain;
285 p_credential->psz_username = psz_delim + 1;
289 static void
290 credential_find_keystore(vlc_credential *p_credential, vlc_keystore *p_keystore)
292 const vlc_url_t *p_url = p_credential->p_url;
294 const char *ppsz_values[KEY_MAX] = { 0 };
295 ppsz_values[KEY_PROTOCOL] = p_url->psz_protocol;
296 ppsz_values[KEY_USER] = p_credential->psz_username;
297 ppsz_values[KEY_SERVER] = p_url->psz_host;
298 /* don't try to match with the path */
299 ppsz_values[KEY_REALM] = p_credential->psz_realm;
300 ppsz_values[KEY_AUTHTYPE] = p_credential->psz_authtype;
301 char psz_port[21];
302 if (protocol_set_port(p_url, psz_port))
303 ppsz_values[KEY_PORT] = psz_port;
305 vlc_keystore_entry *p_entries;
306 unsigned int i_entries_count;
307 i_entries_count = vlc_keystore_find(p_keystore, ppsz_values, &p_entries);
309 /* Remove last entries after vlc_keystore_find call since
310 * p_credential->psz_username (default username) can be a pointer to an
311 * entry */
312 if (p_credential->i_entries_count > 0)
314 vlc_keystore_release_entries(p_credential->p_entries,
315 p_credential->i_entries_count);
316 p_credential->psz_username = NULL;
318 p_credential->p_entries = p_entries;
319 p_credential->i_entries_count = i_entries_count;
321 if (p_credential->i_entries_count > 0)
323 vlc_keystore_entry *p_entry;
325 if (protocol_store_path(p_url))
326 p_entry = find_closest_path(p_credential->p_entries,
327 p_credential->i_entries_count,
328 p_url->psz_path);
329 else
330 p_entry = &p_credential->p_entries[0];
332 if (!p_entry || p_entry->p_secret[p_entry->i_secret_len - 1] != '\0')
334 vlc_keystore_release_entries(p_credential->p_entries,
335 p_credential->i_entries_count);
336 p_credential->i_entries_count = 0;
338 else
340 p_credential->psz_password = (const char *)p_entry->p_secret;
341 p_credential->psz_username = p_entry->ppsz_values[KEY_USER];
342 p_credential->psz_realm = p_entry->ppsz_values[KEY_REALM];
343 p_credential->psz_authtype = p_entry->ppsz_values[KEY_AUTHTYPE];
344 p_credential->b_from_keystore = true;
349 void
350 vlc_credential_init(vlc_credential *p_credential, const vlc_url_t *p_url)
352 assert(p_credential);
354 memset(p_credential, 0, sizeof(*p_credential));
355 p_credential->i_get_order = GET_FROM_URL;
356 p_credential->p_url = p_url;
359 void
360 vlc_credential_clean(vlc_credential *p_credential)
362 if (p_credential->i_entries_count > 0)
363 vlc_keystore_release_entries(p_credential->p_entries,
364 p_credential->i_entries_count);
365 if (p_credential->p_keystore)
366 vlc_keystore_release(p_credential->p_keystore);
368 free(p_credential->psz_split_domain);
369 free(p_credential->psz_var_username);
370 free(p_credential->psz_var_password);
371 free(p_credential->psz_dialog_username);
372 free(p_credential->psz_dialog_password);
375 #undef vlc_credential_get
376 bool
377 vlc_credential_get(vlc_credential *p_credential, vlc_object_t *p_parent,
378 const char *psz_option_username,
379 const char *psz_option_password,
380 const char *psz_dialog_title,
381 const char *psz_dialog_fmt, ...)
383 assert(p_credential && p_parent);
384 const vlc_url_t *p_url = p_credential->p_url;
386 if (!is_url_valid(p_url))
388 msg_Err(p_parent, "vlc_credential_get: invalid url");
389 return false;
392 p_credential->b_from_keystore = false;
393 /* Don't set username to NULL, we may want to use the last one set */
394 p_credential->psz_password = NULL;
396 while (!is_credential_valid(p_credential))
398 /* First, fetch credential from URL (if any).
399 * Secondly, fetch credential from VLC Options (if any).
400 * Thirdly, fetch credential from keystore (if any) using user and realm
401 * previously set by the caller, the URL or by VLC Options.
402 * Finally, fetch credential from the dialog (if any). This last will be
403 * repeated until user cancel the dialog. */
405 switch (p_credential->i_get_order)
407 case GET_FROM_URL:
408 p_credential->psz_username = p_url->psz_username;
409 p_credential->psz_password = p_url->psz_password;
411 if (p_credential->psz_password)
412 msg_Warn(p_parent, "Password in a URI is DEPRECATED");
414 if (p_url->psz_username && protocol_is_smb(p_url))
415 smb_split_domain(p_credential);
416 p_credential->i_get_order++;
417 break;
419 case GET_FROM_OPTION:
420 free(p_credential->psz_var_username);
421 free(p_credential->psz_var_password);
422 p_credential->psz_var_username =
423 p_credential->psz_var_password = NULL;
425 if (psz_option_username)
426 p_credential->psz_var_username =
427 var_InheritString(p_parent, psz_option_username);
428 if (psz_option_password)
429 p_credential->psz_var_password =
430 var_InheritString(p_parent, psz_option_password);
432 if (p_credential->psz_var_username)
433 p_credential->psz_username = p_credential->psz_var_username;
434 if (p_credential->psz_var_password)
435 p_credential->psz_password = p_credential->psz_var_password;
437 p_credential->i_get_order++;
438 break;
440 case GET_FROM_MEMORY_KEYSTORE:
442 if (!psz_dialog_title || !psz_dialog_fmt)
443 return false;
445 vlc_keystore *p_keystore = get_memory_keystore(p_parent);
446 if (p_keystore != NULL)
447 credential_find_keystore(p_credential, p_keystore);
448 p_credential->i_get_order++;
449 break;
452 case GET_FROM_KEYSTORE:
453 if (!psz_dialog_title || !psz_dialog_fmt)
454 return false;
456 if (p_credential->p_keystore == NULL)
457 p_credential->p_keystore = vlc_keystore_create(p_parent);
458 if (p_credential->p_keystore != NULL)
459 credential_find_keystore(p_credential, p_credential->p_keystore);
461 p_credential->i_get_order++;
462 break;
464 default:
465 case GET_FROM_DIALOG:
466 if (!psz_dialog_title || !psz_dialog_fmt)
467 return false;
468 char *psz_dialog_username = NULL;
469 char *psz_dialog_password = NULL;
470 va_list ap;
471 va_start(ap, psz_dialog_fmt);
472 bool *p_store = p_credential->p_keystore != NULL ?
473 &p_credential->b_store : NULL;
474 int i_ret =
475 vlc_dialog_wait_login_va(p_parent,
476 &psz_dialog_username,
477 &psz_dialog_password, p_store,
478 p_credential->psz_username,
479 psz_dialog_title, psz_dialog_fmt, ap);
480 va_end(ap);
482 /* Free previous dialog strings after vlc_dialog_wait_login_va call
483 * since p_credential->psz_username (default username) can be a
484 * pointer to p_credential->psz_dialog_username */
485 free(p_credential->psz_dialog_username);
486 free(p_credential->psz_dialog_password);
487 p_credential->psz_dialog_username = psz_dialog_username;
488 p_credential->psz_dialog_password = psz_dialog_password;
490 if (i_ret != 1)
492 p_credential->psz_username = p_credential->psz_password = NULL;
493 return false;
496 p_credential->psz_username = p_credential->psz_dialog_username;
497 p_credential->psz_password = p_credential->psz_dialog_password;
499 if (protocol_is_smb(p_url))
500 smb_split_domain(p_credential);
502 break;
505 return is_credential_valid(p_credential);
508 #undef vlc_credential_store
509 bool
510 vlc_credential_store(vlc_credential *p_credential, vlc_object_t *p_parent)
512 if (!is_credential_valid(p_credential))
513 return false;
514 /* Don't need to store again */
515 if (p_credential->b_from_keystore)
516 return p_credential->b_from_keystore;
518 vlc_keystore *p_keystore;
519 if (p_credential->b_store)
521 /* Store in permanent keystore */
522 assert(p_credential->p_keystore != NULL);
523 p_keystore = p_credential->p_keystore;
525 else
527 /* Store in memory keystore */
528 p_keystore = get_memory_keystore(p_parent);
530 if (p_keystore == NULL)
531 return false;
533 const vlc_url_t *p_url = p_credential->p_url;
535 char *psz_path = NULL;
536 if (protocol_store_path(p_url)
537 && (psz_path = vlc_uri_decode_duplicate(p_url->psz_path)) != NULL)
539 char *p_slash;
540 if (protocol_is_smb(p_url))
542 /* Remove all characters after the first slash (store the share but
543 * not the path) */
544 p_slash = strchr(psz_path + 1, '/');
546 else
548 /* Remove all characters after the last slash (store the path
549 * without the filename) */
550 p_slash = strrchr(psz_path + 1, '/');
552 if (p_slash && psz_path != p_slash)
553 *p_slash = '\0';
556 const char *ppsz_values[KEY_MAX] = { 0 };
557 ppsz_values[KEY_PROTOCOL] = p_url->psz_protocol;
558 ppsz_values[KEY_USER] = p_credential->psz_username;
559 ppsz_values[KEY_SERVER] = p_url->psz_host;
560 ppsz_values[KEY_PATH] = psz_path;
561 ppsz_values[KEY_REALM] = p_credential->psz_realm;
562 ppsz_values[KEY_AUTHTYPE] = p_credential->psz_authtype;
564 char psz_port[21];
565 if (protocol_set_port(p_url, psz_port))
566 ppsz_values[KEY_PORT] = psz_port;
568 char *psz_label;
569 if (asprintf(&psz_label, "LibVLC password for %s://%s%s",
570 p_url->psz_protocol, p_url->psz_host,
571 psz_path ? psz_path : "") == -1)
573 free(psz_path);
574 return false;
577 const uint8_t *p_password = (const uint8_t *)
578 (p_credential->psz_password != NULL ? p_credential->psz_password : "");
580 bool b_ret = vlc_keystore_store(p_keystore, ppsz_values, p_password,
581 -1, psz_label) == VLC_SUCCESS;
582 free(psz_label);
583 free(psz_path);
584 return b_ret;