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/>.
25 #include "kdc/kdc-glue.h"
26 #include "kdc/pac-glue.h"
29 * Given the right private pointer from hdb_samba4,
30 * get a PAC from the attached ldb messages.
32 * For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
34 static krb5_error_code
samba_wdc_get_pac(void *priv
, krb5_context context
,
35 struct hdb_entry_ex
*client
,
36 const krb5_keyblock
*pk_reply_key
,
40 DATA_BLOB
*logon_blob
= NULL
;
41 DATA_BLOB
*cred_ndr
= NULL
;
42 DATA_BLOB
**cred_ndr_ptr
= NULL
;
43 DATA_BLOB _cred_blob
= data_blob_null
;
44 DATA_BLOB
*cred_blob
= NULL
;
45 DATA_BLOB
*upn_blob
= NULL
;
48 struct samba_kdc_entry
*skdc_entry
=
49 talloc_get_type_abort(client
->ctx
,
50 struct samba_kdc_entry
);
52 mem_ctx
= talloc_named(client
->ctx
, 0, "samba_get_pac context");
57 if (pk_reply_key
!= NULL
) {
58 cred_ndr_ptr
= &cred_ndr
;
61 nt_status
= samba_kdc_get_pac_blobs(mem_ctx
, skdc_entry
,
65 if (!NT_STATUS_IS_OK(nt_status
)) {
70 if (pk_reply_key
!= NULL
&& cred_ndr
!= NULL
) {
71 ret
= samba_kdc_encrypt_pac_credentials(context
,
80 cred_blob
= &_cred_blob
;
83 ret
= samba_make_krb5_pac(context
, logon_blob
, cred_blob
,
90 static krb5_error_code
samba_wdc_get_pac_compat(void *priv
, krb5_context context
,
91 struct hdb_entry_ex
*client
,
94 return samba_wdc_get_pac(priv
, context
, client
, NULL
, pac
);
97 /* Resign (and reform, including possibly new groups) a PAC */
99 static krb5_error_code
samba_wdc_reget_pac(void *priv
, krb5_context context
,
100 const krb5_principal client_principal
,
101 const krb5_principal delegated_proxy_principal
,
102 struct hdb_entry_ex
*client
,
103 struct hdb_entry_ex
*server
,
104 struct hdb_entry_ex
*krbtgt
,
107 struct samba_kdc_entry
*p
=
108 talloc_get_type_abort(server
->ctx
,
109 struct samba_kdc_entry
);
110 struct samba_kdc_entry
*krbtgt_skdc_entry
=
111 talloc_get_type_abort(krbtgt
->ctx
,
112 struct samba_kdc_entry
);
113 TALLOC_CTX
*mem_ctx
= talloc_named(p
, 0, "samba_kdc_reget_pac context");
114 krb5_pac new_pac
= NULL
;
115 DATA_BLOB
*pac_blob
= NULL
;
116 DATA_BLOB
*upn_blob
= NULL
;
117 DATA_BLOB
*deleg_blob
= NULL
;
120 struct PAC_SIGNATURE_DATA
*pac_srv_sig
;
121 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
;
122 bool is_in_db
, is_untrusted
;
123 size_t num_types
= 0;
124 uint32_t *types
= NULL
;
125 uint32_t forced_next_type
= 0;
127 ssize_t logon_info_idx
= -1;
128 ssize_t delegation_idx
= -1;
129 ssize_t logon_name_idx
= -1;
130 ssize_t upn_dns_info_idx
= -1;
131 ssize_t srv_checksum_idx
= -1;
132 ssize_t kdc_checksum_idx
= -1;
138 /* The user account may be set not to want the PAC */
139 if (!samba_princ_needs_pac(p
)) {
140 talloc_free(mem_ctx
);
144 /* If the krbtgt was generated by an RODC, and we are not that
145 * RODC, then we need to regenerate the PAC - we can't trust
147 ret
= samba_krbtgt_is_in_db(krbtgt_skdc_entry
, &is_in_db
, &is_untrusted
);
149 talloc_free(mem_ctx
);
154 struct samba_kdc_entry
*client_skdc_entry
= NULL
;
156 if (client
== NULL
) {
157 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
160 client_skdc_entry
= talloc_get_type_abort(client
->ctx
,
161 struct samba_kdc_entry
);
163 nt_status
= samba_kdc_get_pac_blobs(mem_ctx
, client_skdc_entry
,
164 &pac_blob
, NULL
, &upn_blob
);
165 if (!NT_STATUS_IS_OK(nt_status
)) {
166 talloc_free(mem_ctx
);
170 pac_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
172 talloc_free(mem_ctx
);
176 pac_srv_sig
= talloc_zero(mem_ctx
, struct PAC_SIGNATURE_DATA
);
178 talloc_free(mem_ctx
);
182 pac_kdc_sig
= talloc_zero(mem_ctx
, struct PAC_SIGNATURE_DATA
);
184 talloc_free(mem_ctx
);
188 nt_status
= samba_kdc_update_pac_blob(mem_ctx
, context
,
190 pac_srv_sig
, pac_kdc_sig
);
191 if (!NT_STATUS_IS_OK(nt_status
)) {
192 DEBUG(0, ("Building PAC failed: %s\n",
193 nt_errstr(nt_status
)));
194 talloc_free(mem_ctx
);
199 /* Now check the KDC signature, fetching the correct key based on the enc type */
200 ret
= kdc_check_pac(context
, pac_srv_sig
->signature
, pac_kdc_sig
, krbtgt
);
202 DEBUG(1, ("PAC KDC signature failed to verify\n"));
203 talloc_free(mem_ctx
);
209 if (delegated_proxy_principal
) {
210 deleg_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
212 talloc_free(mem_ctx
);
216 nt_status
= samba_kdc_update_delegation_info_blob(mem_ctx
,
218 server
->entry
.principal
,
219 delegated_proxy_principal
,
221 if (!NT_STATUS_IS_OK(nt_status
)) {
222 DEBUG(0, ("Building PAC failed: %s\n",
223 nt_errstr(nt_status
)));
224 talloc_free(mem_ctx
);
229 /* Check the types of the given PAC */
230 ret
= krb5_pac_get_types(context
, *pac
, &num_types
, &types
);
232 talloc_free(mem_ctx
);
236 for (i
= 0; i
< num_types
; i
++) {
238 case PAC_TYPE_LOGON_INFO
:
239 if (logon_info_idx
!= -1) {
240 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
245 talloc_free(mem_ctx
);
250 case PAC_TYPE_CONSTRAINED_DELEGATION
:
251 if (delegation_idx
!= -1) {
252 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
257 talloc_free(mem_ctx
);
262 case PAC_TYPE_LOGON_NAME
:
263 if (logon_name_idx
!= -1) {
264 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
269 talloc_free(mem_ctx
);
274 case PAC_TYPE_UPN_DNS_INFO
:
275 if (upn_dns_info_idx
!= -1) {
276 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
281 talloc_free(mem_ctx
);
284 upn_dns_info_idx
= i
;
286 case PAC_TYPE_SRV_CHECKSUM
:
287 if (srv_checksum_idx
!= -1) {
288 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
293 talloc_free(mem_ctx
);
296 srv_checksum_idx
= i
;
298 case PAC_TYPE_KDC_CHECKSUM
:
299 if (kdc_checksum_idx
!= -1) {
300 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
305 talloc_free(mem_ctx
);
308 kdc_checksum_idx
= i
;
315 if (logon_info_idx
== -1) {
316 DEBUG(1, ("PAC_TYPE_LOGON_INFO missing\n"));
318 talloc_free(mem_ctx
);
321 if (logon_name_idx
== -1) {
322 DEBUG(1, ("PAC_TYPE_LOGON_NAME missing\n"));
324 talloc_free(mem_ctx
);
327 if (srv_checksum_idx
== -1) {
328 DEBUG(1, ("PAC_TYPE_SRV_CHECKSUM missing\n"));
330 talloc_free(mem_ctx
);
333 if (kdc_checksum_idx
== -1) {
334 DEBUG(1, ("PAC_TYPE_KDC_CHECKSUM missing\n"));
336 talloc_free(mem_ctx
);
340 /* Build an updated PAC */
341 ret
= krb5_pac_init(context
, &new_pac
);
344 talloc_free(mem_ctx
);
349 const uint8_t zero_byte
= 0;
351 DATA_BLOB type_blob
= data_blob_null
;
354 if (forced_next_type
!= 0) {
356 * We need to inject possible missing types
358 type
= forced_next_type
;
359 forced_next_type
= 0;
360 } else if (i
< num_types
) {
368 case PAC_TYPE_LOGON_INFO
:
369 type_blob
= *pac_blob
;
371 if (delegation_idx
== -1 && deleg_blob
!= NULL
) {
372 /* inject CONSTRAINED_DELEGATION behind */
373 forced_next_type
= PAC_TYPE_CONSTRAINED_DELEGATION
;
376 case PAC_TYPE_CONSTRAINED_DELEGATION
:
377 if (deleg_blob
!= NULL
) {
378 type_blob
= *deleg_blob
;
381 case PAC_TYPE_CREDENTIAL_INFO
:
383 * Note that we copy the credential blob,
384 * as it's only usable with the PKINIT based
385 * AS-REP reply key, it's only available on the
386 * host which did the AS-REQ/AS-REP exchange.
388 * This matches Windows 2008R2...
391 case PAC_TYPE_LOGON_NAME
:
393 * this is generated in the main KDC code
394 * we just add a place holder here.
396 type_blob
= data_blob_const(&zero_byte
, 1);
398 if (upn_dns_info_idx
== -1 && upn_blob
!= NULL
) {
399 /* inject UPN_DNS_INFO behind */
400 forced_next_type
= PAC_TYPE_UPN_DNS_INFO
;
403 case PAC_TYPE_UPN_DNS_INFO
:
405 * Replace in the RODC case, otherwise
406 * upn_blob is NULL and we just copy.
408 if (upn_blob
!= NULL
) {
409 type_blob
= *upn_blob
;
412 case PAC_TYPE_SRV_CHECKSUM
:
414 * this are generated in the main KDC code
415 * we just add a place holder here.
417 type_blob
= data_blob_const(&zero_byte
, 1);
419 case PAC_TYPE_KDC_CHECKSUM
:
421 * this are generated in the main KDC code
422 * we just add a place holders here.
424 type_blob
= data_blob_const(&zero_byte
, 1);
431 if (type_blob
.length
!= 0) {
432 ret
= smb_krb5_copy_data_contents(&type_data
,
437 krb5_pac_free(context
, new_pac
);
438 talloc_free(mem_ctx
);
442 ret
= krb5_pac_get_buffer(context
, *pac
,
446 krb5_pac_free(context
, new_pac
);
447 talloc_free(mem_ctx
);
452 ret
= krb5_pac_add_buffer(context
, new_pac
,
454 smb_krb5_free_data_contents(context
, &type_data
);
457 krb5_pac_free(context
, new_pac
);
458 talloc_free(mem_ctx
);
465 /* We now replace the pac */
466 krb5_pac_free(context
, *pac
);
469 talloc_free(mem_ctx
);
473 static char *get_netbios_name(TALLOC_CTX
*mem_ctx
, HostAddresses
*addrs
)
475 char *nb_name
= NULL
;
479 for (i
= 0; addrs
&& i
< addrs
->len
; i
++) {
480 if (addrs
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
483 len
= MIN(addrs
->val
[i
].address
.length
, 15);
484 nb_name
= talloc_strndup(mem_ctx
,
485 addrs
->val
[i
].address
.data
, len
);
491 if ((nb_name
== NULL
) || (nb_name
[0] == '\0')) {
495 /* Strip space padding */
496 for (len
= strlen(nb_name
) - 1;
497 (len
> 0) && (nb_name
[len
] == ' ');
505 static krb5_data
fill_krb5_data(void *data
, size_t length
)
510 kdata
.length
= length
;
515 /* this function allocates 'data' using malloc.
516 * The caller is responsible for freeing it */
517 static void samba_kdc_build_edata_reply(NTSTATUS nt_status
, DATA_BLOB
*e_data
)
519 krb5_error_code ret
= 0;
530 pa
.padata_type
= KRB5_PADATA_PW_SALT
;
531 pa
.padata_value
.length
= 12;
532 pa
.padata_value
.data
= malloc(pa
.padata_value
.length
);
533 if (!pa
.padata_value
.data
) {
539 SIVAL(pa
.padata_value
.data
, 0, NT_STATUS_V(nt_status
));
540 SIVAL(pa
.padata_value
.data
, 4, 0);
541 SIVAL(pa
.padata_value
.data
, 8, 1);
543 ASN1_MALLOC_ENCODE(PA_DATA
, buf
, len
, &pa
, &len
, ret
);
544 free(pa
.padata_value
.data
);
550 e_data
->length
= len
;
556 static krb5_error_code
samba_wdc_check_client_access(void *priv
,
557 krb5_context context
,
558 krb5_kdc_configuration
*config
,
559 hdb_entry_ex
*client_ex
, const char *client_name
,
560 hdb_entry_ex
*server_ex
, const char *server_name
,
564 struct samba_kdc_entry
*kdc_entry
;
565 bool password_change
;
569 kdc_entry
= talloc_get_type(client_ex
->ctx
, struct samba_kdc_entry
);
570 password_change
= (server_ex
&& server_ex
->entry
.flags
.change_pw
);
571 workstation
= get_netbios_name((TALLOC_CTX
*)client_ex
->ctx
,
572 req
->req_body
.addresses
);
574 nt_status
= samba_kdc_check_client_access(kdc_entry
,
579 if (!NT_STATUS_IS_OK(nt_status
)) {
580 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
587 samba_kdc_build_edata_reply(nt_status
, &data
);
588 *e_data
= fill_krb5_data(data
.data
, data
.length
);
591 return samba_kdc_map_policy_err(nt_status
);
594 /* Now do the standard Heimdal check */
595 return kdc_check_flags(context
, config
,
596 client_ex
, client_name
,
597 server_ex
, server_name
,
598 req
->msg_type
== krb_as_req
);
601 static krb5_error_code
samba_wdc_plugin_init(krb5_context context
, void **ptr
)
607 static void samba_wdc_plugin_fini(void *ptr
)
612 struct krb5plugin_windc_ftable windc_plugin_table
= {
613 .minor_version
= KRB5_WINDC_PLUGIN_MINOR
,
614 .init
= samba_wdc_plugin_init
,
615 .fini
= samba_wdc_plugin_fini
,
616 .pac_generate
= samba_wdc_get_pac_compat
,
617 .pac_verify
= samba_wdc_reget_pac
,
618 .client_access
= samba_wdc_check_client_access
,
619 .pac_pk_generate
= samba_wdc_get_pac
,