2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Guenther Deschner 2003
8 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "system/kerberos.h"
27 #include "libcli/auth/kerberos.h"
29 #include "lib/ldb/include/ldb.h"
35 #if !defined(HAVE_KRB5_PRINC_COMPONENT)
36 const krb5_data
*krb5_princ_component(krb5_context
, krb5_principal
, int );
38 static DATA_BLOB
unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
)
41 DATA_BLOB pac_contents
= data_blob(NULL
, 0);
42 struct asn1_data data
;
44 if (!auth_data
->length
) {
45 return data_blob(NULL
, 0);
48 asn1_load(&data
, *auth_data
);
49 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
50 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
51 asn1_start_tag(&data
, ASN1_CONTEXT(0));
52 asn1_read_Integer(&data
, &data_type
);
54 asn1_start_tag(&data
, ASN1_CONTEXT(1));
55 asn1_read_OctetString(&data
, &pac_contents
);
61 out
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
63 data_blob_free(&pac_contents
);
68 /**********************************************************************************
69 Try to verify a ticket using the system keytab... the system keytab has kvno -1 entries, so
70 it's more like what microsoft does... see comment in utils/net_ads.c in the
71 ads_keytab_add_entry function for details.
72 ***********************************************************************************/
74 static krb5_error_code
ads_keytab_verify_ticket(TALLOC_CTX
*mem_ctx
, krb5_context context
,
75 krb5_auth_context auth_context
,
77 const DATA_BLOB
*ticket
, krb5_data
*p_packet
,
79 krb5_keyblock
*keyblock
)
81 krb5_error_code ret
= 0;
83 krb5_keytab keytab
= NULL
;
86 fstring my_Fqdn
, my_NAME
;
88 char *host_princ_s
[18];
89 krb5_principal host_princ
;
92 ret
= krb5_kt_default(context
, &keytab
);
94 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_default failed (%s)\n", error_message(ret
)));
98 /* Generate the list of principal names which we expect clients might
99 * want to use for authenticating to the file service. */
101 fstrcpy(my_name
, lp_netbios_name());
104 fstrcpy(my_NAME
, lp_netbios_name());
107 my_fqdn
= name_to_fqdn(mem_ctx
, lp_netbios_name());
110 p_fqdn
= strchr_m(my_fqdn
, '.');
111 fstrcpy(my_Fqdn
, my_NAME
);
113 fstrcat(my_Fqdn
, p_fqdn
);
116 asprintf(&host_princ_s
[0], "%s$@%s", my_name
, lp_realm());
117 asprintf(&host_princ_s
[1], "%s$@%s", my_NAME
, lp_realm());
118 asprintf(&host_princ_s
[2], "host/%s@%s", my_name
, lp_realm());
119 asprintf(&host_princ_s
[3], "host/%s@%s", my_NAME
, lp_realm());
120 asprintf(&host_princ_s
[4], "host/%s@%s", my_fqdn
, lp_realm());
121 asprintf(&host_princ_s
[5], "host/%s@%s", my_Fqdn
, lp_realm());
122 asprintf(&host_princ_s
[6], "HOST/%s@%s", my_name
, lp_realm());
123 asprintf(&host_princ_s
[7], "HOST/%s@%s", my_NAME
, lp_realm());
124 asprintf(&host_princ_s
[8], "HOST/%s@%s", my_fqdn
, lp_realm());
125 asprintf(&host_princ_s
[9], "HOST/%s@%s", my_Fqdn
, lp_realm());
126 asprintf(&host_princ_s
[10], "%s/%s@%s", service
, my_name
, lp_realm());
127 asprintf(&host_princ_s
[11], "%s/%s@%s", service
, my_NAME
, lp_realm());
128 asprintf(&host_princ_s
[12], "%s/%s@%s", service
, my_fqdn
, lp_realm());
129 asprintf(&host_princ_s
[13], "%s/%s@%s", service
, my_Fqdn
, lp_realm());
130 asprintf(&host_princ_s
[14], "%s/%s@%s", strupper_talloc(mem_ctx
, service
), my_name
, lp_realm());
131 asprintf(&host_princ_s
[15], "%s/%s@%s", strupper_talloc(mem_ctx
, service
), my_NAME
, lp_realm());
132 asprintf(&host_princ_s
[16], "%s/%s@%s", strupper_talloc(mem_ctx
, service
), my_fqdn
, lp_realm());
133 asprintf(&host_princ_s
[17], "%s/%s@%s", strupper_talloc(mem_ctx
, service
), my_Fqdn
, lp_realm());
135 /* Now try to verify the ticket using the key associated with each of
136 * the principals which we think clients will expect us to be
137 * participating as. */
138 for (i
= 0; i
< sizeof(host_princ_s
) / sizeof(host_princ_s
[0]); i
++) {
140 ret
= krb5_parse_name(context
, host_princ_s
[i
], &host_princ
);
142 DEBUG(1, ("ads_keytab_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
143 host_princ_s
[i
], error_message(ret
)));
146 p_packet
->length
= ticket
->length
;
147 p_packet
->data
= (krb5_pointer
)ticket
->data
;
149 ret
= krb5_rd_req(context
, &auth_context
, p_packet
, host_princ
, keytab
, NULL
, pp_tkt
);
150 krb5_free_principal(context
, host_princ
);
152 DEBUG(0, ("krb5_rd_req(%s) failed: %s\n", host_princ_s
[i
], error_message(ret
)));
154 DEBUG(10,("krb5_rd_req succeeded for principal %s\n", host_princ_s
[i
]));
160 for (i
= 0; i
< sizeof(host_princ_s
) / sizeof(host_princ_s
[0]); i
++) {
161 SAFE_FREE(host_princ_s
[i
]);
167 krb5_kt_close(context
, keytab
);
173 /**********************************************************************************
174 Try to verify a ticket using the secrets.tdb.
175 ***********************************************************************************/
177 static krb5_error_code
ads_secrets_verify_ticket(TALLOC_CTX
*mem_ctx
, krb5_context context
,
178 krb5_auth_context auth_context
,
179 krb5_principal host_princ
,
180 const DATA_BLOB
*ticket
, krb5_data
*p_packet
,
181 krb5_ticket
**pp_tkt
,
182 krb5_keyblock
*keyblock
)
184 krb5_error_code ret
= 0;
186 krb5_enctype
*enctypes
= NULL
;
188 const struct ldb_val
*password_v
;
189 struct ldb_wrap
*ldb
;
191 struct ldb_message
**msgs
;
192 const char *base_dn
= SECRETS_PRIMARY_DOMAIN_DN
;
193 const char *attrs
[] = {
198 ZERO_STRUCTP(keyblock
);
200 /* Local secrets are stored in secrets.ldb */
201 ldb
= secrets_db_connect(mem_ctx
);
206 /* search for the secret record */
207 ldb_ret
= gendb_search(ldb
,
208 mem_ctx
, base_dn
, &msgs
, attrs
,
209 SECRETS_PRIMARY_REALM_FILTER
,
212 DEBUG(1, ("Could not find domain join record for %s\n",
215 } else if (ldb_ret
!= 1) {
216 DEBUG(1, ("Found %d records matching cn=%s under DN %s\n", ldb_ret
,
217 lp_realm(), base_dn
));
221 password_v
= ldb_msg_find_ldb_val(msgs
[0], "secret");
223 password
.data
= password_v
->data
;
224 password
.length
= password_v
->length
;
226 /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
228 if ((ret
= get_kerberos_allowed_etypes(context
, &enctypes
))) {
229 DEBUG(1,("ads_secrets_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n",
230 error_message(ret
)));
234 p_packet
->length
= ticket
->length
;
235 p_packet
->data
= (krb5_pointer
)ticket
->data
;
237 ret
= KRB5_BAD_ENCTYPE
;
238 /* We need to setup a auth context with each possible encoding type in turn. */
239 for (i
=0;enctypes
[i
];i
++) {
240 krb5_error_code our_ret
;
241 our_ret
= create_kerberos_key_from_string(context
, host_princ
, &password
, keyblock
, enctypes
[i
]);
247 krb5_auth_con_setuseruserkey(context
, auth_context
, keyblock
);
249 our_ret
= krb5_rd_req(context
, &auth_context
, p_packet
,
253 DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
254 (unsigned int)enctypes
[i
] ));
259 krb5_free_keyblock_contents(context
, keyblock
);
261 DEBUG((our_ret
!= KRB5_BAD_ENCTYPE
) ? 3 : 10,
262 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
263 (unsigned int)enctypes
[i
], error_message(our_ret
)));
265 if (our_ret
!= KRB5_BAD_ENCTYPE
) {
272 free_kerberos_etypes(context
, enctypes
);
277 /**********************************************************************************
278 Verify an incoming ticket and parse out the principal name and
279 authorization_data if available.
280 ***********************************************************************************/
282 NTSTATUS
ads_verify_ticket(TALLOC_CTX
*mem_ctx
,
283 krb5_context context
,
284 krb5_auth_context auth_context
,
285 const char *realm
, const char *service
,
286 const DATA_BLOB
*ticket
,
287 char **principal
, DATA_BLOB
*auth_data
,
289 krb5_keyblock
*keyblock
)
291 NTSTATUS sret
= NT_STATUS_LOGON_FAILURE
;
293 krb5_ticket
*tkt
= NULL
;
294 krb5_rcache rcache
= NULL
;
297 krb5_principal host_princ
= NULL
;
298 char *host_princ_s
= NULL
;
299 BOOL got_replay_mutex
= False
;
301 char *malloc_principal
;
304 ZERO_STRUCTP(auth_data
);
305 ZERO_STRUCTP(ap_rep
);
307 /* This whole process is far more complex than I would
308 like. We have to go through all this to allow us to store
309 the secret internally, instead of using /etc/krb5.keytab */
311 asprintf(&host_princ_s
, "%s$", lp_netbios_name());
312 strlower_m(host_princ_s
);
313 ret
= krb5_parse_name(context
, host_princ_s
, &host_princ
);
315 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
316 host_princ_s
, error_message(ret
)));
321 /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
322 * code surrounding the replay cache... */
324 if (!grab_server_mutex("replay cache mutex")) {
325 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
329 got_replay_mutex
= True
;
332 * JRA. We must set the rcache here. This will prevent replay attacks.
335 ret
= krb5_get_server_rcache(context
, krb5_princ_component(context
, host_princ
, 0), &rcache
);
337 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret
)));
341 ret
= krb5_auth_con_setrcache(context
, auth_context
, rcache
);
343 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret
)));
347 ret
= ads_keytab_verify_ticket(mem_ctx
, context
, auth_context
,
348 service
, ticket
, &packet
, &tkt
, keyblock
);
350 DEBUG(10, ("ads_secrets_verify_ticket: using host principal: [%s]\n", host_princ_s
));
351 ret
= ads_secrets_verify_ticket(mem_ctx
, context
, auth_context
,
353 &packet
, &tkt
, keyblock
);
356 release_server_mutex();
357 got_replay_mutex
= False
;
360 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n",
361 error_message(ret
)));
365 ret
= krb5_mk_rep(context
, auth_context
, &packet
);
367 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
368 error_message(ret
)));
372 *ap_rep
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
373 SAFE_FREE(packet
.data
);
377 file_save("/tmp/ticket.dat", ticket
->data
, ticket
->length
);
380 *auth_data
= get_auth_data_from_tkt(mem_ctx
, tkt
);
382 *auth_data
= unwrap_pac(mem_ctx
, auth_data
);
385 if (tkt
->enc_part2
) {
386 file_save("/tmp/authdata.dat",
387 tkt
->enc_part2
->authorization_data
[0]->contents
,
388 tkt
->enc_part2
->authorization_data
[0]->length
);
392 if ((ret
= krb5_unparse_name(context
, get_principal_from_tkt(tkt
),
393 &malloc_principal
))) {
394 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n",
395 error_message(ret
)));
396 sret
= NT_STATUS_LOGON_FAILURE
;
400 *principal
= talloc_strdup(mem_ctx
, malloc_principal
);
401 SAFE_FREE(malloc_principal
);
403 DEBUG(3,("ads_verify_ticket: talloc_strdup() failed\n"));
404 sret
= NT_STATUS_NO_MEMORY
;
412 if (got_replay_mutex
) {
413 release_server_mutex();
416 if (!NT_STATUS_IS_OK(sret
)) {
417 data_blob_free(auth_data
);
420 if (!NT_STATUS_IS_OK(sret
)) {
421 data_blob_free(ap_rep
);
425 krb5_free_principal(context
, host_princ
);
429 krb5_free_ticket(context
, tkt
);
432 SAFE_FREE(host_princ_s
);
437 #endif /* HAVE_KRB5 */