iPXE
httpauth.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/**
28 * @file
29 *
30 * Hyper Text Transfer Protocol (HTTP) authentication
31 *
32 */
33
34#include <stdio.h>
35#include <strings.h>
36#include <errno.h>
37#include <ipxe/http.h>
38
39/**
40 * Identify authentication scheme
41 *
42 * @v http HTTP transaction
43 * @v name Scheme name
44 * @ret auth Authentication scheme, or NULL
45 */
46static struct http_authentication * http_authentication ( const char *name ) {
47 struct http_authentication *auth;
48
49 /* Identify authentication scheme */
51 if ( strcasecmp ( name, auth->name ) == 0 )
52 return auth;
53 }
54
55 return NULL;
56}
57
58/**
59 * Parse HTTP "WWW-Authenticate" header
60 *
61 * @v http HTTP transaction
62 * @v line Remaining header line
63 * @ret rc Return status code
64 */
66 char *line ) {
67 struct http_authentication *auth;
68 char *name;
69 int rc;
70
71 /* Get scheme name */
72 name = http_token ( &line, NULL );
73 if ( ! name ) {
74 DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
75 http, line );
76 return -EPROTO;
77 }
78
79 /* Identify scheme */
80 auth = http_authentication ( name );
81 if ( ! auth ) {
82 DBGC ( http, "HTTP %p unrecognised authentication scheme "
83 "\"%s\"\n", http, name );
84 /* Ignore; the server may offer other schemes */
85 return 0;
86 }
87
88 /* Use first supported scheme */
89 if ( http->response.auth.auth )
90 return 0;
91 http->response.auth.auth = auth;
92
93 /* Parse remaining header line */
94 if ( ( rc = auth->parse ( http, line ) ) != 0 ) {
95 DBGC ( http, "HTTP %p could not parse %s WWW-Authenticate "
96 "\"%s\": %s\n", http, name, line, strerror ( rc ) );
97 return rc;
98 }
99
100 return 0;
101}
102
103/** HTTP "WWW-Authenticate" header */
105http_response_www_authenticate __http_response_header = {
106 .name = "WWW-Authenticate",
108};
109
110/**
111 * Construct HTTP "Authorization" header
112 *
113 * @v http HTTP transaction
114 * @v buf Buffer
115 * @v len Length of buffer
116 * @ret len Length of header value, or negative error
117 */
119 char *buf, size_t len ) {
120 struct http_authentication *auth = http->request.auth.auth;
121 size_t used;
122 int auth_len;
123 int rc;
124
125 /* Do nothing unless we have an authentication scheme */
126 if ( ! auth )
127 return 0;
128
129 /* Construct header */
130 used = snprintf ( buf, len, "%s ", auth->name );
131 auth_len = auth->format ( http, ( buf + used ),
132 ( ( used < len ) ? ( len - used ) : 0 ) );
133 if ( auth_len < 0 ) {
134 rc = auth_len;
135 return rc;
136 }
137 used += auth_len;
138
139 return used;
140}
141
142/** HTTP "Authorization" header */
143struct http_request_header http_request_authorization __http_request_header = {
144 .name = "Authorization",
146};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
const char * name
Definition ath9k_hw.c:1986
ring len
Length.
Definition dwmac.h:226
Error codes.
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EPROTO
Protocol error.
Definition errno.h:625
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Hyper Text Transport Protocol.
#define __http_response_header
Declare an HTTP response header.
Definition http.h:382
#define HTTP_AUTHENTICATIONS
HTTP authentication scheme table.
Definition http.h:547
#define __http_request_header
Declare an HTTP request header.
Definition http.h:246
static struct http_authentication * http_authentication(const char *name)
Identify authentication scheme.
Definition httpauth.c:46
static int http_parse_www_authenticate(struct http_transaction *http, char *line)
Parse HTTP "WWW-Authenticate" header.
Definition httpauth.c:65
static int http_format_authorization(struct http_transaction *http, char *buf, size_t len)
Construct HTTP "Authorization" header.
Definition httpauth.c:118
char * http_token(char **line, char **value)
Get HTTP response token.
Definition httpcore.c:196
String functions.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition string.c:209
An HTTP authentication scheme.
Definition http.h:519
int(* format)(struct http_transaction *http, char *buf, size_t len)
Construct remaining "Authorization" header line.
Definition http.h:542
int(* parse)(struct http_transaction *http, char *line)
Parse remaining "WWW-Authenticate" header line.
Definition http.h:528
const char * name
Name (e.g.
Definition http.h:521
struct http_authentication * auth
Authentication scheme (if any)
Definition http.h:190
An HTTP request header.
Definition http.h:227
struct http_request_auth auth
Authentication descriptor.
Definition http.h:223
struct http_authentication * auth
Authentication scheme (if any)
Definition http.h:298
An HTTP response header.
Definition http.h:365
struct http_response_auth auth
Authorization descriptor.
Definition http.h:347
An HTTP transaction.
Definition http.h:416
struct http_response response
Response.
Definition http.h:439
struct http_request request
Request.
Definition http.h:437
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383