For now just disable this Heindal specific stuff in the MIT build
[Samba/bjacke.git] / libcli / auth / krb5_wrap.c
blob55a224612d11c8895fab4adf9433b4cd48d73d34
1 /*
2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2011
7 Copyright (C) Guenther Deschner 2005-2009
8 Copyright (C) Simo Sorce 2010.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #ifdef HAVE_KRB5
27 #include "libcli/auth/krb5_wrap.h"
28 #include "librpc/gen_ndr/krb5pac.h"
30 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
31 /* MIT */
32 int create_kerberos_key_from_string_direct(krb5_context context,
33 krb5_principal host_princ,
34 krb5_data *password,
35 krb5_keyblock *key,
36 krb5_enctype enctype)
38 int ret = 0;
39 krb5_data salt;
41 ret = krb5_principal2salt(context, host_princ, &salt);
42 if (ret) {
43 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
44 return ret;
46 ret = krb5_c_string_to_key(context, enctype, password, &salt, key);
47 SAFE_FREE(salt.data);
49 return ret;
51 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
52 /* Heimdal */
53 int create_kerberos_key_from_string_direct(krb5_context context,
54 krb5_principal host_princ,
55 krb5_data *password,
56 krb5_keyblock *key,
57 krb5_enctype enctype)
59 int ret;
60 krb5_salt salt;
62 ret = krb5_get_pw_salt(context, host_princ, &salt);
63 if (ret) {
64 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
65 return ret;
68 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
69 krb5_free_salt(context, salt);
71 return ret;
73 #else
74 #error UNKNOWN_CREATE_KEY_FUNCTIONS
75 #endif
77 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
79 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
80 if (pdata->data) {
81 krb5_free_data_contents(context, pdata);
83 #elif defined(HAVE_KRB5_DATA_FREE)
84 krb5_data_free(context, pdata);
85 #else
86 SAFE_FREE(pdata->data);
87 #endif
91 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
93 /* Try krb5_free_keytab_entry_contents first, since
94 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
95 * krb5_kt_free_entry but only has a prototype for the first, while the
96 * second is considered private.
98 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
99 return krb5_free_keytab_entry_contents(context, kt_entry);
100 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
101 return krb5_kt_free_entry(context, kt_entry);
102 #else
103 #error UNKNOWN_KT_FREE_FUNCTION
104 #endif
107 /**************************************************************
108 Wrappers around kerberos string functions that convert from
109 utf8 -> unix charset and vica versa.
110 **************************************************************/
112 /**************************************************************
113 krb5_parse_name that takes a UNIX charset.
114 **************************************************************/
116 krb5_error_code smb_krb5_parse_name(krb5_context context,
117 const char *name, /* in unix charset */
118 krb5_principal *principal)
120 krb5_error_code ret;
121 char *utf8_name;
122 size_t converted_size;
123 TALLOC_CTX *frame = talloc_stackframe();
125 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
126 talloc_free(frame);
127 return ENOMEM;
130 ret = krb5_parse_name(context, utf8_name, principal);
131 TALLOC_FREE(frame);
132 return ret;
135 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
136 static void krb5_free_unparsed_name(krb5_context context, char *val)
138 SAFE_FREE(val);
140 #endif
142 /**************************************************************
143 krb5_parse_name that returns a UNIX charset name. Must
144 be freed with talloc_free() call.
145 **************************************************************/
147 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
148 krb5_context context,
149 krb5_const_principal principal,
150 char **unix_name)
152 krb5_error_code ret;
153 char *utf8_name;
154 size_t converted_size;
156 *unix_name = NULL;
157 ret = krb5_unparse_name(context, principal, &utf8_name);
158 if (ret) {
159 return ret;
162 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
163 krb5_free_unparsed_name(context, utf8_name);
164 return ENOMEM;
166 krb5_free_unparsed_name(context, utf8_name);
167 return 0;
170 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
171 const char *name,
172 krb5_principal *principal)
174 /* we are cheating here because parse_name will in fact set the realm.
175 * We don't care as the only caller of smb_krb5_parse_name_norealm
176 * ignores the realm anyway when calling
177 * smb_krb5_principal_compare_any_realm later - Guenther */
179 return smb_krb5_parse_name(context, name, principal);
182 bool smb_krb5_principal_compare_any_realm(krb5_context context,
183 krb5_const_principal princ1,
184 krb5_const_principal princ2)
186 return krb5_principal_compare_any_realm(context, princ1, princ2);
189 char *gssapi_error_string(TALLOC_CTX *mem_ctx,
190 OM_uint32 maj_stat, OM_uint32 min_stat,
191 const gss_OID mech)
193 OM_uint32 disp_min_stat, disp_maj_stat;
194 gss_buffer_desc maj_error_message;
195 gss_buffer_desc min_error_message;
196 char *maj_error_string, *min_error_string;
197 OM_uint32 msg_ctx = 0;
199 char *ret;
201 maj_error_message.value = NULL;
202 min_error_message.value = NULL;
203 maj_error_message.length = 0;
204 min_error_message.length = 0;
206 disp_maj_stat = gss_display_status(&disp_min_stat, maj_stat, GSS_C_GSS_CODE,
207 mech, &msg_ctx, &maj_error_message);
208 disp_maj_stat = gss_display_status(&disp_min_stat, min_stat, GSS_C_MECH_CODE,
209 mech, &msg_ctx, &min_error_message);
211 maj_error_string = talloc_strndup(mem_ctx, (char *)maj_error_message.value, maj_error_message.length);
213 min_error_string = talloc_strndup(mem_ctx, (char *)min_error_message.value, min_error_message.length);
215 ret = talloc_asprintf(mem_ctx, "%s: %s", maj_error_string, min_error_string);
217 talloc_free(maj_error_string);
218 talloc_free(min_error_string);
220 gss_release_buffer(&disp_min_stat, &maj_error_message);
221 gss_release_buffer(&disp_min_stat, &min_error_message);
223 return ret;
227 char *smb_get_krb5_error_message(krb5_context context, krb5_error_code code, TALLOC_CTX *mem_ctx)
229 char *ret;
231 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
232 const char *context_error = krb5_get_error_message(context, code);
233 if (context_error) {
234 ret = talloc_asprintf(mem_ctx, "%s: %s", error_message(code), context_error);
235 krb5_free_error_message(context, context_error);
236 return ret;
238 #endif
239 ret = talloc_strdup(mem_ctx, error_message(code));
240 return ret;
243 #endif