s4:kdc: Add asserted identity SID to identify whether S4U2Self has occurred
[Samba.git] / source4 / kdc / wdc-samba4.c
blobd1825c2902c28cd08b63801cb3abf1182aa5dd64
1 /*
2 Unix SMB/CIFS implementation.
4 PAC Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
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.
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.
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 #include "kdc/kdc-glue.h"
26 #include "kdc/db-glue.h"
27 #include "kdc/pac-glue.h"
28 #include "sdb.h"
29 #include "sdb_hdb.h"
30 #include "librpc/gen_ndr/auth.h"
31 #include <krb5_locl.h>
33 static bool samba_wdc_is_s4u2self_req(astgs_request_t r)
35 krb5_kdc_configuration *config = kdc_request_get_config((kdc_request_t)r);
36 const KDC_REQ *req = kdc_request_get_req(r);
37 const PA_DATA *pa_for_user = NULL;
39 if (req->msg_type != krb_tgs_req) {
40 return false;
43 if (config->enable_fast && req->padata != NULL) {
44 const PA_DATA *pa_fx_fast = NULL;
45 int idx = 0;
47 pa_fx_fast = krb5_find_padata(req->padata->val,
48 req->padata->len,
49 KRB5_PADATA_FX_FAST,
50 &idx);
51 if (pa_fx_fast != NULL) {
53 * We're in the outer request
54 * with KRB5_PADATA_FX_FAST
55 * if fast is enabled we'll
56 * process the s4u2self
57 * request only in the
58 * inner request.
60 return false;
64 if (req->padata != NULL) {
65 int idx = 0;
67 pa_for_user = krb5_find_padata(req->padata->val,
68 req->padata->len,
69 KRB5_PADATA_FOR_USER,
70 &idx);
73 if (pa_for_user != NULL) {
74 return true;
77 return false;
81 * Given the right private pointer from hdb_samba4,
82 * get a PAC from the attached ldb messages.
84 * For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
86 static krb5_error_code samba_wdc_get_pac(void *priv,
87 astgs_request_t r,
88 hdb_entry *client,
89 hdb_entry *server,
90 const krb5_keyblock *pk_reply_key,
91 uint64_t pac_attributes,
92 krb5_pac *pac)
94 krb5_context context = kdc_request_get_context((kdc_request_t)r);
95 TALLOC_CTX *mem_ctx;
96 DATA_BLOB *logon_blob = NULL;
97 DATA_BLOB *cred_ndr = NULL;
98 DATA_BLOB **cred_ndr_ptr = NULL;
99 DATA_BLOB _cred_blob = data_blob_null;
100 DATA_BLOB *cred_blob = NULL;
101 DATA_BLOB *upn_blob = NULL;
102 DATA_BLOB *pac_attrs_blob = NULL;
103 DATA_BLOB *requester_sid_blob = NULL;
104 krb5_error_code ret;
105 NTSTATUS nt_status;
106 struct samba_kdc_entry *skdc_entry =
107 talloc_get_type_abort(client->context,
108 struct samba_kdc_entry);
109 bool is_krbtgt;
110 bool is_s4u2self = samba_wdc_is_s4u2self_req(r);
111 enum samba_asserted_identity asserted_identity =
112 (is_s4u2self) ?
113 SAMBA_ASSERTED_IDENTITY_SERVICE :
114 SAMBA_ASSERTED_IDENTITY_AUTHENTICATION_AUTHORITY;
116 mem_ctx = talloc_named(client->context, 0, "samba_get_pac context");
117 if (!mem_ctx) {
118 return ENOMEM;
121 if (pk_reply_key != NULL) {
122 cred_ndr_ptr = &cred_ndr;
125 is_krbtgt = krb5_principal_is_krbtgt(context, server->principal);
127 nt_status = samba_kdc_get_pac_blobs(mem_ctx, skdc_entry,
128 asserted_identity,
129 &logon_blob,
130 cred_ndr_ptr,
131 &upn_blob,
132 is_krbtgt ? &pac_attrs_blob : NULL,
133 pac_attributes,
134 is_krbtgt ? &requester_sid_blob : NULL);
135 if (!NT_STATUS_IS_OK(nt_status)) {
136 talloc_free(mem_ctx);
137 return EINVAL;
140 if (pk_reply_key != NULL && cred_ndr != NULL) {
141 ret = samba_kdc_encrypt_pac_credentials(context,
142 pk_reply_key,
143 cred_ndr,
144 mem_ctx,
145 &_cred_blob);
146 if (ret != 0) {
147 talloc_free(mem_ctx);
148 return ret;
150 cred_blob = &_cred_blob;
153 ret = krb5_pac_init(context, pac);
154 if (ret != 0) {
155 talloc_free(mem_ctx);
156 return ret;
159 ret = samba_make_krb5_pac(context, logon_blob, cred_blob,
160 upn_blob, pac_attrs_blob,
161 requester_sid_blob, NULL, *pac);
163 talloc_free(mem_ctx);
164 return ret;
167 static krb5_error_code samba_wdc_reget_pac2(astgs_request_t r,
168 const krb5_principal delegated_proxy_principal,
169 hdb_entry *client,
170 hdb_entry *server,
171 hdb_entry *krbtgt,
172 krb5_pac *pac,
173 krb5_cksumtype ctype)
175 krb5_context context = kdc_request_get_context((kdc_request_t)r);
176 struct samba_kdc_entry *client_skdc_entry = NULL;
177 struct samba_kdc_entry *server_skdc_entry =
178 talloc_get_type_abort(server->context, struct samba_kdc_entry);
179 struct samba_kdc_entry *krbtgt_skdc_entry =
180 talloc_get_type_abort(krbtgt->context, struct samba_kdc_entry);
181 TALLOC_CTX *mem_ctx = NULL;
182 krb5_pac new_pac = NULL;
183 krb5_error_code ret;
184 bool is_s4u2self = samba_wdc_is_s4u2self_req(r);
185 bool is_in_db = false;
186 bool is_untrusted = false;
187 uint32_t flags = 0;
189 mem_ctx = talloc_named(NULL, 0, "samba_kdc_reget_pac2 context");
190 if (mem_ctx == NULL) {
191 return ENOMEM;
194 if (client != NULL) {
195 client_skdc_entry = talloc_get_type_abort(client->context,
196 struct samba_kdc_entry);
200 * If the krbtgt was generated by an RODC, and we are not that
201 * RODC, then we need to regenerate the PAC - we can't trust
202 * it, and confirm that the RODC was permitted to print this ticket
204 * Becasue of the samba_kdc_validate_pac_blob() step we can be
205 * sure that the record in 'client' matches the SID in the
206 * original PAC.
208 ret = samba_krbtgt_is_in_db(krbtgt_skdc_entry, &is_in_db, &is_untrusted);
209 if (ret != 0) {
210 goto out;
213 if (is_s4u2self) {
214 flags |= SAMBA_KDC_FLAG_PROTOCOL_TRANSITION;
217 if (delegated_proxy_principal != NULL) {
218 krb5_enctype etype;
219 Key *key = NULL;
221 if (!is_in_db) {
223 * The RODC-issued PAC was signed by a KDC entry that we
224 * don't have a key for. The server signature is not
225 * trustworthy, since it could have been created by the
226 * server we got the ticket from. We must not proceed as
227 * otherwise the ticket signature is unchecked.
229 ret = HDB_ERR_NOT_FOUND_HERE;
230 goto out;
233 /* Fetch the correct key depending on the checksum type. */
234 if (ctype == CKSUMTYPE_HMAC_MD5) {
235 etype = ENCTYPE_ARCFOUR_HMAC;
236 } else {
237 ret = krb5_cksumtype_to_enctype(context,
238 ctype,
239 &etype);
240 if (ret != 0) {
241 goto out;
244 ret = hdb_enctype2key(context, krbtgt, NULL, etype, &key);
245 if (ret != 0) {
246 goto out;
249 /* Check the KDC and ticket signatures. */
250 ret = krb5_pac_verify(context,
251 *pac,
253 NULL,
254 NULL,
255 &key->key);
256 if (ret != 0) {
257 DEBUG(1, ("PAC KDC signature failed to verify\n"));
258 goto out;
261 flags |= SAMBA_KDC_FLAG_CONSTRAINED_DELEGATION;
264 if (is_untrusted) {
265 flags |= SAMBA_KDC_FLAG_KRBTGT_IS_UNTRUSTED;
268 if (is_in_db) {
269 flags |= SAMBA_KDC_FLAG_KRBTGT_IN_DB;
272 ret = krb5_pac_init(context, &new_pac);
273 if (ret != 0) {
274 new_pac = NULL;
275 goto out;
278 ret = samba_kdc_update_pac(mem_ctx,
279 context,
280 krbtgt_skdc_entry->kdc_db_ctx->samdb,
281 flags,
282 client_skdc_entry,
283 server->principal,
284 server_skdc_entry,
285 krbtgt_skdc_entry,
286 delegated_proxy_principal,
287 *pac,
288 new_pac);
289 if (ret != 0) {
290 krb5_pac_free(context, new_pac);
291 if (ret == ENODATA) {
292 krb5_pac_free(context, *pac);
293 *pac = NULL;
294 ret = 0;
296 goto out;
299 /* Replace the pac */
300 krb5_pac_free(context, *pac);
301 *pac = new_pac;
303 out:
304 talloc_free(mem_ctx);
305 return ret;
308 /* Resign (and reform, including possibly new groups) a PAC */
310 static krb5_error_code samba_wdc_reget_pac(void *priv, astgs_request_t r,
311 const krb5_principal client_principal,
312 const krb5_principal delegated_proxy_principal,
313 hdb_entry *client,
314 hdb_entry *server,
315 hdb_entry *krbtgt,
316 krb5_pac *pac)
318 krb5_context context = kdc_request_get_context((kdc_request_t)r);
319 krb5_kdc_configuration *config = kdc_request_get_config((kdc_request_t)r);
320 struct samba_kdc_entry *krbtgt_skdc_entry =
321 talloc_get_type_abort(krbtgt->context,
322 struct samba_kdc_entry);
323 krb5_error_code ret;
324 krb5_cksumtype ctype = CKSUMTYPE_NONE;
325 hdb_entry signing_krbtgt_hdb;
327 if (delegated_proxy_principal) {
328 uint16_t rodc_id;
329 unsigned int my_krbtgt_number;
332 * We're using delegated_proxy_principal for the moment to
333 * indicate cases where the ticket was encrypted with the server
334 * key, and not a krbtgt key. This cannot be trusted, so we need
335 * to find a krbtgt key that signs the PAC in order to trust the
336 * ticket.
338 * The krbtgt passed in to this function refers to the krbtgt
339 * used to decrypt the ticket of the server requesting
340 * S4U2Proxy.
342 * When we implement service ticket renewal, we need to check
343 * the PAC, and this will need to be updated.
345 ret = krb5_pac_get_kdc_checksum_info(context,
346 *pac,
347 &ctype,
348 &rodc_id);
349 if (ret != 0) {
350 DEBUG(1, ("Failed to get PAC checksum info\n"));
351 return ret;
355 * We need to check the KDC and ticket signatures, fetching the
356 * correct key based on the enctype.
359 my_krbtgt_number = krbtgt_skdc_entry->kdc_db_ctx->my_krbtgt_number;
361 if (my_krbtgt_number != 0) {
363 * If we are an RODC, and we are not the KDC that signed
364 * the evidence ticket, then we need to proxy the
365 * request.
367 if (rodc_id != my_krbtgt_number) {
368 return HDB_ERR_NOT_FOUND_HERE;
370 } else {
372 * If we are a DC, the ticket may have been signed by a
373 * different KDC than the one that issued the header
374 * ticket.
376 if (rodc_id != krbtgt->kvno >> 16) {
377 struct sdb_entry signing_krbtgt_sdb;
380 * If we didn't sign the ticket, then return an
381 * error.
383 if (rodc_id != 0) {
384 return KRB5KRB_AP_ERR_MODIFIED;
388 * Fetch our key from the database. To support
389 * key rollover, we're going to need to try
390 * multiple keys by trial and error. For now,
391 * krbtgt keys aren't assumed to change.
393 ret = samba_kdc_fetch(context,
394 krbtgt_skdc_entry->kdc_db_ctx,
395 krbtgt->principal,
396 SDB_F_GET_KRBTGT | SDB_F_CANON,
398 &signing_krbtgt_sdb);
399 if (ret != 0) {
400 return ret;
403 ret = sdb_entry_to_hdb_entry(context,
404 &signing_krbtgt_sdb,
405 &signing_krbtgt_hdb);
406 sdb_entry_free(&signing_krbtgt_sdb);
407 if (ret != 0) {
408 return ret;
412 * Replace the krbtgt entry with our own entry
413 * for further processing.
415 krbtgt = &signing_krbtgt_hdb;
420 ret = samba_wdc_reget_pac2(r,
421 delegated_proxy_principal,
422 client,
423 server,
424 krbtgt,
425 pac,
426 ctype);
428 if (krbtgt == &signing_krbtgt_hdb) {
429 hdb_free_entry(context, config->db[0], &signing_krbtgt_hdb);
432 return ret;
435 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
437 char *nb_name = NULL;
438 size_t len;
439 unsigned int i;
441 for (i = 0; addrs && i < addrs->len; i++) {
442 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
443 continue;
445 len = MIN(addrs->val[i].address.length, 15);
446 nb_name = talloc_strndup(mem_ctx,
447 addrs->val[i].address.data, len);
448 if (nb_name) {
449 break;
453 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
454 return NULL;
457 /* Strip space padding */
458 for (len = strlen(nb_name) - 1;
459 (len > 0) && (nb_name[len] == ' ');
460 --len) {
461 nb_name[len] = '\0';
464 return nb_name;
467 /* this function allocates 'data' using malloc.
468 * The caller is responsible for freeing it */
469 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, krb5_data *e_data)
471 e_data->data = malloc(12);
472 if (e_data->data == NULL) {
473 e_data->length = 0;
474 e_data->data = NULL;
475 return;
477 e_data->length = 12;
479 SIVAL(e_data->data, 0, NT_STATUS_V(nt_status));
480 SIVAL(e_data->data, 4, 0);
481 SIVAL(e_data->data, 8, 1);
483 return;
486 static krb5_error_code samba_wdc_check_client_access(void *priv,
487 astgs_request_t r)
489 struct samba_kdc_entry *kdc_entry;
490 bool password_change;
491 char *workstation;
492 NTSTATUS nt_status;
495 kdc_entry = talloc_get_type(kdc_request_get_client(r)->context, struct samba_kdc_entry);
496 password_change = (kdc_request_get_server(r) && kdc_request_get_server(r)->flags.change_pw);
497 workstation = get_netbios_name((TALLOC_CTX *)kdc_request_get_client(r)->context,
498 kdc_request_get_req(r)->req_body.addresses);
500 nt_status = samba_kdc_check_client_access(kdc_entry,
501 kdc_request_get_cname((kdc_request_t)r),
502 workstation,
503 password_change);
505 if (!NT_STATUS_IS_OK(nt_status)) {
506 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
507 return ENOMEM;
510 if (kdc_request_get_rep(r)->padata) {
511 int ret;
512 krb5_data kd;
514 samba_kdc_build_edata_reply(nt_status, &kd);
515 ret = krb5_padata_add(kdc_request_get_context((kdc_request_t)r), kdc_request_get_rep(r)->padata,
516 KRB5_PADATA_PW_SALT,
517 kd.data, kd.length);
518 if (ret != 0) {
520 * So we do not leak the allocated
521 * memory on kd in the error case
523 krb5_data_free(&kd);
527 return samba_kdc_map_policy_err(nt_status);
530 /* Now do the standard Heimdal check */
531 return KRB5_PLUGIN_NO_HANDLE;
534 /* this function allocates 'data' using malloc.
535 * The caller is responsible for freeing it */
536 static krb5_error_code samba_kdc_build_supported_etypes(uint32_t supported_etypes,
537 krb5_data *e_data)
539 e_data->data = malloc(4);
540 if (e_data->data == NULL) {
541 return ENOMEM;
543 e_data->length = 4;
545 PUSH_LE_U32(e_data->data, 0, supported_etypes);
547 return 0;
550 static krb5_error_code samba_wdc_finalize_reply(void *priv,
551 astgs_request_t r)
553 struct samba_kdc_entry *server_kdc_entry;
554 uint32_t supported_enctypes;
556 server_kdc_entry = talloc_get_type(kdc_request_get_server(r)->context, struct samba_kdc_entry);
559 * If the canonicalize flag is set, add PA-SUPPORTED-ENCTYPES padata
560 * type to indicate what encryption types the server supports.
562 supported_enctypes = server_kdc_entry->supported_enctypes;
563 if (kdc_request_get_req(r)->req_body.kdc_options.canonicalize && supported_enctypes != 0) {
564 krb5_error_code ret;
566 PA_DATA md;
568 ret = samba_kdc_build_supported_etypes(supported_enctypes, &md.padata_value);
569 if (ret != 0) {
570 return ret;
573 md.padata_type = KRB5_PADATA_SUPPORTED_ETYPES;
575 ret = kdc_request_add_encrypted_padata(r, &md);
576 if (ret != 0) {
578 * So we do not leak the allocated
579 * memory on kd in the error case
581 krb5_data_free(&md.padata_value);
585 return 0;
588 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
590 *ptr = NULL;
591 return 0;
594 static void samba_wdc_plugin_fini(void *ptr)
596 return;
599 static krb5_error_code samba_wdc_referral_policy(void *priv,
600 astgs_request_t r)
602 return kdc_request_get_error_code((kdc_request_t)r);
605 struct krb5plugin_kdc_ftable kdc_plugin_table = {
606 .minor_version = KRB5_PLUGIN_KDC_VERSION_10,
607 .init = samba_wdc_plugin_init,
608 .fini = samba_wdc_plugin_fini,
609 .pac_verify = samba_wdc_reget_pac,
610 .client_access = samba_wdc_check_client_access,
611 .finalize_reply = samba_wdc_finalize_reply,
612 .pac_generate = samba_wdc_get_pac,
613 .referral_policy = samba_wdc_referral_policy,