Correct Heimdal conversion of libadmin/adminutil
[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 #include <roken.h>
30
31 #include <windows.h>
32 #include "krb5_nt.h"
33
34 # include <krb5\krb5.h>
35 # include <com_err.h>
36
37 static int krb5_initialized = 0;
38
39 void
40 initialize_krb5(void)
41 {
42     /*
43      * On Windows, the error table will be initialized when the
44      * krb5 library is loaded into the process for MIT KFW but
45      * for Heimdal the error table is initialized when the
46      * krb5_init_context() call is issued.  We always build
47      * against the Kerberos Compat SDK now so we do not have
48      * load by function pointer.  Use DelayLoadHeimd
49      *
50      * On Unix, the MIT krb5 error table will be initialized
51      * by the library on first use.
52      *
53      * initialize_krb5_error_table is a macro substitution to
54      * nothing.
55      */
56     if (!DelayLoadHeimdal()) {
57         fprintf(stderr, "Kerberos for Windows or Heimdal is not available.\n");
58     } else {
59         krb5_initialized = 1;
60     }
61 }
62
63 const char *
64 fetch_krb5_error_message(afs_uint32 code)
65 {
66     static char errorText[1024];
67     char *msg = NULL;
68     krb5_context context;
69
70     if (krb5_initialized && krb5_init_context(&context) == 0) {
71         msg = krb5_get_error_message(context, code);
72         if (msg) {
73             strncpy(errorText, msg, sizeof(errorText));
74             errorText[sizeof(errorText)-1]='\0';
75             krb5_free_error_message(context, msg);
76             msg = errorText;
77         }
78         krb5_free_context(context);
79     }
80     return msg;
81 }