10e3359cdc03e97e2dd97b7a15065c8bfa59844e
[mech_eap.orig] / mech_eap / util_tld.c
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Thread local data abstraction, using pthreads on Unix and the TlsXXX
35  * APIs on Windows.
36  */
37
38 #include "gssapiP_eap.h"
39
40 /* Clean up thread-local data; called on thread detach */
41 static void
42 destroyThreadLocalData(struct gss_eap_thread_local_data *tld)
43 {
44     if (tld->statusInfo != NULL)
45         gssEapDestroyStatusInfo(tld->statusInfo);
46     if (tld->krbContext != NULL)
47         gssEapDestroyKrbContext(tld->krbContext);
48     GSSEAP_FREE(tld);
49 }
50
51 #ifdef WIN32
52
53 /*
54  * This is the TLS index returned by TlsAlloc() on process init.
55  * Each thread, on thread attach in DllMain(), allocates its thread-local
56  * data and uses this index with TlsSetValue() to store it.
57  * It can then subsequently be retrieved with TlsGetValue().
58  */
59 static DWORD tlsIndex;
60
61 /* Access thread-local data */
62 struct gss_eap_thread_local_data *
63 gssEapGetThreadLocalData(void)
64 {
65     struct gss_eap_thread_local_data *tld;
66
67     tld = TlsGetValue(tlsIndex);
68
69     GSSEAP_ASSERT(tld != NULL);
70
71     return tld;
72 }
73
74 BOOL WINAPI
75 DllMain(HINSTANCE hDLL,     /* DLL module handle */
76         DWORD reason,       /* reason called */
77         LPVOID reserved)    /* reserved */
78 {
79     struct gss_eap_thread_local_data *tlsData;
80     OM_uint32 major, minor;
81
82     switch (reason) {
83         case DLL_PROCESS_ATTACH:
84             /* Allocate a TLS index. */
85             major = gssEapInitiatorInit(&minor);
86             if (GSS_ERROR(major))
87                 return FALSE;
88
89             tlsIndex = TlsAlloc();
90             if (tlsIndex == TLS_OUT_OF_INDEXES)
91                 return FALSE;
92             /* No break: Initialize the index for first thread.*/
93         case DLL_THREAD_ATTACH:
94             /* Initialize the TLS index for this thread. */
95             tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
96             if (tlsData != NULL)
97                 TlsSetValue(tlsIndex, tlsData);
98             break;
99         case DLL_THREAD_DETACH:
100             /* Release the allocated memory for this thread. */
101             tlsData = TlsGetValue(tlsIndex);
102             if (tlsData != NULL) {
103                 destroyThreadLocalData(tlsData);
104                 TlsSetValue(tlsIndex, NULL);
105             }
106             break;
107         case DLL_PROCESS_DETACH:
108             /* Release the TLS index. */
109             TlsFree(tlsIndex);
110             gssEapFinalize();
111             break;
112         default:
113             break;
114     }
115
116     return TRUE;
117     UNREFERENCED_PARAMETER(hDLL);
118     UNREFERENCED_PARAMETER(reserved);
119 }
120
121 #else /* WIN32 */
122
123 /* pthreads implementation */
124
125 static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
126 static GSSEAP_THREAD_KEY tldKey;
127
128 static void
129 pthreadDestroyThreadLocalData(void *arg)
130 {
131     struct gss_eap_thread_local_data* tld = arg;
132
133     if (tld != NULL)
134         destroyThreadLocalData(tld);
135 }
136
137 static void
138 createThreadLocalDataKey(void)
139 {
140     GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
141 }
142
143 struct gss_eap_thread_local_data *
144 gssEapGetThreadLocalData()
145 {
146     struct gss_eap_thread_local_data *tld;
147
148     GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
149
150     tld = GSSEAP_GETSPECIFIC(tldKey);
151     if (tld == NULL) {
152         tld = GSSEAP_CALLOC(1, sizeof(*tld));
153         if (tld == NULL)
154             return NULL;
155
156         GSSEAP_SETSPECIFIC(tldKey, tld);
157     }
158
159     return tld;
160 }
161
162 #endif /* WIN32 */