2
0

vauth.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "vauth.h"
  27. #include "urldata.h"
  28. #include "strcase.h"
  29. #include "curl_multibyte.h"
  30. #include "curl_printf.h"
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. /*
  35. * Curl_auth_build_spn()
  36. *
  37. * This is used to build a SPN string in the following formats:
  38. *
  39. * service/host@realm (Not currently used)
  40. * service/host (Not used by GSS-API)
  41. * service@realm (Not used by Windows SSPI)
  42. *
  43. * Parameters:
  44. *
  45. * service [in] - The service type such as http, smtp, pop or imap.
  46. * host [in] - The host name.
  47. * realm [in] - The realm.
  48. *
  49. * Returns a pointer to the newly allocated SPN.
  50. */
  51. #if !defined(USE_WINDOWS_SSPI)
  52. char *Curl_auth_build_spn(const char *service, const char *host,
  53. const char *realm)
  54. {
  55. char *spn = NULL;
  56. /* Generate our SPN */
  57. if(host && realm)
  58. spn = aprintf("%s/%s@%s", service, host, realm);
  59. else if(host)
  60. spn = aprintf("%s/%s", service, host);
  61. else if(realm)
  62. spn = aprintf("%s@%s", service, realm);
  63. /* Return our newly allocated SPN */
  64. return spn;
  65. }
  66. #else
  67. TCHAR *Curl_auth_build_spn(const char *service, const char *host,
  68. const char *realm)
  69. {
  70. char *utf8_spn = NULL;
  71. TCHAR *tchar_spn = NULL;
  72. TCHAR *dupe_tchar_spn = NULL;
  73. (void) realm;
  74. /* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather
  75. than doing this ourselves but the first is only available in Windows XP
  76. and Windows Server 2003 and the latter is only available in Windows 2000
  77. but not Windows95/98/ME or Windows NT4.0 unless the Active Directory
  78. Client Extensions are installed. As such it is far simpler for us to
  79. formulate the SPN instead. */
  80. /* Generate our UTF8 based SPN */
  81. utf8_spn = aprintf("%s/%s", service, host);
  82. if(!utf8_spn)
  83. return NULL;
  84. /* Allocate and return a TCHAR based SPN. Since curlx_convert_UTF8_to_tchar
  85. must be freed by curlx_unicodefree we'll dupe the result so that the
  86. pointer this function returns can be normally free'd. */
  87. tchar_spn = curlx_convert_UTF8_to_tchar(utf8_spn);
  88. free(utf8_spn);
  89. if(!tchar_spn)
  90. return NULL;
  91. dupe_tchar_spn = _tcsdup(tchar_spn);
  92. curlx_unicodefree(tchar_spn);
  93. return dupe_tchar_spn;
  94. }
  95. #endif /* USE_WINDOWS_SSPI */
  96. /*
  97. * Curl_auth_user_contains_domain()
  98. *
  99. * This is used to test if the specified user contains a Windows domain name as
  100. * follows:
  101. *
  102. * Domain\User (Down-level Logon Name)
  103. * Domain/User (curl Down-level format - for compatibility with existing code)
  104. * User@Domain (User Principal Name)
  105. *
  106. * Note: The user name may be empty when using a GSS-API library or Windows
  107. * SSPI as the user and domain are either obtained from the credentials cache
  108. * when using GSS-API or via the currently logged in user's credentials when
  109. * using Windows SSPI.
  110. *
  111. * Parameters:
  112. *
  113. * user [in] - The user name.
  114. *
  115. * Returns TRUE on success; otherwise FALSE.
  116. */
  117. bool Curl_auth_user_contains_domain(const char *user)
  118. {
  119. bool valid = FALSE;
  120. if(user && *user) {
  121. /* Check we have a domain name or UPN present */
  122. char *p = strpbrk(user, "\\/@");
  123. valid = (p != NULL && p > user && p < user + strlen(user) - 1 ? TRUE :
  124. FALSE);
  125. }
  126. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  127. else
  128. /* User and domain are obtained from the GSS-API credentials cache or the
  129. currently logged in user from Windows */
  130. valid = TRUE;
  131. #endif
  132. return valid;
  133. }
  134. /*
  135. * Curl_auth_ollowed_to_host() tells if authentication, cookies or other
  136. * "sensitive data" can (still) be sent to this host.
  137. */
  138. bool Curl_auth_allowed_to_host(struct Curl_easy *data)
  139. {
  140. struct connectdata *conn = data->conn;
  141. return (!data->state.this_is_a_follow ||
  142. data->set.allow_auth_to_other_hosts ||
  143. (data->state.first_host &&
  144. strcasecompare(data->state.first_host, conn->host.name) &&
  145. (data->state.first_remote_port == conn->remote_port) &&
  146. (data->state.first_remote_protocol == conn->handler->protocol)));
  147. }