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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Hyper Text Transfer Protocol (HTTP) authentication
30  *
31  */
32 
33 #include <stdio.h>
34 #include <strings.h>
35 #include <errno.h>
36 #include <ipxe/http.h>
37 
38 /**
39  * Identify authentication scheme
40  *
41  * @v http HTTP transaction
42  * @v name Scheme name
43  * @ret auth Authentication scheme, or NULL
44  */
45 static struct http_authentication * http_authentication ( const char *name ) {
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 }
56 
57 /**
58  * Parse HTTP "WWW-Authenticate" header
59  *
60  * @v http HTTP transaction
61  * @v line Remaining header line
62  * @ret rc Return status code
63  */
65  char *line ) {
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 }
101 
102 /** HTTP "WWW-Authenticate" header */
104 http_response_www_authenticate __http_response_header = {
105  .name = "WWW-Authenticate",
107 };
108 
109 /**
110  * Construct HTTP "Authorization" header
111  *
112  * @v http HTTP transaction
113  * @v buf Buffer
114  * @v len Length of buffer
115  * @ret len Length of header value, or negative error
116  */
117 static int http_format_authorization ( struct http_transaction *http,
118  char *buf, size_t len ) {
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 }
140 
141 /** HTTP "Authorization" header */
142 struct http_request_header http_request_authorization __http_request_header = {
143  .name = "Authorization",
144  .format = http_format_authorization,
145 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int(* format)(struct http_transaction *http, char *buf, size_t len)
Construct remaining "Authorization" header line.
Definition: http.h:541
Error codes.
const char * name
Header name (e.g.
Definition: http.h:230
#define DBGC(...)
Definition: compiler.h:505
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:192
An HTTP authentication scheme.
Definition: http.h:518
An HTTP request header.
Definition: http.h:228
struct http_response_auth auth
Authorization descriptor.
Definition: http.h:348
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct http_request request
Request.
Definition: http.h:436
struct http_response response
Response.
Definition: http.h:438
An HTTP transaction.
Definition: http.h:417
Hyper Text Transport Protocol.
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:224
#define EPROTO
Protocol error.
Definition: errno.h:624
struct http_request_header http_request_authorization __http_request_header
HTTP "Authorization" header.
Definition: httpauth.c:142
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:191
const char * name
Header name (e.g.
Definition: http.h:368
static int http_parse_www_authenticate(struct http_transaction *http, char *line)
Parse HTTP "WWW-Authenticate" header.
Definition: httpauth.c:64
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
char * http_token(char **line, char **value)
Get HTTP response token.
Definition: httpcore.c:190
uint32_t len
Length.
Definition: ena.h:14
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:299
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
struct http_response_header http_response_www_authenticate __http_response_header
HTTP "WWW-Authenticate" header.
Definition: httpauth.c:104
int(* parse)(struct http_transaction *http, char *line)
Parse remaining "WWW-Authenticate" header line.
Definition: http.h:527
#define HTTP_AUTHENTICATIONS
HTTP authentication scheme table.
Definition: http.h:546
static int http_format_authorization(struct http_transaction *http, char *buf, size_t len)
Construct HTTP "Authorization" header.
Definition: httpauth.c:117
#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
An HTTP response header.
Definition: http.h:366
String functions.