[vlc-devel] [PATCH 4/8] raop: Implement options for password

Michael Hanselmann public at hansmi.ch
Sat Jan 10 00:46:49 CET 2009


The password can either be directly passed as an option, or it can
be stored in a file whose path is then passed as an option.
---
 modules/stream_out/raop.c |   87 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/modules/stream_out/raop.c b/modules/stream_out/raop.c
index 5a84d3b..54e3968 100644
--- a/modules/stream_out/raop.c
+++ b/modules/stream_out/raop.c
@@ -29,6 +29,7 @@
 #endif
 
 #include <assert.h>
+#include <fcntl.h>
 
 #include <gcrypt.h>
 
@@ -70,6 +71,7 @@ static const char psz_delim_space[] = " ";
 static const char psz_delim_colon[] = ":";
 static const char psz_delim_equal[] = "=";
 static const char psz_delim_semicolon[] = ";";
+static const char psz_delim_linebreak[] = "\r\n";
 
 
 /*****************************************************************************
@@ -97,6 +99,7 @@ struct sout_stream_sys_t
 {
     /* Input parameters */
     char *psz_host;
+    char *psz_password;
     int i_volume;
 
     /* Plugin status */
@@ -145,6 +148,12 @@ struct sout_stream_id_t
 #define VOLUME_LONGTEXT N_("Output volume for analog output: 0 for silence, " \
                            "1..255 from almost silent to very loud.")
 
+#define PASSWORD_TEXT N_("Password")
+#define PASSWORD_LONGTEXT N_("Password for target device.")
+
+#define PASSWORD_FILE_TEXT N_("Password file")
+#define PASSWORD_FILE_LONGTEXT N_("Read password for target device from file.")
+
 vlc_module_begin();
     set_shortname( N_("RAOP") )
     set_description( N_("Remote Audio Output Protocol streaming plugin") );
@@ -154,6 +163,10 @@ vlc_module_begin();
     set_subcategory( SUBCAT_SOUT_STREAM );
     add_string( SOUT_CFG_PREFIX "host", "", NULL,
                 HOST_TEXT, HOST_LONGTEXT, false );
+    add_password( SOUT_CFG_PREFIX "password", NULL, NULL,
+                  PASSWORD_TEXT, PASSWORD_LONGTEXT, false );
+    add_file( SOUT_CFG_PREFIX "password-file", NULL, NULL,
+              PASSWORD_FILE_TEXT, PASSWORD_FILE_LONGTEXT, false );
     add_integer_with_range( SOUT_CFG_PREFIX "volume", 100, 0, 255, NULL,
                             VOLUME_TEXT, VOLUME_LONGTEXT, false );
     set_callbacks( Open, Close );
@@ -161,6 +174,8 @@ vlc_module_end();
 
 static const char *const ppsz_sout_options[] = {
     "host",
+    "password",
+    "password-file",
     "volume",
     NULL
 };
@@ -185,6 +200,7 @@ static void FreeSys( vlc_object_t *p_this, sout_stream_sys_t *p_sys )
 
     free( p_sys->p_sendbuf );
     free( p_sys->psz_host );
+    free( p_sys->psz_password );
     free( p_sys->psz_url );
     free( p_sys->psz_session );
     free( p_sys->psz_client_instance );
@@ -541,6 +557,53 @@ error:
     return i_err;
 }
 
+static char *ReadPasswordFile( vlc_object_t *p_this, const char *psz_path )
+{
+    char *psz_password = NULL;
+    char *psz_next;
+    char ps_buffer[256];
+    int fd = -1;
+    ssize_t i_len;
+
+    fd = utf8_open( psz_path, O_RDONLY, 0400 );
+    if ( fd == -1 )
+    {
+        msg_Err( p_this, "Unable to open password file %s: %m", psz_path );
+        goto error;
+    }
+
+    do
+        i_len = read( fd, ps_buffer, sizeof( ps_buffer ) - 1 );
+    while ( i_len == -1 && errno == EINTR );
+
+    if ( i_len == -1 )
+    {
+        msg_Err( p_this, "Unable to read password file %s: %m", psz_path );
+        goto error;
+    }
+    else if ( i_len >= (ssize_t)( sizeof( ps_buffer ) - 2 ) )
+    {
+        msg_Err( p_this, "Password file %s too large", psz_path );
+        goto error;
+    }
+
+    /* Make sure buffer ends with \0 */
+    ps_buffer[__MIN(i_len, (ssize_t)( sizeof(ps_buffer) - 1 ))] = '\0';
+
+    psz_next = ps_buffer;
+
+    /* Replace first newline with '\0' */
+    strsep( &psz_next, psz_delim_linebreak );
+
+    psz_password = strdup( ps_buffer );
+
+error:
+    if ( fd >= 0 )
+        close( fd );
+
+    return psz_password;
+}
+
 /* Splits the value of a received header.
  *
  * Example: "Transport: RTP/AVP/TCP;unicast;mode=record;server_port=6000"
@@ -1246,6 +1309,7 @@ static int Open( vlc_object_t *p_this )
     sout_stream_t *p_stream = (sout_stream_t*)p_this;
     sout_stream_sys_t *p_sys;
     char psz_local[NI_MAXNUMERICHOST];
+    char *psz_pwfile = NULL;
     gcry_error_t i_gcrypt_err;
     int i_err = VLC_SUCCESS;
     uint32_t i_session_id;
@@ -1283,6 +1347,27 @@ static int Open( vlc_object_t *p_this )
         goto error;
     }
 
+    p_sys->psz_password = var_GetNonEmptyString( p_stream,
+                                                 SOUT_CFG_PREFIX "password" );
+    if ( p_sys->psz_password == NULL )
+    {
+        /* Try password file instead */
+        psz_pwfile = var_GetNonEmptyString( p_stream,
+                                            SOUT_CFG_PREFIX "password-file" );
+        if ( psz_pwfile != NULL )
+        {
+            p_sys->psz_password = ReadPasswordFile( p_this, psz_pwfile );
+            if ( p_sys->psz_password == NULL )
+            {
+                i_err = VLC_EGENERIC;
+                goto error;
+            }
+        }
+    }
+
+    if ( p_sys->psz_password != NULL )
+        msg_Info( p_this, "Using password authentication" );
+
     var_AddCallback( p_stream, SOUT_CFG_PREFIX "volume",
                      VolumeCallback, NULL );
     p_sys->b_volume_callback = true;
@@ -1383,6 +1468,8 @@ static int Open( vlc_object_t *p_this )
     }
 
 error:
+    free( psz_pwfile );
+
     if ( i_err != VLC_SUCCESS )
         FreeSys( p_this, p_sys );
 
-- 
1.5.6.3




More information about the vlc-devel mailing list