You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httpauth.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * Hyper Text Transfer Protocol (HTTP) authentication
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <strings.h>
  32. #include <errno.h>
  33. #include <ipxe/http.h>
  34. /**
  35. * Identify authentication scheme
  36. *
  37. * @v http HTTP transaction
  38. * @v name Scheme name
  39. * @ret auth Authentication scheme, or NULL
  40. */
  41. static struct http_authentication * http_authentication ( const char *name ) {
  42. struct http_authentication *auth;
  43. /* Identify authentication scheme */
  44. for_each_table_entry ( auth, HTTP_AUTHENTICATIONS ) {
  45. if ( strcasecmp ( name, auth->name ) == 0 )
  46. return auth;
  47. }
  48. return NULL;
  49. }
  50. /** An HTTP "WWW-Authenticate" response field */
  51. struct http_www_authenticate_field {
  52. /** Name */
  53. const char *name;
  54. /** Offset */
  55. size_t offset;
  56. };
  57. /** Define an HTTP "WWW-Authenticate" response field */
  58. #define HTTP_WWW_AUTHENTICATE_FIELD( _name ) { \
  59. .name = #_name, \
  60. .offset = offsetof ( struct http_transaction, \
  61. response.auth._name ), \
  62. }
  63. /**
  64. * Set HTTP "WWW-Authenticate" response field value
  65. *
  66. * @v http HTTP transaction
  67. * @v field Response field
  68. * @v value Field value
  69. */
  70. static inline void
  71. http_www_auth_field ( struct http_transaction *http,
  72. struct http_www_authenticate_field *field, char *value ) {
  73. char **ptr;
  74. ptr = ( ( ( void * ) http ) + field->offset );
  75. *ptr = value;
  76. }
  77. /** HTTP "WWW-Authenticate" fields */
  78. static struct http_www_authenticate_field http_www_auth_fields[] = {
  79. HTTP_WWW_AUTHENTICATE_FIELD ( realm ),
  80. HTTP_WWW_AUTHENTICATE_FIELD ( qop ),
  81. HTTP_WWW_AUTHENTICATE_FIELD ( algorithm ),
  82. HTTP_WWW_AUTHENTICATE_FIELD ( nonce ),
  83. HTTP_WWW_AUTHENTICATE_FIELD ( opaque ),
  84. };
  85. /**
  86. * Parse HTTP "WWW-Authenticate" header
  87. *
  88. * @v http HTTP transaction
  89. * @v line Remaining header line
  90. * @ret rc Return status code
  91. */
  92. static int http_parse_www_authenticate ( struct http_transaction *http,
  93. char *line ) {
  94. struct http_www_authenticate_field *field;
  95. struct http_authentication *auth;
  96. char *name;
  97. char *key;
  98. char *value;
  99. unsigned int i;
  100. /* Get scheme name */
  101. name = http_token ( &line, NULL );
  102. if ( ! name ) {
  103. DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
  104. http, value );
  105. return -EPROTO;
  106. }
  107. /* Identify scheme */
  108. auth = http_authentication ( name );
  109. if ( ! auth ) {
  110. DBGC ( http, "HTTP %p unrecognised authentication scheme "
  111. "\"%s\"\n", http, name );
  112. /* Ignore; the server may offer other schemes */
  113. return 0;
  114. }
  115. /* Use first supported scheme */
  116. if ( http->response.auth.auth )
  117. return 0;
  118. http->response.auth.auth = auth;
  119. /* Process fields */
  120. while ( ( key = http_token ( &line, &value ) ) ) {
  121. for ( i = 0 ; i < ( sizeof ( http_www_auth_fields ) /
  122. sizeof ( http_www_auth_fields[0] ) ) ; i++){
  123. field = &http_www_auth_fields[i];
  124. if ( strcasecmp ( key, field->name ) == 0 )
  125. http_www_auth_field ( http, field, value );
  126. }
  127. }
  128. /* Allow HTTP request to be retried if the request had not
  129. * already tried authentication.
  130. */
  131. if ( ! http->request.auth.auth )
  132. http->response.flags |= HTTP_RESPONSE_RETRY;
  133. return 0;
  134. }
  135. /** HTTP "WWW-Authenticate" header */
  136. struct http_response_header
  137. http_response_www_authenticate __http_response_header = {
  138. .name = "WWW-Authenticate",
  139. .parse = http_parse_www_authenticate,
  140. };
  141. /**
  142. * Construct HTTP "Authorization" header
  143. *
  144. * @v http HTTP transaction
  145. * @v buf Buffer
  146. * @v len Length of buffer
  147. * @ret len Length of header value, or negative error
  148. */
  149. static int http_format_authorization ( struct http_transaction *http,
  150. char *buf, size_t len ) {
  151. struct http_authentication *auth = http->request.auth.auth;
  152. size_t used;
  153. int auth_len;
  154. int rc;
  155. /* Do nothing unless we have an authentication scheme */
  156. if ( ! auth )
  157. return 0;
  158. /* Construct header */
  159. used = snprintf ( buf, len, "%s ", auth->name );
  160. auth_len = auth->format ( http, ( buf + used ),
  161. ( ( used < len ) ? ( len - used ) : 0 ) );
  162. if ( auth_len < 0 ) {
  163. rc = auth_len;
  164. return rc;
  165. }
  166. used += auth_len;
  167. return used;
  168. }
  169. /** HTTP "Authorization" header */
  170. struct http_request_header http_request_authorization __http_request_header = {
  171. .name = "Authorization",
  172. .format = http_format_authorization,
  173. };