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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. char *name;
  96. char *key;
  97. char *value;
  98. unsigned int i;
  99. /* Get scheme name */
  100. name = http_token ( &line, NULL );
  101. if ( ! name ) {
  102. DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
  103. http, value );
  104. return -EPROTO;
  105. }
  106. /* Identify scheme */
  107. http->response.auth.auth = http_authentication ( name );
  108. if ( ! http->response.auth.auth ) {
  109. DBGC ( http, "HTTP %p unrecognised authentication scheme "
  110. "\"%s\"\n", http, name );
  111. return -ENOTSUP;
  112. }
  113. /* Process fields */
  114. while ( ( key = http_token ( &line, &value ) ) ) {
  115. for ( i = 0 ; i < ( sizeof ( http_www_auth_fields ) /
  116. sizeof ( http_www_auth_fields[0] ) ) ; i++){
  117. field = &http_www_auth_fields[i];
  118. if ( strcasecmp ( key, field->name ) == 0 )
  119. http_www_auth_field ( http, field, value );
  120. }
  121. }
  122. /* Allow HTTP request to be retried if the request had not
  123. * already tried authentication.
  124. */
  125. if ( ! http->request.auth.auth )
  126. http->response.flags |= HTTP_RESPONSE_RETRY;
  127. return 0;
  128. }
  129. /** HTTP "WWW-Authenticate" header */
  130. struct http_response_header
  131. http_response_www_authenticate __http_response_header = {
  132. .name = "WWW-Authenticate",
  133. .parse = http_parse_www_authenticate,
  134. };
  135. /**
  136. * Construct HTTP "Authorization" header
  137. *
  138. * @v http HTTP transaction
  139. * @v buf Buffer
  140. * @v len Length of buffer
  141. * @ret len Length of header value, or negative error
  142. */
  143. static int http_format_authorization ( struct http_transaction *http,
  144. char *buf, size_t len ) {
  145. struct http_authentication *auth = http->request.auth.auth;
  146. size_t used;
  147. int auth_len;
  148. int rc;
  149. /* Do nothing unless we have an authentication scheme */
  150. if ( ! auth )
  151. return 0;
  152. /* Construct header */
  153. used = snprintf ( buf, len, "%s ", auth->name );
  154. auth_len = auth->format ( http, ( buf + used ),
  155. ( ( used < len ) ? ( len - used ) : 0 ) );
  156. if ( auth_len < 0 ) {
  157. rc = auth_len;
  158. return rc;
  159. }
  160. used += auth_len;
  161. return used;
  162. }
  163. /** HTTP "Authorization" header */
  164. struct http_request_header http_request_authorization __http_request_header = {
  165. .name = "Authorization",
  166. .format = http_format_authorization,
  167. };