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.

httpntlm.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2017 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) NTLM authentication
  28. *
  29. */
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <ipxe/uri.h>
  33. #include <ipxe/base64.h>
  34. #include <ipxe/ntlm.h>
  35. #include <ipxe/http.h>
  36. struct http_authentication http_ntlm_auth __http_authentication;
  37. /** Workstation name used for NTLM authentication */
  38. static const char http_ntlm_workstation[] = "iPXE";
  39. /**
  40. * Parse HTTP "WWW-Authenticate" header for NTLM authentication
  41. *
  42. * @v http HTTP transaction
  43. * @v line Remaining header line
  44. * @ret rc Return status code
  45. */
  46. static int http_parse_ntlm_auth ( struct http_transaction *http, char *line ) {
  47. struct http_response_auth_ntlm *rsp = &http->response.auth.ntlm;
  48. char *copy;
  49. int len;
  50. int rc;
  51. /* Create temporary copy of Base64-encoded challenge message */
  52. copy = strdup ( line );
  53. if ( ! copy ) {
  54. rc = -ENOMEM;
  55. goto err_alloc;
  56. }
  57. /* Decode challenge message, overwriting the original */
  58. len = base64_decode ( copy, line, strlen ( line ) );
  59. if ( len < 0 ) {
  60. rc = len;
  61. DBGC ( http, "HTTP %p could not decode NTLM challenge "
  62. "\"%s\": %s\n", http, copy, strerror ( rc ) );
  63. goto err_decode;
  64. }
  65. /* Parse challenge, if present */
  66. if ( len ) {
  67. rsp->challenge = ( ( void * ) line );
  68. if ( ( rc = ntlm_challenge ( rsp->challenge, len,
  69. &rsp->info ) ) != 0 ) {
  70. DBGC ( http, "HTTP %p could not parse NTLM challenge: "
  71. "%s\n", http, strerror ( rc ) );
  72. goto err_challenge;
  73. }
  74. }
  75. /* Allow HTTP request to be retried if the request had not
  76. * already tried authentication. Note that NTLM requires an
  77. * additional round trip to obtain the challenge message,
  78. * which is not present in the initial WWW-Authenticate.
  79. */
  80. if ( ( http->request.auth.auth == NULL ) ||
  81. ( ( http->request.auth.auth == &http_ntlm_auth ) &&
  82. ( http->request.auth.ntlm.len == 0 ) && len ) ) {
  83. http->response.flags |= HTTP_RESPONSE_RETRY;
  84. }
  85. /* Success */
  86. rc = 0;
  87. err_challenge:
  88. err_decode:
  89. free ( copy );
  90. err_alloc:
  91. return rc;
  92. }
  93. /**
  94. * Perform HTTP NTLM authentication
  95. *
  96. * @v http HTTP transaction
  97. * @ret rc Return status code
  98. */
  99. static int http_ntlm_authenticate ( struct http_transaction *http ) {
  100. struct http_request_auth_ntlm *req = &http->request.auth.ntlm;
  101. struct http_response_auth_ntlm *rsp = &http->response.auth.ntlm;
  102. struct ntlm_key key;
  103. const char *password;
  104. /* If we have no challenge yet, then just send a Negotiate message */
  105. if ( ! rsp->challenge ) {
  106. DBGC ( http, "HTTP %p sending NTLM Negotiate\n", http );
  107. return 0;
  108. }
  109. /* Record username */
  110. if ( ! http->uri->user ) {
  111. DBGC ( http, "HTTP %p has no username for NTLM "
  112. "authentication\n", http );
  113. return -EACCES;
  114. }
  115. req->username = http->uri->user;
  116. password = ( http->uri->password ? http->uri->password : "" );
  117. /* Generate key */
  118. ntlm_key ( NULL, req->username, password, &key );
  119. /* Generate responses */
  120. ntlm_response ( &rsp->info, &key, NULL, &req->lm, &req->nt );
  121. /* Calculate Authenticate message length */
  122. req->len = ntlm_authenticate_len ( &rsp->info, NULL, req->username,
  123. http_ntlm_workstation );
  124. return 0;
  125. }
  126. /**
  127. * Construct HTTP "Authorization" header for NTLM authentication
  128. *
  129. * @v http HTTP transaction
  130. * @v buf Buffer
  131. * @v len Length of buffer
  132. * @ret len Length of header value, or negative error
  133. */
  134. static int http_format_ntlm_auth ( struct http_transaction *http,
  135. char *buf, size_t len ) {
  136. struct http_request_auth_ntlm *req = &http->request.auth.ntlm;
  137. struct http_response_auth_ntlm *rsp = &http->response.auth.ntlm;
  138. struct ntlm_authenticate *auth;
  139. size_t check;
  140. /* If we have no challenge yet, then just send a Negotiate message */
  141. if ( ! rsp->challenge ) {
  142. return base64_encode ( &ntlm_negotiate,
  143. sizeof ( ntlm_negotiate ), buf, len );
  144. }
  145. /* Skip allocation if just calculating length */
  146. if ( ! len )
  147. return base64_encoded_len ( req->len );
  148. /* Allocate temporary buffer for Authenticate message */
  149. auth = malloc ( req->len );
  150. if ( ! auth )
  151. return -ENOMEM;
  152. /* Construct raw Authenticate message */
  153. check = ntlm_authenticate ( &rsp->info, NULL, req->username,
  154. http_ntlm_workstation, &req->lm,
  155. &req->nt, auth );
  156. assert ( check == req->len );
  157. /* Base64-encode Authenticate message */
  158. len = base64_encode ( auth, req->len, buf, len );
  159. /* Free raw Authenticate message */
  160. free ( auth );
  161. return len;
  162. }
  163. /** HTTP NTLM authentication scheme */
  164. struct http_authentication http_ntlm_auth __http_authentication = {
  165. .name = "NTLM",
  166. .parse = http_parse_ntlm_auth,
  167. .authenticate = http_ntlm_authenticate,
  168. .format = http_format_ntlm_auth,
  169. };
  170. /* Drag in HTTP authentication support */
  171. REQUIRING_SYMBOL ( http_ntlm_auth );
  172. REQUIRE_OBJECT ( httpauth );