krb5_wrap: Move smb_krb5_kt_add_entry() to krb5_wrap
[samba.git] / lib / krb5_wrap / krb5_samba.h
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
7    Copyright (C) Guenther Deschner 2005-2009
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef _KRB5_SAMBA_H
24 #define _KRB5_SAMBA_H
25
26 #ifdef HAVE_KRB5
27
28 #define KRB5_PRIVATE    1       /* this file uses PRIVATE interfaces! */
29 /* this file uses DEPRECATED interfaces! */
30
31 #ifdef KRB5_DEPRECATED
32 #undef KRB5_DEPRECATED
33 #endif
34
35 #if defined(HAVE_KRB5_DEPRECATED_WITH_IDENTIFIER)
36 #define KRB5_DEPRECATED 1
37 #else
38 #define KRB5_DEPRECATED
39 #endif
40
41 #include "system/kerberos.h"
42 #include "system/network.h"
43
44 #ifndef KRB5_ADDR_NETBIOS
45 #define KRB5_ADDR_NETBIOS 0x14
46 #endif
47
48 #ifndef KRB5KRB_ERR_RESPONSE_TOO_BIG
49 #define KRB5KRB_ERR_RESPONSE_TOO_BIG (-1765328332L)
50 #endif
51
52 /* Heimdal uses a slightly different name */
53 #if defined(HAVE_ENCTYPE_ARCFOUR_HMAC_MD5) && !defined(HAVE_ENCTYPE_ARCFOUR_HMAC)
54 #define ENCTYPE_ARCFOUR_HMAC ENCTYPE_ARCFOUR_HMAC_MD5
55 #endif
56 #if defined(HAVE_ENCTYPE_ARCFOUR_HMAC_MD5_56) && !defined(HAVE_ENCTYPE_ARCFOUR_HMAC_EXP)
57 #define ENCTYPE_ARCFOUR_HMAC_EXP ENCTYPE_ARCFOUR_HMAC_MD5_56
58 #endif
59
60 /* The older versions of heimdal that don't have this
61    define don't seem to use it anyway.  I'm told they
62    always use a subkey */
63 #ifndef HAVE_AP_OPTS_USE_SUBKEY
64 #define AP_OPTS_USE_SUBKEY 0
65 #endif
66
67 #ifndef KRB5_PW_SALT
68 #define KRB5_PW_SALT 3
69 #endif
70
71 /* CKSUMTYPE_HMAC_MD5 in Heimdal
72    CKSUMTYPE_HMAC_MD5_ARCFOUR in MIT */
73 #if defined(CKSUMTYPE_HMAC_MD5_ARCFOUR) && !defined(CKSUMTYPE_HMAC_MD5)
74 #define CKSUMTYPE_HMAC_MD5 CKSUMTYPE_HMAC_MD5_ARCFOUR
75 #endif
76
77 typedef struct {
78 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
79         krb5_address **addrs;
80 #elif defined(HAVE_KRB5_ADDRESSES) /* Heimdal */
81         krb5_addresses *addrs;
82 #else
83 #error UNKNOWN_KRB5_ADDRESS_TYPE
84 #endif /* defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) */
85 } smb_krb5_addresses;
86
87 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY               /* MIT */
88 #define KRB5_KT_KEY(k)          (&(k)->key)
89 #elif HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK          /* Heimdal */
90 #define KRB5_KT_KEY(k)          (&(k)->keyblock)
91 #else
92 #error krb5_keytab_entry has no key or keyblock member
93 #endif /* HAVE_KRB5_KEYTAB_ENTRY_KEY */
94
95 /* work around broken krb5.h on sles9 */
96 #ifdef SIZEOF_LONG
97 #undef SIZEOF_LONG
98 #endif
99
100 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
101 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
102 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
103 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
104 #define KRB5_KEY_DATA_CAST      void
105 #else /* MIT */
106 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
107 #define KRB5_KEY_LENGTH(k)      ((k)->length)
108 #define KRB5_KEY_DATA(k)        ((k)->contents)
109 #define KRB5_KEY_DATA_CAST      krb5_octet
110 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
111
112 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
113 #define KRB5_ERROR_CODE(k)      ((k)->error_code)
114 #else /* MIT */
115 #define KRB5_ERROR_CODE(k)      ((k)->error)
116 #endif /* HAVE_E_DATA_POINTER_IN_KRB5_ERROR */
117
118 krb5_error_code smb_krb5_parse_name(krb5_context context,
119                                 const char *name, /* in unix charset */
120                                 krb5_principal *principal);
121
122 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
123                                       krb5_context context,
124                                       krb5_const_principal principal,
125                                       char **unix_name);
126
127 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc);
128
129 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
130 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock *keyblock);
131 #endif
132
133 #ifndef HAVE_KRB5_FREE_UNPARSED_NAME
134 void krb5_free_unparsed_name(krb5_context ctx, char *val);
135 #endif
136
137 /* Stub out initialize_krb5_error_table since it is not present in all
138  * Kerberos implementations. If it's not present, it's not necessary to
139  * call it.
140  */
141 #ifndef HAVE_INITIALIZE_KRB5_ERROR_TABLE
142 #define initialize_krb5_error_table()
143 #endif
144
145 /* Samba wrapper functions for krb5 functionality. */
146 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr);
147
148 krb5_error_code get_kerberos_allowed_etypes(krb5_context context, krb5_enctype **enctypes);
149 bool get_krb5_smb_session_key(TALLOC_CTX *mem_ctx,
150                               krb5_context context,
151                               krb5_auth_context auth_context,
152                               DATA_BLOB *session_key, bool remote);
153 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry);
154 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype);
155 bool kerberos_compatible_enctypes(krb5_context context, krb5_enctype enctype1, krb5_enctype enctype2);
156 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata);
157 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
158                                             const char *name,
159                                             krb5_principal *principal);
160 bool smb_krb5_principal_compare_any_realm(krb5_context context,
161                                           krb5_const_principal princ1,
162                                           krb5_const_principal princ2);
163 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, const char *client_string, const char *service_string, time_t *expire_time);
164 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
165                                                   const char *netbios_name);
166 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr);
167 NTSTATUS krb5_to_nt_status(krb5_error_code kerberos_error);
168 krb5_error_code nt_status_to_krb5(NTSTATUS nt_status);
169 void smb_krb5_free_error(krb5_context context, krb5_error *krberror);
170 krb5_error_code handle_krberror_packet(krb5_context context,
171                                          krb5_data *packet);
172
173 void smb_krb5_get_init_creds_opt_free(krb5_context context,
174                                     krb5_get_init_creds_opt *opt);
175 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
176                                     krb5_get_init_creds_opt **opt);
177 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry);
178 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
179                                             krb5_enctype enctype,
180                                             char **etype_s);
181 krb5_error_code smb_krb5_open_keytab(krb5_context context,
182                                       const char *keytab_name,
183                                       bool write_access,
184                                       krb5_keytab *keytab);
185 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
186                                      krb5_context context,
187                                      krb5_keytab keytab,
188                                      const char **keytab_name);
189 krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
190                                                         krb5_keytab keytab,
191                                                         krb5_kvno kvno,
192                                                         const char *princ_s,
193                                                         krb5_principal princ,
194                                                         bool flush,
195                                                         bool keep_old_entries);
196 krb5_error_code smb_krb5_kt_add_entry(krb5_context context,
197                                       krb5_keytab keytab,
198                                       krb5_kvno kvno,
199                                       const char *princ_s,
200                                       const char *salt_principal,
201                                       krb5_enctype enctype,
202                                       krb5_data *password,
203                                       bool no_salt,
204                                       bool keep_old_entries);
205
206 krb5_error_code smb_krb5_get_credentials(krb5_context context,
207                                          krb5_ccache ccache,
208                                          krb5_principal me,
209                                          krb5_principal server,
210                                          krb5_principal impersonate_princ,
211                                          krb5_creds **out_creds);
212 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
213                                                 krb5_enctype enctype,
214                                                 const void *data,
215                                                 size_t length,
216                                                 krb5_keyblock *key);
217 krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc,
218                                            krb5_principal principal,
219                                            krb5_keyblock *keyblock,
220                                            const char *target_service,
221                                            krb5_get_init_creds_opt *krb_options,
222                                            time_t *expire_time,
223                                            time_t *kdc_time);
224 krb5_error_code kerberos_kinit_password_cc(krb5_context ctx,
225                                            krb5_ccache cc,
226                                            krb5_principal principal,
227                                            const char *password,
228                                            const char *target_service,
229                                            krb5_get_init_creds_opt *krb_options,
230                                            time_t *expire_time,
231                                            time_t *kdc_time);
232 #ifdef SAMBA4_USES_HEIMDAL
233 krb5_error_code kerberos_kinit_s4u2_cc(krb5_context ctx,
234                                         krb5_ccache store_cc,
235                                         krb5_principal init_principal,
236                                         const char *init_password,
237                                         krb5_principal impersonate_principal,
238                                         const char *self_service,
239                                         const char *target_service,
240                                         krb5_get_init_creds_opt *krb_options,
241                                         time_t *expire_time,
242                                         time_t *kdc_time);
243 #endif
244
245 #if defined(HAVE_KRB5_MAKE_PRINCIPAL)
246 #define smb_krb5_make_principal krb5_make_principal
247 #elif defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
248 krb5_error_code smb_krb5_make_principal(krb5_context context,
249                                         krb5_principal *principal,
250                                         const char *realm, ...);
251 #else
252 #error krb5_make_principal not available
253 #endif
254
255 #if defined(HAVE_KRB5_CC_GET_LIFETIME)
256 #define smb_krb5_cc_get_lifetime krb5_cc_get_lifetime
257 #elif defined(HAVE_KRB5_CC_RETRIEVE_CRED)
258 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
259                                          krb5_ccache id,
260                                          time_t *t);
261 #else
262 #error krb5_cc_get_lifetime not available
263 #endif
264
265 #if defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS)
266 #define smb_krb5_free_checksum_contents krb5_free_checksum_contents
267 #elif defined (HAVE_FREE_CHECKSUM)
268 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum);
269 #else
270 #error krb5_free_checksum_contents/free_Checksum is not vailable
271 #endif
272
273 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
274                                            DATA_BLOB *pac_data,
275                                            krb5_context context,
276                                            const krb5_keyblock *keyblock,
277                                            uint32_t *sig_type,
278                                            DATA_BLOB *sig_blob);
279
280 char *smb_krb5_principal_get_realm(krb5_context context,
281                                    krb5_const_principal principal);
282
283 void smb_krb5_principal_set_type(krb5_context context,
284                                  krb5_principal principal,
285                                  int type);
286
287 krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
288                                              krb5_principal principal,
289                                              const char *realm);
290
291 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
292                                                    const char *service,
293                                                    const char *remote_name,
294                                                    const char *default_realm);
295
296 char *smb_get_krb5_error_message(krb5_context context,
297                                  krb5_error_code code,
298                                  TALLOC_CTX *mem_ctx);
299
300 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
301                            DATA_BLOB *edata,
302                            DATA_BLOB *edata_out);
303
304
305 krb5_error_code kt_copy(krb5_context context,
306                         const char *from,
307                         const char *to);
308 krb5_error_code kt_copy_one_principal(krb5_context context,
309                                       const char *from,
310                                       const char *to,
311                                       const char *principal,
312                                       krb5_kvno kvno,
313                                       const krb5_enctype *enctypes);
314
315 #if defined(HAVE_KRB5_KT_COMPARE)
316 #define smb_krb5_kt_compare krb5_kt_compare
317 #else
318 krb5_boolean smb_krb5_kt_compare(krb5_context context,
319                                  krb5_keytab_entry *entry,
320                                  krb5_const_principal principal,
321                                  krb5_kvno vno,
322                                  krb5_enctype enctype);
323 #endif
324
325 const krb5_enctype *samba_all_enctypes(void);
326
327 uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum);
328 krb5_enctype ms_suptype_to_ietf_enctype(uint32_t enctype_bitmap);
329 krb5_error_code ms_suptypes_to_ietf_enctypes(TALLOC_CTX *mem_ctx,
330                                              uint32_t enctype_bitmap,
331                                              krb5_enctype **enctypes);
332 int smb_krb5_get_pw_salt(krb5_context context,
333                          krb5_const_principal host_princ,
334                          krb5_data *psalt);
335
336 int smb_krb5_create_key_from_string(krb5_context context,
337                                     krb5_const_principal host_princ,
338                                     krb5_data *salt,
339                                     krb5_data *password,
340                                     krb5_enctype enctype,
341                                     krb5_keyblock *key);
342
343 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context);
344
345 #ifndef krb5_princ_size
346 #if defined(HAVE_KRB5_PRINCIPAL_GET_NUM_COMP)
347 #define krb5_princ_size krb5_principal_get_num_comp
348 #else
349 #error krb5_princ_size unavailable
350 #endif
351 #endif
352
353 char *smb_krb5_principal_get_comp_string(TALLOC_CTX *mem_ctx,
354                                          krb5_context context,
355                                          krb5_const_principal principal,
356                                          unsigned int component);
357
358 krb5_error_code krb5_copy_data_contents(krb5_data *p,
359                                         const void *data,
360                                         size_t len);
361
362 int smb_krb5_principal_get_type(krb5_context context,
363                                 krb5_const_principal principal);
364
365 #if !defined(HAVE_KRB5_WARNX)
366 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...);
367 #endif
368
369 #endif /* HAVE_KRB5 */
370
371 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
372                         const char *principal, time_t time_offset,
373                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
374                         uint32_t extra_ap_opts, const char *ccname,
375                         time_t *tgs_expire,
376                         const char *impersonate_princ_s);
377
378 #endif /* _KRB5_SAMBA_H */