s4-auth-krb: Move function to db-glue.c and make it static.
[Samba/vl.git] / source4 / auth / kerberos / kerberos_util.c
blobf38bc17212782e84d880e932a784929cec56f5f3
1 /*
2 Unix SMB/CIFS implementation.
4 Kerberos utility functions for GENSEC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
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/>.
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_proto.h"
28 #include "auth/credentials/credentials_krb5.h"
29 #include "auth/kerberos/kerberos_credentials.h"
30 #include "auth/kerberos/kerberos_util.h"
32 krb5_error_code free_principal(struct principal_container *pc)
34 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
35 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
37 return 0;
41 krb5_error_code parse_principal(TALLOC_CTX *parent_ctx,
42 const char *princ_string,
43 struct smb_krb5_context *smb_krb5_context,
44 krb5_principal *princ,
45 const char **error_string)
47 int ret;
48 struct principal_container *mem_ctx;
49 if (princ_string == NULL) {
50 *princ = NULL;
51 return 0;
54 ret = krb5_parse_name(smb_krb5_context->krb5_context,
55 princ_string, princ);
57 if (ret) {
58 (*error_string) = smb_get_krb5_error_message(
59 smb_krb5_context->krb5_context,
60 ret, parent_ctx);
61 return ret;
64 mem_ctx = talloc(parent_ctx, struct principal_container);
65 if (!mem_ctx) {
66 (*error_string) = error_message(ENOMEM);
67 return ENOMEM;
70 /* This song-and-dance effectivly puts the principal
71 * into talloc, so we can't loose it. */
72 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx,
73 smb_krb5_context);
74 mem_ctx->principal = *princ;
75 talloc_set_destructor(mem_ctx, free_principal);
76 return 0;
79 /* Obtain the principal set on this context. Requires a
80 * smb_krb5_context because we are doing krb5 principal parsing with
81 * the library routines. The returned princ is placed in the talloc
82 * system by means of a destructor (do *not* free). */
84 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
85 struct cli_credentials *credentials,
86 struct smb_krb5_context *smb_krb5_context,
87 krb5_principal *princ,
88 enum credentials_obtained *obtained,
89 const char **error_string)
91 krb5_error_code ret;
92 const char *princ_string;
93 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
94 *obtained = CRED_UNINITIALISED;
96 if (!mem_ctx) {
97 (*error_string) = error_message(ENOMEM);
98 return ENOMEM;
100 princ_string = cli_credentials_get_principal_and_obtained(credentials,
101 mem_ctx,
102 obtained);
103 if (!princ_string) {
104 *princ = NULL;
105 return 0;
108 ret = parse_principal(parent_ctx, princ_string,
109 smb_krb5_context, princ, error_string);
110 talloc_free(mem_ctx);
111 return ret;
114 /* Obtain the principal set on this context. Requires a
115 * smb_krb5_context because we are doing krb5 principal parsing with
116 * the library routines. The returned princ is placed in the talloc
117 * system by means of a destructor (do *not* free). */
119 static krb5_error_code impersonate_principal_from_credentials(
120 TALLOC_CTX *parent_ctx,
121 struct cli_credentials *credentials,
122 struct smb_krb5_context *smb_krb5_context,
123 krb5_principal *princ,
124 const char **error_string)
126 return parse_principal(parent_ctx,
127 cli_credentials_get_impersonate_principal(credentials),
128 smb_krb5_context, princ, error_string);
132 * Return a freshly allocated ccache (destroyed by destructor on child
133 * of parent_ctx), for a given set of client credentials
136 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
137 struct cli_credentials *credentials,
138 struct smb_krb5_context *smb_krb5_context,
139 struct tevent_context *event_ctx,
140 krb5_ccache ccache,
141 enum credentials_obtained *obtained,
142 const char **error_string)
144 krb5_error_code ret;
145 const char *password;
146 const char *self_service;
147 const char *target_service;
148 time_t kdc_time = 0;
149 krb5_principal princ;
150 krb5_principal impersonate_principal;
151 int tries;
152 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
153 krb5_get_init_creds_opt *krb_options;
155 if (!mem_ctx) {
156 (*error_string) = strerror(ENOMEM);
157 return ENOMEM;
160 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ, obtained, error_string);
161 if (ret) {
162 talloc_free(mem_ctx);
163 return ret;
166 if (princ == NULL) {
167 (*error_string) = talloc_asprintf(credentials, "principal, username or realm was not specified in the credentials");
168 talloc_free(mem_ctx);
169 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
172 ret = impersonate_principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &impersonate_principal, error_string);
173 if (ret) {
174 talloc_free(mem_ctx);
175 return ret;
178 self_service = cli_credentials_get_self_service(credentials);
179 target_service = cli_credentials_get_target_service(credentials);
181 password = cli_credentials_get_password(credentials);
183 /* setup the krb5 options we want */
184 if ((ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options))) {
185 (*error_string) = talloc_asprintf(credentials, "krb5_get_init_creds_opt_alloc failed (%s)\n",
186 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
187 ret, mem_ctx));
188 talloc_free(mem_ctx);
189 return ret;
192 /* get the defaults */
193 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context->krb5_context, NULL, NULL, krb_options);
195 /* set if we want a forwardable ticket */
196 switch (cli_credentials_get_krb_forwardable(credentials)) {
197 case CRED_AUTO_KRB_FORWARDABLE:
198 break;
199 case CRED_NO_KRB_FORWARDABLE:
200 krb5_get_init_creds_opt_set_forwardable(krb_options, FALSE);
201 break;
202 case CRED_FORCE_KRB_FORWARDABLE:
203 krb5_get_init_creds_opt_set_forwardable(krb_options, TRUE);
204 break;
208 * In order to work against windows KDCs even if we use
209 * the netbios domain name as realm, we need to add the following
210 * flags:
211 * KRB5_INIT_CREDS_NO_C_CANON_CHECK;
212 * KRB5_INIT_CREDS_NO_C_NO_EKU_CHECK;
214 krb5_get_init_creds_opt_set_win2k(smb_krb5_context->krb5_context,
215 krb_options, true);
217 tries = 2;
218 while (tries--) {
219 struct tevent_context *previous_ev;
220 /* Do this every time, in case we have weird recursive issues here */
221 ret = smb_krb5_context_set_event_ctx(smb_krb5_context, event_ctx, &previous_ev);
222 if (ret) {
223 talloc_free(mem_ctx);
224 return ret;
226 if (password) {
227 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
228 princ, password,
229 impersonate_principal,
230 self_service,
231 target_service,
232 krb_options,
233 NULL, &kdc_time);
234 } else if (impersonate_principal) {
235 talloc_free(mem_ctx);
236 (*error_string) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
237 return EINVAL;
238 } else {
239 /* No password available, try to use a keyblock instead */
241 krb5_keyblock keyblock;
242 const struct samr_Password *mach_pwd;
243 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
244 if (!mach_pwd) {
245 talloc_free(mem_ctx);
246 (*error_string) = "kinit_to_ccache: No password available for kinit\n";
247 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
248 smb_krb5_context_remove_event_ctx(smb_krb5_context, previous_ev, event_ctx);
249 return EINVAL;
251 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
252 ENCTYPE_ARCFOUR_HMAC,
253 mach_pwd->hash, sizeof(mach_pwd->hash),
254 &keyblock);
256 if (ret == 0) {
257 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
258 princ, &keyblock,
259 target_service, krb_options,
260 NULL, &kdc_time);
261 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
265 smb_krb5_context_remove_event_ctx(smb_krb5_context, previous_ev, event_ctx);
267 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
268 /* Perhaps we have been given an invalid skew, so try again without it */
269 time_t t = time(NULL);
270 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
271 } else {
272 /* not a skew problem */
273 break;
277 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
279 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
280 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
281 cli_credentials_get_principal(credentials, mem_ctx),
282 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
283 ret, mem_ctx));
284 talloc_free(mem_ctx);
285 return ret;
288 /* cope with ticket being in the future due to clock skew */
289 if ((unsigned)kdc_time > time(NULL)) {
290 time_t t = time(NULL);
291 int time_offset =(unsigned)kdc_time-t;
292 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
293 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
296 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
297 ret = kinit_to_ccache(parent_ctx,
298 credentials,
299 smb_krb5_context,
300 event_ctx,
301 ccache, obtained,
302 error_string);
305 if (ret) {
306 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
307 cli_credentials_get_principal(credentials, mem_ctx),
308 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
309 ret, mem_ctx));
310 talloc_free(mem_ctx);
311 return ret;
313 talloc_free(mem_ctx);
314 return 0;
317 static krb5_error_code free_keytab_container(struct keytab_container *ktc)
319 return krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
322 krb5_error_code smb_krb5_get_keytab_container(TALLOC_CTX *mem_ctx,
323 struct smb_krb5_context *smb_krb5_context,
324 const char *keytab_name,
325 struct keytab_container **ktc)
327 krb5_keytab keytab;
328 krb5_error_code ret;
329 ret = krb5_kt_resolve(smb_krb5_context->krb5_context,
330 keytab_name, &keytab);
331 if (ret) {
332 DEBUG(1,("failed to open krb5 keytab: %s\n",
333 smb_get_krb5_error_message(
334 smb_krb5_context->krb5_context,
335 ret, mem_ctx)));
336 return ret;
339 *ktc = talloc(mem_ctx, struct keytab_container);
340 if (!*ktc) {
341 return ENOMEM;
344 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
345 (*ktc)->keytab = keytab;
346 talloc_set_destructor(*ktc, free_keytab_container);
348 return 0;