iPXE
Defines | Functions | Variables
httpbasic.c File Reference

Hyper Text Transfer Protocol (HTTP) Basic authentication. More...

#include <stdio.h>
#include <errno.h>
#include <ipxe/uri.h>
#include <ipxe/base64.h>
#include <ipxe/http.h>

Go to the source code of this file.

Defines

#define EACCES_USERNAME   __einfo_error ( EINFO_EACCES_USERNAME )
#define EINFO_EACCES_USERNAME

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int http_parse_basic_auth (struct http_transaction *http, char *line __unused)
 Parse HTTP "WWW-Authenticate" header for Basic authentication.
static int http_basic_authenticate (struct http_transaction *http)
 Perform HTTP Basic authentication.
static int http_format_basic_auth (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Authorization" header for Basic authentication.
 REQUIRING_SYMBOL (http_basic_auth)
 REQUIRE_OBJECT (httpauth)

Variables

struct http_authentication
http_basic_auth 
__http_authentication
 HTTP Basic authentication scheme.

Detailed Description

Hyper Text Transfer Protocol (HTTP) Basic authentication.

Definition in file httpbasic.c.


Define Documentation

Definition at line 40 of file httpbasic.c.

Referenced by http_basic_authenticate().

Value:
__einfo_uniqify ( EINFO_EACCES, 0x01,                           \
                          "No username available for Basic authentication" )

Definition at line 41 of file httpbasic.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int http_parse_basic_auth ( struct http_transaction http,
char *line  __unused 
) [static]

Parse HTTP "WWW-Authenticate" header for Basic authentication.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 52 of file httpbasic.c.

References http_request_auth::auth, http_request::auth, http_response::flags, HTTP_RESPONSE_RETRY, http_transaction::request, and http_transaction::response.

                                                         {

        /* Allow HTTP request to be retried if the request had not
         * already tried authentication.
         */
        if ( ! http->request.auth.auth )
                http->response.flags |= HTTP_RESPONSE_RETRY;

        return 0;
}
static int http_basic_authenticate ( struct http_transaction http) [static]

Perform HTTP Basic authentication.

Parameters:
httpHTTP transaction
Return values:
rcReturn status code

Definition at line 70 of file httpbasic.c.

References http_request::auth, http_request_auth::basic, DBGC, EACCES_USERNAME, uri::password, http_request_auth_basic::password, http_transaction::request, http_transaction::uri, uri::user, and http_request_auth_basic::username.

                                                                     {
        struct http_request_auth_basic *req = &http->request.auth.basic;

        /* Record username and password */
        if ( ! http->uri->user ) {
                DBGC ( http, "HTTP %p has no username for Basic "
                       "authentication\n", http );
                return -EACCES_USERNAME;
        }
        req->username = http->uri->user;
        req->password = ( http->uri->password ? http->uri->password : "" );

        return 0;
}
static int http_format_basic_auth ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Authorization" header for Basic authentication.

Parameters:
httpHTTP transaction
bufBuffer
lenLength of buffer
Return values:
lenLength of header value, or negative error

Definition at line 93 of file httpbasic.c.

References assert, http_request::auth, base64_encode(), http_request_auth::basic, NULL, http_request_auth_basic::password, http_transaction::request, snprintf(), strlen(), and http_request_auth_basic::username.

                                                            {
        struct http_request_auth_basic *req = &http->request.auth.basic;
        size_t user_pw_len = ( strlen ( req->username ) + 1 /* ":" */ +
                               strlen ( req->password ) );
        char user_pw[ user_pw_len + 1 /* NUL */ ];

        /* Sanity checks */
        assert ( req->username != NULL );
        assert ( req->password != NULL );

        /* Construct "user:password" string */
        snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
                   req->username, req->password );

        /* Construct response */
        return base64_encode ( user_pw, user_pw_len, buf, len );
}
REQUIRING_SYMBOL ( http_basic_auth  )
REQUIRE_OBJECT ( httpauth  )

Variable Documentation

Initial value:
 {
        .name = "Basic",
        .parse = http_parse_basic_auth,
        .authenticate = http_basic_authenticate,
        .format = http_format_basic_auth,
}

HTTP Basic authentication scheme.

Definition at line 113 of file httpbasic.c.