1 /*****************************************************************************
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 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_dialog.h>
27 #include <vlc_keystore.h>
28 #include <vlc_modules.h>
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
),
40 if (unlikely(p_keystore
== 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
);
49 assert(p_keystore
->pf_store
);
50 assert(p_keystore
->pf_find
);
51 assert(p_keystore
->pf_remove
);
56 #undef vlc_keystore_create
58 vlc_keystore_create(vlc_object_t
*p_parent
)
61 return keystore_create(p_parent
, "$keystore");
65 vlc_keystore_release(vlc_keystore
*p_keystore
)
68 module_unneed(p_keystore
, p_keystore
->p_module
);
70 vlc_object_release(p_keystore
);
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");
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");
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
,
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
);
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
);
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
]);
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
;
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
)
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
;
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
;
192 is_credential_valid(vlc_credential
*p_credential
)
194 if (p_credential
->psz_username
&& *p_credential
->psz_username
!= '\0'
195 && p_credential
->psz_password
)
199 p_credential
->psz_password
= NULL
;
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 */
215 const char * psz_protocol
;
217 } protocol_default_ports
[] = {
226 /* Don't store a port if it's the default one */
228 protocol_set_port(const vlc_url_t
*p_url
, char *psz_port
)
232 if (p_url
->i_port
!= 0 && p_url
->i_port
<= UINT16_MAX
)
233 i_port
= p_url
->i_port
;
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
;
249 sprintf(psz_port
, "%u", (uint16_t) i_port
);
256 protocol_is_smb(const vlc_url_t
*p_url
)
258 return strcasecmp(p_url
->psz_protocol
, "smb") == 0;
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 */
272 smb_split_domain(vlc_credential
*p_credential
)
274 char *psz_delim
= strchr(p_credential
->psz_username
, ';');
277 size_t i_len
= psz_delim
- p_credential
->psz_username
;
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;
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
;
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
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
,
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;
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;
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
;
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
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");
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
)
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
++;
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
++;
440 case GET_FROM_MEMORY_KEYSTORE
:
442 if (!psz_dialog_title
|| !psz_dialog_fmt
)
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
++;
452 case GET_FROM_KEYSTORE
:
453 if (!psz_dialog_title
|| !psz_dialog_fmt
)
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
++;
465 case GET_FROM_DIALOG
:
466 if (!psz_dialog_title
|| !psz_dialog_fmt
)
468 char *psz_dialog_username
= NULL
;
469 char *psz_dialog_password
= NULL
;
471 va_start(ap
, psz_dialog_fmt
);
472 bool *p_store
= p_credential
->p_keystore
!= NULL
?
473 &p_credential
->b_store
: NULL
;
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
);
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
;
492 p_credential
->psz_username
= p_credential
->psz_password
= NULL
;
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
);
505 return is_credential_valid(p_credential
);
508 #undef vlc_credential_store
510 vlc_credential_store(vlc_credential
*p_credential
, vlc_object_t
*p_parent
)
512 if (!is_credential_valid(p_credential
))
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
;
527 /* Store in memory keystore */
528 p_keystore
= get_memory_keystore(p_parent
);
530 if (p_keystore
== NULL
)
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
)
540 if (protocol_is_smb(p_url
))
542 /* Remove all characters after the first slash (store the share but
544 p_slash
= strchr(psz_path
+ 1, '/');
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
)
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
;
565 if (protocol_set_port(p_url
, psz_port
))
566 ppsz_values
[KEY_PORT
] = psz_port
;
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)
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
;