e747d86b1d2684053ec4b63365158ceff4f25992
[platform/upstream/curl.git] / lib / curl_sspi.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.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
23 #include "curl_setup.h"
24
25 #ifdef USE_WINDOWS_SSPI
26
27 #include <curl/curl.h>
28
29 #include "curl_sspi.h"
30
31 #define _MPRINTF_REPLACE /* use our functions only */
32 #include <curl/mprintf.h>
33
34 #include "curl_memory.h"
35 /* The last #include file should be: */
36 #include "memdebug.h"
37
38 /* We use our own typedef here since some headers might lack these */
39 typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID);
40
41 /* See definition of SECURITY_ENTRYPOINT in sspi.h */
42 #ifdef UNICODE
43 #  ifdef _WIN32_WCE
44 #    define SECURITYENTRYPOINT L"InitSecurityInterfaceW"
45 #  else
46 #    define SECURITYENTRYPOINT "InitSecurityInterfaceW"
47 #  endif
48 #else
49 #  define SECURITYENTRYPOINT "InitSecurityInterfaceA"
50 #endif
51
52 /* Handle of security.dll or secur32.dll, depending on Windows version */
53 HMODULE s_hSecDll = NULL;
54
55 /* Pointer to SSPI dispatch table */
56 PSecurityFunctionTable s_pSecFn = NULL;
57
58 /*
59  * Curl_sspi_global_init()
60  *
61  * This is used to load the Security Service Provider Interface (SSPI)
62  * dynamic link library portably across all Windows versions, without
63  * the need to directly link libcurl, nor the application using it, at
64  * build time.
65  *
66  * Once this function has been executed, Windows SSPI functions can be
67  * called through the Security Service Provider Interface dispatch table.
68  */
69 CURLcode Curl_sspi_global_init(void)
70 {
71   OSVERSIONINFO osver;
72   INITSECURITYINTERFACE_FN pInitSecurityInterface;
73
74   /* If security interface is not yet initialized try to do this */
75   if(!s_hSecDll) {
76
77     /* Find out Windows version */
78     memset(&osver, 0, sizeof(osver));
79     osver.dwOSVersionInfoSize = sizeof(osver);
80     if(!GetVersionEx(&osver))
81       return CURLE_FAILED_INIT;
82
83     /* Security Service Provider Interface (SSPI) functions are located in
84      * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
85      * have both these DLLs (security.dll forwards calls to secur32.dll) */
86
87     /* Load SSPI dll into the address space of the calling process */
88     if(osver.dwPlatformId == VER_PLATFORM_WIN32_NT
89       && osver.dwMajorVersion == 4)
90       s_hSecDll = LoadLibrary(TEXT("security.dll"));
91     else
92       s_hSecDll = LoadLibrary(TEXT("secur32.dll"));
93     if(!s_hSecDll)
94       return CURLE_FAILED_INIT;
95
96     /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
97     pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
98       GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
99     if(!pInitSecurityInterface)
100       return CURLE_FAILED_INIT;
101
102     /* Get pointer to Security Service Provider Interface dispatch table */
103     s_pSecFn = pInitSecurityInterface();
104     if(!s_pSecFn)
105       return CURLE_FAILED_INIT;
106   }
107
108   return CURLE_OK;
109 }
110
111 /*
112  * Curl_sspi_global_cleanup()
113  *
114  * This deinitializes the Security Service Provider Interface from libcurl.
115  */
116
117 void Curl_sspi_global_cleanup(void)
118 {
119   if(s_hSecDll) {
120     FreeLibrary(s_hSecDll);
121     s_hSecDll = NULL;
122     s_pSecFn = NULL;
123   }
124 }
125
126 #endif /* USE_WINDOWS_SSPI */