]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/raop.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / stream_out / raop.c
index 20b2ee8df642c687fd3f1ddf8f96a2c47a9a62e1..c7e5a041f1e15faa42078904357a018fc892f857 100644 (file)
@@ -29,7 +29,6 @@
 #endif
 
 #include <assert.h>
-#include <strings.h>
 
 #include <gcrypt.h>
 
 #include <vlc_network.h>
 #include <vlc_strings.h>
 #include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_gcrypt.h>
 #include <vlc_es.h>
+#include <vlc_http.h>
+#include <vlc_memory.h>
 
 #define RAOP_PORT 5000
 #define RAOP_USER_AGENT "VLC " VERSION
@@ -124,6 +126,8 @@ struct sout_stream_sys_t
     int i_audio_latency;
     int i_jack_type;
 
+    http_auth_t auth;
+
     /* Send buffer */
     size_t i_sendbuf_len;
     uint8_t *p_sendbuf;
@@ -443,7 +447,7 @@ static int EncryptAesKeyBase64( vlc_object_t *p_this, char **result )
     unsigned char ps_padded_key[256];
     unsigned char *ps_value;
     size_t i_value_size;
-    int i_err = VLC_SUCCESS;
+    int i_err;
 
     /* Add RSA-OAES-SHA1 padding */
     i_err = AddOaepPadding( p_this,
@@ -452,25 +456,20 @@ static int EncryptAesKeyBase64( vlc_object_t *p_this, char **result )
                             NULL, 0 );
     if ( i_err != VLC_SUCCESS )
         goto error;
+    i_err = VLC_EGENERIC;
 
     /* Read public key */
     i_gcrypt_err = gcry_mpi_scan( &mpi_pubkey, GCRYMPI_FMT_USG,
                                   ps_raop_rsa_pubkey,
                                   sizeof( ps_raop_rsa_pubkey ) - 1, NULL );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* Read exponent */
     i_gcrypt_err = gcry_mpi_scan( &mpi_exp, GCRYMPI_FMT_USG, ps_raop_rsa_exp,
                                   sizeof( ps_raop_rsa_exp ) - 1, NULL );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* If the input data starts with a set bit (0x80), gcrypt thinks it's a
      * signed integer and complains. Prefixing it with a zero byte (\0)
@@ -481,45 +480,32 @@ static int EncryptAesKeyBase64( vlc_object_t *p_this, char **result )
                                   ps_padded_key, sizeof( ps_padded_key ),
                                   NULL);
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* Build S-expression with RSA parameters */
     i_gcrypt_err = gcry_sexp_build( &sexp_rsa_params, NULL,
                                     "(public-key(rsa(n %m)(e %m)))",
                                     mpi_pubkey, mpi_exp );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* Build S-expression for data */
     i_gcrypt_err = gcry_sexp_build( &sexp_input, NULL, "(data(value %m))",
                                     mpi_input );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* Encrypt data */
     i_gcrypt_err = gcry_pk_encrypt( &sexp_encrypted, sexp_input,
                                     sexp_rsa_params );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
-    {
-        i_err = VLC_EGENERIC;
         goto error;
-    }
 
     /* Extract encrypted data */
     sexp_token_a = gcry_sexp_find_token( sexp_encrypted, "a", 0 );
     if ( !sexp_token_a )
     {
         msg_Err( p_this , "Token 'a' not found in result S-expression" );
-        i_err = VLC_EGENERIC;
         goto error;
     }
 
@@ -527,7 +513,6 @@ static int EncryptAesKeyBase64( vlc_object_t *p_this, char **result )
     if ( !mpi_output )
     {
         msg_Err( p_this, "Unable to extract MPI from result" );
-        i_err = VLC_EGENERIC;
         goto error;
     }
 
@@ -536,12 +521,12 @@ static int EncryptAesKeyBase64( vlc_object_t *p_this, char **result )
                                     mpi_output );
     if ( CheckForGcryptError( p_stream, i_gcrypt_err ) )
     {
-        i_err = VLC_EGENERIC;
         goto error;
     }
 
     /* Encode in Base64 */
     *result = vlc_b64_encode_binary( ps_value, i_value_size );
+    i_err = VLC_SUCCESS;
 
 error:
     gcry_sexp_release( sexp_rsa_params );
@@ -563,7 +548,7 @@ static char *ReadPasswordFile( vlc_object_t *p_this, const char *psz_path )
     char *psz_newline;
     char ps_buffer[256];
 
-    p_file = utf8_fopen( psz_path, "rt" );
+    p_file = vlc_fopen( psz_path, "rt" );
     if ( p_file == NULL )
     {
         msg_Err( p_this, "Unable to open password file '%s': %m", psz_path );
@@ -587,12 +572,12 @@ static char *ReadPasswordFile( vlc_object_t *p_this, const char *psz_path )
 
     } else {
         /* Replace first newline with '\0' */
-        psz_newline = index( ps_buffer, '\n' );
+        psz_newline = strchr( ps_buffer, '\n' );
         if ( psz_newline != NULL )
             *psz_newline = '\0';
     }
 
