2 * Portability glue functions for Kerberos.
4 * This file provides definitions of the interfaces that portable/krb5.h
5 * ensures exist if the function wasn't available in the Kerberos libraries.
6 * Everything in this file will be protected by #ifndef. If the native
7 * Kerberos libraries are fully capable, this file will be skipped.
9 * The canonical version of this file is maintained in the rra-c-util package,
10 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
12 * Written by Russ Allbery <eagle@eyrie.org>
13 * Copyright 2015-2016, 2018 Russ Allbery <eagle@eyrie.org>
14 * Copyright 2010-2012, 2014
15 * The Board of Trustees of the Leland Stanford Junior University
17 * Copying and distribution of this file, with or without modification, are
18 * permitted in any medium without royalty provided the copyright notice and
19 * this notice are preserved. This file is offered as-is, without any
22 * SPDX-License-Identifier: FSFAP
26 #include <portable/krb5.h>
27 #include <portable/macros.h>
28 #include <portable/system.h>
32 /* Figure out what header files to include for error reporting. */
33 #if !defined(HAVE_KRB5_GET_ERROR_MESSAGE) && !defined(HAVE_KRB5_GET_ERR_TEXT)
34 # if !defined(HAVE_KRB5_GET_ERROR_STRING)
35 # if defined(HAVE_IBM_SVC_KRB5_SVC_H)
36 # include <ibm_svc/krb5_svc.h>
37 # elif defined(HAVE_ET_COM_ERR_H)
38 # include <et/com_err.h>
39 # elif defined(HAVE_KERBEROSV5_COM_ERR_H)
40 # include <kerberosv5/com_err.h>
47 /* Used for unused parameters to silence gcc warnings. */
48 #define UNUSED __attribute__((__unused__))
51 * This string is returned for unknown error messages. We use a static
52 * variable so that we can be sure not to free it.
54 #if !defined(HAVE_KRB5_GET_ERROR_MESSAGE) \
55 || !defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
56 static const char error_unknown[] = "unknown error";
60 #ifndef HAVE_KRB5_GET_ERROR_MESSAGE
62 * Given a Kerberos error code, return the corresponding error. Prefer the
63 * Kerberos interface if available since it will provide context-specific
64 * error information, whereas the error_message() call will only provide a
68 krb5_get_error_message(krb5_context ctx UNUSED, krb5_error_code code UNUSED)
72 # if defined(HAVE_KRB5_GET_ERROR_STRING)
73 msg = krb5_get_error_string(ctx);
74 # elif defined(HAVE_KRB5_GET_ERR_TEXT)
75 msg = krb5_get_err_text(ctx, code);
76 # elif defined(HAVE_KRB5_SVC_GET_MSG)
77 krb5_svc_get_msg(code, (char **) &msg);
79 msg = error_message(code);
86 #endif /* !HAVE_KRB5_GET_ERROR_MESSAGE */
89 #ifndef HAVE_KRB5_FREE_ERROR_MESSAGE
91 * Free an error string if necessary. If we returned a static string, make
92 * sure we don't free it.
94 * This code assumes that the set of implementations that have
95 * krb5_free_error_message is a subset of those with krb5_get_error_message.
96 * If this assumption ever breaks, we may call the wrong free function.
99 krb5_free_error_message(krb5_context ctx UNUSED, const char *msg)
101 if (msg == error_unknown)
103 # if defined(HAVE_KRB5_GET_ERROR_STRING)
104 krb5_free_error_string(ctx, (char *) msg);
105 # elif defined(HAVE_KRB5_SVC_GET_MSG)
106 krb5_free_string(ctx, (char *) msg);
109 #endif /* !HAVE_KRB5_FREE_ERROR_MESSAGE */
112 #ifndef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
114 * Allocate and initialize a krb5_get_init_creds_opt struct. This code
115 * assumes that an all-zero bit pattern will create a NULL pointer.
118 krb5_get_init_creds_opt_alloc(krb5_context ctx UNUSED,
119 krb5_get_init_creds_opt **opts)
121 *opts = calloc(1, sizeof(krb5_get_init_creds_opt));
124 krb5_get_init_creds_opt_init(*opts);
127 #endif /* !HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
130 #ifndef HAVE_KRB5_PRINCIPAL_GET_REALM
132 * Return the realm of a principal as a const char *.
135 krb5_principal_get_realm(krb5_context ctx UNUSED, krb5_const_principal princ)
137 const krb5_data *data;
139 data = krb5_princ_realm(ctx, princ);
140 if (data == NULL || data->data == NULL)
144 #endif /* !HAVE_KRB5_PRINCIPAL_GET_REALM */