iPXE
httpbasic.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) Basic authentication
31 *
32 */
33
34#include <stdio.h>
35#include <errno.h>
36#include <ipxe/uri.h>
37#include <ipxe/base64.h>
38#include <ipxe/http.h>
39
40/* Disambiguate the various error causes */
41#define EACCES_USERNAME __einfo_error ( EINFO_EACCES_USERNAME )
42#define EINFO_EACCES_USERNAME \
43 __einfo_uniqify ( EINFO_EACCES, 0x01, \
44 "No username available for Basic authentication" )
45
46/**
47 * Parse HTTP "WWW-Authenticate" header for Basic authentication
48 *
49 * @v http HTTP transaction
50 * @v line Remaining header line
51 * @ret rc Return status code
52 */
53static int http_parse_basic_auth ( struct http_transaction *http,
54 char *line __unused ) {
55
56 /* Allow HTTP request to be retried if the request had not
57 * already tried authentication.
58 */
59 if ( ! http->request.auth.auth )
61
62 return 0;
63}
64
65/**
66 * Perform HTTP Basic authentication
67 *
68 * @v http HTTP transaction
69 * @ret rc Return status code
70 */
71static int http_basic_authenticate ( struct http_transaction *http ) {
72 struct http_request_auth_basic *req = &http->request.auth.basic;
73
74 /* Record username and password */
75 if ( ! http->uri->user ) {
76 DBGC ( http, "HTTP %p has no username for Basic "
77 "authentication\n", http );
78 return -EACCES_USERNAME;
79 }
80 req->username = http->uri->user;
81 req->password = ( http->uri->password ? http->uri->password : "" );
82
83 return 0;
84}
85
86/**
87 * Construct HTTP "Authorization" header for Basic authentication
88 *
89 * @v http HTTP transaction
90 * @v buf Buffer
91 * @v len Length of buffer
92 * @ret len Length of header value, or negative error
93 */
94static int http_format_basic_auth ( struct http_transaction *http,
95 char *buf, size_t len ) {
96 struct http_request_auth_basic *req = &http->request.auth.basic;
97 size_t user_pw_len = ( strlen ( req->username ) + 1 /* ":" */ +
98 strlen ( req->password ) );
99 char user_pw[ user_pw_len + 1 /* NUL */ ];
100
101 /* Sanity checks */
102 assert ( req->username != NULL );
103 assert ( req->password != NULL );
104
105 /* Construct "user:password" string */
106 snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
107 req->username, req->password );
108
109 /* Construct response */
110 return base64_encode ( user_pw, user_pw_len, buf, len );
111}
112
113/** HTTP Basic authentication scheme */
115 .name = "Basic",
116 .parse = http_parse_basic_auth,
117 .authenticate = http_basic_authenticate,
118 .format = http_format_basic_auth,
119};
120
121/* Drag in HTTP authentication support */
122REQUIRING_SYMBOL ( http_basic_auth );
123REQUIRE_OBJECT ( httpauth );
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition base64.c:52
Base64 encoding.
ring len
Length.
Definition dwmac.h:226
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define REQUIRE_OBJECT(object)
Require an object.
Definition compiler.h:202
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define REQUIRING_SYMBOL(symbol)
Specify the file's requiring symbol.
Definition compiler.h:140
Hyper Text Transport Protocol.
@ HTTP_RESPONSE_RETRY
Transaction may be retried on failure.
Definition http.h:361
#define __http_authentication
Declare an HTTP authentication scheme.
Definition http.h:551
static int http_basic_authenticate(struct http_transaction *http)
Perform HTTP Basic authentication.
Definition httpbasic.c:71
#define EACCES_USERNAME
Definition httpbasic.c:41
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:94
static int http_parse_basic_auth(struct http_transaction *http, char *line __unused)
Parse HTTP "WWW-Authenticate" header for Basic authentication.
Definition httpbasic.c:53
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
An HTTP authentication scheme.
Definition http.h:519
HTTP request Basic authentication descriptor.
Definition http.h:154
const char * username
Username.
Definition http.h:156
const char * password
Password.
Definition http.h:158
struct http_authentication * auth
Authentication scheme (if any)
Definition http.h:190
struct http_request_auth_basic basic
Basic authentication descriptor.
Definition http.h:194
struct http_request_auth auth
Authentication descriptor.
Definition http.h:223
unsigned int flags
Flags.
Definition http.h:351
An HTTP transaction.
Definition http.h:416
struct http_response response
Response.
Definition http.h:439
struct uri * uri
Request URI.
Definition http.h:435
struct http_request request
Request.
Definition http.h:437
const char * user
User name.
Definition uri.h:73
const char * password
Password.
Definition uri.h:75
Uniform Resource Identifiers.
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383