iPXE
Macros | 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.

Macros

#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. More...
 
static int http_basic_authenticate (struct http_transaction *http)
 Perform HTTP Basic authentication. More...
 
static int http_format_basic_auth (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Authorization" header for Basic authentication. More...
 
 REQUIRING_SYMBOL (http_basic_auth)
 
 REQUIRE_OBJECT (httpauth)
 

Variables

struct http_authentication http_basic_auth __http_authentication
 HTTP Basic authentication scheme. More...
 

Detailed Description

Hyper Text Transfer Protocol (HTTP) Basic authentication.

Definition in file httpbasic.c.

Macro Definition Documentation

◆ EACCES_USERNAME

#define EACCES_USERNAME   __einfo_error ( EINFO_EACCES_USERNAME )

Definition at line 40 of file httpbasic.c.

◆ EINFO_EACCES_USERNAME

#define EINFO_EACCES_USERNAME
Value:
"No username available for Basic authentication" )
#define EINFO_EACCES
Definition: errno.h:299
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 41 of file httpbasic.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ http_parse_basic_auth()

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.

53  {
54 
55  /* Allow HTTP request to be retried if the request had not
56  * already tried authentication.
57  */
58  if ( ! http->request.auth.auth )
60 
61  return 0;
62 }
unsigned int flags
Flags.
Definition: http.h:352
struct http_request request
Request.
Definition: http.h:436
struct http_response response
Response.
Definition: http.h:438
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:224
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:191
Transaction may be retried on failure.
Definition: http.h:362

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

◆ http_basic_authenticate()

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.

70  {
71  struct http_request_auth_basic *req = &http->request.auth.basic;
72 
73  /* Record username and password */
74  if ( ! http->uri->user ) {
75  DBGC ( http, "HTTP %p has no username for Basic "
76  "authentication\n", http );
77  return -EACCES_USERNAME;
78  }
79  req->username = http->uri->user;
80  req->password = ( http->uri->password ? http->uri->password : "" );
81 
82  return 0;
83 }
#define DBGC(...)
Definition: compiler.h:505
struct uri * uri
Request URI.
Definition: http.h:434
#define EACCES_USERNAME
Definition: httpbasic.c:40
struct http_request request
Request.
Definition: http.h:436
const char * username
Username.
Definition: http.h:157
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:224
const char * password
Password.
Definition: http.h:159
const char * password
Password.
Definition: uri.h:60
const char * user
User name.
Definition: uri.h:58
HTTP request Basic authentication descriptor.
Definition: http.h:155
struct http_request_auth_basic basic
Basic authentication descriptor.
Definition: http.h:195

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.

◆ http_format_basic_auth()

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.

94  {
95  struct http_request_auth_basic *req = &http->request.auth.basic;
96  size_t user_pw_len = ( strlen ( req->username ) + 1 /* ":" */ +
97  strlen ( req->password ) );
98  char user_pw[ user_pw_len + 1 /* NUL */ ];
99 
100  /* Sanity checks */
101  assert ( req->username != NULL );
102  assert ( req->password != NULL );
103 
104  /* Construct "user:password" string */
105  snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
106  req->username, req->password );
107 
108  /* Construct response */
109  return base64_encode ( user_pw, user_pw_len, buf, len );
110 }
struct http_request request
Request.
Definition: http.h:436
const char * username
Username.
Definition: http.h:157
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:224
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
uint32_t len
Length.
Definition: ena.h:14
const char * password
Password.
Definition: http.h:159
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
HTTP request Basic authentication descriptor.
Definition: http.h:155
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition: base64.c:51
struct http_request_auth_basic basic
Basic authentication descriptor.
Definition: http.h:195

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

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( http_basic_auth  )

◆ REQUIRE_OBJECT()

REQUIRE_OBJECT ( httpauth  )

Variable Documentation

◆ __http_authentication

struct http_authentication http_basic_auth __http_authentication
Initial value:
= {
.name = "Basic",
.authenticate = http_basic_authenticate,
}
static int http_basic_authenticate(struct http_transaction *http)
Perform HTTP Basic authentication.
Definition: httpbasic.c:70
static int http_format_basic_auth(struct http_transaction *http, char *buf, size_t len)
Construct HTTP "Authorization" header for Basic authentication.
Definition: httpbasic.c:93
static int http_parse_basic_auth(struct http_transaction *http, char *line __unused)
Parse HTTP "WWW-Authenticate" header for Basic authentication.
Definition: httpbasic.c:52

HTTP Basic authentication scheme.

Definition at line 113 of file httpbasic.c.