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
,
189 krbtgt_skdc_entry
->kdc_db_ctx
->samdb
,
191 pac_srv_sig
, pac_kdc_sig
);
192 if (!NT_STATUS_IS_OK(nt_status
)) {
193 DEBUG(0, ("Building PAC failed: %s\n",
194 nt_errstr(nt_status
)));
195 talloc_free(mem_ctx
);
200 /* Now check the KDC signature, fetching the correct key based on the enc type */
201 ret
= kdc_check_pac(context
, pac_srv_sig
->signature
, pac_kdc_sig
, krbtgt
);
203 DEBUG(1, ("PAC KDC signature failed to verify\n"));
204 talloc_free(mem_ctx
);
210 if (delegated_proxy_principal
) {
211 deleg_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
213 talloc_free(mem_ctx
);
217 nt_status
= samba_kdc_update_delegation_info_blob(mem_ctx
,
219 server
->entry
.principal
,
220 delegated_proxy_principal
,
222 if (!NT_STATUS_IS_OK(nt_status
)) {
223 DEBUG(0, ("Building PAC failed: %s\n",
224 nt_errstr(nt_status
)));
225 talloc_free(mem_ctx
);
230 /* Check the types of the given PAC */
231 ret
= krb5_pac_get_types(context
, *pac
, &num_types
, &types
);
233 talloc_free(mem_ctx
);
237 for (i
= 0; i
< num_types
; i
++) {
239 case PAC_TYPE_LOGON_INFO
:
240 if (logon_info_idx
!= -1) {
241 DEBUG(1, ("logon info type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
246 talloc_free(mem_ctx
);
251 case PAC_TYPE_CONSTRAINED_DELEGATION
:
252 if (delegation_idx
!= -1) {
253 DEBUG(1, ("constrained delegation type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
258 talloc_free(mem_ctx
);
263 case PAC_TYPE_LOGON_NAME
:
264 if (logon_name_idx
!= -1) {
265 DEBUG(1, ("logon name type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
270 talloc_free(mem_ctx
);
275 case PAC_TYPE_UPN_DNS_INFO
:
276 if (upn_dns_info_idx
!= -1) {
277 DEBUG(1, ("upn dns info type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
282 talloc_free(mem_ctx
);
285 upn_dns_info_idx
= i
;
287 case PAC_TYPE_SRV_CHECKSUM
:
288 if (srv_checksum_idx
!= -1) {
289 DEBUG(1, ("server checksum type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
294 talloc_free(mem_ctx
);
297 srv_checksum_idx
= i
;
299 case PAC_TYPE_KDC_CHECKSUM
:
300 if (kdc_checksum_idx
!= -1) {
301 DEBUG(1, ("kdc checksum type[%"PRIu32
"] twice [%zd] and [%zu]: \n",
306 talloc_free(mem_ctx
);
309 kdc_checksum_idx
= i
;
316 if (logon_info_idx
== -1) {
317 DEBUG(1, ("PAC_TYPE_LOGON_INFO missing\n"));
319 talloc_free(mem_ctx
);
322 if (logon_name_idx
== -1) {
323 DEBUG(1, ("PAC_TYPE_LOGON_NAME missing\n"));
325 talloc_free(mem_ctx
);
328 if (srv_checksum_idx
== -1) {
329 DEBUG(1, ("PAC_TYPE_SRV_CHECKSUM missing\n"));
331 talloc_free(mem_ctx
);
334 if (kdc_checksum_idx
== -1) {
335 DEBUG(1, ("PAC_TYPE_KDC_CHECKSUM missing\n"));
337 talloc_free(mem_ctx
);
341 /* Build an updated PAC */
342 ret
= krb5_pac_init(context
, &new_pac
);
345 talloc_free(mem_ctx
);
350 const uint8_t zero_byte
= 0;
352 DATA_BLOB type_blob
= data_blob_null
;
355 if (forced_next_type
!= 0) {
357 * We need to inject possible missing types
359 type
= forced_next_type
;
360 forced_next_type
= 0;
361 } else if (i
< num_types
) {
369 case PAC_TYPE_LOGON_INFO
:
370 type_blob
= *pac_blob
;
372 if (delegation_idx
== -1 && deleg_blob
!= NULL
) {
373 /* inject CONSTRAINED_DELEGATION behind */
374 forced_next_type
= PAC_TYPE_CONSTRAINED_DELEGATION
;
377 case PAC_TYPE_CONSTRAINED_DELEGATION
:
378 if (deleg_blob
!= NULL
) {
379 type_blob
= *deleg_blob
;
382 case PAC_TYPE_CREDENTIAL_INFO
:
384 * Note that we copy the credential blob,
385 * as it's only usable with the PKINIT based
386 * AS-REP reply key, it's only available on the
387 * host which did the AS-REQ/AS-REP exchange.
389 * This matches Windows 2008R2...
392 case PAC_TYPE_LOGON_NAME
:
394 * this is generated in the main KDC code
395 * we just add a place holder here.
397 type_blob
= data_blob_const(&zero_byte
, 1);
399 if (upn_dns_info_idx
== -1 && upn_blob
!= NULL
) {
400 /* inject UPN_DNS_INFO behind */
401 forced_next_type
= PAC_TYPE_UPN_DNS_INFO
;
404 case PAC_TYPE_UPN_DNS_INFO
:
406 * Replace in the RODC case, otherwise
407 * upn_blob is NULL and we just copy.
409 if (upn_blob
!= NULL
) {
410 type_blob
= *upn_blob
;
413 case PAC_TYPE_SRV_CHECKSUM
:
415 * this are generated in the main KDC code
416 * we just add a place holder here.
418 type_blob
= data_blob_const(&zero_byte
, 1);
420 case PAC_TYPE_KDC_CHECKSUM
:
422 * this are generated in the main KDC code
423 * we just add a place holders here.
425 type_blob
= data_blob_const(&zero_byte
, 1);
432 if (type_blob
.length
!= 0) {
433 ret
= smb_krb5_copy_data_contents(&type_data
,
438 krb5_pac_free(context
, new_pac
);
439 talloc_free(mem_ctx
);
443 ret
= krb5_pac_get_buffer(context
, *pac
,
447 krb5_pac_free(context
, new_pac
);
448 talloc_free(mem_ctx
);
453 ret
= krb5_pac_add_buffer(context
, new_pac
,
455 smb_krb5_free_data_contents(context
, &type_data
);
458 krb5_pac_free(context
, new_pac
);
459 talloc_free(mem_ctx
);
466 /* We now replace the pac */
467 krb5_pac_free(context
, *pac
);
470 talloc_free(mem_ctx
);
474 static char *get_netbios_name(TALLOC_CTX
*mem_ctx
, HostAddresses
*addrs
)
476 char *nb_name
= NULL
;
480 for (i
= 0; addrs
&& i
< addrs
->len
; i
++) {
481 if (addrs
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
484 len
= MIN(addrs
->val
[i
].address
.length
, 15);
485 nb_name
= talloc_strndup(mem_ctx
,
486 addrs
->val
[i
].address
.data
, len
);
492 if ((nb_name
== NULL
) || (nb_name
[0] == '\0')) {
496 /* Strip space padding */
497 for (len
= strlen(nb_name
) - 1;
498 (len
> 0) && (nb_name
[len
] == ' ');
506 static krb5_data
fill_krb5_data(void *data
, size_t length
)
511 kdata
.length
= length
;
516 /* this function allocates 'data' using malloc.
517 * The caller is responsible for freeing it */
518 static void samba_kdc_build_edata_reply(NTSTATUS nt_status
, DATA_BLOB
*e_data
)
520 krb5_error_code ret
= 0;
531 pa
.padata_type
= KRB5_PADATA_PW_SALT
;
532 pa
.padata_value
.length
= 12;
533 pa
.padata_value
.data
= malloc(pa
.padata_value
.length
);
534 if (!pa
.padata_value
.data
) {
540 SIVAL(pa
.padata_value
.data
, 0, NT_STATUS_V(nt_status
));
541 SIVAL(pa
.padata_value
.data
, 4, 0);
542 SIVAL(pa
.padata_value
.data
, 8, 1);
544 ASN1_MALLOC_ENCODE(PA_DATA
, buf
, len
, &pa
, &len
, ret
);
545 free(pa
.padata_value
.data
);
551 e_data
->length
= len
;
557 static krb5_error_code
samba_wdc_check_client_access(void *priv
,
558 krb5_context context
,
559 krb5_kdc_configuration
*config
,
560 hdb_entry_ex
*client_ex
, const char *client_name
,
561 hdb_entry_ex
*server_ex
, const char *server_name
,
565 struct samba_kdc_entry
*kdc_entry
;
566 bool password_change
;
570 kdc_entry
= talloc_get_type(client_ex
->ctx
, struct samba_kdc_entry
);
571 password_change
= (server_ex
&& server_ex
->entry
.flags
.change_pw
);
572 workstation
= get_netbios_name((TALLOC_CTX
*)client_ex
->ctx
,
573 req
->req_body
.addresses
);
575 nt_status
= samba_kdc_check_client_access(kdc_entry
,
580 if (!NT_STATUS_IS_OK(nt_status
)) {
581 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
588 samba_kdc_build_edata_reply(nt_status
, &data
);
589 *e_data
= fill_krb5_data(data
.data
, data
.length
);
592 return samba_kdc_map_policy_err(nt_status
);
595 /* Now do the standard Heimdal check */
596 return kdc_check_flags(context
, config
,
597 client_ex
, client_name
,
598 server_ex
, server_name
,
599 req
->msg_type
== krb_as_req
);
602 static krb5_error_code
samba_wdc_plugin_init(krb5_context context
, void **ptr
)
608 static void samba_wdc_plugin_fini(void *ptr
)
613 struct krb5plugin_windc_ftable windc_plugin_table
= {
614 .minor_version
= KRB5_WINDC_PLUGIN_MINOR
,
615 .init
= samba_wdc_plugin_init
,
616 .fini
= samba_wdc_plugin_fini
,
617 .pac_generate
= samba_wdc_get_pac_compat
,
618 .pac_verify
= samba_wdc_reget_pac
,
619 .client_access
= samba_wdc_check_client_access
,
620 .pac_pk_generate
= samba_wdc_get_pac
,