Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
| 25 | #ifdef USE_WINDOWS_SSPI |
| 26 | |
| 27 | #include <curl/curl.h> |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 28 | #include "curl_sspi.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 29 | #include "curl_multibyte.h" |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 30 | #include "system_win32.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 31 | #include "warnless.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 32 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 33 | /* The last #include files should be: */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 34 | #include "curl_memory.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 35 | #include "memdebug.h" |
| 36 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 37 | /* We use our own typedef here since some headers might lack these */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 38 | typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID); |
| 39 | |
| 40 | /* See definition of SECURITY_ENTRYPOINT in sspi.h */ |
| 41 | #ifdef UNICODE |
| 42 | # ifdef _WIN32_WCE |
| 43 | # define SECURITYENTRYPOINT L"InitSecurityInterfaceW" |
| 44 | # else |
| 45 | # define SECURITYENTRYPOINT "InitSecurityInterfaceW" |
| 46 | # endif |
| 47 | #else |
| 48 | # define SECURITYENTRYPOINT "InitSecurityInterfaceA" |
| 49 | #endif |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 50 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 51 | /* Handle of security.dll or secur32.dll, depending on Windows version */ |
| 52 | HMODULE s_hSecDll = NULL; |
| 53 | |
| 54 | /* Pointer to SSPI dispatch table */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 55 | PSecurityFunctionTable s_pSecFn = NULL; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Curl_sspi_global_init() |
| 59 | * |
| 60 | * This is used to load the Security Service Provider Interface (SSPI) |
| 61 | * dynamic link library portably across all Windows versions, without |
| 62 | * the need to directly link libcurl, nor the application using it, at |
| 63 | * build time. |
| 64 | * |
| 65 | * Once this function has been executed, Windows SSPI functions can be |
| 66 | * called through the Security Service Provider Interface dispatch table. |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 67 | * |
| 68 | * Parameters: |
| 69 | * |
| 70 | * None. |
| 71 | * |
| 72 | * Returns CURLE_OK on success. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 73 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 74 | CURLcode Curl_sspi_global_init(void) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 75 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 76 | INITSECURITYINTERFACE_FN pInitSecurityInterface; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 77 | |
| 78 | /* If security interface is not yet initialized try to do this */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 79 | if(!s_hSecDll) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 80 | /* Security Service Provider Interface (SSPI) functions are located in |
| 81 | * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP |
| 82 | * have both these DLLs (security.dll forwards calls to secur32.dll) */ |
| 83 | |
| 84 | /* Load SSPI dll into the address space of the calling process */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 85 | if(Curl_verify_windows_version(4, 0, PLATFORM_WINNT, VERSION_EQUAL)) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 86 | s_hSecDll = Curl_load_library(TEXT("security.dll")); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 87 | else |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 88 | s_hSecDll = Curl_load_library(TEXT("secur32.dll")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 89 | if(!s_hSecDll) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 90 | return CURLE_FAILED_INIT; |
| 91 | |
| 92 | /* Get address of the InitSecurityInterfaceA function from the SSPI dll */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 93 | pInitSecurityInterface = (INITSECURITYINTERFACE_FN) |
| 94 | GetProcAddress(s_hSecDll, SECURITYENTRYPOINT); |
| 95 | if(!pInitSecurityInterface) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 96 | return CURLE_FAILED_INIT; |
| 97 | |
| 98 | /* Get pointer to Security Service Provider Interface dispatch table */ |
| 99 | s_pSecFn = pInitSecurityInterface(); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 100 | if(!s_pSecFn) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 101 | return CURLE_FAILED_INIT; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 102 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 103 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 104 | return CURLE_OK; |
| 105 | } |
| 106 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 107 | /* |
| 108 | * Curl_sspi_global_cleanup() |
| 109 | * |
| 110 | * This deinitializes the Security Service Provider Interface from libcurl. |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 111 | * |
| 112 | * Parameters: |
| 113 | * |
| 114 | * None. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 115 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 116 | void Curl_sspi_global_cleanup(void) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 117 | { |
| 118 | if(s_hSecDll) { |
| 119 | FreeLibrary(s_hSecDll); |
| 120 | s_hSecDll = NULL; |
| 121 | s_pSecFn = NULL; |
| 122 | } |
| 123 | } |
| 124 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Curl_create_sspi_identity() |
| 127 | * |
| 128 | * This is used to populate a SSPI identity structure based on the supplied |
| 129 | * username and password. |
| 130 | * |
| 131 | * Parameters: |
| 132 | * |
| 133 | * userp [in] - The user name in the format User or Domain\User. |
| 134 | * passdwp [in] - The user's password. |
| 135 | * identity [in/out] - The identity structure. |
| 136 | * |
| 137 | * Returns CURLE_OK on success. |
| 138 | */ |
| 139 | CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp, |
| 140 | SEC_WINNT_AUTH_IDENTITY *identity) |
| 141 | { |
| 142 | xcharp_u useranddomain; |
| 143 | xcharp_u user, dup_user; |
| 144 | xcharp_u domain, dup_domain; |
| 145 | xcharp_u passwd, dup_passwd; |
| 146 | size_t domlen = 0; |
| 147 | |
| 148 | domain.const_tchar_ptr = TEXT(""); |
| 149 | |
| 150 | /* Initialize the identity */ |
| 151 | memset(identity, 0, sizeof(*identity)); |
| 152 | |
| 153 | useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp); |
| 154 | if(!useranddomain.tchar_ptr) |
| 155 | return CURLE_OUT_OF_MEMORY; |
| 156 | |
| 157 | user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\')); |
| 158 | if(!user.const_tchar_ptr) |
| 159 | user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/')); |
| 160 | |
| 161 | if(user.tchar_ptr) { |
| 162 | domain.tchar_ptr = useranddomain.tchar_ptr; |
| 163 | domlen = user.tchar_ptr - useranddomain.tchar_ptr; |
| 164 | user.tchar_ptr++; |
| 165 | } |
| 166 | else { |
| 167 | user.tchar_ptr = useranddomain.tchar_ptr; |
| 168 | domain.const_tchar_ptr = TEXT(""); |
| 169 | domlen = 0; |
| 170 | } |
| 171 | |
| 172 | /* Setup the identity's user and length */ |
| 173 | dup_user.tchar_ptr = _tcsdup(user.tchar_ptr); |
| 174 | if(!dup_user.tchar_ptr) { |
| 175 | Curl_unicodefree(useranddomain.tchar_ptr); |
| 176 | return CURLE_OUT_OF_MEMORY; |
| 177 | } |
| 178 | identity->User = dup_user.tbyte_ptr; |
| 179 | identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr)); |
| 180 | dup_user.tchar_ptr = NULL; |
| 181 | |
| 182 | /* Setup the identity's domain and length */ |
| 183 | dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1)); |
| 184 | if(!dup_domain.tchar_ptr) { |
| 185 | Curl_unicodefree(useranddomain.tchar_ptr); |
| 186 | return CURLE_OUT_OF_MEMORY; |
| 187 | } |
| 188 | _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen); |
| 189 | *(dup_domain.tchar_ptr + domlen) = TEXT('\0'); |
| 190 | identity->Domain = dup_domain.tbyte_ptr; |
| 191 | identity->DomainLength = curlx_uztoul(domlen); |
| 192 | dup_domain.tchar_ptr = NULL; |
| 193 | |
| 194 | Curl_unicodefree(useranddomain.tchar_ptr); |
| 195 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 196 | /* Setup the identity's password and length */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 197 | passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp); |
| 198 | if(!passwd.tchar_ptr) |
| 199 | return CURLE_OUT_OF_MEMORY; |
| 200 | dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr); |
| 201 | if(!dup_passwd.tchar_ptr) { |
| 202 | Curl_unicodefree(passwd.tchar_ptr); |
| 203 | return CURLE_OUT_OF_MEMORY; |
| 204 | } |
| 205 | identity->Password = dup_passwd.tbyte_ptr; |
| 206 | identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr)); |
| 207 | dup_passwd.tchar_ptr = NULL; |
| 208 | |
| 209 | Curl_unicodefree(passwd.tchar_ptr); |
| 210 | |
| 211 | /* Setup the identity's flags */ |
| 212 | identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY; |
| 213 | |
| 214 | return CURLE_OK; |
| 215 | } |
| 216 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Curl_sspi_free_identity() |
| 219 | * |
| 220 | * This is used to free the contents of a SSPI identifier structure. |
| 221 | * |
| 222 | * Parameters: |
| 223 | * |
| 224 | * identity [in/out] - The identity structure. |
| 225 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 226 | void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity) |
| 227 | { |
| 228 | if(identity) { |
| 229 | Curl_safefree(identity->User); |
| 230 | Curl_safefree(identity->Password); |
| 231 | Curl_safefree(identity->Domain); |
| 232 | } |
| 233 | } |
| 234 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 235 | #endif /* USE_WINDOWS_SSPI */ |