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

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