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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_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  */
53 static 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  */
71 static 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  */
94 static 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 */
114 struct http_authentication http_basic_auth __http_authentication = {
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 */
122 REQUIRING_SYMBOL ( http_basic_auth );
123 REQUIRE_OBJECT ( httpauth );
unsigned int flags
Flags.
Definition: http.h:351
FILE_SECBOOT(PERMITTED)
Error codes.
#define DBGC(...)
Definition: compiler.h:505
struct uri * uri
Request URI.
Definition: http.h:435
An HTTP authentication scheme.
Definition: http.h:519
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Uniform Resource Identifiers.
REQUIRING_SYMBOL(http_basic_auth)
#define EACCES_USERNAME
Definition: httpbasic.c:41
struct http_request request
Request.
Definition: http.h:437
struct http_response response
Response.
Definition: http.h:439
An HTTP transaction.
Definition: http.h:416
Hyper Text Transport Protocol.
const char * username
Username.
Definition: http.h:156
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:223
ring len
Length.
Definition: dwmac.h:231
static int http_basic_authenticate(struct http_transaction *http)
Perform HTTP Basic authentication.
Definition: httpbasic.c:71
struct http_authentication http_basic_auth __http_authentication
HTTP Basic authentication scheme.
Definition: httpbasic.c:114
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:190
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
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
Base64 encoding.
const char * password
Password.
Definition: http.h:158
const char * name
Name (e.g.
Definition: http.h:521
const char * password
Password.
Definition: uri.h:75
const char * user
User name.
Definition: uri.h:73
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:383
Transaction may be retried on failure.
Definition: http.h:361
HTTP request Basic authentication descriptor.
Definition: http.h:154
REQUIRE_OBJECT(httpauth)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
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 base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition: base64.c:52
struct http_request_auth_basic basic
Basic authentication descriptor.
Definition: http.h:194