curl_sspi.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #ifdef USE_WINDOWS_SSPI
00026 
00027 #include <curl/curl.h>
00028 #include "curl_sspi.h"
00029 #include "curl_multibyte.h"
00030 #include "system_win32.h"
00031 #include "warnless.h"
00032 
00033 /* The last #include files should be: */
00034 #include "curl_memory.h"
00035 #include "memdebug.h"
00036 
00037 /* We use our own typedef here since some headers might lack these */
00038 typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID);
00039 
00040 /* See definition of SECURITY_ENTRYPOINT in sspi.h */
00041 #ifdef UNICODE
00042 #  ifdef _WIN32_WCE
00043 #    define SECURITYENTRYPOINT L"InitSecurityInterfaceW"
00044 #  else
00045 #    define SECURITYENTRYPOINT "InitSecurityInterfaceW"
00046 #  endif
00047 #else
00048 #  define SECURITYENTRYPOINT "InitSecurityInterfaceA"
00049 #endif
00050 
00051 /* Handle of security.dll or secur32.dll, depending on Windows version */
00052 HMODULE s_hSecDll = NULL;
00053 
00054 /* Pointer to SSPI dispatch table */
00055 PSecurityFunctionTable s_pSecFn = NULL;
00056 
00057 /*
00058  * Curl_sspi_global_init()
00059  *
00060  * This is used to load the Security Service Provider Interface (SSPI)
00061  * dynamic link library portably across all Windows versions, without
00062  * the need to directly link libcurl, nor the application using it, at
00063  * build time.
00064  *
00065  * Once this function has been executed, Windows SSPI functions can be
00066  * called through the Security Service Provider Interface dispatch table.
00067  *
00068  * Parameters:
00069  *
00070  * None.
00071  *
00072  * Returns CURLE_OK on success.
00073  */
00074 CURLcode Curl_sspi_global_init(void)
00075 {
00076   INITSECURITYINTERFACE_FN pInitSecurityInterface;
00077 
00078   /* If security interface is not yet initialized try to do this */
00079   if(!s_hSecDll) {
00080     /* Security Service Provider Interface (SSPI) functions are located in
00081      * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
00082      * have both these DLLs (security.dll forwards calls to secur32.dll) */
00083 
00084     /* Load SSPI dll into the address space of the calling process */
00085     if(Curl_verify_windows_version(4, 0, PLATFORM_WINNT, VERSION_EQUAL))
00086       s_hSecDll = Curl_load_library(TEXT("security.dll"));
00087     else
00088       s_hSecDll = Curl_load_library(TEXT("secur32.dll"));
00089     if(!s_hSecDll)
00090       return CURLE_FAILED_INIT;
00091 
00092     /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
00093     pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
00094       GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
00095     if(!pInitSecurityInterface)
00096       return CURLE_FAILED_INIT;
00097 
00098     /* Get pointer to Security Service Provider Interface dispatch table */
00099     s_pSecFn = pInitSecurityInterface();
00100     if(!s_pSecFn)
00101       return CURLE_FAILED_INIT;
00102   }
00103 
00104   return CURLE_OK;
00105 }
00106 
00107 /*
00108  * Curl_sspi_global_cleanup()
00109  *
00110  * This deinitializes the Security Service Provider Interface from libcurl.
00111  *
00112  * Parameters:
00113  *
00114  * None.
00115  */
00116 void Curl_sspi_global_cleanup(void)
00117 {
00118   if(s_hSecDll) {
00119     FreeLibrary(s_hSecDll);
00120     s_hSecDll = NULL;
00121     s_pSecFn = NULL;
00122   }
00123 }
00124 
00125 /*
00126  * Curl_create_sspi_identity()
00127  *
00128  * This is used to populate a SSPI identity structure based on the supplied
00129  * username and password.
00130  *
00131  * Parameters:
00132  *
00133  * userp    [in]     - The user name in the format User or Domain\User.
00134  * passdwp  [in]     - The user's password.
00135  * identity [in/out] - The identity structure.
00136  *
00137  * Returns CURLE_OK on success.
00138  */
00139 CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
00140                                    SEC_WINNT_AUTH_IDENTITY *identity)
00141 {
00142   xcharp_u useranddomain;
00143   xcharp_u user, dup_user;
00144   xcharp_u domain, dup_domain;
00145   xcharp_u passwd, dup_passwd;
00146   size_t domlen = 0;
00147 
00148   domain.const_tchar_ptr = TEXT("");
00149 
00150   /* Initialize the identity */
00151   memset(identity, 0, sizeof(*identity));
00152 
00153   useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
00154   if(!useranddomain.tchar_ptr)
00155     return CURLE_OUT_OF_MEMORY;
00156 
00157   user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
00158   if(!user.const_tchar_ptr)
00159     user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
00160 
00161   if(user.tchar_ptr) {
00162     domain.tchar_ptr = useranddomain.tchar_ptr;
00163     domlen = user.tchar_ptr - useranddomain.tchar_ptr;
00164     user.tchar_ptr++;
00165   }
00166   else {
00167     user.tchar_ptr = useranddomain.tchar_ptr;
00168     domain.const_tchar_ptr = TEXT("");
00169     domlen = 0;
00170   }
00171 
00172   /* Setup the identity's user and length */
00173   dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
00174   if(!dup_user.tchar_ptr) {
00175     Curl_unicodefree(useranddomain.tchar_ptr);
00176     return CURLE_OUT_OF_MEMORY;
00177   }
00178   identity->User = dup_user.tbyte_ptr;
00179   identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
00180   dup_user.tchar_ptr = NULL;
00181 
00182   /* Setup the identity's domain and length */
00183   dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
00184   if(!dup_domain.tchar_ptr) {
00185     Curl_unicodefree(useranddomain.tchar_ptr);
00186     return CURLE_OUT_OF_MEMORY;
00187   }
00188   _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
00189   *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
00190   identity->Domain = dup_domain.tbyte_ptr;
00191   identity->DomainLength = curlx_uztoul(domlen);
00192   dup_domain.tchar_ptr = NULL;
00193 
00194   Curl_unicodefree(useranddomain.tchar_ptr);
00195 
00196   /* Setup the identity's password and length */
00197   passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
00198   if(!passwd.tchar_ptr)
00199     return CURLE_OUT_OF_MEMORY;
00200   dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
00201   if(!dup_passwd.tchar_ptr) {
00202     Curl_unicodefree(passwd.tchar_ptr);
00203     return CURLE_OUT_OF_MEMORY;
00204   }
00205   identity->Password = dup_passwd.tbyte_ptr;
00206   identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
00207   dup_passwd.tchar_ptr = NULL;
00208 
00209   Curl_unicodefree(passwd.tchar_ptr);
00210 
00211   /* Setup the identity's flags */
00212   identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY;
00213 
00214   return CURLE_OK;
00215 }
00216 
00217 /*
00218  * Curl_sspi_free_identity()
00219  *
00220  * This is used to free the contents of a SSPI identifier structure.
00221  *
00222  * Parameters:
00223  *
00224  * identity [in/out] - The identity structure.
00225  */
00226 void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
00227 {
00228   if(identity) {
00229     Curl_safefree(identity->User);
00230     Curl_safefree(identity->Password);
00231     Curl_safefree(identity->Domain);
00232   }
00233 }
00234 
00235 #endif /* USE_WINDOWS_SSPI */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02