vauth.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2021, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "vauth.h"
  25. #include "curl_multibyte.h"
  26. #include "curl_printf.h"
  27. /* The last #include files should be: */
  28. #include "curl_memory.h"
  29. #include "memdebug.h"
  30. /*
  31. * Curl_auth_build_spn()
  32. *
  33. * This is used to build a SPN string in the following formats:
  34. *
  35. * service/host@realm (Not currently used)
  36. * service/host (Not used by GSS-API)
  37. * service@realm (Not used by Windows SSPI)
  38. *
  39. * Parameters:
  40. *
  41. * service [in] - The service type such as http, smtp, pop or imap.
  42. * host [in] - The host name.
  43. * realm [in] - The realm.
  44. *
  45. * Returns a pointer to the newly allocated SPN.
  46. */
  47. #if !defined(USE_WINDOWS_SSPI)
  48. char *Curl_auth_build_spn(const char *service, const char *host,
  49. const char *realm)
  50. {
  51. char *spn = NULL;
  52. /* Generate our SPN */
  53. if(host && realm)
  54. spn = aprintf("%s/%s@%s", service, host, realm);
  55. else if(host)
  56. spn = aprintf("%s/%s", service, host);
  57. else if(realm)
  58. spn = aprintf("%s@%s", service, realm);
  59. /* Return our newly allocated SPN */
  60. return spn;
  61. }
  62. #else
  63. TCHAR *Curl_auth_build_spn(const char *service, const char *host,
  64. const char *realm)
  65. {
  66. char *utf8_spn = NULL;
  67. TCHAR *tchar_spn = NULL;
  68. TCHAR *dupe_tchar_spn = NULL;
  69. (void) realm;
  70. /* Note: We could use DsMakeSPN() or DsClientMakeSpnForTargetServer() rather
  71. than doing this ourselves but the first is only available in Windows XP
  72. and Windows Server 2003 and the latter is only available in Windows 2000
  73. but not Windows95/98/ME or Windows NT4.0 unless the Active Directory
  74. Client Extensions are installed. As such it is far simpler for us to
  75. formulate the SPN instead. */
  76. /* Generate our UTF8 based SPN */
  77. utf8_spn = aprintf("%s/%s", service, host);
  78. if(!utf8_spn)
  79. return NULL;
  80. /* Allocate and return a TCHAR based SPN. Since curlx_convert_UTF8_to_tchar
  81. must be freed by curlx_unicodefree we'll dupe the result so that the
  82. pointer this function returns can be normally free'd. */
  83. tchar_spn = curlx_convert_UTF8_to_tchar(utf8_spn);
  84. free(utf8_spn);
  85. if(!tchar_spn)
  86. return NULL;
  87. dupe_tchar_spn = _tcsdup(tchar_spn);
  88. curlx_unicodefree(tchar_spn);
  89. return dupe_tchar_spn;
  90. }
  91. #endif /* USE_WINDOWS_SSPI */
  92. /*
  93. * Curl_auth_user_contains_domain()
  94. *
  95. * This is used to test if the specified user contains a Windows domain name as
  96. * follows:
  97. *
  98. * Domain\User (Down-level Logon Name)
  99. * Domain/User (curl Down-level format - for compatibility with existing code)
  100. * User@Domain (User Principal Name)
  101. *
  102. * Note: The user name may be empty when using a GSS-API library or Windows
  103. * SSPI as the user and domain are either obtained from the credentials cache
  104. * when using GSS-API or via the currently logged in user's credentials when
  105. * using Windows SSPI.
  106. *
  107. * Parameters:
  108. *
  109. * user [in] - The user name.
  110. *
  111. * Returns TRUE on success; otherwise FALSE.
  112. */
  113. bool Curl_auth_user_contains_domain(const char *user)
  114. {
  115. bool valid = FALSE;
  116. if(user && *user) {
  117. /* Check we have a domain name or UPN present */
  118. char *p = strpbrk(user, "\\/@");
  119. valid = (p != NULL && p > user && p < user + strlen(user) - 1 ? TRUE :
  120. FALSE);
  121. }
  122. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  123. else
  124. /* User and domain are obtained from the GSS-API credentials cache or the
  125. currently logged in user from Windows */
  126. valid = TRUE;
  127. #endif
  128. return valid;
  129. }