+
+/*****************************************************************************
+ * HTTP authentication
+ *****************************************************************************/
+static char *AuthGetParam( const char *psz_header, const char *psz_param )
+{
+ char psz_what[strlen(psz_param)+3];
+ sprintf( psz_what, "%s=\"", psz_param );
+ psz_header = strstr( psz_header, psz_what );
+ if( psz_header )
+ {
+ const char *psz_end;
+ psz_header += strlen( psz_what );
+ psz_end = strchr( psz_header, '"' );
+ if( !psz_end ) /* Invalid since we should have a closing quote */
+ return strdup( psz_header );
+ return strndup( psz_header, psz_end - psz_header );
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
+{
+ char psz_what[strlen(psz_param)+2];
+ sprintf( psz_what, "%s=", psz_param );
+ psz_header = strstr( psz_header, psz_what );
+ if( psz_header )
+ {
+ const char *psz_end;
+ psz_header += strlen( psz_what );
+ psz_end = strchr( psz_header, ',' );
+ /* XXX: Do we need to filter out trailing space between the value and
+ * the comma/end of line? */
+ if( !psz_end ) /* Can be valid if this is the last parameter */
+ return strdup( psz_header );
+ return strndup( psz_header, psz_end - psz_header );
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+static void AuthParseHeader( access_t *p_access, const char *psz_header,
+ http_auth_t *p_auth )
+{
+ /* FIXME: multiple auth methods can be listed (comma seperated) */
+
+ /* 2 Basic Authentication Scheme */
+ if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
+ {
+ msg_Dbg( p_access, "Using Basic Authentication" );
+ psz_header += strlen( "Basic " );
+ p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
+ if( !p_auth->psz_realm )
+ msg_Warn( p_access, "Basic Authentication: "
+ "Mandatory 'realm' parameter is missing" );
+ }
+ /* 3 Digest Access Authentication Scheme */
+ else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
+ {
+ msg_Dbg( p_access, "Using Digest Access Authentication" );
+ if( p_auth->psz_nonce ) return; /* FIXME */
+ psz_header += strlen( "Digest " );
+ p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
+ p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
+ p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
+ p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
+ p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
+ p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
+ p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
+ p_auth->i_nonce = 0;
+ /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
+ "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
+ p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
+ p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
+ p_auth->psz_qop); */
+ if( !p_auth->psz_realm )
+ msg_Warn( p_access, "Digest Access Authentication: "
+ "Mandatory 'realm' parameter is missing" );
+ if( !p_auth->psz_nonce )
+ msg_Warn( p_access, "Digest Access Authentication: "
+ "Mandatory 'nonce' parameter is missing" );
+ if( p_auth->psz_qop ) /* FIXME: parse the qop list */
+ {
+ char *psz_tmp = strchr( p_auth->psz_qop, ',' );
+ if( psz_tmp ) *psz_tmp = '\0';
+ }
+ }
+ else
+ {
+ const char *psz_end = strchr( psz_header, ' ' );
+ if( psz_end )
+ msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
+ (int)(psz_end - psz_header), psz_header );
+ else
+ msg_Warn( p_access, "Unknown authentication scheme: '%s'",
+ psz_header );
+ }
+}
+
+static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
+ http_auth_t *p_auth, const char *psz_method )
+{
+ (void)p_access;
+ const char *psz_username = p_url->psz_username ? p_url->psz_username : "";
+ const char *psz_password = p_url->psz_password ? p_url->psz_password : "";
+
+ char *psz_HA1 = NULL;
+ char *psz_HA2 = NULL;
+ char *psz_response = NULL;
+ struct md5_s md5;
+
+ /* H(A1) */
+ if( p_auth->psz_HA1 )
+ {
+ psz_HA1 = strdup( p_auth->psz_HA1 );
+ if( !psz_HA1 ) goto error;
+ }
+ else
+ {
+ InitMD5( &md5 );
+ AddMD5( &md5, psz_username, strlen( psz_username ) );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, psz_password, strlen( psz_password ) );
+ EndMD5( &md5 );
+
+ psz_HA1 = psz_md5_hash( &md5 );
+ if( !psz_HA1 ) goto error;
+
+ if( p_auth->psz_algorithm
+ && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
+ {
+ InitMD5( &md5 );
+ AddMD5( &md5, psz_HA1, 32 );
+ free( psz_HA1 );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
+ EndMD5( &md5 );
+
+ psz_HA1 = psz_md5_hash( &md5 );
+ if( !psz_HA1 ) goto error;
+ p_auth->psz_HA1 = strdup( psz_HA1 );
+ if( !p_auth->psz_HA1 ) goto error;
+ }
+ }
+
+ /* H(A2) */
+ InitMD5( &md5 );
+ if( *psz_method )
+ AddMD5( &md5, psz_method, strlen( psz_method ) );
+ AddMD5( &md5, ":", 1 );
+ if( p_url->psz_path )
+ AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
+ else
+ AddMD5( &md5, "/", 1 );
+ if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
+ {
+ char *psz_ent;
+ struct md5_s ent;
+ InitMD5( &ent );
+ AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
+ EndMD5( &ent );
+ psz_ent = psz_md5_hash( &ent );
+ if( !psz_ent ) goto error;
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, psz_ent, 32 );
+ free( psz_ent );
+ }
+ EndMD5( &md5 );
+ psz_HA2 = psz_md5_hash( &md5 );
+ if( !psz_HA2 ) goto error;
+
+ /* Request digest */
+ InitMD5( &md5 );
+ AddMD5( &md5, psz_HA1, 32 );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
+ AddMD5( &md5, ":", 1 );
+ if( p_auth->psz_qop
+ && ( !strcmp( p_auth->psz_qop, "auth" )
+ || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
+ {
+ char psz_inonce[9];
+ snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
+ AddMD5( &md5, psz_inonce, 8 );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
+ AddMD5( &md5, ":", 1 );
+ AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
+ AddMD5( &md5, ":", 1 );
+ }
+ AddMD5( &md5, psz_HA2, 32 );
+ EndMD5( &md5 );
+ psz_response = psz_md5_hash( &md5 );
+
+ error:
+ free( psz_HA1 );
+ free( psz_HA2 );
+ return psz_response;
+}
+
+
+static void AuthReply( access_t *p_access, const char *psz_prefix,
+ vlc_url_t *p_url, http_auth_t *p_auth )
+{
+ access_sys_t *p_sys = p_access->p_sys;
+ char *psz_value;
+
+ psz_value =
+ http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
+ "GET", p_url->psz_path,
+ p_url->psz_username,
+ p_url->psz_password );
+ if ( psz_value == NULL )
+ return;
+
+ net_Printf( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs,
+ "%sAuthorization: %s\r\n", psz_prefix, psz_value );
+ free( psz_value );
+}
+
+static int AuthCheckReply( access_t *p_access, const char *psz_header,
+ vlc_url_t *p_url, http_auth_t *p_auth )
+{
+ return
+ http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
+ psz_header, "",
+ p_url->psz_path,
+ p_url->psz_username,
+ p_url->psz_password );
+}