krb5samba: Add compat krb5_make_principal for MIT build
[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
57 /* The older versions of heimdal that don't have this
58    define don't seem to use it anyway.  I'm told they
59    always use a subkey */
60 #ifndef HAVE_AP_OPTS_USE_SUBKEY
61 #define AP_OPTS_USE_SUBKEY 0
62 #endif
63
64 #ifndef krb5_princ_size
65 #if defined(HAVE_KRB5_KRB5_PRINCIPAL_GET_NUM_COMP)
66 #define krb5_princ_size krb5_principal_get_num_comp
67 #else
68 #error krb5_princ_size unavailable
69 #endif
70 #endif
71
72 typedef struct {
73 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
74         krb5_address **addrs;
75 #elif defined(HAVE_KRB5_ADDRESSES) /* Heimdal */
76         krb5_addresses *addrs;
77 #else
78 #error UNKNOWN_KRB5_ADDRESS_TYPE
79 #endif /* defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) */
80 } smb_krb5_addresses;
81
82 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY               /* MIT */
83 #define KRB5_KT_KEY(k)          (&(k)->key)
84 #elif HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK          /* Heimdal */
85 #define KRB5_KT_KEY(k)          (&(k)->keyblock)
86 #else
87 #error krb5_keytab_entry has no key or keyblock member
88 #endif /* HAVE_KRB5_KEYTAB_ENTRY_KEY */
89
90 /* work around broken krb5.h on sles9 */
91 #ifdef SIZEOF_LONG
92 #undef SIZEOF_LONG
93 #endif
94
95 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
96 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
97 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
98 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
99 #define KRB5_KEY_DATA_CAST      void
100 #else /* MIT */
101 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
102 #define KRB5_KEY_LENGTH(k)      ((k)->length)
103 #define KRB5_KEY_DATA(k)        ((k)->contents)
104 #define KRB5_KEY_DATA_CAST      krb5_octet
105 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
106
107 krb5_error_code smb_krb5_parse_name(krb5_context context,
108                                 const char *name, /* in unix charset */
109                                 krb5_principal *principal);
110
111 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
112                                       krb5_context context,
113                                       krb5_const_principal principal,
114                                       char **unix_name);
115
116 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc);
117
118 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
119 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock *keyblock);
120 #endif
121
122 #ifndef HAVE_KRB5_FREE_UNPARSED_NAME
123 void krb5_free_unparsed_name(krb5_context ctx, char *val);
124 #endif
125
126 /* Stub out initialize_krb5_error_table since it is not present in all
127  * Kerberos implementations. If it's not present, it's not necessary to
128  * call it.
129  */
130 #ifndef HAVE_INITIALIZE_KRB5_ERROR_TABLE
131 #define initialize_krb5_error_table()
132 #endif
133
134 /* Samba wrapper functions for krb5 functionality. */
135 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr);
136 int create_kerberos_key_from_string(krb5_context context,
137                                     krb5_principal host_princ,
138                                     krb5_data *password,
139                                     krb5_keyblock *key,
140                                     krb5_enctype enctype,
141                                     bool no_salt);
142 int create_kerberos_key_from_string_direct(krb5_context context,
143                                            krb5_principal host_princ,
144                                            krb5_data *password,
145                                            krb5_keyblock *key,
146                                            krb5_enctype enctype);
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 kpasswd_err_to_krb5_err(krb5_error_code res_code);
165 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
166                                                   const char *netbios_name);
167 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr);
168 NTSTATUS krb5_to_nt_status(krb5_error_code kerberos_error);
169 krb5_error_code nt_status_to_krb5(NTSTATUS nt_status);
170 void smb_krb5_free_error(krb5_context context, krb5_error *krberror);
171 krb5_error_code handle_krberror_packet(krb5_context context,
172                                          krb5_data *packet);
173
174 void smb_krb5_get_init_creds_opt_free(krb5_context context,
175                                     krb5_get_init_creds_opt *opt);
176 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
177                                     krb5_get_init_creds_opt **opt);
178 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry);
179 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
180                                             krb5_enctype enctype,
181                                             char **etype_s);
182 krb5_error_code smb_krb5_open_keytab(krb5_context context,
183                                       const char *keytab_name,
184                                       bool write_access,
185                                       krb5_keytab *keytab);
186 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
187                                      krb5_context context,
188                                      krb5_keytab keytab,
189                                      const char **keytab_name);
190 krb5_error_code smb_krb5_get_credentials(krb5_context context,
191                                          krb5_ccache ccache,
192                                          krb5_principal me,
193                                          krb5_principal server,
194                                          krb5_principal impersonate_princ,
195                                          krb5_creds **out_creds);
196 krb5_error_code smb_krb5_get_creds(const char *server_s,
197                                    time_t time_offset,
198                                    const char *cc,
199                                    const char *impersonate_princ_s,
200                                    krb5_creds **creds_p);
201 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
202                                                 krb5_enctype enctype,
203                                                 const void *data,
204                                                 size_t length,
205                                                 krb5_keyblock *key);
206 krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc,
207                                            krb5_principal principal,
208                                            krb5_keyblock *keyblock,
209                                            const char *target_service,
210                                            krb5_get_init_creds_opt *krb_options,
211                                            time_t *expire_time,
212                                            time_t *kdc_time);
213 krb5_error_code kerberos_kinit_password_cc(krb5_context ctx,
214                                            krb5_ccache cc,
215                                            krb5_principal principal,
216                                            const char *password,
217                                            const char *target_service,
218                                            krb5_get_init_creds_opt *krb_options,
219                                            time_t *expire_time,
220                                            time_t *kdc_time);
221 #ifdef SAMBA4_USES_HEIMDAL
222 krb5_error_code kerberos_kinit_s4u2_cc(krb5_context ctx,
223                                         krb5_ccache store_cc,
224                                         krb5_principal init_principal,
225                                         const char *init_password,
226                                         krb5_principal impersonate_principal,
227                                         const char *self_service,
228                                         const char *target_service,
229                                         krb5_get_init_creds_opt *krb_options,
230                                         time_t *expire_time,
231                                         time_t *kdc_time);
232 #endif
233
234 #if defined(HAVE_KRB5_MAKE_PRINCIPAL)
235 #define smb_krb5_make_principal krb5_make_principal
236 #elif defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
237 krb5_error_code smb_krb5_make_principal(krb5_context context,
238                                         krb5_principal *principal,
239                                         const char *realm, ...);
240 #else
241 #error krb5_make_principal not available
242 #endif
243 char *smb_krb5_principal_get_realm(krb5_context context,
244                                    krb5_principal principal);
245
246 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
247                                                    const char *service,
248                                                    const char *remote_name,
249                                                    const char *default_realm);
250
251 char *smb_get_krb5_error_message(krb5_context context,
252                                  krb5_error_code code,
253                                  TALLOC_CTX *mem_ctx);
254
255 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
256                            DATA_BLOB *edata,
257                            DATA_BLOB *edata_out);
258
259
260 krb5_error_code kt_copy(krb5_context context,
261                         const char *from,
262                         const char *to);
263 krb5_error_code kt_copy_one_principal(krb5_context context,
264                                       const char *from,
265                                       const char *to,
266                                       const char *principal,
267                                       krb5_kvno kvno,
268                                       krb5_enctype *enctypes);
269
270 #endif /* HAVE_KRB5 */
271
272 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
273                         const char *principal, time_t time_offset,
274                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
275                         uint32_t extra_ap_opts, const char *ccname,
276                         time_t *tgs_expire,
277                         const char *impersonate_princ_s);
278
279 #endif /* _KRB5_SAMBA_H */