您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

httpbasic.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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) Basic authentication
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <ipxe/uri.h>
  33. #include <ipxe/base64.h>
  34. #include <ipxe/http.h>
  35. /* Disambiguate the various error causes */
  36. #define EACCES_USERNAME __einfo_error ( EINFO_EACCES_USERNAME )
  37. #define EINFO_EACCES_USERNAME \
  38. __einfo_uniqify ( EINFO_EACCES, 0x01, \
  39. "No username available for Basic authentication" )
  40. /**
  41. * Parse HTTP "WWW-Authenticate" header for Basic authentication
  42. *
  43. * @v http HTTP transaction
  44. * @v line Remaining header line
  45. * @ret rc Return status code
  46. */
  47. static int http_parse_basic_auth ( struct http_transaction *http,
  48. char *line __unused ) {
  49. /* Allow HTTP request to be retried if the request had not
  50. * already tried authentication.
  51. */
  52. if ( ! http->request.auth.auth )
  53. http->response.flags |= HTTP_RESPONSE_RETRY;
  54. return 0;
  55. }
  56. /**
  57. * Perform HTTP Basic authentication
  58. *
  59. * @v http HTTP transaction
  60. * @ret rc Return status code
  61. */
  62. static int http_basic_authenticate ( struct http_transaction *http ) {
  63. struct http_request_auth_basic *req = &http->request.auth.basic;
  64. /* Record username and password */
  65. if ( ! http->uri->user ) {
  66. DBGC ( http, "HTTP %p has no username for Basic "
  67. "authentication\n", http );
  68. return -EACCES_USERNAME;
  69. }
  70. req->username = http->uri->user;
  71. req->password = ( http->uri->password ? http->uri->password : "" );
  72. return 0;
  73. }
  74. /**
  75. * Construct HTTP "Authorization" header for Basic authentication
  76. *
  77. * @v http HTTP transaction
  78. * @v buf Buffer
  79. * @v len Length of buffer
  80. * @ret len Length of header value, or negative error
  81. */
  82. static int http_format_basic_auth ( struct http_transaction *http,
  83. char *buf, size_t len ) {
  84. struct http_request_auth_basic *req = &http->request.auth.basic;
  85. size_t user_pw_len = ( strlen ( req->username ) + 1 /* ":" */ +
  86. strlen ( req->password ) );
  87. char user_pw[ user_pw_len + 1 /* NUL */ ];
  88. /* Sanity checks */
  89. assert ( req->username != NULL );
  90. assert ( req->password != NULL );
  91. /* Construct "user:password" string */
  92. snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
  93. req->username, req->password );
  94. /* Construct response */
  95. return base64_encode ( user_pw, user_pw_len, buf, len );
  96. }
  97. /** HTTP Basic authentication scheme */
  98. struct http_authentication http_basic_auth __http_authentication = {
  99. .name = "Basic",
  100. .parse = http_parse_basic_auth,
  101. .authenticate = http_basic_authenticate,
  102. .format = http_format_basic_auth,
  103. };
  104. /* Drag in HTTP authentication support */
  105. REQUIRING_SYMBOL ( http_basic_auth );
  106. REQUIRE_OBJECT ( httpauth );