-    if ( strlen( ps_buffer ) == 0 ) {
+    if ( *ps_buffer == '\0' ) {
         msg_Err( p_this, "No password could be read from '%s'", psz_path );
         goto error;
     }
@@ -643,15 +628,12 @@ static int ReadStatusLine( vlc_object_t *p_this )
     char *psz_line = NULL;
     char *psz_token;
     char *psz_next;
-    int i_result;
+    int i_result = VLC_EGENERIC;
 
     p_sys->psz_last_status_line = net_Gets( p_this, p_sys->i_control_fd,
                                             NULL );
     if ( !p_sys->psz_last_status_line )
-    {
-        i_result = VLC_EGENERIC;
         goto error;
-    }
 
     /* Create working copy */
     psz_line = strdup( p_sys->psz_last_status_line );
@@ -663,7 +645,6 @@ static int ReadStatusLine( vlc_object_t *p_this )
     {
         msg_Err( p_this, "Unknown protocol (%s)",
                  p_sys->psz_last_status_line );
-        i_result = VLC_EGENERIC;
         goto error;
     }
 
@@ -673,7 +654,6 @@ static int ReadStatusLine( vlc_object_t *p_this )
     {
         msg_Err( p_this, "Request failed (%s)",
                  p_sys->psz_last_status_line );
-        i_result = VLC_EGENERIC;
         goto error;
     }
 
@@ -840,6 +820,30 @@ error:
     return i_err;
 }
 
+static int ParseAuthenticateHeader( vlc_object_t *p_this,
+                                    vlc_dictionary_t *p_resp_headers )
+{
+    sout_stream_t *p_stream = (sout_stream_t*)p_this;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+    char *psz_auth;
+    int i_err = VLC_SUCCESS;
+
+    psz_auth = vlc_dictionary_value_for_key( p_resp_headers,
+                                             "WWW-Authenticate" );
+    if ( psz_auth == NULL )
+    {
+        msg_Err( p_this, "HTTP 401 response missing "
+                         "WWW-Authenticate header" );
+        i_err = VLC_EGENERIC;
+        goto error;
+    }
+
+    http_auth_ParseWwwAuthenticateHeader( p_this, &p_sys->auth, psz_auth );
+
+error:
+    return i_err;
+}
+
 static int ExecRequest( vlc_object_t *p_this, const char *psz_method,
                         const char *psz_content_type, const char *psz_body,
                         vlc_dictionary_t *p_req_headers,
@@ -847,9 +851,11 @@ static int ExecRequest( vlc_object_t *p_this, const char *psz_method,
 {
     sout_stream_t *p_stream = (sout_stream_t*)p_this;
     sout_stream_sys_t *p_sys = p_stream->p_sys;
+    char *psz_authorization = NULL;
     int headers_done;
     int i_err = VLC_SUCCESS;
     int i_status;
+    int i_auth_state;
 
     if ( p_sys->i_control_fd < 0 )
     {
@@ -858,8 +864,29 @@ static int ExecRequest( vlc_object_t *p_this, const char *psz_method,
         goto error;
     }
 
+    i_auth_state = 0;
     while ( 1 )
     {
+        /* Send header only when Digest authentication is used */
+        if ( p_sys->psz_password != NULL && p_sys->auth.psz_nonce != NULL )
+        {
+            FREENULL( psz_authorization );
+
+            psz_authorization =
+                http_auth_FormatAuthorizationHeader( p_this, &p_sys->auth,
+                                                     psz_method,
+                                                     p_sys->psz_url, "",
+                                                     p_sys->psz_password );
+            if ( psz_authorization == NULL )
+            {
+                i_err = VLC_EGENERIC;
+                goto error;
+            }
+
+            vlc_dictionary_insert( p_req_headers, "Authorization",
+                                   psz_authorization );
+        }
+
         /* Send request */
         i_err = SendRequest( p_this, psz_method, psz_content_type, psz_body,
                              p_req_headers);
@@ -888,6 +915,22 @@ static int ExecRequest( vlc_object_t *p_this, const char *psz_method,
         if ( i_status == 200 )
             /* Request successful */
             break;
+        else if ( i_status == 401 )
+        {
+            /* Authorization required */
+            if ( i_auth_state == 1 || p_sys->psz_password == NULL )
+            {
+                msg_Err( p_this, "Access denied, password invalid" );
+                i_err = VLC_EGENERIC;
+                goto error;
+            }
+
+            i_err = ParseAuthenticateHeader( p_this, p_resp_headers );
+            if ( i_err != VLC_SUCCESS )
+                goto error;
+
+            i_auth_state = 1;
+        }
         else
         {
             msg_Err( p_this, "Request failed (%s), status is %d",
@@ -899,6 +942,7 @@ static int ExecRequest( vlc_object_t *p_this, const char *psz_method,
 
 error:
     FREENULL( p_sys->psz_last_status_line );
+    free( psz_authorization );
 
     return i_err;
 }
@@ -1257,7 +1301,7 @@ static void SendAudio( sout_stream_t *p_stream, block_t *p_buffer )
             /* Grow in blocks of 4K */
             i_realloc_len = (1 + (i_len / 4096)) * 4096;
 
-            p_sys->p_sendbuf = realloc( p_sys->p_sendbuf, i_realloc_len );
+            p_sys->p_sendbuf = realloc_or_free( p_sys->p_sendbuf, i_realloc_len );
             if ( p_sys->p_sendbuf == NULL )
                 goto error;
 
@@ -1357,6 +1401,8 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_volume = var_GetInteger( p_stream, SOUT_CFG_PREFIX "volume");
     p_sys->i_jack_type = JACK_TYPE_NONE;
 
+    http_auth_Init( &p_sys->auth );
+
     p_sys->psz_host = var_GetNonEmptyString( p_stream,
                                              SOUT_CFG_PREFIX "host" );
     if ( p_sys->psz_host == NULL )