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. More...
 
static int http_parse_www_authenticate (struct http_transaction *http, char *line)
 Parse HTTP "WWW-Authenticate" header. More...
 
static int http_format_authorization (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Authorization" header. More...
 

Variables

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

Detailed Description

Hyper Text Transfer Protocol (HTTP) authentication.

Definition in file httpauth.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ http_authentication()

static struct http_authentication* http_authentication ( const char *  name)
static

Identify authentication scheme.

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

Definition at line 45 of file httpauth.c.

45  {
46  struct http_authentication *auth;
47 
48  /* Identify authentication scheme */
50  if ( strcasecmp ( name, auth->name ) == 0 )
51  return auth;
52  }
53 
54  return NULL;
55 }
const char * name
Definition: ath9k_hw.c:1984
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:192
An HTTP authentication scheme.
Definition: http.h:518
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
const char * name
Name (e.g.
Definition: http.h:520
#define HTTP_AUTHENTICATIONS
HTTP authentication scheme table.
Definition: http.h:546
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by http_parse_www_authenticate().

◆ http_parse_www_authenticate()

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.

65  {
66  struct http_authentication *auth;
67  char *name;
68  int rc;
69 
70  /* Get scheme name */
71  name = http_token ( &line, NULL );
72  if ( ! name ) {
73  DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
74  http, line );
75  return -EPROTO;
76  }
77 
78  /* Identify scheme */
79  auth = http_authentication ( name );
80  if ( ! auth ) {
81  DBGC ( http, "HTTP %p unrecognised authentication scheme "
82  "\"%s\"\n", http, name );
83  /* Ignore; the server may offer other schemes */
84  return 0;
85  }
86 
87  /* Use first supported scheme */
88  if ( http->response.auth.auth )
89  return 0;
90  http->response.auth.auth = auth;
91 
92  /* Parse remaining header line */
93  if ( ( rc = auth->parse ( http, line ) ) != 0 ) {
94  DBGC ( http, "HTTP %p could not parse %s WWW-Authenticate "
95  "\"%s\": %s\n", http, name, line, strerror ( rc ) );
96  return rc;
97  }
98 
99  return 0;
100 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
An HTTP authentication scheme.
Definition: http.h:518
struct http_response_auth auth
Authorization descriptor.
Definition: http.h:348
struct http_response response
Response.
Definition: http.h:438
#define EPROTO
Protocol error.
Definition: errno.h:624
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * http_token(char **line, char **value)
Get HTTP response token.
Definition: httpcore.c:190
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:299
int(* parse)(struct http_transaction *http, char *line)
Parse remaining "WWW-Authenticate" header line.
Definition: http.h:527
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static struct http_authentication * http_authentication(const char *name)
Identify authentication scheme.
Definition: httpauth.c:45

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().

◆ http_format_authorization()

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.

118  {
119  struct http_authentication *auth = http->request.auth.auth;
120  size_t used;
121  int auth_len;
122  int rc;
123 
124  /* Do nothing unless we have an authentication scheme */
125  if ( ! auth )
126  return 0;
127 
128  /* Construct header */
129  used = snprintf ( buf, len, "%s ", auth->name );
130  auth_len = auth->format ( http, ( buf + used ),
131  ( ( used < len ) ? ( len - used ) : 0 ) );
132  if ( auth_len < 0 ) {
133  rc = auth_len;
134  return rc;
135  }
136  used += auth_len;
137 
138  return used;
139 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int(* format)(struct http_transaction *http, char *buf, size_t len)
Construct remaining "Authorization" header line.
Definition: http.h:541
An HTTP authentication scheme.
Definition: http.h:518
struct http_request request
Request.
Definition: http.h:436
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:224
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:191
uint32_t len
Length.
Definition: ena.h:14
const char * name
Name (e.g.
Definition: http.h:520
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382

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

Variable Documentation

◆ __http_response_header

struct http_response_header http_response_www_authenticate __http_response_header
Initial value:
= {
.name = "WWW-Authenticate",
}
static int http_parse_www_authenticate(struct http_transaction *http, char *line)
Parse HTTP "WWW-Authenticate" header.
Definition: httpauth.c:64

HTTP "WWW-Authenticate" header.

Definition at line 104 of file httpauth.c.

◆ __http_request_header

struct http_request_header http_request_authorization __http_request_header
Initial value:
= {
.name = "Authorization",
}
static int http_format_authorization(struct http_transaction *http, char *buf, size_t len)
Construct HTTP "Authorization" header.
Definition: httpauth.c:117

HTTP "Authorization" header.

Definition at line 142 of file httpauth.c.