]> eyrie.org Git - kerberos/krb5-strength.git/blob - portable/krb5-extra.c
New upstream version 3.1
[kerberos/krb5-strength.git] / portable / krb5-extra.c
1 /*
2  * Portability glue functions for Kerberos.
3  *
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.
8  *
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/>.
11  *
12  * Written by Russ Allbery <eagle@eyrie.org>
13  *
14  * The authors hereby relinquish any claim to any copyright that they may have
15  * in this work, whether granted under contract or by operation of law or
16  * international treaty, and hereby commit to the public, at large, that they
17  * shall not, at any time in the future, seek to enforce any copyright in this
18  * work against any person or entity, or prevent any person or entity from
19  * copying, publishing, distributing or creating derivative works of this
20  * work.
21  */
22
23 #include <config.h>
24 #include <portable/krb5.h>
25 #include <portable/macros.h>
26 #include <portable/system.h>
27
28 #include <errno.h>
29
30 /* Figure out what header files to include for error reporting. */
31 #if !defined(HAVE_KRB5_GET_ERROR_MESSAGE) && !defined(HAVE_KRB5_GET_ERR_TEXT)
32 # if !defined(HAVE_KRB5_GET_ERROR_STRING)
33 #  if defined(HAVE_IBM_SVC_KRB5_SVC_H)
34 #   include <ibm_svc/krb5_svc.h>
35 #  elif defined(HAVE_ET_COM_ERR_H)
36 #   include <et/com_err.h>
37 #  elif defined(HAVE_KERBEROSV5_COM_ERR_H)
38 #   include <kerberosv5/com_err.h>
39 #  else
40 #   include <com_err.h>
41 #  endif
42 # endif
43 #endif
44
45 /* Used for unused parameters to silence gcc warnings. */
46 #define UNUSED __attribute__((__unused__))
47
48 /*
49  * This string is returned for unknown error messages.  We use a static
50  * variable so that we can be sure not to free it.
51  */
52 #if !defined(HAVE_KRB5_GET_ERROR_MESSAGE) \
53     || !defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
54 static const char error_unknown[] = "unknown error";
55 #endif
56
57
58 #ifndef HAVE_KRB5_GET_ERROR_MESSAGE
59 /*
60  * Given a Kerberos error code, return the corresponding error.  Prefer the
61  * Kerberos interface if available since it will provide context-specific
62  * error information, whereas the error_message() call will only provide a
63  * fixed message.
64  */
65 const char *
66 krb5_get_error_message(krb5_context ctx UNUSED, krb5_error_code code UNUSED)
67 {
68     const char *msg = NULL;
69
70 # if defined(HAVE_KRB5_GET_ERROR_STRING)
71     msg = krb5_get_error_string(ctx);
72 # elif defined(HAVE_KRB5_GET_ERR_TEXT)
73     msg = krb5_get_err_text(ctx, code);
74 # elif defined(HAVE_KRB5_SVC_GET_MSG)
75     krb5_svc_get_msg(code, (char **) &msg);
76 # else
77     msg = error_message(code);
78 # endif
79     if (msg == NULL)
80         return error_unknown;
81     else
82         return msg;
83 }
84 #endif /* !HAVE_KRB5_GET_ERROR_MESSAGE */
85
86
87 #ifndef HAVE_KRB5_FREE_ERROR_MESSAGE
88 /*
89  * Free an error string if necessary.  If we returned a static string, make
90  * sure we don't free it.
91  *
92  * This code assumes that the set of implementations that have
93  * krb5_free_error_message is a subset of those with krb5_get_error_message.
94  * If this assumption ever breaks, we may call the wrong free function.
95  */
96 void
97 krb5_free_error_message(krb5_context ctx UNUSED, const char *msg)
98 {
99     if (msg == error_unknown)
100         return;
101 # if defined(HAVE_KRB5_GET_ERROR_STRING)
102     krb5_free_error_string(ctx, (char *) msg);
103 # elif defined(HAVE_KRB5_SVC_GET_MSG)
104     krb5_free_string(ctx, (char *) msg);
105 # endif
106 }
107 #endif /* !HAVE_KRB5_FREE_ERROR_MESSAGE */
108
109
110 #ifndef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
111 /*
112  * Allocate and initialize a krb5_get_init_creds_opt struct.  This code
113  * assumes that an all-zero bit pattern will create a NULL pointer.
114  */
115 krb5_error_code
116 krb5_get_init_creds_opt_alloc(krb5_context ctx UNUSED,
117                               krb5_get_init_creds_opt **opts)
118 {
119     *opts = calloc(1, sizeof(krb5_get_init_creds_opt));
120     if (*opts == NULL)
121         return errno;
122     krb5_get_init_creds_opt_init(*opts);
123     return 0;
124 }
125 #endif /* !HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
126
127
128 #ifndef HAVE_KRB5_PRINCIPAL_GET_REALM
129 /*
130  * Return the realm of a principal as a const char *.
131  */
132 const char *
133 krb5_principal_get_realm(krb5_context ctx UNUSED, krb5_const_principal princ)
134 {
135     const krb5_data *data;
136
137     data = krb5_princ_realm(ctx, princ);
138     if (data == NULL || data->data == NULL)
139         return NULL;
140     return data->data;
141 }
142 #endif /* !HAVE_KRB5_PRINCIPAL_GET_REALM */