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.

httpbasic.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * Perform HTTP Basic authentication
  42. *
  43. * @v http HTTP transaction
  44. * @ret rc Return status code
  45. */
  46. static int http_basic_authenticate ( struct http_transaction *http ) {
  47. struct http_request_auth *req = &http->request.auth;
  48. /* Record username and password */
  49. if ( ! http->uri->user ) {
  50. DBGC ( http, "HTTP %p has no username for Basic "
  51. "authentication\n", http );
  52. return -EACCES_USERNAME;
  53. }
  54. req->username = http->uri->user;
  55. req->password = ( http->uri->password ? http->uri->password : "" );
  56. return 0;
  57. }
  58. /**
  59. * Construct HTTP "Authorization" header for Basic authentication
  60. *
  61. * @v http HTTP transaction
  62. * @v buf Buffer
  63. * @v len Length of buffer
  64. * @ret len Length of header value, or negative error
  65. */
  66. static int http_format_basic_auth ( struct http_transaction *http,
  67. char *buf, size_t len ) {
  68. struct http_request_auth *req = &http->request.auth;
  69. size_t user_pw_len = ( strlen ( req->username ) + 1 /* ":" */ +
  70. strlen ( req->password ) );
  71. char user_pw[ user_pw_len + 1 /* NUL */ ];
  72. /* Sanity checks */
  73. assert ( req->username != NULL );
  74. assert ( req->password != NULL );
  75. /* Construct "user:password" string */
  76. snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
  77. req->username, req->password );
  78. /* Construct response */
  79. return base64_encode ( user_pw, user_pw_len, buf, len );
  80. }
  81. /** HTTP Basic authentication scheme */
  82. struct http_authentication http_basic_auth __http_authentication = {
  83. .name = "Basic",
  84. .authenticate = http_basic_authenticate,
  85. .format = http_format_basic_auth,
  86. };
  87. /* Drag in HTTP authentication support */
  88. REQUIRING_SYMBOL ( http_basic_auth );
  89. REQUIRE_OBJECT ( httpauth );