iPXE
Functions | Variables
httpauth.c File Reference

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

#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <ipxe/http.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static struct http_authenticationhttp_authentication (const char *name)
 Identify authentication scheme.
static int http_parse_www_authenticate (struct http_transaction *http, char *line)
 Parse HTTP "WWW-Authenticate" header.
static int http_format_authorization (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Authorization" header.

Variables

struct http_response_header
http_response_www_authenticate 
__http_response_header
 HTTP "WWW-Authenticate" header.
struct http_request_header
http_request_authorization 
__http_request_header
 HTTP "Authorization" header.

Detailed Description

Hyper Text Transfer Protocol (HTTP) authentication.

Definition in file httpauth.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static struct http_authentication* http_authentication ( const char *  name) [static, read]

Identify authentication scheme.

Parameters:
httpHTTP transaction
nameScheme name
Return values:
authAuthentication scheme, or NULL

Definition at line 45 of file httpauth.c.

References for_each_table_entry, HTTP_AUTHENTICATIONS, http_authentication::name, NULL, and strcasecmp().

Referenced by http_parse_www_authenticate().

                                                                             {
        struct http_authentication *auth;

        /* Identify authentication scheme */
        for_each_table_entry ( auth, HTTP_AUTHENTICATIONS ) {
                if ( strcasecmp ( name, auth->name ) == 0 )
                        return auth;
        }

        return NULL;
}
static int http_parse_www_authenticate ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "WWW-Authenticate" header.

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

Definition at line 64 of file httpauth.c.

References http_response_auth::auth, http_response::auth, DBGC, EPROTO, http_authentication(), http_token(), name, NULL, http_authentication::parse, rc, http_transaction::response, and strerror().

                                                      {
        struct http_authentication *auth;
        char *name;
        int rc;

        /* Get scheme name */
        name = http_token ( &line, NULL );
        if ( ! name ) {
                DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
                       http, line );
                return -EPROTO;
        }

        /* Identify scheme */
        auth = http_authentication ( name );
        if ( ! auth ) {
                DBGC ( http, "HTTP %p unrecognised authentication scheme "
                       "\"%s\"\n", http, name );
                /* Ignore; the server may offer other schemes */
                return 0;
        }

        /* Use first supported scheme */
        if ( http->response.auth.auth )
                return 0;
        http->response.auth.auth = auth;

        /* Parse remaining header line */
        if ( ( rc = auth->parse ( http, line ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not parse %s WWW-Authenticate "
                       "\"%s\": %s\n", http, name, line, strerror ( rc ) );
                return rc;
        }

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

Construct HTTP "Authorization" header.

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

Definition at line 117 of file httpauth.c.

References http_request_auth::auth, http_request::auth, http_authentication::format, http_authentication::name, rc, http_transaction::request, and snprintf().

                                                               {
        struct http_authentication *auth = http->request.auth.auth;
        size_t used;
        int auth_len;
        int rc;

        /* Do nothing unless we have an authentication scheme */
        if ( ! auth )
                return 0;

        /* Construct header */
        used = snprintf ( buf, len, "%s ", auth->name );
        auth_len = auth->format ( http, ( buf + used ),
                                  ( ( used < len ) ? ( len - used ) : 0 ) );
        if ( auth_len < 0 ) {
                rc = auth_len;
                return rc;
        }
        used += auth_len;

        return used;
}

Variable Documentation

struct http_response_header http_response_www_authenticate __http_response_header
Initial value:
 {
        .name = "WWW-Authenticate",
        .parse = http_parse_www_authenticate,
}

HTTP "WWW-Authenticate" header.

Definition at line 104 of file httpauth.c.

struct http_request_header http_request_authorization __http_request_header
Initial value:
 {
        .name = "Authorization",
        .format = http_format_authorization,
}

HTTP "Authorization" header.

Definition at line 142 of file httpauth.c.