Provide an abstract thread pool object
[openafs.git] / src / util / krb5_nt.c
1 /*
2  * Copyright (c) 2010 Your File System, Inc.
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 are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Neither the name of Your File System, Inc. nor the names of its contributors may be
11  *   used to endorse or promote products derived from this software without
12  *   specific prior written permission from Your File System, Inc.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27  #include <afsconfig.h>
28 #include <afs/param.h>
29
30 #include <windows.h>
31 #include "krb5_nt.h"
32
33 static char * (KRB5_CALLCONV *pkrb5_get_error_message)(krb5_context context, krb5_error_code code) = NULL;
34 static void (KRB5_CALLCONV *pkrb5_free_error_message)(krb5_context context, char *s) = NULL;
35
36 # ifndef _WIN64
37 #  define KRB5LIB "krb5_32.dll"
38 # else
39 #  define KRB5LIB "krb5_64.dll"
40 # endif
41
42
43 void
44 initialize_krb5(void)
45 {
46     /*
47      * On Windows, the error table will be initialized when the
48      * krb5 library is loaded into the process.  Since not all
49      * versions of krb5 contain krb5_{get,free}_error_message()
50      * we load them conditionally by function pointer.
51      *
52      * On Unix, the MIT krb5 error table will be initialized
53      * by the library on first use.
54      *
55      * initialize_krb5_error_table is a macro substitution to
56      * nothing.
57      */
58     HINSTANCE h = LoadLibrary(KRB5LIB);
59     if (h) {
60         (FARPROC)pkrb5_get_error_message = GetProcAddress(h, "krb5_get_error_message");
61         (FARPROC)pkrb5_free_error_message = GetProcAddress(h, "krb5_free_error_message");
62     }
63 }
64
65 const char *
66 fetch_krb5_error_message(krb5_context context, krb5_error_code code)
67 {
68     static char errorText[1024];
69
70     if (pkrb5_get_error_message) {
71         char *msg = pkrb5_get_error_message(context, code);
72         if (msg) {
73             strncpy(errorText, msg, sizeof(errorText));
74             errorText[sizeof(errorText)-1]='\0';
75             pkrb5_free_error_message(context, msg);
76             return errorText;
77         }
78     }
79     return NULL;
80 }