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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  51. * Parse HTTP "WWW-Authenticate" header
  52. *
  53. * @v http HTTP transaction
  54. * @v line Remaining header line
  55. * @ret rc Return status code
  56. */
  57. static int http_parse_www_authenticate ( struct http_transaction *http,
  58. char *line ) {
  59. struct http_authentication *auth;
  60. char *name;
  61. int rc;
  62. /* Get scheme name */
  63. name = http_token ( &line, NULL );
  64. if ( ! name ) {
  65. DBGC ( http, "HTTP %p malformed WWW-Authenticate \"%s\"\n",
  66. http, line );
  67. return -EPROTO;
  68. }
  69. /* Identify scheme */
  70. auth = http_authentication ( name );
  71. if ( ! auth ) {
  72. DBGC ( http, "HTTP %p unrecognised authentication scheme "
  73. "\"%s\"\n", http, name );
  74. /* Ignore; the server may offer other schemes */
  75. return 0;
  76. }
  77. /* Use first supported scheme */
  78. if ( http->response.auth.auth )
  79. return 0;
  80. http->response.auth.auth = auth;
  81. /* Parse remaining header line */
  82. if ( ( rc = auth->parse ( http, line ) ) != 0 ) {
  83. DBGC ( http, "HTTP %p could not parse %s WWW-Authenticate "
  84. "\"%s\": %s\n", http, name, line, strerror ( rc ) );
  85. return rc;
  86. }
  87. return 0;
  88. }
  89. /** HTTP "WWW-Authenticate" header */
  90. struct http_response_header
  91. http_response_www_authenticate __http_response_header = {
  92. .name = "WWW-Authenticate",
  93. .parse = http_parse_www_authenticate,
  94. };
  95. /**
  96. * Construct HTTP "Authorization" header
  97. *
  98. * @v http HTTP transaction
  99. * @v buf Buffer
  100. * @v len Length of buffer
  101. * @ret len Length of header value, or negative error
  102. */
  103. static int http_format_authorization ( struct http_transaction *http,
  104. char *buf, size_t len ) {
  105. struct http_authentication *auth = http->request.auth.auth;
  106. size_t used;
  107. int auth_len;
  108. int rc;
  109. /* Do nothing unless we have an authentication scheme */
  110. if ( ! auth )
  111. return 0;
  112. /* Construct header */
  113. used = snprintf ( buf, len, "%s ", auth->name );
  114. auth_len = auth->format ( http, ( buf + used ),
  115. ( ( used < len ) ? ( len - used ) : 0 ) );
  116. if ( auth_len < 0 ) {
  117. rc = auth_len;
  118. return rc;
  119. }
  120. used += auth_len;
  121. return used;
  122. }
  123. /** HTTP "Authorization" header */
  124. struct http_request_header http_request_authorization __http_request_header = {
  125. .name = "Authorization",
  126. .format = http_format_authorization,
  127. };